kern_auth.c revision 1.1.2.13 1 /* $NetBSD: kern_auth.c,v 1.1.2.13 2006/03/10 19:11:50 elad 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Elad Efrat.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Todo:
35 * - Garbage collection to pool_put() unused scopes/listeners.
36 */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/time.h>
42 #include <sys/proc.h>
43 #include <sys/ucred.h>
44 #include <sys/pool.h>
45 #include <sys/kauth.h>
46 #include <sys/acct.h>
47
48 /*
49 * Credentials.
50 */
51 struct kauth_cred {
52 struct simplelock cr_lock; /* lock */
53 uint32_t cr_refcnt; /* reference count */
54 uid_t cr_uid; /* user id */
55 uid_t cr_euid; /* effective user id */
56 uid_t cr_svuid; /* saved effective user id */
57 gid_t cr_gid; /* group id */
58 gid_t cr_egid; /* effective group id */
59 gid_t cr_svgid; /* saved effective group id */
60 uint16_t cr_ngroups; /* number of groups */
61 gid_t cr_groups[NGROUPS]; /* group memberships */
62 };
63
64 /*
65 * Listener.
66 */
67 struct kauth_listener {
68 kauth_scope_callback_t func; /* callback */
69 kauth_scope_t scope; /* scope backpointer */
70 uint32_t refcnt; /* reference count */
71 SIMPLEQ_ENTRY(kauth_listener) listener_next; /* listener list */
72 };
73
74 /*
75 * Scope.
76 */
77 struct kauth_scope {
78 const char *id; /* scope name */
79 void *cookie; /* user cookie */
80 uint32_t nlisteners; /* # of listeners */
81 SIMPLEQ_HEAD(, kauth_listener) listenq; /* listener list */
82 SIMPLEQ_ENTRY(kauth_scope) next_scope; /* scope list */
83 };
84
85 POOL_INIT(kauth_scope_pool, sizeof(struct kauth_scope), 0, 0, 0,
86 "kauth_scopepl", &pool_allocator_nointr);
87 POOL_INIT(kauth_listener_pool, sizeof(struct kauth_listener), 0, 0, 0,
88 "kauth_listenerpl", &pool_allocator_nointr);
89 POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
90 "kauth_credpl", &pool_allocator_nointr);
91
92 /* List of scopes and its lock. */
93 SIMPLEQ_HEAD(, kauth_scope) scope_list;
94 struct simplelock scopes_lock;
95
96 /* Built-in scopes: generic, process. */
97 static kauth_scope_t kauth_builtin_scope_generic;
98 static kauth_scope_t kauth_builtin_scope_process;
99
100 /* Allocate new, empty kauth credentials. */
101 kauth_cred_t
102 kauth_cred_alloc(void) {
103 kauth_cred_t cred;
104
105 cred = pool_get(&kauth_cred_pool, PR_WAITOK);
106 memset(cred, 0, sizeof(*cred));
107 simple_lock_init(&cred->cr_lock);
108 cred->cr_refcnt = 1;
109
110 return (cred);
111 }
112
113 /* Increment reference count to cred. */
114 void
115 kauth_cred_hold(kauth_cred_t cred)
116 {
117 KASSERT(cred != NULL);
118
119 simple_lock(&cred->cr_lock);
120 cred->cr_refcnt++;
121 simple_unlock(&cred->cr_lock);
122 }
123
124 void
125 kauth_cred_destroy(kauth_cred_t cred)
126 {
127 KASSERT(cred != NULL);
128
129 pool_put(&kauth_cred_pool, cred);
130 }
131
132 /* Decrease reference count to cred. If reached zero, free it. */
133 void
134 kauth_cred_free(kauth_cred_t cred) {
135 KASSERT(cred != NULL);
136
137 simple_lock(&cred->cr_lock);
138 cred->cr_refcnt--;
139 simple_unlock(&cred->cr_lock);
140
141 if (cred->cr_refcnt == 0)
142 kauth_cred_destroy(cred);
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
151 to->cr_uid = from->cr_uid;
152 to->cr_euid = from->cr_euid;
153 to->cr_svuid = from->cr_svuid;
154 to->cr_gid = from->cr_gid;
155 to->cr_egid = from->cr_egid;
156 to->cr_svgid = from->cr_svgid;
157 to->cr_ngroups = from->cr_ngroups;
158 memcpy(to->cr_groups, from->cr_groups,
159 sizeof(to->cr_groups));
160 }
161
162 /*
163 * Duplicate cred and return a new kauth_cred_t.
164 */
165 kauth_cred_t
166 kauth_cred_dup(kauth_cred_t cred)
167 {
168 kauth_cred_t new_cred;
169
170 KASSERT(cred != NULL);
171
172 new_cred = kauth_cred_alloc();
173
174 kauth_cred_clone(cred, new_cred);
175
176 return (new_cred);
177 }
178
179 /*
180 * Similar to crcopy(), only on a kauth_cred_t.
181 * XXX: Is this even needed? [kauth_cred_copy]
182 */
183 kauth_cred_t
184 kauth_cred_copy(kauth_cred_t cred)
185 {
186 kauth_cred_t new_cred;
187
188 KASSERT(cred != NULL);
189
190 /* If the provided credentials already have one reference, use them. */
191 if (cred->cr_refcnt == 1)
192 return (cred);
193
194 new_cred = kauth_cred_alloc();
195
196 kauth_cred_clone(cred, new_cred);
197
198 kauth_cred_free(cred);
199
200 return (new_cred);
201 }
202
203 /*
204 * Zero a kauth_cred_t, and re-initialize the lock.
205 * XXX: Is this needed? [kauth_cred_zero]
206 */
207 void
208 kauth_cred_zero(kauth_cred_t cred)
209 {
210 KASSERT(cred != NULL);
211
212 memset(cred, 0, sizeof(*cred));
213 simple_lock_init(&cred->cr_lock);
214 }
215
216 uid_t
217 kauth_cred_getuid(kauth_cred_t cred)
218 {
219 KASSERT(cred != NULL);
220
221 return (cred->cr_uid);
222 }
223
224 uid_t
225 kauth_cred_geteuid(kauth_cred_t cred)
226 {
227 KASSERT(cred != NULL);
228
229 return (cred->cr_euid);
230 }
231
232 uid_t
233 kauth_cred_getsvuid(kauth_cred_t cred)
234 {
235 KASSERT(cred != NULL);
236
237 return (cred->cr_svuid);
238 }
239
240 gid_t
241 kauth_cred_getgid(kauth_cred_t cred)
242 {
243 KASSERT(cred != NULL);
244
245 return (cred->cr_gid);
246 }
247
248 gid_t
249 kauth_cred_getegid(kauth_cred_t cred)
250 {
251 KASSERT(cred != NULL);
252
253 return (cred->cr_egid);
254 }
255
256 gid_t
257 kauth_cred_getsvgid(kauth_cred_t cred)
258 {
259 KASSERT(cred != NULL);
260
261 return (cred->cr_svgid);
262 }
263
264 void
265 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
266 {
267 KASSERT(cred != NULL);
268 KASSERT(uid >= 0 && uid <= UID_MAX);
269
270 cred->cr_uid = uid;
271 }
272
273 void
274 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
275 {
276 KASSERT(cred != NULL);
277 KASSERT(uid >= 0 && uid <= UID_MAX);
278
279 cred->cr_euid = uid;
280 }
281
282 void
283 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
284 {
285 KASSERT(cred != NULL);
286 KASSERT(uid >= 0 && uid <= UID_MAX);
287
288 cred->cr_svuid = uid;
289 }
290
291 void
292 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
293 {
294 KASSERT(cred != NULL);
295 KASSERT(gid >= 0 && gid <= GID_MAX);
296
297 cred->cr_gid = gid;
298 }
299
300 void
301 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
302 {
303 KASSERT(cred != NULL);
304 KASSERT(gid >= 0 && gid <= GID_MAX);
305
306 cred->cr_egid = gid;
307 }
308
309 void
310 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
311 {
312 KASSERT(cred != NULL);
313 KASSERT(gid >= 0 && gid <= GID_MAX);
314
315 cred->cr_svgid = gid;
316 }
317
318 /* Checks if gid is a member of the groups in cred. */
319 int
320 kauth_cred_groupmember(kauth_cred_t cred, gid_t gid)
321 {
322 int i;
323
324 KASSERT(cred != NULL);
325 KASSERT(gid >= 0 && gid <= GID_MAX);
326
327 for (i = 0; i < cred->cr_ngroups; i++) {
328 if (cred->cr_groups[i] == gid)
329 return (1);
330 }
331
332 return (0);
333 }
334
335 uint16_t
336 kauth_cred_ngroups(kauth_cred_t cred)
337 {
338 KASSERT(cred != NULL);
339
340 return (cred->cr_ngroups);
341 }
342
343 /*
344 * Return the group at index idx from the groups in cred.
345 */
346 gid_t
347 kauth_cred_group(kauth_cred_t cred, uint16_t idx)
348 {
349 KASSERT(cred != NULL);
350 KASSERT(idx < cred->cr_ngroups);
351
352 return (cred->cr_groups[idx]);
353 }
354
355 /* XXX: Is this needed? [setngroups] */
356 void
357 kauth_cred_setngroups(kauth_cred_t cred, uint16_t ngroups)
358 {
359 KASSERT(cred != NULL);
360 KASSERT(ngroups < NGROUPS);
361
362 cred->cr_ngroups = ngroups;
363 }
364
365 /* Sort len groups in grbuf. */
366 static void
367 kauth_cred_sortgroups(gid_t *grbuf, size_t len)
368 {
369 int i, j;
370 gid_t v;
371
372 KASSERT(grbuf != NULL);
373
374 /* Insertion sort. */
375 for (i = 1; i < len; i++) {
376 v = grbuf[i];
377 /* find correct slot for value v, moving others up */
378 for (j = i; --j >= 0 && v < grbuf[j];)
379 grbuf[j + 1] = grbuf[j];
380 grbuf[j + 1] = v;
381 }
382 }
383
384 /* Add group gid to groups in cred. Maintain order. */
385 int
386 kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
387 {
388 KASSERT(cred != NULL);
389 KASSERT(gid >= 0 && gid <= GID_MAX);
390
391 simple_lock(&cred->cr_lock);
392
393 if (cred->cr_ngroups >=
394 (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
395 simple_unlock(&cred->cr_lock);
396 return (E2BIG);
397 }
398
399 if (kauth_cred_groupmember(cred, gid)) {
400 simple_unlock(&cred->cr_lock);
401 return (0);
402 }
403
404 cred->cr_groups[cred->cr_ngroups++] = gid;
405
406 kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
407
408 simple_unlock(&cred->cr_lock);
409
410 return (0);
411 }
412
413 /* Delete group gid from groups in cred. Maintain order. */
414 int
415 kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
416 {
417 KASSERT(cred != NULL);
418 KASSERT(gid >= 0 && gid <= GID_MAX);
419
420 simple_lock(&cred->cr_lock);
421
422 if (!kauth_cred_groupmember(cred, gid)) {
423 simple_unlock(&cred->cr_lock);
424 return (0);
425 }
426
427 if (cred->cr_ngroups == 1)
428 cred->cr_groups[0] = (gid_t)-1; /* XXX */
429 else {
430 int i;
431
432 for (i = 0; i < cred->cr_ngroups; i++)
433 if (cred->cr_groups[i] == gid) {
434 cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
435 cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
436 }
437 }
438
439 cred->cr_ngroups--;
440
441 kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
442
443 simple_unlock(&cred->cr_lock);
444
445 return (0);
446 }
447
448 /*
449 * Match uids in two credentials. Checks if cred1 can access stuff owned by
450 * cred2.
451 * XXX: root bypasses this!
452 */
453 int
454 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
455 {
456 KASSERT(cred1 != NULL);
457 KASSERT(cred2 != NULL);
458
459 /* Are we root? */
460 if (cred1->cr_euid == 0)
461 return (1);
462
463 if (cred1->cr_uid == cred2->cr_uid ||
464 cred1->cr_euid == cred2->cr_uid ||
465 cred1->cr_uid == cred2->cr_euid ||
466 cred1->cr_euid == cred2->cr_euid)
467 return (1);
468
469 return (0);
470 }
471
472 uint32_t
473 kauth_cred_getrefcnt(kauth_cred_t cred)
474 {
475 KASSERT(cred != NULL);
476
477 return (cred->cr_refcnt);
478 }
479
480 /* XXX: memcmp() wrapper needed for NFSW_SAMECRED() in nfs/nfs.h */
481 int
482 kauth_cred_memcmp(kauth_cred_t cred1, kauth_cred_t cred2)
483 {
484 KASSERT(cred1 != NULL);
485 KASSERT(cred2 != NULL);
486
487 return (memcmp(cred1, cred2, sizeof(*cred1)));
488 }
489
490 /*
491 * Convert userland credentials (struct uucred) to kauth_cred_t.
492 * XXX: For NFS code.
493 */
494 void
495 kauth_cred_convert(kauth_cred_t cred, const struct uucred *uuc)
496 {
497 int i;
498
499 KASSERT(cred != NULL);
500 KASSERT(uuc != NULL);
501
502 cred->cr_refcnt = 1;
503 cred->cr_euid = uuc->cr_uid;
504 cred->cr_egid = uuc->cr_gid;
505 cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
506 for (i = 0; i < cred->cr_ngroups; i++)
507 kauth_cred_addgroup(cred, uuc->cr_groups[i]);
508 }
509
510 /*
511 * Compare kauth_cred_t and uucred credentials.
512 * XXX: Modelled after crcmp() for NFS.
513 */
514 int
515 kauth_cred_compare(kauth_cred_t cred, const struct uucred *uuc)
516 {
517 KASSERT(cred != NULL);
518 KASSERT(uuc != NULL);
519
520 if (cred->cr_euid == uuc->cr_uid &&
521 cred->cr_egid == uuc->cr_gid &&
522 cred->cr_ngroups == uuc->cr_ngroups &&
523 !memcmp(cred->cr_groups, uuc->cr_groups,
524 cred->cr_ngroups * sizeof(cred->cr_groups[0])))
525 return (1);
526
527 return (0);
528 }
529
530 /*
531 * Make a struct ucred out of a kauth_cred_t.
532 * XXX: For sysctl.
533 */
534 void
535 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
536 {
537 KASSERT(cred != NULL);
538 KASSERT(uc != NULL);
539
540 uc->cr_uid = cred->cr_euid;
541 uc->cr_gid = cred->cr_egid;
542 uc->cr_ngroups = min(cred->cr_ngroups,
543 sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
544 memcpy(uc->cr_groups, cred->cr_groups,
545 uc->cr_ngroups * sizeof(uc->cr_groups[0]));
546 }
547
548 /*
549 * Make a struct pcred out of a kauth_cred_t.
550 * XXX: For sysctl.
551 */
552 void
553 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
554 {
555 KASSERT(cred != NULL);
556 KASSERT(pc != NULL);
557
558 pc->pc_ucred = (struct ucred *)cred; /* XXX this is just wrong */
559 pc->p_ruid = cred->cr_uid;
560 pc->p_svuid = cred->cr_svuid;
561 pc->p_rgid = cred->cr_gid;
562 pc->p_svgid = cred->cr_svgid;
563 pc->p_refcnt = cred->cr_refcnt;
564 }
565
566 /*
567 * Return kauth_cred_t for the current process.
568 */
569 kauth_cred_t
570 kauth_cred_get(void)
571 {
572 return (curproc->p_cred);
573 }
574
575 /*
576 * Returns a scope matching the provided id.
577 * Requires the scope list lock to be held by the caller.
578 */
579 static kauth_scope_t
580 kauth_ifindscope(const char *id) {
581 kauth_scope_t scope;
582
583 /* XXX: assert lock on scope list? */
584
585 scope = NULL;
586 SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
587 if (strcmp(scope->id, id) == 0)
588 break;
589 }
590
591 return (scope);
592 }
593
594 /*
595 * Register a new scope.
596 *
597 * id - identifier for the scope
598 * callback - the scope's default listener
599 * cookie - cookie to be passed to the listener(s)
600 */
601 kauth_scope_t
602 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
603 void *cookie)
604 {
605 kauth_scope_t scope;
606 kauth_listener_t listener;
607
608 /* Sanitize input */
609 if (id == NULL || callback == NULL)
610 return (NULL);
611
612 /* Allocate space for a new scope and listener. */
613 scope = pool_get(&kauth_scope_pool, PR_WAITOK);
614 listener = pool_get(&kauth_listener_pool, PR_WAITOK);
615
616 /* Acquire scope list lock. */
617 simple_lock(&scopes_lock);
618
619 /* Check we don't already have a scope with the same id */
620 if (kauth_ifindscope(id) != NULL) {
621 simple_unlock(&scopes_lock);
622
623 pool_put(&kauth_scope_pool, scope);
624 pool_put(&kauth_listener_pool, listener);
625
626 return (NULL);
627 }
628
629 /* Initialize new scope with parameters */
630 scope->id = id;
631 scope->cookie = cookie;
632 scope->nlisteners = 1;
633
634 /* Add default listener */
635 listener->func = callback;
636 listener->scope = scope;
637 listener->refcnt = 0;
638 SIMPLEQ_INIT(&scope->listenq);
639 SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
640
641 /* Insert scope to scopes list */
642 if (SIMPLEQ_EMPTY(&scope_list))
643 SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
644 else
645 SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
646
647 simple_unlock(&scopes_lock);
648
649 return (scope);
650 }
651
652 /*
653 * Initialize the kernel authorization subsystem.
654 *
655 * Initialize the scopes list lock.
656 * Register built-in scopes: generic, process.
657 */
658 void
659 kauth_init(void)
660 {
661 simple_lock_init(&scopes_lock);
662
663 /* Register generic scope. */
664 kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
665 kauth_authorize_cb_generic, NULL);
666
667 /* Register process scope. */
668 kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
669 kauth_authorize_cb_process, NULL);
670 }
671
672 /*
673 * Deregister a scope.
674 * Requires scope list lock to be held by the caller.
675 *
676 * scope - the scope to deregister
677 */
678 void
679 kauth_deregister_scope(kauth_scope_t scope)
680 {
681 if (scope != NULL) {
682 /* Remove scope from list */
683 SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
684 }
685 }
686
687 /*
688 * Register a listener.
689 *
690 * id - scope identifier.
691 * callback - the callback routine for the listener.
692 * cookie - cookie to pass unmoidfied to the callback.
693 */
694 kauth_listener_t
695 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
696 void *cookie)
697 {
698 kauth_scope_t scope;
699 kauth_listener_t listener;
700
701 /* Find scope struct */
702 simple_lock(&scopes_lock);
703 scope = kauth_ifindscope(id);
704 simple_unlock(&scopes_lock);
705 if (scope == NULL)
706 return (NULL);
707
708 /* Allocate listener */
709 listener = pool_get(&kauth_listener_pool, PR_WAITOK);
710
711 /* Initialize listener with parameters */
712 listener->func = callback;
713 listener->refcnt = 0;
714
715 /* Add listener to scope */
716 SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
717
718 /* Raise number of listeners on scope. */
719 scope->nlisteners++;
720 listener->scope = scope;
721
722 return (listener);
723 }
724
725 /*
726 * Deregister a listener.
727 *
728 * listener - listener reference as returned from kauth_listen_scope().
729 */
730 void
731 kauth_unlisten_scope(kauth_listener_t listener)
732 {
733 if (listener != NULL) {
734 SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
735 listener_next);
736 listener->scope->nlisteners--;
737 }
738 }
739
740 /*
741 * Authorize a request.
742 *
743 * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
744 * returned from kauth_register_scope().
745 * credential - credentials of the user ("actor") making the request.
746 * action - request identifier.
747 * arg[0-3] - passed unmodified to listener(s).
748 */
749 int
750 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
751 kauth_action_t action, void *arg0, void *arg1,
752 void *arg2, void *arg3)
753 {
754 kauth_listener_t listener;
755 int error, allow, fail;
756
757 /* Sanitize input */
758 if (scope == NULL || cred == NULL)
759 return (EFAULT);
760 if (!action)
761 return (EINVAL);
762
763 /*
764 * Each scope is associated with at least one listener. We need to
765 * traverse that list of listeners, as long as they return either
766 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
767 */
768 fail = 0;
769 allow = 0;
770 SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
771 error = listener->func(cred, action, scope->cookie, arg0,
772 arg1, arg2, arg3);
773
774 if (error == KAUTH_RESULT_ALLOW)
775 allow = 1;
776 else if (error == KAUTH_RESULT_DENY)
777 fail = 1;
778 }
779
780 return ((allow && !fail) ? 0 : EPERM);
781 };
782
783 /*
784 * Generic scope default callback.
785 */
786 int
787 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
788 void *cookie, void *arg0, void *arg1, void *arg2,
789 void *arg3)
790 {
791 int error;
792
793 error = KAUTH_RESULT_DEFER;
794
795 switch (action) {
796 case KAUTH_GENERIC_ISSUSER:
797 /* Check if credential belongs to superuser. */
798 if (cred->cr_euid == 0) {
799 u_short *acflag = (u_short *)arg0;
800
801 if (acflag != NULL)
802 *acflag |= ASU;
803
804 error = KAUTH_RESULT_ALLOW;
805 } else
806 error = KAUTH_RESULT_DENY;
807 break;
808 }
809
810 return (error);
811 }
812
813 /*
814 * Generic scope authorization wrapper.
815 */
816 int
817 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
818 {
819 return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
820 action, arg0, NULL, NULL, NULL));
821 }
822
823 /*
824 * Process scope default callback.
825 */
826 int
827 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
828 void *cookie, void *arg0, void *arg1, void *arg2,
829 void *arg3)
830 {
831 struct proc *p;
832 kauth_cred_t cred2;
833 int error;
834
835 error = KAUTH_RESULT_DEFER;
836
837 p = arg0;
838 cred2 = arg1;
839
840 switch (action) {
841 case KAUTH_PROCESS_CANSIGNAL: {
842 struct proc *to;
843 int signum;
844
845 to = arg2;
846 signum = (int)(unsigned long)arg3;
847
848 if (kauth_cred_uidmatch(cred, cred2) ||
849 (signum == SIGCONT && (p->p_session == to->p_session)))
850 error = KAUTH_RESULT_ALLOW;
851 else
852 error = KAUTH_RESULT_DEFER;
853
854 break;
855 }
856
857 case KAUTH_PROCESS_CANPTRACE:
858 if (kauth_cred_uidmatch(cred, cred2))
859 error = KAUTH_RESULT_ALLOW;
860 else
861 error = KAUTH_RESULT_DENY;
862 break;
863
864 case KAUTH_PROCESS_CANSEE:
865 if (kauth_cred_uidmatch(cred, cred2))
866 error = KAUTH_RESULT_ALLOW;
867 else
868 error = KAUTH_RESULT_DENY;
869 /* arg2 - type of information [XXX NOTIMPL] */
870 break;
871 }
872
873 return (error);
874 }
875
876 /*
877 * Process scope authorization wrapper.
878 */
879 int
880 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
881 struct proc *p, void *arg1, void *arg2, void *arg3)
882 {
883 return (kauth_authorize_action(kauth_builtin_scope_process, cred,
884 action, p, arg1, arg2, arg3));
885 }
886