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