vmd.c revision 1.102
1/*	$OpenBSD: vmd.c,v 1.102 2018/09/29 22:33:09 pd Exp $	*/
2
3/*
4 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/param.h>	/* nitems */
20#include <sys/queue.h>
21#include <sys/wait.h>
22#include <sys/cdefs.h>
23#include <sys/stat.h>
24#include <sys/tty.h>
25#include <sys/ttycom.h>
26#include <sys/ioctl.h>
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <termios.h>
32#include <errno.h>
33#include <event.h>
34#include <fcntl.h>
35#include <pwd.h>
36#include <signal.h>
37#include <syslog.h>
38#include <unistd.h>
39#include <util.h>
40#include <ctype.h>
41#include <pwd.h>
42#include <grp.h>
43
44#include <machine/specialreg.h>
45#include <machine/vmmvar.h>
46
47#include "proc.h"
48#include "atomicio.h"
49#include "vmd.h"
50
51__dead void usage(void);
52
53int	 main(int, char **);
54int	 vmd_configure(void);
55void	 vmd_sighdlr(int sig, short event, void *arg);
56void	 vmd_shutdown(void);
57int	 vmd_control_run(void);
58int	 vmd_dispatch_control(int, struct privsep_proc *, struct imsg *);
59int	 vmd_dispatch_vmm(int, struct privsep_proc *, struct imsg *);
60int	 vmd_check_vmh(struct vm_dump_header *);
61
62int	 vm_instance(struct privsep *, struct vmd_vm **,
63	    struct vmop_create_params *, uid_t);
64int	 vm_checkinsflag(struct vmop_create_params *, unsigned int, uid_t);
65
66struct vmd	*env;
67
68static struct privsep_proc procs[] = {
69	/* Keep "priv" on top as procs[0] */
70	{ "priv",	PROC_PRIV,	NULL, priv },
71	{ "control",	PROC_CONTROL,	vmd_dispatch_control, control },
72	{ "vmm",	PROC_VMM,	vmd_dispatch_vmm, vmm, vmm_shutdown },
73};
74
75/* For the privileged process */
76static struct privsep_proc *proc_priv = &procs[0];
77static struct passwd proc_privpw;
78static const uint8_t zero_mac[ETHER_ADDR_LEN];
79
80int
81vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
82{
83	struct privsep			*ps = p->p_ps;
84	int				 res = 0, ret = 0, cmd = 0, verbose;
85	unsigned int			 v = 0, flags;
86	struct vmop_create_params	 vmc;
87	struct vmop_id			 vid;
88	struct vmop_result		 vmr;
89	struct vm_dump_header		 vmh;
90	struct vmd_vm			*vm = NULL;
91	char				*str = NULL;
92	uint32_t			 id = 0;
93	struct control_sock		*rcs;
94
95	switch (imsg->hdr.type) {
96	case IMSG_VMDOP_START_VM_REQUEST:
97		IMSG_SIZE_CHECK(imsg, &vmc);
98		memcpy(&vmc, imsg->data, sizeof(vmc));
99		ret = vm_register(ps, &vmc, &vm, 0, vmc.vmc_owner.uid);
100		if (vmc.vmc_flags == 0) {
101			/* start an existing VM with pre-configured options */
102			if (!(ret == -1 && errno == EALREADY &&
103			    vm->vm_running == 0)) {
104				res = errno;
105				cmd = IMSG_VMDOP_START_VM_RESPONSE;
106			}
107		} else if (ret != 0) {
108			res = errno;
109			cmd = IMSG_VMDOP_START_VM_RESPONSE;
110		}
111		if (res == 0 &&
112		    config_setvm(ps, vm,
113		    imsg->hdr.peerid, vm->vm_params.vmc_owner.uid) == -1) {
114			res = errno;
115			cmd = IMSG_VMDOP_START_VM_RESPONSE;
116		}
117		break;
118	case IMSG_VMDOP_TERMINATE_VM_REQUEST:
119		IMSG_SIZE_CHECK(imsg, &vid);
120		memcpy(&vid, imsg->data, sizeof(vid));
121		flags = vid.vid_flags;
122
123		if ((id = vid.vid_id) == 0) {
124			/* Lookup vm (id) by name */
125			if ((vm = vm_getbyname(vid.vid_name)) == NULL) {
126				res = ENOENT;
127				cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE;
128				break;
129			} else if (vm->vm_shutdown &&
130			    (flags & VMOP_FORCE) == 0) {
131				res = EALREADY;
132				cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE;
133				break;
134			} else if (vm->vm_running == 0) {
135				res = EINVAL;
136				cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE;
137				break;
138			}
139			id = vm->vm_vmid;
140		} else if ((vm = vm_getbyvmid(id)) == NULL) {
141			res = ENOENT;
142			cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE;
143			break;
144		}
145		if (vm_checkperm(vm, &vm->vm_params.vmc_owner,
146		    vid.vid_uid) != 0) {
147			res = EPERM;
148			cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE;
149			break;
150		}
151
152		memset(&vid, 0, sizeof(vid));
153		vid.vid_id = id;
154		vid.vid_flags = flags;
155		if (proc_compose_imsg(ps, PROC_VMM, -1, imsg->hdr.type,
156		    imsg->hdr.peerid, -1, &vid, sizeof(vid)) == -1)
157			return (-1);
158		break;
159	case IMSG_VMDOP_GET_INFO_VM_REQUEST:
160		proc_forward_imsg(ps, imsg, PROC_VMM, -1);
161		break;
162	case IMSG_VMDOP_LOAD:
163		IMSG_SIZE_CHECK(imsg, str); /* at least one byte for path */
164		str = get_string((uint8_t *)imsg->data,
165		    IMSG_DATA_SIZE(imsg));
166	case IMSG_VMDOP_RELOAD:
167		if (vmd_reload(0, str) == -1)
168			cmd = IMSG_CTL_FAIL;
169		else
170			cmd = IMSG_CTL_OK;
171		free(str);
172		break;
173	case IMSG_CTL_RESET:
174		IMSG_SIZE_CHECK(imsg, &v);
175		memcpy(&v, imsg->data, sizeof(v));
176		if (vmd_reload(v, NULL) == -1)
177			cmd = IMSG_CTL_FAIL;
178		else
179			cmd = IMSG_CTL_OK;
180		break;
181	case IMSG_CTL_VERBOSE:
182		IMSG_SIZE_CHECK(imsg, &verbose);
183		memcpy(&verbose, imsg->data, sizeof(verbose));
184		log_setverbose(verbose);
185
186		proc_forward_imsg(ps, imsg, PROC_VMM, -1);
187		proc_forward_imsg(ps, imsg, PROC_PRIV, -1);
188		cmd = IMSG_CTL_OK;
189		break;
190	case IMSG_VMDOP_PAUSE_VM:
191	case IMSG_VMDOP_UNPAUSE_VM:
192		IMSG_SIZE_CHECK(imsg, &vid);
193		memcpy(&vid, imsg->data, sizeof(vid));
194		if (vid.vid_id == 0) {
195			if ((vm = vm_getbyname(vid.vid_name)) == NULL) {
196				res = ENOENT;
197				cmd = IMSG_VMDOP_PAUSE_VM_RESPONSE;
198				break;
199			} else {
200				vid.vid_id = vm->vm_vmid;
201			}
202		} else if ((vm = vm_getbyid(vid.vid_id)) == NULL) {
203			res = ENOENT;
204			cmd = IMSG_VMDOP_PAUSE_VM_RESPONSE;
205			break;
206		}
207		if (vm_checkperm(vm, &vm->vm_params.vmc_owner,
208		    vid.vid_uid) != 0) {
209			res = EPERM;
210			cmd = IMSG_VMDOP_PAUSE_VM_RESPONSE;
211			break;
212		}
213		proc_compose_imsg(ps, PROC_VMM, -1, imsg->hdr.type,
214		    imsg->hdr.peerid, -1, &vid, sizeof(vid));
215		break;
216	case IMSG_VMDOP_SEND_VM_REQUEST:
217		IMSG_SIZE_CHECK(imsg, &vid);
218		memcpy(&vid, imsg->data, sizeof(vid));
219		id = vid.vid_id;
220		if (vid.vid_id == 0) {
221			if ((vm = vm_getbyname(vid.vid_name)) == NULL) {
222				res = ENOENT;
223				cmd = IMSG_VMDOP_SEND_VM_RESPONSE;
224				close(imsg->fd);
225				break;
226			} else {
227				vid.vid_id = vm->vm_vmid;
228			}
229		} else if ((vm = vm_getbyvmid(vid.vid_id)) == NULL) {
230			res = ENOENT;
231			cmd = IMSG_VMDOP_SEND_VM_RESPONSE;
232			close(imsg->fd);
233			break;
234		} else {
235		}
236		vmr.vmr_id = vid.vid_id;
237		log_debug("%s: sending fd to vmm", __func__);
238		proc_compose_imsg(ps, PROC_VMM, -1, imsg->hdr.type,
239		    imsg->hdr.peerid, imsg->fd, &vid, sizeof(vid));
240		break;
241	case IMSG_VMDOP_RECEIVE_VM_REQUEST:
242		IMSG_SIZE_CHECK(imsg, &vid);
243		memcpy(&vid, imsg->data, sizeof(vid));
244		if (imsg->fd == -1) {
245			log_warnx("%s: invalid fd", __func__);
246			return (-1);
247		}
248		if (atomicio(read, imsg->fd, &vmh, sizeof(vmh)) !=
249		    sizeof(vmh)) {
250			log_warnx("%s: error reading vmh from received vm",
251			    __func__);
252			res = EIO;
253			close(imsg->fd);
254			cmd = IMSG_VMDOP_START_VM_RESPONSE;
255			break;
256		}
257
258		if (vmd_check_vmh(&vmh)) {
259			res = ENOENT;
260			close(imsg->fd);
261			cmd = IMSG_VMDOP_START_VM_RESPONSE;
262			break;
263		}
264		if (atomicio(read, imsg->fd, &vmc, sizeof(vmc)) !=
265		    sizeof(vmc)) {
266			log_warnx("%s: error reading vmc from received vm",
267			    __func__);
268			res = EIO;
269			close(imsg->fd);
270			cmd = IMSG_VMDOP_START_VM_RESPONSE;
271			break;
272		}
273		strlcpy(vmc.vmc_params.vcp_name, vid.vid_name,
274		    sizeof(vmc.vmc_params.vcp_name));
275		vmc.vmc_params.vcp_id = 0;
276
277		ret = vm_register(ps, &vmc, &vm, 0, vmc.vmc_owner.uid);
278		if (ret != 0) {
279			res = errno;
280			cmd = IMSG_VMDOP_START_VM_RESPONSE;
281			close(imsg->fd);
282		} else {
283			vm->vm_received = 1;
284			config_setvm(ps, vm, imsg->hdr.peerid,
285			    vmc.vmc_owner.uid);
286			log_debug("%s: sending fd to vmm", __func__);
287			proc_compose_imsg(ps, PROC_VMM, -1,
288			    IMSG_VMDOP_RECEIVE_VM_END, vm->vm_vmid, imsg->fd,
289			    NULL, 0);
290		}
291		break;
292	case IMSG_VMDOP_DONE:
293		control_reset(&ps->ps_csock);
294		TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
295			control_reset(rcs);
296		cmd = 0;
297		break;
298	default:
299		return (-1);
300	}
301
302	switch (cmd) {
303	case 0:
304		break;
305	case IMSG_VMDOP_START_VM_RESPONSE:
306	case IMSG_VMDOP_TERMINATE_VM_RESPONSE:
307		memset(&vmr, 0, sizeof(vmr));
308		vmr.vmr_result = res;
309		vmr.vmr_id = id;
310		if (proc_compose_imsg(ps, PROC_CONTROL, -1, cmd,
311		    imsg->hdr.peerid, -1, &vmr, sizeof(vmr)) == -1)
312			return (-1);
313		break;
314	default:
315		if (proc_compose_imsg(ps, PROC_CONTROL, -1, cmd,
316		    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
317			return (-1);
318		break;
319	}
320
321	return (0);
322}
323
324int
325vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg)
326{
327	struct vmop_result	 vmr;
328	struct privsep		*ps = p->p_ps;
329	int			 res = 0;
330	struct vmd_vm		*vm;
331	struct vm_create_params	*vcp;
332	struct vmop_info_result	 vir;
333
334	switch (imsg->hdr.type) {
335	case IMSG_VMDOP_PAUSE_VM_RESPONSE:
336		IMSG_SIZE_CHECK(imsg, &vmr);
337		memcpy(&vmr, imsg->data, sizeof(vmr));
338		if ((vm = vm_getbyvmid(vmr.vmr_id)) == NULL)
339			break;
340		proc_compose_imsg(ps, PROC_CONTROL, -1,
341		    imsg->hdr.type, imsg->hdr.peerid, -1,
342		    imsg->data, sizeof(imsg->data));
343		log_info("%s: paused vm %d successfully",
344		    vm->vm_params.vmc_params.vcp_name,
345		    vm->vm_vmid);
346		break;
347	case IMSG_VMDOP_UNPAUSE_VM_RESPONSE:
348		IMSG_SIZE_CHECK(imsg, &vmr);
349		memcpy(&vmr, imsg->data, sizeof(vmr));
350		if ((vm = vm_getbyvmid(vmr.vmr_id)) == NULL)
351			break;
352		proc_compose_imsg(ps, PROC_CONTROL, -1,
353		    imsg->hdr.type, imsg->hdr.peerid, -1,
354		    imsg->data, sizeof(imsg->data));
355		log_info("%s: unpaused vm %d successfully.",
356		    vm->vm_params.vmc_params.vcp_name,
357		    vm->vm_vmid);
358		break;
359	case IMSG_VMDOP_START_VM_RESPONSE:
360		IMSG_SIZE_CHECK(imsg, &vmr);
361		memcpy(&vmr, imsg->data, sizeof(vmr));
362		if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL)
363			break;
364		vm->vm_pid = vmr.vmr_pid;
365		vcp = &vm->vm_params.vmc_params;
366		vcp->vcp_id = vmr.vmr_id;
367
368		/*
369		 * If the peerid is not -1, forward the response back to the
370		 * the control socket.  If it is -1, the request originated
371		 * from the parent, not the control socket.
372		 */
373		if (vm->vm_peerid != (uint32_t)-1) {
374			(void)strlcpy(vmr.vmr_ttyname, vm->vm_ttyname,
375			    sizeof(vmr.vmr_ttyname));
376			if (proc_compose_imsg(ps, PROC_CONTROL, -1,
377			    imsg->hdr.type, vm->vm_peerid, -1,
378			    &vmr, sizeof(vmr)) == -1) {
379				errno = vmr.vmr_result;
380				log_warn("%s: failed to foward vm result",
381				    vcp->vcp_name);
382				vm_remove(vm, __func__);
383				return (-1);
384			}
385		}
386
387		if (vmr.vmr_result) {
388			errno = vmr.vmr_result;
389			log_warn("%s: failed to start vm", vcp->vcp_name);
390			vm_remove(vm, __func__);
391			break;
392		}
393
394		/* Now configure all the interfaces */
395		if (vm_priv_ifconfig(ps, vm) == -1) {
396			log_warn("%s: failed to configure vm", vcp->vcp_name);
397			vm_remove(vm, __func__);
398			break;
399		}
400
401		log_info("%s: started vm %d successfully, tty %s",
402		    vcp->vcp_name, vm->vm_vmid, vm->vm_ttyname);
403		break;
404	case IMSG_VMDOP_TERMINATE_VM_RESPONSE:
405		IMSG_SIZE_CHECK(imsg, &vmr);
406		memcpy(&vmr, imsg->data, sizeof(vmr));
407		DPRINTF("%s: forwarding TERMINATE VM for vm id %d",
408		    __func__, vmr.vmr_id);
409		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
410		if ((vm = vm_getbyvmid(vmr.vmr_id)) == NULL)
411			break;
412		if (vmr.vmr_result == 0) {
413			/* Mark VM as shutting down */
414			vm->vm_shutdown = 1;
415		}
416		break;
417	case IMSG_VMDOP_SEND_VM_RESPONSE:
418		IMSG_SIZE_CHECK(imsg, &vmr);
419		memcpy(&vmr, imsg->data, sizeof(vmr));
420		if ((vm = vm_getbyvmid(vmr.vmr_id)) == NULL)
421			break;
422		if (!vmr.vmr_result) {
423			log_info("%s: sent vm %d successfully.",
424			    vm->vm_params.vmc_params.vcp_name,
425			    vm->vm_vmid);
426			if (vm->vm_from_config)
427				vm_stop(vm, 0, __func__);
428			else
429				vm_remove(vm, __func__);
430		}
431
432		/* Send a response if a control client is waiting for it */
433		if (imsg->hdr.peerid != (uint32_t)-1) {
434			/* the error is meaningless for deferred responses */
435			vmr.vmr_result = 0;
436
437			if (proc_compose_imsg(ps, PROC_CONTROL, -1,
438			    IMSG_VMDOP_SEND_VM_RESPONSE,
439			    imsg->hdr.peerid, -1, &vmr, sizeof(vmr)) == -1)
440				return (-1);
441		}
442		break;
443	case IMSG_VMDOP_TERMINATE_VM_EVENT:
444		IMSG_SIZE_CHECK(imsg, &vmr);
445		memcpy(&vmr, imsg->data, sizeof(vmr));
446		DPRINTF("%s: handling TERMINATE_EVENT for vm id %d ret %d",
447		    __func__, vmr.vmr_id, vmr.vmr_result);
448		if ((vm = vm_getbyvmid(vmr.vmr_id)) == NULL) {
449			log_debug("%s: vm %d is no longer available",
450			    __func__, vmr.vmr_id);
451			break;
452		}
453		if (vmr.vmr_result != EAGAIN) {
454			if (vm->vm_from_config)
455				vm_stop(vm, 0, __func__);
456			else
457				vm_remove(vm, __func__);
458		} else {
459			/* Stop VM instance but keep the tty open */
460			vm_stop(vm, 1, __func__);
461			config_setvm(ps, vm, (uint32_t)-1, vm->vm_uid);
462		}
463
464		/* Send a response if a control client is waiting for it */
465		if (imsg->hdr.peerid != (uint32_t)-1) {
466			/* the error is meaningless for deferred responses */
467			vmr.vmr_result = 0;
468
469			if (proc_compose_imsg(ps, PROC_CONTROL, -1,
470			    IMSG_VMDOP_TERMINATE_VM_RESPONSE,
471			    imsg->hdr.peerid, -1, &vmr, sizeof(vmr)) == -1)
472				return (-1);
473		}
474		break;
475	case IMSG_VMDOP_GET_INFO_VM_DATA:
476		IMSG_SIZE_CHECK(imsg, &vir);
477		memcpy(&vir, imsg->data, sizeof(vir));
478		if ((vm = vm_getbyvmid(vir.vir_info.vir_id)) != NULL) {
479			memset(vir.vir_ttyname, 0, sizeof(vir.vir_ttyname));
480			if (vm->vm_ttyname != NULL)
481				strlcpy(vir.vir_ttyname, vm->vm_ttyname,
482				    sizeof(vir.vir_ttyname));
483			if (vm->vm_shutdown) {
484				/* XXX there might be a nicer way */
485				(void)strlcat(vir.vir_info.vir_name,
486				    " - stopping",
487				    sizeof(vir.vir_info.vir_name));
488			}
489			/* get the user id who started the vm */
490			vir.vir_uid = vm->vm_uid;
491			vir.vir_gid = vm->vm_params.vmc_owner.gid;
492		}
493		if (proc_compose_imsg(ps, PROC_CONTROL, -1, imsg->hdr.type,
494		    imsg->hdr.peerid, -1, &vir, sizeof(vir)) == -1) {
495			log_debug("%s: GET_INFO_VM failed for vm %d, removing",
496			    __func__, vm->vm_vmid);
497			vm_remove(vm, __func__);
498			return (-1);
499		}
500		break;
501	case IMSG_VMDOP_GET_INFO_VM_END_DATA:
502		/*
503		 * PROC_VMM has responded with the *running* VMs, now we
504		 * append the others. These use the special value 0 for their
505		 * kernel id to indicate that they are not running.
506		 */
507		TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
508			if (!vm->vm_running) {
509				memset(&vir, 0, sizeof(vir));
510				vir.vir_info.vir_id = vm->vm_vmid;
511				strlcpy(vir.vir_info.vir_name,
512				    vm->vm_params.vmc_params.vcp_name,
513				    VMM_MAX_NAME_LEN);
514				vir.vir_info.vir_memory_size =
515				    vm->vm_params.vmc_params.
516				    vcp_memranges[0].vmr_size;
517				vir.vir_info.vir_ncpus =
518				    vm->vm_params.vmc_params.vcp_ncpus;
519				/* get the configured user id for this vm */
520				vir.vir_uid = vm->vm_params.vmc_owner.uid;
521				vir.vir_gid = vm->vm_params.vmc_owner.gid;
522				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
523				    IMSG_VMDOP_GET_INFO_VM_DATA,
524				    imsg->hdr.peerid, -1, &vir,
525				    sizeof(vir)) == -1) {
526					log_debug("%s: GET_INFO_VM_END failed",
527					    __func__);
528					vm_remove(vm, __func__);
529					return (-1);
530				}
531			}
532		}
533		IMSG_SIZE_CHECK(imsg, &res);
534		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
535		break;
536	default:
537		return (-1);
538	}
539
540	return (0);
541}
542
543int
544vmd_check_vmh(struct vm_dump_header *vmh)
545{
546	int i;
547	unsigned int code, leaf;
548	unsigned int a, b, c, d;
549
550
551	if (vmh->vmh_version != VM_DUMP_VERSION) {
552		log_warnx("%s: incompatible dump version", __func__);
553		return (-1);
554	}
555
556	for (i = 0; i < VM_DUMP_HEADER_CPUID_COUNT; i++) {
557		code = vmh->vmh_cpuids[i].code;
558		leaf = vmh->vmh_cpuids[i].leaf;
559		if (leaf != 0x00) {
560			log_debug("%s: invalid leaf 0x%x for code 0x%x",
561			    __func__, leaf, code);
562			return (-1);
563		}
564
565		switch (code) {
566		case 0x00:
567			CPUID_LEAF(code, leaf, a, b, c, d);
568			if (vmh->vmh_cpuids[i].a > a) {
569				log_debug("%s: incompatible cpuid level",
570				    __func__);
571				return (-1);
572			}
573			if (!(vmh->vmh_cpuids[i].b == b &&
574			    vmh->vmh_cpuids[i].c == c &&
575			    vmh->vmh_cpuids[i].d == d)) {
576				log_debug("%s: incompatible cpu brand",
577				    __func__);
578				return (-1);
579			}
580			break;
581
582		case 0x01:
583			CPUID_LEAF(code, leaf, a, b, c, d);
584			if ((vmh->vmh_cpuids[i].c & c & VMM_CPUIDECX_MASK) !=
585			    (vmh->vmh_cpuids[i].c & VMM_CPUIDECX_MASK)) {
586				log_debug("%s: incompatible cpu features "
587				    "code: 0x%x leaf: 0x%x  reg: c", __func__,
588				    code, leaf);
589				return (-1);
590			}
591			if ((vmh->vmh_cpuids[i].d & d & VMM_CPUIDEDX_MASK) !=
592			    (vmh->vmh_cpuids[i].d & VMM_CPUIDEDX_MASK)) {
593				log_debug("%s: incompatible cpu features "
594				    "code: 0x%x leaf: 0x%x  reg: d", __func__,
595				    code, leaf);
596				return (-1);
597			}
598			break;
599
600		case 0x07:
601			CPUID_LEAF(code, leaf, a, b, c, d);
602			if ((vmh->vmh_cpuids[i].b & b & VMM_SEFF0EBX_MASK) !=
603			    (vmh->vmh_cpuids[i].b & VMM_SEFF0EBX_MASK)) {
604				log_debug("%s: incompatible cpu features "
605				    "code: 0x%x leaf: 0x%x  reg: c", __func__,
606				    code, leaf);
607				return (-1);
608			}
609			if ((vmh->vmh_cpuids[i].c & c & VMM_SEFF0ECX_MASK) !=
610			    (vmh->vmh_cpuids[i].c & VMM_SEFF0ECX_MASK)) {
611				log_debug("%s: incompatible cpu features "
612				    "code: 0x%x leaf: 0x%x  reg: d", __func__,
613				    code, leaf);
614				return (-1);
615			}
616			break;
617
618		case 0x0d:
619			CPUID_LEAF(code, leaf, a, b, c, d);
620			if (vmh->vmh_cpuids[i].b > b) {
621				log_debug("%s: incompatible cpu: insufficient "
622				    "max save area for enabled XCR0 features",
623				    __func__);
624				return (-1);
625			}
626			if (vmh->vmh_cpuids[i].c > c) {
627				log_debug("%s: incompatible cpu: insufficient "
628				    "max save area for supported XCR0 features",
629				    __func__);
630				return (-1);
631			}
632			break;
633
634		case 0x80000001:
635			CPUID_LEAF(code, leaf, a, b, c, d);
636			if ((vmh->vmh_cpuids[i].a & a) !=
637			    vmh->vmh_cpuids[i].a) {
638				log_debug("%s: incompatible cpu features "
639				    "code: 0x%x leaf: 0x%x  reg: a", __func__,
640				    code, leaf);
641				return (-1);
642			}
643			if ((vmh->vmh_cpuids[i].c & c) !=
644			    vmh->vmh_cpuids[i].c) {
645				log_debug("%s: incompatible cpu features "
646				    "code: 0x%x leaf: 0x%x  reg: c", __func__,
647				    code, leaf);
648				return (-1);
649			}
650			if ((vmh->vmh_cpuids[i].d & d) !=
651			    vmh->vmh_cpuids[i].d) {
652				log_debug("%s: incompatible cpu features "
653				    "code: 0x%x leaf: 0x%x  reg: d", __func__,
654				    code, leaf);
655				return (-1);
656			}
657			break;
658
659		default:
660			log_debug("%s: unknown code 0x%x", __func__, code);
661			return (-1);
662		}
663	}
664
665	return (0);
666}
667
668void
669vmd_sighdlr(int sig, short event, void *arg)
670{
671	if (privsep_process != PROC_PARENT)
672		return;
673	log_debug("%s: handling signal", __func__);
674
675	switch (sig) {
676	case SIGHUP:
677		log_info("%s: reload requested with SIGHUP", __func__);
678
679		/*
680		 * This is safe because libevent uses async signal handlers
681		 * that run in the event loop and not in signal context.
682		 */
683		(void)vmd_reload(0, NULL);
684		break;
685	case SIGPIPE:
686		log_info("%s: ignoring SIGPIPE", __func__);
687		break;
688	case SIGUSR1:
689		log_info("%s: ignoring SIGUSR1", __func__);
690		break;
691	case SIGTERM:
692	case SIGINT:
693		vmd_shutdown();
694		break;
695	default:
696		fatalx("unexpected signal");
697	}
698}
699
700__dead void
701usage(void)
702{
703	extern char *__progname;
704	fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
705	    __progname);
706	exit(1);
707}
708
709int
710main(int argc, char **argv)
711{
712	struct privsep		*ps;
713	int			 ch;
714	const char		*conffile = VMD_CONF;
715	enum privsep_procid	 proc_id = PROC_PARENT;
716	int			 proc_instance = 0;
717	const char		*errp, *title = NULL;
718	int			 argc0 = argc;
719
720	log_init(0, LOG_DAEMON);
721
722	if ((env = calloc(1, sizeof(*env))) == NULL)
723		fatal("calloc: env");
724
725	while ((ch = getopt(argc, argv, "D:P:I:df:vn")) != -1) {
726		switch (ch) {
727		case 'D':
728			if (cmdline_symset(optarg) < 0)
729				log_warnx("could not parse macro definition %s",
730				    optarg);
731			break;
732		case 'd':
733			env->vmd_debug = 2;
734			break;
735		case 'f':
736			conffile = optarg;
737			break;
738		case 'v':
739			env->vmd_verbose++;
740			break;
741		case 'n':
742			env->vmd_noaction = 1;
743			break;
744		case 'P':
745			title = optarg;
746			proc_id = proc_getid(procs, nitems(procs), title);
747			if (proc_id == PROC_MAX)
748				fatalx("invalid process name");
749			break;
750		case 'I':
751			proc_instance = strtonum(optarg, 0,
752			    PROC_MAX_INSTANCES, &errp);
753			if (errp)
754				fatalx("invalid process instance");
755			break;
756		default:
757			usage();
758		}
759	}
760
761	argc -= optind;
762	if (argc > 0)
763		usage();
764
765	if (env->vmd_noaction && !env->vmd_debug)
766		env->vmd_debug = 1;
767
768	/* check for root privileges */
769	if (env->vmd_noaction == 0) {
770		if (geteuid())
771			fatalx("need root privileges");
772	}
773
774	ps = &env->vmd_ps;
775	ps->ps_env = env;
776	env->vmd_fd = -1;
777
778	if (config_init(env) == -1)
779		fatal("failed to initialize configuration");
780
781	if ((ps->ps_pw = getpwnam(VMD_USER)) == NULL)
782		fatal("unknown user %s", VMD_USER);
783
784	/* First proc runs as root without pledge but in default chroot */
785	proc_priv->p_pw = &proc_privpw; /* initialized to all 0 */
786	proc_priv->p_chroot = ps->ps_pw->pw_dir; /* from VMD_USER */
787
788	/* Open /dev/vmm */
789	if (env->vmd_noaction == 0) {
790		env->vmd_fd = open(VMM_NODE, O_RDWR);
791		if (env->vmd_fd == -1)
792			fatal("%s", VMM_NODE);
793	}
794
795	/* Configure the control socket */
796	ps->ps_csock.cs_name = SOCKET_NAME;
797	TAILQ_INIT(&ps->ps_rcsocks);
798
799	/* Configuration will be parsed after forking the children */
800	env->vmd_conffile = conffile;
801
802	log_init(env->vmd_debug, LOG_DAEMON);
803	log_setverbose(env->vmd_verbose);
804
805	if (env->vmd_noaction)
806		ps->ps_noaction = 1;
807	ps->ps_instance = proc_instance;
808	if (title != NULL)
809		ps->ps_title[proc_id] = title;
810
811	/* only the parent returns */
812	proc_init(ps, procs, nitems(procs), env->vmd_debug, argc0, argv,
813	    proc_id);
814
815	log_procinit("parent");
816	if (!env->vmd_debug && daemon(0, 0) == -1)
817		fatal("can't daemonize");
818
819	if (ps->ps_noaction == 0)
820		log_info("startup");
821
822	event_init();
823
824	signal_set(&ps->ps_evsigint, SIGINT, vmd_sighdlr, ps);
825	signal_set(&ps->ps_evsigterm, SIGTERM, vmd_sighdlr, ps);
826	signal_set(&ps->ps_evsighup, SIGHUP, vmd_sighdlr, ps);
827	signal_set(&ps->ps_evsigpipe, SIGPIPE, vmd_sighdlr, ps);
828	signal_set(&ps->ps_evsigusr1, SIGUSR1, vmd_sighdlr, ps);
829
830	signal_add(&ps->ps_evsigint, NULL);
831	signal_add(&ps->ps_evsigterm, NULL);
832	signal_add(&ps->ps_evsighup, NULL);
833	signal_add(&ps->ps_evsigpipe, NULL);
834	signal_add(&ps->ps_evsigusr1, NULL);
835
836	if (!env->vmd_noaction)
837		proc_connect(ps);
838
839	if (vmd_configure() == -1)
840		fatalx("configuration failed");
841
842	event_dispatch();
843
844	log_debug("parent exiting");
845
846	return (0);
847}
848
849int
850vmd_configure(void)
851{
852	struct vmd_vm		*vm;
853	struct vmd_switch	*vsw;
854
855	if ((env->vmd_ptmfd = open(PATH_PTMDEV, O_RDWR|O_CLOEXEC)) == -1)
856		fatal("open %s", PATH_PTMDEV);
857
858	/*
859	 * pledge in the parent process:
860	 * stdio - for malloc and basic I/O including events.
861	 * rpath - for reload to open and read the configuration files.
862	 * wpath - for opening disk images and tap devices.
863	 * tty - for openpty and TIOCUCNTL.
864	 * proc - run kill to terminate its children safely.
865	 * sendfd - for disks, interfaces and other fds.
866	 * recvfd - for send and receive.
867	 * getpw - lookup user or group id by name.
868	 * chown, fattr - change tty ownership
869	 * flock - locking disk files
870	 */
871	if (pledge("stdio rpath wpath proc tty recvfd sendfd getpw"
872	    " chown fattr flock", NULL) == -1)
873		fatal("pledge");
874
875	if (parse_config(env->vmd_conffile) == -1) {
876		proc_kill(&env->vmd_ps);
877		exit(1);
878	}
879
880	if (env->vmd_noaction) {
881		fprintf(stderr, "configuration OK\n");
882		proc_kill(&env->vmd_ps);
883		exit(0);
884	}
885
886	/* Send shared global configuration to all children */
887	if (config_setconfig(env) == -1)
888		return (-1);
889
890	TAILQ_FOREACH(vsw, env->vmd_switches, sw_entry) {
891		if (vsw->sw_running)
892			continue;
893		if (vm_priv_brconfig(&env->vmd_ps, vsw) == -1) {
894			log_warn("%s: failed to create switch %s",
895			    __func__, vsw->sw_name);
896			switch_remove(vsw);
897			return (-1);
898		}
899	}
900
901	TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
902		if (vm->vm_disabled) {
903			log_debug("%s: not creating vm %s (disabled)",
904			    __func__,
905			    vm->vm_params.vmc_params.vcp_name);
906			continue;
907		}
908		if (config_setvm(&env->vmd_ps, vm,
909		    -1, vm->vm_params.vmc_owner.uid) == -1)
910			return (-1);
911	}
912
913	return (0);
914}
915
916int
917vmd_reload(unsigned int reset, const char *filename)
918{
919	struct vmd_vm		*vm, *next_vm;
920	struct vmd_switch	*vsw;
921	int			 reload = 0;
922
923	/* Switch back to the default config file */
924	if (filename == NULL || *filename == '\0') {
925		filename = env->vmd_conffile;
926		reload = 1;
927	}
928
929	log_debug("%s: level %d config file %s", __func__, reset, filename);
930
931	if (reset) {
932		/* Purge the configuration */
933		config_purge(env, reset);
934		config_setreset(env, reset);
935	} else {
936		/*
937		 * Load or reload the configuration.
938		 *
939		 * Reloading removes all non-running VMs before processing the
940		 * config file, whereas loading only adds to the existing list
941		 * of VMs.
942		 */
943
944		if (reload) {
945			TAILQ_FOREACH_SAFE(vm, env->vmd_vms, vm_entry,
946			    next_vm) {
947				if (vm->vm_running == 0) {
948					DPRINTF("%s: calling vm_remove",
949					    __func__);
950					vm_remove(vm, __func__);
951				}
952			}
953		}
954
955		if (parse_config(filename) == -1) {
956			log_debug("%s: failed to load config file %s",
957			    __func__, filename);
958			return (-1);
959		}
960
961		if (reload) {
962			/* Update shared global configuration in all children */
963			if (config_setconfig(env) == -1)
964				return (-1);
965		}
966
967		TAILQ_FOREACH(vsw, env->vmd_switches, sw_entry) {
968			if (vsw->sw_running)
969				continue;
970			if (vm_priv_brconfig(&env->vmd_ps, vsw) == -1) {
971				log_warn("%s: failed to create switch %s",
972				    __func__, vsw->sw_name);
973				switch_remove(vsw);
974				return (-1);
975			}
976		}
977
978		TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
979			if (vm->vm_running == 0) {
980				if (vm->vm_disabled) {
981					log_debug("%s: not creating vm %s"
982					    " (disabled)", __func__,
983					    vm->vm_params.vmc_params.vcp_name);
984					continue;
985				}
986				if (config_setvm(&env->vmd_ps, vm,
987				    -1, vm->vm_params.vmc_owner.uid) == -1)
988					return (-1);
989			} else {
990				log_debug("%s: not creating vm \"%s\": "
991				    "(running)", __func__,
992				    vm->vm_params.vmc_params.vcp_name);
993			}
994		}
995	}
996
997	return (0);
998}
999
1000void
1001vmd_shutdown(void)
1002{
1003	struct vmd_vm *vm, *vm_next;
1004
1005	log_debug("%s: performing shutdown", __func__);
1006
1007	TAILQ_FOREACH_SAFE(vm, env->vmd_vms, vm_entry, vm_next) {
1008		vm_remove(vm, __func__);
1009	}
1010
1011	proc_kill(&env->vmd_ps);
1012	free(env);
1013
1014	log_warnx("parent terminating");
1015	exit(0);
1016}
1017
1018struct vmd_vm *
1019vm_getbyvmid(uint32_t vmid)
1020{
1021	struct vmd_vm	*vm;
1022
1023	if (vmid == 0)
1024		return (NULL);
1025	TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
1026		if (vm->vm_vmid == vmid)
1027			return (vm);
1028	}
1029
1030	return (NULL);
1031}
1032
1033struct vmd_vm *
1034vm_getbyid(uint32_t id)
1035{
1036	struct vmd_vm	*vm;
1037
1038	if (id == 0)
1039		return (NULL);
1040	TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
1041		if (vm->vm_params.vmc_params.vcp_id == id)
1042			return (vm);
1043	}
1044
1045	return (NULL);
1046}
1047
1048uint32_t
1049vm_id2vmid(uint32_t id, struct vmd_vm *vm)
1050{
1051	if (vm == NULL && (vm = vm_getbyid(id)) == NULL)
1052		return (0);
1053	DPRINTF("%s: vmm id %u is vmid %u", __func__,
1054	    id, vm->vm_vmid);
1055	return (vm->vm_vmid);
1056}
1057
1058uint32_t
1059vm_vmid2id(uint32_t vmid, struct vmd_vm *vm)
1060{
1061	if (vm == NULL && (vm = vm_getbyvmid(vmid)) == NULL)
1062		return (0);
1063	DPRINTF("%s: vmid %u is vmm id %u", __func__,
1064	    vmid, vm->vm_params.vmc_params.vcp_id);
1065	return (vm->vm_params.vmc_params.vcp_id);
1066}
1067
1068struct vmd_vm *
1069vm_getbyname(const char *name)
1070{
1071	struct vmd_vm	*vm;
1072
1073	if (name == NULL)
1074		return (NULL);
1075	TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
1076		if (strcmp(vm->vm_params.vmc_params.vcp_name, name) == 0)
1077			return (vm);
1078	}
1079
1080	return (NULL);
1081}
1082
1083struct vmd_vm *
1084vm_getbypid(pid_t pid)
1085{
1086	struct vmd_vm	*vm;
1087
1088	TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) {
1089		if (vm->vm_pid == pid)
1090			return (vm);
1091	}
1092
1093	return (NULL);
1094}
1095
1096void
1097vm_stop(struct vmd_vm *vm, int keeptty, const char *caller)
1098{
1099	struct privsep	*ps = &env->vmd_ps;
1100	unsigned int	 i;
1101
1102	if (vm == NULL)
1103		return;
1104
1105	log_debug("%s: %s %s stopping vm %d%s",
1106	    __func__, ps->ps_title[privsep_process], caller,
1107	    vm->vm_vmid, keeptty ? ", keeping tty open" : "");
1108
1109	vm->vm_running = 0;
1110	vm->vm_shutdown = 0;
1111
1112	user_inc(&vm->vm_params.vmc_params, vm->vm_user, 0);
1113	user_put(vm->vm_user);
1114
1115	if (vm->vm_iev.ibuf.fd != -1) {
1116		event_del(&vm->vm_iev.ev);
1117		close(vm->vm_iev.ibuf.fd);
1118	}
1119	for (i = 0; i < VMM_MAX_DISKS_PER_VM; i++) {
1120		if (vm->vm_disks[i] != -1) {
1121			close(vm->vm_disks[i]);
1122			vm->vm_disks[i] = -1;
1123		}
1124	}
1125	for (i = 0; i < VMM_MAX_NICS_PER_VM; i++) {
1126		if (vm->vm_ifs[i].vif_fd != -1) {
1127			close(vm->vm_ifs[i].vif_fd);
1128			vm->vm_ifs[i].vif_fd = -1;
1129		}
1130		free(vm->vm_ifs[i].vif_name);
1131		free(vm->vm_ifs[i].vif_switch);
1132		free(vm->vm_ifs[i].vif_group);
1133		vm->vm_ifs[i].vif_name = NULL;
1134		vm->vm_ifs[i].vif_switch = NULL;
1135		vm->vm_ifs[i].vif_group = NULL;
1136	}
1137	if (vm->vm_kernel != -1) {
1138		close(vm->vm_kernel);
1139		vm->vm_kernel = -1;
1140	}
1141	if (vm->vm_cdrom != -1) {
1142		close(vm->vm_cdrom);
1143		vm->vm_cdrom = -1;
1144	}
1145	if (!keeptty) {
1146		vm_closetty(vm);
1147		vm->vm_uid = 0;
1148	}
1149}
1150
1151void
1152vm_remove(struct vmd_vm *vm, const char *caller)
1153{
1154	struct privsep	*ps = &env->vmd_ps;
1155
1156	if (vm == NULL)
1157		return;
1158
1159	log_debug("%s: %s %s removing vm %d from running config",
1160	    __func__, ps->ps_title[privsep_process], caller,
1161	    vm->vm_vmid);
1162
1163	TAILQ_REMOVE(env->vmd_vms, vm, vm_entry);
1164
1165	user_put(vm->vm_user);
1166	vm_stop(vm, 0, caller);
1167	free(vm);
1168}
1169
1170int
1171vm_register(struct privsep *ps, struct vmop_create_params *vmc,
1172    struct vmd_vm **ret_vm, uint32_t id, uid_t uid)
1173{
1174	struct vmd_vm		*vm = NULL, *vm_parent = NULL;
1175	struct vm_create_params	*vcp = &vmc->vmc_params;
1176	struct vmop_owner	*vmo = NULL;
1177	struct vmd_user		*usr = NULL;
1178	uint32_t		 rng;
1179	unsigned int		 i;
1180	struct vmd_switch	*sw;
1181	char			*s;
1182
1183	/* Check if this is an instance of another VM */
1184	if (vm_instance(ps, &vm_parent, vmc, uid) == -1)
1185		return (-1);
1186
1187	errno = 0;
1188	*ret_vm = NULL;
1189
1190	if ((vm = vm_getbyname(vcp->vcp_name)) != NULL ||
1191	    (vm = vm_getbyvmid(vcp->vcp_id)) != NULL) {
1192		if (vm_checkperm(vm, &vm->vm_params.vmc_owner,
1193		    uid) != 0) {
1194			errno = EPERM;
1195			goto fail;
1196		}
1197		*ret_vm = vm;
1198		errno = EALREADY;
1199		goto fail;
1200	}
1201
1202	if (vm_parent != NULL)
1203		vmo = &vm_parent->vm_params.vmc_insowner;
1204
1205	/* non-root users can only start existing VMs or instances */
1206	if (vm_checkperm(NULL, vmo, uid) != 0) {
1207		log_warnx("permission denied");
1208		errno = EPERM;
1209		goto fail;
1210	}
1211	if (vmc->vmc_flags == 0) {
1212		log_warnx("invalid configuration, no devices");
1213		errno = VMD_DISK_MISSING;
1214		goto fail;
1215	}
1216	if (vcp->vcp_ncpus == 0)
1217		vcp->vcp_ncpus = 1;
1218	if (vcp->vcp_memranges[0].vmr_size == 0)
1219		vcp->vcp_memranges[0].vmr_size = VM_DEFAULT_MEMORY;
1220	if (vcp->vcp_ncpus > VMM_MAX_VCPUS_PER_VM) {
1221		log_warnx("invalid number of CPUs");
1222		goto fail;
1223	} else if (vcp->vcp_ndisks > VMM_MAX_DISKS_PER_VM) {
1224		log_warnx("invalid number of disks");
1225		goto fail;
1226	} else if (vcp->vcp_nnics > VMM_MAX_NICS_PER_VM) {
1227		log_warnx("invalid number of interfaces");
1228		goto fail;
1229	} else if (strlen(vcp->vcp_kernel) == 0 &&
1230	    vcp->vcp_ndisks == 0 && strlen(vcp->vcp_cdrom) == 0) {
1231		log_warnx("no kernel or disk/cdrom specified");
1232		goto fail;
1233	} else if (strlen(vcp->vcp_name) == 0) {
1234		log_warnx("invalid VM name");
1235		goto fail;
1236	} else if (*vcp->vcp_name == '-' || *vcp->vcp_name == '.' ||
1237	    *vcp->vcp_name == '_') {
1238		log_warnx("invalid VM name");
1239		goto fail;
1240	} else {
1241		for (s = vcp->vcp_name; *s != '\0'; ++s) {
1242			if (!(isalnum(*s) || *s == '.' || *s == '-' ||
1243			    *s == '_')) {
1244				log_warnx("invalid VM name");
1245				goto fail;
1246			}
1247		}
1248	}
1249
1250	/* track active users */
1251	if (uid != 0 && env->vmd_users != NULL &&
1252	    (usr = user_get(uid)) == NULL) {
1253		log_warnx("could not add user");
1254		goto fail;
1255	}
1256
1257	if ((vm = calloc(1, sizeof(*vm))) == NULL)
1258		goto fail;
1259
1260	memcpy(&vm->vm_params, vmc, sizeof(vm->vm_params));
1261	vmc = &vm->vm_params;
1262	vcp = &vmc->vmc_params;
1263	vm->vm_pid = -1;
1264	vm->vm_tty = -1;
1265	vm->vm_receive_fd = -1;
1266	vm->vm_paused = 0;
1267	vm->vm_user = usr;
1268
1269	for (i = 0; i < VMM_MAX_DISKS_PER_VM; i++)
1270		vm->vm_disks[i] = -1;
1271	for (i = 0; i < VMM_MAX_NICS_PER_VM; i++)
1272		vm->vm_ifs[i].vif_fd = -1;
1273	for (i = 0; i < vcp->vcp_nnics; i++) {
1274		if ((sw = switch_getbyname(vmc->vmc_ifswitch[i])) != NULL) {
1275			/* inherit per-interface flags from the switch */
1276			vmc->vmc_ifflags[i] |= (sw->sw_flags & VMIFF_OPTMASK);
1277		}
1278
1279		/*
1280		 * If the MAC address is zero, always randomize it in vmd(8)
1281		 * because we cannot rely on the guest OS to do the right
1282		 * thing like OpenBSD does.  Based on ether_fakeaddr()
1283		 * from the kernel, incremented by one to differentiate
1284		 * the source.
1285		 */
1286		if (memcmp(zero_mac, &vcp->vcp_macs[i], ETHER_ADDR_LEN) == 0) {
1287			rng = arc4random();
1288			vcp->vcp_macs[i][0] = 0xfe;
1289			vcp->vcp_macs[i][1] = 0xe1;
1290			vcp->vcp_macs[i][2] = 0xba + 1;
1291			vcp->vcp_macs[i][3] = 0xd0 | ((i + 1) & 0xf);
1292			vcp->vcp_macs[i][4] = rng;
1293			vcp->vcp_macs[i][5] = rng >> 8;
1294		}
1295	}
1296	vm->vm_kernel = -1;
1297	vm->vm_cdrom = -1;
1298	vm->vm_iev.ibuf.fd = -1;
1299
1300	if (++env->vmd_nvm == 0)
1301		fatalx("too many vms");
1302
1303	/* Assign a new internal Id if not specified */
1304	vm->vm_vmid = id == 0 ? env->vmd_nvm : id;
1305
1306	log_debug("%s: registering vm %d", __func__, vm->vm_vmid);
1307	TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry);
1308
1309	*ret_vm = vm;
1310	return (0);
1311 fail:
1312	if (errno == 0)
1313		errno = EINVAL;
1314	return (-1);
1315}
1316
1317int
1318vm_instance(struct privsep *ps, struct vmd_vm **vm_parent,
1319    struct vmop_create_params *vmc, uid_t uid)
1320{
1321	char			*name;
1322	struct vm_create_params	*vcp = &vmc->vmc_params;
1323	struct vmop_create_params *vmcp;
1324	struct vm_create_params	*vcpp;
1325	struct vmd_vm		*vm = NULL;
1326	unsigned int		 i, j;
1327	uint32_t		 id;
1328
1329	/* return without error if the parent is NULL (nothing to inherit) */
1330	if ((vmc->vmc_flags & VMOP_CREATE_INSTANCE) == 0 ||
1331	    (*vm_parent = vm_getbyname(vmc->vmc_instance)) == NULL)
1332		return (0);
1333
1334	errno = 0;
1335	vmcp = &(*vm_parent)->vm_params;
1336	vcpp = &vmcp->vmc_params;
1337
1338	/* Are we allowed to create an instance from this VM? */
1339	if (vm_checkperm(NULL, &vmcp->vmc_insowner, uid) != 0) {
1340		log_warnx("vm \"%s\" no permission to create vm instance",
1341		    vcpp->vcp_name);
1342		errno = ENAMETOOLONG;
1343		return (-1);
1344	}
1345
1346	id = vcp->vcp_id;
1347	name = vcp->vcp_name;
1348
1349	if ((vm = vm_getbyname(vcp->vcp_name)) != NULL ||
1350	    (vm = vm_getbyvmid(vcp->vcp_id)) != NULL) {
1351		errno = EPROCLIM;
1352		return (-1);
1353	}
1354
1355	/* CPU */
1356	if (vcp->vcp_ncpus == 0)
1357		vcp->vcp_ncpus = vcpp->vcp_ncpus;
1358	if (vm_checkinsflag(vmcp, VMOP_CREATE_CPU, uid) != 0 &&
1359	    vcp->vcp_ncpus != vcpp->vcp_ncpus) {
1360		log_warnx("vm \"%s\" no permission to set cpus", name);
1361		errno = EPERM;
1362		return (-1);
1363	}
1364
1365	/* memory */
1366	if (vcp->vcp_memranges[0].vmr_size == 0)
1367		vcp->vcp_memranges[0].vmr_size =
1368		    vcpp->vcp_memranges[0].vmr_size;
1369	if (vm_checkinsflag(vmcp, VMOP_CREATE_MEMORY, uid) != 0 &&
1370	    vcp->vcp_memranges[0].vmr_size !=
1371	    vcpp->vcp_memranges[0].vmr_size) {
1372		log_warnx("vm \"%s\" no permission to set memory", name);
1373		errno = EPERM;
1374		return (-1);
1375	}
1376
1377	/* disks cannot be inherited */
1378	if (vm_checkinsflag(vmcp, VMOP_CREATE_DISK, uid) != 0 &&
1379	    vcp->vcp_ndisks) {
1380		log_warnx("vm \"%s\" no permission to set disks", name);
1381		errno = EPERM;
1382		return (-1);
1383	}
1384	for (i = 0; i < vcp->vcp_ndisks; i++) {
1385		/* Check if this disk is already used in the parent */
1386		for (j = 0; j < vcpp->vcp_ndisks; j++) {
1387			if (strcmp(vcp->vcp_disks[i],
1388			    vcpp->vcp_disks[j]) == 0) {
1389				log_warnx("vm \"%s\" disk %s cannot be reused",
1390				    name, vcp->vcp_disks[i]);
1391				errno = EBUSY;
1392				return (-1);
1393			}
1394		}
1395		vmc->vmc_checkaccess |= VMOP_CREATE_DISK;
1396	}
1397
1398	/* interfaces */
1399	if (vcp->vcp_nnics > 0 &&
1400	    vm_checkinsflag(vmcp, VMOP_CREATE_NETWORK, uid) != 0 &&
1401	    vcp->vcp_nnics != vcpp->vcp_nnics) {
1402		log_warnx("vm \"%s\" no permission to set interfaces", name);
1403		errno = EPERM;
1404		return (-1);
1405	}
1406	for (i = 0; i < vcpp->vcp_nnics; i++) {
1407		/* Interface got overwritten */
1408		if (i < vcp->vcp_nnics)
1409			continue;
1410
1411		/* Copy interface from parent */
1412		vmc->vmc_ifflags[i] = vmcp->vmc_ifflags[i];
1413		(void)strlcpy(vmc->vmc_ifnames[i], vmcp->vmc_ifnames[i],
1414		    sizeof(vmc->vmc_ifnames[i]));
1415		(void)strlcpy(vmc->vmc_ifswitch[i], vmcp->vmc_ifswitch[i],
1416		    sizeof(vmc->vmc_ifswitch[i]));
1417		(void)strlcpy(vmc->vmc_ifgroup[i], vmcp->vmc_ifgroup[i],
1418		    sizeof(vmc->vmc_ifgroup[i]));
1419		memcpy(vcp->vcp_macs[i], vcpp->vcp_macs[i],
1420		    sizeof(vcp->vcp_macs[i]));
1421		vmc->vmc_ifrdomain[i] = vmcp->vmc_ifrdomain[i];
1422		vcp->vcp_nnics++;
1423	}
1424	for (i = 0; i < vcp->vcp_nnics; i++) {
1425		for (j = 0; j < vcpp->vcp_nnics; j++) {
1426			if (memcmp(zero_mac, vcp->vcp_macs[i],
1427			    sizeof(vcp->vcp_macs[i])) != 0 &&
1428			    memcmp(vcpp->vcp_macs[i], vcp->vcp_macs[i],
1429			    sizeof(vcp->vcp_macs[i])) != 0) {
1430				log_warnx("vm \"%s\" lladdr cannot be reused",
1431				    name);
1432				errno = EBUSY;
1433				return (-1);
1434			}
1435			if (strlen(vmc->vmc_ifnames[i]) &&
1436			    strcmp(vmc->vmc_ifnames[i],
1437			    vmcp->vmc_ifnames[j]) == 0) {
1438				log_warnx("vm \"%s\" %s cannot be reused",
1439				    vmc->vmc_ifnames[i], name);
1440				errno = EBUSY;
1441				return (-1);
1442			}
1443		}
1444	}
1445
1446	/* kernel */
1447	if (strlen(vcp->vcp_kernel) > 0) {
1448		if (vm_checkinsflag(vmcp, VMOP_CREATE_KERNEL, uid) != 0) {
1449			log_warnx("vm \"%s\" no permission to set boot image",
1450			    name);
1451			errno = EPERM;
1452			return (-1);
1453		}
1454		vmc->vmc_checkaccess |= VMOP_CREATE_KERNEL;
1455	} else if (strlcpy(vcp->vcp_kernel, vcpp->vcp_kernel,
1456	    sizeof(vcp->vcp_kernel)) >= sizeof(vcp->vcp_kernel)) {
1457		log_warnx("vm \"%s\" kernel name too long", name);
1458		errno = EINVAL;
1459		return (-1);
1460	}
1461
1462	/* cdrom */
1463	if (strlen(vcp->vcp_cdrom) > 0) {
1464		if (vm_checkinsflag(vmcp, VMOP_CREATE_CDROM, uid) != 0) {
1465			log_warnx("vm \"%s\" no permission to set cdrom", name);
1466			errno = EPERM;
1467			return (-1);
1468		}
1469		vmc->vmc_checkaccess |= VMOP_CREATE_CDROM;
1470	} else if (strlcpy(vcp->vcp_cdrom, vcpp->vcp_cdrom,
1471	    sizeof(vcp->vcp_cdrom)) >= sizeof(vcp->vcp_cdrom)) {
1472		log_warnx("vm \"%s\" cdrom name too long", name);
1473		errno = EINVAL;
1474		return (-1);
1475	}
1476
1477	/* user */
1478	if (vmc->vmc_owner.uid == 0)
1479		vmc->vmc_owner.uid = vmcp->vmc_owner.uid;
1480	else if (vmc->vmc_owner.uid != uid &&
1481	    vmc->vmc_owner.uid != vmcp->vmc_owner.uid) {
1482		log_warnx("vm \"%s\" user mismatch", name);
1483		errno = EPERM;
1484		return (-1);
1485	}
1486
1487	/* group */
1488	if (vmc->vmc_owner.gid == 0)
1489		vmc->vmc_owner.gid = vmcp->vmc_owner.gid;
1490	else if (vmc->vmc_owner.gid != vmcp->vmc_owner.gid) {
1491		log_warnx("vm \"%s\" group mismatch", name);
1492		errno = EPERM;
1493		return (-1);
1494	}
1495
1496	/* child instances */
1497	if (vmc->vmc_insflags) {
1498		log_warnx("vm \"%s\" cannot change instance permissions", name);
1499		errno = EPERM;
1500		return (-1);
1501	}
1502	if (vmcp->vmc_insflags & VMOP_CREATE_INSTANCE) {
1503		vmc->vmc_insowner.gid = vmcp->vmc_insowner.gid;
1504		vmc->vmc_insowner.uid = vmcp->vmc_insowner.gid;
1505		vmc->vmc_insflags = vmcp->vmc_insflags;
1506	} else {
1507		vmc->vmc_insowner.gid = 0;
1508		vmc->vmc_insowner.uid = 0;
1509		vmc->vmc_insflags = 0;
1510	}
1511
1512	/* finished, remove instance flags */
1513	vmc->vmc_flags &= ~VMOP_CREATE_INSTANCE;
1514
1515	return (0);
1516}
1517
1518/*
1519 * vm_checkperm
1520 *
1521 * Checks if the user represented by the 'uid' parameter is allowed to
1522 * manipulate the VM described by the 'vm' parameter (or connect to said VM's
1523 * console.)
1524 *
1525 * Parameters:
1526 *  vm: the VM whose permission is to be checked
1527 *  vmo: the required uid/gid to be checked
1528 *  uid: the user ID of the user making the request
1529 *
1530 * Return values:
1531 *   0: the permission should be granted
1532 *  -1: the permission check failed (also returned if vm == null)
1533 */
1534int
1535vm_checkperm(struct vmd_vm *vm, struct vmop_owner *vmo, uid_t uid)
1536{
1537	struct group	*gr;
1538	struct passwd	*pw;
1539	char		**grmem;
1540
1541	/* root has no restrictions */
1542	if (uid == 0)
1543		return (0);
1544
1545	if (vmo == NULL)
1546		return (-1);
1547
1548	/* check user */
1549	if (vm == NULL) {
1550		if  (vmo->uid == uid)
1551			return (0);
1552	} else {
1553		/*
1554		 * check user of running vm (the owner of a running vm can
1555		 * be different to (or more specific than) the configured owner.
1556		 */
1557		if ((vm->vm_running && vm->vm_uid == uid) ||
1558		    (!vm->vm_running && vmo->uid == uid))
1559			return (0);
1560	}
1561
1562	/* check groups */
1563	if (vmo->gid != -1) {
1564		if ((pw = getpwuid(uid)) == NULL)
1565			return (-1);
1566		if (pw->pw_gid == vmo->gid)
1567			return (0);
1568		if ((gr = getgrgid(vmo->gid)) != NULL) {
1569			for (grmem = gr->gr_mem; *grmem; grmem++)
1570				if (strcmp(*grmem, pw->pw_name) == 0)
1571					return (0);
1572		}
1573	}
1574
1575	return (-1);
1576}
1577
1578/*
1579 * vm_checkinsflag
1580 *
1581 * Checks wheter the non-root user is allowed to set an instance option.
1582 *
1583 * Parameters:
1584 *  vmc: the VM create parameters
1585 *  flag: the flag to be checked
1586 *  uid: the user ID of the user making the request
1587 *
1588 * Return values:
1589 *   0: the permission should be granted
1590 *  -1: the permission check failed (also returned if vm == null)
1591 */
1592int
1593vm_checkinsflag(struct vmop_create_params *vmc, unsigned int flag, uid_t uid)
1594{
1595	/* root has no restrictions */
1596	if (uid == 0)
1597		return (0);
1598
1599	if ((vmc->vmc_insflags & flag) == 0)
1600		return (-1);
1601
1602	return (0);
1603}
1604
1605/*
1606 * vm_checkaccess
1607 *
1608 * Checks if the user represented by the 'uid' parameter is allowed to
1609 * access the file described by the 'path' parameter.
1610 *
1611 * Parameters:
1612 *  fd: the file descriptor of the opened file
1613 *  uflag: check if the userid has access to the file
1614 *  uid: the user ID of the user making the request
1615 *  amode: the access flags of R_OK and W_OK
1616 *
1617 * Return values:
1618 *   0: the permission should be granted
1619 *  -1: the permission check failed
1620 */
1621int
1622vm_checkaccess(int fd, unsigned int uflag, uid_t uid, int amode)
1623{
1624	struct group	*gr;
1625	struct passwd	*pw;
1626	char		**grmem;
1627	struct stat	 st;
1628	mode_t		 mode;
1629
1630	if (fd == -1)
1631		return (-1);
1632
1633	/*
1634	 * File has to be accessible and a regular file
1635	 */
1636	if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode))
1637		return (-1);
1638
1639	/* root has no restrictions */
1640	if (uid == 0 || uflag == 0)
1641		return (0);
1642
1643	/* check other */
1644	mode = amode & W_OK ? S_IWOTH : 0;
1645	mode |= amode & R_OK ? S_IROTH : 0;
1646	if ((st.st_mode & mode) == mode)
1647		return (0);
1648
1649	/* check user */
1650	mode = amode & W_OK ? S_IWUSR : 0;
1651	mode |= amode & R_OK ? S_IRUSR : 0;
1652	if (uid == st.st_uid && (st.st_mode & mode) == mode)
1653		return (0);
1654
1655	/* check groups */
1656	mode = amode & W_OK ? S_IWGRP : 0;
1657	mode |= amode & R_OK ? S_IRGRP : 0;
1658	if ((st.st_mode & mode) != mode)
1659		return (-1);
1660	if ((pw = getpwuid(uid)) == NULL)
1661		return (-1);
1662	if (pw->pw_gid == st.st_gid)
1663		return (0);
1664	if ((gr = getgrgid(st.st_gid)) != NULL) {
1665		for (grmem = gr->gr_mem; *grmem; grmem++)
1666			if (strcmp(*grmem, pw->pw_name) == 0)
1667				return (0);
1668	}
1669
1670	return (-1);
1671}
1672
1673int
1674vm_opentty(struct vmd_vm *vm)
1675{
1676	struct ptmget		 ptm;
1677	struct stat		 st;
1678	struct group		*gr;
1679	uid_t			 uid;
1680	gid_t			 gid;
1681	mode_t			 mode;
1682	int			 on;
1683
1684	/*
1685	 * Open tty with pre-opened PTM fd
1686	 */
1687	if ((ioctl(env->vmd_ptmfd, PTMGET, &ptm) == -1))
1688		return (-1);
1689
1690	/*
1691	 * We use user ioctl(2) mode to pass break commands.
1692	 */
1693	on = 1;
1694	if (ioctl(ptm.cfd, TIOCUCNTL, &on))
1695		fatal("could not enable user ioctl mode");
1696
1697	vm->vm_tty = ptm.cfd;
1698	close(ptm.sfd);
1699	if ((vm->vm_ttyname = strdup(ptm.sn)) == NULL)
1700		goto fail;
1701
1702	uid = vm->vm_uid;
1703	gid = vm->vm_params.vmc_owner.gid;
1704
1705	if (vm->vm_params.vmc_owner.gid != -1) {
1706		mode = 0660;
1707	} else if ((gr = getgrnam("tty")) != NULL) {
1708		gid = gr->gr_gid;
1709		mode = 0620;
1710	} else {
1711		mode = 0600;
1712		gid = 0;
1713	}
1714
1715	log_debug("%s: vm %s tty %s uid %d gid %d mode %o",
1716	    __func__, vm->vm_params.vmc_params.vcp_name,
1717	    vm->vm_ttyname, uid, gid, mode);
1718
1719	/*
1720	 * Change ownership and mode of the tty as required.
1721	 * Loosely based on the implementation of sshpty.c
1722	 */
1723	if (stat(vm->vm_ttyname, &st) == -1)
1724		goto fail;
1725
1726	if (st.st_uid != uid || st.st_gid != gid) {
1727		if (chown(vm->vm_ttyname, uid, gid) == -1) {
1728			log_warn("chown %s %d %d failed, uid %d",
1729			    vm->vm_ttyname, uid, gid, getuid());
1730
1731			/* Ignore failure on read-only filesystems */
1732			if (!((errno == EROFS) &&
1733			    (st.st_uid == uid || st.st_uid == 0)))
1734				goto fail;
1735		}
1736	}
1737
1738	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
1739		if (chmod(vm->vm_ttyname, mode) == -1) {
1740			log_warn("chmod %s %o failed, uid %d",
1741			    vm->vm_ttyname, mode, getuid());
1742
1743			/* Ignore failure on read-only filesystems */
1744			if (!((errno == EROFS) &&
1745			    (st.st_uid == uid || st.st_uid == 0)))
1746				goto fail;
1747		}
1748	}
1749
1750	return (0);
1751 fail:
1752	vm_closetty(vm);
1753	return (-1);
1754}
1755
1756void
1757vm_closetty(struct vmd_vm *vm)
1758{
1759	if (vm->vm_tty != -1) {
1760		/* Release and close the tty */
1761		if (fchown(vm->vm_tty, 0, 0) == -1)
1762			log_warn("chown %s 0 0 failed", vm->vm_ttyname);
1763		if (fchmod(vm->vm_tty, 0666) == -1)
1764			log_warn("chmod %s 0666 failed", vm->vm_ttyname);
1765		close(vm->vm_tty);
1766		vm->vm_tty = -1;
1767	}
1768	free(vm->vm_ttyname);
1769	vm->vm_ttyname = NULL;
1770}
1771
1772void
1773switch_remove(struct vmd_switch *vsw)
1774{
1775	if (vsw == NULL)
1776		return;
1777
1778	TAILQ_REMOVE(env->vmd_switches, vsw, sw_entry);
1779
1780	free(vsw->sw_group);
1781	free(vsw->sw_name);
1782	free(vsw);
1783}
1784
1785struct vmd_switch *
1786switch_getbyname(const char *name)
1787{
1788	struct vmd_switch	*vsw;
1789
1790	if (name == NULL)
1791		return (NULL);
1792	TAILQ_FOREACH(vsw, env->vmd_switches, sw_entry) {
1793		if (strcmp(vsw->sw_name, name) == 0)
1794			return (vsw);
1795	}
1796
1797	return (NULL);
1798}
1799
1800struct vmd_user *
1801user_get(uid_t uid)
1802{
1803	struct vmd_user		*usr;
1804
1805	if (uid == 0)
1806		return (NULL);
1807
1808	/* first try to find an existing user */
1809	TAILQ_FOREACH(usr, env->vmd_users, usr_entry) {
1810		if (usr->usr_id.uid == uid)
1811			goto done;
1812	}
1813
1814	if ((usr = calloc(1, sizeof(*usr))) == NULL) {
1815		log_warn("could not allocate user");
1816		return (NULL);
1817	}
1818
1819	usr->usr_id.uid = uid;
1820	usr->usr_id.gid = -1;
1821	TAILQ_INSERT_TAIL(env->vmd_users, usr, usr_entry);
1822
1823 done:
1824	DPRINTF("%s: uid %d #%d +",
1825	    __func__, usr->usr_id.uid, usr->usr_refcnt + 1);
1826	usr->usr_refcnt++;
1827
1828	return (usr);
1829}
1830
1831void
1832user_put(struct vmd_user *usr)
1833{
1834	if (usr == NULL)
1835		return;
1836
1837	DPRINTF("%s: uid %d #%d -",
1838	    __func__, usr->usr_id.uid, usr->usr_refcnt - 1);
1839
1840	if (--usr->usr_refcnt > 0)
1841		return;
1842
1843	TAILQ_REMOVE(env->vmd_users, usr, usr_entry);
1844	free(usr);
1845}
1846
1847void
1848user_inc(struct vm_create_params *vcp, struct vmd_user *usr, int inc)
1849{
1850	char	 mem[FMT_SCALED_STRSIZE];
1851
1852	if (usr == NULL)
1853		return;
1854
1855	/* increment or decrement counters */
1856	inc = inc ? 1 : -1;
1857
1858	usr->usr_maxcpu += vcp->vcp_ncpus * inc;
1859	usr->usr_maxmem += vcp->vcp_memranges[0].vmr_size * inc;
1860	usr->usr_maxifs += vcp->vcp_nnics * inc;
1861
1862	if (log_getverbose() > 1) {
1863		(void)fmt_scaled(usr->usr_maxmem * 1024 * 1024, mem);
1864		log_debug("%s: %c uid %d ref %d cpu %llu mem %s ifs %llu",
1865		    __func__, inc == 1 ? '+' : '-',
1866		    usr->usr_id.uid, usr->usr_refcnt,
1867		    usr->usr_maxcpu, mem, usr->usr_maxifs);
1868	}
1869}
1870
1871int
1872user_checklimit(struct vmd_user *usr, struct vm_create_params *vcp)
1873{
1874	const char	*limit = "";
1875
1876	/* XXX make the limits configurable */
1877	if (usr->usr_maxcpu > VM_DEFAULT_USER_MAXCPU) {
1878		limit = "cpu ";
1879		goto fail;
1880	}
1881	if (usr->usr_maxmem > VM_DEFAULT_USER_MAXMEM) {
1882		limit = "memory ";
1883		goto fail;
1884	}
1885	if (usr->usr_maxifs > VM_DEFAULT_USER_MAXIFS) {
1886		limit = "interface ";
1887		goto fail;
1888	}
1889
1890	return (0);
1891
1892 fail:
1893	log_warnx("%s: user %d %slimit reached", vcp->vcp_name,
1894	    usr->usr_id.uid, limit);
1895	return (-1);
1896}
1897
1898char *
1899get_string(uint8_t *ptr, size_t len)
1900{
1901	size_t	 i;
1902
1903	for (i = 0; i < len; i++)
1904		if (!isprint(ptr[i]))
1905			break;
1906
1907	return strndup(ptr, i);
1908}
1909
1910uint32_t
1911prefixlen2mask(uint8_t prefixlen)
1912{
1913	if (prefixlen == 0)
1914		return (0);
1915
1916	if (prefixlen > 32)
1917		prefixlen = 32;
1918
1919	return (htonl(0xffffffff << (32 - prefixlen)));
1920}
1921