Commit
56c3824de56b5275c02ff6b6aa27ddbacfbb9b25
ntfs3: free replaced mount options after a grace period
ntfs_fs_reconfigure() replaces the old mount options with new mount
options in sbi->options. The old mount options land in fs_context and
are freed as soon as remount returns. With a pathwalk held up in
ntfs_nls_to_utf16() between loading sbi->options and dereferencing it
while a remount replaces them:
BUG: KASAN: slab-use-after-free in ntfs_nls_to_utf16+0x7ae/0x7c0
Read of size 8 at addr ff110001044fd448 by task walker/98
Call Trace:
<TASK>
ntfs_nls_to_utf16+0x7ae/0x7c0
ntfs_d_hash+0x22b/0x500
link_path_walk+0x11ad/0x1d30
path_lookupat+0x63/0xb00
filename_lookup+0x1f1/0x5b0
vfs_statx+0xd7/0x3b0
[...]
Allocated by task 95:
__kmalloc_node_track_caller_noprof+0x2a2/0x730
kmemdup_noprof+0x25/0x50
ntfs_fill_super+0x1e5/0x4550
[...]
Freed by task 97:
kfree+0x1db/0x5b0
put_fs_context+0x13c/0x7d0
path_mount+0x54a/0x2130
__x64_sys_mount+0x20c/0x270
That info is needed in rcu lookup in the dentry hash and dentry compary
methods for ntfs3 and causes a UAF today. Delay freeing with an rcu
grace period.
ntfs_nls_to_utf16() reads ->nls and ->ads once under rcu_read_lock() so
the hooks pair with the release in ntfs_fs_reconfigure(). That also
covers callers outside of rcu pathwalk such as ntfs_lookup(). They don't
hold rcu_read_lock() themselves and wouldn't be protected by the grace
period otherwise. Remount refuses to change the iocharset so ->nls stays
valid after rcu_read_unlock().
Fixes: a3a956c78efa ("fs/ntfs3: Add option "nocase"")
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261009-work-fixes-rcu-v1-8-7446d2d335e6@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Christian Brauner <brauner@kernel.org> · 2026-10-09 21:51 UTC
3 files changed,
+28
−3
modified fs/ntfs3/dir.c
| @@ -186,11 +186,20 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len, | |||
| 186 | 186 | { | |
| 187 | 187 | int ret, slen, i; | |
| 188 | 188 | const u8 *end; | |
| 189 | - | struct nls_table *nls = sbi->options->nls; | |
| 189 | + | struct ntfs_mount_options *opts; | |
| 190 | + | struct nls_table *nls; | |
| 191 | + | bool ads; | |
| 190 | 192 | u16 *uname = uni->name; | |
| 191 | 193 | ||
| 192 | 194 | static_assert(sizeof(wchar_t) == sizeof(u16)); | |
| 193 | 195 | ||
| 196 | + | /* ntfs_fs_reconfigure() may replace the options, but not ->nls */ | |
| 197 | + | rcu_read_lock(); | |
| 198 | + | opts = READ_ONCE(sbi->options); | |
| 199 | + | nls = opts->nls; | |
| 200 | + | ads = opts->ads; | |
| 201 | + | rcu_read_unlock(); | |
| 202 | + | ||
| 194 | 203 | if (!nls) { | |
| 195 | 204 | /* utf8 -> utf16 */ | |
| 196 | 205 | ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen); | |
| @@ -230,7 +239,7 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len, | |||
| 230 | 239 | ||
| 231 | 240 | uni->len = ret; | |
| 232 | 241 | uni->ads_len = 0; | |
| 233 | - | if (ret > 0 && sbi->options->ads) { | |
| 242 | + | if (ret > 0 && ads) { | |
| 234 | 243 | uname = uni->name; | |
| 235 | 244 | /* Find delimiter in range [1 : ret-2). */ | |
| 236 | 245 | for (i = 1; i + 1 < ret; i++) { | |
modified fs/ntfs3/ntfs_fs.h
| @@ -91,6 +91,8 @@ enum utf16_endian; | |||
| 91 | 91 | struct ntfs_mount_options { | |
| 92 | 92 | char *nls_name; | |
| 93 | 93 | struct nls_table *nls; | |
| 94 | + | /* ->d_hash() and ->d_compare() read the options in rcu pathwalk */ | |
| 95 | + | struct rcu_head rcu; | |
| 94 | 96 | ||
| 95 | 97 | kuid_t fs_uid; | |
| 96 | 98 | kgid_t fs_gid; | |
modified fs/ntfs3/super.c
| @@ -251,6 +251,11 @@ static inline void put_mount_options(struct ntfs_mount_options *options) | |||
| 251 | 251 | kfree(options); | |
| 252 | 252 | } | |
| 253 | 253 | ||
| 254 | + | static void put_mount_options_rcu(struct rcu_head *head) | |
| 255 | + | { | |
| 256 | + | put_mount_options(container_of(head, struct ntfs_mount_options, rcu)); | |
| 257 | + | } | |
| 258 | + | ||
| 254 | 259 | enum Opt { | |
| 255 | 260 | Opt_uid, | |
| 256 | 261 | Opt_gid, | |
| @@ -444,6 +449,7 @@ static int ntfs_fs_reconfigure(struct fs_context *fc) | |||
| 444 | 449 | struct super_block *sb = fc->root->d_sb; | |
| 445 | 450 | struct ntfs_sb_info *sbi = sb->s_fs_info; | |
| 446 | 451 | struct ntfs_mount_options *new_opts = fc->fs_private; | |
| 452 | + | struct ntfs_mount_options *old_opts; | |
| 447 | 453 | int ro_rw; | |
| 448 | 454 | ||
| 449 | 455 | ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); | |
| @@ -473,7 +479,15 @@ static int ntfs_fs_reconfigure(struct fs_context *fc) | |||
| 473 | 479 | } | |
| 474 | 480 | ||
| 475 | 481 | sync_filesystem(sb); | |
| 476 | - | swap(sbi->options, fc->fs_private); | |
| 482 | + | old_opts = sbi->options; | |
| 483 | + | /* pairs with the READ_ONCE() in ntfs_nls_to_utf16() */ | |
| 484 | + | smp_store_release(&sbi->options, new_opts); | |
| 485 | + | fc->fs_private = NULL; | |
| 486 | + | /* | |
| 487 | + | * ntfs_d_hash() and ntfs_d_compare() of a pathwalk in rcu mode may | |
| 488 | + | * still read the old options through the sbi. | |
| 489 | + | */ | |
| 490 | + | call_rcu(&old_opts->rcu, put_mount_options_rcu); | |
| 477 | 491 | ||
| 478 | 492 | return 0; | |
| 479 | 493 | } | |