When audit_dupe_rule() fails inside update_lsm_rule(), the error branch removes the old entry from the filter lists and frees it via call_rcu(), but it only unlinks the entry from its watch. A rule carrying an AUDIT_DIR field is linked into tree->rules through its rlist, and that link is left in place, so the freed audit_entry stays reachable from the audit_tree. The next kill_rules() for that tree (umount of the tagged mount, DELETE_SELF on the tagged directory, or a prune) walks tree->rules through the freed entry: KASAN reports a use-after-free read of rule->tree, and the subsequent list_del_rcu()/list_del() on the poisoned list pointers cause a general protection fault in __list_del_entry_valid_or_report(). The tree can never be pruned either, since its rule list never becomes empty. A rule mixing AUDIT_DIR, an LSM field and AUDIT_EXE reaches this path without memory pressure: audit_dupe_exe() fails with -ENOENT once the exe path no longer resolves, and the update is triggered by writing 1 to /sys/fs/selinux/enforce (avc_ss_reset() -> audit_update_lsm_rules()). Call audit_remove_tree_rule() for tree rules in the error branch, as audit_del_rule() does, so that the rlist is unlinked and the tree reference dropped before the entry is freed. KASAN report: ================================================================== BUG: KASAN: slab-use-after-free in kill_rules+0x39/0x4a0 Read of size 8 at addr ffff888015b30570 by task poc-a53-2/332 CPU: 1 UID: 0 PID: 332 Comm: poc-a53-2 Not tainted 7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy) 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 kasan_report+0xd6/0x110 __asan_load8+0x82/0xb0 kill_rules+0x39/0x4a0 audit_tree_freeing_mark+0x337/0x560 fsnotify_free_mark+0x7c/0xa0 fsnotify_destroy_marks+0x1c1/0x270 fsnotify_unmount_inodes+0x128/0x230 fsnotify_sb_delete+0x9f/0x270 generic_shutdown_super+0x4f/0x240 kill_anon_super+0x29/0x50 deactivate_locked_super+0x6d/0xc0 deactivate_super+0x63/0x70 cleanup_mnt+0x5d/0xb0 __cleanup_mnt+0x16/0x20 task_work_run+0x10d/0x190 exit_to_user_mode_loop+0x18d/0x5b0 do_syscall_64+0x4dc/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fe4e7864b87 Code: 7f a2 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 0f 1f 44 00 00 31 f6 e9 09 00 00 00 66 0f 1f 84 00 00 00 00 00 b8 a6 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 01 c3 48 8b 15 49 a2 0c 00 f7 d8 64 89 02 b8 RSP: 002b:00007ffe32b8dbe8 EFLAGS: 00000246 ORIG_RAX: 00000000000000a6 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 00007fe4e7864b87 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000055ac53383092 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000073 R10: 00007fe4e7772418 R11: 0000000000000246 R12: 0000000000000000 R13: 0000000000000000 R14: 000055ac53395100 R15: 0000000000000447 Allocated by task 332: kasan_save_stack+0x2c/0x60 kasan_save_track+0x18/0x50 kasan_save_alloc_info+0x3a/0x50 __kasan_kmalloc+0x88/0xa0 __kmalloc_cache_noprof+0x24d/0x600 audit_data_to_entry+0x13b/0xed0 audit_rule_change+0xe3/0x740 audit_receive_msg+0x26a/0x1bc0 audit_receive+0x17f/0x2a0 netlink_unicast+0x3d4/0x500 netlink_sendmsg+0x451/0x7f0 __sys_sendto+0x384/0x3a0 __x64_sys_sendto+0x84/0xa0 x64_sys_call+0xf18/0x21d0 do_syscall_64+0x11a/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 318: kasan_save_stack+0x2c/0x60 kasan_save_track+0x18/0x50 kasan_save_free_info+0x3e/0x60 __kasan_slab_free+0x5c/0x80 kfree+0x240/0x500 audit_free_rule_rcu+0x128/0x140 rcu_core+0x431/0xe30 rcu_core_si+0x12/0x20 handle_softirqs+0x130/0x610 __irq_exit_rcu+0x10f/0x180 irq_exit_rcu+0x12/0x20 sysvec_apic_timer_interrupt+0x7e/0x90 asm_sysvec_apic_timer_interrupt+0x1f/0x30 Last potentially related work creation: kasan_save_stack+0x2c/0x60 __kasan_record_aux_stack+0x9d/0xb0 kasan_record_aux_stack+0xf/0x20 __call_rcu_common.constprop.0+0x88/0x200 call_rcu+0x12/0x20 audit_update_lsm_rules+0x201/0x530 selinux_audit_rule_avc_callback+0x1e/0x30 avc_ss_reset+0x193/0x290 sel_write_enforce+0x28a/0x2c0 vfs_write+0x1d1/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 ffff888015b30400 which belongs to the cache kmalloc-512 of size 512 The buggy address is located 368 bytes inside of freed 512-byte region [ffff888015b30400, ffff888015b30600) The buggy address belongs to the physical page: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x15b30 head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 flags: 0x100000000000040(head|node=0|zone=1) page_type: f5(slab) raw: 0100000000000040 ffff888007441c80 dead000000000100 dead000000000122 raw: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000 head: 0100000000000040 ffff888007441c80 dead000000000100 dead000000000122 head: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000 head: 0100000000000002 ffffffffffffff01 00000000ffffffff 00000000ffffffff head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff888015b30400: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff888015b30480: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >ffff888015b30500: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ^ ffff888015b30580: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff888015b30600: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ================================================================== Oops in the same run: Oops: general protection fault, probably for non-canonical address 0x18202950000013e: 0000 [#1] SMP KASAN PTI CPU: 0 UID: 0 PID: 332 Comm: poc-a53-2 Tainted: G B 7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy) Tainted: [B]=BAD_PAGE Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 RIP: 0010:__list_del_entry_valid_or_report+0x5f/0x160 Code: 48 b8 00 01 00 00 00 00 ad de 49 39 c6 74 7c 48 b8 22 01 00 00 00 00 ad de 49 39 c5 0f 84 87 00 00 00 4c 89 ef e8 f1 63 8f ff <4d> 39 65 00 0f 85 93 00 00 00 4d 8d 6e 08 4c 89 ef e8 db 63 8f ff RSP: 0018:ffff88801361fa80 EFLAGS: 00010282 RAX: 0000000000000000 RBX: ffff888015b30570 RCX: ffffffff81e808bf RDX: 0000000000000000 RSI: 0000000000000008 RDI: 018202950000013e RBP: ffff88801361fa98 R08: ffffffff8152883c R09: fffffbfff0ae5ed4 R10: ffffffff8572f6a7 R11: 74206c656e72656b R12: ffff888015b30400 R13: 018202950000013e R14: ffff88800a2fce40 R15: ffff888015b30400 FS: 00007fe4e7759740(0000) GS:ffff8880e7930000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f4e959f1358 CR3: 0000000015628000 CR4: 00000000000006f0 Call Trace: kill_rules+0x129/0x4a0 audit_tree_freeing_mark+0x337/0x560 fsnotify_free_mark+0x7c/0xa0 fsnotify_destroy_marks+0x1c1/0x270 fsnotify_unmount_inodes+0x128/0x230 fsnotify_sb_delete+0x9f/0x270 generic_shutdown_super+0x4f/0x240 kill_anon_super+0x29/0x50 deactivate_locked_super+0x6d/0xc0 deactivate_super+0x63/0x70 cleanup_mnt+0x5d/0xb0 __cleanup_mnt+0x16/0x20 task_work_run+0x10d/0x190 exit_to_user_mode_loop+0x18d/0x5b0 do_syscall_64+0x4dc/0x620 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fe4e7864b87 Code: 7f a2 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 0f 1f 44 00 00 31 f6 e9 09 00 00 00 66 0f 1f 84 00 00 00 00 00 b8 a6 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 01 c3 48 8b 15 49 a2 0c 00 f7 d8 64 89 02 b8 RSP: 002b:00007ffe32b8dbe8 EFLAGS: 00000246 ORIG_RAX: 00000000000000a6 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 00007fe4e7864b87 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000055ac53383092 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000073 R10: 00007fe4e7772418 R11: 0000000000000246 R12: 0000000000000000 R13: 0000000000000000 R14: 000055ac53395100 R15: 0000000000000447 Modules linked in: Fixes: 74c3cbe33bc0 ("[PATCH] audit: watching subtrees") Cc: stable@vger.kernel.org Signed-off-by: Xingyuan Mo --- kernel/auditfilter.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c index cf91ba6f3617b..06fe7ab13eb7a 100644 --- a/kernel/auditfilter.c +++ b/kernel/auditfilter.c @@ -1422,6 +1422,8 @@ static int update_lsm_rule(struct audit_krule *r) audit_panic("error updating LSM filters"); if (r->watch) list_del(&r->rlist); + if (r->tree) + audit_remove_tree_rule(r); list_del_rcu(&entry->list); list_del(&r->list); audit_rule_unaccount(r); -- 2.43.0