]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
mutex: Fix robust mutex lock acquire (Bug 21778)
authorCarlos O'Donell <carlos@redhat.com>
Sat, 29 Jul 2017 04:02:03 +0000 (00:02 -0400)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 28 Aug 2017 13:13:07 +0000 (15:13 +0200)
65810f0ef05e8c9e333f17a44e77808b163ca298 fixed a robust mutex bug but
introduced BZ 21778: if the CAS used to try to acquire a lock fails, the
expected value is not updated, which breaks other cases in the loce
acquisition loop.  The fix is to simply update the expected value with
the value returned by the CAS, which ensures that behavior is as if the
first case with the CAS never happened (if the CAS fails).

This is a regression introduced in the last release.

Tested on x86_64, i686, ppc64, ppc64le, s390x, aarch64, armv7hl.

(cherry picked from commit 5920a4a624b1f4db310d1c44997b640e2a4653e5)

ChangeLog
NEWS
nptl/Makefile
nptl/pthread_mutex_lock.c
nptl/pthread_mutex_timedlock.c
nptl/tst-mutex7.c
nptl/tst-mutex7robust.c [new file with mode: 0644]

index 01298daf38fc01b78431acf020632002b83b0c8e..13415f91636fc9a14111aecdf989f48754db3769 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2017-07-29  Torvald Riegel  <triegel@redhat.com>
+           Carlos O'Donell  <carlos@redhat.com>
+
+       [BZ 21778]
+       * nptl/pthread_mutex_timedlock.c (__pthread_mutex_timedlock): Update
+       oldval if the CAS fails.
+       * nptl/pthread_mutex_lock.c (__pthread_mutex_lock_full): Likewise.
+       * nptl/tst-mutex7.c: Add comments explaining template test.
+       (ROBUST, DELAY_NSEC, ROUNDS, N): New.
+       (tf, do_test): Use them.
+       * nptl/tst-mutex7robust.c: New file.
+       * nptl/Makefile (tests): Add new test.
+
 2017-07-28  Torvald Riegel  <triegel@redhat.com>
            Carlos O'Donell  <carlos@redhat.com>
 
diff --git a/NEWS b/NEWS
index 6963f3374b7470b89f4191ca2f4fd17f1d4489a8..f7057710f14d6cf17d3f465261a26a0f6ffe460b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,7 @@ The following bugs are resolved with this release:
   [21298] rwlock can deadlock on frequent reader/writer phase switching
   [21386] Assertion in fork for distinct parent PID is incorrect
   [21624] Unsafe alloca allows local attackers to alias stack and heap (CVE-2017-1000366)
+  [21778] Robust mutex may deadlock
   [21972] assert macro requires operator== (int) for its argument type
 \f
 Version 2.25
index 0cc287305ff32daf4b219819cffc9c0659b1ca2e..24067768edefef84f9250319c38f8720e1e47deb 100644 (file)
@@ -224,6 +224,7 @@ tests = tst-typesizes \
        tst-attr1 tst-attr2 tst-attr3 tst-default-attr \
        tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex5 tst-mutex6 \
        tst-mutex7 tst-mutex8 tst-mutex9 tst-mutex5a tst-mutex7a \
+       tst-mutex7robust \
        tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 tst-mutexpi5 \
        tst-mutexpi5a tst-mutexpi6 tst-mutexpi7 tst-mutexpi7a tst-mutexpi8 \
        tst-mutexpi9 \
index dc9ca4c4764be2654141493330bd8a91a989f601..4425927c300ade6c0fbbba0e486fbb3a7881c229 100644 (file)
@@ -197,11 +197,14 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex)
        {
          /* Try to acquire the lock through a CAS from 0 (not acquired) to
             our TID | assume_other_futex_waiters.  */
-         if (__glibc_likely ((oldval == 0)
-                             && (atomic_compare_and_exchange_bool_acq
-                                 (&mutex->__data.__lock,
-                                  id | assume_other_futex_waiters, 0) == 0)))
-             break;
+         if (__glibc_likely (oldval == 0))
+           {
+             oldval
+               = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
+                   id | assume_other_futex_waiters, 0);
+             if (__glibc_likely (oldval == 0))
+               break;
+           }
 
          if ((oldval & FUTEX_OWNER_DIED) != 0)
            {
index a4beb7b0dca312b627d5725e758a8d07b8232973..dd88cc4ec9dcabde50c7517146b9bca0ce57f6bd 100644 (file)
@@ -154,11 +154,14 @@ pthread_mutex_timedlock (pthread_mutex_t *mutex,
        {
          /* Try to acquire the lock through a CAS from 0 (not acquired) to
             our TID | assume_other_futex_waiters.  */
-         if (__glibc_likely ((oldval == 0)
-                             && (atomic_compare_and_exchange_bool_acq
-                                 (&mutex->__data.__lock,
-                                  id | assume_other_futex_waiters, 0) == 0)))
-             break;
+         if (__glibc_likely (oldval == 0))
+           {
+             oldval
+               = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
+                   id | assume_other_futex_waiters, 0);
+             if (__glibc_likely (oldval == 0))
+               break;
+           }
 
          if ((oldval & FUTEX_OWNER_DIED) != 0)
            {
index a11afdba5e0ef27ddcb447006f4b623e5765d0e0..08fe251eebba8c7e0ca4b2b95bbe0804f8d69a4a 100644 (file)
 #include <stdlib.h>
 #include <time.h>
 
-
+/* This test is a template for other tests to use.  Other tests define
+   the following macros to change the behaviour of the template test.
+   The test is very simple, it configures N threads given the parameters
+   below and then proceeds to go through mutex lock and unlock
+   operations in each thread as described before for the thread
+   function.  */
 #ifndef TYPE
 # define TYPE PTHREAD_MUTEX_DEFAULT
 #endif
-
+#ifndef ROBUST
+# define ROBUST PTHREAD_MUTEX_STALLED
+#endif
+#ifndef DELAY_NSEC
+# define DELAY_NSEC 11000
+#endif
+#ifndef ROUNDS
+# define ROUNDS 1000
+#endif
+#ifndef N
+# define N 100
+#endif
 
 static pthread_mutex_t lock;
 
-
-#define ROUNDS 1000
-#define N 100
-
-
+/* Each thread locks and the subsequently unlocks the lock, yielding
+   the smallest critical section possible.  After the unlock the thread
+   waits DELAY_NSEC nanoseconds before doing the lock and unlock again.
+   Every thread does this ROUNDS times.  The lock and unlock are
+   checked for errors.  */
 static void *
 tf (void *arg)
 {
   int nr = (long int) arg;
   int cnt;
-  struct timespec ts = { .tv_sec = 0, .tv_nsec = 11000 };
+  struct timespec ts = { .tv_sec = 0, .tv_nsec = DELAY_NSEC };
 
   for (cnt = 0; cnt < ROUNDS; ++cnt)
     {
@@ -56,13 +72,16 @@ tf (void *arg)
          return (void *) 1l;
        }
 
-      nanosleep (&ts, NULL);
+      if ((ts.tv_sec > 0) || (ts.tv_nsec > 0))
+       nanosleep (&ts, NULL);
     }
 
   return NULL;
 }
 
-
+/* Setup and run N threads, where each thread does as described
+   in the above thread function.  The threads are given a minimal 1MiB
+   stack since they don't do anything between the lock and unlock.  */
 static int
 do_test (void)
 {
@@ -80,6 +99,12 @@ do_test (void)
       exit (1);
     }
 
+  if (pthread_mutexattr_setrobust (&a, ROBUST) != 0)
+    {
+      puts ("mutexattr_setrobust failed");
+      exit (1);
+    }
+
 #ifdef ENABLE_PI
   if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
     {
diff --git a/nptl/tst-mutex7robust.c b/nptl/tst-mutex7robust.c
new file mode 100644 (file)
index 0000000..8221a61
--- /dev/null
@@ -0,0 +1,7 @@
+/* Bug 21778: Fix oversight in robust mutex lock acquisition.  */
+#define TYPE PTHREAD_MUTEX_NORMAL
+#define ROBUST PTHREAD_MUTEX_ROBUST
+#define DELAY_NSEC 0
+#define ROUNDS 1000
+#define N 32
+#include "tst-mutex7.c"