Write beyond bounds in nsZipWriter::ReadFile()
Categories
(Core :: Networking: JAR, defect, P3)
Tracking
()
People
(Reporter: mozillabugs, Assigned: jesup)
Details
(Keywords: reporter-external, sec-other, Whiteboard: [necko-triaged][necko-priority-queue][adv-main122-])
Attachments
(3 files, 1 external file)
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:
-
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(). -
Copy
toolbar.jsintodist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/components/toolsbar.js. -
Copy
har-menu-utils.jsintodist/bin/browser/chrome/devtools/modules/devtools/client/netmonitor/src/har/har-menu-utils.js. -
Copy
foo.zipinto some folder accessible to FF, and edit the line
zipfile.initWithPath("c:\\users\browser\\downloads\\foo.zip");
in har-menu-utils.js appropriately.
-
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).
-
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).
-
When the BP fires, step line 142 and observe that
commentLenhas the value0x1e4. -
Step through line 153 and notice that it allocates
0x1e4bytes of heap forfield. Display the memory there so that you can witness the WBB. -
Step through line 160 and watch it read
length(0x2f9) bytes intofield, stomping the heap with data read from the corrupted ZIP file. -
By suitably modifying the ZIP file, you can cause longer or shorter WBBs.
| Reporter | ||
Comment 1•3 years ago
|
||
| Reporter | ||
Comment 2•3 years ago
|
||
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Defer for now, keep in priority review for future priority selection.
Updated•3 years ago
|
| Assignee | ||
Updated•3 years ago
|
| Assignee | ||
Updated•3 years ago
|
| Assignee | ||
Comment 5•3 years ago
|
||
Comment 7•3 years ago
|
||
Updated•3 years ago
|
Comment 8•3 years ago
|
||
The patch landed in nightly and beta is affected.
:jesup, is this bug important enough to require an uplift?
- If yes, please nominate the patch for beta approval.
- If no, please set
status-firefox121towontfix.
For more information, please visit BugBot documentation.
| Assignee | ||
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Updated•2 years ago
|
Updated•2 years ago
|
Comment 9•2 years ago
|
||
Making Firefox 122 security bugs public. [bugspam filter string: Pilgarlic-Towers]
Updated•2 years ago
|
Description
•