Runner isolation
Protected runners, the Docker socket problem, and allowed images.
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: beaba58081b090818e5649456fc95222d5cad5a31e17d5bfbba5f9100d33337bProtected 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:
# A normal job, and a job that asks for the Docker CLI on a runner tagged "socket".build:image: secopslog-py:3.15script:- echo "built $CI_COMMIT_SHORT_SHA on $CI_RUNNER_DESCRIPTION"look-around:image: docker:29-cli@sha256:2357639ad1ea7d0194134bf3292622fd4e562483966ed674e5d7c945b5f3b28dtags: [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)/'
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:
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.
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:
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.
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:
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.
| Control | Set in | Stops |
|---|---|---|
Protected runner (access_level) | GitLab, per runner | Unprotected refs using a runner meant for releases |
| Separate runners per trust level | Infrastructure | Feature work sharing a machine with production access |
| No socket, no privileged mode | config.toml | Jobs taking over the runner host |
allowed_images, allowed_services | config.toml | Jobs 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.
access_level: ref_protected. A Developer pushes feature/x. What happens to its jobs that match this runner's tags?/var/run/docker.sock into job containers so builds can call docker build. What have they given every job on that runner?allowed_images in a runner's config protect against?docker:29-cli before starting it.allowed_images only matches names.