iomap: Fix a false positive of UBSAN in iomap_seek_data()
Subsystems (2)
PATCH 1/1 iomap: Fix a false positive of UBSAN in iomap_seek_data()
Move the evaluation expression "size - offset" after the "if (offset < 0)" judgment statement to eliminate a false positive produced by the UBSAN.
No functional changes.
==========================================================================
UBSAN: Undefined behaviour in fs/iomap.c:1435:9 signed integer overflow: 0 - -9223372036854775808 cannot be represented in type 'long long int' CPU: 1 PID: 462 Comm: syz-executor852 Tainted: G ---------r- - 4.18.0+ #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ... Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0xca/0x13e lib/dump_stack.c:113 ubsan_epilogue+0xe/0x81 lib/ubsan.c:159 handle_overflow+0x193/0x1e2 lib/ubsan.c:190 iomap_seek_data+0x128/0x140 fs/iomap.c:1435 ext4_llseek+0x1e3/0x290 fs/ext4/file.c:494 vfs_llseek fs/read_write.c:300 [inline] ksys_lseek+0xe9/0x160 fs/read_write.c:313 do_syscall_64+0xca/0x5b0 arch/x86/entry/common.c:293 entry_SYSCALL_64_after_hwframe+0x6a/0xdf ==========================================================================
--- fs/iomap/seek.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/iomap/seek.c +2 -1
@@ -83,13 +83,14 @@ loff_t iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset; +loff_t length;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; + length = size - offset;while (length >0 ) { ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, &offset, iomap_seek_data_actor);
Signature · 1 line
2.25.1
We might as well just kill off the length variable while we're at it:
fs/iomap/seek.c +3 -5
@@ -35,23 +35,21 @@ loff_t iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; -while (length >0 ) { - ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, - &offset, iomap_seek_hole_actor); +while (offset < size) { + ret =iomap_apply (inode, offset, size - offset,IOMAP_REPORT , + ops, &offset, iomap_seek_hole_actor);if (ret <0 )return ret;if (ret ==0 )break ; offset += ret; - length -= ret; }return offset;
Quoted 1 line · Christoph Hellwig
We might as well just kill off the length variable while we're at it:
Hi, Christoph:
Maybe you need to write a separate patch. Because the patch I sent is
to modify function iomap_seek_data(). I didn't look at the other functions. In fact, both iomap_seek_data() and iomap_seek_hole() need to be modified. The iomap_seek_data() may not be intuitive to delete the variable 'length'.
I'm now analyzing if the "if (length <= 0)" statement in iomap_seek_data() is redundant (the condition is never true).
Quoted 36 lines
fs/iomap/seek.c +3
-5@@ -35,23 +35,21 @@ loff_t iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; -while (length >0 ) { - ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, - &offset, iomap_seek_hole_actor); +while (offset < size) { + ret =iomap_apply (inode, offset, size - offset,IOMAP_REPORT , + ops, &offset, iomap_seek_hole_actor);if (ret <0 )return ret;if (ret ==0 )break ; offset += ret; - length -= ret; }return offset;.
Quoted 5 lines · Leizhen (ThunderTown)
Quoted 1 line · Christoph Hellwig
We might as well just kill off the length variable while we're at it:
Quoted 8 lines
Hi, Christoph:
Maybe you need to write a separate patch. Because the patch I sent is
to modify functioniomap_seek_data(). I didn't look at the other functions. In fact, bothiomap_seek_data()andiomap_seek_hole()need to be modified. Theiomap_seek_data()may not be intuitive to delete the variable 'length'.I'm now analyzing if the "if (length <= 0)" statement in
iomap_seek_data()is redundant (the condition is never true).
I've thought about it, and that "if" statement can be removed as follows:
fs/iomap/seek.c +3 -4
@@ -96,14 +96,13 @@ iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)if (ret <0 )return ret;if (ret ==0 ) -break ; +return offset; offset += ret; length -= ret; } -if (length <=0 ) -return -ENXIO ; -return offset; +/* The end of the file is reached, and no data is found */ +return -ENXIO ; }EXPORT_SYMBOL_GPL (iomap_seek_data);
Quoted 37 lines
Quoted 37 lines
fs/iomap/seek.c +3
-5@@ -35,23 +35,21 @@ loff_t iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; -while (length >0 ) { - ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, - &offset, iomap_seek_hole_actor); +while (offset < size) { + ret =iomap_apply (inode, offset, size - offset,IOMAP_REPORT , + ops, &offset, iomap_seek_hole_actor);if (ret <0 )return ret;if (ret ==0 )break ; offset += ret; - length -= ret; }return offset;.
Quoted 1 line · Leizhen (ThunderTown)
I've thought about it, and that "if" statement can be removed as follows:
I think this really misses Christoph's point. He's looking for something more like this:
@@ -83,27 +83,23 @@ loff_t
iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
{
loff_t size = i_size_read(inode);
- loff_t length = size - offset;
loff_t ret;
/* Nothing to be found before or beyond the end of the file. */
if (offset < 0 || offset >= size)
return -ENXIO;- while (length > 0) {
+ while (offset < size) {
ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
&offset, iomap_seek_data_actor);
if (ret < 0)
return ret;
if (ret == 0)- break;
+ return offset;
offset += ret;
- length -= ret;
}- if (length <= 0)
- return -ENXIO;
- return offset;
+ return -ENXIO;
}
EXPORT_SYMBOL_GPL(iomap_seek_data);
(not even slightly tested)
Quoted 5 lines · Matthew Wilcox
Quoted 1 line · Leizhen (ThunderTown)
I've thought about it, and that "if" statement can be removed as follows:
I think this really misses Christoph's point. He's looking for something more like this:
Yes, I know that. I need to get rid of the "if" judgment first, and then I can modify iomap_seek_data() according to Christoph's point. Otherwise there are too many conversions from "length" to "size - offset" and the code doesn't look clear.
Quoted 37 lines
fs/iomap/seek.c +3
-7@@ -83,27 +83,23 @@ loff_t iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; -while (length >0 ) { +while (offset < size) { ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, &offset, iomap_seek_data_actor);if (ret <0 )return ret;if (ret ==0 ) -break ; +return offset; offset += ret; - length -= ret; } -if (length <=0 ) -return -ENXIO ; -return offset; +return -ENXIO ; }EXPORT_SYMBOL_GPL (iomap_seek_data);(not even slightly tested)
.
Matthew Wilcox
Acked-byQuoted 1 line · Christoph Hellwig
We might as well just kill off the length variable while we're at it:
Quoted 31 lines
fs/iomap/seek.c +3
-5@@ -35,23 +35,21 @@ loff_t iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) { loff_t size = i_size_read(inode); -loff_t length = size - offset;loff_t ret;/* Nothing to be found before or beyond the end of the file. */ if (offset <0 || offset >= size)return -ENXIO ; -while (length >0 ) { - ret =iomap_apply (inode, offset, length,IOMAP_REPORT , ops, - &offset, iomap_seek_hole_actor); +while (offset < size) { + ret =iomap_apply (inode, offset, size - offset,IOMAP_REPORT , + ops, &offset, iomap_seek_hole_actor);if (ret <0 )return ret;if (ret ==0 )break ; offset += ret; - length -= ret; }return offset;
Quoted 9 lines · Zhen Lei
Move the evaluation expression "size - offset" after the "if (offset < 0)" judgment statement to eliminate a false positive produced by the UBSAN.
No functional changes.
==========================================================================
UBSAN: Undefined behaviour in fs/iomap.c:1435:9signed integer overflow:
0 - -9223372036854775808 cannot be represented in type 'long long int'
I don't understand. I thought we defined the behaviour of signed integer overflow in the kernel with whatever-the-gcc-flag-is?
Quoted 6 lines · Matthew Wilcox
Quoted 5 lines · Zhen Lei
Move the evaluation expression "size - offset" after the "if (offset < 0)" judgment statement to eliminate a false positive produced by the UBSAN.
No functional changes.
Quoted 7 lines
Quoted 4 lines
==========================================================================
UBSAN: Undefined behaviour in fs/iomap.c:1435:9signed integer overflow:
0 - -9223372036854775808 cannot be represented in type 'long long int'I don't understand. I thought we defined the behaviour of signed integer overflow in the kernel with whatever-the-gcc-flag-is?
-9223372036854775808 ==> 0x8000000000000000 ==> -0
I don't fully understand what you mean. This is triggered by explicit error injection '-0' at runtime, which should not be detected by compilation options.
lseek(r1, 0x8000000000000000, 0x3)
Quoted 4 lines
.
Quoted 14 lines · Leizhen (ThunderTown)
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Zhen Lei
Move the evaluation expression "size - offset" after the "if (offset < 0)" judgment statement to eliminate a false positive produced by the UBSAN.
No functional changes.
==========================================================================
UBSAN: Undefined behaviour in fs/iomap.c:1435:9signed integer overflow:
0 - -9223372036854775808 cannot be represented in type 'long long int'
Quoted 7 lines
Quoted 2 lines
I don't understand. I thought we defined the behaviour of signed integer overflow in the kernel with whatever-the-gcc-flag-is?
-9223372036854775808 ==>
0x8000000000000000==> -0I don't fully understand what you mean. This is triggered by explicit error injection '-0' at runtime, which should not be detected by compilation options.
We use -fwrapv on the gcc command line:
'-fwrapv'
This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others.
Quoted 1 line
lseek(r1, 0x8000000000000000, 0x3)
I'll see about adding this to xfstests ...
Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Leizhen (ThunderTown)
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Zhen Lei
Move the evaluation expression "size - offset" after the "if (offset < 0)" judgment statement to eliminate a false positive produced by the UBSAN.
No functional changes.
==========================================================================
UBSAN: Undefined behaviour in fs/iomap.c:1435:9signed integer overflow:
0 - -9223372036854775808 cannot be represented in type 'long long int'
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
I don't understand. I thought we defined the behaviour of signed integer overflow in the kernel with whatever-the-gcc-flag-is?
-9223372036854775808 ==>
0x8000000000000000==> -0
(actually, this is incorrect. think about how twos-complement arithmetic works. first you negate every bit, so 8000..000 turns into 7fff..fff, then you add one, returning to 8000..000, so -LLONG_MIN == LLONG_MIN)
Quoted 11 lines
Quoted 2 lines
I don't fully understand what you mean. This is triggered by explicit error injection '-0' at runtime, which should not be detected by compilation options.
We use -fwrapv on the gcc command line:
'-fwrapv'
This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others.
Quoted 3 lines
Quoted 1 line
lseek(r1, 0x8000000000000000, 0x3)I'll see about adding this to xfstests ...
I have and it doesn't produce the problem. My config:
CONFIG_UBSAN=y
# CONFIG_UBSAN_TRAP is not setCONFIG_CC_HAS_UBSAN_BOUNDS=yCONFIG_UBSAN_BOUNDS=yCONFIG_UBSAN_ONLY_BOUNDS=yCONFIG_UBSAN_SHIFT=yCONFIG_UBSAN_DIV_ZERO=yCONFIG_UBSAN_BOOL=yCONFIG_UBSAN_ENUM=y
# CONFIG_UBSAN_ALIGNMENT is not setCONFIG_UBSAN_SANITIZE_ALL=y
# CONFIG_TEST_UBSAN is not set
I even went as far as adding printks to be sure I'm hitting it:
hole length 0x8000000000000000
data length 0x8000000000000000
Are you compiling with:KBUILD_CFLAGS += -fno-strict-overflow
Or have you done something weird? What compiler version are you using?