Project

General

Profile

Bug #371 » reproducer2.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 <endian.h>
7
#include <errno.h>
8
#include <fcntl.h>
9
#include <net/if_arp.h>
10
#include <sched.h>
11
#include <stdarg.h>
12
#include <stdbool.h>
13
#include <stdint.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
#include <sys/ioctl.h>
18
#include <sys/mount.h>
19
#include <sys/prctl.h>
20
#include <sys/resource.h>
21
#include <sys/stat.h>
22
#include <sys/syscall.h>
23
#include <sys/time.h>
24
#include <sys/types.h>
25
#include <sys/uio.h>
26
#include <sys/wait.h>
27
#include <unistd.h>
28

    
29
#include <linux/if.h>
30
#include <linux/if_ether.h>
31
#include <linux/if_tun.h>
32
#include <linux/ip.h>
33
#include <linux/tcp.h>
34

    
35
unsigned long long procid;
36

    
37
static void vsnprintf_check(char* str, size_t size, const char* format,
38
                            va_list args)
39
{
40
  int rv;
41
  rv = vsnprintf(str, size, format, args);
42
  if (rv < 0)
43
    exit(1);
44
  if ((size_t)rv >= size)
45
    exit(1);
46
}
47

    
48
#define COMMAND_MAX_LEN 128
49
#define PATH_PREFIX                                                            \
50
  "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin "
51
#define PATH_PREFIX_LEN (sizeof(PATH_PREFIX) - 1)
52

    
53
static void execute_command(bool panic, const char* format, ...)
54
{
55
  va_list args;
56
  char command[PATH_PREFIX_LEN + COMMAND_MAX_LEN];
57
  int rv;
58
  va_start(args, format);
59
  memcpy(command, PATH_PREFIX, PATH_PREFIX_LEN);
60
  vsnprintf_check(command + PATH_PREFIX_LEN, COMMAND_MAX_LEN, format, args);
61
  va_end(args);
62
  rv = system(command);
63
  if (rv) {
64
    if (panic)
65
      exit(1);
66
  }
67
}
68

    
69
#define DEV_IPV4 "172.20.20.%d"
70
#define DEV_IPV6 "fe80::%02hx"
71
#define DEV_MAC "aa:aa:aa:aa:aa:%02hx"
72

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

    
176
static bool write_file(const char* file, const char* what, ...)
177
{
178
  char buf[1024];
179
  va_list args;
180
  va_start(args, what);
181
  vsnprintf(buf, sizeof(buf), what, args);
182
  va_end(args);
183
  buf[sizeof(buf) - 1] = 0;
184
  int len = strlen(buf);
185
  int fd = open(file, O_WRONLY | O_CLOEXEC);
186
  if (fd == -1)
187
    return false;
188
  if (write(fd, buf, len) != len) {
189
    int err = errno;
190
    close(fd);
191
    errno = err;
192
    return false;
193
  }
194
  close(fd);
195
  return true;
196
}
197

    
198
static void setup_common()
199
{
200
  if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
201
  }
202
}
203

    
204
static void loop();
205

    
206
static void sandbox_common()
207
{
208
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
209
  setpgrp();
210
  setsid();
211
  struct rlimit rlim;
212
  rlim.rlim_cur = rlim.rlim_max = 200 << 20;
213
  setrlimit(RLIMIT_AS, &rlim);
214
  rlim.rlim_cur = rlim.rlim_max = 32 << 20;
215
  setrlimit(RLIMIT_MEMLOCK, &rlim);
216
  rlim.rlim_cur = rlim.rlim_max = 136 << 20;
217
  setrlimit(RLIMIT_FSIZE, &rlim);
218
  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
219
  setrlimit(RLIMIT_STACK, &rlim);
220
  rlim.rlim_cur = rlim.rlim_max = 0;
221
  setrlimit(RLIMIT_CORE, &rlim);
222
  rlim.rlim_cur = rlim.rlim_max = 256;
223
  setrlimit(RLIMIT_NOFILE, &rlim);
224
  if (unshare(CLONE_NEWNS)) {
225
  }
226
  if (unshare(CLONE_NEWIPC)) {
227
  }
228
  if (unshare(0x02000000)) {
229
  }
230
  if (unshare(CLONE_NEWUTS)) {
231
  }
232
  if (unshare(CLONE_SYSVSEM)) {
233
  }
234
  typedef struct {
235
    const char* name;
236
    const char* value;
237
  } sysctl_t;
238
  static const sysctl_t sysctls[] = {
239
      {"/proc/sys/kernel/shmmax", "16777216"},
240
      {"/proc/sys/kernel/shmall", "536870912"},
241
      {"/proc/sys/kernel/shmmni", "1024"},
242
      {"/proc/sys/kernel/msgmax", "8192"},
243
      {"/proc/sys/kernel/msgmni", "1024"},
244
      {"/proc/sys/kernel/msgmnb", "1024"},
245
      {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
246
  };
247
  unsigned i;
248
  for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++) {
249
    if (!write_file(sysctls[i].name, sysctls[i].value)) {
250
    }
251
  }
252
}
253

    
254
int wait_for_loop(int pid)
255
{
256
  if (pid < 0)
257
    exit(1);
258
  int status = 0;
259
  while (waitpid(-1, &status, __WALL) != pid) {
260
  }
261
  return WEXITSTATUS(status);
262
}
263

    
264
static int do_sandbox_none(void)
265
{
266
  if (unshare(CLONE_NEWPID)) {
267
  }
268
  int pid = fork();
269
  if (pid != 0)
270
    return wait_for_loop(pid);
271
  setup_common();
272
  sandbox_common();
273
  initialize_netdevices_init();
274
  if (unshare(CLONE_NEWNET)) {
275
  }
276
  initialize_netdevices();
277
  loop();
278
  exit(1);
279
}
280

    
281
uint64_t r[2] = {0xffffffffffffffff, 0x0};
282

    
283
void loop(void)
284
{
285
  long res = 0;
286
  res = syscall(__NR_socket, 0x11, 0x100000000000003, 0x300);
287
  if (res != -1)
288
    r[0] = res;
289
  memcpy((void*)0x20000200, "batadv0\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
290
  *(uint32_t*)0x20000210 = 0;
291
  res = syscall(__NR_ioctl, r[0], 0x8933, 0x20000200);
292
  if (res != -1)
293
    r[1] = *(uint32_t*)0x20000210;
294
  *(uint32_t*)0x20000100 = 4;
295
  syscall(__NR_setsockopt, r[0], 0x107, 0x14, 0x20000100, 4);
296
  *(uint16_t*)0x20000040 = 0x11;
297
  *(uint16_t*)0x20000042 = htobe16(0);
298
  *(uint32_t*)0x20000044 = r[1];
299
  *(uint16_t*)0x20000048 = 1;
300
  *(uint8_t*)0x2000004a = 0;
301
  *(uint8_t*)0x2000004b = 6;
302
  memcpy((void*)0x2000004c, "\x32\xca\xe4\x78\x3d\x32", 6);
303
  *(uint8_t*)0x20000052 = 0;
304
  *(uint8_t*)0x20000053 = 0;
305
  syscall(__NR_bind, r[0], 0x20000040, 0x14);
306
  memcpy((void*)0x20000180,
307
         "\x05\x03\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc5\x2c", 14);
308
  syscall(__NR_sendto, r[0], 0x20000180, 0xe, 0, 0, 0);
309
}
310
int main(void)
311
{
312
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
313
  do_sandbox_none();
314
  return 0;
315
}
(2-2/3)