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