Bug 289917 - PID leak in proc_id_reapmap
Summary: PID leak in proc_id_reapmap
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Many People
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-09-30 14:01 UTC by Andriy Gapon
Modified: 2025-10-13 00:31 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andriy Gapon freebsd_committer freebsd_triage 2025-09-30 14:01:48 UTC
I have an easy way to reproduce a PID leak in proc_id_reapmap using timeout(1) command.
It does not require any elevated privileges, that is, it can be done as a regular user.

The leak is readily reproducible using these versions of FreeBSD: 14.3, stable/13, 13.5.

The leak is a bit harder to reproduce on main, stable/15, upcoming 15.0, stable/14.
What I mean by a bit harder is that the leak is not reproducible in a default installation, but if I take a copy of timeout(1) command from an affected version (e.g., 14.3), then I can reproduce the leak using the command.

I conclude that the kernel allows for a resource leak via procctl(2).

In the affected versions, timeout(1) has some bug(s) which trigger the resource leak.
In newer versions, timeout(1) is much better behaved but procctl(2) can still be "exploited".

By filling proc_id_reapmap it's possible to create a situation where no new PID could be allocated.
An attempt to do so would get stuck in an infinite loop in fork_findpid.
Subsequent attempts would be blocked on procid lock.

To reproduce the issue I use a small script that kills itself like this:
    #!/bin/sh
    sleep 0.0001 ; kill $$

Then I run the script under timeout(1) like this:
    timeout 0.01 ./timeout-test.sh
Or in a loop for a more pronounced effect:
    while true ; do timeout 0.01 ./timeout-test.sh ; done


proc_id_reapmap can be checked using a custom gdb function like:
define bitset_count
    set $bitset = $arg0
    set $bitcount = $arg1
    set $c = 0
    set $i = 0
    while ($i < $bitcount)
        set $n = $i / 64
        set $b = $i % 64
        if (($bitset[$n] & (1ul << $b)) != 0)
            set $c = $c + 1
        end
        set $i = $i + 1
    end
    print $c
end

Example from a test on 14.3 (running the command just moments apart):
(kgdb) bitset_count proc_id_reapmap 100000
$1 = 23
(kgdb) bitset_count proc_id_reapmap 100000
$2 = 1485

According to my brief research the userland misbehavior of timeoout(1) was fixed by the commit series to bin/timeout done on April 16 (2025) by bapt, with most of those changes authored by Aaron LI.
Comment 1 Andriy Gapon freebsd_committer freebsd_triage 2025-09-30 14:03:36 UTC
Just in case, I am reporting this with my Panzura contractor hat on.
Comment 2 Konstantin Belousov freebsd_committer freebsd_triage 2025-09-30 14:27:23 UTC
Peter,
could you please try this?  Most likely, using the old version of timeout(1)
from the beginning, to get faster reproduction times.
Comment 3 Peter Holm freebsd_committer freebsd_triage 2025-09-30 17:40:27 UTC
(In reply to Konstantin Belousov from comment #2)
I was unable to trigger any leaks with the current version of timeout(1), but using an old version of timeout(1) (May 30th) from when we looked at the timeout(1) problem, I see:

(kgdb) bitset_count proc_id_reapmap 100000
$2 = 18
(kgdb) bitset_count proc_id_reapmap 100000
$3 = 74896
(kgdb) x/s version
0xffffffff8164f500 <version>:   "FreeBSD 16.0-CURRENT #0 main-n280736-05e5de00b9ea-dirty: Mon Sep 29 20:25:28 CEST 2025\n    pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO\n"
(kgdb)
Comment 4 Mark Johnston freebsd_committer freebsd_triage 2025-10-03 16:12:52 UTC
https://reviews.freebsd.org/D52814
Comment 5 commit-hook freebsd_committer freebsd_triage 2025-10-06 16:49:10 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f19ef352ec63f9fcc10076a263b8ce402ba31379

commit f19ef352ec63f9fcc10076a263b8ce402ba31379
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-10-01 03:17:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-10-06 16:48:26 +0000

    kern_exit.c: do not leak reaper bitmap busy bits

    When re-assigning p_reapsubtree value, we must check if the result of
    obliterating the old value would free the corresponding pid in the
    proc_id_reap bitmap, and indeed free it when needed.

    PR:     289917
    Reported by:    avg
    Reviewed by:    avg, markj
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D52814

 sys/kern/kern_exit.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2025-10-11 05:08:59 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6eb4708a84d773742a7d36e530d06164b3733ba7

commit 6eb4708a84d773742a7d36e530d06164b3733ba7
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-10-01 03:17:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-10-11 05:08:13 +0000

    kern_exit.c: do not leak reaper bitmap busy bits

    PR:     289917

    (cherry picked from commit f19ef352ec63f9fcc10076a263b8ce402ba31379)

 sys/kern/kern_exit.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-10-13 00:31:07 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f0f57fde342d79aec17ef56406aa08bb1eaa1f79

commit f0f57fde342d79aec17ef56406aa08bb1eaa1f79
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-10-01 03:17:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-10-13 00:23:42 +0000

    kern_exit.c: do not leak reaper bitmap busy bits

    PR:     289917

    (cherry picked from commit f19ef352ec63f9fcc10076a263b8ce402ba31379)

 sys/kern/kern_exit.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)