audit_bsm_klib.c revision 182090
1184610Salfred/* 2184610Salfred * Copyright (c) 1999-2005 Apple Inc. 3184610Salfred * Copyright (c) 2005 Robert N. M. Watson 4184610Salfred * All rights reserved. 5184610Salfred * 6184610Salfred * Redistribution and use in source and binary forms, with or without 7184610Salfred * modification, are permitted provided that the following conditions 8184610Salfred * are met: 9184610Salfred * 1. Redistributions of source code must retain the above copyright 10184610Salfred * notice, this list of conditions and the following disclaimer. 11184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 12184610Salfred * notice, this list of conditions and the following disclaimer in the 13184610Salfred * documentation and/or other materials provided with the distribution. 14184610Salfred * 3. Neither the name of Apple Inc. ("Apple") nor the names of 15184610Salfred * its contributors may be used to endorse or promote products derived 16184610Salfred * from this software without specific prior written permission. 17184610Salfred * 18184610Salfred * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 19184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 22184610Salfred * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26184610Salfred * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27184610Salfred * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28184610Salfred * POSSIBILITY OF SUCH DAMAGE. 29184610Salfred */ 30184610Salfred 31184610Salfred#include <sys/cdefs.h> 32184610Salfred__FBSDID("$FreeBSD: head/sys/security/audit/audit_bsm_klib.c 182090 2008-08-24 03:12:17Z csjp $"); 33184610Salfred 34184610Salfred#include <sys/param.h> 35184610Salfred#include <sys/fcntl.h> 36184610Salfred#include <sys/filedesc.h> 37184610Salfred#include <sys/libkern.h> 38184610Salfred#include <sys/malloc.h> 39184610Salfred#include <sys/mount.h> 40184610Salfred#include <sys/proc.h> 41184610Salfred#include <sys/sem.h> 42184610Salfred#include <sys/sbuf.h> 43184610Salfred#include <sys/syscall.h> 44184610Salfred#include <sys/sysctl.h> 45184610Salfred#include <sys/sysent.h> 46184610Salfred#include <sys/vnode.h> 47184610Salfred 48184610Salfred#include <bsm/audit.h> 49184610Salfred#include <bsm/audit_kevents.h> 50184610Salfred#include <security/audit/audit.h> 51184610Salfred#include <security/audit/audit_private.h> 52184610Salfred 53184610Salfred/* 54184610Salfred * Hash table functions for the audit event number to event class mask 55184610Salfred * mapping. 56184610Salfred */ 57184610Salfred#define EVCLASSMAP_HASH_TABLE_SIZE 251 58184610Salfredstruct evclass_elem { 59184610Salfred au_event_t event; 60184610Salfred au_class_t class; 61184610Salfred LIST_ENTRY(evclass_elem) entry; 62184610Salfred}; 63184610Salfredstruct evclass_list { 64184610Salfred LIST_HEAD(, evclass_elem) head; 65184610Salfred}; 66184610Salfred 67184610Salfredstatic MALLOC_DEFINE(M_AUDITEVCLASS, "audit_evclass", "Audit event class"); 68184610Salfredstatic struct mtx evclass_mtx; 69184610Salfredstatic struct evclass_list evclass_hash[EVCLASSMAP_HASH_TABLE_SIZE]; 70184610Salfred 71184610Salfred/* 72184610Salfred * Look up the class for an audit event in the class mapping table. 73184610Salfred */ 74184610Salfredau_class_t 75184610Salfredau_event_class(au_event_t event) 76184610Salfred{ 77184610Salfred struct evclass_list *evcl; 78184610Salfred struct evclass_elem *evc; 79184610Salfred au_class_t class; 80184610Salfred 81184610Salfred mtx_lock(&evclass_mtx); 82184610Salfred evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE]; 83184610Salfred class = 0; 84184610Salfred LIST_FOREACH(evc, &evcl->head, entry) { 85184610Salfred if (evc->event == event) { 86184610Salfred class = evc->class; 87184610Salfred goto out; 88184610Salfred } 89184610Salfred } 90184610Salfredout: 91184610Salfred mtx_unlock(&evclass_mtx); 92184610Salfred return (class); 93184610Salfred} 94184610Salfred 95184610Salfred/* 96184610Salfred * Insert a event to class mapping. If the event already exists in the 97184610Salfred * mapping, then replace the mapping with the new one. 98184610Salfred * 99184610Salfred * XXX There is currently no constraints placed on the number of mappings. 100184610Salfred * May want to either limit to a number, or in terms of memory usage. 101184610Salfred */ 102184610Salfredvoid 103184610Salfredau_evclassmap_insert(au_event_t event, au_class_t class) 104184610Salfred{ 105184610Salfred struct evclass_list *evcl; 106184610Salfred struct evclass_elem *evc, *evc_new; 107184610Salfred 108184610Salfred /* 109184610Salfred * Pessimistically, always allocate storage before acquiring mutex. 110184610Salfred * Free if there is already a mapping for this event. 111184610Salfred */ 112184610Salfred evc_new = malloc(sizeof(*evc), M_AUDITEVCLASS, M_WAITOK); 113184610Salfred 114184610Salfred mtx_lock(&evclass_mtx); 115184610Salfred evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE]; 116184610Salfred LIST_FOREACH(evc, &evcl->head, entry) { 117184610Salfred if (evc->event == event) { 118184610Salfred evc->class = class; 119184610Salfred mtx_unlock(&evclass_mtx); 120184610Salfred free(evc_new, M_AUDITEVCLASS); 121184610Salfred return; 122184610Salfred } 123184610Salfred } 124184610Salfred evc = evc_new; 125184610Salfred evc->event = event; 126184610Salfred evc->class = class; 127184610Salfred LIST_INSERT_HEAD(&evcl->head, evc, entry); 128184610Salfred mtx_unlock(&evclass_mtx); 129184610Salfred} 130184610Salfred 131184610Salfredvoid 132184610Salfredau_evclassmap_init(void) 133184610Salfred{ 134184610Salfred int i; 135184610Salfred 136184610Salfred mtx_init(&evclass_mtx, "evclass_mtx", NULL, MTX_DEF); 137184610Salfred for (i = 0; i < EVCLASSMAP_HASH_TABLE_SIZE; i++) 138184610Salfred LIST_INIT(&evclass_hash[i].head); 139184610Salfred 140184610Salfred /* 141184610Salfred * Set up the initial event to class mapping for system calls. 142184610Salfred * 143184610Salfred * XXXRW: Really, this should walk all possible audit events, not all 144184610Salfred * native ABI system calls, as there may be audit events reachable 145184610Salfred * only through non-native system calls. It also seems a shame to 146184610Salfred * frob the mutex this early. 147184610Salfred */ 148184610Salfred for (i = 0; i < SYS_MAXSYSCALL; i++) { 149184610Salfred if (sysent[i].sy_auevent != AUE_NULL) 150184610Salfred au_evclassmap_insert(sysent[i].sy_auevent, 0); 151184610Salfred } 152184610Salfred} 153184610Salfred 154184610Salfred/* 155184610Salfred * Check whether an event is aditable by comparing the mask of classes this 156184610Salfred * event is part of against the given mask. 157184610Salfred */ 158184610Salfredint 159184610Salfredau_preselect(au_event_t event, au_class_t class, au_mask_t *mask_p, int sorf) 160184610Salfred{ 161184610Salfred au_class_t effmask = 0; 162184610Salfred 163184610Salfred if (mask_p == NULL) 164184610Salfred return (-1); 165184610Salfred 166184610Salfred /* 167184610Salfred * Perform the actual check of the masks against the event. 168184610Salfred */ 169184610Salfred if (sorf & AU_PRS_SUCCESS) 170184610Salfred effmask |= (mask_p->am_success & class); 171184610Salfred 172184610Salfred if (sorf & AU_PRS_FAILURE) 173184610Salfred effmask |= (mask_p->am_failure & class); 174184610Salfred 175184610Salfred if (effmask) 176184610Salfred return (1); 177193644Sthompsa else 178184610Salfred return (0); 179184610Salfred} 180184610Salfred 181184610Salfred/* 182184610Salfred * Convert sysctl names and present arguments to events. 183184610Salfred */ 184184610Salfredau_event_t 185184610Salfredaudit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg) 186184610Salfred{ 187184610Salfred 188184610Salfred /* can't parse it - so return the worst case */ 189184610Salfred if ((valid_arg & (ARG_CTLNAME | ARG_LEN)) != (ARG_CTLNAME | ARG_LEN)) 190184610Salfred return (AUE_SYSCTL); 191184610Salfred 192184610Salfred switch (name[0]) { 193184610Salfred /* non-admin "lookups" treat them special */ 194184610Salfred case KERN_OSTYPE: 195184610Salfred case KERN_OSRELEASE: 196184610Salfred case KERN_OSREV: 197184610Salfred case KERN_VERSION: 198184610Salfred case KERN_ARGMAX: 199184610Salfred case KERN_CLOCKRATE: 200184610Salfred case KERN_BOOTTIME: 201184610Salfred case KERN_POSIX1: 202184610Salfred case KERN_NGROUPS: 203184610Salfred case KERN_JOB_CONTROL: 204184610Salfred case KERN_SAVED_IDS: 205184610Salfred case KERN_OSRELDATE: 206184610Salfred case KERN_DUMMY: 207184610Salfred return (AUE_SYSCTL_NONADMIN); 208184610Salfred 209184610Salfred /* only treat the changeable controls as admin */ 210184610Salfred case KERN_MAXVNODES: 211184610Salfred case KERN_MAXPROC: 212184610Salfred case KERN_MAXFILES: 213184610Salfred case KERN_MAXPROCPERUID: 214184610Salfred case KERN_MAXFILESPERPROC: 215184610Salfred case KERN_HOSTID: 216184610Salfred case KERN_SECURELVL: 217184610Salfred case KERN_HOSTNAME: 218184610Salfred case KERN_VNODE: 219184610Salfred case KERN_PROC: 220198776Sthompsa case KERN_FILE: 221184610Salfred case KERN_PROF: 222184610Salfred case KERN_NISDOMAINNAME: 223184610Salfred case KERN_UPDATEINTERVAL: 224184610Salfred case KERN_NTP_PLL: 225184610Salfred case KERN_BOOTFILE: 226184610Salfred case KERN_DUMPDEV: 227184610Salfred case KERN_IPC: 228184610Salfred case KERN_PS_STRINGS: 229184610Salfred case KERN_USRSTACK: 230184610Salfred case KERN_LOGSIGEXIT: 231184610Salfred case KERN_IOV_MAX: 232184610Salfred case KERN_MAXID: 233184610Salfred return ((valid_arg & ARG_VALUE) ? 234193644Sthompsa AUE_SYSCTL : AUE_SYSCTL_NONADMIN); 235184610Salfred 236184610Salfred default: 237184610Salfred return (AUE_SYSCTL); 238184610Salfred } 239184610Salfred /* NOTREACHED */ 240184610Salfred} 241193074Sthompsa 242193074Sthompsa/* 243193074Sthompsa * Convert an open flags specifier into a specific type of open event for 244193045Sthompsa * auditing purposes. 245184610Salfred */ 246184610Salfredau_event_t 247184610Salfredaudit_flags_and_error_to_openevent(int oflags, int error) 248184610Salfred{ 249184610Salfred au_event_t aevent; 250184610Salfred 251184610Salfred /* 252184610Salfred * Need to check only those flags we care about. 253184610Salfred */ 254184610Salfred oflags = oflags & (O_RDONLY | O_CREAT | O_TRUNC | O_RDWR | O_WRONLY); 255193045Sthompsa 256193045Sthompsa /* 257184610Salfred * These checks determine what flags are on with the condition that 258184610Salfred * ONLY that combination is on, and no other flags are on. 259184610Salfred */ 260184610Salfred switch (oflags) { 261184610Salfred case O_RDONLY: 262184610Salfred aevent = AUE_OPEN_R; 263184610Salfred break; 264184610Salfred 265195966Salfred case (O_RDONLY | O_CREAT): 266184610Salfred aevent = AUE_OPEN_RC; 267184610Salfred break; 268184610Salfred 269184610Salfred case (O_RDONLY | O_CREAT | O_TRUNC): 270184610Salfred aevent = AUE_OPEN_RTC; 271184610Salfred break; 272184610Salfred 273184610Salfred case (O_RDONLY | O_TRUNC): 274184610Salfred aevent = AUE_OPEN_RT; 275193644Sthompsa break; 276185948Sthompsa 277193045Sthompsa case O_RDWR: 278185948Sthompsa aevent = AUE_OPEN_RW; 279185948Sthompsa break; 280185948Sthompsa 281193045Sthompsa case (O_RDWR | O_CREAT): 282184610Salfred aevent = AUE_OPEN_RWC; 283185948Sthompsa break; 284185948Sthompsa 285184610Salfred case (O_RDWR | O_CREAT | O_TRUNC): 286185948Sthompsa aevent = AUE_OPEN_RWTC; 287185948Sthompsa break; 288184610Salfred 289184610Salfred case (O_RDWR | O_TRUNC): 290193074Sthompsa aevent = AUE_OPEN_RWT; 291185948Sthompsa break; 292194228Sthompsa 293184610Salfred case O_WRONLY: 294193074Sthompsa aevent = AUE_OPEN_W; 295184610Salfred break; 296184610Salfred 297184610Salfred case (O_WRONLY | O_CREAT): 298184610Salfred aevent = AUE_OPEN_WC; 299184610Salfred break; 300184610Salfred 301184610Salfred case (O_WRONLY | O_CREAT | O_TRUNC): 302198776Sthompsa aevent = AUE_OPEN_WTC; 303198776Sthompsa break; 304198776Sthompsa 305198776Sthompsa case (O_WRONLY | O_TRUNC): 306198776Sthompsa aevent = AUE_OPEN_WT; 307184610Salfred break; 308193317Sthompsa 309184610Salfred default: 310184610Salfred aevent = AUE_OPEN; 311 break; 312 } 313 314#if 0 315 /* 316 * Convert chatty errors to better matching events. Failures to 317 * find a file are really just attribute events -- so recast them as 318 * such. 319 * 320 * XXXAUDIT: Solaris defines that AUE_OPEN will never be returned, it 321 * is just a placeholder. However, in Darwin we return that in 322 * preference to other events. For now, comment this out as we don't 323 * have a BSM conversion routine for AUE_OPEN. 324 */ 325 switch (aevent) { 326 case AUE_OPEN_R: 327 case AUE_OPEN_RT: 328 case AUE_OPEN_RW: 329 case AUE_OPEN_RWT: 330 case AUE_OPEN_W: 331 case AUE_OPEN_WT: 332 if (error == ENOENT) 333 aevent = AUE_OPEN; 334 } 335#endif 336 return (aevent); 337} 338 339/* 340 * Convert a MSGCTL command to a specific event. 341 */ 342int 343audit_msgctl_to_event(int cmd) 344{ 345 346 switch (cmd) { 347 case IPC_RMID: 348 return (AUE_MSGCTL_RMID); 349 350 case IPC_SET: 351 return (AUE_MSGCTL_SET); 352 353 case IPC_STAT: 354 return (AUE_MSGCTL_STAT); 355 356 default: 357 /* We will audit a bad command. */ 358 return (AUE_MSGCTL); 359 } 360} 361 362/* 363 * Convert a SEMCTL command to a specific event. 364 */ 365int 366audit_semctl_to_event(int cmd) 367{ 368 369 switch (cmd) { 370 case GETALL: 371 return (AUE_SEMCTL_GETALL); 372 373 case GETNCNT: 374 return (AUE_SEMCTL_GETNCNT); 375 376 case GETPID: 377 return (AUE_SEMCTL_GETPID); 378 379 case GETVAL: 380 return (AUE_SEMCTL_GETVAL); 381 382 case GETZCNT: 383 return (AUE_SEMCTL_GETZCNT); 384 385 case IPC_RMID: 386 return (AUE_SEMCTL_RMID); 387 388 case IPC_SET: 389 return (AUE_SEMCTL_SET); 390 391 case SETALL: 392 return (AUE_SEMCTL_SETALL); 393 394 case SETVAL: 395 return (AUE_SEMCTL_SETVAL); 396 397 case IPC_STAT: 398 return (AUE_SEMCTL_STAT); 399 400 default: 401 /* We will audit a bad command. */ 402 return (AUE_SEMCTL); 403 } 404} 405 406/* 407 * Convert a command for the auditon() system call to a audit event. 408 */ 409int 410auditon_command_event(int cmd) 411{ 412 413 switch(cmd) { 414 case A_GETPOLICY: 415 return (AUE_AUDITON_GPOLICY); 416 417 case A_SETPOLICY: 418 return (AUE_AUDITON_SPOLICY); 419 420 case A_GETKMASK: 421 return (AUE_AUDITON_GETKMASK); 422 423 case A_SETKMASK: 424 return (AUE_AUDITON_SETKMASK); 425 426 case A_GETQCTRL: 427 return (AUE_AUDITON_GQCTRL); 428 429 case A_SETQCTRL: 430 return (AUE_AUDITON_SQCTRL); 431 432 case A_GETCWD: 433 return (AUE_AUDITON_GETCWD); 434 435 case A_GETCAR: 436 return (AUE_AUDITON_GETCAR); 437 438 case A_GETSTAT: 439 return (AUE_AUDITON_GETSTAT); 440 441 case A_SETSTAT: 442 return (AUE_AUDITON_SETSTAT); 443 444 case A_SETUMASK: 445 return (AUE_AUDITON_SETUMASK); 446 447 case A_SETSMASK: 448 return (AUE_AUDITON_SETSMASK); 449 450 case A_GETCOND: 451 return (AUE_AUDITON_GETCOND); 452 453 case A_SETCOND: 454 return (AUE_AUDITON_SETCOND); 455 456 case A_GETCLASS: 457 return (AUE_AUDITON_GETCLASS); 458 459 case A_SETCLASS: 460 return (AUE_AUDITON_SETCLASS); 461 462 case A_GETPINFO: 463 case A_SETPMASK: 464 case A_SETFSIZE: 465 case A_GETFSIZE: 466 case A_GETPINFO_ADDR: 467 case A_GETKAUDIT: 468 case A_SETKAUDIT: 469 default: 470 return (AUE_AUDITON); /* No special record */ 471 } 472} 473 474/* 475 * Create a canonical path from given path by prefixing either the root 476 * directory, or the current working directory. If the process working 477 * directory is NULL, we could use 'rootvnode' to obtain the root directory, 478 * but this results in a volfs name written to the audit log. So we will 479 * leave the filename starting with '/' in the audit log in this case. 480 */ 481void 482audit_canon_path(struct thread *td, char *path, char *cpath) 483{ 484 struct vnode *cvnp, *rvnp; 485 char *rbuf, *fbuf, *copy; 486 struct filedesc *fdp; 487 struct sbuf sbf; 488 int error, cwir, locked; 489 490 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d", 491 __func__, __FILE__, __LINE__); 492 493 copy = path; 494 rvnp = cvnp = NULL; 495 fdp = td->td_proc->p_fd; 496 FILEDESC_SLOCK(fdp); 497 /* 498 * Make sure that we handle the chroot(2) case. If there is an 499 * alternate root directory, prepend it to the audited pathname. 500 */ 501 if (fdp->fd_rdir != NULL && fdp->fd_rdir != rootvnode) { 502 rvnp = fdp->fd_rdir; 503 vhold(rvnp); 504 } 505 /* 506 * If the supplied path is relative, make sure we capture the current 507 * working directory so we can prepend it to the supplied relative 508 * path. 509 */ 510 if (*path != '/') { 511 cvnp = fdp->fd_cdir; 512 vhold(cvnp); 513 } 514 cwir = (fdp->fd_rdir == fdp->fd_cdir); 515 FILEDESC_SUNLOCK(fdp); 516 /* 517 * NB: We require that the supplied array be at least MAXPATHLEN bytes 518 * long. If this is not the case, then we can run into serious trouble. 519 */ 520 (void) sbuf_new(&sbf, cpath, MAXPATHLEN, SBUF_FIXEDLEN); 521 /* 522 * Strip leading forward slashes. 523 */ 524 while (*copy == '/') 525 copy++; 526 /* 527 * Make sure we handle chroot(2) and prepend the global path to these 528 * environments. 529 * 530 * NB: vn_fullpath(9) on FreeBSD is less reliable than vn_getpath(9) 531 * on Darwin. As a result, this may need some additional attention 532 * in the future. 533 */ 534 if (rvnp != NULL) { 535 /* 536 * Although unlikely, it is possible for filesystems to define 537 * their own VOP_LOCK, so strictly speaking, we need to 538 * conditionally pickup Giant around calls to vn_lock(9) 539 */ 540 locked = VFS_LOCK_GIANT(rvnp->v_mount); 541 vn_lock(rvnp, LK_EXCLUSIVE | LK_RETRY); 542 vdrop(rvnp); 543 error = vn_fullpath_global(td, rvnp, &rbuf, &fbuf); 544 VOP_UNLOCK(rvnp, 0); 545 VFS_UNLOCK_GIANT(locked); 546 if (error) { 547 cpath[0] = '\0'; 548 if (cvnp != NULL) 549 vdrop(cvnp); 550 return; 551 } 552 (void) sbuf_cat(&sbf, rbuf); 553 free(fbuf, M_TEMP); 554 } 555 if (cvnp != NULL) { 556 locked = VFS_LOCK_GIANT(cvnp->v_mount); 557 vn_lock(cvnp, LK_EXCLUSIVE | LK_RETRY); 558 vdrop(cvnp); 559 error = vn_fullpath(td, cvnp, &rbuf, &fbuf); 560 VOP_UNLOCK(cvnp, 0); 561 VFS_UNLOCK_GIANT(locked); 562 if (error) { 563 cpath[0] = '\0'; 564 return; 565 } 566 (void) sbuf_cat(&sbf, rbuf); 567 free(fbuf, M_TEMP); 568 } 569 if (cwir == 0 || (cwir != 0 && cvnp == NULL)) 570 (void) sbuf_putc(&sbf, '/'); 571 /* 572 * Now that we have processed any alternate root and relative path 573 * names, add the supplied pathname. 574 */ 575 (void) sbuf_cat(&sbf, copy); 576 /* 577 * One or more of the previous sbuf operations could have resulted in 578 * the supplied buffer being overflowed. Check to see if this is the 579 * case. 580 */ 581 if (sbuf_overflowed(&sbf) != 0) { 582 cpath[0] = '\0'; 583 return; 584 } 585 sbuf_finish(&sbf); 586} 587