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
6 * Copyright (c) 2006 SPARTA, Inc.
7 * Copyright (c) 2008 Apple Inc.
10 * This software was developed by Robert Watson and Ilmar Habibulin for the
13 * This software was developed for the FreeBSD Project in part by Network
14 * Associates Laboratories, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
16 * as part of the DARPA CHATS research program.
18 * This software was enhanced by SPARTA ISSO under SPAWAR contract
19 * N66001-04-C-6019 ("SEFOS").
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/condvar.h>
50 #include <sys/imgact.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
62 #include <sys/namei.h>
63 #include <sys/sysctl.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
70 #include <security/mac/mac_framework.h>
71 #include <security/mac/mac_internal.h>
72 #include <security/mac/mac_policy.h>
74 static int mac_mmap_revocation = 1;
75 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
76 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
79 static int mac_mmap_revocation_via_cow = 0;
80 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
81 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
82 "copy-on-write semantics, or by removing all write access");
84 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
85 struct ucred *cred, struct vm_map *map);
88 mac_cred_label_alloc(void)
92 label = mac_labelzone_alloc(M_WAITOK);
93 MAC_PERFORM(cred_init_label, label);
98 mac_cred_init(struct ucred *cred)
101 if (mac_labeled & MPC_OBJECT_CRED)
102 cred->cr_label = mac_cred_label_alloc();
104 cred->cr_label = NULL;
107 static struct label *
108 mac_proc_label_alloc(void)
112 label = mac_labelzone_alloc(M_WAITOK);
113 MAC_PERFORM(proc_init_label, label);
118 mac_proc_init(struct proc *p)
121 if (mac_labeled & MPC_OBJECT_PROC)
122 p->p_label = mac_proc_label_alloc();
128 mac_cred_label_free(struct label *label)
131 MAC_PERFORM(cred_destroy_label, label);
132 mac_labelzone_free(label);
136 mac_cred_destroy(struct ucred *cred)
139 if (cred->cr_label != NULL) {
140 mac_cred_label_free(cred->cr_label);
141 cred->cr_label = NULL;
146 mac_proc_label_free(struct label *label)
149 MAC_PERFORM(proc_destroy_label, label);
150 mac_labelzone_free(label);
154 mac_proc_destroy(struct proc *p)
157 if (p->p_label != NULL) {
158 mac_proc_label_free(p->p_label);
164 mac_cred_externalize_label(struct label *label, char *elements,
165 char *outbuf, size_t outbuflen)
169 MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
175 mac_cred_internalize_label(struct label *label, char *string)
179 MAC_INTERNALIZE(cred, label, string);
185 * Initialize MAC label for the first kernel process, from which other kernel
186 * processes and threads are spawned.
189 mac_proc_create_swapper(struct ucred *cred)
192 MAC_PERFORM(proc_create_swapper, cred);
196 * Initialize MAC label for the first userland process, from which other
197 * userland processes and threads are spawned.
200 mac_proc_create_init(struct ucred *cred)
203 MAC_PERFORM(proc_create_init, cred);
207 * When a thread becomes an NFS server daemon, its credential may need to be
208 * updated to reflect this so that policies can recognize when file system
209 * operations originate from the network.
211 * At some point, it would be desirable if the credential used for each NFS
212 * RPC could be set based on the RPC context (i.e., source system, etc) to
213 * provide more fine-grained access control.
216 mac_proc_associate_nfsd(struct ucred *cred)
219 MAC_PERFORM(proc_associate_nfsd, cred);
223 mac_thread_userret(struct thread *td)
226 MAC_PERFORM(thread_userret, td);
230 * When a new process is created, its label must be initialized. Generally,
231 * this involves inheritence from the parent process, modulo possible deltas.
232 * This function allows that processing to take place.
235 mac_cred_copy(struct ucred *src, struct ucred *dest)
238 MAC_PERFORM(cred_copy_label, src->cr_label, dest->cr_label);
242 mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
252 if (!(mac_labeled & MPC_OBJECT_CRED))
255 error = copyin(mac_p, &mac, sizeof(mac));
259 error = mac_check_structmac_consistent(&mac);
263 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
264 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
266 free(buffer, M_MACTEMP);
270 label = mac_cred_label_alloc();
271 error = mac_cred_internalize_label(label, buffer);
272 free(buffer, M_MACTEMP);
274 mac_cred_label_free(label);
277 imgp->execlabel = label;
282 mac_execve_exit(struct image_params *imgp)
284 if (imgp->execlabel != NULL) {
285 mac_cred_label_free(imgp->execlabel);
286 imgp->execlabel = NULL;
291 mac_execve_interpreter_enter(struct vnode *interpvp,
292 struct label **interpvplabel)
295 if (mac_labeled & MPC_OBJECT_VNODE) {
296 *interpvplabel = mac_vnode_label_alloc();
297 mac_vnode_copy_label(interpvp->v_label, *interpvplabel);
299 *interpvplabel = NULL;
303 mac_execve_interpreter_exit(struct label *interpvplabel)
306 if (interpvplabel != NULL)
307 mac_vnode_label_free(interpvplabel);
311 * When relabeling a process, call out to the policies for the maximum
312 * permission allowed for each object type we know about in its memory space,
313 * and revoke access (in the least surprising ways we know) when necessary.
314 * The process lock is not held here.
317 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
320 /* XXX freeze all other threads */
321 mac_cred_mmapped_drop_perms_recurse(td, cred,
322 &td->td_proc->p_vmspace->vm_map);
323 /* XXX allow other threads to continue */
326 static __inline const char *
327 prot2str(vm_prot_t prot)
330 switch (prot & VM_PROT_ALL) {
333 case VM_PROT_READ | VM_PROT_WRITE:
335 case VM_PROT_READ | VM_PROT_EXECUTE:
337 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
341 case VM_PROT_EXECUTE:
343 case VM_PROT_WRITE | VM_PROT_EXECUTE:
351 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
354 struct vm_map_entry *vme;
355 int vfslocked, result;
356 vm_prot_t revokeperms;
357 vm_object_t backing_object, object;
362 if (!mac_mmap_revocation)
365 vm_map_lock_read(map);
366 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
367 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
368 mac_cred_mmapped_drop_perms_recurse(td, cred,
369 vme->object.sub_map);
373 * Skip over entries that obviously are not shared.
375 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
376 !vme->max_protection)
379 * Drill down to the deepest backing object.
381 offset = vme->offset;
382 object = vme->object.vm_object;
385 VM_OBJECT_LOCK(object);
386 while ((backing_object = object->backing_object) != NULL) {
387 VM_OBJECT_LOCK(backing_object);
388 offset += object->backing_object_offset;
389 VM_OBJECT_UNLOCK(object);
390 object = backing_object;
392 VM_OBJECT_UNLOCK(object);
394 * At the moment, vm_maps and objects aren't considered by
395 * the MAC system, so only things with backing by a normal
396 * object (read: vnodes) are checked.
398 if (object->type != OBJT_VNODE)
400 vp = (struct vnode *)object->handle;
401 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
402 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
403 result = vme->max_protection;
404 mac_vnode_check_mmap_downgrade(cred, vp, &result);
407 * Find out what maximum protection we may be allowing now
408 * but a policy needs to get removed.
410 revokeperms = vme->max_protection & ~result;
412 VFS_UNLOCK_GIANT(vfslocked);
415 printf("pid %ld: revoking %s perms from %#lx:%ld "
416 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
417 prot2str(revokeperms), (u_long)vme->start,
418 (long)(vme->end - vme->start),
419 prot2str(vme->max_protection), prot2str(vme->protection));
420 vm_map_lock_upgrade(map);
422 * This is the really simple case: if a map has more
423 * max_protection than is allowed, but it's not being
424 * actually used (that is, the current protection is still
425 * allowed), we can just wipe it out and do nothing more.
427 if ((vme->protection & revokeperms) == 0) {
428 vme->max_protection -= revokeperms;
430 if (revokeperms & VM_PROT_WRITE) {
432 * In the more complicated case, flush out all
433 * pending changes to the object then turn it
436 vm_object_reference(object);
437 (void) vn_start_write(vp, &mp, V_WAIT);
438 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
439 VM_OBJECT_LOCK(object);
440 vm_object_page_clean(object,
442 OFF_TO_IDX(offset + vme->end - vme->start +
445 VM_OBJECT_UNLOCK(object);
447 vn_finished_write(mp);
448 vm_object_deallocate(object);
450 * Why bother if there's no read permissions
451 * anymore? For the rest, we need to leave
452 * the write permissions on for COW, or
453 * remove them entirely if configured to.
455 if (!mac_mmap_revocation_via_cow) {
456 vme->max_protection &= ~VM_PROT_WRITE;
457 vme->protection &= ~VM_PROT_WRITE;
458 } if ((revokeperms & VM_PROT_READ) == 0)
459 vme->eflags |= MAP_ENTRY_COW |
460 MAP_ENTRY_NEEDS_COPY;
462 if (revokeperms & VM_PROT_EXECUTE) {
463 vme->max_protection &= ~VM_PROT_EXECUTE;
464 vme->protection &= ~VM_PROT_EXECUTE;
466 if (revokeperms & VM_PROT_READ) {
467 vme->max_protection = 0;
470 pmap_protect(map->pmap, vme->start, vme->end,
471 vme->protection & ~revokeperms);
472 vm_map_simplify_entry(map, vme);
474 vm_map_lock_downgrade(map);
475 VFS_UNLOCK_GIANT(vfslocked);
477 vm_map_unlock_read(map);
481 * When the subject's label changes, it may require revocation of privilege
482 * to mapped objects. This can't be done on-the-fly later with a unified
486 mac_cred_relabel(struct ucred *cred, struct label *newlabel)
489 MAC_PERFORM(cred_relabel, cred, newlabel);
493 mac_cred_check_relabel(struct ucred *cred, struct label *newlabel)
497 MAC_CHECK(cred_check_relabel, cred, newlabel);
503 mac_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
507 MAC_CHECK(cred_check_visible, cr1, cr2);
513 mac_proc_check_debug(struct ucred *cred, struct proc *p)
517 PROC_LOCK_ASSERT(p, MA_OWNED);
519 MAC_CHECK(proc_check_debug, cred, p);
525 mac_proc_check_sched(struct ucred *cred, struct proc *p)
529 PROC_LOCK_ASSERT(p, MA_OWNED);
531 MAC_CHECK(proc_check_sched, cred, p);
537 mac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
541 PROC_LOCK_ASSERT(p, MA_OWNED);
543 MAC_CHECK(proc_check_signal, cred, p, signum);
549 mac_proc_check_setuid(struct proc *p, struct ucred *cred, uid_t uid)
553 PROC_LOCK_ASSERT(p, MA_OWNED);
555 MAC_CHECK(proc_check_setuid, cred, uid);
560 mac_proc_check_seteuid(struct proc *p, struct ucred *cred, uid_t euid)
564 PROC_LOCK_ASSERT(p, MA_OWNED);
566 MAC_CHECK(proc_check_seteuid, cred, euid);
571 mac_proc_check_setgid(struct proc *p, struct ucred *cred, gid_t gid)
575 PROC_LOCK_ASSERT(p, MA_OWNED);
577 MAC_CHECK(proc_check_setgid, cred, gid);
583 mac_proc_check_setegid(struct proc *p, struct ucred *cred, gid_t egid)
587 PROC_LOCK_ASSERT(p, MA_OWNED);
589 MAC_CHECK(proc_check_setegid, cred, egid);
595 mac_proc_check_setgroups(struct proc *p, struct ucred *cred, int ngroups,
600 PROC_LOCK_ASSERT(p, MA_OWNED);
602 MAC_CHECK(proc_check_setgroups, cred, ngroups, gidset);
607 mac_proc_check_setreuid(struct proc *p, struct ucred *cred, uid_t ruid,
612 PROC_LOCK_ASSERT(p, MA_OWNED);
614 MAC_CHECK(proc_check_setreuid, cred, ruid, euid);
620 mac_proc_check_setregid(struct proc *proc, struct ucred *cred, gid_t rgid,
625 PROC_LOCK_ASSERT(proc, MA_OWNED);
627 MAC_CHECK(proc_check_setregid, cred, rgid, egid);
633 mac_proc_check_setresuid(struct proc *p, struct ucred *cred, uid_t ruid,
634 uid_t euid, uid_t suid)
638 PROC_LOCK_ASSERT(p, MA_OWNED);
640 MAC_CHECK(proc_check_setresuid, cred, ruid, euid, suid);
645 mac_proc_check_setresgid(struct proc *p, struct ucred *cred, gid_t rgid,
646 gid_t egid, gid_t sgid)
650 PROC_LOCK_ASSERT(p, MA_OWNED);
652 MAC_CHECK(proc_check_setresgid, cred, rgid, egid, sgid);
658 mac_proc_check_wait(struct ucred *cred, struct proc *p)
662 PROC_LOCK_ASSERT(p, MA_OWNED);
664 MAC_CHECK(proc_check_wait, cred, p);