xref: /freebsd/tools/test/stress2/misc/syzkaller31.sh (revision 7f658f99f7ed5d1d0e0802073bb22eb8a0a784fb)
1#!/bin/sh
2
3# panic: Bad tailq NEXT(0xfffffe0079608f00->tqh_last) != NULL
4# cpuid = 20
5# time = 1616404997
6# KDB: stack backtrace:
7# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01af6d8580
8# vpanic() at vpanic+0x181/frame 0xfffffe01af6d85d0
9# panic() at panic+0x43/frame 0xfffffe01af6d8630
10# sctp_ss_default_add() at sctp_ss_default_add+0xd7/frame 0xfffffe01af6d8660
11# sctp_lower_sosend() at sctp_lower_sosend+0x3b24/frame 0xfffffe01af6d8830
12# sctp_sosend() at sctp_sosend+0x344/frame 0xfffffe01af6d8950
13# sosend() at sosend+0x66/frame 0xfffffe01af6d8980
14# kern_sendit() at kern_sendit+0x1ec/frame 0xfffffe01af6d8a10
15# sendit() at sendit+0x1db/frame 0xfffffe01af6d8a60
16# sys_sendmsg() at sys_sendmsg+0x61/frame 0xfffffe01af6d8ac0
17# amd64_syscall() at amd64_syscall+0x147/frame 0xfffffe01af6d8bf0
18# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01af6d8bf0
19# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8003b042a, rsp = 0x7fffdffdcf68, rbp = 0x7fffdffdcf90 ---
20# KDB: enter: panic
21# [ thread pid 7402 tid 730532 ]
22# Stopped at      kdb_enter+0x37: movq    $0,0x1286f8e(%rip)
23# db> x/s version
24# version: FreeBSD 14.0-CURRENT #0 main-n245565-25bfa448602: Mon Mar 22 09:13:03 CET 2021
25# pho@t2.osted.lan:/usr/src/sys/amd64/compile/PHO\012
26# db>
27
28[ `uname -p` != "amd64" ] && exit 0
29
30. ../default.cfg
31kldstat -v | grep -q sctp || kldload sctp.ko
32
33cat > /tmp/syzkaller31.c <<EOF
34// https://syzkaller.appspot.com/bug?id=57c78914e36b822eaa173cfd681e4f63822e0844
35// autogenerated by syzkaller (https://github.com/google/syzkaller)
36// Reported-by: syzbot+ed6e8de942351d0309f4@syzkaller.appspotmail.com
37
38#define _GNU_SOURCE
39
40#include <sys/types.h>
41
42#include <errno.h>
43#include <pthread.h>
44#include <pwd.h>
45#include <signal.h>
46#include <stdarg.h>
47#include <stdbool.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <sys/endian.h>
53#include <sys/syscall.h>
54#include <sys/wait.h>
55#include <time.h>
56#include <unistd.h>
57
58static void kill_and_wait(int pid, int* status)
59{
60  kill(pid, SIGKILL);
61  while (waitpid(-1, status, 0) != pid) {
62  }
63}
64
65static void sleep_ms(uint64_t ms)
66{
67  usleep(ms * 1000);
68}
69
70static uint64_t current_time_ms(void)
71{
72  struct timespec ts;
73  if (clock_gettime(CLOCK_MONOTONIC, &ts))
74    exit(1);
75  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
76}
77
78static void thread_start(void* (*fn)(void*), void* arg)
79{
80  pthread_t th;
81  pthread_attr_t attr;
82  pthread_attr_init(&attr);
83  pthread_attr_setstacksize(&attr, 128 << 10);
84  int i = 0;
85  for (; i < 100; i++) {
86    if (pthread_create(&th, &attr, fn, arg) == 0) {
87      pthread_attr_destroy(&attr);
88      return;
89    }
90    if (errno == EAGAIN) {
91      usleep(50);
92      continue;
93    }
94    break;
95  }
96  exit(1);
97}
98
99typedef struct {
100  pthread_mutex_t mu;
101  pthread_cond_t cv;
102  int state;
103} event_t;
104
105static void event_init(event_t* ev)
106{
107  if (pthread_mutex_init(&ev->mu, 0))
108    exit(1);
109  if (pthread_cond_init(&ev->cv, 0))
110    exit(1);
111  ev->state = 0;
112}
113
114static void event_reset(event_t* ev)
115{
116  ev->state = 0;
117}
118
119static void event_set(event_t* ev)
120{
121  pthread_mutex_lock(&ev->mu);
122  if (ev->state)
123    exit(1);
124  ev->state = 1;
125  pthread_mutex_unlock(&ev->mu);
126  pthread_cond_broadcast(&ev->cv);
127}
128
129static void event_wait(event_t* ev)
130{
131  pthread_mutex_lock(&ev->mu);
132  while (!ev->state)
133    pthread_cond_wait(&ev->cv, &ev->mu);
134  pthread_mutex_unlock(&ev->mu);
135}
136
137static int event_isset(event_t* ev)
138{
139  pthread_mutex_lock(&ev->mu);
140  int res = ev->state;
141  pthread_mutex_unlock(&ev->mu);
142  return res;
143}
144
145static int event_timedwait(event_t* ev, uint64_t timeout)
146{
147  uint64_t start = current_time_ms();
148  uint64_t now = start;
149  pthread_mutex_lock(&ev->mu);
150  for (;;) {
151    if (ev->state)
152      break;
153    uint64_t remain = timeout - (now - start);
154    struct timespec ts;
155    ts.tv_sec = remain / 1000;
156    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
157    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
158    now = current_time_ms();
159    if (now - start > timeout)
160      break;
161  }
162  int res = ev->state;
163  pthread_mutex_unlock(&ev->mu);
164  return res;
165}
166
167struct thread_t {
168  int created, call;
169  event_t ready, done;
170};
171
172static struct thread_t threads[16];
173static void execute_call(int call);
174static int running;
175
176static void* thr(void* arg)
177{
178  struct thread_t* th = (struct thread_t*)arg;
179  for (;;) {
180    event_wait(&th->ready);
181    event_reset(&th->ready);
182    execute_call(th->call);
183    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
184    event_set(&th->done);
185  }
186  return 0;
187}
188
189static void execute_one(void)
190{
191  int i, call, thread;
192  int collide = 0;
193again:
194  for (call = 0; call < 3; call++) {
195    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
196         thread++) {
197      struct thread_t* th = &threads[thread];
198      if (!th->created) {
199        th->created = 1;
200        event_init(&th->ready);
201        event_init(&th->done);
202        event_set(&th->done);
203        thread_start(thr, th);
204      }
205      if (!event_isset(&th->done))
206        continue;
207      event_reset(&th->done);
208      th->call = call;
209      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
210      event_set(&th->ready);
211      if (collide && (call % 2) == 0)
212        break;
213      event_timedwait(&th->done, 50);
214      break;
215    }
216  }
217  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
218    sleep_ms(1);
219  if (!collide) {
220    collide = 1;
221    goto again;
222  }
223}
224
225static void execute_one(void);
226
227#define WAIT_FLAGS 0
228
229static void loop(void)
230{
231  int iter __unused = 0;
232  for (;; iter++) {
233    int pid = fork();
234    if (pid < 0)
235      exit(1);
236    if (pid == 0) {
237      execute_one();
238      exit(0);
239    }
240    int status = 0;
241    uint64_t start = current_time_ms();
242    for (;;) {
243      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
244        break;
245      sleep_ms(1);
246      if (current_time_ms() - start < 5000) {
247        continue;
248      }
249      kill_and_wait(pid, &status);
250      break;
251    }
252  }
253}
254
255uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
256
257void execute_call(int call)
258{
259  intptr_t res = 0;
260  switch (call) {
261  case 0:
262    res = syscall(SYS_socket, 2ul, 5ul, 0x84);
263    if (res != -1)
264      r[0] = res;
265    break;
266  case 1:
267    res = syscall(SYS_fcntl, r[0], 0ul, r[0]);
268    if (res != -1)
269      r[1] = res;
270    break;
271  case 2:
272    *(uint64_t*)0x20000040 = 0x20000000;
273    *(uint8_t*)0x20000000 = 0x1c;
274    *(uint8_t*)0x20000001 = 0x1c;
275    *(uint16_t*)0x20000002 = htobe16(0x4e22);
276    *(uint32_t*)0x20000004 = 0;
277    *(uint8_t*)0x20000008 = 0;
278    *(uint8_t*)0x20000009 = 0;
279    *(uint8_t*)0x2000000a = 0;
280    *(uint8_t*)0x2000000b = 0;
281    *(uint8_t*)0x2000000c = 0;
282    *(uint8_t*)0x2000000d = 0;
283    *(uint8_t*)0x2000000e = 0;
284    *(uint8_t*)0x2000000f = 0;
285    *(uint8_t*)0x20000010 = 0;
286    *(uint8_t*)0x20000011 = 0;
287    *(uint8_t*)0x20000012 = -1;
288    *(uint8_t*)0x20000013 = -1;
289    *(uint32_t*)0x20000014 = htobe32(0x203);
290    *(uint32_t*)0x20000018 = 0;
291    *(uint32_t*)0x20000048 = 0x1c;
292    *(uint64_t*)0x20000050 = 0x20000100;
293    *(uint64_t*)0x20000100 = 0x20000080;
294    memcpy((void*)0x20000080, "\000", 1);
295    *(uint64_t*)0x20000108 = 1;
296    *(uint32_t*)0x20000058 = 1;
297    *(uint64_t*)0x20000060 = 0x20000200;
298    memcpy(
299        (void*)0x20000200,
300        "\x1c\x00\x00\x00\x84\x00\x00\x00\x01\x00\x00\x00\x40\x08\xec\x70\xa0",
301        17);
302    *(uint32_t*)0x20000068 = 0x1c;
303    *(uint32_t*)0x2000006c = 0;
304    syscall(SYS_sendmsg, r[1], 0x20000040ul, 0ul);
305    break;
306  }
307}
308int main(void)
309{
310  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
311  loop();
312  return 0;
313}
314EOF
315mycc -o /tmp/syzkaller31 -Wall -Wextra -O0 /tmp/syzkaller31.c -lpthread ||
316    exit 1
317
318(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
319(cd /tmp; timeout 3m ./syzkaller31)
320while pkill swap; do :; done
321wait
322
323rm -rf /tmp/syzkaller31 /tmp/syzkaller31.c /tmp/syzkaller.*
324exit 0
325