[Bug report] audit: fsnotify autoremove handler reads a freed audit_entry when the rule is deleted while the handler is blocked in audit_log_start()

From: Guikun Yang

Date: Fri Nov 20 2026 - 13:25:25 EST


Hi Paul, Eric,

An fsnotify watch rule (-w) keeps a raw back-pointer from its mark to
the owning audit_krule (audit_mark->rule, kernel/audit_fsnotify.c:108).
When the watched directory is renamed or removed, the autoremove path
runs in the fsnotify event handler, and a concurrent AUDIT_DEL_RULE can
free the rule while that handler is still inside the SRCU read section
of fsnotify event delivery. The handler then reads the freed
audit_entry.

BUG: KASAN: slab-use-after-free in audit_mark_handle_event+0x42b/0x450
Read of size 8 at addr ffff888007225138 by task init/104
audit_log_key+.. <- audit_mark_log_rule_change <- audit_mark_handle_event
fsnotify <- fsnotify_move <- vfs_rename
...
Allocated by task 1:
audit_data_to_entry (kmalloc-512, offset 312 = audit_entry)
Freed by task 95:
kfree <- rcu_core <- call_rcu <- audit_del_rule+0x44d

## The mismatch

The deletion side waits with plain RCU:

kernel/auditfilter.c, audit_del_rule():
list_del_rcu(&e->list);
synchronize_rcu(); /* plain RCU */
...
call_rcu(&e->rcu, audit_free_rule_rcu);

but the consuming side runs under fsnotify_mark_srcu (fs/notify/mark.c):
fsnotify_handle_event() takes srcu_read_lock(&fsnotify_mark_srcu) and
calls the group's handle_inode_event under it, so a handler that blocks
(e.g. in audit_log_start() with a full backlog) is an SRCU reader that a
plain RCU grace period does not wait for. When the handler finally gets
an audit_buffer and reaches audit_mark_log_rule_change(), it dereferences
the already-freed rule:

kernel/audit_fsnotify.c:129 struct audit_krule *rule = audit_mark->rule;
kernel/audit_fsnotify.c:139 audit_log_key(ab, rule->filterkey);

The second return path has the same problem: when the backlog wait times
out, audit_log_start() returns NULL and the handler falls through to
audit_autoremove_mark_rule() -> audit_del_rule(entry), which itself
reads the freed entry through the same back-pointer.

## Reproducer timing

The window needs the handler to block inside audit_log_start() long
enough for a concurrent DEL_RULE to complete a full plain RCU grace
period, which requires an audit backlog. The reproducer orchestrates:

- one -F exe= rule installed via AUDIT_ADD_RULE (CAP_AUDIT_CONTROL),
backlog_limit=2, backlog_wait_time=8s;
- a fake auditd child that registers (AUDIT_SET PID) and then never
reads its netlink socket, so kauditd blocks in netlink_attachskb on
the full rcvbuf and the backlog stays over the limit;
- 8 private-socket AUDIT_USER senders that push qlen to 8 within
milliseconds, so non-auditd tasks sleep 8s in audit_log_start();
- thread B: rename() of the watched directory -> FS_MOVE_SELF ->
the autoremove handler enters and blocks in audit_log_start();
- a deleter child: chroot() into a mirror tree so the rule's exe path
string still compares equal (the EXE field match is a plain strcmp
of path strings) but nothing touches the renamed directory, then
AUDIT_DEL_RULE; list_del -> synchronize_rcu -> mark removal ->
call_rcu -> kfree of the audit_entry about 0.3s later;
- B wakes ~8s later and reads rule->filterkey -> KASAN report.

One run out of three hit the report on the queue-drained path; the
remaining two verified the deleter's DEL_RULE returned 0 and the entry
was freed while B was still blocked (serial console shows B inside
audit_log_start at that point). Not reachable by plain syscall fuzzing:
it needs CAP_AUDIT_CONTROL rule management plus a multi-thread timing
window that fuzzers do not model.

The window also exists on the DEL_RULE-same-rule path without any
rename event, whenever a watch rule is deleted while an event handler
for it is blocked in the same way.

## Versions

Reproduced on 7.3.0 (KASAN inline, GPF/KASAN build, QEMU). The
back-pointer and the plain-RCU wait are unchanged in current mainline
(kernel/audit_fsnotify.c:108/129/139, kernel/auditfilter.c:1053).
syzbot has no report for this path; the audit entries in its dashboard
are all on the data plane (audit_log_*/kauditd), none on rule
lifecycle. No CVE or list thread found for this mechanism.

## Possible fix direction

The deletion side already knows the rule carries a watch mark
(e->rule.exe). For such rules, the free must not proceed on plain RCU
alone: waiting for fsnotify_mark_srcu readers before call_rcu (or
routing the free through a work that first does
synchronize_srcu(&fsnotify_mark_srcu)) would close the window, in the
same spirit as the exe-rule lifetime work in 888a0396e1 ("audit: fix
removal of dangling executable rules").

The reproducer (self-contained C, PID1 in a VM, netlink rule setup,
flood/B/deleter threads) is available on request.

Thanks,
Guikun Yang