aboutsummaryrefslogtreecommitdiff
path: root/projects/openvpn/fuzz_forward.c
blob: dff3e93d3c50cfd65dff95b5d92e770dc187c3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "config.h"
#include <sys/time.h>
#include "syshead.h"
#include "interval.h"
#include "init.h"
#include "buffer.h"
#include "forward.h"

#include "fuzz_randomizer.h"


static int init_c2_outgoing_link(struct context_2 *c2, struct gc_arena *gc) {
  struct link_socket_actual *to_link_addr = NULL;
  struct link_socket *link_socket = NULL;
  struct socks_proxy_info *socks_proxy = NULL;
  struct buffer buf;

  c2->tun_write_bytes = 0;
  ALLOC_ARRAY_GC(link_socket, struct link_socket, 1, gc);
  memset(link_socket, 0, sizeof(*link_socket));

  c2->link_socket = link_socket;

  if (fuzz_randomizer_get_int(0, 2) != 0) {
    c2->link_socket->info.proto = PROTO_UDP;
  } else {
    c2->link_socket->info.proto = PROTO_TCP_SERVER;
  }

  ALLOC_ARRAY_GC(socks_proxy, struct socks_proxy_info, 1, gc);
  memset(socks_proxy, 0, sizeof(*socks_proxy));
  c2->link_socket->socks_proxy = socks_proxy;

  c2->frame.link_mtu_dynamic = fuzz_randomizer_get_int(0, 0xfffffff);
  c2->frame.extra_frame = fuzz_randomizer_get_int(0, 0xfffffff);
  c2->frame.extra_tun = fuzz_randomizer_get_int(0, 0xfffffff);
  c2->frame.link_mtu = fuzz_randomizer_get_int(0, 0xfffffff);

  ALLOC_ARRAY_GC(to_link_addr, struct link_socket_actual, 1, gc);
  memset(to_link_addr, 0, sizeof(*to_link_addr));
  c2->to_link_addr = to_link_addr;

  c2->to_link_addr->dest.addr.sa.sa_family = AF_INET;
  c2->to_link_addr->dest.addr.in4.sin_addr.s_addr = 1;

  char *tmp = get_random_string();
  buf = alloc_buf_gc(strlen(tmp), gc);
  buf_write(&buf, tmp, strlen(tmp));
  int val = fuzz_randomizer_get_int(0, strlen(tmp));
  buf.offset = val;
  free(tmp);

  c2->link_socket->stream_buf.maxlen = BLEN(&buf);
  c2->to_link = buf;

  if (buf.offset < 10) {
    return -1;
  }
  return 0;
}

void fuzz_process_outgoing_link(const uint8_t *data, size_t size) {
  struct context ctx;
  struct gc_arena gc = gc_new();
  memset(&ctx, 0, sizeof(ctx));

  if (init_c2_outgoing_link(&ctx.c2, &gc) == 0) {
    process_outgoing_link(&ctx);
  }

  gc_free(&gc);
}

static int _init_options(struct options *options, struct client_nat_entry **cne,
                         struct gc_arena *gc) {
  options->passtos = false;
  options->mode = MODE_POINT_TO_POINT;
  options->allow_recursive_routing = true;
  options->client_nat = new_client_nat_list(gc);

  struct client_nat_entry *_cne;
  ALLOC_ARRAY_GC(cne[0], struct client_nat_entry, 1, gc);
  _cne = cne[0];
  memset(_cne, 0, sizeof(struct client_nat_entry));

  struct client_nat_option_list clist;
  clist.n = 1;
  clist.entries[0] = *_cne;
  copy_client_nat_option_list(options->client_nat, &clist);
  options->route_gateway_via_dhcp = false;

  return 0;
}

static int init_c2_incoming_tun(struct context_2 *c2, struct gc_arena *gc) {
  struct buffer buf;
  memset(&buf, 0, sizeof(buf));

  struct link_socket *link_socket = NULL;
  ALLOC_ARRAY_GC(link_socket, struct link_socket, 1, gc);
  c2->link_socket = link_socket;

  ALLOC_OBJ_GC(c2->link_socket_info, struct link_socket_info, gc);
  ALLOC_OBJ_GC(c2->link_socket_info->lsa, struct link_socket_addr, gc);
  c2->link_socket_info->lsa->bind_local = NULL;
  c2->link_socket_info->lsa->remote_list = NULL;
  c2->link_socket_info->lsa->current_remote = NULL;
  c2->link_socket_info->lsa->remote_list = NULL;
  c2->es = env_set_create(gc);

  c2->frame.link_mtu_dynamic = 0;
  c2->frame.extra_frame = 0;
  c2->frame.extra_tun = 0;
  c2->to_link_addr = NULL;

  char *tmp = get_random_string();
  buf = alloc_buf(strlen(tmp));
  buf_write(&buf, tmp, strlen(tmp));

  int retval;
  if (strlen(tmp) > 5) {
    retval = 0;
  } else {
    retval = 1;
  }

  free(tmp);

  c2->buf = buf;
  c2->buffers = init_context_buffers(&c2->frame);
  c2->log_rw = false;

  return retval;
}

int run_process_incoming_tun(const uint8_t *data, size_t size) {
  struct gc_arena gc;
  struct context ctx;
  struct client_nat_entry *cne[MAX_CLIENT_NAT];
  struct route_list route_list;

  memset(&ctx, 0, sizeof(ctx));
  memset(cne, 0, sizeof(cne));

  gc = gc_new();

  _init_options(&ctx.options, cne, &gc);

  // Init tuntap
  struct tuntap tuntap;
  tuntap.type = DEV_TYPE_TAP;

  ctx.c1.tuntap = &tuntap;

  int retval = init_c2_incoming_tun(&ctx.c2, &gc);
  ctx.c1.route_list = &route_list;
  if (retval == 0) {
    process_incoming_tun(&ctx);
  }

  free(ctx.c2.buf.data);
  free_context_buffers(ctx.c2.buffers);
  gc_free(&gc);
}

static int init_c2_outgoing_tun(struct context_2 *c2, struct gc_arena *gc) {
  struct buffer buf;

  c2->tun_write_bytes = 0;
  c2->frame.link_mtu_dynamic = fuzz_randomizer_get_int(0, 0xfffffff);
  c2->frame.extra_frame = fuzz_randomizer_get_int(0, 0xfffffff);
  c2->frame.extra_tun = fuzz_randomizer_get_int(0, 0xfffffff);

  char *tmp = get_random_string();
  buf = alloc_buf_gc(strlen(tmp), gc);
  buf_write(&buf, tmp, strlen(tmp));
  free(tmp);

  c2->to_tun = buf;
  return 0;
}

void run_process_outgoing_tun(uint8_t *data, size_t size) {
  struct gc_arena gc;
  struct context ctx;
  struct tuntap tuntap;

  memset(&ctx, 0, sizeof(ctx));
  gc = gc_new();

  tuntap.type = DEV_TYPE_TAP;
  ctx.c1.tuntap = &tuntap;

  init_c2_outgoing_tun(&ctx.c2, &gc);
  process_outgoing_tun(&ctx);

  gc_free(&gc);
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  fuzz_random_init(data, size);

  int dec = fuzz_randomizer_get_int(0, 2);
  if (dec == 0) {
    run_process_incoming_tun(data, size);
  }
	else if (dec == 1) {
		run_process_outgoing_tun(data, size);
	}
  else {
    fuzz_process_outgoing_link(data, size);
  }

  fuzz_random_destroy();
  return 0;
}