Skip to contentSkip to conversation

ext4: avoid BUG_ON() in ext4_get_inline_entry()

PATCH In review Vineet Agarwal 2 participants 2 messages Apr 27 – Apr 28 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: avoid BUG_ON() in ext4_get_inline_entry()

Corrupted inline directory metadata can cause offset to exceed the inline data size through rec_len processing in empty_inline_dir().

This triggers BUG_ON() in ext4_get_inline_entry(), causing a kernel panic before ext4_check_dir_entry() can handle the corruption gracefully.

Replace BUG_ON() with a NULL return and handle the invalid offset in the caller by emitting a warning and exiting safely.

This prevents a kernel panic from corrupted inline directory metadata.

---
 fs/ext4/inline.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/ext4/inline.c +8 -1
index 8045e4ff270c..bca9936ed6d0 100644
@@ -1718,7 +1718,8 @@ ext4_get_inline_entry(struct inode *inode,
 {
 	void *inline_pos;
 
-	BUG_ON(offset > ext4_get_inline_size(inode));
+	if (offset > ext4_get_inline_size(inode))
+		return NULL;
 
 	if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
 		inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
@@ -1773,6 +1774,12 @@ bool empty_inline_dir(struct inode *dir, int *has_inline_data)
 	while (offset < inline_len) {
 		de = ext4_get_inline_entry(dir, &iloc, offset,
 					   &inline_pos, &inline_size);
+		if (!de) {
+			ext4_warning(dir->i_sb,
+				     "bad inline directory (dir #%llu) - invalid offset",
+				     dir->i_ino);
+			goto out;
+		}
 		if (ext4_check_dir_entry(dir, NULL, de,
 					 iloc.bh, inline_pos,
 					 inline_size, offset)) {
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 8 lines · Vineet Agarwal

Corrupted inline directory metadata can cause offset to exceed the inline data size through rec_len processing in empty_inline_dir().

This triggers BUG_ON() in ext4_get_inline_entry(), causing a kernel panic before ext4_check_dir_entry() can handle the corruption gracefully.

Quoted 7 lines

Replace BUG_ON() with a NULL return and handle the invalid offset in the caller by emitting a warning and exiting safely.

This prevents a kernel panic from corrupted inline directory metadata.

I guess this is motivated by some syskaller fuzzing (would be good to reference). It however doesn't explain how the BUG_ON can trigger when empty_inline_dir() just did:

	inline_len = ext4_get_inline_size(dir);
	...
	while (offset < inline_len) {
		de = ext4_get_inline_entry(dir, &iloc, offset, ...)

I suspect modification of the fs image while being mounted which is not something we care about.

Honza

Quoted 34 lines
---
 fs/ext4/inline.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/ext4/inline.c +8 -1
index 8045e4ff270c..bca9936ed6d0 100644
@@ -1718,7 +1718,8 @@ ext4_get_inline_entry(struct inode *inode,
 {
 	void *inline_pos;
 
-	BUG_ON(offset > ext4_get_inline_size(inode));
+	if (offset > ext4_get_inline_size(inode))
+		return NULL;
 
 	if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
 		inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
@@ -1773,6 +1774,12 @@ bool empty_inline_dir(struct inode *dir, int *has_inline_data)
 	while (offset < inline_len) {
 		de = ext4_get_inline_entry(dir, &iloc, offset,
 					   &inline_pos, &inline_size);
+		if (!de) {
+			ext4_warning(dir->i_sb,
+				     "bad inline directory (dir #%llu) - invalid offset",
+				     dir->i_ino);
+			goto out;
+		}
 		if (ext4_check_dir_entry(dir, NULL, de,
 					 iloc.bh, inline_pos,
 					 inline_size, offset)) {
Signature · 1 line
2.54.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR