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