Coverage Report

Created: 2023-05-30 07:03

/src/fuzz_route.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2021 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include "config.h"
14
#include "syshead.h"
15
#include "init.h"
16
#include "proxy.h"
17
#include "interval.h"
18
#include "route.h"
19
#include "buffer.h"
20
21
#include "fuzz_randomizer.h"
22
23
230
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
24
25
230
  fuzz_random_init(data, size);
26
27
230
  gb_init();
28
29
230
  struct route_option_list *opt;
30
230
  struct route_list rl;
31
32
230
  int route_list_inited = 0;
33
230
  int route_list_ipv6_inited = 0;
34
35
230
  struct context c;
36
230
  memset(&c, 0, sizeof(struct context));
37
230
  gc_init(&c.gc);
38
230
  c.es = env_set_create(&c.gc);
39
230
  init_options(&c.options, true);
40
230
  net_ctx_init(&c, &c.net_ctx);
41
230
  init_verb_mute(&c, IVM_LEVEL_1);
42
43
230
  init_options_dev(&c.options);
44
45
  // options_postprocess(&c.options);
46
230
  pre_setup(&c.options);
47
48
230
  setenv_settings(c.es, &c.options);
49
50
230
  ALLOC_OBJ_CLEAR_GC(c.options.connection_list, struct connection_list,
51
230
                     &c.options.gc);
52
230
  context_init_1(&c);
53
54
230
  in_addr_t remote_host;
55
230
  ssize_t default_metric;
56
57
230
  struct route_ipv6_list rl6;
58
230
  struct route_ipv6_option_list *opt6;
59
60
230
  memset(&rl, 0, sizeof(rl));
61
230
  memset(&rl6, 0, sizeof(rl6));
62
230
  memset(&opt, 0, sizeof(opt));
63
230
  memset(&opt6, 0, sizeof(opt6));
64
65
230
  opt6 = new_route_ipv6_option_list(&c.gc);
66
230
  opt = new_route_option_list(&c.gc);
67
68
230
  int total_to_fuzz = fuzz_randomizer_get_int(1, 20);
69
3.80k
  for (int i = 0; i < total_to_fuzz; i++) {
70
3.57k
    int selector = fuzz_randomizer_get_int(0, 13);
71
3.57k
    switch (selector) {
72
1.47k
    case 0:
73
1.47k
      if (route_list_inited == 0) {
74
369
        const char *remote_endpoint = gb_get_random_string();
75
369
        memset(&rl, 0, sizeof(struct route_list));
76
369
        rl.flags = fuzz_randomizer_get_int(0, 0xffffff);
77
78
369
        init_route_list(&rl, opt, remote_endpoint, default_metric, remote_host,
79
369
                        c.es, &c);
80
369
        route_list_inited = 1;
81
369
      }
82
1.47k
      break;
83
115
    case 1:
84
115
      if (route_list_inited) {
85
5
        in_addr_t addr;
86
5
        route_list_add_vpn_gateway(&rl, c.es, addr);
87
5
      }
88
115
      break;
89
68
    case 2:
90
68
      if (route_list_inited && route_list_ipv6_inited) {
91
34
        struct tuntap tt;
92
34
        memset(&tt, 0, sizeof(tt));
93
34
        add_routes(&rl, &rl6, &tt, 0, c.es, &c);
94
34
      }
95
68
      break;
96
218
    case 3:
97
218
      if (route_list_inited) {
98
69
        setenv_routes(c.es, &rl);
99
69
      }
100
218
      break;
101
49
    case 4:
102
49
      if (route_list_inited) {
103
31
        struct route_ipv4 r;
104
31
        struct route_option ro;
105
31
        ro.network = gb_get_random_string();
106
31
        ro.netmask = gb_get_random_string();
107
31
        ro.gateway = gb_get_random_string();
108
31
        ro.metric = gb_get_random_string();
109
31
        ro.next = NULL;
110
111
31
        memset(&r, 0, sizeof(struct route_ipv4));
112
31
        r.option = &ro;
113
31
        r.flags = RT_DEFINED;
114
31
        add_route(&r, NULL, 0, NULL, c.es, &c);
115
31
      }
116
49
      break;
117
93
    case 5:
118
93
      if (route_list_inited) {
119
74
        char *s1 = get_random_string();
120
74
        is_special_addr(s1);
121
74
        free(s1);
122
74
      }
123
93
      break;
124
316
    case 6:
125
316
      if (route_list_ipv6_inited == 0) {
126
282
        const char *remote_endpoint = gb_get_random_string();
127
282
        memset(&rl, 0, sizeof(struct route_list));
128
282
        struct in6_addr remote_host;
129
130
282
        rl6.rgi6.flags = fuzz_randomizer_get_int(0, 0xffffff);
131
282
        fuzz_get_random_data(&rl6.rgi6.hwaddr, 6);
132
133
282
        char *t1 = gb_get_random_string();
134
282
        if (strlen(t1) > 16) {
135
48
          memcpy(rl6.rgi6.iface, t1, 16);
136
234
        } else {
137
234
          memcpy(rl6.rgi6.iface, t1, strlen(t1));
138
234
        }
139
140
282
        init_route_ipv6_list(&rl6, opt6, remote_endpoint, 0, &remote_host, c.es,
141
282
                             &c);
142
282
        route_list_ipv6_inited = 1;
143
282
      }
144
316
      break;
145
150
    case 7: {
146
150
      unsigned int flags;
147
150
      struct route_ipv6 r6;
148
150
      struct tuntap tt;
149
150
      memset(&tt, 0, sizeof(tt));
150
150
      tt.actual_name = gb_get_random_string();
151
150
      r6.iface = gb_get_random_string();
152
150
      r6.flags = fuzz_randomizer_get_int(0, 0xfffff);
153
150
      r6.netbits = fuzz_randomizer_get_int(0, 0xfffff);
154
150
      r6.metric = fuzz_randomizer_get_int(0, 0xfffff);
155
156
150
      r6.next = NULL;
157
158
150
      add_route_ipv6(&r6, &tt, 0, c.es, &c);
159
150
    } break;
160
243
    case 8:
161
243
      if (route_list_ipv6_inited && route_list_inited) {
162
185
        delete_routes(&rl, &rl6, NULL, 0, c.es, &c);
163
185
        route_list_ipv6_inited = 0;
164
185
        route_list_inited = 0;
165
185
      }
166
243
      break;
167
42
    case 9:
168
42
      if (route_list_ipv6_inited) {
169
21
        setenv_routes_ipv6(c.es, &rl6);
170
21
      }
171
42
      break;
172
201
    case 10: {
173
201
      add_route_ipv6_to_option_list(opt6, gb_get_random_string(),
174
201
                                    gb_get_random_string(),
175
201
                                    gb_get_random_string());
176
201
    } break;
177
22
    case 11: {
178
22
      print_route_options(opt, M_NONFATAL);
179
22
    } break;
180
525
    case 12: {
181
525
      add_route_to_option_list(opt, gb_get_random_string(),
182
525
                               gb_get_random_string(), gb_get_random_string(),
183
525
                               gb_get_random_string());
184
525
    } break;
185
62
    default:
186
62
      break;
187
3.57k
    }
188
3.57k
  }
189
190
230
  if (route_list_inited) {
191
184
    gc_free(&rl.gc);
192
184
  }
193
230
  env_set_destroy(c.es);
194
230
  context_gc_free(&c);
195
196
230
  fuzz_random_destroy();
197
198
230
  gb_cleanup();
199
200
230
  return 0;
201
230
}