1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/* $NetBSD: t_bpf.c,v 1.9 2022/09/10 12:14:18 rillig 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.9 2022/09/10 12:14:18 rillig 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 <net/dlt.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_TC(bpf_ioctl_BLEN);
ATF_TC_HEAD(bpf_ioctl_BLEN, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks behaviors of BIOCGBLEN and "
"BIOCSBLEN");
}
ATF_TC_BODY(bpf_ioctl_BLEN, tc)
{
struct ifreq ifr;
int ifnum, bpfd;
u_int blen = 0;
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, BIOCGBLEN, &blen));
ATF_REQUIRE(blen != 0);
blen = 100;
RL(rump_sys_ioctl(bpfd, BIOCSBLEN, &blen));
RL(rump_sys_ioctl(bpfd, BIOCGBLEN, &blen));
ATF_REQUIRE_EQ(blen, 100);
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCSBLEN, &blen), -1,
"Don't allow to change buflen after binding bpf to an interface");
}
ATF_TC(bpf_ioctl_PROMISC);
ATF_TC_HEAD(bpf_ioctl_PROMISC, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks behaviors of BIOCPROMISC");
}
ATF_TC_BODY(bpf_ioctl_PROMISC, tc)
{
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));
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCPROMISC, NULL), -1,
"Don't allow to call ioctl(BIOCPROMISC) without interface");
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
RL(rump_sys_ioctl(bpfd, BIOCPROMISC, NULL));
/* TODO check if_flags */
}
ATF_TC(bpf_ioctl_SETIF);
ATF_TC_HEAD(bpf_ioctl_SETIF, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks behaviors of BIOCSETIF");
}
ATF_TC_BODY(bpf_ioctl_SETIF, tc)
{
struct ifreq ifr;
int ifnum, bpfd;
RZ(rump_init());
RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
RZ(rump_pub_shmif_create(NULL, &ifnum));
sprintf(ifr.ifr_name, "shmif%d", ifnum);
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
/* Change the listening interface */
RZ(rump_pub_shmif_create(NULL, &ifnum));
sprintf(ifr.ifr_name, "shmif%d", ifnum);
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
}
ATF_TC(bpf_ioctl_DLT);
ATF_TC_HEAD(bpf_ioctl_DLT, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks behaviors of BIOCGDLT and "
"BIOCSDLT");
}
ATF_TC_BODY(bpf_ioctl_DLT, tc)
{
struct ifreq ifr;
int ifnum, bpfd;
u_int dlt;
RZ(rump_init());
RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
RZ(rump_pub_shmif_create(NULL, &ifnum));
sprintf(ifr.ifr_name, "shmif%d", ifnum);
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCGDLT, &dlt), -1,
"Don't allow to get a DLT without interfaces");
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
RL(rump_sys_ioctl(bpfd, BIOCGDLT, &dlt));
ATF_REQUIRE(dlt == DLT_EN10MB);
dlt = DLT_NULL;
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCSDLT, &dlt), -1,
"Don't allow to set a DLT that doesn't match any listening "
"interfaces");
}
ATF_TC(bpf_ioctl_GDLTLIST);
ATF_TC_HEAD(bpf_ioctl_GDLTLIST, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks behaviors of BIOCGDLTLIST");
}
ATF_TC_BODY(bpf_ioctl_GDLTLIST, tc)
{
struct ifreq ifr;
int ifnum, bpfd;
struct bpf_dltlist dltlist;
RZ(rump_init());
RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
RZ(rump_pub_shmif_create(NULL, &ifnum));
sprintf(ifr.ifr_name, "shmif%d", ifnum);
dltlist.bfl_len = 0;
dltlist.bfl_list = NULL;
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCGDLTLIST, &dltlist), -1,
"Don't allow to get a DLT list without interfaces");
RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
/* Get the size of an available DLT list */
dltlist.bfl_len = 0;
dltlist.bfl_list = NULL;
RL(rump_sys_ioctl(bpfd, BIOCGDLTLIST, &dltlist));
ATF_REQUIRE(dltlist.bfl_len == 1);
/* Get an available DLT list */
dltlist.bfl_list = calloc(sizeof(u_int), 1);
dltlist.bfl_len = 1;
RL(rump_sys_ioctl(bpfd, BIOCGDLTLIST, &dltlist));
ATF_REQUIRE(dltlist.bfl_len == 1);
ATF_REQUIRE(dltlist.bfl_list[0] == DLT_EN10MB);
/* Get an available DLT list with a less buffer (fake with bfl_len) */
dltlist.bfl_len = 0;
ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(bpfd, BIOCGDLTLIST, &dltlist), -1,
"This should fail with ENOMEM");
free(dltlist.bfl_list);
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, bpfwriteleak);
#if (SIZE_MAX > UINT_MAX)
ATF_TP_ADD_TC(tp, bpfwritetrunc);
#endif
ATF_TP_ADD_TC(tp, bpf_ioctl_BLEN);
ATF_TP_ADD_TC(tp, bpf_ioctl_PROMISC);
ATF_TP_ADD_TC(tp, bpf_ioctl_SETIF);
ATF_TP_ADD_TC(tp, bpf_ioctl_DLT);
ATF_TP_ADD_TC(tp, bpf_ioctl_GDLTLIST);
return atf_no_error();
}
|