/src/libpcap/testprogs/fuzz/fuzz_both.c
Line | Count | Source |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include <fcntl.h> |
4 | | #include <errno.h> |
5 | | #include <string.h> |
6 | | #include <unistd.h> |
7 | | |
8 | | #include <pcap/pcap.h> |
9 | | #include "portability-exec.h" |
10 | | |
11 | | FILE * outfile = NULL; |
12 | | |
13 | 14.8k | static int bufferToFile(const char * name, const uint8_t *Data, size_t Size) { |
14 | 14.8k | FILE * fd; |
15 | 14.8k | if (remove(name) != 0) { |
16 | 0 | if (errno != ENOENT) { |
17 | 0 | printf("failed remove, errno=%d\n", errno); |
18 | 0 | return -1; |
19 | 0 | } |
20 | 0 | } |
21 | 14.8k | fd = fopen(name, "wb"); |
22 | 14.8k | if (fd == NULL) { |
23 | 0 | printf("failed open, errno=%d\n", errno); |
24 | 0 | return -2; |
25 | 0 | } |
26 | 14.8k | if (fwrite (Data, 1, Size, fd) != Size) { |
27 | 0 | fclose(fd); |
28 | 0 | return -3; |
29 | 0 | } |
30 | 14.8k | fclose(fd); |
31 | 14.8k | return 0; |
32 | 14.8k | } |
33 | | |
34 | 0 | void fuzz_openFile(const char * name) { |
35 | 0 | if (outfile != NULL) { |
36 | 0 | fclose(outfile); |
37 | 0 | } |
38 | 0 | outfile = fopen(name, "w"); |
39 | 0 | } |
40 | | |
41 | 14.8k | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
42 | 14.8k | pcap_t * pkts; |
43 | 14.8k | char errbuf[PCAP_ERRBUF_SIZE]; |
44 | 14.8k | char filename[FILENAME_MAX] = { 0 }; |
45 | 14.8k | const u_char *pkt; |
46 | 14.8k | struct pcap_pkthdr *header; |
47 | 14.8k | int fd = -1, r; |
48 | 14.8k | size_t filterSize; |
49 | 14.8k | char * filter; |
50 | 14.8k | struct bpf_program bpf; |
51 | | |
52 | | |
53 | | //initialize output file |
54 | 14.8k | if (outfile == NULL) { |
55 | 1 | outfile = fopen(DEV_NULL, "w"); |
56 | 1 | if (outfile == NULL) { |
57 | 0 | return 0; |
58 | 0 | } |
59 | 1 | } |
60 | | |
61 | 14.8k | if (Size < 1) { |
62 | 0 | return 0; |
63 | 0 | } |
64 | 14.8k | filterSize = Data[0]; |
65 | 14.8k | if (Size < 1+filterSize || filterSize == 0) { |
66 | 16 | return 0; |
67 | 16 | } |
68 | | |
69 | | //generate temporary file name |
70 | 14.8k | snprintf(filename, FILENAME_MAX, "/tmp/libpcap_fuzz_both.XXXXXX"); |
71 | 14.8k | if ((fd = mkstemp(filename)) < 0) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | 14.8k | close(fd); |
75 | | |
76 | | //rewrite buffer to a file as libpcap does not have buffer inputs |
77 | 14.8k | if (bufferToFile(filename, Data+1+filterSize, Size-(1+filterSize)) < 0) { |
78 | 0 | unlink(filename); |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | | //initialize structure |
83 | 14.8k | pkts = pcap_open_offline(filename, errbuf); |
84 | 14.8k | if (pkts == NULL) { |
85 | 779 | fprintf(outfile, "Couldn't open pcap file %s\n", errbuf); |
86 | 779 | unlink(filename); |
87 | 779 | return 0; |
88 | 779 | } |
89 | | |
90 | 14.0k | filter = malloc(filterSize); |
91 | 14.0k | memcpy(filter, Data+1, filterSize); |
92 | | //null terminate string |
93 | 14.0k | filter[filterSize-1] = 0; |
94 | | |
95 | 14.0k | if (pcap_compile(pkts, &bpf, filter, 1, PCAP_NETMASK_UNKNOWN) == 0) { |
96 | | //loop over packets |
97 | 8.54k | r = pcap_next_ex(pkts, &header, &pkt); |
98 | 37.9k | while (r > 0) { |
99 | | //checks filter |
100 | 29.4k | fprintf(outfile, "packet length=%d/%d filter=%d\n",header->caplen, header->len, pcap_offline_filter(&bpf, header, pkt)); |
101 | 29.4k | r = pcap_next_ex(pkts, &header, &pkt); |
102 | 29.4k | } |
103 | | //close structure |
104 | 8.54k | pcap_close(pkts); |
105 | 8.54k | pcap_freecode(&bpf); |
106 | 8.54k | } |
107 | 5.54k | else { |
108 | 5.54k | pcap_close(pkts); |
109 | 5.54k | } |
110 | 14.0k | free(filter); |
111 | | |
112 | 14.0k | unlink(filename); |
113 | 14.0k | return 0; |
114 | 14.8k | } |