Skip to content

Instantly share code, notes, and snippets.

@srikanth007m
Created February 3, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikanth007m/9c0236dac6cf2e8b1969899f611a99a6 to your computer and use it in GitHub Desktop.
Save srikanth007m/9c0236dac6cf2e8b1969899f611a99a6 to your computer and use it in GitHub Desktop.
// autogenerated by syzkaller (http://github.com/google/syzkaller)
#ifndef __NR_mmap
#define __NR_mmap 9
#endif
#ifndef __NR_openat
#define __NR_openat 257
#endif
#ifndef __NR_getsockopt
#define __NR_getsockopt 55
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 16
#endif
#ifndef __NR_setsockopt
#define __NR_setsockopt 54
#endif
#ifndef __NR_socketpair
#define __NR_socketpair 53
#endif
#ifndef __NR_getpid
#define __NR_getpid 39
#endif
#ifndef __NR_ptrace
#define __NR_ptrace 101
#endif
#ifndef __NR_get_robust_list
#define __NR_get_robust_list 274
#endif
#ifndef __NR_getpeername
#define __NR_getpeername 52
#endif
#ifndef __NR_fcntl
#define __NR_fcntl 72
#endif
#define _GNU_SOURCE
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/capability.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/kvm.h>
#include <linux/sched.h>
#include <net/if_arp.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
const int kFailStatus = 67;
const int kErrorStatus = 68;
const int kRetryStatus = 69;
__attribute__((noreturn)) void doexit(int status)
{
volatile unsigned i;
syscall(__NR_exit_group, status);
for (i = 0;; i++) {
}
}
__attribute__((noreturn)) void fail(const char* msg, ...)
{
int e = errno;
fflush(stdout);
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " (errno %d)\n", e);
doexit(e == ENOMEM ? kRetryStatus : kFailStatus);
}
__attribute__((noreturn)) void exitf(const char* msg, ...)
{
int e = errno;
fflush(stdout);
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " (errno %d)\n", e);
doexit(kRetryStatus);
}
static int flag_debug;
void debug(const char* msg, ...)
{
if (!flag_debug)
return;
va_list args;
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fflush(stdout);
}
__thread int skip_segv;
__thread jmp_buf segv_env;
static void segv_handler(int sig, siginfo_t* info, void* uctx)
{
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
(addr < prog_start || addr > prog_end)) {
debug("SIGSEGV on %p, skipping\n", addr);
_longjmp(segv_env, 1);
}
debug("SIGSEGV on %p, exiting\n", addr);
doexit(sig);
for (;;) {
}
}
static void install_segv_handler()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#define NONFAILING(...) \
{ \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
}
#define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1)
#define BITMASK_LEN_OFF(type, bf_off, bf_len) \
(type)(BITMASK_LEN(type, (bf_len)) << (bf_off))
#define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len) \
if ((bf_off) == 0 && (bf_len) == 0) { \
*(type*)(addr) = (type)(val); \
} else { \
type new_val = *(type*)(addr); \
new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len)); \
new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off); \
*(type*)(addr) = new_val; \
}
static uintptr_t execute_syscall(int nr, uintptr_t a0, uintptr_t a1,
uintptr_t a2, uintptr_t a3,
uintptr_t a4, uintptr_t a5,
uintptr_t a6, uintptr_t a7,
uintptr_t a8)
{
switch (nr) {
default:
return syscall(nr, a0, a1, a2, a3, a4, a5);
}
}
static void setup_main_process()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
install_segv_handler();
char tmpdir_template[] = "./syzkaller.XXXXXX";
char* tmpdir = mkdtemp(tmpdir_template);
if (!tmpdir)
fail("failed to mkdtemp");
if (chmod(tmpdir, 0777))
fail("failed to chmod");
if (chdir(tmpdir))
fail("failed to chdir");
}
static void loop();
static void sandbox_common()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setsid();
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = 128 << 20;
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
unshare(CLONE_NEWNS);
unshare(CLONE_NEWIPC);
unshare(CLONE_IO);
}
static int do_sandbox_setuid(int executor_pid, bool enable_tun)
{
int pid = fork();
if (pid)
return pid;
sandbox_common();
setup_tun(executor_pid, enable_tun);
const int nobody = 65534;
if (setgroups(0, NULL))
fail("failed to setgroups");
if (syscall(SYS_setresgid, nobody, nobody, nobody))
fail("failed to setresgid");
if (syscall(SYS_setresuid, nobody, nobody, nobody))
fail("failed to setresuid");
loop();
doexit(1);
}
static void remove_dir(const char* dir)
{
DIR* dp;
struct dirent* ep;
int iter = 0;
retry:
dp = opendir(dir);
if (dp == NULL) {
if (errno == EMFILE) {
exitf("opendir(%s) failed due to NOFILE, exiting");
}
exitf("opendir(%s) failed", dir);
}
while ((ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
continue;
char filename[FILENAME_MAX];
snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
struct stat st;
if (lstat(filename, &st))
exitf("lstat(%s) failed", filename);
if (S_ISDIR(st.st_mode)) {
remove_dir(filename);
continue;
}
int i;
for (i = 0;; i++) {
debug("unlink(%s)\n", filename);
if (unlink(filename) == 0)
break;
if (errno == EROFS) {
debug("ignoring EROFS\n");
break;
}
if (errno != EBUSY || i > 100)
exitf("unlink(%s) failed", filename);
debug("umount(%s)\n", filename);
if (umount2(filename, MNT_DETACH))
exitf("umount(%s) failed", filename);
}
}
closedir(dp);
int i;
for (i = 0;; i++) {
debug("rmdir(%s)\n", dir);
if (rmdir(dir) == 0)
break;
if (i < 100) {
if (errno == EROFS) {
debug("ignoring EROFS\n");
break;
}
if (errno == EBUSY) {
debug("umount(%s)\n", dir);
if (umount2(dir, MNT_DETACH))
exitf("umount(%s) failed", dir);
continue;
}
if (errno == ENOTEMPTY) {
if (iter < 100) {
iter++;
goto retry;
}
}
}
exitf("rmdir(%s) failed", dir);
}
}
static uint64_t current_time_ms()
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
fail("clock_gettime failed");
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
static void test();
void loop()
{
int iter;
for (iter = 0;; iter++) {
char cwdbuf[256];
sprintf(cwdbuf, "./%d", iter);
if (mkdir(cwdbuf, 0777))
fail("failed to mkdir");
int pid = fork();
if (pid < 0)
fail("clone failed");
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
if (chdir(cwdbuf))
fail("failed to chdir");
test();
doexit(0);
}
int status = 0;
uint64_t start = current_time_ms();
for (;;) {
int res = waitpid(-1, &status, __WALL | WNOHANG);
if (res == pid)
break;
usleep(1000);
if (current_time_ms() - start > 5 * 1000) {
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
while (waitpid(-1, &status, __WALL) != pid) {
}
break;
}
}
remove_dir(cwdbuf);
}
}
long r[88];
void* thr(void* arg)
{
switch ((long)arg) {
case 0:
r[0] = execute_syscall(__NR_getpid, 0, 0, 0, 0, 0, 0, 0, 0, 0);
break;
case 1:
r[1] = execute_syscall(__NR_ptrace, 0x5ul, r[0], 0x20de0ff8ul,
0x5ul, 0, 0, 0, 0, 0);
break;
case 2:
r[2] =
execute_syscall(__NR_mmap, 0x20000000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 3:
r[3] =
execute_syscall(__NR_mmap, 0x20000000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 4:
NONFAILING(*(uint64_t*)0x20000000 = (uint64_t)0x20b88000);
NONFAILING(*(uint64_t*)0x20000ff8 = (uint64_t)0x18);
r[6] = execute_syscall(__NR_get_robust_list, r[0], 0x20000000ul,
0x20000ff8ul, 0, 0, 0, 0, 0, 0);
break;
case 5:
NONFAILING(memcpy((void*)0x20000ff7,
"\x2f\x64\x65\x76\x2f\x70\x70\x70\x00", 9));
r[8] =
execute_syscall(__NR_openat, 0xffffffffffffff9cul, 0x20000ff7ul,
0x84200ul, 0x0ul, 0, 0, 0, 0, 0);
break;
case 6:
r[9] =
execute_syscall(__NR_mmap, 0x20001000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 7:
NONFAILING(*(uint32_t*)0x20000000 = (uint32_t)0x4);
NONFAILING(*(uint32_t*)0x20000004 = (uint32_t)0xfff);
NONFAILING(*(uint32_t*)0x20000008 = (uint32_t)0x800000000);
NONFAILING(*(uint32_t*)0x20001000 = (uint32_t)0xc);
r[14] = execute_syscall(__NR_getsockopt, r[8], 0x84ul, 0x10ul,
0x20000000ul, 0x20001000ul, 0, 0, 0, 0);
break;
case 8:
r[15] =
execute_syscall(__NR_mmap, 0x20002000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 9:
r[16] = execute_syscall(__NR_ioctl, r[8], 0x540ful, 0x20002000ul, 0,
0, 0, 0, 0, 0);
break;
case 10:
r[17] =
execute_syscall(__NR_mmap, 0x20003000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 11:
NONFAILING(*(uint32_t*)0x20003f10 = (uint32_t)0x0);
NONFAILING(*(uint32_t*)0x20003f20 = (uint32_t)0x100007f);
NONFAILING(*(uint16_t*)0x20003f30 = (uint16_t)0x214e);
NONFAILING(*(uint16_t*)0x20003f32 = (uint16_t)0x5a8fb113);
NONFAILING(*(uint16_t*)0x20003f34 = (uint16_t)0x224e);
NONFAILING(*(uint16_t*)0x20003f36 = (uint16_t)0x0);
NONFAILING(*(uint16_t*)0x20003f38 = (uint16_t)0x9);
NONFAILING(*(uint8_t*)0x20003f3a = (uint8_t)0x40);
NONFAILING(*(uint8_t*)0x20003f3b = (uint8_t)0x0);
NONFAILING(*(uint8_t*)0x20003f3c = (uint8_t)0x7ff);
NONFAILING(*(uint32_t*)0x20003f40 = (uint32_t)0x1000);
NONFAILING(*(uint32_t*)0x20003f44 = (uint32_t)0xfff);
NONFAILING(*(uint64_t*)0x20003f48 = (uint64_t)0x7);
NONFAILING(*(uint64_t*)0x20003f50 = (uint64_t)0xfff);
NONFAILING(*(uint64_t*)0x20003f58 = (uint64_t)0x1);
NONFAILING(*(uint64_t*)0x20003f60 = (uint64_t)0x3);
NONFAILING(*(uint64_t*)0x20003f68 = (uint64_t)0x0);
NONFAILING(*(uint64_t*)0x20003f70 = (uint64_t)0x3);
NONFAILING(*(uint64_t*)0x20003f78 = (uint64_t)0xffff);
NONFAILING(*(uint64_t*)0x20003f80 = (uint64_t)0x100);
NONFAILING(*(uint64_t*)0x20003f88 = (uint64_t)0x7e3);
NONFAILING(*(uint64_t*)0x20003f90 = (uint64_t)0x64be);
NONFAILING(*(uint64_t*)0x20003f98 = (uint64_t)0x3);
NONFAILING(*(uint64_t*)0x20003fa0 = (uint64_t)0x6);
NONFAILING(*(uint32_t*)0x20003fa8 = (uint32_t)0x9);
NONFAILING(*(uint32_t*)0x20003fac = (uint32_t)0x3);
NONFAILING(*(uint8_t*)0x20003fb0 = (uint8_t)0x80000000);
NONFAILING(*(uint8_t*)0x20003fb1 = (uint8_t)0x8);
NONFAILING(*(uint8_t*)0x20003fb2 = (uint8_t)0x1);
NONFAILING(*(uint8_t*)0x20003fb3 = (uint8_t)0x2f98);
NONFAILING(*(uint64_t*)0x20003fb8 = (uint64_t)0x0);
NONFAILING(*(uint64_t*)0x20003fc0 = (uint64_t)0x0);
NONFAILING(*(uint32_t*)0x20003fc8 = (uint32_t)0x1000000000000000);
NONFAILING(*(uint8_t*)0x20003fcc = (uint8_t)0x81);
NONFAILING(*(uint16_t*)0x20003fd0 = (uint16_t)0xba);
NONFAILING(*(uint32_t*)0x20003fd8 = (uint32_t)0x100007f);
NONFAILING(*(uint32_t*)0x20003fe8 = (uint32_t)0x5f);
NONFAILING(*(uint8_t*)0x20003fec = (uint8_t)0x8);
NONFAILING(*(uint8_t*)0x20003fed = (uint8_t)0x5);
NONFAILING(*(uint8_t*)0x20003fee = (uint8_t)0x18000000000);
NONFAILING(*(uint32_t*)0x20003ff0 = (uint32_t)0x3);
NONFAILING(*(uint32_t*)0x20003ff4 = (uint32_t)0x7);
NONFAILING(*(uint32_t*)0x20003ff8 = (uint32_t)0x6);
r[61] = execute_syscall(__NR_setsockopt, r[8], 0x0ul, 0x10ul,
0x20003f10ul, 0xf0ul, 0, 0, 0, 0);
break;
case 12:
r[62] = execute_syscall(__NR_ioctl, r[8], 0x4b4eul, 0x8ul, 0, 0, 0,
0, 0, 0);
break;
case 13:
r[63] =
execute_syscall(__NR_mmap, 0x20004000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 14:
NONFAILING(*(uint32_t*)0x20003000 = (uint32_t)0x4);
r[65] = execute_syscall(__NR_getsockopt, r[8], 0x84ul, 0xcul,
0x20004000ul, 0x20003000ul, 0, 0, 0, 0);
break;
case 15:
r[66] = execute_syscall(__NR_ioctl, r[8], 0x4b32ul, 0x5ul, 0, 0, 0,
0, 0, 0);
break;
case 16:
r[67] =
execute_syscall(__NR_mmap, 0x20005000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 17:
NONFAILING(*(uint32_t*)0x20001000 = (uint32_t)0x4);
r[69] = execute_syscall(__NR_getsockopt, r[8], 0x84ul, 0x21ul,
0x20005ffcul, 0x20001000ul, 0, 0, 0, 0);
break;
case 18:
NONFAILING(*(uint32_t*)0x20004000 = (uint32_t)0x1);
r[71] = execute_syscall(__NR_setsockopt, r[8], 0x84ul, 0x7ul,
0x20004000ul, 0x4ul, 0, 0, 0, 0);
break;
case 19:
r[72] =
execute_syscall(__NR_mmap, 0x20006000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 20:
NONFAILING(*(uint32_t*)0x20003ffc = (uint32_t)0x10);
r[74] = execute_syscall(__NR_getpeername, r[8], 0x20006000ul,
0x20003ffcul, 0, 0, 0, 0, 0, 0);
break;
case 21:
NONFAILING(*(uint32_t*)0x20002ffc = (uint32_t)0x85);
r[76] = execute_syscall(__NR_setsockopt, r[8], 0x84ul, 0xcul,
0x20002ffcul, 0x4ul, 0, 0, 0, 0);
break;
case 22:
NONFAILING(*(uint32_t*)0x20003ff8 = r[8]);
NONFAILING(*(uint32_t*)0x20003ffc = (uint32_t)0x0);
r[79] = execute_syscall(__NR_ioctl, r[8], 0x400443c8ul,
0x20003ff8ul, 0, 0, 0, 0, 0, 0);
break;
case 23:
r[80] = execute_syscall(__NR_ioctl, r[8], 0x2403ul, 0x9ul, 0, 0, 0,
0, 0, 0);
break;
case 24:
r[81] = execute_syscall(__NR_fcntl, r[8], 0x1ul, 0x8ul, 0, 0, 0, 0,
0, 0);
break;
case 25:
r[82] =
execute_syscall(__NR_mmap, 0x20007000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 26:
r[83] =
execute_syscall(__NR_mmap, 0x20007000ul, 0x1000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 27:
NONFAILING(*(uint32_t*)0x20007000 = (uint32_t)0x8);
r[85] = execute_syscall(__NR_getpeername, r[8], 0x20007ff8ul,
0x20007000ul, 0, 0, 0, 0, 0, 0);
break;
case 28:
r[86] = execute_syscall(__NR_fcntl, r[8], 0x10ul, 0x20004000ul, 0,
0, 0, 0, 0, 0);
break;
case 29:
r[87] = execute_syscall(__NR_socketpair, 0xaul, 0x0ul, 0x4ul,
0x20007000ul, 0, 0, 0, 0, 0);
break;
}
return 0;
}
void test()
{
long i;
pthread_t th[60];
memset(r, -1, sizeof(r));
srand(getpid());
for (i = 0; i < 30; i++) {
pthread_create(&th[i], 0, thr, (void*)i);
usleep(10000);
}
for (i = 0; i < 30; i++) {
pthread_create(&th[30 + i], 0, thr, (void*)i);
if (rand() % 2)
usleep(rand() % 10000);
}
usleep(100000);
}
int main()
{
int i;
for (i = 0; i < 8; i++) {
if (fork() == 0) {
setup_main_process();
do_sandbox_setuid(i, false);
return 0;
}
}
sleep(1000000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment