linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning
@ 2022-11-10  3:10 Zhen Lei
  2022-11-19 19:39 ` Eric Biggers
  0 siblings, 1 reply; 3+ messages in thread
From: Zhen Lei @ 2022-11-10  3:10 UTC (permalink / raw)
  To: Alexander Viro, linux-fsdevel, linux-kernel; +Cc: Zhen Lei

UBSAN: shift-out-of-bounds in fs/locks.c:2572:16
left shift of 1 by 63 places cannot be represented in type 'long long int'

Switch the calculation method to ((quarter - 1) * 2 + 1) can help us
eliminate this false positive.

On the other hand, the old implementation has problems with char and
short types, although not currently involved.
printf("%d: %x\n", sizeof(char),  INT_LIMIT(char));
printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1: ffffff7f
2: ffff7fff

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 include/linux/fs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index e654435f16512c1..88d42e2daed9f6c 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1131,7 +1131,7 @@ struct file_lock_context {
 
 /* The following constant reflects the upper bound of the file/locking space */
 #ifndef OFFSET_MAX
-#define INT_LIMIT(x)	(~((x)1 << (sizeof(x)*8 - 1)))
+#define INT_LIMIT(x)	((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2  + 1)
 #define OFFSET_MAX	INT_LIMIT(loff_t)
 #define OFFT_OFFSET_MAX	INT_LIMIT(off_t)
 #endif
-- 
2.25.1


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

* Re: [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning
  2022-11-10  3:10 [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning Zhen Lei
@ 2022-11-19 19:39 ` Eric Biggers
  2022-11-21  1:38   ` Leizhen (ThunderTown)
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Biggers @ 2022-11-19 19:39 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Alexander Viro, linux-fsdevel, linux-kernel

On Thu, Nov 10, 2022 at 11:10:24AM +0800, Zhen Lei wrote:
> UBSAN: shift-out-of-bounds in fs/locks.c:2572:16
> left shift of 1 by 63 places cannot be represented in type 'long long int'
> 
> Switch the calculation method to ((quarter - 1) * 2 + 1) can help us
> eliminate this false positive.
> 
> On the other hand, the old implementation has problems with char and
> short types, although not currently involved.
> printf("%d: %x\n", sizeof(char),  INT_LIMIT(char));
> printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
> 1: ffffff7f
> 2: ffff7fff
> 
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  include/linux/fs.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index e654435f16512c1..88d42e2daed9f6c 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1131,7 +1131,7 @@ struct file_lock_context {
>  
>  /* The following constant reflects the upper bound of the file/locking space */
>  #ifndef OFFSET_MAX
> -#define INT_LIMIT(x)	(~((x)1 << (sizeof(x)*8 - 1)))
> +#define INT_LIMIT(x)	((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2  + 1)
>  #define OFFSET_MAX	INT_LIMIT(loff_t)
>  #define OFFT_OFFSET_MAX	INT_LIMIT(off_t)
>  #endif

This problem has already been solved by type_max() in include/linux/overflow.h.
How about removing INT_LIMIT() and using type_max() instead?

- Eric

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

* Re: [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning
  2022-11-19 19:39 ` Eric Biggers
@ 2022-11-21  1:38   ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 3+ messages in thread
From: Leizhen (ThunderTown) @ 2022-11-21  1:38 UTC (permalink / raw)
  To: Eric Biggers; +Cc: Alexander Viro, linux-fsdevel, linux-kernel



On 2022/11/20 3:39, Eric Biggers wrote:
> On Thu, Nov 10, 2022 at 11:10:24AM +0800, Zhen Lei wrote:
>> UBSAN: shift-out-of-bounds in fs/locks.c:2572:16
>> left shift of 1 by 63 places cannot be represented in type 'long long int'
>>
>> Switch the calculation method to ((quarter - 1) * 2 + 1) can help us
>> eliminate this false positive.
>>
>> On the other hand, the old implementation has problems with char and
>> short types, although not currently involved.
>> printf("%d: %x\n", sizeof(char),  INT_LIMIT(char));
>> printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
>> 1: ffffff7f
>> 2: ffff7fff
>>
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
>>  include/linux/fs.h | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/include/linux/fs.h b/include/linux/fs.h
>> index e654435f16512c1..88d42e2daed9f6c 100644
>> --- a/include/linux/fs.h
>> +++ b/include/linux/fs.h
>> @@ -1131,7 +1131,7 @@ struct file_lock_context {
>>  
>>  /* The following constant reflects the upper bound of the file/locking space */
>>  #ifndef OFFSET_MAX
>> -#define INT_LIMIT(x)	(~((x)1 << (sizeof(x)*8 - 1)))
>> +#define INT_LIMIT(x)	((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2  + 1)
>>  #define OFFSET_MAX	INT_LIMIT(loff_t)
>>  #define OFFT_OFFSET_MAX	INT_LIMIT(off_t)
>>  #endif
> 
> This problem has already been solved by type_max() in include/linux/overflow.h.
> How about removing INT_LIMIT() and using type_max() instead?

It's better to use type_max(). INT_LIMIT() is currently valid only for signed type.
Besides, my method is actually the same as type_max(). Using type_max() does not
result in code duplication.

> 
> - Eric
> .
> 

-- 
Regards,
  Zhen Lei

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

end of thread, other threads:[~2022-11-21  1:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-10  3:10 [PATCH] fs: Clear a UBSAN shift-out-of-bounds warning Zhen Lei
2022-11-19 19:39 ` Eric Biggers
2022-11-21  1:38   ` Leizhen (ThunderTown)

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).