Commit 7329a655 authored by Eric Biggers's avatar Eric Biggers Committed by Kees Cook

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: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent ef0e1ea8
...@@ -124,7 +124,7 @@ static inline const char *check_kernel_text_object(const void *ptr, ...@@ -124,7 +124,7 @@ static inline const char *check_kernel_text_object(const void *ptr,
static inline const char *check_bogus_address(const void *ptr, unsigned long n) static inline const char *check_bogus_address(const void *ptr, unsigned long n)
{ {
/* Reject if object wraps past end of memory. */ /* Reject if object wraps past end of memory. */
if (ptr + n < ptr) if ((unsigned long)ptr + n < (unsigned long)ptr)
return "<wrapped address>"; return "<wrapped address>";
/* Reject if NULL or ZERO-allocation. */ /* Reject if NULL or ZERO-allocation. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment