Skip to contentSkip to conversation

ext4: inline data cleanup

PATCH New Thadeu Lima de Souza Cascardo 1 participant 6 messages Dec 6, 2024 ext4 file system

lore
Share & tools
lore
#0 lore raw

cover ext4: inline data cleanup

This series starts some cleanup of the inline data support, making it easier to handle cases where the disk data is changed underneath the system.

They were tested with some test cases that have good coverage with both regular files and directories (indexed and not).

A few syzkaller or hand-written reproducers have also been tested and shown not to crash the system anymore, but mount the filesystem read-only, failing with -EUCLEAN.

Thadeu Lima de Souza Cascardo (5):
ext4: inline: remove extra size check when writing data
ext4: inline: use cached i_inline_size instead of e_value_size
ext4: introduce wrapper to read data from inode
ext4: add a wrapper to update i_inline_off and i_inline_size
ext4: make i_inline_off point to data

 fs/ext4/inline.c | 176 ++++++++++++++++++++++-------------------------
 1 file changed, 84 insertions(+), 92 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/5 ext4: inline: remove extra size check when writing data

ext4_prepare_inline_data which is called right after the removed check has the same check. This avoids extra calls to ext4_get_inode_loc and having to traverse all xattrs in the inode twice for that check.

---
 fs/ext4/inline.c | 3 ---
 1 file changed, 3 deletions(-)
fs/ext4/inline.c +0 -3
index 401a224f0956..0611a050668b 100644
@@ -668,9 +668,6 @@ int ext4_try_to_write_inline_data(struct address_space *mapping,
 	struct folio *folio;
 	struct ext4_iloc iloc;
 
-	if (pos + len > ext4_get_max_inline_size(inode))
-		goto convert;
-
 	ret = ext4_get_inode_loc(inode, &iloc);
 	if (ret)
 		return ret;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/5 ext4: inline: use cached i_inline_size instead of e_value_size

In reply to Thadeu Lima de Souza Cascardo

i_inline_size caches the total size of the inline data, it is always EXT4_MIN_INLINE_DATA_SIZE plus the size of the system.data xattr value.

This avoids looking up for the entry again, which allows i_inline_off to be used to point directly to the inline data.

---
 fs/ext4/inline.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/ext4/inline.c +2 -4
index 0611a050668b..b49cfcadbd36 100644
@@ -80,10 +80,8 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
 		((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
 
 	if (EXT4_I(inode)->i_inline_off) {
-		entry = (struct ext4_xattr_entry *)
-			((void *)raw_inode + EXT4_I(inode)->i_inline_off);
-
-		free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
+		free += EXT4_I(inode)->i_inline_size -
+				EXT4_MIN_INLINE_DATA_SIZE;
 		goto out;
 	}
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/5 ext4: introduce wrapper to read data from inode

In reply to Thadeu Lima de Souza Cascardo

ext4_read_inline_data callers will usually follow similar steps, fetching inode block, parsing xattrs, allocating a buffer, and copying the data. Put it all under the same wrapper and have most callers use it.

Some callers allocate their own buffers, so allow those to be used.

The exception here is ext4_convert_inline_data.

This avoids OOB when reading inline dir and other potential bugs.

When reading inline directory, if e_value_offs is changed underneath the filesystem by some change in the block device, it will lead to an out-of-bounds access that KASAN detects as an UAF.

The wrapper function takes care of checking xattrs are still valid.

---
 fs/ext4/inline.c | 110 +++++++++++++++++++++++++----------------------
 1 file changed, 59 insertions(+), 51 deletions(-)
fs/ext4/inline.c +59 -51
index b49cfcadbd36..c3d2fcae6191 100644
@@ -212,6 +212,51 @@ static int ext4_read_inline_data(struct inode *inode, void *buffer,
 	return cp_len;
 }
 
+static int ext4_read_inline_data_from_inode(struct inode *inode, void **buffer,
+					    size_t *len,
+					    struct ext4_iloc *iloc,
+					    int *has_inline_data)
+{
+	struct ext4_xattr_ibody_find is = {
+		.s = { .not_found = -ENODATA, },
+	};
+	struct ext4_xattr_info i = {
+		.name_index = EXT4_XATTR_INDEX_SYSTEM,
+		.name = EXT4_XATTR_SYSTEM_DATA,
+	};
+	int ret;
+
+	ret = ext4_get_inode_loc(inode, &is.iloc);
+	if (ret)
+		goto out;
+
+	ret = ext4_xattr_ibody_find(inode, &i, &is);
+	if (ret)
+		goto out;
+
+	if (!ext4_has_inline_data(inode)) {
+		if (has_inline_data)
+			*has_inline_data = 0;
+		goto out;
+	}
+
+	if (!*len)
+		*len = ext4_get_inline_size(inode);
+	if (!*buffer) {
+		*buffer = kmalloc(*len, GFP_NOFS);
+		if (!*buffer) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+
+	ret = ext4_read_inline_data(inode, *buffer, *len, &is.iloc);
+
+out:
+	*iloc = is.iloc;
+	return ret;
+}
+
 /*
  * write the buffer to the inline inode.
  * If 'create' is set, we don't need to do the extra copy in the xattr
@@ -492,14 +537,10 @@ static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
 		goto out;
 	}
 
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		goto out;
-
 	len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
 	BUG_ON(len > PAGE_SIZE);
 	kaddr = kmap_local_folio(folio, 0);
-	ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
+	ret = ext4_read_inline_data_from_inode(inode, &kaddr, &len, &iloc, NULL);
 	kaddr = folio_zero_tail(folio, len, kaddr + len);
 	kunmap_local(kaddr);
 	folio_mark_uptodate(folio);
@@ -1333,32 +1374,16 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
 	int pos;
 	struct ext4_dir_entry_2 *de;
 	struct inode *inode = file_inode(dir_file);
-	int ret, inline_size = 0;
+	int ret;
+	size_t inline_size = 0;
 	struct ext4_iloc iloc;
 	void *dir_buf = NULL;
 	struct ext4_dir_entry_2 fake;
 	struct fscrypt_str tmp_str;
 
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		return ret;
-
 	down_read(&EXT4_I(inode)->xattr_sem);
-	if (!ext4_has_inline_data(inode)) {
-		up_read(&EXT4_I(inode)->xattr_sem);
-		*has_inline_data = 0;
-		goto out;
-	}
-
-	inline_size = ext4_get_inline_size(inode);
-	dir_buf = kmalloc(inline_size, GFP_NOFS);
-	if (!dir_buf) {
-		ret = -ENOMEM;
-		up_read(&EXT4_I(inode)->xattr_sem);
-		goto out;
-	}
-
-	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
+	ret = ext4_read_inline_data_from_inode(inode, &dir_buf, &inline_size,
+					       &iloc, has_inline_data);
 	up_read(&EXT4_I(inode)->xattr_sem);
 	if (ret < 0)
 		goto out;
@@ -1452,32 +1477,16 @@ int ext4_read_inline_dir(struct file *file,
 	struct ext4_dir_entry_2 *de;
 	struct super_block *sb;
 	struct inode *inode = file_inode(file);
-	int ret, inline_size = 0;
+	int ret;
+	size_t inline_size = 0;
 	struct ext4_iloc iloc;
 	void *dir_buf = NULL;
 	int dotdot_offset, dotdot_size, extra_offset, extra_size;
 	struct dir_private_info *info = file->private_data;
 
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		return ret;
-
 	down_read(&EXT4_I(inode)->xattr_sem);
-	if (!ext4_has_inline_data(inode)) {
-		up_read(&EXT4_I(inode)->xattr_sem);
-		*has_inline_data = 0;
-		goto out;
-	}
-
-	inline_size = ext4_get_inline_size(inode);
-	dir_buf = kmalloc(inline_size, GFP_NOFS);
-	if (!dir_buf) {
-		ret = -ENOMEM;
-		up_read(&EXT4_I(inode)->xattr_sem);
-		goto out;
-	}
-
-	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
+	ret = ext4_read_inline_data_from_inode(inode, &dir_buf, &inline_size,
+					       &iloc, has_inline_data);
 	up_read(&EXT4_I(inode)->xattr_sem);
 	if (ret < 0)
 		goto out;
@@ -1577,26 +1586,25 @@ int ext4_read_inline_dir(struct file *file,
 void *ext4_read_inline_link(struct inode *inode)
 {
 	struct ext4_iloc iloc;
-	int ret, inline_size;
+	int ret;
+	size_t inline_size;
 	void *link;
 
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		return ERR_PTR(ret);
-
 	ret = -ENOMEM;
 	inline_size = ext4_get_inline_size(inode);
 	link = kmalloc(inline_size + 1, GFP_NOFS);
 	if (!link)
 		goto out;
 
-	ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
+	down_read(&EXT4_I(inode)->xattr_sem);
+	ret = ext4_read_inline_data_from_inode(inode, &link, &inline_size, &iloc, NULL);
 	if (ret < 0) {
 		kfree(link);
 		goto out;
 	}
 	nd_terminate_link(link, inode->i_size, ret);
 out:
+	up_read(&EXT4_I(inode)->xattr_sem);
 	if (ret < 0)
 		link = ERR_PTR(ret);
 	brelse(iloc.bh);
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/5 ext4: add a wrapper to update i_inline_off and i_inline_size

In reply to Thadeu Lima de Souza Cascardo

They were updated in three different places, with slight variations. This will make it easier to change them.

---
 fs/ext4/inline.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
fs/ext4/inline.c +14 -11
index c3d2fcae6191..cd2014af9823 100644
@@ -129,6 +129,15 @@ int ext4_get_max_inline_size(struct inode *inode)
 	return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
 }
 
+static void ext4_update_inline_off_size(struct inode *inode,
+					struct ext4_inode *raw_inode,
+					struct ext4_xattr_entry *entry)
+{
+	EXT4_I(inode)->i_inline_off = (u16)((void *)entry - (void *)raw_inode);
+	EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
+			le32_to_cpu(entry->e_value_size);
+}
+
 /*
  * this function does not take xattr_sem, which is OK because it is
  * currently only used in a code path coming form ext4_iget, before
@@ -163,10 +172,8 @@ int ext4_find_inline_data_nolock(struct inode *inode)
 			error = -EFSCORRUPTED;
 			goto out;
 		}
-		EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
-					(void *)ext4_raw_inode(&is.iloc));
-		EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
-				le32_to_cpu(is.s.here->e_value_size);
+		ext4_update_inline_off_size(inode, ext4_raw_inode(&is.iloc),
+					    is.s.here);
 	}
 out:
 	brelse(is.iloc.bh);
@@ -354,9 +361,8 @@ static int ext4_create_inline_data(handle_t *handle,
 	memset((void *)ext4_raw_inode(&is.iloc)->i_block,
 		0, EXT4_MIN_INLINE_DATA_SIZE);
 
-	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
-				      (void *)ext4_raw_inode(&is.iloc));
-	EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
+	ext4_update_inline_off_size(inode, ext4_raw_inode(&is.iloc), is.s.here);
+
 	ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
 	ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
 	get_bh(is.iloc.bh);
@@ -420,10 +426,7 @@ static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
 	if (error)
 		goto out;
 
-	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
-				      (void *)ext4_raw_inode(&is.iloc));
-	EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
-				le32_to_cpu(is.s.here->e_value_size);
+	ext4_update_inline_off_size(inode, ext4_raw_inode(&is.iloc), is.s.here);
 	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	get_bh(is.iloc.bh);
 	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
Signature · 1 line
2.34.1
#5 lore raw

PATCH 5/5 ext4: make i_inline_off point to data

In reply to Thadeu Lima de Souza Cascardo

Instead of having i_inline_off pointing to the xattr entry, make it point directly to the value offset. That will allow e_value_offs to be ignored, preventing many issues where it might have changed on disk after being validated.

This prevents many potential out-of-bound accesses.

---
 fs/ext4/inline.c | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)
fs/ext4/inline.c +11 -24
index cd2014af9823..92e300c3f873 100644
@@ -133,7 +133,12 @@ static void ext4_update_inline_off_size(struct inode *inode,
 					struct ext4_inode *raw_inode,
 					struct ext4_xattr_entry *entry)
 {
-	EXT4_I(inode)->i_inline_off = (u16)((void *)entry - (void *)raw_inode);
+	struct ext4_xattr_ibody_header *header;
+	void *off;
+
+	header = IHDR(inode, raw_inode);
+	off = (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
+	EXT4_I(inode)->i_inline_off = (u16)(off - (void *)raw_inode);
 	EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
 			le32_to_cpu(entry->e_value_size);
 }
@@ -184,8 +189,6 @@ static int ext4_read_inline_data(struct inode *inode, void *buffer,
 				 unsigned int len,
 				 struct ext4_iloc *iloc)
 {
-	struct ext4_xattr_entry *entry;
-	struct ext4_xattr_ibody_header *header;
 	int cp_len = 0;
 	struct ext4_inode *raw_inode;
 
@@ -205,14 +208,7 @@ static int ext4_read_inline_data(struct inode *inode, void *buffer,
 	if (!len)
 		goto out;
 
-	header = IHDR(inode, raw_inode);
-	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
-					    EXT4_I(inode)->i_inline_off);
-	len = min_t(unsigned int, len,
-		    (unsigned int)le32_to_cpu(entry->e_value_size));
-
-	memcpy(buffer,
-	       (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
+	memcpy(buffer, (void *)raw_inode + EXT4_I(inode)->i_inline_off, len);
 	cp_len += len;
 
 out:
@@ -273,8 +269,6 @@ static int ext4_read_inline_data_from_inode(struct inode *inode, void **buffer,
 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
 				   void *buffer, loff_t pos, unsigned int len)
 {
-	struct ext4_xattr_entry *entry;
-	struct ext4_xattr_ibody_header *header;
 	struct ext4_inode *raw_inode;
 	int cp_len = 0;
 
@@ -301,11 +295,8 @@ static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
 		return;
 
 	pos -= EXT4_MIN_INLINE_DATA_SIZE;
-	header = IHDR(inode, raw_inode);
-	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
-					    EXT4_I(inode)->i_inline_off);
 
-	memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
+	memcpy((void *)raw_inode + EXT4_I(inode)->i_inline_off + pos,
 	       buffer, len);
 }
 
@@ -1085,16 +1076,12 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
 static void *ext4_get_inline_xattr_pos(struct inode *inode,
 				       struct ext4_iloc *iloc)
 {
-	struct ext4_xattr_entry *entry;
-	struct ext4_xattr_ibody_header *header;
+	struct ext4_inode *raw_inode;
 
 	BUG_ON(!EXT4_I(inode)->i_inline_off);
 
-	header = IHDR(inode, ext4_raw_inode(iloc));
-	entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
-					    EXT4_I(inode)->i_inline_off);
-
-	return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
+	raw_inode = ext4_raw_inode(iloc);
+	return (void *)raw_inode + EXT4_I(inode)->i_inline_off;
 }
 
 /* Set the final de to cover the whole block. */
Signature · 1 line
2.34.1