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 <sys/mac_policy.h>
67 #include <security/mac/mac_internal.h>
69 int mac_enforce_process = 1;
70 SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
71 &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
72 TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
74 int mac_enforce_vm = 1;
75 SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
76 &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
77 TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
79 static int mac_mmap_revocation = 1;
80 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
81 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
84 static int mac_mmap_revocation_via_cow = 0;
85 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
86 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
87 "copy-on-write semantics, or by removing all write access");
89 static int mac_enforce_suid = 1;
90 SYSCTL_INT(_security_mac, OID_AUTO, enforce_suid, CTLFLAG_RW,
91 &mac_enforce_suid, 0, "Enforce MAC policy on suid/sgid operations");
92 TUNABLE_INT("security.mac.enforce_suid", &mac_enforce_suid);
95 static unsigned int nmaccreds, nmacprocs;
96 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
97 &nmaccreds, 0, "number of ucreds in use");
98 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, procs, CTLFLAG_RD,
99 &nmacprocs, 0, "number of procs in use");
102 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
103 struct ucred *cred, struct vm_map *map);
106 mac_cred_label_alloc(void)
110 label = mac_labelzone_alloc(M_WAITOK);
111 MAC_PERFORM(init_cred_label, label);
112 MAC_DEBUG_COUNTER_INC(&nmaccreds);
117 mac_init_cred(struct ucred *cred)
120 cred->cr_label = mac_cred_label_alloc();
123 static struct label *
124 mac_proc_label_alloc(void)
128 label = mac_labelzone_alloc(M_WAITOK);
129 MAC_PERFORM(init_proc_label, label);
130 MAC_DEBUG_COUNTER_INC(&nmacprocs);
135 mac_init_proc(struct proc *p)
138 p->p_label = mac_proc_label_alloc();
142 mac_cred_label_free(struct label *label)
145 MAC_PERFORM(destroy_cred_label, label);
146 mac_labelzone_free(label);
147 MAC_DEBUG_COUNTER_DEC(&nmaccreds);
151 mac_destroy_cred(struct ucred *cred)
154 mac_cred_label_free(cred->cr_label);
155 cred->cr_label = NULL;
159 mac_proc_label_free(struct label *label)
162 MAC_PERFORM(destroy_proc_label, label);
163 mac_labelzone_free(label);
164 MAC_DEBUG_COUNTER_DEC(&nmacprocs);
168 mac_destroy_proc(struct proc *p)
171 mac_proc_label_free(p->p_label);
176 mac_externalize_cred_label(struct label *label, char *elements,
177 char *outbuf, size_t outbuflen)
181 MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
187 mac_internalize_cred_label(struct label *label, char *string)
191 MAC_INTERNALIZE(cred, label, string);
197 * Initialize MAC label for the first kernel process, from which other
198 * kernel processes and threads are spawned.
201 mac_create_proc0(struct ucred *cred)
204 MAC_PERFORM(create_proc0, cred);
208 * Initialize MAC label for the first userland process, from which other
209 * userland processes and threads are spawned.
212 mac_create_proc1(struct ucred *cred)
215 MAC_PERFORM(create_proc1, cred);
219 mac_thread_userret(struct thread *td)
222 MAC_PERFORM(thread_userret, td);
226 * When a new process is created, its label must be initialized. Generally,
227 * this involves inheritence from the parent process, modulo possible
228 * deltas. This function allows that processing to take place.
231 mac_copy_cred(struct ucred *src, struct ucred *dest)
234 MAC_PERFORM(copy_cred_label, src->cr_label, dest->cr_label);
238 mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
248 error = copyin(mac_p, &mac, sizeof(mac));
252 error = mac_check_structmac_consistent(&mac);
256 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
257 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
259 free(buffer, M_MACTEMP);
263 label = mac_cred_label_alloc();
264 error = mac_internalize_cred_label(label, buffer);
265 free(buffer, M_MACTEMP);
267 mac_cred_label_free(label);
270 imgp->execlabel = label;
275 mac_execve_exit(struct image_params *imgp)
277 if (imgp->execlabel != NULL) {
278 mac_cred_label_free(imgp->execlabel);
279 imgp->execlabel = NULL;
284 * When relabeling a process, call out to the policies for the maximum
285 * permission allowed for each object type we know about in its
286 * memory space, and revoke access (in the least surprising ways we
287 * know) when necessary. The process lock is not held here.
290 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
293 /* XXX freeze all other threads */
294 mac_cred_mmapped_drop_perms_recurse(td, cred,
295 &td->td_proc->p_vmspace->vm_map);
296 /* XXX allow other threads to continue */
299 static __inline const char *
300 prot2str(vm_prot_t prot)
303 switch (prot & VM_PROT_ALL) {
306 case VM_PROT_READ | VM_PROT_WRITE:
308 case VM_PROT_READ | VM_PROT_EXECUTE:
310 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
314 case VM_PROT_EXECUTE:
316 case VM_PROT_WRITE | VM_PROT_EXECUTE:
324 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
327 struct vm_map_entry *vme;
328 int vfslocked, result;
329 vm_prot_t revokeperms;
330 vm_object_t backing_object, object;
334 if (!mac_mmap_revocation)
337 vm_map_lock_read(map);
338 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
339 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
340 mac_cred_mmapped_drop_perms_recurse(td, cred,
341 vme->object.sub_map);
345 * Skip over entries that obviously are not shared.
347 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
348 !vme->max_protection)
351 * Drill down to the deepest backing object.
353 offset = vme->offset;
354 object = vme->object.vm_object;
357 VM_OBJECT_LOCK(object);
358 while ((backing_object = object->backing_object) != NULL) {
359 VM_OBJECT_LOCK(backing_object);
360 offset += object->backing_object_offset;
361 VM_OBJECT_UNLOCK(object);
362 object = backing_object;
364 VM_OBJECT_UNLOCK(object);
366 * At the moment, vm_maps and objects aren't considered
367 * by the MAC system, so only things with backing by a
368 * normal object (read: vnodes) are checked.
370 if (object->type != OBJT_VNODE)
372 vp = (struct vnode *)object->handle;
373 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
374 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
375 result = vme->max_protection;
376 mac_check_vnode_mmap_downgrade(cred, vp, &result);
377 VOP_UNLOCK(vp, 0, td);
379 * Find out what maximum protection we may be allowing
380 * now but a policy needs to get removed.
382 revokeperms = vme->max_protection & ~result;
384 VFS_UNLOCK_GIANT(vfslocked);
387 printf("pid %ld: revoking %s perms from %#lx:%ld "
388 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
389 prot2str(revokeperms), (u_long)vme->start,
390 (long)(vme->end - vme->start),
391 prot2str(vme->max_protection), prot2str(vme->protection));
392 vm_map_lock_upgrade(map);
394 * This is the really simple case: if a map has more
395 * max_protection than is allowed, but it's not being
396 * actually used (that is, the current protection is
397 * still allowed), we can just wipe it out and do
400 if ((vme->protection & revokeperms) == 0) {
401 vme->max_protection -= revokeperms;
403 if (revokeperms & VM_PROT_WRITE) {
405 * In the more complicated case, flush out all
406 * pending changes to the object then turn it
409 vm_object_reference(object);
410 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
411 VM_OBJECT_LOCK(object);
412 vm_object_page_clean(object,
414 OFF_TO_IDX(offset + vme->end - vme->start +
417 VM_OBJECT_UNLOCK(object);
418 VOP_UNLOCK(vp, 0, td);
419 vm_object_deallocate(object);
421 * Why bother if there's no read permissions
422 * anymore? For the rest, we need to leave
423 * the write permissions on for COW, or
424 * remove them entirely if configured to.
426 if (!mac_mmap_revocation_via_cow) {
427 vme->max_protection &= ~VM_PROT_WRITE;
428 vme->protection &= ~VM_PROT_WRITE;
429 } if ((revokeperms & VM_PROT_READ) == 0)
430 vme->eflags |= MAP_ENTRY_COW |
431 MAP_ENTRY_NEEDS_COPY;
433 if (revokeperms & VM_PROT_EXECUTE) {
434 vme->max_protection &= ~VM_PROT_EXECUTE;
435 vme->protection &= ~VM_PROT_EXECUTE;
437 if (revokeperms & VM_PROT_READ) {
438 vme->max_protection = 0;
441 pmap_protect(map->pmap, vme->start, vme->end,
442 vme->protection & ~revokeperms);
443 vm_map_simplify_entry(map, vme);
445 vm_map_lock_downgrade(map);
446 VFS_UNLOCK_GIANT(vfslocked);
448 vm_map_unlock_read(map);
452 * When the subject's label changes, it may require revocation of privilege
453 * to mapped objects. This can't be done on-the-fly later with a unified
457 mac_relabel_cred(struct ucred *cred, struct label *newlabel)
460 MAC_PERFORM(relabel_cred, cred, newlabel);
464 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
468 MAC_CHECK(check_cred_relabel, cred, newlabel);
474 mac_check_cred_visible(struct ucred *u1, struct ucred *u2)
478 if (!mac_enforce_process)
481 MAC_CHECK(check_cred_visible, u1, u2);
487 mac_check_proc_debug(struct ucred *cred, struct proc *proc)
491 PROC_LOCK_ASSERT(proc, MA_OWNED);
493 if (!mac_enforce_process)
496 MAC_CHECK(check_proc_debug, cred, proc);
502 mac_check_proc_sched(struct ucred *cred, struct proc *proc)
506 PROC_LOCK_ASSERT(proc, MA_OWNED);
508 if (!mac_enforce_process)
511 MAC_CHECK(check_proc_sched, cred, proc);
517 mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
521 PROC_LOCK_ASSERT(proc, MA_OWNED);
523 if (!mac_enforce_process)
526 MAC_CHECK(check_proc_signal, cred, proc, signum);
532 mac_check_proc_setuid(struct proc *proc, struct ucred *cred, uid_t uid)
536 PROC_LOCK_ASSERT(proc, MA_OWNED);
538 if (!mac_enforce_suid)
541 MAC_CHECK(check_proc_setuid, cred, uid);
546 mac_check_proc_seteuid(struct proc *proc, struct ucred *cred, uid_t euid)
550 PROC_LOCK_ASSERT(proc, MA_OWNED);
552 if (!mac_enforce_suid)
555 MAC_CHECK(check_proc_seteuid, cred, euid);
560 mac_check_proc_setgid(struct proc *proc, struct ucred *cred, gid_t gid)
564 PROC_LOCK_ASSERT(proc, MA_OWNED);
566 if (!mac_enforce_suid)
569 MAC_CHECK(check_proc_setgid, cred, gid);
574 mac_check_proc_setegid(struct proc *proc, struct ucred *cred, gid_t egid)
578 PROC_LOCK_ASSERT(proc, MA_OWNED);
580 if (!mac_enforce_suid)
583 MAC_CHECK(check_proc_setegid, cred, egid);
588 mac_check_proc_setgroups(struct proc *proc, struct ucred *cred,
589 int ngroups, gid_t *gidset)
593 PROC_LOCK_ASSERT(proc, MA_OWNED);
595 if (!mac_enforce_suid)
598 MAC_CHECK(check_proc_setgroups, cred, ngroups, gidset);
603 mac_check_proc_setreuid(struct proc *proc, struct ucred *cred, uid_t ruid,
608 PROC_LOCK_ASSERT(proc, MA_OWNED);
610 if (!mac_enforce_suid)
613 MAC_CHECK(check_proc_setreuid, cred, ruid, euid);
618 mac_check_proc_setregid(struct proc *proc, struct ucred *cred, gid_t rgid,
623 PROC_LOCK_ASSERT(proc, MA_OWNED);
625 if (!mac_enforce_suid)
628 MAC_CHECK(check_proc_setregid, cred, rgid, egid);
633 mac_check_proc_setresuid(struct proc *proc, struct ucred *cred, uid_t ruid,
634 uid_t euid, uid_t suid)
638 PROC_LOCK_ASSERT(proc, MA_OWNED);
640 if (!mac_enforce_suid)
643 MAC_CHECK(check_proc_setresuid, cred, ruid, euid, suid);
648 mac_check_proc_setresgid(struct proc *proc, struct ucred *cred, gid_t rgid,
649 gid_t egid, gid_t sgid)
653 PROC_LOCK_ASSERT(proc, MA_OWNED);
655 if (!mac_enforce_suid)
658 MAC_CHECK(check_proc_setresgid, cred, rgid, egid, sgid);
663 mac_check_proc_wait(struct ucred *cred, struct proc *proc)
667 PROC_LOCK_ASSERT(proc, MA_OWNED);
669 if (!mac_enforce_process)
672 MAC_CHECK(check_proc_wait, cred, proc);