mm: avoid undefined behavior in hardened usercopy check
PATCH 1/1 mm: avoid undefined behavior in hardened usercopy check
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.
--- mm/usercopy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/usercopy.c +1 -1
@@ -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. */
Signature · 7 lines
2.8.0.rc3.226.g39d4020 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Eric Biggers
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
Quoted 7 lines
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 <[email protected]>
Cool, thanks. I'll get this into my tree.
-Kees
Quoted 20 lines
--- mm/usercopy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/usercopy.c +1
-2@@ -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. */ --2.8.0.rc3.226.g39d4020
Signature · 8 lines
Kees Cook Nexus Security -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>