1 /*	$NetBSD: accessors.c,v 1.3 2016/01/26 23:12:17 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2007-2011 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 /*
29  * This file contains various data structure accessor routines.
30  * They are meant to help clients that make calls into the depths
31  * of the kernel (e.g. at vfs layer) bypassing the syscall layer.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: accessors.c,v 1.3 2016/01/26 23:12:17 pooka Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/kauth.h>
39 #include <sys/kmem.h>
40 #include <sys/uio.h>
41 
42 #include <rump-sys/kern.h>
43 
44 struct uio *
rump_uio_setup(void * buf,size_t bufsize,off_t offset,enum rump_uiorw rw)45 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
46 {
47 	struct uio *uio;
48 	enum uio_rw uiorw;
49 
50 	switch (rw) {
51 	case RUMPUIO_READ:
52 		uiorw = UIO_READ;
53 		break;
54 	case RUMPUIO_WRITE:
55 		uiorw = UIO_WRITE;
56 		break;
57 	default:
58 		panic("%s: invalid rw %d", __func__, rw);
59 	}
60 
61 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
62 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
63 
64 	uio->uio_iov->iov_base = buf;
65 	uio->uio_iov->iov_len = bufsize;
66 
67 	uio->uio_iovcnt = 1;
68 	uio->uio_offset = offset;
69 	uio->uio_resid = bufsize;
70 	uio->uio_rw = uiorw;
71 	uio->uio_vmspace = curproc->p_vmspace;
72 
73 	return uio;
74 }
75 
76 size_t
rump_uio_getresid(struct uio * uio)77 rump_uio_getresid(struct uio *uio)
78 {
79 
80 	return uio->uio_resid;
81 }
82 
83 off_t
rump_uio_getoff(struct uio * uio)84 rump_uio_getoff(struct uio *uio)
85 {
86 
87 	return uio->uio_offset;
88 }
89 
90 size_t
rump_uio_free(struct uio * uio)91 rump_uio_free(struct uio *uio)
92 {
93 	size_t resid;
94 
95 	resid = uio->uio_resid;
96 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
97 	kmem_free(uio, sizeof(*uio));
98 
99 	return resid;
100 }
101 
102 kauth_cred_t
rump_cred_create(uid_t uid,gid_t gid,size_t ngroups,gid_t * groups)103 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
104 {
105 	kauth_cred_t cred;
106 	int rv;
107 
108 	cred = kauth_cred_alloc();
109 	kauth_cred_setuid(cred, uid);
110 	kauth_cred_seteuid(cred, uid);
111 	kauth_cred_setsvuid(cred, uid);
112 	kauth_cred_setgid(cred, gid);
113 	kauth_cred_setgid(cred, gid);
114 	kauth_cred_setegid(cred, gid);
115 	kauth_cred_setsvgid(cred, gid);
116 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
117 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
118 	assert(rv == 0);
119 
120 	return cred;
121 }
122 
123 void
rump_cred_put(kauth_cred_t cred)124 rump_cred_put(kauth_cred_t cred)
125 {
126 
127 	kauth_cred_free(cred);
128 }
129