Sub topic 5
What goes wrong when a runner is reused
This chapter assumes you can read a GitHub Actions workflow and use a Linux shell. It also assumes you know that a self-hosted runner is a machine you register with GitHub to run your jobs. A persistent runner is one that stays registered and takes job after job without being rebuilt. That is the default when you run ./run.sh on a server. The sections below cover what that default costs you.
The runner is a shared machine
A GitHub-hosted runner is a fresh virtual machine for each job. A persistent self-hosted runner is not. Every job runs as the same operating system user, in the same work directory, on the same disk. Anything one job leaves behind is visible to the next job.
How state leaks between jobs
Four kinds of leftovers cause most problems.
- Files outside the workspace. Jobs can write to
$HOME,/tmp, and tool caches. Package manager caches, Docker layers, and build outputs persist. A later job may read them. - Credentials on disk. Tools write secrets to files.
docker loginwrites~/.docker/config.json. Cloud CLIs write to~/.awsor~/.config. Git credential helpers can store tokens. If a job does not remove them, the next job can read them. - Background processes. A step can start a process that outlives the job. It keeps running with the runner's privileges and can watch or alter later jobs.
- Modified tools and configuration. A job can change
PATHentries, shell profiles, installed packages, or files in~/.ssh. Later jobs run with those changes.
The runner masks secrets in logs, but only for values it knows about. Secrets copied to disk, transformed, or read by another job are not protected.
Two kinds of failure
Reuse causes two different problems, and they need different fixes.
| Problem | What happens | Example |
|---|---|---|
| Reliability | A job passes or fails depending on what ran before it | A stale build directory hides a missing file. A full disk breaks the next job. |
| Security | A job reads or alters what another job left behind | A pull request job reads a deploy token that a previous release job wrote to disk. |
The reliability problem is the one you notice first. Builds become hard to reproduce, and "works on that runner" becomes a common phrase. The security problem is quieter. It only shows up when someone abuses it.
A concrete attack path
Suppose a persistent runner serves both a release workflow and a pull request workflow. The release job logs in to a registry. The login leaves a credentials file on disk.
Later, a pull request job runs. If the pull request changes a script that the workflow executes, that script runs on the runner. It can read ~/.docker/config.json and send the contents elsewhere. It can also plant a file in ~/.bashrc or start a hidden process, so that the next release job runs attacker-controlled code with release credentials.
A single compromised job is therefore enough to compromise every job that follows. The runner also usually has network access to internal systems, so the damage can extend past GitHub.
Cleanup scripts are not a fix
A common response is to add a cleanup step, for example:
rm -rf "$GITHUB_WORKSPACE"/* ~/.docker ~/.aws /tmp/*
docker system prune -afThis helps with reliability but fails as a security measure, for three reasons.
- You must list every place a job can write. You will miss some.
- The cleanup runs inside the same compromised environment. Malicious code can skip it, tamper with it, or re-plant files afterwards.
- If the job crashes or the runner loses power, the cleanup never runs.
The reliable fix is to not reuse the machine at all. The later chapters on ephemeral runners cover that.
Exercise
A persistent runner runs a release job that executes docker login, then runs a pull request job. Name the file the second job might read, and explain why a cleanup step at the end of the release job is not a dependable defense.
Show answer
The second job can read ~/.docker/config.json, which holds the registry credentials. A cleanup step must know every path a job wrote, it can be skipped or tampered with by code running in the same environment, and it does not run if the job crashes or the host fails. Using a fresh machine for each job avoids all three problems.