sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

fs_deny: on-behalf open serves the supervisor's /proc/self, leaking its environ

Open congwang-mk opened this issue 2 months ago • 1 comments

With any --fs-deny active and /proc readable, the child's /proc/self is the supervisor's, including its environment.

$ SUPMARK=topsecret sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /proc -r /dev \
    --fs-deny /tmp/nonexistent -- env -u SUPMARK sh -c \
    'cat /proc/self/comm; tr "\0" "\n" < /proc/self/environ | grep SUPMARK'
sandlock
SUPMARK=topsecret

Without --fs-deny the same command prints cat and nothing else. The denied path does not have to exist or be related to /proc. Seen on main at 0f1d389.

The supervisor's environment is where API keys and tokens usually live, and the variable above was removed from the child's own environment, so this hands the sandbox secrets it was never given. Asking for the supervisor by pid (/proc/<supervisor pid>/environ) is refused by the per-PID filter; this path gets the same file without naming the pid.

Cause: once a deny is active, handle_notification turns every open that the handlers answered with Continue into on_behalf_open_for_deny (seccomp/notif.rs). That function resolves the child's path string with openat2 in the supervisor, and procfs resolves self and thread-self against the calling task, which is now the supervisor. The pinned file is /proc/<supervisor pid>/environ; deny_open_verdict accepts it because it sits under the /proc grant; the supervisor reopens it with its own credentials and injects the fd. handle_proc_open ran earlier but only saw the string /proc/self/environ, which carries no pid to filter.

A symlink reaches the same place, so rewriting the leading /proc/self in the string is not enough:

ln -s /proc/self /tmp/p; cat /tmp/p/comm      -> sandlock

Proposed fix: vet the pinned target, not only the written string. In reopen_existing_on_behalf, apply the per-PID filter to the real path of the probe fd; the supervisor's pid is never in ProcessIndex, so every route to its /proc entry fails closed whatever the spelling. For the direct spelling to keep working, the child's own /proc/self has to be addressed as /proc/<tgid> before the on-behalf open, which #233 already does by serving those reads itself ahead of this path.

Related, same function: RESOLVE_NO_MAGICLINKS on the probe makes /dev/stdin and any /proc/self/fd/N fail with ELOOP while a deny is active (echo hi | cat /dev/stdin prints "Too many levels of symbolic links").

Found while reviewing #236, which proposes reusing this path for all /proc opens.

congwang-mk avatar Sep 21 '26 04:09 congwang-mk

Fixed on main by #248 (538fe6f) and #250 (24aa55c, a96f028), with the direct spelling already covered by #233.

For the record, the leak was wider than the title says: through a link to /proc/self the child could also read the supervisor's maps and mem and list its fd directory, not only environ.

  • #248 vets the resolved /proc target of an open done for a deny with the same sensitive-path and per-PID checks as handle_proc_open, so every route to the supervisor's entry is refused.
  • #250 resolves /proc/self for the caller instead: /proc/self/fd/N and /dev/stdin, /dev/stdout, /dev/stderr, /dev/fd/N reopen the caller's own fd through pidfd_getfd, and a probe that lands in the supervisor's /proc directory is resolved again inside the caller's. The /dev/stdin ELOOP noted above is gone.

Still ELOOP while a deny is active: a link the user made to a magic link, and a magic link in the middle of a path (/proc/self/cwd/x).

congwang-mk avatar Sep 21 '26 21:09 congwang-mk