Skip to content

lkl: mmu: fix shm leak and add shmem_cleanup host operation - #649

Open
clingfei wants to merge 1 commit into
lkl:masterfrom
clingfei:fix
Open

clingfei wants to merge 1 commit into
lkl:masterfrom
clingfei:fix

Conversation

@clingfei

Copy link
Copy Markdown

In MMU mode, shmem_init() allocates POSIX shared memory via shm_open() to back LKL physical memory. Two issues currently exist:

  1. Because POSIX shared memory has kernel persistence, /dev/shm/lkl_phys_mem_ remains alive after the process exits or crashes, leading to SHM exhaustion under automated testing or fuzzing. Fix this by unlinking the shm object immediately after shm_open().

  2. shared_mem_fd is currently never closed. If an application repeatedly boots and halts LKL within the same host process, each subsequent shmem_init() overwrites shared_mem_fd without closing the previous one, leaking both host file descriptors and physical memory across LKL restarts. Furthermore, missing FD_CLOEXEC causes child processes spawned via fork/exec to inherit the memory backing descriptor.

Fix these by:

  • Calling shm_unlink() right after shm_open() and setting FD_CLOEXEC.
  • Defensively closing any existing shared_mem_fd in shmem_init().
  • Adding a shmem_cleanup() host operation in lkl_host_operations, called from free_mem() during lkl_sys_halt(), to properly close shared_mem_fd when LKL shuts down.

Closes: #629

In MMU mode, shmem_init() allocates POSIX shared memory via shm_open()
to back LKL physical memory. Two issues currently exist:

1. Because POSIX shared memory has kernel persistence,
   /dev/shm/lkl_phys_mem_<pid> remains alive after the process exits or
   crashes, leading to SHM exhaustion under automated testing or fuzzing.
   Fix this by unlinking the shm object immediately after shm_open().

2. shared_mem_fd is currently never closed. If an application repeatedly
   boots and halts LKL within the same host process, each subsequent
   shmem_init() overwrites shared_mem_fd without closing the previous
   one, leaking both host file descriptors and physical memory across LKL
   restarts. Furthermore, missing FD_CLOEXEC causes child processes spawned
   via fork/exec to inherit the memory backing descriptor.

Fix these by:
- Calling shm_unlink() right after shm_open() and setting FD_CLOEXEC.
- Defensively closing any existing shared_mem_fd in shmem_init().
- Adding a shmem_cleanup() host operation in lkl_host_operations, called
  from free_mem() during lkl_sys_halt(), to properly close shared_mem_fd
  when LKL shuts down.

Closes: lkl#629

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
@github-actions

Copy link
Copy Markdown

Test Results

106 files  ±0  106 suites  ±0   11m 6s ⏱️ +36s
206 tests ±0  195 ✅ ±0  11 💤 ±0  0 ❌ ±0 
790 runs  ±0  734 ✅ ±0  56 💤 ±0  0 ❌ ±0 

Results for commit 5fafdb6. ± Comparison against base commit d0f76a7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MMU lkl_phys_mem shm leak

1 participant