Skip to content

Instantly share code, notes, and snippets.

@tribela
Last active November 21, 2016 19:09
Show Gist options
  • Save tribela/6a8f1a828f3a7f1fafab4c699b6b5149 to your computer and use it in GitHub Desktop.
Save tribela/6a8f1a828f3a7f1fafab4c699b6b5149 to your computer and use it in GitHub Desktop.
Unshared jail
#!/bin/bash
if [ "$1" != "_unshared_" ]; then
exec unshare -ruim "$0" _unshared_ "$@"
else
shift
fi
timeout=$1
tempdir=$(mktemp -d)
dirs_to_mount=({/bin,/lib,/lib64,/usr/{bin,lib,local}})
for dir in ${dirs_to_mount[@]}; do
mkdir -p ${tempdir}${dir}
mount -o bind,ro ${dir} ${tempdir}${dir}
done
# chroot $tempdir /usr/bin/timeout 5 "$@"
env -i $(which chroot) $tempdir /usr/bin/timeout $timeout ${@:2}
for dir in ${dirs_to_mount[@]}; do
umount ${tempdir}${dir}
done
rm -rf $tempdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment