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