Subscribe

Securing GitLab runners: protected runners, tags and the Docker socket

Anyone who can push a branch can run code on your GitLab runner. How protected runners, tags and protected variables split jobs by trust level, and a demo of why mounting docker.sock into a job is root on the host.

Securing GitLab runners: protected runners, tags and the Docker socket

A GitLab runner runs whatever is in .gitlab-ci.yml. Anyone who can push a branch can change that file, so anyone with the Developer role can run code on your runner host. GitLab's own runner security page says it plainly: pipelines are a remote code execution service. In this blog we will look at the settings that decide which jobs land on which runner, what protected runners and protected variables really protect, and why mounting /var/run/docker.sock into jobs hands out root on the host.

Level is advanced. You should already have a self-managed runner, for example from installing a GitLab Runner on EC2. Versions: GitLab 19.6 docs, GitLab Runner 19.5, Docker 29.10 images. I have no GitLab instance for this post, so the runner settings are checked against the docs and parsed as TOML, the pipeline was checked with gitlab-ci-local 4.77.1, and the Docker socket part I really ran, on my Mac in OrbStack with throwaway containers.

Who can run code on your runner

Think about it from the job's side. A job gets:

  • the repo, including anything a developer just pushed
  • every CI/CD variable the pipeline is allowed to see
  • CI_JOB_TOKEN, which can call the GitLab API as that job
  • whatever the executor gives it on the host (a container, a shell, a privileged container, a socket)

The first three come from GitLab. The last one comes from your config.toml, and that's where most of the damage happens.

The Docker socket is root on the host

People mount the socket to make docker build work inside jobs without privileged mode. GitLab's docs even list it as an option (socket binding) and then warn that it shares the host's daemon and can lead to container breakout. Let me show what that basically means.

I ran a fake "job" container with the socket mounted. First, a fake "other job" with a token in its environment, as a CI variable would be:

docker run -d --rm --name w2b2-otherjob -e DEPLOY_TOKEN=not-a-real-token-123 debian:trixie-slim sleep 60

Then the job:

docker run --rm --name w2b2-job -v /var/run/docker.sock:/var/run/docker.sock \
  --entrypoint sh docker:29.10.0-dind -c '
  echo "== containers this job can see on the host:"; docker ps -q | wc -l
  echo "== another job'\''s environment:"
  docker inspect w2b2-otherjob --format "{{range .Config.Env}}{{println .}}{{end}}" | grep TOKEN
  echo "== a sibling container with the host root mounted (read-only here):"
  docker run --rm --name w2b2-escape -v /:/host:ro debian:trixie-slim sh -c \
    "id; grep PRETTY /host/etc/os-release; cut -d: -f1 /host/etc/shadow | head -3"
'
== containers this job can see on the host:
4
== another job's environment:
DEPLOY_TOKEN=not-a-real-token-123
== a sibling container with the host root mounted (read-only here):
uid=0(root) gid=0(root) groups=0(root)
PRETTY_NAME="OrbStack"
root
bin
daemon

Three lines of shell and the job read another container's secrets and the host's /etc/shadow. On my Mac the "host" is OrbStack's Linux VM, that's why it says OrbStack. On a runner EC2 instance it would be the instance itself. I mounted / read-only because I didn't want to break anything. Drop the :ro and the job can write to /etc, add an SSH key, read the runner's config.toml with every runner token in it. That last one is the nasty part. With a runner's token someone can clone the runner and pick up jobs that were meant for it.

Honestly, this one surprised me less than it should have. The socket isn't a "docker build" permission. It is the Docker API, and the Docker API can start containers with any mount, any capability and --privileged. There is no flag that makes the socket safe.

So the rule I follow: no /var/run/docker.sock in volumes on any runner that runs code from people you don't fully trust. For image builds use one of the rootless builders instead (BuildKit rootless or Buildah), or keep DinD on its own isolated runner.

Privileged mode is the same problem

privileged = true gives the job container all capabilities and the host's devices. The runner security docs say a job could gain full root on the host with it. If you need it for Docker-in-Docker, the runner can limit which containers get it:

[runners.docker]
  privileged = false
  services_privileged = true
  allowed_privileged_services = ["docker:*-dind", "docker.io/library/docker:*-dind"]

The job's own container stays unprivileged and only the dind service get privileged. It's better. It is still a privileged container on that host, so that runner belongs on its own machine, ideally one that's thrown away after each job.

One runner per trust level

This is actually the main idea of the post. Don't have one runner that does everything. I'd split it in three:

  1. general: runs any branch, merge requests, forks. No secrets, never privileged, no socket.
  2. protected-build: only protected branches and tags. Builds and pushes images.
  3. protected-deploy: only protected branches and tags, only jobs tagged deploy. The only place a job can reach production.

Each on its own host, because runners on one host share that host's Docker daemon. Here is the config.toml side:

concurrent = 4

# Level 1: any branch, merge requests, forks. No secrets, nothing privileged.
[[runners]]
  name = "general"
  url = "https://gitlab.com/"
  token = "glrt-GENERAL_PLACEHOLDER"
  executor = "docker"
  [runners.docker]
    image = "alpine:3.24"
    privileged = false
    volumes = ["/cache"]
    pull_policy = ["always"]
    allowed_pull_policies = ["always"]
    security_opt = ["no-new-privileges:true"]

# Level 2: protected branches and tags only, builds and pushes images.
[[runners]]
  name = "protected-build"
  url = "https://gitlab.com/"
  token = "glrt-BUILD_PLACEHOLDER"
  executor = "docker"
  [runners.docker]
    image = "alpine:3.24"
    privileged = false
    security_opt = ["seccomp:unconfined"]
    allowed_images = ["moby/buildkit:*-rootless", "golang:*", "alpine:*"]
    volumes = ["/cache"]
    pull_policy = ["always"]

# Level 3: deploys. The only runner whose jobs may assume the deploy role in AWS.
[[runners]]
  name = "protected-deploy"
  url = "https://gitlab.com/"
  token = "glrt-DEPLOY_PLACEHOLDER"
  executor = "docker"
  [runners.docker]
    image = "amazon/aws-cli:2.41.1"
    privileged = false
    allowed_images = ["amazon/aws-cli:*"]
    volumes = ["/cache"]
    pull_policy = ["always"]
    security_opt = ["no-new-privileges:true"]

A few notes. pull_policy = ["always"] matters on shared runners: with if-not-present, a private image pulled by one project stays on the host and another project can use it without having the registry credentials. The docs call this out. allowed_images stops a job from asking for some random image on the deploy runner. The seccomp:unconfined on the build runner is what rootless BuildKit needs to create its user namespace, I hit fork/exec /proc/self/exe: operation not permitted without it. It's a hole, but a much smaller one than privileged.

I parsed this with Python's tomllib to catch typos. That only proves it is valid TOML, not that the runner accepts every key. The key names come from the runner's advanced configuration page.

Protected and tags live in GitLab, not in config.toml

This tripped me up while reading the docs itself. With the new runner workflow (runner authentication tokens that start with glrt-), things like "protected", tags and "run untagged jobs" are set when you create the runner in GitLab, not when you register it. The old --access-level and --tag-list flags on gitlab-runner register belong to the deprecated registration token flow.

In the UI it's the runner's settings page: tick Protected, set the tags, untick Run untagged jobs. With the API it's POST /user/runners:

curl --request POST "https://gitlab.com/api/v4/user/runners" \
  --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
  --data "runner_type=project_type" \
  --data "project_id=12345678" \
  --data "description=protected-deploy" \
  --data "access_level=ref_protected" \
  --data "tag_list=deploy" \
  --data "run_untagged=false"

The response has the glrt- token. Copy it then and there only, it is not shown again. I didn't run this one, there was no GitLab to send it to.

A protected runner only picks up jobs from pipelines on protected branches or protected tags. From GitLab 18.1 there's a project setting that lets merge request pipelines use protected runners and variables, but only when both branches are protected, they are in the same project and the user can push to the target. MRs from forks never get them. Leave that setting off unless you have a reason.

The pipeline side

stages: [test, build, deploy]

test:
  stage: test
  image: golang:1.27.4
  script:
    - go test -race -count=1 ./...

build:
  stage: build
  tags: [build]
  image:
    name: moby/buildkit:v0.35.0-rootless
    entrypoint: [""]
  variables:
    BUILDKITD_FLAGS: --oci-worker-no-process-sandbox
  rules:
    - if: $CI_COMMIT_REF_PROTECTED == "true"
  before_script:
    - mkdir -p ~/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > ~/.docker/config.json
  script:
    - buildctl-daemonless.sh build --frontend dockerfile.v0 --local context=. --local dockerfile=.
        --output type=image,name=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA,push=true

deploy:
  stage: deploy
  tags: [deploy]
  image:
    name: amazon/aws-cli:2.41.1
    entrypoint: [""]
  environment: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_REF_PROTECTED == "true"
  script:
    - aws sts get-caller-identity

test has no tags, so it goes to the general runner. build and deploy carry tags that only the protected runners have. The rules are there so a feature branch pipeline doesn't create jobs that sit in "pending" forever waiting for a runner that will never take them. With gitlab-ci-local, main listed all three jobs and a feature branch listed only test, as expected.

Remember tags are for routing. Anyone can write tags: [deploy] in their branch. What stops them is the protected flag on the runner. Tags only make sure the right jobs land on it.

Protected and masked variables

Same idea for secrets. A protected variable is only passed to pipelines on protected branches and tags. Masked hides the value in job logs. Masked and hidden also hides it in the settings UI after you save it.

One thing to be clear about, masking is not a security boundary. The variables docs say masking is not a guaranteed way to keep a value secret, and a base64 in the script is enough to get around it. Protection is the real control, together with who may push to protected branches. Anything that can deploy should be protected. Better still, don't store cloud keys at all and use OIDC with id_tokens.

My checklist

  • No docker.sock in volumes, anywhere untrusted code runs.
  • privileged = false everywhere, except one isolated runner if you truly need DinD.
  • Runner per trust level, each on its own host.
  • Deploy runners: protected, tagged, "run untagged" off.
  • Deploy secrets: protected variables, or no secrets at all.
  • pull_policy = ["always"] on shared runners.
  • Protected branches with a short list of who can push and merge.

If you're on GitHub too, securing GitHub Actions workflows covers the same ideas there, and the side by side post maps the terms.

Start with the socket though. If you find one line with docker.sock in a runner config today, that is the one to fix first.