Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.43
      1 /* $NetBSD: kern_auth.c,v 1.43 2007/02/07 08:04:48 elad 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.43 2007/02/07 08:04:48 elad 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/specificdata.h>
     42 
     43 /*
     44  * Secmodel-specific credentials.
     45  */
     46 struct kauth_key {
     47 	const char *ks_secmodel;	/* secmodel */
     48 	specificdata_key_t ks_key;	/* key */
     49 };
     50 
     51 /*
     52  * Credentials.
     53  *
     54  * A subset of this structure is used in kvm(3) (src/lib/libkvm/kvm_proc.c)
     55  * and should be synchronized with this structure when the update is
     56  * relevant.
     57  */
     58 struct kauth_cred {
     59 	struct simplelock cr_lock;	/* lock on cr_refcnt */
     60 	u_int cr_refcnt;		/* reference count */
     61 	uid_t cr_uid;			/* user id */
     62 	uid_t cr_euid;			/* effective user id */
     63 	uid_t cr_svuid;			/* saved effective user id */
     64 	gid_t cr_gid;			/* group id */
     65 	gid_t cr_egid;			/* effective group id */
     66 	gid_t cr_svgid;			/* saved effective group id */
     67 	u_int cr_ngroups;		/* number of groups */
     68 	gid_t cr_groups[NGROUPS];	/* group memberships */
     69 	specificdata_reference cr_sd;	/* specific data */
     70 };
     71 
     72 /*
     73  * Listener.
     74  */
     75 struct kauth_listener {
     76 	kauth_scope_callback_t		func;		/* callback */
     77 	kauth_scope_t			scope;		/* scope backpointer */
     78 	u_int				refcnt;		/* reference count */
     79 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     80 };
     81 
     82 /*
     83  * Scope.
     84  */
     85 struct kauth_scope {
     86 	const char		       *id;		/* scope name */
     87 	void			       *cookie;		/* user cookie */
     88 	u_int				nlisteners;	/* # of listeners */
     89 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     90 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     91 };
     92 
     93 static int kauth_cred_hook(kauth_cred_t, kauth_action_t, void *, void *);
     94 
     95 static POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     96     "kauthcredpl", &pool_allocator_nointr);
     97 
     98 /* List of scopes and its lock. */
     99 static SIMPLEQ_HEAD(, kauth_scope) scope_list;
    100 static struct simplelock scopes_lock;
    101 
    102 /* Built-in scopes: generic, process. */
    103 static kauth_scope_t kauth_builtin_scope_generic;
    104 static kauth_scope_t kauth_builtin_scope_system;
    105 static kauth_scope_t kauth_builtin_scope_process;
    106 static kauth_scope_t kauth_builtin_scope_network;
    107 static kauth_scope_t kauth_builtin_scope_machdep;
    108 static kauth_scope_t kauth_builtin_scope_device;
    109 static kauth_scope_t kauth_builtin_scope_cred;
    110 
    111 static unsigned int nsecmodels = 0;
    112 
    113 static specificdata_domain_t kauth_domain;
    114 
    115 /* Allocate new, empty kauth credentials. */
    116 kauth_cred_t
    117 kauth_cred_alloc(void)
    118 {
    119 	kauth_cred_t cred;
    120 
    121 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    122 	memset(cred, 0, sizeof(*cred));
    123 	simple_lock_init(&cred->cr_lock);
    124 	cred->cr_refcnt = 1;
    125 	specificdata_init(kauth_domain, &cred->cr_sd);
    126 	kauth_cred_hook(cred, KAUTH_CRED_INIT, NULL, NULL);
    127 
    128 	return (cred);
    129 }
    130 
    131 /* Increment reference count to cred. */
    132 void
    133 kauth_cred_hold(kauth_cred_t cred)
    134 {
    135 	KASSERT(cred != NULL);
    136 	KASSERT(cred->cr_refcnt > 0);
    137 
    138         simple_lock(&cred->cr_lock);
    139         cred->cr_refcnt++;
    140         simple_unlock(&cred->cr_lock);
    141 }
    142 
    143 /* Decrease reference count to cred. If reached zero, free it. */
    144 void
    145 kauth_cred_free(kauth_cred_t cred)
    146 {
    147 	u_int refcnt;
    148 
    149 	KASSERT(cred != NULL);
    150 	KASSERT(cred->cr_refcnt > 0);
    151 
    152 	simple_lock(&cred->cr_lock);
    153 	refcnt = --cred->cr_refcnt;
    154 	simple_unlock(&cred->cr_lock);
    155 
    156 	if (refcnt == 0) {
    157 		kauth_cred_hook(cred, KAUTH_CRED_FREE, NULL, NULL);
    158 		specificdata_fini(kauth_domain, &cred->cr_sd);
    159 		pool_put(&kauth_cred_pool, cred);
    160 	}
    161 }
    162 
    163 void
    164 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    165 {
    166 	KASSERT(from != NULL);
    167 	KASSERT(to != NULL);
    168 	KASSERT(from->cr_refcnt > 0);
    169 
    170 	to->cr_uid = from->cr_uid;
    171 	to->cr_euid = from->cr_euid;
    172 	to->cr_svuid = from->cr_svuid;
    173 	to->cr_gid = from->cr_gid;
    174 	to->cr_egid = from->cr_egid;
    175 	to->cr_svgid = from->cr_svgid;
    176 	to->cr_ngroups = from->cr_ngroups;
    177 	memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
    178 
    179 	kauth_cred_hook(from, KAUTH_CRED_COPY, to, NULL);
    180 }
    181 
    182 /*
    183  * Duplicate cred and return a new kauth_cred_t.
    184  */
    185 kauth_cred_t
    186 kauth_cred_dup(kauth_cred_t cred)
    187 {
    188 	kauth_cred_t new_cred;
    189 
    190 	KASSERT(cred != NULL);
    191 	KASSERT(cred->cr_refcnt > 0);
    192 
    193 	new_cred = kauth_cred_alloc();
    194 
    195 	kauth_cred_clone(cred, new_cred);
    196 
    197 	return (new_cred);
    198 }
    199 
    200 /*
    201  * Similar to crcopy(), only on a kauth_cred_t.
    202  * XXX: Is this even needed? [kauth_cred_copy]
    203  */
    204 kauth_cred_t
    205 kauth_cred_copy(kauth_cred_t cred)
    206 {
    207 	kauth_cred_t new_cred;
    208 
    209 	KASSERT(cred != NULL);
    210 	KASSERT(cred->cr_refcnt > 0);
    211 
    212 	/* If the provided credentials already have one reference, use them. */
    213 	if (cred->cr_refcnt == 1)
    214 		return (cred);
    215 
    216 	new_cred = kauth_cred_alloc();
    217 
    218 	kauth_cred_clone(cred, new_cred);
    219 
    220 	kauth_cred_free(cred);
    221 
    222 	return (new_cred);
    223 }
    224 
    225 void
    226 kauth_proc_fork(struct proc *parent, struct proc *child)
    227 {
    228 	/* mutex_enter(&parent->p_mutex); */
    229 	kauth_cred_hold(parent->p_cred);
    230 	child->p_cred = parent->p_cred;
    231 	/* mutex_exit(&parent->p_mutex); */
    232 
    233 	kauth_cred_hook(parent->p_cred, KAUTH_CRED_FORK, parent,
    234 	    child);
    235 }
    236 
    237 uid_t
    238 kauth_cred_getuid(kauth_cred_t cred)
    239 {
    240 	KASSERT(cred != NULL);
    241 
    242 	return (cred->cr_uid);
    243 }
    244 
    245 uid_t
    246 kauth_cred_geteuid(kauth_cred_t cred)
    247 {
    248 	KASSERT(cred != NULL);
    249 
    250 	return (cred->cr_euid);
    251 }
    252 
    253 uid_t
    254 kauth_cred_getsvuid(kauth_cred_t cred)
    255 {
    256 	KASSERT(cred != NULL);
    257 
    258 	return (cred->cr_svuid);
    259 }
    260 
    261 gid_t
    262 kauth_cred_getgid(kauth_cred_t cred)
    263 {
    264 	KASSERT(cred != NULL);
    265 
    266 	return (cred->cr_gid);
    267 }
    268 
    269 gid_t
    270 kauth_cred_getegid(kauth_cred_t cred)
    271 {
    272 	KASSERT(cred != NULL);
    273 
    274 	return (cred->cr_egid);
    275 }
    276 
    277 gid_t
    278 kauth_cred_getsvgid(kauth_cred_t cred)
    279 {
    280 	KASSERT(cred != NULL);
    281 
    282 	return (cred->cr_svgid);
    283 }
    284 
    285 void
    286 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    287 {
    288 	KASSERT(cred != NULL);
    289 	KASSERT(cred->cr_refcnt == 1);
    290 
    291 	cred->cr_uid = uid;
    292 }
    293 
    294 void
    295 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    296 {
    297 	KASSERT(cred != NULL);
    298 	KASSERT(cred->cr_refcnt == 1);
    299 
    300 	cred->cr_euid = uid;
    301 }
    302 
    303 void
    304 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    305 {
    306 	KASSERT(cred != NULL);
    307 	KASSERT(cred->cr_refcnt == 1);
    308 
    309 	cred->cr_svuid = uid;
    310 }
    311 
    312 void
    313 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    314 {
    315 	KASSERT(cred != NULL);
    316 	KASSERT(cred->cr_refcnt == 1);
    317 
    318 	cred->cr_gid = gid;
    319 }
    320 
    321 void
    322 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    323 {
    324 	KASSERT(cred != NULL);
    325 	KASSERT(cred->cr_refcnt == 1);
    326 
    327 	cred->cr_egid = gid;
    328 }
    329 
    330 void
    331 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    332 {
    333 	KASSERT(cred != NULL);
    334 	KASSERT(cred->cr_refcnt == 1);
    335 
    336 	cred->cr_svgid = gid;
    337 }
    338 
    339 /* Checks if gid is a member of the groups in cred. */
    340 int
    341 kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    342 {
    343 	int i;
    344 
    345 	KASSERT(cred != NULL);
    346 	KASSERT(resultp != NULL);
    347 
    348 	*resultp = 0;
    349 
    350 	for (i = 0; i < cred->cr_ngroups; i++)
    351 		if (cred->cr_groups[i] == gid) {
    352 			*resultp = 1;
    353 			break;
    354 		}
    355 
    356 	return (0);
    357 }
    358 
    359 u_int
    360 kauth_cred_ngroups(kauth_cred_t cred)
    361 {
    362 	KASSERT(cred != NULL);
    363 
    364 	return (cred->cr_ngroups);
    365 }
    366 
    367 /*
    368  * Return the group at index idx from the groups in cred.
    369  */
    370 gid_t
    371 kauth_cred_group(kauth_cred_t cred, u_int idx)
    372 {
    373 	KASSERT(cred != NULL);
    374 	KASSERT(idx < cred->cr_ngroups);
    375 
    376 	return (cred->cr_groups[idx]);
    377 }
    378 
    379 /* XXX elad: gmuid is unused for now. */
    380 int
    381 kauth_cred_setgroups(kauth_cred_t cred, gid_t *grbuf, size_t len, uid_t gmuid)
    382 {
    383 	KASSERT(cred != NULL);
    384 	KASSERT(cred->cr_refcnt == 1);
    385 	KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
    386 
    387 	if (len)
    388 		memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
    389 	memset(cred->cr_groups + len, 0xff,
    390 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    391 
    392 	cred->cr_ngroups = len;
    393 
    394 	return (0);
    395 }
    396 
    397 int
    398 kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
    399 {
    400 	KASSERT(cred != NULL);
    401 	KASSERT(len <= cred->cr_ngroups);
    402 
    403 	memset(grbuf, 0xff, sizeof(*grbuf) * len);
    404 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    405 
    406 	return (0);
    407 }
    408 
    409 int
    410 kauth_register_key(const char *secmodel, kauth_key_t *result)
    411 {
    412 	kauth_key_t k;
    413 	specificdata_key_t key;
    414 	int error;
    415 
    416 	KASSERT(result != NULL);
    417 
    418 	error = specificdata_key_create(kauth_domain, &key, NULL);
    419 	if (error)
    420 		return (error);
    421 
    422 	k = kmem_alloc(sizeof(*k), KM_SLEEP);
    423 	k->ks_secmodel = secmodel;
    424 	k->ks_key = key;
    425 
    426 	*result = k;
    427 
    428 	return (0);
    429 }
    430 
    431 int
    432 kauth_deregister_key(kauth_key_t key)
    433 {
    434 	KASSERT(key != NULL);
    435 
    436 	specificdata_key_delete(kauth_domain, key->ks_key);
    437 	kmem_free(key, sizeof(*key));
    438 
    439 	return (0);
    440 }
    441 
    442 void *
    443 kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)
    444 {
    445 	KASSERT(cred != NULL);
    446 	KASSERT(key != NULL);
    447 
    448 	return (specificdata_getspecific(kauth_domain, &cred->cr_sd,
    449 	    key->ks_key));
    450 }
    451 
    452 void
    453 kauth_cred_setdata(kauth_cred_t cred, kauth_key_t key, void *data)
    454 {
    455 	KASSERT(cred != NULL);
    456 	KASSERT(key != NULL);
    457 
    458 	specificdata_setspecific(kauth_domain, &cred->cr_sd, key->ks_key, data);
    459 }
    460 
    461 /*
    462  * Match uids in two credentials.
    463  */
    464 int
    465 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    466 {
    467 	KASSERT(cred1 != NULL);
    468 	KASSERT(cred2 != NULL);
    469 
    470 	if (cred1->cr_uid == cred2->cr_uid ||
    471 	    cred1->cr_euid == cred2->cr_uid ||
    472 	    cred1->cr_uid == cred2->cr_euid ||
    473 	    cred1->cr_euid == cred2->cr_euid)
    474 		return (1);
    475 
    476 	return (0);
    477 }
    478 
    479 u_int
    480 kauth_cred_getrefcnt(kauth_cred_t cred)
    481 {
    482 	KASSERT(cred != NULL);
    483 
    484 	return (cred->cr_refcnt);
    485 }
    486 
    487 /*
    488  * Convert userland credentials (struct uucred) to kauth_cred_t.
    489  * XXX: For NFS & puffs
    490  */
    491 void
    492 kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    493 {
    494 	KASSERT(cred != NULL);
    495 	KASSERT(uuc != NULL);
    496 
    497 	cred->cr_refcnt = 1;
    498 	cred->cr_uid = uuc->cr_uid;
    499 	cred->cr_euid = uuc->cr_uid;
    500 	cred->cr_svuid = uuc->cr_uid;
    501 	cred->cr_gid = uuc->cr_gid;
    502 	cred->cr_egid = uuc->cr_gid;
    503 	cred->cr_svgid = uuc->cr_gid;
    504 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    505 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    506 	    cred->cr_ngroups, -1);
    507 }
    508 
    509 /*
    510  * Convert kauth_cred_t to userland credentials (struct uucred).
    511  * XXX: For NFS & puffs
    512  */
    513 void
    514 kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    515 {
    516 	KASSERT(cred != NULL);
    517 	KASSERT(uuc != NULL);
    518 	int ng;
    519 
    520 	ng = min(cred->cr_ngroups, NGROUPS);
    521 	uuc->cr_uid = cred->cr_euid;
    522 	uuc->cr_gid = cred->cr_egid;
    523 	uuc->cr_ngroups = ng;
    524 	kauth_cred_getgroups(cred, uuc->cr_groups, ng);
    525 }
    526 
    527 /*
    528  * Compare kauth_cred_t and uucred credentials.
    529  * XXX: Modelled after crcmp() for NFS.
    530  */
    531 int
    532 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    533 {
    534 	KASSERT(cred != NULL);
    535 	KASSERT(uuc != NULL);
    536 
    537 	if (cred->cr_euid == uuc->cr_uid &&
    538 	    cred->cr_egid == uuc->cr_gid &&
    539 	    cred->cr_ngroups == uuc->cr_ngroups) {
    540 		int i;
    541 
    542 		/* Check if all groups from uuc appear in cred. */
    543 		for (i = 0; i < uuc->cr_ngroups; i++) {
    544 			int ismember;
    545 
    546 			ismember = 0;
    547 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    548 			    &ismember) != 0 || !ismember)
    549 				return (1);
    550 		}
    551 
    552 		return (0);
    553 	}
    554 
    555 	return (1);
    556 }
    557 
    558 /*
    559  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    560  */
    561 void
    562 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    563 {
    564 	KASSERT(cred != NULL);
    565 	KASSERT(uc != NULL);
    566 
    567 	uc->cr_ref = cred->cr_refcnt;
    568 	uc->cr_uid = cred->cr_euid;
    569 	uc->cr_gid = cred->cr_egid;
    570 	uc->cr_ngroups = min(cred->cr_ngroups,
    571 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    572 	memcpy(uc->cr_groups, cred->cr_groups,
    573 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    574 }
    575 
    576 /*
    577  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    578  */
    579 void
    580 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    581 {
    582 	KASSERT(cred != NULL);
    583 	KASSERT(pc != NULL);
    584 
    585 	pc->pc_ucred = NULL;
    586 	pc->p_ruid = cred->cr_uid;
    587 	pc->p_svuid = cred->cr_svuid;
    588 	pc->p_rgid = cred->cr_gid;
    589 	pc->p_svgid = cred->cr_svgid;
    590 	pc->p_refcnt = cred->cr_refcnt;
    591 }
    592 
    593 /*
    594  * Return kauth_cred_t for the current LWP.
    595  */
    596 kauth_cred_t
    597 kauth_cred_get(void)
    598 {
    599 	return (curlwp->l_cred);
    600 }
    601 
    602 /*
    603  * Returns a scope matching the provided id.
    604  * Requires the scope list lock to be held by the caller.
    605  */
    606 static kauth_scope_t
    607 kauth_ifindscope(const char *id)
    608 {
    609 	kauth_scope_t scope;
    610 
    611 	/* XXX: assert lock on scope list? */
    612 
    613 	scope = NULL;
    614 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    615 		if (strcmp(scope->id, id) == 0)
    616 			break;
    617 	}
    618 
    619 	return (scope);
    620 }
    621 
    622 /*
    623  * Register a new scope.
    624  *
    625  * id - identifier for the scope
    626  * callback - the scope's default listener
    627  * cookie - cookie to be passed to the listener(s)
    628  */
    629 kauth_scope_t
    630 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    631     void *cookie)
    632 {
    633 	kauth_scope_t scope;
    634 	kauth_listener_t listener = NULL; /* XXX gcc */
    635 
    636 	/* Sanitize input */
    637 	if (id == NULL)
    638 		return (NULL);
    639 
    640 	/* Allocate space for a new scope and listener. */
    641 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    642 	if (scope == NULL)
    643 		return NULL;
    644 	if (callback != NULL) {
    645 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    646 		if (listener == NULL) {
    647 			kmem_free(scope, sizeof(*scope));
    648 			return (NULL);
    649 		}
    650 	}
    651 
    652 	/*
    653 	 * Acquire scope list lock.
    654 	 *
    655 	 * XXXSMP insufficient locking.
    656 	 */
    657 	simple_lock(&scopes_lock);
    658 
    659 	/* Check we don't already have a scope with the same id */
    660 	if (kauth_ifindscope(id) != NULL) {
    661 		simple_unlock(&scopes_lock);
    662 
    663 		kmem_free(scope, sizeof(*scope));
    664 		if (callback != NULL)
    665 			kmem_free(listener, sizeof(*listener));
    666 
    667 		return (NULL);
    668 	}
    669 
    670 	/* Initialize new scope with parameters */
    671 	scope->id = id;
    672 	scope->cookie = cookie;
    673 	scope->nlisteners = 1;
    674 
    675 	SIMPLEQ_INIT(&scope->listenq);
    676 
    677 	/* Add default listener */
    678 	if (callback != NULL) {
    679 		listener->func = callback;
    680 		listener->scope = scope;
    681 		listener->refcnt = 0;
    682 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    683 	}
    684 
    685 	/* Insert scope to scopes list */
    686 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    687 
    688 	simple_unlock(&scopes_lock);
    689 
    690 	return (scope);
    691 }
    692 
    693 /*
    694  * Initialize the kernel authorization subsystem.
    695  *
    696  * Initialize the scopes list lock.
    697  * Create specificdata domain.
    698  * Register the credentials scope, used in kauth(9) internally.
    699  * Register built-in scopes: generic, system, process, network, machdep, device.
    700  */
    701 void
    702 kauth_init(void)
    703 {
    704 	SIMPLEQ_INIT(&scope_list);
    705 	simple_lock_init(&scopes_lock);
    706 
    707 	/* Create specificdata domain. */
    708 	kauth_domain = specificdata_domain_create();
    709 
    710 	/* Register credentials scope. */
    711 	kauth_builtin_scope_cred =
    712 	    kauth_register_scope(KAUTH_SCOPE_CRED, NULL, NULL);
    713 
    714 	/* Register generic scope. */
    715 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    716 	    NULL, NULL);
    717 
    718 	/* Register system scope. */
    719 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    720 	    NULL, NULL);
    721 
    722 	/* Register process scope. */
    723 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    724 	    NULL, NULL);
    725 
    726 	/* Register network scope. */
    727 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    728 	    NULL, NULL);
    729 
    730 	/* Register machdep scope. */
    731 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    732 	    NULL, NULL);
    733 
    734 	/* Register device scope. */
    735 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    736 	    NULL, NULL);
    737 }
    738 
    739 /*
    740  * Deregister a scope.
    741  * Requires scope list lock to be held by the caller.
    742  *
    743  * scope - the scope to deregister
    744  */
    745 void
    746 kauth_deregister_scope(kauth_scope_t scope)
    747 {
    748 	if (scope != NULL) {
    749 		/* Remove scope from list */
    750 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    751 		kmem_free(scope, sizeof(*scope));
    752 	}
    753 }
    754 
    755 /*
    756  * Register a listener.
    757  *
    758  * id - scope identifier.
    759  * callback - the callback routine for the listener.
    760  * cookie - cookie to pass unmoidfied to the callback.
    761  */
    762 kauth_listener_t
    763 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    764    void *cookie)
    765 {
    766 	kauth_scope_t scope;
    767 	kauth_listener_t listener;
    768 
    769 	/*
    770 	 * Find scope struct.
    771 	 *
    772 	 * XXXSMP insufficient locking.
    773 	 */
    774 	simple_lock(&scopes_lock);
    775 	scope = kauth_ifindscope(id);
    776 	simple_unlock(&scopes_lock);
    777 	if (scope == NULL)
    778 		return (NULL);
    779 
    780 	/* Allocate listener */
    781 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    782 	if (listener == NULL)
    783 		return (NULL);
    784 
    785 	/* Initialize listener with parameters */
    786 	listener->func = callback;
    787 	listener->refcnt = 0;
    788 
    789 	/* Add listener to scope */
    790 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    791 
    792 	/* Raise number of listeners on scope. */
    793 	scope->nlisteners++;
    794 	listener->scope = scope;
    795 
    796 	return (listener);
    797 }
    798 
    799 /*
    800  * Deregister a listener.
    801  *
    802  * listener - listener reference as returned from kauth_listen_scope().
    803  */
    804 void
    805 kauth_unlisten_scope(kauth_listener_t listener)
    806 {
    807 	if (listener != NULL) {
    808 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    809 		    kauth_listener, listener_next);
    810 		listener->scope->nlisteners--;
    811 		kmem_free(listener, sizeof(*listener));
    812 	}
    813 }
    814 
    815 /*
    816  * Authorize a request.
    817  *
    818  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    819  *	   returned from kauth_register_scope().
    820  * credential - credentials of the user ("actor") making the request.
    821  * action - request identifier.
    822  * arg[0-3] - passed unmodified to listener(s).
    823  */
    824 int
    825 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    826 		       kauth_action_t action, void *arg0, void *arg1,
    827 		       void *arg2, void *arg3)
    828 {
    829 	kauth_listener_t listener;
    830 	int error, allow, fail;
    831 
    832 #if 0 /* defined(LOCKDEBUG) */
    833 	spinlock_switchcheck();
    834 	simple_lock_only_held(NULL, "kauth_authorize_action");
    835 #endif
    836 
    837 	KASSERT(cred != NULL);
    838 	KASSERT(action != 0);
    839 
    840 	/* Short-circuit requests coming from the kernel. */
    841 	if (cred == NOCRED || cred == FSCRED)
    842 		return (0);
    843 
    844 	KASSERT(scope != NULL);
    845 
    846 	fail = 0;
    847 	allow = 0;
    848 
    849 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    850 		error = listener->func(cred, action, scope->cookie, arg0,
    851 				       arg1, arg2, arg3);
    852 
    853 		if (error == KAUTH_RESULT_ALLOW)
    854 			allow = 1;
    855 		else if (error == KAUTH_RESULT_DENY)
    856 			fail = 1;
    857 	}
    858 
    859 	if (fail)
    860 		return (EPERM);
    861 
    862 	if (allow)
    863 		return (0);
    864 
    865 	if (!nsecmodels)
    866 		return (0);
    867 
    868 	return (EPERM);
    869 };
    870 
    871 /*
    872  * Generic scope authorization wrapper.
    873  */
    874 int
    875 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    876 {
    877 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    878 	    action, arg0, NULL, NULL, NULL));
    879 }
    880 
    881 /*
    882  * System scope authorization wrapper.
    883  */
    884 int
    885 kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    886     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    887 {
    888 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    889 	    action, (void *)req, arg1, arg2, arg3));
    890 }
    891 
    892 /*
    893  * Process scope authorization wrapper.
    894  */
    895 int
    896 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    897     struct proc *p, void *arg1, void *arg2, void *arg3)
    898 {
    899 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    900 	    action, p, arg1, arg2, arg3));
    901 }
    902 
    903 /*
    904  * Network scope authorization wrapper.
    905  */
    906 int
    907 kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
    908     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
    909 {
    910 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
    911 	    action, (void *)req, arg1, arg2, arg3));
    912 }
    913 
    914 int
    915 kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
    916     void *arg0, void *arg1, void *arg2, void *arg3)
    917 {
    918 	return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
    919 	    action, arg0, arg1, arg2, arg3));
    920 }
    921 
    922 int
    923 kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
    924     void *arg0, void *arg1, void *arg2, void *arg3)
    925 {
    926 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    927 	    action, arg0, arg1, arg2, arg3));
    928 }
    929 
    930 int
    931 kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
    932     struct tty *tty)
    933 {
    934 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    935 	    action, tty, NULL, NULL, NULL));
    936 }
    937 
    938 int
    939 kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
    940     struct vnode *vp)
    941 {
    942 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    943 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
    944 }
    945 
    946 int
    947 kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
    948     void *data)
    949 {
    950 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    951 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
    952 	    data, NULL));
    953 }
    954 
    955 static int
    956 kauth_cred_hook(kauth_cred_t cred, kauth_action_t action, void *arg0,
    957     void *arg1)
    958 {
    959 	int r;
    960 
    961 	r = kauth_authorize_action(kauth_builtin_scope_cred, cred, action,
    962 	    arg0, arg1, NULL, NULL);
    963 
    964 #ifdef DIAGNOSTIC
    965 	if (!SIMPLEQ_EMPTY(&kauth_builtin_scope_cred->listenq))
    966 		KASSERT(r == 0);
    967 #endif /* DIAGNOSTIC */
    968 
    969 	return (r);
    970 }
    971 
    972 void
    973 secmodel_register(void)
    974 {
    975 	KASSERT(nsecmodels + 1 != 0);
    976 
    977 	nsecmodels++;
    978 }
    979 
    980 void
    981 secmodel_deregister(void)
    982 {
    983 	KASSERT(nsecmodels != 0);
    984 
    985 	nsecmodels--;
    986 }
    987