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