From 98c5f9884e8385137c6d3a69a2d7cc4bc94672b9 Mon Sep 17 00:00:00 2001 From: Kristof Provost Date: Fri, 6 Apr 2018 15:03:48 +0000 Subject: [PATCH] pf tests: Basic ioctl validation tests Validate the DIOCRADDTABLES and DIOCRDELTABLES ioctls with invalid size values. All of these requests should fail. MFC after: 1 week --- etc/mtree/BSD.tests.dist | 2 + tests/sys/netpfil/pf/Makefile | 1 + tests/sys/netpfil/pf/ioctl/Makefile | 10 ++ tests/sys/netpfil/pf/ioctl/validation.c | 130 ++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 tests/sys/netpfil/pf/ioctl/Makefile create mode 100644 tests/sys/netpfil/pf/ioctl/validation.c diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index bc9305344554..0882badf5406 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -742,6 +742,8 @@ .. netpfil pf + ioctl + .. .. .. opencrypto diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile index 259e1275d9c6..c055e6840bd8 100644 --- a/tests/sys/netpfil/pf/Makefile +++ b/tests/sys/netpfil/pf/Makefile @@ -3,6 +3,7 @@ PACKAGE= tests TESTSDIR= ${TESTSBASE}/sys/netpfil/pf +TESTS_SUBDIRS+= ioctl ATF_TESTS_SH+= pass_block \ forward \ diff --git a/tests/sys/netpfil/pf/ioctl/Makefile b/tests/sys/netpfil/pf/ioctl/Makefile new file mode 100644 index 000000000000..c2612ac7082e --- /dev/null +++ b/tests/sys/netpfil/pf/ioctl/Makefile @@ -0,0 +1,10 @@ +# $FreeBSD$ + +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/sys/netpfil/pf/ioctl + +ATF_TESTS_C += \ + validation + +.include diff --git a/tests/sys/netpfil/pf/ioctl/validation.c b/tests/sys/netpfil/pf/ioctl/validation.c new file mode 100644 index 000000000000..4ca8308fc881 --- /dev/null +++ b/tests/sys/netpfil/pf/ioctl/validation.c @@ -0,0 +1,130 @@ +/*- + * Copyright (c) 2018 Kristof Provost + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include + +static int dev; + +#define COMMON_HEAD() \ + if (modfind("pf") == -1) \ + atf_tc_skip("pf not loaded"); \ + dev = open("/dev/pf", O_RDWR); \ + if (dev == -1) \ + atf_tc_skip("Failed to open /dev/pf"); + +#define COMMON_CLEANUP() \ + close(dev); + +ATF_TC_WITHOUT_HEAD(addtables); +ATF_TC_BODY(addtables, tc) +{ + struct pfioc_table io; + struct pfr_table tbl; + int flags; + + COMMON_HEAD(); + + flags = 0; + + bzero(&io, sizeof(io)); + io.pfrio_flags = flags; + io.pfrio_buffer = &tbl; + io.pfrio_esize = sizeof(tbl); + + /* Negative size */ + io.pfrio_size = -1; + if (ioctl(dev, DIOCRADDTABLES, &io) == 0) + atf_tc_fail("Request with size -1 succeeded"); + + /* Overly large size */ + io.pfrio_size = 1 << 24; + if (ioctl(dev, DIOCRADDTABLES, &io) == 0) + atf_tc_fail("Request with size 1 << 24 succeeded"); + + /* NULL buffer */ + io.pfrio_size = 1; + io.pfrio_buffer = NULL; + if (ioctl(dev, DIOCRADDTABLES, &io) == 0) + atf_tc_fail("Request with NULL buffer succeeded"); + + COMMON_CLEANUP(); +} + +ATF_TC_WITHOUT_HEAD(deltables); +ATF_TC_BODY(deltables, tc) +{ + struct pfioc_table io; + struct pfr_table tbl; + int flags; + + COMMON_HEAD(); + + flags = 0; + + bzero(&io, sizeof(io)); + io.pfrio_flags = flags; + io.pfrio_buffer = &tbl; + io.pfrio_esize = sizeof(tbl); + + /* Negative size */ + io.pfrio_size = -1; + if (ioctl(dev, DIOCRDELTABLES, &io) == 0) + atf_tc_fail("Request with size -1 succeeded"); + + /* Overly large size */ + io.pfrio_size = 1 << 24; + if (ioctl(dev, DIOCRDELTABLES, &io) == 0) + atf_tc_fail("Request with size 1 << 24 succeeded"); + + /* NULL buffer */ + io.pfrio_size = 1; + io.pfrio_buffer = NULL; + if (ioctl(dev, DIOCRDELTABLES, &io) == 0) + atf_tc_fail("Request with NULL buffer succeeded"); + + COMMON_CLEANUP(); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, addtables); + ATF_TP_ADD_TC(tp, deltables); + + return (atf_no_error()); +}