kern_auth.c revision 1.1.2.9 1 /* $NetBSD: kern_auth.c,v 1.1.2.9 2006/03/09 17:07:10 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 /* XXX: For debugging. */
130 memset(cred, 0, sizeof(*cred));
131
132 pool_put(&kauth_cred_pool, cred);
133 }
134
135 /* Decrease reference count to cred. If reached zero, free it. */
136 void
137 kauth_cred_free(kauth_cred_t cred) {
138 KASSERT(cred != NULL);
139
140 simple_lock(&cred->cr_lock);
141 cred->cr_refcnt--;
142 simple_unlock(&cred->cr_lock);
143
144 if (cred->cr_refcnt == 0)
145 kauth_cred_destroy(cred);
146 }
147
148 void
149 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
150 {
151 KASSERT(from != NULL);
152 KASSERT(to != NULL);
153
154 to->cr_uid = from->cr_uid;
155 to->cr_euid = from->cr_euid;
156 to->cr_svuid = from->cr_svuid;
157 to->cr_gid = from->cr_gid;
158 to->cr_egid = from->cr_egid;
159 to->cr_svgid = from->cr_svgid;
160 to->cr_ngroups = from->cr_ngroups;
161 memcpy(to->cr_groups, from->cr_groups,
162 sizeof(to->cr_groups));
163 }
164
165 /*
166 * Duplicate cred and return a new kauth_cred_t.
167 */
168 kauth_cred_t
169 kauth_cred_dup(kauth_cred_t cred)
170 {
171 kauth_cred_t new_cred;
172
173 KASSERT(cred != NULL);
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
193 /* If the provided credentials already have one reference, use them. */
194 if (cred->cr_refcnt == 1)
195 return (cred);
196
197 new_cred = kauth_cred_alloc();
198
199 kauth_cred_clone(cred, new_cred);
200
201 kauth_cred_free(cred);
202
203 return (new_cred);
204 }
205
206 /*
207 * Zero a kauth_cred_t, and re-initialize the lock.
208 * XXX: Is this needed? [kauth_cred_zero]
209 */
210 void
211 kauth_cred_zero(kauth_cred_t cred)
212 {
213 KASSERT(cred != NULL);
214
215 memset(cred, 0, sizeof(*cred));
216 simple_lock_init(&cred->cr_lock);
217 }
218
219 uid_t
220 kauth_cred_getuid(kauth_cred_t cred)
221 {
222 KASSERT(cred != NULL);
223
224 return (cred->cr_uid);
225 }
226
227 uid_t
228 kauth_cred_geteuid(kauth_cred_t cred)
229 {
230 KASSERT(cred != NULL);
231
232 return (cred->cr_euid);
233 }
234
235 uid_t
236 kauth_cred_getsvuid(kauth_cred_t cred)
237 {
238 KASSERT(cred != NULL);
239
240 return (cred->cr_svuid);
241 }
242
243 gid_t
244 kauth_cred_getgid(kauth_cred_t cred)
245 {
246 KASSERT(cred != NULL);
247
248 return (cred->cr_gid);
249 }
250
251 gid_t
252 kauth_cred_getegid(kauth_cred_t cred)
253 {
254 KASSERT(cred != NULL);
255
256 return (cred->cr_egid);
257 }
258
259 gid_t
260 kauth_cred_getsvgid(kauth_cred_t cred)
261 {
262 KASSERT(cred != NULL);
263
264 return (cred->cr_svgid);
265 }
266
267 void
268 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
269 {
270 KASSERT(cred != NULL);
271 KASSERT(uid >= 0 && uid <= UID_MAX);
272
273 cred->cr_uid = uid;
274 }
275
276 void
277 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
278 {
279 KASSERT(cred != NULL);
280 KASSERT(uid >= 0 && uid <= UID_MAX);
281
282 cred->cr_euid = uid;
283 }
284
285 void
286 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
287 {
288 KASSERT(cred != NULL);
289 KASSERT(uid >= 0 && uid <= UID_MAX);
290
291 cred->cr_svuid = uid;
292 }
293
294 void
295 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
296 {
297 KASSERT(cred != NULL);
298 KASSERT(gid >= 0 && gid <= GID_MAX);
299
300 cred->cr_gid = gid;
301 }
302
303 void
304 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
305 {
306 KASSERT(cred != NULL);
307 KASSERT(gid >= 0 && gid <= GID_MAX);
308
309 cred->cr_egid = gid;
310 }
311
312 void
313 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
314 {
315 KASSERT(cred != NULL);
316 KASSERT(gid >= 0 && gid <= GID_MAX);
317
318 cred->cr_svgid = gid;
319 }
320
321 /* Checks if gid is a member of the groups in cred. */
322 int
323 kauth_cred_groupmember(kauth_cred_t cred, gid_t gid)
324 {
325 int i;
326
327 KASSERT(cred != NULL);
328 KASSERT(gid >= 0 && gid <= GID_MAX);
329
330 for (i = 0; i < cred->cr_ngroups; i++) {
331 if (cred->cr_groups[i] == gid)
332 return (1);
333 }
334
335 return (0);
336 }
337
338 uint16_t
339 kauth_cred_ngroups(kauth_cred_t cred)
340 {
341 KASSERT(cred != NULL);
342
343 return (cred->cr_ngroups);
344 }
345
346 /*
347 * Return the group at index idx from the groups in cred.
348 */
349 gid_t
350 kauth_cred_group(kauth_cred_t cred, uint16_t idx)
351 {
352 KASSERT(cred != NULL);
353 KASSERT(idx < cred->cr_ngroups);
354
355 return (cred->cr_groups[idx]);
356 }
357
358 /* XXX: Is this needed? [setngroups] */
359 void
360 kauth_cred_setngroups(kauth_cred_t cred, uint16_t ngroups)
361 {
362 KASSERT(cred != NULL);
363 KASSERT(ngroups < NGROUPS);
364
365 cred->cr_ngroups = ngroups;
366 }
367
368 /* Sort len groups in grbuf. */
369 static void
370 kauth_cred_sortgroups(gid_t *grbuf, size_t len)
371 {
372 int i, j;
373 gid_t v;
374
375 KASSERT(grbuf != NULL);
376
377 /* Insertion sort. */
378 for (i = 1; i < len; i++) {
379 v = grbuf[i];
380 /* find correct slot for value v, moving others up */
381 for (j = i; --j >= 0 && v < grbuf[j];)
382 grbuf[j + 1] = grbuf[j];
383 grbuf[j + 1] = v;
384 }
385 }
386
387 /* Add group gid to groups in cred. Maintain order. */
388 int
389 kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
390 {
391 KASSERT(cred != NULL);
392 KASSERT(gid >= 0 && gid <= GID_MAX);
393
394 simple_lock(&cred->cr_lock);
395
396 if (cred->cr_ngroups >=
397 (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
398 simple_unlock(&cred->cr_lock);
399 return (E2BIG);
400 }
401
402 if (kauth_cred_groupmember(cred, gid)) {
403 simple_unlock(&cred->cr_lock);
404 return (0);
405 }
406
407 cred->cr_groups[cred->cr_ngroups++] = gid;
408
409 kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
410
411 simple_unlock(&cred->cr_lock);
412
413 return (0);
414 }
415
416 /* Delete group gid from groups in cred. Maintain order. */
417 int
418 kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
419 {
420 KASSERT(cred != NULL);
421 KASSERT(gid >= 0 && gid <= GID_MAX);
422
423 simple_lock(&cred->cr_lock);
424
425 if (!kauth_cred_groupmember(cred, gid)) {
426 simple_unlock(&cred->cr_lock);
427 return (0);
428 }
429
430 if (cred->cr_ngroups == 1)
431 cred->cr_groups[0] = (gid_t)-1; /* XXX */
432 else {
433 int i;
434
435 for (i = 0; i < cred->cr_ngroups; i++)
436 if (cred->cr_groups[i] == gid) {
437 cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
438 cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
439 }
440 }
441
442 cred->cr_ngroups--;
443
444 kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
445
446 simple_unlock(&cred->cr_lock);
447
448 return (0);
449 }
450
451 /*
452 * Match uids in two credentials. Checks if cred1 can access stuff owned by
453 * cred2.
454 * XXX: root bypasses this!
455 */
456 int
457 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
458 {
459 KASSERT(cred1 != NULL);
460 KASSERT(cred2 != NULL);
461
462 /* Are we root? */
463 if (cred1->cr_euid == 0)
464 return (1);
465
466 if (cred1->cr_uid == cred2->cr_uid ||
467 cred1->cr_euid == cred2->cr_uid ||
468 cred1->cr_uid == cred2->cr_euid ||
469 cred1->cr_euid == cred2->cr_euid)
470 return (1);
471
472 return (0);
473 }
474
475 uint32_t
476 kauth_cred_getrefcnt(kauth_cred_t cred)
477 {
478 KASSERT(cred != NULL);
479
480 return (cred->cr_refcnt);
481 }
482
483 void
484 kauth_cred_setrefcnt(kauth_cred_t cred, uint32_t refcnt)
485 {
486 KASSERT(cred != NULL);
487
488 cred->cr_refcnt = refcnt;
489 }
490
491 /* XXX: memcmp() wrapper needed for NFSW_SAMECRED() in nfs/nfs.h */
492 int
493 kauth_cred_memcmp(kauth_cred_t cred1, kauth_cred_t cred2)
494 {
495 KASSERT(cred1 != NULL);
496 KASSERT(cred2 != NULL);
497
498 return (memcmp(cred1, cred2, sizeof(*cred1)));
499 }
500
501 /*
502 * Convert userland credentials (struct uucred) to kauth_cred_t.
503 * XXX: For NFS code.
504 */
505 void
506 kauth_cred_convert(kauth_cred_t cred, const struct uucred *uuc)
507 {
508 int i;
509
510 KASSERT(cred != NULL);
511 KASSERT(uuc != NULL);
512
513 cred->cr_refcnt = 0;
514 cred->cr_euid = uuc->cr_uid;
515 cred->cr_egid = uuc->cr_gid;
516 cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
517 for (i = 0; i < cred->cr_ngroups; i++)
518 kauth_cred_addgroup(cred, uuc->cr_groups[i]);
519 }
520
521 /*
522 * Compare kauth_cred_t and uucred credentials.
523 * XXX: Modelled after crcmp() for NFS.
524 */
525 int
526 kauth_cred_compare(kauth_cred_t cred, const struct uucred *uuc)
527 {
528 KASSERT(cred != NULL);
529 KASSERT(uuc != NULL);
530
531 if (cred->cr_euid == uuc->cr_uid &&
532 cred->cr_egid == uuc->cr_gid &&
533 cred->cr_ngroups == uuc->cr_ngroups &&
534 !memcmp(cred->cr_groups, uuc->cr_groups,
535 cred->cr_ngroups * sizeof(cred->cr_groups[0])))
536 return (1);
537
538 return (0);
539 }
540
541 /*
542 * Make a struct ucred out of a kauth_cred_t.
543 * XXX: For sysctl.
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_uid = cred->cr_euid;
552 uc->cr_gid = cred->cr_egid;
553 uc->cr_ngroups = min(cred->cr_ngroups,
554 sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
555 memcpy(uc->cr_groups, cred->cr_groups,
556 uc->cr_ngroups * sizeof(uc->cr_groups[0]));
557 }
558
559 /*
560 * Make a struct pcred out of a kauth_cred_t.
561 * XXX: For sysctl.
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 = (struct ucred *)cred; /* XXX this is just wrong */
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 process.
579 */
580 kauth_cred_t
581 kauth_cred_get(void)
582 {
583 return (curproc->p_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 kauth_scope_t scope;
593
594 /* XXX: assert lock on scope list? */
595
596 scope = NULL;
597 SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
598 if (strcmp(scope->id, id) == 0)
599 break;
600 }
601
602 return (scope);
603 }
604
605 /*
606 * Register a new scope.
607 *
608 * id - identifier for the scope
609 * callback - the scope's default listener
610 * cookie - cookie to be passed to the listener(s)
611 */
612 kauth_scope_t
613 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
614 void *cookie)
615 {
616 kauth_scope_t scope;
617 kauth_listener_t listener;
618
619 /* Sanitize input */
620 if (id == NULL || callback == NULL)
621 return (NULL);
622
623 /* Allocate space for a new scope and listener. */
624 scope = pool_get(&kauth_scope_pool, PR_WAITOK);
625 listener = pool_get(&kauth_listener_pool, PR_WAITOK);
626
627 /* Acquire scope list lock. */
628 simple_lock(&scopes_lock);
629
630 /* Check we don't already have a scope with the same id */
631 if (kauth_ifindscope(id) != NULL) {
632 simple_unlock(&scopes_lock);
633
634 pool_put(&kauth_scope_pool, scope);
635 pool_put(&kauth_listener_pool, listener);
636
637 return (NULL);
638 }
639
640 /* Initialize new scope with parameters */
641 scope->id = id;
642 scope->cookie = cookie;
643 scope->nlisteners = 1;
644
645 /* Add default listener */
646 listener->func = callback;
647 listener->scope = scope;
648 listener->refcnt = 0;
649 SIMPLEQ_INIT(&scope->listenq);
650 SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
651
652 /* Insert scope to scopes list */
653 if (SIMPLEQ_EMPTY(&scope_list))
654 SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
655 else
656 SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
657
658 simple_unlock(&scopes_lock);
659
660 return (scope);
661 }
662
663 /*
664 * Initialize the kernel authorization subsystem.
665 *
666 * Initialize the scopes list lock.
667 * Register built-in scopes: generic, process.
668 */
669 void
670 kauth_init(void)
671 {
672 simple_lock_init(&scopes_lock);
673
674 /* Register generic scope. */
675 kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
676 kauth_authorize_cb_generic, NULL);
677
678 /* Register process scope. */
679 kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
680 kauth_authorize_cb_process, NULL);
681 }
682
683 /*
684 * Deregister a scope.
685 * Requires scope list lock to be held by the caller.
686 *
687 * scope - the scope to deregister
688 */
689 void
690 kauth_deregister_scope(kauth_scope_t scope)
691 {
692 if (scope != NULL) {
693 /* Remove scope from list */
694 SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
695 }
696 }
697
698 /*
699 * Register a listener.
700 *
701 * id - scope identifier.
702 * callback - the callback routine for the listener.
703 * cookie - cookie to pass unmoidfied to the callback.
704 */
705 kauth_listener_t
706 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
707 void *cookie)
708 {
709 kauth_scope_t scope;
710 kauth_listener_t listener;
711
712 /* Find scope struct */
713 simple_lock(&scopes_lock);
714 scope = kauth_ifindscope(id);
715 simple_unlock(&scopes_lock);
716 if (scope == NULL)
717 return (NULL);
718
719 /* Allocate listener */
720 listener = pool_get(&kauth_listener_pool, PR_WAITOK);
721
722 /* Initialize listener with parameters */
723 listener->func = callback;
724 listener->refcnt = 0;
725
726 /* Add listener to scope */
727 SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
728
729 /* Raise number of listeners on scope. */
730 scope->nlisteners++;
731 listener->scope = scope;
732
733 return (listener);
734 }
735
736 /*
737 * Deregister a listener.
738 *
739 * listener - listener reference as returned from kauth_listen_scope().
740 */
741 void
742 kauth_unlisten_scope(kauth_listener_t listener)
743 {
744 if (listener != NULL) {
745 SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
746 listener_next);
747 listener->scope->nlisteners--;
748 }
749 }
750
751 /*
752 * Authorize a request.
753 *
754 * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
755 * returned from kauth_register_scope().
756 * credential - credentials of the user ("actor") making the request.
757 * action - request identifier.
758 * arg[0-3] - passed unmodified to listener(s).
759 */
760 int
761 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
762 kauth_action_t action, void *arg0, void *arg1,
763 void *arg2, void *arg3)
764 {
765 kauth_listener_t listener;
766 int error, allow, fail;
767
768 /* Sanitize input */
769 if (scope == NULL || cred == NULL)
770 return (EFAULT);
771 if (!action)
772 return (EINVAL);
773
774 /*
775 * Each scope is associated with at least one listener. We need to
776 * traverse that list of listeners, as long as they return either
777 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
778 */
779 fail = 0;
780 allow = 0;
781 SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
782 error = listener->func(cred, action, scope->cookie, arg0,
783 arg1, arg2, arg3);
784
785 if (error == KAUTH_RESULT_ALLOW)
786 allow = 1;
787 else if (error == KAUTH_RESULT_DENY)
788 fail = 1;
789 }
790
791 return ((allow && !fail) ? 0 : EPERM);
792 };
793
794 /*
795 * Generic scope default callback.
796 */
797 int
798 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
799 void *cookie, void *arg0, void *arg1, void *arg2,
800 void *arg3)
801 {
802 int error;
803
804 error = KAUTH_RESULT_DEFER;
805
806 switch (action) {
807 case KAUTH_GENERIC_ISSUSER:
808 /* Check if credential belongs to superuser. */
809 if (cred->cr_euid == 0) {
810 u_short *acflag = (u_short *)arg0;
811
812 if (acflag != NULL)
813 *acflag |= ASU;
814
815 error = KAUTH_RESULT_ALLOW;
816 } else
817 error = KAUTH_RESULT_DENY;
818 break;
819 }
820
821 return (error);
822 }
823
824 /*
825 * Generic scope authorization wrapper.
826 */
827 int
828 generic_authorize(kauth_cred_t cred, kauth_action_t action, void *arg0)
829 {
830 return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
831 action, arg0, NULL, NULL, NULL));
832 }
833
834 /*
835 * Process scope default callback.
836 */
837 int
838 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
839 void *cookie, void *arg0, void *arg1, void *arg2,
840 void *arg3)
841 {
842 struct proc *p;
843 kauth_cred_t cred2;
844 int error;
845
846 error = KAUTH_RESULT_DEFER;
847
848 p = arg0;
849 cred2 = arg1;
850
851 switch (action) {
852 case KAUTH_PROCESS_CANSIGNAL: {
853 struct proc *to;
854 int signum;
855
856 to = arg2;
857 signum = (int)(unsigned long)arg3;
858
859 if (kauth_cred_uidmatch(cred, cred2) ||
860 (signum == SIGCONT && (p->p_session == to->p_session)))
861 error = KAUTH_RESULT_ALLOW;
862 else
863 error = KAUTH_RESULT_DEFER;
864
865 break;
866 }
867
868 case KAUTH_PROCESS_CANPTRACE:
869 if (kauth_cred_uidmatch(cred, cred2))
870 error = KAUTH_RESULT_ALLOW;
871 else
872 error = KAUTH_RESULT_DENY;
873 break;
874
875 case KAUTH_PROCESS_CANSEE:
876 if (kauth_cred_uidmatch(cred, cred2))
877 error = KAUTH_RESULT_ALLOW;
878 else
879 error = KAUTH_RESULT_DENY;
880 /* arg2 - type of information [XXX NOTIMPL] */
881 break;
882 }
883
884 return (error);
885 }
886
887 /*
888 * Process scope authorization wrapper.
889 */
890 int
891 process_authorize(kauth_cred_t cred, kauth_action_t action, struct proc *p,
892 void *arg1, void *arg2, void *arg3)
893 {
894 return (kauth_authorize_action(kauth_builtin_scope_process, cred,
895 action, p, arg1, arg2, arg3));
896 }
897