8000 mnt: anchor bind-mount sources to the inode that was inspected by derioii · Pull Request #392 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content

mnt: anchor bind-mount sources to the inode that was inspected - #392

Open
derioii wants to merge 1 commit into
google:masterfrom
derioii:mnt-anchor-bind-source-fd
Open

derioii wants to merge 1 commit into
google:masterfrom
derioii:mnt-anchor-bind-source-fd

Conversation

@derioii
@derioii derioii commented Nov 13, 2026
Copy link
Copy Markdown

mnt: anchor bind-mount sources to the inode that was inspected

Summary

prepareMountPoint() resolves a bind-mount source to decide whether it is a
directory, and the mount code then resolves the same string a second time. If
anything replaces the source between those two steps (rename, symlink swap from
an unprivileged user that can write the source path), nsjail mounts a different
host object than the one it inspected. This change opens the source once, in
prepareMountPoint(), and mounts that descriptor.

Both backends are patched, because both have the same double resolution:

backend before after
legacy (default) mount(src, dst, ...) mount("/proc/self/fd/<n>", dst, ...)
new API (--experimental_mnt new|auto) open_tree(AT_FDCWD, src, ...) open_tree(fd, "", ... | AT_EMPTY_PATH)

/proc/self/fd/<n> is only a stable name for an already-open descriptor: no
path is re-resolved. If /proc is not reachable the legacy backend falls back to
the path and says so once at warning level.

Why it matters

nsjail's filesystem isolation is the boundary it offers to whoever runs code
inside the jail. With the re-resolution, an unprivileged user who can write the
configured source path can make a root-owned nsjail bind an unrelated ho 8000 st
directory into the sandbox (for example a 0700 root-only directory that the
jailed process, running as uid 0 in the global user namespace, can then read).

The same attacker does not need this window: a source path they can write can simply be left pointing at the target directory, and both the patched and unpatched builds mount it. What this change removes is the narrower case where nsjail's own inspection and its mount disagree.

Measured on this tree with a live harness (see Testing): when the swap is
performed after nsjail inspected the source and before it mounts it, the
unpatched build mounts the substituted directory in 3-7% of attempts with a
plain destination path and in 68-80% with a destination path that makes nsjail
do more work in between. After the patch, 0 of 600 post-inspection swaps win,
in both backends (400 on the first build of the patch, 120 on a rebuilt tree,
80 after a fresh apply to a clean clone). The 0/600 is a consequence of
how the fixed code is built - the descriptor is taken before the point where
the attacker's swap lands - and not an independent result. The informative half
of the measurement is the unpatched build winning 68-80% of those same
attempts. Two further patched legacy runs (200 attempts, results/rerun1- and
rerun2-sync-patched-legacy-d40.csv) also produced 0 wins.

Scope of the guarantee (please read before merging)

The patch guarantees exactly one property: the object that is mounted is the
object that was inspected
. It does not turn an attacker-controlled source path
into a safe one. An attacker who substitutes before the inspection - for
example by flipping the path continuously, which is trivially possible for
anyone who can write the directory - still gets the substituted directory
mounted, with or without this patch (measured: ~50% of runs in both builds).
Keeping bind sources out of user-writable trees is, and remains, the operator's
responsibility. What the patch removes is the extra window in which nsjail's own
inspection can disagree with what it mounts, which is also the window any future
validation of the source would have to rely on.

Changes

  • mnt.h: mount_t gains src_fd.
  • mnt_legacy.cc: open the bind source with O_PATH | O_CLOEXEC in
    prepareMountPoint(), use fstat() on that descriptor for the is_dir
    decision, mount /proc/self/fd/<n> in mountSinglePoint(), close the
    descriptor on every exit path (including the containment early-returns) and
    warn once when /proc/self/fd is unavailable.
  • mnt_newapi.cc: same opening in prepareMountPoint();
    doBindMountAt() clones that descriptor with AT_EMPTY_PATH, falling back to
    /proc/self/fd/<n> on kernels whose open_tree(2) does not accept
    AT_EMPTY_PATH, and warns once if it has to fall back to the path.
  • mnt.cc: the cleanup loop releases src_fd as well.
  • tests/mount_source_anchor_test.c (new): performs the swap in both orders and
    asserts the outcome; needs root and uses a private mount namespace.
  • Makefile: the new test is added to TEST_BINS, gets its own build rule and
    runs as part of make test. It exits 0 when not root, so an unprivileged
    make test is unaffected.

Source-only diff: +127/-4 across four files (mnt.h, mnt.cc, mnt_legacy.cc,
mnt_newapi.cc). The new test adds +148/-0 and the Makefile wiring +5/-1, for a
total of +280/-5 across six files (git apply --numstat of PATCH-v3.diff).

Testing

  • New tests/mount_source_anchor_test.c follows both kernel sequences the two
    backends use - stat() then open_tree(AT_FDCWD, path), and
    open(O_PATH) then open_tree(fd, "", AT_EMPTY_PATH) - and performs the swap
    before the second call in each. It does not link nsjail code and therefore
    also passes on the unpatched tree; it demonstrates the mechanism, not the
    change.
$ gcc -O2 -o /tmp/msa tests/mount_source_anchor_test.c && sudo /tmp/msa
path re-resolved after swap : mounted 'E'  (attacker's directory)
descriptor anchored, swap   : mounted 'R'  (validated directory)
PASS: anchoring prevents mount-source substitution

The test is now also part of make test: it is listed in TEST_BINS and
invoked as the fifth test, so a plain make test really runs it (as root)
before the pre-existing setpriv failure stops the recipe.

  • Live race harness (attacker = uid 1000 owning the source directory, victim =
    root nsjail; win = the jailed process prints a 0600 root-only canary file):
                                   unpatched        patched
swap after inspection, dst /leak      3, 7 /100       0, 0 /100
swap after inspection, dst 40 deep   80, 77 /100      0, 0 /100
continuous flip (no window)         51, 46 /100      48, 49 /100
no substitution (control)            0, 0 /40        0, 0 /40

(pairs are legacy / new API; the patched cells were repeated: 0/60 twice per
backend, plus 0/40 twice per backend on a fresh clone with the patch applied
and rebuilt.)

  • Functional, both backends: -R mounts are still read-only inside the jail
    (touch /leak/probe -> Read-only file system); a symlinked source still
    resolves; a missing source still fails closed (exit 255); a 40-component
    destination path still mounts.
  • make test (root): 15 PASS / 1 FAIL on the patched tree and 14 PASS / 1 FAIL
    on the pristine tree - the difference is exactly the new test. The failure
    (setpriv ... --groups 1234, expects 255, gets 0) is environment-specific and
    reproduces on the pristine tree. Both runs stop at that point: the remaining
    tests live in the same recipe and are not reached, and make -k does not
    change that, because it continues with other targets rather than with the rest
    of a failed recipe. The patch does not change that behaviour.
  • Whole suite, run past that failure by prefixing each recipe line with - in a
    copy of each Makefile (measurement only, not part of this patch): 56 PASS /
    32 FAIL of 88 tests on the patched tree, 55 PASS / 32 FAIL of 87 on the
    pristine tree. The 32 failures are identical command for command and are
    environment-specific (no outbound network for the nat/socks5/connect configs,
    pasta/netsock, fake-geteuid and execveat config checks).

Notes

  • No public API, configuration option or exit-code change.
  • The new-API backend is opt-in; the legacy backend is the default, so both were
    patched to keep the two implementations consistent.
  • open(path, O_PATH) deliberately follows symlinks, which preserves the
    documented behaviour of symlinked bind sources; only the second resolution is
    removed.
  • CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:L/I:N/A:N = 2.5 (Low); this is preventive hardening and no exploitable oracle is claimed.

@google-cla
google-cla Bot commented Nov 13, 2026
Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

This branch has not been deployed

No deployments
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.

1 participant

0