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