diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index c285e3ff48..8a328620d3 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -14,6 +14,7 @@ on: - 'docker/healthcheck.sh' - 'docker/run-docker-tests.sh' - 'docker/test-run-docker-tests.sh' + - 'docker/test-compat-socket.sh' - 'compatibility-tests/**' - '.github/ci/compat-timing-report.py' - '.github/ci/compat-opentofu-drift-check.py' @@ -94,6 +95,9 @@ jobs: docker rm -f compat-smoke >/dev/null [ "$health" = healthy ] || { echo "::error::compat image did not become healthy: $health"; exit 1; } + - name: Compat image reaches a Docker socket owned by a non-root group + run: sh docker/test-compat-socket.sh floci-compat-shim-test + - name: Unflagged aws sqs call routes to AWS_ENDPOINT_URL run: | out=$(docker run --rm --entrypoint sh floci-compat-shim-test -c \ diff --git a/docker/Dockerfile.compat b/docker/Dockerfile.compat index 98cc36f83a..f27269f130 100644 --- a/docker/Dockerfile.compat +++ b/docker/Dockerfile.compat @@ -34,6 +34,10 @@ ENV AWS_SECRET_ACCESS_KEY=test ENV AWS_ENDPOINT_URL="http://localhost:4566" ENV AWS_CONFIG_FILE=/etc/floci/aws/config ENV AWS_PAGER="" +# The image starts as root so the entrypoint can read the mounted Docker socket's group before it +# drops to uid 1001, and that drop keeps the environment. Without this, hooks would run as uid 1001 +# with HOME=/root, which they cannot write to, and `pip install` would have nowhere to put a package. +ENV HOME=/app # AWS CLI v2. v1 (the only version pip3 install awscli can give you) resolves # a fixed set of services (SQS, RDS, DocDB, Neptune, EMR, Health) through @@ -125,5 +129,3 @@ HEALTHCHECK --interval=5s --timeout=3s --retries=5 --start-period=10s --start-in ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] # A published port needs 0.0.0.0 inside the container, which Floci only accepts with explicit consent. CMD ["/app/application", "-Dquarkus.http.host=0.0.0.0"] - -USER 1001 diff --git a/docker/test-compat-socket.sh b/docker/test-compat-socket.sh new file mode 100644 index 0000000000..8a621564af --- /dev/null +++ b/docker/test-compat-socket.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# Starts the compat image with the host's Docker socket mounted and checks what PID 1 ends up as. +# Run directly: sh docker/test-compat-socket.sh +# +# Needs a socket whose group is not root, as on native Linux: on Docker Desktop the socket is +# root:root, group 0 is every container's primary group, and the check could not fail. + +set -eu + +IMAGE="${1:?usage: test-compat-socket.sh }" +SOCKET=/var/run/docker.sock +NAME="floci-compat-socket-test-$$" +FAIL=0 + +SOCKET_GID="$(stat -c '%g' "${SOCKET}")" +if [ "${SOCKET_GID}" = '0' ]; then + echo "${SOCKET} is owned by group 0, so this cannot tell a working image from a broken one." >&2 + exit 1 +fi + +remove_container() { + # A stop, not a kill, so Floci removes the helper containers it started on this daemon. + docker stop -t 10 "${NAME}" >/dev/null 2>&1 || true + docker rm -f "${NAME}" >/dev/null 2>&1 || true +} +trap remove_container EXIT INT TERM + +check() { + desc="$1"; shift + if "$@"; then + printf '[PASS] %s\n' "${desc}" + else + printf '[FAIL] %s\n' "${desc}" + FAIL=1 + fi +} + +start() { + remove_container + docker run -d --name "${NAME}" -v "${SOCKET}:${SOCKET}" "$@" "${IMAGE}" >/dev/null + for _ in $(seq 1 60); do + [ "$(docker inspect -f '{{.State.Health.Status}}' "${NAME}")" = healthy ] && return 0 + sleep 1 + done + docker logs "${NAME}" 2>&1 | tail -20 + echo "${IMAGE} did not become healthy" >&2 + exit 1 +} + +pid1_uid() { docker exec "${NAME}" sh -c "awk '/^Uid:/ {print \$2}' /proc/1/status"; } +pid1_has_socket_group() { docker exec "${NAME}" grep -Eq "^Groups:.*\b${SOCKET_GID}\b" /proc/1/status; } +# As uid 1001: root in a container has no CAP_SYS_PTRACE, so it cannot read another user's environ. +pid1_home() { docker exec -u 1001 "${NAME}" sh -c "tr '\0' '\n' < /proc/1/environ | sed -n 's/^HOME=//p'"; } +uid_is() { [ "$(pid1_uid)" = "$1" ]; } +home_is() { [ "$(pid1_home)" = "$1" ]; } +reports_unreachable() { docker logs "${NAME}" 2>&1 | grep -q 'Docker is not reachable'; } + +# The availability report runs on its own thread after startup and logs nothing when Docker answers. +# Only this case has something to wait for. A missing warning would not prove the report ran, so the +# other cases do not look for it: the group and uid checks on PID 1 are what prove the fix. +wait_for_unreachable_report() { + for _ in $(seq 1 30); do + reports_unreachable && return 0 + sleep 1 + done + return 1 +} + +echo "Socket group: ${SOCKET_GID}" + +echo '--- started as uid 1001: the entrypoint cannot add the group, so the check can fail' +start --user 1001 +check 'PID 1 is uid 1001' uid_is 1001 +check 'Floci reports Docker as unreachable' wait_for_unreachable_report + +echo '--- default' +start +check 'PID 1 is uid 1001' uid_is 1001 +check 'PID 1 has the socket group' pid1_has_socket_group +check 'PID 1 HOME is /app' home_is /app +check 'HOME is writable by uid 1001' docker exec -u 1001 "${NAME}" touch /app/.home-write-probe + +echo '--- FLOCI_RUN_AS_ROOT=true' +start -e FLOCI_RUN_AS_ROOT=true +check 'PID 1 is uid 0' uid_is 0 + +exit "${FAIL}" diff --git a/docs/configuration/docker.md b/docs/configuration/docker.md index c4cf614755..4c0d4b2288 100644 --- a/docs/configuration/docker.md +++ b/docs/configuration/docker.md @@ -48,6 +48,8 @@ services: Use this only when the socket's permissions require root. Without the option, Floci retains its unprivileged default. The setting applies to the container entrypoint, not to `floci.docker` configuration. +The entrypoint needs to start as root to read the socket's group and to honour this option. A container started as a non-root user, with `--user 1001` or a Kubernetes `runAsUser`, skips both, so give it the socket's group yourself: `--group-add "$(stat -c '%g' /var/run/docker.sock)"`. + ## Connection Pool Every Docker call Floci makes goes through one shared client with a bounded connection pool. Some of those connections stay open for as long as a container runs: each Lambda container holds one (the watcher that notices its runtime exiting) plus one per Lambda extension. Log-follow streams use a second pool, described below. When those long-lived connections fill the pool, every other Docker call (create, start, stop, remove) waits for one to free up, and Floci stops making progress.