Skip to content

Commit c5b8d0b

Browse files
author
Christoph Hellwig
committed
hfsplus: fix failed mount handling
Currently the error handling in hfsplus_fill_super is a mess, and can lead to accessing fields in the superblock that haven't been even set up yet. Fix this by making sure we do not set up sb->s_root until we have the mount fully set up, and before that do proper step by step unwinding instead of using hfsplus_put_super as a big hammer. Reported-by: Dan Williams <dcbw@redhat.com> Signed-off-by: Christoph Hellwig <hch@tuxera.com>
1 parent ebf5382 commit c5b8d0b

1 file changed

Lines changed: 61 additions & 45 deletions

File tree

‎fs/hfsplus/super.c‎

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -338,37 +338,37 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
338338
struct inode *root, *inode;
339339
struct qstr str;
340340
struct nls_table *nls = NULL;
341-
int err = -EINVAL;
341+
int err;
342342

343+
err = -EINVAL;
343344
sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
344345
if (!sbi)
345-
return -ENOMEM;
346+
goto out;
346347

347348
sb->s_fs_info = sbi;
348349
mutex_init(&sbi->alloc_mutex);
349350
mutex_init(&sbi->vh_mutex);
350351
hfsplus_fill_defaults(sbi);
352+
353+
err = -EINVAL;
351354
if (!hfsplus_parse_options(data, sbi)) {
352355
printk(KERN_ERR "hfs: unable to parse mount options\n");
353-
err = -EINVAL;
354-
goto cleanup;
356+
goto out_unload_nls;
355357
}
356358

357359
/* temporarily use utf8 to correctly find the hidden dir below */
358360
nls = sbi->nls;
359361
sbi->nls = load_nls("utf8");
360362
if (!sbi->nls) {
361363
printk(KERN_ERR "hfs: unable to load nls for utf8\n");
362-
err = -EINVAL;
363-
goto cleanup;
364+
goto out_unload_nls;
364365
}
365366

366367
/* Grab the volume header */
367368
if (hfsplus_read_wrapper(sb)) {
368369
if (!silent)
369370
printk(KERN_WARNING "hfs: unable to find HFS+ superblock\n");
370-
err = -EINVAL;
371-
goto cleanup;
371+
goto out_unload_nls;
372372
}
373373
vhdr = sbi->s_vhdr;
374374

@@ -377,7 +377,7 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
377377
if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
378378
be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
379379
printk(KERN_ERR "hfs: wrong filesystem version\n");
380-
goto cleanup;
380+
goto out_free_vhdr;
381381
}
382382
sbi->total_blocks = be32_to_cpu(vhdr->total_blocks);
383383
sbi->free_blocks = be32_to_cpu(vhdr->free_blocks);
@@ -421,19 +421,19 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
421421
sbi->ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
422422
if (!sbi->ext_tree) {
423423
printk(KERN_ERR "hfs: failed to load extents file\n");
424-
goto cleanup;
424+
goto out_free_vhdr;
425425
}
426426
sbi->cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
427427
if (!sbi->cat_tree) {
428428
printk(KERN_ERR "hfs: failed to load catalog file\n");
429-
goto cleanup;
429+
goto out_close_ext_tree;
430430
}
431431

432432
inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
433433
if (IS_ERR(inode)) {
434434
printk(KERN_ERR "hfs: failed to load allocation file\n");
435435
err = PTR_ERR(inode);
436-
goto cleanup;
436+
goto out_close_cat_tree;
437437
}
438438
sbi->alloc_file = inode;
439439

@@ -442,14 +442,7 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
442442
if (IS_ERR(root)) {
443443
printk(KERN_ERR "hfs: failed to load root directory\n");
444444
err = PTR_ERR(root);
445-
goto cleanup;
446-
}
447-
sb->s_d_op = &hfsplus_dentry_operations;
448-
sb->s_root = d_alloc_root(root);
449-
if (!sb->s_root) {
450-
iput(root);
451-
err = -ENOMEM;
452-
goto cleanup;
445+
goto out_put_alloc_file;
453446
}
454447

455448
str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
@@ -459,46 +452,69 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
459452
if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
460453
hfs_find_exit(&fd);
461454
if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
462-
goto cleanup;
455+
goto out_put_root;
463456
inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
464457
if (IS_ERR(inode)) {
465458
err = PTR_ERR(inode);
466-
goto cleanup;
459+
goto out_put_root;
467460
}
468461
sbi->hidden_dir = inode;
469462
} else
470463
hfs_find_exit(&fd);
471464

472-
if (sb->s_flags & MS_RDONLY)
473-
goto out;
465+
if (!(sb->s_flags & MS_RDONLY)) {
466+
/*
467+
* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
468+
* all three are registered with Apple for our use
469+
*/
470+
vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
471+
vhdr->modify_date = hfsp_now2mt();
472+
be32_add_cpu(&vhdr->write_count, 1);
473+
vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
474+
vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
475+
hfsplus_sync_fs(sb, 1);
474476

475-
/* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
476-
* all three are registered with Apple for our use
477-
*/
478-
vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
479-
vhdr->modify_date = hfsp_now2mt();
480-
be32_add_cpu(&vhdr->write_count, 1);
481-
vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
482-
vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
483-
hfsplus_sync_fs(sb, 1);
484-
485-
if (!sbi->hidden_dir) {
486-
mutex_lock(&sbi->vh_mutex);
487-
sbi->hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
488-
hfsplus_create_cat(sbi->hidden_dir->i_ino, sb->s_root->d_inode,
489-
&str, sbi->hidden_dir);
490-
mutex_unlock(&sbi->vh_mutex);
491-
492-
hfsplus_mark_inode_dirty(sbi->hidden_dir, HFSPLUS_I_CAT_DIRTY);
477+
if (!sbi->hidden_dir) {
478+
mutex_lock(&sbi->vh_mutex);
479+
sbi->hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
480+
hfsplus_create_cat(sbi->hidden_dir->i_ino, root, &str,
481+
sbi->hidden_dir);
482+
mutex_unlock(&sbi->vh_mutex);
483+
484+
hfsplus_mark_inode_dirty(sbi->hidden_dir,
485+
HFSPLUS_I_CAT_DIRTY);
486+
}
493487
}
494-
out:
488+
489+
sb->s_d_op = &hfsplus_dentry_operations;
490+
sb->s_root = d_alloc_root(root);
491+
if (!sb->s_root) {
492+
err = -ENOMEM;
493+
goto out_put_hidden_dir;
494+
}
495+
495496
unload_nls(sbi->nls);
496497
sbi->nls = nls;
497498
return 0;
498499

499-
cleanup:
500-
hfsplus_put_super(sb);
500+
out_put_hidden_dir:
501+
iput(sbi->hidden_dir);
502+
out_put_root:
503+
iput(sbi->alloc_file);
504+
out_put_alloc_file:
505+
iput(sbi->alloc_file);
506+
out_close_cat_tree:
507+
hfs_btree_close(sbi->cat_tree);
508+
out_close_ext_tree:
509+
hfs_btree_close(sbi->ext_tree);
510+
out_free_vhdr:
511+
kfree(sbi->s_vhdr);
512+
kfree(sbi->s_backup_vhdr);
513+
out_unload_nls:
514+
unload_nls(sbi->nls);
501515
unload_nls(nls);
516+
kfree(sbi);
517+
out:
502518
return err;
503519
}
504520

0 commit comments

Comments
 (0)