KASAN: use-after-free Read in __io_remove_buffers

From: Dipanjan Das
Date: Wed Jul 20 2022 - 21:41:50 EST


Hi,

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

======================================================
description: KASAN: use-after-free Read in __io_remove_buffers
affected file: fs/io_uring.c
kernel version: 5.19-rc6
kernel commit: 32346491ddf24599decca06190ebca03ff9de7f8
git tree: upstream
kernel config: https://syzkaller.appspot.com/text?tag=KernelConfig&x=cd73026ceaed1402
crash reproducer: attached
======================================================
Crash log:
======================================================
BUG: KASAN: use-after-free in __io_remove_buffers.isra.0+0x401/0x530
fs/io_uring.c:5540
Read of size 2 at addr ffff88812e562012 by task kworker/u4:9/3708

CPU: 0 PID: 3708 Comm: kworker/u4:9 Tainted: G OE
5.19.0-rc6-g2eae0556bb9d #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Workqueue: events_unbound io_ring_exit_work
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106
print_address_description mm/kasan/report.c:313 [inline]
print_report.cold+0xe5/0x659 mm/kasan/report.c:429
kasan_report+0x8a/0x1b0 mm/kasan/report.c:491
__io_remove_buffers.isra.0+0x401/0x530 fs/io_uring.c:5540
io_destroy_buffers fs/io_uring.c:11077 [inline]
io_ring_ctx_free fs/io_uring.c:11159 [inline]
io_ring_exit_work+0x9a2/0xf4b fs/io_uring.c:11338
process_one_work+0x9cc/0x1650 kernel/workqueue.c:2289
worker_thread+0x623/0x1070 kernel/workqueue.c:2436
kthread+0x2e9/0x3a0 kernel/kthread.c:376
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:302
</TASK>

Allocated by task 10568:
kasan_save_stack+0x1e/0x40 mm/kasan/common.c:39
kasan_set_track mm/kasan/common.c:46 [inline]
set_alloc_info mm/kasan/common.c:441 [inline]
____kasan_kmalloc mm/kasan/common.c:526 [inline]
____kasan_kmalloc mm/kasan/common.c:479 [inline]
__kasan_kmalloc+0xb5/0xe0 mm/kasan/common.c:535
kasan_kmalloc include/linux/kasan.h:234 [inline]
__kmalloc+0x1c9/0x4c0 mm/slub.c:4446
kcalloc include/linux/slab.h:671 [inline]
io_init_bl_list.isra.0+0x24/0x102 fs/io_uring.c:5703
io_register_pbuf_ring fs/io_uring.c:12955 [inline]
__io_uring_register.cold+0xd/0x10b5 fs/io_uring.c:13151
__do_sys_io_uring_register fs/io_uring.c:13187 [inline]
__se_sys_io_uring_register fs/io_uring.c:13167 [inline]
__x64_sys_io_uring_register+0x2f2/0x650 fs/io_uring.c:13167
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x46/0xb0

Freed by task 10568:
kasan_save_stack+0x1e/0x40 mm/kasan/common.c:39
kasan_set_track+0x21/0x30 mm/kasan/common.c:46
kasan_set_free_info+0x20/0x30 mm/kasan/generic.c:378
____kasan_slab_free mm/kasan/common.c:371 [inline]
____kasan_slab_free mm/kasan/common.c:329 [inline]
__kasan_slab_free+0x12b/0x1a0 mm/kasan/common.c:379
kasan_slab_free include/linux/kasan.h:200 [inline]
slab_free_hook mm/slub.c:1754 [inline]
slab_free_freelist_hook mm/slub.c:1780 [inline]
slab_free mm/slub.c:3536 [inline]
kfree+0xec/0x4b0 mm/slub.c:4584
io_register_pbuf_ring fs/io_uring.c:12975 [inline]
__io_uring_register+0x16aa/0x19d0 fs/io_uring.c:13151
__do_sys_io_uring_register fs/io_uring.c:13187 [inline]
__se_sys_io_uring_register fs/io_uring.c:13167 [inline]
__x64_sys_io_uring_register+0x2f2/0x650 fs/io_uring.c:13167
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x46/0xb0

Last potentially related work creation:
kasan_save_stack+0x1e/0x40 mm/kasan/common.c:39
__kasan_record_aux_stack+0xbe/0xd0 mm/kasan/generic.c:356
call_rcu kernel/rcu/tree.c:3126 [inline]
call_rcu+0x99/0x740 kernel/rcu/tree.c:3101
netlink_release+0xe42/0x1cb0 net/netlink/af_netlink.c:815
__sock_release+0xcd/0x280 net/socket.c:650
sock_close+0x18/0x20 net/socket.c:1365
__fput+0x277/0x9d0 fs/file_table.c:317
task_work_run+0xe0/0x1a0 kernel/task_work.c:177
resume_user_mode_work include/linux/resume_user_mode.h:49 [inline]
exit_to_user_mode_loop kernel/entry/common.c:169 [inline]
exit_to_user_mode_prepare+0x253/0x260 kernel/entry/common.c:201
__syscall_exit_to_user_mode_work kernel/entry/common.c:283 [inline]
syscall_exit_to_user_mode+0x19/0x50 kernel/entry/common.c:294
do_syscall_64+0x42/0xb0 arch/x86/entry/common.c:86
entry_SYSCALL_64_after_hwframe+0x46/0xb0

Second to last potentially related work creation:
kasan_save_stack+0x1e/0x40 mm/kasan/common.c:39
__kasan_record_aux_stack+0xbe/0xd0 mm/kasan/generic.c:356
call_rcu kernel/rcu/tree.c:3126 [inline]
call_rcu+0x99/0x740 kernel/rcu/tree.c:3101
netlink_release+0xe42/0x1cb0 net/netlink/af_netlink.c:815
__sock_release+0xcd/0x280 net/socket.c:650
sock_close+0x18/0x20 net/socket.c:1365
__fput+0x277/0x9d0 fs/file_table.c:317
task_work_run+0xe0/0x1a0 kernel/task_work.c:177
exit_task_work include/linux/task_work.h:38 [inline]
do_exit+0xb1e/0x2dd0 kernel/exit.c:797
do_group_exit+0xd2/0x2f0 kernel/exit.c:927
get_signal+0x2842/0x2870 kernel/signal.c:2857
arch_do_signal_or_restart+0x82/0x2270 arch/x86/kernel/signal.c:869
exit_to_user_mode_loop kernel/entry/common.c:166 [inline]
exit_to_user_mode_prepare+0x174/0x260 kernel/entry/common.c:201
__syscall_exit_to_user_mode_work kernel/entry/common.c:283 [inline]
syscall_exit_to_user_mode+0x19/0x50 kernel/entry/common.c:294
do_syscall_64+0x42/0xb0 arch/x86/entry/common.c:86
entry_SYSCALL_64_after_hwframe+0x46/0xb0

The buggy address belongs to the object at ffff88812e562000
which belongs to the cache kmalloc-2k of size 2048
The buggy address is located 18 bytes inside of
2048-byte region [ffff88812e562000, ffff88812e562800)

The buggy address belongs to the physical page:
page:ffffea0004b95800 refcount:1 mapcount:0 mapping:0000000000000000
index:0x0 pfn:0x12e560
head:ffffea0004b95800 order:3 compound_mapcount:0 compound_pincount:0
flags: 0x17ff00000010200(slab|head|node=0|zone=2|lastcpupid=0x7ff)
raw: 017ff00000010200 ffffea0005da9600 dead000000000002 ffff888100042000
raw: 0000000000000000 0000000080080008 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask
0xd28c0(GFP_NOWAIT|__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC),
pid 6706, tgid 6706 (syz-executor.4), ts 116920478716, free_ts 0
set_page_owner include/linux/page_owner.h:31 [inline]
post_alloc_hook mm/page_alloc.c:2449 [inline]
prep_new_page+0x297/0x330 mm/page_alloc.c:2456
get_page_from_freelist+0x2142/0x3c80 mm/page_alloc.c:4198
__alloc_pages+0x321/0x710 mm/page_alloc.c:5426
alloc_pages+0x119/0x250 mm/mempolicy.c:2272
alloc_slab_page mm/slub.c:1824 [inline]
allocate_slab mm/slub.c:1969 [inline]
new_slab+0x2a9/0x3f0 mm/slub.c:2029
___slab_alloc+0xd5a/0x1140 mm/slub.c:3031
__slab_alloc.isra.0+0x4d/0xa0 mm/slub.c:3118
slab_alloc_node mm/slub.c:3209 [inline]
__kmalloc_node_track_caller+0x321/0x440 mm/slub.c:4979
kmalloc_reserve+0x32/0xd0 net/core/skbuff.c:354
pskb_expand_head+0x148/0x1060 net/core/skbuff.c:1695
netlink_trim+0x1ea/0x240 net/netlink/af_netlink.c:1301
netlink_broadcast+0x5b/0xd00 net/netlink/af_netlink.c:1497
nlmsg_multicast include/net/netlink.h:1033 [inline]
nlmsg_notify+0x8f/0x280 net/netlink/af_netlink.c:2544
rtnl_notify net/core/rtnetlink.c:767 [inline]
rtmsg_ifinfo_send net/core/rtnetlink.c:3928 [inline]
rtmsg_ifinfo_event.part.0+0xb6/0xe0 net/core/rtnetlink.c:3943
rtmsg_ifinfo_event net/core/rtnetlink.c:6147 [inline]
rtnetlink_event+0x11e/0x150 net/core/rtnetlink.c:6140
notifier_call_chain+0xb5/0x200 kernel/notifier.c:87
page_owner free stack trace missing

Memory state around the buggy address:
ffff88812e561f00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88812e561f80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
>ffff88812e562000: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff88812e562080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff88812e562100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb

--
Thanks and Regards,

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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <setjmp.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>

#include <linux/futex.h>

#ifndef __NR_io_uring_register
#define __NR_io_uring_register 427
#endif
#ifndef __NR_io_uring_setup
#define __NR_io_uring_setup 425
#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; })

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 void thread_start(void* (*fn)(void*), void* arg)
{
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 128 << 10);
int i = 0;
for (; i < 100; i++) {
if (pthread_create(&th, &attr, fn, arg) == 0) {
pthread_attr_destroy(&attr);
return;
}
if (errno == EAGAIN) {
usleep(50);
continue;
}
break;
}
exit(1);
}

typedef struct {
int state;
} event_t;

static void event_init(event_t* ev)
{
ev->state = 0;
}

static void event_reset(event_t* ev)
{
ev->state = 0;
}

static void event_set(event_t* ev)
{
if (ev->state)
exit(1);
__atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}

static void event_wait(event_t* ev)
{
while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}

static int event_isset(event_t* ev)
{
return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}

static int event_timedwait(event_t* ev, uint64_t timeout)
{
uint64_t start = current_time_ms();
uint64_t now = start;
for (;;) {
uint64_t remain = timeout - (now - start);
struct timespec ts;
ts.tv_sec = remain / 1000;
ts.tv_nsec = (remain % 1000) * 1000 * 1000;
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
return 1;
now = current_time_ms();
if (now - start > timeout)
return 0;
}
}

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;
}

#define SIZEOF_IO_URING_SQE 64
#define SIZEOF_IO_URING_CQE 16
#define SQ_HEAD_OFFSET 0
#define SQ_TAIL_OFFSET 64
#define SQ_RING_MASK_OFFSET 256
#define SQ_RING_ENTRIES_OFFSET 264
#define SQ_FLAGS_OFFSET 276
#define SQ_DROPPED_OFFSET 272
#define CQ_HEAD_OFFSET 128
#define CQ_TAIL_OFFSET 192
#define CQ_RING_MASK_OFFSET 260
#define CQ_RING_ENTRIES_OFFSET 268
#define CQ_RING_OVERFLOW_OFFSET 284
#define CQ_FLAGS_OFFSET 280
#define CQ_CQES_OFFSET 320

static long syz_io_uring_submit(volatile long a0, volatile long a1, volatile long a2, volatile long a3)
{
char* ring_ptr = (char*)a0;
char* sqes_ptr = (char*)a1;
char* sqe = (char*)a2;
uint32_t sqes_index = (uint32_t)a3;
uint32_t sq_ring_entries = *(uint32_t*)(ring_ptr + SQ_RING_ENTRIES_OFFSET);
uint32_t cq_ring_entries = *(uint32_t*)(ring_ptr + CQ_RING_ENTRIES_OFFSET);
uint32_t sq_array_off = (CQ_CQES_OFFSET + cq_ring_entries * SIZEOF_IO_URING_CQE + 63) & ~63;
if (sq_ring_entries)
sqes_index %= sq_ring_entries;
char* sqe_dest = sqes_ptr + sqes_index * SIZEOF_IO_URING_SQE;
memcpy(sqe_dest, sqe, SIZEOF_IO_URING_SQE);
uint32_t sq_ring_mask = *(uint32_t*)(ring_ptr + SQ_RING_MASK_OFFSET);
uint32_t* sq_tail_ptr = (uint32_t*)(ring_ptr + SQ_TAIL_OFFSET);
uint32_t sq_tail = *sq_tail_ptr & sq_ring_mask;
uint32_t sq_tail_next = *sq_tail_ptr + 1;
uint32_t* sq_array = (uint32_t*)(ring_ptr + sq_array_off);
*(sq_array + sq_tail) = sqes_index;
__atomic_store_n(sq_tail_ptr, sq_tail_next, __ATOMIC_RELEASE);
return 0;
}

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");
}

struct thread_t {
int created, call;
event_t ready, done;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;

static void* thr(void* arg)
{
struct thread_t* th = (struct thread_t*)arg;
for (;;) {
event_wait(&th->ready);
event_reset(&th->ready);
execute_call(th->call);
__atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
event_set(&th->done);
}
return 0;
}

static void execute_one(void)
{
int i, call, thread;
for (call = 0; call < 5; call++) {
for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) {
struct thread_t* th = &threads[thread];
if (!th->created) {
th->created = 1;
event_init(&th->ready);
event_init(&th->done);
event_set(&th->done);
thread_start(thr, th);
}
if (!event_isset(&th->done))
continue;
event_reset(&th->done);
th->call = call;
__atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
event_set(&th->ready);
if (call == 3)
break;
event_timedwait(&th->done, 50);
break;
}
}
for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
sleep_ms(1);
}

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[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0x0};

void execute_call(int call)
{
intptr_t res = 0;
switch (call) {
case 0:
NONFAILING(*(uint32_t*)0x20000284 = 0);
NONFAILING(*(uint32_t*)0x20000288 = 0);
NONFAILING(*(uint32_t*)0x2000028c = 0);
NONFAILING(*(uint32_t*)0x20000290 = 0);
NONFAILING(*(uint32_t*)0x20000298 = -1);
NONFAILING(memset((void*)0x2000029c, 0, 12));
res = syscall(__NR_io_uring_setup, 0x1224, 0x20000280ul);
if (res != -1)
r[0] = res;
break;
case 1:
NONFAILING(*(uint32_t*)0x20000284 = 0);
NONFAILING(*(uint32_t*)0x20000288 = 0);
NONFAILING(*(uint32_t*)0x2000028c = 0);
NONFAILING(*(uint32_t*)0x20000290 = 0);
NONFAILING(*(uint32_t*)0x20000298 = -1);
NONFAILING(memset((void*)0x2000029c, 0, 12));
res = syscall(__NR_io_uring_setup, 0x1224, 0x20000280ul);
if (res != -1)
r[1] = res;
break;
case 2:
res = syscall(__NR_io_uring_register, r[1], 9ul, 0ul, 0ul);
if (res != -1)
r[2] = res;
break;
case 3:
syscall(__NR_io_uring_register, r[0], 0x16ul, 0x20000000ul, r[2]);
break;
case 4:
NONFAILING(*(uint8_t*)0x200001c0 = 0x1c);
NONFAILING(*(uint8_t*)0x200001c1 = 0);
NONFAILING(*(uint16_t*)0x200001c2 = 0);
NONFAILING(*(uint32_t*)0x200001c4 = -1);
NONFAILING(*(uint64_t*)0x200001c8 = 0x20000000);
NONFAILING(*(uint64_t*)0x20000000 = 0x40000);
NONFAILING(*(uint64_t*)0x20000008 = 8);
NONFAILING(*(uint64_t*)0x20000010 = 0);
NONFAILING(*(uint64_t*)0x200001d0 = 0);
NONFAILING(*(uint32_t*)0x200001d8 = 0x18);
NONFAILING(*(uint32_t*)0x200001dc = 0);
NONFAILING(*(uint64_t*)0x200001e0 = 0x23456);
NONFAILING(*(uint16_t*)0x200001e8 = 0);
NONFAILING(*(uint16_t*)0x200001ea = r[2]);
NONFAILING(memset((void*)0x200001ec, 0, 20));
NONFAILING(syz_io_uring_submit(0, 0, 0x200001c0, 8));
break;
}

}
int main(void)
{
syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
install_segv_handler();
loop();
return 0;
}

Attachment: repro.syz
Description: Binary data