1 /*- 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 * All rights reserved. 7 * 8 * This software was developed by Robert Watson and Ilmar Habibulin for the 9 * TrustedBSD Project. 10 * 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. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 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. 24 * 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 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_mac.h" 42 43 #include <sys/param.h> 44 #include <sys/condvar.h> 45 #include <sys/imgact.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mutex.h> 50 #include <sys/mac.h> 51 #include <sys/proc.h> 52 #include <sys/sbuf.h> 53 #include <sys/systm.h> 54 #include <sys/vnode.h> 55 #include <sys/mount.h> 56 #include <sys/file.h> 57 #include <sys/namei.h> 58 #include <sys/sysctl.h> 59 60 #include <vm/vm.h> 61 #include <vm/pmap.h> 62 #include <vm/vm_map.h> 63 #include <vm/vm_object.h> 64 65 #include <sys/mac_policy.h> 66 67 #include <security/mac/mac_framework.h> 68 #include <security/mac/mac_internal.h> 69 70 int mac_enforce_process = 1; 71 SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW, 72 &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations"); 73 TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process); 74 75 int mac_enforce_vm = 1; 76 SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW, 77 &mac_enforce_vm, 0, "Enforce MAC policy on vm operations"); 78 TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm); 79 80 static int mac_mmap_revocation = 1; 81 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW, 82 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject " 83 "relabel"); 84 85 static int mac_mmap_revocation_via_cow = 0; 86 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW, 87 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via " 88 "copy-on-write semantics, or by removing all write access"); 89 90 static int mac_enforce_suid = 1; 91 SYSCTL_INT(_security_mac, OID_AUTO, enforce_suid, CTLFLAG_RW, 92 &mac_enforce_suid, 0, "Enforce MAC policy on suid/sgid operations"); 93 TUNABLE_INT("security.mac.enforce_suid", &mac_enforce_suid); 94 95 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td, 96 struct ucred *cred, struct vm_map *map); 97 98 struct label * 99 mac_cred_label_alloc(void) 100 { 101 struct label *label; 102 103 label = mac_labelzone_alloc(M_WAITOK); 104 MAC_PERFORM(init_cred_label, label); 105 return (label); 106 } 107 108 void 109 mac_init_cred(struct ucred *cred) 110 { 111 112 cred->cr_label = mac_cred_label_alloc(); 113 } 114 115 static struct label * 116 mac_proc_label_alloc(void) 117 { 118 struct label *label; 119 120 label = mac_labelzone_alloc(M_WAITOK); 121 MAC_PERFORM(init_proc_label, label); 122 return (label); 123 } 124 125 void 126 mac_init_proc(struct proc *p) 127 { 128 129 p->p_label = mac_proc_label_alloc(); 130 } 131 132 void 133 mac_cred_label_free(struct label *label) 134 { 135 136 MAC_PERFORM(destroy_cred_label, label); 137 mac_labelzone_free(label); 138 } 139 140 void 141 mac_destroy_cred(struct ucred *cred) 142 { 143 144 mac_cred_label_free(cred->cr_label); 145 cred->cr_label = NULL; 146 } 147 148 static void 149 mac_proc_label_free(struct label *label) 150 { 151 152 MAC_PERFORM(destroy_proc_label, label); 153 mac_labelzone_free(label); 154 } 155 156 void 157 mac_destroy_proc(struct proc *p) 158 { 159 160 mac_proc_label_free(p->p_label); 161 p->p_label = NULL; 162 } 163 164 int 165 mac_externalize_cred_label(struct label *label, char *elements, 166 char *outbuf, size_t outbuflen) 167 { 168 int error; 169 170 MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen); 171 172 return (error); 173 } 174 175 int 176 mac_internalize_cred_label(struct label *label, char *string) 177 { 178 int error; 179 180 MAC_INTERNALIZE(cred, label, string); 181 182 return (error); 183 } 184 185 /* 186 * Initialize MAC label for the first kernel process, from which other 187 * kernel processes and threads are spawned. 188 */ 189 void 190 mac_create_proc0(struct ucred *cred) 191 { 192 193 MAC_PERFORM(create_proc0, cred); 194 } 195 196 /* 197 * Initialize MAC label for the first userland process, from which other 198 * userland processes and threads are spawned. 199 */ 200 void 201 mac_create_proc1(struct ucred *cred) 202 { 203 204 MAC_PERFORM(create_proc1, cred); 205 } 206 207 void 208 mac_thread_userret(struct thread *td) 209 { 210 211 MAC_PERFORM(thread_userret, td); 212 } 213 214 /* 215 * When a new process is created, its label must be initialized. Generally, 216 * this involves inheritence from the parent process, modulo possible 217 * deltas. This function allows that processing to take place. 218 */ 219 void 220 mac_copy_cred(struct ucred *src, struct ucred *dest) 221 { 222 223 MAC_PERFORM(copy_cred_label, src->cr_label, dest->cr_label); 224 } 225 226 int 227 mac_execve_enter(struct image_params *imgp, struct mac *mac_p) 228 { 229 struct label *label; 230 struct mac mac; 231 char *buffer; 232 int error; 233 234 if (mac_p == NULL) 235 return (0); 236 237 error = copyin(mac_p, &mac, sizeof(mac)); 238 if (error) 239 return (error); 240 241 error = mac_check_structmac_consistent(&mac); 242 if (error) 243 return (error); 244 245 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); 246 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); 247 if (error) { 248 free(buffer, M_MACTEMP); 249 return (error); 250 } 251 252 label = mac_cred_label_alloc(); 253 error = mac_internalize_cred_label(label, buffer); 254 free(buffer, M_MACTEMP); 255 if (error) { 256 mac_cred_label_free(label); 257 return (error); 258 } 259 imgp->execlabel = label; 260 return (0); 261 } 262 263 void 264 mac_execve_exit(struct image_params *imgp) 265 { 266 if (imgp->execlabel != NULL) { 267 mac_cred_label_free(imgp->execlabel); 268 imgp->execlabel = NULL; 269 } 270 } 271 272 /* 273 * When relabeling a process, call out to the policies for the maximum 274 * permission allowed for each object type we know about in its 275 * memory space, and revoke access (in the least surprising ways we 276 * know) when necessary. The process lock is not held here. 277 */ 278 void 279 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred) 280 { 281 282 /* XXX freeze all other threads */ 283 mac_cred_mmapped_drop_perms_recurse(td, cred, 284 &td->td_proc->p_vmspace->vm_map); 285 /* XXX allow other threads to continue */ 286 } 287 288 static __inline const char * 289 prot2str(vm_prot_t prot) 290 { 291 292 switch (prot & VM_PROT_ALL) { 293 case VM_PROT_READ: 294 return ("r--"); 295 case VM_PROT_READ | VM_PROT_WRITE: 296 return ("rw-"); 297 case VM_PROT_READ | VM_PROT_EXECUTE: 298 return ("r-x"); 299 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 300 return ("rwx"); 301 case VM_PROT_WRITE: 302 return ("-w-"); 303 case VM_PROT_EXECUTE: 304 return ("--x"); 305 case VM_PROT_WRITE | VM_PROT_EXECUTE: 306 return ("-wx"); 307 default: 308 return ("---"); 309 } 310 } 311 312 static void 313 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred, 314 struct vm_map *map) 315 { 316 struct vm_map_entry *vme; 317 int vfslocked, result; 318 vm_prot_t revokeperms; 319 vm_object_t backing_object, object; 320 vm_ooffset_t offset; 321 struct vnode *vp; 322 struct mount *mp; 323 324 if (!mac_mmap_revocation) 325 return; 326 327 vm_map_lock_read(map); 328 for (vme = map->header.next; vme != &map->header; vme = vme->next) { 329 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) { 330 mac_cred_mmapped_drop_perms_recurse(td, cred, 331 vme->object.sub_map); 332 continue; 333 } 334 /* 335 * Skip over entries that obviously are not shared. 336 */ 337 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) || 338 !vme->max_protection) 339 continue; 340 /* 341 * Drill down to the deepest backing object. 342 */ 343 offset = vme->offset; 344 object = vme->object.vm_object; 345 if (object == NULL) 346 continue; 347 VM_OBJECT_LOCK(object); 348 while ((backing_object = object->backing_object) != NULL) { 349 VM_OBJECT_LOCK(backing_object); 350 offset += object->backing_object_offset; 351 VM_OBJECT_UNLOCK(object); 352 object = backing_object; 353 } 354 VM_OBJECT_UNLOCK(object); 355 /* 356 * At the moment, vm_maps and objects aren't considered 357 * by the MAC system, so only things with backing by a 358 * normal object (read: vnodes) are checked. 359 */ 360 if (object->type != OBJT_VNODE) 361 continue; 362 vp = (struct vnode *)object->handle; 363 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 364 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 365 result = vme->max_protection; 366 mac_check_vnode_mmap_downgrade(cred, vp, &result); 367 VOP_UNLOCK(vp, 0, td); 368 /* 369 * Find out what maximum protection we may be allowing 370 * now but a policy needs to get removed. 371 */ 372 revokeperms = vme->max_protection & ~result; 373 if (!revokeperms) { 374 VFS_UNLOCK_GIANT(vfslocked); 375 continue; 376 } 377 printf("pid %ld: revoking %s perms from %#lx:%ld " 378 "(max %s/cur %s)\n", (long)td->td_proc->p_pid, 379 prot2str(revokeperms), (u_long)vme->start, 380 (long)(vme->end - vme->start), 381 prot2str(vme->max_protection), prot2str(vme->protection)); 382 vm_map_lock_upgrade(map); 383 /* 384 * This is the really simple case: if a map has more 385 * max_protection than is allowed, but it's not being 386 * actually used (that is, the current protection is 387 * still allowed), we can just wipe it out and do 388 * nothing more. 389 */ 390 if ((vme->protection & revokeperms) == 0) { 391 vme->max_protection -= revokeperms; 392 } else { 393 if (revokeperms & VM_PROT_WRITE) { 394 /* 395 * In the more complicated case, flush out all 396 * pending changes to the object then turn it 397 * copy-on-write. 398 */ 399 vm_object_reference(object); 400 (void) vn_start_write(vp, &mp, V_WAIT); 401 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 402 VM_OBJECT_LOCK(object); 403 vm_object_page_clean(object, 404 OFF_TO_IDX(offset), 405 OFF_TO_IDX(offset + vme->end - vme->start + 406 PAGE_MASK), 407 OBJPC_SYNC); 408 VM_OBJECT_UNLOCK(object); 409 VOP_UNLOCK(vp, 0, td); 410 vn_finished_write(mp); 411 vm_object_deallocate(object); 412 /* 413 * Why bother if there's no read permissions 414 * anymore? For the rest, we need to leave 415 * the write permissions on for COW, or 416 * remove them entirely if configured to. 417 */ 418 if (!mac_mmap_revocation_via_cow) { 419 vme->max_protection &= ~VM_PROT_WRITE; 420 vme->protection &= ~VM_PROT_WRITE; 421 } if ((revokeperms & VM_PROT_READ) == 0) 422 vme->eflags |= MAP_ENTRY_COW | 423 MAP_ENTRY_NEEDS_COPY; 424 } 425 if (revokeperms & VM_PROT_EXECUTE) { 426 vme->max_protection &= ~VM_PROT_EXECUTE; 427 vme->protection &= ~VM_PROT_EXECUTE; 428 } 429 if (revokeperms & VM_PROT_READ) { 430 vme->max_protection = 0; 431 vme->protection = 0; 432 } 433 pmap_protect(map->pmap, vme->start, vme->end, 434 vme->protection & ~revokeperms); 435 vm_map_simplify_entry(map, vme); 436 } 437 vm_map_lock_downgrade(map); 438 VFS_UNLOCK_GIANT(vfslocked); 439 } 440 vm_map_unlock_read(map); 441 } 442 443 /* 444 * When the subject's label changes, it may require revocation of privilege 445 * to mapped objects. This can't be done on-the-fly later with a unified 446 * buffer cache. 447 */ 448 void 449 mac_relabel_cred(struct ucred *cred, struct label *newlabel) 450 { 451 452 MAC_PERFORM(relabel_cred, cred, newlabel); 453 } 454 455 int 456 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel) 457 { 458 int error; 459 460 MAC_CHECK(check_cred_relabel, cred, newlabel); 461 462 return (error); 463 } 464 465 int 466 mac_check_cred_visible(struct ucred *u1, struct ucred *u2) 467 { 468 int error; 469 470 if (!mac_enforce_process) 471 return (0); 472 473 MAC_CHECK(check_cred_visible, u1, u2); 474 475 return (error); 476 } 477 478 int 479 mac_check_proc_debug(struct ucred *cred, struct proc *proc) 480 { 481 int error; 482 483 PROC_LOCK_ASSERT(proc, MA_OWNED); 484 485 if (!mac_enforce_process) 486 return (0); 487 488 MAC_CHECK(check_proc_debug, cred, proc); 489 490 return (error); 491 } 492 493 int 494 mac_check_proc_sched(struct ucred *cred, struct proc *proc) 495 { 496 int error; 497 498 PROC_LOCK_ASSERT(proc, MA_OWNED); 499 500 if (!mac_enforce_process) 501 return (0); 502 503 MAC_CHECK(check_proc_sched, cred, proc); 504 505 return (error); 506 } 507 508 int 509 mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum) 510 { 511 int error; 512 513 PROC_LOCK_ASSERT(proc, MA_OWNED); 514 515 if (!mac_enforce_process) 516 return (0); 517 518 MAC_CHECK(check_proc_signal, cred, proc, signum); 519 520 return (error); 521 } 522 523 int 524 mac_check_proc_setuid(struct proc *proc, struct ucred *cred, uid_t uid) 525 { 526 int error; 527 528 PROC_LOCK_ASSERT(proc, MA_OWNED); 529 530 if (!mac_enforce_suid) 531 return (0); 532 533 MAC_CHECK(check_proc_setuid, cred, uid); 534 return (error); 535 } 536 537 int 538 mac_check_proc_seteuid(struct proc *proc, struct ucred *cred, uid_t euid) 539 { 540 int error; 541 542 PROC_LOCK_ASSERT(proc, MA_OWNED); 543 544 if (!mac_enforce_suid) 545 return (0); 546 547 MAC_CHECK(check_proc_seteuid, cred, euid); 548 return (error); 549 } 550 551 int 552 mac_check_proc_setgid(struct proc *proc, struct ucred *cred, gid_t gid) 553 { 554 int error; 555 556 PROC_LOCK_ASSERT(proc, MA_OWNED); 557 558 if (!mac_enforce_suid) 559 return (0); 560 561 MAC_CHECK(check_proc_setgid, cred, gid); 562 return (error); 563 } 564 565 int 566 mac_check_proc_setegid(struct proc *proc, struct ucred *cred, gid_t egid) 567 { 568 int error; 569 570 PROC_LOCK_ASSERT(proc, MA_OWNED); 571 572 if (!mac_enforce_suid) 573 return (0); 574 575 MAC_CHECK(check_proc_setegid, cred, egid); 576 return (error); 577 } 578 579 int 580 mac_check_proc_setgroups(struct proc *proc, struct ucred *cred, 581 int ngroups, gid_t *gidset) 582 { 583 int error; 584 585 PROC_LOCK_ASSERT(proc, MA_OWNED); 586 587 if (!mac_enforce_suid) 588 return (0); 589 590 MAC_CHECK(check_proc_setgroups, cred, ngroups, gidset); 591 return (error); 592 } 593 594 int 595 mac_check_proc_setreuid(struct proc *proc, struct ucred *cred, uid_t ruid, 596 uid_t euid) 597 { 598 int error; 599 600 PROC_LOCK_ASSERT(proc, MA_OWNED); 601 602 if (!mac_enforce_suid) 603 return (0); 604 605 MAC_CHECK(check_proc_setreuid, cred, ruid, euid); 606 return (error); 607 } 608 609 int 610 mac_check_proc_setregid(struct proc *proc, struct ucred *cred, gid_t rgid, 611 gid_t egid) 612 { 613 int error; 614 615 PROC_LOCK_ASSERT(proc, MA_OWNED); 616 617 if (!mac_enforce_suid) 618 return (0); 619 620 MAC_CHECK(check_proc_setregid, cred, rgid, egid); 621 return (error); 622 } 623 624 int 625 mac_check_proc_setresuid(struct proc *proc, struct ucred *cred, uid_t ruid, 626 uid_t euid, uid_t suid) 627 { 628 int error; 629 630 PROC_LOCK_ASSERT(proc, MA_OWNED); 631 632 if (!mac_enforce_suid) 633 return (0); 634 635 MAC_CHECK(check_proc_setresuid, cred, ruid, euid, suid); 636 return (error); 637 } 638 639 int 640 mac_check_proc_setresgid(struct proc *proc, struct ucred *cred, gid_t rgid, 641 gid_t egid, gid_t sgid) 642 { 643 int error; 644 645 PROC_LOCK_ASSERT(proc, MA_OWNED); 646 647 if (!mac_enforce_suid) 648 return (0); 649 650 MAC_CHECK(check_proc_setresgid, cred, rgid, egid, sgid); 651 return (error); 652 } 653 654 int 655 mac_check_proc_wait(struct ucred *cred, struct proc *proc) 656 { 657 int error; 658 659 PROC_LOCK_ASSERT(proc, MA_OWNED); 660 661 if (!mac_enforce_process) 662 return (0); 663 664 MAC_CHECK(check_proc_wait, cred, proc); 665 666 return (error); 667 } 668