nsLocalFile::GetNativeTarget() may use incorrect string length for Unix symlink target
The function that resolves a Unix symlink sizes its output buffer from the length reported by lstat() and then writes the target with readlink() without checking how many bytes were actually written. For special paths such as /proc/<pid>/fd/ entries, where the reported size does not match the real target length, the result is a partially uninitialised buffer or a truncated target string.
Originally disclosed on Mozilla
- Fixed in 22 days
- Paid in 26 days
- Disclosed after 22 days
When resolving a symlink on a *nix filesystem, the target string may be set to an incorrect length, resulting in a partially uninitialized buffer or truncation.
Testcase
Depending on the build, <img src="file:///proc/self/fd/1"> gives me an error such as
Security Error: Content at http://localhost/ may not load or link to file:///dev/pts/9oc/self/fd/1%00%00%20%00d%00a%00t%00a%00%20%00f%00r%00o%00m%00%20%00l%00o%00c%00a%00l%00h%00o%00s%00t.
or
Security Error: Content at http://localhost/ may not load or link to file:///dev/null%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5%E5.
Details
In nsLocalFileUnix.cpp nsLocalFile::GetNativeTarget() the steps to obtain a symlink's target are roughly:
- Use
lstat()on the symlink path.
struct STAT symStat;
if (LSTAT(mPath.get(), &symStat) == -1) {
return NSRESULT_FOR_ERRNO();
}
- Set
targetstring length to the symlink size given insymStat.st_size.
int32_t size = (int32_t)symStat.st_size;
nsAutoCString target;
if (!target.SetLength(size, mozilla::fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
- Use
readlink()to write the symlink value (without trailing null) totarget.
if (readlink(mPath.get(), target.BeginWriting(), (size_t)size) < 0) {
return NSRESULT_FOR_ERRNO();
}
This assumes that the length reported by lstat() equals the number of bytes later written to the buffer by readlink().
However, aside from potential races, this doesn't hold true for some special paths: E.g., on the Linux systems I checked, any of the symlinks in /proc/<pid>/fd/ reports 64 bytes, no matter its target. (Not sure why that is, I haven't found it documented anywhere.) So when resolving a link such as /proc/self/fd/5, target may remain partially uninitialized or truncate the value to 64 bytes.
I suppose it'd make sense here to check the return value of readlink() for the actual number of bytes written.
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 code trusts the size reported by lstat() as the exact number of bytes readlink() will write, instead of using readlink()'s return value to set the real length.
Impact
The resolved symlink target may contain uninitialised memory or be truncated, which could leak leftover buffer contents into error messages or cause incorrect path handling; the text does not demonstrate a full exploit.
What the attacker needed
Ability to load a page with a file:// reference; target path is a special /proc symlink
The fix
The code should use the return value of readlink() to set the actual length of the target string rather than relying on the lstat() size.
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 symlink is a shortcut file that points at another file. The code asks the operating system how long the shortcut's target is, reserves exactly that much space, then copies the target in. On some special Linux paths the reported length is wrong, so the copied text either gets cut short or leaves leftover memory in the buffer.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.