#!/bin/sh

# panic: __rw_wlock_hard: recursing but non-recursive rw sctp-info @ /usr/src/sys/netinet/sctp_pcb.c:1856
# cpuid = 10
# time = 1631108444
# KDB: stack backtrace:
# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe065f32e7f0
# vpanic() at vpanic+0x187/frame 0xfffffe065f32e850
# panic() at panic+0x43/frame 0xfffffe065f32e8b0
# __rw_wlock_hard() at __rw_wlock_hard+0x7d1/frame 0xfffffe065f32e960
# _rw_wlock_cookie() at _rw_wlock_cookie+0xb7/frame 0xfffffe065f32e9a0
# sctp_swap_inpcb_for_listen() at sctp_swap_inpcb_for_listen+0x7f/frame 0xfffffe065f32e9f0
# sctp_listen() at sctp_listen+0x248/frame 0xfffffe065f32ea60
# solisten() at solisten+0x42/frame 0xfffffe065f32ea80
# kern_listen() at kern_listen+0x7d/frame 0xfffffe065f32eac0
# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe065f32ebf0
# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe065f32ebf0
# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x800380a4a, rsp = 0x7fffffffe9b8, rbp = 0x7fffffffe9d0 ---
# KDB: enter: panic
# [ thread pid 26683 tid 924765 ]
# Stopped at      kdb_enter+0x37: movq    $0,0x12724de(%rip)
# db> x/s version
# version: FreeBSD 14.0-CURRENT #0 main-n249253-4ccbbe5f0689: Wed Sep  8 07:26:48 CEST 2021
# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO\012
# db> 

# Fixed by 6e3af6321ba4 - main - sctp: Fix lock recursion in sctp_swap_inpcb_for_listen()

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

. ../default.cfg
cat > /tmp/syzkaller44.c <<EOF
// https://syzkaller.appspot.com/bug?id=367509f8fdee02c7d5a6c24b77eb2643792ef376
// autogenerated by syzkaller (https://github.com/google/syzkaller)
// Reported-by: syzbot+a1b6cef257ad145b7187@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[2] = {0xffffffffffffffff, 0xffffffffffffffff};

int main(void)
{
  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
  intptr_t res = 0;
  res = syscall(SYS_socket, 0x1cul, 1ul, 0x84);
  if (res != -1)
    r[0] = res;
  *(uint32_t*)0x20000140 = 0;
  syscall(SYS_setsockopt, r[0], 0x84, 0x1c, 0x20000140ul, 4ul);
  *(uint8_t*)0x20000080 = 0x46;
  *(uint8_t*)0x20000081 = 0x1c;
  *(uint16_t*)0x20000082 = htobe16(0x4e22);
  *(uint32_t*)0x20000084 = 0;
  memset((void*)0x20000088, 0, 16);
  *(uint32_t*)0x20000098 = 0;
  syscall(SYS_bind, r[0], 0x20000080ul, 0x1cul);
  res = syscall(SYS_socket, 0x1cul, 1ul, 0x84);
  if (res != -1)
    r[1] = res;
  *(uint32_t*)0x20000140 = 0;
  syscall(SYS_setsockopt, r[1], 0x84, 0x1c, 0x20000140ul, 4ul);
  *(uint8_t*)0x20000080 = 0x46;
  *(uint8_t*)0x20000081 = 0x1c;
  *(uint16_t*)0x20000082 = htobe16(0x4e22);
  *(uint32_t*)0x20000084 = 0;
  memset((void*)0x20000088, 0, 16);
  *(uint32_t*)0x20000098 = 0;
  syscall(SYS_bind, r[1], 0x20000080ul, 0x1cul);
  syscall(SYS_listen, r[1], 0);
  return 0;
}
EOF
mycc -o /tmp/syzkaller44 -Wall -Wextra -O0 /tmp/syzkaller44.c || exit 1

kldstat -v | grep -q sctp || kldload sctp.ko
(cd /tmp; timeout 3m ./syzkaller44)

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