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

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/resource.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 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

  if (unshare(CLONE_NEWNS)) {
  }
  if (unshare(CLONE_NEWIPC)) {
  }
  if (unshare(CLONE_NEWCGROUP)) {
  }
  if (unshare(CLONE_NEWUTS)) {
  }
  if (unshare(CLONE_SYSVSEM)) {
  }
}

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

  sandbox_common();
  if (unshare(CLONE_NEWNET)) {
  }

  loop();
  doexit(1);
}

#ifndef __NR_bpf
#define __NR_bpf 321
#endif

long r[1];
void loop()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000, 0x24000, 3, 0x32, -1, 0);
  *(uint32_t*)0x20020fd4 = 0xb;
  *(uint32_t*)0x20020fd8 = 0x33;
  *(uint32_t*)0x20020fdc = 3;
  *(uint32_t*)0x20020fe0 = 0x677;
  *(uint32_t*)0x20020fe4 = 1;
  *(uint32_t*)0x20020fe8 = 0;
  *(uint32_t*)0x20020fec = 0;
  *(uint8_t*)0x20020ff0 = 0;
  *(uint8_t*)0x20020ff1 = 0;
  *(uint8_t*)0x20020ff2 = 0;
  *(uint8_t*)0x20020ff3 = 0;
  *(uint8_t*)0x20020ff4 = 0;
  *(uint8_t*)0x20020ff5 = 0;
  *(uint8_t*)0x20020ff6 = 0;
  *(uint8_t*)0x20020ff7 = 0;
  *(uint8_t*)0x20020ff8 = 0;
  *(uint8_t*)0x20020ff9 = 0;
  *(uint8_t*)0x20020ffa = 0;
  *(uint8_t*)0x20020ffb = 0;
  *(uint8_t*)0x20020ffc = 0;
  *(uint8_t*)0x20020ffd = 0;
  *(uint8_t*)0x20020ffe = 0;
  *(uint8_t*)0x20020fff = 0;
  r[0] = syscall(__NR_bpf, 0, 0x20020fd4, 0x2c);
  *(uint32_t*)0x2001cfe0 = r[0];
  *(uint64_t*)0x2001cfe8 = 0x20013000;
  *(uint64_t*)0x2001cff0 = 0x20007000;
  *(uint64_t*)0x2001cff8 = 0;
  syscall(__NR_bpf, 2, 0x2001cfe0, 0x20);
  *(uint32_t*)0x2001f000 = r[0];
  *(uint64_t*)0x2001f008 = 0x20013f33;
  *(uint64_t*)0x2001f010 = 0x2001ffd8;
  syscall(__NR_bpf, 4, 0x2001f000, 0x18);
}

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