kern_auth.c revision 1.8.4.8 1 1.8.4.8 yamt /* $NetBSD: kern_auth.c,v 1.8.4.8 2007/12/07 17:32:37 yamt Exp $ */
2 1.8.4.8 yamt
3 1.8.4.8 yamt /*-
4 1.8.4.8 yamt * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 1.8.4.8 yamt * All rights reserved.
6 1.8.4.8 yamt *
7 1.8.4.8 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.8.4.8 yamt * by Andrew Doran.
9 1.8.4.8 yamt *
10 1.8.4.8 yamt * Redistribution and use in source and binary forms, with or without
11 1.8.4.8 yamt * modification, are permitted provided that the following conditions
12 1.8.4.8 yamt * are met:
13 1.8.4.8 yamt * 1. Redistributions of source code must retain the above copyright
14 1.8.4.8 yamt * notice, this list of conditions and the following disclaimer.
15 1.8.4.8 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.4.8 yamt * notice, this list of conditions and the following disclaimer in the
17 1.8.4.8 yamt * documentation and/or other materials provided with the distribution.
18 1.8.4.8 yamt * 3. All advertising materials mentioning features or use of this software
19 1.8.4.8 yamt * must display the following acknowledgement:
20 1.8.4.8 yamt * This product includes software developed by the NetBSD
21 1.8.4.8 yamt * Foundation, Inc. and its contributors.
22 1.8.4.8 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8.4.8 yamt * contributors may be used to endorse or promote products derived
24 1.8.4.8 yamt * from this software without specific prior written permission.
25 1.8.4.8 yamt *
26 1.8.4.8 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8.4.8 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8.4.8 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8.4.8 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8.4.8 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8.4.8 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8.4.8 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8.4.8 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8.4.8 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8.4.8 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8.4.8 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.8.4.8 yamt */
38 1.8.4.2 yamt
39 1.8.4.2 yamt /*-
40 1.8.4.2 yamt * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
41 1.8.4.2 yamt * All rights reserved.
42 1.8.4.2 yamt *
43 1.8.4.2 yamt * Redistribution and use in source and binary forms, with or without
44 1.8.4.2 yamt * modification, are permitted provided that the following conditions
45 1.8.4.2 yamt * are met:
46 1.8.4.2 yamt * 1. Redistributions of source code must retain the above copyright
47 1.8.4.2 yamt * notice, this list of conditions and the following disclaimer.
48 1.8.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
49 1.8.4.2 yamt * notice, this list of conditions and the following disclaimer in the
50 1.8.4.2 yamt * documentation and/or other materials provided with the distribution.
51 1.8.4.4 yamt * 3. The name of the author may not be used to endorse or promote products
52 1.8.4.2 yamt * derived from this software without specific prior written permission.
53 1.8.4.2 yamt *
54 1.8.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 1.8.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 1.8.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 1.8.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 1.8.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 1.8.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 1.8.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 1.8.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 1.8.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 1.8.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 1.8.4.2 yamt */
65 1.8.4.2 yamt
66 1.8.4.3 yamt #include <sys/cdefs.h>
67 1.8.4.8 yamt __KERNEL_RCSID(0, "$NetBSD: kern_auth.c,v 1.8.4.8 2007/12/07 17:32:37 yamt Exp $");
68 1.8.4.2 yamt
69 1.8.4.2 yamt #include <sys/types.h>
70 1.8.4.2 yamt #include <sys/param.h>
71 1.8.4.2 yamt #include <sys/queue.h>
72 1.8.4.2 yamt #include <sys/proc.h>
73 1.8.4.2 yamt #include <sys/ucred.h>
74 1.8.4.2 yamt #include <sys/pool.h>
75 1.8.4.2 yamt #include <sys/kauth.h>
76 1.8.4.3 yamt #include <sys/kmem.h>
77 1.8.4.4 yamt #include <sys/rwlock.h>
78 1.8.4.4 yamt #include <sys/sysctl.h> /* for pi_[p]cread */
79 1.8.4.8 yamt #include <sys/atomic.h>
80 1.8.4.4 yamt #include <sys/specificdata.h>
81 1.8.4.4 yamt
82 1.8.4.4 yamt /*
83 1.8.4.4 yamt * Secmodel-specific credentials.
84 1.8.4.4 yamt */
85 1.8.4.4 yamt struct kauth_key {
86 1.8.4.4 yamt const char *ks_secmodel; /* secmodel */
87 1.8.4.4 yamt specificdata_key_t ks_key; /* key */
88 1.8.4.4 yamt };
89 1.8.4.2 yamt
90 1.8.4.2 yamt /*
91 1.8.4.2 yamt * Credentials.
92 1.8.4.4 yamt *
93 1.8.4.4 yamt * A subset of this structure is used in kvm(3) (src/lib/libkvm/kvm_proc.c)
94 1.8.4.4 yamt * and should be synchronized with this structure when the update is
95 1.8.4.4 yamt * relevant.
96 1.8.4.2 yamt */
97 1.8.4.2 yamt struct kauth_cred {
98 1.8.4.7 yamt /*
99 1.8.4.7 yamt * Ensure that the first part of the credential resides in its own
100 1.8.4.7 yamt * cache line. Due to sharing there aren't many kauth_creds in a
101 1.8.4.7 yamt * typical system, but the reference counts change very often.
102 1.8.4.7 yamt * Keeping it seperate from the rest of the data prevents false
103 1.8.4.7 yamt * sharing between CPUs.
104 1.8.4.7 yamt */
105 1.8.4.3 yamt u_int cr_refcnt; /* reference count */
106 1.8.4.8 yamt uint8_t cr_pad[CACHE_LINE_SIZE - sizeof(u_int)];
107 1.8.4.8 yamt uid_t cr_uid; /* user id */
108 1.8.4.2 yamt uid_t cr_euid; /* effective user id */
109 1.8.4.2 yamt uid_t cr_svuid; /* saved effective user id */
110 1.8.4.2 yamt gid_t cr_gid; /* group id */
111 1.8.4.2 yamt gid_t cr_egid; /* effective group id */
112 1.8.4.2 yamt gid_t cr_svgid; /* saved effective group id */
113 1.8.4.3 yamt u_int cr_ngroups; /* number of groups */
114 1.8.4.2 yamt gid_t cr_groups[NGROUPS]; /* group memberships */
115 1.8.4.4 yamt specificdata_reference cr_sd; /* specific data */
116 1.8.4.2 yamt };
117 1.8.4.2 yamt
118 1.8.4.2 yamt /*
119 1.8.4.2 yamt * Listener.
120 1.8.4.2 yamt */
121 1.8.4.2 yamt struct kauth_listener {
122 1.8.4.2 yamt kauth_scope_callback_t func; /* callback */
123 1.8.4.2 yamt kauth_scope_t scope; /* scope backpointer */
124 1.8.4.3 yamt u_int refcnt; /* reference count */
125 1.8.4.2 yamt SIMPLEQ_ENTRY(kauth_listener) listener_next; /* listener list */
126 1.8.4.2 yamt };
127 1.8.4.2 yamt
128 1.8.4.2 yamt /*
129 1.8.4.2 yamt * Scope.
130 1.8.4.2 yamt */
131 1.8.4.2 yamt struct kauth_scope {
132 1.8.4.2 yamt const char *id; /* scope name */
133 1.8.4.2 yamt void *cookie; /* user cookie */
134 1.8.4.3 yamt u_int nlisteners; /* # of listeners */
135 1.8.4.2 yamt SIMPLEQ_HEAD(, kauth_listener) listenq; /* listener list */
136 1.8.4.2 yamt SIMPLEQ_ENTRY(kauth_scope) next_scope; /* scope list */
137 1.8.4.2 yamt };
138 1.8.4.2 yamt
139 1.8.4.4 yamt static int kauth_cred_hook(kauth_cred_t, kauth_action_t, void *, void *);
140 1.8.4.2 yamt
141 1.8.4.2 yamt /* List of scopes and its lock. */
142 1.8.4.7 yamt static SIMPLEQ_HEAD(, kauth_scope) scope_list =
143 1.8.4.7 yamt SIMPLEQ_HEAD_INITIALIZER(scope_list);
144 1.8.4.2 yamt
145 1.8.4.2 yamt /* Built-in scopes: generic, process. */
146 1.8.4.2 yamt static kauth_scope_t kauth_builtin_scope_generic;
147 1.8.4.3 yamt static kauth_scope_t kauth_builtin_scope_system;
148 1.8.4.2 yamt static kauth_scope_t kauth_builtin_scope_process;
149 1.8.4.3 yamt static kauth_scope_t kauth_builtin_scope_network;
150 1.8.4.3 yamt static kauth_scope_t kauth_builtin_scope_machdep;
151 1.8.4.3 yamt static kauth_scope_t kauth_builtin_scope_device;
152 1.8.4.4 yamt static kauth_scope_t kauth_builtin_scope_cred;
153 1.8.4.3 yamt
154 1.8.4.4 yamt static unsigned int nsecmodels = 0;
155 1.8.4.4 yamt
156 1.8.4.4 yamt static specificdata_domain_t kauth_domain;
157 1.8.4.7 yamt static pool_cache_t kauth_cred_cache;
158 1.8.4.4 yamt krwlock_t kauth_lock;
159 1.8.4.2 yamt
160 1.8.4.2 yamt /* Allocate new, empty kauth credentials. */
161 1.8.4.2 yamt kauth_cred_t
162 1.8.4.2 yamt kauth_cred_alloc(void)
163 1.8.4.2 yamt {
164 1.8.4.2 yamt kauth_cred_t cred;
165 1.8.4.2 yamt
166 1.8.4.7 yamt cred = pool_cache_get(kauth_cred_cache, PR_WAITOK);
167 1.8.4.7 yamt
168 1.8.4.2 yamt cred->cr_refcnt = 1;
169 1.8.4.7 yamt cred->cr_uid = 0;
170 1.8.4.7 yamt cred->cr_euid = 0;
171 1.8.4.7 yamt cred->cr_svuid = 0;
172 1.8.4.7 yamt cred->cr_gid = 0;
173 1.8.4.7 yamt cred->cr_egid = 0;
174 1.8.4.7 yamt cred->cr_svgid = 0;
175 1.8.4.7 yamt cred->cr_ngroups = 0;
176 1.8.4.7 yamt
177 1.8.4.4 yamt specificdata_init(kauth_domain, &cred->cr_sd);
178 1.8.4.4 yamt kauth_cred_hook(cred, KAUTH_CRED_INIT, NULL, NULL);
179 1.8.4.2 yamt
180 1.8.4.2 yamt return (cred);
181 1.8.4.2 yamt }
182 1.8.4.2 yamt
183 1.8.4.2 yamt /* Increment reference count to cred. */
184 1.8.4.2 yamt void
185 1.8.4.2 yamt kauth_cred_hold(kauth_cred_t cred)
186 1.8.4.2 yamt {
187 1.8.4.2 yamt KASSERT(cred != NULL);
188 1.8.4.3 yamt KASSERT(cred->cr_refcnt > 0);
189 1.8.4.2 yamt
190 1.8.4.8 yamt atomic_inc_uint(&cred->cr_refcnt);
191 1.8.4.2 yamt }
192 1.8.4.2 yamt
193 1.8.4.2 yamt /* Decrease reference count to cred. If reached zero, free it. */
194 1.8.4.2 yamt void
195 1.8.4.2 yamt kauth_cred_free(kauth_cred_t cred)
196 1.8.4.2 yamt {
197 1.8.4.3 yamt
198 1.8.4.2 yamt KASSERT(cred != NULL);
199 1.8.4.3 yamt KASSERT(cred->cr_refcnt > 0);
200 1.8.4.2 yamt
201 1.8.4.8 yamt if (atomic_dec_uint_nv(&cred->cr_refcnt) > 0)
202 1.8.4.8 yamt return;
203 1.8.4.8 yamt
204 1.8.4.8 yamt kauth_cred_hook(cred, KAUTH_CRED_FREE, NULL, NULL);
205 1.8.4.8 yamt specificdata_fini(kauth_domain, &cred->cr_sd);
206 1.8.4.8 yamt pool_cache_put(kauth_cred_cache, cred);
207 1.8.4.2 yamt }
208 1.8.4.2 yamt
209 1.8.4.5 yamt static void
210 1.8.4.5 yamt kauth_cred_clone1(kauth_cred_t from, kauth_cred_t to, bool copy_groups)
211 1.8.4.2 yamt {
212 1.8.4.2 yamt KASSERT(from != NULL);
213 1.8.4.2 yamt KASSERT(to != NULL);
214 1.8.4.3 yamt KASSERT(from->cr_refcnt > 0);
215 1.8.4.2 yamt
216 1.8.4.2 yamt to->cr_uid = from->cr_uid;
217 1.8.4.2 yamt to->cr_euid = from->cr_euid;
218 1.8.4.2 yamt to->cr_svuid = from->cr_svuid;
219 1.8.4.2 yamt to->cr_gid = from->cr_gid;
220 1.8.4.2 yamt to->cr_egid = from->cr_egid;
221 1.8.4.2 yamt to->cr_svgid = from->cr_svgid;
222 1.8.4.5 yamt if (copy_groups) {
223 1.8.4.5 yamt to->cr_ngroups = from->cr_ngroups;
224 1.8.4.5 yamt memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
225 1.8.4.5 yamt }
226 1.8.4.4 yamt
227 1.8.4.4 yamt kauth_cred_hook(from, KAUTH_CRED_COPY, to, NULL);
228 1.8.4.2 yamt }
229 1.8.4.2 yamt
230 1.8.4.5 yamt void
231 1.8.4.5 yamt kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
232 1.8.4.5 yamt {
233 1.8.4.5 yamt kauth_cred_clone1(from, to, true);
234 1.8.4.5 yamt }
235 1.8.4.5 yamt
236 1.8.4.2 yamt /*
237 1.8.4.2 yamt * Duplicate cred and return a new kauth_cred_t.
238 1.8.4.2 yamt */
239 1.8.4.2 yamt kauth_cred_t
240 1.8.4.2 yamt kauth_cred_dup(kauth_cred_t cred)
241 1.8.4.2 yamt {
242 1.8.4.2 yamt kauth_cred_t new_cred;
243 1.8.4.2 yamt
244 1.8.4.2 yamt KASSERT(cred != NULL);
245 1.8.4.3 yamt KASSERT(cred->cr_refcnt > 0);
246 1.8.4.2 yamt
247 1.8.4.2 yamt new_cred = kauth_cred_alloc();
248 1.8.4.2 yamt
249 1.8.4.2 yamt kauth_cred_clone(cred, new_cred);
250 1.8.4.2 yamt
251 1.8.4.2 yamt return (new_cred);
252 1.8.4.2 yamt }
253 1.8.4.2 yamt
254 1.8.4.2 yamt /*
255 1.8.4.2 yamt * Similar to crcopy(), only on a kauth_cred_t.
256 1.8.4.2 yamt * XXX: Is this even needed? [kauth_cred_copy]
257 1.8.4.2 yamt */
258 1.8.4.2 yamt kauth_cred_t
259 1.8.4.2 yamt kauth_cred_copy(kauth_cred_t cred)
260 1.8.4.2 yamt {
261 1.8.4.2 yamt kauth_cred_t new_cred;
262 1.8.4.2 yamt
263 1.8.4.2 yamt KASSERT(cred != NULL);
264 1.8.4.3 yamt KASSERT(cred->cr_refcnt > 0);
265 1.8.4.2 yamt
266 1.8.4.2 yamt /* If the provided credentials already have one reference, use them. */
267 1.8.4.2 yamt if (cred->cr_refcnt == 1)
268 1.8.4.2 yamt return (cred);
269 1.8.4.2 yamt
270 1.8.4.2 yamt new_cred = kauth_cred_alloc();
271 1.8.4.2 yamt
272 1.8.4.2 yamt kauth_cred_clone(cred, new_cred);
273 1.8.4.2 yamt
274 1.8.4.2 yamt kauth_cred_free(cred);
275 1.8.4.2 yamt
276 1.8.4.2 yamt return (new_cred);
277 1.8.4.2 yamt }
278 1.8.4.2 yamt
279 1.8.4.4 yamt void
280 1.8.4.4 yamt kauth_proc_fork(struct proc *parent, struct proc *child)
281 1.8.4.4 yamt {
282 1.8.4.4 yamt
283 1.8.4.4 yamt mutex_enter(&parent->p_mutex);
284 1.8.4.4 yamt kauth_cred_hold(parent->p_cred);
285 1.8.4.4 yamt child->p_cred = parent->p_cred;
286 1.8.4.4 yamt mutex_exit(&parent->p_mutex);
287 1.8.4.4 yamt
288 1.8.4.4 yamt /* XXX: relies on parent process stalling during fork() */
289 1.8.4.4 yamt kauth_cred_hook(parent->p_cred, KAUTH_CRED_FORK, parent,
290 1.8.4.4 yamt child);
291 1.8.4.4 yamt }
292 1.8.4.4 yamt
293 1.8.4.2 yamt uid_t
294 1.8.4.2 yamt kauth_cred_getuid(kauth_cred_t cred)
295 1.8.4.2 yamt {
296 1.8.4.2 yamt KASSERT(cred != NULL);
297 1.8.4.2 yamt
298 1.8.4.2 yamt return (cred->cr_uid);
299 1.8.4.2 yamt }
300 1.8.4.2 yamt
301 1.8.4.2 yamt uid_t
302 1.8.4.2 yamt kauth_cred_geteuid(kauth_cred_t cred)
303 1.8.4.2 yamt {
304 1.8.4.2 yamt KASSERT(cred != NULL);
305 1.8.4.2 yamt
306 1.8.4.2 yamt return (cred->cr_euid);
307 1.8.4.2 yamt }
308 1.8.4.2 yamt
309 1.8.4.2 yamt uid_t
310 1.8.4.2 yamt kauth_cred_getsvuid(kauth_cred_t cred)
311 1.8.4.2 yamt {
312 1.8.4.2 yamt KASSERT(cred != NULL);
313 1.8.4.2 yamt
314 1.8.4.2 yamt return (cred->cr_svuid);
315 1.8.4.2 yamt }
316 1.8.4.2 yamt
317 1.8.4.2 yamt gid_t
318 1.8.4.2 yamt kauth_cred_getgid(kauth_cred_t cred)
319 1.8.4.2 yamt {
320 1.8.4.2 yamt KASSERT(cred != NULL);
321 1.8.4.2 yamt
322 1.8.4.2 yamt return (cred->cr_gid);
323 1.8.4.2 yamt }
324 1.8.4.2 yamt
325 1.8.4.2 yamt gid_t
326 1.8.4.2 yamt kauth_cred_getegid(kauth_cred_t cred)
327 1.8.4.2 yamt {
328 1.8.4.2 yamt KASSERT(cred != NULL);
329 1.8.4.2 yamt
330 1.8.4.2 yamt return (cred->cr_egid);
331 1.8.4.2 yamt }
332 1.8.4.2 yamt
333 1.8.4.2 yamt gid_t
334 1.8.4.2 yamt kauth_cred_getsvgid(kauth_cred_t cred)
335 1.8.4.2 yamt {
336 1.8.4.2 yamt KASSERT(cred != NULL);
337 1.8.4.2 yamt
338 1.8.4.2 yamt return (cred->cr_svgid);
339 1.8.4.2 yamt }
340 1.8.4.2 yamt
341 1.8.4.2 yamt void
342 1.8.4.2 yamt kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
343 1.8.4.2 yamt {
344 1.8.4.2 yamt KASSERT(cred != NULL);
345 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
346 1.8.4.2 yamt
347 1.8.4.2 yamt cred->cr_uid = uid;
348 1.8.4.2 yamt }
349 1.8.4.2 yamt
350 1.8.4.2 yamt void
351 1.8.4.2 yamt kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
352 1.8.4.2 yamt {
353 1.8.4.2 yamt KASSERT(cred != NULL);
354 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
355 1.8.4.2 yamt
356 1.8.4.2 yamt cred->cr_euid = uid;
357 1.8.4.2 yamt }
358 1.8.4.2 yamt
359 1.8.4.2 yamt void
360 1.8.4.2 yamt kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
361 1.8.4.2 yamt {
362 1.8.4.2 yamt KASSERT(cred != NULL);
363 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
364 1.8.4.2 yamt
365 1.8.4.2 yamt cred->cr_svuid = uid;
366 1.8.4.2 yamt }
367 1.8.4.2 yamt
368 1.8.4.2 yamt void
369 1.8.4.2 yamt kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
370 1.8.4.2 yamt {
371 1.8.4.2 yamt KASSERT(cred != NULL);
372 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
373 1.8.4.2 yamt
374 1.8.4.2 yamt cred->cr_gid = gid;
375 1.8.4.2 yamt }
376 1.8.4.2 yamt
377 1.8.4.2 yamt void
378 1.8.4.2 yamt kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
379 1.8.4.2 yamt {
380 1.8.4.2 yamt KASSERT(cred != NULL);
381 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
382 1.8.4.2 yamt
383 1.8.4.2 yamt cred->cr_egid = gid;
384 1.8.4.2 yamt }
385 1.8.4.2 yamt
386 1.8.4.2 yamt void
387 1.8.4.2 yamt kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
388 1.8.4.2 yamt {
389 1.8.4.2 yamt KASSERT(cred != NULL);
390 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
391 1.8.4.2 yamt
392 1.8.4.2 yamt cred->cr_svgid = gid;
393 1.8.4.2 yamt }
394 1.8.4.2 yamt
395 1.8.4.2 yamt /* Checks if gid is a member of the groups in cred. */
396 1.8.4.2 yamt int
397 1.8.4.2 yamt kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
398 1.8.4.2 yamt {
399 1.8.4.2 yamt int i;
400 1.8.4.2 yamt
401 1.8.4.2 yamt KASSERT(cred != NULL);
402 1.8.4.2 yamt KASSERT(resultp != NULL);
403 1.8.4.2 yamt
404 1.8.4.2 yamt *resultp = 0;
405 1.8.4.2 yamt
406 1.8.4.2 yamt for (i = 0; i < cred->cr_ngroups; i++)
407 1.8.4.2 yamt if (cred->cr_groups[i] == gid) {
408 1.8.4.2 yamt *resultp = 1;
409 1.8.4.2 yamt break;
410 1.8.4.2 yamt }
411 1.8.4.2 yamt
412 1.8.4.2 yamt return (0);
413 1.8.4.2 yamt }
414 1.8.4.2 yamt
415 1.8.4.3 yamt u_int
416 1.8.4.2 yamt kauth_cred_ngroups(kauth_cred_t cred)
417 1.8.4.2 yamt {
418 1.8.4.2 yamt KASSERT(cred != NULL);
419 1.8.4.2 yamt
420 1.8.4.2 yamt return (cred->cr_ngroups);
421 1.8.4.2 yamt }
422 1.8.4.2 yamt
423 1.8.4.2 yamt /*
424 1.8.4.2 yamt * Return the group at index idx from the groups in cred.
425 1.8.4.2 yamt */
426 1.8.4.2 yamt gid_t
427 1.8.4.3 yamt kauth_cred_group(kauth_cred_t cred, u_int idx)
428 1.8.4.2 yamt {
429 1.8.4.2 yamt KASSERT(cred != NULL);
430 1.8.4.2 yamt KASSERT(idx < cred->cr_ngroups);
431 1.8.4.2 yamt
432 1.8.4.2 yamt return (cred->cr_groups[idx]);
433 1.8.4.2 yamt }
434 1.8.4.2 yamt
435 1.8.4.2 yamt /* XXX elad: gmuid is unused for now. */
436 1.8.4.2 yamt int
437 1.8.4.5 yamt kauth_cred_setgroups(kauth_cred_t cred, const gid_t *grbuf, size_t len,
438 1.8.4.6 yamt uid_t gmuid, enum uio_seg seg)
439 1.8.4.2 yamt {
440 1.8.4.5 yamt int error = 0;
441 1.8.4.5 yamt
442 1.8.4.2 yamt KASSERT(cred != NULL);
443 1.8.4.3 yamt KASSERT(cred->cr_refcnt == 1);
444 1.8.4.2 yamt
445 1.8.4.5 yamt if (len > sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))
446 1.8.4.5 yamt return EINVAL;
447 1.8.4.5 yamt
448 1.8.4.5 yamt if (len) {
449 1.8.4.6 yamt if (seg == UIO_SYSSPACE) {
450 1.8.4.5 yamt memcpy(cred->cr_groups, grbuf,
451 1.8.4.5 yamt len * sizeof(cred->cr_groups[0]));
452 1.8.4.6 yamt } else {
453 1.8.4.5 yamt error = copyin(grbuf, cred->cr_groups,
454 1.8.4.5 yamt len * sizeof(cred->cr_groups[0]));
455 1.8.4.5 yamt if (error != 0)
456 1.8.4.5 yamt len = 0;
457 1.8.4.5 yamt }
458 1.8.4.5 yamt }
459 1.8.4.2 yamt memset(cred->cr_groups + len, 0xff,
460 1.8.4.2 yamt sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
461 1.8.4.2 yamt
462 1.8.4.2 yamt cred->cr_ngroups = len;
463 1.8.4.2 yamt
464 1.8.4.5 yamt return error;
465 1.8.4.2 yamt }
466 1.8.4.2 yamt
467 1.8.4.5 yamt /* This supports sys_setgroups() */
468 1.8.4.2 yamt int
469 1.8.4.5 yamt kauth_proc_setgroups(struct lwp *l, kauth_cred_t ncred)
470 1.8.4.5 yamt {
471 1.8.4.5 yamt kauth_cred_t cred;
472 1.8.4.5 yamt int error;
473 1.8.4.5 yamt
474 1.8.4.5 yamt /*
475 1.8.4.5 yamt * At this point we could delete duplicate groups from ncred,
476 1.8.4.5 yamt * and plausibly sort the list - but in general the later is
477 1.8.4.5 yamt * a bad idea.
478 1.8.4.5 yamt */
479 1.8.4.5 yamt proc_crmod_enter();
480 1.8.4.5 yamt /* Maybe we should use curproc here ? */
481 1.8.4.5 yamt cred = l->l_proc->p_cred;
482 1.8.4.5 yamt
483 1.8.4.5 yamt kauth_cred_clone1(cred, ncred, false);
484 1.8.4.5 yamt
485 1.8.4.5 yamt error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
486 1.8.4.5 yamt l->l_proc, NULL, NULL, NULL);
487 1.8.4.5 yamt if (error != 0) {
488 1.8.4.5 yamt proc_crmod_leave(cred, ncred, false);
489 1.8.4.5 yamt return error;
490 1.8.4.5 yamt }
491 1.8.4.5 yamt
492 1.8.4.5 yamt /* Broadcast our credentials to the process and other LWPs. */
493 1.8.4.5 yamt proc_crmod_leave(ncred, cred, true);
494 1.8.4.5 yamt return 0;
495 1.8.4.5 yamt }
496 1.8.4.5 yamt
497 1.8.4.5 yamt int
498 1.8.4.5 yamt kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len,
499 1.8.4.6 yamt enum uio_seg seg)
500 1.8.4.2 yamt {
501 1.8.4.2 yamt KASSERT(cred != NULL);
502 1.8.4.2 yamt
503 1.8.4.5 yamt if (len > cred->cr_ngroups)
504 1.8.4.5 yamt return EINVAL;
505 1.8.4.5 yamt
506 1.8.4.6 yamt if (seg == UIO_USERSPACE)
507 1.8.4.5 yamt return copyout(cred->cr_groups, grbuf, sizeof(*grbuf) * len);
508 1.8.4.2 yamt memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
509 1.8.4.2 yamt
510 1.8.4.5 yamt return 0;
511 1.8.4.2 yamt }
512 1.8.4.2 yamt
513 1.8.4.4 yamt int
514 1.8.4.4 yamt kauth_register_key(const char *secmodel, kauth_key_t *result)
515 1.8.4.4 yamt {
516 1.8.4.4 yamt kauth_key_t k;
517 1.8.4.4 yamt specificdata_key_t key;
518 1.8.4.4 yamt int error;
519 1.8.4.4 yamt
520 1.8.4.4 yamt KASSERT(result != NULL);
521 1.8.4.4 yamt
522 1.8.4.4 yamt error = specificdata_key_create(kauth_domain, &key, NULL);
523 1.8.4.4 yamt if (error)
524 1.8.4.4 yamt return (error);
525 1.8.4.4 yamt
526 1.8.4.4 yamt k = kmem_alloc(sizeof(*k), KM_SLEEP);
527 1.8.4.4 yamt k->ks_secmodel = secmodel;
528 1.8.4.4 yamt k->ks_key = key;
529 1.8.4.4 yamt
530 1.8.4.4 yamt *result = k;
531 1.8.4.4 yamt
532 1.8.4.4 yamt return (0);
533 1.8.4.4 yamt }
534 1.8.4.4 yamt
535 1.8.4.4 yamt int
536 1.8.4.4 yamt kauth_deregister_key(kauth_key_t key)
537 1.8.4.4 yamt {
538 1.8.4.4 yamt KASSERT(key != NULL);
539 1.8.4.4 yamt
540 1.8.4.4 yamt specificdata_key_delete(kauth_domain, key->ks_key);
541 1.8.4.4 yamt kmem_free(key, sizeof(*key));
542 1.8.4.4 yamt
543 1.8.4.4 yamt return (0);
544 1.8.4.4 yamt }
545 1.8.4.4 yamt
546 1.8.4.4 yamt void *
547 1.8.4.4 yamt kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)
548 1.8.4.4 yamt {
549 1.8.4.4 yamt KASSERT(cred != NULL);
550 1.8.4.4 yamt KASSERT(key != NULL);
551 1.8.4.4 yamt
552 1.8.4.4 yamt return (specificdata_getspecific(kauth_domain, &cred->cr_sd,
553 1.8.4.4 yamt key->ks_key));
554 1.8.4.4 yamt }
555 1.8.4.4 yamt
556 1.8.4.4 yamt void
557 1.8.4.4 yamt kauth_cred_setdata(kauth_cred_t cred, kauth_key_t key, void *data)
558 1.8.4.4 yamt {
559 1.8.4.4 yamt KASSERT(cred != NULL);
560 1.8.4.4 yamt KASSERT(key != NULL);
561 1.8.4.4 yamt
562 1.8.4.4 yamt specificdata_setspecific(kauth_domain, &cred->cr_sd, key->ks_key, data);
563 1.8.4.4 yamt }
564 1.8.4.4 yamt
565 1.8.4.2 yamt /*
566 1.8.4.3 yamt * Match uids in two credentials.
567 1.8.4.2 yamt */
568 1.8.4.3 yamt int
569 1.8.4.2 yamt kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
570 1.8.4.2 yamt {
571 1.8.4.2 yamt KASSERT(cred1 != NULL);
572 1.8.4.2 yamt KASSERT(cred2 != NULL);
573 1.8.4.2 yamt
574 1.8.4.2 yamt if (cred1->cr_uid == cred2->cr_uid ||
575 1.8.4.2 yamt cred1->cr_euid == cred2->cr_uid ||
576 1.8.4.2 yamt cred1->cr_uid == cred2->cr_euid ||
577 1.8.4.2 yamt cred1->cr_euid == cred2->cr_euid)
578 1.8.4.2 yamt return (1);
579 1.8.4.2 yamt
580 1.8.4.2 yamt return (0);
581 1.8.4.2 yamt }
582 1.8.4.2 yamt
583 1.8.4.3 yamt u_int
584 1.8.4.2 yamt kauth_cred_getrefcnt(kauth_cred_t cred)
585 1.8.4.2 yamt {
586 1.8.4.2 yamt KASSERT(cred != NULL);
587 1.8.4.2 yamt
588 1.8.4.2 yamt return (cred->cr_refcnt);
589 1.8.4.2 yamt }
590 1.8.4.2 yamt
591 1.8.4.2 yamt /*
592 1.8.4.2 yamt * Convert userland credentials (struct uucred) to kauth_cred_t.
593 1.8.4.3 yamt * XXX: For NFS & puffs
594 1.8.4.2 yamt */
595 1.8.4.3 yamt void
596 1.8.4.3 yamt kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
597 1.8.4.3 yamt {
598 1.8.4.2 yamt KASSERT(cred != NULL);
599 1.8.4.2 yamt KASSERT(uuc != NULL);
600 1.8.4.3 yamt
601 1.8.4.2 yamt cred->cr_refcnt = 1;
602 1.8.4.2 yamt cred->cr_uid = uuc->cr_uid;
603 1.8.4.2 yamt cred->cr_euid = uuc->cr_uid;
604 1.8.4.2 yamt cred->cr_svuid = uuc->cr_uid;
605 1.8.4.2 yamt cred->cr_gid = uuc->cr_gid;
606 1.8.4.2 yamt cred->cr_egid = uuc->cr_gid;
607 1.8.4.2 yamt cred->cr_svgid = uuc->cr_gid;
608 1.8.4.2 yamt cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
609 1.8.4.2 yamt kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
610 1.8.4.5 yamt cred->cr_ngroups, -1, UIO_SYSSPACE);
611 1.8.4.2 yamt }
612 1.8.4.2 yamt
613 1.8.4.2 yamt /*
614 1.8.4.3 yamt * Convert kauth_cred_t to userland credentials (struct uucred).
615 1.8.4.3 yamt * XXX: For NFS & puffs
616 1.8.4.3 yamt */
617 1.8.4.3 yamt void
618 1.8.4.3 yamt kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
619 1.8.4.3 yamt {
620 1.8.4.3 yamt KASSERT(cred != NULL);
621 1.8.4.3 yamt KASSERT(uuc != NULL);
622 1.8.4.3 yamt int ng;
623 1.8.4.3 yamt
624 1.8.4.3 yamt ng = min(cred->cr_ngroups, NGROUPS);
625 1.8.4.3 yamt uuc->cr_uid = cred->cr_euid;
626 1.8.4.3 yamt uuc->cr_gid = cred->cr_egid;
627 1.8.4.3 yamt uuc->cr_ngroups = ng;
628 1.8.4.5 yamt kauth_cred_getgroups(cred, uuc->cr_groups, ng, UIO_SYSSPACE);
629 1.8.4.3 yamt }
630 1.8.4.3 yamt
631 1.8.4.3 yamt /*
632 1.8.4.2 yamt * Compare kauth_cred_t and uucred credentials.
633 1.8.4.2 yamt * XXX: Modelled after crcmp() for NFS.
634 1.8.4.2 yamt */
635 1.8.4.2 yamt int
636 1.8.4.2 yamt kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
637 1.8.4.2 yamt {
638 1.8.4.2 yamt KASSERT(cred != NULL);
639 1.8.4.2 yamt KASSERT(uuc != NULL);
640 1.8.4.2 yamt
641 1.8.4.2 yamt if (cred->cr_euid == uuc->cr_uid &&
642 1.8.4.2 yamt cred->cr_egid == uuc->cr_gid &&
643 1.8.4.2 yamt cred->cr_ngroups == uuc->cr_ngroups) {
644 1.8.4.2 yamt int i;
645 1.8.4.2 yamt
646 1.8.4.2 yamt /* Check if all groups from uuc appear in cred. */
647 1.8.4.2 yamt for (i = 0; i < uuc->cr_ngroups; i++) {
648 1.8.4.2 yamt int ismember;
649 1.8.4.2 yamt
650 1.8.4.2 yamt ismember = 0;
651 1.8.4.2 yamt if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
652 1.8.4.2 yamt &ismember) != 0 || !ismember)
653 1.8.4.2 yamt return (1);
654 1.8.4.2 yamt }
655 1.8.4.2 yamt
656 1.8.4.2 yamt return (0);
657 1.8.4.2 yamt }
658 1.8.4.2 yamt
659 1.8.4.2 yamt return (1);
660 1.8.4.2 yamt }
661 1.8.4.2 yamt
662 1.8.4.2 yamt /*
663 1.8.4.3 yamt * Make a struct ucred out of a kauth_cred_t. For compatibility.
664 1.8.4.2 yamt */
665 1.8.4.2 yamt void
666 1.8.4.4 yamt kauth_cred_toucred(kauth_cred_t cred, struct ki_ucred *uc)
667 1.8.4.2 yamt {
668 1.8.4.2 yamt KASSERT(cred != NULL);
669 1.8.4.2 yamt KASSERT(uc != NULL);
670 1.8.4.2 yamt
671 1.8.4.3 yamt uc->cr_ref = cred->cr_refcnt;
672 1.8.4.2 yamt uc->cr_uid = cred->cr_euid;
673 1.8.4.2 yamt uc->cr_gid = cred->cr_egid;
674 1.8.4.2 yamt uc->cr_ngroups = min(cred->cr_ngroups,
675 1.8.4.2 yamt sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
676 1.8.4.2 yamt memcpy(uc->cr_groups, cred->cr_groups,
677 1.8.4.2 yamt uc->cr_ngroups * sizeof(uc->cr_groups[0]));
678 1.8.4.2 yamt }
679 1.8.4.2 yamt
680 1.8.4.2 yamt /*
681 1.8.4.3 yamt * Make a struct pcred out of a kauth_cred_t. For compatibility.
682 1.8.4.2 yamt */
683 1.8.4.2 yamt void
684 1.8.4.4 yamt kauth_cred_topcred(kauth_cred_t cred, struct ki_pcred *pc)
685 1.8.4.2 yamt {
686 1.8.4.2 yamt KASSERT(cred != NULL);
687 1.8.4.2 yamt KASSERT(pc != NULL);
688 1.8.4.2 yamt
689 1.8.4.4 yamt pc->p_pad = NULL;
690 1.8.4.2 yamt pc->p_ruid = cred->cr_uid;
691 1.8.4.2 yamt pc->p_svuid = cred->cr_svuid;
692 1.8.4.2 yamt pc->p_rgid = cred->cr_gid;
693 1.8.4.2 yamt pc->p_svgid = cred->cr_svgid;
694 1.8.4.2 yamt pc->p_refcnt = cred->cr_refcnt;
695 1.8.4.2 yamt }
696 1.8.4.2 yamt
697 1.8.4.2 yamt /*
698 1.8.4.3 yamt * Return kauth_cred_t for the current LWP.
699 1.8.4.2 yamt */
700 1.8.4.2 yamt kauth_cred_t
701 1.8.4.2 yamt kauth_cred_get(void)
702 1.8.4.2 yamt {
703 1.8.4.3 yamt return (curlwp->l_cred);
704 1.8.4.2 yamt }
705 1.8.4.2 yamt
706 1.8.4.2 yamt /*
707 1.8.4.2 yamt * Returns a scope matching the provided id.
708 1.8.4.2 yamt * Requires the scope list lock to be held by the caller.
709 1.8.4.2 yamt */
710 1.8.4.2 yamt static kauth_scope_t
711 1.8.4.2 yamt kauth_ifindscope(const char *id)
712 1.8.4.2 yamt {
713 1.8.4.2 yamt kauth_scope_t scope;
714 1.8.4.2 yamt
715 1.8.4.4 yamt KASSERT(rw_lock_held(&kauth_lock));
716 1.8.4.2 yamt
717 1.8.4.2 yamt scope = NULL;
718 1.8.4.2 yamt SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
719 1.8.4.2 yamt if (strcmp(scope->id, id) == 0)
720 1.8.4.2 yamt break;
721 1.8.4.2 yamt }
722 1.8.4.2 yamt
723 1.8.4.2 yamt return (scope);
724 1.8.4.2 yamt }
725 1.8.4.2 yamt
726 1.8.4.2 yamt /*
727 1.8.4.2 yamt * Register a new scope.
728 1.8.4.2 yamt *
729 1.8.4.2 yamt * id - identifier for the scope
730 1.8.4.2 yamt * callback - the scope's default listener
731 1.8.4.2 yamt * cookie - cookie to be passed to the listener(s)
732 1.8.4.2 yamt */
733 1.8.4.2 yamt kauth_scope_t
734 1.8.4.2 yamt kauth_register_scope(const char *id, kauth_scope_callback_t callback,
735 1.8.4.3 yamt void *cookie)
736 1.8.4.2 yamt {
737 1.8.4.2 yamt kauth_scope_t scope;
738 1.8.4.3 yamt kauth_listener_t listener = NULL; /* XXX gcc */
739 1.8.4.2 yamt
740 1.8.4.2 yamt /* Sanitize input */
741 1.8.4.3 yamt if (id == NULL)
742 1.8.4.2 yamt return (NULL);
743 1.8.4.2 yamt
744 1.8.4.2 yamt /* Allocate space for a new scope and listener. */
745 1.8.4.3 yamt scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
746 1.8.4.3 yamt if (scope == NULL)
747 1.8.4.3 yamt return NULL;
748 1.8.4.3 yamt if (callback != NULL) {
749 1.8.4.3 yamt listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
750 1.8.4.3 yamt if (listener == NULL) {
751 1.8.4.3 yamt kmem_free(scope, sizeof(*scope));
752 1.8.4.3 yamt return (NULL);
753 1.8.4.3 yamt }
754 1.8.4.3 yamt }
755 1.8.4.2 yamt
756 1.8.4.3 yamt /*
757 1.8.4.3 yamt * Acquire scope list lock.
758 1.8.4.3 yamt */
759 1.8.4.4 yamt rw_enter(&kauth_lock, RW_WRITER);
760 1.8.4.2 yamt
761 1.8.4.2 yamt /* Check we don't already have a scope with the same id */
762 1.8.4.2 yamt if (kauth_ifindscope(id) != NULL) {
763 1.8.4.4 yamt rw_exit(&kauth_lock);
764 1.8.4.2 yamt
765 1.8.4.3 yamt kmem_free(scope, sizeof(*scope));
766 1.8.4.3 yamt if (callback != NULL)
767 1.8.4.3 yamt kmem_free(listener, sizeof(*listener));
768 1.8.4.2 yamt
769 1.8.4.2 yamt return (NULL);
770 1.8.4.2 yamt }
771 1.8.4.2 yamt
772 1.8.4.2 yamt /* Initialize new scope with parameters */
773 1.8.4.2 yamt scope->id = id;
774 1.8.4.2 yamt scope->cookie = cookie;
775 1.8.4.2 yamt scope->nlisteners = 1;
776 1.8.4.2 yamt
777 1.8.4.2 yamt SIMPLEQ_INIT(&scope->listenq);
778 1.8.4.3 yamt
779 1.8.4.3 yamt /* Add default listener */
780 1.8.4.3 yamt if (callback != NULL) {
781 1.8.4.3 yamt listener->func = callback;
782 1.8.4.3 yamt listener->scope = scope;
783 1.8.4.3 yamt listener->refcnt = 0;
784 1.8.4.3 yamt SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
785 1.8.4.3 yamt }
786 1.8.4.2 yamt
787 1.8.4.2 yamt /* Insert scope to scopes list */
788 1.8.4.3 yamt SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
789 1.8.4.2 yamt
790 1.8.4.4 yamt rw_exit(&kauth_lock);
791 1.8.4.2 yamt
792 1.8.4.2 yamt return (scope);
793 1.8.4.2 yamt }
794 1.8.4.2 yamt
795 1.8.4.2 yamt /*
796 1.8.4.2 yamt * Initialize the kernel authorization subsystem.
797 1.8.4.2 yamt *
798 1.8.4.2 yamt * Initialize the scopes list lock.
799 1.8.4.4 yamt * Create specificdata domain.
800 1.8.4.4 yamt * Register the credentials scope, used in kauth(9) internally.
801 1.8.4.4 yamt * Register built-in scopes: generic, system, process, network, machdep, device.
802 1.8.4.2 yamt */
803 1.8.4.2 yamt void
804 1.8.4.2 yamt kauth_init(void)
805 1.8.4.2 yamt {
806 1.8.4.4 yamt rw_init(&kauth_lock);
807 1.8.4.4 yamt
808 1.8.4.7 yamt kauth_cred_cache = pool_cache_init(sizeof(struct kauth_cred),
809 1.8.4.7 yamt CACHE_LINE_SIZE, 0, 0, "kcredpl", NULL, IPL_NONE,
810 1.8.4.8 yamt NULL, NULL, NULL);
811 1.8.4.7 yamt
812 1.8.4.4 yamt /* Create specificdata domain. */
813 1.8.4.4 yamt kauth_domain = specificdata_domain_create();
814 1.8.4.4 yamt
815 1.8.4.4 yamt /* Register credentials scope. */
816 1.8.4.4 yamt kauth_builtin_scope_cred =
817 1.8.4.4 yamt kauth_register_scope(KAUTH_SCOPE_CRED, NULL, NULL);
818 1.8.4.2 yamt
819 1.8.4.2 yamt /* Register generic scope. */
820 1.8.4.2 yamt kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
821 1.8.4.3 yamt NULL, NULL);
822 1.8.4.3 yamt
823 1.8.4.3 yamt /* Register system scope. */
824 1.8.4.3 yamt kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
825 1.8.4.3 yamt NULL, NULL);
826 1.8.4.2 yamt
827 1.8.4.2 yamt /* Register process scope. */
828 1.8.4.2 yamt kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
829 1.8.4.3 yamt NULL, NULL);
830 1.8.4.3 yamt
831 1.8.4.3 yamt /* Register network scope. */
832 1.8.4.3 yamt kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
833 1.8.4.3 yamt NULL, NULL);
834 1.8.4.3 yamt
835 1.8.4.3 yamt /* Register machdep scope. */
836 1.8.4.3 yamt kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
837 1.8.4.3 yamt NULL, NULL);
838 1.8.4.3 yamt
839 1.8.4.3 yamt /* Register device scope. */
840 1.8.4.3 yamt kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
841 1.8.4.3 yamt NULL, NULL);
842 1.8.4.2 yamt }
843 1.8.4.2 yamt
844 1.8.4.2 yamt /*
845 1.8.4.2 yamt * Deregister a scope.
846 1.8.4.2 yamt * Requires scope list lock to be held by the caller.
847 1.8.4.2 yamt *
848 1.8.4.2 yamt * scope - the scope to deregister
849 1.8.4.2 yamt */
850 1.8.4.2 yamt void
851 1.8.4.2 yamt kauth_deregister_scope(kauth_scope_t scope)
852 1.8.4.2 yamt {
853 1.8.4.2 yamt if (scope != NULL) {
854 1.8.4.2 yamt /* Remove scope from list */
855 1.8.4.2 yamt SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
856 1.8.4.4 yamt kmem_free(scope, sizeof(*scope));
857 1.8.4.2 yamt }
858 1.8.4.2 yamt }
859 1.8.4.2 yamt
860 1.8.4.2 yamt /*
861 1.8.4.2 yamt * Register a listener.
862 1.8.4.2 yamt *
863 1.8.4.2 yamt * id - scope identifier.
864 1.8.4.2 yamt * callback - the callback routine for the listener.
865 1.8.4.2 yamt * cookie - cookie to pass unmoidfied to the callback.
866 1.8.4.2 yamt */
867 1.8.4.2 yamt kauth_listener_t
868 1.8.4.2 yamt kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
869 1.8.4.3 yamt void *cookie)
870 1.8.4.2 yamt {
871 1.8.4.2 yamt kauth_scope_t scope;
872 1.8.4.2 yamt kauth_listener_t listener;
873 1.8.4.2 yamt
874 1.8.4.4 yamt listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
875 1.8.4.4 yamt if (listener == NULL)
876 1.8.4.4 yamt return (NULL);
877 1.8.4.4 yamt
878 1.8.4.4 yamt rw_enter(&kauth_lock, RW_WRITER);
879 1.8.4.4 yamt
880 1.8.4.3 yamt /*
881 1.8.4.3 yamt * Find scope struct.
882 1.8.4.3 yamt */
883 1.8.4.2 yamt scope = kauth_ifindscope(id);
884 1.8.4.4 yamt if (scope == NULL) {
885 1.8.4.4 yamt rw_exit(&kauth_lock);
886 1.8.4.4 yamt kmem_free(listener, sizeof(*listener));
887 1.8.4.2 yamt return (NULL);
888 1.8.4.4 yamt }
889 1.8.4.2 yamt
890 1.8.4.2 yamt /* Allocate listener */
891 1.8.4.2 yamt
892 1.8.4.2 yamt /* Initialize listener with parameters */
893 1.8.4.2 yamt listener->func = callback;
894 1.8.4.2 yamt listener->refcnt = 0;
895 1.8.4.2 yamt
896 1.8.4.2 yamt /* Add listener to scope */
897 1.8.4.2 yamt SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
898 1.8.4.2 yamt
899 1.8.4.2 yamt /* Raise number of listeners on scope. */
900 1.8.4.2 yamt scope->nlisteners++;
901 1.8.4.2 yamt listener->scope = scope;
902 1.8.4.2 yamt
903 1.8.4.4 yamt rw_exit(&kauth_lock);
904 1.8.4.3 yamt
905 1.8.4.2 yamt return (listener);
906 1.8.4.2 yamt }
907 1.8.4.2 yamt
908 1.8.4.2 yamt /*
909 1.8.4.2 yamt * Deregister a listener.
910 1.8.4.2 yamt *
911 1.8.4.2 yamt * listener - listener reference as returned from kauth_listen_scope().
912 1.8.4.2 yamt */
913 1.8.4.2 yamt void
914 1.8.4.2 yamt kauth_unlisten_scope(kauth_listener_t listener)
915 1.8.4.2 yamt {
916 1.8.4.4 yamt
917 1.8.4.2 yamt if (listener != NULL) {
918 1.8.4.4 yamt rw_enter(&kauth_lock, RW_WRITER);
919 1.8.4.2 yamt SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
920 1.8.4.2 yamt kauth_listener, listener_next);
921 1.8.4.2 yamt listener->scope->nlisteners--;
922 1.8.4.4 yamt rw_exit(&kauth_lock);
923 1.8.4.4 yamt kmem_free(listener, sizeof(*listener));
924 1.8.4.2 yamt }
925 1.8.4.2 yamt }
926 1.8.4.2 yamt
927 1.8.4.2 yamt /*
928 1.8.4.2 yamt * Authorize a request.
929 1.8.4.2 yamt *
930 1.8.4.2 yamt * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
931 1.8.4.2 yamt * returned from kauth_register_scope().
932 1.8.4.2 yamt * credential - credentials of the user ("actor") making the request.
933 1.8.4.2 yamt * action - request identifier.
934 1.8.4.2 yamt * arg[0-3] - passed unmodified to listener(s).
935 1.8.4.2 yamt */
936 1.8.4.2 yamt int
937 1.8.4.2 yamt kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
938 1.8.4.2 yamt kauth_action_t action, void *arg0, void *arg1,
939 1.8.4.2 yamt void *arg2, void *arg3)
940 1.8.4.2 yamt {
941 1.8.4.2 yamt kauth_listener_t listener;
942 1.8.4.2 yamt int error, allow, fail;
943 1.8.4.2 yamt
944 1.8.4.3 yamt KASSERT(cred != NULL);
945 1.8.4.3 yamt KASSERT(action != 0);
946 1.8.4.3 yamt
947 1.8.4.3 yamt /* Short-circuit requests coming from the kernel. */
948 1.8.4.3 yamt if (cred == NOCRED || cred == FSCRED)
949 1.8.4.3 yamt return (0);
950 1.8.4.3 yamt
951 1.8.4.3 yamt KASSERT(scope != NULL);
952 1.8.4.3 yamt
953 1.8.4.2 yamt fail = 0;
954 1.8.4.2 yamt allow = 0;
955 1.8.4.4 yamt
956 1.8.4.4 yamt /* rw_enter(&kauth_lock, RW_READER); XXX not yet */
957 1.8.4.2 yamt SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
958 1.8.4.2 yamt error = listener->func(cred, action, scope->cookie, arg0,
959 1.8.4.4 yamt arg1, arg2, arg3);
960 1.8.4.2 yamt
961 1.8.4.2 yamt if (error == KAUTH_RESULT_ALLOW)
962 1.8.4.2 yamt allow = 1;
963 1.8.4.2 yamt else if (error == KAUTH_RESULT_DENY)
964 1.8.4.2 yamt fail = 1;
965 1.8.4.2 yamt }
966 1.8.4.4 yamt /* rw_exit(&kauth_lock); */
967 1.8.4.4 yamt
968 1.8.4.4 yamt if (fail)
969 1.8.4.4 yamt return (EPERM);
970 1.8.4.2 yamt
971 1.8.4.4 yamt if (allow)
972 1.8.4.4 yamt return (0);
973 1.8.4.4 yamt
974 1.8.4.4 yamt if (!nsecmodels)
975 1.8.4.4 yamt return (0);
976 1.8.4.4 yamt
977 1.8.4.4 yamt return (EPERM);
978 1.8.4.2 yamt };
979 1.8.4.2 yamt
980 1.8.4.2 yamt /*
981 1.8.4.2 yamt * Generic scope authorization wrapper.
982 1.8.4.2 yamt */
983 1.8.4.2 yamt int
984 1.8.4.2 yamt kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
985 1.8.4.2 yamt {
986 1.8.4.2 yamt return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
987 1.8.4.2 yamt action, arg0, NULL, NULL, NULL));
988 1.8.4.2 yamt }
989 1.8.4.2 yamt
990 1.8.4.2 yamt /*
991 1.8.4.3 yamt * System scope authorization wrapper.
992 1.8.4.2 yamt */
993 1.8.4.2 yamt int
994 1.8.4.3 yamt kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
995 1.8.4.3 yamt enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
996 1.8.4.2 yamt {
997 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_system, cred,
998 1.8.4.3 yamt action, (void *)req, arg1, arg2, arg3));
999 1.8.4.2 yamt }
1000 1.8.4.2 yamt
1001 1.8.4.2 yamt /*
1002 1.8.4.2 yamt * Process scope authorization wrapper.
1003 1.8.4.2 yamt */
1004 1.8.4.2 yamt int
1005 1.8.4.2 yamt kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
1006 1.8.4.2 yamt struct proc *p, void *arg1, void *arg2, void *arg3)
1007 1.8.4.2 yamt {
1008 1.8.4.2 yamt return (kauth_authorize_action(kauth_builtin_scope_process, cred,
1009 1.8.4.2 yamt action, p, arg1, arg2, arg3));
1010 1.8.4.2 yamt }
1011 1.8.4.3 yamt
1012 1.8.4.3 yamt /*
1013 1.8.4.3 yamt * Network scope authorization wrapper.
1014 1.8.4.3 yamt */
1015 1.8.4.3 yamt int
1016 1.8.4.3 yamt kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
1017 1.8.4.3 yamt enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
1018 1.8.4.3 yamt {
1019 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_network, cred,
1020 1.8.4.3 yamt action, (void *)req, arg1, arg2, arg3));
1021 1.8.4.3 yamt }
1022 1.8.4.3 yamt
1023 1.8.4.3 yamt int
1024 1.8.4.3 yamt kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
1025 1.8.4.3 yamt void *arg0, void *arg1, void *arg2, void *arg3)
1026 1.8.4.3 yamt {
1027 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
1028 1.8.4.3 yamt action, arg0, arg1, arg2, arg3));
1029 1.8.4.3 yamt }
1030 1.8.4.3 yamt
1031 1.8.4.3 yamt int
1032 1.8.4.3 yamt kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
1033 1.8.4.3 yamt void *arg0, void *arg1, void *arg2, void *arg3)
1034 1.8.4.3 yamt {
1035 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_device, cred,
1036 1.8.4.3 yamt action, arg0, arg1, arg2, arg3));
1037 1.8.4.3 yamt }
1038 1.8.4.3 yamt
1039 1.8.4.3 yamt int
1040 1.8.4.3 yamt kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
1041 1.8.4.3 yamt struct tty *tty)
1042 1.8.4.3 yamt {
1043 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_device, cred,
1044 1.8.4.3 yamt action, tty, NULL, NULL, NULL));
1045 1.8.4.3 yamt }
1046 1.8.4.3 yamt
1047 1.8.4.3 yamt int
1048 1.8.4.3 yamt kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
1049 1.8.4.3 yamt struct vnode *vp)
1050 1.8.4.3 yamt {
1051 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_device, cred,
1052 1.8.4.3 yamt KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
1053 1.8.4.3 yamt }
1054 1.8.4.3 yamt
1055 1.8.4.3 yamt int
1056 1.8.4.3 yamt kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
1057 1.8.4.3 yamt void *data)
1058 1.8.4.3 yamt {
1059 1.8.4.3 yamt return (kauth_authorize_action(kauth_builtin_scope_device, cred,
1060 1.8.4.3 yamt KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
1061 1.8.4.3 yamt data, NULL));
1062 1.8.4.3 yamt }
1063 1.8.4.4 yamt
1064 1.8.4.4 yamt static int
1065 1.8.4.4 yamt kauth_cred_hook(kauth_cred_t cred, kauth_action_t action, void *arg0,
1066 1.8.4.4 yamt void *arg1)
1067 1.8.4.4 yamt {
1068 1.8.4.4 yamt int r;
1069 1.8.4.4 yamt
1070 1.8.4.4 yamt r = kauth_authorize_action(kauth_builtin_scope_cred, cred, action,
1071 1.8.4.4 yamt arg0, arg1, NULL, NULL);
1072 1.8.4.4 yamt
1073 1.8.4.4 yamt #ifdef DIAGNOSTIC
1074 1.8.4.4 yamt if (!SIMPLEQ_EMPTY(&kauth_builtin_scope_cred->listenq))
1075 1.8.4.4 yamt KASSERT(r == 0);
1076 1.8.4.4 yamt #endif /* DIAGNOSTIC */
1077 1.8.4.4 yamt
1078 1.8.4.4 yamt return (r);
1079 1.8.4.4 yamt }
1080 1.8.4.4 yamt
1081 1.8.4.4 yamt void
1082 1.8.4.4 yamt secmodel_register(void)
1083 1.8.4.4 yamt {
1084 1.8.4.4 yamt KASSERT(nsecmodels + 1 != 0);
1085 1.8.4.4 yamt
1086 1.8.4.4 yamt rw_enter(&kauth_lock, RW_WRITER);
1087 1.8.4.4 yamt nsecmodels++;
1088 1.8.4.4 yamt rw_exit(&kauth_lock);
1089 1.8.4.4 yamt }
1090 1.8.4.4 yamt
1091 1.8.4.4 yamt void
1092 1.8.4.4 yamt secmodel_deregister(void)
1093 1.8.4.4 yamt {
1094 1.8.4.4 yamt KASSERT(nsecmodels != 0);
1095 1.8.4.4 yamt
1096 1.8.4.4 yamt rw_enter(&kauth_lock, RW_WRITER);
1097 1.8.4.4 yamt nsecmodels--;
1098 1.8.4.4 yamt rw_exit(&kauth_lock);
1099 1.8.4.4 yamt }
1100