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