Skip to contentSkip to conversation

ext4: Fix integer overflow in ext4fs_read_symlink()

PATCH In review Richard Weinberger 5 participants 9 messages Aug 9, 2024 – Aug 26, 2024 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 ext4: Fix integer overflow in ext4fs_read_symlink()

While zalloc() takes a size_t type, adding 1 to the le32 variable will overflow.
A carefully crafted ext4 filesystem can exhibit an inode size of 0xffffffff and as consequence zalloc() will do a zero allocation.

Later in the function the inode size is again used for copying data. So an attacker can overwrite memory.

Avoid the overflow by using the __builtin_add_overflow() helper.

---
 fs/ext4/ext4_common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/ext4_common.c +6 -1
index 52152a2295..36999b608f 100644
@@ -2181,13 +2181,18 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)
 	struct ext2fs_node *diro = node;
 	int status;
 	loff_t actread;
+	size_t alloc_size;
 
 	if (!diro->inode_read) {
 		status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
 		if (status == 0)
 			return NULL;
 	}
-	symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
+
+	if (__builtin_add_overflow(le32_to_cpu(diro->inode.size), 1, &alloc_size))
+		return NULL;
+
+	symlink = zalloc(alloc_size);
 	if (!symlink)
 		return NULL;
 
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 2/3 compiler: Ensure __builtin_*_overflow() support

Both gcc and clang support this for a long time.
Make sure the feature is present.

---
 include/linux/compiler_types.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/compiler_types.h +7 -0
index 1a3060117f..8b6ce9c11c 100644
@@ -70,6 +70,13 @@ extern void __chk_io_ptr(const volatile void __iomem *);
 #error "Unknown compiler"
 #endif
 
+/*
+ * At least gcc 5.1 or clang 8 are needed.
+ */
+#ifndef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
+#error Unsupported compiler
+#endif
+
 /*
  * Some architectures need to provide custom definitions of macros provided
  * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
Signature · 1 line
2.35.3
#2 lore raw

PATCH v2 3/3 ext4: Fix zalloc()

In reply to Richard Weinberger

Currently, zalloc() calls uncondtionally memset(),
if the allocation failes, memset() will write to a null pointer.

Fix by using kzalloc().

---
 fs/ext4/ext4_common.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/ext4_common.h +2 -3
index 84500e990a..346752092b 100644
@@ -24,6 +24,7 @@
 #include <ext4fs.h>
 #include <malloc.h>
 #include <asm/cache.h>
+#include <linux/compat.h>
 #include <linux/errno.h>
 #if defined(CONFIG_EXT4_WRITE)
 #include "ext4_journal.h"
@@ -43,9 +44,7 @@
 
 static inline void *zalloc(size_t size)
 {
-	void *p = memalign(ARCH_DMA_MINALIGN, size);
-	memset(p, 0, size);
-	return p;
+	return kzalloc(size, 0);
 }
 
 int ext4fs_read_inode(struct ext2_data *data, int ino,
Signature · 1 line
2.35.3
Reviewed-by on v2 3/3
#3 lore raw

Re: [PATCH v2 3/3] ext4: Fix zalloc()

Quoted 6 lines · Richard Weinberger

Currently, zalloc() calls uncondtionally memset(),
if the allocation failes, memset() will write to a null pointer.

Fix by using kzalloc().

Quoted 27 lines
---
  fs/ext4/ext4_common.h | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/ext4_common.h +2 -3
index 84500e990a..346752092b 100644
@@ -24,6 +24,7 @@
  #include <ext4fs.h>
  #include <malloc.h>
  #include <asm/cache.h>
+#include <linux/compat.h>
  #include <linux/errno.h>
  #if defined(CONFIG_EXT4_WRITE)
  #include "ext4_journal.h"
@@ -43,9 +44,7 @@
 
  static inline void *zalloc(size_t size)
  {
-	void *p = memalign(ARCH_DMA_MINALIGN, size);
-	memset(p, 0, size);
-	return p;
+	return kzalloc(size, 0);
  }
 
  int ext4fs_read_inode(struct ext2_data *data, int ino,
Reviewed-by on v2 3/3
#4 lore raw

Re: [PATCH v2 3/3] ext4: Fix zalloc()

In reply to Richard Weinberger
Quoted 35 lines · Richard Weinberger

Currently, zalloc() calls uncondtionally memset(),
if the allocation failes, memset() will write to a null pointer.

Fix by using kzalloc().

---
 fs/ext4/ext4_common.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ext4/ext4_common.h +2 -4
index 84500e990a..346752092b 100644
@@ -24,6 +24,7 @@
 #include <ext4fs.h>
 #include <malloc.h>
 #include <asm/cache.h>
+#include <linux/compat.h>
 #include <linux/errno.h>
 #if defined(CONFIG_EXT4_WRITE)
 #include "ext4_journal.h"
@@ -43,9 +44,7 @@
 
 static inline void *zalloc(size_t size)
 {
-       void *p = memalign(ARCH_DMA_MINALIGN, size);
-       memset(p, 0, size);
-       return p;
+       return kzalloc(size, 0);
 }
 
 int ext4fs_read_inode(struct ext2_data *data, int ino,
--
Quoted 2 lines

2.35.3

#5 lore raw
In reply to Richard Weinberger
Quoted 24 lines · Richard Weinberger

While zalloc() takes a size_t type, adding 1 to the le32 variable will overflow.
A carefully crafted ext4 filesystem can exhibit an inode size of 0xffffffff and as consequence zalloc() will do a zero allocation.

Later in the function the inode size is again used for copying data. So an attacker can overwrite memory.

Avoid the overflow by using the __builtin_add_overflow() helper.

---
  fs/ext4/ext4_common.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/ext4_common.c +1 -0
index 52152a2295..36999b608f 100644
@@ -2181,13 +2181,18 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)
  	struct ext2fs_node *diro = node;
  	int status;
  	loff_t actread;
+	size_t alloc_size;

fs/ext4/ext4_common.c static char *ext4fs_read_symlink(struct ext2fs_node *node) · line 2193

Quoted 10 lines
fs/ext4/ext4_common.c +3 -1
 
  	if (!diro->inode_read) {
  		status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
  		if (status == 0)
  			return NULL;
  	}
-	symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
+
+	if (__builtin_add_overflow(le32_to_cpu(diro->inode.size), 1, &alloc_size))
+		return NULL;

Thank you for pointing at the problematic code.

You are calling __builtin_add_overflow(int, int, size_t *).

__builtin_add_overflow() is not defined in the C-standard.

Is there any well defined behavior for this on systems where size_t has more bits than int?

I could imagine implementations just copying an int to the first 32 bits of alloc_size and leaving the other bits untouched.

Hopefully your C-compiler simply refuses to compile this code.

I would prefer to stick to standard C:

alloc_size = le32_to_cpu(diro->inode.size) + 1UL;
if (!alloc_size)

	return NULL;

Here an overflow can only occur on 32bit systems.

Best regards

Heinrich

Quoted 5 lines
fs/ext4/ext4_common.c +2 -0
+
+	symlink = zalloc(alloc_size);
  	if (!symlink)
  		return NULL;
 
#6 lore raw

Heinrich,

Am Freitag, 9. August 2024, 18:13:27 CEST schrieb 'Heinrich Schuchardt' via upstream:

Quoted 8 lines

Thank you for pointing at the problematic code.

You are calling __builtin_add_overflow(int, int, size_t *).

__builtin_add_overflow() is not defined in the C-standard.

Is there any well defined behavior for this on systems where size_t has more bits than int?

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html says:

Built-in Function: bool __builtin_add_overflow (type1 a, type2 b, type3 *res)

These built-in functions promote the first two operands into infinite precision signed type and perform addition on those promoted operands. The result is then cast to the type the third pointer argument points to and stored there.
If the stored result is equal to the infinite precision result, the built-in functions return false, otherwise they return true. As the addition is performed in infinite signed precision, these built-in functions have fully defined behavior for all argument values.

So, I don't really see a problem here.

Quoted 5 lines

I could imagine implementations just copying an int to the first 32 bits of alloc_size and leaving the other bits untouched.

Hopefully your C-compiler simply refuses to compile this code.

Quoted 7 lines

I would prefer to stick to standard C:

alloc_size = le32_to_cpu(diro->inode.size) + 1UL;
if (!alloc_size)

	return NULL;

Here an overflow can only occur on 32bit systems.

In this specific case we could avoid the builtis, yes. But I prefer using __builtin_add_overflow() as it offers a generic and bullet prove mechanism to avoid these kind of bugs.

It's not that they are something new and funky.
Other projects use them since ages.

Thanks,
//richard

Signature · 2 lines
​​​​​sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT
UID/VAT Nr: ATU 66964118 | FN: 374287y
#7 lore raw
Quoted 34 lines · Richard Weinberger

Heinrich,

Am Freitag, 9. August 2024, 18:13:27 CEST schrieb 'Heinrich Schuchardt' via upstream:

Quoted 8 lines

Thank you for pointing at the problematic code.

You are calling __builtin_add_overflow(int, int, size_t *).

__builtin_add_overflow() is not defined in the C-standard.

Is there any well defined behavior for this on systems where size_t has more bits than int?

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html says:

Built-in Function: bool __builtin_add_overflow (type1 a, type2 b, type3 *res)

These built-in functions promote the first two operands into infinite precision signed type and perform addition on those promoted operands. The result is then cast to the type the third pointer argument points to and stored there.
If the stored result is equal to the infinite precision result, the built-in functions return false, otherwise they return true. As the addition is performed in infinite signed precision, these built-in functions have fully defined behavior for all argument values.

So, I don't really see a problem here.

Quoted 11 lines

I could imagine implementations just copying an int to the first 32 bits of alloc_size and leaving the other bits untouched.

Hopefully your C-compiler simply refuses to compile this code.

I would prefer to stick to standard C:

alloc_size = le32_to_cpu(diro->inode.size) + 1UL;
if (!alloc_size)

	return NULL;
Quoted 8 lines
Quoted 1 line

Here an overflow can only occur on 32bit systems.

In this specific case we could avoid the builtis, yes. But I prefer using __builtin_add_overflow() as it offers a generic and bullet prove mechanism to avoid these kind of bugs.

It's not that they are something new and funky.
Other projects use them since ages.

I'm fine with using this buildin, it's widely enough implemented, for good reason.

Signature · 1 line
Tom
#8 lore raw
In reply to Richard Weinberger
Quoted 9 lines · Richard Weinberger

While zalloc() takes a size_t type, adding 1 to the le32 variable will overflow.
A carefully crafted ext4 filesystem can exhibit an inode size of 0xffffffff and as consequence zalloc() will do a zero allocation.

Later in the function the inode size is again used for copying data. So an attacker can overwrite memory.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom