kern_auth.c revision 1.28 1 1.28 elad /* $NetBSD: kern_auth.c,v 1.28 2006/10/22 10:37:19 elad Exp $ */
2 1.2 elad
3 1.2 elad /*-
4 1.2 elad * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
5 1.2 elad * All rights reserved.
6 1.2 elad *
7 1.2 elad * Redistribution and use in source and binary forms, with or without
8 1.2 elad * modification, are permitted provided that the following conditions
9 1.2 elad * are met:
10 1.2 elad * 1. Redistributions of source code must retain the above copyright
11 1.2 elad * notice, this list of conditions and the following disclaimer.
12 1.2 elad * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 elad * notice, this list of conditions and the following disclaimer in the
14 1.2 elad * documentation and/or other materials provided with the distribution.
15 1.2 elad * 3. All advertising materials mentioning features or use of this software
16 1.2 elad * must display the following acknowledgement:
17 1.2 elad * This product includes software developed by Elad Efrat.
18 1.2 elad * 4. The name of the author may not be used to endorse or promote products
19 1.2 elad * derived from this software without specific prior written permission.
20 1.2 elad *
21 1.2 elad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 elad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 elad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 elad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 elad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 elad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 elad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 elad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 elad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.2 elad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.2 elad */
32 1.2 elad
33 1.20 elad #include <sys/cdefs.h>
34 1.28 elad __KERNEL_RCSID(0, "$NetBSD: kern_auth.c,v 1.28 2006/10/22 10:37:19 elad Exp $");
35 1.20 elad
36 1.2 elad #include <sys/types.h>
37 1.2 elad #include <sys/param.h>
38 1.2 elad #include <sys/queue.h>
39 1.2 elad #include <sys/time.h>
40 1.2 elad #include <sys/proc.h>
41 1.2 elad #include <sys/ucred.h>
42 1.2 elad #include <sys/pool.h>
43 1.2 elad #include <sys/kauth.h>
44 1.2 elad #include <sys/acct.h>
45 1.2 elad #include <sys/sysctl.h>
46 1.2 elad
47 1.2 elad /*
48 1.2 elad * Credentials.
49 1.2 elad */
50 1.2 elad struct kauth_cred {
51 1.11 ad struct simplelock cr_lock; /* lock on cr_refcnt */
52 1.11 ad u_int cr_refcnt; /* reference count */
53 1.2 elad uid_t cr_uid; /* user id */
54 1.2 elad uid_t cr_euid; /* effective user id */
55 1.2 elad uid_t cr_svuid; /* saved effective user id */
56 1.2 elad gid_t cr_gid; /* group id */
57 1.2 elad gid_t cr_egid; /* effective group id */
58 1.2 elad gid_t cr_svgid; /* saved effective group id */
59 1.11 ad u_int cr_ngroups; /* number of groups */
60 1.2 elad gid_t cr_groups[NGROUPS]; /* group memberships */
61 1.2 elad };
62 1.2 elad
63 1.2 elad /*
64 1.2 elad * Listener.
65 1.2 elad */
66 1.2 elad struct kauth_listener {
67 1.2 elad kauth_scope_callback_t func; /* callback */
68 1.2 elad kauth_scope_t scope; /* scope backpointer */
69 1.11 ad u_int refcnt; /* reference count */
70 1.2 elad SIMPLEQ_ENTRY(kauth_listener) listener_next; /* listener list */
71 1.2 elad };
72 1.2 elad
73 1.2 elad /*
74 1.2 elad * Scope.
75 1.2 elad */
76 1.2 elad struct kauth_scope {
77 1.2 elad const char *id; /* scope name */
78 1.2 elad void *cookie; /* user cookie */
79 1.11 ad u_int nlisteners; /* # of listeners */
80 1.2 elad SIMPLEQ_HEAD(, kauth_listener) listenq; /* listener list */
81 1.2 elad SIMPLEQ_ENTRY(kauth_scope) next_scope; /* scope list */
82 1.2 elad };
83 1.2 elad
84 1.6 yamt static POOL_INIT(kauth_scope_pool, sizeof(struct kauth_scope), 0, 0, 0,
85 1.2 elad "kauth_scopepl", &pool_allocator_nointr);
86 1.6 yamt static POOL_INIT(kauth_listener_pool, sizeof(struct kauth_listener), 0, 0, 0,
87 1.2 elad "kauth_listenerpl", &pool_allocator_nointr);
88 1.6 yamt static POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
89 1.2 elad "kauth_credpl", &pool_allocator_nointr);
90 1.2 elad
91 1.2 elad /* List of scopes and its lock. */
92 1.6 yamt static SIMPLEQ_HEAD(, kauth_scope) scope_list;
93 1.6 yamt static struct simplelock scopes_lock;
94 1.2 elad
95 1.2 elad /* Built-in scopes: generic, process. */
96 1.2 elad static kauth_scope_t kauth_builtin_scope_generic;
97 1.19 elad static kauth_scope_t kauth_builtin_scope_system;
98 1.2 elad static kauth_scope_t kauth_builtin_scope_process;
99 1.19 elad static kauth_scope_t kauth_builtin_scope_network;
100 1.19 elad static kauth_scope_t kauth_builtin_scope_machdep;
101 1.25 elad static kauth_scope_t kauth_builtin_scope_device;
102 1.2 elad
103 1.22 elad static boolean_t listeners_have_been_loaded = FALSE;
104 1.22 elad
105 1.2 elad /* Allocate new, empty kauth credentials. */
106 1.2 elad kauth_cred_t
107 1.3 yamt kauth_cred_alloc(void)
108 1.3 yamt {
109 1.2 elad kauth_cred_t cred;
110 1.2 elad
111 1.2 elad cred = pool_get(&kauth_cred_pool, PR_WAITOK);
112 1.2 elad memset(cred, 0, sizeof(*cred));
113 1.2 elad simple_lock_init(&cred->cr_lock);
114 1.2 elad cred->cr_refcnt = 1;
115 1.2 elad
116 1.2 elad return (cred);
117 1.2 elad }
118 1.2 elad
119 1.2 elad /* Increment reference count to cred. */
120 1.2 elad void
121 1.2 elad kauth_cred_hold(kauth_cred_t cred)
122 1.2 elad {
123 1.2 elad KASSERT(cred != NULL);
124 1.11 ad KASSERT(cred->cr_refcnt > 0);
125 1.2 elad
126 1.2 elad simple_lock(&cred->cr_lock);
127 1.2 elad cred->cr_refcnt++;
128 1.2 elad simple_unlock(&cred->cr_lock);
129 1.2 elad }
130 1.2 elad
131 1.2 elad /* Decrease reference count to cred. If reached zero, free it. */
132 1.2 elad void
133 1.3 yamt kauth_cred_free(kauth_cred_t cred)
134 1.3 yamt {
135 1.11 ad u_int refcnt;
136 1.11 ad
137 1.2 elad KASSERT(cred != NULL);
138 1.11 ad KASSERT(cred->cr_refcnt > 0);
139 1.2 elad
140 1.2 elad simple_lock(&cred->cr_lock);
141 1.11 ad refcnt = --cred->cr_refcnt;
142 1.2 elad simple_unlock(&cred->cr_lock);
143 1.2 elad
144 1.11 ad if (refcnt == 0)
145 1.5 yamt pool_put(&kauth_cred_pool, cred);
146 1.2 elad }
147 1.2 elad
148 1.2 elad void
149 1.2 elad kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
150 1.2 elad {
151 1.2 elad KASSERT(from != NULL);
152 1.2 elad KASSERT(to != NULL);
153 1.11 ad KASSERT(from->cr_refcnt > 0);
154 1.2 elad
155 1.2 elad to->cr_uid = from->cr_uid;
156 1.2 elad to->cr_euid = from->cr_euid;
157 1.2 elad to->cr_svuid = from->cr_svuid;
158 1.2 elad to->cr_gid = from->cr_gid;
159 1.2 elad to->cr_egid = from->cr_egid;
160 1.2 elad to->cr_svgid = from->cr_svgid;
161 1.2 elad to->cr_ngroups = from->cr_ngroups;
162 1.11 ad memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
163 1.2 elad }
164 1.2 elad
165 1.2 elad /*
166 1.2 elad * Duplicate cred and return a new kauth_cred_t.
167 1.2 elad */
168 1.2 elad kauth_cred_t
169 1.2 elad kauth_cred_dup(kauth_cred_t cred)
170 1.2 elad {
171 1.2 elad kauth_cred_t new_cred;
172 1.2 elad
173 1.2 elad KASSERT(cred != NULL);
174 1.11 ad KASSERT(cred->cr_refcnt > 0);
175 1.2 elad
176 1.2 elad new_cred = kauth_cred_alloc();
177 1.2 elad
178 1.2 elad kauth_cred_clone(cred, new_cred);
179 1.2 elad
180 1.2 elad return (new_cred);
181 1.2 elad }
182 1.2 elad
183 1.2 elad /*
184 1.2 elad * Similar to crcopy(), only on a kauth_cred_t.
185 1.2 elad * XXX: Is this even needed? [kauth_cred_copy]
186 1.2 elad */
187 1.2 elad kauth_cred_t
188 1.2 elad kauth_cred_copy(kauth_cred_t cred)
189 1.2 elad {
190 1.2 elad kauth_cred_t new_cred;
191 1.2 elad
192 1.2 elad KASSERT(cred != NULL);
193 1.11 ad KASSERT(cred->cr_refcnt > 0);
194 1.2 elad
195 1.2 elad /* If the provided credentials already have one reference, use them. */
196 1.2 elad if (cred->cr_refcnt == 1)
197 1.2 elad return (cred);
198 1.2 elad
199 1.2 elad new_cred = kauth_cred_alloc();
200 1.2 elad
201 1.2 elad kauth_cred_clone(cred, new_cred);
202 1.2 elad
203 1.2 elad kauth_cred_free(cred);
204 1.2 elad
205 1.2 elad return (new_cred);
206 1.2 elad }
207 1.2 elad
208 1.2 elad uid_t
209 1.2 elad kauth_cred_getuid(kauth_cred_t cred)
210 1.2 elad {
211 1.2 elad KASSERT(cred != NULL);
212 1.2 elad
213 1.2 elad return (cred->cr_uid);
214 1.2 elad }
215 1.2 elad
216 1.2 elad uid_t
217 1.2 elad kauth_cred_geteuid(kauth_cred_t cred)
218 1.2 elad {
219 1.2 elad KASSERT(cred != NULL);
220 1.2 elad
221 1.2 elad return (cred->cr_euid);
222 1.2 elad }
223 1.2 elad
224 1.2 elad uid_t
225 1.2 elad kauth_cred_getsvuid(kauth_cred_t cred)
226 1.2 elad {
227 1.2 elad KASSERT(cred != NULL);
228 1.2 elad
229 1.2 elad return (cred->cr_svuid);
230 1.2 elad }
231 1.2 elad
232 1.2 elad gid_t
233 1.2 elad kauth_cred_getgid(kauth_cred_t cred)
234 1.2 elad {
235 1.2 elad KASSERT(cred != NULL);
236 1.2 elad
237 1.2 elad return (cred->cr_gid);
238 1.2 elad }
239 1.2 elad
240 1.2 elad gid_t
241 1.2 elad kauth_cred_getegid(kauth_cred_t cred)
242 1.2 elad {
243 1.2 elad KASSERT(cred != NULL);
244 1.2 elad
245 1.2 elad return (cred->cr_egid);
246 1.2 elad }
247 1.2 elad
248 1.2 elad gid_t
249 1.2 elad kauth_cred_getsvgid(kauth_cred_t cred)
250 1.2 elad {
251 1.2 elad KASSERT(cred != NULL);
252 1.2 elad
253 1.2 elad return (cred->cr_svgid);
254 1.2 elad }
255 1.2 elad
256 1.2 elad void
257 1.2 elad kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
258 1.2 elad {
259 1.2 elad KASSERT(cred != NULL);
260 1.11 ad KASSERT(cred->cr_refcnt == 1);
261 1.2 elad
262 1.2 elad cred->cr_uid = uid;
263 1.2 elad }
264 1.2 elad
265 1.2 elad void
266 1.2 elad kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
267 1.2 elad {
268 1.2 elad KASSERT(cred != NULL);
269 1.11 ad KASSERT(cred->cr_refcnt == 1);
270 1.2 elad
271 1.2 elad cred->cr_euid = uid;
272 1.2 elad }
273 1.2 elad
274 1.2 elad void
275 1.2 elad kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
276 1.2 elad {
277 1.2 elad KASSERT(cred != NULL);
278 1.11 ad KASSERT(cred->cr_refcnt == 1);
279 1.2 elad
280 1.2 elad cred->cr_svuid = uid;
281 1.2 elad }
282 1.2 elad
283 1.2 elad void
284 1.2 elad kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
285 1.2 elad {
286 1.2 elad KASSERT(cred != NULL);
287 1.11 ad KASSERT(cred->cr_refcnt == 1);
288 1.2 elad
289 1.2 elad cred->cr_gid = gid;
290 1.2 elad }
291 1.2 elad
292 1.2 elad void
293 1.2 elad kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
294 1.2 elad {
295 1.2 elad KASSERT(cred != NULL);
296 1.11 ad KASSERT(cred->cr_refcnt == 1);
297 1.2 elad
298 1.2 elad cred->cr_egid = gid;
299 1.2 elad }
300 1.2 elad
301 1.2 elad void
302 1.2 elad kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
303 1.2 elad {
304 1.2 elad KASSERT(cred != NULL);
305 1.11 ad KASSERT(cred->cr_refcnt == 1);
306 1.2 elad
307 1.2 elad cred->cr_svgid = gid;
308 1.2 elad }
309 1.2 elad
310 1.2 elad /* Checks if gid is a member of the groups in cred. */
311 1.2 elad int
312 1.2 elad kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
313 1.2 elad {
314 1.2 elad int i;
315 1.2 elad
316 1.2 elad KASSERT(cred != NULL);
317 1.2 elad KASSERT(resultp != NULL);
318 1.2 elad
319 1.2 elad *resultp = 0;
320 1.2 elad
321 1.2 elad for (i = 0; i < cred->cr_ngroups; i++)
322 1.2 elad if (cred->cr_groups[i] == gid) {
323 1.2 elad *resultp = 1;
324 1.2 elad break;
325 1.2 elad }
326 1.2 elad
327 1.2 elad return (0);
328 1.2 elad }
329 1.2 elad
330 1.11 ad u_int
331 1.2 elad kauth_cred_ngroups(kauth_cred_t cred)
332 1.2 elad {
333 1.2 elad KASSERT(cred != NULL);
334 1.2 elad
335 1.2 elad return (cred->cr_ngroups);
336 1.2 elad }
337 1.2 elad
338 1.2 elad /*
339 1.2 elad * Return the group at index idx from the groups in cred.
340 1.2 elad */
341 1.2 elad gid_t
342 1.11 ad kauth_cred_group(kauth_cred_t cred, u_int idx)
343 1.2 elad {
344 1.2 elad KASSERT(cred != NULL);
345 1.2 elad KASSERT(idx < cred->cr_ngroups);
346 1.2 elad
347 1.2 elad return (cred->cr_groups[idx]);
348 1.2 elad }
349 1.2 elad
350 1.2 elad /* XXX elad: gmuid is unused for now. */
351 1.2 elad int
352 1.27 christos kauth_cred_setgroups(kauth_cred_t cred, gid_t *grbuf, size_t len,
353 1.27 christos uid_t gmuid __unused)
354 1.2 elad {
355 1.2 elad KASSERT(cred != NULL);
356 1.11 ad KASSERT(cred->cr_refcnt == 1);
357 1.9 yamt KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
358 1.2 elad
359 1.2 elad if (len)
360 1.2 elad memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
361 1.2 elad memset(cred->cr_groups + len, 0xff,
362 1.2 elad sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
363 1.2 elad
364 1.2 elad cred->cr_ngroups = len;
365 1.2 elad
366 1.2 elad return (0);
367 1.2 elad }
368 1.2 elad
369 1.2 elad int
370 1.2 elad kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
371 1.2 elad {
372 1.2 elad KASSERT(cred != NULL);
373 1.2 elad KASSERT(len <= cred->cr_ngroups);
374 1.2 elad
375 1.2 elad memset(grbuf, 0xff, sizeof(*grbuf) * len);
376 1.2 elad memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
377 1.2 elad
378 1.2 elad return (0);
379 1.2 elad }
380 1.2 elad
381 1.2 elad /*
382 1.19 elad * Match uids in two credentials.
383 1.2 elad */
384 1.19 elad int
385 1.2 elad kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
386 1.2 elad {
387 1.2 elad KASSERT(cred1 != NULL);
388 1.2 elad KASSERT(cred2 != NULL);
389 1.2 elad
390 1.2 elad if (cred1->cr_uid == cred2->cr_uid ||
391 1.2 elad cred1->cr_euid == cred2->cr_uid ||
392 1.2 elad cred1->cr_uid == cred2->cr_euid ||
393 1.2 elad cred1->cr_euid == cred2->cr_euid)
394 1.2 elad return (1);
395 1.2 elad
396 1.2 elad return (0);
397 1.2 elad }
398 1.2 elad
399 1.11 ad u_int
400 1.2 elad kauth_cred_getrefcnt(kauth_cred_t cred)
401 1.2 elad {
402 1.2 elad KASSERT(cred != NULL);
403 1.2 elad
404 1.2 elad return (cred->cr_refcnt);
405 1.2 elad }
406 1.2 elad
407 1.2 elad /*
408 1.2 elad * Convert userland credentials (struct uucred) to kauth_cred_t.
409 1.2 elad * XXX: For NFS code.
410 1.2 elad */
411 1.2 elad void
412 1.2 elad kauth_cred_uucvt(kauth_cred_t cred, const struct uucred *uuc)
413 1.2 elad {
414 1.2 elad KASSERT(cred != NULL);
415 1.2 elad KASSERT(uuc != NULL);
416 1.2 elad
417 1.2 elad cred->cr_refcnt = 1;
418 1.2 elad cred->cr_uid = uuc->cr_uid;
419 1.2 elad cred->cr_euid = uuc->cr_uid;
420 1.2 elad cred->cr_svuid = uuc->cr_uid;
421 1.2 elad cred->cr_gid = uuc->cr_gid;
422 1.2 elad cred->cr_egid = uuc->cr_gid;
423 1.2 elad cred->cr_svgid = uuc->cr_gid;
424 1.2 elad cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
425 1.2 elad kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
426 1.2 elad cred->cr_ngroups, -1);
427 1.2 elad }
428 1.2 elad
429 1.2 elad /*
430 1.2 elad * Compare kauth_cred_t and uucred credentials.
431 1.2 elad * XXX: Modelled after crcmp() for NFS.
432 1.2 elad */
433 1.2 elad int
434 1.2 elad kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
435 1.2 elad {
436 1.2 elad KASSERT(cred != NULL);
437 1.2 elad KASSERT(uuc != NULL);
438 1.2 elad
439 1.2 elad if (cred->cr_euid == uuc->cr_uid &&
440 1.2 elad cred->cr_egid == uuc->cr_gid &&
441 1.2 elad cred->cr_ngroups == uuc->cr_ngroups) {
442 1.2 elad int i;
443 1.2 elad
444 1.2 elad /* Check if all groups from uuc appear in cred. */
445 1.2 elad for (i = 0; i < uuc->cr_ngroups; i++) {
446 1.2 elad int ismember;
447 1.2 elad
448 1.2 elad ismember = 0;
449 1.2 elad if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
450 1.2 elad &ismember) != 0 || !ismember)
451 1.4 yamt return (1);
452 1.2 elad }
453 1.2 elad
454 1.4 yamt return (0);
455 1.2 elad }
456 1.2 elad
457 1.4 yamt return (1);
458 1.2 elad }
459 1.2 elad
460 1.2 elad /*
461 1.12 ad * Make a struct ucred out of a kauth_cred_t. For compatibility.
462 1.2 elad */
463 1.2 elad void
464 1.2 elad kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
465 1.2 elad {
466 1.2 elad KASSERT(cred != NULL);
467 1.2 elad KASSERT(uc != NULL);
468 1.2 elad
469 1.12 ad uc->cr_ref = cred->cr_refcnt;
470 1.2 elad uc->cr_uid = cred->cr_euid;
471 1.2 elad uc->cr_gid = cred->cr_egid;
472 1.2 elad uc->cr_ngroups = min(cred->cr_ngroups,
473 1.2 elad sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
474 1.2 elad memcpy(uc->cr_groups, cred->cr_groups,
475 1.2 elad uc->cr_ngroups * sizeof(uc->cr_groups[0]));
476 1.2 elad }
477 1.2 elad
478 1.2 elad /*
479 1.12 ad * Make a struct pcred out of a kauth_cred_t. For compatibility.
480 1.2 elad */
481 1.2 elad void
482 1.2 elad kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
483 1.2 elad {
484 1.2 elad KASSERT(cred != NULL);
485 1.2 elad KASSERT(pc != NULL);
486 1.2 elad
487 1.12 ad pc->pc_ucred = NULL;
488 1.2 elad pc->p_ruid = cred->cr_uid;
489 1.2 elad pc->p_svuid = cred->cr_svuid;
490 1.2 elad pc->p_rgid = cred->cr_gid;
491 1.2 elad pc->p_svgid = cred->cr_svgid;
492 1.2 elad pc->p_refcnt = cred->cr_refcnt;
493 1.2 elad }
494 1.2 elad
495 1.2 elad /*
496 1.14 ad * Return kauth_cred_t for the current LWP.
497 1.2 elad */
498 1.2 elad kauth_cred_t
499 1.2 elad kauth_cred_get(void)
500 1.2 elad {
501 1.14 ad return (curlwp->l_cred);
502 1.2 elad }
503 1.2 elad
504 1.2 elad /*
505 1.2 elad * Returns a scope matching the provided id.
506 1.2 elad * Requires the scope list lock to be held by the caller.
507 1.2 elad */
508 1.2 elad static kauth_scope_t
509 1.3 yamt kauth_ifindscope(const char *id)
510 1.3 yamt {
511 1.2 elad kauth_scope_t scope;
512 1.2 elad
513 1.2 elad /* XXX: assert lock on scope list? */
514 1.2 elad
515 1.2 elad scope = NULL;
516 1.2 elad SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
517 1.2 elad if (strcmp(scope->id, id) == 0)
518 1.2 elad break;
519 1.2 elad }
520 1.2 elad
521 1.2 elad return (scope);
522 1.2 elad }
523 1.2 elad
524 1.2 elad /*
525 1.2 elad * Register a new scope.
526 1.2 elad *
527 1.2 elad * id - identifier for the scope
528 1.2 elad * callback - the scope's default listener
529 1.2 elad * cookie - cookie to be passed to the listener(s)
530 1.2 elad */
531 1.2 elad kauth_scope_t
532 1.2 elad kauth_register_scope(const char *id, kauth_scope_callback_t callback,
533 1.16 christos void *cookie)
534 1.2 elad {
535 1.2 elad kauth_scope_t scope;
536 1.21 yamt kauth_listener_t listener = NULL; /* XXX gcc */
537 1.2 elad
538 1.2 elad /* Sanitize input */
539 1.16 christos if (id == NULL)
540 1.2 elad return (NULL);
541 1.2 elad
542 1.2 elad /* Allocate space for a new scope and listener. */
543 1.2 elad scope = pool_get(&kauth_scope_pool, PR_WAITOK);
544 1.21 yamt if (callback != NULL) {
545 1.21 yamt listener = pool_get(&kauth_listener_pool, PR_WAITOK);
546 1.21 yamt }
547 1.2 elad
548 1.2 elad /* Acquire scope list lock. */
549 1.2 elad simple_lock(&scopes_lock);
550 1.2 elad
551 1.2 elad /* Check we don't already have a scope with the same id */
552 1.2 elad if (kauth_ifindscope(id) != NULL) {
553 1.2 elad simple_unlock(&scopes_lock);
554 1.2 elad
555 1.2 elad pool_put(&kauth_scope_pool, scope);
556 1.21 yamt if (callback != NULL) {
557 1.21 yamt pool_put(&kauth_listener_pool, listener);
558 1.21 yamt }
559 1.2 elad
560 1.2 elad return (NULL);
561 1.2 elad }
562 1.2 elad
563 1.2 elad /* Initialize new scope with parameters */
564 1.2 elad scope->id = id;
565 1.2 elad scope->cookie = cookie;
566 1.2 elad scope->nlisteners = 1;
567 1.2 elad
568 1.16 christos SIMPLEQ_INIT(&scope->listenq);
569 1.16 christos
570 1.2 elad /* Add default listener */
571 1.16 christos if (callback != NULL) {
572 1.16 christos listener->func = callback;
573 1.16 christos listener->scope = scope;
574 1.16 christos listener->refcnt = 0;
575 1.16 christos SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
576 1.16 christos }
577 1.2 elad
578 1.2 elad /* Insert scope to scopes list */
579 1.16 christos SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
580 1.2 elad
581 1.2 elad simple_unlock(&scopes_lock);
582 1.2 elad
583 1.2 elad return (scope);
584 1.2 elad }
585 1.2 elad
586 1.2 elad /*
587 1.2 elad * Initialize the kernel authorization subsystem.
588 1.2 elad *
589 1.2 elad * Initialize the scopes list lock.
590 1.2 elad * Register built-in scopes: generic, process.
591 1.2 elad */
592 1.2 elad void
593 1.2 elad kauth_init(void)
594 1.2 elad {
595 1.16 christos SIMPLEQ_INIT(&scope_list);
596 1.2 elad simple_lock_init(&scopes_lock);
597 1.2 elad
598 1.2 elad /* Register generic scope. */
599 1.2 elad kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
600 1.19 elad NULL, NULL);
601 1.19 elad
602 1.19 elad /* Register system scope. */
603 1.19 elad kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
604 1.19 elad NULL, NULL);
605 1.2 elad
606 1.2 elad /* Register process scope. */
607 1.2 elad kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
608 1.19 elad NULL, NULL);
609 1.19 elad
610 1.19 elad /* Register network scope. */
611 1.19 elad kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
612 1.19 elad NULL, NULL);
613 1.19 elad
614 1.19 elad /* Register machdep scope. */
615 1.19 elad kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
616 1.19 elad NULL, NULL);
617 1.25 elad
618 1.25 elad /* Register device scope. */
619 1.25 elad kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
620 1.25 elad NULL, NULL);
621 1.2 elad }
622 1.2 elad
623 1.2 elad /*
624 1.2 elad * Deregister a scope.
625 1.2 elad * Requires scope list lock to be held by the caller.
626 1.2 elad *
627 1.2 elad * scope - the scope to deregister
628 1.2 elad */
629 1.2 elad void
630 1.2 elad kauth_deregister_scope(kauth_scope_t scope)
631 1.2 elad {
632 1.2 elad if (scope != NULL) {
633 1.2 elad /* Remove scope from list */
634 1.2 elad SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
635 1.2 elad }
636 1.2 elad }
637 1.2 elad
638 1.2 elad /*
639 1.2 elad * Register a listener.
640 1.2 elad *
641 1.2 elad * id - scope identifier.
642 1.2 elad * callback - the callback routine for the listener.
643 1.2 elad * cookie - cookie to pass unmoidfied to the callback.
644 1.2 elad */
645 1.2 elad kauth_listener_t
646 1.2 elad kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
647 1.27 christos void *cookie __unused)
648 1.2 elad {
649 1.2 elad kauth_scope_t scope;
650 1.2 elad kauth_listener_t listener;
651 1.2 elad
652 1.2 elad /* Find scope struct */
653 1.2 elad simple_lock(&scopes_lock);
654 1.2 elad scope = kauth_ifindscope(id);
655 1.2 elad simple_unlock(&scopes_lock);
656 1.2 elad if (scope == NULL)
657 1.2 elad return (NULL);
658 1.2 elad
659 1.2 elad /* Allocate listener */
660 1.2 elad listener = pool_get(&kauth_listener_pool, PR_WAITOK);
661 1.2 elad
662 1.2 elad /* Initialize listener with parameters */
663 1.2 elad listener->func = callback;
664 1.2 elad listener->refcnt = 0;
665 1.2 elad
666 1.2 elad /* Add listener to scope */
667 1.2 elad SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
668 1.2 elad
669 1.2 elad /* Raise number of listeners on scope. */
670 1.2 elad scope->nlisteners++;
671 1.2 elad listener->scope = scope;
672 1.2 elad
673 1.22 elad listeners_have_been_loaded = TRUE;
674 1.22 elad
675 1.2 elad return (listener);
676 1.2 elad }
677 1.2 elad
678 1.2 elad /*
679 1.2 elad * Deregister a listener.
680 1.2 elad *
681 1.2 elad * listener - listener reference as returned from kauth_listen_scope().
682 1.2 elad */
683 1.2 elad void
684 1.2 elad kauth_unlisten_scope(kauth_listener_t listener)
685 1.2 elad {
686 1.2 elad if (listener != NULL) {
687 1.3 yamt SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
688 1.3 yamt kauth_listener, listener_next);
689 1.2 elad listener->scope->nlisteners--;
690 1.2 elad }
691 1.2 elad }
692 1.2 elad
693 1.2 elad /*
694 1.2 elad * Authorize a request.
695 1.2 elad *
696 1.2 elad * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
697 1.2 elad * returned from kauth_register_scope().
698 1.2 elad * credential - credentials of the user ("actor") making the request.
699 1.2 elad * action - request identifier.
700 1.2 elad * arg[0-3] - passed unmodified to listener(s).
701 1.2 elad */
702 1.2 elad int
703 1.2 elad kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
704 1.2 elad kauth_action_t action, void *arg0, void *arg1,
705 1.2 elad void *arg2, void *arg3)
706 1.2 elad {
707 1.2 elad kauth_listener_t listener;
708 1.2 elad int error, allow, fail;
709 1.2 elad
710 1.15 elad #if 0 /* defined(LOCKDEBUG) */
711 1.13 elad spinlock_switchcheck();
712 1.13 elad simple_lock_only_held(NULL, "kauth_authorize_action");
713 1.13 elad #endif
714 1.13 elad
715 1.26 elad KASSERT(cred != NULL);
716 1.26 elad KASSERT(action != 0);
717 1.2 elad
718 1.17 christos /* Short-circuit requests coming from the kernel. */
719 1.17 christos if (cred == NOCRED || cred == FSCRED)
720 1.17 christos return (0);
721 1.17 christos
722 1.26 elad KASSERT(scope != NULL);
723 1.26 elad
724 1.22 elad if (!listeners_have_been_loaded) {
725 1.22 elad KASSERT(SIMPLEQ_EMPTY(&scope->listenq));
726 1.22 elad
727 1.18 elad return (0);
728 1.22 elad }
729 1.18 elad
730 1.2 elad fail = 0;
731 1.2 elad allow = 0;
732 1.2 elad SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
733 1.2 elad error = listener->func(cred, action, scope->cookie, arg0,
734 1.2 elad arg1, arg2, arg3);
735 1.2 elad
736 1.2 elad if (error == KAUTH_RESULT_ALLOW)
737 1.2 elad allow = 1;
738 1.2 elad else if (error == KAUTH_RESULT_DENY)
739 1.2 elad fail = 1;
740 1.2 elad }
741 1.2 elad
742 1.2 elad return ((allow && !fail) ? 0 : EPERM);
743 1.2 elad };
744 1.2 elad
745 1.2 elad /*
746 1.2 elad * Generic scope authorization wrapper.
747 1.2 elad */
748 1.2 elad int
749 1.2 elad kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
750 1.2 elad {
751 1.2 elad return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
752 1.2 elad action, arg0, NULL, NULL, NULL));
753 1.2 elad }
754 1.2 elad
755 1.2 elad /*
756 1.19 elad * System scope authorization wrapper.
757 1.2 elad */
758 1.2 elad int
759 1.19 elad kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
760 1.19 elad enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
761 1.2 elad {
762 1.19 elad return (kauth_authorize_action(kauth_builtin_scope_system, cred,
763 1.19 elad action, (void *)req, arg1, arg2, arg3));
764 1.2 elad }
765 1.2 elad
766 1.2 elad /*
767 1.2 elad * Process scope authorization wrapper.
768 1.2 elad */
769 1.2 elad int
770 1.2 elad kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
771 1.2 elad struct proc *p, void *arg1, void *arg2, void *arg3)
772 1.2 elad {
773 1.2 elad return (kauth_authorize_action(kauth_builtin_scope_process, cred,
774 1.2 elad action, p, arg1, arg2, arg3));
775 1.2 elad }
776 1.19 elad
777 1.19 elad /*
778 1.19 elad * Network scope authorization wrapper.
779 1.19 elad */
780 1.19 elad int
781 1.19 elad kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
782 1.23 elad enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
783 1.19 elad {
784 1.19 elad return (kauth_authorize_action(kauth_builtin_scope_network, cred,
785 1.23 elad action, (void *)req, arg1, arg2, arg3));
786 1.19 elad }
787 1.19 elad
788 1.19 elad int
789 1.19 elad kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
790 1.24 elad enum kauth_machdep_req req, void *arg1, void *arg2, void *arg3)
791 1.19 elad {
792 1.19 elad return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
793 1.24 elad action, (void *)req, arg1, arg2, arg3));
794 1.19 elad }
795 1.25 elad
796 1.25 elad int
797 1.25 elad kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
798 1.25 elad struct tty *tty)
799 1.25 elad {
800 1.25 elad return (kauth_authorize_action(kauth_builtin_scope_device, cred,
801 1.25 elad action, tty, NULL, NULL, NULL));
802 1.25 elad }
803