Runner isolation

Protected runners, the Docker socket problem, and allowed images.

Advanced14 min · lesson 8 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (15 files, 6 KB): runnerhard.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/runnerhard.tar.gz && tar -xzf runnerhard.tar.gz, which creates ~/lab/runnerhard/. SHA-256: beaba58081b090818e5649456fc95222d5cad5a31e17d5bfbba5f9100d33337b

Protected variables keep the production token away from feature branch pipelines. But the token is used on a runner, and runners are shared. If a feature branch job can run on the same machine, it can leave something behind for the next job, read what another job left, or, on a badly configured runner, take over the machine. Isolation decides which jobs share a runner, and what a job on that runner can touch.

A runner only protected branches can use

A runner's access level can be ref_protected. GitLab then gives it jobs only from pipelines on protected branches and tags (and from merge request pipelines, if the project allows them protected resources). Make the lab runner one:

app/.gitlab-ci.yml
# A normal job, and a job that asks for the Docker CLI on a runner tagged "socket".
build:
image: secopslog-py:3.15
script:
- echo "built $CI_COMMIT_SHORT_SHA on $CI_RUNNER_DESCRIPTION"
look-around:
image: docker:29-cli@sha256:2357639ad1ea7d0194134bf3292622fd4e562483966ed674e5d7c945b5f3b28d
tags: [socket]
rules:
- if: $SOCKET_DEMO == "yes"
script:
- docker ps --format '{{.Names}}' | sort
- docker inspect lab-gitlab --format '{{range .Config.Env}}{{println .}}{{end}}' | grep ROOT_PASSWORD | sed -E 's/=(.{4}).*/=\1... (readable)/'
ubuntu@secopslog-cicd:~/lab/runnerhard · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./gl.sh /runners/$(./gl.sh '/runners/all?type=instance_type' | jq -r '[.[] | select(.description=="lab-runner")][0].id') -X PUT -d access_level=ref_protected | jq -c '{description, access_level}'
{"description":"lab-runner","access_level":"ref_protected"}
$ ./seed.sh app app/ && ./pipeline.sh app/ | grep -E "^(--- job|built|pipeline)"
pushed app: 192d97c first commit --- job build (test): success built 192d97cb on lab-runner pipeline #1: success
$ echo "# change" >> app/README && ./commit.sh app/ "feature work" feature/x && PIPE_WAIT=30 ./pipeline.sh app/ | grep -E "^(--- job|pipeline)"; exit ${PIPESTATUS[0]}
pushed: 2490932 feature work to feature/x --- job build (test): pending pipeline #2 still pending after 30s

main built on lab-runner. The feature branch job is pending: no runner may take it. That's the intended state, and the fix isn't to unprotect the runner. Untrusted work gets its own runner:

ubuntu@secopslog-cicd:~/lab/runnerhard · GitLab 19.5.3
$ ./runner.sh untrusted,docker untrusted-runner && ./gl.sh /runners/$(./gl.sh "/runners/all?type=instance_type" | jq -r "[.[] | select(.description==\"untrusted-runner\")][0].id") -X PUT -d run_untagged=true -o /dev/null && ./pipeline.sh app/ | grep -E "^(--- job|built|pipeline)"
runner untrusted-runner: online (tags: untrusted,docker) --- job build (test): success built 24909320 on untrusted-runner pipeline #2: success

Now feature work runs on untrusted-runner, and the protected runner still serves only protected refs. In production these are different machines, or different Kubernetes namespaces, with different network access: the protected runner can reach production, and the untrusted one can't, not even the cloud metadata address 169.254.169.254, which hands out the machine's cloud credentials. The untrusted step also set run_untagged=true on the new runner, because the lab's jobs have no tags and a runner takes untagged jobs only when told to.

Watch out
"Protected" is decided per project. lab-runner is an instance runner, so with ref_protected it still takes protected-branch jobs from every project on the instance, including forks and personal projects, whose owners are Maintainers of their own main. A runner that can reach production must be a project or group runner, assigned only to the projects that deploy. Keep instance runners for untrusted work.

A job with the Docker socket

Jobs that build container images often get the host's Docker socket mounted, because it's the quickest way to make docker build work inside a job. Register a runner that does that, and run a job on it that only looks around:

ubuntu@secopslog-cicd:~/lab/runnerhard · GitLab 19.5.3
$ ./runner.sh socket socket-runner --docker-volumes /var/run/docker.sock:/var/run/docker.sock
runner socket-runner: online (tags: socket)
$ git -C app checkout -q main && ./run-pipeline.sh app SOCKET_DEMO=yes >/dev/null && sleep 3 && ./pipeline.sh app/ | sed -n "/--- job look-around/,\$p"
--- job look-around (test): success $ docker ps --format '{{.Names}}' | sort lab-gitlab lab-gitlab-runner runner-ycp0y-7lg-project-1-concurrent-0-1194a5d16a8d303a-build $ docker inspect lab-gitlab --format '{{range .Config.Env}}{{println .}}{{end}}' | grep ROOT_PASSWORD | sed -E 's/=(.{4}).*/=\1... (readable)/' GITLAB_ROOT_PASSWORD=c849... (readable) pipeline #3: success

The job saw every container on the host, including GitLab itself, and read GitLab's root password from that container's configuration. Through the socket it could also start a privileged container that mounts the host's disk. A Docker socket in a job is root on the runner's host, and so is everything else on that host. Build images with tools that need no daemon, such as BuildKit in rootless mode or Buildah, or on a dedicated, disposable runner that holds nothing else.

The lab cuts a corner here on purpose. With the Docker executor, the runner manager itself needs a Docker API, so it holds the socket, and in this lab GitLab runs on the same Docker host. Any job that escapes is therefore a GitLab compromise. In production, a runner host runs only the runner, and never on the GitLab server.

Watch out
privileged = true in a runner's config is the same decision in another form: a privileged job container can reach the host. Docker-in-Docker setups often turn it on. Treat any runner with the socket or privileged mode as belonging to whoever can push a job to it.

Refuse images the runner was not set up for

allowed_images limits which images a runner will start for jobs. Add it to the socket runner, allowing only the lab's Python image:

ubuntu@secopslog-cicd:~/lab/runnerhard · GitLab 19.5.3
$ docker compose exec -T runner sed -i '/name = "socket-runner"/,/\[runners.docker\]/ s/\[runners.docker\]/[runners.docker]\n allowed_images = ["secopslog-py:*"]/' /etc/gitlab-runner/config.toml && docker compose exec -T runner grep -A1 -F '[runners.docker]' /etc/gitlab-runner/config.toml | grep allowed
allowed_images = ["secopslog-py:*"]
$ sleep 5; ./run-pipeline.sh app SOCKET_DEMO=yes >/dev/null && sleep 3 && ./pipeline.sh app/ | sed -n "/--- job look-around/,\$p"; exit ${PIPESTATUS[0]}
--- job look-around (test): failed ERROR: The "docker:29-cli@sha256:2357639ad1ea7d0194134bf3292622fd4e562483966ed674e5d7c945b5f3b28d" image is not present on list of allowed images: - secopslog-py:* Please check runner's allowed_images configuration: https://docs.gitlab.com/runner/configuration/advanced-configuration/ ERROR: Failed to remove network for build ERROR: Preparation failed: disallowed image Will be retried in 3s ... ... ERROR: Job failed (system failure): disallowed image pipeline #4: failed

The runner refused the job before starting any container, with disallowed image. The full log shows the same refusal three times, each followed by Will be retried in 3s, because the runner retries preparation errors. Then the job ended as a system failure. allowed_images is a guard against mistakes and casual misuse. It isn't a sandbox, because an allowed image can still run any command the job script gives it. It also matches names only: on a daemon shared with a socket job, anyone can docker tag another image as secopslog-py:3.15. On shared runners, set pull_policy = ["always"] so the name is resolved from the registry every time.

ControlSet inStops
Protected runner (access_level)GitLab, per runnerUnprotected refs using a runner meant for releases
Separate runners per trust levelInfrastructureFeature work sharing a machine with production access
No socket, no privileged modeconfig.tomlJobs taking over the runner host
allowed_images, allowed_servicesconfig.tomlJobs choosing arbitrary images on that runner

Hands-on

Unprotect lab-runner again with access_level=not_protected, pause untrusted-runner with paused=true (both through ./gl.sh /runners/ID -X PUT -d ...; find a runner's ID with ./gl.sh '/runners/all?type=instance_type' | jq '.[] | {id, description}'). Then switch to the branch with git -C app checkout feature/x, change a file (echo '# more' >> app/README), and push with ./commit.sh app/ "more feature work" and ./pipeline.sh app/. Which runner takes the job now? Then think through the change: what would a feature branch job on lab-runner be able to reach in a real setup? Finish with ./down.sh.

What you should see: the API returns "access_level":"not_protected" for lab-runner and "paused":true for untrusted-runner, and the push to feature/x builds on lab-runner. The feature branch job now runs on the runner that also takes main's jobs. In a real setup that means the same host, cache and network as your release jobs, and whatever credentials sit on that host. Unprotecting a runner is a quick way to unblock a branch, and it removes the split this lesson built.

Remember

Separate runners by trust, give release work project or group runners that are also protected, and never give a shared runner the Docker socket or privileged mode.

Quick check
01A runner has access_level: ref_protected. A Developer pushes feature/x. What happens to its jobs that match this runner's tags?
Incorrect — A protected runner does not take jobs from unprotected refs at all.
Correct — This runner only takes jobs from protected refs. The lab's feature job stayed pending until an untrusted runner appeared.
Incorrect — They are not refused with an error. They wait.
Incorrect — There is no approval step for runners.
02A team mounts /var/run/docker.sock into job containers so builds can call docker build. What have they given every job on that runner?
Incorrect — The socket is the host's Docker API, not a registry.
Incorrect — The Docker API can run containers, including privileged ones.
Incorrect — It reaches every container and the host itself.
Correct — That includes root on the machine. The lab job listed GitLab's container and read its root password.
03What does allowed_images in a runner's config protect against?
Correct — The runner refused docker:29-cli before starting it.
Incorrect — An allowed image can run any script. It is not a sandbox.
Incorrect — Scanning finds those. allowed_images only matches names.
Incorrect — Transport security is a registry setting.

Related