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