Write beyond bounds in nsZipWriter::ReadFile()
A logic error in nsZipWriter::ReadFile() causes a heap buffer overflow when reading a malformed ZIP file: it allocates a buffer of commentLen bytes but reads length bytes into it. An attacker could potentially exploit this to corrupt memory, though the function is only used in tests.
Originally disclosed on Mozilla
- Fixed in 10 months
- Paid in 10 months
- Disclosed after 10 months
Created attachment 9314715
Toolbar.js
`nsZipWriter::ReadFile()` (`modules/libjar/zipwriter/nsZipWriter.cpp`) can write beyond bounds ("WBB") when reading a defective ZIP file because of a logic error. The bug appears to be latent because the function appears to be used only in tests.
The bug is in line 160, which reads `length` bytes into `field`, where `field` is only `commentLen` bytes long (as allocated by line 153):
'''
91: nsresult nsZipWriter::ReadFile(nsIFile* aFile) {
92: int64_t size;
93: nsresult rv = aFile->GetFileSize(&size);
94: NS_ENSURE_SUCCESS(rv, rv);
95:
96: // If the file is too short, it cannot be a valid archive, thus we fail
97: // without even attempting to open it
98: NS_ENSURE_TRUE(size > ZIP_EOCDR_HEADER_SIZE, NS_ERROR_FILE_CORRUPTED);
99:
100: nsCOMPtr<nsIInputStream> inputStream;
101: rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aFile);
102: NS_ENSURE_SUCCESS(rv, rv);
103:
104: uint8_t buf[1024];
105: int64_t seek = size - 1024;
106: uint32_t length = 1024;
107:
108: nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(inputStream);
109:
110: while (true) {
111: if (seek < 0) {
112: length += (int32_t)seek;
113: seek = 0;
114: }
115:
116: rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, seek);
117: if (NS_FAILED(rv)) {
118: inputStream->Close();
119: return rv;
120: }
121: rv = ZW_ReadData(inputStream, (char*)buf, length);
122: if (NS_FAILED(rv)) {
123: inputStream->Close();
124: return rv;
125: }
...
132: for (uint32_t pos = length - ZIP_EOCDR_HEADER_SIZE; (int32_t)pos >= 0;
133: pos--) {
134: uint32_t sig = PEEK32(buf + pos);
135: if (sig == ZIP_EOCDR_HEADER_SIGNATURE) {
136: // Skip down to entry count
137: pos += 10;
138: uint32_t entries = READ16(buf, &pos);
139: // Skip past CDS size
140: pos += 4;
141: mCDSOffset = READ32(buf, &pos);
142: uint32_t commentlen = READ16(buf, &pos);
143:
144: if (commentlen == 0)
145: mComment.Truncate();
146: else if (pos + commentlen <= length)
147: mComment.Assign((const char*)buf + pos, commentlen);
148: else {
149: if ((seek + pos + commentlen) > size) {
150: inputStream->Close();
151: return NS_ERROR_FILE_CORRUPTED;
152: }
153: auto field = MakeUnique<char[]>(commentlen);
154: NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
155: rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, seek + pos);
156: if (NS_FAILED(rv)) {
157: inputStream->Close();
158: return rv;
159: }
160: rv = ZW_ReadData(inputStream, field.get(), length);
161: if (NS_FAILED(rv)) {
162: inputStream->Close();
163: return rv;
164: }
'''
You can verify the WBB by using the attachments thusly:
1. Start with a built version of FF, not an installed one, because you will need to replace some JS files to create the chrome necessary to invoke `nsZipWriter::ReadFile()`.
2. Copy `toolbar.js` into `dist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/components/toolsbar.js`.
3. Copy `har-menu-utils.js` into `dist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/har/har-menu-utils.js`.
4. Copy `foo.zip` into some folder accessible to FF, and edit the line
'''
zipfile.initWithPath("c:\\users\browser\\downloads\\foo.zip");
'''
in `har-menu-utils.js` appropriately.
5. Start FF, attach a debugger to it, and set a BP on line 142, above (line numbers are from FF v.105.0.1, and are slightly different in trunk, but the relevant code is the same).
6. Open the developer tools, network page, and select "Exercise the bug" from the star menu just right of the throttling control (it's the one that has "import HAR File" and similar selections).
7. When the BP fires, step line 142 and observe that `commentLen` has the value `0x1e4`.
8. Step through line 153 and notice that it allocates `0x1e4` bytes of heap for `field`. Display the memory there so that you can witness the WBB.
9. Step through line 160 and watch it read `length` (`0x2f9`) bytes into `field`, stomping the heap with data read from the corrupted ZIP file.
10. By suitably modifying the ZIP file, you can cause longer or shorter WBBs.
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 code allocates a buffer of size commentlen but then reads length bytes into it, using the wrong size variable.
Impact
The bug could allow writing beyond bounds and corrupting the heap, but it is latent because the function is only used in tests.
What the attacker needed
Crafted ZIP file; access to test-only function
How it was found
- Build Firefox from source and replace two JavaScript files to expose the vulnerable function.
- Place a crafted ZIP file and update the path in the JavaScript file.
- Start Firefox with a debugger and set a breakpoint at the vulnerable line.
- Open developer tools, go to the network page, and trigger the bug from the star menu.
- Observe the allocated buffer size and the larger read size, confirming the overflow.
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.
This is a memory corruption bug in a ZIP file reader. The program sets aside a small box for the file comment but then pours in more data than the box can hold, spilling into neighbouring memory. That can crash the program or let an attacker run their own code.
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.