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