Skip to content

Commit 02f3127

Browse files
anmuxixixiaalexandrovich
authored andcommitted
ntfs3: fix use-after-free of sbi->options in cmp_fnames
The root cause is that sbi->options points directly to fc->fs_private. If fc->fs_private is freed while sbi still exists, sbi->options becomes a dangling pointer. This patch ensures that sbi->options is a separate copy of fc->fs_private and duplicates nls_name if present. On superblock release or error, sbi->options->nls_name and sbi->options are freed and sbi->options is set to NULL to avoid any dangling pointer. Reported-by: syzbot+d77c546c60db651a389c@syzkaller.appspotmail.com Signed-off-by: YangWen <anmuxixixi@gmail.com> [almaz.alexandrovich@paragon-software.com: remove syzbot logs from description] Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
1 parent 68f6bd1 commit 02f3127

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

fs/ntfs3/super.c

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,14 @@ static void ntfs_put_super(struct super_block *sb)
702702

703703
/* Mark rw ntfs as clear, if possible. */
704704
ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
705+
706+
if (sbi->options) {
707+
unload_nls(sbi->options->nls);
708+
kfree(sbi->options->nls);
709+
kfree(sbi->options);
710+
sbi->options = NULL;
711+
}
712+
705713
ntfs3_put_sbi(sbi);
706714
}
707715

@@ -1203,7 +1211,8 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
12031211
int err;
12041212
struct ntfs_sb_info *sbi = sb->s_fs_info;
12051213
struct block_device *bdev = sb->s_bdev;
1206-
struct ntfs_mount_options *options;
1214+
struct ntfs_mount_options *fc_opts;
1215+
struct ntfs_mount_options *options = NULL;
12071216
struct inode *inode;
12081217
struct ntfs_inode *ni;
12091218
size_t i, tt, bad_len, bad_frags;
@@ -1220,20 +1229,35 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
12201229
ref.high = 0;
12211230

12221231
sbi->sb = sb;
1223-
sbi->options = options = fc->fs_private;
1224-
fc->fs_private = NULL;
1232+
fc_opts = fc->fs_private;
1233+
if (!fc_opts) {
1234+
errorf(fc, "missing mount options");
1235+
return -EINVAL;
1236+
}
1237+
options = kmemdup(fc_opts, sizeof(*fc_opts), GFP_KERNEL);
1238+
if (!options)
1239+
return -ENOMEM;
1240+
1241+
if (fc_opts->nls_name) {
1242+
options->nls_name = kstrdup(fc_opts->nls_name, GFP_KERNEL);
1243+
if (!options->nls_name) {
1244+
kfree(options);
1245+
return -ENOMEM;
1246+
}
1247+
}
1248+
sbi->options = options;
12251249
sb->s_flags |= SB_NODIRATIME;
12261250
sb->s_magic = 0x7366746e; // "ntfs"
12271251
sb->s_op = &ntfs_sops;
12281252
sb->s_export_op = &ntfs_export_ops;
12291253
sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
12301254
sb->s_xattr = ntfs_xattr_handlers;
1231-
set_default_d_op(sb, options->nocase ? &ntfs_dentry_ops : NULL);
1255+
set_default_d_op(sb, sbi->options->nocase ? &ntfs_dentry_ops : NULL);
12321256

1233-
options->nls = ntfs_load_nls(options->nls_name);
1234-
if (IS_ERR(options->nls)) {
1235-
options->nls = NULL;
1236-
errorf(fc, "Cannot load nls %s", options->nls_name);
1257+
sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
1258+
if (IS_ERR(sbi->options->nls)) {
1259+
sbi->options->nls = NULL;
1260+
errorf(fc, "Cannot load nls %s", fc_opts->nls_name);
12371261
err = -EINVAL;
12381262
goto out;
12391263
}
@@ -1645,6 +1669,13 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
16451669
put_inode_out:
16461670
iput(inode);
16471671
out:
1672+
if (sbi && sbi->options) {
1673+
unload_nls(sbi->options->nls);
1674+
kfree(sbi->options->nls);
1675+
kfree(sbi->options);
1676+
sbi->options = NULL;
1677+
}
1678+
16481679
ntfs3_put_sbi(sbi);
16491680
kfree(boot2);
16501681
return err;

0 commit comments

Comments
 (0)