Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751956AbcDOMf5 (ORCPT ); Fri, 15 Apr 2016 08:35:57 -0400 Received: from www.linutronix.de ([62.245.132.108]:53565 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750716AbcDOMf4 (ORCPT ); Fri, 15 Apr 2016 08:35:56 -0400 From: Sebastian Andrzej Siewior To: Thomas Gleixner Cc: Davidlohr Bueso , linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: [PATCH] kernel/futex: handle the case where we got a "late" waiter Date: Fri, 15 Apr 2016 14:35:39 +0200 Message-Id: <1460723739-5195-1-git-send-email-bigeasy@linutronix.de> X-Mailer: git-send-email 2.8.0.rc3 X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001,URIBL_BLOCKED=0.001 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2405 Lines: 67 futex_unlock_pi() gets uval before taking the hb lock. Now imagine someone in futex_lock_pi() took the lock. While futex_unlock_pi() waits for the hb lock, the LOCK_PI sets FUTEX_WAITERS and drops the lock. Now, futex_unlock_pi() figures out that there is waiter and invokes wake_futex_pi() with the old uval which does not yet have FUTEX_WAITERS set. This flaw lets cmpxchg_futex_value_locked() fail and return -EINVAL. To address this race we could either use get_futex_value_locked() after we obtained the lock but then we would need to handle the EFAULT case (which I think means get_user() without the lock). To avoid this I let wake_futex_pi() detect this and return -EAGAIN so it can read the new value and try again. As bad as this may sound: FUTEX_OWNER_DIED is not set while the owner is unlocking the lock. FUTEX_WAITERS gets set by another task *but* this means that the owner invoked the futex() syscall rather than doing a cmpxchg() in userland (and libc does not do that). Fixes: ccf9e6a80d9e ("futex: Make unlock_pi more robust") Signed-off-by: Sebastian Andrzej Siewior --- kernel/futex.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/kernel/futex.c b/kernel/futex.c index 0d557e13823a..2911f54a6dde 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -1496,8 +1496,12 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this, if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) ret = -EFAULT; - else if (curval != uval) - ret = -EINVAL; + else if (curval != uval) { + if ((FUTEX_TID_MASK & curval) == uval) + ret = -EAGAIN; + else + ret = -EINVAL; + } if (ret) { raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); return ret; @@ -2852,6 +2856,12 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags) if (ret == -EFAULT) goto pi_faulted; /* + * Between get_user() and obtaining the hb->lock the uval + * gained a flag. Retry with the new value. + */ + if (ret == -EAGAIN) + goto pi_faulted; + /* * wake_futex_pi has detected invalid state. Tell user * space. */ @@ -2883,6 +2893,8 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags) spin_unlock(&hb->lock); put_futex_key(&key); + if (ret == -EAGAIN) + goto retry; ret = fault_in_user_writeable(uaddr); if (!ret) goto retry; -- 2.8.0.rc3