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