Closed Bug 1813337 Opened 3 years ago Closed 3 years ago

Write beyond bounds in nsZipWriter::ReadFile()

Categories

(Core :: Networking: JAR, defect, P3)

All
Unspecified
defect

Tracking

()

RESOLVED FIXED
122 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox111 --- wontfix
firefox120 --- wontfix
firefox121 --- wontfix
firefox122 --- fixed

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)

22.14 KB, application/x-javascript
Details
3.84 KB, application/x-javascript
Details
1.72 KB, application/x-zip-compressed
Details
Attached file 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.

  1. 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).

  2. 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).

  3. When the BP fires, step line 142 and observe that commentLen has the value 0x1e4.

  4. 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.

  5. Step through line 160 and watch it read length (0x2f9) bytes into field, stomping the heap with data read from the corrupted ZIP file.

  6. By suitably modifying the ZIP file, you can cause longer or shorter WBBs.

Attached file har-menu-utils.js —
Attached file foo.zip —
Flags: sec-bounty?
Group: core-security → network-core-security
Severity: -- → S3
Priority: -- → P2
Whiteboard: [necko-triaged][necko-priority-review]
Keywords: sec-other
Priority: P2 → P3

Assessed as low effort, easy fix.

Defer for now, keep in priority review for future priority selection.

Whiteboard: [necko-triaged][necko-priority-review] → [necko-triaged][necko-priority-next]
Whiteboard: [necko-triaged][necko-priority-next] → [necko-triaged][necko-priority-queue]
Assignee: nobody → rjesup
Group: network-core-security → core-security-release
Status: NEW → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 122 Branch

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-firefox121 to wontfix.

For more information, please visit BugBot documentation.

Flags: needinfo?(rjesup)
Flags: needinfo?(rjesup)
Flags: sec-bounty? → sec-bounty+
QA Whiteboard: [post-critsmash-triage]
Flags: qe-verify-
Whiteboard: [necko-triaged][necko-priority-queue] → [necko-triaged][necko-priority-queue][adv-main122-]

Making Firefox 122 security bugs public. [bugspam filter string: Pilgarlic-Towers]

Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: