Skip to content

prowld: close PID reuse race when signaling an adopted pdfork(2) daemon - #709

Merged
laffer1 merged 2 commits into
masterfrom
prowld-procdesc-adopt
Nov 30, 2026
Merged

laffer1 merged 2 commits into
masterfrom
prowld-procdesc-adopt

Conversation

@laffer1

@laffer1 laffer1 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #677.

Problem

supervisor_signal() checks an adopted daemon's parent and start time, then calls kill(2). That is safe only while the daemon stays a zombie until prowld reaps it. A pdfork(2) daemon breaks this. While its descriptor is attached, wildcard wait() skips it and prowld gets no SIGCHLD. Whichever process closes the last descriptor reaps the zombie itself (procdesc_close() calls proc_reap()), so the PID can be reused between the check and the signal.

Fix

Kernel (756b4c29bd)

  • fill_kinfo_proc sets a new flag, KI_PROCDESC (0x8 in ki_kiflag), while p_procdesc is attached. It reads the field under proctree_lock, which serializes it against procdesc_close() and procdesc_reap().
  • A descriptor is attached only at pdfork(2) time and never comes back once detached. So a process seen without one stays safe to signal for its whole life.
  • A new kinfo_procdesc kernel feature lets userland tell an old kernel apart from a process that has no descriptor.
  • New test: tests/sys/kern/sysctl_kern_proc:sysctl_kern_proc_procdesc.

prowld (65d0bb7545)

  • A new adoptable() helper, used by find_daemon() and child_start_time(), rejects KI_PROCDESC candidates. That covers selection, handoff and every check before a signal.
  • A launcher that drops its descriptor on exit is still adopted on the next poll. A descriptor held past the 10-second adoption window cancels the launch, and the private reaper cleans up.
  • Kernels without the feature keep the old behavior, and prowld logs a notice once at startup. This is the agreed handling for split kernel/userland upgrades.
  • New tests in sbin/prowlctl/tests/prowlctl_test.sh:
    • forking_pdfork_held: never adopted, launch cancelled, daemon killed.
    • forking_pdfork_closed: adopted once the descriptor closes, then stopped.
    • forking_uid: double fork plus chroot -u nobody, adopted and stopped.
  • New helper pdfork_helper for those tests.
  • prowl-unit.5 and prowld-spec.md document the rule.

Testing

  • make buildkernel KERNCONF=GENERIC NO_MODULES=yes succeeds.
  • prowld, pdfork_helper and sysctl_kern_proc.c compile with -Wall -Wextra -Werror against the new user.h.
  • checkstyle9.pl passes on the C diff.
  • bhyve guest run (UEFI, GENERIC with these changes, prowld from this branch as PID 1), two boots:
    • New kernel: kern.features.kinfo_procdesc=1. Boot-time pidfile adoption of inetd and a forking test daemon passes, and prowlctl restart re-adopts inetd. sysctl_kern_proc passes 3/3, including sysctl_kern_proc_procdesc. sbin/prowlctl passes 40, skips 1 (trust_test:nfsv4_acl, which skips on /tmp without NFSv4 ACLs). The passes include forking_pdfork_held, forking_pdfork_closed, forking_uid, forking, forking_launch_identity and forking_pidfile.
    • Old kernel, new prowld: the feature is absent and prowld logs "kernel does not report process descriptors; pdfork(2) daemons are adopted unchecked". Boot adoption and restart still pass. sbin/prowlctl passes 38 and skips 3, the two pdfork tests among them. sysctl_kern_proc_procdesc fails on its feature_present() check, which is expected for a kernel test run on a kernel without the change.
  • C changes still need a manual audit per AI_POLICY.md.

AI-Assisted-by: Claude Opus 5.5

🤖 Generated with Claude Code

laffer1 and others added 2 commits November 30, 2026 02:03
Closing the last descriptor of a pdfork(2) process that has exited
reaps it without its parent, so a supervisor that checks a child and
then signals it cannot rely on the PID staying reserved.  Set
KI_PROCDESC in ki_kiflag while a descriptor is attached.  A descriptor
is attached only at pdfork(2) time and never returns once detached, so
a process reported without one stays reaped only by its parent.

Advertise the flag as the kinfo_procdesc kernel feature so userland can
tell an old kernel from a process without a descriptor.

PR:		677
AI-Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lucas Holt <luke@foolishgames.com>
The identity check in supervisor_signal() relies on an adopted daemon
staying a zombie until prowld reaps it.  A pdfork(2) daemon whose
descriptor another process holds can be reaped by that close instead,
and its PID reused before kill(2) runs.

Treat a candidate with KI_PROCDESC as not adoptable.  The launch keeps
polling, so a launcher that drops its descriptor on exit is adopted as
before; one held past the adoption window cancels the launch.  Kernels
without kinfo_procdesc keep the previous behavior, and prowld logs a
notice once at startup.

Add regression tests for a held and a closed descriptor, and for a
double-forked daemon that changes user ID.

PR:		677
AI-Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lucas Holt <luke@foolishgames.com>
@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Sorry @laffer1, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 17 hours and 7 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@laffer1
laffer1 merged commit 282a6d4 into master Nov 30, 2026
10 of 14 checks passed
@laffer1
laffer1 deleted the prowld-procdesc-adopt branch November 30, 2026 15:35
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.

prowld: close PID reuse race when signaling an adopted pdfork(2) daemon

1 participant