]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-bridge-helper.c
qemu-bridge-helper: restrict interface name to IFNAMSIZ
[mirror_qemu.git] / qemu-bridge-helper.c
1 /*
2 * QEMU Bridge Helper
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Richa Marwaha <rmarwah@linux.vnet.ibm.com>
9 * Corey Bryant <coreyb@linux.vnet.ibm.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 */
14
15 /*
16 * Known shortcomings:
17 * - There is no manual page
18 * - The syntax of the ACL file is not documented anywhere
19 * - parse_acl_file() doesn't report fopen() failure properly, fails
20 * to check ferror() after fgets() failure, arbitrarily truncates
21 * long lines, handles whitespace inconsistently, error messages
22 * don't point to the offending file and line, errors in included
23 * files are reported, but otherwise ignored, ...
24 */
25
26 #include "qemu/osdep.h"
27
28
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <sys/prctl.h>
33
34 #include <net/if.h>
35
36 #include <linux/sockios.h>
37
38 #ifndef SIOCBRADDIF
39 #include <linux/if_bridge.h>
40 #endif
41
42 #include "qemu/queue.h"
43
44 #include "net/tap-linux.h"
45
46 #ifdef CONFIG_LIBCAP
47 #include <cap-ng.h>
48 #endif
49
50 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
51
52 enum {
53 ACL_ALLOW = 0,
54 ACL_ALLOW_ALL,
55 ACL_DENY,
56 ACL_DENY_ALL,
57 };
58
59 typedef struct ACLRule {
60 int type;
61 char iface[IFNAMSIZ];
62 QSIMPLEQ_ENTRY(ACLRule) entry;
63 } ACLRule;
64
65 typedef QSIMPLEQ_HEAD(ACLList, ACLRule) ACLList;
66
67 static void usage(void)
68 {
69 fprintf(stderr,
70 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
71 }
72
73 static int parse_acl_file(const char *filename, ACLList *acl_list)
74 {
75 FILE *f;
76 char line[4096];
77 ACLRule *acl_rule;
78
79 f = fopen(filename, "r");
80 if (f == NULL) {
81 return -1;
82 }
83
84 while (fgets(line, sizeof(line), f) != NULL) {
85 char *ptr = line;
86 char *cmd, *arg, *argend;
87
88 while (g_ascii_isspace(*ptr)) {
89 ptr++;
90 }
91
92 /* skip comments and empty lines */
93 if (*ptr == '#' || *ptr == 0) {
94 continue;
95 }
96
97 cmd = ptr;
98 arg = strchr(cmd, ' ');
99 if (arg == NULL) {
100 arg = strchr(cmd, '\t');
101 }
102
103 if (arg == NULL) {
104 fprintf(stderr, "Invalid config line:\n %s\n", line);
105 fclose(f);
106 errno = EINVAL;
107 return -1;
108 }
109
110 *arg = 0;
111 arg++;
112 while (g_ascii_isspace(*arg)) {
113 arg++;
114 }
115
116 argend = arg + strlen(arg);
117 while (arg != argend && g_ascii_isspace(*(argend - 1))) {
118 argend--;
119 }
120 *argend = 0;
121
122 if (!g_str_equal(cmd, "include") && strlen(arg) >= IFNAMSIZ) {
123 fprintf(stderr, "name `%s' too long: %zu\n", arg, strlen(arg));
124 fclose(f);
125 errno = EINVAL;
126 return -1;
127 }
128
129 if (strcmp(cmd, "deny") == 0) {
130 acl_rule = g_malloc(sizeof(*acl_rule));
131 if (strcmp(arg, "all") == 0) {
132 acl_rule->type = ACL_DENY_ALL;
133 } else {
134 acl_rule->type = ACL_DENY;
135 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
136 }
137 QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
138 } else if (strcmp(cmd, "allow") == 0) {
139 acl_rule = g_malloc(sizeof(*acl_rule));
140 if (strcmp(arg, "all") == 0) {
141 acl_rule->type = ACL_ALLOW_ALL;
142 } else {
143 acl_rule->type = ACL_ALLOW;
144 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
145 }
146 QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
147 } else if (strcmp(cmd, "include") == 0) {
148 /* ignore errors */
149 parse_acl_file(arg, acl_list);
150 } else {
151 fprintf(stderr, "Unknown command `%s'\n", cmd);
152 fclose(f);
153 errno = EINVAL;
154 return -1;
155 }
156 }
157
158 fclose(f);
159
160 return 0;
161 }
162
163 static bool has_vnet_hdr(int fd)
164 {
165 unsigned int features = 0;
166
167 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
168 return false;
169 }
170
171 if (!(features & IFF_VNET_HDR)) {
172 return false;
173 }
174
175 return true;
176 }
177
178 static void prep_ifreq(struct ifreq *ifr, const char *ifname)
179 {
180 memset(ifr, 0, sizeof(*ifr));
181 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", ifname);
182 }
183
184 static int send_fd(int c, int fd)
185 {
186 char msgbuf[CMSG_SPACE(sizeof(fd))];
187 struct msghdr msg = {
188 .msg_control = msgbuf,
189 .msg_controllen = sizeof(msgbuf),
190 };
191 struct cmsghdr *cmsg;
192 struct iovec iov;
193 char req[1] = { 0x00 };
194
195 cmsg = CMSG_FIRSTHDR(&msg);
196 cmsg->cmsg_level = SOL_SOCKET;
197 cmsg->cmsg_type = SCM_RIGHTS;
198 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
199 msg.msg_controllen = cmsg->cmsg_len;
200
201 iov.iov_base = req;
202 iov.iov_len = sizeof(req);
203
204 msg.msg_iov = &iov;
205 msg.msg_iovlen = 1;
206 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
207
208 return sendmsg(c, &msg, 0);
209 }
210
211 #ifdef CONFIG_LIBCAP
212 static int drop_privileges(void)
213 {
214 /* clear all capabilities */
215 capng_clear(CAPNG_SELECT_BOTH);
216
217 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
218 CAP_NET_ADMIN) < 0) {
219 return -1;
220 }
221
222 /* change to calling user's real uid and gid, retaining supplemental
223 * groups and CAP_NET_ADMIN */
224 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING)) {
225 return -1;
226 }
227
228 return 0;
229 }
230 #endif
231
232 int main(int argc, char **argv)
233 {
234 struct ifreq ifr;
235 #ifndef SIOCBRADDIF
236 unsigned long ifargs[4];
237 #endif
238 int ifindex;
239 int fd = -1, ctlfd = -1, unixfd = -1;
240 int use_vnet = 0;
241 int mtu;
242 const char *bridge = NULL;
243 char iface[IFNAMSIZ];
244 int index;
245 ACLRule *acl_rule;
246 ACLList acl_list;
247 int access_allowed, access_denied;
248 int ret = EXIT_SUCCESS;
249
250 #ifdef CONFIG_LIBCAP
251 /* if we're run from an suid binary, immediately drop privileges preserving
252 * cap_net_admin */
253 if (geteuid() == 0 && getuid() != geteuid()) {
254 if (drop_privileges() == -1) {
255 fprintf(stderr, "failed to drop privileges\n");
256 return 1;
257 }
258 }
259 #endif
260
261 /* parse arguments */
262 for (index = 1; index < argc; index++) {
263 if (strcmp(argv[index], "--use-vnet") == 0) {
264 use_vnet = 1;
265 } else if (strncmp(argv[index], "--br=", 5) == 0) {
266 bridge = &argv[index][5];
267 } else if (strncmp(argv[index], "--fd=", 5) == 0) {
268 unixfd = atoi(&argv[index][5]);
269 } else {
270 usage();
271 return EXIT_FAILURE;
272 }
273 }
274
275 if (bridge == NULL || unixfd == -1) {
276 usage();
277 return EXIT_FAILURE;
278 }
279 if (strlen(bridge) >= IFNAMSIZ) {
280 fprintf(stderr, "name `%s' too long: %zu\n", bridge, strlen(bridge));
281 return EXIT_FAILURE;
282 }
283
284 /* parse default acl file */
285 QSIMPLEQ_INIT(&acl_list);
286 if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
287 fprintf(stderr, "failed to parse default acl file `%s'\n",
288 DEFAULT_ACL_FILE);
289 ret = EXIT_FAILURE;
290 goto cleanup;
291 }
292
293 /* validate bridge against acl -- default policy is to deny
294 * according acl policy if we have a deny and allow both
295 * then deny should always win over allow
296 */
297 access_allowed = 0;
298 access_denied = 0;
299 QSIMPLEQ_FOREACH(acl_rule, &acl_list, entry) {
300 switch (acl_rule->type) {
301 case ACL_ALLOW_ALL:
302 access_allowed = 1;
303 break;
304 case ACL_ALLOW:
305 if (strcmp(bridge, acl_rule->iface) == 0) {
306 access_allowed = 1;
307 }
308 break;
309 case ACL_DENY_ALL:
310 access_denied = 1;
311 break;
312 case ACL_DENY:
313 if (strcmp(bridge, acl_rule->iface) == 0) {
314 access_denied = 1;
315 }
316 break;
317 }
318 }
319
320 if ((access_allowed == 0) || (access_denied == 1)) {
321 fprintf(stderr, "access denied by acl file\n");
322 ret = EXIT_FAILURE;
323 goto cleanup;
324 }
325
326 /* open a socket to use to control the network interfaces */
327 ctlfd = socket(AF_INET, SOCK_STREAM, 0);
328 if (ctlfd == -1) {
329 fprintf(stderr, "failed to open control socket: %s\n", strerror(errno));
330 ret = EXIT_FAILURE;
331 goto cleanup;
332 }
333
334 /* open the tap device */
335 fd = open("/dev/net/tun", O_RDWR);
336 if (fd == -1) {
337 fprintf(stderr, "failed to open /dev/net/tun: %s\n", strerror(errno));
338 ret = EXIT_FAILURE;
339 goto cleanup;
340 }
341
342 /* request a tap device, disable PI, and add vnet header support if
343 * requested and it's available. */
344 prep_ifreq(&ifr, "tap%d");
345 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
346 if (use_vnet && has_vnet_hdr(fd)) {
347 ifr.ifr_flags |= IFF_VNET_HDR;
348 }
349
350 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
351 fprintf(stderr, "failed to create tun device: %s\n", strerror(errno));
352 ret = EXIT_FAILURE;
353 goto cleanup;
354 }
355
356 /* save tap device name */
357 snprintf(iface, sizeof(iface), "%s", ifr.ifr_name);
358
359 /* get the mtu of the bridge */
360 prep_ifreq(&ifr, bridge);
361 if (ioctl(ctlfd, SIOCGIFMTU, &ifr) == -1) {
362 fprintf(stderr, "failed to get mtu of bridge `%s': %s\n",
363 bridge, strerror(errno));
364 ret = EXIT_FAILURE;
365 goto cleanup;
366 }
367
368 /* save mtu */
369 mtu = ifr.ifr_mtu;
370
371 /* set the mtu of the interface based on the bridge */
372 prep_ifreq(&ifr, iface);
373 ifr.ifr_mtu = mtu;
374 if (ioctl(ctlfd, SIOCSIFMTU, &ifr) == -1) {
375 fprintf(stderr, "failed to set mtu of device `%s' to %d: %s\n",
376 iface, mtu, strerror(errno));
377 ret = EXIT_FAILURE;
378 goto cleanup;
379 }
380
381 /* Linux uses the lowest enslaved MAC address as the MAC address of
382 * the bridge. Set MAC address to a high value so that it doesn't
383 * affect the MAC address of the bridge.
384 */
385 if (ioctl(ctlfd, SIOCGIFHWADDR, &ifr) < 0) {
386 fprintf(stderr, "failed to get MAC address of device `%s': %s\n",
387 iface, strerror(errno));
388 ret = EXIT_FAILURE;
389 goto cleanup;
390 }
391 ifr.ifr_hwaddr.sa_data[0] = 0xFE;
392 if (ioctl(ctlfd, SIOCSIFHWADDR, &ifr) < 0) {
393 fprintf(stderr, "failed to set MAC address of device `%s': %s\n",
394 iface, strerror(errno));
395 ret = EXIT_FAILURE;
396 goto cleanup;
397 }
398
399 /* add the interface to the bridge */
400 prep_ifreq(&ifr, bridge);
401 ifindex = if_nametoindex(iface);
402 #ifndef SIOCBRADDIF
403 ifargs[0] = BRCTL_ADD_IF;
404 ifargs[1] = ifindex;
405 ifargs[2] = 0;
406 ifargs[3] = 0;
407 ifr.ifr_data = (void *)ifargs;
408 ret = ioctl(ctlfd, SIOCDEVPRIVATE, &ifr);
409 #else
410 ifr.ifr_ifindex = ifindex;
411 ret = ioctl(ctlfd, SIOCBRADDIF, &ifr);
412 #endif
413 if (ret == -1) {
414 fprintf(stderr, "failed to add interface `%s' to bridge `%s': %s\n",
415 iface, bridge, strerror(errno));
416 ret = EXIT_FAILURE;
417 goto cleanup;
418 }
419
420 /* bring the interface up */
421 prep_ifreq(&ifr, iface);
422 if (ioctl(ctlfd, SIOCGIFFLAGS, &ifr) == -1) {
423 fprintf(stderr, "failed to get interface flags for `%s': %s\n",
424 iface, strerror(errno));
425 ret = EXIT_FAILURE;
426 goto cleanup;
427 }
428
429 ifr.ifr_flags |= IFF_UP;
430 if (ioctl(ctlfd, SIOCSIFFLAGS, &ifr) == -1) {
431 fprintf(stderr, "failed to bring up interface `%s': %s\n",
432 iface, strerror(errno));
433 ret = EXIT_FAILURE;
434 goto cleanup;
435 }
436
437 /* write fd to the domain socket */
438 if (send_fd(unixfd, fd) == -1) {
439 fprintf(stderr, "failed to write fd to unix socket: %s\n",
440 strerror(errno));
441 ret = EXIT_FAILURE;
442 goto cleanup;
443 }
444
445 /* ... */
446
447 /* profit! */
448
449 cleanup:
450 if (fd >= 0) {
451 close(fd);
452 }
453 if (ctlfd >= 0) {
454 close(ctlfd);
455 }
456 while ((acl_rule = QSIMPLEQ_FIRST(&acl_list)) != NULL) {
457 QSIMPLEQ_REMOVE_HEAD(&acl_list, entry);
458 g_free(acl_rule);
459 }
460
461 return ret;
462 }