Skip to content

fix(sandbox): keep shared folders out of namespace jails, read-only py/, own PID namespace - #3393

Draft
ckelly17 wants to merge 3 commits into
devfrom
fix-namespace-shared-mounts
Draft

ckelly17 wants to merge 3 commits into
devfrom
fix-namespace-shared-mounts

Conversation

@ckelly17

@ckelly17 ckelly17 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes three holes in the namespace sandbox (py/sandbox_launcher.py):

  • Every user's jail got the shared py/ folder and the whole insight cache mounted read-write. User code could rewrite the Python code every worker on the server runs, including the unjailed engine workers, and read or write other users' insight folders.
  • The read-only remount of py/ was best-effort and never checked, so a refused remount left it writable with nothing logged.
  • The jailed worker shared the host's PID namespace and runs as the same host user as Tomcat, so it could find and signal Tomcat and other workers. The jail root and the empty top-level mount folders (inject-root skeletons) were also writable, on the host disk with no quota.

Changes Made

  • SymlinkHelper.enqueueOrInject refuses any namespace mount at, above or inside py/, and at or above the insight cache, comparing real paths. Individual insight folders are still mounted when granted. Fakechroot links and OpenShell are unchanged.
  • _bind / _make_read_only: a refused read-only remount is retried with the mount flags a user namespace locks (nosuid, nodev, noexec, atime), and the result is checked with statvfs. For py/ (the --py-folder), a bind that is still writable raises EROFS and the sandbox does not start. Other read-only paths stay best-effort.
  • Own PID namespace: the supervisor unshares a PID namespace before forking. A small init runs as PID 1. It starts the worker, forwards stop signals, reaps orphans and clears the namespace when the worker exits. On a stop signal the supervisor stops the jail and reaps it, killing it after a 5 s grace.
  • The jail root and inject-root skeletons are remounted read-only in the interpreter's own view. /tmp, read-write binds and live injects stay writable.
  • The worker starts in its insight folder and gets HOME=/tmp when the server's HOME is not usable in the jail.

How to Test

  1. python3 py/sandbox_launcher.py --self-test (needs unprivileged user namespaces). Expected: ALL PASS (21/21). The new checks cover PID 1, an outside process that cannot be signalled, the read-only root and skeleton, a writable /tmp, a writable read-write inject and read-only worker code.
  2. cd py && python3 -m unittest testing.test_sandbox_launcher_read_only. Expected: 4 tests OK. They cover the locked-flag retry and the fatal path a normal kernel does not reach.
  3. mvn test -Dtest=SymlinkHelperUnitTests. Covers the refused mounts. nobodyCanWriteToAViewersCopy fails when run as root, because root ignores file permissions. It fails the same way on dev.

Notes

  • Overlaps with sandbox-customization, which also changes read-only remounts, the shared py/ and insight-cache mounts, and adds an opt-in PID namespace. The two take different approaches. This PR never mounts the shared folders and always uses a PID namespace. That branch mounts py/ read-only, keeps sharing the whole insight cache by default, and makes the PID namespace opt-in. We should settle on one model before either merges. Expect conflicts in inject(), _self_test() and SymlinkHelper.
  • Live read-only injects are still writable from inside the jail. inject() remounts the destination read-only after the bind, and that change does not reach the interpreter's copy of the mount, so viewer copies of projects stay writable. This PR does not change inject(). The fix (a staged private read-only bind) is in the jail secrets draft PR from Python-jail-secrets, so the two should land together.
  • Known gaps, for follow-up:
    • The self-test runs the interpreter as PID 1 directly, so the init process and the supervisor's signal handling (_run_init, _stop_on_signal) only run in real mode and have no automated test.
    • If the supervisor is SIGKILLed, the jail's init is re-parented and keeps running. PR_SET_PDEATHSIG on init, plus a getppid() check right after it, would close that.
    • A stop signal that arrives between init's fork() and its signal.signal() calls is dropped.

…a namespace jail

In NAMESPACE mode, SymlinkHelper's chroot setup asked for the SEMOSS py/
folder and the whole INSIGHT_CACHE_DIR to be linked, and symlinkFolder
turns each into a read-write namespace mount. Both are under the SEMOSS
home, so the launcher accepted them and replayed them into every user's
jail. User code could then read and write other users' insight folders,
and rewrite the py/ code that every python worker on the server runs,
including the unjailed engine workers.

enqueueOrInject now refuses any mount at, above or inside the py folder,
and at or above the insight cache, comparing real paths. The launcher
still binds py/ read-only, and individual insight folders are still
mounted when granted. Fakechroot links and OpenShell are unchanged.
The launcher binds the worker code folder (--py-folder) into the jail and
then remounts it read-only, but the remount was best-effort and its result
was never checked. If it was refused, py/ stayed writable inside the jail
with nothing logged, and code written there runs in every python worker on
the server.

_bind now checks the result with statvfs. A refused remount is retried
with the mount flags the outer mount already carries (a user namespace
refuses a remount that drops a locked flag such as nosuid or nodev). For
a strict path, the py folder, a bind that is still writable raises EROFS
and the sandbox does not start. Other read-only paths stay best-effort.

The self-test now binds a strict read-only code folder and checks the
interpreter can read it but not change it; unit tests cover the retry
and the fatal path, which a normal kernel does not reach.
… workers

The jailed worker runs as the same host user as Tomcat and every other
worker on the server, and shared their PID namespace. Without /proc it
could still find them with kill(pid, 0) and stop them: a probe in the
jail found and SIGTERMed a stand-in Tomcat. Its root folder and the
inject-root skeletons were also writable, on the host disk with no quota.

- The supervisor unshares a PID namespace before forking the jail. Its
  first process is a small init (PID 1) that starts the worker, forwards
  stop signals (PID 1 ignores signals it has no handler for, so a worker
  that was PID 1 would ignore Java's SIGTERM), reaps orphans, and clears
  the namespace when the worker exits. The worker sees only itself and
  init; a stand-in Tomcat outside cannot be signalled (ESRCH).
- On a stop signal the supervisor stops the jail and reaps it, killing it
  after a 5 s grace, so init is never left a zombie. Java's destroy (a
  SIGTERM to every process), a SIGTERM to the supervisor alone, and a
  worker that ignores SIGTERM all end with no processes left.
- The jail root and inject-root skeletons are remounted read-only in the
  interpreter's own view; /tmp, rw binds and live injects stay writable.
- The python worker starts in its insight folder, like the node worker,
  and gets HOME=/tmp when the server's HOME is not usable in the jail.

The self-test checks PID 1, that an outside process cannot be signalled,
the read-only root and skeleton, a writable /tmp and a writable rw inject
(21/21).
@snyk-io

snyk-io Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

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