Skip to content

Commit 82f44c5

Browse files
committed
fuzzing: Add uri_parser fuzzer setup
1 parent 5c51686 commit 82f44c5

File tree

9 files changed

+52
-32
lines changed

9 files changed

+52
-32
lines changed

fuzzing/uri_parser/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include ../Makefile.fuzzing_common
2+
3+
USEMODULE += uri_parser
4+
5+
include $(RIOTBASE)/Makefile.include

fuzzing/uri_parser/input/input0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coap:///R@[2008::1]:5own//R@[2008::1]:5own/?v=1

fuzzing/uri_parser/input/input1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coap://user@[2001:db8::1]:12345

fuzzing/uri_parser/input/input2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ftp://riot-os.org:99/bar/foo

fuzzing/uri_parser/input/input3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://riot-os.org:99/bar/foo

fuzzing/uri_parser/input/input4.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coap://user@[2001:db8::1%eth0]:12345

fuzzing/uri_parser/main.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2022 HAW Hamburg
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
#include <err.h>
10+
#include <unistd.h>
11+
12+
#include "uri_parser.h"
13+
#include "fuzzing.h"
14+
15+
int main(void)
16+
{
17+
size_t input_len;
18+
char *input_buf = (char *)fuzzing_read_bytes(STDIN_FILENO, &input_len);
19+
20+
if (input_buf == NULL) {
21+
errx(EXIT_FAILURE, "fuzzing_read_bytes failed");
22+
}
23+
24+
uri_parser_result_t uri_res;
25+
26+
uri_parser_process(&uri_res, input_buf, input_len);
27+
28+
exit(EXIT_SUCCESS);
29+
return EXIT_SUCCESS;
30+
}

sys/fuzzing/fuzzing.c

+11-25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
extern int fuzzing_netdev(gnrc_netif_t *);
2424
extern void fuzzing_netdev_wait(void);
2525

26+
/* buffer sizes for reading from an fd */
27+
#define FUZZING_BSIZE 1024
28+
#define FUZZING_BSTEP 128
29+
2630
/* used by gnrc_pktbuf_malloc to exit on free */
2731
gnrc_pktsnip_t *gnrc_pktbuf_fuzzptr = NULL;
2832

@@ -48,40 +52,22 @@ fuzzing_init(ipv6_addr_t *addr, unsigned pfx_len)
4852
int
4953
fuzzing_read_packet(int fd, gnrc_pktsnip_t *pkt)
5054
{
51-
ssize_t r;
52-
size_t csiz, rsiz;
55+
size_t rsiz;
5356

5457
/* can only be called once currently */
5558
assert(gnrc_pktbuf_fuzzptr == NULL);
5659

57-
csiz = 0;
58-
rsiz = FUZZING_BSIZE;
59-
if (gnrc_pktbuf_realloc_data(pkt, rsiz)) {
60-
return -ENOMEM;
61-
}
62-
63-
while ((r = read(fd, &((char *)pkt->data)[csiz], rsiz)) > 0) {
64-
assert((size_t)r <= rsiz);
65-
66-
csiz += r;
67-
rsiz -= r;
68-
69-
if (rsiz == 0) {
70-
if (gnrc_pktbuf_realloc_data(pkt, csiz + FUZZING_BSTEP)) {
71-
return -ENOMEM;
72-
}
73-
rsiz += FUZZING_BSTEP;
74-
}
75-
}
76-
if (r == -1) {
60+
uint8_t *input = fuzzing_read_bytes(fd, &rsiz);
61+
if (input == NULL) {
7762
return -errno;
7863
}
7964

80-
/* shrink packet to actual size */
81-
if (gnrc_pktbuf_realloc_data(pkt, csiz)) {
65+
if (gnrc_pktbuf_realloc_data(pkt, rsiz)) {
8266
return -ENOMEM;
8367
}
8468

69+
memcpy(pkt->data, input, rsiz);
70+
8571
gnrc_pktbuf_fuzzptr = pkt;
8672
return 0;
8773
}
@@ -116,7 +102,7 @@ fuzzing_read_bytes(int fd, size_t *size)
116102
return NULL;
117103
}
118104

119-
/* shrink packet to actual size */
105+
/* shrink buffer to actual size */
120106
if ((buffer = realloc(buffer, csiz)) == NULL) {
121107
return NULL;
122108
}

sys/include/fuzzing.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@
2626
extern "C" {
2727
#endif
2828

29-
3029
#include <stdint.h>
3130

3231
#include "net/ipv6/addr.h"
3332
#include "net/gnrc/pkt.h"
3433

35-
36-
/* buffer sizes for reading from an fd */
37-
#define FUZZING_BSIZE 1024
38-
#define FUZZING_BSTEP 128
39-
4034
/**
4135
* @brief Initialize dummy network interface with given address.
4236
*
@@ -63,7 +57,7 @@ int fuzzing_read_packet(int fd, gnrc_pktsnip_t *pkt);
6357
*
6458
* @param fd File descriptor to read data from.
6559
* @param size Byte count of the data read.
66-
*
60+
*
6761
* @return pointer to the data on success, NULL otherwise.
6862
*/
6963
uint8_t *fuzzing_read_bytes(int fd, size_t *size);

0 commit comments

Comments
 (0)