1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/fuzzing/fuzzing.c
Sören Tempel 9e72f717e0 sys/fuzzing: Initialize
This adds a utility module which is used to write applications for
fuzzing RIOT network modules. The module provides a dummy network
interface which is configured with a static IPv6 addresses for modules
which perform operations on the underlying network interface. Besides,
it contains a utility function for transforming data received on
standard input into a `gnrc_pktsnip_t`.
2020-04-07 17:48:39 +02:00

90 lines
1.9 KiB
C

/*
* Copyright (C) 2019 Sören Tempel <tempel@uni-bremen.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include "assert.h"
#include "fuzzing.h"
#include "net/ipv6/addr.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/pktbuf.h"
#include "net/gnrc/pkt.h"
extern int fuzzing_netdev(gnrc_netif_t *);
extern void fuzzing_netdev_wait(void);
/* used by gnrc_pktbuf_malloc to exit on free */
gnrc_pktsnip_t *gnrc_pktbuf_fuzzptr = NULL;
/* buffer sizes for reading from an fd */
#define FUZZING_BSIZE 1024
#define FUZZING_BSTEP 128
int
fuzzing_init(ipv6_addr_t *addr, unsigned pfx_len)
{
static gnrc_netif_t netif;
if (fuzzing_netdev(&netif)) {
return -ENXIO;
}
fuzzing_netdev_wait();
if (addr != NULL) {
if (gnrc_netif_ipv6_addr_add(&netif, addr, pfx_len, 0) != sizeof(ipv6_addr_t)) {
return -EINVAL;
}
}
return 0;
}
int
fuzzing_read_packet(int fd, gnrc_pktsnip_t *pkt)
{
ssize_t r;
size_t csiz, rsiz;
/* can only be called once currently */
assert(gnrc_pktbuf_fuzzptr == NULL);
csiz = 0;
rsiz = FUZZING_BSIZE;
if (gnrc_pktbuf_realloc_data(pkt, rsiz)) {
return -ENOMEM;
}
while ((r = read(fd, &((char *)pkt->data)[csiz], rsiz)) > 0) {
assert((size_t)r <= rsiz);
csiz += r;
rsiz -= r;
if (rsiz == 0) {
if (gnrc_pktbuf_realloc_data(pkt, csiz + FUZZING_BSTEP)) {
return -ENOMEM;
}
rsiz += FUZZING_BSTEP;
}
}
if (r == -1) {
return -errno;
}
/* shrink packet to actual size */
if (gnrc_pktbuf_realloc_data(pkt, csiz)) {
return -ENOMEM;
}
gnrc_pktbuf_fuzzptr = pkt;
return 0;
}