Unpacker improperly validates symlinks, allowing gems writes to arbitrary locations
The RubyGems installer's check that a gem cannot write outside its install directory could be bypassed using a symbolic link inside a crafted gem. An attacker could then write a file with arbitrary name, contents and permissions to any location, potentially achieving code execution.
- Triaged in 5 months
- Fixed in 5 months
- Paid in 15 months
- Disclosed after 15 months
Unpacker improperly validates symlinks, allowing gems writes to arbitrary locations
The RubyGems installer attempts to prevent a gem from writing any files outside the install directory; however it is possible to bypass the check with a symbolic link in a crafted gem.
Example structure of malicious gem
$ tar -xvf symlink.gem
metadata.gz
data.tar.gz
$ tar -tvf data.tar.gz
-rw-r--r-- 0/0 12 1969-12-31 16:00 README
lrw-r--r-- 0/0 0 1969-12-31 16:00 link -> /tmp
-rw-r--r-- 0/0 6 1969-12-31 16:00 link/HACKED
Proof of concept
Using the attached symlink.gem:
gem install symlink.gem
# or
gem unpack symlink.gem
This will create a file /tmp/HACKED.
Impact
The name and contents of the written file, as well as the file permissions, are arbitrary.
Using this technique, an attacker could easily get code execution, for example by overwriting a system binary or writing into a user's .profile.
Note that the exploit will even work with gem unpack, which is supposed to be safe of system-level side-effects.
For comparison, this exploit is more powerful than #243156 (and #270068) as the target directory doesn't need to contain a dash.
Root cause
The code in install_location is supposed to check if the target filename is outside the destination directory. It does this by fully resolving (using File.realpath) the destination directory and then seeing if the target filename .start_with? that directory.
This test succeeds for a symlink that points outside the gem's install directory, because its "destination directory" is the directory where it's located (not where it points), which is local.
The test also succeeds for a file that uses the symlink to "escape" the local directory, because the symlink really is its prefix.
However, in combination, these files can allow for arbitrary writes, as shown.
The root cause vulnerability is the ability of symlinks to point outside the gem. This was actually forbidden in a commit from 2015, but was made more permissive in a later commit, creating this vulnerability.
Suggested remediation
The course of action we recommend is to (again) disallow symlinks that point outside the gem directory.
Attached files
symlink.gem, an example of a vulnerable gem. Note: extract this usingtarinstead ofgemto avoid triggering the vulnerability (e.g.,tar -Oxf symlink.gem data.tar.gz | tar -tzvf -)make-symlink-gem.py, sample code that generates the proof of concept (to run:./make-symlink-gem.py > symlink.gem)0001-Add-a-test-test_extract_symlink_parent.patch, test code that can be added to RubyGems to test for this vulnerability
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 installer resolved the destination directory but then only checked that the target path started with it, so a symlink pointing outside the install directory, and a file written through that symlink, both passed the check.
Impact
The written file's name, contents and permissions are arbitrary, so an attacker could overwrite a system binary or a user's profile file to gain code execution; the exploit also works with gem unpack, which is meant to have no system-level side effects.
What the attacker needed
Victim installs or unpacks a crafted gem
The fix
Symlinks that point outside the gem directory should again be disallowed.
How it was found
- Build or obtain a gem whose data archive contains a symlink pointing outside the install directory and a file placed under that symlink.
- Install the gem with the gem install command, or unpack it with gem unpack.
- Observe that the file is created at the location the symlink points to, outside the install directory.
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 gem is a package of Ruby code that gets installed into a folder. The installer tries to stop a package from writing files anywhere else on the computer, but a symbolic link (a shortcut that points to another place) inside the package tricks that check. So a malicious package can drop a file wherever it likes, which could let an attacker run their own code.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.