| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | set -e |
| 3 | |
| 4 | # Set the container env-var, so that AppArmor is enabled in the daemon and |
| 5 | # containerd when running docker-in-docker. |
| 6 | # |
| 7 | # see: https://github.com/containerd/containerd/blob/787943dc1027a67f3b52631e084db0d4a6be2ccc/pkg/apparmor/apparmor_linux.go#L29-L45 |
| 8 | # see: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497 |
| 9 | container=docker |
| 10 | export container |
| 11 | |
| 12 | if [scip-shell system . . `[`. $# -eq 0 ]; then |
| 13 | echoscip-shell system . . echo. >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?' |
| 14 | exit 1 |
| 15 | fi |
| 16 | |
| 17 | if [scip-shell system . . `[`. ! -t 0 ]; then |
| 18 | echoscip-shell system . . echo. >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).' |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | # Mount /tmp (conditionally) |
| 23 | # /tmp must be 'exec,rw', and 'dev' (defaults) to allow mknod to work for the |
| 24 | # pkg/archive/archive_linux_test.go tests. |
| 25 | if ! mountpointscip-shell system . . mountpoint. -q /tmpscip-shell filesystem . . `/tmp`.; then |
| 26 | mountscip-shell system . . mount. -t tmpfs none /tmpscip-shell filesystem . . `/tmp`. |
| 27 | fi |
| 28 | |
| 29 | # Change mount propagation to shared, which SystemD PID 1 would normally do |
| 30 | # itself when started by the kernel. SystemD skips that when it detects it is |
| 31 | # running in a container. |
| 32 | mountscip-shell system . . mount. --make-rshared /scip-shell filesystem . . `/`. |
| 33 | |
| 34 | # Allow AppArmor to work inside the container; |
| 35 | # |
| 36 | # aa-status |
| 37 | # apparmor filesystem is not mounted. |
| 38 | # apparmor module is loaded. |
| 39 | # |
| 40 | # mount -t securityfs none /sys/kernel/security |
| 41 | # |
| 42 | # aa-status |
| 43 | # apparmor module is loaded. |
| 44 | # 30 profiles are loaded. |
| 45 | # 30 profiles are in enforce mode. |
| 46 | # /snap/snapd/18357/usr/lib/snapd/snap-confine |
| 47 | # ... |
| 48 | # |
| 49 | # Note: https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts#sys-kernel-security |
| 50 | # |
| 51 | # ## /sys/kernel/security |
| 52 | # |
| 53 | # In /sys/kernel/security mounted the securityfs interface, which allows |
| 54 | # configuration of Linux Security Modules. This allows configuration of |
| 55 | # AppArmor policies, and so access to this may allow a container to disable |
| 56 | # its MAC system. |
| 57 | # |
| 58 | # Given that we're running privileged already, this should not be an issue. |
| 59 | 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 |
| 60 | mountscip-shell system . . mount. -t securityfs none /sys/kernel/securityscip-shell filesystem . . `/sys/kernel/security`. || { |
| 61 | echoscip-shell system . . echo. >&2 'Could not mount /sys/kernel/security.' |
| 62 | echoscip-shell system . . echo. >&2 'AppArmor detection and --privileged mode might break.' |
| 63 | } |
| 64 | fi |
| 65 | |
| 66 | # Allow connections coming from the host (through eth0). This is needed to |
| 67 | # access the daemon port (independently of which port is used), or run a |
| 68 | # 'remote' Delve session, etc... |
| 69 | if [scip-shell system . . `[`. "${FIREWALLD:-}" = "true" ]; then |
| 70 | catscip-shell system . . cat. > /etc/firewalld/zones/trusted.xmlscip-shell filesystem . . `/etc/firewalld/zones/trusted.xml`. << EOF |
| 71 | <?xml version="1.0" encoding="utf-8"?> |
| 72 | <zone target="ACCEPT"> |
| 73 | <short>Trusted</short> |
| 74 | <description>All network connections are accepted.</description> |
| 75 | <interface name="eth0"/> |
| 76 | <forward/> |
| 77 | </zone> |
| 78 | EOF |
| 79 | fi |
| 80 | |
| 81 | envscip-shell system . . env. > /etc/docker-entrypoint-envscip-shell filesystem . . `/etc/docker-entrypoint-env`. |
| 82 | |
| 83 | catscip-shell system . . cat. > /etc/systemd/system/docker-entrypoint.targetscip-shell filesystem . . `/etc/systemd/system/docker-entrypoint.target`. << EOF |
| 84 | [Unit] |
| 85 | Description=the target for docker-entrypoint.service |
| 86 | Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service $([scip-shell system . . `[`. "${FIREWALLD:-}" = "true" ] && echoscip-shell system . . echo. firewalld.service) |
| 87 | EOF |
| 88 | |
| 89 | quoted_args="$(printfscip-shell system . . printf. " %q" "${@}")" |
| 90 | echoscip-shell system . . echo. "${quoted_args}" > /etc/docker-entrypoint-cmdscip-shell filesystem . . `/etc/docker-entrypoint-cmd`. |
| 91 | |
| 92 | catscip-shell system . . cat. > /etc/systemd/system/docker-entrypoint.servicescip-shell filesystem . . `/etc/systemd/system/docker-entrypoint.service`. << EOF |
| 93 | [Unit] |
| 94 | Description=docker-entrypoint.service |
| 95 | |
| 96 | [Service] |
| 97 | ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd" |
| 98 | # EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5) |
| 99 | ExecStopPost=/bin/bash -ec "if echo \${EXIT_STATUS} | grep [A-Z] > /dev/null; then echo >&2 \"got signal \${EXIT_STATUS}\"; systemctl exit \$(( 128 + \$( kill -l \${EXIT_STATUS} ) )); else systemctl exit \${EXIT_STATUS}; fi" |
| 100 | StandardInput=tty-force |
| 101 | StandardOutput=inherit |
| 102 | StandardError=inherit |
| 103 | WorkingDirectory=$(pwdscip-shell system . . pwd.) |
| 104 | EnvironmentFile=/etc/docker-entrypoint-env |
| 105 | |
| 106 | [Install] |
| 107 | WantedBy=multi-user.target |
| 108 | EOF |
| 109 | |
| 110 | systemctl mask systemd-firstboot.service systemd-udevd.service |
| 111 | systemctl unmask systemd-logind |
| 112 | systemctl enable docker-entrypoint.service |
| 113 | |
| 114 | systemd= |
| 115 | if [scip-shell system . . `[`. -x /lib/systemd/systemdscip-shell filesystem . . `/lib/systemd/systemd`. ]; then |
| 116 | systemd=/lib/systemd/systemdscip-shell filesystem . . `/lib/systemd/systemd`. |
| 117 | elif [scip-shell system . . `[`. -x /usr/lib/systemd/systemdscip-shell filesystem . . `/usr/lib/systemd/systemd`. ]; then |
| 118 | systemd=/usr/lib/systemd/systemdscip-shell filesystem . . `/usr/lib/systemd/systemd`. |
| 119 | elif [scip-shell system . . `[`. -x /sbin/initscip-shell filesystem . . `/sbin/init`. ]; then |
| 120 | systemd=/sbin/initscip-shell filesystem . . `/sbin/init`. |
| 121 | else |
| 122 | echoscip-shell system . . echo. >&2 'ERROR: systemd is not installed' |
| 123 | exit 1 |
| 124 | fi |
| 125 | systemd_args="--show-status=false --unit=docker-entrypoint.target" |
| 126 | echoscip-shell system . . echo. "$0: starting $systemd $systemd_args" |
| 127 | exec $systemd $systemd_args |