#!/bin/sh set -eu # --------------------------------------------------------------------------- # cgroup v2 workaround for nested dockerd + runsc # # runsc creates a cgroup for each sandbox and must enable controllers on the # root of its cgroup namespace. On cgroup v2, a cgroup with processes in it # cannot have domain controllers enabled ("no internal process" rule). The # container runtime puts this container's processes in the namespace root, so # runsc gets EBUSY on cgroup.subtree_control. # # Moving PID 1 into a child cgroup makes the namespace root empty. Every s6 # service (dockerd, containerd, act_runner) inherits that child, so jobs can # still receive real CPU/memory limits from Docker. # --------------------------------------------------------------------------- if [ -f /sys/fs/cgroup/cgroup.controllers ]; then mkdir -p /sys/fs/cgroup/init echo "$$" > /sys/fs/cgroup/init/cgroup.procs 2>/dev/null || true fi # --------------------------------------------------------------------------- # Block direct access to the outer Docker host from CI jobs. # # Jobs share the runner's network namespace, and the default gateway is the # Docker host that runs this runner. If that host exposes a Docker API (or any # other administrative service) on its bridge address, a workflow could use it # to escape the sandbox entirely. Keep DNS to the gateway working, but reject # other direct traffic to the gateway IP. Outbound traffic to the internet is # unaffected because its destination is not the gateway address. # # Set BLOCK_HOST_GATEWAY=0 to disable this (not recommended). # --------------------------------------------------------------------------- if [ "${BLOCK_HOST_GATEWAY:-1}" = "1" ]; then gateway=$(ip route 2>/dev/null | awk '/^default / {print $3; exit}') if [ -n "${gateway:-}" ]; then if command -v iptables >/dev/null 2>&1; then IPTABLES=iptables elif command -v iptables-legacy >/dev/null 2>&1; then IPTABLES=iptables-legacy else IPTABLES="" fi if [ -n "${IPTABLES}" ]; then if ! "$IPTABLES" -C OUTPUT -d "$gateway" -j REJECT >/dev/null 2>&1; then "$IPTABLES" -I OUTPUT -d "$gateway" -p udp --dport 53 -j ACCEPT 2>/dev/null || true "$IPTABLES" -I OUTPUT -d "$gateway" -p tcp --dport 53 -j ACCEPT 2>/dev/null || true "$IPTABLES" -A OUTPUT -d "$gateway" -j REJECT 2>/dev/null || true echo "Blocked direct CI access to Docker-host gateway ${gateway} (DNS allowed)." fi fi fi fi # --------------------------------------------------------------------------- # Optional zero-touch registration. # # If no explicit registration token was supplied, use an existing Gitea admin # account to fetch a global runner registration token from the API. This keeps # the token out of the Compose file and works with a normal admin-created user. # --------------------------------------------------------------------------- if [ -z "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ] \ && [ -n "${GITEA_ADMIN_USER:-}" ] \ && [ -n "${GITEA_ADMIN_PASSWORD:-}" ]; then echo "Waiting for Gitea to issue a runner registration token..." i=0 while [ "$i" -lt 60 ]; do response=$(curl -fsS \ -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" \ -X POST \ "${GITEA_INSTANCE_URL%/}/api/v1/admin/actions/runners/registration-token" \ 2>/dev/null || true) token=$(printf '%s' "$response" | sed -n 's/.*"token":"\([^"]*\)".*/\1/p') if [ -n "$token" ]; then export GITEA_RUNNER_REGISTRATION_TOKEN="$token" echo "Obtained a runner registration token from the Gitea API." break fi i=$((i + 1)) sleep 2 done fi exec "$@"