-
Notifications
You must be signed in to change notification settings - Fork 1
/
pl7m.c
1521 lines (1355 loc) · 36 KB
/
pl7m.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2023-24 Ivan Nardi <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <pcap.h>
#include <asm/byteorder.h>
#include <linux/ppp_defs.h>
#include "pl7m.h"
#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
#include <sanitizer/msan_interface.h>
#endif
#endif
#ifdef __cplusplus
extern "C"
#endif
/* Configuration options/defines:
* PL7M_ENABLE_ERROR: to enable logging of important/critical errors
* PL7M_ENABLE_LOG: to enable verbose logging
* PL7M_USE_INTERNAL_FUZZER_MUTATE: instead of using the standard function
`LLVMFuzzerMutate()` provided by libfuzz, use a custom/internal logic
to randomize the data. It is usefull if you want to use this code
without linking to libfuzz
* PL7M_USE_SIMPLEST_MUTATOR: instead of fuzzing only the L7 part of the
packets, randomize the entire data. Note that the output of the
mutator will be a valid pcap file anyway
* PL7M_DISABLE_PACKET_MUTATION: disable mutations at packet level (see
below). The output trace contains the same packets (in the same order
and with the same timestamp) of the input trace
* PL7M_DISABLE_PAYLOAD_MUTATION: disable mutations at payload level
(see below)
* PL7M_USE_64K_PACKETS: allow packets with maximum size (~64k) instead of
the standard size (~1526). Useful for handling TSO packets or for
checking integer overflow on u_int16_t variables (i.e. ip length...).
Note that this option might lead to significant bigger corpus
Mutations happens at two different levels:
* packet level: each packet might be dropped, duplicated, swapped or
its direction might be swapped (i.e. from client->server to server->client)
* payload level: packet (L5/7) payload (i.e. data after TCP/UDP header)
is changed
*/
#ifndef IPPROTO_IPV4
#define IPPROTO_IPV4 4
#endif
#ifndef IPPROTO_OSPF
#define IPPROTO_OSPF 89
#endif
#ifndef IPPROTO_VRRP
#define IPPROTO_VRRP 112
#endif
#ifndef IPPROTO_PGM
#define IPPROTO_PGM 113
#endif
#ifdef PL7M_ENABLE_ERROR
#define derr(fmt, args...) \
do { \
fprintf(stderr, "" fmt, ## args);\
} while (0)
#else
#define derr(fmt, args...) \
do { \
} while (0)
#endif
#ifdef PL7M_ENABLE_LOG
#define ddbg(fmt, args...) \
do { \
fprintf(stderr, "" fmt, ## args);\
} while (0)
#else
#define ddbg(fmt, args...) \
do { \
} while (0)
#endif
#ifdef PL7M_USE_64K_PACKETS
#define MAX_PKT_LENGTH (26 + 20 + 1024 * 64) /* Max possible size: ethernet + ip(v4) + 64k ip payload */
#else
#define MAX_PKT_LENGTH (26 + 1500) /* "Standard" maximum packet size */
#endif
#ifndef PL7M_USE_INTERNAL_FUZZER_MUTATE
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
#endif
/* If you want custom memory allocators, you can simply change these defines */
#define pl7m_malloc(size) malloc(size)
#define pl7m_calloc(num, size) calloc(num, size)
#define pl7m_free(p) free(p)
struct gre_header {
#if defined(__LITTLE_ENDIAN_BITFIELD)
u_int16_t rec:3,
srr:1,
seq:1,
key:1,
routing:1,
csum:1,
version:3,
reserved:4,
ack:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
u_int16_t csum:1,
routing:1,
key:1,
seq:1,
srr:1,
rec:3,
ack:1,
reserved:4,
version:3;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
__u16 protocol;
};
#define GTP_MSG_TPDU 0xFF
struct gtp_header {
#if defined(__LITTLE_ENDIAN_BITFIELD)
u_int16_t n_pdu:1,
sequence:1,
extension:1,
reserved:1,
protocol:1,
version:3,
type:8;
#elif defined(__BIG_ENDIAN_BITFIELD)
u_int16_t version:3,
protocol:1,
reserved:1,
extension:1,
sequence:1,
n_pdu:1,
type:8;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
u_int16_t total_length;
u_int32_t teid;
};
struct gtp_header_optional {
u_int16_t sn;
u_int8_t n_pdu_nbr;
u_int8_t next_hdr;
};
struct m_pkt {
unsigned char *raw_data;
struct pcap_pkthdr header;
int l2_offset;
int prev_l3_offset;
u_int16_t prev_l3_proto;
int gtp_offset;
int l3_offset;
u_int16_t l3_proto;
int l4_offset;
u_int8_t l4_proto;
int l4_length;
int l5_offset;
int l5_length;
int is_l3_fragment;
int skip_l4_dissection;
int skip_payload_actions;
struct m_pkt *next;
};
struct pl7m_handle {
int datalink;
struct m_pkt *head;
struct m_pkt *tail;
};
/*
Dissection code: START
*/
static int __is_datalink_supported(int datalink_type)
{
switch(datalink_type) {
case DLT_NULL:
case DLT_EN10MB:
case DLT_PPP:
case DLT_C_HDLC:
case DLT_RAW:
case DLT_LINUX_SLL:
case DLT_LINUX_SLL2:
case DLT_IPV4:
case DLT_IPV6:
case DLT_PPI:
return 1;
default:
return 0;
}
}
static int __is_l3_proto_supported(int proto)
{
switch(proto) {
case ETH_P_IP:
case ETH_P_IPV6:
case ETH_P_ARP:
/* TODO: add other protocols */
return 1;
default:
return 0;
}
}
static int dissect_l2(int datalink_type, struct m_pkt *p)
{
int l2_offset, l3_offset;
u_int16_t l3_proto = 0, next, header_length;
u_int32_t dlt;
unsigned char *data = p->raw_data;
int data_len = p->header.caplen;
if (data_len <= 0) {
derr("Invalid len %d\n", data_len);
return -1;
}
l2_offset = p->l2_offset;
assert(l2_offset >= 0 && l2_offset < (int)p->header.caplen);
switch(datalink_type) {
case DLT_NULL:
if (data_len < l2_offset + 5)
return -1;
l3_offset = l2_offset + 4;
if ((data[l3_offset] & 0xF0) == 0x40)
l3_proto = ETH_P_IP;
else if ((data[l3_offset] & 0xF0) == 0x60)
l3_proto = ETH_P_IPV6;
break;
case DLT_RAW:
if (data_len < l2_offset + 1)
return -1;
l3_offset = l2_offset + 0;
if ((data[l3_offset] & 0xF0) == 0x40)
l3_proto = ETH_P_IP;
else if ((data[l3_offset] & 0xF0) == 0x60)
l3_proto = ETH_P_IPV6;
break;
case DLT_IPV4:
l3_proto = ETH_P_IP;
l3_offset = l2_offset + 0;
break;
case DLT_IPV6:
l3_proto = ETH_P_IPV6;
l3_offset = l2_offset + 0;
break;
case DLT_LINUX_SLL:
if (data_len < l2_offset + 16)
return -1;
l3_proto = ntohs(*((u_int16_t *)&data[l2_offset + 14]));
l3_offset = 16;
break;
case DLT_LINUX_SLL2:
if (data_len < l2_offset + 20)
return -1;
l3_proto = ntohs(*((u_int16_t *)&data[l2_offset]));
l3_offset = 20;
break;
case DLT_PPI:
if (data_len < l2_offset + 8)
return -1;
header_length = le16toh(*(u_int16_t *)&data[l2_offset + 2]);
dlt = le32toh(*(u_int32_t *)&data[l2_offset + 4]);
if(dlt != DLT_EN10MB) /* Handle only standard ethernet, for the time being */
return -1;
p->l2_offset += header_length;
if (p->l2_offset >= (int)p->header.caplen)
return -1;
return dissect_l2(dlt, p);
case DLT_PPP:
case DLT_C_HDLC:
if (data[l2_offset + 0] == 0x0f || data[l2_offset + 0] == 0x8f) {
if (data_len < l2_offset + 4)
return -1;
l3_offset = 4;
l3_proto = ntohs(*((u_int16_t *)&data[l2_offset + 2]));
} else {
if (data_len < l2_offset + 2)
return -1;
l3_offset = l2_offset + 2;
next = ntohs(*((u_int16_t *)&data[l2_offset + 0]));
switch (next) {
case 0x0021:
l3_proto = ETH_P_IP;
break;
case 0x0057:
l3_proto = ETH_P_IPV6;
break;
default:
derr("Unknown next proto on ppp 0x%x\n", next);
return -1;
}
}
break;
case DLT_EN10MB:
if (data_len < l2_offset + 14)
return -1;
l3_offset = l2_offset + 14;
l3_proto = ntohs(*((u_int16_t *)&data[l3_offset - 2]));
/* VLAN */
while (l3_proto == 0x8100 && l3_offset + 4 < data_len) {
l3_offset += 4;
l3_proto = ntohs(*((u_int16_t *)&data[l3_offset - 2]));
}
/* PPPoES */
if (l3_proto == 0x8864) {
if (data_len < l3_offset + 8)
return -1;
l3_offset += 8;
next = ntohs(*((u_int16_t *)&data[l3_offset - 2]));
switch (next) {
case 0x0021:
l3_proto = ETH_P_IP;
break;
case 0x0057:
l3_proto = ETH_P_IPV6;
break;
default:
derr("Unknown next proto on pppoes 0x%x\n", next);
return -1;
}
}
break;
default:
derr("Unknown datalink %d\n", datalink_type);
return -1;
}
if (data_len < l3_offset) {
derr("Invalid length %d < %d\n", data_len, l3_offset);
return -1;
}
if (!__is_l3_proto_supported(l3_proto)) {
derr("Unsupported l3_proto 0x%x\n", l3_proto);
return -1;
}
p->l3_offset = l3_offset;
p->l3_proto = l3_proto;
return 0;
}
static int __is_l4_proto_supported(int proto)
{
switch(proto) {
case IPPROTO_UDP:
case IPPROTO_TCP:
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
case IPPROTO_IGMP:
case IPPROTO_VRRP:
case IPPROTO_AH:
case IPPROTO_ESP:
case IPPROTO_SCTP:
case IPPROTO_PGM:
case IPPROTO_PIM:
case IPPROTO_IPV4:
case IPPROTO_IPV6:
case IPPROTO_GRE:
case IPPROTO_OSPF:
/* TODO: add other protocols */
return 1;
default:
return 0;
}
}
static int dissect_l3(struct m_pkt *p)
{
struct ip *ip4;
struct ip6_hdr *ip6;
struct ip6_ext *ipv6_opt;
int num_eh, ip_hdr_len, l3_len;
unsigned char *data = p->raw_data + p->l3_offset;
int data_len = p->header.caplen - p->l3_offset;
ddbg("L3: l3_proto %d data_len %d\n", p->l3_proto, data_len);
if (data_len < 0)
return -1;
switch (p->l3_proto) {
case ETH_P_IP:
ip4 = (struct ip *)data;
if (data_len < 20 /* min */ ||
ip4->ip_v != 4 ||
ip4->ip_hl < 5 ||
data_len < ip4->ip_hl * 4 ||
ntohs(ip4->ip_len) < ip4->ip_hl * 4) {
derr("Wrong lengths %d %d %d\n", data_len, ip4->ip_hl,
ntohs(ip4->ip_len));
return -1;
}
/* TODO: properly handle fragments */
if ((ntohs(ip4->ip_off) & IP_MF) ||
(ntohs(ip4->ip_off) & IP_OFFMASK)) {
ddbg("Fragment\n");
p->is_l3_fragment = 1;
p->skip_payload_actions = 1;
}
if (!__is_l4_proto_supported(ip4->ip_p)) {
derr("Unsupported L4: %d\n", ip4->ip_p);
return -1;
}
p->l4_proto = ip4->ip_p;
p->l4_offset = p->l3_offset + ip4->ip_hl * 4;
p->l4_length = ntohs(ip4->ip_len) - ip4->ip_hl * 4;
break;
case ETH_P_IPV6:
ip6 = (struct ip6_hdr *)data;
if (data_len < (int)sizeof(struct ip6_hdr))
return -1;
/* It may be a IPv6 Jumbograms but it is probably a
malformed packet */
if (ip6->ip6_plen == 0) {
derr("Invalid ext len\n");
return -1;
}
ip_hdr_len = sizeof(struct ip6_hdr);
l3_len = ntohs(ip6->ip6_plen) + ip_hdr_len;
if (l3_len < ip_hdr_len || data_len < l3_len) {
derr("Invalid ipv6 lengths %d %d %d\n",
l3_len, ip_hdr_len, data_len);
return -1;
}
p->l4_proto = ip6->ip6_nxt;
/* Extension header */
num_eh = 0;
while (p->l4_proto == IPPROTO_HOPOPTS ||
p->l4_proto == IPPROTO_DSTOPTS ||
p->l4_proto == IPPROTO_ROUTING ||
p->l4_proto == IPPROTO_AH ||
p->l4_proto == IPPROTO_FRAGMENT) {
num_eh++;
if (data_len < ip_hdr_len + (int)sizeof(struct ip6_ext)) {
derr("Error ipv6 (a) %d %d\n", data_len, ip_hdr_len);
return -1;
}
if (ip_hdr_len >= l3_len) {
derr("Error ipv6 (b) %d %d\n", ip_hdr_len, l3_len);
return -1;
}
/* RFC2460 4.1 . Hop-by-Hop Options header [..] is
restricted to appear immediately after an IPv6 header only */
if (p->l4_proto == IPPROTO_HOPOPTS && num_eh != 1) {
derr("Hop-by-Hop Options not first header\n");
return -1;
}
ipv6_opt = (struct ip6_ext *)&data[ip_hdr_len];
ip_hdr_len += sizeof(struct ip6_ext);
ddbg("EH (%d) %d ip6e_len %d\n", num_eh, p->l4_proto,
ipv6_opt->ip6e_len);
if (p->l4_proto == IPPROTO_AH) {
/* RFC4302 2.2. Payload Length: This 8-bit
field specifies the length of AH in
32-bit words (4-byte units), minus "2". */
ip_hdr_len += ipv6_opt->ip6e_len * 4;
} else if (p->l4_proto == IPPROTO_HOPOPTS) {
/* RFC2460 4.3. Hdr Ext Len: Length of the Hop-by-Hop
Options header in 8-octet units, not including
the first 8 octets */
ip_hdr_len += (8 + ipv6_opt->ip6e_len * 8);
} else if (p->l4_proto == IPPROTO_ROUTING) {
/* RFC8200 4.4. Hdr Ext Len: Length of the Routing
header in 8-octet units, not including the
first 8 octets. */
ip_hdr_len += (8 + ipv6_opt->ip6e_len * 8);
} else {
if (p->l4_proto != IPPROTO_FRAGMENT) {
ip_hdr_len += ipv6_opt->ip6e_len;
} else {
ip_hdr_len += 6;
ddbg("Fragment IPv6\n");
p->is_l3_fragment = 1;
p->skip_payload_actions = 1;
}
}
p->l4_proto = ipv6_opt->ip6e_nxt;
if (ip_hdr_len >= l3_len) {
derr("Error ipv6 (c) %d %d\n", ip_hdr_len, l3_len);
return -1;
}
}
if (!__is_l4_proto_supported(p->l4_proto)) {
derr("Unsupported L4: %d\n", p->l4_proto);
return -1;
}
p->l4_proto = ip6->ip6_nxt;
p->l4_offset = p->l3_offset + ip_hdr_len;
p->l4_length = ntohs(ip6->ip6_plen) - (ip_hdr_len - sizeof(struct ip6_hdr));
break;
case ETH_P_ARP:
p->skip_l4_dissection = 1;
p->skip_payload_actions = 1;
break;
default:
assert(0);
}
return 0;
}
static int dissect_l4(struct m_pkt *p)
{
struct udphdr *udp_h;
struct tcphdr *tcp_h;
struct gre_header *gre_h;
unsigned char *data = p->raw_data + p->l4_offset;
int data_len = p->header.caplen - p->l4_offset;
int l4_hdr_len, rc;
unsigned char *ppp_h;
u_int16_t ppp_proto;
ddbg("L4: l4_proto %d data_len %d l4_length %d\n",
p->l4_proto, data_len, p->l4_length);
if (data_len < 0 || p->l4_length > data_len)
return -1;
if (p->is_l3_fragment) {
ddbg("Skip L4 dissection because it is a fragment\n");
return 0;
}
if (p->skip_l4_dissection) {
ddbg("Skip L4 dissection\n");
return 0;
}
switch(p->l4_proto) {
case IPPROTO_UDP:
udp_h = (struct udphdr *)data;
if (p->l4_length < (int)sizeof(struct udphdr) ||
ntohs(udp_h->len) > p->l4_length ||
ntohs(udp_h->len) < sizeof(struct udphdr)) {
derr("Unexpected udp len %u vs %u\n",
ntohs(udp_h->len), p->l4_length);
return -1;
}
p->l5_offset = p->l4_offset + sizeof(struct udphdr);
p->l5_length = ntohs(udp_h->len) - sizeof(struct udphdr);
break;
case IPPROTO_TCP:
tcp_h = (struct tcphdr *)data;
if (p->l4_length < (int)sizeof(struct tcphdr)) {
derr("Unexpected tcp len %d\n", p->l4_length);
return -1;
}
l4_hdr_len = tcp_h->doff << 2;
if (l4_hdr_len < (int)sizeof(struct tcphdr) ||
l4_hdr_len > p->l4_length) {
derr("Unexpected tcp len %u %u\n",
l4_hdr_len, p->l4_length);
return -1;
}
p->l5_offset = p->l4_offset + l4_hdr_len;
p->l5_length = p->l4_length - l4_hdr_len;
break;
case IPPROTO_IPV4: /* IP in IP tunnel */
case IPPROTO_IPV6: /* IP in IP tunnel */
if (p->prev_l3_proto == 0) {
assert(p->prev_l3_offset == 0);
p->prev_l3_proto = p->l3_proto;
p->prev_l3_offset = p->l3_offset;
} else {
derr("More than 2 ip headers. Unsupported\n");
return -1;
}
p->l3_offset = p->l4_offset;
p->l3_proto = (p->l4_proto == IPPROTO_IPV4) ? ETH_P_IP : ETH_P_IPV6;
rc = dissect_l3(p);
if (rc != 0) {
derr("Error dissect_l3 (second header)\n");
return -1;
}
return dissect_l4(p);
case IPPROTO_GRE:
gre_h = (struct gre_header *)data;
if (p->l4_length < (int)sizeof(struct gre_header)) {
derr("Unexpected gre len %d\n", p->l4_length);
return -1;
}
/* Check version. 0 = GRE, 1 = ENHANCED GRE (used for PPTP) */
if ((gre_h->version != 0 && gre_h->version != 1) ||
(gre_h->version == 1 && ntohs(gre_h->protocol) != 0x880b)) {
derr("Unexpected gre version %d\n", gre_h->version);
return -1;
}
l4_hdr_len = sizeof(struct gre_header);
if (gre_h->key)
l4_hdr_len += 4;
if (gre_h->seq)
l4_hdr_len += 4;
if (gre_h->csum || gre_h->routing)
l4_hdr_len += 4;
if (gre_h->ack)
l4_hdr_len += 4;
if (p->l4_length < l4_hdr_len) {
derr("Unexpected gre len %d/%d\n", l4_hdr_len, p->l4_length);
return -1;
}
if (p->prev_l3_proto == 0) {
assert(p->prev_l3_offset == 0);
p->prev_l3_proto = p->l3_proto;
p->prev_l3_offset = p->l3_offset;
} else {
derr("More than 2 ip headers. Unsupported\n");
return -1;
}
if (gre_h->version == 0) {
p->l3_proto = ntohs(gre_h->protocol);
if (p->l3_proto == 0 && l4_hdr_len == p->l4_length) {
derr("GRE keepalive\n");
return -1;
}
if (p->l3_proto != ETH_P_IP && p->l3_proto != ETH_P_IPV6) {
derr("Invalid L3 after GRE: 0x%x\n", p->l3_proto);
return -1;
}
} else {
ppp_h = &data[l4_hdr_len];
if (ppp_h[0] == 0xFF) { /* PPP HDLC encapsulation */
if(p->l4_length < l4_hdr_len + 4) {
derr("Unexpected gre ppp len %d/%d\n",
l4_hdr_len, p->l4_length);
return -1;
}
ppp_proto = ntohs(*(u_int16_t *)(ppp_h + 2));
l4_hdr_len += 4;
} else { /* Address and control are compressed */
ppp_proto = ppp_h[0];
l4_hdr_len += 1;
}
switch (ppp_proto) {
case PPP_IP:
p->l3_proto = ETH_P_IP;
break;
case PPP_IPV6:
p->l3_proto = ETH_P_IPV6;
break;
default:
derr("Unexpected ppp proto %d\n", ppp_proto);
return -1;
}
}
p->l3_offset = p->l4_offset + l4_hdr_len;
rc = dissect_l3(p);
if (rc != 0) {
derr("Error dissect_l3 (after gre)\n");
return -1;
}
return dissect_l4(p);
default:
/* Fuzz also the L4 header itself, but leave at least 1 byte:
this way, we will never have an empty ip4/6 header */
if (p->l4_length == 0)
return -1;
p->l5_offset = p->l4_offset + 1;
p->l5_length = p->l4_length - 1;
break;
}
return 0;
}
static int is_gtp_u(unsigned char *gtp_buffer, int gtp_buffer_len,
const struct m_pkt *p, u_int16_t *l3_proto)
{
struct gtp_header *gtp_h;
struct udphdr *udp_h;
uint16_t new_layer_len = 0;
unsigned char sub_proto;
if (p->l4_proto != IPPROTO_UDP ||
gtp_buffer_len < (int)sizeof(struct gtp_header))
return 0;
/* Only default port */
udp_h = (struct udphdr *)(p->raw_data + p->l4_offset);
if(udp_h->source != htons(2152) &&
udp_h->dest != htons(2152))
return 0;
gtp_h = (struct gtp_header *)gtp_buffer;
if (gtp_h->version != 1 ||
gtp_h->type != GTP_MSG_TPDU ||
gtp_h->reserved != 0 ||
ntohs(gtp_h->total_length) > (gtp_buffer_len - sizeof(struct gtp_header))) {
ddbg("Invalid gtp header: %d, 0x%x, 0x%0x, %d vs %d\n",
gtp_h->version, gtp_h->type, gtp_h->reserved,
ntohs(gtp_h->total_length), gtp_buffer_len);
return 0;
}
new_layer_len = sizeof(struct gtp_header);
/* Optional header version 1 */
if (gtp_h->extension || gtp_h->sequence || gtp_h->n_pdu) {
new_layer_len += sizeof(struct gtp_header_optional);
if (gtp_buffer_len < new_layer_len)
return 0;
}
if (gtp_h->extension) {
unsigned int length = 0;
while (new_layer_len < (gtp_buffer_len - 1)) {
length = gtp_buffer[new_layer_len] << 2;
new_layer_len += length;
if (new_layer_len > gtp_buffer_len ||
gtp_buffer[new_layer_len - 1] == 0 || length == 0)
break;
}
if (new_layer_len > gtp_buffer_len ||
gtp_buffer[new_layer_len - 1] != 0 ||
length == 0) {
return 0;
}
}
/* Trying to detect next proto. Code taken from wireshark */
if (gtp_buffer_len < new_layer_len + 1)
return 0;
sub_proto = gtp_buffer[new_layer_len];
if ((sub_proto >= 0x45) && (sub_proto <= 0x4e)) {
/* This is most likely an IPv4 packet
* we can exclude 0x40 - 0x44 because the minimum header size is 20 octets
* 0x4f is excluded because PPP protocol type "IPv6 header compression"
* with protocol field compression is more likely than a plain
* IPv4 packet with 60 octet header size */
*l3_proto = ETH_P_IP;
} else if ((sub_proto & 0xf0) == 0x60) {
/* This is most likely an IPv6 packet */
*l3_proto = ETH_P_IPV6;
} else {
/* This seems to be a PPP packet */
/* TODO: code not back-ported from wireshark yet*/
return 0;
}
return new_layer_len;
}
static int dissect_l4_detunneling(struct m_pkt *p)
{
unsigned char *data = p->raw_data + p->l5_offset;
int data_len = p->header.caplen - p->l5_offset;
u_int16_t next_l3_proto;
int gtp_header_len, rc;
ddbg("L4(detunel): l4_proto %d data_len %d l5_length %d\n",
p->l4_proto, data_len, p->l5_length);
if (data_len < 0 || p->l5_length > data_len)
return -1;
/* TODO: try to handle tunnel over fragment */
if (p->is_l3_fragment) {
ddbg("Skip L4(detunnel) dissection because it is a fragment\n");
return 0;
}
/* No reasons to detunnel if we skipped L4 dissection */
if (p->skip_l4_dissection) {
ddbg("Skip L4 dissection\n");
return 0;
}
/* GTP detunneling: looking only for MSG T-PDU that carries
encapsulated data */
gtp_header_len = is_gtp_u(data, data_len, p, &next_l3_proto);
if (gtp_header_len > 0) {
ddbg("Found GTP-U\n");
if (p->prev_l3_proto == 0) {
assert(p->prev_l3_offset == 0);
p->prev_l3_proto = p->l3_proto;
p->prev_l3_offset = p->l3_offset;
} else {
derr("Multiple tunnels. Unsupported\n");
return -1;
}
assert(p->gtp_offset == 0);
p->gtp_offset = p->l5_offset;
p->l3_proto = next_l3_proto;
p->l3_offset = p->l5_offset + gtp_header_len;
rc = dissect_l3(p);
if (rc != 0) {
derr("Error dissect_l3 (after gtp)\n");
return -1;
}
return dissect_l4(p);
}
/* "Normal" L4 traffic */
return 0;
}
static int dissect_do(int datalink_type, struct m_pkt *p)
{
int rc;
rc = dissect_l2(datalink_type, p);
if (rc != 0) {
derr("Error dissect_l2\n");
return -1;
}
rc = dissect_l3(p);
if (rc != 0) {
derr("Error dissect_l3\n");
return -1;
}
rc = dissect_l4(p);
if (rc != 0) {
derr("Error dissect_l4\n");
return -1;
}
/* Some kind of detunneling over L4 (usually over UDP). Example: GTP */
rc = dissect_l4_detunneling(p);
if (rc != 0) {
derr("Error dissect_l5\n");
return -1;
}
return 0;
}
/*
Dissection code: END
*/
#ifdef PL7M_USE_INTERNAL_FUZZER_MUTATE
static size_t internal_FuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize)
{
int r;
unsigned char rand_byte;
size_t new_len = Size, offset;
r = rand();
switch (r % 5) {
case 0:
ddbg("Payload action: unchange\n");
new_len = Size;
break;
case 1:
ddbg("Payload action: change one byte at a random location\n");
if (Size > 0) {
offset = rand() % Size;
rand_byte = rand() % 255;
Data[offset] = rand_byte;
}
break;
case 2:
ddbg("Payload action: append zero bytes\n");
new_len = rand() % MaxSize;
if (new_len > Size)
memset(&Data[Size], '\0', new_len - Size);
else
new_len = Size;
break;
case 3:
ddbg("Payload action: add one random byte at random location\n");
if (MaxSize >= Size + 1) {
offset = Size == 0 ? 0 : rand() % Size;
rand_byte = rand() % 255;
new_len = Size + 1;
memmove(Data + offset + 1, Data + offset, Size - offset);
Data[offset] = rand_byte;
}
break;
case 4:
ddbg("Payload action: remove one byte from a random location\n");
if (Size > 0) {
offset = rand() % Size;
new_len = Size - 1;
memmove(Data + offset, Data + offset + 1, Size - offset - 1);
}
break;
}
return new_len;
}
#endif
static void update_do_l7(struct m_pkt *p)
{
struct udphdr *udp_h;
struct tcphdr *tcp_h;
struct gtp_header *gtp_h;
size_t new_l5_len;
int l4_header_len = 0;
int l5_len_diff;
struct ip *ip4;
struct ip6_hdr *ip6;
assert(p->l5_offset + p->l5_length <= (int)p->header.caplen);
assert(p->header.caplen <= MAX_PKT_LENGTH);
#ifndef PL7M_USE_INTERNAL_FUZZER_MUTATE
new_l5_len = LLVMFuzzerMutate(p->raw_data + p->l5_offset,
p->l5_length, MAX_PKT_LENGTH - p->l5_offset);
/* It seems the MASAN returns false positives. The value from
LLVMFuzzerMutate needs to be treated as initialized.
See a similar report:
https://github.com/google/libprotobuf-mutator/pull/213/commits/51629aaf874b38c42f5dc8b970cdf9156895c7e3
*/
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
__msan_unpoison(p->raw_data + p->l5_offset, new_l5_len);
# endif
#endif
#else
new_l5_len = internal_FuzzerMutate(p->raw_data + p->l5_offset,
p->l5_length,
MAX_PKT_LENGTH - p->l5_offset);
#endif
l5_len_diff = new_l5_len - p->l5_length;
ddbg("l5_len %u->%zu (%d)\n", p->l5_length, new_l5_len, l5_len_diff);
switch (p->l4_proto) {
case IPPROTO_UDP:
l4_header_len = sizeof(struct udphdr);
udp_h = (struct udphdr *)(p->raw_data + p->l4_offset);
udp_h->len = htons(l4_header_len + new_l5_len);