| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | set -e |
| 3 | |
| 4 | # DinD: a wrapper script which allows docker to be run inside a docker container. |
| 5 | # Original version by Jerome Petazzoni <[email protected]> |
| 6 | # See the blog post: https://www.docker.com/blog/docker-can-now-run-within-docker/ |
| 7 | # |
| 8 | # This script should be executed inside a docker container in privileged mode |
| 9 | # ('docker run --privileged', introduced in docker 0.6). |
| 10 | |
| 11 | # Usage: dind CMD [ARG...] |
| 12 | |
| 13 | # apparmor sucks and Docker needs to know that it's in a container (c) @tianon |
| 14 | # |
| 15 | # Set the container env-var, so that AppArmor is enabled in the daemon and |
| 16 | # containerd when running docker-in-docker. |
| 17 | # |
| 18 | # see: https://github.com/containerd/containerd/blob/787943dc1027a67f3b52631e084db0d4a6be2ccc/pkg/apparmor/apparmor_linux.go#L29-L45 |
| 19 | # see: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497 |
| 20 | export container=docker |
| 21 | |
| 22 | # Allow AppArmor to work inside the container; |
| 23 | # |
| 24 | # aa-status |
| 25 | # apparmor filesystem is not mounted. |
| 26 | # apparmor module is loaded. |
| 27 | # |
| 28 | # mount -t securityfs none /sys/kernel/security |
| 29 | # |
| 30 | # aa-status |
| 31 | # apparmor module is loaded. |
| 32 | # 30 profiles are loaded. |
| 33 | # 30 profiles are in enforce mode. |
| 34 | # /snap/snapd/18357/usr/lib/snapd/snap-confine |
| 35 | # ... |
| 36 | # |
| 37 | # Note: https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts#sys-kernel-security |
| 38 | # |
| 39 | # ## /sys/kernel/security |
| 40 | # |
| 41 | # In /sys/kernel/security mounted the securityfs interface, which allows |
| 42 | # configuration of Linux Security Modules. This allows configuration of |
| 43 | # AppArmor policies, and so access to this may allow a container to disable |
| 44 | # its MAC system. |
| 45 | # |
| 46 | # Given that we're running privileged already, this should not be an issue. |
| 47 | if [scip-shell system . . `[`. -d /sys/kernel/securityscip-shell filesystem . . `/sys/kernel/security`. ] && ! mountpointscip-shell system . . mountpoint. -q /sys/kernel/securityscip-shell filesystem . . `/sys/kernel/security`.; then |
| 48 | mountscip-shell system . . mount. -t securityfs none /sys/kernel/securityscip-shell filesystem . . `/sys/kernel/security`. || { |
| 49 | echoscip-shell system . . echo. >&2 'Could not mount /sys/kernel/security.' |
| 50 | echoscip-shell system . . echo. >&2 'AppArmor detection and --privileged mode might break.' |
| 51 | } |
| 52 | fi |
| 53 | |
| 54 | # Mount /tmp (conditionally) |
| 55 | # /tmp must be 'exec,rw', and 'dev' to allow mknod to work for the |
| 56 | # pkg/archive/archive_linux_test.go tests. |
| 57 | if ! mountpointscip-shell system . . mountpoint. -q /tmpscip-shell filesystem . . `/tmp`.; then |
| 58 | mountscip-shell system . . mount. -t tmpfs none /tmpscip-shell filesystem . . `/tmp`. |
| 59 | fi |
| 60 | |
| 61 | # cgroup v2: enable nesting |
| 62 | if [scip-shell system . . `[`. -f /sys/fs/cgroup/cgroup.controllersscip-shell filesystem . . `/sys/fs/cgroup/cgroup.controllers`. ]; then |
| 63 | # move the processes from the root group to the /init group, |
| 64 | # otherwise writing subtree_control fails with EBUSY. |
| 65 | # An error during moving non-existent process (i.e., "cat") is ignored. |
| 66 | mkdirscip-shell system . . mkdir. -p /sys/fs/cgroup/initscip-shell filesystem . . `/sys/fs/cgroup/init`. |
| 67 | # this happens in a loop because things like "docker exec" on our dind |
| 68 | # container will create new processes, which creates a race between our |
| 69 | # moving everything to "init" and enabling subtree_control |
| 70 | while ! { |
| 71 | # move the processes from the root group to the /init group, |
| 72 | # otherwise writing subtree_control fails with EBUSY. |
| 73 | # An error during moving non-existent process (i.e., "cat") is ignored. |
| 74 | xargsscip-shell system . . xargs. -rn1 < /sys/fs/cgroup/cgroup.procsscip-shell filesystem . . `/sys/fs/cgroup/cgroup.procs`. > /sys/fs/cgroup/init/cgroup.procsscip-shell filesystem . . `/sys/fs/cgroup/init/cgroup.procs`. || : |
| 75 | # enable controllers |
| 76 | sedscip-shell system . . sed. -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllersscip-shell filesystem . . `/sys/fs/cgroup/cgroup.controllers`. \ |
| 77 | > /sys/fs/cgroup/cgroup.subtree_controlscip-shell filesystem . . `/sys/fs/cgroup/cgroup.subtree_control`. |
| 78 | }; do truescip-shell system . . true.; done |
| 79 | fi |
| 80 | |
| 81 | # Change mount propagation to shared to make the environment more similar to a |
| 82 | # modern Linux system, e.g. with SystemD as PID 1. |
| 83 | mountscip-shell system . . mount. --make-rshared /scip-shell filesystem . . `/`. |
| 84 | |
| 85 | if [scip-shell system . . `[`. $# -gt 0 ]; then |
| 86 | exec "$@" |
| 87 | fi |
| 88 | |
| 89 | echoscip-shell system . . echo. >&2 'ERROR: No command specified.' |
| 90 | echoscip-shell system . . echo. >&2 'You probably want to run hack/make.sh, or maybe a shell?' |