GitHub Cache Poisoning
Published on scribesecurity.com
Do you know what happens under the hood of your CI? Without deep understanding, you might be vulnerable to innovative supply chain attacks.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
GitHub Actions pipelines can use a shared cache to speed up runs. Instead of downloading or building the same dependencies on every run, a workflow stores files or directories under a string key and later retrieves them by that key. The cache is exposed through an API and is normally used through the actions/cache action.
This article describes an experiment in which a low-privilege workflow poisons that cache, and a higher-privilege workflow later consumes the poisoned entry. The author reports the issue to GitHub, which replied that the actions/cache action behaves as expected and that it has no plans to tighten the scoping of the cache. The same kind of logical weakness is said to exist in other CI products.
The attack is described in four steps:
- An attacker publishes a malicious tool or a GitHub Action which is picked up by an unsuspecting workflow in GitHub.
- The workflow is designed with a cache.
- The malicious payload modifies the cache to include malicious data.
- Other workflows that call on this cache from that point might be affected.
Setup
The experiment uses an example CI with two workflows:
- A unit-test workflow running unit-test and code coverage tools. The author assumes one of the tools is malicious or vulnerable to remote code execution. This workflow does need to use the
actions/cacheaction; any workflow can access the cache. - A release workflow that builds and releases the application artifact. This workflow uses a cache to optimise its use of the Golang dependencies.
The goal is to show how an attack can pivot from a low-permission workflow to a high-permission one.
Finding it
The starting point is the way the cache is scoped. GitHub describes cache access as isolated by branch: a cache created for one branch (for example Feature-A, based on main) would not be accessible to a pull request for another branch (Feature-B, also based on main). The cache action first searches for a hit in the branch containing the workflow run; if there is no hit there, it searches the parent branch and upstream branches.
That means access is scoped by branch (current and parent), and within that scope it is granted to all workflows across all runs of the branch. The author notes that the cache scope is defined in a way that can break other scope restrictions, for example because a cache can affect multiple workflows.
GitHub's CI has several scoping mechanisms, each with its own values and features:
- a virtual machine (VM) for each job
- job permissions
- workflow scopes
- workflow runs
- Git branches
- workflow identity tokens
- and others
The cache scope cuts across these. Entries are also read-only once pushed: GitHub does not allow modifications to existing entries.
Exploitation
Step 1: understand how the cache action runs
Using actions/cache anywhere in the CI runs two steps. One takes place during the run process when the action is called, and the other takes place after the workflow if the run action returned a cache miss.
- Run action: searches for and retrieves the cache. The search uses the cache key and results in either a cache hit (data found) or a cache miss. On a hit, the files and directories are retrieved from the cache for active use. On a miss, the desired files and directories are downloaded as if they were being fetched for the first time.
- Post workflow action: saves the cache. If the run action returned a cache miss, this action saves the current state of the directories to be cached under the provided key. It happens automatically and does not need to be called explicitly.
Step 2: poison the cache from the unit-test workflow
The unit-test workflow uses a malicious action that adds a cache entry with malicious content. The payload changes a Golang logging library, go.uber.org/zap@v1, so that it adds the string BAD library to the application artifact description.
Step 3: consume the poisoned cache in the release workflow
The release workflow builds and releases the application artifact and uses the cache to optimise its Golang dependencies. It picks up the poisoned entry written by the unit-test workflow. As a result, the malicious code is injected into the built Golang binary and image.
In the test, the string BAD library was injected into the image description. This was in version 0.4.1. The author then updated the tag and rebuilt the image several times, and observed that Bad library remained in the description.
Step 4: persistence and spread
The cache remains poisoned until the entry key is discarded, which is usually triggered by dependency updates. Until then, the same poisoned cache affects any other workflow, run and child branch that uses the same cache key.
Root cause
The weakness is in the design of the cache scope rather than in a coding error in a single action. Cache entries are keyed by a string and shared across all workflows and runs of a branch and its parents. A workflow with low privileges can therefore write an entry that a workflow with higher privileges will read.
GitHub's position, as reported, is that actions/cache behaves as expected and that there are no plans to tighten the scoping of the cache.
Impact
An attacker who can get a malicious tool or GitHub Action picked up by a low-privilege workflow can poison the cache. Any later workflow, run or child branch that uses the same cache key can then build with the attacker's content. In the experiment this meant injecting the string BAD library into the built Golang binary and image, and the injected string persisted across tag updates and rebuilds until the cache key was discarded. The attack is described as hidden from the eyes of the DevSecOps team, and as a way to pivot from a low-permission workflow to a high-permission one. The author hedges that other workflows "might" be affected, and that a similar logical vulnerability exists in other CI products.
Fix
The reported reply was that the action behaves as expected and that there are no plans to tighten the cache scoping. The author proposes the following mitigations:
- Do not use caches in release or in important workflows.
- Run workflows sequentially, with the trusted workflow running first, so that the cache is created in a trusted workflow.
- Vendor your dependencies. In Go, vendoring ensures that all third-party packages used in the project are consistent for everyone who develops for the application, so cached dependencies remain valid for all branches. Other languages might not support this method.
- Sign the cache value cryptographically and verify the signature before use.
The author's own product, Scribe, mitigates such attacks by granularly signing any object or directory in a workflow, such as a cache, so that before release you can validate that only a cache generated by a trusted workflow was used to build the release.
- Cross-workflow poisoning on the same branch. Have a low-privilege workflow write a cache entry under a key that a privileged workflow reads. A positive result is the privileged build containing content introduced by the low-privilege workflow.
- Parent and upstream branch lookup. Create the poisoned entry on a parent or upstream branch and check whether a workflow on a child branch picks it up when it misses locally. A positive result is the child build using the poisoned entry.
- Persistence across rebuilds. After poisoning, update the tag and rebuild several times, as the author did. A positive result is the malicious content surviving the rebuilds until the key is discarded.
- Other CI products. Apply the same reasoning to CI systems that share caches across jobs or pipelines. A positive result is a cache written by one job being consumed by a more privileged job.
What happened, in plain words
Imagine a shared kitchen in an office. One person prepares ingredients and leaves them in a labelled box in the fridge so the next person does not have to chop them again. That saves time. But if the first person is careless, or someone slips something bad into the box, the next cook uses it without checking and the bad ingredient ends up in the meal.
This article is about the same idea inside GitHub, a website where software teams store their code and run automated jobs. Those automated jobs are called a CI pipeline. CI stands for continuous integration: a set of steps a computer runs automatically every time code changes, such as testing the code and building the final program. To save time, GitHub lets these jobs keep a cache: a store of files that can be reused on later runs instead of being downloaded or built again.
The author of this article, from a company called Scribe Security, ran an experiment. They showed that a job with low permissions can put bad content into that shared cache, and a later job with higher permissions picks it up and builds it into the released software. They told GitHub about it. According to the article, GitHub replied that the cache feature behaves as expected and that it has no plans to tighten how the cache is shared. The author says a similar logical weakness exists in other CI products too.
The attack is described in four steps:
- An attacker publishes a malicious tool or a GitHub Action which is picked up by an unsuspecting workflow in GitHub.
- The workflow is designed with a cache.
- The malicious payload modifies the cache to include malicious data.
- Other workflows that call on this cache from this point might be affected.
A few words before we go further. A workflow is one automated job defined in a GitHub repository, made of steps that run on GitHub's computers. A GitHub Action is a ready-made building block that a workflow can call, a bit like an app you install on a phone. A payload is the part of an attack that actually does the damage, the bad content itself. And permissions are what a job is allowed to do: some jobs can only read and test, others can publish a release.
GitHub Cache
Workflows often reuse the same outputs or downloaded dependencies from one run to another. A dependency is a piece of software written by someone else that your project uses, for example a library for logging messages. The article names package and dependency management tools such as Maven, Gradle, npm and Yarn, which usually keep downloaded packages in a local cache of downloaded dependencies.
To speed up CI runs, GitHub provides a cache resource that can be used across the pipeline through an API. An API is a way for one program to talk to another program over the network, like a service counter where you ask for something by name and get it back. The entries in the cache are a key-value combination: the key is a string (a piece of text) used as a label, and the value is the files or directories you want to keep.
Using the action/cache Git action anywhere in the CI will run two steps: one during the run process when it is called, and one after the workflow if the run action returned a cache miss.
- Run action: searches for and retrieves the cache. The search uses the cache key, and the result is either a cache hit (success, data found in the cache) or a cache miss. On a hit, the files and directories are retrieved from the cache for active use. On a miss, the desired files and directories are downloaded as if they were being fetched for the first time.
- Post workflow action: saves the cache. If the run action returned a cache miss, this action saves the current state of the directories to be cached under the provided key. It happens automatically and does not need to be called explicitly.
Think of a cache hit as finding your leftovers already in the fridge, and a cache miss as finding the fridge empty so you cook from scratch.
GitHub Cache Permissions
Access restrictions provide cache isolation and security by creating a logical boundary between different branches. A branch is a separate line of work in a code repository, like a draft copy of a document that you can edit without touching the main version. The article gives this example: a cache created for the branch Feature-A (with the base main) would not be accessible to a pull request for the branch Feature-B (with the base main). A pull request is a proposal to merge one branch into another.
The cache action first searches for cache hits for a key and restores keys in the branch containing the workflow run. If there are no hits in the current branch, the cache action searches for the key and restores keys in the parent branch and upstream branches. A parent branch is the branch yours was copied from, and upstream means further back along that line.
Access to a cache is scoped by branch (current and parent), meaning access is provided to all workflows across runs of that branch. So the boundary separates unrelated branches, but inside one branch and its parents, every workflow shares the same cache.
Another important note is that GitHub does not allow modifications once entries are pushed: cache entries are read-only records.
GitHub CI Scoping
GitHub scopes allow specifying exactly what type of access is needed for various tasks. A scope is a limit on what something can reach, like a keycard that opens only certain doors. GitHub's CI is scoped in a variety of ways, each with its own set of values and features:
- Virtual Machine (VM) for each job
- Job permissions
- Workflow scopes
- Workflow runs
- Git branches
- Workflow identity tokens
- and others
A virtual machine is a simulated computer that a job runs on, kept separate from other jobs. A workflow identity token is a short-lived credential that a workflow uses to prove who it is, similar to a visitor badge that expires at the end of the day.
The article's point is that the cache scope has been defined in a way that can break some of the other scope restrictions, for example because the GitHub cache can affect multiple workflows. The cache scope can break some of the other scope restrictions.
The Attack
The author used an example CI that included two workflows. This example shows how an attack can pivot from a low permission workflow to a high permission one. To pivot means to use one small foothold to reach something bigger, like climbing from a low window onto the roof.
- Unit-test workflow running unit-test and code coverage tools. The author assumes that one of the tools is malicious or vulnerable to remote code execution. The workflow does need to use the action/cache Git action. Any workflow can access the cache.
- Release workflow builds and releases the application artifact. This workflow uses a cache to optimise using the Golang dependencies.
A unit test is a small automatic check that one piece of code does what it should. Code coverage measures how much of the code those tests exercise. Remote code execution means an attacker can make the computer run their own commands, which is about as bad as it gets. Golang (or Go) is a programming language, and an artifact is the finished file produced by a build, such as a program or a container image.
The unit-test workflow uses a malicious action that adds a cache entry with malicious content by changing a Golang logging library (go.uber.org/zap@v1) to add the string, 'BAD library' to the application artifact description. A logging library is code that writes messages about what a program is doing, and go.uber.org/zap@v1 is the name and version of one such library.
Next, the release workflow uses this poisoned cache entry. As a result, the malicious code is injected into the built Golang binary and image. A binary is the compiled program that actually runs, and an image here is a packaged copy of the software ready to be shipped. The cache remains poisoned until the entry key is discarded, usually triggered by dependency updates. Until then, the same poisoned cache will affect any other workflow, run, and child branch using the same cache key. A child branch is a branch created from another branch.
In the test the author performed, they managed to inject the string 'BAD library' into the image description. This was in version 0.4.1. They then updated the tag and rebuilt the image several times, and observed that 'Bad library' remained in the description. A tag is a label attached to a particular version of the software, and a description is the text shown alongside the image.
GitHub Disclosure
GitHub's response to the disclosure was that the Git action, action/cache behaves as expected and they have no plans to tighten the scoping of the cache. A disclosure is when a researcher privately tells the vendor about a problem. Although the GitHub team does not consider this behaviour problematic, the author advises DevSecOps practitioners to be wary of this attack vector. DevSecOps is the practice of building security into the way software is developed and delivered, and an attack vector is the route an attacker uses to get in.
Mitigations
A mitigation is something that reduces the risk, even if it does not remove the cause. The author proposes four:
- Do not use caches in release or in important workflows.
- Run your workflows sequentially, with the trusted workflow running first to make sure your cache is created in a trusted workflow. Sequentially means one after another rather than at the same time.
- Vendor your dependencies. Vendoring in GoLang is a method of ensuring that all third party packages used in the Go project are consistent for everyone who develops for that application. That way, cached dependencies will remain valid for all branches of the project. Other languages might not support this method. Vendoring means keeping a copy of the dependency inside your own project instead of fetching it fresh each time.
- Sign the cache value cryptographically and verify the signature before usage. A cryptographic signature is a mathematical stamp that proves who produced a piece of data and that it has not been changed, like a wax seal on an envelope.
Scribe mitigates such attacks by granularly signing any object or directory in a workflow such as a cache. Before the release, you can use Scribe to validate that only a cache generated by a trusted workflow was used to build the release.
Summary
In this post, the author outlined a cache poisoning attack in CI workflows, which is hidden from the eyes of the DevSecOps team, and discussed mitigations.
Glossary
- API: a way for one program to ask another program for something over the network, like a service counter.
- Artifact: the finished file produced by a build, such as a program or a container image.
- Attack vector: the route an attacker uses to get into a system.
- Binary: the compiled program that actually runs on a computer.
- Branch: a separate line of work in a code repository, like a draft copy of a document.
- Cache: a store of files kept so they can be reused instead of downloaded or built again.
- Cache hit: the requested data was found in the cache.
- Cache miss: the requested data was not in the cache, so it has to be fetched or built.
- Cache poisoning: putting bad content into a shared cache so that later users of the cache get the bad content.
- Child branch: a branch created from another branch.
- CI (continuous integration): automated steps a computer runs when code changes, such as testing and building.
- Code coverage: a measure of how much of the code the tests exercise.
- Cryptographic signature: a mathematical stamp proving who produced data and that it has not been changed.
- Dependency: software written by someone else that your project uses.
- Description (image): the text shown alongside a container image.
- DevSecOps: the practice of building security into software development and delivery.
- Disclosure: privately telling the vendor about a security problem.
- GitHub Action: a ready-made building block that a workflow can call.
- Golang (Go): a programming language.
- Image: a packaged copy of software ready to be shipped.
- Key-value: a label (key) paired with the data (value) stored under it.
- Logging library: code that writes messages about what a program is doing.
- Mitigation: something that reduces risk, even if it does not remove the cause.
- Parent branch: the branch yours was copied from.
- Payload: the part of an attack that actually does the damage.
- Permissions: what a job is allowed to do.
- Pivot: using one small foothold to reach something bigger.
- Pull request: a proposal to merge one branch into another.
- Remote code execution: an attacker can make a computer run their own commands.
- Scope: a limit on what something can reach, like a keycard that opens only certain doors.
- Sequentially: one after another rather than at the same time.
- Tag: a label attached to a particular version of software.
- Unit test: a small automatic check that one piece of code does what it should.
- Upstream: further back along the line a branch came from.
- Vendoring: keeping a copy of a dependency inside your own project instead of fetching it fresh each time.
- Virtual machine (VM): a simulated computer that a job runs on, kept separate from other jobs.
- Workflow: one automated job defined in a repository, made of steps that run on GitHub's computers.
- Workflow identity token: a short-lived credential a workflow uses to prove who it is.