Skip to content

Commit 2a29edc

Browse files
liubchrismason-xx
authored andcommitted
btrfs: fix several uncheck memory allocations
To make btrfs more stable, add several missing necessary memory allocation checks, and when no memory, return proper errno. We've checked that some of those -ENOMEM errors will be returned to userspace, and some will be catched by BUG_ON() in the upper callers, and none will be ignored silently. Signed-off-by: Liu Bo <liubo2009@cn.fujitsu.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 6b82ce8 commit 2a29edc

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎fs/btrfs/export.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ static struct dentry *btrfs_get_parent(struct dentry *child)
176176
int ret;
177177

178178
path = btrfs_alloc_path();
179+
if (!path)
180+
return ERR_PTR(-ENOMEM);
179181

180182
if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
181183
key.objectid = root->root_key.objectid;

‎fs/btrfs/file-item.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
536536
root = root->fs_info->csum_root;
537537

538538
path = btrfs_alloc_path();
539+
if (!path)
540+
return -ENOMEM;
539541

540542
while (1) {
541543
key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;

‎fs/btrfs/file.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
945945
PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
946946
(sizeof(struct page *)));
947947
pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
948+
if (!pages) {
949+
ret = -ENOMEM;
950+
goto out;
951+
}
948952

949953
/* generic_write_checks can change our pos */
950954
start_pos = pos;

‎fs/btrfs/tree-log.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,
338338
}
339339
dst_copy = kmalloc(item_size, GFP_NOFS);
340340
src_copy = kmalloc(item_size, GFP_NOFS);
341+
if (!dst_copy || !src_copy) {
342+
btrfs_release_path(root, path);
343+
kfree(dst_copy);
344+
kfree(src_copy);
345+
return -ENOMEM;
346+
}
341347

342348
read_extent_buffer(eb, src_copy, src_ptr, item_size);
343349

@@ -665,6 +671,9 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
665671
btrfs_dir_item_key_to_cpu(leaf, di, &location);
666672
name_len = btrfs_dir_name_len(leaf, di);
667673
name = kmalloc(name_len, GFP_NOFS);
674+
if (!name)
675+
return -ENOMEM;
676+
668677
read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
669678
btrfs_release_path(root, path);
670679

@@ -744,6 +753,9 @@ static noinline int backref_in_log(struct btrfs_root *log,
744753
int match = 0;
745754

746755
path = btrfs_alloc_path();
756+
if (!path)
757+
return -ENOMEM;
758+
747759
ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
748760
if (ret != 0)
749761
goto out;
@@ -967,6 +979,8 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
967979
key.offset = (u64)-1;
968980

969981
path = btrfs_alloc_path();
982+
if (!path)
983+
return -ENOMEM;
970984

971985
while (1) {
972986
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -1178,6 +1192,9 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans,
11781192

11791193
name_len = btrfs_dir_name_len(eb, di);
11801194
name = kmalloc(name_len, GFP_NOFS);
1195+
if (!name)
1196+
return -ENOMEM;
1197+
11811198
log_type = btrfs_dir_type(eb, di);
11821199
read_extent_buffer(eb, name, (unsigned long)(di + 1),
11831200
name_len);
@@ -1692,6 +1709,8 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
16921709
root_owner = btrfs_header_owner(parent);
16931710

16941711
next = btrfs_find_create_tree_block(root, bytenr, blocksize);
1712+
if (!next)
1713+
return -ENOMEM;
16951714

16961715
if (*level == 1) {
16971716
wc->process_func(root, next, wc, ptr_gen);
@@ -2194,6 +2213,9 @@ int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
21942213

21952214
log = root->log_root;
21962215
path = btrfs_alloc_path();
2216+
if (!path)
2217+
return -ENOMEM;
2218+
21972219
di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
21982220
name, name_len, -1);
21992221
if (IS_ERR(di)) {
@@ -2594,6 +2616,9 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
25942616

25952617
ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
25962618
nr * sizeof(u32), GFP_NOFS);
2619+
if (!ins_data)
2620+
return -ENOMEM;
2621+
25972622
ins_sizes = (u32 *)ins_data;
25982623
ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
25992624

0 commit comments

Comments
 (0)