[2557] | 1 | From b58623fb64ff0454ec20bce7a02275a20c23086d Mon Sep 17 00:00:00 2001 |
---|
| 2 | From: Thomas Gleixner <tglx@linutronix.de> |
---|
| 3 | Date: Tue, 3 Jun 2014 12:27:06 +0000 |
---|
| 4 | Subject: [PATCH 1/4] futex-prevent-requeue-pi-on-same-futex.patch futex: |
---|
| 5 | Forbid uaddr == uaddr2 in futex_requeue(..., |
---|
| 6 | requeue_pi=1) |
---|
| 7 | |
---|
| 8 | commit e9c243a5a6de0be8e584c604d353412584b592f8 upstream. |
---|
| 9 | |
---|
| 10 | If uaddr == uaddr2, then we have broken the rule of only requeueing from |
---|
| 11 | a non-pi futex to a pi futex with this call. If we attempt this, then |
---|
| 12 | dangling pointers may be left for rt_waiter resulting in an exploitable |
---|
| 13 | condition. |
---|
| 14 | |
---|
| 15 | This change brings futex_requeue() in line with futex_wait_requeue_pi() |
---|
| 16 | which performs the same check as per commit 6f7b0a2a5c0f ("futex: Forbid |
---|
| 17 | uaddr == uaddr2 in futex_wait_requeue_pi()") |
---|
| 18 | |
---|
| 19 | [ tglx: Compare the resulting keys as well, as uaddrs might be |
---|
| 20 | different depending on the mapping ] |
---|
| 21 | |
---|
| 22 | Fixes CVE-2014-3153. |
---|
| 23 | |
---|
| 24 | Reported-by: Pinkie Pie |
---|
| 25 | Signed-off-by: Will Drewry <wad@chromium.org> |
---|
| 26 | Signed-off-by: Kees Cook <keescook@chromium.org> |
---|
| 27 | Signed-off-by: Thomas Gleixner <tglx@linutronix.de> |
---|
| 28 | Reviewed-by: Darren Hart <dvhart@linux.intel.com> |
---|
| 29 | Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> |
---|
| 30 | Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
---|
| 31 | --- |
---|
| 32 | kernel/futex.c | 25 +++++++++++++++++++++++++ |
---|
| 33 | 1 file changed, 25 insertions(+) |
---|
| 34 | |
---|
| 35 | diff --git a/kernel/futex.c b/kernel/futex.c |
---|
| 36 | index 58743c0..93e522f 100644 |
---|
| 37 | --- a/kernel/futex.c |
---|
| 38 | +++ b/kernel/futex.c |
---|
| 39 | @@ -1293,6 +1293,13 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, |
---|
| 40 | |
---|
| 41 | if (requeue_pi) { |
---|
| 42 | /* |
---|
| 43 | + * Requeue PI only works on two distinct uaddrs. This |
---|
| 44 | + * check is only valid for private futexes. See below. |
---|
| 45 | + */ |
---|
| 46 | + if (uaddr1 == uaddr2) |
---|
| 47 | + return -EINVAL; |
---|
| 48 | + |
---|
| 49 | + /* |
---|
| 50 | * requeue_pi requires a pi_state, try to allocate it now |
---|
| 51 | * without any locks in case it fails. |
---|
| 52 | */ |
---|
| 53 | @@ -1330,6 +1337,15 @@ retry: |
---|
| 54 | if (unlikely(ret != 0)) |
---|
| 55 | goto out_put_key1; |
---|
| 56 | |
---|
| 57 | + /* |
---|
| 58 | + * The check above which compares uaddrs is not sufficient for |
---|
| 59 | + * shared futexes. We need to compare the keys: |
---|
| 60 | + */ |
---|
| 61 | + if (requeue_pi && match_futex(&key1, &key2)) { |
---|
| 62 | + ret = -EINVAL; |
---|
| 63 | + goto out_put_keys; |
---|
| 64 | + } |
---|
| 65 | + |
---|
| 66 | hb1 = hash_futex(&key1); |
---|
| 67 | hb2 = hash_futex(&key2); |
---|
| 68 | |
---|
| 69 | @@ -2360,6 +2376,15 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, |
---|
| 70 | if (ret) |
---|
| 71 | goto out_key2; |
---|
| 72 | |
---|
| 73 | + /* |
---|
| 74 | + * The check above which compares uaddrs is not sufficient for |
---|
| 75 | + * shared futexes. We need to compare the keys: |
---|
| 76 | + */ |
---|
| 77 | + if (match_futex(&q.key, &key2)) { |
---|
| 78 | + ret = -EINVAL; |
---|
| 79 | + goto out_put_keys; |
---|
| 80 | + } |
---|
| 81 | + |
---|
| 82 | /* Queue the futex_q, drop the hb lock, wait for wakeup. */ |
---|
| 83 | futex_wait_queue_me(hb, &q, to); |
---|
| 84 | |
---|
| 85 | -- |
---|
| 86 | 1.7.10.4 |
---|
| 87 | |
---|