KASAN: slab-use-after-free Read in put_ctx

Status: moderation: reported on 2026/11/15 12:56
Subsystems: perf
Labels: prio:high
[Documentation on labels]
Reported-by: syzbot+9b562fcd543eac5d27ee@syzkaller.appspotmail.com
First crash: 4d01h, last: 4d01h
✨ AI-generated root cause summary:

In put_ctx() (kernel/events/core.c), when refcount_dec_and_test(&ctx->refcount) returns false, the caller enters the else branch after already dropping its reference to struct perf_event_context without holding an RCU read lock. Concurrently, another thread (e.g., an exiting task in perf_event_exit_task_context()) can drop the final reference, causing call_rcu(&ctx->rcu_head, free_ctx) to be invoked. Once the RCU grace period completes and free_ctx() frees the context via kfree(), any preempted or delayed thread resuming in put_ctx() accesses ctx->task at line 1394 on already-freed memory, triggering a slab-use-after-free.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 475a0cfd02f1:

Technical Root Cause Analysis

Overview

The reported bug is a post-decrement slab-use-after-free in put_ctx() at kernel/events/core.c:1394. A thread dropping a reference to struct perf_event_context accesses ctx->task after refcount_dec_and_test(&ctx->refcount) returns false. Because the thread no longer owns a reference to ctx and does not hold an RCU read-side critical section, a concurrent thread can release the final reference, leading to RCU reclamation and freeing of ctx before the first thread completes its access.


Key Entities and Files Involved

  • Source File: kernel/events/core.c
  • Functions:
    • put_ctx() (kernel/events/core.c:1384–1397)
    • __free_event() (kernel/events/core.c:5812–5818)
    • perf_event_exit_task_context() (kernel/events/core.c:14744–14820)
    • free_ctx() (kernel/events/core.c:1376–1382)
  • Structures & Fields:
    • struct perf_event_context:
      • refcount (refcount_t): reference counter for the context object.
      • task (struct task_struct *, offset 296): pointer to the task tracked by this context (or TASK_TOMBSTONE when dead).
      • rcu_head: RCU callback head for deferred freeing via free_ctx().

Detailed Trigger Mechanism & Invariant Violation

In kernel/events/core.c, put_ctx() is defined as follows:

static void put_ctx(struct perf_event_context *ctx)
{
	if (refcount_dec_and_test(&ctx->refcount)) {
		if (ctx->parent_ctx)
			put_ctx(ctx->parent_ctx);
		if (ctx->task && ctx->task != TASK_TOMBSTONE)
			put_task_struct(ctx->task);
		call_rcu(&ctx->rcu_head, free_ctx);
	} else {
		smp_mb__after_atomic(); /* pairs with wait_var_event() */
		if (ctx->task == TASK_TOMBSTONE)
			wake_up_var(&ctx->refcount);
	}
}

Standard reference counting rules dictate that once a caller calls refcount_dec_and_test(&ctx->refcount) and receives false, it has relinquished its reference to ctx. Unless protected by another mechanism (such as holding rcu_read_lock() or holding a lock embedded in a parent container), the caller cannot safely dereference ctx after the decrement because another holder can concurrently drop the final reference and free the object.

Here, the post-decrement check was added to wake up wait_var_event(&ctx->refcount, ...) in perf_event_exit_task_context() (which waits for event references to be dropped when !exit). However, this violates memory safety during task exit:

  1. Context State: A task context (struct perf_event_context *ctx) has two references:

    • One held by an open perf event (event->ctx).
    • One held by the target task's exit sequence via perf_pin_task_context() in perf_event_exit_task_context(task, exit=true) (after dropping the task's task->perf_event_ctxp reference at line 14779). ctx->task has been marked WRITE_ONCE(ctx->task, TASK_TOMBSTONE) at line 14780.
  2. Concurrent Decrement:

    • Task A (PID 8324, event closer): Closes the event file (__fput $\to$ perf_release $\to$ perf_event_release_kernel $\to$ __free_event), which invokes put_ctx(event->ctx) at line 5817.
    • refcount_dec_and_test(&ctx->refcount) decrements ctx->refcount from 2 to 1 and returns false.
    • Task A enters the else branch. In a preemptible kernel (PREEMPT(full)), Task A is preempted or delayed right before line 1394 without holding an RCU read lock.
  3. Final Free:

    • Task B (PID 8338, exiting target task): In perf_event_exit_task_context(task, true), because exit == true, it skips wait_var_event() and calls put_ctx(ctx) at line 14819.
    • refcount_dec_and_test(&ctx->refcount) decrements ctx->refcount from 1 to 0 and returns true.
    • Task B calls call_rcu(&ctx->rcu_head, free_ctx).
    • The RCU grace period elapses, and the RCU core executes free_ctx(), which calls kfree(ctx).
  4. Use-After-Free:

    • Task A resumes execution at line 1394:
      if (ctx->task == TASK_TOMBSTONE)
      
    • Task A reads ctx->task (offset 296, address ffff88807abdfd28) from the freed kmalloc-512 slab object, triggering the KASAN slab-use-after-free splat.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
32a9417e repro-c 💥 KASAN: slab-use-after-free Read in put_ctx 2026/11/14 00:03 2026/11/14 00:03 2026/11/14 06:18 819ebe34
97f51c5f assessment-security DenialOfService: ✅ Exploitable: ✅ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ✅ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KASAN: slab-use-after-free Read in put_ctx 2026/11/11 12:37 2026/11/11 12:38 2026/11/11 12:46 d3056657

Sample crash report:
==================================================================
BUG: KASAN: slab-use-after-free in put_ctx+0xb4/0x1d0 kernel/events/core.c:1394
BUG: KASAN: slab-use-after-free in put_ctx+0xb4/0x1d0 kernel/events/core.c:1394
Read of size 8 at addr ffff88807abdfd28 by task syz.3.460/8324

CPU: 0 UID: 0 PID: 8324 Comm: syz.3.460 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 print_address_description+0x55/0x1e0 mm/kasan/report.c:378
 print_report+0x58/0x70 mm/kasan/report.c:482
 kasan_report+0x117/0x150 mm/kasan/report.c:595
 put_ctx+0xb4/0x1d0 kernel/events/core.c:1394
 __free_event+0x4f6/0x790 kernel/events/core.c:5817
 put_event kernel/events/core.c:5936 [inline]
 perf_event_release_kernel+0x40e/0x4c0 kernel/events/core.c:6046
 perf_release+0x38/0x50 kernel/events/core.c:6056
 __fput+0x408/0xa40 fs/file_table.c:512
 fput_close_sync+0x11f/0x240 fs/file_table.c:618
 filp_close_sync+0x6e/0xa0 fs/open.c:1573
 close_files fs/file.c:572 [inline]
 put_files_struct+0x230/0x3c0 fs/file.c:584
 do_exit+0x6f0/0x2360 kernel/exit.c:977
 __do_sys_exit kernel/exit.c:1090 [inline]
 __se_sys_exit kernel/exit.c:1088 [inline]
 __x64_sys_exit+0x40/0x40 kernel/exit.c:1088
 x64_sys_call+0x18b4/0x18d0 arch/x86/include/generated/asm/syscalls_64.h:61
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f38d41a8f19
Code: Unable to access opcode bytes at 0x7f38d41a8eef.
RSP: 002b:00007f38d4ff7fd8 EFLAGS: 00000246 ORIG_RAX: 000000000000003c
RAX: ffffffffffffffda RBX: 00007f38d4426090 RCX: 00007f38d41a8f19
RDX: 00007f38d4ff89c8 RSI: 0000000000000000 RDI: 0000000000000000
RBP: 00007f38d4237f6d R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f38d4426128 R14: 00007f38d4426090 R15: 00007ffce1039d08
 </TASK>

Allocated by task 8313:
 kasan_save_stack mm/kasan/common.c:57 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
 poison_kmalloc_redzone mm/kasan/common.c:409 [inline]
 __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:426
 kasan_kmalloc include/linux/kasan.h:263 [inline]
 __kmalloc_cache_noprof+0x31c/0x600 mm/slub.c:5680
 _kmalloc_noprof include/linux/slab.h:991 [inline]
 _kzalloc_noprof include/linux/slab.h:1312 [inline]
 alloc_perf_context kernel/events/core.c:5082 [inline]
 find_get_context+0xc7/0x860 kernel/events/core.c:5152
 __do_sys_perf_event_open kernel/events/core.c:14200 [inline]
 __se_sys_perf_event_open+0xc40/0x1ee0 kernel/events/core.c:14031
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 8338:
 kasan_save_stack mm/kasan/common.c:57 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
 kasan_save_free_info+0x40/0x50 mm/kasan/generic.c:584
 poison_slab_object mm/kasan/common.c:264 [inline]
 __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:296
 kasan_slab_free include/linux/kasan.h:235 [inline]
 slab_free_hook mm/slub.c:2780 [inline]
 slab_free mm/slub.c:6630 [inline]
 kfree+0x1c5/0x650 mm/slub.c:6923
 rcu_do_batch kernel/rcu/tree.c:2650 [inline]
 rcu_core+0x92e/0x1230 kernel/rcu/tree.c:2919
 handle_softirqs+0x265/0x8d0 kernel/softirq.c:669
 __do_softirq kernel/softirq.c:703 [inline]
 invoke_softirq kernel/softirq.c:542 [inline]
 __irq_exit_rcu+0xc6/0x220 kernel/softirq.c:786
 irq_exit_rcu+0x9/0x30 kernel/softirq.c:809
 instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
 sysvec_apic_timer_interrupt+0xa6/0xc0 arch/x86/kernel/apic/apic.c:1062
 asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674

Last potentially related work creation:
 kasan_save_stack+0x3e/0x60 mm/kasan/common.c:57
 kasan_record_aux_stack+0xbd/0xd0 mm/kasan/generic.c:556
 __call_rcu_common kernel/rcu/tree.c:3287 [inline]
 call_rcu+0x86/0x300 kernel/rcu/tree.c:3395
 perf_event_exit_task_context+0x68e/0x7c0 kernel/events/core.c:14819
 perf_event_exit_task+0x232/0x3a0 kernel/events/core.c:14848
 do_exit+0x69b/0x2360 kernel/exit.c:959
 do_group_exit+0x22d/0x2f0 kernel/exit.c:1123
 get_signal+0x136a/0x1430 kernel/signal.c:3113
 arch_do_signal_or_restart+0xbb/0x860 arch/x86/kernel/signal.c:337
 __exit_to_user_mode_loop kernel/entry/common.c:66 [inline]
 exit_to_user_mode_loop+0x10e/0x770 kernel/entry/common.c:101
 __exit_to_user_mode_prepare include/linux/irq-entry-common.h:207 [inline]
 syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:230 [inline]
 syscall_exit_to_user_mode include/linux/entry-common.h:343 [inline]
 do_syscall_64+0x328/0x520 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

The buggy address belongs to the object at ffff88807abdfc00
 which belongs to the cache kmalloc-512 of size 512
The buggy address is located 296 bytes inside of
 freed 512-byte region [ffff88807abdfc00, ffff88807abdfe00)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff88807abdf800 pfn:0x7abdc
head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000000240(workingset|head|node=0|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 00fff00000000240 ffff88813ff28c80 ffffea00014a5110 ffffea000074ba10
raw: ffff88807abdf800 0000000000100008 00000000f5000000 0000000000000000
head: 00fff00000000240 ffff88813ff28c80 ffffea00014a5110 ffffea000074ba10
head: ffff88807abdf800 0000000000100008 00000000f5000000 0000000000000000
head: 00fff00000000002 ffffffffffffff01 00000000ffffffff 00000000ffffffff
head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000004
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 2, migratetype Unmovable, gfp_mask 0xd2040(__GFP_IO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 5277, tgid 5277 (S30dbus), ts 49489599498
 set_page_owner include/linux/page_owner.h:33 [inline]
 post_alloc_hook+0x1f9/0x250 mm/page_alloc.c:1856
 prep_new_page mm/page_alloc.c:1864 [inline]
 get_page_from_freelist+0x220f/0x2280 mm/page_alloc.c:3935
 __alloc_frozen_pages_noprof+0x1fe/0x410 mm/page_alloc.c:5444
 alloc_slab_page mm/slub.c:3384 [inline]
 allocate_slab+0x7d/0x5e0 mm/slub.c:3499
 new_slab mm/slub.c:3550 [inline]
 refill_objects+0x2d5/0x350 mm/slub.c:7539
 refill_sheaf mm/slub.c:2917 [inline]
 __pcs_replace_empty_main+0x2c8/0x6c0 mm/slub.c:4811
 alloc_from_pcs mm/slub.c:4887 [inline]
 slab_alloc_node mm/slub.c:5021 [inline]
 __do_kmalloc_node mm/slub.c:5530 [inline]
 __kmalloc_noprof+0x476/0x710 mm/slub.c:5556
 _kmalloc_noprof include/linux/slab.h:995 [inline]
 _kzalloc_noprof include/linux/slab.h:1312 [inline]
 tomoyo_init_log+0x1a6b/0x1f60 security/tomoyo/audit.c:273
 tomoyo_supervisor+0x35f/0x1550 security/tomoyo/common.c:2232
 tomoyo_audit_path_number_log security/tomoyo/file.c:239 [inline]
 tomoyo_path_number_perm+0x463/0x5f0 security/tomoyo/file.c:739
 security_file_ioctl+0xc3/0x2a0 security/security.c:2533
 __do_sys_ioctl fs/ioctl.c:591 [inline]
 __se_sys_ioctl+0x47/0x170 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5003 tgid 5003 ts 41185611989 stack trace:
 reset_page_owner include/linux/page_owner.h:26 [inline]
 __free_pages_prepare mm/page_alloc.c:1403 [inline]
 __free_frozen_pages+0xc97/0xd90 mm/page_alloc.c:2954
 __slab_free+0x269/0x2c0 mm/slub.c:5926
 qlink_free mm/kasan/quarantine.c:163 [inline]
 qlist_free_all+0x99/0x100 mm/kasan/quarantine.c:179
 kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
 __kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:361
 kasan_slab_alloc include/linux/kasan.h:253 [inline]
 slab_post_alloc_hook mm/slub.c:4720 [inline]
 slab_alloc_node mm/slub.c:5033 [inline]
 kmem_cache_alloc_noprof+0x2b4/0x600 mm/slub.c:5047
 lsm_file_alloc security/security.c:172 [inline]
 security_file_alloc+0x34/0x310 security/security.c:2428
 init_file+0x90/0x2b0 fs/file_table.c:184
 alloc_empty_file+0x74/0x1d0 fs/file_table.c:266
 path_openat+0x96/0x20f0 fs/namei.c:5002
 do_file_open+0x23e/0x4a0 fs/namei.c:5045
 do_sys_openat2+0x115/0x200 fs/open.c:1442
 do_sys_open fs/open.c:1448 [inline]
 __do_sys_openat fs/open.c:1464 [inline]
 __se_sys_openat fs/open.c:1459 [inline]
 __x64_sys_openat+0x138/0x170 fs/open.c:1459
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
 ffff88807abdfc00: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff88807abdfc80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88807abdfd00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                  ^
 ffff88807abdfd80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff88807abdfe00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/11 12:37 bpf 475a0cfd02f1 d3056657 .config console log report info [disk image] [vmlinux] [kernel image] ci-upstream-bpf-kasan-gce KASAN: slab-use-after-free Read in put_ctx
* Struck through repros no longer work on HEAD.