xref: /freebsd/tools/test/stress2/misc/syzkaller16.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2
3# Fatal trap 9: general protection fault while in kernel mode
4# cpuid = 10; apic id = 0a
5# instruction pointer     = 0x20:0xffffffff80db1fd9
6# stack pointer           = 0x0:0xfffffe00e2240660
7# frame pointer           = 0x0:0xfffffe00e2240830
8# code segment            = base 0x0, limit 0xfffff, type 0x1b
9#                         = DPL 0, pres 1, long 1, def32 0, gran 1
10# processor eflags        = interrupt enabled, resume, IOPL = 0
11# current process         = 29524 (syzkaller16)
12# trap number             = 9
13# panic: general protection fault
14# cpuid = 10
15# time = 1592652215
16# KDB: stack backtrace:
17# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e2240370
18# vpanic() at vpanic+0x182/frame 0xfffffe00e22403c0
19# panic() at panic+0x43/frame 0xfffffe00e2240420
20# trap_fatal() at trap_fatal+0x387/frame 0xfffffe00e2240480
21# trap() at trap+0x80/frame 0xfffffe00e2240590
22# calltrap() at calltrap+0x8/frame 0xfffffe00e2240590
23# --- trap 0x9, rip = 0xffffffff80db1fd9, rsp = 0xfffffe00e2240660, rbp = 0xfffffe00e2240830 ---
24# sctp_lower_sosend() at sctp_lower_sosend+0x21b9/frame 0xfffffe00e2240830
25# sctp_sosend() at sctp_sosend+0x346/frame 0xfffffe00e2240950
26# sosend() at sosend+0x66/frame 0xfffffe00e2240980
27# kern_sendit() at kern_sendit+0x246/frame 0xfffffe00e2240a20
28# sendit() at sendit+0x1d8/frame 0xfffffe00e2240a70
29# sys_sendto() at sys_sendto+0x4d/frame 0xfffffe00e2240ac0
30# ia32_syscall() at ia32_syscall+0x150/frame 0xfffffe00e2240bf0
31# int0x80_syscall_common() at int0x80_syscall_common+0x9c/frame 0xfbffcf8c
32# db> x/s version
33# version: FreeBSD 13.0-CURRENT #0 r362431: Sat Jun 20 09:52:53 CEST 2020
34# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
35# db>
36
37[ `uname -p` != "amd64" ] && exit 0
38
39. ../default.cfg
40kldstat -v | grep -q sctp || kldload sctp.ko
41cat > /tmp/syzkaller16.c <<EOF
42// https://syzkaller.appspot.com/bug?id=4b11a1abc41d0a67736ef9a7506ab3ec7e378102
43// autogenerated by syzkaller (https://github.com/google/syzkaller)
44// Reported-by: syzbot+bc02585076c3cc977f9b@syzkaller.appspotmail.com
45
46#define _GNU_SOURCE
47
48#include <sys/types.h>
49
50#include <errno.h>
51#include <pthread.h>
52#include <pwd.h>
53#include <setjmp.h>
54#include <signal.h>
55#include <stdarg.h>
56#include <stdbool.h>
57#include <stdint.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <sys/endian.h>
62#include <sys/syscall.h>
63#include <sys/wait.h>
64#include <time.h>
65#include <unistd.h>
66
67static unsigned long long procid;
68
69static __thread int skip_segv;
70static __thread jmp_buf segv_env;
71
72static void segv_handler(int sig, siginfo_t* info, void* ctx __unused)
73{
74  uintptr_t addr = (uintptr_t)info->si_addr;
75  const uintptr_t prog_start = 1 << 20;
76  const uintptr_t prog_end = 100 << 20;
77  if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
78      (addr < prog_start || addr > prog_end)) {
79    _longjmp(segv_env, 1);
80  }
81  exit(sig);
82}
83
84static void install_segv_handler(void)
85{
86  struct sigaction sa;
87  memset(&sa, 0, sizeof(sa));
88  sa.sa_sigaction = segv_handler;
89  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
90  sigaction(SIGSEGV, &sa, NULL);
91  sigaction(SIGBUS, &sa, NULL);
92}
93
94#define NONFAILING(...)                                                        \
95  {                                                                            \
96    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
97    if (_setjmp(segv_env) == 0) {                                              \
98      __VA_ARGS__;                                                             \
99    }                                                                          \
100    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
101  }
102
103static void kill_and_wait(int pid, int* status)
104{
105  kill(pid, SIGKILL);
106  while (waitpid(-1, status, 0) != pid) {
107  }
108}
109
110static void sleep_ms(uint64_t ms)
111{
112  usleep(ms * 1000);
113}
114
115static uint64_t current_time_ms(void)
116{
117  struct timespec ts;
118  if (clock_gettime(CLOCK_MONOTONIC, &ts))
119    exit(1);
120  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
121}
122
123static void thread_start(void* (*fn)(void*), void* arg)
124{
125  pthread_t th;
126  pthread_attr_t attr;
127  pthread_attr_init(&attr);
128  pthread_attr_setstacksize(&attr, 128 << 10);
129  int i;
130  for (i = 0; i < 100; i++) {
131    if (pthread_create(&th, &attr, fn, arg) == 0) {
132      pthread_attr_destroy(&attr);
133      return;
134    }
135    if (errno == EAGAIN) {
136      usleep(50);
137      continue;
138    }
139    break;
140  }
141  exit(1);
142}
143
144typedef struct {
145  pthread_mutex_t mu;
146  pthread_cond_t cv;
147  int state;
148} event_t;
149
150static void event_init(event_t* ev)
151{
152  if (pthread_mutex_init(&ev->mu, 0))
153    exit(1);
154  if (pthread_cond_init(&ev->cv, 0))
155    exit(1);
156  ev->state = 0;
157}
158
159static void event_reset(event_t* ev)
160{
161  ev->state = 0;
162}
163
164static void event_set(event_t* ev)
165{
166  pthread_mutex_lock(&ev->mu);
167  if (ev->state)
168    exit(1);
169  ev->state = 1;
170  pthread_mutex_unlock(&ev->mu);
171  pthread_cond_broadcast(&ev->cv);
172}
173
174static void event_wait(event_t* ev)
175{
176  pthread_mutex_lock(&ev->mu);
177  while (!ev->state)
178    pthread_cond_wait(&ev->cv, &ev->mu);
179  pthread_mutex_unlock(&ev->mu);
180}
181
182static int event_isset(event_t* ev)
183{
184  pthread_mutex_lock(&ev->mu);
185  int res = ev->state;
186  pthread_mutex_unlock(&ev->mu);
187  return res;
188}
189
190static int event_timedwait(event_t* ev, uint64_t timeout)
191{
192  uint64_t start = current_time_ms();
193  uint64_t now = start;
194  pthread_mutex_lock(&ev->mu);
195  for (;;) {
196    if (ev->state)
197      break;
198    uint64_t remain = timeout - (now - start);
199    struct timespec ts;
200    ts.tv_sec = remain / 1000;
201    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
202    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
203    now = current_time_ms();
204    if (now - start > timeout)
205      break;
206  }
207  int res = ev->state;
208  pthread_mutex_unlock(&ev->mu);
209  return res;
210}
211
212struct thread_t {
213  int created, call;
214  event_t ready, done;
215};
216
217static struct thread_t threads[16];
218static void execute_call(int call);
219static int running;
220
221static void* thr(void* arg)
222{
223  struct thread_t* th = (struct thread_t*)arg;
224  for (;;) {
225    event_wait(&th->ready);
226    event_reset(&th->ready);
227    execute_call(th->call);
228    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
229    event_set(&th->done);
230  }
231  return 0;
232}
233
234static void execute_one(void)
235{
236  int i, call, thread;
237  int collide = 0;
238again:
239  for (call = 0; call < 6; call++) {
240    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
241         thread++) {
242      struct thread_t* th = &threads[thread];
243      if (!th->created) {
244        th->created = 1;
245        event_init(&th->ready);
246        event_init(&th->done);
247        event_set(&th->done);
248        thread_start(thr, th);
249      }
250      if (!event_isset(&th->done))
251        continue;
252      event_reset(&th->done);
253      th->call = call;
254      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
255      event_set(&th->ready);
256      if (collide && (call % 2) == 0)
257        break;
258      event_timedwait(&th->done, 45);
259      break;
260    }
261  }
262  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
263    sleep_ms(1);
264  if (!collide) {
265    collide = 1;
266    goto again;
267  }
268}
269
270static void execute_one(void);
271
272#define WAIT_FLAGS 0
273
274static void loop(void)
275{
276  int iter __unused;
277  for (iter = 0;; iter++) {
278    int pid = fork();
279    if (pid < 0)
280      exit(1);
281    if (pid == 0) {
282      execute_one();
283      exit(0);
284    }
285    int status = 0;
286    uint64_t start = current_time_ms();
287    for (;;) {
288      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
289        break;
290      sleep_ms(1);
291      if (current_time_ms() - start < 5 * 1000)
292        continue;
293      kill_and_wait(pid, &status);
294      break;
295    }
296  }
297}
298
299uint64_t r[1] = {0xffffffffffffffff};
300
301void execute_call(int call)
302{
303  intptr_t res = 0;
304  switch (call) {
305  case 0:
306    res = syscall(SYS_socket, 0x1c, 1, 0x84);
307    if (res != -1)
308      r[0] = res;
309    break;
310  case 1:
311    NONFAILING(*(uint8_t*)0x10000000 = 0x1c);
312    NONFAILING(*(uint8_t*)0x10000001 = 0x1c);
313    NONFAILING(*(uint16_t*)0x10000002 = htobe16(0x4e22 + procid * 4));
314    NONFAILING(*(uint32_t*)0x10000004 = 0);
315    NONFAILING(*(uint8_t*)0x10000008 = 0);
316    NONFAILING(*(uint8_t*)0x10000009 = 0);
317    NONFAILING(*(uint8_t*)0x1000000a = 0);
318    NONFAILING(*(uint8_t*)0x1000000b = 0);
319    NONFAILING(*(uint8_t*)0x1000000c = 0);
320    NONFAILING(*(uint8_t*)0x1000000d = 0);
321    NONFAILING(*(uint8_t*)0x1000000e = 0);
322    NONFAILING(*(uint8_t*)0x1000000f = 0);
323    NONFAILING(*(uint8_t*)0x10000010 = 0);
324    NONFAILING(*(uint8_t*)0x10000011 = 0);
325    NONFAILING(*(uint8_t*)0x10000012 = 0);
326    NONFAILING(*(uint8_t*)0x10000013 = 0);
327    NONFAILING(*(uint8_t*)0x10000014 = 0);
328    NONFAILING(*(uint8_t*)0x10000015 = 0);
329    NONFAILING(*(uint8_t*)0x10000016 = 0);
330    NONFAILING(*(uint8_t*)0x10000017 = 0);
331    NONFAILING(*(uint32_t*)0x10000018 = 0);
332    syscall(SYS_bind, (intptr_t)r[0], 0x10000000, 0x1c);
333    break;
334  case 2:
335    NONFAILING(*(uint8_t*)0x10000180 = 0x5f);
336    NONFAILING(*(uint8_t*)0x10000181 = 0x1c);
337    NONFAILING(*(uint16_t*)0x10000182 = htobe16(0x4e22 + procid * 4));
338    NONFAILING(*(uint32_t*)0x10000184 = 0);
339    NONFAILING(*(uint64_t*)0x10000188 = htobe64(0));
340    NONFAILING(*(uint64_t*)0x10000190 = htobe64(1));
341    NONFAILING(*(uint32_t*)0x10000198 = 0);
342    syscall(SYS_connect, (intptr_t)r[0], 0x10000180, 0x1c);
343    break;
344  case 3:
345    NONFAILING(*(uint32_t*)0x100001c0 = 0x401);
346    syscall(SYS_setsockopt, (intptr_t)r[0], 0x84, 0x1b, 0x100001c0, 4);
347    break;
348  case 4:
349    NONFAILING(memcpy(
350        (void*)0x10000380,
351        "\xa3\x1b\xe1\x78\x8e\x58\x9b\x38\x59\xf3\xbb\xdd\x7e\xf7\x51\x23\x97"
352        "\x31\xb2\x90\x4a\xd0\x4e\xb7\xdc\x37\xc6\x95\xf6\x05\x5c\xa8\x36\x54"
353        "\x7e\x7b\x6c\xc3\x7d\xae\x2a\xe4\x77\x08\x94\x67\x3c\x89\x65\x93\x24"
354        "\x1c\x56\x3e\x08\x69\x05\x35\xeb\x3b\x7f\x19\x7d\xda\x44\x54\xb4\x42"
355        "\x4f\x7f\xf9\xf9\xfe\x4e\xac\xef\xa6\xd4\xb1\x61\x9d\xd9\x0b\x97\x7c"
356        "\xd9\x82\x16\xc9\x7b\x2e\xb3\x9f\x02\xde\x0f\xae\xe7\x0b\xec\xa3\x66"
357        "\x3c\x2e\x6c\xc5\x1d\xa2\xb4\x8b\x4b\x39\x49\xcc\x14\x5a\x50\x62\x0f"
358        "\xd5\x65\xb9\x7c\x5c\xa0\xea\xfc\xa4\xc9\x13\x73\x14\x16\xba\xcc\xae"
359        "\x89\xe2\x68\x01\x06\x94\x78\xa3\xee\xa8\x45\xf7\xc2\xcb\x48\x93\xe5"
360        "\x83\x52\x45\x26\xe3\xeb\x73\xa2\xe4\xf1\x11\xcf\x40\x5f\xef\x99\xc2"
361        "\xa1\xeb\x2c\x96\x70\x56\x88\xc8\xc7\x6b\xa1\x66\xd2\x23\x20\x07\x62"
362        "\x69\xd2\x1c\x52\xbb\x5e\x86\x43\x7d\x6c\x65\x44\x42\xf6\xd8\x45\xe2"
363        "\x00\x24\x00\x00\xf0\x1d\x29\xf6\xd3\x74\x83\x25\x40\x56\x50\x17\x7f"
364        "\xc3\x60\xd7\xed\xb1\xfb\x7a\x74\x38\x2b\x47\x34\x93\x9c\xee\xc9\xb0"
365        "\xbf\x7d\xc4\x19\xe2\x77\x4c\xa2\x71\x9c\x30",
366        249));
367    NONFAILING(*(uint8_t*)0x10000140 = 0x10);
368    NONFAILING(*(uint8_t*)0x10000141 = 2);
369    NONFAILING(*(uint16_t*)0x10000142 = htobe16(0x4e22 + procid * 4));
370    NONFAILING(*(uint32_t*)0x10000144 = htobe32(-1));
371    NONFAILING(*(uint8_t*)0x10000148 = 0);
372    NONFAILING(*(uint8_t*)0x10000149 = 0);
373    NONFAILING(*(uint8_t*)0x1000014a = 0);
374    NONFAILING(*(uint8_t*)0x1000014b = 0);
375    NONFAILING(*(uint8_t*)0x1000014c = 0);
376    NONFAILING(*(uint8_t*)0x1000014d = 0);
377    NONFAILING(*(uint8_t*)0x1000014e = 0);
378    NONFAILING(*(uint8_t*)0x1000014f = 0);
379    syscall(SYS_sendto, (intptr_t)r[0], 0x10000380, 0xfffffcaa, 0x20000,
380            0x10000140, 0x10);
381    break;
382  case 5:
383    NONFAILING(*(uint32_t*)0x10000540 = 0);
384    NONFAILING(*(uint32_t*)0x10000544 = 0);
385    NONFAILING(*(uint32_t*)0x10000548 = 0);
386    NONFAILING(*(uint32_t*)0x1000054c = 0);
387    NONFAILING(*(uint32_t*)0x10000550 = 0);
388    NONFAILING(*(uint32_t*)0x10000554 = 0);
389    NONFAILING(*(uint32_t*)0x10000558 = 0x20008);
390    syscall(SYS_sendmsg, (intptr_t)r[0], 0x10000540, 0x100);
391    break;
392  }
393}
394int main(void)
395{
396  syscall(SYS_mmap, 0x10000000, 0x1000000, 7, 0x1012, -1, 0);
397  install_segv_handler();
398  for (procid = 0; procid < 4; procid++) {
399    if (fork() == 0) {
400      loop();
401    }
402  }
403  sleep(1000000);
404  return 0;
405}
406EOF
407mycc -o /tmp/syzkaller16 -Wall -Wextra -O0 -m32 /tmp/syzkaller16.c -pthread ||
408    exit 1
409
410(cd ../testcases/swap; ./swap -t 1m -i 20 -h > /dev/null 2>&1) &
411(cd /tmp; ./syzkaller16) &
412sleep 60
413pkill -9 syzkaller16
414while pkill swap; do sleep 1; done
415wait
416
417rm -f /tmp/syzkaller16 /tmp/syzkaller16.c /tmp/syzkaller16.core
418exit 0
419