Race condition in glibc/NPTL/Linux robust mutexes?

前端 未结 2 871
刺人心
刺人心 2021-02-05 11:22

In a comment on the question Automatically release mutex on crashes in Unix back in 2010, jilles claimed:

glibc\'s robust mutexes are so fast because glib

相关标签:
2条回答
  • 2021-02-05 11:44

    The description of the race by FreeBSD pthread developer David Xu: http://lists.freebsd.org/pipermail/svn-src-user/2010-November/003668.html

    I don't think the munmap/mmap cycle is strictly required for the race. The piece of shared memory might be put to a different use as well. This is uncommon but valid.

    As also mentioned in that message, more "fun" occurs if threads with different privilege access a common robust mutex. Because the node for the list of owned robust mutexes is in the mutex itself, a thread with low privilege may corrupt a high privilege thread's list. This could be exploited easily to make the high privilege thread crash and in rare cases this might allow the high privilege thread's memory to be corrupted. Apparently Linux's robust mutexes are only designed for use by threads with the same privileges. This could have been avoided easily by making the robust list an array fully in the thread's memory instead of a linked list.

    0 讨论(0)
  • 2021-02-05 11:46

    I think I found the race, and it is indeed very ugly. It goes like this:

    Thread A has held the robust mutex and unlocks it. The basic procedure is:

    1. Put it in the "pending" slot of the thread's robust list header.
    2. Remove it from the linked list of robust mutexes held by the current thread.
    3. Unlock the mutex.
    4. Clear the "pending" slot of the thread's robust list header.

    The problem is that between steps 3 and 4, another thread in the same process could obtain the mutex, then unlock it, and (rightly) believing itself to be the final user of the mutex, destroy and free/munmap it. After that, if any thread in the process creates a shared mapping of a file, device, or shared memory and it happens to get assigned the same address, and the value at that location happens to match the pid of the thread that's still between steps 3 and 4 of unlocking, you have a situation whereby, if the process is killed, the kernel will corrupt the mapped file by setting the high bit of a 32-bit integer it thinks is the mutex owner id.

    The solution is to hold a global lock on mmap/munmap between steps 2 and 4 above, exactly the same as in my solution to the barrier issue described in my answer to this question:

    Can a correct fail-safe process-shared barrier be implemented on Linux?

    0 讨论(0)
提交回复
热议问题