// https://syzkaller.appspot.com/bug?id=ed934a04ea5f3d45a45f73da9f3b130715ab5e53
// 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 <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.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);
}

#define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1)

#define BITMASK_LEN_OFF(type, bf_off, bf_len)                                  \
  (type)(BITMASK_LEN(type, (bf_len)) << (bf_off))

#define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len)                      \
  if ((bf_off) == 0 && (bf_len) == 0) {                                        \
    *(type*)(addr) = (type)(val);                                              \
  } else {                                                                     \
    type new_val = *(type*)(addr);                                             \
    new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len));                     \
    new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off);          \
    *(type*)(addr) = new_val;                                                  \
  }

struct csum_inet {
  uint32_t acc;
};

static void csum_inet_init(struct csum_inet* csum)
{
  csum->acc = 0;
}

static void csum_inet_update(struct csum_inet* csum, const uint8_t* data,
                             size_t length)
{
  if (length == 0)
    return;

  size_t i;
  for (i = 0; i < length - 1; i += 2)
    csum->acc += *(uint16_t*)&data[i];

  if (length & 1)
    csum->acc += (uint16_t)data[length - 1];

  while (csum->acc > 0xffff)
    csum->acc = (csum->acc & 0xffff) + (csum->acc >> 16);
}

static uint16_t csum_inet_digest(struct csum_inet* csum)
{
  return ~csum->acc;
}

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

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

uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
void loop()
{
  long res;
  res = syscall(__NR_socket, 0x11, 0x100000802, 0);
  if (res != -1)
    r[0] = res;
  memcpy((void*)0x20000040, "/dev/net/tun", 13);
  res = syz_open_dev(0x20000040, 0, 2);
  if (res != -1)
    r[1] = res;
  memcpy((void*)0x20000300,
         "\x69\x66\x62\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         16);
  *(uint16_t*)0x20000310 = 1;
  syscall(__NR_ioctl, r[1], 0x400454ca, 0x20000300);
  memcpy((void*)0x20000000,
         "\x69\x66\x62\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         16);
  *(uint16_t*)0x20000010 = 0xa201;
  syscall(__NR_ioctl, r[0], 0x8914, 0x20000000);
  *(uint16_t*)0x20000180 = 0;
  *(uint16_t*)0x20000182 = htobe16(0x88a8);
  *(uint8_t*)0x20000184 = 1;
  *(uint8_t*)0x20000185 = 0x81;
  *(uint16_t*)0x20000186 = 2;
  *(uint16_t*)0x20000188 = 0x401;
  *(uint16_t*)0x2000018a = 1;
  *(uint16_t*)0x2000018c = 0x75b;
  STORE_BY_BITMASK(uint8_t, 0x2000018e, 0x17, 0, 4);
  STORE_BY_BITMASK(uint8_t, 0x2000018e, 4, 4, 4);
  STORE_BY_BITMASK(uint8_t, 0x2000018f, 0x40, 0, 2);
  STORE_BY_BITMASK(uint8_t, 0x2000018f, 0, 2, 6);
  *(uint16_t*)0x20000190 = htobe16(0x149);
  *(uint16_t*)0x20000192 = htobe16(0x68);
  *(uint16_t*)0x20000194 = htobe16(8);
  *(uint8_t*)0x20000196 = 0x44;
  *(uint8_t*)0x20000197 = 0x89;
  *(uint16_t*)0x20000198 = 0;
  *(uint8_t*)0x2000019a = 0xac;
  *(uint8_t*)0x2000019b = 0x14;
  *(uint8_t*)0x2000019c = 0x14;
  *(uint8_t*)0x2000019d = 0x18;
  *(uint32_t*)0x2000019e = htobe32(0xe0000002);
  *(uint8_t*)0x200001a2 = 1;
  *(uint8_t*)0x200001a3 = 0x83;
  *(uint8_t*)0x200001a4 = 0x13;
  *(uint8_t*)0x200001a5 = 8;
  *(uint32_t*)0x200001a6 = htobe32(-1);
  *(uint32_t*)0x200001aa = htobe32(0xe0000001);
  *(uint8_t*)0x200001ae = 0xac;
  *(uint8_t*)0x200001af = 0x14;
  *(uint8_t*)0x200001b0 = 0x14;
  *(uint8_t*)0x200001b1 = 0xc;
  *(uint32_t*)0x200001b2 = htobe32(0xe0000002);
  *(uint8_t*)0x200001b6 = 0x44;
  *(uint8_t*)0x200001b7 = 0x34;
  *(uint8_t*)0x200001b8 = 0;
  STORE_BY_BITMASK(uint8_t, 0x200001b9, 3, 0, 4);
  STORE_BY_BITMASK(uint8_t, 0x200001b9, 0xb8, 4, 4);
  *(uint32_t*)0x200001ba = htobe32(0xc);
  *(uint32_t*)0x200001be = htobe32(0xe0000002);
  *(uint32_t*)0x200001c2 = htobe32(5);
  *(uint32_t*)0x200001c6 = htobe32(0x385e);
  *(uint32_t*)0x200001ca = htobe32(0xe0000002);
  *(uint32_t*)0x200001ce = htobe32(5);
  *(uint32_t*)0x200001d2 = htobe32(0xe0000001);
  *(uint32_t*)0x200001d6 = htobe32(0x753a);
  *(uint32_t*)0x200001da = htobe32(0x7f000001);
  *(uint32_t*)0x200001de = htobe32(3);
  *(uint32_t*)0x200001e2 = htobe32(7);
  *(uint32_t*)0x200001e6 = htobe32(4);
  *(uint16_t*)0x200001ea = htobe16(0x4e24);
  *(uint16_t*)0x200001ec = htobe16(0x4e20);
  *(uint8_t*)0x200001ee = 4;
  STORE_BY_BITMASK(uint8_t, 0x200001ef, 1, 0, 4);
  STORE_BY_BITMASK(uint8_t, 0x200001ef, 2, 4, 4);
  *(uint16_t*)0x200001f0 = 0;
  STORE_BY_BITMASK(uint8_t, 0x200001f2, 0, 0, 1);
  STORE_BY_BITMASK(uint8_t, 0x200001f2, 6, 1, 4);
  STORE_BY_BITMASK(uint8_t, 0x200001f2, 0xf1, 5, 3);
  memcpy((void*)0x200001f3, "\x84\x1b\xe1", 3);
  *(uint8_t*)0x200001f6 = 0;
  memcpy((void*)0x200001f7, "\x43\x9c\x19", 3);
  memcpy((void*)0x200001fa,
         "\xee\x62\x5b\xdc\x9b\x7b\x06\x29\xac\x10\x1b\xaa\x94\xd4\xde\x9c\x6e"
         "\xd4\x12\x70\xf4\xef\x8a\xe0\x9f\x6c\xdc\xc8\x19\x27\x48\x53\x23\x54"
         "\x94\x5e\x46\xca\xbb\x8f\x12\xb0\xb6\xee\x6e\x97\x92\x22\xbe\xe9\x42"
         "\x37\x89\x72\xfc\x6f\xc5\x6b\x23\x5b\x8f\x06\xad\xee\x23\x77\x9f\x79"
         "\xfb\x35\xb2\xbb\x4f\xa3\xe4\x5e\x30\xbe\x71\x3a\xa5\x5b\x91\x68\x1a"
         "\x5f\x15\x56\x53\x81\x37\x88\x61\xb9\xc0\x0e\xfa\x63\xf5\xa5\x5c\x11"
         "\xd5\x00\xff\xad\xc2\xb1\xd2\x36\xbd\x54\xc7\x80\x04\x72\x38\x96\xcc"
         "\x6b\x22\x86\x1c\xcf\xb1\xd3\x93\x40\x41\xba\x08\x2b\xe9\x99\x62\x5e"
         "\xc7\x23\x2c\xbf\xf5\x92\x9a\x64\x19\x60\x0e\x4e\x9e\xc9\x6e\x6f\xf5"
         "\x94\x3d\xd4\x13\x30\xcc\x38\x7b\x5c\x47\x26\x25\xaf\x24\x38\x6c\xf0"
         "\x81\x0d\x87\xf8\xbe\x11\xa0\x75\x2f\xbf\xf5\xdd\xd5\x8d\x93\xff\x08"
         "\x02\x52\x86\x2f\x85\xa2\x0f\xff\xfa\x10\xb4\xaa\xf8\xdc\xaa\x64\xb4"
         "\x05\x9a\x36\x37\x08\xe1\x95\xd9\x62\x2c\x35\x58\x9c\x27\xe1\x26\x1f",
         221);
  struct csum_inet csum_1;
  csum_inet_init(&csum_1);
  csum_inet_update(&csum_1, (const uint8_t*)0x2000019a, 4);
  csum_inet_update(&csum_1, (const uint8_t*)0x2000019e, 4);
  uint16_t csum_1_chunk_2 = 0x2100;
  csum_inet_update(&csum_1, (const uint8_t*)&csum_1_chunk_2, 2);
  uint16_t csum_1_chunk_3 = 0x1000;
  csum_inet_update(&csum_1, (const uint8_t*)&csum_1_chunk_3, 2);
  csum_inet_update(&csum_1, (const uint8_t*)0x200001ea, 16);
  *(uint16_t*)0x200001f0 = csum_inet_digest(&csum_1);
  struct csum_inet csum_2;
  csum_inet_init(&csum_2);
  csum_inet_update(&csum_2, (const uint8_t*)0x2000018e, 92);
  *(uint16_t*)0x20000198 = csum_inet_digest(&csum_2);
  syscall(__NR_write, r[1], 0x20000180, 0x157);
}

int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  int pid = do_sandbox_none();
  int status = 0;
  while (waitpid(pid, &status, __WALL) != pid) {
  }
  return 0;
}