Skip to content
Memory corruption Disclosed report Advanced

Use after free and undefined behavior in MediaType

The MediaType constructors in WebRTC's Windows video capture code ignore the return value of Assign(), so when memory allocation fails the destination object never takes a reference on its pUnk member. The source object's release then frees that object while the copy still points at it, giving a use-after-free; the report also notes undefined behaviour from memcpy into a derived object.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 22 days
  • Paid in 28 days
  • Disclosed after 22 days
Text
`MediaType::Assign()` (media/webrtc/trunk/webrtc/video_capture/windows/MediaType.cpp) has use-after-free bugs and undefined behavior.

----------
The use-after-free is in the constructors 

'''
   22: MediaType::MediaType(const MediaType& aMediaType)
   23: {
   24:   memset(this, 0, sizeof(MediaType));
   25:   Assign(aMediaType);
   26: }
   27: 
   28: 
   29: MediaType::MediaType(const AM_MEDIA_TYPE* aMediaType)
   30: {
   31:   memset(this, 0, sizeof(MediaType));
   32:   Assign(aMediaType);
   33: }
'''

because both functions don't check the return value from `MediaType::Assign()`. If `Assign()` fails to allocate memory, it skips adding a reference to the destination object's `pUnk` member (lines 89-91; 95-6):

'''
   72: HRESULT
   73: MediaType::Assign(const AM_MEDIA_TYPE* aMediaType)
   74: {
   75:   if (!aMediaType)
   76:     return E_POINTER;
   77: 
   78:   if (aMediaType == this)
   79:     return S_OK;
   80: 
   81:   // Release old data...
   82:   Clear();
   83: 
   84:   // Shallow copy.
   85:   memcpy(this, aMediaType, sizeof(AM_MEDIA_TYPE));
   86: 
   87:   // Create deep copy of incoming data...
   88:   if (cbFormat) {
   89:     pbFormat = (BYTE*)CoTaskMemAlloc(cbFormat);
   90:     if (!pbFormat)
   91:       return E_OUTOFMEMORY;
   92:     memcpy(pbFormat, aMediaType->pbFormat, cbFormat);
   93:   }
   94: 
   95:   if (pUnk)
   96:     pUnk->AddRef();
   97: 
   98:   return S_OK;
   99: }
'''

This means that the destination object's `pUnk` object will be freed when the source object `aMediaType` is deleted.

There is also a similar bug in media/webrtc/trunk/webrtc/modules/video_capture/windows/BasePin.cpp line 647:

'''
   638: STDMETHODIMP
   639: BasePin::ConnectionMediaType(AM_MEDIA_TYPE *aMediaType)
   640: {
   641:   if (!aMediaType)
   642:     return E_POINTER;
   643: 
   644:   CriticalSectionAutoEnter monitor(*mLock);
   645: 
   646:   if (IsConnected()) {
   647:     reinterpret_cast<MediaType*>(aMediaType)->Assign(&mMediaType);
   648:     return S_OK;
   649:   } else {
   650:     memset(aMediaType, 0, sizeof(AM_MEDIA_TYPE));
   651:     return VFW_E_NOT_CONNECTED;
   652:   }
   653: }
'''

I will try to produce a POC; gotta get a webcam.


----------
MediaType::Assign() also invokes undefined behavior on

'''
   85: memcpy(this, aMediaType, sizeof(AM_MEDIA_TYPE));
'''

because the location of the base-class subobject `AM_MEDIA_TYPE` inside the derived-class object `MediaType` is implementation-defined. Per C++17 s.13(5) "The order in which the base class subobjects are allocated in the most derived object (4.5) is unspecified."

The original report as published on Mozilla's Bugzilla, by mozillabugs. 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

Impact

A use-after-free could let an attacker who can influence the media type or exhaust memory cause a crash or potentially memory corruption in the video capture component; the report does not demonstrate exploitation.

What the attacker needed

Windows WebRTC video capture; memory allocation failure; attacker-influenced media type

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 C++ code that copies a description of a video stream. When copying, the code is supposed to register that it is now also using a shared object, so the object is not deleted while still in use. If a memory allocation fails partway through, that registration is skipped, and the shared object can be freed while the copy still refers to it, which can crash or corrupt memory.

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 mozillabugs

View all

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