ext4_quota_on() sets or clears the per-superblock DQUOT_NOLIST_DIRTY flag from s_qf_names[type] before dquot_quota_on() runs, so a Q_QUOTAON that fails afterwards (-EACCES for a directory, -EBUSY for a type that is already loaded) still flips the flag. With usrjquota= mounted and user quota active, root can therefore toggle the flag at will by alternating Q_QUOTAON(USRQUOTA) and Q_QUOTAON(GRPQUOTA). The quota core reads that flag without any locking in dquot_mark_dquot_dirty() and clear_dquot_dirty(). If the flag changes between a dquot's mark and its clear, which are separated by the sleeping ext4_journal_start() in ext4_write_dquot(), the clear takes the bit-only path and leaves the dquot linked on dqi_dirty_list with DQ_MOD_B clear. dquot_writeback_dquots() then spins forever on it, and once the dquot drops to refcount 0 and is moved to free_dquots, dqcache_shrink_scan() frees it while it is still linked. The next list_add() of any other dquot then reads and writes the freed object. Decide the flag only while no quota is loaded on the superblock, the one situation in which no dquot can be dirty or on the list. KASAN report: BUG: KASAN: slab-use-after-free in __list_add_valid_or_report+0x2f/0x110 Read of size 8 at addr ffff88801c1dfdf8 by task poc-q1/343 CPU: 0 UID: 0 PID: 343 Comm: poc-q1 Tainted: G W 7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy) Tainted: [W]=WARN Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: dump_stack_lvl+0x74/0xb0 print_report+0xce/0x5f0 ? __list_add_valid_or_report+0x2f/0x110 ? kasan_complete_mode_report_info+0x80/0x200 ? __list_add_valid_or_report+0x2f/0x110 kasan_report+0xd6/0x110 ? __list_add_valid_or_report+0x2f/0x110 __asan_load8+0x82/0xb0 __list_add_valid_or_report+0x2f/0x110 dquot_mark_dquot_dirty+0x15d/0x1a0 ext4_mark_dquot_dirty+0x77/0xc0 dquot_set_dqblk+0x306/0x830 quota_setquota+0x305/0x390 ? __pfx_quota_setquota+0x10/0x10 ? avc_has_perm_noaudit+0xda/0x290 ? __pfx_avc_has_perm+0x10/0x10 ? selinux_quotactl+0xcf/0x190 do_quotactl+0x750/0xb60 ? __pfx_do_quotactl+0x10/0x10 ? __kasan_slab_free+0x6d/0x80 ? kmem_cache_free+0x22a/0x530 ? putname+0x5b/0x90 ? putname+0x5b/0x90 ? quotactl_block+0x361/0x3f0 ? __pfx_quotactl_block+0x10/0x10 ? mntput_no_expire+0x92/0x130 __x64_sys_quotactl+0x113/0x280 ? __pfx___x64_sys_quotactl+0x10/0x10 ? __pfx_fput_close_sync+0x10/0x10 ? do_raw_spin_unlock+0x9f/0x100 ? rcu_is_watching+0x25/0x60 x64_sys_call+0x183a/0x21d0 do_syscall_64+0x11a/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f1c6a63809a Code: 73 01 c3 48 8b 0d 66 8d 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 49 89 ca b8 b3 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 36 8d 0c 00 f7 d8 64 89 01 48 RSP: 002b:00007ffeff832798 EFLAGS: 00000206 ORIG_RAX: 00000000000000b3 RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 00007f1c6a63809a RDX: 0000000000001388 RSI: 00007ffeff833ebe RDI: 0000000080000800 RBP: 00007ffeff8328c0 R08: 0000000000000007 R09: 00005583316653d0 R10: 00007ffeff8327c0 R11: 0000000000000206 R12: 0000000000000001 R13: 0000000000001388 R14: 0000000000000004 R15: 00007f1c6a52b6c0 Allocated by task 348: kasan_save_stack+0x2c/0x60 kasan_save_track+0x18/0x50 kasan_save_alloc_info+0x3a/0x50 __kasan_slab_alloc+0x62/0x80 kmem_cache_alloc_noprof+0x1ed/0x5d0 dquot_alloc+0x1e/0x30 dqget+0x3db/0x6e0 __dquot_initialize+0x24c/0x6d0 dquot_initialize+0x17/0x20 __ext4_new_inode+0x41d/0x28c0 ext4_create+0x1f5/0x300 lookup_open+0x708/0xba0 path_openat+0x698/0xfb0 do_file_open+0x188/0x2c0 do_sys_openat2+0x110/0x170 __x64_sys_openat+0xea/0x170 x64_sys_call+0x1eac/0x21d0 do_syscall_64+0x11a/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 343: kasan_save_stack+0x2c/0x60 kasan_save_track+0x18/0x50 kasan_save_free_info+0x3e/0x60 __kasan_slab_free+0x5c/0x80 kmem_cache_free+0x22a/0x530 dquot_destroy+0x1c/0x30 dqcache_shrink_scan+0x17a/0x320 shrink_slab+0x3fc/0xa70 drop_slab+0x91/0x130 drop_caches_sysctl_handler+0x8a/0xf0 proc_sys_call_handler+0x27e/0x390 proc_sys_write+0x17/0x20 vfs_write+0x609/0x7f0 ksys_write+0xd9/0x180 __x64_sys_write+0x47/0x60 x64_sys_call+0x16f5/0x21d0 do_syscall_64+0x11a/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f The buggy address belongs to the object at ffff88801c1dfdc0 which belongs to the cache dquot of size 376 The buggy address is located 56 bytes inside of freed 376-byte region [ffff88801c1dfdc0, ffff88801c1dff38) The buggy address belongs to the physical page: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x1c1de head: order:1 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 flags: 0x100000000000040(head|node=0|zone=1) page_type: f5(slab) raw: 0100000000000040 ffff888007f188c0 dead000000000122 0000000000000000 raw: 0000000000000000 0000000000120012 00000000f5000000 0000000000000000 head: 0100000000000040 ffff888007f188c0 dead000000000122 0000000000000000 head: 0000000000000000 0000000000120012 00000000f5000000 0000000000000000 head: 0100000000000001 ffffffffffffff81 00000000ffffffff 00000000ffffffff head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff88801c1dfc80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ffff88801c1dfd00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fc >ffff88801c1dfd80: fc fc fc fc fc fc fc fc fa fb fb fb fb fb fb fb ^ ffff88801c1dfe00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff88801c1dfe80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ================================================================== Fixes: 91389240a252 ("ext4: Disable dirty list tracking of dquots when journalling quotas") Cc: stable@vger.kernel.org Signed-off-by: Xingyuan Mo --- fs/ext4/super.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 880f664d6c04b..22438d9f2bb3b 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -7292,13 +7292,12 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id, ext4_msg(sb, KERN_WARNING, "Quota file not on filesystem root. " "Journaled quota will not work"); - sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY; - } else { - /* - * Clear the flag just in case mount options changed since - * last time. - */ - sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY; + } + if (!sb_any_quota_loaded(sb)) { + if (EXT4_SB(sb)->s_qf_names[type]) + sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY; + else + sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY; } lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA); -- 2.43.0