accessors.c revision 1.2 1 1.2 pooka /* $NetBSD: accessors.c,v 1.2 2015/04/03 16:37:02 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka /*
29 1.1 pooka * This file contains various data structure accessor routines.
30 1.1 pooka * They are meant to help clients that make calls into the depths
31 1.1 pooka * of the kernel (e.g. at vfs layer) bypassing the syscall layer.
32 1.1 pooka */
33 1.1 pooka
34 1.1 pooka #include <sys/cdefs.h>
35 1.2 pooka __KERNEL_RCSID(0, "$NetBSD: accessors.c,v 1.2 2015/04/03 16:37:02 pooka Exp $");
36 1.1 pooka
37 1.1 pooka #include <sys/param.h>
38 1.1 pooka #include <sys/kauth.h>
39 1.1 pooka #include <sys/kmem.h>
40 1.1 pooka #include <sys/uio.h>
41 1.1 pooka
42 1.1 pooka #include "rump_private.h"
43 1.1 pooka
44 1.1 pooka struct uio *
45 1.1 pooka rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
46 1.1 pooka {
47 1.1 pooka struct uio *uio;
48 1.1 pooka enum uio_rw uiorw;
49 1.1 pooka
50 1.1 pooka switch (rw) {
51 1.1 pooka case RUMPUIO_READ:
52 1.1 pooka uiorw = UIO_READ;
53 1.1 pooka break;
54 1.1 pooka case RUMPUIO_WRITE:
55 1.1 pooka uiorw = UIO_WRITE;
56 1.1 pooka break;
57 1.1 pooka default:
58 1.1 pooka panic("%s: invalid rw %d", __func__, rw);
59 1.1 pooka }
60 1.1 pooka
61 1.1 pooka uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
62 1.1 pooka uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
63 1.1 pooka
64 1.1 pooka uio->uio_iov->iov_base = buf;
65 1.1 pooka uio->uio_iov->iov_len = bufsize;
66 1.1 pooka
67 1.1 pooka uio->uio_iovcnt = 1;
68 1.1 pooka uio->uio_offset = offset;
69 1.1 pooka uio->uio_resid = bufsize;
70 1.1 pooka uio->uio_rw = uiorw;
71 1.2 pooka uio->uio_vmspace = curproc->p_vmspace;
72 1.1 pooka
73 1.1 pooka return uio;
74 1.1 pooka }
75 1.1 pooka
76 1.1 pooka size_t
77 1.1 pooka rump_uio_getresid(struct uio *uio)
78 1.1 pooka {
79 1.1 pooka
80 1.1 pooka return uio->uio_resid;
81 1.1 pooka }
82 1.1 pooka
83 1.1 pooka off_t
84 1.1 pooka rump_uio_getoff(struct uio *uio)
85 1.1 pooka {
86 1.1 pooka
87 1.1 pooka return uio->uio_offset;
88 1.1 pooka }
89 1.1 pooka
90 1.1 pooka size_t
91 1.1 pooka rump_uio_free(struct uio *uio)
92 1.1 pooka {
93 1.1 pooka size_t resid;
94 1.1 pooka
95 1.1 pooka resid = uio->uio_resid;
96 1.1 pooka kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
97 1.1 pooka kmem_free(uio, sizeof(*uio));
98 1.1 pooka
99 1.1 pooka return resid;
100 1.1 pooka }
101 1.1 pooka
102 1.1 pooka kauth_cred_t
103 1.1 pooka rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
104 1.1 pooka {
105 1.1 pooka kauth_cred_t cred;
106 1.1 pooka int rv;
107 1.1 pooka
108 1.1 pooka cred = kauth_cred_alloc();
109 1.1 pooka kauth_cred_setuid(cred, uid);
110 1.1 pooka kauth_cred_seteuid(cred, uid);
111 1.1 pooka kauth_cred_setsvuid(cred, uid);
112 1.1 pooka kauth_cred_setgid(cred, gid);
113 1.1 pooka kauth_cred_setgid(cred, gid);
114 1.1 pooka kauth_cred_setegid(cred, gid);
115 1.1 pooka kauth_cred_setsvgid(cred, gid);
116 1.1 pooka rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
117 1.1 pooka /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
118 1.1 pooka assert(rv == 0);
119 1.1 pooka
120 1.1 pooka return cred;
121 1.1 pooka }
122 1.1 pooka
123 1.1 pooka void
124 1.1 pooka rump_cred_put(kauth_cred_t cred)
125 1.1 pooka {
126 1.1 pooka
127 1.1 pooka kauth_cred_free(cred);
128 1.1 pooka }
129