Skip to content

Commit

Permalink
futex: Always cleanup owner tid in unlock_pi
Browse files Browse the repository at this point in the history
commit 13fbca4 upstream.

If the owner died bit is set at futex_unlock_pi, we currently do not
cleanup the user space futex.  So the owner TID of the current owner
(the unlocker) persists.  That's observable inconsistant state,
especially when the ownership of the pi state got transferred.

Clean it up unconditionally.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Drewry <wad@chromium.org>
Cc: Darren Hart <dvhart@linux.intel.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Thomas Gleixner authored and Greg Kroah-Hartman committed Jun 7, 2014
1 parent cae300d commit 19040c5
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions kernel/futex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
struct task_struct *new_owner;
struct futex_pi_state *pi_state = this->pi_state;
u32 uninitialized_var(curval), newval;
int ret = 0;

if (!pi_state)
return -EINVAL;
Expand All @@ -1061,23 +1062,19 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
new_owner = this->task;

/*
* We pass it to the next owner. (The WAITERS bit is always
* kept enabled while there is PI state around. We must also
* preserve the owner died bit.)
* We pass it to the next owner. The WAITERS bit is always
* kept enabled while there is PI state around. We cleanup the
* owner died bit, because we are the owner.
*/
if (!(uval & FUTEX_OWNER_DIED)) {
int ret = 0;

newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
newval = FUTEX_WAITERS | task_pid_vnr(new_owner);

if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
ret = -EFAULT;
else if (curval != uval)
ret = -EINVAL;
if (ret) {
raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
return ret;
}
if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
ret = -EFAULT;
else if (curval != uval)
ret = -EINVAL;
if (ret) {
raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
return ret;
}

raw_spin_lock_irq(&pi_state->owner->pi_lock);
Expand Down Expand Up @@ -2337,9 +2334,10 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
/*
* To avoid races, try to do the TID -> 0 atomic transition
* again. If it succeeds then we can return without waking
* anyone else up:
* anyone else up. We only try this if neither the waiters nor
* the owner died bit are set.
*/
if (!(uval & FUTEX_OWNER_DIED) &&
if (!(uval & ~FUTEX_TID_MASK) &&
cmpxchg_futex_value_locked(&uval, uaddr, vpid, 0))
goto pi_faulted;
/*
Expand Down Expand Up @@ -2369,11 +2367,9 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
/*
* No waiters - kernel unlocks the futex:
*/
if (!(uval & FUTEX_OWNER_DIED)) {
ret = unlock_futex_pi(uaddr, uval);
if (ret == -EFAULT)
goto pi_faulted;
}
ret = unlock_futex_pi(uaddr, uval);
if (ret == -EFAULT)
goto pi_faulted;

out_unlock:
spin_unlock(&hb->lock);
Expand Down

0 comments on commit 19040c5

Please sign in to comment.