Repository navigation
Conversation
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
mnt: anchor bind-mount sources to the inode that was inspected
Summary
prepareMountPoint()resolves a bind-mount source to decide whether it is adirectory, 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:
mount(src, dst, ...)mount("/proc/self/fd/<n>", dst, ...)--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: nopath is re-resolved. If
/procis not reachable the legacy backend falls back tothe 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_tgainssrc_fd.mnt_legacy.cc: open the bind source withO_PATH | O_CLOEXECinprepareMountPoint(), usefstat()on that descriptor for theis_dirdecision, mount
/proc/self/fd/<n>inmountSinglePoint(), close thedescriptor on every exit path (including the containment early-returns) and
warn once when
/proc/self/fdis unavailable.mnt_newapi.cc: same opening inprepareMountPoint();doBindMountAt()clones that descriptor withAT_EMPTY_PATH, falling back to/proc/self/fd/<n>on kernels whoseopen_tree(2)does not acceptAT_EMPTY_PATH, and warns once if it has to fall back to the path.mnt.cc: the cleanup loop releasessrc_fdas well.tests/mount_source_anchor_test.c(new): performs the swap in both orders andasserts the outcome; needs root and uses a private mount namespace.
Makefile: the new test is added toTEST_BINS, gets its own build rule andruns as part of
make test. It exits 0 when not root, so an unprivilegedmake testis 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 --numstatof PATCH-v3.diff).Testing
tests/mount_source_anchor_test.cfollows both kernel sequences the twobackends use -
stat()thenopen_tree(AT_FDCWD, path), andopen(O_PATH)thenopen_tree(fd, "", AT_EMPTY_PATH)- and performs the swapbefore 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.
The test is now also part of
make test: it is listed inTEST_BINSandinvoked as the fifth test, so a plain
make testreally runs it (as root)before the pre-existing setpriv failure stops the recipe.
root nsjail; win = the jailed process prints a 0600 root-only canary file):
(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.)
-Rmounts are still read-only inside the jail(
touch /leak/probe->Read-only file system); a symlinked source stillresolves; 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 FAILon the pristine tree - the difference is exactly the new test. The failure
(
setpriv ... --groups 1234, expects 255, gets 0) is environment-specific andreproduces on the pristine tree. Both runs stop at that point: the remaining
tests live in the same recipe and are not reached, and
make -kdoes notchange that, because it continues with other targets rather than with the rest
of a failed recipe. The patch does not change that behaviour.
-in acopy 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
patched to keep the two implementations consistent.
open(path, O_PATH)deliberately follows symlinks, which preserves thedocumented behaviour of symlinked bind sources; only the second resolution is
removed.