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