#!/bin/sh

# panic: Assertion (cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0 failed at ../../../kern/vfs_lookup.c:490
# cpuid = 22
# time = 1620845561
# KDB: stack backtrace:
# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01499e7690
# vpanic() at vpanic+0x181/frame 0xfffffe01499e76e0
# panic() at panic+0x43/frame 0xfffffe01499e7740
# namei() at namei+0xb4e/frame 0xfffffe01499e77f0
# vn_open_cred() at vn_open_cred+0x11d/frame 0xfffffe01499e7960
# kern_openat() at kern_openat+0x28f/frame 0xfffffe01499e7ac0
# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe01499e7bf0
# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01499e7bf0
# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80038254a, rsp = 0x7fffffffe4f8, rbp = 0x7fffffffe540 ---
# KDB: enter: panic
# [ thread pid 2990 tid 100320 ]
# Stopped at      kdb_enter+0x37: movq    $0,0x12819de(%rip)
# db> x/s version
# version: FreeBSD 14.0-CURRENT #0 main-n246600-e681dd3e2c1-dirty: Wed May 12 07:56:58 CEST 2021
# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO\012
# db>

# Fixed by: 6de3cf14c47d - main - vn_open_cred(): disallow O_CREAT | O_EMPTY_PATH

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

. ../default.cfg
cat > /tmp/syzkaller38.c <<EOF
// https://syzkaller.appspot.com/bug?id=72344b68a3a10a92bf1ef18b0c8286409c81b1c9
// autogenerated by syzkaller (https://github.com/google/syzkaller)
// Reported-by: syzbot+dbfd5b122ad66ae9a14b@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>

int main(void)
{
  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);

  memset((void*)0x20000180, 0, 1);
  syscall(SYS_open, 0x20000180ul, 0x20c0200ul, 0ul);
  return 0;
}
EOF
mycc -o /tmp/syzkaller38 -Wall -Wextra -O0 /tmp/syzkaller38.c ||
    exit 1

(cd /tmp; timeout 3m ./syzkaller38)

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