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