Skip to content

Commit a0626e7

Browse files
djwongtytso
authored andcommitted
ext4: check EA value offset when loading
When loading extended attributes, check each entry's value offset to make sure it doesn't collide with the entries. Without this check it is easy to crash the kernel by mounting a malicious FS containing a file with an EA wherein e_value_offs = 0 and e_value_size > 0 and then deleting the EA, which corrupts the name list. (See the f_ea_value_crash test's FS image in e2fsprogs for an example.) Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@vger.kernel.org
1 parent 684de57 commit a0626e7

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

‎fs/ext4/xattr.c‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,28 @@ ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
190190
}
191191

192192
static int
193-
ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
193+
ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
194+
void *value_start)
194195
{
195-
while (!IS_LAST_ENTRY(entry)) {
196-
struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
196+
struct ext4_xattr_entry *e = entry;
197+
198+
while (!IS_LAST_ENTRY(e)) {
199+
struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197200
if ((void *)next >= end)
198201
return -EIO;
199-
entry = next;
202+
e = next;
200203
}
204+
205+
while (!IS_LAST_ENTRY(entry)) {
206+
if (entry->e_value_size != 0 &&
207+
(value_start + le16_to_cpu(entry->e_value_offs) <
208+
(void *)e + sizeof(__u32) ||
209+
value_start + le16_to_cpu(entry->e_value_offs) +
210+
le32_to_cpu(entry->e_value_size) > end))
211+
return -EIO;
212+
entry = EXT4_XATTR_NEXT(entry);
213+
}
214+
201215
return 0;
202216
}
203217

@@ -214,7 +228,8 @@ ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
214228
return -EIO;
215229
if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
216230
return -EIO;
217-
error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
231+
error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
232+
bh->b_data);
218233
if (!error)
219234
set_buffer_verified(bh);
220235
return error;
@@ -331,7 +346,7 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
331346
header = IHDR(inode, raw_inode);
332347
entry = IFIRST(header);
333348
end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
334-
error = ext4_xattr_check_names(entry, end);
349+
error = ext4_xattr_check_names(entry, end, entry);
335350
if (error)
336351
goto cleanup;
337352
error = ext4_xattr_find_entry(&entry, name_index, name,
@@ -463,7 +478,7 @@ ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
463478
raw_inode = ext4_raw_inode(&iloc);
464479
header = IHDR(inode, raw_inode);
465480
end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
466-
error = ext4_xattr_check_names(IFIRST(header), end);
481+
error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
467482
if (error)
468483
goto cleanup;
469484
error = ext4_xattr_list_entries(dentry, IFIRST(header),
@@ -980,7 +995,8 @@ int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
980995
is->s.here = is->s.first;
981996
is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
982997
if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
983-
error = ext4_xattr_check_names(IFIRST(header), is->s.end);
998+
error = ext4_xattr_check_names(IFIRST(header), is->s.end,
999+
IFIRST(header));
9841000
if (error)
9851001
return error;
9861002
/* Find the named attribute. */

0 commit comments

Comments
 (0)