// https://syzkaller.appspot.com/bug?id=f766e5c6b61f80f26829d370bacb752b929d1777
// autogenerated by syzkaller (http://github.com/google/syzkaller)

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>

__attribute__((noreturn)) static void doexit(int status)
{
  volatile unsigned i;
  syscall(__NR_exit_group, status);
  for (i = 0;; i++) {
  }
}
#include <stdint.h>
#include <string.h>

const int kFailStatus = 67;
const int kRetryStatus = 69;

static void fail(const char* msg, ...)
{
  int e = errno;
  va_list args;
  va_start(args, msg);
  vfprintf(stderr, msg, args);
  va_end(args);
  fprintf(stderr, " (errno %d)\n", e);
  doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus);
}

static uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2)
{
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1,
            (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char* hash;
    strncpy(buf, (char*)a0, sizeof(buf));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

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 = 8 << 20;
  setrlimit(RLIMIT_MEMLOCK, &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);

#define CLONE_NEWCGROUP 0x02000000

  unshare(CLONE_NEWNS);
  unshare(CLONE_NEWIPC);
  unshare(CLONE_NEWCGROUP);
  unshare(CLONE_NEWNET);
  unshare(CLONE_NEWUTS);
  unshare(CLONE_SYSVSEM);
}

static int do_sandbox_none(int executor_pid, bool enable_tun)
{
  unshare(CLONE_NEWPID);
  int pid = fork();
  if (pid < 0)
    fail("sandbox fork failed");
  if (pid)
    return pid;

  sandbox_common();

  loop();
  doexit(1);
}

#ifndef __NR_socket
#define __NR_socket 359
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_write
#define __NR_write 4
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

long r[2];
void loop()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0);
  r[0] = syscall(__NR_socket, 2, 1, 0);
  memcpy((void*)0x20a9dff3, "/dev/net/tun", 13);
  r[1] = syz_open_dev(0x20a9dff3, 0, 2);
  memcpy((void*)0x20f32000,
         "\x65\x71\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         16);
  *(uint32_t*)0x20f32010 = 9;
  *(uint32_t*)0x20f32014 = 0;
  *(uint32_t*)0x20f32018 = 0x2042ffe8;
  *(uint32_t*)0x2042ffe8 = 0;
  *(uint32_t*)0x2042ffec = 0;
  *(uint32_t*)0x2042fff0 = 0;
  *(uint32_t*)0x2042fff4 = 0;
  *(uint32_t*)0x2042fff8 = 0;
  *(uint16_t*)0x2042fffc = 0;
  *(uint16_t*)0x2042fffe = 0;
  syscall(__NR_ioctl, r[1], 0x400454ca, 0x20f32000);
  memcpy((void*)0x20fd8fe0,
         "\x65\x71\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         16);
  *(uint16_t*)0x20fd8ff0 = 0x301;
  syscall(__NR_ioctl, r[0], 0x8914, 0x20fd8fe0);
  *(uint16_t*)0x20b61fd2 = 0;
  *(uint16_t*)0x20b61fd4 = htobe16(0xc);
  *(uint8_t*)0x20b61fd6 = 0;
  *(uint8_t*)0x20b61fd7 = 0;
  *(uint8_t*)0x20b61fd8 = 0;
  *(uint8_t*)0x20b61fd9 = 0;
  *(uint8_t*)0x20b61fda = 0;
  *(uint8_t*)0x20b61fdb = 0;
  *(uint8_t*)0x20b61fdc = 0xaa;
  *(uint8_t*)0x20b61fdd = 0xaa;
  *(uint8_t*)0x20b61fde = 0xaa;
  *(uint8_t*)0x20b61fdf = 0xaa;
  *(uint8_t*)0x20b61fe0 = 0;
  *(uint8_t*)0x20b61fe1 = 0xbb;
  *(uint16_t*)0x20b61fe2 = htobe16(0x806);
  *(uint16_t*)0x20b61fe4 = htobe16(1);
  *(uint16_t*)0x20b61fe6 = htobe16(0x800);
  *(uint8_t*)0x20b61fe8 = 6;
  *(uint8_t*)0x20b61fe9 = 4;
  *(uint16_t*)0x20b61fea = htobe16(0xb);
  *(uint8_t*)0x20b61fec = 0xaa;
  *(uint8_t*)0x20b61fed = 0xaa;
  *(uint8_t*)0x20b61fee = 0xaa;
  *(uint8_t*)0x20b61fef = 0xaa;
  *(uint8_t*)0x20b61ff0 = 0;
  *(uint8_t*)0x20b61ff1 = 0xbb;
  *(uint32_t*)0x20b61ff2 = htobe32(3);
  *(uint8_t*)0x20b61ff6 = 0xaa;
  *(uint8_t*)0x20b61ff7 = 0xaa;
  *(uint8_t*)0x20b61ff8 = 0xaa;
  *(uint8_t*)0x20b61ff9 = 0xaa;
  *(uint8_t*)0x20b61ffa = 0;
  *(uint8_t*)0x20b61ffb = 0x10;
  *(uint32_t*)0x20b61ffc = htobe32(-1);
  syscall(__NR_write, r[1], 0x20b61fd2, 0x2e);
}

int main()
{
  int pid = do_sandbox_none(0, false);
  int status = 0;
  while (waitpid(pid, &status, __WALL) != pid) {
  }
  return 0;
}