Skip to content
Memory corruption Disclosed report Advanced CVE-2021-43537

heap buffer overflow in nsStructuredCloneContainer::GetDataAsBase64 from integer overflow

A 64-bit length value in nsStructuredCloneContainer::GetDataAsBase64 is truncated to 32 bits before a buffer is sized, so a crafted structured-clone payload can cause a heap buffer overflow. The author states this could be exploited for remote code execution.

bo13oy Follow

Originally disclosed on Mozilla

  • Fixed in 12 days
  • Paid in 12 days
  • Disclosed after 12 days

Created attachment 9248226 poc.zip

Tested Version: Windows 10 1909 x64 memory 16G + firfox 93.0 64-bit.

####Root cause: In functionn sStructuredCloneContainer::GetDataAsBase64, at the position #1, v10 Force conversion from 64-bit to 32-bit, which leads to Integer Overflow vulnerability. This vulnerability can be easily exploited for Remote Code Execution Vulnerability. The patch for this vulnerability is simple, you just need to determine if v10 overflows before calling SetLength.

####Code:

__int64 __fastcall nsStructuredCloneContainer::GetDataAsBase64(__int64 a1, __int64 a2)
{
  __int64 v2; // rsi
  __int64 v3; // rbx
  __int64 v4; // rax
  _QWORD *v5; // rcx
  unsigned int v6; // ebp
  _QWORD *v8; // rcx
  __int64 v9; // rax
  unsigned __int64 v10; // rdi
  __int64 v11; // rax
  signed __int64 v12; // r15
  __int64 v13; // r12
  __int64 v14; // rbx
  size_t v15; // rbp
  int v16; // eax
  __int64 v17; // [rsp+0h] [rbp-D8h]
  void *Src[2]; // [rsp+20h] [rbp-B8h]
  __int128 v19; // [rsp+30h] [rbp-A8h]
  char *v20; // [rsp+40h] [rbp-98h]
  __int64 v21; // [rsp+48h] [rbp-90h]
  int v22; // [rsp+50h] [rbp-88h]
  char v23; // [rsp+54h] [rbp-84h]
  unsigned __int64 v24; // [rsp+98h] [rbp-40h]

  v2 = a2;
  v3 = a1;
  v24 = (unsigned __int64)&v17 ^ _security_cookie;
  nsTSubstring_char16_t_::Truncate(a2);
  v4 = *(_QWORD *)(v3 + 240);
  v5 = (_QWORD *)(v4 + 64);
  if ( !v4 )
    v5 = (_QWORD *)(v3 + 168);
  v6 = -2147467259;
  if ( *v5 && !**(_DWORD **)(v3 + 56) && !**(_DWORD **)(v3 + 64) && !**(_DWORD **)(v3 + 80) && !**(_DWORD **)(v3 + 72) )
  {
    v8 = (_QWORD *)(v4 + 8);
    if ( !v4 )
      v8 = (_QWORD *)(v3 + 112);
    v19 = 0i64;
    *(_OWORD *)Src = 0i64;
    if ( v8[2] )
    {
      v9 = v8[1];
      Src[1] = *(void **)v9;
      *(_QWORD *)&v19 = (char *)Src[1] + *(_QWORD *)(v9 + 8);
    }
    v10 = v8[7];
    v20 = &v23;
    v21 = 844497944576000i64;
    v22 = 63;
    v23 = 0;
    v6 = -2147024882;
    if ( nsTSubstring_char_::SetLength((volatile signed __int32 **)&v20, v10) )
...
    if ( (unsigned __int8)nsTSubstring_char_::SetLength(&v20, (unsigned int)v10, &std::nothrow) ) #1
    {
      v11 = *(_QWORD *)(v3 + 240);
      v12 = v11 + 8;
      if ( !v11 )
        v12 = v3 + 112;
      if ( !(unsigned __int8)nsTSubstring_char_::EnsureMutable(&v20, 0xFFFFFFFFi64) )
        NS_ABORT_OOM((unsigned int)v21);
      if ( v10 )
      {
        v13 = (__int64)v20;
        v14 = 0i64;
        do
        {
          if ( Src[1] > (void *)v19 )
          {
            gMozCrashReason = "MOZ_RELEASE_ASSERT(mData <= mDataEnd)";
            __debugbreak();
            MOZ_NoReturn(209i64);
          }
          v15 = v19 - (unsigned __int64)Src[1];
          if ( v10 < (unsigned __int64)v19 - (unsigned __int64)Src[1] )
            v15 = v10;
          if ( !v15 )
            break;
          if ( Src[1] == (void *)v19 )
          {
            gMozCrashReason = "MOZ_RELEASE_ASSERT(!Done())";
            __debugbreak();
            MOZ_NoReturn(196i64);
          }
          memcpy_0((void *)(v13 + v14), Src[1], v15);
          v14 += v15;
          mozilla::BufferList_js::SystemAllocPolicy_::IterImpl::Advance(Src, v12, v15);
          v10 -= v15;
        }
        while ( v10 );
      }
    ....
}

####OOB Write details:

Debugging Details:
------------------

0:000> r
rax=000001d4af2fe808 rbx=00000000000ad000 rcx=000001d4af2fef20
rdx=000001d4b5f16718 rsi=000001d4b57996a0 rdi=00000000fff53430
rip=00007ffd35bd151e rsp=000000036c7fb7f8 rbp=0000000000001000
 r8=00000000000008e8  r9=ffffffffffffffe8 r10=00007ffd35bd0000
r11=000001d665c00000 r12=000001d4af251808 r13=000000036c7fbaa0
r14=000000036c7fb820 r15=000001d4af205828
iopl=0         nv up ei pl nz na po nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010206
VCRUNTIME140!memcpy+0x22e:
00007ffd`35bd151e c5fd7fa1e0000000 vmovdqa ymmword ptr [rcx+0E0h],ymm4 ds:000001d4`af2ff000=??

0:000> kb
RetAddr           : Args to Child                                                           : Call Site
00007ffd`16446f5b : 00000000`00000000 0000f1a7`6bbca904 000001d4`b5624000 00000003`6c7fb8b0 : VCRUNTIME140!memcpy+0x22e
00007ffd`17083170 : 0000f1a7`6bbca9e4 00000003`6c7fb978 000001d4`b5624088 00000003`6c7fb968 : xul!nsStructuredCloneContainer::GetDataAsBase64+0x18b [/builds/worker/checkouts/gecko/dom/base/nsStructuredCloneContainer.cpp @ 151]
00007ffd`1707f930 : 000001d4`b5750c00 00007ffd`165d8d8c 0000f1a7`6bbca614 fffe02e8`44f009b0 : xul!mozilla::dom::Notification::InitFromJSVal+0x80 [/builds/worker/checkouts/gecko/dom/notification/Notification.cpp @ 1952]
00007ffd`1707f7a0 : 000001d4`b5624068 00000003`6c7fbde8 fff98000`00000000 000001d4`00000c01 : xul!mozilla::dom::Notification::CreateAndShow+0xe0 [/builds/worker/checkouts/gecko/dom/notification/Notification.cpp @ 2234]
00007ffd`165f2a2d : 00000003`6c7fba88 00000000`00000000 00000000`00000002 000001d4`b735c1e0 : xul!mozilla::dom::Notification::Constructor+0x140 [/builds/worker/checkouts/gecko/dom/notification/Notification.cpp @ 779]
00007ffd`157b6066 : 000001d4`b5620001 00000003`6c7fbf00 00000000`00000000 00000000`b5010111 : xul!mozilla::dom::Notification_Binding::_constructor+0x41d [/builds/worker/workspace/obj-build/dom/bindings/NotificationBinding.cpp @ 2212]
00007ffd`159753cb : 00000101`0000fe05 00000003`6c7fc400 00007ffd`18ae8bb0 00000000`00000077 : xul!InternalConstruct+0x296 [/builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp @ 612]

0:000> dd rcx
000001d4`af2fef20  41414141 41414141 41414141 41414141
000001d4`af2fef30  41414141 41414141 41414141 41414141
000001d4`af2fef40  41414141 41414141 41414141 41414141
000001d4`af2fef50  41414141 41414141 41414141 41414141
000001d4`af2fef60  41414141 41414141 41414141 41414141
000001d4`af2fef70  41414141 41414141 41414141 41414141
000001d4`af2fef80  41414141 41414141 41414141 41414141
000001d4`af2fef90  41414141 41414141 41414141 41414141

#########################################################################################################################################

This vuln is discovered by bo13oy of Cyber Kunlun Lab.

Thanks.

The original report as published on Mozilla's Bugzilla, by bo13oy. 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 code passes a 64-bit length to SetLength after casting it to 32 bits, so an oversized value wraps to a small allocation that the subsequent copy overruns.

Impact

The author claims the overflow can be exploited for remote code execution, though the report shows only a crash under a debugger.

What the attacker needed

Firefox 93 on 64-bit Windows; victim opens a crafted page

The fix

The length should be checked for overflow before SetLength is called, as the author suggests.

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.

When a program reserves memory it must know how much to reserve. Here a very large size was squeezed into a smaller number, so the program reserved too little space and then copied more data than fits, overwriting neighbouring memory. Overwriting memory in a browser can 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.

More by bo13oy

View all

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