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