]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commit
fix: fork FD race condition + orphaned zombie memory leak
authorTulio A M Mendes <[email protected]>
Mon, 16 Feb 2026 22:32:09 +0000 (19:32 -0300)
committerTulio A M Mendes <[email protected]>
Mon, 16 Feb 2026 22:32:09 +0000 (19:32 -0300)
commitf4905d0f2ce29d56ee71aabad15c552682539fca
tree65d0d27b52c94eae588dde1b1b52dbaaf50bf27b
parent161b41b2d60ad383169ff03d7b094f3e13dea2db
fix: fork FD race condition + orphaned zombie memory leak

Bug 1 — Fork FD race (HIGH severity):
  process_fork_create() enqueued the child to the runqueue under
  sched_lock, but syscall_fork_impl() copied file descriptors AFTER
  the function returned — with sched_lock released. On SMP, the child
  could be scheduled on another CPU and reach userspace before FDs
  were populated, seeing NULL file descriptors.

  Fix: move FD copying (with refcount bumps) into process_fork_create()
  itself, under sched_lock, before the child is enqueued. Added proper
  rollback of refcount bumps if kstack_alloc fails.

Bug 2 — Orphaned zombie leak (MEDIUM severity):
  When a process exited, its children were not reparented to PID 1
  (init). Zombie children of exited parents could never be reaped via
  waitpid, leaking process structs and kernel stacks forever.

  Fix: in process_exit_notify(), iterate the process list and reparent
  all children to PID 1. If any reparented child is already a zombie
  and init is blocked in waitpid(-1), wake init immediately.

Also verified (no bugs found):
- EOI handling correct (sent before handlers, spurious skips EOI)
- Lock ordering safe (all locks use irqsave, no cross-CPU ABBA)
- Heap has double-free and corruption detection
- User stack has guard pages

83/83 smoke tests pass, cppcheck clean.
src/kernel/scheduler.c
src/kernel/syscall.c