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