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