perf/core: Fix refcount bug and potential UAF in perf_mmap
PATCH 1/1 perf/core: Fix refcount bug and potential UAF in perf_mmap
Hello,
I would like to report a reference counting vulnerability in the Linux kernel perf_event subsystem, which I discovered using a modified syzkaller-based kernel fuzzing tool that I developed.
Summary
-------
A local user can trigger a reference count saturation or a use-after-free (UAF) vulnerability in the perf_mmap function. This is caused by a race condition where a ring_buffer object's reference count is incremented after it has already reached zero.
The vulnerability exists in the perf_mmap() function in kernel/events/core.c. While the function uses mmap_mutex to protect the initial buffer setup, it performs subsequent operations (such as map_range) on event->rb outside of the locked scope. If the event is closed or the buffer is detached concurrently, the reference count of the ring_buffer can drop to zero, leading to an 'addition on 0' warning or a UAF when the kernel attempts to access or increment it later.
I verified this on Linux kernel version 6.18.5.
Environment
-----------
- Kernel version: 6.18.5 (the complete config is attached)
- Architecture: x86_64
- Hypervisor: QEMU (Standard PC i440FX + PIIX, BIOS 1.13.0-1ubuntu1.1)
Symptoms and logs
-----------------
The kernel triggers a 'refcount_t: addition on 0; use-after-free' warning followed by a memory leak warning
The full report is as below:
audit: type=1400 audit(1769676568.351:202): avc: denied { open } for pid=21484 comm="syz.6.2386" scontext=system_u:system_r:kernel_t:s0 tcontext=system_u:system_r:kernel_t:s0 tclass=perf_event permissive=1
------------[ cut here ]------------ refcount_t: addition on 0; use-after-free. WARNING: CPU: 3 PID: 21486 at lib/refcount.c:25 refcount_warn_saturate+0x13c/0x1b0 lib/refcount.c:25 Modules linked in: CPU: 3 UID: 0 PID: 21486 Comm: syz.6.2386 Not tainted 6.18.5 #1 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 RIP: 0010:refcount_warn_saturate+0x13c/0x1b0 lib/refcount.c:25 Code: f0 40 ff 80 3d 70 44 61 03 00 0f 85 52 ff ff ff e8 c9 f0 40 ff c6 05 5e 44 61 03 01 90 48 c7 c7 80 43 5c b8 e8 75 5d 0f ff 90 <0f> 0b 90 90 e9 2f ff ff ff e8 a6 f0 40 ff 80 3d 3d 44 61 03 00 0f RSP: 0018:ffff888103c17678 EFLAGS: 00010286 RAX: 0000000000000000 RBX: ffff888107641190 RCX: ffffffff8137110c RDX: 0000000000080000 RSI: ffffc90002279000 RDI: ffff88811b3a3e88 RBP: 0000000000000002 R08: fffffbfff7219644 R09: ffffed10236747d2 R10: ffffed10236747d1 R11: ffff88811b3a3e8b R12: 0000000000000000 R13: ffff888002255a10 R14: ffff888002255a00 R15: ffff888107641170 FS: 00007f7ef46f3640(0000) GS:ffff888160a03000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000000478 CR3: 000000010698c006 CR4: 0000000000770ff0 DR0: 0000000000000000 DR1: 00000200000000a2 DR2: 00000200000000a2 DR3: 00000200000000a2 DR6: 00000000ffff0ff0 DR7: 0000000000000600 PKRU: 80000000 Call Trace: <TASK> __refcount_add include/linux/refcount.h:289 [inline] __refcount_inc include/linux/refcount.h:366 [inline] refcount_inc include/linux/refcount.h:383 [inline] perf_mmap_rb kernel/events/core.c:7005 [inline] perf_mmap+0x126d/0x1990 kernel/events/core.c:7163 vfs_mmap include/linux/fs.h:2405 [inline] mmap_file mm/internal.h:167 [inline] __mmap_new_file_vma mm/vma.c:2413 [inline] __mmap_new_vma mm/vma.c:2476 [inline] __mmap_region+0xea5/0x2250 mm/vma.c:2670 mmap_region+0x267/0x350 mm/vma.c:2740 do_mmap+0x769/0xe50 mm/mmap.c:558 vm_mmap_pgoff+0x1e1/0x330 mm/util.c:581 ksys_mmap_pgoff+0x35d/0x4b0 mm/mmap.c:604 __do_sys_mmap arch/x86/kernel/sys_x86_64.c:89 [inline] __se_sys_mmap arch/x86/kernel/sys_x86_64.c:82 [inline] __x64_sys_mmap+0x116/0x180 arch/x86/kernel/sys_x86_64.c:82 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xac/0x2a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f7ef5cabb9d Code: 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f7ef46f2fc8 EFLAGS: 00000246 ORIG_RAX: 0000000000000009 RAX: ffffffffffffffda RBX: 00007f7ef5f01fa0 RCX: 00007f7ef5cabb9d RDX: 0000000001000003 RSI: 0000000000002000 RDI: 0000200000ffa000 RBP: 00007f7ef5d2f00a R08: 0000000000000007 R09: 0000000000000000 R10: 0000000000000013 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f7ef5f01fac R14: 00007f7ef5f02038 R15: 00007f7ef46f3640 </TASK> ---[ end trace 0000000000000000 ]---
EXT4-fs error (device loop0): ext4_mb_generate_buddy:1303: group 0, block bitmap and bg descriptor inconsistent: 219 vs 12386523 free clusters EXT4-fs (loop0): Delayed block allocation failed for inode 15 at logical offset 1 with max blocks 2048 with error 28 EXT4-fs (loop0): This should not happen!! Data will be lost
EXT4-fs (loop0): Total free blocks count 0
EXT4-fs (loop0): Free/Dirty block details
EXT4-fs (loop0): free_blocks=12386304
EXT4-fs (loop0): dirty_blocks=16387
EXT4-fs (loop0): Block reservation details
EXT4-fs (loop0): i_reserved_data_blocks=16387
EXT4-fs (loop0): Delayed block allocation failed for inode 15 at logical offset 2052 with max blocks 2048 with error 28 EXT4-fs (loop0): This should not happen!! Data will be lost
<<<<<<<<<<<<<<< tail report >>>>>>>>>>>>>>>
SYZFAIL: failed to recv rpc
fd=3 want=4 recv=0 n=0 (errno 9: Bad file descriptor)
<<<<<<<<<<<<<<< tail report >>>>>>>>>>>>>>>
Reproduce
----------
The issue is reproducible using the C reproducer attached. The reproducer triggers the vulnerability by creating a high-frequency race condition between memory mapping and event teardown.
The reproducer follows this execution flow:
1. Event Creation: It initializes a performance monitoring event via perf_event_open(), typically with inherit or specific sample_type flags that necessitate the allocation of a kernel ring_buffer.
2. Multithreaded Hammering: The program spawns multiple threads or forks child processes to perform concurrent operations on the same file descriptor.
3. The Race: Thread A continuously calls mmap() on the perf file descriptor. This enters the kernel-side perf_mmap() function, which briefly acquires the mmap_mutex to set up the buffer but then drops it. While thread B (or the main loop) attempts to close the descriptor or modify the event state, which can trigger the destruction or detachment of the ring_buffer.
4. Vulnerability Trigger: Because perf_mmap() accesses event->rb to perform map_range() after the mmap_mutex has been released, Thread B can drop the buffer's reference count to zero during this unprotected window.
5. Crash/Warning: When Thread A finally reaches the code that increments the reference count or accesses the buffer (e.g., in perf_mmap_rb or map_range), the refcount_t infrastructure detects an "addition on 0," resulting in the KASAN or refcount_warn_saturate report.
Security impact
---------------
The vulnerability allows a local user to compromise system integrity by triggering a reference count saturation or a Use-After-Free (UAF) condition. While the immediate symptom is typically a kernel warning or a Denial of Service through a system hang or panic, especially in environments with panic_on_warn enabled, the underlying memory corruption represents a more significant threat. By causing a ring_buffer object to be accessed after its reference count has reached zero, an attacker may be able to leverage this UAF state to perform heap grooming. If the freed memory is reallocated with a controlled structure, it could potentially be exploited to achieve local privilege escalation, making this a critical issue for multi-user systems or containerized environments where the perf_event interface is accessible.
Patch
--------------
From 34545a4d43adef3147e0ba1c744deb128a05a101 Mon Sep 17 00:00:00 2001
From: 0ne1r0s <[email protected]>
Date: Sat, 31 Jan 2026 21:16:52 +0800
Subject: [PATCH] perf/core: Fix refcount bug and potential UAF in perf_mmap
The issue is caused by a race condition between mmap() and event teardown. In perf_mmap(), the ring_buffer (rb) is accessed via map_range() after the mmap_mutex is released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation when map_range() or subsequent logic attempts to use it.
Fix this by extending the scope of mmap_mutex to cover the entire setup process, including map_range(), ensuring the buffer remains valid until the mapping is complete.
--- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
kernel/events/core.c +21 -21
@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } - -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); - -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; + + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); + +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }
Signature · 16 lines
2.51.0 Request ------- Could you please review this issue and the proposed fix? If this is a confirmed new vulnerability, I would appreciate coordination on a CVE ID. Best regards, Haocheng Yu Zhejiang University
Syzkaller hit 'WARNING: refcount bug in perf_mmap' bug.
audit: type=1400 audit(1769331402.322:11): avc: denied { read } for pid=1256 comm="syz.3.17" scontext=system_u:system_r:kernel_t:s0 tcontext=system_u:system_r:kernel_t:s0 tclass=perf_event permissive=1
------------[ cut here ]------------ refcount_t: addition on 0; use-after-free. WARNING: CPU: 0 PID: 1256 at lib/refcount.c:25 refcount_warn_saturate+0x13c/0x1b0 lib/refcount.c:25 Modules linked in: CPU: 0 UID: 0 PID: 1256 Comm: syz.3.17 Not tainted 6.18.5 #1 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 RIP: 0010:refcount_warn_saturate+0x13c/0x1b0 lib/refcount.c:25 Code: f0 40 ff 80 3d 70 44 61 03 00 0f 85 52 ff ff ff e8 c9 f0 40 ff c6 05 5e 44 61 03 01 90 48 c7 c7 80 43 7c 9a e8 75 5d 0f ff 90 <0f> 0b 90 90 e9 2f ff ff ff e8 a6 f0 40 ff 80 3d 3d 44 61 03 00 0f RSP: 0018:ffff8881036bf678 EFLAGS: 00010286 RAX: 0000000000000000 RBX: ffff8881027387c0 RCX: ffffffff9757110c RDX: ffff888102cb4800 RSI: 0000000000000008 RDI: ffff88811b228000 RBP: 0000000000000002 R08: fffffbfff3659644 R09: ffffed10206d7e8c R10: ffffed10206d7e8b R11: ffff8881036bf45f R12: 0000000000000000 R13: ffff88810e816310 R14: ffff88810e816300 R15: ffff8881027387a0 FS: 000055558ca3a540(0000) GS:ffff88817e683000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000000078 CR3: 000000010406c005 CR4: 0000000000770ff0 PKRU: 55555554 Call Trace: <TASK> __refcount_add include/linux/refcount.h:289 [inline] __refcount_inc include/linux/refcount.h:366 [inline] refcount_inc include/linux/refcount.h:383 [inline] perf_mmap_rb kernel/events/core.c:7005 [inline] perf_mmap+0x126d/0x1990 kernel/events/core.c:7163 vfs_mmap include/linux/fs.h:2405 [inline] mmap_file mm/internal.h:167 [inline] __mmap_new_file_vma mm/vma.c:2413 [inline] __mmap_new_vma mm/vma.c:2476 [inline] __mmap_region+0xea5/0x2250 mm/vma.c:2670 mmap_region+0x267/0x350 mm/vma.c:2740 do_mmap+0x769/0xe50 mm/mmap.c:558 vm_mmap_pgoff+0x1e1/0x330 mm/util.c:581 ksys_mmap_pgoff+0x35d/0x4b0 mm/mmap.c:604 __do_sys_mmap arch/x86/kernel/sys_x86_64.c:89 [inline] __se_sys_mmap arch/x86/kernel/sys_x86_64.c:82 [inline] __x64_sys_mmap+0x116/0x180 arch/x86/kernel/sys_x86_64.c:82 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xac/0x2a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f4a5add3b9d Code: 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffe1d5a4a68 EFLAGS: 00000246 ORIG_RAX: 0000000000000009 RAX: ffffffffffffffda RBX: 00007f4a5b029fa0 RCX: 00007f4a5add3b9d RDX: 000000000100000b RSI: 0000000000001000 RDI: 0000200000186000 RBP: 00007f4a5ae5700a R08: 0000000000000004 R09: 0000000000000000 R10: 0000000000000013 R11: 0000000000000246 R12: 0000000000000000 R13: 0000000000000000 R14: 0000000000001067 R15: 00007f4a5b029fa0 </TASK> ---[ end trace 0000000000000000 ]---
Syzkaller reproducer:
# {Threaded:false Repeat:false RepeatTimes:0 Procs:1 Slowdown:1 Sandbox: SandboxArg:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false NicVF:false USB:false VhciInjection:false Wifi:false IEEE802154:false Sysctl:false Swap:false UseTmpDir:false HandleSegv:true Trace:false CallComments:true LegacyOptions:{Collide:false Fault:false FaultCall:0 FaultNth:0}} pkey_mprotect(&(0x7f0000000000/0x2000)=nil, 0x2000, 0x5, 0xffffffffffffffff)
r0 = perf_event_open(&(0x7f0000000000)={0x2, 0x80, 0x8, 0x1, 0x8, 0x1, 0x0, 0x2, 0x84143, 0x10, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x1, 0x1, 0x0, 0x0, 0x1, 0x0, 0x1, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7fff, 0x2, @perf_config_ext={0x29a, 0x8}, 0x1800, 0x7, 0x10000, 0x1, 0x4, 0xffffff7f, 0xfffe, 0x0, 0x8000003, 0x0, 0x7}, 0x0, 0x1, 0xffffffffffffffff, 0x8) mmap(&(0x7f0000002000/0x1000)=nil, 0x1000, 0x0, 0x11, r0, 0x0)
r1 = perf_event_open(&(0x7f0000000000)={0x2, 0x80, 0x8, 0x1, 0x8, 0x1, 0x0, 0x2, 0x84143, 0x10, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x1, 0x1, 0x0, 0x0, 0x1, 0x0, 0x1, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7fff, 0x2, @perf_config_ext={0x29a, 0x8}, 0x1800, 0x7, 0x10000, 0x1, 0x4, 0xffffff7f, 0xfffe, 0x0, 0x8000003, 0x0, 0x7}, 0x0, 0x1, r0, 0x2) mmap(&(0x7f0000186000/0x1000)=nil, 0x1000, 0x100000b, 0x13, r1, 0x0)
C reproducer:
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef __NR_pkey_mprotect
#define __NR_pkey_mprotect 329
#endif
static __thread int clone_ongoing;
static __thread int skip_segv;
static __thread jmp_buf segv_env;
static void segv_handler(int sig, siginfo_t* info, void* ctx) {
if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) {
exit(sig);
}
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
int valid = addr < prog_start || addr > prog_end;
if (skip && valid) {
_longjmp(segv_env, 1); }
exit(sig);
}
static void install_segv_handler(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#define NONFAILING(...) \
({ \
int ok = 1; \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} else \
ok = 0; \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
ok; \})
#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
*(type*)(addr) = \
htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
(((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
/*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
const char* reason;
(void)reason;
install_segv_handler();
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
}
// pkey_mprotect arguments: [
// addr: VMA[0x2000]
// len: len = 0x2000 (8 bytes)
// prot: mmap_prot = 0x5 (8 bytes)
// key: pkey (resource)
// ]
syscall(__NR_pkey_mprotect, /*addr=*/0x200000000000ul, /*len=*/0x2000ul,
/*prot=PROT_READ|PROT_EXEC*/ 5ul, /*key=*/(intptr_t)-1);
// perf_event_open arguments: [
// attr: ptr[in, perf_event_attr] {
// perf_event_attr {
// type: perf_event_type = 0x2 (4 bytes)
// size: len = 0x80 (4 bytes)
// config0: int8 = 0x8 (1 bytes)
// config1: int8 = 0x1 (1 bytes)
// config2: int8 = 0x8 (1 bytes)
// config3: int8 = 0x1 (1 bytes)
// config4: const = 0x0 (4 bytes)
// sample_freq: int64 = 0x2 (8 bytes)
// sample_type: perf_sample_type = 0x84143 (8 bytes)
// read_format: perf_read_format = 0x10 (8 bytes)
// disabled: int64 = 0x1 (0 bytes)
// inherit: int64 = 0x1 (0 bytes)
// pinned: int64 = 0x1 (0 bytes)
// exclusive: int64 = 0x0 (0 bytes)
// exclude_user: int64 = 0x0 (0 bytes)
// exclude_kernel: int64 = 0x1 (0 bytes)
// exclude_hv: int64 = 0x1 (0 bytes)
// exclude_idle: int64 = 0x1 (0 bytes)
// mmap: int64 = 0x0 (0 bytes)
// comm: int64 = 0x1 (0 bytes)
// freq: int64 = 0x0 (0 bytes)
// inherit_stat: int64 = 0x0 (0 bytes)
// enable_on_exec: int64 = 0x0 (0 bytes)
// task: int64 = 0x1 (0 bytes)
// watermark: int64 = 0x0 (0 bytes)
// precise_ip: int64 = 0x3 (0 bytes)
// mmap_data: int64 = 0x1 (0 bytes)
// sample_id_all: int64 = 0x1 (0 bytes)
// exclude_host: int64 = 0x0 (0 bytes)
// exclude_guest: int64 = 0x0 (0 bytes)
// exclude_callchain_kernel: int64 = 0x1 (0 bytes)
// exclude_callchain_user: int64 = 0x0 (0 bytes)
// mmap2: int64 = 0x1 (0 bytes)
// comm_exec: int64 = 0x1 (0 bytes)
// use_clockid: int64 = 0x0 (0 bytes)
// context_switch: int64 = 0x0 (0 bytes)
// write_backward: int64 = 0x1 (0 bytes)
// namespaces: int64 = 0x0 (0 bytes)
// ksymbol: int64 = 0x0 (0 bytes)
// bpf_event: int64 = 0x1 (0 bytes)
// aux_output: int64 = 0x0 (0 bytes)
// cgroup: int64 = 0x0 (0 bytes)
// text_poke: int64 = 0x0 (0 bytes)
// build_id: int64 = 0x0 (0 bytes)
// inherit_thread: int64 = 0x1 (0 bytes)
// remove_on_exec: int64 = 0x0 (0 bytes)
// sigtrap: int64 = 0x0 (0 bytes)
// __reserved_1: const = 0x0 (8 bytes)
// wakeup_events: int32 = 0x7fff (4 bytes)
// bp_type: perf_bp_type = 0x2 (4 bytes)
// bp_config: union perf_bp_config {
// perf_config_ext: perf_config_ext {
// config1: int64 = 0x29a (8 bytes)
// config2: int64 = 0x8 (8 bytes)
// }
// }
// branch_sample_type: perf_branch_sample_type = 0x1800 (8 bytes)
// sample_regs_user: int64 = 0x7 (8 bytes)
// sample_stack_user: int32 = 0x10000 (4 bytes)
// clockid: clock_type = 0x1 (4 bytes)
// sample_regs_intr: int64 = 0x4 (8 bytes)
// aux_watermark: int32 = 0xffffff7f (4 bytes)
// sample_max_stack: int16 = 0xfffe (2 bytes)
// __reserved_2: const = 0x0 (2 bytes)
// aux_sample_size: int32 = 0x8000003 (4 bytes)
// __reserved_3: const = 0x0 (4 bytes)
// sig_data: int64 = 0x7 (8 bytes)
// }
// }
// pid: pid (resource)
// cpu: intptr = 0x1 (8 bytes)
// group: fd_perf (resource)
// flags: perf_flags = 0x8 (8 bytes)
// ]
// returns fd_perf
NONFAILING(*(uint32_t*)0x200000000000 = 2);
NONFAILING(*(uint32_t*)0x200000000004 = 0x80);
NONFAILING(*(uint8_t*)0x200000000008 = 8);
NONFAILING(*(uint8_t*)0x200000000009 = 1);
NONFAILING(*(uint8_t*)0x20000000000a = 8);
NONFAILING(*(uint8_t*)0x20000000000b = 1);
NONFAILING(*(uint32_t*)0x20000000000c = 0);
NONFAILING(*(uint64_t*)0x200000000010 = 2);
NONFAILING(*(uint64_t*)0x200000000018 = 0x84143);
NONFAILING(*(uint64_t*)0x200000000020 = 0x10);
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 0, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 1, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 2, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 3, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 4, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 5, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 6, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 7, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 8, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 9, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 10, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 11, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 12, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 13, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 14, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 3, 15, 2));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 17, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 18, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 19, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 20, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 21, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 22, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 23, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 24, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 25, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 26, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 27, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 28, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 29, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 30, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 31, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 32, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 33, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 34, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 35, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 36, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 37, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 38, 26));
NONFAILING(*(uint32_t*)0x200000000030 = 0x7fff);
NONFAILING(*(uint32_t*)0x200000000034 = 2);
NONFAILING(*(uint64_t*)0x200000000038 = 0x29a);
NONFAILING(*(uint64_t*)0x200000000040 = 8);
NONFAILING(*(uint64_t*)0x200000000048 = 0x1800);
NONFAILING(*(uint64_t*)0x200000000050 = 7);
NONFAILING(*(uint32_t*)0x200000000058 = 0x10000);
NONFAILING(*(uint32_t*)0x20000000005c = 1);
NONFAILING(*(uint64_t*)0x200000000060 = 4);
NONFAILING(*(uint32_t*)0x200000000068 = 0xffffff7f);
NONFAILING(*(uint16_t*)0x20000000006c = 0xfffe);
NONFAILING(*(uint16_t*)0x20000000006e = 0);
NONFAILING(*(uint32_t*)0x200000000070 = 0x8000003);
NONFAILING(*(uint32_t*)0x200000000074 = 0);
NONFAILING(*(uint64_t*)0x200000000078 = 7);
res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/(intptr_t)-1,
/*flags=PERF_FLAG_FD_CLOEXEC*/ 8ul);
if (res != -1)
r[0] = res;
// mmap arguments: [
// addr: VMA[0x1000]
// len: len = 0x1000 (8 bytes)
// prot: mmap_prot = 0x0 (8 bytes)
// flags: mmap_flags = 0x11 (8 bytes)
// fd: fd (resource)
// offset: intptr = 0x0 (8 bytes)
// ]
syscall(__NR_mmap, /*addr=*/0x200000002000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[0], /*offset=*/0ul);
// perf_event_open arguments: [
// attr: ptr[in, perf_event_attr] {
// perf_event_attr {
// type: perf_event_type = 0x2 (4 bytes)
// size: len = 0x80 (4 bytes)
// config0: int8 = 0x8 (1 bytes)
// config1: int8 = 0x1 (1 bytes)
// config2: int8 = 0x8 (1 bytes)
// config3: int8 = 0x1 (1 bytes)
// config4: const = 0x0 (4 bytes)
// sample_freq: int64 = 0x2 (8 bytes)
// sample_type: perf_sample_type = 0x84143 (8 bytes)
// read_format: perf_read_format = 0x10 (8 bytes)
// disabled: int64 = 0x1 (0 bytes)
// inherit: int64 = 0x1 (0 bytes)
// pinned: int64 = 0x1 (0 bytes)
// exclusive: int64 = 0x0 (0 bytes)
// exclude_user: int64 = 0x0 (0 bytes)
// exclude_kernel: int64 = 0x1 (0 bytes)
// exclude_hv: int64 = 0x1 (0 bytes)
// exclude_idle: int64 = 0x1 (0 bytes)
// mmap: int64 = 0x0 (0 bytes)
// comm: int64 = 0x1 (0 bytes)
// freq: int64 = 0x0 (0 bytes)
// inherit_stat: int64 = 0x0 (0 bytes)
// enable_on_exec: int64 = 0x0 (0 bytes)
// task: int64 = 0x1 (0 bytes)
// watermark: int64 = 0x0 (0 bytes)
// precise_ip: int64 = 0x3 (0 bytes)
// mmap_data: int64 = 0x1 (0 bytes)
// sample_id_all: int64 = 0x1 (0 bytes)
// exclude_host: int64 = 0x0 (0 bytes)
// exclude_guest: int64 = 0x0 (0 bytes)
// exclude_callchain_kernel: int64 = 0x1 (0 bytes)
// exclude_callchain_user: int64 = 0x0 (0 bytes)
// mmap2: int64 = 0x1 (0 bytes)
// comm_exec: int64 = 0x1 (0 bytes)
// use_clockid: int64 = 0x0 (0 bytes)
// context_switch: int64 = 0x0 (0 bytes)
// write_backward: int64 = 0x1 (0 bytes)
// namespaces: int64 = 0x0 (0 bytes)
// ksymbol: int64 = 0x0 (0 bytes)
// bpf_event: int64 = 0x1 (0 bytes)
// aux_output: int64 = 0x0 (0 bytes)
// cgroup: int64 = 0x0 (0 bytes)
// text_poke: int64 = 0x0 (0 bytes)
// build_id: int64 = 0x0 (0 bytes)
// inherit_thread: int64 = 0x1 (0 bytes)
// remove_on_exec: int64 = 0x0 (0 bytes)
// sigtrap: int64 = 0x0 (0 bytes)
// __reserved_1: const = 0x0 (8 bytes)
// wakeup_events: int32 = 0x7fff (4 bytes)
// bp_type: perf_bp_type = 0x2 (4 bytes)
// bp_config: union perf_bp_config {
// perf_config_ext: perf_config_ext {
// config1: int64 = 0x29a (8 bytes)
// config2: int64 = 0x8 (8 bytes)
// }
// }
// branch_sample_type: perf_branch_sample_type = 0x1800 (8 bytes)
// sample_regs_user: int64 = 0x7 (8 bytes)
// sample_stack_user: int32 = 0x10000 (4 bytes)
// clockid: clock_type = 0x1 (4 bytes)
// sample_regs_intr: int64 = 0x4 (8 bytes)
// aux_watermark: int32 = 0xffffff7f (4 bytes)
// sample_max_stack: int16 = 0xfffe (2 bytes)
// __reserved_2: const = 0x0 (2 bytes)
// aux_sample_size: int32 = 0x8000003 (4 bytes)
// __reserved_3: const = 0x0 (4 bytes)
// sig_data: int64 = 0x7 (8 bytes)
// }
// }
// pid: pid (resource)
// cpu: intptr = 0x1 (8 bytes)
// group: fd_perf (resource)
// flags: perf_flags = 0x2 (8 bytes)
// ]
// returns fd_perf
NONFAILING(*(uint32_t*)0x200000000000 = 2);
NONFAILING(*(uint32_t*)0x200000000004 = 0x80);
NONFAILING(*(uint8_t*)0x200000000008 = 8);
NONFAILING(*(uint8_t*)0x200000000009 = 1);
NONFAILING(*(uint8_t*)0x20000000000a = 8);
NONFAILING(*(uint8_t*)0x20000000000b = 1);
NONFAILING(*(uint32_t*)0x20000000000c = 0);
NONFAILING(*(uint64_t*)0x200000000010 = 2);
NONFAILING(*(uint64_t*)0x200000000018 = 0x84143);
NONFAILING(*(uint64_t*)0x200000000020 = 0x10);
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 0, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 1, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 2, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 3, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 4, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 5, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 6, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 7, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 8, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 9, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 10, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 11, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 12, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 13, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 14, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 3, 15, 2));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 17, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 18, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 19, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 20, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 21, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 22, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 23, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 24, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 25, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 26, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 27, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 28, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 29, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 30, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 31, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 32, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 33, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 34, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 1, 35, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 36, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 37, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000028, 0, 38, 26));
NONFAILING(*(uint32_t*)0x200000000030 = 0x7fff);
NONFAILING(*(uint32_t*)0x200000000034 = 2);
NONFAILING(*(uint64_t*)0x200000000038 = 0x29a);
NONFAILING(*(uint64_t*)0x200000000040 = 8);
NONFAILING(*(uint64_t*)0x200000000048 = 0x1800);
NONFAILING(*(uint64_t*)0x200000000050 = 7);
NONFAILING(*(uint32_t*)0x200000000058 = 0x10000);
NONFAILING(*(uint32_t*)0x20000000005c = 1);
NONFAILING(*(uint64_t*)0x200000000060 = 4);
NONFAILING(*(uint32_t*)0x200000000068 = 0xffffff7f);
NONFAILING(*(uint16_t*)0x20000000006c = 0xfffe);
NONFAILING(*(uint16_t*)0x20000000006e = 0);
NONFAILING(*(uint32_t*)0x200000000070 = 0x8000003);
NONFAILING(*(uint32_t*)0x200000000074 = 0);
NONFAILING(*(uint64_t*)0x200000000078 = 7);
res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/r[0], /*flags=PERF_FLAG_FD_OUTPUT*/ 2ul);
if (res != -1)
r[1] = res;
// mmap arguments: [
// addr: VMA[0x1000]
// len: len = 0x1000 (8 bytes)
// prot: mmap_prot = 0x100000b (8 bytes)
// flags: mmap_flags = 0x13 (8 bytes)
// fd: fd (resource)
// offset: intptr = 0x0 (8 bytes)
// ]
syscall(__NR_mmap, /*addr=*/0x200000186000ul, /*len=*/0x1000ul,
/*prot=PROT_GROWSDOWN|PROT_SEM|PROT_WRITE|PROT_READ*/ 0x100000bul,
/*flags=MAP_SHARED_VALIDATE|MAP_FIXED*/ 0x13ul, /*fd=*/r[1],
/*offset=*/0ul); return 0;
}
From 34545a4d43adef3147e0ba1c744deb128a05a101 Mon Sep 17 00:00:00 2001
From: 0ne1r0s <[email protected]>
Date: Sat, 31 Jan 2026 21:16:52 +0800
Subject: [PATCH] perf/core: Fix refcount bug and potential UAF in perf_mmap
The issue is caused by a race condition between mmap() and event teardown. In perf_mmap(), the ring_buffer (rb) is accessed via map_range() after the mmap_mutex is released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation when map_range() or subsequent logic attempts to use it.
Fix this by extending the scope of mmap_mutex to cover the entire setup process, including map_range(), ensuring the buffer remains valid until the mapping is complete.
--- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
kernel/events/core.c +21 -21
49 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 78 lines · 余昊铖
From
34545a4d43adef3147e0ba1c744deb128a05a101Mon Sep 17 00:00:00 2001
From: 0ne1r0s <[email protected]>
Date: Sat, 31 Jan 2026 21:16:52 +0800
Subject: [PATCH] perf/core: Fix refcount bug and potential UAF inperf_mmapThe issue is caused by a race condition between
mmap()and event teardown. Inperf_mmap(), thering_buffer(rb) is accessed viamap_range()after themmap_mutexis released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation whenmap_range()or subsequent logic attempts to use it.Fix this by extending the scope of
mmap_mutexto cover the entire setup process, includingmap_range(), ensuring the buffer remains valid until the mapping is complete.Signed-off-by 0ne1r0s <[email protected]>--- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)kernel/events/core.c +21
-21@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } - -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); - -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; + + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); + +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }Signature · 1 line
2.51.0
Can you turn this into a patch we can apply (properly sent, real name used, etc.) so that the maintainers can review it and apply it correctly?
Also, be sure to send this to the correct people, I don't think that the ext4 developers care that much about perf :)
thanks,
greg k-h
PATCH 1/1 perf/core: Fix refcount bug and potential UAF in perf_mmap
The issue is caused by a race condition between mmap() and event teardown. In perf_mmap(), the ring_buffer (rb) is accessed via map_range() after the mmap_mutex is released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation when map_range() or subsequent logic attempts to use it.
Fix this by extending the scope of mmap_mutex to cover the entire setup process, including map_range(), ensuring the buffer remains valid until the mapping is complete.
--- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
kernel/events/core.c +21 -21
@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } - -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); - -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; + + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); + +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }
Signature · 1 line
2.51.0
Quoted 58 lines · Haocheng Yu
The issue is caused by a race condition between
mmap()and event teardown. Inperf_mmap(), thering_buffer(rb) is accessed viamap_range()after themmap_mutexis released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation whenmap_range()or subsequent logic attempts to use it.Fix this by extending the scope of
mmap_mutexto cover the entire setup process, includingmap_range(), ensuring the buffer remains valid until the mapping is complete.Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)kernel/events/core.c +11
-21@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } - -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); - -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; + + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); +
kernel/events/core.c static int perf_mmap(struct file *file, struct vm_area_struct *vma) · line 7191
Quoted 10 lines
kernel/events/core.c +10
-0+/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }
This indentation looks very odd, are you sure it is correct?
thanks,
greg k-h
PATCH 1/1 perf/core: Fix refcount bug and potential UAF in perf_mmap
Syzkaller reported a refcount_t: addition on 0; use-after-free warning in perf_mmap.
The issue is caused by a race condition between mmap() and event teardown. In perf_mmap(), the ring_buffer (rb) is accessed via map_range() after the mmap_mutex is released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation when map_range() or subsequent logic attempts to use it.
Fix this by extending the scope of mmap_mutex to cover the entire setup process, including map_range(), ensuring the buffer remains valid until the mapping is complete.
--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/events/core.c +19 -19
@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }
base-commit: 7d0a66e4bb9081d75c82ec4957c50034cb0ea449
Signature · 1 line
2.51.0
Peter Zijlstra
maintainerQuoted 10 lines · Haocheng Yu
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between
mmap()and event teardown. Inperf_mmap(), thering_buffer(rb) is accessed viamap_range()after themmap_mutexis released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation whenmap_range()or subsequent logic attempts to use it.
So you're saying this is something like:
Thread-1 Thread-2 mmap(fd) close(fd) / ioctl(fd, IOC_SET_OUTPUT)
I don't think close() is possible, because mmap() should have a reference on the struct file from fget(), no?
That leaves the ioctl(), let me go have a peek.
Peter Zijlstra
maintainerQuoted 20 lines · Peter Zijlstra
Quoted 10 lines · Haocheng Yu
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between
mmap()and event teardown. Inperf_mmap(), thering_buffer(rb) is accessed viamap_range()after themmap_mutexis released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation whenmap_range()or subsequent logic attempts to use it.So you're saying this is something like:
Thread-1 Thread-2 mmap(fd) close(fd) / ioctl(fd, IOC_SET_OUTPUT)
Quoted 4 lines
I don't think
close()is possible, becausemmap()should have a reference on the struct file fromfget(), no?That leaves the
ioctl(), let me go have a peek.
I'm not seeing it; once perf_mmap_rb() completes, we should have event->mmap_count != 0, and this the IOC_SET_OUTPUT will fail.
Please provide a better explanation.
Hi Peter,
Thanks for the review. You are right, my previous explanation was inaccurate. The actual race condition occurs between a failing mmap() on one event and a concurrent mmap() on a second event that shares the ring buffer (e.g., via output redirection).
Detailed scenario is as follows, for example:
1. Thread A calls mmap(event_A). It allocates the ring buffer, sets event_A->rb, and initializes refcount to 1. It then drops mmap_mutex.
2. Thread A calls map_range(). Suppose this fails. Thread A then proceeds to the error path and calls perf_mmap_close().
3. Thread B concurrently calls mmap(event_B), where event_B is configured to share event_A's buffer. Thread B acquires event_A->mmap_mutex and sees the valid event_A->rb pointer.
4. The race triggers here: If Thread A's perf_mmap_close() logic decrements the ring buffer's refcount to 0 (releasing it) but the pointer event_A->rb is still visible to Thread B (or was read by Thread B before it was cleared), Thread B triggers the "refcount_t: addition on 0" warning when it attempts to increment the refcount in perf_mmap_rb().
The fix extends the scope of mmap_mutex to cover map_range() and the potential error handling path. This ensures that event->rb is only exposed to other threads after it is fully successfully mapped, or it is cleaned up atomically inside the lock if mapping fails.
I have updated the commit message accordingly.
Thanks,
Haocheng
PATCH v2 1/1 perf/core: Fix refcount bug and potential UAF in perf_mmap
From: Haocheng Yu <[email protected]>
Syzkaller reported a refcount_t: addition on 0; use-after-free warning in perf_mmap.
The issue is caused by a race condition between a failing mmap() setup and a concurrent mmap() on a dependent event (e.g., using output redirection).
In perf_mmap(), the ring_buffer (rb) is allocated and assigned to event->rb with the mmap_mutex held. The mutex is then released to perform map_range().
If map_range() fails, perf_mmap_close() is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the valid event->rb pointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.
Fix this by extending the scope of mmap_mutex to cover the map_range() call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.
--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/events/core.c +19 -19
@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }
base-commit: 7d0a66e4bb9081d75c82ec4957c50034cb0ea449
Signature · 1 line
2.51.0
Peter Zijlstra
maintainerQuoted 21 lines · On Tue, Feb 03, 2026 at 12:20:56AM +0800, [email protected] wrote:
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.
Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.
And you're sure this time? To me it feels bit like talking to an LLM.
I suppose there is nothing wrong with having an LLM process syzkaller output and even have it propose patches, but before you send it out an actual human should get involved and apply critical thinking skills.
Just throwing stuff at a maintainer and hoping he does the thinking for you is not appreciated.
Quoted 64 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449Signature · 1 line
2.51.0
These explanations and patches were indeed generated by LLM, but I considered and reviewed them before sending the email, along with the kernel code and the C reproducer, and modified anything I deemed unreasonable. I believe this patch is meaningful, and there are indeed some issues with the kernel code, and that's why I send it out.
Below is my own thinking:
In the C reproducer, these four system calls are the core to this problem. Specifically, the third syscall takes r0 as an argument(group), establishing a shared ring buffer. The fourth syscall uses an unusual flag combination, which is likely the reason this bug can be triggered.
res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/(intptr_t)-1,
/*flags=PERF_FLAG_FD_CLOEXEC*/ 8ul);
syscall(__NR_mmap, /*addr=*/0x200000002000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[0], /*offset=*/0ul); res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/r[0], /*flags=PERF_FLAG_FD_OUTPUT*/ 2ul);
syscall(__NR_mmap, /*addr=*/0x200000186000ul, /*len=*/0x1000ul,
/*prot=PROT_GROWSDOWN|PROT_SEM|PROT_WRITE|PROT_READ*/ 0x100000bul,
/*flags=MAP_SHARED_VALIDATE|MAP_FIXED*/ 0x13ul, /*fd=*/r[1],
/*offset=*/0ul);The sequence is as follows: r0 enters perf_mmap first. It acquires the mutex, executes perf_mmap_rb, releases the mutex, and then calls map_range. If map_range fails, the function enters perf_mmap_close, which calls ring_buffer_put and drops the refcount to 0.
At this moment, r1 also enters perf_mmap and attempts to attach to r0's ring buffer. Because the mutex is released during the r0's execution of map_range, the second mmap can acquire the mutex and access the rb pointer which is shared with r0 before it is cleared, attempting to increment the refcount on a buffer that is already being destroyed.
So I think simply extend the scope of mutex to let it cover perf_mmap_close could solve this problem.
Quoted 101 lines · On Tue, Feb 03, 2026 at 12:20:56AM +0800, [email protected] wrote:
Quoted 21 lines
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.And you're sure this time? To me it feels bit like talking to an LLM.
I suppose there is nothing wrong with having an LLM process syzkaller output and even have it propose patches, but before you send it out an actual human should get involved and apply critical thinking skills.
Just throwing stuff at a maintainer and hoping he does the thinking for you is not appreciated.
Quoted 64 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449--
2.51.0
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 77de62ad3de3967818c3dbe656b7336ebee461d2 Gitweb: https://git.kernel.org/tip/77de62ad3de3967818c3dbe656b7336ebee461d2 Author: Haocheng Yu <[email protected]> AuthorDate: Tue, 03 Feb 2026 00:20:56 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 23 Feb 2026 11:19:25 +01:00
perf/core: Fix refcount bug and potential UAF in perf_mmap
Syzkaller reported a refcount_t: addition on 0; use-after-free warning in perf_mmap.
The issue is caused by a race condition between a failing mmap() setup and a concurrent mmap() on a dependent event (e.g., using output redirection).
In perf_mmap(), the ring_buffer (rb) is allocated and assigned to event->rb with the mmap_mutex held. The mutex is then released to perform map_range().
If map_range() fails, perf_mmap_close() is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the valid event->rb pointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.
Fix this by extending the scope of mmap_mutex to cover the map_range() call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.
--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/events/core.c +19 -19
@@ -7465,28 +7465,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }
Ian Rogers
reviewerQuoted 22 lines · On Mon, Feb 2, 2026 at 8:30 AM <[email protected]> wrote:
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.
Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.
As perf_mmap_close is now called inside the guarded region, is there potential for self deadlock?
In perf_mmap it is now calling perf_mmap_close holding the event->mmap_mutex:
```
scoped_guard (mutex, &event->mmap_mutex) {
[...]
ret = map_range(event->rb, vma);
if (ret)
perf_mmap_close(vma);
}
```
and in perf_mmap_close the mutex will be taken again:
```
static void perf_mmap_close(struct vm_area_struct *vma)
{
struct perf_event *event = vma->vm_file->private_data;
[...]
if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
goto out_put;
```Thanks,
Ian
Quoted 65 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449--
2.51.0
PATCH v2 1/1 perf/core: Fix refcount bug and potential UAF in perf_mmap
From: Haocheng Yu <[email protected]>
Syzkaller reported a refcount_t: addition on 0; use-after-free warning in perf_mmap.
The issue is caused by a race condition between a failing mmap() setup and a concurrent mmap() on a dependent event (e.g., using output redirection).
In perf_mmap(), the ring_buffer (rb) is allocated and assigned to event->rb with the mmap_mutex held. The mutex is then released to perform map_range().
If map_range() fails, perf_mmap_close() is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the valid event->rb pointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.
Fix this by extending the scope of mmap_mutex to cover the map_range() call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.
v2:
Because expanding the guarded region would cause the event->mmap_mutex to be acquired repeatedly in the perf_mmap_close function, potentially leading to a self deadlock, the original logic of perf_mmap_close was retained, and the mutex-holding logic was modified to obtain the perf_mmap_close_locked function.
--- kernel/events/core.c | 152 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 19 deletions(-)
kernel/events/core.c +133 -19
@@ -6842,6 +6842,120 @@ static void perf_mmap_close(struct vm_area_struct *vma)ring_buffer_put (rb);/* could be last */ } +static void perf_mmap_close_locked (struct vm_area_struct *vma) +{ +struct perf_event *event = vma->vm_file->private_data; +struct perf_event *iter_event; +mapped_f unmapped =get_mapped (event, event_unmapped); +struct perf_buffer *rb =ring_buffer_get (event); +struct user_struct *mmap_user = rb->mmap_user; +int mmap_locked = rb->mmap_locked; +unsigned long size =perf_data_size (rb); +bool detach_rest = false; + +/* FIXIES vs perf_pmu_unregister() */ +if (unmapped) +unmapped (event, vma->vm_mm); + +/* +* The AUX buffer is strictly a sub-buffer, serialize using aux_mutex +* to avoid complications. +*/ +if (rb_has_aux (rb) && vma->vm_pgoff == rb->aux_pgoff && +refcount_dec_and_mutex_lock (&rb->aux_mmap_count, &rb->aux_mutex)) { +/* +* Stop all AUX events that are writing to this buffer, +* so that we can free its AUX pages and corresponding PMU +* data. Note that after rb::aux_mmap_count dropped to zero, +* they won't start any more (see perf_aux_output_begin()). +*/ +perf_pmu_output_stop (event); + +/* now it's safe to free the pages */ +atomic_long_sub (rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); +atomic64_sub (rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); + +/* this has to be the last one */ +rb_free_aux (rb); +WARN_ON_ONCE (refcount_read (&rb->aux_refcount)); + +mutex_unlock (&rb->aux_mutex); + } + +if (refcount_dec_and_test (&rb->mmap_count)) + detach_rest = true; + +if (!refcount_dec_and_test (&event->mmap_count)) + goto out_put; + +ring_buffer_attach (event,NULL ); + +/* If there's still other mmap()s of this buffer, we're done. */ +if (!detach_rest) + goto out_put; + +/* +* No other mmap()s, detach from all other events that might redirect +* into the now unreachable buffer. Somewhat complicated by the +* fact that rb::event_lock otherwise nests inside mmap_mutex. +*/ +again: +rcu_read_lock (); +list_for_each_entry_rcu (iter_event, &rb->event_list, rb_entry) { +if (!atomic_long_inc_not_zero (&iter_event->refcount)) { +/* +* This event is en-route to free_event() which will +* detach it and remove it from the list. +*/ +continue ; + } +rcu_read_unlock (); + +if (iter_event != event) { +mutex_lock (&iter_event->mmap_mutex); +/* +* Check we didn't race with perf_event_set_output() which can +* swizzle the rb from under us while we were waiting to +* acquire mmap_mutex. +* +* If we find a different rb; ignore this event, a next +* iteration will no longer find it on the list. We have to +* still restart the iteration to make sure we're not now +* iterating the wrong list. +*/ +if (iter_event->rb == rb) +ring_buffer_attach (iter_event,NULL ); + +mutex_unlock (&iter_event->mmap_mutex); + } +put_event (iter_event); + +/* +* Restart the iteration; either we're on the wrong list or +* destroyed its integrity by doing a deletion. +*/ + goto again; + } +rcu_read_unlock (); + +/* +* It could be there's still a few 0-ref events on the list; they'll +* get cleaned up by free_event() -- they'll also still have their +* ref on the rb and will free it whenever they are done with it. +* +* Aside from that, this buffer is 'fully' detached and unmapped, +* undo the VM accounting. +*/ + +atomic_long_sub ((size >>PAGE_SHIFT ) +1 - mmap_locked, + &mmap_user->locked_vm); +atomic64_sub (mmap_locked, &vma->vm_mm->pinned_vm); +free_uid (mmap_user); + +out_put: +ring_buffer_put (rb);/* could be last */ +} +static vm_fault_t perf_mmap_pfn_mkwrite (struct vm_fault *vmf) {/* The first page is the user control page, others are read-only. */
@@ -7167,28 +7281,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close_locked (vma); + }return ret; }
base-commit: 7d0a66e4bb9081d75c82ec4957c50034cb0ea449
Signature · 1 line
2.51.0
That makes a lot of sense. It's indeed possible for a self deadlock to occur.
I tried updating my patch by modifying perf_mmap_close to get a perf_mmap_close_locked
function that handles the case where event->mutex is held from start. But this approach
isn't very concise, and I'm not so sure if I changed the original logic for some unexpected reasons.
Nevertheless, releasing the mutex before perf_mmap_close finishes executing might cause the
original race condition issue again, which puts me in a dilemma.
Do you have any suggestions?
Thanks,
Haocheng
Quoted 119 lines · On Mon, Feb 2, 2026 at 8:30 AM <[email protected]> wrote:
Quoted 22 lines
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.As
perf_mmap_closeis now called inside the guarded region, is there potential for self deadlock?In
perf_mmapit is now callingperf_mmap_closeholding theevent->mmap_mutex:``` scoped_guard (mutex, &event->mmap_mutex) { [...] ret = map_range(event->rb, vma); if (ret) perf_mmap_close(vma); } ``` and in perf_mmap_close the mutex will be taken again: ``` static void perf_mmap_close(struct vm_area_struct *vma) { struct perf_event *event = vma->vm_file->private_data; [...] if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) goto out_put; ```Thanks,
IanQuoted 65 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449--
2.51.0
Ian Rogers
reviewerQuoted 13 lines · Haocheng Yu
That makes a lot of sense. It's indeed possible for a self deadlock to occur.
I tried updating my patch by modifying
perf_mmap_closeto get aperf_mmap_close_locked
function that handles the case whereevent->mutexis held from start. But this approach
isn't very concise, and I'm not so sure if I changed the original logic for some unexpected reasons.
Nevertheless, releasing the mutex beforeperf_mmap_closefinishes executing might cause the
original race condition issue again, which puts me in a dilemma.
Quoted 1 line
Do you have any suggestions?
With the:
``` + if (ret) + perf_mmap_close_locked(vma); ```
Wouldn't moving it outside the "scoped_guard(mutex, &event->mmap_mutex)" be a fix?
Thanks,
Ian
Quoted 124 lines
Thanks,
HaochengQuoted 119 lines · On Mon, Feb 2, 2026 at 8:30 AM <[email protected]> wrote:
Quoted 22 lines
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.As
perf_mmap_closeis now called inside the guarded region, is there potential for self deadlock?In
perf_mmapit is now callingperf_mmap_closeholding theevent->mmap_mutex:``` scoped_guard (mutex, &event->mmap_mutex) { [...] ret = map_range(event->rb, vma); if (ret) perf_mmap_close(vma); } ``` and in perf_mmap_close the mutex will be taken again: ``` static void perf_mmap_close(struct vm_area_struct *vma) { struct perf_event *event = vma->vm_file->private_data; [...] if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) goto out_put; ```Thanks,
IanQuoted 65 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449--
2.51.0
But if it is moved out, other events might be able to hold the mutex after the current event finishes executing refcount_dec but before releasing rb, thus causing the race condition I mentioned in the patch.
The following is a more detailed analysis:
In the C reproducer, these four system calls are central to this problem. Specifically, the third syscall takes r0 as an argument(group), establishing a shared ring buffer. The fourth syscall uses an unusual flag combination, which is likely the reason this bug can be triggered.
res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/(intptr_t)-1,
/*flags=PERF_FLAG_FD_CLOEXEC*/ 8ul);
syscall(__NR_mmap, /*addr=*/0x200000002000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[0], /*offset=*/0ul); res = syscall(__NR_perf_event_open, /*attr=*/0x200000000000ul, /*pid=*/0,
/*cpu=*/1ul, /*group=*/r[0], /*flags=PERF_FLAG_FD_OUTPUT*/ 2ul);
syscall(__NR_mmap, /*addr=*/0x200000186000ul, /*len=*/0x1000ul,
/*prot=PROT_GROWSDOWN|PROT_SEM|PROT_WRITE|PROT_READ*/ 0x100000bul,
/*flags=MAP_SHARED_VALIDATE|MAP_FIXED*/ 0x13ul, /*fd=*/r[1],
/*offset=*/0ul);And that's what happened: r0 enters perf_mmap first. It acquires the mutex, executes perf_mmap_rb, releases the mutex, and then calls map_range. If map_range fails, the function enters perf_mmap_close, which drops the refcount to 0 and finally releases the rb.
At an exact moment(after decreasing refcount but before releasing rb), r1 also enters perf_mmap and attempts to attach to r0's ring buffer. Because the mutex is released, the second mmap can acquire the mutex and access the rb pointer which is shared with r0 before it is cleared, attempting to increment the refcount
on a buffer that is already being destroyed.
Quoted 151 lines · Haocheng Yu
Quoted 13 lines
That makes a lot of sense. It's indeed possible for a self deadlock to occur.
I tried updating my patch by modifying
perf_mmap_closeto get aperf_mmap_close_locked
function that handles the case whereevent->mutexis held from start. But this approach
isn't very concise, and I'm not so sure if I changed the original logic for some unexpected reasons.
Nevertheless, releasing the mutex beforeperf_mmap_closefinishes executing might cause the
original race condition issue again, which puts me in a dilemma.Quoted 1 line
Do you have any suggestions?
With the:
```(quoted diff) +2
-0+ if (ret) + perf_mmap_close_locked(vma);```
Wouldn't moving it outside the "scoped_guard(mutex, &event->mmap_mutex)" be a fix?Thanks,
IanQuoted 124 lines
Thanks,
HaochengQuoted 119 lines · On Mon, Feb 2, 2026 at 8:30 AM <[email protected]> wrote:
Quoted 22 lines
From: Haocheng Yu <[email protected]>
Syzkaller reported a
refcount_t: addition on 0; use-after-free warning inperf_mmap.The issue is caused by a race condition between a failing
mmap()setup and a concurrentmmap()on a dependent event (e.g., using output redirection).In
perf_mmap(), thering_buffer(rb) is allocated and assigned toevent->rbwith themmap_mutexheld. The mutex is then released to performmap_range().If
map_range()fails,perf_mmap_close()is called to clean up. However, since the mutex was dropped, another thread attaching to this event (via inherited events or output redirection) can acquire the mutex, observe the validevent->rbpointer, and attempt to increment its reference count. If the cleanup path has already dropped the reference count to zero, this results in a use-after-free or refcount saturation warning.Quoted 5 lines
Fix this by extending the scope of
mmap_mutexto cover themap_range()call. This ensures that the ring buffer initialization and mapping (or cleanup on failure) happens atomically effectively, preventing other threads from accessing a half-initialized or dying ring buffer.As
perf_mmap_closeis now called inside the guarded region, is there potential for self deadlock?In
perf_mmapit is now callingperf_mmap_closeholding theevent->mmap_mutex:``` scoped_guard (mutex, &event->mmap_mutex) { [...] ret = map_range(event->rb, vma); if (ret) perf_mmap_close(vma); } ``` and in perf_mmap_close the mutex will be taken again: ``` static void perf_mmap_close(struct vm_area_struct *vma) { struct perf_event *event = vma->vm_file->private_data; [...] if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) goto out_put; ```Thanks,
IanQuoted 65 lines
Reported-by kernel test robot <[email protected]>Signed-off-by Haocheng Yu <[email protected]>--- kernel/events/core.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)kernel/events/core.c +19
-19@@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) ret =perf_mmap_aux (vma, event, nr_pages);if (ret)return ret; - } -/* -* Since pinned accounting is per vm we cannot allow fork() to copy our -* vma. -*/ -vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); - vma->vm_ops = &perf_mmap_vmops; +/* +* Since pinned accounting is per vm we cannot allow fork() to copy our +* vma. +*/ +vm_flags_set (vma,VM_DONTCOPY |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &perf_mmap_vmops; - mapped =get_mapped (event, event_mapped); -if (mapped) -mapped (event, vma->vm_mm); + mapped =get_mapped (event, event_mapped); +if (mapped) +mapped (event, vma->vm_mm); -/* -* Try to map it into the page table. On fail, invoke -* perf_mmap_close() to undo the above, as the callsite expects -* full cleanup in this case and therefore does not invoke -* vmops::close(). -*/ - ret =map_range (event->rb, vma); -if (ret) -perf_mmap_close (vma); +/* +* Try to map it into the page table. On fail, invoke +* perf_mmap_close() to undo the above, as the callsite expects +* full cleanup in this case and therefore does not invoke +* vmops::close(). +*/ + ret =map_range (event->rb, vma); +if (ret) +perf_mmap_close (vma); + }return ret; }base-commit:
7d0a66e4bb9081d75c82ec4957c50034cb0ea449--
2.51.0
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Haocheng,
kernel test robot noticed the following build warnings:
[auto build test WARNING on perf-tools-next/perf-tools-next]
[also build test WARNING on tip/perf/core perf-tools/perf-tools linus/master v6.19-rc7 next-20260130] [cannot apply to acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Haocheng-Yu/perf-core-Fix-refcount-bug-and-potential-UAF-in-perf_mmap/20260201-193746 base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next patch link: https://lore.kernel.org/r/20260201113446.4328-1-yuhaocheng035%40gmail.com patch subject: [PATCH] perf/core: Fix refcount bug and potential UAF in perf_mmap config: mips-randconfig-r072-20260201 (https://download.01.org/0day-ci/archive/20260202/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 9b8addffa70cee5b2acc5454712d9cf78ce45710) smatch version: v0.5.0-8994-gd50c5a4c
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
smatch warnings:kernel/events/core.c:7183 perf_mmap() warn: inconsistent indenting
vim +7183 kernel/events/core.c
7b732a75047738 kernel/perf_counter.c Peter Zijlstra 2009-03-23 7131
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7132 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7133 {
cdd6c482c9ff9c kernel/perf_event.c Ingo Molnar 2009-09-21 7134 struct perf_event *event = file->private_data;
81e026ca47b386 kernel/events/core.c Thomas Gleixner 2025-08-12 7135 unsigned long vma_size, nr_pages;
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7136 mapped_f mapped;
5d299897f1e360 kernel/events/core.c Peter Zijlstra 2025-08-12 7137 int ret;
d57e34fdd60be7 kernel/perf_event.c Peter Zijlstra 2010-05-28 7138
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7139 /*
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7140 * Don't allow mmap() of inherited per-task counters. This would
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7141 * create a performance issue due to all children writing to the
76369139ceb955 kernel/events/core.c Frederic Weisbecker 2011-05-19 7142 * same rb.
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7143 */
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7144 if (event->cpu == -1 && event->attr.inherit)
c7920614cebbf2 kernel/perf_event.c Peter Zijlstra 2010-05-18 7145 return -EINVAL;
4ec8363dfc1451 kernel/events/core.c Vince Weaver 2011-06-01 7146
43a21ea81a2400 kernel/perf_counter.c Peter Zijlstra 2009-03-25 7147 if (!(vma->vm_flags & VM_SHARED))
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7148 return -EINVAL;
26cb63ad11e040 kernel/events/core.c Peter Zijlstra 2013-05-28 7149
da97e18458fb42 kernel/events/core.c Joel Fernandes (Google 2019-10-14 7150) ret = security_perf_event_read(event);
da97e18458fb42 kernel/events/core.c Joel Fernandes (Google 2019-10-14 7151) if (ret)
da97e18458fb42 kernel/events/core.c Joel Fernandes (Google 2019-10-14 7152) return ret;
26cb63ad11e040 kernel/events/core.c Peter Zijlstra 2013-05-28 7153
7b732a75047738 kernel/perf_counter.c Peter Zijlstra 2009-03-23 7154 vma_size = vma->vm_end - vma->vm_start;
0c8a4e4139adf0 kernel/events/core.c Peter Zijlstra 2024-11-04 7155 nr_pages = vma_size / PAGE_SIZE;
ac9721f3f54b27 kernel/perf_event.c Peter Zijlstra 2010-05-27 7156
0c8a4e4139adf0 kernel/events/core.c Peter Zijlstra 2024-11-04 7157 if (nr_pages > INT_MAX)
0c8a4e4139adf0 kernel/events/core.c Peter Zijlstra 2024-11-04 7158 return -ENOMEM;
9a0f05cb368885 kernel/events/core.c Peter Zijlstra 2011-11-21 7159
0c8a4e4139adf0 kernel/events/core.c Peter Zijlstra 2024-11-04 7160 if (vma_size != PAGE_SIZE * nr_pages)
0c8a4e4139adf0 kernel/events/core.c Peter Zijlstra 2024-11-04 7161 return -EINVAL;
45bfb2e50471ab kernel/events/core.c Peter Zijlstra 2015-01-14 7162
d23a6dbc0a7174 kernel/events/core.c Peter Zijlstra 2025-08-12 7163 scoped_guard (mutex, &event->mmap_mutex) {
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7164 /*
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7165 * This relies on __pmu_detach_event() taking mmap_mutex after marking
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7166 * the event REVOKED. Either we observe the state, or __pmu_detach_event()
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7167 * will detach the rb created here.
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7168 */
d23a6dbc0a7174 kernel/events/core.c Peter Zijlstra 2025-08-12 7169 if (event->state <= PERF_EVENT_STATE_REVOKED)
d23a6dbc0a7174 kernel/events/core.c Peter Zijlstra 2025-08-12 7170 return -ENODEV;
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7171
5d299897f1e360 kernel/events/core.c Peter Zijlstra 2025-08-12 7172 if (vma->vm_pgoff == 0)
5d299897f1e360 kernel/events/core.c Peter Zijlstra 2025-08-12 7173 ret = perf_mmap_rb(vma, event, nr_pages);
5d299897f1e360 kernel/events/core.c Peter Zijlstra 2025-08-12 7174 else
2aee3768239133 kernel/events/core.c Peter Zijlstra 2025-08-12 7175 ret = perf_mmap_aux(vma, event, nr_pages);
07091aade394f6 kernel/events/core.c Thomas Gleixner 2025-08-02 7176 if (ret)
07091aade394f6 kernel/events/core.c Thomas Gleixner 2025-08-02 7177 return ret;
07091aade394f6 kernel/events/core.c Thomas Gleixner 2025-08-02 7178
9bb5d40cd93c9d kernel/events/core.c Peter Zijlstra 2013-06-04 7179 /*
9bb5d40cd93c9d kernel/events/core.c Peter Zijlstra 2013-06-04 7180 * Since pinned accounting is per vm we cannot allow fork() to copy our
9bb5d40cd93c9d kernel/events/core.c Peter Zijlstra 2013-06-04 7181 * vma.
9bb5d40cd93c9d kernel/events/core.c Peter Zijlstra 2013-06-04 7182 */
1c71222e5f2393 kernel/events/core.c Suren Baghdasaryan 2023-01-26 @7183 vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP);
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7184 vma->vm_ops = &perf_mmap_vmops;
7b732a75047738 kernel/perf_counter.c Peter Zijlstra 2009-03-23 7185
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7186 mapped = get_mapped(event, event_mapped);
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7187 if (mapped)
da916e96e2dedc kernel/events/core.c Peter Zijlstra 2024-10-25 7188 mapped(event, vma->vm_mm);
1e0fb9ec679c92 kernel/events/core.c Andy Lutomirski 2014-10-24 7189
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7190 /*
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7191 * Try to map it into the page table. On fail, invoke
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7192 * perf_mmap_close() to undo the above, as the callsite expects
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7193 * full cleanup in this case and therefore does not invoke
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7194 * vmops::close().
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7195 */
191759e5ea9f69 kernel/events/core.c Peter Zijlstra 2025-08-12 7196 ret = map_range(event->rb, vma);
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7197 if (ret)
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7198 perf_mmap_close(vma);
8f75f689bf8133 kernel/events/core.c Haocheng Yu 2026-02-01 7199 }
f74b9f4ba63ffd kernel/events/core.c Thomas Gleixner 2025-08-02 7200
7b732a75047738 kernel/perf_counter.c Peter Zijlstra 2009-03-23 7201 return ret;
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7202 }
37d81828385f8f kernel/perf_counter.c Paul Mackerras 2009-03-23 7203 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 10 lines
Can you turn this into a patch we can apply (properly sent, real name used, etc.) so that the maintainers can review it and apply it correctly?
Also, be sure to send this to the correct people, I don't think that the ext4 developers care that much about perf :)
thanks,
greg k-h
Hi Greg,
Sorry for not knowing the rules and sending to the wrong people mistakenly. I have just submitted the formal patch to the perf subsystem maintainers with the correct formatting and real name.
Thanks for the guidance!
Best regards,
Haocheng Yu