// https://syzkaller.appspot.com/bug?id=5bbec74cb4d7b4ae0ac1e45d4a965fdaa3413422
// 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>

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

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

uint64_t r[1] = {0xffffffffffffffff};

void loop(void)
{
  intptr_t res = 0;
  res = syscall(__NR_socket, 0x2000000000000021ul, 2ul, 2);
  if (res != -1)
    r[0] = res;
  *(uint16_t*)0x20000040 = 0x21;
  *(uint16_t*)0x20000042 = 0;
  *(uint16_t*)0x20000044 = 2;
  *(uint16_t*)0x20000046 = 0x10;
  *(uint16_t*)0x20000048 = 2;
  *(uint16_t*)0x2000004a = htobe16(0);
  *(uint32_t*)0x2000004c = htobe32(0x7f000001);
  syscall(__NR_connect, r[0], 0x20000040ul, 0x24ul);
  *(uint64_t*)0x20005c00 = 0;
  *(uint32_t*)0x20005c08 = 0;
  *(uint64_t*)0x20005c10 = 0;
  *(uint64_t*)0x20005c18 = 0;
  *(uint64_t*)0x20005c20 = 0x20000440;
  memcpy(
      (void*)0x20000440,
      "\x18\x00\x00\x00\x00\x00\x00\x00\x10\x01\x00\x00\x01\x00\x00\x00\x77\x00"
      "\x00\xf2\x00\x00\x00\x00\xa6\xe2\x17\xb9\x1c\x3b\x0d\x87\x37\x22\xb4\x1a"
      "\x11\x8f\xd5\x8f\xa1\xf0\xa3\x9a\xd9\x58\x22\x03\xda\xa3\x2c\x98\x31\x69"
      "\x9a\xcc\x52\xf1\x41\xee\xca\x2b\xd1\x2d\x2f\x6f\xb5\x4e\x5b\xff\x40\xe0"
      "\x62\xa4\x3f\x80\xb1\xba\xbf\xd0\x76\xd2\x50\x28\x92\x25\x0d\xee\xe2\x72"
      "\x83\x16\xe6\x3e\x58\x9d\xdf\x14\x2e\x89\x36\x5b\xd4\x8c\xf7\x66\x02\xc6"
      "\xf6\xaa\x76\xbc\x37\x28\x47\x32\xbd\x1e\x19\x1d\xdd\x3c\x9f\x31\x0f\x82"
      "\x7c\x19\x5f\x3c\x49\x57\xc6\x7a\x08\x46\x65\x17\xba\x13\x6c\x8c\x9e\x01"
      "\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\x3e\x26\xb5\xca\x26\xbb\x43"
      "\x4d\xbd\x0e\x50\x85\xc2\x94\xe5\x77\xbb\x08\x18\x76\xe6\x3e\x7c\x28\x34"
      "\x53\x39\x25\xdb\x8b\x54\xb3\x3d\xa7\xb9\x47\xae\xfc\xa1\xf9\xc4\x9c\x64"
      "\x8d\x58\xd4\xf9\x44\x26\x4f\xca\xce\x71\xd5\xc7\x98\xcd\x3c\x13\xd7\x08"
      "\x3a\x7e\xb4\xc5\x26\xe0\x72\xa2\x3b\xd2\x4c\xcc\xb3\x8e\x4e\xff\x54\x25"
      "\x20\x85\x8c\x94\x0f\xbb\x95\x97\x9a\x56\x4d\x73\xa7\xaf\x10\x7f\xd5\xdd"
      "\xaa\x49\x18\x38\x87\x39\x14\xa4\x67\xc3\xce\x28\x05\x68\x33\xeb\x45\x59"
      "\x86\x43\x17\xc0\xa1\xda\xc9\x2d\x09\x0f\x4f\x90\x93\x67\x97\x91\xa2\x84"
      "\x0f\x28\x51\x32\x4a\xcd\xf4\x4a\x80\x7e\x69\xa1\xbc\x25\xd2\x65\x30\x30"
      "\x04\xe8\xea\xd1\x28\xa3\xd6\x1c\x00\x63\x23\x35\x01\xcf\xdf\x76\xd9\x9d"
      "\xbc\x46\xb9\xc0\x02\x03\xa9\x16\x1b\x8a\x44\x40\x01\xc3\x86\x47\x5c\x43"
      "\x2c\x7a\x6e\x89\x88\x85\x42\x90\x63\x95\x4e\xa0\xbd\xf9\xab\xc7\xb9\x32"
      "\x2f\x34\x85\xd9\x9a\xb3\x8a\x32\x31\xea\x08\x58\x47\x95\x64\x7d\xdb\x75"
      "\xc5\x93\x93\x3e\x82\x4a\xcd\xab\x29\x24\x82\x73\x5c\xd9\x15\x72\xd0\xbc"
      "\x3f\x95\xb6\x1e\x03\x91\x83\xf5\xed\x7c\xb7\x5f\x2b\xa7\x92\x96\xf7\x3c"
      "\xa6\xf0\x0a\x0f\x6c\xe5\xc2\xbc\x03\x95\x7a\x77\xdb\xd2\x29\x92\x11\x24"
      "\x58\x0f\xc4\xdd\xd4\xaa\x3f\x37\x52\xc7\xf2\xdc\xa3\xda\x2b\xa7\xb7\x72"
      "\xe0\x12\x15\xf0\x5d\xc1\xcf\x74\xdd\xf5\x1a\xf1\xa4\xeb\xba\x44\x33\x74"
      "\xe0\x9d\x5f\xf9\x6d\xb1\xa1\xa6\x03\x68\x49\x3f\xf3\x64\x31\x5b",
      484);
  *(uint64_t*)0x20005c28 = 0x18;
  *(uint32_t*)0x20005c30 = 0;
  *(uint32_t*)0x20005c38 = 0;
  syscall(__NR_sendmmsg, r[0], 0x20005c00ul, 1ul, 0x4048000ul);
  close_fds();
}
int main(void)
{
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);
  do_sandbox_none();
  return 0;
}