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