1#!/bin/sh 2 3# panic: Bad link elm 0xfffff80b62eb7500 prev->next != elm 4# cpuid = 2 5# time = 1598546886 6# KDB: stack backtrace: 7# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe010154a6e0 8# vpanic() at vpanic+0x182/frame 0xfffffe010154a730 9# panic() at panic+0x43/frame 0xfffffe010154a790 10# unp_disconnect() at unp_disconnect+0x279/frame 0xfffffe010154a7c0 11# uipc_disconnect() at uipc_disconnect+0xfd/frame 0xfffffe010154a800 12# soclose() at soclose+0xa2/frame 0xfffffe010154a870 13# _fdrop() at _fdrop+0x1a/frame 0xfffffe010154a890 14# closef() at closef+0x1ea/frame 0xfffffe010154a920 15# fdescfree_fds() at fdescfree_fds+0x8c/frame 0xfffffe010154a980 16# fdescfree() at fdescfree+0x3cd/frame 0xfffffe010154aa50 17# exit1() at exit1+0x487/frame 0xfffffe010154aac0 18# sys_sys_exit() at sys_sys_exit+0xd/frame 0xfffffe010154aad0 19# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe010154abf0 20# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe010154abf0 21# --- syscall (1, FreeBSD ELF64, sys_sys_exit), rip = 0x80045376a, rsp = 0x7fffffffe4c8, rbp = 0x7fffffffe4e0 --- 22 23[ `uname -p` != "amd64" ] && exit 0 24 25# Obtained from markj (syzkaller). 26 27. ../default.cfg 28cat > /tmp/syzkaller23.c <<EOF 29// autogenerated by syzkaller (https://github.com/google/syzkaller) 30 31#define _GNU_SOURCE 32 33#include <sys/types.h> 34 35#include <dirent.h> 36#include <errno.h> 37#include <pthread.h> 38#include <pwd.h> 39#include <setjmp.h> 40#include <signal.h> 41#include <stdarg.h> 42#include <stdbool.h> 43#include <stdint.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <sys/endian.h> 48#include <sys/stat.h> 49#include <sys/syscall.h> 50#include <sys/wait.h> 51#include <time.h> 52#include <unistd.h> 53 54static __thread int skip_segv; 55static __thread jmp_buf segv_env; 56 57static void segv_handler(int sig, siginfo_t* info, void* ctx __unused) 58{ 59 uintptr_t addr = (uintptr_t)info->si_addr; 60 const uintptr_t prog_start = 1 << 20; 61 const uintptr_t prog_end = 100 << 20; 62 int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0; 63 int valid = addr < prog_start || addr > prog_end; 64 if (sig == SIGBUS) { 65 valid = 1; 66 } 67 if (skip && valid) { 68 _longjmp(segv_env, 1); 69 } 70 exit(sig); 71} 72 73static void install_segv_handler(void) 74{ 75 struct sigaction sa; 76 memset(&sa, 0, sizeof(sa)); 77 sa.sa_sigaction = segv_handler; 78 sa.sa_flags = SA_NODEFER | SA_SIGINFO; 79 sigaction(SIGSEGV, &sa, NULL); 80 sigaction(SIGBUS, &sa, NULL); 81} 82 83#define NONFAILING(...) \ 84 { \ 85 __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \ 86 if (_setjmp(segv_env) == 0) { \ 87 __VA_ARGS__; \ 88 } \ 89 __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ 90 } 91 92static void kill_and_wait(int pid, int* status) 93{ 94 kill(pid, SIGKILL); 95 while (waitpid(-1, status, 0) != pid) { 96 } 97} 98 99static void sleep_ms(uint64_t ms) 100{ 101 usleep(ms * 1000); 102} 103 104static uint64_t current_time_ms(void) 105{ 106 struct timespec ts; 107 if (clock_gettime(CLOCK_MONOTONIC, &ts)) 108 exit(1); 109 return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; 110} 111 112static void use_temporary_dir(void) 113{ 114 char tmpdir_template[] = "./syzkaller.XXXXXX"; 115 char* tmpdir = mkdtemp(tmpdir_template); 116 if (!tmpdir) 117 exit(1); 118 if (chmod(tmpdir, 0777)) 119 exit(1); 120 if (chdir(tmpdir)) 121 exit(1); 122} 123 124static void remove_dir(const char* dir) 125{ 126 DIR* dp = opendir(dir); 127 if (dp == NULL) 128 exit(1); 129 struct dirent* ep = 0; 130 while ((ep = readdir(dp))) { 131 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) 132 continue; 133 char filename[FILENAME_MAX]; 134 snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); 135 struct stat st; 136 if (lstat(filename, &st)) 137 exit(1); 138 if (S_ISDIR(st.st_mode)) { 139 remove_dir(filename); 140 continue; 141 } 142 if (unlink(filename)) 143 exit(1); 144 } 145 closedir(dp); 146 if (rmdir(dir)) 147 exit(1); 148} 149 150static void thread_start(void* (*fn)(void*), void* arg) 151{ 152 pthread_t th; 153 pthread_attr_t attr; 154 pthread_attr_init(&attr); 155 pthread_attr_setstacksize(&attr, 128 << 10); 156 int i = 0; 157 for (; i < 100; i++) { 158 if (pthread_create(&th, &attr, fn, arg) == 0) { 159 pthread_attr_destroy(&attr); 160 return; 161 } 162 if (errno == EAGAIN) { 163 usleep(50); 164 continue; 165 } 166 break; 167 } 168 exit(1); 169} 170 171typedef struct { 172 pthread_mutex_t mu; 173 pthread_cond_t cv; 174 int state; 175} event_t; 176 177static void event_init(event_t* ev) 178{ 179 if (pthread_mutex_init(&ev->mu, 0)) 180 exit(1); 181 if (pthread_cond_init(&ev->cv, 0)) 182 exit(1); 183 ev->state = 0; 184} 185 186static void event_reset(event_t* ev) 187{ 188 ev->state = 0; 189} 190 191static void event_set(event_t* ev) 192{ 193 pthread_mutex_lock(&ev->mu); 194 if (ev->state) 195 exit(1); 196 ev->state = 1; 197 pthread_mutex_unlock(&ev->mu); 198 pthread_cond_broadcast(&ev->cv); 199} 200 201static void event_wait(event_t* ev) 202{ 203 pthread_mutex_lock(&ev->mu); 204 while (!ev->state) 205 pthread_cond_wait(&ev->cv, &ev->mu); 206 pthread_mutex_unlock(&ev->mu); 207} 208 209static int event_isset(event_t* ev) 210{ 211 pthread_mutex_lock(&ev->mu); 212 int res = ev->state; 213 pthread_mutex_unlock(&ev->mu); 214 return res; 215} 216 217static int event_timedwait(event_t* ev, uint64_t timeout) 218{ 219 uint64_t start = current_time_ms(); 220 uint64_t now = start; 221 pthread_mutex_lock(&ev->mu); 222 for (;;) { 223 if (ev->state) 224 break; 225 uint64_t remain = timeout - (now - start); 226 struct timespec ts; 227 ts.tv_sec = remain / 1000; 228 ts.tv_nsec = (remain % 1000) * 1000 * 1000; 229 pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); 230 now = current_time_ms(); 231 if (now - start > timeout) 232 break; 233 } 234 int res = ev->state; 235 pthread_mutex_unlock(&ev->mu); 236 return res; 237} 238 239struct thread_t { 240 int created, call; 241 event_t ready, done; 242}; 243 244static struct thread_t threads[16]; 245static void execute_call(int call); 246static int running; 247 248static void* thr(void* arg) 249{ 250 struct thread_t* th = (struct thread_t*)arg; 251 for (;;) { 252 event_wait(&th->ready); 253 event_reset(&th->ready); 254 execute_call(th->call); 255 __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); 256 event_set(&th->done); 257 } 258 return 0; 259} 260 261static void execute_one(void) 262{ 263 int i, call, thread; 264 int collide = 0; 265again: 266 for (call = 0; call < 5; call++) { 267 for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); 268 thread++) { 269 struct thread_t* th = &threads[thread]; 270 if (!th->created) { 271 th->created = 1; 272 event_init(&th->ready); 273 event_init(&th->done); 274 event_set(&th->done); 275 thread_start(thr, th); 276 } 277 if (!event_isset(&th->done)) 278 continue; 279 event_reset(&th->done); 280 th->call = call; 281 __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); 282 event_set(&th->ready); 283 if (collide && (call % 2) == 0) 284 break; 285 event_timedwait(&th->done, 45); 286 break; 287 } 288 } 289 for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) 290 sleep_ms(1); 291 if (!collide) { 292 collide = 1; 293 goto again; 294 } 295} 296 297static void execute_one(void); 298 299#define WAIT_FLAGS 0 300 301static void loop(void) 302{ 303 int iter = 0; 304 for (;; iter++) { 305 char cwdbuf[32]; 306 sprintf(cwdbuf, "./%d", iter); 307 if (mkdir(cwdbuf, 0777)) 308 exit(1); 309 int pid = fork(); 310 if (pid < 0) 311 exit(1); 312 if (pid == 0) { 313 if (chdir(cwdbuf)) 314 exit(1); 315 execute_one(); 316 exit(0); 317 } 318 int status = 0; 319 uint64_t start = current_time_ms(); 320 for (;;) { 321 if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) 322 break; 323 sleep_ms(1); 324 if (current_time_ms() - start < 5 * 1000) 325 continue; 326 kill_and_wait(pid, &status); 327 break; 328 } 329 remove_dir(cwdbuf); 330 } 331} 332 333uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff}; 334 335void execute_call(int call) 336{ 337 intptr_t res = 0; 338 switch (call) { 339 case 0: 340 NONFAILING(memcpy((void*)0x20000000, ".\000", 2)); 341 res = syscall(SYS_open, 0x20000000ul, 0ul, 0ul); 342 if (res != -1) 343 r[0] = res; 344 break; 345 case 1: 346 res = syscall(SYS_socketpair, 1ul, 2ul, 0, 0x20000100ul); 347 if (res != -1) 348 NONFAILING(r[1] = *(uint32_t*)0x20000100); 349 break; 350 case 2: 351 NONFAILING(memcpy( 352 (void*)0x20000180, 353 "\x0a\x01\x2e\x2f\x66\x69\x6c\x65\x30\x00\x46\xd3\xc5\xea\xb2\xf9\xd0" 354 "\x81\x73\xef\x4a\xd0\xe9\x00\x9b\x10\xb2\x6d\x40\xab\xf3\x06\x35\x55" 355 "\x92\x6b\xa7\x48\x51\xed\xf1\x47\x2a\x1c\x57\xda\x98\x4d\x23\x4f\x6d" 356 "\x63\x57\x71\x99\x01\x97\x42\xef\xe9\xd6\x91\x78\x55\x43\x7f\x80\x8b" 357 "\xf3\xfa\x29\xeb\x62\xeb\x2f\x4b\xb5\xcd\x3f\xd7\x4f\x78\x35\xfb\xd3" 358 "\x24\xe3\xc4\x10\x31\x53\xdd\xe7\x9a\x1c\x23\x36\x48\xe4\x7a\xf8\x3a" 359 "\x7e\xd0\x1f\x89\xf7\xb6\xee\xff\x69\x1d\xad\x02\x57\x01\xf2\x52\x93" 360 "\x3e\x6b\xda\x65\x2a\xc7\x6c\x27\xf9\x0a\xd4\x3b\x05\x00\x00\x00\x1b" 361 "\x1c\x89\xc9\x3b\x41\x9c\x7b\x4f\x31\xbb\x6a\x5c\x17\xed\x78\xf1\x64" 362 "\xc4\xa3\x17\x5c\x7a\x8c\x02\x75\x00\xaf\x61\xac\x90\x22\x86\x15\x67" 363 "\x90\x03\x17\x8c\x55\xf6\xb9\x3a\xf8\x6c\xab\xcb\xf1\x74\x09\xc0\xbc" 364 "\xf4\x5b\x76\x33\x8f\x9a\x76\xf3\x6e", 365 196)); 366 syscall(SYS_bindat, r[0], r[1], 0x20000180ul, 0xaul); 367 break; 368 case 3: 369 NONFAILING(*(uint8_t*)0x20000040 = 0xa); 370 NONFAILING(*(uint8_t*)0x20000041 = 1); 371 NONFAILING(memcpy((void*)0x20000042, "./file0\000", 8)); 372 syscall(SYS_connectat, r[0], r[1], 0x20000040ul, 0xaul); 373 break; 374 case 4: 375 NONFAILING(*(uint8_t*)0x20000000 = 0xa); 376 NONFAILING(*(uint8_t*)0x20000001 = 1); 377 NONFAILING(memcpy((void*)0x20000002, "./file0\000", 8)); 378 syscall(SYS_connect, r[1], 0x20000000ul, 0xaul); 379 break; 380 } 381} 382int main(void) 383{ 384 syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul); 385 install_segv_handler(); 386 use_temporary_dir(); 387 loop(); 388 return 0; 389} 390EOF 391mycc -o /tmp/syzkaller23 -Wall -Wextra -O0 /tmp/syzkaller23.c -lpthread || 392 exit 1 393 394for i in `jot 256`; do 395 (cd /tmp; timeout 3m ./syzkaller23) & 396done 397wait 398 399rm -rf /tmp/syzkaller23 /tmp/syzkaller23.c /tmp/syzkaller.* 400exit 0 401