Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.1.2.10
      1 /* $NetBSD: kern_auth.c,v 1.1.2.10 2006/03/09 23:47:07 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Elad Efrat.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Todo:
     35  *   - Garbage collection to pool_put() unused scopes/listeners.
     36  */
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/queue.h>
     41 #include <sys/time.h>
     42 #include <sys/proc.h>
     43 #include <sys/ucred.h>
     44 #include <sys/pool.h>
     45 #include <sys/kauth.h>
     46 #include <sys/acct.h>
     47 
     48 /*
     49  * Credentials.
     50  */
     51 struct kauth_cred {
     52 	struct simplelock cr_lock;	/* lock */
     53 	uint32_t cr_refcnt;		/* reference count */
     54 	uid_t cr_uid;			/* user id */
     55 	uid_t cr_euid;			/* effective user id */
     56 	uid_t cr_svuid;			/* saved effective user id */
     57 	gid_t cr_gid;			/* group id */
     58 	gid_t cr_egid;			/* effective group id */
     59 	gid_t cr_svgid;			/* saved effective group id */
     60 	uint16_t cr_ngroups;		/* number of groups */
     61 	gid_t cr_groups[NGROUPS];	/* group memberships */
     62 };
     63 
     64 /*
     65  * Listener.
     66  */
     67 struct kauth_listener {
     68 	kauth_scope_callback_t		func;		/* callback */
     69 	kauth_scope_t			scope;		/* scope backpointer */
     70 	uint32_t			refcnt;		/* reference count */
     71 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     72 };
     73 
     74 /*
     75  * Scope.
     76  */
     77 struct kauth_scope {
     78 	const char		       *id;		/* scope name */
     79 	void			       *cookie;		/* user cookie */
     80 	uint32_t			nlisteners;	/* # of listeners */
     81 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     82 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     83 };
     84 
     85 POOL_INIT(kauth_scope_pool, sizeof(struct kauth_scope), 0, 0, 0,
     86 	  "kauth_scopepl", &pool_allocator_nointr);
     87 POOL_INIT(kauth_listener_pool, sizeof(struct kauth_listener), 0, 0, 0,
     88 	  "kauth_listenerpl", &pool_allocator_nointr);
     89 POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     90 	  "kauth_credpl", &pool_allocator_nointr);
     91 
     92 /* List of scopes and its lock. */
     93 SIMPLEQ_HEAD(, kauth_scope) scope_list;
     94 struct simplelock scopes_lock;
     95 
     96 /* Built-in scopes: generic, process. */
     97 static kauth_scope_t kauth_builtin_scope_generic;
     98 static kauth_scope_t kauth_builtin_scope_process;
     99 
    100 /* Allocate new, empty kauth credentials. */
    101 kauth_cred_t
    102 kauth_cred_alloc(void) {
    103 	kauth_cred_t cred;
    104 
    105 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    106 	memset(cred, 0, sizeof(*cred));
    107 	simple_lock_init(&cred->cr_lock);
    108 	cred->cr_refcnt = 1;
    109 
    110 	return (cred);
    111 }
    112 
    113 /* Increment reference count to cred. */
    114 void
    115 kauth_cred_hold(kauth_cred_t cred)
    116 {
    117 	KASSERT(cred != NULL);
    118 
    119         simple_lock(&cred->cr_lock);
    120         cred->cr_refcnt++;
    121         simple_unlock(&cred->cr_lock);
    122 }
    123 
    124 void
    125 kauth_cred_destroy(kauth_cred_t cred)
    126 {
    127 	KASSERT(cred != NULL);
    128 
    129 	pool_put(&kauth_cred_pool, cred);
    130 }
    131 
    132 /* Decrease reference count to cred. If reached zero, free it. */
    133 void
    134 kauth_cred_free(kauth_cred_t cred) {
    135 	KASSERT(cred != NULL);
    136 
    137 	simple_lock(&cred->cr_lock);
    138 	cred->cr_refcnt--;
    139 	simple_unlock(&cred->cr_lock);
    140 
    141 	if (cred->cr_refcnt == 0)
    142 		kauth_cred_destroy(cred);
    143 }
    144 
    145 void
    146 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    147 {
    148 	KASSERT(from != NULL);
    149 	KASSERT(to != NULL);
    150 
    151 	to->cr_uid = from->cr_uid;
    152 	to->cr_euid = from->cr_euid;
    153 	to->cr_svuid = from->cr_svuid;
    154 	to->cr_gid = from->cr_gid;
    155 	to->cr_egid = from->cr_egid;
    156 	to->cr_svgid = from->cr_svgid;
    157 	to->cr_ngroups = from->cr_ngroups;
    158 	memcpy(to->cr_groups, from->cr_groups,
    159 	       sizeof(to->cr_groups));
    160 }
    161 
    162 /*
    163  * Duplicate cred and return a new kauth_cred_t.
    164  */
    165 kauth_cred_t
    166 kauth_cred_dup(kauth_cred_t cred)
    167 {
    168 	kauth_cred_t new_cred;
    169 
    170 	KASSERT(cred != NULL);
    171 
    172 	new_cred = kauth_cred_alloc();
    173 
    174 	kauth_cred_clone(cred, new_cred);
    175 
    176 	return (new_cred);
    177 }
    178 
    179 /*
    180  * Similar to crcopy(), only on a kauth_cred_t.
    181  * XXX: Is this even needed? [kauth_cred_copy]
    182  */
    183 kauth_cred_t
    184 kauth_cred_copy(kauth_cred_t cred)
    185 {
    186 	kauth_cred_t new_cred;
    187 
    188 	KASSERT(cred != NULL);
    189 
    190 	/* If the provided credentials already have one reference, use them. */
    191 	if (cred->cr_refcnt == 1)
    192 		return (cred);
    193 
    194 	new_cred = kauth_cred_alloc();
    195 
    196 	kauth_cred_clone(cred, new_cred);
    197 
    198 	kauth_cred_free(cred);
    199 
    200 	return (new_cred);
    201 }
    202 
    203 /*
    204  * Zero a kauth_cred_t, and re-initialize the lock.
    205  * XXX: Is this needed? [kauth_cred_zero]
    206  */
    207 void
    208 kauth_cred_zero(kauth_cred_t cred)
    209 {
    210 	KASSERT(cred != NULL);
    211 
    212 	memset(cred, 0, sizeof(*cred));
    213 	simple_lock_init(&cred->cr_lock);
    214 }
    215 
    216 uid_t
    217 kauth_cred_getuid(kauth_cred_t cred)
    218 {
    219 	KASSERT(cred != NULL);
    220 
    221 	return (cred->cr_uid);
    222 }
    223 
    224 uid_t
    225 kauth_cred_geteuid(kauth_cred_t cred)
    226 {
    227 	KASSERT(cred != NULL);
    228 
    229 	return (cred->cr_euid);
    230 }
    231 
    232 uid_t
    233 kauth_cred_getsvuid(kauth_cred_t cred)
    234 {
    235 	KASSERT(cred != NULL);
    236 
    237 	return (cred->cr_svuid);
    238 }
    239 
    240 gid_t
    241 kauth_cred_getgid(kauth_cred_t cred)
    242 {
    243 	KASSERT(cred != NULL);
    244 
    245 	return (cred->cr_gid);
    246 }
    247 
    248 gid_t
    249 kauth_cred_getegid(kauth_cred_t cred)
    250 {
    251 	KASSERT(cred != NULL);
    252 
    253 	return (cred->cr_egid);
    254 }
    255 
    256 gid_t
    257 kauth_cred_getsvgid(kauth_cred_t cred)
    258 {
    259 	KASSERT(cred != NULL);
    260 
    261 	return (cred->cr_svgid);
    262 }
    263 
    264 void
    265 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    266 {
    267 	KASSERT(cred != NULL);
    268 	KASSERT(uid >= 0 && uid <= UID_MAX);
    269 
    270 	cred->cr_uid = uid;
    271 }
    272 
    273 void
    274 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    275 {
    276 	KASSERT(cred != NULL);
    277 	KASSERT(uid >= 0 && uid <= UID_MAX);
    278 
    279 	cred->cr_euid = uid;
    280 }
    281 
    282 void
    283 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    284 {
    285 	KASSERT(cred != NULL);
    286 	KASSERT(uid >= 0 && uid <= UID_MAX);
    287 
    288 	cred->cr_svuid = uid;
    289 }
    290 
    291 void
    292 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    293 {
    294 	KASSERT(cred != NULL);
    295 	KASSERT(gid >= 0 && gid <= GID_MAX);
    296 
    297 	cred->cr_gid = gid;
    298 }
    299 
    300 void
    301 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    302 {
    303 	KASSERT(cred != NULL);
    304 	KASSERT(gid >= 0 && gid <= GID_MAX);
    305 
    306 	cred->cr_egid = gid;
    307 }
    308 
    309 void
    310 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    311 {
    312 	KASSERT(cred != NULL);
    313 	KASSERT(gid >= 0 && gid <= GID_MAX);
    314 
    315 	cred->cr_svgid = gid;
    316 }
    317 
    318 /* Checks if gid is a member of the groups in cred. */
    319 int
    320 kauth_cred_groupmember(kauth_cred_t cred, gid_t gid)
    321 {
    322 	int i;
    323 
    324 	KASSERT(cred != NULL);
    325 	KASSERT(gid >= 0 && gid <= GID_MAX);
    326 
    327 	for (i = 0; i < cred->cr_ngroups; i++) {
    328 		if (cred->cr_groups[i] == gid)
    329 			return (1);
    330 	}
    331 
    332 	return (0);
    333 }
    334 
    335 uint16_t
    336 kauth_cred_ngroups(kauth_cred_t cred)
    337 {
    338 	KASSERT(cred != NULL);
    339 
    340 	return (cred->cr_ngroups);
    341 }
    342 
    343 /*
    344  * Return the group at index idx from the groups in cred.
    345  */
    346 gid_t
    347 kauth_cred_group(kauth_cred_t cred, uint16_t idx)
    348 {
    349 	KASSERT(cred != NULL);
    350 	KASSERT(idx < cred->cr_ngroups);
    351 
    352 	return (cred->cr_groups[idx]);
    353 }
    354 
    355 /* XXX: Is this needed? [setngroups] */
    356 void
    357 kauth_cred_setngroups(kauth_cred_t cred, uint16_t ngroups)
    358 {
    359 	KASSERT(cred != NULL);
    360 	KASSERT(ngroups < NGROUPS);
    361 
    362 	cred->cr_ngroups = ngroups;
    363 }
    364 
    365 /* Sort len groups in grbuf. */
    366 static void
    367 kauth_cred_sortgroups(gid_t *grbuf, size_t len)
    368 {
    369 	int i, j;
    370 	gid_t v;
    371 
    372 	KASSERT(grbuf != NULL);
    373 
    374 	/* Insertion sort. */
    375 	for (i = 1; i < len; i++) {
    376 		v = grbuf[i];
    377 		/* find correct slot for value v, moving others up */
    378 		for (j = i; --j >= 0 && v < grbuf[j];)
    379 			grbuf[j + 1] = grbuf[j];
    380 		grbuf[j + 1] = v;
    381 	}
    382 }
    383 
    384 /* Add group gid to groups in cred. Maintain order. */
    385 int
    386 kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
    387 {
    388 	KASSERT(cred != NULL);
    389 	KASSERT(gid >= 0 && gid <= GID_MAX);
    390 
    391 	simple_lock(&cred->cr_lock);
    392 
    393 	if (cred->cr_ngroups >=
    394 	    (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
    395 		simple_unlock(&cred->cr_lock);
    396 		return (E2BIG);
    397 	}
    398 
    399 	if (kauth_cred_groupmember(cred, gid)) {
    400 		simple_unlock(&cred->cr_lock);
    401 		return (0);
    402 	}
    403 
    404 	cred->cr_groups[cred->cr_ngroups++] = gid;
    405 
    406 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    407 
    408 	simple_unlock(&cred->cr_lock);
    409 
    410 	return (0);
    411 }
    412 
    413 /* Delete group gid from groups in cred. Maintain order. */
    414 int
    415 kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
    416 {
    417 	KASSERT(cred != NULL);
    418 	KASSERT(gid >= 0 && gid <= GID_MAX);
    419 
    420 	simple_lock(&cred->cr_lock);
    421 
    422 	if (!kauth_cred_groupmember(cred, gid)) {
    423 		simple_unlock(&cred->cr_lock);
    424 		return (0);
    425 	}
    426 
    427 	if (cred->cr_ngroups == 1)
    428 		cred->cr_groups[0] = (gid_t)-1; /* XXX */
    429 	else {
    430 		int i;
    431 
    432 		for (i = 0; i < cred->cr_ngroups; i++)
    433 			if (cred->cr_groups[i] == gid) {
    434 				cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
    435 				cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
    436 			}
    437 	}
    438 
    439 	cred->cr_ngroups--;
    440 
    441 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    442 
    443 	simple_unlock(&cred->cr_lock);
    444 
    445 	return (0);
    446 }
    447 
    448 /*
    449  * Match uids in two credentials. Checks if cred1 can access stuff owned by
    450  * cred2.
    451  * XXX: root bypasses this!
    452  */
    453 int
    454 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    455 {
    456 	KASSERT(cred1 != NULL);
    457 	KASSERT(cred2 != NULL);
    458 
    459 	/* Are we root? */
    460 	if (cred1->cr_euid == 0)
    461 		return (1);
    462 
    463 	if (cred1->cr_uid == cred2->cr_uid ||
    464 	    cred1->cr_euid == cred2->cr_uid ||
    465 	    cred1->cr_uid == cred2->cr_euid ||
    466 	    cred1->cr_euid == cred2->cr_euid)
    467 		return (1);
    468 
    469 	return (0);
    470 }
    471 
    472 uint32_t
    473 kauth_cred_getrefcnt(kauth_cred_t cred)
    474 {
    475 	KASSERT(cred != NULL);
    476 
    477 	return (cred->cr_refcnt);
    478 }
    479 
    480 void
    481 kauth_cred_setrefcnt(kauth_cred_t cred, uint32_t refcnt)
    482 {
    483 	KASSERT(cred != NULL);
    484 
    485 	cred->cr_refcnt = refcnt;
    486 }
    487 
    488 /* XXX: memcmp() wrapper needed for NFSW_SAMECRED() in nfs/nfs.h */
    489 int
    490 kauth_cred_memcmp(kauth_cred_t cred1, kauth_cred_t cred2)
    491 {
    492 	KASSERT(cred1 != NULL);
    493 	KASSERT(cred2 != NULL);
    494 
    495 	return (memcmp(cred1, cred2, sizeof(*cred1)));
    496 }
    497 
    498 /*
    499  * Convert userland credentials (struct uucred) to kauth_cred_t.
    500  * XXX: For NFS code.
    501  */
    502 void
    503 kauth_cred_convert(kauth_cred_t cred, const struct uucred *uuc)
    504 {
    505 	int i;
    506 
    507 	KASSERT(cred != NULL);
    508 	KASSERT(uuc != NULL);
    509 
    510 	cred->cr_refcnt = 0;
    511 	cred->cr_euid = uuc->cr_uid;
    512 	cred->cr_egid = uuc->cr_gid;
    513 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    514 	for (i = 0; i < cred->cr_ngroups; i++)
    515 		kauth_cred_addgroup(cred, uuc->cr_groups[i]);
    516 }
    517 
    518 /*
    519  * Compare kauth_cred_t and uucred credentials.
    520  * XXX: Modelled after crcmp() for NFS.
    521  */
    522 int
    523 kauth_cred_compare(kauth_cred_t cred, const struct uucred *uuc)
    524 {
    525 	KASSERT(cred != NULL);
    526 	KASSERT(uuc != NULL);
    527 
    528 	if (cred->cr_euid == uuc->cr_uid &&
    529 	    cred->cr_egid == uuc->cr_gid &&
    530 	    cred->cr_ngroups == uuc->cr_ngroups &&
    531 	    !memcmp(cred->cr_groups, uuc->cr_groups,
    532 		    cred->cr_ngroups * sizeof(cred->cr_groups[0])))
    533 		return (1);
    534 
    535 	return (0);
    536 }
    537 
    538 /*
    539  * Make a struct ucred out of a kauth_cred_t.
    540  * XXX: For sysctl.
    541  */
    542 void
    543 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    544 {
    545 	KASSERT(cred != NULL);
    546 	KASSERT(uc != NULL);
    547 
    548 	uc->cr_uid = cred->cr_euid;
    549 	uc->cr_gid = cred->cr_egid;
    550 	uc->cr_ngroups = min(cred->cr_ngroups,
    551 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    552 	memcpy(uc->cr_groups, cred->cr_groups,
    553 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    554 }
    555 
    556 /*
    557  * Make a struct pcred out of a kauth_cred_t.
    558  * XXX: For sysctl.
    559  */
    560 void
    561 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    562 {
    563 	KASSERT(cred != NULL);
    564 	KASSERT(pc != NULL);
    565 
    566 	pc->pc_ucred = (struct ucred *)cred; /* XXX this is just wrong */
    567 	pc->p_ruid = cred->cr_uid;
    568 	pc->p_svuid = cred->cr_svuid;
    569 	pc->p_rgid = cred->cr_gid;
    570 	pc->p_svgid = cred->cr_svgid;
    571 	pc->p_refcnt = cred->cr_refcnt;
    572 }
    573 
    574 /*
    575  * Return kauth_cred_t for the current process.
    576  */
    577 kauth_cred_t
    578 kauth_cred_get(void)
    579 {
    580 	return (curproc->p_cred);
    581 }
    582 
    583 /*
    584  * Returns a scope matching the provided id.
    585  * Requires the scope list lock to be held by the caller.
    586  */
    587 static kauth_scope_t
    588 kauth_ifindscope(const char *id) {
    589 	kauth_scope_t scope;
    590 
    591 	/* XXX: assert lock on scope list? */
    592 
    593 	scope = NULL;
    594 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    595 		if (strcmp(scope->id, id) == 0)
    596 			break;
    597 	}
    598 
    599 	return (scope);
    600 }
    601 
    602 /*
    603  * Register a new scope.
    604  *
    605  * id - identifier for the scope
    606  * callback - the scope's default listener
    607  * cookie - cookie to be passed to the listener(s)
    608  */
    609 kauth_scope_t
    610 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    611 		     void *cookie)
    612 {
    613 	kauth_scope_t scope;
    614 	kauth_listener_t listener;
    615 
    616 	/* Sanitize input */
    617 	if (id == NULL || callback == NULL)
    618 		return (NULL);
    619 
    620 	/* Allocate space for a new scope and listener. */
    621 	scope = pool_get(&kauth_scope_pool, PR_WAITOK);
    622 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    623 
    624 	/* Acquire scope list lock. */
    625 	simple_lock(&scopes_lock);
    626 
    627 	/* Check we don't already have a scope with the same id */
    628 	if (kauth_ifindscope(id) != NULL) {
    629 		simple_unlock(&scopes_lock);
    630 
    631 		pool_put(&kauth_scope_pool, scope);
    632 		pool_put(&kauth_listener_pool, listener);
    633 
    634 		return (NULL);
    635 	}
    636 
    637 	/* Initialize new scope with parameters */
    638 	scope->id = id;
    639 	scope->cookie = cookie;
    640 	scope->nlisteners = 1;
    641 
    642 	/* Add default listener */
    643 	listener->func = callback;
    644 	listener->scope = scope;
    645 	listener->refcnt = 0;
    646 	SIMPLEQ_INIT(&scope->listenq);
    647 	SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    648 
    649 	/* Insert scope to scopes list */
    650 	if (SIMPLEQ_EMPTY(&scope_list))
    651 		SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
    652 	else
    653 		SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    654 
    655 	simple_unlock(&scopes_lock);
    656 
    657 	return (scope);
    658 }
    659 
    660 /*
    661  * Initialize the kernel authorization subsystem.
    662  *
    663  * Initialize the scopes list lock.
    664  * Register built-in scopes: generic, process.
    665  */
    666 void
    667 kauth_init(void)
    668 {
    669 	simple_lock_init(&scopes_lock);
    670 
    671 	/* Register generic scope. */
    672 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    673 	    kauth_authorize_cb_generic, NULL);
    674 
    675 	/* Register process scope. */
    676 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    677 	    kauth_authorize_cb_process, NULL);
    678 }
    679 
    680 /*
    681  * Deregister a scope.
    682  * Requires scope list lock to be held by the caller.
    683  *
    684  * scope - the scope to deregister
    685  */
    686 void
    687 kauth_deregister_scope(kauth_scope_t scope)
    688 {
    689 	if (scope != NULL) {
    690 		/* Remove scope from list */
    691 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    692 	}
    693 }
    694 
    695 /*
    696  * Register a listener.
    697  *
    698  * id - scope identifier.
    699  * callback - the callback routine for the listener.
    700  * cookie - cookie to pass unmoidfied to the callback.
    701  */
    702 kauth_listener_t
    703 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    704 		   void *cookie)
    705 {
    706 	kauth_scope_t scope;
    707 	kauth_listener_t listener;
    708 
    709 	/* Find scope struct */
    710 	simple_lock(&scopes_lock);
    711 	scope = kauth_ifindscope(id);
    712 	simple_unlock(&scopes_lock);
    713 	if (scope == NULL)
    714 		return (NULL);
    715 
    716 	/* Allocate listener */
    717 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    718 
    719 	/* Initialize listener with parameters */
    720 	listener->func = callback;
    721 	listener->refcnt = 0;
    722 
    723 	/* Add listener to scope */
    724 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    725 
    726 	/* Raise number of listeners on scope. */
    727 	scope->nlisteners++;
    728 	listener->scope = scope;
    729 
    730 	return (listener);
    731 }
    732 
    733 /*
    734  * Deregister a listener.
    735  *
    736  * listener - listener reference as returned from kauth_listen_scope().
    737  */
    738 void
    739 kauth_unlisten_scope(kauth_listener_t listener)
    740 {
    741 	if (listener != NULL) {
    742 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
    743 			       listener_next);
    744 		listener->scope->nlisteners--;
    745 	}
    746 }
    747 
    748 /*
    749  * Authorize a request.
    750  *
    751  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    752  *	   returned from kauth_register_scope().
    753  * credential - credentials of the user ("actor") making the request.
    754  * action - request identifier.
    755  * arg[0-3] - passed unmodified to listener(s).
    756  */
    757 int
    758 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    759 		       kauth_action_t action, void *arg0, void *arg1,
    760 		       void *arg2, void *arg3)
    761 {
    762 	kauth_listener_t listener;
    763 	int error, allow, fail;
    764 
    765 	/* Sanitize input */
    766 	if (scope == NULL || cred == NULL)
    767 		return (EFAULT);
    768 	if (!action)
    769 		return (EINVAL);
    770 
    771 	/*
    772 	 * Each scope is associated with at least one listener. We need to
    773 	 * traverse that list of listeners, as long as they return either
    774 	 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
    775 	 */
    776 	fail = 0;
    777 	allow = 0;
    778 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    779 		error = listener->func(cred, action, scope->cookie, arg0,
    780 				       arg1, arg2, arg3);
    781 
    782 		if (error == KAUTH_RESULT_ALLOW)
    783 			allow = 1;
    784 		else if (error == KAUTH_RESULT_DENY)
    785 			fail = 1;
    786 	}
    787 
    788 	return ((allow && !fail) ? 0 : EPERM);
    789 };
    790 
    791 /*
    792  * Generic scope default callback.
    793  */
    794 int
    795 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
    796 			   void *cookie, void *arg0, void *arg1, void *arg2,
    797 			   void *arg3)
    798 {
    799 	int error;
    800 
    801 	error = KAUTH_RESULT_DEFER;
    802 
    803 	switch (action) {
    804 	case KAUTH_GENERIC_ISSUSER:
    805 		/* Check if credential belongs to superuser. */
    806 		if (cred->cr_euid == 0) {
    807 			u_short *acflag = (u_short *)arg0;
    808 
    809 			if (acflag != NULL)
    810 				*acflag |= ASU;
    811 
    812 			error = KAUTH_RESULT_ALLOW;
    813 		} else
    814 			error = KAUTH_RESULT_DENY;
    815 		break;
    816 	}
    817 
    818 	return (error);
    819 }
    820 
    821 /*
    822  * Generic scope authorization wrapper.
    823  */
    824 int
    825 generic_authorize(kauth_cred_t cred, kauth_action_t action, void *arg0)
    826 {
    827 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    828 	    action, arg0, NULL, NULL, NULL));
    829 }
    830 
    831 /*
    832  * Process scope default callback.
    833  */
    834 int
    835 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
    836 			   void *cookie, void *arg0, void *arg1, void *arg2,
    837 			   void *arg3)
    838 {
    839 	struct proc *p;
    840 	kauth_cred_t cred2;
    841 	int error;
    842 
    843 	error = KAUTH_RESULT_DEFER;
    844 
    845 	p = arg0;
    846 	cred2 = arg1;
    847 
    848 	switch (action) {
    849 	case KAUTH_PROCESS_CANSIGNAL: {
    850 		struct proc *to;
    851 		int signum;
    852 
    853 		to = arg2;
    854 		signum = (int)(unsigned long)arg3;
    855 
    856 		if (kauth_cred_uidmatch(cred, cred2)  ||
    857 		    (signum == SIGCONT && (p->p_session == to->p_session)))
    858 			error = KAUTH_RESULT_ALLOW;
    859 		else
    860 			error = KAUTH_RESULT_DEFER;
    861 
    862 		break;
    863 		}
    864 
    865 	case KAUTH_PROCESS_CANPTRACE:
    866 		if (kauth_cred_uidmatch(cred, cred2))
    867 			error = KAUTH_RESULT_ALLOW;
    868 		else
    869 			error = KAUTH_RESULT_DENY;
    870 		break;
    871 
    872 	case KAUTH_PROCESS_CANSEE:
    873 		if (kauth_cred_uidmatch(cred, cred2))
    874 			error = KAUTH_RESULT_ALLOW;
    875 		else
    876 			error = KAUTH_RESULT_DENY;
    877 			/* arg2 - type of information [XXX NOTIMPL] */
    878 		break;
    879 	}
    880 
    881 	return (error);
    882 }
    883 
    884 /*
    885  * Process scope authorization wrapper.
    886  */
    887 int
    888 process_authorize(kauth_cred_t cred, kauth_action_t action, struct proc *p,
    889 	       void *arg1, void *arg2, void *arg3)
    890 {
    891 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    892 	    action, p, arg1, arg2, arg3));
    893 }
    894