Skip to contentSkip to conversation

ext4: avoid oversized shift in ext4_fill_flex_info()

PATCH In review Xi Wang 2 participants 3 messages Dec 26, 2011 – Dec 27, 2011 ext4 file system

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH 1/1 ext4: avoid oversized shift in ext4_fill_flex_info()

Commit 503358ae fixed a division by zero, but groups_per_flex still overflows due to an oversized shift, given a large s_log_groups_per_flex like 36. (1 << 36) is undefined in C; the result may vary depending on the architecture, e.g., 16 on x86, thus bypassing the sanity check (groups_per_flex < 2).

---
 fs/ext4/super.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
fs/ext4/super.c +0 -2
index 3e1329e..6deaf41 100644
@@ -2010,14 +2010,15 @@ static int ext4_fill_flex_info(struct super_block *sb)
 	size_t size;
 	int i;
 
-	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
-	groups_per_flex = 1 << sbi->s_log_groups_per_flex;
#1 lore raw
Quoted 5 lines · Xi Wang

Commit 503358ae fixed a division by zero, but groups_per_flex still overflows due to an oversized shift, given a large s_log_groups_per_flex like 36. (1 << 36) is undefined in C; the result may vary depending on the architecture, e.g., 16 on x86, thus bypassing the sanity check (groups_per_flex < 2).

While this is true in theory, it is not possible to have 2^32 groups per flex group. This would mean 2^32 block bitmaps and inode bitmaps in a single group, which is impossible.

Quoted 32 lines

---
fs/ext4/super.c | 9 +++++----

1 files changed, 5 insertions(+), 4 deletions(-)
fs/ext4/super.c +5 -4
index 3e1329e..6deaf41 100644
@@ -2010,14 +2010,15 @@ static int ext4_fill_flex_info(struct super_block *sb)
   size_t size;
   int i;
 
-    sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
-    groups_per_flex = 1 << sbi->s_log_groups_per_flex;
-
-    if (groups_per_flex < 2) {
+    if (sbi->s_es->s_log_groups_per_flex == 0 ||
+        sbi->s_es->s_log_groups_per_flex >= 32) {
       sbi->s_log_groups_per_flex = 0;
       return 1;
   }
 
+    sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
+    groups_per_flex = 1 << sbi->s_log_groups_per_flex;
+
   /* We allocate both existing and potentially added groups */
   flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
           ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
Signature · 1 line
1.7.5.4
author
#2 lore raw
Quoted 1 line · Andreas Dilger

While this is true in theory, it is not possible to have 2^32 groups per flex group. This would mean 2^32 block bitmaps and inode bitmaps in a single group, which is impossible.

This patch is based on commit 503358ae "ext4: avoid divide by zero when trying to mount a corrupted file system", as I quote: "If s_log_groups_per_flex is greater than 31, then groups_per_flex will will overflow and cause a divide by zero error." The intent is to avoid oversized shift in that commit, because the behavior is undefined in C and may vary on different architectures.

My understanding is that sbi->s_es->s_log_groups_per_flex needs a sanity check if it is read from a malicious or corrupted filesystem in ext4_fill_flex_info(), which is called from ext4_fill_super().

Did I miss anything?

- xi