Skip to content

Commit

Permalink
usercopy: avoid potentially undefined behavior in pointer math
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ebiggers authored and kees committed Aug 23, 2016
1 parent ef0e1ea commit 7329a65
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion mm/usercopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
/* Reject if object wraps past end of memory. */
if (ptr + n < ptr)
if ((unsigned long)ptr + n < (unsigned long)ptr)
return "<wrapped address>";

/* Reject if NULL or ZERO-allocation. */
Expand Down

0 comments on commit 7329a65

Please sign in to comment.