|
|
37667d |
From 517d5c245c9805b56f73c7fa0e23e8853fe22da6 Mon Sep 17 00:00:00 2001
|
|
|
37667d |
From: Artem Savkov <asavkov@redhat.com>
|
|
|
37667d |
Date: Fri, 21 May 2021 14:20:32 +0200
|
|
|
37667d |
Subject: [RHEL7.9 KPATCH] CVE-2021-3347 Use after free via PI futex state
|
|
|
37667d |
|
|
|
37667d |
Kernels:
|
|
|
37667d |
3.10.0-1160.el7
|
|
|
37667d |
3.10.0-1160.2.1.el7
|
|
|
37667d |
3.10.0-1160.2.2.el7
|
|
|
37667d |
3.10.0-1160.6.1.el7
|
|
|
37667d |
3.10.0-1160.11.1.el7
|
|
|
37667d |
3.10.0-1160.15.2.el7
|
|
|
37667d |
3.10.0-1160.21.1.el7
|
|
|
37667d |
3.10.0-1160.24.1.el7
|
|
|
37667d |
3.10.0-1160.25.1.el7
|
|
|
37667d |
|
|
|
37667d |
Changes since last build:
|
|
|
37667d |
[x86_64]:
|
|
|
37667d |
futex.o: changed function: do_futex
|
|
|
37667d |
futex.o: changed function: fixup_owner
|
|
|
37667d |
futex.o: changed function: fixup_pi_state_owner.isra.16
|
|
|
37667d |
futex.o: changed function: free_pi_state
|
|
|
37667d |
futex.o: changed function: futex_lock_pi.isra.20
|
|
|
37667d |
futex.o: changed function: futex_wait_requeue_pi.constprop.22
|
|
|
37667d |
futex.o: new function: pi_state_update_owner
|
|
|
37667d |
|
|
|
37667d |
[ppc64le]:
|
|
|
37667d |
futex.o: changed function: do_futex
|
|
|
37667d |
futex.o: changed function: fixup_owner
|
|
|
37667d |
futex.o: changed function: fixup_pi_state_owner.isra.9
|
|
|
37667d |
futex.o: changed function: free_pi_state
|
|
|
37667d |
futex.o: changed function: futex_lock_pi.isra.16
|
|
|
37667d |
futex.o: changed function: futex_wait_requeue_pi.constprop.17
|
|
|
37667d |
futex.o: changed function: unqueue_me_pi
|
|
|
37667d |
futex.o: new function: pi_state_update_owner
|
|
|
37667d |
|
|
|
37667d |
---------------------------
|
|
|
37667d |
|
|
|
37667d |
Modifications: added -fno-optimize-sibling-calls to fixup_owner()
|
|
|
37667d |
|
|
|
37667d |
commit d2fb2a9cf682bdba4b66103fb079c13a04039430
|
|
|
37667d |
Author: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
Date: Thu May 20 16:35:49 2021 -0400
|
|
|
37667d |
|
|
|
37667d |
futex: Handle faults correctly for PI futexes
|
|
|
37667d |
|
|
|
37667d |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1935108
|
|
|
37667d |
Upstream status: 34b1a1ce1458f50ef27c54e28eb9b1947012907a
|
|
|
37667d |
CVE: CVE-2021-3347
|
|
|
37667d |
|
|
|
37667d |
Conflicts:
|
|
|
37667d |
The original patch is intent to make the state of rtmutex and pi_state consistent
|
|
|
37667d |
if the kernel is unable to update the user space futex word, rather than unlocking
|
|
|
37667d |
the rtmutex and leaving pi_state out of synched. As a result, this original fix
|
|
|
37667d |
removed part of the code which was introduced by 16ffa12d7 ("futex: Pull
|
|
|
37667d |
rt_mutex_futex_unlock() out from under hb->lock") to the functions futex_lock_pi()
|
|
|
37667d |
and futex_wait_requeue_pi() to avoid the inconsistency. So the conflicts are related
|
|
|
37667d |
to the following two commits, though git blame displayed a much longer list which
|
|
|
37667d |
shows the chain of dependency in the history.
|
|
|
37667d |
|
|
|
37667d |
16ffa12d7425 ("futex: Pull rt_mutex_futex_unlock() out from under hb->lock")
|
|
|
37667d |
c236c8e95a3d ("futex: Fix potential use-after-free in FUTEX_REQUEUE_PI")
|
|
|
37667d |
|
|
|
37667d |
commit 34b1a1ce1458f50ef27c54e28eb9b1947012907a
|
|
|
37667d |
Author: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Date: Mon, 18 Jan 2021 19:01:21 +0100
|
|
|
37667d |
|
|
|
37667d |
futex: Handle faults correctly for PI futexes
|
|
|
37667d |
|
|
|
37667d |
fixup_pi_state_owner() tries to ensure that the state of the rtmutex,
|
|
|
37667d |
pi_state and the user space value related to the PI futex are consistent
|
|
|
37667d |
before returning to user space. In case that the user space value update
|
|
|
37667d |
faults and the fault cannot be resolved by faulting the page in via
|
|
|
37667d |
fault_in_user_writeable() the function returns with -EFAULT and leaves
|
|
|
37667d |
the rtmutex and pi_state owner state inconsistent.
|
|
|
37667d |
|
|
|
37667d |
A subsequent futex_unlock_pi() operates on the inconsistent pi_state and
|
|
|
37667d |
releases the rtmutex despite not owning it which can corrupt the RB tree of
|
|
|
37667d |
the rtmutex and cause a subsequent kernel stack use after free.
|
|
|
37667d |
|
|
|
37667d |
It was suggested to loop forever in fixup_pi_state_owner() if the fault
|
|
|
37667d |
cannot be resolved, but that results in runaway tasks which is especially
|
|
|
37667d |
undesired when the problem happens due to a programming error and not due
|
|
|
37667d |
to malice.
|
|
|
37667d |
|
|
|
37667d |
As the user space value cannot be fixed up, the proper solution is to make
|
|
|
37667d |
the rtmutex and the pi_state consistent so both have the same owner. This
|
|
|
37667d |
leaves the user space value out of sync. Any subsequent operation on the
|
|
|
37667d |
futex will fail because the 10th rule of PI futexes (pi_state owner and
|
|
|
37667d |
user space value are consistent) has been violated.
|
|
|
37667d |
|
|
|
37667d |
As a consequence this removes the inept attempts of 'fixing' the situation
|
|
|
37667d |
in case that the current task owns the rtmutex when returning with an
|
|
|
37667d |
unresolvable fault by unlocking the rtmutex which left pi_state::owner and
|
|
|
37667d |
rtmutex::owner out of sync in a different and only slightly less dangerous
|
|
|
37667d |
way.
|
|
|
37667d |
|
|
|
37667d |
Fixes: 1b7558e457ed ("futexes: fix fault handling in futex_lock_pi")
|
|
|
37667d |
Reported-by: gzobqq@gmail.com
|
|
|
37667d |
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
|
|
|
37667d |
Cc: stable@vger.kernel.org
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
|
|
|
37667d |
commit 25077b49b47c1cdf224b54c837172ff820e8be88
|
|
|
37667d |
Author: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
Date: Thu May 20 16:30:16 2021 -0400
|
|
|
37667d |
|
|
|
37667d |
futex: Provide and use pi_state_update_owner()
|
|
|
37667d |
|
|
|
37667d |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1935108
|
|
|
37667d |
Upstream status: c5cade200ab9a2a3be9e7f32a752c8d86b502ec7
|
|
|
37667d |
CVE: CVE-2021-3347
|
|
|
37667d |
|
|
|
37667d |
Conflicts:
|
|
|
37667d |
Updating the owner of pi_state requires that we remove the pi_state structure from
|
|
|
37667d |
the old owner's pi_state_list then add it to the new owner's pi_state_list. Because
|
|
|
37667d |
this action takes place in multiple occassions in the current upstream futex.c, so
|
|
|
37667d |
the similar code is duplicated in all these places. The purpose of this patch is to
|
|
|
37667d |
eliminate these code duplications with a new routine pi_state_update_owner().
|
|
|
37667d |
|
|
|
37667d |
The conflicts in 7.9.z are caused by the differences in places where updating owner
|
|
|
37667d |
takes place. After sorting out the details, the relevant commit IDs as below :
|
|
|
37667d |
|
|
|
37667d |
734009e96d19 ("futex: Change locking rules")
|
|
|
37667d |
b4abf91047cf ("rtmutex: Make wait_lock irq safe")
|
|
|
37667d |
|
|
|
37667d |
commit c5cade200ab9a2a3be9e7f32a752c8d86b502ec7
|
|
|
37667d |
Author: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Date: Tue, 19 Jan 2021 15:21:35 +0100
|
|
|
37667d |
|
|
|
37667d |
futex: Provide and use pi_state_update_owner()
|
|
|
37667d |
|
|
|
37667d |
Updating pi_state::owner is done at several places with the same
|
|
|
37667d |
code. Provide a function for it and use that at the obvious places.
|
|
|
37667d |
|
|
|
37667d |
This is also a preparation for a bug fix to avoid yet another copy of the
|
|
|
37667d |
same code or alternatively introducing a completely unpenetratable mess of
|
|
|
37667d |
gotos.
|
|
|
37667d |
|
|
|
37667d |
Originally-by: Peter Zijlstra <peterz@infradead.org>
|
|
|
37667d |
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
|
|
|
37667d |
Cc: stable@vger.kernel.org
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
|
|
|
37667d |
commit 69414a50f8bad2063b89981110fb374733209d9d
|
|
|
37667d |
Author: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
Date: Wed May 19 14:24:04 2021 -0400
|
|
|
37667d |
|
|
|
37667d |
futex: Replace pointless printk in fixup_owner()
|
|
|
37667d |
|
|
|
37667d |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1935108
|
|
|
37667d |
Upstream status: 04b79c55201f02ffd675e1231d731365e335c307
|
|
|
37667d |
CVE: CVE-2021-3347
|
|
|
37667d |
|
|
|
37667d |
commit 04b79c55201f02ffd675e1231d731365e335c307
|
|
|
37667d |
Author: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Date: Tue, 19 Jan 2021 16:06:10 +0100
|
|
|
37667d |
|
|
|
37667d |
futex: Replace pointless printk in fixup_owner()
|
|
|
37667d |
|
|
|
37667d |
If that unexpected case of inconsistent arguments ever happens then the
|
|
|
37667d |
futex state is left completely inconsistent and the printk is not really
|
|
|
37667d |
helpful. Replace it with a warning and make the state consistent.
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
|
|
|
37667d |
Cc: stable@vger.kernel.org
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
|
|
|
37667d |
commit 7e96fb06469c95628039ead2591f82e88af5da10
|
|
|
37667d |
Author: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
Date: Wed May 19 14:19:05 2021 -0400
|
|
|
37667d |
|
|
|
37667d |
futex: Ensure the correct return value from futex_lock_pi()
|
|
|
37667d |
|
|
|
37667d |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1935108
|
|
|
37667d |
Upstream status: 12bb3f7f1b03d5913b3f9d4236a488aa7774dfe9
|
|
|
37667d |
CVE: CVE-2021-3347
|
|
|
37667d |
|
|
|
37667d |
Conflicts:
|
|
|
37667d |
This original upstream patch relies heavily on c1e2f0eaf015 ("futex: Avoid
|
|
|
37667d |
violating the 10th rule of futex") which is one of the upstream commits listed
|
|
|
37667d |
below. But the backport for c1e2f0eaf015 requires we resolve very complex chain
|
|
|
37667d |
of dependencies across multiple critical kernel source files therefore the risk
|
|
|
37667d |
is considered too high for 7.9.z.
|
|
|
37667d |
|
|
|
37667d |
Instead of pulling together tons of the relevant commits in to 7.9.z, we just
|
|
|
37667d |
want to take a light risk approach by digesting the fix 12bb3f7f1b03 ("futex:
|
|
|
37667d |
Ensure the correct return value from futex_lock_pi()") for 7.9.z. All we need
|
|
|
37667d |
to do is to make the changed functions fixup_owner() and fixup_pi_state_owner()
|
|
|
37667d |
of 7.9.z return the required values as this upstream fix suggests in every
|
|
|
37667d |
circumstance. This way, we can cleanly cut this CVE patch set with merely four
|
|
|
37667d |
patches, without having to backport tons of patches in the chain of dependency.
|
|
|
37667d |
|
|
|
37667d |
Besides, an extra change made to fixup_owner() (see HUNK -2063,13 +2062,11 in
|
|
|
37667d |
this backport patch) is to eliminate a mistake made by upstream, where the
|
|
|
37667d |
specification of a local variable "ret" was removed from that function, but
|
|
|
37667d |
there was still a dereference to "ret" as shown by that HUNK.
|
|
|
37667d |
|
|
|
37667d |
16ffa12d7425 ("futex: Pull rt_mutex_futex_unlock() out from under hb->lock")
|
|
|
37667d |
c1e2f0eaf015 ("futex: Avoid violating the 10th rule of futex")
|
|
|
37667d |
734009e96d19 ("futex: Change locking rules")
|
|
|
37667d |
d7c5ed73b19c ("futex: Remove needless goto's")
|
|
|
37667d |
6b4f4bc9cb22 ("locking/futex: Allow low-level atomic operations to return -EAGAIN")
|
|
|
37667d |
|
|
|
37667d |
commit 12bb3f7f1b03d5913b3f9d4236a488aa7774dfe9
|
|
|
37667d |
Author: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Date: Wed, 20 Jan 2021 16:00:24 +0100
|
|
|
37667d |
|
|
|
37667d |
futex: Ensure the correct return value from futex_lock_pi()
|
|
|
37667d |
|
|
|
37667d |
In case that futex_lock_pi() was aborted by a signal or a timeout and the
|
|
|
37667d |
task returned without acquiring the rtmutex, but is the designated owner of
|
|
|
37667d |
the futex due to a concurrent futex_unlock_pi() fixup_owner() is invoked to
|
|
|
37667d |
establish consistent state. In that case it invokes fixup_pi_state_owner()
|
|
|
37667d |
which in turn tries to acquire the rtmutex again. If that succeeds then it
|
|
|
37667d |
does not propagate this success to fixup_owner() and futex_lock_pi()
|
|
|
37667d |
returns -EINTR or -ETIMEOUT despite having the futex locked.
|
|
|
37667d |
|
|
|
37667d |
Return success from fixup_pi_state_owner() in all cases where the current
|
|
|
37667d |
task owns the rtmutex and therefore the futex and propagate it correctly
|
|
|
37667d |
through fixup_owner(). Fixup the other callsite which does not expect a
|
|
|
37667d |
positive return value.
|
|
|
37667d |
|
|
|
37667d |
Fixes: c1e2f0eaf015 ("futex: Avoid violating the 10th rule of futex")
|
|
|
37667d |
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
|
|
|
37667d |
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
|
|
|
37667d |
Cc: stable@vger.kernel.org
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Donghai Qiao <dqiao@redhat.com>
|
|
|
37667d |
|
|
|
37667d |
Signed-off-by: Artem Savkov <asavkov@redhat.com>
|
|
|
37667d |
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
|
|
|
37667d |
Acked-by: Yannick Cote <ycote@redhat.com>
|
|
|
37667d |
---
|
|
|
37667d |
kernel/futex.c | 123 +++++++++++++++++++++++++------------------------
|
|
|
37667d |
1 file changed, 63 insertions(+), 60 deletions(-)
|
|
|
37667d |
|
|
|
37667d |
diff --git a/kernel/futex.c b/kernel/futex.c
|
|
|
37667d |
index 877831775d7aa..8ec57c357ca58 100644
|
|
|
37667d |
--- a/kernel/futex.c
|
|
|
37667d |
+++ b/kernel/futex.c
|
|
|
37667d |
@@ -640,6 +640,29 @@ static struct futex_pi_state * alloc_pi_state(void)
|
|
|
37667d |
return pi_state;
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
+static void pi_state_update_owner(struct futex_pi_state *pi_state,
|
|
|
37667d |
+ struct task_struct *new_owner)
|
|
|
37667d |
+{
|
|
|
37667d |
+ struct task_struct *old_owner = pi_state->owner;
|
|
|
37667d |
+
|
|
|
37667d |
+ lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
|
|
|
37667d |
+
|
|
|
37667d |
+ if (old_owner) {
|
|
|
37667d |
+ raw_spin_lock_irq(&old_owner->pi_lock);
|
|
|
37667d |
+ WARN_ON(list_empty(&pi_state->list));
|
|
|
37667d |
+ list_del_init(&pi_state->list);
|
|
|
37667d |
+ raw_spin_unlock_irq(&old_owner->pi_lock);
|
|
|
37667d |
+ }
|
|
|
37667d |
+
|
|
|
37667d |
+ if (new_owner) {
|
|
|
37667d |
+ raw_spin_lock_irq(&new_owner->pi_lock);
|
|
|
37667d |
+ WARN_ON(!list_empty(&pi_state->list));
|
|
|
37667d |
+ list_add(&pi_state->list, &new_owner->pi_state_list);
|
|
|
37667d |
+ pi_state->owner = new_owner;
|
|
|
37667d |
+ raw_spin_unlock_irq(&new_owner->pi_lock);
|
|
|
37667d |
+ }
|
|
|
37667d |
+}
|
|
|
37667d |
+
|
|
|
37667d |
static void free_pi_state(struct futex_pi_state *pi_state)
|
|
|
37667d |
{
|
|
|
37667d |
if (!atomic_dec_and_test(&pi_state->refcount))
|
|
|
37667d |
@@ -650,10 +673,7 @@ static void free_pi_state(struct futex_pi_state *pi_state)
|
|
|
37667d |
* and has cleaned up the pi_state already
|
|
|
37667d |
*/
|
|
|
37667d |
if (pi_state->owner) {
|
|
|
37667d |
- raw_spin_lock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
- list_del_init(&pi_state->list);
|
|
|
37667d |
- raw_spin_unlock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
-
|
|
|
37667d |
+ pi_state_update_owner(pi_state, NULL);
|
|
|
37667d |
rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
@@ -791,7 +811,8 @@ void exit_pi_state_list(struct task_struct *curr)
|
|
|
37667d |
* FUTEX_OWNER_DIED bit. See [4]
|
|
|
37667d |
*
|
|
|
37667d |
* [10] There is no transient state which leaves owner and user space
|
|
|
37667d |
- * TID out of sync.
|
|
|
37667d |
+ * TID out of sync. Except one error case where the kernel is denied
|
|
|
37667d |
+ * write access to the user address, see fixup_pi_state_owner().
|
|
|
37667d |
*/
|
|
|
37667d |
static int
|
|
|
37667d |
lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
|
|
|
37667d |
@@ -1168,16 +1189,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
|
|
|
37667d |
return ret;
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
- raw_spin_lock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
- WARN_ON(list_empty(&pi_state->list));
|
|
|
37667d |
- list_del_init(&pi_state->list);
|
|
|
37667d |
- raw_spin_unlock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
-
|
|
|
37667d |
- raw_spin_lock_irq(&new_owner->pi_lock);
|
|
|
37667d |
- WARN_ON(!list_empty(&pi_state->list));
|
|
|
37667d |
- list_add(&pi_state->list, &new_owner->pi_state_list);
|
|
|
37667d |
- pi_state->owner = new_owner;
|
|
|
37667d |
- raw_spin_unlock_irq(&new_owner->pi_lock);
|
|
|
37667d |
+ pi_state_update_owner(pi_state, new_owner);
|
|
|
37667d |
|
|
|
37667d |
raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
|
|
|
37667d |
rt_mutex_unlock(&pi_state->pi_mutex);
|
|
|
37667d |
@@ -1953,20 +1965,9 @@ retry:
|
|
|
37667d |
* We fixed up user space. Now we need to fix the pi_state
|
|
|
37667d |
* itself.
|
|
|
37667d |
*/
|
|
|
37667d |
- if (pi_state->owner != NULL) {
|
|
|
37667d |
- raw_spin_lock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
- WARN_ON(list_empty(&pi_state->list));
|
|
|
37667d |
- list_del_init(&pi_state->list);
|
|
|
37667d |
- raw_spin_unlock_irq(&pi_state->owner->pi_lock);
|
|
|
37667d |
- }
|
|
|
37667d |
+ pi_state_update_owner(pi_state, newowner);
|
|
|
37667d |
|
|
|
37667d |
- pi_state->owner = newowner;
|
|
|
37667d |
-
|
|
|
37667d |
- raw_spin_lock_irq(&newowner->pi_lock);
|
|
|
37667d |
- WARN_ON(!list_empty(&pi_state->list));
|
|
|
37667d |
- list_add(&pi_state->list, &newowner->pi_state_list);
|
|
|
37667d |
- raw_spin_unlock_irq(&newowner->pi_lock);
|
|
|
37667d |
- return 0;
|
|
|
37667d |
+ return newowner == current;
|
|
|
37667d |
|
|
|
37667d |
/*
|
|
|
37667d |
* To handle the page fault we need to drop the hash bucket
|
|
|
37667d |
@@ -1989,10 +1990,26 @@ handle_fault:
|
|
|
37667d |
* Check if someone else fixed it for us:
|
|
|
37667d |
*/
|
|
|
37667d |
if (pi_state->owner != oldowner)
|
|
|
37667d |
- return 0;
|
|
|
37667d |
+ return newowner == current;
|
|
|
37667d |
+
|
|
|
37667d |
+ if (ret) {
|
|
|
37667d |
+ /*
|
|
|
37667d |
+ * fault_in_user_writeable() failed so user state is immutable. At
|
|
|
37667d |
+ * best we can make the kernel state consistent but user state will
|
|
|
37667d |
+ * be most likely hosed and any subsequent unlock operation will be
|
|
|
37667d |
+ * rejected due to PI futex rule [10].
|
|
|
37667d |
+ *
|
|
|
37667d |
+ * Ensure that the rtmutex owner is also the pi_state owner despite
|
|
|
37667d |
+ * the user space value claiming something different. There is no
|
|
|
37667d |
+ * point in unlocking the rtmutex if current is the owner as it
|
|
|
37667d |
+ * would need to wait until the next waiter has taken the rtmutex
|
|
|
37667d |
+ * to guarantee consistent state. Keep it simple. Userspace asked
|
|
|
37667d |
+ * for this wreckaged state.
|
|
|
37667d |
+ */
|
|
|
37667d |
+ pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
|
|
|
37667d |
|
|
|
37667d |
- if (ret)
|
|
|
37667d |
return ret;
|
|
|
37667d |
+ }
|
|
|
37667d |
|
|
|
37667d |
goto retry;
|
|
|
37667d |
}
|
|
|
37667d |
@@ -2014,10 +2031,10 @@ static long futex_wait_restart(struct restart_block *restart);
|
|
|
37667d |
* 0 - success, lock not taken;
|
|
|
37667d |
* <0 - on error (-EFAULT)
|
|
|
37667d |
*/
|
|
|
37667d |
+__attribute__((optimize("-fno-optimize-sibling-calls")))
|
|
|
37667d |
static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
|
|
|
37667d |
{
|
|
|
37667d |
struct task_struct *owner;
|
|
|
37667d |
- int ret = 0;
|
|
|
37667d |
|
|
|
37667d |
if (locked) {
|
|
|
37667d |
/*
|
|
|
37667d |
@@ -2025,8 +2042,8 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
|
|
|
37667d |
* did a lock-steal - fix up the PI-state in that case:
|
|
|
37667d |
*/
|
|
|
37667d |
if (q->pi_state->owner != current)
|
|
|
37667d |
- ret = fixup_pi_state_owner(uaddr, q, current);
|
|
|
37667d |
- goto out;
|
|
|
37667d |
+ return fixup_pi_state_owner(uaddr, q, current);
|
|
|
37667d |
+ return 1;
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
/*
|
|
|
37667d |
@@ -2040,8 +2057,7 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
|
|
|
37667d |
* rt_mutex waiters list.
|
|
|
37667d |
*/
|
|
|
37667d |
if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
|
|
|
37667d |
- locked = 1;
|
|
|
37667d |
- goto out;
|
|
|
37667d |
+ return 1;
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
/*
|
|
|
37667d |
@@ -2054,22 +2070,18 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
|
|
|
37667d |
if (!owner)
|
|
|
37667d |
owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
|
|
|
37667d |
raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
|
|
|
37667d |
- ret = fixup_pi_state_owner(uaddr, q, owner);
|
|
|
37667d |
- goto out;
|
|
|
37667d |
+
|
|
|
37667d |
+ return fixup_pi_state_owner(uaddr, q, owner);
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
/*
|
|
|
37667d |
* Paranoia check. If we did not take the lock, then we should not be
|
|
|
37667d |
- * the owner of the rt_mutex.
|
|
|
37667d |
+ * the owner of the rt_mutex. Warn and establish consistent state.
|
|
|
37667d |
*/
|
|
|
37667d |
- if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
|
|
|
37667d |
- printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
|
|
|
37667d |
- "pi-state %p\n", ret,
|
|
|
37667d |
- q->pi_state->pi_mutex.owner,
|
|
|
37667d |
- q->pi_state->owner);
|
|
|
37667d |
+ if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
|
|
|
37667d |
+ return fixup_pi_state_owner(uaddr, q, current);
|
|
|
37667d |
|
|
|
37667d |
-out:
|
|
|
37667d |
- return ret ? ret : locked;
|
|
|
37667d |
+ return 0;
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
/**
|
|
|
37667d |
@@ -2363,13 +2375,6 @@ retry_private:
|
|
|
37667d |
if (res)
|
|
|
37667d |
ret = (res < 0) ? res : 0;
|
|
|
37667d |
|
|
|
37667d |
- /*
|
|
|
37667d |
- * If fixup_owner() faulted and was unable to handle the fault, unlock
|
|
|
37667d |
- * it and return the fault to userspace.
|
|
|
37667d |
- */
|
|
|
37667d |
- if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
|
|
|
37667d |
- rt_mutex_unlock(&q.pi_state->pi_mutex);
|
|
|
37667d |
-
|
|
|
37667d |
/* Unqueue and drop the lock */
|
|
|
37667d |
unqueue_me_pi(&q);
|
|
|
37667d |
|
|
|
37667d |
@@ -2666,6 +2671,11 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
|
|
|
37667d |
spin_lock(q.lock_ptr);
|
|
|
37667d |
ret = fixup_pi_state_owner(uaddr2, &q, current);
|
|
|
37667d |
spin_unlock(q.lock_ptr);
|
|
|
37667d |
+ /*
|
|
|
37667d |
+ * Adjust the return value. It's either -EFAULT or
|
|
|
37667d |
+ * success (1) but the caller expects 0 for success.
|
|
|
37667d |
+ */
|
|
|
37667d |
+ ret = ret < 0 ? ret : 0;
|
|
|
37667d |
}
|
|
|
37667d |
} else {
|
|
|
37667d |
/*
|
|
|
37667d |
@@ -2695,14 +2705,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
|
|
|
37667d |
unqueue_me_pi(&q);
|
|
|
37667d |
}
|
|
|
37667d |
|
|
|
37667d |
- /*
|
|
|
37667d |
- * If fixup_pi_state_owner() faulted and was unable to handle the
|
|
|
37667d |
- * fault, unlock the rt_mutex and return the fault to userspace.
|
|
|
37667d |
- */
|
|
|
37667d |
- if (ret == -EFAULT) {
|
|
|
37667d |
- if (pi_mutex && rt_mutex_owner(pi_mutex) == current)
|
|
|
37667d |
- rt_mutex_unlock(pi_mutex);
|
|
|
37667d |
- } else if (ret == -EINTR) {
|
|
|
37667d |
+ if (ret == -EINTR) {
|
|
|
37667d |
/*
|
|
|
37667d |
* We've already been requeued, but cannot restart by calling
|
|
|
37667d |
* futex_lock_pi() directly. We could restart this syscall, but
|
|
|
37667d |
--
|
|
|
37667d |
2.26.3
|
|
|
37667d |
|