]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-dns-packet.c
Merge pull request #7913 from sourcejedi/devpts
[thirdparty/systemd.git] / src / fuzz / fuzz-dns-packet.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2018 Jonathan Rudenberg
4
5 systemd is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 systemd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with systemd; If not, see <http://www.gnu.org/licenses/>.
17 ***/
18
19 #include "fuzz.h"
20 #include "resolved-dns-packet.h"
21
22 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
24 int r;
25
26 if (size > DNS_PACKET_SIZE_MAX)
27 return 0;
28
29 r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX);
30 if (r < 0)
31 return 0;
32 p->size = 0; /* by default append starts after the header, undo that */
33 dns_packet_append_blob(p, data, size, NULL);
34 if (size < DNS_PACKET_HEADER_SIZE) {
35 /* make sure we pad the packet back up to the minimum header size */
36 assert(p->allocated >= DNS_PACKET_HEADER_SIZE);
37 memzero(DNS_PACKET_DATA(p) + size, DNS_PACKET_HEADER_SIZE - size);
38 p->size = DNS_PACKET_HEADER_SIZE;
39 }
40 dns_packet_extract(p);
41
42 return 0;
43 }