Skip to content

Commit 26a836c

Browse files
Su Yuekdave
authored andcommitted
btrfs: Check name_len on add_inode_ref call path
replay_one_buffer first reads buffers and dispatches items accroding to the item type. In this patch, add_inode_ref handles inode_ref and inode_extref. Then add_inode_ref calls ref_get_fields and extref_get_fields to read ref/extref name for the first time. So checking name_len before reading those two is fine. add_inode_ref also calls inode_in_dir to match ref/extref in parent_dir. The call graph includes btrfs_match_dir_item_name to read dir_item name in the parent dir. Checking first dir_item is not enough. Change it to verify every dir_item while doing matches. Signed-off-by: Su Yue <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent e79a332 commit 26a836c

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

fs/btrfs/dir-item.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,6 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
395395

396396
leaf = path->nodes[0];
397397
dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
398-
if (verify_dir_item(fs_info, leaf, path->slots[0], dir_item))
399-
return NULL;
400398

401399
total_len = btrfs_item_size_nr(leaf, path->slots[0]);
402400
while (cur < total_len) {
@@ -405,6 +403,8 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
405403
btrfs_dir_data_len(leaf, dir_item);
406404
name_ptr = (unsigned long)(dir_item + 1);
407405

406+
if (verify_dir_item(fs_info, leaf, path->slots[0], dir_item))
407+
return NULL;
408408
if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
409409
memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
410410
return dir_item;

fs/btrfs/tree-log.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,15 +1175,19 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
11751175
return 0;
11761176
}
11771177

1178-
static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1179-
u32 *namelen, char **name, u64 *index,
1180-
u64 *parent_objectid)
1178+
static int extref_get_fields(struct extent_buffer *eb, int slot,
1179+
unsigned long ref_ptr, u32 *namelen, char **name,
1180+
u64 *index, u64 *parent_objectid)
11811181
{
11821182
struct btrfs_inode_extref *extref;
11831183

11841184
extref = (struct btrfs_inode_extref *)ref_ptr;
11851185

11861186
*namelen = btrfs_inode_extref_name_len(eb, extref);
1187+
if (!btrfs_is_name_len_valid(eb, slot, (unsigned long)&extref->name,
1188+
*namelen))
1189+
return -EIO;
1190+
11871191
*name = kmalloc(*namelen, GFP_NOFS);
11881192
if (*name == NULL)
11891193
return -ENOMEM;
@@ -1198,14 +1202,19 @@ static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
11981202
return 0;
11991203
}
12001204

1201-
static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1202-
u32 *namelen, char **name, u64 *index)
1205+
static int ref_get_fields(struct extent_buffer *eb, int slot,
1206+
unsigned long ref_ptr, u32 *namelen, char **name,
1207+
u64 *index)
12031208
{
12041209
struct btrfs_inode_ref *ref;
12051210

12061211
ref = (struct btrfs_inode_ref *)ref_ptr;
12071212

12081213
*namelen = btrfs_inode_ref_name_len(eb, ref);
1214+
if (!btrfs_is_name_len_valid(eb, slot, (unsigned long)(ref + 1),
1215+
*namelen))
1216+
return -EIO;
1217+
12091218
*name = kmalloc(*namelen, GFP_NOFS);
12101219
if (*name == NULL)
12111220
return -ENOMEM;
@@ -1280,8 +1289,8 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
12801289

12811290
while (ref_ptr < ref_end) {
12821291
if (log_ref_ver) {
1283-
ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1284-
&ref_index, &parent_objectid);
1292+
ret = extref_get_fields(eb, slot, ref_ptr, &namelen,
1293+
&name, &ref_index, &parent_objectid);
12851294
/*
12861295
* parent object can change from one array
12871296
* item to another.
@@ -1293,8 +1302,8 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
12931302
goto out;
12941303
}
12951304
} else {
1296-
ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1297-
&ref_index);
1305+
ret = ref_get_fields(eb, slot, ref_ptr, &namelen,
1306+
&name, &ref_index);
12981307
}
12991308
if (ret)
13001309
goto out;

0 commit comments

Comments
 (0)