Race condition in |once| can cause use after free
- Fixed in 21 days
- Paid in 23 days
- Disclosed after 21 days
|once| (media\libvpx\vpx_ports\vpx_once.h) has a race condition that can cause it to read and write to a freed object.
This function is used by various en/decoder initialization functions in media\libvpx . I don't know whether any of these functions are, at present, called in more than one thread context.
Details
-------
The bug is in the handling of the deletion of the critical section object:
19: static void once(void (*func)(void))
20: {
21: static CRITICAL_SECTION *lock;
22: static LONG waiters;
23: static int done;
24: void *lock_ptr = &lock;
25:
26: /* If the initialization is complete, return early. This isn't just an
27: * optimization, it prevents races on the destruction of the global
28: * lock.
29: */
30: if(done)
31: return;
32:
33: InterlockedIncrement(&waiters);
34:
35: /* Get a lock. We create one and try to make it the one-true-lock,
36: * throwing it away if we lost the race.
37: */
38:
39: {
40: /* Scope to protect access to new_lock */
41: CRITICAL_SECTION *new_lock = malloc(sizeof(CRITICAL_SECTION));
42: InitializeCriticalSection(new_lock);
43: if (InterlockedCompareExchangePointer(lock_ptr, new_lock, NULL) != NULL)
44: {
45: DeleteCriticalSection(new_lock);
46: free(new_lock);
47: }
48: }
49:
50: /* At this point, we have a lock that can be synchronized on. We don't
51: * care which thread actually performed the allocation.
52: */
53:
54: EnterCriticalSection(lock);
55:
56: if (!done)
57: {
58: func();
59: done = 1;
60: }
61:
62: LeaveCriticalSection(lock);
63:
64: /* Last one out should free resources. The destructed objects are
65: * protected by checking if(done) above.
66: */
67: if(!InterlockedDecrement(&waiters))
68: {
69: DeleteCriticalSection(lock);
70: free(lock);
71: lock = NULL;
72: }
73: }
such that the following race can unfold, assuming |done == 0| and |waiters == 0| initially:
thread 1 thread 2
-------- --------
execute up to (but not into)
line 33, then get preempted
execute through line 67,
find that |waiters| is now 0,
then get preempted
resume execution at line 33,
skip creating a new critical
section object in lines 39-48
(because the existing one is
still pointed to by |lock|)
then get preempted before
executing line 54
resume execution at line 68,
execute lines 69-70 (deleting and freeing
the critical section), then get
preempted
resume execution at line 54,
calling |EnterCriticalSection|
with a deleted and freed critical
section object
There are also some other problems in this code.
1. The |lock = NULL| on line 71 (which does not use an interlocked instruction) is not properly interlocked with respect to |InterlockedCompareExchangePointer| on line 43.
2. The early-out check at lines 30-31 is questionable. I wonder whether the interlocked instruction presumably executed by LeaveCriticalSection on line 62 necessarily forces all processors in the system (under all architectures) to read the updated value of |done|, though this appears to be the case on Intel hardware; see http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html s.8.2.3.9.
3. |once| is hazardous because nothing prevents it from being called with different function objects in the same compilation unit. In this case, the first use would set |done == 1|, causing the second use to be skipped:
void buggy () {
once (f);
once (g); // g() is skipped
}
4. |once| also allows its function object to be executed more than once if |once| is called with the same function object in different compilation units:
unit1.cpp
---------
#include "vpx_once.h"
void f ();
void z1 () {
once (f); // f() is called
}
unit2.cpp
---------
#include "vpx_once.h"
void f ();
void z2 () {
once (f); // f() is called again
}
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 low-severity race condition bug in Audio/Video. Mozilla made it public on 14 November 2015 as CVE-2016-1972.
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.