1#!/bin/sh 2 3# 4# Copyright (c) 2011-2013 Peter Holm 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29# Threaded syscall(2) fuzz test inspired by the iknowthis test suite 30# by Tavis Ormandy <taviso cmpxchg8b com> 31 32# Usage: syscall4.sh [syscall number] 33# Without an argument random syscall numbers are tested. 34# With an argument only the specified syscall number is tested. 35 36# Sample problems found: 37# Thread stuck in stopprof. 38# http://people.freebsd.org/~pho/stress/log/kostik732.txt 39# Fixed by r275121. 40 41# panic: td 0xcbe1ac40 is not suspended. 42# https://people.freebsd.org/~pho/stress/log/kostik807.txt 43# Fixed by r282944. 44 45[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 46 47. ../default.cfg 48 49odir=`pwd` 50cd /tmp 51sed '1,/^EOF/d' < $odir/$0 > syscall4.c 52sed -i '' -e "s#MNTPOINT#$mntpoint#" syscall4.c 53rm -f /tmp/syscall4 54mycc -o syscall4 -Wall -Wextra -O2 -g syscall4.c -lpthread || exit 1 55rm -f syscall4.c 56 57kldstat -v | grep -q sysvmsg || $stress2tools/kldload.sh sysvmsg 58kldstat -v | grep -q sysvsem || $stress2tools/kldload.sh sysvsem 59kldstat -v | grep -q sysvshm || $stress2tools/kldload.sh sysvshm 60kldstat -v | grep -q aio || $stress2tools/kldload.sh aio 61kldstat -v | grep -q mqueuefs || $stress2tools/kldload.sh mqueuefs 62 63mount | grep -q "on $mntpoint " && umount -f $mntpoint 64[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 65 66mdconfig -a -t swap -s 2g -u $mdstart || exit 1 67newfs $newfs_flags -n md$mdstart > /dev/null 68mount /dev/md$mdstart $mntpoint 69chmod 777 $mntpoint 70 71[ -z "$noswap" ] && 72 daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 10m -i 20 -k)" > \ 73 /dev/null 74sleeptime=${sleeptime:-12} 75st=`date '+%s'` 76while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do 77 (cd $mntpoint; /tmp/syscall4 $* 1>>stdout 2>>stderr) & 78 start=`date '+%s'` 79 while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do 80 pgrep syscall4 > /dev/null || break 81 sleep .5 82 done 83 while pkill -9 syscall4; do :; done 84 wait 85 ipcs | grep nobody | awk '/^(q|m|s)/ {print " -" $1, $2}' | 86 xargs -L 1 ipcrm 87done 88while pkill -9 swap; do :; done 89while pkill -9 syscall4; do :; done 90 91for i in `jot 10`; do 92 mount | grep -q md$mdstart && \ 93 umount $mntpoint && mdconfig -d -u $mdstart && break 94 sleep 10 95done 96if mount | grep -q md$mdstart; then 97 fstat $mntpoint 98 echo "umount $mntpoint failed" 99 exit 1 100fi 101rm -f /tmp/syscall4 102exit 0 103EOF 104#include <sys/types.h> 105#include <sys/event.h> 106#include <sys/resource.h> 107#include <sys/socket.h> 108#include <sys/stat.h> 109#include <sys/syscall.h> 110#include <sys/wait.h> 111 112#include <err.h> 113#include <errno.h> 114#include <fcntl.h> 115#include <fts.h> 116#include <libutil.h> 117#include <pthread.h> 118#if defined(__FreeBSD__) 119#include <pthread_np.h> 120#define __NP__ 121#endif 122#include <pwd.h> 123#include <signal.h> 124#include <stdint.h> 125#include <stdio.h> 126#include <stdlib.h> 127#include <string.h> 128#include <unistd.h> 129 130static int ignore[] = { 131 SYS_syscall, 132 SYS_exit, 133 SYS_fork, 134 11, /* 11 is obsolete execv */ 135 SYS_reboot, 136 SYS_vfork, 137 109, /* 109 is old sigblock */ 138 111, /* 111 is old sigsuspend */ 139 SYS_shutdown, 140 SYS___syscall, 141 216, /* custom syscall */ 142 SYS_rfork, 143 SYS_mac_syscall, 144}; 145 146static int fd[900], fds[2], kq, socketpr[2]; 147#ifndef nitems 148#define nitems(x) (sizeof((x)) / sizeof((x)[0])) 149#endif 150#define N 4096 151#define MAGIC 1664 152#define RUNTIME 120 153#define THREADS 50 154 155static uint32_t r[N]; 156static int magic1, syscallno, magic2; 157 158static int 159random_int(int mi, int ma) 160{ 161 return (arc4random() % (ma - mi + 1) + mi); 162} 163 164static void 165hand(int i __unused) { /* handler */ 166 exit(1); 167} 168 169static unsigned long 170makearg(void) 171{ 172 unsigned int i; 173 unsigned long val; 174 175 val = arc4random(); 176 i = arc4random() % 100; 177 if (i < 20) 178 val = val & 0xff; 179 if (i >= 20 && i < 40) 180 val = val & 0xffff; 181 if (i >= 40 && i < 60) 182 val = (unsigned long)(r) | (val & 0xffff); 183#if defined(__LP64__) 184 if (i >= 60) { 185 val = (val << 32) | arc4random(); 186 if (i > 80) 187 val = val & 0x00007fffffffffffUL; 188 } 189#endif 190 191 return(val); 192} 193 194static void * 195test(void *arg __unused) 196{ 197 FTS *fts; 198 FTSENT *p; 199 time_t start; 200 int ftsoptions, i, numfiles; 201 char *args[] = { 202 "/dev", 203 "/proc", 204 "MNTPOINT", 205 "mnt2", 206 ".", 207 NULL, 208 }; 209 210#ifdef __NP__ 211 pthread_set_name_np(pthread_self(), __func__); 212#endif 213 numfiles = 0; 214 ftsoptions = FTS_PHYSICAL; 215 start = time(NULL); 216 while (time(NULL) - start < 2) { 217 for (i = 0; i < N; i++) 218 r[i] = arc4random(); 219 220 if (pipe(fds) == -1) 221 err(1, "pipe()"); 222 if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, socketpr) == -1) 223 err(1, "socketpair()"); 224 kq = kqueue(); 225 226 if ((fts = fts_open(args, ftsoptions, NULL)) == NULL) 227 err(1, "fts_open"); 228 229 i = 0; 230 while ((p = fts_read(fts)) != NULL) { 231 if (fd[i] > 0) 232 close(fd[i]); 233 if ((fd[i] = open(p->fts_path, O_RDWR)) == -1) 234 if ((fd[i] = open(p->fts_path, O_WRONLY)) == 235 -1) 236 if ((fd[i] = open(p->fts_path, 237 O_RDONLY)) == -1) 238 continue; 239 i++; 240 i = i % nitems(fd); 241 if (numfiles++ < 10) { 242 fprintf(stderr, "%d: pts_path = %s\n", 243 numfiles, p->fts_path); 244 } 245 } 246 247 if (fts_close(fts) == -1) 248 warn("fts_close()"); 249 sleep(1); 250 close(socketpr[0]); 251 close(socketpr[1]); 252 close(fds[0]); 253 close(fds[1]); 254 close(kq); 255 } 256 return(NULL); 257} 258 259static void * 260calls(void *arg __unused) 261{ 262 time_t start; 263 int i __unused, j, num; 264 unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7; 265 266#ifdef __NP__ 267 pthread_set_name_np(pthread_self(), __func__); 268#endif 269 start = time(NULL); 270 for (i = 0; time(NULL) - start < 10; i++) { 271 num = syscallno; 272 while (num == 0) { 273 num = random_int(0, SYS_MAXSYSCALL); 274 for (j = 0; j < (int)nitems(ignore); j++) 275 if (num == ignore[j]) { 276 num = 0; 277 break; 278 } 279 } 280 arg1 = makearg(); 281 arg2 = makearg(); 282 arg3 = makearg(); 283 arg4 = makearg(); 284 arg5 = makearg(); 285 arg6 = makearg(); 286 arg7 = makearg(); 287 288#if 0 /* Debug mode */ 289 fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx," 290 " %lx, %lx)\n", 291 i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7); 292 sleep(2); 293#endif 294 alarm(1); 295 syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7); 296 num = 0; 297 if (magic1 != MAGIC || magic2 != MAGIC) 298 exit(1); 299 } 300 301 return (NULL); 302} 303 304int 305main(int argc, char **argv) 306{ 307 struct passwd *pw; 308 struct rlimit limit; 309 pthread_t rp, cp[THREADS]; 310 time_t start; 311 int e, j; 312 313 magic1 = magic2 = MAGIC; 314 if ((pw = getpwnam("nobody")) == NULL) 315 err(1, "failed to resolve nobody"); 316 317 if (getenv("USE_ROOT") && argc == 2) 318 fprintf(stderr, "Running syscall4 as root for %s.\n", 319 argv[1]); 320 else { 321 if (setgroups(1, &pw->pw_gid) || 322 setegid(pw->pw_gid) || setgid(pw->pw_gid) || 323 seteuid(pw->pw_uid) || setuid(pw->pw_uid)) 324 err(1, "Can't drop privileges to \"nobody\""); 325 endpwent(); 326 } 327 328 limit.rlim_cur = limit.rlim_max = 1000; 329#if defined(RLIMIT_NPTS) 330 if (setrlimit(RLIMIT_NPTS, &limit) < 0) 331 err(1, "setrlimit"); 332#endif 333 334 signal(SIGALRM, hand); 335 signal(SIGILL, hand); 336 signal(SIGFPE, hand); 337 signal(SIGSEGV, hand); 338 signal(SIGBUS, hand); 339 signal(SIGURG, hand); 340 signal(SIGSYS, hand); 341 signal(SIGTRAP, hand); 342 343 if (argc > 2) { 344 fprintf(stderr, "usage: %s [syscall-num]\n", argv[0]); 345 exit(1); 346 } 347 if (argc == 2) { 348 syscallno = atoi(argv[1]); 349 for (j = 0; j < (int)nitems(ignore); j++) 350 if (syscallno == ignore[j]) 351 errx(0, "syscall #%d is on the ignore list.", 352 syscallno); 353 } 354 355 if (daemon(1, 1) == -1) 356 err(1, "daemon()"); 357 358 system("touch aaa bbb ccc; mkdir -p ddd"); 359 start = time(NULL); 360 while ((time(NULL) - start) < RUNTIME) { 361 if (fork() == 0) { 362 if ((e = pthread_create(&rp, NULL, test, NULL)) != 0) 363 errc(1, e, "pthread_create"); 364 usleep(1000); 365 for (j = 0; j < THREADS; j++) 366 if ((e = pthread_create(&cp[j], NULL, calls, 367 NULL)) != 0) 368 errc(1, e, "pthread_create"); 369 for (j = 0; j < THREADS; j++) 370 pthread_join(cp[j], NULL); 371 372 if ((e = pthread_kill(rp, SIGINT)) != 0) 373 errc(1, e, "pthread_kill"); 374 exit(0); 375 } 376 wait(NULL); 377 usleep(10000); 378 } 379 380 return (0); 381} 382