Home | History | Annotate | Line # | Download | only in bpf
t_bpf.c revision 1.1
      1 /*	$NetBSD: t_bpf.c,v 1.1 2010/12/06 11:32:01 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/param.h>
     29 #include <sys/ioctl.h>
     30 #include <sys/socket.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/sysctl.h>
     33 
     34 #include <net/if.h>
     35 #include <net/bpf.h>
     36 
     37 #include <fcntl.h>
     38 #include <stdio.h>
     39 #include <string.h>
     40 
     41 #include <rump/rump.h>
     42 #include <rump/rump_syscalls.h>
     43 
     44 /* XXX: atf-c.h has collisions with mbuf */
     45 #undef m_type
     46 #undef m_data
     47 #include <atf-c.h>
     48 
     49 #include "../../h_macros.h"
     50 
     51 ATF_TC(bpfwriteleak);
     52 ATF_TC_HEAD(bpfwriteleak, tc)
     53 {
     54 
     55 	atf_tc_set_md_var(tc, "descr", "Checks that writing to /dev/bpf "
     56 	    "does not leak mbufs");
     57 }
     58 
     59 static int
     60 getmtdata(void)
     61 {
     62 	struct mbstat mbstat;
     63 	size_t mbstatlen = sizeof(mbstat);
     64 	const int mbstat_mib[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
     65 
     66 	RL(rump_sys___sysctl(mbstat_mib, __arraycount(mbstat_mib),
     67 	    &mbstat, &mbstatlen, NULL, 0));
     68 	return mbstat.m_mtypes[MT_DATA];
     69 }
     70 
     71 ATF_TC_BODY(bpfwriteleak, tc)
     72 {
     73 	char buf[28]; /* sizeof(garbage) > etherhdrlen */
     74 	struct ifreq ifr;
     75 	int ifnum, bpfd;
     76 	u_int x;
     77 
     78 	RZ(rump_init());
     79 	RZ(rump_pub_shmif_create(NULL, &ifnum));
     80 	sprintf(ifr.ifr_name, "shmif%d", ifnum);
     81 
     82 	RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
     83 	RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
     84         x = 1;
     85 	RL(rump_sys_ioctl(bpfd, BIOCSFEEDBACK, &ifr));
     86 
     87 	if (getmtdata() != 0)
     88 		atf_tc_fail("test precondition failed: MT_DATA mbufs != 0");
     89 
     90 	ATF_REQUIRE_ERRNO(ENETDOWN, rump_sys_write(bpfd, buf, sizeof(buf))==-1);
     91 
     92 	atf_tc_expect_fail("PR kern/44196");
     93 	ATF_REQUIRE_EQ(getmtdata(), 0);
     94 }
     95 
     96 ATF_TP_ADD_TCS(tp)
     97 {
     98 
     99 	ATF_TP_ADD_TC(tp, bpfwriteleak);
    100 	return atf_no_error();
    101 }
    102