Commit

c01b091b06412d3b1b53d8c2925f6de586c8a2fc

ntfs3: free the sbi after a grace period

If the "nocase" mount option ntfs3 uses custom hash and dentry compare
functions. Both access ntfs3 superblock private information in rcu
pathwalk. ntfs_put_super() and ntfs3_kill_sb() drop the info without any
rcu delay. A lookup that's still inside the filesystem when the last
reference to the mount is dropped causes a UAF. With a pathwalk held up
in ntfs_d_hash() while the last reference to the mount goes away:

  BUG: KASAN: slab-use-after-free in ntfs_nls_to_utf16+0x752/0x940
  Read of size 8 at addr ff1100010453c850 by task walker/99
  Call Trace:
   <TASK>
   ntfs_nls_to_utf16+0x752/0x940
   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_cache_noprof+0x249/0x560
   ntfs_init_fs_context+0x2db/0x5a0
   alloc_fs_context+0x52e/0xc70
  [...]
  Freed by task 98:
   kfree+0x1db/0x5b0
   deactivate_locked_super+0x9c/0xf0
   cleanup_mnt+0x9f/0x130
   task_work_run+0x131/0x250

The bug is currently masked by the grace period in namespace_unlock()
for umount(2). Keep the necessary information until the filesystem
private info is freed and free it with an rcu delay.

ntfs3_free_sbi() now runs from an rcu callback and drops the upcase
table via ntfs_put_shared(). So switch s_shared_lock over to
spin_lock_bh(). Otherwise the callback can deadlock against a task
holding the lock on the same cpu.

Fixes: a3a956c78efa ("fs/ntfs3: Add option "nocase"")
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261009-work-fixes-rcu-v1-7-7446d2d335e6@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Christian Brauner <brauner@kernel.org> · 2026-10-09 21:51 UTC

Browse files
2 files changed, +22 −12
modified fs/ntfs3/ntfs_fs.h
@@ -211,6 +211,7 @@ struct ntfs_index {
211 211 /* Ntfs file system in-core superblock data. */
212 212 struct ntfs_sb_info {
213 213 struct super_block *sb;
214 + struct rcu_head rcu;
214 215
215 216 u32 discard_granularity;
216 217 u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1)
modified fs/ntfs3/super.c
@@ -196,7 +196,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes)
196 196 void *ret = NULL;
197 197 int i, j = -1;
198 198
199 - spin_lock(&s_shared_lock);
199 + spin_lock_bh(&s_shared_lock);
200 200 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
201 201 if (!s_shared[i].cnt) {
202 202 j = i;
@@ -214,7 +214,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes)
214 214 s_shared[j].cnt = 1;
215 215 ret = ptr;
216 216 }
217 - spin_unlock(&s_shared_lock);
217 + spin_unlock_bh(&s_shared_lock);
218 218
219 219 return ret;
220 220 }
@@ -231,7 +231,7 @@ void *ntfs_put_shared(void *ptr)
231 231 void *ret = ptr;
232 232 int i;
233 233
234 - spin_lock(&s_shared_lock);
234 + spin_lock_bh(&s_shared_lock);
235 235 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
236 236 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
237 237 if (--s_shared[i].cnt)
@@ -239,7 +239,7 @@ void *ntfs_put_shared(void *ptr)
239 239 break;
240 240 }
241 241 }
242 - spin_unlock(&s_shared_lock);
242 + spin_unlock_bh(&s_shared_lock);
243 243
244 244 return ret;
245 245 }
@@ -708,6 +708,8 @@ static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
708 708
709 709 static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
710 710 {
711 + if (sbi->options)
712 + put_mount_options(sbi->options);
711 713 kfree(sbi->new_rec);
712 714 kvfree(ntfs_put_shared(sbi->upcase));
713 715 kvfree(sbi->def_table);
@@ -719,6 +721,11 @@ static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
719 721 kfree(sbi);
720 722 }
721 723
724 + static void ntfs3_free_sbi_rcu(struct rcu_head *head)
725 + {
726 + ntfs3_free_sbi(container_of(head, struct ntfs_sb_info, rcu));
727 + }
728 +
722 729 static void ntfs_put_super(struct super_block *sb)
723 730 {
724 731 struct ntfs_sb_info *sbi = sb->s_fs_info;
@@ -728,11 +735,11 @@ static void ntfs_put_super(struct super_block *sb)
728 735 /* Mark rw ntfs as clear, if possible. */
729 736 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
730 737
731 - if (sbi->options) {
732 - put_mount_options(sbi->options);
733 - sbi->options = NULL;
734 - }
735 -
738 + /*
739 + * The mount options stay until the sbi is freed: ->d_hash() and
740 + * ->d_compare() of a pathwalk in rcu mode read the nls table through
741 + * them.
742 + */
736 743 ntfs3_put_sbi(sbi);
737 744 }
738 745
@@ -1939,9 +1946,11 @@ static void ntfs3_kill_sb(struct super_block *sb)
1939 1946
1940 1947 kill_block_super(sb);
1941 1948
1942 - if (sbi->options)
1943 - put_mount_options(sbi->options);
1944 - ntfs3_free_sbi(sbi);
1949 + /*
1950 + * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare()
1951 + * and read the upcase table and the nls table through the sbi.
1952 + */
1953 + call_rcu(&sbi->rcu, ntfs3_free_sbi_rcu);
1945 1954 }
1946 1955
1947 1956 // clang-format off