xref: /freebsd/tools/test/stress2/misc/syzkaller74.sh (revision 736e2ab5ef3c84dc9c08351822ff2d8473ae5330)
1#!/bin/sh
2
3# panic: _pctrie_lookup_node: freed node in iter path
4# cpuid = 0
5# time = 1745029155
6# KDB: stack backtrace:
7# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01085d2a30
8# vpanic() at vpanic+0x136/frame 0xfffffe01085d2b60
9# panic() at panic+0x43/frame 0xfffffe01085d2bc0
10# pctrie_iter_lookup_ge() at pctrie_iter_lookup_ge+0x166/frame 0xfffffe01085d2bd0
11# vm_object_page_clean() at vm_object_page_clean+0x22e/frame 0xfffffe01085d2c50
12# vnode_pager_clean_async() at vnode_pager_clean_async+0x48/frame 0xfffffe01085d2c70
13# vinactivef() at vinactivef+0x75/frame 0xfffffe01085d2cb0
14# vput_final() at vput_final+0x29c/frame 0xfffffe01085d2d00
15# vm_map_process_deferred() at vm_map_process_deferred+0xa9/frame 0xfffffe01085d2d20
16# vm_map_remove() at vm_map_remove+0xcb/frame 0xfffffe01085d2d50
17# vmspace_exit() at vmspace_exit+0xa8/frame 0xfffffe01085d2d80
18# exit1() at exit1+0x533/frame 0xfffffe01085d2df0
19# sys_exit() at sys_exit+0xd/frame 0xfffffe01085d2e00
20# amd64_syscall() at amd64_syscall+0x15a/frame 0xfffffe01085d2f30
21# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01085d2f30
22# --- syscall (1, FreeBSD ELF64, exit), rip = 0x822a9b7fa, rsp = 0x8202b4de8, rbp = 0x8202b4e00 ---
23# KDB: enter: panic
24# [ thread pid 21141 tid 100328 ]
25# Stopped at      kdb_enter+0x33: movq    $0,0x104d862(%rip)
26# db>
27
28[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
29
30. ../default.cfg
31set -u
32prog=$(basename "$0" .sh)
33cat > /tmp/$prog.c <<EOF
34// https://syzkaller.appspot.com/bug?id=8658267170f7f61f4317bb04c1fe9add379ffaf4
35// autogenerated by syzkaller (https://github.com/google/syzkaller)
36// syzbot+b5f9ebf4c2c63a5db681@syzkaller.appspotmail.com
37
38#define _GNU_SOURCE
39
40#include <sys/types.h>
41
42#include <dirent.h>
43#include <errno.h>
44#include <pthread.h>
45#include <pwd.h>
46#include <signal.h>
47#include <stdarg.h>
48#include <stdbool.h>
49#include <stdint.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <sys/endian.h>
54#include <sys/resource.h>
55#include <sys/stat.h>
56#include <sys/syscall.h>
57#include <sys/wait.h>
58#include <time.h>
59#include <unistd.h>
60
61static void kill_and_wait(int pid, int* status)
62{
63  kill(pid, SIGKILL);
64  while (waitpid(-1, status, 0) != pid) {
65  }
66}
67
68static void sleep_ms(uint64_t ms)
69{
70  usleep(ms * 1000);
71}
72
73static uint64_t current_time_ms(void)
74{
75  struct timespec ts;
76  if (clock_gettime(CLOCK_MONOTONIC, &ts))
77    exit(1);
78  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
79}
80
81static void use_temporary_dir(void)
82{
83  char tmpdir_template[] = "./syzkaller.XXXXXX";
84  char* tmpdir = mkdtemp(tmpdir_template);
85  if (!tmpdir)
86    exit(1);
87  if (chmod(tmpdir, 0777))
88    exit(1);
89  if (chdir(tmpdir))
90    exit(1);
91}
92
93static void reset_flags(const char* filename)
94{
95  struct stat st;
96  if (lstat(filename, &st))
97    exit(1);
98  st.st_flags &= ~(SF_NOUNLINK | UF_NOUNLINK | SF_IMMUTABLE | UF_IMMUTABLE |
99                   SF_APPEND | UF_APPEND);
100  if (lchflags(filename, st.st_flags))
101    exit(1);
102}
103static void __attribute__((noinline)) remove_dir(const char* dir)
104{
105  DIR* dp = opendir(dir);
106  if (dp == NULL) {
107    if (errno == EACCES) {
108      if (rmdir(dir))
109        exit(1);
110      return;
111    }
112    exit(1);
113  }
114  struct dirent* ep = 0;
115  while ((ep = readdir(dp))) {
116    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
117      continue;
118    char filename[FILENAME_MAX];
119    snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
120    struct stat st;
121    if (lstat(filename, &st))
122      exit(1);
123    if (S_ISDIR(st.st_mode)) {
124      remove_dir(filename);
125      continue;
126    }
127    if (unlink(filename)) {
128      if (errno == EPERM) {
129        reset_flags(filename);
130        reset_flags(dir);
131        if (unlink(filename) == 0)
132          continue;
133      }
134      exit(1);
135    }
136  }
137  closedir(dp);
138  while (rmdir(dir)) {
139    if (errno == EPERM) {
140      reset_flags(dir);
141      if (rmdir(dir) == 0)
142        break;
143    }
144    exit(1);
145  }
146}
147
148static void thread_start(void* (*fn)(void*), void* arg)
149{
150  pthread_t th;
151  pthread_attr_t attr;
152  pthread_attr_init(&attr);
153  pthread_attr_setstacksize(&attr, 128 << 10);
154  int i = 0;
155  for (; i < 100; i++) {
156    if (pthread_create(&th, &attr, fn, arg) == 0) {
157      pthread_attr_destroy(&attr);
158      return;
159    }
160    if (errno == EAGAIN) {
161      usleep(50);
162      continue;
163    }
164    break;
165  }
166  exit(1);
167}
168
169typedef struct {
170  pthread_mutex_t mu;
171  pthread_cond_t cv;
172  int state;
173} event_t;
174
175static void event_init(event_t* ev)
176{
177  if (pthread_mutex_init(&ev->mu, 0))
178    exit(1);
179  if (pthread_cond_init(&ev->cv, 0))
180    exit(1);
181  ev->state = 0;
182}
183
184static void event_reset(event_t* ev)
185{
186  ev->state = 0;
187}
188
189static void event_set(event_t* ev)
190{
191  pthread_mutex_lock(&ev->mu);
192  if (ev->state)
193    exit(1);
194  ev->state = 1;
195  pthread_mutex_unlock(&ev->mu);
196  pthread_cond_broadcast(&ev->cv);
197}
198
199static void event_wait(event_t* ev)
200{
201  pthread_mutex_lock(&ev->mu);
202  while (!ev->state)
203    pthread_cond_wait(&ev->cv, &ev->mu);
204  pthread_mutex_unlock(&ev->mu);
205}
206
207static int event_isset(event_t* ev)
208{
209  pthread_mutex_lock(&ev->mu);
210  int res = ev->state;
211  pthread_mutex_unlock(&ev->mu);
212  return res;
213}
214
215static int event_timedwait(event_t* ev, uint64_t timeout)
216{
217  uint64_t start = current_time_ms();
218  uint64_t now = start;
219  pthread_mutex_lock(&ev->mu);
220  for (;;) {
221    if (ev->state)
222      break;
223    uint64_t remain = timeout - (now - start);
224    struct timespec ts;
225    ts.tv_sec = remain / 1000;
226    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
227    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
228    now = current_time_ms();
229    if (now - start > timeout)
230      break;
231  }
232  int res = ev->state;
233  pthread_mutex_unlock(&ev->mu);
234  return res;
235}
236
237static void sandbox_common()
238{
239  struct rlimit rlim;
240  rlim.rlim_cur = rlim.rlim_max = 128 << 20;
241  setrlimit(RLIMIT_AS, &rlim);
242  rlim.rlim_cur = rlim.rlim_max = 8 << 20;
243  setrlimit(RLIMIT_MEMLOCK, &rlim);
244  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
245  setrlimit(RLIMIT_FSIZE, &rlim);
246  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
247  setrlimit(RLIMIT_STACK, &rlim);
248  rlim.rlim_cur = rlim.rlim_max = 0;
249  setrlimit(RLIMIT_CORE, &rlim);
250  rlim.rlim_cur = rlim.rlim_max = 256;
251  setrlimit(RLIMIT_NOFILE, &rlim);
252}
253
254static void loop();
255
256static int do_sandbox_none(void)
257{
258  sandbox_common();
259  loop();
260  return 0;
261}
262
263struct thread_t {
264  int created, call;
265  event_t ready, done;
266};
267
268static struct thread_t threads[16];
269static void execute_call(int call);
270static int running;
271
272static void* thr(void* arg)
273{
274  struct thread_t* th = (struct thread_t*)arg;
275  for (;;) {
276    event_wait(&th->ready);
277    event_reset(&th->ready);
278    execute_call(th->call);
279    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
280    event_set(&th->done);
281  }
282  return 0;
283}
284
285static void execute_one(void)
286{
287  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
288  }
289  int i, call, thread;
290  for (call = 0; call < 12; call++) {
291    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
292         thread++) {
293      struct thread_t* th = &threads[thread];
294      if (!th->created) {
295        th->created = 1;
296        event_init(&th->ready);
297        event_init(&th->done);
298        event_set(&th->done);
299        thread_start(thr, th);
300      }
301      if (!event_isset(&th->done))
302        continue;
303      event_reset(&th->done);
304      th->call = call;
305      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
306      event_set(&th->ready);
307      event_timedwait(&th->done, 50);
308      break;
309    }
310  }
311  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
312    sleep_ms(1);
313}
314
315static void execute_one(void);
316
317#define WAIT_FLAGS 0
318
319static void loop(void)
320{
321  int iter = 0;
322  for (;; iter++) {
323    char cwdbuf[32];
324    sprintf(cwdbuf, "./%d", iter);
325    if (mkdir(cwdbuf, 0777))
326      exit(1);
327    int pid = fork();
328    if (pid < 0)
329      exit(1);
330    if (pid == 0) {
331      if (chdir(cwdbuf))
332        exit(1);
333      execute_one();
334      exit(0);
335    }
336    int status = 0;
337    uint64_t start = current_time_ms();
338    for (;;) {
339      sleep_ms(10);
340      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
341        break;
342      if (current_time_ms() - start < 5000)
343        continue;
344      kill_and_wait(pid, &status);
345      break;
346    }
347    remove_dir(cwdbuf);
348  }
349}
350
351uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
352                 0xffffffffffffffff, 0xffffffffffffffff};
353
354void execute_call(int call)
355{
356  intptr_t res = 0;
357  switch (call) {
358  case 0:
359    memcpy((void*)0x200000000000, "./file0\000", 8);
360    res = syscall(SYS_openat, /*fd=*/0xffffff9c, /*file=*/0x200000000000ul,
361                  /*flags=O_VERIFY|O_CREAT|O_WRONLY*/ 0x200201ul, /*mode=*/0ul);
362    if (res != -1)
363      r[0] = res;
364    break;
365  case 1:
366    *(uint32_t*)0x200000000240 = r[0];
367    *(uint64_t*)0x200000000248 = 0x80000001;
368    *(uint64_t*)0x200000000250 = 0x2000000004c0;
369    memcpy((void*)0x2000000004c0, "\x45\x09\xee\x8f\xcd", 5);
370    *(uint64_t*)0x200000000258 = 5;
371    *(uint32_t*)0x200000000260 = 3;
372    *(uint32_t*)0x200000000264 = 0;
373    *(uint64_t*)0x200000000268 = 0x200000000000000;
374    *(uint32_t*)0x200000000270 = 8;
375    *(uint32_t*)0x200000000274 = 0;
376    *(uint64_t*)0x200000000278 = 1;
377    *(uint64_t*)0x200000000280 = 7;
378    *(uint64_t*)0x200000000288 = 0;
379    *(uint32_t*)0x200000000290 = 0;
380    *(uint32_t*)0x200000000294 = 0x20000005;
381    *(uint32_t*)0x200000000298 = 0x2e5562f1;
382    *(uint16_t*)0x2000000002a0 = 0xc088;
383    syscall(SYS_aio_write, /*iocb=*/0x200000000240ul);
384    break;
385  case 2:
386    memcpy((void*)0x200000000480, "./file0\000", 8);
387    res = syscall(SYS_open, /*file=*/0x200000000480ul,
388                  /*flags=O_VERIFY*/ 0x200000ul,
389                  /*mode=S_IWOTH|S_IWGRP|S_IXUSR|S_IWUSR*/ 0xd2ul);
390    if (res != -1)
391      r[1] = res;
392    break;
393  case 3:
394    syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x200000ul,
395            /*prot=PROT_WRITE|PROT_READ*/ 3ul,
396            /*flags=MAP_FIXED|MAP_PRIVATE*/ 0x12ul, /*fd=*/r[1],
397            /*offset=*/0ul);
398    break;
399  case 4:
400    memcpy((void*)0x200000000100, "./file0\000", 8);
401    res = syscall(SYS_open, /*file=*/0x200000000100ul,
402                  /*flags=O_DIRECT*/ 0x10000ul, /*mode=*/0ul);
403    if (res != -1)
404      r[2] = res;
405    break;
406  case 5:
407    memcpy((void*)0x2000000016c0, "./file0\000", 8);
408    res = syscall(SYS_open, /*file=*/0x2000000016c0ul, /*flags=O_WRONLY*/ 1ul,
409                  /*mode=*/0ul);
410    if (res != -1)
411      r[3] = res;
412    break;
413  case 6:
414    *(uint64_t*)0x200000000080 = 0x2000000006c0;
415    *(uint64_t*)0x200000000088 = 0x100000;
416    syscall(SYS_pwritev, /*fd=*/r[3], /*vec=*/0x200000000080ul, /*vlen=*/1ul,
417            /*off=*/0ul);
418    break;
419  case 7:
420    *(uint64_t*)0x200000001780 = 0x200000000180;
421    *(uint64_t*)0x200000001788 = 0x1b133353141e377d;
422    syscall(SYS_preadv, /*fd=*/r[2], /*vec=*/0x200000001780ul,
423            /*vlen=*/0x10000000000000d1ul, /*off=*/0ul);
424    break;
425  case 8:
426    memcpy((void*)0x200000000480, "./file0\000", 8);
427    res = syscall(
428        SYS_open, /*file=*/0x200000000480ul,
429        /*flags=O_NONBLOCK|O_CREAT|O_RDWR|0x80400000000000*/ 0x80400000000206ul,
430        /*mode=*/0ul);
431    if (res != -1)
432      r[4] = res;
433    break;
434  case 9:
435    syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x200000ul,
436            /*prot=PROT_WRITE|PROT_READ*/ 3ul,
437            /*flags=MAP_FIXED|MAP_SHARED|0x20000*/ 0x20011ul, /*fd=*/r[4],
438            /*offset=*/0ul);
439    break;
440  case 10:
441    syscall(SYS_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/6,
442            /*optname=TCP_LOGID*/ 0x24, /*optval=*/0ul, /*optlen=*/0ul);
443    break;
444  case 11:
445    syscall(SYS_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/6,
446            /*optname=TCP_LOGID*/ 0x24, /*optval=*/0ul, /*optlen=*/0ul);
447    break;
448  }
449}
450int main(void)
451{
452  syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
453          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
454          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x1012ul,
455          /*fd=*/(intptr_t)-1, /*offset=*/0ul);
456  const char* reason;
457  (void)reason;
458  use_temporary_dir();
459  do_sandbox_none();
460  return 0;
461}
462EOF
463mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1
464
465cd /tmp
466timeout 3m /tmp/$prog > /dev/null 2>&1
467
468rm -rf /tmp/$prog /tmp/$prog.c /tmp/$prog.core /tmp/$prog.??????
469exit 0
470