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.
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.
Any reason not to also open it w/ CLOEXEC to be safe?
(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.
Created attachment 270464 [details] [PATCH 1/3] Fix PR#295052 by closing jail configuration files after parsing.
Created attachment 270465 [details] [PATCH 2/3] Improve fix for PR#295052 by adding "e" to mode.
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).
Created attachment 270469 [details] Just commit one and two squashed together I squashed the first two commits together as requested by Kyle Evans.
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(-)
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(-)
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.
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(-)
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(-)
Please try to get this into releng/15.1.
> 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.
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(-)
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.
(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.
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.
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(-)
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(-)