ntfs: fix default_upcase refcount underflow and UAF on fs_context teardown
Subsystems (2)
PATCH 1/1 ntfs: fix default_upcase refcount underflow and UAF on fs_context teardown
ntfs_init_fs_context() allocates a fresh ntfs_volume with vol->upcase left as NULL. ntfs_free_fs_context() unconditionally calls ntfs_volume_free() during fs_context teardown, even when ntfs_fill_super() never ran or already cleaned up. ntfs_volume_free() then executes:
mutex_lock(&ntfs_lock);
if (vol->upcase == default_upcase) {
ntfs_nr_upcase_users--;
vol->upcase = NULL;
}When the global default_upcase is also NULL (very first mount attempt, or all prior mounts have released the table), the comparison is NULL == NULL, and ntfs_nr_upcase_users is decremented even though this volume never claimed a reference. ntfs_nr_upcase_users is unsigned long, so the decrement wraps to ULONG_MAX.
A subsequent successful mount can then free the shared table while the mounted volume still points at it:
1. ntfs_fill_super() does the temporary ntfs_nr_upcase_users++ at the "Generate the global default upcase table if necessary" block. With the prior wraparound this brings the counter back to 0.
2. If the volume's $UpCase matches the default, the match path does ntfs_nr_upcase_users++ and sets vol->upcase = default_upcase. The counter is now 1.
3. On the success path, !--ntfs_nr_upcase_users evaluates true and default_upcase is kvfree()'d while vol->upcase still points at it. Subsequent upcase comparisons through that mount touch freed memory.
This was reproduced with KASAN by closing a fresh fsopen("ntfs") context, then mounting an NTFS image whose $UpCase table matches generate_default_upcase(), and finally doing a case-insensitive lookup. KASAN reports the dangling vol->upcase access:
BUG: KASAN: use-after-free in ntfs_collate_names+0x3b4/0x420 Read of size 2 at addr ffff888008d40048 by task init/1 ntfs_collate_names+0x3b4/0x420 ntfs_lookup_inode_by_name+0x1921/0x3130 ntfs_lookup+0x193/0xc40 vfs_statx+0xc7/0x190 vfs_fstatat+0x4b/0xa0 __do_sys_newfstatat+0x92/0xf0
The same QEMU reproducer was rerun after this change with KASAN enabled. It reached "reproducer finished", and the log contained no KASAN, use-after-free, Oops, or panic signatures.
Guard each comparison with an explicit vol->upcase non-NULL check so a volume that never took a reference cannot decrement the global users counter. Apply the same guard to the other default_upcase release sites so all cleanup paths follow the same ownership rule: only volumes that actually hold a default_upcase reference may drop one.
--- fs/ntfs/super.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/ntfs/super.c +3 -3
@@ -1671,7 +1671,7 @@ static bool load_system_files(struct ntfs_volume *vol) iput_upcase_err_out: vol->upcase_len =0 ;mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; }
@@ -1701,7 +1701,7 @@ static void ntfs_volume_free(struct ntfs_volume *vol)* the number of upcase users if we are a user. */ mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; }
@@ -2494,7 +2494,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) } vol->upcase_len =0 ;mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; }
Signature · 1 line
2.43.0
2026년 5월 5일 (화) 오후 10:07, DaeMyung Kang <[email protected]>님이 작성:
Quoted 50 lines
ntfs_init_fs_context()allocates a freshntfs_volumewithvol->upcaseleft as NULL.ntfs_free_fs_context()unconditionally callsntfs_volume_free()duringfs_contextteardown, even whenntfs_fill_super()never ran or already cleaned up.ntfs_volume_free()then executes:mutex_lock(&ntfs_lock); if (vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase = NULL; }When the global
default_upcaseis also NULL (very first mount attempt, or all prior mounts have released the table), the comparison is NULL == NULL, andntfs_nr_upcase_usersis decremented even though this volume never claimed a reference.ntfs_nr_upcase_usersis unsigned long, so the decrement wraps toULONG_MAX.A subsequent successful mount can then free the shared table while the mounted volume still points at it:
1.
ntfs_fill_super()does the temporary ntfs_nr_upcase_users++ at the "Generate the global default upcase table if necessary" block. With the prior wraparound this brings the counter back to 0.
2. If the volume's $UpCase matches the default, the match path does ntfs_nr_upcase_users++ and setsvol->upcase=default_upcase. The counter is now 1.
3. On the success path, !--ntfs_nr_upcase_users evaluates true anddefault_upcaseis kvfree()'d whilevol->upcasestill points at it. Subsequent upcase comparisons through that mount touch freed memory.This was reproduced with KASAN by closing a fresh
fsopen("ntfs")context, then mounting an NTFS image whose $UpCase table matchesgenerate_default_upcase(), and finally doing a case-insensitive lookup. KASAN reports the danglingvol->upcaseaccess:BUG: KASAN: use-after-free in ntfs_collate_names+0x3b4/0x420 Read of size 2 at addr ffff888008d40048 by task init/1 ntfs_collate_names+0x3b4/0x420 ntfs_lookup_inode_by_name+0x1921/0x3130 ntfs_lookup+0x193/0xc40 vfs_statx+0xc7/0x190 vfs_fstatat+0x4b/0xa0 __do_sys_newfstatat+0x92/0xf0The same QEMU reproducer was rerun after this change with KASAN enabled. It reached "reproducer finished", and the log contained no KASAN, use-after-free, Oops, or panic signatures.
Quoted 8 lines
Guard each comparison with an explicit
vol->upcasenon-NULL check so a volume that never took a reference cannot decrement the global users counter. Apply the same guard to the otherdefault_upcaserelease sites so all cleanup paths follow the same ownership rule: only volumes that actually hold adefault_upcasereference may drop one.Fixes 1e9ea7e04472 ("Revert "fs: Remove NTFS classic"")Signed-off-by DaeMyung Kang <[email protected]>
Looks good to me.
Quoted 36 lines
--- fs/ntfs/super.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/ntfs/super.c +3
-4@@ -1671,7 +1671,7 @@ static bool load_system_files(struct ntfs_volume *vol) iput_upcase_err_out: vol->upcase_len =0 ;mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; }@@ -1701,7 +1701,7 @@ static void ntfs_volume_free(struct ntfs_volume *vol)* the number of upcase users if we are a user. */ mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; }@@ -2494,7 +2494,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) } vol->upcase_len =0 ;mutex_lock (&ntfs_lock); -if (vol->upcase == default_upcase) { +if (vol->upcase && vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase =NULL ; } --
Quoted 1 line
2.43.0
--
Thanks,
Hyunchul
Quoted 50 lines · DaeMyung Kang
ntfs_init_fs_context()allocates a freshntfs_volumewithvol->upcaseleft as NULL.ntfs_free_fs_context()unconditionally callsntfs_volume_free()duringfs_contextteardown, even whenntfs_fill_super()never ran or already cleaned up.ntfs_volume_free()then executes:mutex_lock(&ntfs_lock); if (vol->upcase == default_upcase) { ntfs_nr_upcase_users--; vol->upcase = NULL; }When the global
default_upcaseis also NULL (very first mount attempt, or all prior mounts have released the table), the comparison is NULL == NULL, andntfs_nr_upcase_usersis decremented even though this volume never claimed a reference.ntfs_nr_upcase_usersis unsigned long, so the decrement wraps toULONG_MAX.A subsequent successful mount can then free the shared table while the mounted volume still points at it:
1.
ntfs_fill_super()does the temporary ntfs_nr_upcase_users++ at the "Generate the global default upcase table if necessary" block. With the prior wraparound this brings the counter back to 0.
2. If the volume's $UpCase matches the default, the match path does ntfs_nr_upcase_users++ and setsvol->upcase=default_upcase. The counter is now 1.
3. On the success path, !--ntfs_nr_upcase_users evaluates true anddefault_upcaseis kvfree()'d whilevol->upcasestill points at it. Subsequent upcase comparisons through that mount touch freed memory.This was reproduced with KASAN by closing a fresh
fsopen("ntfs")context, then mounting an NTFS image whose $UpCase table matchesgenerate_default_upcase(), and finally doing a case-insensitive lookup. KASAN reports the danglingvol->upcaseaccess:BUG: KASAN: use-after-free in ntfs_collate_names+0x3b4/0x420 Read of size 2 at addr ffff888008d40048 by task init/1 ntfs_collate_names+0x3b4/0x420 ntfs_lookup_inode_by_name+0x1921/0x3130 ntfs_lookup+0x193/0xc40 vfs_statx+0xc7/0x190 vfs_fstatat+0x4b/0xa0 __do_sys_newfstatat+0x92/0xf0The same QEMU reproducer was rerun after this change with KASAN enabled. It reached "reproducer finished", and the log contained no KASAN, use-after-free, Oops, or panic signatures.
Quoted 8 lines
Guard each comparison with an explicit
vol->upcasenon-NULL check so a volume that never took a reference cannot decrement the global users counter. Apply the same guard to the otherdefault_upcaserelease sites so all cleanup paths follow the same ownership rule: only volumes that actually hold adefault_upcasereference may drop one.Fixes 1e9ea7e04472 ("Revert "fs: Remove NTFS classic"")Signed-off-by DaeMyung Kang <[email protected]>
Applied it to #ntfs-next.
Thanks!