[tip:locking/urgent] futex: Prevent overflow by strengthen input validation

From: tip-bot for Li Jinyue
Date: Sun Jan 14 2018 - 13:08:36 EST


Commit-ID: fbe0e839d1e22d88810f3ee3e2f1479be4c0aa4a
Gitweb: https://git.kernel.org/tip/fbe0e839d1e22d88810f3ee3e2f1479be4c0aa4a
Author: Li Jinyue <lijinyue@xxxxxxxxxx>
AuthorDate: Thu, 14 Dec 2017 17:04:54 +0800
Committer: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
CommitDate: Sun, 14 Jan 2018 18:55:03 +0100

futex: Prevent overflow by strengthen input validation

UBSAN reports signed integer overflow in kernel/futex.c:

UBSAN: Undefined behaviour in kernel/futex.c:2041:18
signed integer overflow:
0 - -2147483648 cannot be represented in type 'int'

Add a sanity check to catch negative values of nr_wake and nr_requeue.

Signed-off-by: Li Jinyue <lijinyue@xxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: peterz@xxxxxxxxxxxxx
Cc: dvhart@xxxxxxxxxxxxx
Cc: stable@xxxxxxxxxxxxxxx
Link: https://lkml.kernel.org/r/1513242294-31786-1-git-send-email-lijinyue@xxxxxxxxxx

---
kernel/futex.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/kernel/futex.c b/kernel/futex.c
index 9e69589..8c5424d 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -1878,6 +1878,9 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
struct futex_q *this, *next;
DEFINE_WAKE_Q(wake_q);

+ if (nr_wake < 0 || nr_requeue < 0)
+ return -EINVAL;
+
/*
* When PI not supported: return -ENOSYS if requeue_pi is true,
* consequently the compiler knows requeue_pi is always false past