1 /* $NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $ */
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __RCSID("$NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $");
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <sys/sysctl.h>
45 #include <rump/rump.h>
46 #include <rump/rump_syscalls.h>
48 /* XXX: atf-c.h has collisions with mbuf */
53 #include "../../h_macros.h"
54 #include "../config/netconfig.c"
57 ATF_TC_HEAD(bpfwriteleak
, tc
)
60 atf_tc_set_md_var(tc
, "descr", "Checks that writing to /dev/bpf "
61 "does not leak mbufs");
68 size_t mbstatlen
= sizeof(mbstat
);
69 const int mbstat_mib
[] = { CTL_KERN
, KERN_MBUF
, MBUF_STATS
};
71 RL(rump_sys___sysctl(mbstat_mib
, __arraycount(mbstat_mib
),
72 &mbstat
, &mbstatlen
, NULL
, 0));
73 return mbstat
.m_mtypes
[MT_DATA
];
76 ATF_TC_BODY(bpfwriteleak
, tc
)
78 char buf
[28]; /* sizeof(garbage) > etherhdrlen */
83 RZ(rump_pub_shmif_create(NULL
, &ifnum
));
84 sprintf(ifr
.ifr_name
, "shmif%d", ifnum
);
86 RL(bpfd
= rump_sys_open("/dev/bpf", O_RDWR
));
87 RL(rump_sys_ioctl(bpfd
, BIOCSETIF
, &ifr
));
88 RL(rump_sys_ioctl(bpfd
, BIOCSFEEDBACK
, &ifr
));
91 atf_tc_fail("test precondition failed: MT_DATA mbufs != 0");
93 ATF_REQUIRE_ERRNO(ENETDOWN
, rump_sys_write(bpfd
, buf
, sizeof(buf
))==-1);
95 ATF_REQUIRE_EQ(getmtdata(), 0);
98 #if (SIZE_MAX > UINT_MAX)
99 ATF_TC(bpfwritetrunc
);
100 ATF_TC_HEAD(bpfwritetrunc
, tc
)
102 atf_tc_set_md_var(tc
, "descr", "Checks that write to /dev/bpf "
103 "does not truncate size_t to int");
106 ATF_TC_BODY(bpfwritetrunc
, tc
)
112 const size_t extra_bytes
= 28;
113 const size_t total
= extra_bytes
+ UINT_MAX
+ 1;
114 long iov_max
, vm_page_size
; /* round_page wants vm_page_size variable */
116 memset(&ifr
, 0, sizeof(ifr
));
118 iov_max
= sysconf(_SC_IOV_MAX
);
119 vm_page_size
= sysconf(_SC_PAGE_SIZE
);
120 ATF_REQUIRE(iov_max
> 1 && vm_page_size
> 1);
123 * Minimize memory consumption by using many iovecs
124 * all pointing to one memory region.
126 iov
= calloc(iov_max
, sizeof(struct iovec
));
127 ATF_REQUIRE(iov
!= NULL
);
129 sz
= round_page((total
+ (iov_max
- 1)) / iov_max
);
132 iov
[0].iov_base
= mmap(NULL
, sz
, PROT_READ
, MAP_ANON
, -1, 0);
133 ATF_REQUIRE(iov
[0].iov_base
!= MAP_FAILED
);
136 while (sz
+ iov
[0].iov_len
<= total
)
138 iov
[iovlen
].iov_len
= iov
[0].iov_len
;
139 iov
[iovlen
].iov_base
= iov
[0].iov_base
;
140 sz
+= iov
[0].iov_len
;
146 iov
[iovlen
].iov_len
= total
- sz
;
147 iov
[iovlen
].iov_base
= iov
[0].iov_base
;
152 ATF_REQUIRE(iovlen
>= 1 && iovlen
<= (size_t)iov_max
);
153 ATF_REQUIRE_EQ(iov
[iovlen
-1].iov_len
, total
% iov
[0].iov_len
);
156 netcfg_rump_makeshmif("bpfwritetrunc", ifr
.ifr_name
);
157 netcfg_rump_if(ifr
.ifr_name
, "10.1.1.1", "255.0.0.0");
159 RL(bpfd
= rump_sys_open("/dev/bpf", O_RDWR
));
160 RL(rump_sys_ioctl(bpfd
, BIOCSETIF
, &ifr
));
162 ATF_CHECK_ERRNO(EMSGSIZE
, rump_sys_writev(bpfd
, iov
, iovlen
) == -1);
164 munmap(iov
[0].iov_base
, iov
[0].iov_len
);
167 #endif /* #if (SIZE_MAX > UINT_MAX) */
172 ATF_TP_ADD_TC(tp
, bpfwriteleak
);
173 #if (SIZE_MAX > UINT_MAX)
174 ATF_TP_ADD_TC(tp
, bpfwritetrunc
);
176 return atf_no_error();