OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/synchronization/lock_impl.h" | 5 #include "base/synchronization/lock_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/synchronization/lock.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace internal { | 14 namespace internal { |
14 | 15 |
| 16 // Determines which platforms can consider using priority inheritance locks. Use |
| 17 // this define for platform code that may not compile if priority inheritance |
| 18 // locks aren't available. For this platform code, |
| 19 // PRIORITY_INHERITANCE_LOCKS_POSSIBLE() is a necessary but insufficient check. |
| 20 // Lock::PriorityInheritanceAvailable still must be checked as the code may |
| 21 // compile but the underlying platform still may not correctly support priority |
| 22 // inheritance locks. |
| 23 #if defined(OS_NACL) || defined(OS_ANDROID) |
| 24 #define PRIORITY_INHERITANCE_LOCKS_POSSIBLE() 0 |
| 25 #else |
| 26 #define PRIORITY_INHERITANCE_LOCKS_POSSIBLE() 1 |
| 27 #endif |
| 28 |
15 LockImpl::LockImpl() { | 29 LockImpl::LockImpl() { |
16 #ifndef NDEBUG | |
17 // In debug, setup attributes for lock error checking. | |
18 pthread_mutexattr_t mta; | 30 pthread_mutexattr_t mta; |
19 int rv = pthread_mutexattr_init(&mta); | 31 int rv = pthread_mutexattr_init(&mta); |
20 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 32 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 33 #if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() |
| 34 if (PriorityInheritanceAvailable()) { |
| 35 rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); |
| 36 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 37 } |
| 38 #endif |
| 39 #ifndef NDEBUG |
| 40 // In debug, setup attributes for lock error checking. |
21 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); | 41 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
22 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 42 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 43 #endif |
23 rv = pthread_mutex_init(&native_handle_, &mta); | 44 rv = pthread_mutex_init(&native_handle_, &mta); |
24 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 45 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
25 rv = pthread_mutexattr_destroy(&mta); | 46 rv = pthread_mutexattr_destroy(&mta); |
26 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 47 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
27 #else | |
28 // In release, go with the default lock attributes. | |
29 pthread_mutex_init(&native_handle_, NULL); | |
30 #endif | |
31 } | 48 } |
32 | 49 |
33 LockImpl::~LockImpl() { | 50 LockImpl::~LockImpl() { |
34 int rv = pthread_mutex_destroy(&native_handle_); | 51 int rv = pthread_mutex_destroy(&native_handle_); |
35 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 52 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
36 } | 53 } |
37 | 54 |
38 bool LockImpl::Try() { | 55 bool LockImpl::Try() { |
39 int rv = pthread_mutex_trylock(&native_handle_); | 56 int rv = pthread_mutex_trylock(&native_handle_); |
40 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); | 57 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); |
41 return rv == 0; | 58 return rv == 0; |
42 } | 59 } |
43 | 60 |
44 void LockImpl::Lock() { | 61 void LockImpl::Lock() { |
45 int rv = pthread_mutex_lock(&native_handle_); | 62 int rv = pthread_mutex_lock(&native_handle_); |
46 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 63 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
47 } | 64 } |
48 | 65 |
49 void LockImpl::Unlock() { | 66 void LockImpl::Unlock() { |
50 int rv = pthread_mutex_unlock(&native_handle_); | 67 int rv = pthread_mutex_unlock(&native_handle_); |
51 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 68 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
52 } | 69 } |
53 | 70 |
| 71 // static |
| 72 bool LockImpl::PriorityInheritanceAvailable() { |
| 73 #if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() && defined(OS_MACOSX) |
| 74 return true; |
| 75 #else |
| 76 // Security concerns prevent the use of priority inheritance mutexes on Linux. |
| 77 // * CVE-2010-0622 - wake_futex_pi unlocks incorrect, possible DoS. |
| 78 // https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-0622 |
| 79 // * CVE-2012-6647 - Linux < 3.5.1, futex_wait_requeue_pi possible DoS. |
| 80 // https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-6647 |
| 81 // * CVE-2014-3153 - Linux <= 3.14.5, futex_requeue, privilege escalation. |
| 82 // https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-3153 |
| 83 // |
| 84 // If the above were all addressed, we still need a runtime check to deal with |
| 85 // the bug below. |
| 86 // * glibc Bug 14652: https://sourceware.org/bugzilla/show_bug.cgi?id=14652 |
| 87 // Fixed in glibc 2.17. |
| 88 // Priority inheritance mutexes may deadlock with condition variables |
| 89 // during recacquisition of the mutex after the condition variable is |
| 90 // signalled. |
| 91 return false; |
| 92 #endif |
| 93 } |
| 94 |
54 } // namespace internal | 95 } // namespace internal |
55 } // namespace base | 96 } // namespace base |
OLD | NEW |