Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.65.16.2
      1  1.65.16.2       mrg /* $NetBSD: kern_auth.c,v 1.65.16.2 2012/04/05 21:33:38 mrg Exp $ */
      2        1.2      elad 
      3        1.2      elad /*-
      4        1.2      elad  * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
      5        1.2      elad  * All rights reserved.
      6        1.2      elad  *
      7        1.2      elad  * Redistribution and use in source and binary forms, with or without
      8        1.2      elad  * modification, are permitted provided that the following conditions
      9        1.2      elad  * are met:
     10        1.2      elad  * 1. Redistributions of source code must retain the above copyright
     11        1.2      elad  *    notice, this list of conditions and the following disclaimer.
     12        1.2      elad  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.2      elad  *    notice, this list of conditions and the following disclaimer in the
     14        1.2      elad  *    documentation and/or other materials provided with the distribution.
     15       1.37      elad  * 3. The name of the author may not be used to endorse or promote products
     16        1.2      elad  *    derived from this software without specific prior written permission.
     17        1.2      elad  *
     18        1.2      elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19        1.2      elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20        1.2      elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21        1.2      elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22        1.2      elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23        1.2      elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24        1.2      elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25        1.2      elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26        1.2      elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27        1.2      elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28        1.2      elad  */
     29        1.2      elad 
     30       1.20      elad #include <sys/cdefs.h>
     31  1.65.16.2       mrg __KERNEL_RCSID(0, "$NetBSD: kern_auth.c,v 1.65.16.2 2012/04/05 21:33:38 mrg Exp $");
     32       1.20      elad 
     33       1.46  christos #include <sys/types.h>
     34        1.2      elad #include <sys/param.h>
     35        1.2      elad #include <sys/queue.h>
     36        1.2      elad #include <sys/proc.h>
     37        1.2      elad #include <sys/ucred.h>
     38        1.2      elad #include <sys/pool.h>
     39        1.2      elad #include <sys/kauth.h>
     40       1.34        ad #include <sys/kmem.h>
     41       1.44        ad #include <sys/rwlock.h>
     42       1.65      elad #include <sys/sysctl.h>
     43       1.55        ad #include <sys/atomic.h>
     44       1.46  christos #include <sys/specificdata.h>
     45       1.64      elad #include <sys/vnode.h>
     46       1.41      elad 
     47  1.65.16.1       mrg #include <secmodel/secmodel.h>
     48  1.65.16.1       mrg 
     49       1.41      elad /*
     50       1.41      elad  * Secmodel-specific credentials.
     51       1.41      elad  */
     52       1.41      elad struct kauth_key {
     53  1.65.16.1       mrg 	secmodel_t ks_secmodel;		/* secmodel */
     54       1.41      elad 	specificdata_key_t ks_key;	/* key */
     55       1.41      elad };
     56        1.2      elad 
     57       1.46  christos /*
     58       1.46  christos  * Credentials.
     59       1.46  christos  *
     60       1.46  christos  * A subset of this structure is used in kvm(3) (src/lib/libkvm/kvm_proc.c)
     61       1.46  christos  * and should be synchronized with this structure when the update is
     62       1.46  christos  * relevant.
     63       1.46  christos  */
     64       1.46  christos struct kauth_cred {
     65       1.53        ad 	/*
     66       1.53        ad 	 * Ensure that the first part of the credential resides in its own
     67       1.53        ad 	 * cache line.  Due to sharing there aren't many kauth_creds in a
     68       1.53        ad 	 * typical system, but the reference counts change very often.
     69       1.53        ad 	 * Keeping it seperate from the rest of the data prevents false
     70       1.53        ad 	 * sharing between CPUs.
     71       1.53        ad 	 */
     72       1.46  christos 	u_int cr_refcnt;		/* reference count */
     73       1.58        ad #if COHERENCY_UNIT > 4
     74       1.58        ad 	uint8_t cr_pad[COHERENCY_UNIT - 4];
     75       1.58        ad #endif
     76       1.55        ad 	uid_t cr_uid;			/* user id */
     77       1.46  christos 	uid_t cr_euid;			/* effective user id */
     78       1.46  christos 	uid_t cr_svuid;			/* saved effective user id */
     79       1.46  christos 	gid_t cr_gid;			/* group id */
     80       1.46  christos 	gid_t cr_egid;			/* effective group id */
     81       1.46  christos 	gid_t cr_svgid;			/* saved effective group id */
     82       1.46  christos 	u_int cr_ngroups;		/* number of groups */
     83       1.46  christos 	gid_t cr_groups[NGROUPS];	/* group memberships */
     84       1.46  christos 	specificdata_reference cr_sd;	/* specific data */
     85       1.46  christos };
     86       1.46  christos 
     87        1.2      elad /*
     88        1.2      elad  * Listener.
     89        1.2      elad  */
     90        1.2      elad struct kauth_listener {
     91        1.2      elad 	kauth_scope_callback_t		func;		/* callback */
     92        1.2      elad 	kauth_scope_t			scope;		/* scope backpointer */
     93       1.11        ad 	u_int				refcnt;		/* reference count */
     94        1.2      elad 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     95        1.2      elad };
     96        1.2      elad 
     97        1.2      elad /*
     98        1.2      elad  * Scope.
     99        1.2      elad  */
    100        1.2      elad struct kauth_scope {
    101        1.2      elad 	const char		       *id;		/* scope name */
    102        1.2      elad 	void			       *cookie;		/* user cookie */
    103       1.11        ad 	u_int				nlisteners;	/* # of listeners */
    104        1.2      elad 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
    105        1.2      elad 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
    106        1.2      elad };
    107        1.2      elad 
    108       1.41      elad static int kauth_cred_hook(kauth_cred_t, kauth_action_t, void *, void *);
    109        1.2      elad 
    110        1.2      elad /* List of scopes and its lock. */
    111       1.54      matt static SIMPLEQ_HEAD(, kauth_scope) scope_list =
    112       1.54      matt     SIMPLEQ_HEAD_INITIALIZER(scope_list);
    113        1.2      elad 
    114        1.2      elad /* Built-in scopes: generic, process. */
    115        1.2      elad static kauth_scope_t kauth_builtin_scope_generic;
    116       1.19      elad static kauth_scope_t kauth_builtin_scope_system;
    117        1.2      elad static kauth_scope_t kauth_builtin_scope_process;
    118       1.19      elad static kauth_scope_t kauth_builtin_scope_network;
    119       1.19      elad static kauth_scope_t kauth_builtin_scope_machdep;
    120       1.25      elad static kauth_scope_t kauth_builtin_scope_device;
    121       1.41      elad static kauth_scope_t kauth_builtin_scope_cred;
    122       1.64      elad static kauth_scope_t kauth_builtin_scope_vnode;
    123        1.2      elad 
    124       1.41      elad static specificdata_domain_t kauth_domain;
    125       1.53        ad static pool_cache_t kauth_cred_cache;
    126       1.65      elad 
    127       1.44        ad krwlock_t	kauth_lock;
    128       1.41      elad 
    129        1.2      elad /* Allocate new, empty kauth credentials. */
    130        1.2      elad kauth_cred_t
    131        1.3      yamt kauth_cred_alloc(void)
    132        1.3      yamt {
    133        1.2      elad 	kauth_cred_t cred;
    134        1.2      elad 
    135       1.53        ad 	cred = pool_cache_get(kauth_cred_cache, PR_WAITOK);
    136       1.53        ad 
    137        1.2      elad 	cred->cr_refcnt = 1;
    138       1.53        ad 	cred->cr_uid = 0;
    139       1.53        ad 	cred->cr_euid = 0;
    140       1.53        ad 	cred->cr_svuid = 0;
    141       1.53        ad 	cred->cr_gid = 0;
    142       1.53        ad 	cred->cr_egid = 0;
    143       1.53        ad 	cred->cr_svgid = 0;
    144       1.53        ad 	cred->cr_ngroups = 0;
    145       1.53        ad 
    146       1.41      elad 	specificdata_init(kauth_domain, &cred->cr_sd);
    147       1.41      elad 	kauth_cred_hook(cred, KAUTH_CRED_INIT, NULL, NULL);
    148        1.2      elad 
    149        1.2      elad 	return (cred);
    150        1.2      elad }
    151        1.2      elad 
    152        1.2      elad /* Increment reference count to cred. */
    153        1.2      elad void
    154        1.2      elad kauth_cred_hold(kauth_cred_t cred)
    155        1.2      elad {
    156        1.2      elad 	KASSERT(cred != NULL);
    157       1.11        ad 	KASSERT(cred->cr_refcnt > 0);
    158        1.2      elad 
    159       1.55        ad         atomic_inc_uint(&cred->cr_refcnt);
    160        1.2      elad }
    161        1.2      elad 
    162        1.2      elad /* Decrease reference count to cred. If reached zero, free it. */
    163        1.2      elad void
    164        1.3      yamt kauth_cred_free(kauth_cred_t cred)
    165        1.3      yamt {
    166       1.11        ad 
    167        1.2      elad 	KASSERT(cred != NULL);
    168       1.11        ad 	KASSERT(cred->cr_refcnt > 0);
    169       1.63      yamt 	ASSERT_SLEEPABLE();
    170        1.2      elad 
    171       1.56        ad 	if (atomic_dec_uint_nv(&cred->cr_refcnt) > 0)
    172       1.55        ad 		return;
    173       1.55        ad 
    174       1.55        ad 	kauth_cred_hook(cred, KAUTH_CRED_FREE, NULL, NULL);
    175       1.55        ad 	specificdata_fini(kauth_domain, &cred->cr_sd);
    176       1.55        ad 	pool_cache_put(kauth_cred_cache, cred);
    177        1.2      elad }
    178        1.2      elad 
    179       1.49       dsl static void
    180       1.48       dsl kauth_cred_clone1(kauth_cred_t from, kauth_cred_t to, bool copy_groups)
    181       1.48       dsl {
    182        1.2      elad 	KASSERT(from != NULL);
    183        1.2      elad 	KASSERT(to != NULL);
    184       1.11        ad 	KASSERT(from->cr_refcnt > 0);
    185        1.2      elad 
    186        1.2      elad 	to->cr_uid = from->cr_uid;
    187        1.2      elad 	to->cr_euid = from->cr_euid;
    188        1.2      elad 	to->cr_svuid = from->cr_svuid;
    189        1.2      elad 	to->cr_gid = from->cr_gid;
    190        1.2      elad 	to->cr_egid = from->cr_egid;
    191        1.2      elad 	to->cr_svgid = from->cr_svgid;
    192       1.48       dsl 	if (copy_groups) {
    193       1.48       dsl 		to->cr_ngroups = from->cr_ngroups;
    194       1.48       dsl 		memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
    195       1.48       dsl 	}
    196       1.41      elad 
    197       1.41      elad 	kauth_cred_hook(from, KAUTH_CRED_COPY, to, NULL);
    198        1.2      elad }
    199        1.2      elad 
    200       1.49       dsl void
    201       1.49       dsl kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    202       1.49       dsl {
    203       1.49       dsl 	kauth_cred_clone1(from, to, true);
    204       1.49       dsl }
    205       1.49       dsl 
    206        1.2      elad /*
    207        1.2      elad  * Duplicate cred and return a new kauth_cred_t.
    208        1.2      elad  */
    209        1.2      elad kauth_cred_t
    210        1.2      elad kauth_cred_dup(kauth_cred_t cred)
    211        1.2      elad {
    212        1.2      elad 	kauth_cred_t new_cred;
    213        1.2      elad 
    214        1.2      elad 	KASSERT(cred != NULL);
    215       1.11        ad 	KASSERT(cred->cr_refcnt > 0);
    216        1.2      elad 
    217        1.2      elad 	new_cred = kauth_cred_alloc();
    218        1.2      elad 
    219        1.2      elad 	kauth_cred_clone(cred, new_cred);
    220        1.2      elad 
    221        1.2      elad 	return (new_cred);
    222        1.2      elad }
    223        1.2      elad 
    224        1.2      elad /*
    225        1.2      elad  * Similar to crcopy(), only on a kauth_cred_t.
    226        1.2      elad  * XXX: Is this even needed? [kauth_cred_copy]
    227        1.2      elad  */
    228        1.2      elad kauth_cred_t
    229        1.2      elad kauth_cred_copy(kauth_cred_t cred)
    230        1.2      elad {
    231        1.2      elad 	kauth_cred_t new_cred;
    232        1.2      elad 
    233        1.2      elad 	KASSERT(cred != NULL);
    234       1.11        ad 	KASSERT(cred->cr_refcnt > 0);
    235        1.2      elad 
    236        1.2      elad 	/* If the provided credentials already have one reference, use them. */
    237        1.2      elad 	if (cred->cr_refcnt == 1)
    238        1.2      elad 		return (cred);
    239        1.2      elad 
    240        1.2      elad 	new_cred = kauth_cred_alloc();
    241        1.2      elad 
    242        1.2      elad 	kauth_cred_clone(cred, new_cred);
    243        1.2      elad 
    244        1.2      elad 	kauth_cred_free(cred);
    245        1.2      elad 
    246        1.2      elad 	return (new_cred);
    247        1.2      elad }
    248        1.2      elad 
    249       1.38      elad void
    250       1.38      elad kauth_proc_fork(struct proc *parent, struct proc *child)
    251       1.38      elad {
    252       1.44        ad 
    253       1.59        ad 	mutex_enter(parent->p_lock);
    254       1.38      elad 	kauth_cred_hold(parent->p_cred);
    255       1.38      elad 	child->p_cred = parent->p_cred;
    256       1.59        ad 	mutex_exit(parent->p_lock);
    257       1.41      elad 
    258       1.44        ad 	/* XXX: relies on parent process stalling during fork() */
    259       1.41      elad 	kauth_cred_hook(parent->p_cred, KAUTH_CRED_FORK, parent,
    260       1.41      elad 	    child);
    261       1.38      elad }
    262       1.38      elad 
    263        1.2      elad uid_t
    264        1.2      elad kauth_cred_getuid(kauth_cred_t cred)
    265        1.2      elad {
    266        1.2      elad 	KASSERT(cred != NULL);
    267        1.2      elad 
    268        1.2      elad 	return (cred->cr_uid);
    269        1.2      elad }
    270        1.2      elad 
    271        1.2      elad uid_t
    272        1.2      elad kauth_cred_geteuid(kauth_cred_t cred)
    273        1.2      elad {
    274        1.2      elad 	KASSERT(cred != NULL);
    275        1.2      elad 
    276        1.2      elad 	return (cred->cr_euid);
    277        1.2      elad }
    278        1.2      elad 
    279        1.2      elad uid_t
    280        1.2      elad kauth_cred_getsvuid(kauth_cred_t cred)
    281        1.2      elad {
    282        1.2      elad 	KASSERT(cred != NULL);
    283        1.2      elad 
    284        1.2      elad 	return (cred->cr_svuid);
    285        1.2      elad }
    286        1.2      elad 
    287        1.2      elad gid_t
    288        1.2      elad kauth_cred_getgid(kauth_cred_t cred)
    289        1.2      elad {
    290        1.2      elad 	KASSERT(cred != NULL);
    291        1.2      elad 
    292        1.2      elad 	return (cred->cr_gid);
    293        1.2      elad }
    294        1.2      elad 
    295        1.2      elad gid_t
    296        1.2      elad kauth_cred_getegid(kauth_cred_t cred)
    297        1.2      elad {
    298        1.2      elad 	KASSERT(cred != NULL);
    299        1.2      elad 
    300        1.2      elad 	return (cred->cr_egid);
    301        1.2      elad }
    302        1.2      elad 
    303        1.2      elad gid_t
    304        1.2      elad kauth_cred_getsvgid(kauth_cred_t cred)
    305        1.2      elad {
    306        1.2      elad 	KASSERT(cred != NULL);
    307        1.2      elad 
    308        1.2      elad 	return (cred->cr_svgid);
    309        1.2      elad }
    310        1.2      elad 
    311        1.2      elad void
    312        1.2      elad kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    313        1.2      elad {
    314        1.2      elad 	KASSERT(cred != NULL);
    315       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    316        1.2      elad 
    317        1.2      elad 	cred->cr_uid = uid;
    318        1.2      elad }
    319        1.2      elad 
    320        1.2      elad void
    321        1.2      elad kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    322        1.2      elad {
    323        1.2      elad 	KASSERT(cred != NULL);
    324       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    325        1.2      elad 
    326        1.2      elad 	cred->cr_euid = uid;
    327        1.2      elad }
    328        1.2      elad 
    329        1.2      elad void
    330        1.2      elad kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    331        1.2      elad {
    332        1.2      elad 	KASSERT(cred != NULL);
    333       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    334        1.2      elad 
    335        1.2      elad 	cred->cr_svuid = uid;
    336        1.2      elad }
    337        1.2      elad 
    338        1.2      elad void
    339        1.2      elad kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    340        1.2      elad {
    341        1.2      elad 	KASSERT(cred != NULL);
    342       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    343        1.2      elad 
    344        1.2      elad 	cred->cr_gid = gid;
    345        1.2      elad }
    346        1.2      elad 
    347        1.2      elad void
    348        1.2      elad kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    349        1.2      elad {
    350        1.2      elad 	KASSERT(cred != NULL);
    351       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    352        1.2      elad 
    353        1.2      elad 	cred->cr_egid = gid;
    354        1.2      elad }
    355        1.2      elad 
    356        1.2      elad void
    357        1.2      elad kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    358        1.2      elad {
    359        1.2      elad 	KASSERT(cred != NULL);
    360       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    361        1.2      elad 
    362        1.2      elad 	cred->cr_svgid = gid;
    363        1.2      elad }
    364        1.2      elad 
    365        1.2      elad /* Checks if gid is a member of the groups in cred. */
    366        1.2      elad int
    367        1.2      elad kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    368        1.2      elad {
    369       1.62     lukem 	uint32_t i;
    370        1.2      elad 
    371        1.2      elad 	KASSERT(cred != NULL);
    372        1.2      elad 	KASSERT(resultp != NULL);
    373        1.2      elad 
    374        1.2      elad 	*resultp = 0;
    375        1.2      elad 
    376        1.2      elad 	for (i = 0; i < cred->cr_ngroups; i++)
    377        1.2      elad 		if (cred->cr_groups[i] == gid) {
    378        1.2      elad 			*resultp = 1;
    379        1.2      elad 			break;
    380        1.2      elad 		}
    381        1.2      elad 
    382        1.2      elad 	return (0);
    383        1.2      elad }
    384        1.2      elad 
    385       1.11        ad u_int
    386        1.2      elad kauth_cred_ngroups(kauth_cred_t cred)
    387        1.2      elad {
    388        1.2      elad 	KASSERT(cred != NULL);
    389        1.2      elad 
    390        1.2      elad 	return (cred->cr_ngroups);
    391        1.2      elad }
    392        1.2      elad 
    393        1.2      elad /*
    394        1.2      elad  * Return the group at index idx from the groups in cred.
    395        1.2      elad  */
    396        1.2      elad gid_t
    397       1.11        ad kauth_cred_group(kauth_cred_t cred, u_int idx)
    398        1.2      elad {
    399        1.2      elad 	KASSERT(cred != NULL);
    400        1.2      elad 	KASSERT(idx < cred->cr_ngroups);
    401        1.2      elad 
    402        1.2      elad 	return (cred->cr_groups[idx]);
    403        1.2      elad }
    404        1.2      elad 
    405        1.2      elad /* XXX elad: gmuid is unused for now. */
    406        1.2      elad int
    407       1.49       dsl kauth_cred_setgroups(kauth_cred_t cred, const gid_t *grbuf, size_t len,
    408       1.52      yamt     uid_t gmuid, enum uio_seg seg)
    409        1.2      elad {
    410       1.49       dsl 	int error = 0;
    411       1.49       dsl 
    412        1.2      elad 	KASSERT(cred != NULL);
    413       1.11        ad 	KASSERT(cred->cr_refcnt == 1);
    414        1.2      elad 
    415       1.61      matt 	if (len > __arraycount(cred->cr_groups))
    416       1.49       dsl 		return EINVAL;
    417       1.49       dsl 
    418       1.49       dsl 	if (len) {
    419       1.52      yamt 		if (seg == UIO_SYSSPACE) {
    420       1.49       dsl 			memcpy(cred->cr_groups, grbuf,
    421       1.49       dsl 			    len * sizeof(cred->cr_groups[0]));
    422       1.52      yamt 		} else {
    423       1.49       dsl 			error = copyin(grbuf, cred->cr_groups,
    424       1.49       dsl 			    len * sizeof(cred->cr_groups[0]));
    425       1.49       dsl 			if (error != 0)
    426       1.49       dsl 				len = 0;
    427       1.49       dsl 		}
    428       1.49       dsl 	}
    429        1.2      elad 	memset(cred->cr_groups + len, 0xff,
    430        1.2      elad 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    431        1.2      elad 
    432        1.2      elad 	cred->cr_ngroups = len;
    433        1.2      elad 
    434       1.49       dsl 	return error;
    435       1.48       dsl }
    436       1.48       dsl 
    437       1.49       dsl /* This supports sys_setgroups() */
    438       1.48       dsl int
    439       1.48       dsl kauth_proc_setgroups(struct lwp *l, kauth_cred_t ncred)
    440       1.48       dsl {
    441       1.48       dsl 	kauth_cred_t cred;
    442       1.48       dsl 	int error;
    443       1.48       dsl 
    444       1.48       dsl 	/*
    445       1.48       dsl 	 * At this point we could delete duplicate groups from ncred,
    446       1.48       dsl 	 * and plausibly sort the list - but in general the later is
    447       1.48       dsl 	 * a bad idea.
    448       1.48       dsl 	 */
    449       1.48       dsl 	proc_crmod_enter();
    450       1.48       dsl 	/* Maybe we should use curproc here ? */
    451       1.48       dsl 	cred = l->l_proc->p_cred;
    452       1.48       dsl 
    453       1.48       dsl 	kauth_cred_clone1(cred, ncred, false);
    454       1.48       dsl 
    455       1.48       dsl 	error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
    456       1.48       dsl 	    l->l_proc, NULL, NULL, NULL);
    457       1.48       dsl 	if (error != 0) {
    458       1.48       dsl 		proc_crmod_leave(cred, ncred, false);
    459       1.48       dsl 			return error;
    460       1.48       dsl 	}
    461       1.48       dsl 
    462       1.48       dsl 	/* Broadcast our credentials to the process and other LWPs. */
    463       1.48       dsl  	proc_crmod_leave(ncred, cred, true);
    464       1.48       dsl 	return 0;
    465       1.48       dsl }
    466       1.48       dsl 
    467        1.2      elad int
    468       1.49       dsl kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len,
    469       1.52      yamt     enum uio_seg seg)
    470        1.2      elad {
    471        1.2      elad 	KASSERT(cred != NULL);
    472        1.2      elad 
    473       1.49       dsl 	if (len > cred->cr_ngroups)
    474       1.49       dsl 		return EINVAL;
    475       1.49       dsl 
    476       1.52      yamt 	if (seg == UIO_USERSPACE)
    477       1.49       dsl 		return copyout(cred->cr_groups, grbuf, sizeof(*grbuf) * len);
    478        1.2      elad 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    479        1.2      elad 
    480       1.49       dsl 	return 0;
    481       1.48       dsl }
    482       1.48       dsl 
    483       1.41      elad int
    484  1.65.16.1       mrg kauth_register_key(secmodel_t secmodel, kauth_key_t *result)
    485       1.41      elad {
    486       1.41      elad 	kauth_key_t k;
    487       1.41      elad 	specificdata_key_t key;
    488       1.41      elad 	int error;
    489       1.41      elad 
    490       1.41      elad 	KASSERT(result != NULL);
    491       1.41      elad 
    492       1.41      elad 	error = specificdata_key_create(kauth_domain, &key, NULL);
    493       1.41      elad 	if (error)
    494       1.41      elad 		return (error);
    495       1.41      elad 
    496       1.41      elad 	k = kmem_alloc(sizeof(*k), KM_SLEEP);
    497       1.41      elad 	k->ks_secmodel = secmodel;
    498       1.41      elad 	k->ks_key = key;
    499       1.41      elad 
    500       1.41      elad 	*result = k;
    501       1.41      elad 
    502       1.41      elad 	return (0);
    503       1.41      elad }
    504       1.41      elad 
    505       1.41      elad int
    506       1.41      elad kauth_deregister_key(kauth_key_t key)
    507       1.41      elad {
    508       1.41      elad 	KASSERT(key != NULL);
    509       1.41      elad 
    510       1.41      elad 	specificdata_key_delete(kauth_domain, key->ks_key);
    511       1.41      elad 	kmem_free(key, sizeof(*key));
    512       1.41      elad 
    513       1.41      elad 	return (0);
    514       1.41      elad }
    515       1.41      elad 
    516       1.41      elad void *
    517       1.41      elad kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)
    518       1.41      elad {
    519       1.41      elad 	KASSERT(cred != NULL);
    520       1.41      elad 	KASSERT(key != NULL);
    521       1.41      elad 
    522       1.41      elad 	return (specificdata_getspecific(kauth_domain, &cred->cr_sd,
    523       1.41      elad 	    key->ks_key));
    524       1.41      elad }
    525       1.41      elad 
    526       1.41      elad void
    527       1.41      elad kauth_cred_setdata(kauth_cred_t cred, kauth_key_t key, void *data)
    528       1.41      elad {
    529       1.41      elad 	KASSERT(cred != NULL);
    530       1.41      elad 	KASSERT(key != NULL);
    531       1.41      elad 
    532       1.41      elad 	specificdata_setspecific(kauth_domain, &cred->cr_sd, key->ks_key, data);
    533       1.41      elad }
    534       1.41      elad 
    535        1.2      elad /*
    536       1.19      elad  * Match uids in two credentials.
    537        1.2      elad  */
    538       1.19      elad int
    539        1.2      elad kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    540        1.2      elad {
    541        1.2      elad 	KASSERT(cred1 != NULL);
    542        1.2      elad 	KASSERT(cred2 != NULL);
    543        1.2      elad 
    544        1.2      elad 	if (cred1->cr_uid == cred2->cr_uid ||
    545        1.2      elad 	    cred1->cr_euid == cred2->cr_uid ||
    546        1.2      elad 	    cred1->cr_uid == cred2->cr_euid ||
    547        1.2      elad 	    cred1->cr_euid == cred2->cr_euid)
    548        1.2      elad 		return (1);
    549        1.2      elad 
    550        1.2      elad 	return (0);
    551        1.2      elad }
    552        1.2      elad 
    553       1.11        ad u_int
    554        1.2      elad kauth_cred_getrefcnt(kauth_cred_t cred)
    555        1.2      elad {
    556        1.2      elad 	KASSERT(cred != NULL);
    557        1.2      elad 
    558        1.2      elad 	return (cred->cr_refcnt);
    559        1.2      elad }
    560        1.2      elad 
    561        1.2      elad /*
    562        1.2      elad  * Convert userland credentials (struct uucred) to kauth_cred_t.
    563       1.29     pooka  * XXX: For NFS & puffs
    564        1.2      elad  */
    565       1.29     pooka void
    566       1.29     pooka kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    567       1.29     pooka {
    568        1.2      elad 	KASSERT(cred != NULL);
    569        1.2      elad 	KASSERT(uuc != NULL);
    570       1.29     pooka 
    571        1.2      elad 	cred->cr_refcnt = 1;
    572        1.2      elad 	cred->cr_uid = uuc->cr_uid;
    573        1.2      elad 	cred->cr_euid = uuc->cr_uid;
    574        1.2      elad 	cred->cr_svuid = uuc->cr_uid;
    575        1.2      elad 	cred->cr_gid = uuc->cr_gid;
    576        1.2      elad 	cred->cr_egid = uuc->cr_gid;
    577        1.2      elad 	cred->cr_svgid = uuc->cr_gid;
    578        1.2      elad 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    579        1.2      elad 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    580       1.49       dsl 	    cred->cr_ngroups, -1, UIO_SYSSPACE);
    581        1.2      elad }
    582        1.2      elad 
    583        1.2      elad /*
    584       1.29     pooka  * Convert kauth_cred_t to userland credentials (struct uucred).
    585       1.29     pooka  * XXX: For NFS & puffs
    586       1.29     pooka  */
    587       1.29     pooka void
    588       1.29     pooka kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    589       1.29     pooka {
    590       1.29     pooka 	KASSERT(cred != NULL);
    591       1.29     pooka 	KASSERT(uuc != NULL);
    592       1.29     pooka 	int ng;
    593       1.29     pooka 
    594       1.29     pooka 	ng = min(cred->cr_ngroups, NGROUPS);
    595       1.29     pooka 	uuc->cr_uid = cred->cr_euid;
    596       1.29     pooka 	uuc->cr_gid = cred->cr_egid;
    597       1.29     pooka 	uuc->cr_ngroups = ng;
    598       1.49       dsl 	kauth_cred_getgroups(cred, uuc->cr_groups, ng, UIO_SYSSPACE);
    599       1.29     pooka }
    600       1.29     pooka 
    601       1.29     pooka /*
    602        1.2      elad  * Compare kauth_cred_t and uucred credentials.
    603        1.2      elad  * XXX: Modelled after crcmp() for NFS.
    604        1.2      elad  */
    605        1.2      elad int
    606        1.2      elad kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    607        1.2      elad {
    608        1.2      elad 	KASSERT(cred != NULL);
    609        1.2      elad 	KASSERT(uuc != NULL);
    610        1.2      elad 
    611        1.2      elad 	if (cred->cr_euid == uuc->cr_uid &&
    612        1.2      elad 	    cred->cr_egid == uuc->cr_gid &&
    613       1.62     lukem 	    cred->cr_ngroups == (uint32_t)uuc->cr_ngroups) {
    614        1.2      elad 		int i;
    615        1.2      elad 
    616        1.2      elad 		/* Check if all groups from uuc appear in cred. */
    617        1.2      elad 		for (i = 0; i < uuc->cr_ngroups; i++) {
    618        1.2      elad 			int ismember;
    619        1.2      elad 
    620        1.2      elad 			ismember = 0;
    621        1.2      elad 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    622        1.2      elad 			    &ismember) != 0 || !ismember)
    623        1.4      yamt 				return (1);
    624        1.2      elad 		}
    625        1.2      elad 
    626        1.4      yamt 		return (0);
    627        1.2      elad 	}
    628        1.2      elad 
    629        1.4      yamt 	return (1);
    630        1.2      elad }
    631        1.2      elad 
    632        1.2      elad /*
    633       1.12        ad  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    634        1.2      elad  */
    635        1.2      elad void
    636       1.45       dsl kauth_cred_toucred(kauth_cred_t cred, struct ki_ucred *uc)
    637        1.2      elad {
    638        1.2      elad 	KASSERT(cred != NULL);
    639        1.2      elad 	KASSERT(uc != NULL);
    640        1.2      elad 
    641       1.12        ad 	uc->cr_ref = cred->cr_refcnt;
    642        1.2      elad 	uc->cr_uid = cred->cr_euid;
    643        1.2      elad 	uc->cr_gid = cred->cr_egid;
    644       1.61      matt 	uc->cr_ngroups = min(cred->cr_ngroups, __arraycount(uc->cr_groups));
    645        1.2      elad 	memcpy(uc->cr_groups, cred->cr_groups,
    646        1.2      elad 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    647        1.2      elad }
    648        1.2      elad 
    649        1.2      elad /*
    650       1.12        ad  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    651        1.2      elad  */
    652        1.2      elad void
    653       1.45       dsl kauth_cred_topcred(kauth_cred_t cred, struct ki_pcred *pc)
    654        1.2      elad {
    655        1.2      elad 	KASSERT(cred != NULL);
    656        1.2      elad 	KASSERT(pc != NULL);
    657        1.2      elad 
    658       1.45       dsl 	pc->p_pad = NULL;
    659        1.2      elad 	pc->p_ruid = cred->cr_uid;
    660        1.2      elad 	pc->p_svuid = cred->cr_svuid;
    661        1.2      elad 	pc->p_rgid = cred->cr_gid;
    662        1.2      elad 	pc->p_svgid = cred->cr_svgid;
    663        1.2      elad 	pc->p_refcnt = cred->cr_refcnt;
    664        1.2      elad }
    665        1.2      elad 
    666        1.2      elad /*
    667       1.14        ad  * Return kauth_cred_t for the current LWP.
    668        1.2      elad  */
    669        1.2      elad kauth_cred_t
    670        1.2      elad kauth_cred_get(void)
    671        1.2      elad {
    672       1.14        ad 	return (curlwp->l_cred);
    673        1.2      elad }
    674        1.2      elad 
    675        1.2      elad /*
    676        1.2      elad  * Returns a scope matching the provided id.
    677        1.2      elad  * Requires the scope list lock to be held by the caller.
    678        1.2      elad  */
    679        1.2      elad static kauth_scope_t
    680        1.3      yamt kauth_ifindscope(const char *id)
    681        1.3      yamt {
    682        1.2      elad 	kauth_scope_t scope;
    683        1.2      elad 
    684       1.44        ad 	KASSERT(rw_lock_held(&kauth_lock));
    685        1.2      elad 
    686        1.2      elad 	scope = NULL;
    687        1.2      elad 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    688        1.2      elad 		if (strcmp(scope->id, id) == 0)
    689        1.2      elad 			break;
    690        1.2      elad 	}
    691        1.2      elad 
    692        1.2      elad 	return (scope);
    693        1.2      elad }
    694        1.2      elad 
    695        1.2      elad /*
    696        1.2      elad  * Register a new scope.
    697        1.2      elad  *
    698        1.2      elad  * id - identifier for the scope
    699        1.2      elad  * callback - the scope's default listener
    700        1.2      elad  * cookie - cookie to be passed to the listener(s)
    701        1.2      elad  */
    702        1.2      elad kauth_scope_t
    703        1.2      elad kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    704       1.16  christos     void *cookie)
    705        1.2      elad {
    706        1.2      elad 	kauth_scope_t scope;
    707       1.21      yamt 	kauth_listener_t listener = NULL; /* XXX gcc */
    708        1.2      elad 
    709        1.2      elad 	/* Sanitize input */
    710       1.16  christos 	if (id == NULL)
    711        1.2      elad 		return (NULL);
    712        1.2      elad 
    713        1.2      elad 	/* Allocate space for a new scope and listener. */
    714       1.34        ad 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    715       1.34        ad 	if (scope == NULL)
    716       1.34        ad 		return NULL;
    717       1.21      yamt 	if (callback != NULL) {
    718       1.34        ad 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    719       1.34        ad 		if (listener == NULL) {
    720       1.34        ad 			kmem_free(scope, sizeof(*scope));
    721       1.34        ad 			return (NULL);
    722       1.34        ad 		}
    723       1.21      yamt 	}
    724        1.2      elad 
    725       1.34        ad 	/*
    726       1.34        ad 	 * Acquire scope list lock.
    727       1.34        ad 	 */
    728       1.44        ad 	rw_enter(&kauth_lock, RW_WRITER);
    729        1.2      elad 
    730        1.2      elad 	/* Check we don't already have a scope with the same id */
    731        1.2      elad 	if (kauth_ifindscope(id) != NULL) {
    732       1.44        ad 		rw_exit(&kauth_lock);
    733        1.2      elad 
    734       1.34        ad 		kmem_free(scope, sizeof(*scope));
    735       1.34        ad 		if (callback != NULL)
    736       1.34        ad 			kmem_free(listener, sizeof(*listener));
    737        1.2      elad 
    738        1.2      elad 		return (NULL);
    739        1.2      elad 	}
    740        1.2      elad 
    741        1.2      elad 	/* Initialize new scope with parameters */
    742        1.2      elad 	scope->id = id;
    743        1.2      elad 	scope->cookie = cookie;
    744        1.2      elad 	scope->nlisteners = 1;
    745        1.2      elad 
    746       1.16  christos 	SIMPLEQ_INIT(&scope->listenq);
    747       1.16  christos 
    748        1.2      elad 	/* Add default listener */
    749       1.16  christos 	if (callback != NULL) {
    750       1.16  christos 		listener->func = callback;
    751       1.16  christos 		listener->scope = scope;
    752       1.16  christos 		listener->refcnt = 0;
    753       1.16  christos 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    754       1.16  christos 	}
    755        1.2      elad 
    756        1.2      elad 	/* Insert scope to scopes list */
    757       1.16  christos 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    758        1.2      elad 
    759       1.44        ad 	rw_exit(&kauth_lock);
    760        1.2      elad 
    761        1.2      elad 	return (scope);
    762        1.2      elad }
    763        1.2      elad 
    764        1.2      elad /*
    765        1.2      elad  * Initialize the kernel authorization subsystem.
    766        1.2      elad  *
    767        1.2      elad  * Initialize the scopes list lock.
    768       1.41      elad  * Create specificdata domain.
    769       1.41      elad  * Register the credentials scope, used in kauth(9) internally.
    770       1.41      elad  * Register built-in scopes: generic, system, process, network, machdep, device.
    771        1.2      elad  */
    772        1.2      elad void
    773        1.2      elad kauth_init(void)
    774        1.2      elad {
    775       1.44        ad 	rw_init(&kauth_lock);
    776        1.2      elad 
    777       1.53        ad 	kauth_cred_cache = pool_cache_init(sizeof(struct kauth_cred),
    778       1.58        ad 	    coherency_unit, 0, 0, "kcredpl", NULL, IPL_NONE,
    779       1.55        ad 	    NULL, NULL, NULL);
    780       1.53        ad 
    781       1.41      elad 	/* Create specificdata domain. */
    782       1.41      elad 	kauth_domain = specificdata_domain_create();
    783       1.41      elad 
    784       1.41      elad 	/* Register credentials scope. */
    785       1.41      elad 	kauth_builtin_scope_cred =
    786       1.41      elad 	    kauth_register_scope(KAUTH_SCOPE_CRED, NULL, NULL);
    787       1.41      elad 
    788        1.2      elad 	/* Register generic scope. */
    789        1.2      elad 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    790       1.19      elad 	    NULL, NULL);
    791       1.19      elad 
    792       1.19      elad 	/* Register system scope. */
    793       1.19      elad 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    794       1.19      elad 	    NULL, NULL);
    795        1.2      elad 
    796        1.2      elad 	/* Register process scope. */
    797        1.2      elad 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    798       1.19      elad 	    NULL, NULL);
    799       1.19      elad 
    800       1.19      elad 	/* Register network scope. */
    801       1.19      elad 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    802       1.19      elad 	    NULL, NULL);
    803       1.19      elad 
    804       1.19      elad 	/* Register machdep scope. */
    805       1.19      elad 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    806       1.19      elad 	    NULL, NULL);
    807       1.25      elad 
    808       1.25      elad 	/* Register device scope. */
    809       1.25      elad 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    810       1.25      elad 	    NULL, NULL);
    811       1.64      elad 
    812       1.64      elad 	/* Register vnode scope. */
    813       1.64      elad 	kauth_builtin_scope_vnode = kauth_register_scope(KAUTH_SCOPE_VNODE,
    814       1.64      elad 	    NULL, NULL);
    815        1.2      elad }
    816        1.2      elad 
    817        1.2      elad /*
    818        1.2      elad  * Deregister a scope.
    819        1.2      elad  * Requires scope list lock to be held by the caller.
    820        1.2      elad  *
    821        1.2      elad  * scope - the scope to deregister
    822        1.2      elad  */
    823        1.2      elad void
    824        1.2      elad kauth_deregister_scope(kauth_scope_t scope)
    825        1.2      elad {
    826        1.2      elad 	if (scope != NULL) {
    827        1.2      elad 		/* Remove scope from list */
    828        1.2      elad 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    829       1.36      elad 		kmem_free(scope, sizeof(*scope));
    830        1.2      elad 	}
    831        1.2      elad }
    832        1.2      elad 
    833        1.2      elad /*
    834        1.2      elad  * Register a listener.
    835        1.2      elad  *
    836        1.2      elad  * id - scope identifier.
    837        1.2      elad  * callback - the callback routine for the listener.
    838        1.2      elad  * cookie - cookie to pass unmoidfied to the callback.
    839        1.2      elad  */
    840        1.2      elad kauth_listener_t
    841        1.2      elad kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    842       1.30      yamt    void *cookie)
    843        1.2      elad {
    844        1.2      elad 	kauth_scope_t scope;
    845        1.2      elad 	kauth_listener_t listener;
    846        1.2      elad 
    847       1.44        ad 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    848       1.44        ad 	if (listener == NULL)
    849       1.44        ad 		return (NULL);
    850       1.44        ad 
    851       1.44        ad 	rw_enter(&kauth_lock, RW_WRITER);
    852       1.44        ad 
    853       1.34        ad 	/*
    854       1.34        ad 	 * Find scope struct.
    855       1.34        ad 	 */
    856        1.2      elad 	scope = kauth_ifindscope(id);
    857       1.44        ad 	if (scope == NULL) {
    858       1.44        ad 		rw_exit(&kauth_lock);
    859       1.44        ad 		kmem_free(listener, sizeof(*listener));
    860        1.2      elad 		return (NULL);
    861       1.44        ad 	}
    862        1.2      elad 
    863        1.2      elad 	/* Allocate listener */
    864        1.2      elad 
    865        1.2      elad 	/* Initialize listener with parameters */
    866        1.2      elad 	listener->func = callback;
    867        1.2      elad 	listener->refcnt = 0;
    868        1.2      elad 
    869        1.2      elad 	/* Add listener to scope */
    870        1.2      elad 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    871        1.2      elad 
    872        1.2      elad 	/* Raise number of listeners on scope. */
    873        1.2      elad 	scope->nlisteners++;
    874        1.2      elad 	listener->scope = scope;
    875        1.2      elad 
    876       1.44        ad 	rw_exit(&kauth_lock);
    877       1.44        ad 
    878        1.2      elad 	return (listener);
    879        1.2      elad }
    880        1.2      elad 
    881        1.2      elad /*
    882        1.2      elad  * Deregister a listener.
    883        1.2      elad  *
    884        1.2      elad  * listener - listener reference as returned from kauth_listen_scope().
    885        1.2      elad  */
    886        1.2      elad void
    887        1.2      elad kauth_unlisten_scope(kauth_listener_t listener)
    888        1.2      elad {
    889       1.44        ad 
    890        1.2      elad 	if (listener != NULL) {
    891       1.44        ad 		rw_enter(&kauth_lock, RW_WRITER);
    892        1.3      yamt 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    893        1.3      yamt 		    kauth_listener, listener_next);
    894        1.2      elad 		listener->scope->nlisteners--;
    895       1.44        ad 		rw_exit(&kauth_lock);
    896       1.36      elad 		kmem_free(listener, sizeof(*listener));
    897        1.2      elad 	}
    898        1.2      elad }
    899        1.2      elad 
    900        1.2      elad /*
    901        1.2      elad  * Authorize a request.
    902        1.2      elad  *
    903        1.2      elad  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    904        1.2      elad  *	   returned from kauth_register_scope().
    905        1.2      elad  * credential - credentials of the user ("actor") making the request.
    906        1.2      elad  * action - request identifier.
    907        1.2      elad  * arg[0-3] - passed unmodified to listener(s).
    908       1.64      elad  *
    909       1.64      elad  * Returns the aggregated result:
    910       1.64      elad  *     - KAUTH_RESULT_ALLOW if there is at least one KAUTH_RESULT_ALLOW and
    911       1.64      elad  *       zero KAUTH_DESULT_DENY
    912       1.64      elad  *     - KAUTH_RESULT_DENY if there is at least one KAUTH_RESULT_DENY
    913       1.64      elad  *     - KAUTH_RESULT_DEFER if there is nothing but KAUTH_RESULT_DEFER
    914        1.2      elad  */
    915       1.64      elad static int
    916       1.64      elad kauth_authorize_action_internal(kauth_scope_t scope, kauth_cred_t cred,
    917       1.64      elad     kauth_action_t action, void *arg0, void *arg1, void *arg2, void *arg3)
    918        1.2      elad {
    919        1.2      elad 	kauth_listener_t listener;
    920        1.2      elad 	int error, allow, fail;
    921        1.2      elad 
    922       1.26      elad 	KASSERT(cred != NULL);
    923       1.26      elad 	KASSERT(action != 0);
    924        1.2      elad 
    925       1.17  christos 	/* Short-circuit requests coming from the kernel. */
    926       1.17  christos 	if (cred == NOCRED || cred == FSCRED)
    927       1.17  christos 		return (0);
    928       1.17  christos 
    929       1.26      elad 	KASSERT(scope != NULL);
    930       1.26      elad 
    931        1.2      elad 	fail = 0;
    932        1.2      elad 	allow = 0;
    933       1.39      elad 
    934       1.44        ad 	/* rw_enter(&kauth_lock, RW_READER); XXX not yet */
    935        1.2      elad 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    936        1.2      elad 		error = listener->func(cred, action, scope->cookie, arg0,
    937       1.44        ad 		    arg1, arg2, arg3);
    938        1.2      elad 
    939        1.2      elad 		if (error == KAUTH_RESULT_ALLOW)
    940        1.2      elad 			allow = 1;
    941        1.2      elad 		else if (error == KAUTH_RESULT_DENY)
    942        1.2      elad 			fail = 1;
    943        1.2      elad 	}
    944       1.44        ad 	/* rw_exit(&kauth_lock); */
    945        1.2      elad 
    946       1.39      elad 	if (fail)
    947       1.64      elad 		return (KAUTH_RESULT_DENY);
    948       1.64      elad 
    949       1.64      elad 	if (allow)
    950       1.64      elad 		return (KAUTH_RESULT_ALLOW);
    951       1.64      elad 
    952       1.64      elad 	return (KAUTH_RESULT_DEFER);
    953       1.64      elad };
    954       1.64      elad 
    955       1.64      elad int
    956       1.64      elad kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    957       1.64      elad     kauth_action_t action, void *arg0, void *arg1, void *arg2, void *arg3)
    958       1.64      elad {
    959       1.64      elad 	int r;
    960       1.64      elad 
    961       1.64      elad 	r = kauth_authorize_action_internal(scope, cred, action, arg0, arg1,
    962       1.64      elad 	    arg2, arg3);
    963       1.64      elad 
    964       1.64      elad 	if (r == KAUTH_RESULT_DENY)
    965       1.39      elad 		return (EPERM);
    966       1.39      elad 
    967       1.64      elad 	if (r == KAUTH_RESULT_ALLOW)
    968       1.39      elad 		return (0);
    969       1.39      elad 
    970  1.65.16.1       mrg 	if (secmodel_nsecmodels() == 0)
    971       1.39      elad 		return (0);
    972       1.39      elad 
    973       1.39      elad 	return (EPERM);
    974       1.64      elad }
    975        1.2      elad 
    976        1.2      elad /*
    977        1.2      elad  * Generic scope authorization wrapper.
    978        1.2      elad  */
    979        1.2      elad int
    980        1.2      elad kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    981        1.2      elad {
    982        1.2      elad 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    983        1.2      elad 	    action, arg0, NULL, NULL, NULL));
    984        1.2      elad }
    985        1.2      elad 
    986        1.2      elad /*
    987       1.19      elad  * System scope authorization wrapper.
    988        1.2      elad  */
    989        1.2      elad int
    990       1.19      elad kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    991       1.19      elad     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    992        1.2      elad {
    993       1.19      elad 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    994       1.19      elad 	    action, (void *)req, arg1, arg2, arg3));
    995        1.2      elad }
    996        1.2      elad 
    997        1.2      elad /*
    998        1.2      elad  * Process scope authorization wrapper.
    999        1.2      elad  */
   1000        1.2      elad int
   1001        1.2      elad kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
   1002        1.2      elad     struct proc *p, void *arg1, void *arg2, void *arg3)
   1003        1.2      elad {
   1004        1.2      elad 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
   1005        1.2      elad 	    action, p, arg1, arg2, arg3));
   1006        1.2      elad }
   1007       1.19      elad 
   1008       1.19      elad /*
   1009       1.19      elad  * Network scope authorization wrapper.
   1010       1.19      elad  */
   1011       1.19      elad int
   1012       1.19      elad kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
   1013       1.23      elad     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
   1014       1.19      elad {
   1015       1.19      elad 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
   1016       1.23      elad 	    action, (void *)req, arg1, arg2, arg3));
   1017       1.19      elad }
   1018       1.19      elad 
   1019       1.19      elad int
   1020       1.19      elad kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
   1021       1.35      elad     void *arg0, void *arg1, void *arg2, void *arg3)
   1022       1.19      elad {
   1023       1.19      elad 	return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
   1024       1.35      elad 	    action, arg0, arg1, arg2, arg3));
   1025       1.19      elad }
   1026       1.25      elad 
   1027       1.25      elad int
   1028       1.32      elad kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
   1029       1.32      elad     void *arg0, void *arg1, void *arg2, void *arg3)
   1030       1.32      elad {
   1031       1.32      elad 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
   1032       1.32      elad 	    action, arg0, arg1, arg2, arg3));
   1033       1.32      elad }
   1034       1.32      elad 
   1035       1.32      elad int
   1036       1.25      elad kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
   1037       1.25      elad     struct tty *tty)
   1038       1.25      elad {
   1039       1.25      elad 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
   1040       1.25      elad 	    action, tty, NULL, NULL, NULL));
   1041       1.25      elad }
   1042       1.31      elad 
   1043       1.31      elad int
   1044       1.31      elad kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
   1045       1.31      elad     struct vnode *vp)
   1046       1.31      elad {
   1047       1.31      elad 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
   1048       1.31      elad 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
   1049       1.31      elad }
   1050       1.31      elad 
   1051       1.31      elad int
   1052       1.33      elad kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
   1053       1.33      elad     void *data)
   1054       1.31      elad {
   1055       1.31      elad 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
   1056       1.33      elad 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
   1057       1.33      elad 	    data, NULL));
   1058       1.31      elad }
   1059       1.39      elad 
   1060       1.64      elad kauth_action_t
   1061       1.64      elad kauth_mode_to_action(mode_t mode)
   1062       1.64      elad {
   1063       1.64      elad 	kauth_action_t action = 0;
   1064       1.64      elad 
   1065       1.64      elad 	if (mode & VREAD)
   1066       1.64      elad 		action |= KAUTH_VNODE_READ_DATA;
   1067       1.64      elad 	if (mode & VWRITE)
   1068       1.64      elad 		action |= KAUTH_VNODE_WRITE_DATA;
   1069       1.64      elad 	if (mode & VEXEC)
   1070       1.64      elad 		action |= KAUTH_VNODE_EXECUTE;
   1071       1.64      elad 
   1072       1.64      elad 	return action;
   1073       1.64      elad }
   1074       1.64      elad 
   1075  1.65.16.2       mrg kauth_action_t
   1076  1.65.16.2       mrg kauth_access_action(mode_t access_mode, enum vtype vn_type, mode_t file_mode)
   1077  1.65.16.2       mrg {
   1078  1.65.16.2       mrg 	kauth_action_t action = kauth_mode_to_action(access_mode);
   1079  1.65.16.2       mrg 
   1080  1.65.16.2       mrg 	if (FS_OBJECT_CAN_EXEC(vn_type, file_mode))
   1081  1.65.16.2       mrg 		action |= KAUTH_VNODE_IS_EXEC;
   1082  1.65.16.2       mrg 
   1083  1.65.16.2       mrg 	return action;
   1084  1.65.16.2       mrg }
   1085  1.65.16.2       mrg 
   1086  1.65.16.2       mrg kauth_action_t
   1087  1.65.16.2       mrg kauth_extattr_action(mode_t access_mode)
   1088  1.65.16.2       mrg {
   1089  1.65.16.2       mrg 	kauth_action_t action = 0;
   1090  1.65.16.2       mrg 
   1091  1.65.16.2       mrg 	if (access_mode & VREAD)
   1092  1.65.16.2       mrg 		action |= KAUTH_VNODE_READ_EXTATTRIBUTES;
   1093  1.65.16.2       mrg 	if (access_mode & VWRITE)
   1094  1.65.16.2       mrg 		action |= KAUTH_VNODE_WRITE_EXTATTRIBUTES;
   1095  1.65.16.2       mrg 
   1096  1.65.16.2       mrg 	return action;
   1097  1.65.16.2       mrg }
   1098  1.65.16.2       mrg 
   1099       1.64      elad int
   1100       1.64      elad kauth_authorize_vnode(kauth_cred_t cred, kauth_action_t action,
   1101       1.64      elad     struct vnode *vp, struct vnode *dvp, int fs_decision)
   1102       1.64      elad {
   1103       1.64      elad 	int error;
   1104       1.64      elad 
   1105       1.64      elad 	error = kauth_authorize_action_internal(kauth_builtin_scope_vnode, cred,
   1106       1.64      elad 	    action, vp, dvp, NULL, NULL);
   1107       1.64      elad 
   1108       1.64      elad 	if (error == KAUTH_RESULT_DENY)
   1109       1.64      elad 		return (EACCES);
   1110       1.64      elad 
   1111       1.64      elad 	if (error == KAUTH_RESULT_ALLOW)
   1112       1.64      elad 		return (0);
   1113       1.64      elad 
   1114       1.64      elad 	/*
   1115       1.64      elad 	 * If the file-system does not support decision-before-action, we can
   1116       1.64      elad 	 * only short-circuit the operation (deny). If we're here, it means no
   1117       1.64      elad 	 * listener denied it, so our only alternative is to supposedly-allow
   1118       1.64      elad 	 * it and let the file-system have the last word.
   1119       1.64      elad 	 */
   1120       1.64      elad 	if (fs_decision == KAUTH_VNODE_REMOTEFS)
   1121       1.64      elad 		return (0);
   1122       1.64      elad 
   1123       1.64      elad 	return (fs_decision);
   1124       1.64      elad }
   1125       1.64      elad 
   1126       1.41      elad static int
   1127       1.41      elad kauth_cred_hook(kauth_cred_t cred, kauth_action_t action, void *arg0,
   1128       1.41      elad     void *arg1)
   1129       1.41      elad {
   1130       1.41      elad 	int r;
   1131       1.41      elad 
   1132       1.41      elad 	r = kauth_authorize_action(kauth_builtin_scope_cred, cred, action,
   1133       1.41      elad 	    arg0, arg1, NULL, NULL);
   1134       1.41      elad 
   1135       1.42      elad #ifdef DIAGNOSTIC
   1136       1.42      elad 	if (!SIMPLEQ_EMPTY(&kauth_builtin_scope_cred->listenq))
   1137       1.42      elad 		KASSERT(r == 0);
   1138       1.42      elad #endif /* DIAGNOSTIC */
   1139       1.41      elad 
   1140       1.41      elad 	return (r);
   1141       1.41      elad }
   1142