Bug 295052 - The jail(8) command leaks potentially sensitive file descriptors to exec.* hooks.
Summary: The jail(8) command leaks potentially sensitive file descriptors to exec.* ho...
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: conf (show other bugs)
Version: 15.0-RELEASE
Hardware: Any Any
: --- Affects Many People
Assignee: Kyle Evans
URL:
Keywords: security
Depends on:
Blocks:
 
Reported: 2026-05-06 12:06 UTC by Jan Bramkamp
Modified: 2026-06-10 04:02 UTC (History)
8 users (show)

See Also:
kevans: mfc-stable15?
kevans: mfc-stable14?


Attachments
[PATCH] Fix PR#295052 by closing jail configuration files after parsing. (1.31 KB, patch)
2026-05-06 12:14 UTC, Jan Bramkamp
no flags Details | Diff
[PATCH 1/3] Fix PR#295052 by closing jail configuration files after parsing. (1.31 KB, patch)
2026-05-06 14:06 UTC, Jan Bramkamp
no flags Details | Diff
[PATCH 2/3] Improve fix for PR#295052 by adding "e" to mode. (1.05 KB, patch)
2026-05-06 14:07 UTC, Jan Bramkamp
no flags Details | Diff
[PATCH 3/3] Open the fstab files with fopen("re"). (860 bytes, patch)
2026-05-06 14:10 UTC, Jan Bramkamp
no flags Details | Diff
Just commit one and two squashed together (1.62 KB, patch)
2026-05-06 14:29 UTC, Jan Bramkamp
no flags Details | Diff
[PATCH] jail: add regression test for config file fd leaks (3.43 KB, patch)
2026-05-07 13:44 UTC, Jan Bramkamp
no flags Details | Diff
[PATCH] jail: replace stdin with /dev/null after parsing (917 bytes, patch)
2026-05-14 17:53 UTC, crest
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Bramkamp 2026-05-06 12:06:28 UTC
The jail(8) command does not close configuration files after parsing them. These configuration files can contain secrets for multiple jails e.g. API tokens.

The file descriptors behind the FILE handles are left open after parse_config() is done parsing the configuration. These file descriptors are later inherited by all child processes jail(8) forks e.g. the exec.* hooks. Some of these hooks run inside individual jails (exec.start, exec.stop) and should **NOT** be considered trusted by the host or other jails. As such this is an information leak across trust boundaries.
Comment 1 Jan Bramkamp 2026-05-06 12:14:42 UTC
Created attachment 270459 [details]
[PATCH] Fix PR#295052 by closing jail configuration files after  parsing.

Attach patch to fix the problem by closing the FILE handles after the parser has been destroyed.
Comment 2 Kyle Evans freebsd_committer freebsd_triage 2026-05-06 13:02:04 UTC
Any reason not to also open it w/ CLOEXEC to be safe?
Comment 3 Jan Bramkamp 2026-05-06 14:02:14 UTC
(In reply to Kyle Evans from comment #2)
No. I just forgot that FreeBSD's fopen() has "e" for FD_CLOEXEC (added all the way back in FreeBSD 10.0 I just found out), but closing the files is still a good idea. Sadly we don't provide a fopen() mode modifier for FD_CLOFORK, but FD_CLOEXEC is good enough.
Comment 4 Jan Bramkamp 2026-05-06 14:06:05 UTC
Created attachment 270464 [details]
[PATCH 1/3] Fix PR#295052 by closing jail configuration files after parsing.
Comment 5 Jan Bramkamp 2026-05-06 14:07:10 UTC
Created attachment 270465 [details]
[PATCH 2/3] Improve fix for PR#295052 by adding "e" to mode.
Comment 6 Jan Bramkamp 2026-05-06 14:10:03 UTC
Created attachment 270467 [details]
[PATCH 3/3] Open the fstab files with fopen("re").

The third commit should probably be left out of any security patch, but it doesn't hurt add the same defense in depth to opened fstab files (which do get closed).
Comment 7 Jan Bramkamp 2026-05-06 14:29:11 UTC
Created attachment 270469 [details]
Just commit one and two squashed together

I squashed the first two commits together as requested by Kyle Evans.
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-05-06 23:29:30 UTC
A commit in branch main references this bug:

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

commit 276d9b88a9e6fd6fd90e57c36444756ad297d2ab
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-05-06 23:28:53 +0000

    jail: avoid leaking jail config fds to exec.* hooks

    The jail(8) command must not leave parsed configuration files open
    since the file descriptors will be leaked to child processes
    including the untrusted exec.start or exec.stop hooks.

    While fopen() doesn't provide direct access to O_CLOEXEC, it does
    provide access to FD_CLOEXEC via "e" in the mode string which
    provides the desired defense in depth against leaking file descriptors
    into exec.* hooks since those always execve() into a shell.

    Jail configuration is potentially sensitive and some hooks execute from
    within the jail context, leaving some opening for the jail to exfiltrate
    information about the host environment.

    (Commit message wordsmithed by kevans)

    PR:             295052
    Reviewed by:    kevans
    MFC after:      3 days

 usr.sbin/jail/config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-05-06 23:29:32 UTC
A commit in branch main references this bug:

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

commit 58811b0ae096c134af372bcf475aea1d8d0e3c08
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-05-06 23:28:53 +0000

    jail: open the fstab files with fopen("re")

    This protects against accidentally leaking them past fork()+exec()
    in future refactorings.

    PR:             295052
    Reviewed by:    kevans

 usr.sbin/jail/config.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 10 Jan Bramkamp 2026-05-07 13:44:28 UTC
Created attachment 270490 [details]
[PATCH] jail: add regression test for config file fd leaks

Add a regression test using ulimit to make jail(8) run out of open files if it doesn't close the configuration files.
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-05-07 17:26:34 UTC
A commit in branch stable/15 references this bug:

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

commit c35bb8ba898482920bf9b57967a9a11f98a89c81
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-05-07 17:26:18 +0000

    jail: avoid leaking jail config fds to exec.* hooks

    The jail(8) command must not leave parsed configuration files open
    since the file descriptors will be leaked to child processes
    including the untrusted exec.start or exec.stop hooks.

    While fopen() doesn't provide direct access to O_CLOEXEC, it does
    provide access to FD_CLOEXEC via "e" in the mode string which
    provides the desired defense in depth against leaking file descriptors
    into exec.* hooks since those always execve() into a shell.

    Jail configuration is potentially sensitive and some hooks execute from
    within the jail context, leaving some opening for the jail to exfiltrate
    information about the host environment.

    (Commit message wordsmithed by kevans)

    PR:             295052
    Reviewed by:    kevans

    (cherry picked from commit 276d9b88a9e6fd6fd90e57c36444756ad297d2ab)

 usr.sbin/jail/config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 12 commit-hook freebsd_committer freebsd_triage 2026-05-07 17:27:36 UTC
A commit in branch stable/14 references this bug:

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

commit 187de25ef99e49dd7a6a4bfa630f10ab9571ef17
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-05-07 17:27:03 +0000

    jail: avoid leaking jail config fds to exec.* hooks

    The jail(8) command must not leave parsed configuration files open
    since the file descriptors will be leaked to child processes
    including the untrusted exec.start or exec.stop hooks.

    While fopen() doesn't provide direct access to O_CLOEXEC, it does
    provide access to FD_CLOEXEC via "e" in the mode string which
    provides the desired defense in depth against leaking file descriptors
    into exec.* hooks since those always execve() into a shell.

    Jail configuration is potentially sensitive and some hooks execute from
    within the jail context, leaving some opening for the jail to exfiltrate
    information about the host environment.

    (Commit message wordsmithed by kevans)

    PR:             295052
    Reviewed by:    kevans

    (cherry picked from commit 276d9b88a9e6fd6fd90e57c36444756ad297d2ab)

 usr.sbin/jail/config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 13 Robert Clausecker freebsd_committer freebsd_triage 2026-05-07 18:33:04 UTC
Please try to get this into releng/15.1.
Comment 14 Kyle Evans freebsd_committer freebsd_triage 2026-05-07 18:36:31 UTC
> Please try to get this into releng/15.1.

That's the goal, but it's since been pointed out that there's no leak into the hooks here.  jail(8) does a `closefrom(3)` prior to exec'ing, so this is just plugging a normal leak rather than any kind of infoleak.
Comment 15 commit-hook freebsd_committer freebsd_triage 2026-05-07 20:41:18 UTC
A commit in branch releng/15.1 references this bug:

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

commit 38c5d60c367e788341e059ee3940183c293956ba
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-05-07 20:39:42 +0000

    jail: avoid leaking jail config fds to exec.* hooks

    The jail(8) command must not leave parsed configuration files open
    since the file descriptors will be leaked to child processes
    including the untrusted exec.start or exec.stop hooks.

    While fopen() doesn't provide direct access to O_CLOEXEC, it does
    provide access to FD_CLOEXEC via "e" in the mode string which
    provides the desired defense in depth against leaking file descriptors
    into exec.* hooks since those always execve() into a shell.

    Jail configuration is potentially sensitive and some hooks execute from
    within the jail context, leaving some opening for the jail to exfiltrate
    information about the host environment.

    (Commit message wordsmithed by kevans)

    Approved by:    re (cperciva)
    PR:             295052
    Reviewed by:    kevans

    (cherry picked from commit 276d9b88a9e6fd6fd90e57c36444756ad297d2ab)
    (cherry picked from commit c35bb8ba898482920bf9b57967a9a11f98a89c81)

 usr.sbin/jail/config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 16 Jan Bramkamp 2026-05-07 21:24:22 UTC
I think the closefrom() before execve() inside of run_command() makes it harder to exploit the fd leak, but is no complete mitigation.

Given a jail that has two or more exec.start/exec.stop hooks an attacker inside the jail (e.g. a malicious rc.d script) can guess the likely pids of new processes attaching to its jail and try to debug them before they reach closefrom().

While attaching to a jail partially protects a process against unwanted debugging by setting P_SUGID until it gets cleared by a call to execve() or fexecve() this only protects it from being debugged by unprivileged processes which would fail anyways because processes necessarily attach to a jail as root. To pass the p_candebug() check in ptrace (and procfs) an attacker needs to have PRIV_DEBUG_SUGID which the jail's root has by default.

If my analysis is correct the bug is still exploitable and every jail start/stop grants the attacker new attempts to win the race.
Comment 17 Kyle Evans freebsd_committer freebsd_triage 2026-05-07 23:23:42 UTC
(In reply to Jan Bramkamp from comment #16)

Your analysis makes me wonder if P_SUGID is not sufficient for navigating the jail boundary, and maybe we really should have something stronger here.
Comment 18 crest 2026-05-14 17:53:47 UTC
Created attachment 270731 [details]
[PATCH] jail: replace stdin with /dev/null after parsing

The jail command can read the configuration from stdin. In which case the configuration file wouldn't be covered by the closefrom(3) in run_command(). Replace stdin with /dev/null so the children will still inherit something harmless as stdin.
Comment 19 commit-hook freebsd_committer freebsd_triage 2026-06-10 04:01:25 UTC
A commit in branch stable/14 references this bug:

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

commit ee07da0c1e95d307d5120ac6a8a0ea5ccb88e61b
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-06-10 04:00:47 +0000

    jail: open the fstab files with fopen("re")

    This protects against accidentally leaking them past fork()+exec()
    in future refactorings.

    PR:             295052
    Reviewed by:    kevans

    (cherry picked from commit 58811b0ae096c134af372bcf475aea1d8d0e3c08)

 usr.sbin/jail/config.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 20 commit-hook freebsd_committer freebsd_triage 2026-06-10 04:02:34 UTC
A commit in branch stable/15 references this bug:

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

commit a03b45d38f8fc312a7a86c3ac2e4bdcbbad9f4d3
Author:     Jan Bramkamp <crest+freebsd@rlwinm.de>
AuthorDate: 2026-05-06 23:28:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-06-10 04:00:36 +0000

    jail: open the fstab files with fopen("re")

    This protects against accidentally leaking them past fork()+exec()
    in future refactorings.

    PR:             295052
    Reviewed by:    kevans

    (cherry picked from commit 58811b0ae096c134af372bcf475aea1d8d0e3c08)

 usr.sbin/jail/config.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)