Skip to content
Race condition Disclosed report CVE-2016-1973

Race condition in GetStaticInstance can cause use after free

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 4 months
  • Paid in 2 months
  • Disclosed after 4 months
Text
GetStaticInstance (media\webrtc\trunk\webrtc\system_wrappers\interface\static_instance.h) can experience a race condition. If this occurs, it spuriously decrements its reference count, causing the object protected by it to be destroyed and its memory to be released before all of the code streams using it have been terminated.


Details
-------

The bug is in the handling of the kAddRef case (which is used in at least 2 places in the codebase [1]), and is illustrated by a thread race graph below the affected code:

34: template <class T>
35: // Construct On First Use idiom. Avoids
36: // "static initialization order fiasco".
37: static T* GetStaticInstance(CountOperation count_operation) {
38:   // TODO (hellner): use atomic wrapper instead.
39:   static volatile long instance_count = 0;
40:   static T* volatile instance = NULL;
41:   CreateOperation state = kInstanceExists;
42: #ifndef _WIN32
...
89: #else  // _WIN32
...
107:   } else if (count_operation == kAddRef) {
108:     if (instance_count == 0) {
109:       state = kCreate;
110:     } else {
111:       if (1 == InterlockedIncrement(&instance_count)) {
112:         // InterlockedDecrement because reference count should not be
113:         // updated just yet (that's done when the instance is created).
114:         InterlockedDecrement(&instance_count);
115:         state = kCreate;
116:       }
117:     }
118:   } else { [kRelease]
119:     int new_value = InterlockedDecrement(&instance_count);
120:     if (new_value == 0) {
121:       state = kDestroy;
122:     }
123:   }
124: 
125:   if (state == kCreate) {
126:     // Create instance and let whichever thread finishes first assign its
127:     // local copy to the global instance. All other threads reclaim their
128:     // local copy.
129:     T* new_instance = T::CreateInstance();
130:     if (1 == InterlockedIncrement(&instance_count)) {
131:       InterlockedExchangePointer(reinterpret_cast<void * volatile*>(&instance),
132:                                  new_instance);
133:     } else {
134:       InterlockedDecrement(&instance_count);
135:       if (new_instance) {
136:         delete static_cast<T*>(new_instance);
137:       }
138:     }
139:   } else if (state == kDestroy) {
140:     T* old_value = static_cast<T*>(InterlockedExchangePointer(
141:         reinterpret_cast<void * volatile*>(&instance), NULL));
142:     if (old_value) {
143:       delete static_cast<T*>(old_value);
144:     }
145:     return NULL;
146:   }
147: #endif  // #ifndef _WIN32
148:   return instance;
149: }


Assume |instance_count == 0| and |instance == nullptr| initially, and |count_operation == kAddRef| for both threads:

Thread 1                                       Thread 2
--------                                       --------

Execute lines 107-09, notice that              Execute lines 107-09, notice that     
|instance_count == 0| and thus set             |instance_count == 0| and thus set
|state = kCreate|                              |state = kCreate|

Execute lines 125-29, notice that              Execute lines 125-29, notice that     
|state == kCreate| and create a                |state == kCreate| and create a
new instance of T into |*new_instance|         new instance of T into |*new_instance|

Execute line 130, notice that
|instance_count == 1|. Execute lines 131-32,
replacing |instance| with |new_instance|,
then execute line 148,
returning |instance| to the caller.

                                               Execute line 130, notice that |instance_count == 2|,
                                               and execute lines 134-37, decrementing |instance_count| to 1,
                                               and deleting |new_instance|, then execute line 148,
                                               returning |instance| to the caller.

Use |instance| for awhile...                   Use |instance| for awhile...

Call GetStaticInstance with |kRelease|.
Execute lines 119-21, decrementing
|instance_count| to 0, noticing that,
and setting |state = kDestroy|.
Execute lines 140-41 (nulling |instance|)
and lines 142-43 (destroying and freeing
|instance|'s old value.

                                               Continue using the now-destroyed and freed |instance|.


BTW, I am also unsure whether the ordinary reads of |instance_count| and |instance| are compatible with the interlocked operations performed on those variables under all relevant architectures.

This bug is still present in the current trunk: http://hg.mozilla.org/mozilla-central/file/fc706d376f06/media/webrtc/trunk/webrtc/system_wrappers/interface/static_instance.h .

[1] media\webrtc\trunk\webrtc\modules\rtc_rtcp\source\ssrc_database.cc and media\webrtc\trunk\webrtc\system_wrappers\source\trace_impl.cc .
    It is also used in the test code media\webrtc\trunk\webrtc\test\channel_transport\udp_socket_manager_wrapper.cc .

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.

What is known

A high-severity race condition bug in WebRTC: Audio/Video. Mozilla made it public on 16 February 2016 as CVE-2016-1973.

About Race condition

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

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.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.