diff options
Diffstat (limited to 'contrib/netbsd-tests/net/bpf')
-rw-r--r-- | contrib/netbsd-tests/net/bpf/h_bpf.h | 171 | ||||
-rw-r--r-- | contrib/netbsd-tests/net/bpf/t_bpf.c | 177 | ||||
-rw-r--r-- | contrib/netbsd-tests/net/bpf/t_div-by-zero.c | 52 | ||||
-rw-r--r-- | contrib/netbsd-tests/net/bpf/t_mbuf.c | 961 |
4 files changed, 1361 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/net/bpf/h_bpf.h b/contrib/netbsd-tests/net/bpf/h_bpf.h new file mode 100644 index 0000000..307bc50 --- /dev/null +++ b/contrib/netbsd-tests/net/bpf/h_bpf.h @@ -0,0 +1,171 @@ +/* $NetBSD: h_bpf.h,v 1.2 2014/07/08 21:44:26 alnsn Exp $ */ + +/*- + * Copyright (c) 2014 Alexander Nasonov. + * All rights reserved. + * + * 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 ``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. + */ + +#ifndef _TESTS_NET_BPF_H_BPF_H_ +#define _TESTS_NET_BPF_H_BPF_H_ + +#include <sys/param.h> +#include <sys/mbuf.h> + +#include <net/bpf.h> +#include <net/bpfjit.h> + +#include <rump/rump.h> +#include <rump/rump_syscalls.h> + +#include <stdint.h> +#include <string.h> + +/* XXX These declarations don't look kosher. */ +int rumpns_bpf_validate(const struct bpf_insn *, int); +unsigned int rumpns_bpf_filter_ext(const bpf_ctx_t *, + const struct bpf_insn *, bpf_args_t *); +bpfjit_func_t rumpns_bpfjit_generate_code(const bpf_ctx_t *, + const struct bpf_insn *, size_t); +void rumpns_bpfjit_free_code(bpfjit_func_t); + +/* + * Init mbuf chain with one or two chunks. The first chunk holds + * [pkt, pkt + split] bytes, the second chunk (if it's not empty) + * holds (pkt + split, pkt + pktsize) bytes. + * The function returns (const uint8_t *)mb1. + */ +static inline const uint8_t * +init_mchain2(struct mbuf *mb1, struct mbuf *mb2, + unsigned char pkt[], size_t pktsize, size_t split) +{ + + (void)memset(mb1, 0, sizeof(*mb1)); + mb1->m_data = (char *)pkt; + mb1->m_next = (split < pktsize) ? mb2 : NULL; + mb1->m_len = (split < pktsize) ? split : pktsize; + + if (split < pktsize) { + (void)memset(mb2, 0, sizeof(*mb2)); + mb2->m_next = NULL; + mb2->m_data = (char *)&pkt[split]; + mb2->m_len = pktsize - split; + } + + return (const uint8_t*)mb1; +} + +/* + * Compile and run a filter program. + */ +static inline unsigned int +exec_prog(struct bpf_insn *insns, size_t insn_count, + unsigned char pkt[], size_t pktsize) +{ + bpfjit_func_t fn; + bpf_args_t args; + unsigned int res; + + args.pkt = (const uint8_t *)pkt; + args.buflen = pktsize; + args.wirelen = pktsize; + + rump_schedule(); + fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count); + rump_unschedule(); + + res = fn(NULL, &args); + + rump_schedule(); + rumpns_bpfjit_free_code(fn); + rump_unschedule(); + + return res; +} + +/* + * Interpret a filter program with mbuf chain passed to bpf_filter_ext(). + */ +static inline unsigned int +interp_prog_mchain2(struct bpf_insn *insns, + unsigned char pkt[], size_t pktsize, size_t split) +{ + uint32_t mem[BPF_MEMWORDS]; + struct mbuf mb1, mb2; + bpf_args_t args; + unsigned int res; + + args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split); + args.buflen = 0; + args.wirelen = pktsize; + args.mem = mem; + + rump_schedule(); + res = rumpns_bpf_filter_ext(NULL, insns, &args); + rump_unschedule(); + + return res; +} + +/* + * Compile and run a filter program with mbuf chain passed to compiled function. + */ +static inline unsigned int +exec_prog_mchain2(struct bpf_insn *insns, size_t insn_count, + unsigned char pkt[], size_t pktsize, size_t split) +{ + bpfjit_func_t fn; + struct mbuf mb1, mb2; + bpf_args_t args; + unsigned int res; + + args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split); + args.buflen = 0; + args.wirelen = pktsize; + + rump_schedule(); + fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count); + rump_unschedule(); + + res = fn(NULL, &args); + + rump_schedule(); + rumpns_bpfjit_free_code(fn); + rump_unschedule(); + + return res; +} + +static inline bool +prog_validate(struct bpf_insn *insns, size_t insn_count) +{ + bool res; + + rump_schedule(); + res = rumpns_bpf_validate(insns, insn_count); + rump_unschedule(); + + return res; +} + +#endif /* _TESTS_NET_BPF_H_BPF_H_ */ diff --git a/contrib/netbsd-tests/net/bpf/t_bpf.c b/contrib/netbsd-tests/net/bpf/t_bpf.c new file mode 100644 index 0000000..95ca2fc --- /dev/null +++ b/contrib/netbsd-tests/net/bpf/t_bpf.c @@ -0,0 +1,177 @@ +/* $NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $ */ + +/*- + * Copyright (c) 2010 Antti Kantee. All Rights Reserved. + * + * 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 ``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. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $"); + +#include <sys/param.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/mbuf.h> +#include <sys/sysctl.h> +#include <sys/mman.h> +#include <unistd.h> + +#include <net/if.h> +#include <net/bpf.h> + +#include <fcntl.h> +#include <stdio.h> +#include <string.h> + +#include <rump/rump.h> +#include <rump/rump_syscalls.h> + +/* XXX: atf-c.h has collisions with mbuf */ +#undef m_type +#undef m_data +#include <atf-c.h> + +#include "../../h_macros.h" +#include "../config/netconfig.c" + +ATF_TC(bpfwriteleak); +ATF_TC_HEAD(bpfwriteleak, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Checks that writing to /dev/bpf " + "does not leak mbufs"); +} + +static int +getmtdata(void) +{ + struct mbstat mbstat; + size_t mbstatlen = sizeof(mbstat); + const int mbstat_mib[] = { CTL_KERN, KERN_MBUF, MBUF_STATS }; + + RL(rump_sys___sysctl(mbstat_mib, __arraycount(mbstat_mib), + &mbstat, &mbstatlen, NULL, 0)); + return mbstat.m_mtypes[MT_DATA]; +} + +ATF_TC_BODY(bpfwriteleak, tc) +{ + char buf[28]; /* sizeof(garbage) > etherhdrlen */ + struct ifreq ifr; + int ifnum, bpfd; + + RZ(rump_init()); + RZ(rump_pub_shmif_create(NULL, &ifnum)); + sprintf(ifr.ifr_name, "shmif%d", ifnum); + + RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR)); + RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr)); + RL(rump_sys_ioctl(bpfd, BIOCSFEEDBACK, &ifr)); + + if (getmtdata() != 0) + atf_tc_fail("test precondition failed: MT_DATA mbufs != 0"); + + ATF_REQUIRE_ERRNO(ENETDOWN, rump_sys_write(bpfd, buf, sizeof(buf))==-1); + + ATF_REQUIRE_EQ(getmtdata(), 0); +} + +#if (SIZE_MAX > UINT_MAX) +ATF_TC(bpfwritetrunc); +ATF_TC_HEAD(bpfwritetrunc, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks that write to /dev/bpf " + "does not truncate size_t to int"); +} + +ATF_TC_BODY(bpfwritetrunc, tc) +{ + int bpfd; + struct ifreq ifr; + struct iovec *iov; + size_t iovlen, sz; + const size_t extra_bytes = 28; + const size_t total = extra_bytes + UINT_MAX + 1; + long iov_max, vm_page_size; /* round_page wants vm_page_size variable */ + + memset(&ifr, 0, sizeof(ifr)); + + iov_max = sysconf(_SC_IOV_MAX); + vm_page_size = sysconf(_SC_PAGE_SIZE); + ATF_REQUIRE(iov_max > 1 && vm_page_size > 1); + + /* + * Minimize memory consumption by using many iovecs + * all pointing to one memory region. + */ + iov = calloc(iov_max, sizeof(struct iovec)); + ATF_REQUIRE(iov != NULL); + + sz = round_page((total + (iov_max - 1)) / iov_max); + + iov[0].iov_len = sz; + iov[0].iov_base = mmap(NULL, sz, PROT_READ, MAP_ANON, -1, 0); + ATF_REQUIRE(iov[0].iov_base != MAP_FAILED); + + iovlen = 1; + while (sz + iov[0].iov_len <= total) + { + iov[iovlen].iov_len = iov[0].iov_len; + iov[iovlen].iov_base = iov[0].iov_base; + sz += iov[0].iov_len; + iovlen++; + } + + if (sz < total) + { + iov[iovlen].iov_len = total - sz; + iov[iovlen].iov_base = iov[0].iov_base; + iovlen++; + } + + /* Sanity checks */ + ATF_REQUIRE(iovlen >= 1 && iovlen <= (size_t)iov_max); + ATF_REQUIRE_EQ(iov[iovlen-1].iov_len, total % iov[0].iov_len); + + RZ(rump_init()); + netcfg_rump_makeshmif("bpfwritetrunc", ifr.ifr_name); + netcfg_rump_if(ifr.ifr_name, "10.1.1.1", "255.0.0.0"); + + RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR)); + RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr)); + + ATF_CHECK_ERRNO(EMSGSIZE, rump_sys_writev(bpfd, iov, iovlen) == -1); + + munmap(iov[0].iov_base, iov[0].iov_len); + free(iov); +} +#endif /* #if (SIZE_MAX > UINT_MAX) */ + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, bpfwriteleak); +#if (SIZE_MAX > UINT_MAX) + ATF_TP_ADD_TC(tp, bpfwritetrunc); +#endif + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/net/bpf/t_div-by-zero.c b/contrib/netbsd-tests/net/bpf/t_div-by-zero.c new file mode 100644 index 0000000..542d08a --- /dev/null +++ b/contrib/netbsd-tests/net/bpf/t_div-by-zero.c @@ -0,0 +1,52 @@ +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <net/bpf.h> + +#include <atf-c.h> +#include <fcntl.h> + +#include <rump/rump.h> +#include <rump/rump_syscalls.h> + +ATF_TC(div_by_zero); +ATF_TC_HEAD(div_by_zero, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF rejects a filter " + "which divides by 0"); +} + +ATF_TC_BODY(div_by_zero, tc) +{ + struct bpf_program bp; + int fd; + + /* + * Source code for following program: + * link[0:4]/0 = 2 + */ + struct bpf_insn bins[] = { + { 0x20, 0, 0, 0x00000000 }, + { 0x34, 0, 0, 0x00000000 }, + { 0x15, 0, 1, 0x00000002 }, + { 0x6, 0, 0, 0x00000060 }, + { 0x6, 0, 0, 0x00000000 }, + }; + + bp.bf_len = __arraycount(bins); + bp.bf_insns = bins; + + rump_init(); + fd = rump_sys_open("/dev/bpf", O_RDWR); + ATF_CHECK(fd != -1); + ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(fd, BIOCSETF, &bp), -1, + "bpf accepted program with division by zero"); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, div_by_zero); + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/net/bpf/t_mbuf.c b/contrib/netbsd-tests/net/bpf/t_mbuf.c new file mode 100644 index 0000000..965dd02 --- /dev/null +++ b/contrib/netbsd-tests/net/bpf/t_mbuf.c @@ -0,0 +1,961 @@ +/* $NetBSD: t_mbuf.c,v 1.2 2014/07/08 21:44:26 alnsn Exp $ */ + +/*- + * Copyright (c) 2014 Alexander Nasonov. + * All rights reserved. + * + * 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 ``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. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_mbuf.c,v 1.2 2014/07/08 21:44:26 alnsn Exp $"); + +#include <sys/param.h> +#include <sys/mbuf.h> + +#include <net/bpf.h> + +#include <stdint.h> +#include <string.h> + +#include <rump/rump.h> +#include <rump/rump_syscalls.h> + +#include "../../net/bpf/h_bpf.h" + +/* XXX: atf-c.h has collisions with mbuf */ +#undef m_type +#undef m_data +#include <atf-c.h> + +#include "../../h_macros.h" + +static bool +test_ldb_abs(size_t split) +{ + /* Return a product of all packet bytes. */ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 0), /* A <- P[0] */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 1), /* A <- P[1] */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 2), /* A <- P[2] */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* A <- P[3] */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 4), /* A <- P[4] */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 120; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldh_abs(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 0), /* A <- P[0:2] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 1), /* A <- P[1:2] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 2), /* A <- P[2:2] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 3), /* A <- P[3:2] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 0x0a0e; /* 10 14 */ + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldw_abs(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 0), /* A <- P[0:4] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- A */ + + BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 1), /* A <- P[1:4] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 0x03050709; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldb_ind(size_t split) +{ + /* Return a sum of all packet bytes. */ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0), /* A <- P[0+X] */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1), /* A <- P[1+X] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1), /* A <- P[1+X] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 2), /* A <- P[2+X] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 3), /* A <- P[3+X] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 15; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldw_ind(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 0), /* A <- P[X+0:4] */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 0), /* A <- P[X+0:4] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0), /* X <- 0 */ + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 1), /* A <- P[X+1:4] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 0x05080b0e; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldh_ind(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 0), /* A <- P[X+0:2] */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 1), /* A <- P[X+1:2] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 1), /* A <- P[X+1:2] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_ST, 0), /* M[0] <- A */ + + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1 */ + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 2), /* A <- P[X+2:2] */ + BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0] */ + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */ + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 0x0a0e; /* 10 14 */ + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_msh(size_t split) +{ + /* Return a product of all packet bytes. */ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 1), /* A <- 1 */ + + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 0), /* X <- 4*(P[0]&0xf) */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4), /* A <- A / 4 */ + + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 1), /* X <- 4*(P[1]&0xf) */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4), /* A <- A / 4 */ + + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 2), /* X <- 4*(P[2]&0xf) */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4), /* A <- A / 4 */ + + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 3), /* X <- 4*(P[3]&0xf) */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4), /* A <- A / 4 */ + + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 4), /* X <- 4*(P[4]&0xf) */ + BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */ + BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4), /* A <- A / 4 */ + + BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + const unsigned int res = 120; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == res; +} + +static bool +test_ldb_abs_overflow(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldh_abs_overflow(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 4), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldw_abs_overflow(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 2), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldb_ind_overflow1(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 5), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldb_ind_overflow2(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4), + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldb_ind_overflow3(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_MAX), + BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldh_ind_overflow1(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 4), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldh_ind_overflow2(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3), + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldh_ind_overflow3(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_MAX), + BPF_STMT(BPF_LD+BPF_H+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldw_ind_overflow1(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 2), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldw_ind_overflow2(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_ldw_ind_overflow3(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_MAX), + BPF_STMT(BPF_LD+BPF_W+BPF_IND, 1), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +static bool +test_msh_overflow(size_t split) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 5), + BPF_STMT(BPF_MISC+BPF_TXA, 0), + BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0), + }; + + static unsigned char P[] = { 1, 2, 3, 4, 5 }; + + if (!prog_validate(insns, sizeof(insns) / sizeof(insns[0]))) + return false; + + return interp_prog_mchain2(insns, P, sizeof(P), split) == 0; +} + +ATF_TC(bpf_mbuf_ldb_abs); +ATF_TC_HEAD(bpf_mbuf_ldb_abs, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_ABS " + "loads bytes from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_abs, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_abs(0)); + ATF_CHECK(test_ldb_abs(1)); + ATF_CHECK(test_ldb_abs(2)); + ATF_CHECK(test_ldb_abs(3)); + ATF_CHECK(test_ldb_abs(4)); + ATF_CHECK(test_ldb_abs(5)); +} + +ATF_TC(bpf_mbuf_ldh_abs); +ATF_TC_HEAD(bpf_mbuf_ldh_abs, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_ABS " + "loads halfwords from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_abs, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_abs(0)); + ATF_CHECK(test_ldh_abs(1)); + ATF_CHECK(test_ldh_abs(2)); + ATF_CHECK(test_ldh_abs(3)); + ATF_CHECK(test_ldh_abs(4)); + ATF_CHECK(test_ldh_abs(5)); +} + +ATF_TC(bpf_mbuf_ldw_abs); +ATF_TC_HEAD(bpf_mbuf_ldw_abs, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_ABS " + "loads words from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_abs, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_abs(0)); + ATF_CHECK(test_ldw_abs(1)); + ATF_CHECK(test_ldw_abs(2)); + ATF_CHECK(test_ldw_abs(3)); + ATF_CHECK(test_ldw_abs(4)); + ATF_CHECK(test_ldw_abs(5)); +} + +ATF_TC(bpf_mbuf_ldb_ind); +ATF_TC_HEAD(bpf_mbuf_ldb_ind, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_IND " + "loads bytes from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_ind, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_ind(0)); + ATF_CHECK(test_ldb_ind(1)); + ATF_CHECK(test_ldb_ind(2)); + ATF_CHECK(test_ldb_ind(3)); + ATF_CHECK(test_ldb_ind(4)); + ATF_CHECK(test_ldb_ind(5)); +} + +ATF_TC(bpf_mbuf_ldh_ind); +ATF_TC_HEAD(bpf_mbuf_ldh_ind, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_IND " + "loads halfwords from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_ind, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_ind(0)); + ATF_CHECK(test_ldh_ind(1)); + ATF_CHECK(test_ldh_ind(2)); + ATF_CHECK(test_ldh_ind(3)); + ATF_CHECK(test_ldh_ind(4)); + ATF_CHECK(test_ldh_ind(5)); +} + +ATF_TC(bpf_mbuf_ldw_ind); +ATF_TC_HEAD(bpf_mbuf_ldw_ind, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_IND " + "loads words from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_ind, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_ind(0)); + ATF_CHECK(test_ldw_ind(1)); + ATF_CHECK(test_ldw_ind(2)); + ATF_CHECK(test_ldw_ind(3)); + ATF_CHECK(test_ldw_ind(4)); + ATF_CHECK(test_ldw_ind(5)); +} + +ATF_TC(bpf_mbuf_msh); +ATF_TC_HEAD(bpf_mbuf_msh, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LDX+BPF_B+BPF_MSH " + "loads bytes from mbuf correctly"); +} + +ATF_TC_BODY(bpf_mbuf_msh, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_msh(0)); + ATF_CHECK(test_msh(1)); + ATF_CHECK(test_msh(2)); + ATF_CHECK(test_msh(3)); + ATF_CHECK(test_msh(4)); + ATF_CHECK(test_msh(5)); +} + +ATF_TC(bpf_mbuf_ldb_abs_overflow); +ATF_TC_HEAD(bpf_mbuf_ldb_abs_overflow, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_ABS " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_abs_overflow, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_abs_overflow(0)); + ATF_CHECK(test_ldb_abs_overflow(1)); + ATF_CHECK(test_ldb_abs_overflow(2)); + ATF_CHECK(test_ldb_abs_overflow(3)); + ATF_CHECK(test_ldb_abs_overflow(4)); + ATF_CHECK(test_ldb_abs_overflow(5)); +} + +ATF_TC(bpf_mbuf_ldh_abs_overflow); +ATF_TC_HEAD(bpf_mbuf_ldh_abs_overflow, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_ABS " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_abs_overflow, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_abs_overflow(0)); + ATF_CHECK(test_ldh_abs_overflow(1)); + ATF_CHECK(test_ldh_abs_overflow(2)); + ATF_CHECK(test_ldh_abs_overflow(3)); + ATF_CHECK(test_ldh_abs_overflow(4)); + ATF_CHECK(test_ldh_abs_overflow(5)); +} + +ATF_TC(bpf_mbuf_ldw_abs_overflow); +ATF_TC_HEAD(bpf_mbuf_ldw_abs_overflow, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_ABS " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_abs_overflow, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_abs_overflow(0)); + ATF_CHECK(test_ldw_abs_overflow(1)); + ATF_CHECK(test_ldw_abs_overflow(2)); + ATF_CHECK(test_ldw_abs_overflow(3)); + ATF_CHECK(test_ldw_abs_overflow(4)); + ATF_CHECK(test_ldw_abs_overflow(5)); +} + +ATF_TC(bpf_mbuf_ldb_ind_overflow1); +ATF_TC_HEAD(bpf_mbuf_ldb_ind_overflow1, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_ind_overflow1, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_ind_overflow1(0)); + ATF_CHECK(test_ldb_ind_overflow1(1)); + ATF_CHECK(test_ldb_ind_overflow1(2)); + ATF_CHECK(test_ldb_ind_overflow1(3)); + ATF_CHECK(test_ldb_ind_overflow1(4)); + ATF_CHECK(test_ldb_ind_overflow1(5)); +} + +ATF_TC(bpf_mbuf_ldb_ind_overflow2); +ATF_TC_HEAD(bpf_mbuf_ldb_ind_overflow2, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_ind_overflow2, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_ind_overflow2(0)); + ATF_CHECK(test_ldb_ind_overflow2(1)); + ATF_CHECK(test_ldb_ind_overflow2(2)); + ATF_CHECK(test_ldb_ind_overflow2(3)); + ATF_CHECK(test_ldb_ind_overflow2(4)); + ATF_CHECK(test_ldb_ind_overflow2(5)); +} + +ATF_TC(bpf_mbuf_ldb_ind_overflow3); +ATF_TC_HEAD(bpf_mbuf_ldb_ind_overflow3, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_B+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldb_ind_overflow3, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldb_ind_overflow3(0)); + ATF_CHECK(test_ldb_ind_overflow3(1)); + ATF_CHECK(test_ldb_ind_overflow3(2)); + ATF_CHECK(test_ldb_ind_overflow3(3)); + ATF_CHECK(test_ldb_ind_overflow3(4)); + ATF_CHECK(test_ldb_ind_overflow3(5)); +} + +ATF_TC(bpf_mbuf_ldh_ind_overflow1); +ATF_TC_HEAD(bpf_mbuf_ldh_ind_overflow1, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_ind_overflow1, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_ind_overflow1(0)); + ATF_CHECK(test_ldh_ind_overflow1(1)); + ATF_CHECK(test_ldh_ind_overflow1(2)); + ATF_CHECK(test_ldh_ind_overflow1(3)); + ATF_CHECK(test_ldh_ind_overflow1(4)); + ATF_CHECK(test_ldh_ind_overflow1(5)); +} + +ATF_TC(bpf_mbuf_ldh_ind_overflow2); +ATF_TC_HEAD(bpf_mbuf_ldh_ind_overflow2, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_ind_overflow2, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_ind_overflow2(0)); + ATF_CHECK(test_ldh_ind_overflow2(1)); + ATF_CHECK(test_ldh_ind_overflow2(2)); + ATF_CHECK(test_ldh_ind_overflow2(3)); + ATF_CHECK(test_ldh_ind_overflow2(4)); + ATF_CHECK(test_ldh_ind_overflow2(5)); +} + +ATF_TC(bpf_mbuf_ldh_ind_overflow3); +ATF_TC_HEAD(bpf_mbuf_ldh_ind_overflow3, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_H+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldh_ind_overflow3, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldh_ind_overflow3(0)); + ATF_CHECK(test_ldh_ind_overflow3(1)); + ATF_CHECK(test_ldh_ind_overflow3(2)); + ATF_CHECK(test_ldh_ind_overflow3(3)); + ATF_CHECK(test_ldh_ind_overflow3(4)); + ATF_CHECK(test_ldh_ind_overflow3(5)); +} + +ATF_TC(bpf_mbuf_ldw_ind_overflow1); +ATF_TC_HEAD(bpf_mbuf_ldw_ind_overflow1, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_ind_overflow1, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_ind_overflow1(0)); + ATF_CHECK(test_ldw_ind_overflow1(1)); + ATF_CHECK(test_ldw_ind_overflow1(2)); + ATF_CHECK(test_ldw_ind_overflow1(3)); + ATF_CHECK(test_ldw_ind_overflow1(4)); + ATF_CHECK(test_ldw_ind_overflow1(5)); +} + +ATF_TC(bpf_mbuf_ldw_ind_overflow2); +ATF_TC_HEAD(bpf_mbuf_ldw_ind_overflow2, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_ind_overflow2, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_ind_overflow2(0)); + ATF_CHECK(test_ldw_ind_overflow2(1)); + ATF_CHECK(test_ldw_ind_overflow2(2)); + ATF_CHECK(test_ldw_ind_overflow2(3)); + ATF_CHECK(test_ldw_ind_overflow2(4)); + ATF_CHECK(test_ldw_ind_overflow2(5)); +} + +ATF_TC(bpf_mbuf_ldw_ind_overflow3); +ATF_TC_HEAD(bpf_mbuf_ldw_ind_overflow3, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LD+BPF_W+BPF_IND " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_ldw_ind_overflow3, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_ldw_ind_overflow3(0)); + ATF_CHECK(test_ldw_ind_overflow3(1)); + ATF_CHECK(test_ldw_ind_overflow3(2)); + ATF_CHECK(test_ldw_ind_overflow3(3)); + ATF_CHECK(test_ldw_ind_overflow3(4)); + ATF_CHECK(test_ldw_ind_overflow3(5)); +} + +ATF_TC(bpf_mbuf_msh_overflow); +ATF_TC_HEAD(bpf_mbuf_msh_overflow, tc) +{ + + atf_tc_set_md_var(tc, "descr", "Check that BPF_LDX+BPF_B+BPF_MSH " + "with out-of-bounds index aborts a filter program"); +} + +ATF_TC_BODY(bpf_mbuf_msh_overflow, tc) +{ + + RZ(rump_init()); + + ATF_CHECK(test_msh_overflow(0)); + ATF_CHECK(test_msh_overflow(1)); + ATF_CHECK(test_msh_overflow(2)); + ATF_CHECK(test_msh_overflow(3)); + ATF_CHECK(test_msh_overflow(4)); + ATF_CHECK(test_msh_overflow(5)); +} + +ATF_TP_ADD_TCS(tp) +{ + + /* + * For every new test please also add a similar test + * to ../../net/bpfjit/t_mbuf.c + */ + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_abs); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_abs); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_abs); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_ind); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_ind); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_ind); + ATF_TP_ADD_TC(tp, bpf_mbuf_msh); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_abs_overflow); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_abs_overflow); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_abs_overflow); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_ind_overflow1); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_ind_overflow2); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldb_ind_overflow3); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_ind_overflow1); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_ind_overflow2); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldh_ind_overflow3); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_ind_overflow1); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_ind_overflow2); + ATF_TP_ADD_TC(tp, bpf_mbuf_ldw_ind_overflow3); + ATF_TP_ADD_TC(tp, bpf_mbuf_msh_overflow); + + return atf_no_error(); +} |