CVE-2021-37701: Arbitrary File Creation/Overwrite via insufficient symlink protection due to directory cache poisoning using symbolic links
Impact
Arbitrary File Creation, Arbitrary File Overwrite, Arbitrary Code Execution
node-tar aims to guarantee that any file whose location would be modified by a symbolic link is not extracted. This is, in part, achieved by ensuring that extracted directories are not symlinks. Additionally, in order to prevent unnecessary stat calls to determine whether a given path is a directory, paths are cached when directories are created.
This logic was insufficient when extracting tar files that contained both a directory and a symlink with the same name as the directory, where the symlink and directory names in the archive entry used backslashes as a path separator on posix systems. The cache checking logic used both \ and / characters as path separators, however \ is a valid filename character on posix systems.
By first creating a directory, and then replacing that directory with a symlink, it was thus possible to bypass node-tar symlink checks on directories, essentially allowing an untrusted tar file to symlink into an arbitrary location and subsequently extracting arbitrary files into that location, thus allowing arbitrary file creation and overwrite.
Additionally, a similar confusion could arise on case-insensitive filesystems. If a tar archive contained a directory at FOO, followed by a symbolic link named foo, then on case-insensitive file systems, the creation of the symbolic link would remove the directory from the filesystem, but not from the internal directory cache, as it would not be treated as a cache hit. A subsequent file entry within the FOO directory would then be placed in the target of the symbolic link, thinking that the directory had already been created.
These issues were addressed in releases 4.4.16, 5.0.8 and 6.1.7.
The v3 branch of node-tar has been deprecated and did not receive patches for these issues. If you are still using a v3 release we recommend you update to a more recent version of node-tar. If this is not possible, a workaround is available below.
Patches
4.4.16 || 5.0.8 || 6.1.7
Workarounds
Users may work around this vulnerability without upgrading by creating a custom filter method which prevents the extraction of symbolic links.
js const tar = require('tar')
tar.x({ file: 'archive.tgz', filter: (file, entry) => { if (entry.type === 'SymbolicLink') { return false } else { return true } } })
Users are encouraged to upgrade to the latest patched versions, rather than attempt to sanitize tar input themselves.
Fix
The problem is addressed in the following ways:
1. All paths are normalized to use / as a path separator, replacing \ with / on Windows systems, and leaving \ intact in the path on posix systems. This is performed in depth, at every level of the program where paths are consumed. 2. Directory cache pruning is performed case-insensitively. This may result in undue cache misses on case-sensitive file systems, but the performance impact is negligible.
Caveat
Note that this means that the entry objects exposed in various parts of tar's API will now always use / as a path separator, even on Windows systems. This is not expected to cause problems, as / is a valid path separator on Windows systems, but may result in issues if entry.path is compared against a path string coming from some other API such as fs.realpath() or path.resolve().
Users are encouraged to always normalize paths using a well-tested method such as path.resolve() before comparing paths to one another.
Other sources
A flaw was found in the npm package "tar" (aka node-tar). Extracting tar files that contain both a directory and a symlink with the same name, where the symlink and directory names in the archive entry used backslashes as a path separator, made it possible to bypass node-tar symlink checks on directories. This flaw allows an untrusted tar file to extract and overwrite files into an arbitrary location. A similar confusion can arise on case-insensitive filesystems. The highest threat from this vulnerability is to integrity and system availability.
node-tar aims to guarantee that any file whose location would be modified by a symbolic link is not extracted. This is, in part, achieved by ensuring that extracted directories are not symlinks. Additionally, in order to prevent unnecessary stat calls to determine whether a given path is a directory, paths are cached when directories are created.
This logic was insufficient when extracting tar files that contained both a directory and a symlink with the same name as the directory, where the symlink and directory names in the archive entry used backslashes as a path separator on posix systems. The cache checking logic used both \ and / characters as path separators, however \ is a valid filename character on posix systems.
By first creating a directory, and then replacing that directory with a symlink, it was thus possible to bypass node-tar symlink checks on directories, essentially allowing an untrusted tar file to symlink into an arbitrary location and subsequently extracting arbitrary files into that location, thus allowing arbitrary file creation and overwrite.
Additionally, a similar confusion could arise on case-insensitive filesystems. If a tar archive contained a directory at FOO, followed by a symbolic link named foo, then on case-insensitive file systems, the creation of the symbolic link would remove the directory from the filesystem, but not from the internal directory cache, as it would not be treated as a cache hit. A subsequent file entry within the FOO directory would then be placed in the target of the symbolic link, thinking that the directory had already been created.
These issues were addressed in releases 4.4.16, 5.0.8 and 6.1.7.
Reference: https://github.com/npm/node-tar/security/advisories/GHSA-9r2w-394v-53qc
— Red Hat
Node.js tar module could allow a local attacker to execute arbitrary code on the system, caused by an arbitrary file creation/overwrite vulnerability. By creating a directory, and then replacing that directory with a symlink, an attacker could use an untrusted tar file to symlink into an arbitrary location and extract arbitrary files into that location to create or overwrite arbitrary files and execute arbitrary code on the system.
— IBM
The npm package "tar" (aka node-tar) before versions 4.4.16, 5.0.8, and 6.1.7 has an arbitrary file creation/overwrite and arbitrary code execution vulnerability. node-tar aims to guarantee that any file whose location would be modified by a symbolic link is not extracted. This is, in part, achieved by ensuring that extracted directories are not symlinks. Additionally, in order to prevent unnecessary stat calls to determine whether a given path is a directory, paths are cached when directories are created. This logic was insufficient when extracting tar files that contained both a directory and a symlink with the same name as the directory, where the symlink and directory names in the archive entry used backslashes as a path separator on posix systems. The cache checking logic used both \ and / characters as path separators, however \ is a valid filename character on posix systems. By first creating a directory, and then replacing that directory with a symlink, it was thus possible to bypass node-tar symlink checks on directories, essentially allowing an untrusted tar file to symlink into an arbitrary location and subsequently extracting arbitrary files into that location, thus allowing arbitrary file creation and overwrite. Additionally, a similar confusion could arise on case-insensitive filesystems. If a tar archive contained a directory at FOO, followed by a symbolic link named foo, then on case-insensitive file systems, the creation of the symbolic link would remove the directory from the filesystem, but not from the internal directory cache, as it would not be treated as a cache hit. A subsequent file entry within the FOO directory would then be placed in the target of the symbolic link, thinking that the directory had already been created. These issues were addressed in releases 4.4.16, 5.0.8 and 6.1.7. The v3 branch of node-tar has been deprecated and did not receive patches for these issues. If you are still using a v3 release we recommend you update to a more recent version of node-tar. If this is not possible, a workaround is available in the referenced GHSA-9r2w-394v-53qc.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
redhat/rh-nodejs14-nodejsto a version that resolves this vulnerability.Fixed in 0:14.18.2-1.el7 - Upgrade
Upgrade
redhat/rh-nodejs14-nodejs-nodemonto a version that resolves this vulnerability.Fixed in 0:2.0.3-6.el7 - Upgrade
Upgrade
redhat/rh-nodejs12-nodejsto a version that resolves this vulnerability.Fixed in 0:12.22.12-2.el7 - Upgrade
Upgrade
debian/node-tarto a version that resolves this vulnerability.Fixed in 4.4.6+ds1-3+deb10u2Fixed in 6.0.5+ds1+~cs11.3.9-1+deb11u2Fixed in 6.1.13+~cs7.0.5-1 - Upgrade
Upgrade
redhat/nodejs-tarto a version that resolves this vulnerability.Fixed in 4.4.16 - Upgrade
Upgrade
redhat/nodejs-tarto a version that resolves this vulnerability.Fixed in 5.0.8 - Upgrade
Upgrade
redhat/nodejs-tarto a version that resolves this vulnerability.Fixed in 6.1.7 - Upgrade
Upgrade
npm/tarto a version that resolves this vulnerability.Fixed in 4.4.16 - Upgrade
Upgrade
npm/tarto a version that resolves this vulnerability.Fixed in 6.1.7 - Upgrade
Upgrade
npm/tarto a version that resolves this vulnerability.Fixed in 5.0.8 - Upgrade
Upgrade
npm/tar (node-tar)to a version that resolves this vulnerability.Fixed in 4.4.16 - Upgrade
Upgrade
npm/tar (node-tar)to a version that resolves this vulnerability.Fixed in 5.0.8 - Upgrade
Upgrade
npm/tar (node-tar)to a version that resolves this vulnerability.Fixed in 6.1.7 - Configuration
When extracting with tar.x, use a custom filter function that returns false when entry.type === 'SymbolicLink' (e.g., filter: (file, entry) => { if (entry.type === 'SymbolicLink') { return false } return true }).
node-tar (tar.x) filter (SymbolicLink extraction) = return false - Configuration
Ensure you normalize paths using a well-tested method such as path.resolve() before comparing entry.path values to other filesystem paths (to avoid issues because entry objects now always use '/' as a path separator).
Application path handling around tar entries entry.path normalization for comparisons = Use path.resolve() normalization before comparing paths - Compensating control
If you are still using an affected v3 release, use a workaround by creating a custom tar filter method that prevents extraction of symbolic links (e.g., return false for entry.type === 'SymbolicLink').
Event History
Parent advisories
This vulnerability appears in the following advisories.
Frequently Asked Questions
What is CVE-2021-37701?
CVE-2021-37701 is a vulnerability in the npm package "tar" that allows a local attacker to execute arbitrary code on the system.
How severe is CVE-2021-37701?
CVE-2021-37701 has a severity score of 8.1, which is considered high.
What software is affected by CVE-2021-37701?
The Node.js "tar" module versions before 4.4.16, 5.0.8, and 6.1.7 are affected by CVE-2021-37701.
How can I fix CVE-2021-37701?
To fix CVE-2021-37701, update the "tar" module to versions 4.4.16, 5.0.8, or 6.1.7.
Where can I find more information about CVE-2021-37701?
More information about CVE-2021-37701 can be found at the following references: [link1](https://github.com/npm/node-tar/security/advisories/GHSA-9r2w-394v-53qc), [link2](https://bugzilla.redhat.com/show_bug.cgi/show_bug.cgi?id=1999738), [link3](https://bugzilla.redhat.com/show_bug.cgi/show_bug.cgi?id=1999737).