Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.1.2.18
      1 /* $NetBSD: kern_auth.c,v 1.1.2.18 2006/03/11 05:17:51 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 uid_t
    204 kauth_cred_getuid(kauth_cred_t cred)
    205 {
    206 	KASSERT(cred != NULL);
    207 
    208 	return (cred->cr_uid);
    209 }
    210 
    211 uid_t
    212 kauth_cred_geteuid(kauth_cred_t cred)
    213 {
    214 	KASSERT(cred != NULL);
    215 
    216 	return (cred->cr_euid);
    217 }
    218 
    219 uid_t
    220 kauth_cred_getsvuid(kauth_cred_t cred)
    221 {
    222 	KASSERT(cred != NULL);
    223 
    224 	return (cred->cr_svuid);
    225 }
    226 
    227 gid_t
    228 kauth_cred_getgid(kauth_cred_t cred)
    229 {
    230 	KASSERT(cred != NULL);
    231 
    232 	return (cred->cr_gid);
    233 }
    234 
    235 gid_t
    236 kauth_cred_getegid(kauth_cred_t cred)
    237 {
    238 	KASSERT(cred != NULL);
    239 
    240 	return (cred->cr_egid);
    241 }
    242 
    243 gid_t
    244 kauth_cred_getsvgid(kauth_cred_t cred)
    245 {
    246 	KASSERT(cred != NULL);
    247 
    248 	return (cred->cr_svgid);
    249 }
    250 
    251 void
    252 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    253 {
    254 	KASSERT(cred != NULL);
    255 	KASSERT(uid >= 0 && uid <= UID_MAX);
    256 
    257 	cred->cr_uid = uid;
    258 }
    259 
    260 void
    261 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    262 {
    263 	KASSERT(cred != NULL);
    264 	KASSERT(uid >= 0 && uid <= UID_MAX);
    265 
    266 	cred->cr_euid = uid;
    267 }
    268 
    269 void
    270 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    271 {
    272 	KASSERT(cred != NULL);
    273 	KASSERT(uid >= 0 && uid <= UID_MAX);
    274 
    275 	cred->cr_svuid = uid;
    276 }
    277 
    278 void
    279 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    280 {
    281 	KASSERT(cred != NULL);
    282 	KASSERT(gid >= 0 && gid <= GID_MAX);
    283 
    284 	cred->cr_gid = gid;
    285 }
    286 
    287 void
    288 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    289 {
    290 	KASSERT(cred != NULL);
    291 	KASSERT(gid >= 0 && gid <= GID_MAX);
    292 
    293 	cred->cr_egid = gid;
    294 }
    295 
    296 void
    297 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    298 {
    299 	KASSERT(cred != NULL);
    300 	KASSERT(gid >= 0 && gid <= GID_MAX);
    301 
    302 	cred->cr_svgid = gid;
    303 }
    304 
    305 /* Checks if gid is a member of the groups in cred. */
    306 int
    307 kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    308 {
    309 	int i;
    310 
    311 	KASSERT(cred != NULL);
    312 	KASSERT(gid >= 0 && gid <= GID_MAX);
    313 	KASSERT(resultp != NULL);
    314 
    315 	*resultp = 0;
    316 
    317 	for (i = 0; i < cred->cr_ngroups; i++) {
    318 		if (cred->cr_groups[i] == gid)
    319 			*resultp = 1;
    320 	}
    321 
    322 	return (0);
    323 }
    324 
    325 uint16_t
    326 kauth_cred_ngroups(kauth_cred_t cred)
    327 {
    328 	KASSERT(cred != NULL);
    329 
    330 	return (cred->cr_ngroups);
    331 }
    332 
    333 /*
    334  * Return the group at index idx from the groups in cred.
    335  */
    336 gid_t
    337 kauth_cred_group(kauth_cred_t cred, uint16_t idx)
    338 {
    339 	KASSERT(cred != NULL);
    340 	KASSERT(idx < cred->cr_ngroups);
    341 
    342 	return (cred->cr_groups[idx]);
    343 }
    344 
    345 /* Sort len groups in grbuf. */
    346 static void
    347 kauth_cred_sortgroups(gid_t *grbuf, size_t len)
    348 {
    349 	int i, j;
    350 	gid_t v;
    351 
    352 	KASSERT(grbuf != NULL);
    353 
    354 	/* Insertion sort. */
    355 	for (i = 1; i < len; i++) {
    356 		v = grbuf[i];
    357 		/* find correct slot for value v, moving others up */
    358 		for (j = i; --j >= 0 && v < grbuf[j];)
    359 			grbuf[j + 1] = grbuf[j];
    360 		grbuf[j + 1] = v;
    361 	}
    362 }
    363 
    364 /* Add group gid to groups in cred. Maintain order. */
    365 int
    366 kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
    367 {
    368 	int error, ismember;
    369 
    370 	KASSERT(cred != NULL);
    371 	KASSERT(gid >= 0 && gid <= GID_MAX);
    372 
    373 	simple_lock(&cred->cr_lock);
    374 
    375 	if (cred->cr_ngroups >=
    376 	    (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
    377 		simple_unlock(&cred->cr_lock);
    378 		return (E2BIG);
    379 	}
    380 
    381 	error = kauth_cred_ismember_gid(cred, gid, &ismember);
    382 	if (error) {
    383 		simple_unlock(&cred->cr_lock);
    384 		return (error);
    385 	}
    386 
    387 	if (ismember) {
    388 		simple_unlock(&cred->cr_lock);
    389 		return (0);
    390 	}
    391 
    392 	cred->cr_groups[cred->cr_ngroups++] = gid;
    393 
    394 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    395 
    396 	simple_unlock(&cred->cr_lock);
    397 
    398 	return (0);
    399 }
    400 
    401 /* Delete group gid from groups in cred. Maintain order. */
    402 int
    403 kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
    404 {
    405 	int error, ismember;
    406 
    407 	KASSERT(cred != NULL);
    408 	KASSERT(gid >= 0 && gid <= GID_MAX);
    409 
    410 	simple_lock(&cred->cr_lock);
    411 
    412 	error = kauth_cred_ismember_gid(cred, gid, &ismember);
    413 	if (error) {
    414 		simple_unlock(&cred->cr_lock);
    415 		return (error);
    416 	}
    417 
    418 	if (!ismember) {
    419 		simple_unlock(&cred->cr_lock);
    420 		return (0);
    421 	}
    422 
    423 	if (cred->cr_ngroups == 1)
    424 		cred->cr_groups[0] = (gid_t)-1; /* XXX */
    425 	else {
    426 		int i;
    427 
    428 		for (i = 0; i < cred->cr_ngroups; i++)
    429 			if (cred->cr_groups[i] == gid) {
    430 				cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
    431 				cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
    432 			}
    433 	}
    434 
    435 	cred->cr_ngroups--;
    436 
    437 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    438 
    439 	simple_unlock(&cred->cr_lock);
    440 
    441 	return (0);
    442 }
    443 
    444 /*
    445  * Match uids in two credentials. Checks if cred1 can access stuff owned by
    446  * cred2.
    447  * XXX: root bypasses this!
    448  */
    449 int
    450 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    451 {
    452 	KASSERT(cred1 != NULL);
    453 	KASSERT(cred2 != NULL);
    454 
    455 	/* Are we root? */
    456 	if (cred1->cr_euid == 0)
    457 		return (1);
    458 
    459 	if (cred1->cr_uid == cred2->cr_uid ||
    460 	    cred1->cr_euid == cred2->cr_uid ||
    461 	    cred1->cr_uid == cred2->cr_euid ||
    462 	    cred1->cr_euid == cred2->cr_euid)
    463 		return (1);
    464 
    465 	return (0);
    466 }
    467 
    468 uint32_t
    469 kauth_cred_getrefcnt(kauth_cred_t cred)
    470 {
    471 	KASSERT(cred != NULL);
    472 
    473 	return (cred->cr_refcnt);
    474 }
    475 
    476 /* XXX: memcmp() wrapper needed for NFSW_SAMECRED() in nfs/nfs.h */
    477 int
    478 kauth_cred_memcmp(kauth_cred_t cred1, kauth_cred_t cred2)
    479 {
    480 	KASSERT(cred1 != NULL);
    481 	KASSERT(cred2 != NULL);
    482 
    483 	return (memcmp(cred1, cred2, sizeof(*cred1)));
    484 }
    485 
    486 /*
    487  * Convert userland credentials (struct uucred) to kauth_cred_t.
    488  * XXX: For NFS code.
    489  */
    490 void
    491 kauth_cred_convert(kauth_cred_t cred, const struct uucred *uuc)
    492 {
    493 	int i;
    494 
    495 	KASSERT(cred != NULL);
    496 	KASSERT(uuc != NULL);
    497 
    498 	cred->cr_refcnt = 1;
    499 	cred->cr_euid = uuc->cr_uid;
    500 	cred->cr_egid = uuc->cr_gid;
    501 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    502 	for (i = 0; i < cred->cr_ngroups; i++)
    503 		kauth_cred_addgroup(cred, uuc->cr_groups[i]);
    504 }
    505 
    506 /*
    507  * Compare kauth_cred_t and uucred credentials.
    508  * XXX: Modelled after crcmp() for NFS.
    509  */
    510 int
    511 kauth_cred_compare(kauth_cred_t cred, const struct uucred *uuc)
    512 {
    513 	KASSERT(cred != NULL);
    514 	KASSERT(uuc != NULL);
    515 
    516 	if (cred->cr_euid == uuc->cr_uid &&
    517 	    cred->cr_egid == uuc->cr_gid &&
    518 	    cred->cr_ngroups == uuc->cr_ngroups) {
    519 		int i;
    520 
    521 		/* Check if all groups from uuc appear in cred. */
    522 		for (i = 0; i < uuc->cr_ngroups; i++) {
    523 			int error, ismember;
    524 
    525 			error = kauth_cred_ismember_gid(cred,
    526 			    uuc->cr_groups[i], &ismember);
    527 			if (error || !ismember)
    528 				return (0);
    529 		}
    530 
    531 		return (1);
    532 	}
    533 
    534 	return (0);
    535 }
    536 
    537 /*
    538  * Make a struct ucred out of a kauth_cred_t.
    539  * XXX: For sysctl.
    540  */
    541 void
    542 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    543 {
    544 	KASSERT(cred != NULL);
    545 	KASSERT(uc != NULL);
    546 
    547 	uc->cr_uid = cred->cr_euid;
    548 	uc->cr_gid = cred->cr_egid;
    549 	uc->cr_ngroups = min(cred->cr_ngroups,
    550 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    551 	memcpy(uc->cr_groups, cred->cr_groups,
    552 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    553 }
    554 
    555 /*
    556  * Make a struct pcred out of a kauth_cred_t.
    557  * XXX: For sysctl.
    558  */
    559 void
    560 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    561 {
    562 	KASSERT(cred != NULL);
    563 	KASSERT(pc != NULL);
    564 
    565 	pc->pc_ucred = (struct ucred *)cred; /* XXX this is just wrong */
    566 	pc->p_ruid = cred->cr_uid;
    567 	pc->p_svuid = cred->cr_svuid;
    568 	pc->p_rgid = cred->cr_gid;
    569 	pc->p_svgid = cred->cr_svgid;
    570 	pc->p_refcnt = cred->cr_refcnt;
    571 }
    572 
    573 /*
    574  * Return kauth_cred_t for the current process.
    575  */
    576 kauth_cred_t
    577 kauth_cred_get(void)
    578 {
    579 	return (curproc->p_cred);
    580 }
    581 
    582 /*
    583  * Returns a scope matching the provided id.
    584  * Requires the scope list lock to be held by the caller.
    585  */
    586 static kauth_scope_t
    587 kauth_ifindscope(const char *id) {
    588 	kauth_scope_t scope;
    589 
    590 	/* XXX: assert lock on scope list? */
    591 
    592 	scope = NULL;
    593 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    594 		if (strcmp(scope->id, id) == 0)
    595 			break;
    596 	}
    597 
    598 	return (scope);
    599 }
    600 
    601 /*
    602  * Register a new scope.
    603  *
    604  * id - identifier for the scope
    605  * callback - the scope's default listener
    606  * cookie - cookie to be passed to the listener(s)
    607  */
    608 kauth_scope_t
    609 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    610 		     void *cookie)
    611 {
    612 	kauth_scope_t scope;
    613 	kauth_listener_t listener;
    614 
    615 	/* Sanitize input */
    616 	if (id == NULL || callback == NULL)
    617 		return (NULL);
    618 
    619 	/* Allocate space for a new scope and listener. */
    620 	scope = pool_get(&kauth_scope_pool, PR_WAITOK);
    621 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    622 
    623 	/* Acquire scope list lock. */
    624 	simple_lock(&scopes_lock);
    625 
    626 	/* Check we don't already have a scope with the same id */
    627 	if (kauth_ifindscope(id) != NULL) {
    628 		simple_unlock(&scopes_lock);
    629 
    630 		pool_put(&kauth_scope_pool, scope);
    631 		pool_put(&kauth_listener_pool, listener);
    632 
    633 		return (NULL);
    634 	}
    635 
    636 	/* Initialize new scope with parameters */
    637 	scope->id = id;
    638 	scope->cookie = cookie;
    639 	scope->nlisteners = 1;
    640 
    641 	/* Add default listener */
    642 	listener->func = callback;
    643 	listener->scope = scope;
    644 	listener->refcnt = 0;
    645 	SIMPLEQ_INIT(&scope->listenq);
    646 	SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    647 
    648 	/* Insert scope to scopes list */
    649 	if (SIMPLEQ_EMPTY(&scope_list))
    650 		SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
    651 	else
    652 		SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    653 
    654 	simple_unlock(&scopes_lock);
    655 
    656 	return (scope);
    657 }
    658 
    659 /*
    660  * Initialize the kernel authorization subsystem.
    661  *
    662  * Initialize the scopes list lock.
    663  * Register built-in scopes: generic, process.
    664  */
    665 void
    666 kauth_init(void)
    667 {
    668 	simple_lock_init(&scopes_lock);
    669 
    670 	/* Register generic scope. */
    671 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    672 	    kauth_authorize_cb_generic, NULL);
    673 
    674 	/* Register process scope. */
    675 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    676 	    kauth_authorize_cb_process, NULL);
    677 }
    678 
    679 /*
    680  * Deregister a scope.
    681  * Requires scope list lock to be held by the caller.
    682  *
    683  * scope - the scope to deregister
    684  */
    685 void
    686 kauth_deregister_scope(kauth_scope_t scope)
    687 {
    688 	if (scope != NULL) {
    689 		/* Remove scope from list */
    690 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    691 	}
    692 }
    693 
    694 /*
    695  * Register a listener.
    696  *
    697  * id - scope identifier.
    698  * callback - the callback routine for the listener.
    699  * cookie - cookie to pass unmoidfied to the callback.
    700  */
    701 kauth_listener_t
    702 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    703 		   void *cookie)
    704 {
    705 	kauth_scope_t scope;
    706 	kauth_listener_t listener;
    707 
    708 	/* Find scope struct */
    709 	simple_lock(&scopes_lock);
    710 	scope = kauth_ifindscope(id);
    711 	simple_unlock(&scopes_lock);
    712 	if (scope == NULL)
    713 		return (NULL);
    714 
    715 	/* Allocate listener */
    716 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    717 
    718 	/* Initialize listener with parameters */
    719 	listener->func = callback;
    720 	listener->refcnt = 0;
    721 
    722 	/* Add listener to scope */
    723 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    724 
    725 	/* Raise number of listeners on scope. */
    726 	scope->nlisteners++;
    727 	listener->scope = scope;
    728 
    729 	return (listener);
    730 }
    731 
    732 /*
    733  * Deregister a listener.
    734  *
    735  * listener - listener reference as returned from kauth_listen_scope().
    736  */
    737 void
    738 kauth_unlisten_scope(kauth_listener_t listener)
    739 {
    740 	if (listener != NULL) {
    741 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
    742 			       listener_next);
    743 		listener->scope->nlisteners--;
    744 	}
    745 }
    746 
    747 /*
    748  * Authorize a request.
    749  *
    750  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    751  *	   returned from kauth_register_scope().
    752  * credential - credentials of the user ("actor") making the request.
    753  * action - request identifier.
    754  * arg[0-3] - passed unmodified to listener(s).
    755  */
    756 int
    757 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    758 		       kauth_action_t action, void *arg0, void *arg1,
    759 		       void *arg2, void *arg3)
    760 {
    761 	kauth_listener_t listener;
    762 	int error, allow, fail;
    763 
    764 	/* Sanitize input */
    765 	if (scope == NULL || cred == NULL)
    766 		return (EFAULT);
    767 	if (!action)
    768 		return (EINVAL);
    769 
    770 	/*
    771 	 * Each scope is associated with at least one listener. We need to
    772 	 * traverse that list of listeners, as long as they return either
    773 	 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
    774 	 */
    775 	fail = 0;
    776 	allow = 0;
    777 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    778 		error = listener->func(cred, action, scope->cookie, arg0,
    779 				       arg1, arg2, arg3);
    780 
    781 		if (error == KAUTH_RESULT_ALLOW)
    782 			allow = 1;
    783 		else if (error == KAUTH_RESULT_DENY)
    784 			fail = 1;
    785 	}
    786 
    787 	return ((allow && !fail) ? 0 : EPERM);
    788 };
    789 
    790 /*
    791  * Generic scope default callback.
    792  */
    793 int
    794 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
    795 			   void *cookie, void *arg0, void *arg1, void *arg2,
    796 			   void *arg3)
    797 {
    798 	int error;
    799 
    800 	error = KAUTH_RESULT_DEFER;
    801 
    802 	switch (action) {
    803 	case KAUTH_GENERIC_ISSUSER:
    804 		/* Check if credential belongs to superuser. */
    805 		if (cred->cr_euid == 0) {
    806 			u_short *acflag = (u_short *)arg0;
    807 
    808 			if (acflag != NULL)
    809 				*acflag |= ASU;
    810 
    811 			error = KAUTH_RESULT_ALLOW;
    812 		} else
    813 			error = KAUTH_RESULT_DENY;
    814 		break;
    815 	}
    816 
    817 	return (error);
    818 }
    819 
    820 /*
    821  * Generic scope authorization wrapper.
    822  */
    823 int
    824 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    825 {
    826 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    827 	    action, arg0, NULL, NULL, NULL));
    828 }
    829 
    830 /*
    831  * Process scope default callback.
    832  */
    833 int
    834 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
    835 			   void *cookie, void *arg0, void *arg1, void *arg2,
    836 			   void *arg3)
    837 {
    838 	struct proc *p;
    839 	kauth_cred_t cred2;
    840 	int error;
    841 
    842 	error = KAUTH_RESULT_DEFER;
    843 
    844 	p = arg0;
    845 	cred2 = arg1;
    846 
    847 	switch (action) {
    848 	case KAUTH_PROCESS_CANSIGNAL: {
    849 		struct proc *to;
    850 		int signum;
    851 
    852 		to = arg2;
    853 		signum = (int)(unsigned long)arg3;
    854 
    855 		if (kauth_cred_uidmatch(cred, cred2)  ||
    856 		    (signum == SIGCONT && (p->p_session == to->p_session)))
    857 			error = KAUTH_RESULT_ALLOW;
    858 		else
    859 			error = KAUTH_RESULT_DEFER;
    860 
    861 		break;
    862 		}
    863 
    864 	case KAUTH_PROCESS_CANPTRACE:
    865 		if (kauth_cred_uidmatch(cred, cred2))
    866 			error = KAUTH_RESULT_ALLOW;
    867 		else
    868 			error = KAUTH_RESULT_DENY;
    869 		break;
    870 
    871 	case KAUTH_PROCESS_CANSEE:
    872 		if (kauth_cred_uidmatch(cred, cred2))
    873 			error = KAUTH_RESULT_ALLOW;
    874 		else
    875 			error = KAUTH_RESULT_DENY;
    876 			/* arg2 - type of information [XXX NOTIMPL] */
    877 		break;
    878 	}
    879 
    880 	return (error);
    881 }
    882 
    883 /*
    884  * Process scope authorization wrapper.
    885  */
    886 int
    887 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    888     struct proc *p, void *arg1, void *arg2, void *arg3)
    889 {
    890 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    891 	    action, p, arg1, arg2, arg3));
    892 }
    893