UPSTREAM: usercopy: avoid potentially undefined behavior in pointer math
authorEric Biggers <ebiggers@google.com>
Fri, 19 Aug 2016 19:15:22 +0000 (12:15 -0700)
committerAmit Pundir <amit.pundir@linaro.org>
Wed, 14 Sep 2016 09:13:25 +0000 (14:43 +0530)
commitc3f4d074ed48e93586acb5b83c0f702094fa6276
tree4e98829d76a8d6c7e9fc9fec79c68c388127cb3c
parent1cbefb3fb1c189e02e8329316fce1fbb2529badb
UPSTREAM: usercopy: avoid potentially undefined behavior in pointer math

check_bogus_address() checked for pointer overflow using this expression,
where 'ptr' has type 'const void *':

ptr + n < ptr

Since pointer wraparound is undefined behavior, gcc at -O2 by default
treats it like the following, which would not behave as intended:

(long)n < 0

Fortunately, this doesn't currently happen for kernel code because kernel
code is compiled with -fno-strict-overflow.  But the expression should be
fixed anyway to use well-defined integer arithmetic, since it could be
treated differently by different compilers in the future or could be
reported by tools checking for undefined behavior.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Change-Id: I73b13be651cf35c03482f2014bf2c3dd291518ab
(cherry picked from commit 7329a655875a2f4bd6984fe8a7e00a6981e802f3)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
mm/usercopy.c