general protection fault in mas_empty_area_rev

From: Marius Fleischer
Date: Sat Apr 20 2024 - 16:08:56 EST


Hi,


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


======================================================

description: general protection fault in mas_empty_area_rev

affected file: lib/maple_tree.c

kernel version: 6.9-rc4

kernel commit: 0bbac3facb5d6cc0171c45c9873a2dc96bea9680

git tree: upstream

kernel config: attached

crash reproducer: attached

======================================================

Crash log:

general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN NOPTI

KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]

CPU: 0 PID: 79545 Comm: syz-executor.0 Not tainted 6.9.0-rc4-dirty #3

Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014

RIP: 0010:ma_dead_node lib/maple_tree.c:560 [inline]

RIP: 0010:mas_data_end lib/maple_tree.c:1450 [inline]

RIP: 0010:mas_empty_area_rev+0x15ad/0x2320 lib/maple_tree.c:5114

Code: 83 fd 02 77 0f 45 85 ed 74 0a e8 ae b8 dc f6 49 8d 5c 24 08 e8 a4 b8 dc f6 48 b8 00 00 00 00 00 fc ff df 4c 89 e2 48 c1 ea 03 <80> 3c 02 00 0f 85 fc 08 00 00 49 8b 04 24 30 c0 49 39 c4 0f 84 92

RSP: 0018:ffffc9000fdff9b8 EFLAGS: 00010246

RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000005

RDX: 0000000000000000 RSI: ffffffff8aaf8efc RDI: 0000000000000007

RBP: 0000000000010000 R08: 0000000000000007 R09: 0000000000000002

R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000

R13: 0000000000000000 R14: ffffc9000fdffb24 R15: ffffc9000fdffae8

FS:  0000555581ad4480(0000) GS:ffff888063600000(0000) knlGS:0000000000000000

CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033

CR2: 0000000000000118 CR3: 0000000040576000 CR4: 0000000000750ef0

DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000

DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400

PKRU: 55555554

Call Trace:

 <TASK>

 unmapped_area_topdown mm/mmap.c:1643 [inline]

 vm_unmapped_area+0x2db/0xb30 mm/mmap.c:1682

 arch_get_unmapped_area_topdown+0x384/0x750 arch/x86/kernel/sys_x86_64.c:212

 thp_get_unmapped_area mm/huge_memory.c:864 [inline]

 thp_get_unmapped_area+0x361/0x430 mm/huge_memory.c:854

 get_unmapped_area+0x1db/0x3e0 mm/mmap.c:1845

 do_mmap+0x282/0xef0 mm/mmap.c:1261

 vm_mmap_pgoff+0x1a7/0x3b0 mm/util.c:573

 ksys_mmap_pgoff+0x7d/0x5b0 mm/mmap.c:1431

 __do_sys_mmap arch/x86/kernel/sys_x86_64.c:86 [inline]

 __se_sys_mmap arch/x86/kernel/sys_x86_64.c:79 [inline]

 __x64_sys_mmap+0x125/0x190 arch/x86/kernel/sys_x86_64.c:79

 do_syscall_x64 arch/x86/entry/common.c:52 [inline]

 do_syscall_64+0xce/0x250 arch/x86/entry/common.c:83

 entry_SYSCALL_64_after_hwframe+0x77/0x7f

RIP: 0033:0x7efd0e48ed67

Code: Unable to access opcode bytes at 0x7efd0e48ed3d.

RSP: 002b:00007fff4c4a2598 EFLAGS: 00000246 ORIG_RAX: 0000000000000009

RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007efd0e48ed67

RDX: 0000000000000000 RSI: 0000000000021000 RDI: 0000000000000000

RBP: 00007efd0d600640 R08: 00000000ffffffff R09: 0000000000000000

R10: 0000000000020022 R11: 0000000000000246 R12: 0000000000000000

R13: 00007fff4c4a2850 R14: 0000000000021000 R15: 0000000000000000

 </TASK>

Modules linked in:

---[ end trace 0000000000000000 ]---

RIP: 0010:ma_dead_node lib/maple_tree.c:560 [inline]

RIP: 0010:mas_data_end lib/maple_tree.c:1450 [inline]

RIP: 0010:mas_empty_area_rev+0x15ad/0x2320 lib/maple_tree.c:5114

Code: 83 fd 02 77 0f 45 85 ed 74 0a e8 ae b8 dc f6 49 8d 5c 24 08 e8 a4 b8 dc f6 48 b8 00 00 00 00 00 fc ff df 4c 89 e2 48 c1 ea 03 <80> 3c 02 00 0f 85 fc 08 00 00 49 8b 04 24 30 c0 49 39 c4 0f 84 92

RSP: 0018:ffffc9000fdff9b8 EFLAGS: 00010246

RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000005

RDX: 0000000000000000 RSI: ffffffff8aaf8efc RDI: 0000000000000007

RBP: 0000000000010000 R08: 0000000000000007 R09: 0000000000000002

R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000

R13: 0000000000000000 R14: ffffc9000fdffb24 R15: ffffc9000fdffae8

FS:  0000555581ad4480(0000) GS:ffff888063600000(0000) knlGS:0000000000000000

CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033

CR2: 000000c01aa34000 CR3: 0000000040576000 CR4: 0000000000750ef0

DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000

DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400

PKRU: 55555554

----------------

Code disassembly (best guess):

   0:    83 fd 02        cmp $0x2,%ebp

   3:    77 0f           ja 0x14

   5:    45 85 ed        test   %r13d,%r13d

   8:    74 0a           je 0x14

   a:    e8 ae b8 dc f6  call   0xf6dcb8bd

   f:    49 8d 5c 24 08  lea 0x8(%r12),%rbx

  14:    e8 a4 b8 dc f6  call   0xf6dcb8bd

  19:    48 b8 00 00 00 00 00     movabs $0xdffffc0000000000,%rax

  20:    fc ff df

  23:    4c 89 e2        mov %r12,%rdx

  26:    48 c1 ea 03     shr $0x3,%rdx

* 2a:    80 3c 02 00     cmpb   $0x0,(%rdx,%rax,1) <-- trapping instruction

  2e:    0f 85 fc 08 00 00   jne 0x930

  34:    49 8b 04 24     mov (%r12),%rax

  38:    30 c0           xor %al,%al

  3a:    49 39 c4        cmp %rax,%r12

  3d:    0f              .byte 0xf

  3e:    84              .byte 0x84

  3f:    92              xchg   %eax,%edx

======================================================


Wishing you a nice day!


Best,

Marius

// 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 <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>

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

struct io_uring_cqe {
uint64_t user_data;
uint32_t res;
uint32_t flags;
};

static long syz_io_uring_complete(volatile long a0)
{
char* ring_ptr = (char*)a0;
uint32_t cq_ring_mask = *(uint32_t*)(ring_ptr + CQ_RING_MASK_OFFSET);
uint32_t* cq_head_ptr = (uint32_t*)(ring_ptr + CQ_HEAD_OFFSET);
uint32_t cq_head = *cq_head_ptr & cq_ring_mask;
uint32_t cq_head_next = *cq_head_ptr + 1;
char* cqe_src = ring_ptr + CQ_CQES_OFFSET + cq_head * SIZEOF_IO_URING_CQE;
struct io_uring_cqe cqe;
memcpy(&cqe, cqe_src, sizeof(cqe));
__atomic_store_n(cq_head_ptr, cq_head_next, __ATOMIC_RELEASE);
return (cqe.user_data == 0x12345 || cqe.user_data == 0x23456) ? (long)cqe.res
: (long)-1;
}

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 < 2; 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 == 0)
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;
}
}
}

void execute_call(int call)
{
switch (call) {
case 0:
syscall(__NR_munmap, /*addr=*/0ul, /*len=*/0x7ffffffff000ul);
break;
case 1:
syz_io_uring_complete(/*ring_ptr=*/0);
break;
}
}
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 < 8; procid++) {
if (fork() == 0) {
loop();
}
}
sleep(1000000);
return 0;
}

Attachment: repro.syz
Description: Binary data

Attachment: config-6.9-rc4
Description: Binary data