Small sandbox inspired by Chromium's good ol' suid sandbox & friends
Note: build as a challenge (archived here) for the thc18 CTF.
Use the provided Makefile:
git clone https://github.com/plcp/sandkox
cd sandkox
Make
How to bootstrap the sandbox is left as an exercise to an attentive reader.
Here are a small listing of symbols exposed by smallkox.so:
- 
sandkoxcreates a new PID namespace, then jail the process and drops its privileges.See
jail_strap+jail_finalanddrop_priv+lock_priv - 
drop_privis a superset ofdrop_rootthat preservesCAP_SET_PCAPSee
lock_caps - 
drop_rootdrops root privileges, checks if effectively dropped then sets the process as not dumpable.See
drop_ptrace - 
drop_uiddrops privileged user to eitherrgid,SUDO_GIDor an unuseduid. - 
drop_giddrops privileged group to eitherrgid,SUDO_GIDor an unusedgid, also cleans supplementary groups. - 
drop_ptracesets the process as not dumpable – forbids unprivilegedptrace(2)calls to attach the process. - 
lock_privis defined aslock_news,lock_bitsandlock_capscalled in sequence. - 
lock_capsdrop all capabilities – may requireCAP_SET_PCAP. - 
lock_bitsdisables thread's "keep capabilities" flag,SECBIT_NOROOTandSECBIT_NO_SETUID_FIXUP, then locks them.See
capabilities(7)+/The securebits - 
lock_newssets thread'sno_new_privsbit to disabled – inherited, see linuxDocumentation/prctl/no_new_privs.txt. - 
jail_strapprepares a jail intosafedir– works best with/proc/self/fdinfo– and returnsfdforjail_final.See
jail_final - 
jail_finaleffectively jail active process – it must be unprivileged to be effective.See
drop_root 
Note: as jail_strap chroot the calling process from a helper child – via
clone(2) + CLONE_FS– into safedir, setting safedir to
/proc/self/fdinfo prevents the unprivileged¹ parent to access the
filesystem – including . and / – as the proc(5) pseudofiles attached
to the privileged child are protected – see ptrace(2) + /pseudofiles.
¹after calling jail_strap, a well-behaved calling process calls
drop_priv and jail_final in sequence, effectively jailing itself after
dropping its privileges.