8000 mnt: mark staging tmpfs MS_UNBINDABLE before processing config mounts by matepad007 · Pull Request #415 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content

mnt: mark staging tmpfs MS_UNBINDABLE before processing config mounts - #415

Merged
robertswiecki merged 1 commit into
google:masterfrom
matepad007:fix-staging-mount-unbindable
Dec 5, 2026
Merged

robertswiecki merged 1 commit into
google:masterfrom
matepad007:fix-staging-mount-unbindable

Conversation

@matepad007
Copy link
Copy Markdown
Contributor

Summary

Both mount backends create a staging tmpfs at destdir (the future jail root) with unrestricted mount propagation, and only lock its submounts down to the configured ro/nosuid/nodev/noexec flags in a later pass, after every config-supplied mount has already been attached under it.

If a config mount recursively binds a host directory that is an ancestor of destdir (e.g. -B /tmp:/tmp when the staging root happens to land under /tmp), that bind's copy_tree/open_tree(AT_RECURSIVE) snapshot includes destdir's own staging submounts as they exist at that instant, still carrying their pre-lockdown flags.

  • In the legacy backend, remountPt's mountinfo walk does reach the cloned copy, but it applies the ancestor bind's own flags to it rather than the original mount's intended flags, so a writable ancestor bind re-flags the leaked clone writable.
  • In the new-API backend there is no walk-back pass at all, so the clone's flags are frozen at clone time and never revisited.

Because a bind mount of tmpfs shares the same superblock/dentries as the original (standard VFS behavior, not kernel-version-dependent), a write through the leaked clone lands in the same filesystem instance as the jail's own root, defeating is_root_rw=false with no additional host-side permission needed in the base case.

This needs a config that recursively binds a whole ancestor of the staging path — none of the project's own example configs do this; they bind narrow sibling paths instead (e.g. /run/user/$UID/wayland-0, /tmp/.X11-unix). It is a real but non-default-config-dependent gap, not a default-exposure issue.

Fix

Mark the staging tmpfs MS_UNBINDABLE immediately after creation, before any config mount is processed, in both backends. Per Documentation/filesystems/sharedsubtree.rst, an unbindable mount is pruned from any later recursive bind's copy_tree, which removes the leak at the root instead of trying to teach the remount pass to distinguish "genuinely nested under my own staging tree" from "a different bind dragged my staging tree in."

The new-API backend additionally reused destdir's own fd (via dup) as the clone source for dynamic-content (src_content) staging. Marking destdir unbindable while that dup is still in use would break src_content mounts, which several existing test configs exercise (tests/connect.cfg, tests/socks5.cfg, tests/nstun.cfg, others). Dynamic-content staging now gets its own dedicated detached tmpfs instead, so destdir can stay unbindable throughout without regressing that path.

tmpdir (the second scratch tmpfs, used as a bind source for dynamic content in the legacy backend) is deliberately left bindable — marking it unbindable would block binding files out of it, which is a real use, not just its own mountpoint.

Test

Added tests/mount_staging_unbindable_test.c, matching the style of the existing tests/mount_source_anchor_test.c (plain C, geteuid()/unshare(CLONE_NEWNS) gated, skips when not root or no userns). It reproduces the staging setup and the ancestor-bind leak directly with mount(2): shows a marker written into the staging tmpfs is visible through a recursive ancestor bind before the fix, and not visible after MS_UNBINDABLE is applied. Wired into the Makefile the same way as the existing test.

Validation

  • make -j2: not run on this machine (macOS, no Linux mount API available in this environment).
  • The new test and the pre-existing mount_source_anchor_test.c both fail to even syntax-check here for the same reason (no linux/mount.h, MS_*, CLONE_NEWNS on macOS) — confirmed this is an environment limitation, not specific to the new code.
  • Traced every mount()/open_tree() call site in both backends against Documentation/filesystems/sharedsubtree.rst's unbindable-mount semantics to confirm the src_content regression before writing the fix, not after.
  • make test is not run by this repo's CI (dockerpush.yml only runs make), so I was not able to confirm ./tests/mount_staging_unbindable_test plus the src_content-exercising config 8000 s pass on a real kernel. A review/run on real Linux would be good before merge.

@matepad007
matepad007 force-pushed the fix-staging-mount-unbindable branch from f72dc12 to c406a68 Compare December 5, 2026 03:33
@matepad007
matepad007 force-pushed the fix-staging-mount-unbindable branch from c406a68 to 2f187b7 Compare December 5, 2026 03:37
@robertswiecki
robertswiecki merged commit 5074e64 into google:master Dec 5, 2026
6 checks passed
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.

2 participants

0