Skip to contentSkip to conversation

ext4: verify orphan file size is not too big

PATCH Applied Jan Kara 3 participants 4 messages Sep 9, 2025 – Oct 7, 2025 ext4 file system

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH 1/1 ext4: verify orphan file size is not too big

In principle orphan file can be arbitrarily large. However orphan replay needs to traverse it all and we also pin all its buffers in memory. Thus filesystems with absurdly large orphan files can lead to big amounts of memory consumed. Limit orphan file size to a sane value and also use kvmalloc() for allocating array of block descriptor structures to avoid large order allocations for sane but large orphan files.

Reported-by [email protected]
Fixes 02f310fcf47f ("ext4: Speedup ext4 orphan inode handling")
---
 fs/ext4/orphan.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ext4/orphan.c +12 -1
index 524d4658fa40..7e4f48c15c2e 100644
@@ -587,9 +587,20 @@ int ext4_init_orphan_info(struct super_block *sb)
 		ext4_msg(sb, KERN_ERR, "get orphan inode failed");
 		return PTR_ERR(inode);
 	}
+	/*
+	 * This is just an artificial limit to prevent corrupted fs from
+	 * consuming absurd amounts of memory when pinning blocks of orphan
+	 * file in memory.
+	 */
+	if (inode->i_size > 8 << 20) {
+		ext4_msg(sb, KERN_ERR, "orphan file too big: %llu",
+			 (unsigned long long)inode->i_size);
+		ret = -EFSCORRUPTED;
+		goto out_put;
+	}
 	oi->of_blocks = inode->i_size >> sb->s_blocksize_bits;
 	oi->of_csum_seed = EXT4_I(inode)->i_csum_seed;
-	oi->of_binfo = kmalloc_array(oi->of_blocks,
+	oi->of_binfo = kvmalloc_array(oi->of_blocks,
 				     sizeof(struct ext4_orphan_block),
 				     GFP_KERNEL);
 	if (!oi->of_binfo) {
Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw
Quoted 8 lines · Jan Kara

In principle orphan file can be arbitrarily large. However orphan replay needs to traverse it all and we also pin all its buffers in memory. Thus filesystems with absurdly large orphan files can lead to big amounts of memory consumed. Limit orphan file size to a sane value and also use kvmalloc() for allocating array of block descriptor structures to avoid large order allocations for sane but large orphan files.

[...]

Applied, thanks!

[1/1] ext4: verify orphan file size is not too big

      commit: 0a6ce20c156442a4ce2a404747bb0fb05d54eeb3

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>
#2 lore raw
In reply to Jan Kara
Quoted 30 lines · On Tue, 9 Sep 2025 13:22:07 +0200 Jan Kara <[email protected]> wrote:

In principle orphan file can be arbitrarily large. However orphan replay needs to traverse it all and we also pin all its buffers in memory. Thus filesystems with absurdly large orphan files can lead to big amounts of memory consumed. Limit orphan file size to a sane value and also use kvmalloc() for allocating array of block descriptor structures to avoid large order allocations for sane but large orphan files.

Reported-by [email protected]
Fixes 02f310fcf47f ("ext4: Speedup ext4 orphan inode handling")
---
 fs/ext4/orphan.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ext4/orphan.c +8 -0
index 524d4658fa40..7e4f48c15c2e 100644
@@ -587,9 +587,20 @@ int ext4_init_orphan_info(struct super_block *sb)
 		ext4_msg(sb, KERN_ERR, "get orphan inode failed");
 		return PTR_ERR(inode);
 	}
+	/*
+	 * This is just an artificial limit to prevent corrupted fs from
+	 * consuming absurd amounts of memory when pinning blocks of orphan
+	 * file in memory.
+	 */
+	if (inode->i_size > 8 << 20) {
+		ext4_msg(sb, KERN_ERR, "orphan file too big: %llu",
+			 (unsigned long long)inode->i_size);

fs/ext4/orphan.c int ext4_init_orphan_info(struct super_block *sb) · line 606

Quoted 10 lines
fs/ext4/orphan.c +4 -1
+		ret = -EFSCORRUPTED;
+		goto out_put;
+	}
 	oi->of_blocks = inode->i_size >> sb->s_blocksize_bits;
 	oi->of_csum_seed = EXT4_I(inode)->i_csum_seed;
-	oi->of_binfo = kmalloc_array(oi->of_blocks,
+	oi->of_binfo = kvmalloc_array(oi->of_blocks,
 				     sizeof(struct ext4_orphan_block),
 				     GFP_KERNEL);
 	if (!oi->of_binfo) {

Hi everyone,

I tripped over this while testing some review automation on linux-next.

Should we swap all the kfree(oi->of_binfo) to kvfree?

-chris

author reviewer
#3 lore raw
Quoted 45 lines · Chris Mason
Quoted 30 lines · On Tue, 9 Sep 2025 13:22:07 +0200 Jan Kara <[email protected]> wrote:

In principle orphan file can be arbitrarily large. However orphan replay needs to traverse it all and we also pin all its buffers in memory. Thus filesystems with absurdly large orphan files can lead to big amounts of memory consumed. Limit orphan file size to a sane value and also use kvmalloc() for allocating array of block descriptor structures to avoid large order allocations for sane but large orphan files.

Reported-by [email protected]
Fixes 02f310fcf47f ("ext4: Speedup ext4 orphan inode handling")
---
 fs/ext4/orphan.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ext4/orphan.c +8 -0
index 524d4658fa40..7e4f48c15c2e 100644
@@ -587,9 +587,20 @@ int ext4_init_orphan_info(struct super_block *sb)
 		ext4_msg(sb, KERN_ERR, "get orphan inode failed");
 		return PTR_ERR(inode);
 	}
+	/*
+	 * This is just an artificial limit to prevent corrupted fs from
+	 * consuming absurd amounts of memory when pinning blocks of orphan
+	 * file in memory.
+	 */
+	if (inode->i_size > 8 << 20) {
+		ext4_msg(sb, KERN_ERR, "orphan file too big: %llu",
+			 (unsigned long long)inode->i_size);

fs/ext4/orphan.c int ext4_init_orphan_info(struct super_block *sb) · line 606

Quoted 10 lines
fs/ext4/orphan.c +4 -1
+		ret = -EFSCORRUPTED;
+		goto out_put;
+	}
 	oi->of_blocks = inode->i_size >> sb->s_blocksize_bits;
 	oi->of_csum_seed = EXT4_I(inode)->i_csum_seed;
-	oi->of_binfo = kmalloc_array(oi->of_blocks,
+	oi->of_binfo = kvmalloc_array(oi->of_blocks,
 				     sizeof(struct ext4_orphan_block),
 				     GFP_KERNEL);
 	if (!oi->of_binfo) {

Hi everyone,

Quoted 3 lines

I tripped over this while testing some review automation on linux-next.

Should we swap all the kfree(oi->of_binfo) to kvfree?

Yes, we should. Thanks for spotting this. I'll send a fix.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR