All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm: avoid undefined behavior in hardened usercopy check
@ 2016-08-19 19:15 ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2016-08-19 19:15 UTC (permalink / raw)
  To: keescook; +Cc: linux-mm, linux-kernel, 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

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>
---
 mm/usercopy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/usercopy.c b/mm/usercopy.c
index 8ebae91..82f81df 100644
--- a/mm/usercopy.c
+++ b/mm/usercopy.c
@@ -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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] mm: avoid undefined behavior in hardened usercopy check
@ 2016-08-19 19:15 ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2016-08-19 19:15 UTC (permalink / raw)
  To: keescook; +Cc: linux-mm, linux-kernel, 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

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>
---
 mm/usercopy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/usercopy.c b/mm/usercopy.c
index 8ebae91..82f81df 100644
--- a/mm/usercopy.c
+++ b/mm/usercopy.c
@@ -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

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: avoid undefined behavior in hardened usercopy check
  2016-08-19 19:15 ` Eric Biggers
@ 2016-08-19 20:47   ` Kees Cook
  -1 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2016-08-19 20:47 UTC (permalink / raw)
  To: Eric Biggers; +Cc: Linux-MM, LKML

On Fri, Aug 19, 2016 at 12:15 PM, Eric Biggers <ebiggers@google.com> wrote:
> 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>

Cool, thanks. I'll get this into my tree.

-Kees

> ---
>  mm/usercopy.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/usercopy.c b/mm/usercopy.c
> index 8ebae91..82f81df 100644
> --- a/mm/usercopy.c
> +++ b/mm/usercopy.c
> @@ -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
>



-- 
Kees Cook
Nexus Security

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: avoid undefined behavior in hardened usercopy check
@ 2016-08-19 20:47   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2016-08-19 20:47 UTC (permalink / raw)
  To: Eric Biggers; +Cc: Linux-MM, LKML

On Fri, Aug 19, 2016 at 12:15 PM, Eric Biggers <ebiggers@google.com> wrote:
> 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>

Cool, thanks. I'll get this into my tree.

-Kees

> ---
>  mm/usercopy.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/usercopy.c b/mm/usercopy.c
> index 8ebae91..82f81df 100644
> --- a/mm/usercopy.c
> +++ b/mm/usercopy.c
> @@ -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
>



-- 
Kees Cook
Nexus Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-08-19 20:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-19 19:15 [PATCH] mm: avoid undefined behavior in hardened usercopy check Eric Biggers
2016-08-19 19:15 ` Eric Biggers
2016-08-19 20:47 ` Kees Cook
2016-08-19 20:47   ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.