Skip to contentSkip to conversation

jbd/jbd2: validate sb->s_first in journal_get_superblock()

PATCH Applied Eryu Guan 4 participants 6 messages Nov 1, 2011 – Nov 2, 2011 Journalling layer for block devices (JBD2)

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/2 jbd/jbd2: validate sb->s_first in journal_get_superblock()

I hit a J_ASSERT(blocknr != 0) failure in cleanup_journal_tail() when mounting a fsfuzzed ext3 image. It turns out that the corrupted ext3 image has s_first = 0 in journal superblock, and the 0 is passed to journal->j_head in journal_reset(), then to blocknr in cleanup_journal_tail(), in the end the J_ASSERT failed.

So validate s_first after reading journal superblock from disk in journal_get_superblock() to ensure s_first is valid.

The following script could reproduce it:

fstype=ext3
blocksize=1024
img=$fstype.img
offset=0
found=0
magic="c0 3b 39 98"

dd if=/dev/zero of=$img bs=1M count=8
mkfs -t $fstype -b $blocksize -F $img
filesize=stat -c %s $img
while [ $offset -lt $filesize ]
do
if od -j $offset -N 4 -t x1 $img | grep -i "$magic";then
echo "Found journal: $offset"
found=1
break
fi
offset=echo "$offset+$blocksize" | bc
done

if [ $found -ne 1 ];then
echo "Magic \"$magic\" not found"
exit 1
fi

dd if=/dev/zero of=$img seek=$(($offset+23)) conv=notrunc bs=1 count=1

mkdir -p ./mnt
mount -o loop $img ./mnt

Cc Jan Kara <[email protected]>
Cc "Theodore Ts'o" <[email protected]>
---
 fs/jbd/journal.c  |    8 ++++++++
 fs/jbd2/journal.c |    8 ++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
fs/jbd/journal.c +8 -0
index 9fe061f..0c401de 100644
@@ -1135,6 +1135,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
+		printk(KERN_WARNING
+			"JBD: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;
 
 out:
fs/jbd2/journal.c +8 -0
index f24df13..e2d0e13 100644
@@ -1251,6 +1251,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
+		printk(KERN_WARNING
+			"JBD2: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;
 
 out:
Signature · 1 line
1.7.7.1
#1 lore raw
Quoted 54 lines · Eryu Guan

I hit a J_ASSERT(blocknr != 0) failure in cleanup_journal_tail() when mounting a fsfuzzed ext3 image. It turns out that the corrupted ext3 image has s_first = 0 in journal superblock, and the 0 is passed to journal->j_head in journal_reset(), then to blocknr in cleanup_journal_tail(), in the end the J_ASSERT failed.

So validate s_first after reading journal superblock from disk in journal_get_superblock() to ensure s_first is valid.

The following script could reproduce it:

fstype=ext3
blocksize=1024
img=$fstype.img
offset=0
found=0
magic="c0 3b 39 98"

dd if=/dev/zero of=$img bs=1M count=8
mkfs -t $fstype -b $blocksize -F $img
filesize=stat -c %s $img
while [ $offset -lt $filesize ]
do
if od -j $offset -N 4 -t x1 $img | grep -i "$magic";then
echo "Found journal: $offset"
found=1
break
fi
offset=echo "$offset+$blocksize" | bc
done

if [ $found -ne 1 ];then
echo "Magic \"$magic\" not found"
exit 1
fi

dd if=/dev/zero of=$img seek=$(($offset+23)) conv=notrunc bs=1 count=1

mkdir -p ./mnt
mount -o loop $img ./mnt

Cc Jan Kara <[email protected]>
Cc "Theodore Ts'o" <[email protected]>

---

fs/jbd/journal.c  |    8 ++++++++
fs/jbd2/journal.c |    8 ++++++++
2 files changed, 16 insertions(+), 0 deletions(-)
fs/jbd/journal.c +0 -0
index 9fe061f..0c401de 100644

fs/jbd/journal.c static int journal_get_superblock(journal_t *journal) · line 1139

Quoted 5 lines
fs/jbd/journal.c +2 -0
@@ -1135,6 +1135,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {

(style) Please fix the indenting to align with '(' on previous line.

Quoted 14 lines
fs/jbd/journal.c +6 -0
+		printk(KERN_WARNING
+			"JBD: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;
 

out:

fs/jbd2/journal.c +0 -0
index f24df13..e2d0e13 100644

fs/jbd2/journal.c static int journal_get_superblock(journal_t *journal) · line 1255

Quoted 5 lines
fs/jbd2/journal.c +2 -0
@@ -1251,6 +1251,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {

(style) same.

fs/jbd2/journal.c static int journal_get_superblock(journal_t *journal) · line 1262

Quoted 7 lines
fs/jbd2/journal.c +6 -0
+		printk(KERN_WARNING
+			"JBD2: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;

Looks fine otherwise.

Cheers, Andreas

#2 lore raw
In reply to Eryu Guan
Quoted 36 lines · Eryu Guan

I hit a J_ASSERT(blocknr != 0) failure in cleanup_journal_tail() when mounting a fsfuzzed ext3 image. It turns out that the corrupted ext3 image has s_first = 0 in journal superblock, and the 0 is passed to journal->j_head in journal_reset(), then to blocknr in cleanup_journal_tail(), in the end the J_ASSERT failed.

So validate s_first after reading journal superblock from disk in journal_get_superblock() to ensure s_first is valid.

The following script could reproduce it:

fstype=ext3
blocksize=1024
img=$fstype.img
offset=0
found=0
magic="c0 3b 39 98"

dd if=/dev/zero of=$img bs=1M count=8
mkfs -t $fstype -b $blocksize -F $img
filesize=stat -c %s $img
while [ $offset -lt $filesize ]
do
if od -j $offset -N 4 -t x1 $img | grep -i "$magic";then
echo "Found journal: $offset"
found=1
break
fi
offset=echo "$offset+$blocksize" | bc
done

if [ $found -ne 1 ];then
echo "Magic \"$magic\" not found"
exit 1
fi

Quoted 4 lines

dd if=/dev/zero of=$img seek=$(($offset+23)) conv=notrunc bs=1 count=1

mkdir -p ./mnt
mount -o loop $img ./mnt

Thanks. I've fixed-up the style problems pointed out by Andreas and added
the patch to my tree.

Honza

Quoted 50 lines
Cc Jan Kara <[email protected]>
Cc "Theodore Ts'o" <[email protected]>
---
 fs/jbd/journal.c  |    8 ++++++++
 fs/jbd2/journal.c |    8 ++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
fs/jbd/journal.c +8 -0
index 9fe061f..0c401de 100644
@@ -1135,6 +1135,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
+		printk(KERN_WARNING
+			"JBD: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;
 
 out:
fs/jbd2/journal.c +8 -0
index f24df13..e2d0e13 100644
@@ -1251,6 +1251,14 @@ static int journal_get_superblock(journal_t *journal)
 		goto out;
 	}
 
+	if (be32_to_cpu(sb->s_first) == 0 ||
+		be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
+		printk(KERN_WARNING
+			"JBD2: Invalid start block of journal: %u\n",
+			be32_to_cpu(sb->s_first));
+		goto out;
+	}
+
 	return 0;
 
 out:
Signature · 1 line
1.7.7.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#5 lore raw
In reply to Eryu Guan
Quoted 6 lines · Eryu Guan

I hit a J_ASSERT(blocknr != 0) failure in cleanup_journal_tail() when mounting a fsfuzzed ext3 image. It turns out that the corrupted ext3 image has s_first = 0 in journal superblock, and the 0 is passed to journal->j_head in journal_reset(), then to blocknr in cleanup_journal_tail(), in the end the J_ASSERT failed.

Quoted 6 lines

So validate s_first after reading journal superblock from disk in journal_get_superblock() to ensure s_first is valid.

...

Thanks, applied. (I took the jbd fixup as well, since it's obvious)

- Ted

maintainer
#3 lore raw
In reply to Jan Kara
Quoted 2 lines · Jan Kara

Thanks. I've fixed-up the style problems pointed out by Andreas and added
the patch to my tree.

Oops, I grabbed it too. How do you want to resolve this? I can just carry the jbd2 change, or I can carry both, or I can let you carry both. Either is fine with me. (And if we both carry it git should do the right thing anyway. :-)

- Ted

#4 lore raw
Quoted 8 lines · Ted Tso
Quoted 2 lines · Jan Kara

Thanks. I've fixed-up the style problems pointed out by Andreas and added
the patch to my tree.

Oops, I grabbed it too. How do you want to resolve this? I can just carry the jbd2 change, or I can carry both, or I can let you carry both. Either is fine with me. (And if we both carry it git should do the right thing anyway. :-)

OK, carry it, I'll remove the patch from my tree.

Honza

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