Coverage Report

Created: 2025-08-31 07:02

/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
2.53k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
24
25
2.53k
  fuzz_random_init(data, size);
26
27
2.53k
  gb_init();
28
29
2.53k
  struct route_option_list *opt;
30
2.53k
  struct route_list rl;
31
32
2.53k
  int route_list_inited = 0;
33
2.53k
  int route_list_ipv6_inited = 0;
34
35
2.53k
  struct context c;
36
2.53k
  memset(&c, 0, sizeof(struct context));
37
2.53k
  gc_init(&c.gc);
38
2.53k
  c.es = env_set_create(&c.gc);
39
2.53k
  init_options(&c.options, true);
40
2.53k
  net_ctx_init(&c, &c.net_ctx);
41
2.53k
  init_verb_mute(&c, IVM_LEVEL_1);
42
43
2.53k
  init_options_dev(&c.options);
44
45
  // options_postprocess(&c.options);
46
2.53k
  pre_setup(&c.options);
47
48
2.53k
  setenv_settings(c.es, &c.options);
49
50
2.53k
  ALLOC_OBJ_CLEAR_GC(c.options.ce.local_list, struct local_list, &c.options.gc);
51
2.53k
  ALLOC_OBJ_CLEAR_GC(c.options.connection_list, struct connection_list,
52
2.53k
                     &c.options.gc);
53
2.53k
  context_init_1(&c);
54
55
2.53k
  in_addr_t remote_host;
56
2.53k
  ssize_t default_metric;
57
58
2.53k
  struct route_ipv6_list rl6;
59
2.53k
  struct route_ipv6_option_list *opt6;
60
61
2.53k
  memset(&rl, 0, sizeof(rl));
62
2.53k
  memset(&rl6, 0, sizeof(rl6));
63
2.53k
  memset(&opt, 0, sizeof(opt));
64
2.53k
  memset(&opt6, 0, sizeof(opt6));
65
66
2.53k
  opt6 = new_route_ipv6_option_list(&c.gc);
67
2.53k
  opt = new_route_option_list(&c.gc);
68
69
2.53k
  int total_to_fuzz = fuzz_randomizer_get_int(1, 20);
70
37.8k
  for (int i = 0; i < total_to_fuzz; i++) {
71
35.2k
    int selector = fuzz_randomizer_get_int(0, 13);
72
35.2k
    switch (selector) {
73
26.9k
    case 0:
74
26.9k
      if (route_list_inited == 0) {
75
3.01k
        const char *remote_endpoint = gb_get_random_string();
76
3.01k
        memset(&rl, 0, sizeof(struct route_list));
77
3.01k
        rl.flags = fuzz_randomizer_get_int(0, 0xffffff);
78
79
3.01k
        init_route_list(&rl, opt, remote_endpoint, default_metric, remote_host,
80
3.01k
                        c.es, &c);
81
3.01k
        route_list_inited = 1;
82
3.01k
      }
83
26.9k
      break;
84
404
    case 1:
85
404
      if (route_list_inited) {
86
264
        in_addr_t addr;
87
264
        route_list_add_vpn_gateway(&rl, c.es, addr);
88
264
      }
89
404
      break;
90
367
    case 2:
91
367
      if (route_list_inited && route_list_ipv6_inited) {
92
248
        struct tuntap tt;
93
248
        memset(&tt, 0, sizeof(tt));
94
248
        add_routes(&rl, &rl6, &tt, 0, c.es, &c);
95
248
      }
96
367
      break;
97
843
    case 3:
98
843
      if (route_list_inited) {
99
723
        setenv_routes(c.es, &rl);
100
723
      }
101
843
      break;
102
175
    case 4:
103
175
      if (route_list_inited) {
104
125
        struct route_ipv4 r;
105
125
        struct route_option ro;
106
125
        ro.network = gb_get_random_string();
107
125
        ro.netmask = gb_get_random_string();
108
125
        ro.gateway = gb_get_random_string();
109
125
        ro.metric = gb_get_random_string();
110
125
        ro.next = NULL;
111
112
125
        memset(&r, 0, sizeof(struct route_ipv4));
113
125
        r.option = &ro;
114
125
        r.flags = RT_DEFINED;
115
125
        add_route(&r, NULL, 0, NULL, c.es, &c);
116
125
      }
117
175
      break;
118
508
    case 5:
119
508
      if (route_list_inited) {
120
442
        char *s1 = get_random_string();
121
442
        is_special_addr(s1);
122
442
        free(s1);
123
442
      }
124
508
      break;
125
1.48k
    case 6:
126
1.48k
      if (route_list_ipv6_inited == 0) {
127
1.34k
        const char *remote_endpoint = gb_get_random_string();
128
1.34k
        memset(&rl, 0, sizeof(struct route_list));
129
1.34k
        struct in6_addr remote_host;
130
131
1.34k
        rl6.rgi6.flags = fuzz_randomizer_get_int(0, 0xffffff);
132
1.34k
        fuzz_get_random_data(&rl6.rgi6.hwaddr, 6);
133
134
1.34k
        char *t1 = gb_get_random_string();
135
1.34k
        if (strlen(t1) > 16) {
136
64
          memcpy(rl6.rgi6.iface, t1, 16);
137
1.27k
        } else {
138
1.27k
          memcpy(rl6.rgi6.iface, t1, strlen(t1));
139
1.27k
        }
140
141
1.34k
        init_route_ipv6_list(&rl6, opt6, remote_endpoint, 0, &remote_host, c.es,
142
1.34k
                             &c);
143
1.34k
        route_list_ipv6_inited = 1;
144
1.34k
      }
145
1.48k
      break;
146
353
    case 7: {
147
353
      unsigned int flags;
148
353
      struct route_ipv6 r6;
149
353
      struct tuntap tt;
150
353
      memset(&tt, 0, sizeof(tt));
151
353
      tt.actual_name = gb_get_random_string();
152
353
      r6.iface = gb_get_random_string();
153
353
      r6.flags = fuzz_randomizer_get_int(0, 0xfffff);
154
353
      r6.netbits = fuzz_randomizer_get_int(0, 0xfffff);
155
353
      r6.metric = fuzz_randomizer_get_int(0, 0xfffff);
156
157
353
      r6.next = NULL;
158
159
353
      add_route_ipv6(&r6, &tt, 0, c.es, &c);
160
353
    } break;
161
746
    case 8:
162
746
      if (route_list_ipv6_inited && route_list_inited) {
163
566
        delete_routes(&rl, &rl6, NULL, 0, c.es, &c);
164
566
        route_list_ipv6_inited = 0;
165
566
        route_list_inited = 0;
166
566
      }
167
746
      break;
168
461
    case 9:
169
461
      if (route_list_ipv6_inited) {
170
404
        setenv_routes_ipv6(c.es, &rl6);
171
404
      }
172
461
      break;
173
978
    case 10: {
174
978
      add_route_ipv6_to_option_list(opt6,
175
978
                        gb_get_random_string(),
176
978
                                    gb_get_random_string(),
177
978
                                    gb_get_random_string(),
178
978
            fuzz_randomizer_get_int(0, 100));
179
978
    } break;
180
204
    case 11: {
181
204
      print_route_options(opt, M_NONFATAL);
182
204
    } break;
183
1.69k
    case 12: {
184
1.69k
      add_route_to_option_list(opt,
185
1.69k
                   gb_get_random_string(),
186
1.69k
                               gb_get_random_string(),
187
1.69k
             gb_get_random_string(),
188
1.69k
                               gb_get_random_string(),
189
1.69k
             fuzz_randomizer_get_int(0, 100));
190
1.69k
    } break;
191
92
    default:
192
92
      break;
193
35.2k
    }
194
35.2k
  }
195
196
2.53k
  if (route_list_inited) {
197
2.44k
    gc_free(&rl.gc);
198
2.44k
  }
199
2.53k
  env_set_destroy(c.es);
200
2.53k
  context_gc_free(&c);
201
202
2.53k
  fuzz_random_destroy();
203
204
2.53k
  gb_cleanup();
205
206
2.53k
  return 0;
207
2.53k
}