Skip to contentSkip to conversation

fs: clear a UBSAN shift-out-of-bounds warning

PATCH In review Zhen Lei 2 participants 5 messages Nov 25, 2022 – Nov 26, 2022

Subsystems (2)
lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

v3 cover fs: clear a UBSAN shift-out-of-bounds warning

v2 --> v3:
Updated the commit message of patch 2/2 based on Alexander Viro's suggestion.

v1 -- > v2:
1. Replace INT_LIMIT(loff_t) with OFFSET_MAX in btrfs.
2. Replace INT_LIMIT() with type_max().

Zhen Lei (2):
btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX
fs: clear a UBSAN shift-out-of-bounds warning

 fs/btrfs/ordered-data.c | 6 +++---
 include/linux/fs.h      | 5 ++---
 2 files changed, 5 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/2 btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX

OFFSET_MAX is self-annotated and more readable.

---
 fs/btrfs/ordered-data.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/ordered-data.c +3 -3
index e54f8280031fa14..100d9f4836b177d 100644
@@ -761,11 +761,11 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
 	struct btrfs_ordered_extent *ordered;
 
 	if (start + len < start) {
-		orig_end = INT_LIMIT(loff_t);
+		orig_end = OFFSET_MAX;
 	} else {
 		orig_end = start + len - 1;
-		if (orig_end > INT_LIMIT(loff_t))
-			orig_end = INT_LIMIT(loff_t);
+		if (orig_end > OFFSET_MAX)
+			orig_end = OFFSET_MAX;
 	}
 
 	/* start IO across the range first to instantiate any delalloc
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v3 2/2 fs: clear a UBSAN shift-out-of-bounds warning

In reply to 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'
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0x8d/0xcf lib/dump_stack.c:106
 ubsan_epilogue+0xa/0x44 lib/ubsan.c:151
 __ubsan_handle_shift_out_of_bounds+0x1e7/0x208 lib/ubsan.c:322
 locks_remove_posix+0xba/0x290 fs/locks.c:2572
 filp_close+0x61/0xa0 fs/open.c:1424
 close_fd+0x56/0x70 fs/file.c:664
 __do_sys_close fs/open.c:1439 [inline]
 __se_sys_close fs/open.c:1437 [inline]
 __x64_sys_close+0x1a/0x50 fs/open.c:1437
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x34/0x80 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
</TASK>

INT_LIMIT() tries to do what type_max() does, except that type_max() doesn't rely upon undefined behaviour, might as well use type_max() instead.

---
 include/linux/fs.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/fs.h +2 -3
index e654435f16512c1..a384741b1449457 100644
@@ -1131,9 +1131,8 @@ 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 OFFSET_MAX	INT_LIMIT(loff_t)
-#define OFFT_OFFSET_MAX	INT_LIMIT(off_t)
+#define OFFSET_MAX	type_max(loff_t)
+#define OFFT_OFFSET_MAX	type_max(off_t)
 #endif
 
 extern void send_sigio(struct fown_struct *fown, int fd, int band);
Signature · 1 line
2.25.1
maintainer Reviewed-by Eric Biggers on the v3 series
#3 lore raw
In reply to Zhen Lei
Quoted 2 lines · Zhen Lei

v2 --> v3:
Updated the commit message of patch 2/2 based on Alexander Viro's suggestion.

Not exactly what I meant... I've tentatively applied it, with the following commit message:

--------------------------------
get rid of INT_LIMIT, use type_max() instead

INT_LIMIT() tries to do what type_max() does, except that type_max() doesn't rely upon undefined behaviour[*], might as well use type_max() instead.

[*] if T is an N-bit signed integer type, the maximal value in T is pow(2, N - 1) - 1, all right, but naive expression for that value ends up with a couple of wraparounds and as usual for wraparounds in signed types, that's an undefined behaviour. type_max() takes care to avoid those...

--------------------------------

Does anybody have objections against the commit message above?

#4 lore raw
Quoted 5 lines · Al Viro
Quoted 2 lines · Zhen Lei

v2 --> v3:
Updated the commit message of patch 2/2 based on Alexander Viro's suggestion.

Not exactly what I meant... I've tentatively applied it, with the

Haha, I felt like something was missing yesterday, too. But as far as my English level is concerned, I usually copy the words suggested by others directly.

Quoted 1 line

following commit message:

Thanks.

Quoted 14 lines

--------------------------------
get rid of INT_LIMIT, use type_max() instead

INT_LIMIT() tries to do what type_max() does, except that type_max() doesn't rely upon undefined behaviour[*], might as well use type_max() instead.

[*] if T is an N-bit signed integer type, the maximal value in T is pow(2, N - 1) - 1, all right, but naive expression for that value ends up with a couple of wraparounds and as usual for wraparounds in signed types, that's an undefined behaviour. type_max() takes care to avoid those...

Quoted 8 lines

--------------------------------

Does anybody have objections against the commit message above?

Looks good to me.

Quoted 3 lines

.

Signature · 2 lines
Regards,
  Zhen Lei