Arbitrary file overwrites in `node-tar`
A tar archive can contain a hardlink to an existing file followed by a regular file with the same name, causing the regular file's contents to overwrite the original file. An attacker could overwrite arbitrary files with the permissions of the user running node-tar.
- Triaged in 5 days
- Fixed in 10 months
- Disclosed after 11 months
Background
I was looking for vulnerabilities in a different tar library, tar-fs, and discovered a bug that allowed me to overwrite arbitrary files on the host system using its default extraction method. After reporting the bug to the maintainer of tar-fs, Mathias Buus, he realized that node-tar was vulnerable to the same attack. However, he found that the npm client is not vulnerable here since the library it uses, pacote, doesn't allow any hardlinks or symlinks.
Vulnerability
If a tar file contains a hardlink to an existing file on the filesystem, and then a subsequent regular file with the same name as this hardlink, the contents of the regular file will overwrite the contents of the original file.
For example, the attached file, hardlink_exploit.tar.gz, contains two relevant files. First, a hardlink to /tmp/overwriteme called hardboi/yyy, and second, a regular file called hardboi/yyy containing oops.
To demonstrate this vulnerability:
- Make a file called
/tmp/overwritemewith whatever contents - Extract the attached tar archive using the following code:
tar = require('tar')
tar.x(
{
file: 'hardlink_exploit.tar.gz'
}
).then(_=> { console.log("done") })
- Observe the contents of /tmp/overwriteme are now "oops"
I'm not sure what the best way to resolve this vulnerability is. My local copy of GNU Tar isn't vulnerable because by default it doesn't allow hardlinks that point to an absolute file or that try to directory traverse above the parent:
$ tar -xvf hardlink_pkg.tar.gz
hardboi/
tar: Removing leading `/' from hard link targets
hardboi/yyy
tar: hardboi/yyy: Cannot hard link to ‘tmp/overwriteme’: No such file or directory
hardboi/yyy
hardboi/package.json
tar: Exiting with failure status due to previous errors
Impact
Applications, of which there are probably many, that rely on node-tar to be secure-by-default when extracting a tar file are in fact vulnerable. Unless hardlinks are explicitly disabled as in pacote, arbitrary files can be overwritten with the permissions of the user running node-tar.
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
Likely the library did not validate that hardlink targets stay within the extraction directory, allowing links to absolute paths outside it.
Impact
Arbitrary files could be overwritten with the permissions of the user running node-tar, unless hardlinks are explicitly disabled.
What the attacker needed
Victim extracts a malicious tar archive; hardlinks are not disabled
The fix
One possible fix is to reject hardlinks to absolute paths or that traverse above the extraction directory, as GNU Tar does by default.
How it was found
- Create a file at /tmp/overwriteme with any contents
- Extract the malicious tar archive using node-tar's extraction function
- Observe that /tmp/overwriteme now contains the attacker's data
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 tar archive is a way to bundle files together, like a zip file. A hardlink is a shortcut that makes two names point to the same file on disk. The library node-tar did not check that a hardlink inside an archive pointed to a safe place, so a malicious archive could make a new file overwrite an existing one on the victim's computer.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.