Refactor code for new fuzzing netlink flag

Changed the configure flag used by netlink fuzzing
and refactored code accordingly.

Signed-off-by: Stephen Worley <sworley@cumulusnetworks.com>
This commit is contained in:
Stephen Worley 2018-07-24 13:39:25 -04:00
parent 001c591673
commit acfa8927f9
4 changed files with 41 additions and 34 deletions

View File

@ -436,6 +436,8 @@ AC_ARG_ENABLE(datacenter,
AS_HELP_STRING([--enable-datacenter], [enable Compilation for Data Center Extensions]))
AC_ARG_ENABLE(fuzzing,
AS_HELP_STRING([--enable-fuzzing], [enable ability to fuzz various parts of FRR]))
AC_ARG_ENABLE(netlink_fuzzing,
AS_HELP_STRING([--enable-netlink-fuzzing], [enable ability to fuzz netlink listening socket in zebra]))
AC_ARG_ENABLE(rr-semantics,
AS_HELP_STRING([--disable-rr-semantics], [disable the v6 Route Replace semantics]))
AC_ARG_ENABLE([protobuf],
@ -501,6 +503,10 @@ if test "${enable_fuzzing}" = "yes" ; then
AC_DEFINE(HANDLE_ZAPI_FUZZING,,Compile extensions to use with a fuzzer)
fi
if test "${enable_netlink_fuzzing}" = "yes" ; then
AC_DEFINE(HANDLE_NETLINK_FUZZING,,Compile extensions to use with a fuzzer for netlink)
fi
if test "${enable_cumulus}" = "yes" ; then
AC_DEFINE(HAVE_CUMULUS,,Compile Special Cumulus Code in)
fi

View File

@ -20,10 +20,10 @@
#include <zebra.h>
#if defined(HANDLE_ZAPI_FUZZING)
#if defined(HANDLE_NETLINK_FUZZING)
#include <stdio.h>
#include <string.h>
#endif
#endif /* HANDLE_NETLINK_FUZZING */
#ifdef HAVE_NETLINK
@ -298,7 +298,7 @@ static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
return 0;
}
#if defined(HANDLE_ZAPI_FUZZING)
#if defined(HANDLE_NETLINK_FUZZING)
/* Using globals here to avoid adding function parameters */
/* Keep distinct filenames for netlink fuzzy collection */
@ -308,17 +308,7 @@ static unsigned int netlink_file_counter = 1;
static char netlink_fuzz_file[MAXPATHLEN] = "";
/* Flag for whether to read from file or not */
int netlink_read;
/**
* netlink_set_read() - Sets the read flag
* @flag: Flag value.
*
*/
void set_netlink_read(int flag)
{
netlink_read = flag;
}
bool netlink_read = false;
/**
* netlink_read_init() - Starts the message parser
@ -388,7 +378,7 @@ static long netlink_read_file(char *buf, const char *fname)
return file_bytes;
}
#endif
#endif /* HANDLE_NETLINK_FUZZING */
static int kernel_read(struct thread *thread)
{
@ -699,7 +689,7 @@ int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
if (count && read_in >= count)
return 0;
#if defined(HANDLE_ZAPI_FUZZING)
#if defined(HANDLE_NETLINK_FUZZING)
/* Check if reading and filename is set */
if (netlink_read && '\0' != netlink_fuzz_file[0]) {
zlog_debug("Reading netlink fuzz file");
@ -710,7 +700,7 @@ int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
}
#else
status = recvmsg(nl->sock, &msg, 0);
#endif
#endif /* HANDLE_NETLINK_FUZZING */
if (status < 0) {
if (errno == EINTR)
continue;
@ -744,13 +734,13 @@ int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
zlog_hexdump(buf, status);
}
#if defined(HANDLE_ZAPI_FUZZING)
#if defined(HANDLE_NETLINK_FUZZING)
if (!netlink_read) {
zlog_debug("Writing incoming netlink message");
netlink_write_incoming(buf, status,
netlink_file_counter++);
}
#endif
#endif /* HANDLE_NETLINK_FUZZING */
read_in++;
for (h = (struct nlmsghdr *)buf;

View File

@ -45,10 +45,10 @@ extern const char *nl_rtproto_to_str(uint8_t rtproto);
extern const char *nl_family_to_str(uint8_t family);
extern const char *nl_rttype_to_str(uint8_t rttype);
#if defined(HANDLE_ZAPI_FUZZING)
extern void set_netlink_read(int flag);
#if defined(HANDLE_NETLINK_FUZZING)
extern bool netlink_read;
extern void netlink_read_init(const char *fname);
#endif
#endif /* HANDLE_NETLINK_FUZZING */
extern int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
struct nlsock *nl, struct zebra_ns *zns,
int count, int startup);

View File

@ -54,9 +54,9 @@
#include "zebra/zebra_rnh.h"
#include "zebra/zebra_pbr.h"
#if defined(HANDLE_ZAPI_FUZZING)
#if defined(HANDLE_NETLINK_FUZZING)
#include "zebra/kernel_netlink.h"
#endif
#endif /* HANDLE_NETLINK_FUZZING */
#define ZEBRA_PTM_SUPPORT
@ -218,8 +218,10 @@ int main(int argc, char **argv)
socklen_t dummylen;
#if defined(HANDLE_ZAPI_FUZZING)
char *zapi_fuzzing = NULL;
#endif /* HANDLE_ZAPI_FUZZING */
#if defined(HANDLE_NETLINK_FUZZING)
char *netlink_fuzzing = NULL;
#endif
#endif /* HANDLE_NETLINK_FUZZING */
vrf_configure_backend(VRF_BACKEND_VRF_LITE);
logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
@ -232,8 +234,11 @@ int main(int argc, char **argv)
"s:n"
#endif
#if defined(HANDLE_ZAPI_FUZZING)
"c:w:"
#endif
"c:"
#endif /* HANDLE_ZAPI_FUZZING */
#if defined(HANDLE_NETLINK_FUZZING)
"w:"
#endif /* HANDLE_NETLINK_FUZZING */
,
longopts,
" -b, --batch Runs in batch mode\n"
@ -250,8 +255,10 @@ int main(int argc, char **argv)
#endif /* HAVE_NETLINK */
#if defined(HANDLE_ZAPI_FUZZING)
" -c <file> Bypass normal startup and use this file for testing of zapi\n"
" -w <file> Bypass normal startup and use this file for testing of netlink input"
#endif
#endif /* HANDLE_ZAPI_FUZZING */
#if defined(HANDLE_NETLINK_FUZZING)
" -w <file> Bypass normal startup and use this file for testing of netlink input\n"
#endif /* HANDLE_NETLINK_FUZZING */
);
while (1) {
@ -313,17 +320,18 @@ int main(int argc, char **argv)
#if defined(HANDLE_ZAPI_FUZZING)
case 'c':
zapi_fuzzing = optarg;
set_netlink_read(1);
break;
#endif /* HANDLE_ZAPI_FUZZING */
#if defined(HANDLE_NETLINK_FUZZING)
case 'w':
netlink_fuzzing = optarg;
/* This ensures we are aren't writing any of the
* startup netlink messages that happen when we
* just want to read.
*/
set_netlink_read(1);
netlink_read = true;
break;
#endif
#endif /* HANDLE_NETLINK_FUZZING */
default:
frr_help_exit(1);
break;
@ -403,11 +411,14 @@ int main(int argc, char **argv)
if (zapi_fuzzing) {
zserv_read_file(zapi_fuzzing);
exit(0);
} else if (netlink_fuzzing) {
}
#endif /* HANDLE_ZAPI_FUZZING */
#if defined(HANDLE_NETLINK_FUZZING)
if (netlink_fuzzing) {
netlink_read_init(netlink_fuzzing);
exit(0);
}
#endif
#endif /* HANDLE_NETLINK_FUZZING */
frr_run(zebrad.master);