WARNING in __perf_event_overflow

From: Marius Fleischer
Date: Tue Jun 11 2024 - 16:00:13 EST


Hi,

We would like to report the following bug which has been found by our
modified version of syzkaller.

======================================================
description: WARNING in __perf_event_overflow
affected file: kernel/events/core.c
kernel version: 5.15.159
kernel commit: 83655231580bc07485a4ac2a6c971c3a175dd27d
git tree: upstream
kernel config: attached
crash reproducer: attached
======================================================
Crash log:
WARNING: CPU: 0 PID: 157335 at kernel/events/core.c:9435
__perf_event_overflow+0x4f4/0x5b0 kernel/events/core.c:9435
Modules linked in:
CPU: 0 PID: 157335 Comm: syz-executor.5 Not tainted 5.15.159 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
RIP: 0010:__perf_event_overflow+0x4f4/0x5b0 kernel/events/core.c:9435
Code: b6 44 24 0f 84 c0 0f 84 68 fd ff ff e8 65 66 e1 ff 44 89 fe 44
89 ef e8 4a 5e e1 ff 45 39 fd 0f 84 4f fd ff ff e8 4c 66 e1 ff <0f> 0b
e9 43 fd ff ff c7 04 24 00 00 00 00 e9 6c fe ff ff 4c 89 ef
RSP: 0000:fffffe0000011ab8 EFLAGS: 00010046
RAX: 0000000080110000 RBX: ffff88807e178bd0 RCX: ffffffff81966076
RDX: ffff888032fa0000 RSI: ffffffff81966084 RDI: 0000000000000004
RBP: fffffe0000011bc0 R08: 0000000000000000 R09: ffff88807e178dcf
R10: 000000001787c2f9 R11: 0000000000000001 R12: fffffe0000011ef8
R13: 000000001787c2f9 R14: ffff88807e178ff4 R15: 0000000076bff1cf
FS: 00007f5415deb640(0000) GS:ffff8880b9c00000(0000) knlGS:ffff8880b9c00000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f5415deaf88 CR3: 0000000123ea5000 CR4: 0000000000750ef0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400
PKRU: 55555554
Call Trace:
<NMI>
handle_pmi_common+0x5c9/0xa20 arch/x86/events/intel/core.c:2899
intel_pmu_handle_irq+0x265/0xf30 arch/x86/events/intel/core.c:2960
perf_event_nmi_handler arch/x86/events/core.c:1745 [inline]
perf_event_nmi_handler+0x48/0x70 arch/x86/events/core.c:1731
nmi_handle+0x13d/0x3a0 arch/x86/kernel/nmi.c:140
default_do_nmi+0x6b/0x170 arch/x86/kernel/nmi.c:334
exc_nmi+0xf0/0x120 arch/x86/kernel/nmi.c:510
end_repeat_nmi+0x16/0x31
RIP: 0010:asm_sysvec_irq_work+0x0/0x20 arch/x86/include/asm/idtentry.h:660
Code: e9 a5 03 00 00 0f 1f 44 00 00 0f 01 ca fc 6a ff e8 a5 02 00 00
48 89 c4 48 89 e7 e8 fa 1c f3 ff e9 85 03 00 00 0f 1f 44 00 00 <0f> 01
ca fc 6a ff e8 85 02 00 00 48 89 c4 48 89 e7 e8 ea 21 f3 ff
RSP: 0000:fffffe0000002fd8 EFLAGS: 00000046
RAX: 0000000000000003 RBX: 00007f54179b8f80 RCX: 00007f541787bdad
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000020000080
RBP: 00007f54178dd4a6 R08: 0000000000000008 R09: 0000000000000000
R10: ffffffffffffffff R11: 0000000000000246 R12: 0000000000000000
R13: 000000000000000b R14: 00007f54179b8f80 R15: 00007f5415dcb000
</NMI>
<ENTRY_TRAMPOLINE>
RIP: 0033:0x7f541787bdad
RSP: 002b:00007f5415deb028 EFLAGS: 00000246
</ENTRY_TRAMPOLINE>
----------------
Code disassembly (best guess):
0: e9 a5 03 00 00 jmp 0x3aa
5: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
a: 0f 01 ca clac
d: fc cld
e: 6a ff push $0xffffffffffffffff
10: e8 a5 02 00 00 call 0x2ba
15: 48 89 c4 mov %rax,%rsp
18: 48 89 e7 mov %rsp,%rdi
1b: e8 fa 1c f3 ff call 0xfff31d1a
20: e9 85 03 00 00 jmp 0x3aa
25: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
* 2a: 0f 01 ca clac <-- trapping instruction
2d: fc cld
2e: 6a ff push $0xffffffffffffffff
30: e8 85 02 00 00 call 0x2ba
35: 48 89 c4 mov %rax,%rsp
38: 48 89 e7 mov %rsp,%rdi
3b: e8 ea 21 f3 ff call 0xfff3222a
======================================================

Please note that there is a potentially related crash found by syzbot
(https://syzkaller.appspot.com/bug?extid=589d998651a580e6135d) and
patched (https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=bb88f9695460bec25aa30ba9072595025cf6c8af).
This patch seems to be present in the kernel we analyzed.

Please let us know if we should provide any additional information!

Wishing you a nice day!

Best,
Marius

Attachment: repro.syz
Description: Binary data

Attachment: config-5.15.159
Description: Binary data

// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static void sleep_ms(uint64_t ms)
{
usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
exit(1);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

#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))))

static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}

static void kill_and_wait(int pid, int* status)
{
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
for (int i = 0; i < 100; i++) {
if (waitpid(-1, status, WNOHANG | __WALL) == pid)
return;
usleep(1000);
}
DIR* dir = opendir("/sys/fs/fuse/connections");
if (dir) {
for (;;) {
struct dirent* ent = readdir(dir);
if (!ent)
break;
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
char abort[300];
snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
ent->d_name);
int fd = open(abort, O_WRONLY);
if (fd == -1) {
continue;
}
if (write(fd, abort, 1) < 0) {
}
close(fd);
}
closedir(dir);
} else {
}
while (waitpid(-1, status, __WALL) != pid) {
}
}

static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
write_file("/proc/self/oom_score_adj", "1000");
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
int iter = 0;
for (;; iter++) {
int pid = fork();
if (pid < 0)
exit(1);
if (pid == 0) {
setup_test();
execute_one();
exit(0);
}
int status = 0;
uint64_t start = current_time_ms();
for (;;) {
if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
break;
sleep_ms(1);
if (current_time_ms() - start < 5000)
continue;
kill_and_wait(pid, &status);
break;
}
}
}

void execute_one(void)
{
*(uint32_t*)0x20000080 = 0;
*(uint32_t*)0x20000084 = 0x80;
*(uint8_t*)0x20000088 = 0;
*(uint8_t*)0x20000089 = 0;
*(uint8_t*)0x2000008a = 0;
*(uint8_t*)0x2000008b = 0;
*(uint32_t*)0x2000008c = 0;
*(uint64_t*)0x20000090 = 8;
*(uint64_t*)0x20000098 = 0;
*(uint64_t*)0x200000a0 = 0;
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 0, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 1, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 2, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 3, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 4, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 5, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 6, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 7, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 8, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 9, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 10, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 11, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 12, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 13, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 14, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 15, 2);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 17, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 18, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 19, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 20, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 21, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 22, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 23, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 24, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 25, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 26, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 27, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 28, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 29, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 30, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 31, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 32, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 33, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 34, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 35, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 36, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 1, 37, 1);
STORE_BY_BITMASK(uint64_t, , 0x200000a8, 0, 38, 26);
*(uint32_t*)0x200000b0 = 0;
*(uint32_t*)0x200000b4 = 0;
*(uint64_t*)0x200000b8 = 0;
*(uint64_t*)0x200000c0 = 0;
*(uint64_t*)0x200000c8 = 0;
*(uint64_t*)0x200000d0 = 0;
*(uint32_t*)0x200000d8 = 0;
*(uint32_t*)0x200000dc = 0;
*(uint64_t*)0x200000e0 = 0;
*(uint32_t*)0x200000e8 = 0;
*(uint16_t*)0x200000ec = 0;
*(uint16_t*)0x200000ee = 0;
*(uint32_t*)0x200000f0 = 0;
*(uint32_t*)0x200000f4 = 0;
*(uint64_t*)0x200000f8 = 5;
syscall(__NR_perf_event_open, /*attr=*/0x20000080ul, /*pid=*/0, /*cpu=*/0ul,
/*group=*/-1, /*flags=*/8ul);
}
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
loop();
return 0;
}