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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <setjmp.h>
#include <signal.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 <time.h>
#include <unistd.h>

#include <linux/capability.h>

static unsigned long long procid;

static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
  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)) {
    _longjmp(segv_env, 1);
  }
  exit(sig);
}

static void install_segv_handler(void)
{
  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);
  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);                       \
  }

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

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;
}

#define MAX_FDS 30

static long syz_open_dev(volatile long a0, volatile long a1, volatile long 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;
    NONFAILING(strncpy(buf, (char*)a0, sizeof(buf) - 1));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

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);
}

static 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);
}

static void kill_and_wait(int pid, int* status)
{
  kill(-pid, SIGKILL);
  kill(pid, SIGKILL);
  int i;
  for (i = 0; i < 100; i++) {
    if (waitpid(-1, status, WNOHANG | __WALL) == pid)
      return;
    usleep(1000);
  }
  DIR* dir = opendir("/sys/fs/fuse/connections");
  if (dir) {
    for (;;) {
      struct dirent* ent = readdir(dir);
      if (!ent)
        break;
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
        continue;
      char abort[300];
      snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
               ent->d_name);
      int fd = open(abort, O_WRONLY);
      if (fd == -1) {
        continue;
      }
      if (write(fd, abort, 1) < 0) {
      }
      close(fd);
    }
    closedir(dir);
  } else {
  }
  while (waitpid(-1, status, __WALL) != pid) {
  }
}

static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  write_file("/proc/self/oom_score_adj", "1000");
}

static void close_fds()
{
  int fd;
  for (fd = 3; fd < MAX_FDS; fd++)
    close(fd);
}

static void setup_binfmt_misc()
{
  if (mount(0, "/proc/sys/fs/binfmt_misc", "binfmt_misc", 0, 0)) {
  }
  write_file("/proc/sys/fs/binfmt_misc/register", ":syz0:M:0:\x01::./file0:");
  write_file("/proc/sys/fs/binfmt_misc/register",
             ":syz1:M:1:\x02::./file0:POC");
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      setup_test();
      execute_one();
      close_fds();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 5 * 1000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
  }
}

uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  syscall(__NR_perf_event_open, 0ul, 0, 0ul, -1, 0ul);
  NONFAILING(memcpy((void*)0x200002c0, "/dev/kvm\000", 9));
  syscall(__NR_openat, 0xffffffffffffff9cul, 0x200002c0ul, 0ul, 0ul);
  syscall(__NR_ioctl, -1, 0xae60, 0);
  res = syscall(__NR_ioctl, -1, 0xae41, 0ul);
  if (res != -1)
    r[0] = res;
  syscall(__NR_ioctl, r[0], 0xae80, 0ul);
  NONFAILING(memcpy((void*)0x20000180, "/dev/fb0\000", 9));
  res = syscall(__NR_openat, 0xffffffffffffff9cul, 0x20000180ul, 0ul, 0ul);
  if (res != -1)
    r[1] = res;
  syz_open_dev(0, 5, 0x100);
  NONFAILING(*(uint32_t*)0x20000000 = 0x504);
  NONFAILING(*(uint32_t*)0x20000004 = 0);
  NONFAILING(*(uint32_t*)0x20000008 = 0);
  NONFAILING(*(uint32_t*)0x2000000c = 0xf0);
  NONFAILING(*(uint32_t*)0x20000010 = 0);
  NONFAILING(*(uint32_t*)0x20000014 = 0);
  NONFAILING(*(uint32_t*)0x20000018 = 4);
  NONFAILING(*(uint32_t*)0x2000001c = 0);
  NONFAILING(*(uint32_t*)0x20000020 = 0);
  NONFAILING(*(uint32_t*)0x20000024 = 0);
  NONFAILING(*(uint32_t*)0x20000028 = 0);
  NONFAILING(*(uint32_t*)0x2000002c = 0);
  NONFAILING(*(uint32_t*)0x20000030 = 0);
  NONFAILING(*(uint32_t*)0x20000034 = 0);
  NONFAILING(*(uint32_t*)0x20000038 = 0);
  NONFAILING(*(uint32_t*)0x2000003c = 0);
  NONFAILING(*(uint32_t*)0x20000040 = 0);
  NONFAILING(*(uint32_t*)0x20000044 = 0);
  NONFAILING(*(uint32_t*)0x20000048 = 0);
  NONFAILING(*(uint32_t*)0x2000004c = 0);
  NONFAILING(*(uint32_t*)0x20000050 = 3);
  NONFAILING(*(uint32_t*)0x20000054 = 0);
  NONFAILING(*(uint32_t*)0x20000058 = 0);
  NONFAILING(*(uint32_t*)0x2000005c = 0);
  NONFAILING(*(uint32_t*)0x20000060 = 0);
  NONFAILING(*(uint32_t*)0x20000064 = 0);
  NONFAILING(*(uint32_t*)0x20000068 = 0);
  NONFAILING(*(uint32_t*)0x2000006c = 0);
  NONFAILING(*(uint32_t*)0x20000070 = 0);
  NONFAILING(*(uint32_t*)0x20000074 = 0);
  NONFAILING(*(uint32_t*)0x20000078 = 0);
  NONFAILING(*(uint32_t*)0x2000007c = 0);
  NONFAILING(*(uint32_t*)0x20000080 = 0);
  NONFAILING(*(uint32_t*)0x20000084 = 0);
  NONFAILING(*(uint32_t*)0x20000088 = 0);
  NONFAILING(*(uint32_t*)0x2000008c = 0);
  NONFAILING(*(uint32_t*)0x20000090 = 0);
  NONFAILING(*(uint32_t*)0x20000094 = 0);
  NONFAILING(*(uint32_t*)0x20000098 = 0);
  NONFAILING(*(uint32_t*)0x2000009c = 0);
  syscall(__NR_ioctl, r[1], 0x4601, 0x20000000ul);
  NONFAILING(memcpy((void*)0x20000000, "./file0\000", 8));
  syscall(__NR_utimensat, -1, 0x20000000ul, 0ul, 0x100ul);
  res = syz_open_dev(0xc, 4, 1);
  if (res != -1)
    r[2] = res;
  NONFAILING(*(uint16_t*)0x20000000 = 0x200);
  NONFAILING(*(uint16_t*)0x20000002 = 2);
  NONFAILING(*(uint64_t*)0x20000008 = 0x200000c0);
  NONFAILING(memcpy(
      (void*)0x200000c0,
      "\x00\x00\x40\x5a\xf6\xf3\x09\x14\x7a\x2d\xda\xe3\x85\x53\x0f\x98\x1c\xc2"
      "\x7a\x7a\x3e\x9f\x9d\x4a\x0c\x12\x38\x62\x6f\xbb\xf3\xec\xf5\x0f\xee\xcb"
      "\x38\x3e\x98\xe0\xb2\xfb\x66\x9d\x7a\xcb\x26\x20\x94\x6a\x5c\x80\xcb\x07"
      "\x7c\x8a\xbb\xce\x62\xac\xa2\xb4\x6d\x2e\x9c\x53\x41\x72\xdb\xa1\x88\x8a"
      "\x09\xa0\x66\xe7\xa5\xe6\x18\xf0\x18\x48\x59\x00\x20\x0e\x4c\x54\x40\x45"
      "\xce\xa0\xe3\xea\x53\x31\x63\x28\xa6\x0a\xba\xc5\x00\x9d\x42\x50\x16\xff"
      "\xe4\xc3\xba\x81\x18\x79\x90\x87\xb7\x94\xd2\x3d\x37\x40\x42\xb4\xd6\x83"
      "\x77\x88\xbc\xf7\x35\x94\xf3\xe7\x6c\x3f\x49\x48\x78\xa8\xc2\x70\xe1\xa6"
      "\x46\x8c\x33\xed\x06\xae\x5e\xf2\x01\x90\x97\x50\x54\xa9\xdb\x93\x97\xd1"
      "\xf0\x0f\x02\x77\xc3\xa0\x25\x19\xf7\x68\x96\xc4\x84\xe8\x80\x6b\xfa\x7d"
      "\x5c\x67\xb2\xf2\x18\xb2\xd4\x50\xd1\x69\x36\x32\xa3\xc8\x83\x04\xcf\x92"
      "\xd2\xff\x7f\x00\x00\x00\x00\x00\x00\x96\xce\x1a\xde\xac\x36\x73\xf7\x5e"
      "\xc6\x56\xf2\x03\xcd\x83\x2f\xbb\xdb\x23\xaa\x83\x69\x98\xa8\x4a\x86\xd1"
      "\x74\x7e\xde\xfe\x0c\x6b\x80\x89\x92\xee\x18\x46\x9a\x5b\x9e\xee\xfe\xb3"
      "\x32\x40\x58\x67\x37\x30\xe0\x9f\x4b\x5c\x3c\x23\x5e\xc3\x9e\xb6\x50\x04"
      "\xdf\xd8\x1d\xaa\x40\x96\x45\x99\xd5\x29\x7b\x6f\xdd\x76\x26\xb9\x6f\x38"
      "\x29\x06\x00\x00\x00\x00\x00\x00\x00\xaf\x89\xd5\x57\x4e\xdb\x15\x58\xd2"
      "\x67\x5a\xf9\xae\x7c\xb0\xfe\x16\x50\x1b\xef\x10\xfa\x06\x77\xcf\xb6\xce"
      "\x0c\x2e\x0b\xa5\xf6\x72\x90\xb8\xbe\xb9\x4f\x0f\xac\x98\x23\xe4\x24\x43"
      "\x62\x0d\x53\x89\x5c\xf6\x69\x50\x96\xc0\x08\xda\xdd\x5f\x3d\x14\xbb\xaa"
      "\xd0\x47\x4b\xfb\x01\x49\x95\xdf\x0a\x97\x0d\x65\x59\x7f\xf1\x03\x81\xd8"
      "\xbb\x3b\xe3\x13\x9a\x4d\x33\xc6\xc2\x1c\xe1\x42\xcd\x54\xa1\x7b\x7b\x1b"
      "\x9b\x89\xaf\x2b\x36\x32\xb0\xd2\x46\x02\x98\x9c\x74\x74\xf5\x70\x81\x61"
      "\x12\xe3\x56\x00\x00\x00\x00\x00\x00\x00\x06\xcf\xb6\x71\x5e\xef\x4e\x2a"
      "\x9b\x37\x21\x1c\x10\x8e\x38\x71\xb9\x2e\xc1\x1f\x89\xfb\xcc\x7a\x1f\x47"
      "\x20\x6d\x5f\xa4\x1f\x46\xcf\x84\x51\x4c\xdf\x2f\xea\x7e\x99\xc6\x82\xec"
      "\xe8\x34\x6e\x5b\x9b\x80\x35\xcb\x28\x4b\x26\x6a\x42\x81\x8a\x9e\x2d\x5d"
      "\x77\x6b\xd1\x0f\x43\x82\x9f\x59\x13\xe1\x48\x1c\x1f\x93\x99\xe6\xfd\xcc"
      "\x88\xc3\x47\x00\x31\x4e\x5b\x53\x94\x1b\x2a\xe5\x40\xa1\xd2\x60\x0c\x91"
      "\xdb\xfe\x88\xd7\x90\x18\x11\xa2\x88\xa2\x4f\x76\xb1\x7b\x00\x0a\x7f\x6c"
      "\xfe\xa9\xb5\xe6\xbd\x8c\x0a\xfe\x2e\x7f\x1e\x9c\xb9\x3a\xcf\x31\x8f\xee"
      "\x28\x9d\xbf\xb6\xe3\x01\xf8\xaf\x99\x26\x2c\x7c\x66\x0a\x5d\x78\x62\x22"
      "\xb6\x6c\x79\xe8\xac\x39\xc6\x9e\x66\xe5\xd9\xaa\x11\xe3\x4c\x0e\x80\xa4"
      "\xa9\xd7\x71\x60\x3d\xa3\xc1\xb9\xec\x0b\xf7\x5e\x57\x99\x1b\xbe\x04\x41"
      "\x7b\xd9\x09\x3b\xdb\x3e\x3b\xa3\xcc\xc7\x09\xc5\x1f\x9a\xd9\xfa\xb9\x08"
      "\x11\xec\x1f\x9f\xd5\x19\x2d\x16\x85\x9b\x24\x57\xd4\x2c\xaa\xc2\xc8\x5a"
      "\xbd\x6c\xea\x00\x70\x21\xaa\xd5\x03\x6c\x7b\x00\x50\xb6\x31\x56\xca\x00"
      "\x9d\x1b\xff\x2b\x46\x1a\xb0\x25\xa7\x47\x23\x0c\xe5\x51\x58\x83\x97\xb3"
      "\x7a\xda\xd5\x08\xc7\xbb\x23\xef\xb5\xee\x4d\xc5\x78\xb3\x6d\xda\x45\x96"
      "\x78\x00\xdd\xfc\xb3\x3a\x08\xd9\x97\xed\x68\xbf\x77\x46\xc2\x2e\x20\x00"
      "\x94\x11\x49\x1a\xc5\x8c\xb9\x60\x51\x01\x41\xc7\xc2\xff\x16\x51\xd4\xc9"
      "\x35\x82\x1a\x36\x5e\xb2\x21\x47\x65\xa7\x14\x00\x04\x00\x00\xa4\xe9\x41"
      "\x38\x8b\x02\x7c\xc2\x50\x71\x43\xb3\xfb\xae\xaa\xc2\x06\x87\x34\xdd\xb9"
      "\x90\x72\x33\x97\x3f\x36\x1a\x2e\xea\x42\x33\xe0\x95\xa7\x57\x35\x21\x30"
      "\x15\xae\x9f\xe8\x73\xde\x64\x40\x83\x01\x9b\x39\x40\x72\x25\xde\xe4\xf2"
      "\xac\x3d\x9d\xb5\xbf\xaa\x03\xc1\x6a\x39\x4d\x99\xa0\xfa\x1f\xf8\xa0\xfb"
      "\x45\x09\x6a\xe3\xe3\xac\x48\x5a\x91\x2f\x47\x95\x64\x8d\x64\x98\x0b\xeb"
      "\x6a\x4a\x84\x3f\x61\x43\xf5\xf6\x91\x83\x9b\x67\x60\x6f\x3d\x10\xd7\x37"
      "\x75\x44\x5b\x54\x16\x20\x29\x09\xb3\xbe\x00\x00\x9e\xe1\x9e\xe0\x12\xff"
      "\x20\xa7\xb3\x66\xec\x4a\x9c\x8b\xc8\x10\xfa\x1a\xa3\x1f\x1d\xf2\x19\x33"
      "\xf4\x68\x84\xc2\xeb\x36\xf0\x45\xd2\x8a\xae\x8c\x13\xf3\x0e\xa2\x39\xd8"
      "\xcd\x6d\x51\x2f\xfc\xa3\x93\x2a\x70\xb9\x81\x9c\x8f\x60\x25\x94\x18\x3f"
      "\xa9\x54\x5c\x94\x85\x50\xc1\x06\x22\x8a\x03\x06\x85\x38\xff\x03\x00\x00"
      "\xce\x92\x16\xc3\x6f\xb4\x13\x5f\xc8\x88\x4c\x34\xe0\x51\x47\x50\x6e\xa8"
      "\xbd\x59\x31\xf8\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
      1024));
  syscall(__NR_ioctl, r[2], 0x4b6c, 0x20000000ul);
  NONFAILING(memcpy((void*)0x20000000, "./file0\000", 8));
  syscall(__NR_utimensat, -1, 0x20000000ul, 0ul, 0x100ul);
}
int main(void)
{
  syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  setup_binfmt_misc();
  install_segv_handler();
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      do_sandbox_none();
    }
  }
  sleep(1000000);
  return 0;
}