[PATCH] futex: make FUTEX_OP_OPARG_SHIFT unsigned

From: Jiri Slaby
Date: Mon Oct 24 2016 - 06:21:33 EST


FUTEX_OP_OPARG_SHIFT is shifted left by 28 in futex_atomic_op_inuser and
other places. Since FUTEX_OP_OPARG_SHIFT is 8, we actually do (8 << 28).
But given both are ints, we perform operation with undefined behaviour
about which UBSAN reports:
UBSAN: Undefined behaviour in arch/x86/include/asm/futex.h:53:13
shift exponent 776 is too large for 32-bit type 'int'
CPU: 1 PID: 27469 Comm: trinity-c1 Tainted: G EL 4.4.26-0-default #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.9.1-0-gb3ef39f-prebuilt.qemu-project.org 04/01/2014
0000000000000000 ffff880553ad7908 ffffffff81cbcc16 0000000041b58ab3
ffffffff82f515a3 ffffffff81cbcb16 ffff880553ad7930 ffff880553ad78d0
ffffffff837d7d60 0000000000000000 0000000000000308 ffffffff837d7d62
Call Trace:
[<ffffffff81d9ec50>] ? __ubsan_handle_shift_out_of_bounds+0x29c/0x300
[<ffffffff81d9e9b4>] ? __ubsan_handle_load_invalid_value+0x1ba/0x1ba
[<ffffffff814202a9>] ? get_futex_key+0x4f9/0xf80
[<ffffffff81422456>] ? futex_wake_op+0x636/0xfc0
[<ffffffff8141fdb0>] ? futex_lock_pi_atomic+0x2a0/0x2a0
[<ffffffff81422b09>] ? futex_wake_op+0xce9/0xfc0
[<ffffffff81421e20>] ? futex_wait_restart+0x240/0x240
[<ffffffff814266ae>] ? do_futex+0x54e/0xb60
[<ffffffff81426df8>] ? SyS_futex+0x138/0x2b0

Fix that by having FUTEX_OP_OPARG_SHIFT unsigned.

Signed-off-by: Jiri Slaby <jslaby@xxxxxxx>
---
include/uapi/linux/futex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/uapi/linux/futex.h b/include/uapi/linux/futex.h
index 0b1f716373c7..954a23b18e68 100644
--- a/include/uapi/linux/futex.h
+++ b/include/uapi/linux/futex.h
@@ -130,7 +130,7 @@ struct robust_list_head {
#define FUTEX_OP_ANDN 3 /* *(int *)UADDR2 &= ~OPARG; */
#define FUTEX_OP_XOR 4 /* *(int *)UADDR2 ^= OPARG; */

-#define FUTEX_OP_OPARG_SHIFT 8 /* Use (1 << OPARG) instead of OPARG. */
+#define FUTEX_OP_OPARG_SHIFT 8U /* Use (1 << OPARG) instead of OPARG. */

#define FUTEX_OP_CMP_EQ 0 /* if (oldval == CMPARG) wake */
#define FUTEX_OP_CMP_NE 1 /* if (oldval != CMPARG) wake */
--
2.10.1