Skip to content

Commit 5bd9d99

Browse files
khoroshilovChristoph Hellwig
authored andcommitted
hfsplus: add error checking for hfs_find_init()
hfs_find_init() may fail with ENOMEM, but there are places, where the returned value is not checked. The consequences can be very unpleasant, e.g. kfree uninitialized pointer and inappropriate mutex unlocking. The patch adds checks for errors in hfs_find_init(). Found by Linux Driver Verification project (linuxtesting.org). Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent c6d5f5f commit 5bd9d99

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎fs/hfsplus/catalog.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir,
212212

213213
dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n",
214214
str->name, cnid, inode->i_nlink);
215-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
215+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
216+
if (err)
217+
return err;
216218

217219
hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
218220
entry_size = hfsplus_fill_cat_thread(sb, &entry,
@@ -269,7 +271,9 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
269271

270272
dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n",
271273
str ? str->name : NULL, cnid);
272-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
274+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
275+
if (err)
276+
return err;
273277

274278
if (!str) {
275279
int len;
@@ -347,12 +351,14 @@ int hfsplus_rename_cat(u32 cnid,
347351
struct hfs_find_data src_fd, dst_fd;
348352
hfsplus_cat_entry entry;
349353
int entry_size, type;
350-
int err = 0;
354+
int err;
351355

352356
dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
353357
cnid, src_dir->i_ino, src_name->name,
354358
dst_dir->i_ino, dst_name->name);
355-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
359+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
360+
if (err)
361+
return err;
356362
dst_fd = src_fd;
357363

358364
/* find the old dir entry and read the data */

‎fs/hfsplus/dir.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
3838
sb = dir->i_sb;
3939

4040
dentry->d_fsdata = NULL;
41-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
41+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
42+
if (err)
43+
return ERR_PTR(err);
4244
hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
4345
again:
4446
err = hfs_brec_read(&fd, &entry, sizeof(entry));
@@ -132,7 +134,9 @@ static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
132134
if (filp->f_pos >= inode->i_size)
133135
return 0;
134136

135-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
137+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
138+
if (err)
139+
return err;
136140
hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
137141
err = hfs_brec_find(&fd);
138142
if (err)

‎fs/hfsplus/extents.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ static void hfsplus_ext_write_extent_locked(struct inode *inode)
124124
if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
125125
struct hfs_find_data fd;
126126

127-
hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
128-
__hfsplus_ext_write_extent(inode, &fd);
129-
hfs_find_exit(&fd);
127+
if (!hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd)) {
128+
__hfsplus_ext_write_extent(inode, &fd);
129+
hfs_find_exit(&fd);
130+
}
130131
}
131132
}
132133

@@ -194,9 +195,11 @@ static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
194195
block < hip->cached_start + hip->cached_blocks)
195196
return 0;
196197

197-
hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
198-
res = __hfsplus_ext_cache_extent(&fd, inode, block);
199-
hfs_find_exit(&fd);
198+
res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
199+
if (!res) {
200+
res = __hfsplus_ext_cache_extent(&fd, inode, block);
201+
hfs_find_exit(&fd);
202+
}
200203
return res;
201204
}
202205

@@ -374,7 +377,9 @@ int hfsplus_free_fork(struct super_block *sb, u32 cnid,
374377
if (total_blocks == blocks)
375378
return 0;
376379

377-
hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
380+
res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
381+
if (res)
382+
return res;
378383
do {
379384
res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
380385
total_blocks, type);
@@ -503,7 +508,6 @@ void hfsplus_file_truncate(struct inode *inode)
503508
struct page *page;
504509
void *fsdata;
505510
u32 size = inode->i_size;
506-
int res;
507511

508512
res = pagecache_write_begin(NULL, mapping, size, 0,
509513
AOP_FLAG_UNINTERRUPTIBLE,
@@ -526,7 +530,12 @@ void hfsplus_file_truncate(struct inode *inode)
526530
goto out;
527531

528532
mutex_lock(&hip->extents_lock);
529-
hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
533+
res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
534+
if (res) {
535+
mutex_unlock(&hip->extents_lock);
536+
/* XXX: We lack error handling of hfsplus_file_truncate() */
537+
return;
538+
}
530539
while (1) {
531540
if (alloc_cnt == hip->first_blocks) {
532541
hfsplus_free_extents(sb, hip->first_extents,

‎fs/hfsplus/inode.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,13 @@ static struct dentry *hfsplus_file_lookup(struct inode *dir,
195195
hip->flags = 0;
196196
set_bit(HFSPLUS_I_RSRC, &hip->flags);
197197

198-
hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
199-
err = hfsplus_find_cat(sb, dir->i_ino, &fd);
200-
if (!err)
201-
err = hfsplus_cat_read_inode(inode, &fd);
202-
hfs_find_exit(&fd);
198+
err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
199+
if (!err) {
200+
err = hfsplus_find_cat(sb, dir->i_ino, &fd);
201+
if (!err)
202+
err = hfsplus_cat_read_inode(inode, &fd);
203+
hfs_find_exit(&fd);
204+
}
203205
if (err) {
204206
iput(inode);
205207
return ERR_PTR(err);

‎fs/hfsplus/super.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
7373

7474
if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
7575
inode->i_ino == HFSPLUS_ROOT_CNID) {
76-
hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
77-
err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
78-
if (!err)
79-
err = hfsplus_cat_read_inode(inode, &fd);
80-
hfs_find_exit(&fd);
76+
err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
77+
if (!err) {
78+
err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
79+
if (!err)
80+
err = hfsplus_cat_read_inode(inode, &fd);
81+
hfs_find_exit(&fd);
82+
}
8183
} else {
8284
err = hfsplus_system_read_inode(inode);
8385
}
@@ -456,7 +458,9 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
456458

457459
str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
458460
str.name = HFSP_HIDDENDIR_NAME;
459-
hfs_find_init(sbi->cat_tree, &fd);
461+
err = hfs_find_init(sbi->cat_tree, &fd);
462+
if (err)
463+
goto out_put_root;
460464
hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
461465
if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
462466
hfs_find_exit(&fd);

0 commit comments

Comments
 (0)