2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2005 Samy Al Bahra
8 * This software was developed by Robert Watson and Ilmar Habibulin for the
11 * This software was developed for the FreeBSD Project in part by Network
12 * Associates Laboratories, the Security Research Division of Network
13 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
14 * as part of the DARPA CHATS research program.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/condvar.h>
45 #include <sys/imgact.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
57 #include <sys/namei.h>
58 #include <sys/sysctl.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_object.h>
65 #include <security/mac/mac_framework.h>
66 #include <security/mac/mac_internal.h>
67 #include <security/mac/mac_policy.h>
69 static int mac_mmap_revocation = 1;
70 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
71 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
74 static int mac_mmap_revocation_via_cow = 0;
75 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
76 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
77 "copy-on-write semantics, or by removing all write access");
79 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
80 struct ucred *cred, struct vm_map *map);
83 mac_cred_label_alloc(void)
87 label = mac_labelzone_alloc(M_WAITOK);
88 MAC_PERFORM(init_cred_label, label);
93 mac_init_cred(struct ucred *cred)
96 cred->cr_label = mac_cred_label_alloc();
100 mac_proc_label_alloc(void)
104 label = mac_labelzone_alloc(M_WAITOK);
105 MAC_PERFORM(init_proc_label, label);
110 mac_init_proc(struct proc *p)
113 p->p_label = mac_proc_label_alloc();
117 mac_cred_label_free(struct label *label)
120 MAC_PERFORM(destroy_cred_label, label);
121 mac_labelzone_free(label);
125 mac_destroy_cred(struct ucred *cred)
128 mac_cred_label_free(cred->cr_label);
129 cred->cr_label = NULL;
133 mac_proc_label_free(struct label *label)
136 MAC_PERFORM(destroy_proc_label, label);
137 mac_labelzone_free(label);
141 mac_destroy_proc(struct proc *p)
144 mac_proc_label_free(p->p_label);
149 mac_externalize_cred_label(struct label *label, char *elements,
150 char *outbuf, size_t outbuflen)
154 MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
160 mac_internalize_cred_label(struct label *label, char *string)
164 MAC_INTERNALIZE(cred, label, string);
170 * Initialize MAC label for the first kernel process, from which other kernel
171 * processes and threads are spawned.
174 mac_create_proc0(struct ucred *cred)
177 MAC_PERFORM(create_proc0, cred);
181 * Initialize MAC label for the first userland process, from which other
182 * userland processes and threads are spawned.
185 mac_create_proc1(struct ucred *cred)
188 MAC_PERFORM(create_proc1, cred);
192 mac_thread_userret(struct thread *td)
195 MAC_PERFORM(thread_userret, td);
199 * When a new process is created, its label must be initialized. Generally,
200 * this involves inheritence from the parent process, modulo possible deltas.
201 * This function allows that processing to take place.
204 mac_copy_cred(struct ucred *src, struct ucred *dest)
207 MAC_PERFORM(copy_cred_label, src->cr_label, dest->cr_label);
211 mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
221 error = copyin(mac_p, &mac, sizeof(mac));
225 error = mac_check_structmac_consistent(&mac);
229 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
230 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
232 free(buffer, M_MACTEMP);
236 label = mac_cred_label_alloc();
237 error = mac_internalize_cred_label(label, buffer);
238 free(buffer, M_MACTEMP);
240 mac_cred_label_free(label);
243 imgp->execlabel = label;
248 mac_execve_exit(struct image_params *imgp)
250 if (imgp->execlabel != NULL) {
251 mac_cred_label_free(imgp->execlabel);
252 imgp->execlabel = NULL;
257 * When relabeling a process, call out to the policies for the maximum
258 * permission allowed for each object type we know about in its memory space,
259 * and revoke access (in the least surprising ways we know) when necessary.
260 * The process lock is not held here.
263 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
266 /* XXX freeze all other threads */
267 mac_cred_mmapped_drop_perms_recurse(td, cred,
268 &td->td_proc->p_vmspace->vm_map);
269 /* XXX allow other threads to continue */
272 static __inline const char *
273 prot2str(vm_prot_t prot)
276 switch (prot & VM_PROT_ALL) {
279 case VM_PROT_READ | VM_PROT_WRITE:
281 case VM_PROT_READ | VM_PROT_EXECUTE:
283 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
287 case VM_PROT_EXECUTE:
289 case VM_PROT_WRITE | VM_PROT_EXECUTE:
297 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
300 struct vm_map_entry *vme;
301 int vfslocked, result;
302 vm_prot_t revokeperms;
303 vm_object_t backing_object, object;
308 if (!mac_mmap_revocation)
311 vm_map_lock_read(map);
312 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
313 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
314 mac_cred_mmapped_drop_perms_recurse(td, cred,
315 vme->object.sub_map);
319 * Skip over entries that obviously are not shared.
321 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
322 !vme->max_protection)
325 * Drill down to the deepest backing object.
327 offset = vme->offset;
328 object = vme->object.vm_object;
331 VM_OBJECT_LOCK(object);
332 while ((backing_object = object->backing_object) != NULL) {
333 VM_OBJECT_LOCK(backing_object);
334 offset += object->backing_object_offset;
335 VM_OBJECT_UNLOCK(object);
336 object = backing_object;
338 VM_OBJECT_UNLOCK(object);
340 * At the moment, vm_maps and objects aren't considered by
341 * the MAC system, so only things with backing by a normal
342 * object (read: vnodes) are checked.
344 if (object->type != OBJT_VNODE)
346 vp = (struct vnode *)object->handle;
347 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
348 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
349 result = vme->max_protection;
350 mac_check_vnode_mmap_downgrade(cred, vp, &result);
351 VOP_UNLOCK(vp, 0, td);
353 * Find out what maximum protection we may be allowing now
354 * but a policy needs to get removed.
356 revokeperms = vme->max_protection & ~result;
358 VFS_UNLOCK_GIANT(vfslocked);
361 printf("pid %ld: revoking %s perms from %#lx:%ld "
362 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
363 prot2str(revokeperms), (u_long)vme->start,
364 (long)(vme->end - vme->start),
365 prot2str(vme->max_protection), prot2str(vme->protection));
366 vm_map_lock_upgrade(map);
368 * This is the really simple case: if a map has more
369 * max_protection than is allowed, but it's not being
370 * actually used (that is, the current protection is still
371 * allowed), we can just wipe it out and do nothing more.
373 if ((vme->protection & revokeperms) == 0) {
374 vme->max_protection -= revokeperms;
376 if (revokeperms & VM_PROT_WRITE) {
378 * In the more complicated case, flush out all
379 * pending changes to the object then turn it
382 vm_object_reference(object);
383 (void) vn_start_write(vp, &mp, V_WAIT);
384 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
385 VM_OBJECT_LOCK(object);
386 vm_object_page_clean(object,
388 OFF_TO_IDX(offset + vme->end - vme->start +
391 VM_OBJECT_UNLOCK(object);
392 VOP_UNLOCK(vp, 0, td);
393 vn_finished_write(mp);
394 vm_object_deallocate(object);
396 * Why bother if there's no read permissions
397 * anymore? For the rest, we need to leave
398 * the write permissions on for COW, or
399 * remove them entirely if configured to.
401 if (!mac_mmap_revocation_via_cow) {
402 vme->max_protection &= ~VM_PROT_WRITE;
403 vme->protection &= ~VM_PROT_WRITE;
404 } if ((revokeperms & VM_PROT_READ) == 0)
405 vme->eflags |= MAP_ENTRY_COW |
406 MAP_ENTRY_NEEDS_COPY;
408 if (revokeperms & VM_PROT_EXECUTE) {
409 vme->max_protection &= ~VM_PROT_EXECUTE;
410 vme->protection &= ~VM_PROT_EXECUTE;
412 if (revokeperms & VM_PROT_READ) {
413 vme->max_protection = 0;
416 pmap_protect(map->pmap, vme->start, vme->end,
417 vme->protection & ~revokeperms);
418 vm_map_simplify_entry(map, vme);
420 vm_map_lock_downgrade(map);
421 VFS_UNLOCK_GIANT(vfslocked);
423 vm_map_unlock_read(map);
427 * When the subject's label changes, it may require revocation of privilege
428 * to mapped objects. This can't be done on-the-fly later with a unified
432 mac_relabel_cred(struct ucred *cred, struct label *newlabel)
435 MAC_PERFORM(relabel_cred, cred, newlabel);
439 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
443 MAC_CHECK(check_cred_relabel, cred, newlabel);
449 mac_check_cred_visible(struct ucred *cr1, struct ucred *cr2)
453 MAC_CHECK(check_cred_visible, cr1, cr2);
459 mac_check_proc_debug(struct ucred *cred, struct proc *p)
463 PROC_LOCK_ASSERT(p, MA_OWNED);
465 MAC_CHECK(check_proc_debug, cred, p);
471 mac_check_proc_sched(struct ucred *cred, struct proc *p)
475 PROC_LOCK_ASSERT(p, MA_OWNED);
477 MAC_CHECK(check_proc_sched, cred, p);
483 mac_check_proc_signal(struct ucred *cred, struct proc *p, int signum)
487 PROC_LOCK_ASSERT(p, MA_OWNED);
489 MAC_CHECK(check_proc_signal, cred, p, signum);
495 mac_check_proc_setuid(struct proc *p, struct ucred *cred, uid_t uid)
499 PROC_LOCK_ASSERT(p, MA_OWNED);
501 MAC_CHECK(check_proc_setuid, cred, uid);
506 mac_check_proc_seteuid(struct proc *p, struct ucred *cred, uid_t euid)
510 PROC_LOCK_ASSERT(p, MA_OWNED);
512 MAC_CHECK(check_proc_seteuid, cred, euid);
517 mac_check_proc_setgid(struct proc *p, struct ucred *cred, gid_t gid)
521 PROC_LOCK_ASSERT(p, MA_OWNED);
523 MAC_CHECK(check_proc_setgid, cred, gid);
529 mac_check_proc_setegid(struct proc *p, struct ucred *cred, gid_t egid)
533 PROC_LOCK_ASSERT(p, MA_OWNED);
535 MAC_CHECK(check_proc_setegid, cred, egid);
541 mac_check_proc_setgroups(struct proc *p, struct ucred *cred, int ngroups,
546 PROC_LOCK_ASSERT(p, MA_OWNED);
548 MAC_CHECK(check_proc_setgroups, cred, ngroups, gidset);
553 mac_check_proc_setreuid(struct proc *p, struct ucred *cred, uid_t ruid,
558 PROC_LOCK_ASSERT(p, MA_OWNED);
560 MAC_CHECK(check_proc_setreuid, cred, ruid, euid);
566 mac_check_proc_setregid(struct proc *proc, struct ucred *cred, gid_t rgid,
571 PROC_LOCK_ASSERT(proc, MA_OWNED);
573 MAC_CHECK(check_proc_setregid, cred, rgid, egid);
579 mac_check_proc_setresuid(struct proc *p, struct ucred *cred, uid_t ruid,
580 uid_t euid, uid_t suid)
584 PROC_LOCK_ASSERT(p, MA_OWNED);
586 MAC_CHECK(check_proc_setresuid, cred, ruid, euid, suid);
591 mac_check_proc_setresgid(struct proc *p, struct ucred *cred, gid_t rgid,
592 gid_t egid, gid_t sgid)
596 PROC_LOCK_ASSERT(p, MA_OWNED);
598 MAC_CHECK(check_proc_setresgid, cred, rgid, egid, sgid);
604 mac_check_proc_wait(struct ucred *cred, struct proc *p)
608 PROC_LOCK_ASSERT(p, MA_OWNED);
610 MAC_CHECK(check_proc_wait, cred, p);