linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/fadvise: Fix signed overflow UBSAN complaint
       [not found] <20180627204808.99988d94180dd144b14aa38b@linux-foundation.org>
@ 2018-06-29 18:44 ` Andrey Ryabinin
  2018-06-30  1:37   ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Andrey Ryabinin @ 2018-06-29 18:44 UTC (permalink / raw)
  To: Andrew Morton
  Cc: icytxw, Alexander Potapenko, Dmitry Vyukov, linux-mm,
	linux-kernel, Andrey Ryabinin

Signed integer overflow is undefined according to the C standard.
The overflow in ksys_fadvise64_64() is deliberate, but since it is signed
overflow, UBSAN complains:
	UBSAN: Undefined behaviour in mm/fadvise.c:76:10
	signed integer overflow:
	4 + 9223372036854775805 cannot be represented in type 'long long int'

Use unsigned types to do math. Unsigned overflow is defined so UBSAN
will not complain about it. This patch doesn't change generated code.

Reported-by: <icytxw@gmail.com>
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
---
 mm/fadvise.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/fadvise.c b/mm/fadvise.c
index afa41491d324..1eaf2002d79a 100644
--- a/mm/fadvise.c
+++ b/mm/fadvise.c
@@ -73,7 +73,7 @@ int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
 	}
 
 	/* Careful about overflows. Len == 0 means "as much as possible" */
-	endbyte = offset + len;
+	endbyte = (u64)offset + (u64)len;
 	if (!len || endbyte < len)
 		endbyte = -1;
 	else
-- 
2.16.4


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

* Re: [PATCH] mm/fadvise: Fix signed overflow UBSAN complaint
  2018-06-29 18:44 ` [PATCH] mm/fadvise: Fix signed overflow UBSAN complaint Andrey Ryabinin
@ 2018-06-30  1:37   ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2018-06-30  1:37 UTC (permalink / raw)
  To: Andrey Ryabinin
  Cc: icytxw, Alexander Potapenko, Dmitry Vyukov, linux-mm, linux-kernel

On Fri, 29 Jun 2018 21:44:53 +0300 Andrey Ryabinin <aryabinin@virtuozzo.com> wrote:

> Signed integer overflow is undefined according to the C standard.
> The overflow in ksys_fadvise64_64() is deliberate, but since it is signed
> overflow, UBSAN complains:
> 	UBSAN: Undefined behaviour in mm/fadvise.c:76:10
> 	signed integer overflow:
> 	4 + 9223372036854775805 cannot be represented in type 'long long int'
> 
> Use unsigned types to do math. Unsigned overflow is defined so UBSAN
> will not complain about it. This patch doesn't change generated code.
> 
> ...
>
> --- a/mm/fadvise.c
> +++ b/mm/fadvise.c
> @@ -73,7 +73,7 @@ int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
>  	}
>  
>  	/* Careful about overflows. Len == 0 means "as much as possible" */
> -	endbyte = offset + len;
> +	endbyte = (u64)offset + (u64)len;
>  	if (!len || endbyte < len)
>  		endbyte = -1;
>  	else

Readers of this code will wonder "what the heck are those casts for". 
Therefore:

--- a/mm/fadvise.c~mm-fadvise-fix-signed-overflow-ubsan-complaint-fix
+++ a/mm/fadvise.c
@@ -72,7 +72,11 @@ int ksys_fadvise64_64(int fd, loff_t off
 		goto out;
 	}
 
-	/* Careful about overflows. Len == 0 means "as much as possible" */
+	/*
+	 * Careful about overflows. Len == 0 means "as much as possible".  Use
+	 * unsigned math because signed overflows are undefined and UBSan
+	 * complains.
+	 */
 	endbyte = (u64)offset + (u64)len;
 	if (!len || endbyte < len)
 		endbyte = -1;
_


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

end of thread, other threads:[~2018-06-30  1:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20180627204808.99988d94180dd144b14aa38b@linux-foundation.org>
2018-06-29 18:44 ` [PATCH] mm/fadvise: Fix signed overflow UBSAN complaint Andrey Ryabinin
2018-06-30  1:37   ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).