#!/bin/sh

# Fatal trap 12: page fault while in kernel mode
# cpuid = 4; apic id = 04
# fault virtual address   = 0x28
# fault code              = supervisor read data, page not present
# instruction pointer     = 0x20:0xffffffff81549dea
# stack pointer           = 0x28:0xfffffe01d8689480
# frame pointer           = 0x28:0xfffffe01d8689490
# code segment            = base 0x0, limit 0xfffff, type 0x1b
#                         = DPL 0, pres 1, long 1, def32 0, gran 1
# processor eflags        = interrupt enabled, resume, IOPL = 0
# current process         = 3050 (syzkaller46)
# trap number             = 12
# panic: page fault
# cpuid = 4
# time = 1635158869
# KDB: stack backtrace:
# db_trace_self_wrapper() at db_trace_self_wrapper+0xa5/frame 0xfffffe01d8688cb0
# kdb_backtrace() at kdb_backtrace+0xc9/frame 0xfffffe01d8688e10
# vpanic() at vpanic+0x248/frame 0xfffffe01d8688ef0
# panic() at panic+0xb5/frame 0xfffffe01d8688fb0
# trap_fatal() at trap_fatal+0x52e/frame 0xfffffe01d86890b0
# trap_pfault() at trap_pfault+0x132/frame 0xfffffe01d86891d0
# trap() at trap+0x53f/frame 0xfffffe01d86893b0
# calltrap() at calltrap+0x8/frame 0xfffffe01d86893b0
# --- trap 0xc, rip = 0xffffffff81549dea, rsp = 0xfffffe01d8689480, rbp = 0xfffffe01d8689490 ---
# filt_bpfwrite() filt_bpfwrite+0x4a/frame 0xfffffe01d8689490
# kqueue_register() at kqueue_register+0xea3/frame 0xfffffe01d86895d0
# kqueue_kevent() at kqueue_kevent+0x26a/frame 0xfffffe01d86899c0
# kern_kevent_fp() at kern_kevent_fp+0xd2/frame 0xfffffe01d8689a10
# kern_kevent() at kern_kevent+0x138/frame 0xfffffe01d8689b10
# kern_kevent_generic() at kern_kevent_gene6/frame 0xfffffesys_kevent() at sys_kevent+0x1e1/frame 0xfffffe01d8689d30
# amd64_syscall() at amd64_syscall+0x31e/frame 0xfffffe01d8689f30
# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01d8689f30
# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8003adafa, rsp = 0x7fffffffe648, rbp = 0x7fffffffe670 ---
# KDB: enter: panic
# [ thread pid 3050 tid 100263 ]
# Stopped at      kdb_enter+0x37: movq    $0,0x2638c4e(%rip)
# db> x/s version
# version: FreeBSD 14.0-CURRENT #0 main-n250242-eab5358b9080-dirty: Mon Oct 25 11:32:45 CEST 2021
# pho@mercat1.netperf.freebsd.org
# db> 


[ `uname -p` != "amd64" ] && exit 0
[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1

. ../default.cfg
cat > /tmp/syzkaller46.c <<EOF
// https://syzkaller.appspot.com/bug?id=a99f705b2b8b854d70ec4d47eed481c90046bd3c
// autogenerated by syzkaller (https://github.com/google/syzkaller)
// Reported-by: syzbot+ae45d5166afe15a5a21d@syzkaller.appspotmail.com

#define _GNU_SOURCE

#include <pwd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/endian.h>
#include <sys/syscall.h>
#include <unistd.h>

uint64_t r[1] = {0xffffffffffffffff};

int main(void)
{
  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
  intptr_t res = 0;
  memcpy((void*)0x20000040, "/dev/bpf\000", 9);
  syscall(SYS_openat, 0xffffffffffffff9cul, 0x20000040ul, 0ul, 0ul);
  res = syscall(SYS_kqueue);
  if (res != -1)
    r[0] = res;
  *(uint64_t*)0x20000480 = 0x284;
  *(uint16_t*)0x20000488 = 0xfff8;
  *(uint16_t*)0x2000048a = 0x10;
  *(uint32_t*)0x2000048c = 1;
  *(uint64_t*)0x20000490 = 0x401;
  *(uint64_t*)0x20000498 = 5;
  *(uint64_t*)0x200004a0 = 5;
  *(uint64_t*)0x200004a8 = 0x24000000;
  *(uint64_t*)0x200004b0 = 0x100000000;
  *(uint64_t*)0x200004b8 = 0x3f;
  *(uint64_t*)0x200004c0 = 3;
  *(uint16_t*)0x200004c8 = 0xfffe;
  *(uint16_t*)0x200004ca = 1;
  *(uint32_t*)0x200004cc = 1;
  *(uint64_t*)0x200004d0 = 1;
  *(uint64_t*)0x200004d8 = 3;
  *(uint64_t*)0x200004e0 = 9;
  *(uint64_t*)0x200004e8 = 0x3ff;
  *(uint64_t*)0x200004f0 = 0x100000001;
  *(uint64_t*)0x200004f8 = 3;
  *(uint64_t*)0x20000500 = 5;
  *(uint16_t*)0x20000508 = 0xfffe;
  *(uint16_t*)0x2000050a = 0x42;
  *(uint32_t*)0x2000050c = 2;
  *(uint64_t*)0x20000510 = 5;
  *(uint64_t*)0x20000518 = 0x7f;
  *(uint64_t*)0x20000520 = 9;
  *(uint64_t*)0x20000528 = 0x600000000;
  *(uint64_t*)0x20000530 = 0x1f;
  *(uint64_t*)0x20000538 = 7;
  syscall(SYS_kevent, r[0], 0x20000480ul, 3ul, 0x200001c0ul, 0xaul, 0ul);
  return 0;
}
EOF
mycc -o /tmp/syzkaller46 -Wall -Wextra -O0 /tmp/syzkaller46.c -lpthread || exit 1

(cd /tmp; ./syzkaller46)

rm -rf /tmp/syzkaller46 /tmp/syzkaller46.c /tmp/syzkaller.*
exit 0
