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

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <linux/capability.h>

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

static bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);
  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

static void setup_common()
{
  if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 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 = (200 << 20);
  setrlimit(RLIMIT_AS, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 32 << 20;
  setrlimit(RLIMIT_MEMLOCK, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 136 << 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);
  rlim.rlim_cur = rlim.rlim_max = 256;
  setrlimit(RLIMIT_NOFILE, &rlim);
  if (unshare(CLONE_NEWNS)) {
  }
  if (unshare(CLONE_NEWIPC)) {
  }
  if (unshare(0x02000000)) {
  }
  if (unshare(CLONE_NEWUTS)) {
  }
  if (unshare(CLONE_SYSVSEM)) {
  }
  typedef struct {
    const char* name;
    const char* value;
  } sysctl_t;
  static const sysctl_t sysctls[] = {
      {"/proc/sys/kernel/shmmax", "16777216"},
      {"/proc/sys/kernel/shmall", "536870912"},
      {"/proc/sys/kernel/shmmni", "1024"},
      {"/proc/sys/kernel/msgmax", "8192"},
      {"/proc/sys/kernel/msgmni", "1024"},
      {"/proc/sys/kernel/msgmnb", "1024"},
      {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
  };
  unsigned i;
  for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
    write_file(sysctls[i].name, sysctls[i].value);
}

int wait_for_loop(int pid)
{
  if (pid < 0)
    exit(1);
  int status = 0;
  while (waitpid(-1, &status, __WALL) != pid) {
  }
  return WEXITSTATUS(status);
}

static void drop_caps(void)
{
  struct __user_cap_header_struct cap_hdr = {};
  struct __user_cap_data_struct cap_data[2] = {};
  cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
  cap_hdr.pid = getpid();
  if (syscall(SYS_capget, &cap_hdr, &cap_data))
    exit(1);
  const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
  cap_data[0].effective &= ~drop;
  cap_data[0].permitted &= ~drop;
  cap_data[0].inheritable &= ~drop;
  if (syscall(SYS_capset, &cap_hdr, &cap_data))
    exit(1);
}

static int do_sandbox_none(void)
{
  if (unshare(CLONE_NEWPID)) {
  }
  int pid = fork();
  if (pid != 0)
    return wait_for_loop(pid);
  setup_common();
  sandbox_common();
  drop_caps();
  if (unshare(CLONE_NEWNET)) {
  }
  loop();
  exit(1);
}

uint64_t r[1] = {0xffffffffffffffff};

void loop(void)
{
  intptr_t res = 0;
  *(uint32_t*)0x20940000 = 2;
  *(uint32_t*)0x20940004 = 0x70;
  *(uint8_t*)0x20940008 = 0x6a;
  *(uint8_t*)0x20940009 = 0;
  *(uint8_t*)0x2094000a = 0;
  *(uint8_t*)0x2094000b = 0;
  *(uint32_t*)0x2094000c = 0;
  *(uint64_t*)0x20940010 = 0;
  *(uint64_t*)0x20940018 = 0;
  *(uint64_t*)0x20940020 = 0;
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 0, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 5, 1, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 2, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 3, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 4, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 5, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 6, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 7, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 8, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 9, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 10, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 11, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 12, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 13, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 14, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 15, 2);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 17, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 18, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 19, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 20, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 21, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 22, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 23, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 24, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 25, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 26, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 27, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 28, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20940028, 0, 29, 35);
  *(uint32_t*)0x20940030 = 0;
  *(uint32_t*)0x20940034 = 0;
  *(uint64_t*)0x20940038 = 0;
  *(uint64_t*)0x20940040 = 0;
  *(uint64_t*)0x20940048 = 0;
  *(uint64_t*)0x20940050 = 0;
  *(uint32_t*)0x20940058 = 0;
  *(uint32_t*)0x2094005c = 0;
  *(uint64_t*)0x20940060 = 0;
  *(uint32_t*)0x20940068 = 0;
  *(uint16_t*)0x2094006c = 0;
  *(uint16_t*)0x2094006e = 0;
  syscall(__NR_perf_event_open, 0x20940000, 0, 0, -1, 0);
  syscall(__NR_clone, 0x2102001ffe, 0, 0x9999999999999999, 0, -1);
  *(uint32_t*)0x2025c000 = 5;
  *(uint32_t*)0x2025c004 = 0x70;
  *(uint8_t*)0x2025c008 = 0;
  *(uint8_t*)0x2025c009 = 0;
  *(uint8_t*)0x2025c00a = 0;
  *(uint8_t*)0x2025c00b = 0;
  *(uint32_t*)0x2025c00c = 0;
  *(uint64_t*)0x2025c010 = 0;
  *(uint64_t*)0x2025c018 = 0;
  *(uint64_t*)0x2025c020 = 0;
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 6, 0, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 1, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 2, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 3, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 4, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 5, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 6, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 7, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 8, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 9, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 10, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 11, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 12, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 13, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 14, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 15, 2);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 17, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0x8000000000000000, 18, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 19, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 20, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 21, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 22, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 8, 23, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 24, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 25, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 9, 26, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 27, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 28, 1);
  STORE_BY_BITMASK(uint64_t, , 0x2025c028, 0, 29, 35);
  *(uint32_t*)0x2025c030 = 0;
  *(uint32_t*)0x2025c034 = 3;
  *(uint64_t*)0x2025c038 = 0;
  *(uint64_t*)0x2025c040 = 1;
  *(uint64_t*)0x2025c048 = 0;
  *(uint64_t*)0x2025c050 = 0;
  *(uint32_t*)0x2025c058 = 0;
  *(uint32_t*)0x2025c05c = 0;
  *(uint64_t*)0x2025c060 = 0;
  *(uint32_t*)0x2025c068 = 0;
  *(uint16_t*)0x2025c06c = 0;
  *(uint16_t*)0x2025c06e = 0;
  res = syscall(__NR_perf_event_open, 0x2025c000, -1, 0, -1, 0);
  if (res != -1)
    r[0] = res;
  syscall(__NR_read, r[0], 0x20000280, 0x2d8);
}
int main(void)
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  do_sandbox_none();
  return 0;
}