Skip to content

Commit 4e986ab

Browse files
adam900710gregkh
authored andcommitted
btrfs: inode: Verify inode mode to avoid NULL pointer dereference
[ Upstream commit 6bf9e4b ] [BUG] When accessing a file on a crafted image, btrfs can crash in block layer: BUG: unable to handle kernel NULL pointer dereference at 0000000000000008 PGD 136501067 P4D 136501067 PUD 124519067 PMD 0 CPU: 3 PID: 0 Comm: swapper/3 Not tainted 5.0.0-rc8-default raspberrypi#252 RIP: 0010:end_bio_extent_readpage+0x144/0x700 Call Trace: <IRQ> blk_update_request+0x8f/0x350 blk_mq_end_request+0x1a/0x120 blk_done_softirq+0x99/0xc0 __do_softirq+0xc7/0x467 irq_exit+0xd1/0xe0 call_function_single_interrupt+0xf/0x20 </IRQ> RIP: 0010:default_idle+0x1e/0x170 [CAUSE] The crafted image has a tricky corruption, the INODE_ITEM has a different type against its parent dir: item 20 key (268 INODE_ITEM 0) itemoff 2808 itemsize 160 generation 13 transid 13 size 1048576 nbytes 1048576 block group 0 mode 121644 links 1 uid 0 gid 0 rdev 0 sequence 9 flags 0x0(none) This mode number 0120000 means it's a symlink. But the dir item think it's still a regular file: item 8 key (264 DIR_INDEX 5) itemoff 3707 itemsize 32 location key (268 INODE_ITEM 0) type FILE transid 13 data_len 0 name_len 2 name: f4 item 40 key (264 DIR_ITEM 51821248) itemoff 1573 itemsize 32 location key (268 INODE_ITEM 0) type FILE transid 13 data_len 0 name_len 2 name: f4 For symlink, we don't set BTRFS_I(inode)->io_tree.ops and leave it empty, as symlink is only designed to have inlined extent, all handled by tree block read. Thus no need to trigger btrfs_submit_bio_hook() for inline file extent. However end_bio_extent_readpage() expects tree->ops populated, as it's reading regular data extent. This causes NULL pointer dereference. [FIX] This patch fixes the problem in two ways: - Verify inode mode against its dir item when looking up inode So in btrfs_lookup_dentry() if we find inode mode mismatch with dir item, we error out so that corrupted inode will not be accessed. - Verify inode mode when getting extent mapping Only regular file should have regular or preallocated extent. If we found regular/preallocated file extent for symlink or the rest, we error out before submitting the read bio. With this fix that crafted image can be rejected gracefully: BTRFS critical (device loop0): inode mode mismatch with dir: inode mode=0121644 btrfs type=7 dir type=1 Reported-by: Yoon Jungyeon <[email protected]> Link: https://bugzilla.kernel.org/show_bug.cgi?id=202763 Reviewed-by: Nikolay Borisov <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 60e1b41 commit 4e986ab

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

fs/btrfs/inode.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5553,12 +5553,14 @@ void btrfs_evict_inode(struct inode *inode)
55535553
}
55545554

55555555
/*
5556-
* this returns the key found in the dir entry in the location pointer.
5556+
* Return the key found in the dir entry in the location pointer, fill @type
5557+
* with BTRFS_FT_*, and return 0.
5558+
*
55575559
* If no dir entries were found, returns -ENOENT.
55585560
* If found a corrupted location in dir entry, returns -EUCLEAN.
55595561
*/
55605562
static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5561-
struct btrfs_key *location)
5563+
struct btrfs_key *location, u8 *type)
55625564
{
55635565
const char *name = dentry->d_name.name;
55645566
int namelen = dentry->d_name.len;
@@ -5591,6 +5593,8 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
55915593
__func__, name, btrfs_ino(BTRFS_I(dir)),
55925594
location->objectid, location->type, location->offset);
55935595
}
5596+
if (!ret)
5597+
*type = btrfs_dir_type(path->nodes[0], di);
55945598
out:
55955599
btrfs_free_path(path);
55965600
return ret;
@@ -5826,25 +5830,43 @@ static struct inode *new_simple_dir(struct super_block *s,
58265830
return inode;
58275831
}
58285832

5833+
static inline u8 btrfs_inode_type(struct inode *inode)
5834+
{
5835+
return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5836+
}
5837+
58295838
struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
58305839
{
58315840
struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
58325841
struct inode *inode;
58335842
struct btrfs_root *root = BTRFS_I(dir)->root;
58345843
struct btrfs_root *sub_root = root;
58355844
struct btrfs_key location;
5845+
u8 di_type = 0;
58365846
int index;
58375847
int ret = 0;
58385848

58395849
if (dentry->d_name.len > BTRFS_NAME_LEN)
58405850
return ERR_PTR(-ENAMETOOLONG);
58415851

5842-
ret = btrfs_inode_by_name(dir, dentry, &location);
5852+
ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
58435853
if (ret < 0)
58445854
return ERR_PTR(ret);
58455855

58465856
if (location.type == BTRFS_INODE_ITEM_KEY) {
58475857
inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5858+
if (IS_ERR(inode))
5859+
return inode;
5860+
5861+
/* Do extra check against inode mode with di_type */
5862+
if (btrfs_inode_type(inode) != di_type) {
5863+
btrfs_crit(fs_info,
5864+
"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5865+
inode->i_mode, btrfs_inode_type(inode),
5866+
di_type);
5867+
iput(inode);
5868+
return ERR_PTR(-EUCLEAN);
5869+
}
58485870
return inode;
58495871
}
58505872

@@ -6455,11 +6477,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
64556477
return ERR_PTR(ret);
64566478
}
64576479

6458-
static inline u8 btrfs_inode_type(struct inode *inode)
6459-
{
6460-
return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6461-
}
6462-
64636480
/*
64646481
* utility function to add 'inode' into 'parent_inode' with
64656482
* a give name and a given sequence number.
@@ -6993,6 +7010,14 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
69937010
extent_start = found_key.offset;
69947011
if (found_type == BTRFS_FILE_EXTENT_REG ||
69957012
found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7013+
/* Only regular file could have regular/prealloc extent */
7014+
if (!S_ISREG(inode->vfs_inode.i_mode)) {
7015+
ret = -EUCLEAN;
7016+
btrfs_crit(fs_info,
7017+
"regular/prealloc extent found for non-regular inode %llu",
7018+
btrfs_ino(inode));
7019+
goto out;
7020+
}
69967021
extent_end = extent_start +
69977022
btrfs_file_extent_num_bytes(leaf, item);
69987023

fs/btrfs/tests/inode-tests.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
232232
return ret;
233233
}
234234

235+
inode->i_mode = S_IFREG;
235236
BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
236237
BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
237238
BTRFS_I(inode)->location.offset = 0;

0 commit comments

Comments
 (0)