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