[Bug] WARNING in static_key_disable_cpuslocked
From: Sam Sun
Date: Tue Mar 05 2024 - 02:56:32 EST
Dear developers and maintainers,
We encountered a kernel warning while using our modified Syzkaller. It
is tested against linux kernel 6.8-rc7. Kernel config and C repro are
attached to this email. The kernel log is listed below.
```
WARNING: CPU: 0 PID: 8286 at kernel/jump_label.c:229
static_key_disable_cpuslocked+0x15e/0x1c0 kernel/jump_label.c:229
Modules linked in:
CPU: 0 PID: 8286 Comm: syz-executor393 Not tainted 6.7.0-rc7 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
RIP: 0010:static_key_disable_cpuslocked+0x15e/0x1c0 kernel/jump_label.c:229
Code: d3 ff 48 c7 c7 a0 cc 0a 8d e8 5e d3 b6 08 5b 5d e9 f7 bd d3 ff
e8 f2 bd d3 ff 48 89 ef e8 2a f8 ff ff eb d9 e8 e3 bd d3 ff 90 <0f> 0b
90 eb 8f e8 d8 bd d3 ff 90 48 c7 c6 00 74 b7 8a 48 89 ea 48
RSP: 0018:ffffc90002c4faf0 EFLAGS: 00010293
RAX: 0000000000000000 RBX: 00000000ffffffff RCX: ffffffff81b1568e
RDX: ffff888013b5d980 RSI: ffffffff81b156fd RDI: 0000000000000005
RBP: ffffffff8d0c75d0 R08: 0000000000000005 R09: 0000000000000000
R10: 00000000ffffffff R11: 0000000000000000 R12: ffffffff8d0c75d0
R13: ffff8880209fb980 R14: 0000000000000000 R15: ffffffff8211e160
FS: 0000555555e0e3c0(0000) GS:ffff888063a00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f49e210e090 CR3: 000000001a2d4000 CR4: 0000000000750ef0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
PKRU: 55555554
Call Trace:
<TASK>
static_key_disable+0x1a/0x20 kernel/jump_label.c:243
enabled_store+0x288/0x2040 mm/vmscan.c:5137
kobj_attr_store+0x54/0x80 lib/kobject.c:836
sysfs_kf_write+0x114/0x170 fs/sysfs/file.c:136
kernfs_fop_write_iter+0x337/0x500 fs/kernfs/file.c:334
call_write_iter include/linux/fs.h:2020 [inline]
new_sync_write fs/read_write.c:491 [inline]
vfs_write+0x96a/0xd80 fs/read_write.c:584
ksys_write+0x122/0x250 fs/read_write.c:637
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0x40/0x110 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x63/0x6b
RIP: 0033:0x7f49e208a7bd
Code: c3 e8 a7 1f 00 00 0f 1f 80 00 00 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 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffe25660128 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
RAX: ffffffffffffffda RBX: 00007f49e20db078 RCX: 00007f49e208a7bd
RDX: 0000000000000002 RSI: 0000000020000080 RDI: 0000000000000004
RBP: 0000000000000000 R08: 00007f49e20dfb55 R09: 00007f49e20dfb55
R10: 00007f49e20dfb55 R11: 0000000000000246 R12: 00000000000207f3
R13: 00007ffe2566013c R14: 00007ffe25660150 R15: 00007ffe25660140
</TASK>
```
We analyzed the cause of this bug. It seems that in function
static_key_disable_cpuslocked(), there is a small racing window
between two atomic_read(&key->enabled) in line 228 & 229, triggering
the WARN_ON_ONCE macro. This might cause function returned without
actually disabling the static_key "key". I am not sure if there is any
other potential threat here.
I searched on web and found that there was a similar bug reported by
syzbot several years ago
(https://groups.google.com/g/syzkaller-bugs/c/_W3lgRCwlb4/m/TqzyQcPpAQAJ).
At that time the fix was in the net instead of kernel/jump_label.c. So
I checked the call stack and cc this email to memory management
maintainers. Hope there is no confusion.
If you have any questions, please contact us.
Reported by: Yue Sun <samsun1006219@xxxxxxxxx>
Reported by: xingwei lee <xrivendell7@xxxxxxxxx>
Best Regards,
Yue
Attachment:
config
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 unsigned long long procid;
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;
}
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;
}
}
}
uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
void execute_one(void)
{
intptr_t res = 0;
memcpy((void*)0x20000180, "/sys/kernel/mm/lru_gen/enabled\000", 31);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x20000180ul,
/*flags=*/2ul, /*mode=*/0ul);
if (res != -1)
r[0] = res;
memcpy((void*)0x20000200, "2\000", 2);
syscall(__NR_write, /*fd=*/r[0], /*val=*/0x20000200ul, /*len=*/2ul);
memcpy((void*)0x20000000, "/sys/kernel/mm/lru_gen/enabled\000", 31);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x20000000ul,
/*flags=*/2ul, /*mode=*/0ul);
if (res != -1)
r[1] = res;
memcpy((void*)0x20000080, "0\000", 2);
syscall(__NR_write, /*fd=*/r[1], /*val=*/0x20000080ul, /*len=*/2ul);
}
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);
for (procid = 0; procid < 4; procid++) {
if (fork() == 0) {
loop();
}
}
sleep(1000000);
return 0;
}