Debian Codegraph @

regress/parser-libfuzzer/test_parser_fuzz.c

raw
1 /* $OpenBSD$ */
2 /*
3 * Fuzz tests for payload parsing
4 *
5 * Placed in the public domain
6 */
7
8 #include <sys/socket.h>
9 #include <sys/queue.h>
10 #include <sys/uio.h>
11
12 #include <event.h>
13 #include <imsg.h>
14 #include <string.h>
15
16 #include "iked.h"
17 #include "ikev2.h"
18
19 u_int8_t cookies[] = {
20 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x00, 0x01, /* initator cookie */
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* responder cookie */
22 };
23
24 u_int8_t genhdr[] = {
25 0x00, 0x20, 0x22, 0x08, /* next, major/minor, exchange type, flags */
26 0x00, 0x00, 0x00, 0x00, /* message ID */
27 0x00, 0x00, 0x00, 0x00 /* total length */
28 };
29
30 #define OFFSET_ICOOKIE 0
31 #define OFFSET_RCOOKIE 8
32 #define OFFSET_NEXTPAYLOAD (0 + sizeof(cookies))
33 #define OFFSET_VERSION (1 + sizeof(cookies))
34 #define OFFSET_EXCHANGE (2 + sizeof(cookies))
35 #define OFFSET_LENGTH (8 + sizeof(cookies))
36
37 static u_int8_t *
38 get_icookie(u_int8_t *data)
39 {
40 return &data[OFFSET_ICOOKIE];
41 }
42
43 static u_int8_t *
44 get_rcookie(u_int8_t *data)
45 {
46 return &data[OFFSET_RCOOKIE];
47 }
48
49 static u_int8_t
50 get_nextpayload(u_int8_t *data)
51 {
52 return data[OFFSET_NEXTPAYLOAD];
53 }
54
55 static u_int8_t
56 get_version(u_int8_t *data)
57 {
58 return data[OFFSET_VERSION];
59 }
60
61 static u_int8_t
62 get_exchange(u_int8_t *data)
63 {
64 return data[OFFSET_EXCHANGE];
65 }
66
67 static u_int32_t
68 get_length(u_int8_t *data)
69 {
70 return *(u_int32_t *)&data[OFFSET_LENGTH];
71 }
72
73 static void
74 prepare_header(struct ike_header *hdr, struct ibuf *data)
75 {
76 bzero(hdr, sizeof(*hdr));
77 bcopy(get_icookie(ibuf_data(data)), &hdr->ike_ispi,
78 sizeof(hdr->ike_ispi));
79 bcopy(get_rcookie(ibuf_data(data)), &hdr->ike_rspi,
80 sizeof(hdr->ike_rspi));
81 hdr->ike_nextpayload = get_nextpayload(ibuf_data(data));
82 hdr->ike_version = get_version(ibuf_data(data));
83 hdr->ike_exchange = get_exchange(ibuf_data(data));
84 hdr->ike_length = get_length(ibuf_data(data));
85 }
86
87 static void
88 prepare_message(struct iked_message *msg, struct ibuf *data)
89 {
90 static struct iked_sa sa;
91
92 bzero(&sa, sizeof(sa));
93 bzero(msg, sizeof(*msg));
94
95 msg->msg_sa = &sa;
96 msg->msg_data = data;
97 msg->msg_e = 1;
98 msg->msg_parent = msg;
99
100 TAILQ_INIT(&msg->msg_proposals);
101 SIMPLEQ_INIT(&msg->msg_certreqs);
102 }
103
104 /* Entry-Point for libFuzzer */
105 int
106 LLVMFuzzerTestOneInput(const char *data, size_t size)
107 {
108 struct ibuf *fuzzed;
109 struct ike_header hdr;
110 struct iked_message msg;
111
112 bzero(&hdr, sizeof(hdr));
113 bzero(&msg, sizeof(msg));
114
115 fuzzed = ibuf_new(data, size);
116 if (fuzzed == NULL){
117 fprintf(stderr, "%s\n", "ERROR: fuzzed == NULL! "
118 "(hint: fuzz-input too long?)");
119 return -1;
120 }
121
122 /* size too small? */
123 if (size < sizeof(cookies) + sizeof(genhdr)){
124 ibuf_free(fuzzed);
125 return 0;
126 }
127
128 prepare_header(&hdr, fuzzed);
129 prepare_message(&msg, fuzzed);
130
131 ikev2_pld_parse(NULL, &hdr, &msg, 0);
132
133 ikev2_msg_cleanup(NULL, &msg);
134
135 return 0;
136 }