Debian Codegraph @

fuzz/fuzz_netlink.c

raw
1 /*
2 * Copyright (c) 2020 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #undef NDEBUG
9
10 #include <assert.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 #include "../openbsd-compat/openbsd-compat.h"
17 #include "mutator_aux.h"
18 #include "dummy.h"
19
20 struct param {
21 int seed;
22 int dev;
23 struct blob wiredata;
24 };
25
26 struct param *
27 unpack(const uint8_t *ptr, size_t len)
28 {
29 cbor_item_t *item = NULL, **v;
30 struct cbor_load_result cbor;
31 struct param *p;
32 int ok = -1;
33
34 if ((p = calloc(1, sizeof(*p))) == NULL ||
35 (item = cbor_load(ptr, len, &cbor)) == NULL ||
36 cbor.read != len ||
37 cbor_isa_array(item) == false ||
38 cbor_array_is_definite(item) == false ||
39 cbor_array_size(item) != 3 ||
40 (v = cbor_array_handle(item)) == NULL)
41 goto fail;
42
43 if (unpack_int(v[0], &p->seed) < 0 ||
44 unpack_int(v[1], &p->dev) < 0 ||
45 unpack_blob(v[2], &p->wiredata) < 0)
46 goto fail;
47
48 ok = 0;
49 fail:
50 if (ok < 0) {
51 free(p);
52 p = NULL;
53 }
54
55 if (item)
56 cbor_decref(&item);
57
58 return p;
59 }
60
61 size_t
62 pack(uint8_t *ptr, size_t len, const struct param *p)
63 {
64 cbor_item_t *argv[3], *array = NULL;
65 size_t cbor_alloc_len, cbor_len = 0;
66 unsigned char *cbor = NULL;
67
68 memset(argv, 0, sizeof(argv));
69
70 if ((array = cbor_new_definite_array(3)) == NULL ||
71 (argv[0] = pack_int(p->seed)) == NULL ||
72 (argv[1] = pack_int(p->dev)) == NULL ||
73 (argv[2] = pack_blob(&p->wiredata)) == NULL)
74 goto fail;
75
76 for (size_t i = 0; i < 3; i++)
77 if (cbor_array_push(array, argv[i]) == false)
78 goto fail;
79
80 if ((cbor_len = cbor_serialize_alloc(array, &cbor,
81 &cbor_alloc_len)) == 0 || cbor_len > len) {
82 cbor_len = 0;
83 goto fail;
84 }
85
86 memcpy(ptr, cbor, cbor_len);
87 fail:
88 for (size_t i = 0; i < 3; i++)
89 if (argv[i])
90 cbor_decref(&argv[i]);
91
92 if (array)
93 cbor_decref(&array);
94
95 free(cbor);
96
97 return cbor_len;
98 }
99
100 size_t
101 pack_dummy(uint8_t *ptr, size_t len)
102 {
103 struct param dummy;
104 uint8_t blob[MAXCORPUS];
105 size_t blob_len;
106
107 memset(&dummy, 0, sizeof(dummy));
108
109 dummy.wiredata.len = sizeof(dummy_netlink_wiredata);
110 memcpy(&dummy.wiredata.body, &dummy_netlink_wiredata,
111 dummy.wiredata.len);
112
113 assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
114
115 if (blob_len > len) {
116 memcpy(ptr, blob, len);
117 return len;
118 }
119
120 memcpy(ptr, blob, blob_len);
121
122 return blob_len;
123 }
124
125 void
126 test(const struct param *p)
127 {
128 fido_nl_t *nl;
129 uint32_t target;
130
131 prng_init((unsigned int)p->seed);
132 fuzz_clock_reset();
133 fido_init(FIDO_DEBUG);
134 fido_set_log_handler(consume_str);
135
136 set_netlink_io_functions(fd_read, fd_write);
137 set_wire_data(p->wiredata.body, p->wiredata.len);
138
139 if ((nl = fido_nl_new()) == NULL)
140 return;
141
142 consume(&nl->fd, sizeof(nl->fd));
143 consume(&nl->nfc_type, sizeof(nl->nfc_type));
144 consume(&nl->nfc_mcastgrp, sizeof(nl->nfc_mcastgrp));
145 consume(&nl->saddr, sizeof(nl->saddr));
146
147 fido_nl_power_nfc(nl, (uint32_t)p->dev);
148
149 if (fido_nl_get_nfc_target(nl, (uint32_t)p->dev, &target) == 0)
150 consume(&target, sizeof(target));
151
152 fido_nl_free(&nl);
153 }
154
155 void
156 mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
157 {
158 if (flags & MUTATE_SEED)
159 p->seed = (int)seed;
160
161 if (flags & MUTATE_PARAM)
162 mutate_int(&p->dev);
163
164 if (flags & MUTATE_WIREDATA)
165 mutate_blob(&p->wiredata);
166 }