JuliaContainerization / Sandbox.jl

The cultured host's toolkit for ill-mannered Linux guests.
Other
35 stars 5 forks source link

sysfs bindmound broke unpriviliged runner on Arch Linux #98

Closed maleadt closed 2 years ago

maleadt commented 2 years ago

Before https://github.com/staticfloat/Sandbox.jl/pull/94:

[ Info: Checking to see if /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/ is encrypted...
┌ Info: Running sandboxed command
│   user_cmd.exec =
│    1-element Vector{String}:
└     "/bin/sh"
verbose sandbox enabled (running in unprivileged container mode)
Parsed --rootfs as "/home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea"
Parsed --cd as "/"
Parsed --uid as "0"
Parsed --gid as "0"
Child Process PID is 9689
--> Mapping 1000:1000 to 0:0 within container namespace
--> Creating overlay workdir at /proc
--> Mounting overlay of /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea at /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea (modifications in /proc/upper/rootfs, workspace in /proc/work/rootfs)
--> Mounting procfs at /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/proc
--> Bind-mounting /dev/null over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/null (read-write)
--> Bind-mounting /dev/tty over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/tty (read-write)
--> Bind-mounting /dev/zero over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/zero (read-write)
--> Bind-mounting /dev/random over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/random (read-write)
--> Bind-mounting /dev/urandom over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/urandom (read-write)
--> Bind-mounting /dev/shm over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/shm (read-write)
--> Bind-mounting /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/dev/pts/ptmx over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/dev/ptmx (read-write)
--> Mounting procfs at /proc
Entering rootfs at /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea
--> pivot_root() succeeded and unmounted old root
About to run `/bin/sh`
/ #

After:

[ Info: Checking to see if /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/ is encrypted...
┌ Info: Running sandboxed command
│   user_cmd.exec =
│    1-element Vector{String}:
└     "/bin/sh"
verbose sandbox enabled (running in unprivileged container mode)
Parsed --rootfs as "/home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea"
Parsed --cd as "/"
Parsed --uid as "0"
Parsed --gid as "0"
Child Process PID is 9417
--> Mapping 1000:1000 to 0:0 within container namespace
--> Creating overlay workdir at /proc
--> Mounting overlay of /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea at /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea (modifications in /proc/upper/rootfs, workspace in /proc/work/rootfs)
--> Mounting procfs at /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea/proc
--> Bind-mounting /dev/null over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/null (read-write)
--> Bind-mounting /dev/tty over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/tty (read-write)
--> Bind-mounting /dev/zero over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/zero (read-write)
--> Bind-mounting /dev/random over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/random (read-write)
--> Bind-mounting /dev/urandom over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/urandom (read-write)
--> Bind-mounting /dev/shm over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//dev/shm (read-write)
--> Bind-mounting /sys over /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea//sys (read-only)
At line 369, ABORTED (1: Operation not permitted)!
At line 929, ABORTED (22: Invalid argument)!
ERROR: LoadError: failed process: Process(setenv(`/home/tim/Julia/depot/scratchspaces/9307e30f-c43e-9ca7-d17c-c2dc59df670d/local_sandbox/sandbox --verbose --rootfs /home/tim/Julia/depot/artifacts/562768a40e93d27b79fbedf9cfa7883409d494ea --cd / --uid 0 --gid 0 -- /bin/sh`,String[]), ProcessExited(1)) [1]

Arch Linux, 5.15.55-2-lts

maleadt commented 2 years ago

The issue looks two-fold. For one, remounting fails, but I'm not sure why we do that in the first place (I'll rip that out in the PR I'm about to create). Second, my /sys has other stuff mounted, so requires MS_REC:

$ mount | grep '/sys'
sys on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
efivarfs on /sys/firmware/efi/efivars type efivarfs (rw,nosuid,nodev,noexec,relatime)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=30,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=35570)
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime)
configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime)
none on /run/credentials/systemd-sysusers.service type ramfs (ro,nosuid,nodev,noexec,relatime,mode=700)
maleadt commented 2 years ago

Oops, just saw https://github.com/staticfloat/Sandbox.jl/pull/97. I don't the issue is MS_ NODEV, it works fine here without the remount and adding MS_REC.