* fix(docker): strip setuid/setgid and file caps from every sandbox file, persistent sandboxes too
#141033 hands root-owned sandbox files back to the host user when a root
container is removed; chown clears setuid/setgid and file capabilities on
the files it touches. Two paths still let a root sandbox leave a file in its
host bind dirs that the host user then runs with raised privileges (proved
on real Docker):
1. The walk only selects root-owned entries. A host-user-owned, group-root
setgid file (host egid 0) or file capabilities on a host-user-owned file
(when SETFCAP is granted through docker_extra_args) are never touched.
2. In the default docker_persist_across_processes mode the container is
never removed, so no handback (and no strip) ever runs.
Every root pass now first re-chowns each setuid/setgid regular file under the
sandbox mounts to its own uid (one find per distinct owner; chown to the same
owner clears the bits and security.capability and changes no owner). find
cannot test xattrs, so when the container was granted SETFCAP / ALL or is
privileged, every regular file is re-chowned the same way (~1-2s per 20k
files measured; the base args drop SETFCAP, so the default path stays
`-perm /6000`). Device nodes/FIFOs keep #141033's filter.
The strip-only pass also runs for persistent containers: on session detach
(cleanup in persist mode, on the atexit-drained teardown thread), before
the first command on reattach, and in reap_orphan_containers before an
exited container is removed. It runs in a throwaway of the image with only
the sandbox dirs bind-mounted (not --volumes-from), so a running container
swapping paths mid-walk can only steer it at sandbox files or the throwaway
rootfs. Same gates as the handback: Linux, Hermes not root, no rootless /
userns-remap, root-run container; bounded at 10s, best-effort.
* fix(docker): one-scan privilege strip that runs on any image, after the handback, from the container's own binds
- Strip setuid/setgid with one `find -perm /6000 -exec chmod ug-s` (no stat, so images without
/bin/stat such as bullseye-slim are covered; GNU and busybox verified). Only a container that
could set file caps (SETFCAP/ALL/privileged) re-chowns every file to its own uid, one throwaway.
- Teardown hands ownership back first, then strips on its own 10s budget, so a huge SETFCAP tree
can no longer starve the handback.
- The strip reads its targets from the container's existing sandbox binds (realpath on both
sides; the creator's sandbox root is labeled), so a profile attaching to a shared container
strips the creator's dirs and a symlinked HERMES_HOME/TERMINAL_SANDBOX_DIR still matches.
- Binds with `--mount` and skips missing sources: a deleted task dir is never recreated root-owned.
- A strip that cannot run warns once per sandbox dir instead of logging at debug.
* fix(docker): strip only sandbox dirs this host derives, never from container labels
The root strip trusted a hermes-sandbox container label to pick which host
binds it mounts and chmods. Labels are settable by docker_extra_args (which
override Hermes's own) and inherited from any image LABEL, so a sandbox could
point the strip at host files outside its sandbox (setuid bits stripped from
a project dir; label "/" matched "/", "/usr", "~/.ssh" binds).
Drop the label. A bind source is kept only if its realpath sits under a
<sandbox>/docker root this process computes itself: its own sandbox dir, or
the default one of any profile of the same Hermes root (shared containers
across profiles). The strip mounts the resolved path, so a symlink swapped
in after the check is never followed.