1*11be35a1SLionel Sambuc #include <sys/types.h>
2*11be35a1SLionel Sambuc #include <sys/ioctl.h>
3*11be35a1SLionel Sambuc
4*11be35a1SLionel Sambuc #include <net/bpf.h>
5*11be35a1SLionel Sambuc
6*11be35a1SLionel Sambuc #include <atf-c.h>
7*11be35a1SLionel Sambuc #include <fcntl.h>
8*11be35a1SLionel Sambuc
9*11be35a1SLionel Sambuc #include <rump/rump.h>
10*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
11*11be35a1SLionel Sambuc
12*11be35a1SLionel Sambuc ATF_TC(div_by_zero);
ATF_TC_HEAD(div_by_zero,tc)13*11be35a1SLionel Sambuc ATF_TC_HEAD(div_by_zero, tc)
14*11be35a1SLionel Sambuc {
15*11be35a1SLionel Sambuc
16*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Check that BPF rejects a filter "
17*11be35a1SLionel Sambuc "which divides by 0");
18*11be35a1SLionel Sambuc }
19*11be35a1SLionel Sambuc
ATF_TC_BODY(div_by_zero,tc)20*11be35a1SLionel Sambuc ATF_TC_BODY(div_by_zero, tc)
21*11be35a1SLionel Sambuc {
22*11be35a1SLionel Sambuc struct bpf_program bp;
23*11be35a1SLionel Sambuc int fd;
24*11be35a1SLionel Sambuc
25*11be35a1SLionel Sambuc /*
26*11be35a1SLionel Sambuc * Source code for following program:
27*11be35a1SLionel Sambuc * link[0:4]/0 = 2
28*11be35a1SLionel Sambuc */
29*11be35a1SLionel Sambuc struct bpf_insn bins[] = {
30*11be35a1SLionel Sambuc { 0x20, 0, 0, 0x00000000 },
31*11be35a1SLionel Sambuc { 0x34, 0, 0, 0x00000000 },
32*11be35a1SLionel Sambuc { 0x15, 0, 1, 0x00000002 },
33*11be35a1SLionel Sambuc { 0x6, 0, 0, 0x00000060 },
34*11be35a1SLionel Sambuc { 0x6, 0, 0, 0x00000000 },
35*11be35a1SLionel Sambuc };
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc bp.bf_len = __arraycount(bins);
38*11be35a1SLionel Sambuc bp.bf_insns = bins;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc rump_init();
41*11be35a1SLionel Sambuc fd = rump_sys_open("/dev/bpf", O_RDWR);
42*11be35a1SLionel Sambuc ATF_CHECK(fd != -1);
43*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(fd, BIOCSETF, &bp), -1,
44*11be35a1SLionel Sambuc "bpf accepted program with division by zero");
45*11be35a1SLionel Sambuc }
46*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)47*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, div_by_zero);
51*11be35a1SLionel Sambuc return atf_no_error();
52*11be35a1SLionel Sambuc }
53