Skip to contentSkip to conversation

fs/erofs: fix an integer overflow in symlink resolution

PATCH Superseded Gao Xiang 2 participants 4 messages Feb 12, 2025 EROFS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 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")
---
 fs/erofs/fs.c | 3 +++
 1 file changed, 3 insertions(+)
fs/erofs/fs.c +3 -0
index 7bd2e8fcfc..64a6c8cad8 100644
@@ -63,6 +63,9 @@ static int erofs_readlink(struct erofs_inode *vi)
 	char *target;
 	int err;
 
+	if (len >= SIZE_MAX)
+		return -EFSCORRUPTED;
+
 	target = malloc(len + 1);
 	if (!target)
 		return -ENOMEM;
Signature · 1 line
2.43.5
#1 lore raw

This is good, but may I suggest using __builtin_add_overflow instead?

Jonathan

Quoted 30 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

Fixes 830613f8f5bb ("fs/erofs: add erofs filesystem support")
---
 fs/erofs/fs.c | 3 +++
 1 file changed, 3 insertions(+)
fs/erofs/fs.c +3 -1
index 7bd2e8fcfc..64a6c8cad8 100644
@@ -63,6 +63,9 @@ static int erofs_readlink(struct erofs_inode *vi)
        char *target;
        int err;
 
+       if (len >= SIZE_MAX)
+               return -EFSCORRUPTED;
+
        target = malloc(len + 1);
        if (!target)
                return -ENOMEM;
--

2.43.5

author
#2 lore raw
Quoted 1 line · Jonathan Bar Or

This is good, but may I suggest using __builtin_add_overflow instead?

They are just the same.

erofs-utils follows the kernel style, mostly the kernel doesn't directly use __builtin_add_overflow, instead it has a wrapper check_add_overflow().

I could use __builtin_add_overflow for u-boot only.

Thanks,
Gao Xiang

Quoted 3 lines

Jonathan

#3 lore raw

Got it, looks good.

Jonathan

Quoted 20 lines · Gao Xiang
Quoted 1 line · Jonathan Bar Or

This is good, but may I suggest using __builtin_add_overflow instead?

They are just the same.

erofs-utils follows the kernel style, mostly the kernel doesn't directly use __builtin_add_overflow, instead it has a wrapper check_add_overflow().

I could use __builtin_add_overflow for u-boot only.

Thanks,
Gao Xiang

Quoted 3 lines

Jonathan