erofs-utils: lib: fix infinite loop on EOF in erofs_io_xcopy
PATCH 1/1 erofs-utils: lib: fix infinite loop on EOF in erofs_io_xcopy
erofs_io_xcopy() has a fallback do-while loop for when the kernel fast-paths (copy_file_range/sendfile) do not handle all the data. The loop does:
ret = erofs_io_read(vin, buf, ret);
if (ret < 0)
return ret;
if (ret > 0) { ... pos += ret; }
len -= ret;} while (len);
When erofs_io_read() returns 0 (EOF -- source exhausted before all bytes were copied), only the ret < 0 and ret > 0 branches were handled. Since ret == 0, len -= ret is a no-op and while (len) stays true, causing the loop to spin forever at 100% CPU with no error and no progress.
This can be triggered when building an EROFS image from an input file that is shorter than expected -- e.g. a truncated source file, a pipe/FIFO that closes early, or a file being modified concurrently during mkfs.
Fix it by treating a zero return as an error (-ENODATA) so the caller fails cleanly instead of hanging indefinitely.
Also fix the long-standing 'pading' -> 'padding' typo in the short-read diagnostic message of erofs_dev_read().
--- lib/io.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
lib/io.c +8 -7
@@ -430,7 +430,7 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,if (read <0 )return read;if (read < len) { -erofs_info ("reach EOF of device @ %llu, pading with zeroes" , +erofs_info ("reach EOF of device @ %llu, padding with zeroes" , offset |0ULL );memset (buf + read,0 , len - read); }
@@ -665,14 +665,15 @@ int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos, int ret = min_t(unsigned int, len, sizeof(buf)); ret =erofs_io_read (vin, buf, ret); -if (ret <0 ) +if (ret <=0 ) { +if (!ret) +return -ENODATA ;return ret; -if (ret >0 ) { - ret =erofs_io_pwrite (vout, buf, pos, ret); -if (ret <0 ) -return ret; - pos += ret; } + ret =erofs_io_pwrite (vout, buf, pos, ret); +if (ret <0 ) +return ret; + pos += ret; len -= ret; }while (len);return 0 ;
Signature · 1 line
2.51.0.windows.1
Quoted 47 lines · Ajay Rajera
erofs_io_xcopy()has a fallback do-while loop for when the kernel fast-paths (copy_file_range/sendfile) do not handle all the data. The loop does:ret = erofs_io_read(vin, buf, ret); if (ret < 0) return ret; if (ret > 0) { ... pos += ret; } len -= ret;} while (len);
When
erofs_io_read()returns 0 (EOF -- source exhausted before all bytes were copied), only the ret < 0 and ret > 0 branches were handled. Since ret == 0,len -= retis a no-op andwhile (len)stays true, causing the loop to spin forever at 100% CPU with no error and no progress.This can be triggered when building an EROFS image from an input file that is shorter than expected -- e.g. a truncated source file, a pipe/FIFO that closes early, or a file being modified concurrently during mkfs.
Fix it by treating a zero return as an error (
-ENODATA) so the caller fails cleanly instead of hanging indefinitely.Also fix the long-standing 'pading' -> 'padding' typo in the short-read diagnostic message of
erofs_dev_read().Signed-off-by Ajay Rajera <[email protected]>--- lib/io.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)lib/io.c +1
-1@@ -430,7 +430,7 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,if (read <0 )return read;if (read < len) { -erofs_info ("reach EOF of device @ %llu, pading with zeroes" , +erofs_info ("reach EOF of device @ %llu, padding with zeroes" , offset |0ULL );memset (buf + read,0 , len - read); }
lib/io.c int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos, · line 671
Quoted 10 lines
lib/io.c +3
-3@@ -665,14 +665,15 @@ int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos, int ret = min_t(unsigned int, len, sizeof(buf)); ret =erofs_io_read (vin, buf, ret); -if (ret <0 ) +if (ret <=0 ) { +if (!ret) +return -ENODATA ;return ret; -if (ret >0 ) { - ret =erofs_io_pwrite (vout, buf, pos, ret);
Change looks good to me, just minor nits.
Don't need the nested if and I think EIO would be better choice here based on your description. We would expect to see 0 from erofs_io_read when there is an issue with our input file, mostly caused by IO issues.
if (ret < 0)
return ret; else if (!ret) return -EIO;
ret = erofs_io_pwrite(vout, buf, pos, ret);
Regards,
Lucas Karpinski
Hi, Thanks for the review.
Okay, I understand it now.
I've just sent out a v2 patch incorporating your suggested changes.
Best regards,
Ajay Rajera
Quoted 79 lines · Lucas Karpinski
Quoted 47 lines · Ajay Rajera
erofs_io_xcopy()has a fallback do-while loop for when the kernel fast-paths (copy_file_range/sendfile) do not handle all the data. The loop does:ret = erofs_io_read(vin, buf, ret); if (ret < 0) return ret; if (ret > 0) { ... pos += ret; } len -= ret;} while (len);
When
erofs_io_read()returns 0 (EOF -- source exhausted before all bytes were copied), only the ret < 0 and ret > 0 branches were handled. Since ret == 0,len -= retis a no-op andwhile (len)stays true, causing the loop to spin forever at 100% CPU with no error and no progress.This can be triggered when building an EROFS image from an input file that is shorter than expected -- e.g. a truncated source file, a pipe/FIFO that closes early, or a file being modified concurrently during mkfs.
Fix it by treating a zero return as an error (
-ENODATA) so the caller fails cleanly instead of hanging indefinitely.Also fix the long-standing 'pading' -> 'padding' typo in the short-read diagnostic message of
erofs_dev_read().Signed-off-by Ajay Rajera <[email protected]>--- lib/io.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)lib/io.c +1
-1@@ -430,7 +430,7 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,if (read <0 )return read;if (read < len) { -erofs_info ("reach EOF of device @ %llu, pading with zeroes" , +erofs_info ("reach EOF of device @ %llu, padding with zeroes" , offset |0ULL );memset (buf + read,0 , len - read); }lib/io.c int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos, · line 671
Quoted 10 lines
lib/io.c +3
-3@@ -665,14 +665,15 @@ int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos, int ret = min_t(unsigned int, len, sizeof(buf)); ret =erofs_io_read (vin, buf, ret); -if (ret <0 ) +if (ret <=0 ) { +if (!ret) +return -ENODATA ;return ret; -if (ret >0 ) { - ret =erofs_io_pwrite (vout, buf, pos, ret);Change looks good to me, just minor nits.
Don't need the nested if and I think EIO would be better choice here based on your description. We would expect to see 0 from
erofs_io_readwhen there is an issue with our input file, mostly caused by IO issues.if (ret < 0)
return ret; else if (!ret) return -EIO;ret =
erofs_io_pwrite(vout, buf, pos, ret);Regards,
Lucas Karpinski