jailbash 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. # Use bubblewrap to run /bin/bash reusing the host OS binaries (/usr), but with
  3. # separate /tmp, /home, /var, /run, and /etc. For /etc we just inherit the
  4. # host's resolv.conf, and set up "stub" passwd/group files.
  5. set -euo pipefail
  6. (exec -a jailbash bwrap --ro-bind /usr /usr \
  7. --ro-bind /lib /lib \
  8. --ro-bind-try /lib64 /lib64 \
  9. --tmpfs /usr/lib/modules \
  10. --tmpfs /usr/lib/systemd \
  11. --ro-bind /bin /bin \
  12. --ro-bind /sbin /sbin \
  13. --dir /var \
  14. --dir /tmp \
  15. --symlink ../tmp var/tmp \
  16. --proc /proc \
  17. --dev /dev \
  18. --bind ${HOME} ${HOME} \
  19. --ro-bind-try /etc/profile /etc/profile \
  20. --ro-bind-try /etc/alternatives /etc/alternatives \
  21. --ro-bind-try /etc/localtime /etc/localtime \
  22. --ro-bind-try /etc/ld.so.cache /etc/ld.so.cache \
  23. --ro-bind-try /etc/resolv.conf /etc/resolv.conf \
  24. --ro-bind-try /etc/hosts /etc/hosts \
  25. --ro-bind-try /etc/nsswitch.conf /etc/nsswitch.conf \
  26. --ro-bind-try /etc/ssl /etc/ssl \
  27. --ro-bind-try /etc/pki /etc/pki \
  28. --ro-bind-try /etc/manpath.config /etc/manpath.config \
  29. --bind-try /run/mysqld/mysqld.sock /run/mysqld/mysqld.sock \
  30. --chdir ${HOME} \
  31. --unshare-all \
  32. --share-net \
  33. --die-with-parent \
  34. --dir /run/user/$(id -u) \
  35. --setenv XDG_RUNTIME_DIR "/run/user/$(id -u)" \
  36. --setenv PS1 "$(id -nu)$ " \
  37. --file 11 /etc/passwd \
  38. --file 12 /etc/group \
  39. /bin/bash -l "$@") \
  40. 11< <(getent passwd $UID 65534) \
  41. 12< <(getent group $(id -g) 65534)