Skip to contentSkip to conversation

fs/erofs: fix an integer overflow in symlink resolution

PATCH In review Gao Xiang 3 participants 4 messages Feb 13, 2025 – Feb 18, 2025 EROFS file system

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v2 1/1 fs/erofs: fix an integer overflow in symlink resolution

See the original report [1], otherwise len + 1 will be overflowed.

Note that EROFS archive can record arbitary symlink sizes in principle, so we don't assume a short number like 4096.

[1] https://lore.kernel.org/r/20250210164151.GN1233568@bill-the-cat

Fixes 830613f8f5bb ("fs/erofs: add erofs filesystem support")

---
v2:
- use __builtin_add_overflow as Jonathan suggested.

 fs/erofs/fs.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fs/erofs/fs.c +7 -4
index 7bd2e8fcfc..dcdc883e34 100644
@@ -59,16 +59,19 @@ struct erofs_dir_stream {
 
 static int erofs_readlink(struct erofs_inode *vi)
 {
-	size_t len = vi->i_size;
+	size_t alloc_size;
 	char *target;
 	int err;
 
-	target = malloc(len + 1);
+	if (__builtin_add_overflow(vi->i_size, 1, &alloc_size))
+		return -EFSCORRUPTED;
+
+	target = malloc(alloc_size);
 	if (!target)
 		return -ENOMEM;
-	target[len] = '\0';
+	target[vi->i_size] = '\0';
 
-	err = erofs_pread(vi, target, len, 0);
+	err = erofs_pread(vi, target, vi->i_size, 0);
 	if (err)
 		goto err_out;
 
Signature · 1 line
2.43.5
#2 lore raw

That's great! Thank you for your work!

Any expected fixes on the other issues I raised Tom?
I'm asking specifically because GRUB2 maintainers are working on solving very similar issues in their repository for SquashFS (being rolled out right now).
It'd be best to solve ASAP as people might realize the similarities between GRUB2 and U-boot in that module.

Best regards,
Jonathan

Quoted 18 lines · Tom Rini
Quoted 8 lines · Gao Xiang

See the original report [1], otherwise len + 1 will be overflowed.

Note that EROFS archive can record arbitary symlink sizes in principle, so we don't assume a short number like 4096.

[1] https://lore.kernel.org/r/20250210164151.GN1233568@bill-the-cat

[...]

Applied to u-boot/master, thanks!

--
Tom

#3 lore raw
Quoted 8 lines · Jonathan Bar Or

That's great! Thank you for your work!

Any expected fixes on the other issues I raised Tom?
I'm asking specifically because GRUB2 maintainers are working on solving very similar issues in their repository for SquashFS (being rolled out right now).
It'd be best to solve ASAP as people might realize the similarities between GRUB2 and U-boot in that module.

Adding back in the squashfs maintainers.

Quoted 23 lines

Best regards,
Jonathan

Quoted 18 lines · Tom Rini
Quoted 8 lines · Gao Xiang

See the original report [1], otherwise len + 1 will be overflowed.

Note that EROFS archive can record arbitary symlink sizes in principle, so we don't assume a short number like 4096.

[1] https://lore.kernel.org/r/20250210164151.GN1233568@bill-the-cat

[...]

Applied to u-boot/master, thanks!

--
Tom

Signature · 1 line
Tom