Skip to content
Memory corruption Disclosed report Advanced

Latent write beyond bounds in MakeCompressedIndexDataValues()

An incorrect overflow check in MakeCompressedIndexDataValues() omitted the sort key length, so the computed buffer size could overflow and cause an undersized allocation. An attacker could potentially trigger a write beyond bounds if the maximum writable data length increased by only two bytes.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 14 months
  • Paid in 2 months
  • Disclosed after 14 months
Text
Due to an incorrect overflow test, MakeCompressedIndexDataValues() (dom\indexedDB\ActorsParent.cpp) could cause data to be written beyond bounds if the maximum length of writable data increased by only 2 bytes.

The bug is that the the overflow test on lines 783-90 does not include the value |sortKeyBufferLength|, but the sum on lines 792-97 -- that the test was meant to protect -- *does* include it:

750: nsresult
751: MakeCompressedIndexDataValues(
752:                              const FallibleTArray<IndexDataValue>& aIndexValues,
753:                              UniqueFreePtr<uint8_t>& aCompressedIndexDataValues,
754:                              uint32_t* aCompressedIndexDataValuesLength)
755: {
...
771:   // First calculate the size of the final buffer.
772:   uint32_t blobDataLength = 0;
773: 
774:   for (uint32_t arrayIndex = 0; arrayIndex < arrayLength; arrayIndex++) {
775:     const IndexDataValue& info = aIndexValues[arrayIndex];
776:     const nsCString& keyBuffer = info.mKey.GetBuffer();
777:     const nsCString& sortKeyBuffer = info.mSortKey.GetBuffer();
778:     const uint32_t keyBufferLength = keyBuffer.Length();
779:     const uint32_t sortKeyBufferLength = sortKeyBuffer.Length();
780: 
781:     MOZ_ASSERT(!keyBuffer.IsEmpty());
782: 
783:     // Don't let |infoLength| overflow.
784:     if (NS_WARN_IF(UINT32_MAX - keyBuffer.Length() <
785:                    CompressedByteCountForIndexId(info.mIndexId) +
786:                    CompressedByteCountForNumber(keyBufferLength) +
787:                    CompressedByteCountForNumber(sortKeyBufferLength))) {
788:       IDB_REPORT_INTERNAL_ERR();
789:       return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
790:     }
791: 
792:     const uint32_t infoLength =
793:       CompressedByteCountForIndexId(info.mIndexId) +
794:       CompressedByteCountForNumber(keyBufferLength) +
795:       CompressedByteCountForNumber(sortKeyBufferLength) +
796:       keyBufferLength +
797:       sortKeyBufferLength;
798: 
799:     // Don't let |blobDataLength| overflow.
800:     if (NS_WARN_IF(UINT32_MAX - infoLength < blobDataLength)) {
801:       IDB_REPORT_INTERNAL_ERR();
802:       return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
803:     }
804: 
805:     blobDataLength += infoLength;
806:   }
807: 
808:   UniqueFreePtr<uint8_t> blobData(
809:     static_cast<uint8_t*>(malloc(blobDataLength)));
810:   if (NS_WARN_IF(!blobData)) {
811:     IDB_REPORT_INTERNAL_ERR();
812:     return NS_ERROR_OUT_OF_MEMORY;
813:   }

The bug is (barely) latent because of various maximum length limits:

   1. The maximum length of an nsCString is 0x7ffffff5, so |keyBufferLength + sortKeyBufferLength| must be <= 0xffffffea;
   2. The maximum value of |info.mIndexId| is 0x7fffffffffffffff, so the maximum value of |CompressedByteCountForIndexId(info.mIndexId)| is 0xa; and
   3. The maximum values of |CompressedByteCountForNumber(keyBufferLength)| and |CompressedByteCountForNumber(sortKeyBufferLength)| are 0x5.

so the sum on lines 792-97 can be, at most, 0xfffffffe.

If the maximum sizes, above, increased only trivially, or some other value, having a maximum of >= 2, were added into line 792 without the test being fixed, the addition could overflow, causing an underallocation on lines 808-09, and a subsequent write beyond bounds.

The original report as published on Mozilla's Bugzilla, by q1. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 overflow test on lines 783-90 does not include the value |sortKeyBufferLength|, but the sum on lines 792-97 -- that the test was meant to protect -- *does* include it.

Impact

If the maximum sizes increased only trivially, or some other value with a maximum of >= 2 were added, the addition could overflow, causing an underallocation and a subsequent write beyond bounds.

What the attacker needed

Maximum writable data length increased by 2 bytes

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 bug in a function that calculates how much memory to allocate for a data structure. The check meant to prevent the size calculation from overflowing (wrapping around to a small number) forgot to include one of the values being added. If the numbers were slightly larger, the total could wrap around, leading to a too-small memory allocation and a subsequent write past the end of that 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.

More by q1

View all

71 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.