GitLab CI runner can read and poison cache of all other projects
The GitLab CI runner's cache download and upload used a user-controlled cache key without sanitisation, allowing path traversal to read or overwrite other projects' cached files. An attacker could poison a victim's cache to execute arbitrary code in their CI pipeline.
- Triaged in 6 days
- Fixed in 6 days
- Paid in 6 days
- Disclosed after 4 months
The GitLab CI runner allows users to cache files and directories in between runs. These files are stored in a ZIP file and uploaded to a shared cache instance. In my testing, the files were uploaded to runners-cache-4-internal.gitlab.com and runners-cache-3-internal.gitlab.com, even for dedicated runners. It seems odd that dedicated runners use the same shared cache, but perhaps that was an intentional design decision. It could also be a vulnerability. I tried reaching the cache servers from a Docker instance itself, but wasn't able to (I tried from a reverse shell spawned from a Docker instance). There are multiple vulnerabilities (same root cause though) that can be chained to successfully poison the CI runner cache of another project.
Reading the cache of other projects
Create a new project with a .gitlab-ci.yml file in it. The file should contain the following contents. By default, when a cache file is downloaded, it'll download the cache from http://runners-cache-4-internal.gitlab.com:444/runner/project/5024150/cache.
.gitlab-ci.yml
a:
script:
- ls -lashR
cache:
key: ../1/cache
policy: pull
paths:
- .
To read the cache, the attacker needs to know two things: a project ID (auto incremental) and a cache key. By default, the project ID will be prepended to download the cache. But because it's an HTTP request and there's no additional checks on the key input, a path traversal vulnerability can be exploited to move up a directory and select the cache from a different project. In this case, when it downloads the cache, it'll request http://runners-cache-4-internal.gitlab.com:444/runner/gitlab/project/1/cache instead of the project ID of the build.
Build output
[0KRunning with gitlab-runner 10.3.0 (5cf5e19a)
on docker-auto-scale (e11ae361)
[0;m[0KUsing Docker executor with image ruby:2.1 ...
[0;m[0KUsing docker image sha256:4eadb9b5cb46f487a71d05717762679404f7f6fdec1ba4fa96304de1db07dfef for predefined container...
[0;m[0KPulling docker image ruby:2.1 ...
[0;m[0KUsing docker image ruby:2.1 ID=sha256:223d1eaa9523fa64e78f5a92b701c9c11cbc507f0ff62246dbbacdae395ffea3 for build container...
[0;msection_start:1514659811:prepare_script
[0KRunning on runner-e11ae361-project-4989754-concurrent-0 via runner-e11ae361-srm-1514658950-a15d8859...
section_end:1514659812:prepare_script
[0Ksection_start:1514659813:get_sources
[0K[32;1mCloning repository...[0;m
Cloning into '/builds/jobertabma/build-test'...
[32;1mChecking out e01918e5 as master...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1514659814:get_sources
[0Ksection_start:1514659814:restore_cache
[0K[32;1mChecking cache for ../13083/ruby-235-with-yarn...[0;m
Downloading cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/13083/ruby-235-with-yarn[0;m
[32;1mSuccessfully extracted cache[0;m
section_end:1514659844:restore_cache
The cache key seems to be guessable pretty easily or even unused when no key is specified, since most will correlate with the step they're executed in. When I started looking at this, I had to specify which paths to download from the cache. This made exploitation more difficult. However, it (conveniently) allowed me to use . as path, extracting all files from the cache into the working directory. Running ls -lashR after that reveals the cache contents in the build output. Files can be read using cat or to store them as build artifacts through the .gitlab-ci.yml.
Writing the cache of other projects Now that the attacker knows what files are stored in the cache, it can poison the cache with its own file contents. Create another CI YAML file with the following contents:
.gitlab-ci.yml
a:
script:
- echo 1 > file-to-poison
cache:
key: ../1/cache
policy: push
paths:
- file-to-poison
The attacker has to run a build, which will overwrite the file-to-poison file in the cache for project ID 1. Now, when the targeted project starts another CI run, the poisoned cache files will be downloaded and used in the CI run. For example, an attacker could poison 13083/ruby-235-with-yarn, which would overwrite the Ruby 2.3.5 executable that is being used for GitLab CE CI runs. As you can imagine, someone could enumerate over other projects that use cached executables and overwrite them with their own code.
This has been tested against the latest version of GitLab.
Impact
Depending on the files that are cached, this may allow an attacker to run arbitrary code on a victim's Docker instance running a CI run. This may expose confidential data, inject artifacts in a build pipeline to ship additional code, among other things.
Context
GitLab CI runners let a job cache files and directories between runs. The runner packs the cached paths into a ZIP file and uploads it to a shared cache instance, then downloads it again on later runs that use the same cache key. The cache is meant to speed up builds by carrying dependencies, compiled artefacts or installed executables from one run to the next.
In the researcher's testing, the cache files were uploaded to runners-cache-4-internal.gitlab.com and runners-cache-3-internal.gitlab.com, even for dedicated runners. The report notes that it seems odd for dedicated runners to use the same shared cache, though it may have been an intentional design decision, and adds that it could also be a vulnerability in itself. The researcher tried reaching the cache servers from a Docker instance directly, including from a reverse shell spawned inside a Docker instance, but was not able to.
The report describes several vulnerabilities that share one root cause and can be chained to poison the CI runner cache of another project.
Setup
The precondition is the ability to create a project and run CI jobs in it. The attacker needs two pieces of information about the target: a project ID and a cache key. Project IDs are auto-incremental, so they are easy to enumerate. Cache keys tend to be guessable, or even unused when no key is specified, because most correlate with the step they are executed in.
The researcher tested against the latest version of GitLab at the time. The build output shown in the report comes from gitlab-runner 10.3.0 (5cf5e19a) on docker-auto-scale (e11ae361), using the Docker executor with the ruby:2.1 image.
Finding it
The cache download is an HTTP request, and the report says there are no additional checks on the key input. By default, when a cache file is downloaded, the runner fetches it from a URL of the form http://runners-cache-4-internal.gitlab.com:444/runner/project/5024150/cache, where the project ID is prepended to the path. Because the key is attacker-controlled and unchecked, a path traversal in the key can move up a directory and select the cache of a different project.
Exploitation
Step 1: read the cache of another project
Create a new project containing a .gitlab-ci.yml with the following contents:
# .gitlab-ci.yml
a:
script:
- ls -lashR
cache:
key: ../1/cache
policy: pull
paths:
- .
The key is ../1/cache, which traverses out of the current project's directory and points at the cache of project ID 1. Using . as the path allowed extracting all files from the cache into the working directory, and the script then lists the extracted tree recursively with ls -lashR, so the cache contents appear in the build output.
With this key, the download request becomes http://runners-cache-4-internal.gitlab.com:444/runner/gitlab/project/1/cache instead of a URL containing the attacker's own project ID.
The build output shows the traversal working against a different key, ../13083/ruby-235-with-yarn:
[0KRunning with gitlab-runner 10.3.0 (5cf5e19a)
on docker-auto-scale (e11ae361)
[0;m[0KUsing Docker executor with image ruby:2.1 ...
[0;m[0KUsing docker image sha256:[redacted] for predefined container...
[0;m[0KPulling docker image ruby:2.1 ...
[0;m[0KUsing docker image ruby:2.1 ID=sha256:[redacted] for build container...
[0;msection_start:1514659811:prepare_script
[0KRunning on runner-e11ae361-project-4989754-concurrent-0 via runner-e11ae361-srm-1514658950-a15d8859...
section_end:1514659812:prepare_script
[0Ksection_start:1514659813:get_sources
[0K[32;1mCloning repository...[0;m
Cloning into '/builds/jobertabma/build-test'...
[32;1mChecking out e01918e5 as master...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1514659814:get_sources
[0Ksection_start:1514659814:restore_cache
[0K[32;1mChecking cache for ../13083/ruby-235-with-yarn...[0;m
Downloading cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/13083/ruby-235-with-yarn[0;m
[32;1mSuccessfully extracted cache[0;m
section_end:1514659844:restore_cache
The restore_cache section confirms the traversal: the runner checks the cache for ../13083/ruby-235-with-yarn and downloads cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/13083/ruby-235-with-yarn, then extracts it successfully. Note that the cache host here is runners-cache-5-internal.gitlab.com, a different instance from the one in the earlier example.
Once the cache is extracted, files can be read with cat, or stored as build artefacts through the .gitlab-ci.yml.
Step 2: write the cache of another project
Knowing what files are stored in the cache, the attacker can poison it with their own contents. Create another CI YAML file with the following contents:
# .gitlab-ci.yml
a:
script:
- echo 1 > file-to-poison
cache:
key: ../1/cache
policy: push
paths:
- file-to-poison
Here the key is again ../1/cache, but the policy is push, so the job uploads a cache instead of downloading one. The script writes 1 into file-to-poison, and paths lists that file as the only cached path. Running the build overwrites file-to-poison in the cache for project ID 1.
When the targeted project starts another CI run, the poisoned cache files are downloaded and used in that run. For example, an attacker could poison 13083/ruby-235-with-yarn, which would overwrite the Ruby 2.3.5 executable used for GitLab CE CI runs. The researcher notes that someone could enumerate over other projects that use cached executables and overwrite them with their own code.
Root cause
The cache key is attacker-controlled and is concatenated into the cache URL path without validation, so ../ sequences in the key traverse out of the project's own cache directory and reach another project's cache. In the researcher's testing, caches were uploaded to shared cache hosts even for dedicated runners, which the report says seemed odd and may have been intentional. The report states there are no additional checks on the key input. The report does not describe any server-side normalisation or authorisation check that would have stopped the traversal.
Impact
Depending on the files that are cached, an attacker may be able to run arbitrary code on a victim's Docker instance running a CI run. This may expose confidential data and allow artefacts to be injected into a build pipeline to ship additional code, among other things. The impact depends on what the target project caches: poisoning a cached executable, as with the Ruby 2.3.5 example, turns the next CI run into code execution.
Fix
The report does not describe a fix or a change made in response. What it implies is needed: validate or normalise the cache key so that traversal sequences cannot escape the project's own cache namespace, and ensure the cache lookup is scoped to the requesting project rather than resolved purely from the path.
Variants to test
- Traverse with a different number of
../segments in thekeyto reach sibling or parent cache namespaces, and check therestore_cacheline in the build output for a URL pointing outside your project ID. - Use
policy: pullwithpaths: .andls -lashRto dump another project's cache, then look for credentials, tokens or executables in the output. - Use
policy: pushwith a key pointing at another project's cache and a single file inpaths, then confirm the target's next run downloads your content. - Target cache keys that match common step names or tool versions, since the report says keys tend to correlate with the step they run in and are easy to guess.
- Try a job with no cache key at all, since the report says the key may be unused when none is specified.
Hunting notes
This kind of bug hides in shared infrastructure that is addressed by a path built from user-controlled input: cache servers, artefact stores, package registries and any per-project storage reached over a URL. During recon, look for CI configuration options that let you set a cache key, an artefact path or a storage location, and for build logs that print the full download URL. A positive signal is a URL whose path contains your project identifier followed by your key, which tells you exactly where to try traversal. The researcher showed reading another project's cache and described how an attacker could poison a cached executable used by GitLab CE CI runs, which could turn a path traversal into code execution on a victim's CI container.
What happened, in plain words
GitLab is a website where teams keep their code and where a robot called a CI runner automatically builds and tests that code every time someone changes it. Building software from scratch is slow, so the runner keeps a box of useful leftovers from the previous build (downloaded libraries, compiled programs, installed tools) and reuses it next time. That box is called the cache.
The cache is stored on a separate server shared by many projects. The runner finds the right box by building a web address out of two things: the project's ID number and a short label called the cache key. The key is dropped into that address, and the report says there are no additional checks on the key input, so a key containing ../ can climb out of the project's own folder and land in another project's folder. That means one user can read another project's cache, and then overwrite files in it. If the other project caches a program it later runs, the attacker's file gets executed on the victim's build machine.
This is a path traversal: the same trick as typing ../ in a file browser to move up a folder, except here the website trusts the text you give it.
Background you need first
A few ideas come up again and again, so here they are from zero.
- GitLab: a website for storing code and collaborating on it. Each piece of work lives in a project, and every project gets a number, its project ID. IDs are handed out in order (1, 2, 3, ...), so they are easy to guess.
- CI (continuous integration): an automatic process that runs whenever code changes. It builds the software and runs tests, so mistakes are caught early.
- Runner: the robot that actually performs those automatic jobs. It starts a fresh, empty computer (a Docker container, think of a disposable virtual machine) and runs the steps written in a configuration file.
.gitlab-ci.yml: the instruction file at the root of a project. It lists the jobs and, for each one, the commands to run (script), what to keep between runs (cache) and other settings.- Cache: the leftovers kept between runs so the next build is faster. The runner packs the chosen files into a ZIP archive and uploads it to a shared cache server, then downloads it again on later runs.
- Cache key: the label that says which cache belongs to which job. The runner turns it into part of a web address.
- HTTP request: the message a browser or program sends to a server to ask for something. The address it asks for is the URL.
- Path traversal: a bug where a program builds a file location from text supplied by a user, and the user includes
../("go up one folder") to escape the intended place. Compare it to a hotel clerk who is told "room 12" and will also accept "room 12, then up the stairs, then into room 5" without checking. - Reverse shell: a way for an attacker who already runs code on a machine to open a connection back to themselves and type commands. The researcher tried one here and it did not help.
The setup
The researcher needed an account able to create a project and run CI jobs. To aim at a victim, two facts are needed: the victim's project ID and the cache key the victim uses.
Project IDs are handed out in sequence, so they can simply be counted through. Cache keys are easy to guess, or sometimes not used at all when a job does not set one, because they usually match the name of the step the job performs.
The testing was done against the latest version of GitLab at the time. The build output in the report comes from gitlab-runner 10.3.0 (5cf5e19a) on docker-auto-scale (e11ae361), using the Docker executor with the ruby:2.1 image.
Finding the weakness
The researcher noticed two things. First, the runner fetches caches over HTTP. Second, the cache key is pasted straight into the address of that request, and the report says there are no additional checks on the key input.
By default, a cache download goes to an address shaped like this:
http://runners-cache-4-internal.gitlab.com:444/runner/project/5024150/cache
Read it from left to right: the server name, the port 444, then runner/project/, then the project ID (5024150 here), then the cache key (cache). Because the key is under the attacker's control and the report says there are no additional checks on it, a key containing ../ can step up out of the project's folder and point at a different project's cache.
In the researcher's testing, caches were uploaded to runners-cache-4-internal.gitlab.com and runners-cache-3-internal.gitlab.com, even for dedicated runners. The report says this seems odd, that it may have been an intentional design decision, and that it could also be a vulnerability in itself. The researcher also tried to reach the cache servers directly from inside a Docker instance, including from a reverse shell spawned there, but was not able to.
The report describes several vulnerabilities that share this one root cause and can be chained together to poison another project's cache.
Step 1: reading another project's cache
The first goal is simply to see what is inside someone else's cache. Create a new project and put this file in it:
# .gitlab-ci.yml
a:
script:
- ls -lashR
cache:
key: ../1/cache
policy: pull
paths:
- .
Line by line:
-
a:is the name of the job. The name is arbitrary. -
script:lists the commands the runner will execute. -
- ls -lashRruns thelscommand, which lists files. The flags make it show hidden files, sizes, permissions and recurse into every subfolder. The point is to print the contents of whatever ends up in the working directory into the build log, which the attacker can read. -
cache:starts the cache settings for this job. -
key: ../1/cacheis the important line.../means "go up one folder", so instead of staying inside the attacker's own project folder, the address climbs out and then enters the folder of project ID1. -
paths:lists which files the cache covers. -
- .was used as the path, which the report says allowed extracting all files from the cache into the working directory, ready forlsto list.
With this key, the download request becomes:
http://runners-cache-4-internal.gitlab.com:444/runner/gitlab/project/1/cache
Compare it with the default address above: the attacker's own project ID has been replaced by 1, because the ../ moved the path up and out. The server simply serves whatever sits at that address.
The build output in the report shows the same trick working with a different key, ../13083/ruby-235-with-yarn:
[0KRunning with gitlab-runner 10.3.0 (5cf5e19a)
on docker-auto-scale (e11ae361)
[0;m[0KUsing Docker executor with image ruby:2.1 ...
[0;m[0KUsing docker image sha256:[redacted] for predefined container...
[0;m[0KPulling docker image ruby:2.1 ...
[0;m[0KUsing docker image ruby:2.1 ID=sha256:[redacted] for build container...
[0;msection_start:1514659811:prepare_script
[0KRunning on runner-e11ae361-project-4989754-concurrent-0 via runner-e11ae361-srm-1514658950-a15d8859...
section_end:1514659812:prepare_script
[0Ksection_start:1514659813:get_sources
[0K[32;1mCloning repository...[0;m
Cloning into '/builds/jobertabma/build-test'...
[32;1mChecking out e01918e5 as master...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1514659814:get_sources
[0Ksection_start:1514659814:restore_cache
[0K[32;1mChecking cache for ../13083/ruby-235-with-yarn...[0;m
Downloading cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/13083/ruby-235-with-yarn[0;m
[32;1mSuccessfully extracted cache[0;m
section_end:1514659844:restore_cache
This is a log, so read it as a story of one build. The first lines say which runner version and Docker image were used. Then the runner clones the repository and checks out a commit. The part that matters is the restore_cache section near the end:
Checking cache for ../13083/ruby-235-with-yarnshows the key the job asked for, traversal and all.Downloading cache.zip from http://runners-cache-5-internal.gitlab.com:444/runner/project/13083/ruby-235-with-yarnshows the address the runner actually fetched. The../has been resolved, and the request now points at project13083rather than the attacker's project.Successfully extracted cacheconfirms the download worked and the ZIP was unpacked.
Notice that the cache host here is runners-cache-5-internal.gitlab.com, a different instance from the one in the earlier example. The odd characters such as [0K and [32;1m are colour and cursor codes that the log viewer normally hides.
Once the cache is unpacked, the attacker can read files with cat, or save them as build artefacts through the .gitlab-ci.yml so they can be downloaded afterwards.
Step 2: writing another project's cache
Reading is only half of it. Now that the attacker knows which files a project keeps in its cache, they can replace them. Create another CI file with these contents:
# .gitlab-ci.yml
a:
script:
- echo 1 > file-to-poison
cache:
key: ../1/cache
policy: push
paths:
- file-to-poison
Line by line:
-
a:is again just the job name. -
script:holds the commands. -
- echo 1 > file-to-poisonwrites the text1into a file calledfile-to-poison. The>means "put the output here, replacing whatever was there". -
cache:starts the cache settings. -
key: ../1/cacheis the same traversal as before, aiming at project ID1. -
paths:lists what to include in the uploaded cache. -
- file-to-poisonmeans only that one file is packed and sent.
Running this build overwrites file-to-poison in the cache belonging to project ID 1. The next time the victim project runs a CI job, it downloads that cache and uses the attacker's file as if it were its own.
The report gives a concrete example: an attacker could poison 13083/ruby-235-with-yarn, which would overwrite the Ruby 2.3.5 executable used for GitLab CE CI runs. In other words, the victim's build would run the attacker's program instead of the real Ruby interpreter. The researcher notes that someone could enumerate over other projects that use cached executables and overwrite them with their own code.
Why it works
Three things line up to make this possible:
- The cache key comes from the user and is glued into the URL path with no validation, so
../sequences escape the project's own cache folder. - In the researcher's testing, caches were uploaded to shared cache hosts even for dedicated runners, which the report says seemed odd and may have been intentional.
- The report states there are no additional checks on the
keyinput.
The report does not describe any server-side normalisation or authorisation check that would have stopped the traversal.
What an attacker could do with it
Depending on which files a project caches, an attacker may be able to run arbitrary code on a victim's Docker instance during a CI run. That can expose confidential data and allow artefacts to be injected into a build pipeline so that extra code ships with the software, among other things. The impact depends on what the target caches: poisoning a cached executable, as in the Ruby 2.3.5 example, turns the next CI run into code execution.
How it should have been fixed
The report does not describe a fix or a change made in response. What it implies is needed: validate or normalise the cache key so that traversal sequences cannot escape the project's own cache namespace, and make sure the cache lookup is scoped to the requesting project rather than resolved purely from the path.
Things worth trying if you hunt for this
- Change the number of
../segments in thekeyto reach neighbouring or parent cache namespaces, and check therestore_cacheline in the build output for a URL pointing outside your own project ID. - Use
policy: pullwithpaths: .andls -lashRto dump another project's cache, then look for credentials, tokens or executables in the output. - Use
policy: pushwith a key pointing at another project's cache and a single file inpaths, then confirm the target's next run downloads your content. - Target cache keys that match common step names or tool versions, since the report says keys tend to match the step they run in and are easy to guess.
- Try a job with no cache key at all, since the report says the key may be unused when none is specified.
Where bugs like this hide
While looking around, watch for CI settings that let you set a cache key, an artefact path or a storage location, and for build logs that print the full download address. A good sign is a URL whose path contains your project identifier followed by your key, because that tells you exactly where to try traversal. The researcher showed reading another project's cache and described how an attacker could poison a cached executable used by GitLab CE CI runs, which could turn a path traversal into code execution on a victim's CI container.
Glossary
- Artefact: a file produced by a build that is kept afterwards so people can download it.
- Cache: files kept between CI runs so the next build is faster.
- Cache key: the label that identifies which cache belongs to which job.
- CI (continuous integration): automatic building and testing of code whenever it changes.
- Container: a disposable, isolated computer environment in which a job runs.
- Docker: the software that creates and runs containers.
- Docker executor: the part of a GitLab runner that runs jobs inside Docker containers.
- GitLab: a website for hosting code and running CI jobs on it.
- HTTP: the plain, unencrypted protocol used to request web pages and files.
- Project: a single code repository on GitLab, with its own settings and CI jobs.
- Project ID: the number GitLab assigns to a project, handed out in sequence.
- Reverse shell: a connection opened from a compromised machine back to the attacker, giving them a command prompt.
- Runner: the machine or agent that executes CI jobs.
- URL: the address of something on a network, such as a web page or a file.
- YAML: a plain-text format for configuration files, used for
.gitlab-ci.yml. - ZIP: a common archive format that packs several files into one.