Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Mon, 10 Dec 2012 13:05:09 -0500
From: Rich Felker <>
Subject: stdio self-synchronized destruction: does it need fixing?

Hi all,
Question about an issue that seems purely theoretical:
self-synchronized destruction of stdio FILEs. Does anything need to be

The basic self-synchronized destruction scenario that affects all
synchronization primitives is: Thread A is waiting to acquire the
resource with the knowledge that there is presently at most one other
thread using it. Thread B releases the lock, then checks to see if
there are any waiters to wake up. But between these two steps, it's
possible that thread A already woke up and destroyed the object, in
which case, reading the waiters field is an invalid read.

glibc has a number of such bugs, but I fixed most of them in musl a
long time ago. (Note that this is a race condition that takes
somewhere on the order of years to hit, unless you artificially poke
at the timing; that's why it doesn't get much attention.) Anyway, I
recently realized that musl still has the issue for stdio FILE
streams. If thread B is holding a lock with flockfile and thread A
calls fclose on the locked stream, it's possible that when thread B
unlocks the stream, thread A gets to free it before thread B can check
to see if there are waiters that need to be woken.

"Fixing" the issue would not be too hard, but since it's nontrivial,
I'd like to consider whether the fix is actually necessary. Unlike
mutexes or semaphores, FILEs do not exist in application-controlled
memory. The allocation of FILE structues is always performed by libc,
and always happens via malloc with a small-size allocation, which
means the memory is managed as part of the heap and never unmapped
once it's mapped. Thus, as far as I can tell, the worst that can
happen is a read-only access to memory no longer owned by the FILE,
but that's still valid, and in this case, it doesn't matter whether a
wakeup futex event is generated, since there can be no waiters;
spurious wakeup events are harmless here.

Any thoughts by others on the matter?


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.