Project

General

Profile

Bug #371 » reproducer1.c

Sven Eckelmann, 12/31/2018 07:56 PM

 
1
// autogenerated by syzkaller (https://github.com/google/syzkaller)
2

    
3
#define _GNU_SOURCE
4

    
5
#include <arpa/inet.h>
6
#include <dirent.h>
7
#include <endian.h>
8
#include <errno.h>
9
#include <fcntl.h>
10
#include <net/if_arp.h>
11
#include <sched.h>
12
#include <signal.h>
13
#include <stdarg.h>
14
#include <stdbool.h>
15
#include <stdint.h>
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <sys/ioctl.h>
20
#include <sys/mount.h>
21
#include <sys/prctl.h>
22
#include <sys/resource.h>
23
#include <sys/stat.h>
24
#include <sys/syscall.h>
25
#include <sys/time.h>
26
#include <sys/types.h>
27
#include <sys/uio.h>
28
#include <sys/wait.h>
29
#include <time.h>
30
#include <unistd.h>
31

    
32
#include <linux/if.h>
33
#include <linux/if_ether.h>
34
#include <linux/if_tun.h>
35
#include <linux/ip.h>
36
#include <linux/tcp.h>
37

    
38
unsigned long long procid;
39

    
40
static void sleep_ms(uint64_t ms)
41
{
42
  usleep(ms * 1000);
43
}
44

    
45
static uint64_t current_time_ms(void)
46
{
47
  struct timespec ts;
48
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
49
    exit(1);
50
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
51
}
52

    
53
static void vsnprintf_check(char* str, size_t size, const char* format,
54
                            va_list args)
55
{
56
  int rv;
57
  rv = vsnprintf(str, size, format, args);
58
  if (rv < 0)
59
    exit(1);
60
  if ((size_t)rv >= size)
61
    exit(1);
62
}
63

    
64
#define COMMAND_MAX_LEN 128
65
#define PATH_PREFIX                                                            \
66
  "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin "
67
#define PATH_PREFIX_LEN (sizeof(PATH_PREFIX) - 1)
68

    
69
static void execute_command(bool panic, const char* format, ...)
70
{
71
  va_list args;
72
  char command[PATH_PREFIX_LEN + COMMAND_MAX_LEN];
73
  int rv;
74
  va_start(args, format);
75
  memcpy(command, PATH_PREFIX, PATH_PREFIX_LEN);
76
  vsnprintf_check(command + PATH_PREFIX_LEN, COMMAND_MAX_LEN, format, args);
77
  va_end(args);
78
  rv = system(command);
79
  if (rv) {
80
    if (panic)
81
      exit(1);
82
  }
83
}
84

    
85
#define DEV_IPV4 "172.20.20.%d"
86
#define DEV_IPV6 "fe80::%02hx"
87
#define DEV_MAC "aa:aa:aa:aa:aa:%02hx"
88

    
89
static void snprintf_check(char* str, size_t size, const char* format, ...)
90
{
91
  va_list args;
92
  va_start(args, format);
93
  vsnprintf_check(str, size, format, args);
94
  va_end(args);
95
}
96
static void initialize_netdevices(void)
97
{
98
  char netdevsim[16];
99
  sprintf(netdevsim, "netdevsim%d", (int)procid);
100
  const char* devtypes[] = {"ip6gretap", "ip6erspan", "bridge", "vcan",
101
                            "bond",      "team",      "dummy",  "nlmon",
102
                            "caif",      "batadv"};
103
  const char* devmasters[] = {"bridge", "bond", "team"};
104
  const char* devnames[] = {"lo",
105
                            "sit0",
106
                            "bridge0",
107
                            "vcan0",
108
                            "tunl0",
109
                            "gre0",
110
                            "gretap0",
111
                            "ip_vti0",
112
                            "ip6_vti0",
113
                            "ip6tnl0",
114
                            "ip6gre0",
115
                            "ip6gretap0",
116
                            "erspan0",
117
                            "bond0",
118
                            "veth0",
119
                            "veth1",
120
                            "team0",
121
                            "veth0_to_bridge",
122
                            "veth1_to_bridge",
123
                            "veth0_to_bond",
124
                            "veth1_to_bond",
125
                            "veth0_to_team",
126
                            "veth1_to_team",
127
                            "veth0_to_hsr",
128
                            "veth1_to_hsr",
129
                            "hsr0",
130
                            "ip6erspan0",
131
                            "dummy0",
132
                            "nlmon0",
133
                            "vxcan1",
134
                            "caif0",
135
                            "batadv0",
136
                            netdevsim};
137
  unsigned i;
138
  for (i = 0; i < sizeof(devtypes) / (sizeof(devtypes[0])); i++)
139
    execute_command(0, "ip link add dev %s0 type %s", devtypes[i], devtypes[i]);
140
  execute_command(0, "ip link add dev vxcan1 type vxcan");
141
  execute_command(0, "ip link add dev %s type netdevsim", netdevsim);
142
  execute_command(0, "ip link add type veth");
143
  for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
144
    execute_command(
145
        0, "ip link add name %s_slave_0 type veth peer name veth0_to_%s",
146
        devmasters[i], devmasters[i]);
147
    execute_command(
148
        0, "ip link add name %s_slave_1 type veth peer name veth1_to_%s",
149
        devmasters[i], devmasters[i]);
150
    execute_command(0, "ip link set %s_slave_0 master %s0", devmasters[i],
151
                    devmasters[i]);
152
    execute_command(0, "ip link set %s_slave_1 master %s0", devmasters[i],
153
                    devmasters[i]);
154
  }
155
  execute_command(0, "ip link set bridge_slave_0 up");
156
  execute_command(0, "ip link set bridge_slave_1 up");
157
  execute_command(
158
      0, "ip link add name hsr_slave_0 type veth peer name veth0_to_hsr");
159
  execute_command(
160
      0, "ip link add name hsr_slave_1 type veth peer name veth1_to_hsr");
161
  execute_command(
162
      0, "ip link add dev hsr0 type hsr slave1 hsr_slave_0 slave2 hsr_slave_1");
163
  execute_command(0, "ip link set hsr_slave_0 up");
164
  execute_command(0, "ip link set hsr_slave_1 up");
165
  for (i = 0; i < sizeof(devnames) / (sizeof(devnames[0])); i++) {
166
    char addr[32];
167
    snprintf_check(addr, sizeof(addr), DEV_IPV4, i + 10);
168
    execute_command(0, "ip -4 addr add %s/24 dev %s", addr, devnames[i]);
169
    snprintf_check(addr, sizeof(addr), DEV_IPV6, i + 10);
170
    execute_command(0, "ip -6 addr add %s/120 dev %s", addr, devnames[i]);
171
    snprintf_check(addr, sizeof(addr), DEV_MAC, i + 10);
172
    execute_command(0, "ip link set dev %s address %s", devnames[i], addr);
173
    execute_command(0, "ip link set dev %s up", devnames[i]);
174
  }
175
}
176
static void initialize_netdevices_init(void)
177
{
178
  int pid = procid;
179
  execute_command(0, "ip link set dev nr%d address bb:bb:bb:bb:bb:00:%02hx",
180
                  pid, pid);
181
  execute_command(0, "ip -4 addr add 172.30.00.%d/24 dev nr%d", pid + 1, pid);
182
  execute_command(0, "ip -6 addr add fe88::00:%02hx/120 dev nr%d", pid + 1,
183
                  pid);
184
  execute_command(0, "ip link set dev nr%d up", pid);
185
  execute_command(0, "ip link set dev rose%d address bb:bb:bb:01:%02hx", pid,
186
                  pid);
187
  execute_command(0, "ip -4 addr add 172.30.01.%d/24 dev rose%d", pid + 1, pid);
188
  execute_command(0, "ip -6 addr add fe88::01:%02hx/120 dev rose%d", pid + 1,
189
                  pid);
190
}
191

    
192
static bool write_file(const char* file, const char* what, ...)
193
{
194
  char buf[1024];
195
  va_list args;
196
  va_start(args, what);
197
  vsnprintf(buf, sizeof(buf), what, args);
198
  va_end(args);
199
  buf[sizeof(buf) - 1] = 0;
200
  int len = strlen(buf);
201
  int fd = open(file, O_WRONLY | O_CLOEXEC);
202
  if (fd == -1)
203
    return false;
204
  if (write(fd, buf, len) != len) {
205
    int err = errno;
206
    close(fd);
207
    errno = err;
208
    return false;
209
  }
210
  close(fd);
211
  return true;
212
}
213

    
214
static void setup_common()
215
{
216
  if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
217
  }
218
}
219

    
220
static void loop();
221

    
222
static void sandbox_common()
223
{
224
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
225
  setpgrp();
226
  setsid();
227
  struct rlimit rlim;
228
  rlim.rlim_cur = rlim.rlim_max = 200 << 20;
229
  setrlimit(RLIMIT_AS, &rlim);
230
  rlim.rlim_cur = rlim.rlim_max = 32 << 20;
231
  setrlimit(RLIMIT_MEMLOCK, &rlim);
232
  rlim.rlim_cur = rlim.rlim_max = 136 << 20;
233
  setrlimit(RLIMIT_FSIZE, &rlim);
234
  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
235
  setrlimit(RLIMIT_STACK, &rlim);
236
  rlim.rlim_cur = rlim.rlim_max = 0;
237
  setrlimit(RLIMIT_CORE, &rlim);
238
  rlim.rlim_cur = rlim.rlim_max = 256;
239
  setrlimit(RLIMIT_NOFILE, &rlim);
240
  if (unshare(CLONE_NEWNS)) {
241
  }
242
  if (unshare(CLONE_NEWIPC)) {
243
  }
244
  if (unshare(0x02000000)) {
245
  }
246
  if (unshare(CLONE_NEWUTS)) {
247
  }
248
  if (unshare(CLONE_SYSVSEM)) {
249
  }
250
  typedef struct {
251
    const char* name;
252
    const char* value;
253
  } sysctl_t;
254
  static const sysctl_t sysctls[] = {
255
      {"/proc/sys/kernel/shmmax", "16777216"},
256
      {"/proc/sys/kernel/shmall", "536870912"},
257
      {"/proc/sys/kernel/shmmni", "1024"},
258
      {"/proc/sys/kernel/msgmax", "8192"},
259
      {"/proc/sys/kernel/msgmni", "1024"},
260
      {"/proc/sys/kernel/msgmnb", "1024"},
261
      {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
262
  };
263
  unsigned i;
264
  for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++) {
265
    if (!write_file(sysctls[i].name, sysctls[i].value)) {
266
    }
267
  }
268
}
269

    
270
int wait_for_loop(int pid)
271
{
272
  if (pid < 0)
273
    exit(1);
274
  int status = 0;
275
  while (waitpid(-1, &status, __WALL) != pid) {
276
  }
277
  return WEXITSTATUS(status);
278
}
279

    
280
static int do_sandbox_none(void)
281
{
282
  if (unshare(CLONE_NEWPID)) {
283
  }
284
  int pid = fork();
285
  if (pid != 0)
286
    return wait_for_loop(pid);
287
  setup_common();
288
  sandbox_common();
289
  initialize_netdevices_init();
290
  if (unshare(CLONE_NEWNET)) {
291
  }
292
  initialize_netdevices();
293
  loop();
294
  exit(1);
295
}
296

    
297
static void kill_and_wait(int pid, int* status)
298
{
299
  kill(-pid, SIGKILL);
300
  kill(pid, SIGKILL);
301
  int i;
302
  for (i = 0; i < 100; i++) {
303
    if (waitpid(-1, status, WNOHANG | __WALL) == pid)
304
      return;
305
    usleep(1000);
306
  }
307
  DIR* dir = opendir("/sys/fs/fuse/connections");
308
  if (dir) {
309
    for (;;) {
310
      struct dirent* ent = readdir(dir);
311
      if (!ent)
312
        break;
313
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
314
        continue;
315
      char abort[300];
316
      snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
317
               ent->d_name);
318
      int fd = open(abort, O_WRONLY);
319
      if (fd == -1) {
320
        continue;
321
      }
322
      if (write(fd, abort, 1) < 0) {
323
      }
324
      close(fd);
325
    }
326
    closedir(dir);
327
  } else {
328
  }
329
  while (waitpid(-1, status, __WALL) != pid) {
330
  }
331
}
332

    
333
#define SYZ_HAVE_SETUP_TEST 1
334
static void setup_test()
335
{
336
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
337
  setpgrp();
338
}
339

    
340
#define SYZ_HAVE_RESET_TEST 1
341
static void reset_test()
342
{
343
  int fd;
344
  for (fd = 3; fd < 30; fd++)
345
    close(fd);
346
}
347

    
348
static void execute_one(void);
349

    
350
#define WAIT_FLAGS __WALL
351

    
352
static void loop(void)
353
{
354
  int iter;
355
  for (iter = 0;; iter++) {
356
    int pid = fork();
357
    if (pid < 0)
358
      exit(1);
359
    if (pid == 0) {
360
      setup_test();
361
      execute_one();
362
      reset_test();
363
      exit(0);
364
    }
365
    int status = 0;
366
    uint64_t start = current_time_ms();
367
    for (;;) {
368
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
369
        break;
370
      sleep_ms(1);
371
      if (current_time_ms() - start < 5 * 1000)
372
        continue;
373
      kill_and_wait(pid, &status);
374
      break;
375
    }
376
  }
377
}
378

    
379
uint64_t r[2] = {0xffffffffffffffff, 0x0};
380

    
381
void execute_one(void)
382
{
383
  long res = 0;
384
  res = syscall(__NR_socket, 0x11, 0x100000000000003, 0x300);
385
  if (res != -1)
386
    r[0] = res;
387
  memcpy((void*)0x20000200, "batadv0\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
388
  *(uint32_t*)0x20000210 = 0;
389
  res = syscall(__NR_ioctl, r[0], 0x8933, 0x20000200);
390
  if (res != -1)
391
    r[1] = *(uint32_t*)0x20000210;
392
  *(uint32_t*)0x20000100 = 4;
393
  syscall(__NR_setsockopt, r[0], 0x107, 0x14, 0x20000100, 4);
394
  *(uint16_t*)0x20000040 = 0x11;
395
  *(uint16_t*)0x20000042 = htobe16(0);
396
  *(uint32_t*)0x20000044 = r[1];
397
  *(uint16_t*)0x20000048 = 1;
398
  *(uint8_t*)0x2000004a = 0;
399
  *(uint8_t*)0x2000004b = 6;
400
  memcpy((void*)0x2000004c, "\x32\xca\xe4\x78\x3d\x32", 6);
401
  *(uint8_t*)0x20000052 = 0;
402
  *(uint8_t*)0x20000053 = 0;
403
  syscall(__NR_bind, r[0], 0x20000040, 0x14);
404
  memcpy((void*)0x20000180,
405
         "\x05\x03\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc5\x2c", 14);
406
  syscall(__NR_sendto, r[0], 0x20000180, 0xe, 0, 0, 0);
407
}
408
int main(void)
409
{
410
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
411
  do_sandbox_none();
412
  return 0;
413
}
(1-1/3)