Skip to contentSkip to conversation

ext4fs: Fix: Read outside partition error

PATCH In review Tony Dinh 2 participants 4 messages May 23, 2025 – May 26, 2025 ext4 file system

lore
Share & tools
lore

Tony Dinh

author
#0 lore raw

PATCH 1/1 ext4fs: Fix: Read outside partition error

Use lbaint_t for blknr to avoid overflow in ext4fs_read_file().

Background:

blknr (block number) used in ext4fs_read_file() could be increased to a very large value and causes a wrap around at 32 bit signed integer max, thus becomes negative. This results in an out-of-normal range for sector number (during the assignment delayed_start = blknr) where delayed_start sector is typed uint64 lbaint_t. This causes the "Read outside partition" error.

This patch was tested on the Synology DS116 (Armada 385) board, and a 4TB Seagate HDD.

---

 fs/ext4/ext4fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4fs.c +1 -1
index 1727da2dc6d..f10c622e62c 100644
@@ -101,7 +101,7 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
 	blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
 
 	for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
-		long int blknr;
+		lbaint_t blknr;
 		int blockoff = pos - (blocksize * i);
 		int blockend = blocksize;
 		int skipfirst = 0;
Signature · 4 lines
2.39.5

base-commit: 421d4844591e967435fe0fcfa048ad45c3a48377
branch: master
#1 lore raw
Quoted 4 lines · Tony Dinh

Use lbaint_t for blknr to avoid overflow in ext4fs_read_file().

Background:

Quoted 8 lines

blknr (block number) used in ext4fs_read_file() could be increased to a very large value and causes a wrap around at 32 bit signed integer max, thus becomes negative. This results in an out-of-normal range for sector number (during the assignment delayed_start = blknr) where delayed_start sector is typed uint64 lbaint_t. This causes the "Read outside partition" error.

[...]

Applied to u-boot/master, thanks!

[1/1] ext4fs: Fix: Read outside partition error

      commit: df2ed552f0b05591090369a7fe7ddc92439dea5c
Signature · 1 line
Tom
#2 lore raw
In reply to Tony Dinh
Quoted 25 lines · Tony Dinh

Use lbaint_t for blknr to avoid overflow in ext4fs_read_file().

Background:

blknr (block number) used in ext4fs_read_file() could be increased to a very large value and causes a wrap around at 32 bit signed integer max, thus becomes negative. This results in an out-of-normal range for sector number (during the assignment delayed_start = blknr) where delayed_start sector is typed uint64 lbaint_t. This causes the "Read outside partition" error.

This patch was tested on the Synology DS116 (Armada 385) board, and a 4TB Seagate HDD.

---

 fs/ext4/ext4fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4fs.c +0 -0
index 1727da2dc6d..f10c622e62c 100644

fs/ext4/ext4fs.c int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, · line 107

Quoted 8 lines
fs/ext4/ext4fs.c +1 -1
@@ -101,7 +101,7 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
 	blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
 
 	for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
-		long int blknr;
+		lbaint_t blknr;
 		int blockoff = pos - (blocksize * i);
 		int blockend = blocksize;
 		int skipfirst = 0;

Sorry, I should have reviewed this deeper before accepting it. This patch isn't right because it introduces a new problem, we ignore all failures from read_allocated_block() now because we can never return a negative value. This was reported by Coverity as CID 131183 and I need to revert this for now.

Signature · 1 line
Tom
author
#3 lore raw

Hi Tom,

Quoted 30 lines · Tom Rini
Quoted 27 lines · Tony Dinh

Use lbaint_t for blknr to avoid overflow in ext4fs_read_file().

Background:

blknr (block number) used in ext4fs_read_file() could be increased to a very large value and causes a wrap around at 32 bit signed integer max, thus becomes negative. This results in an out-of-normal range for sector number (during the assignment delayed_start = blknr) where delayed_start sector is typed uint64 lbaint_t. This causes the "Read outside partition" error.

This patch was tested on the Synology DS116 (Armada 385) board, and a 4TB Seagate HDD.

---

 fs/ext4/ext4fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4fs.c +0 -0
index 1727da2dc6d..f10c622e62c 100644
@@ -101,7 +101,7 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
      blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
 
Quoted 12 lines

fs/ext4/ext4fs.c int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, · line 107

Quoted 6 lines
fs/ext4/ext4fs.c +1 -1
      for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
-             long int blknr;
+             lbaint_t blknr;
              int blockoff = pos - (blocksize * i);
              int blockend = blocksize;
              int skipfirst = 0;

Sorry, I should have reviewed this deeper before accepting it. This patch isn't right because it introduces a new problem, we ignore all failures from read_allocated_block() now because we can never return a negative value. This was reported by Coverity as CID 131183 and I need to revert this for now.

Thanks! Indeed that was bad. I'll take a look again.

All the best,
Tony

Quoted 3 lines

--
Tom