| 1 | /*- |
| 2 | * Copyright (c) 1999-2009 Apple Inc. |
| 3 | * Copyright (c) 2006-2007 Robert N. M. Watson |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR |
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 27 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 | /* |
| 32 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce |
| 33 | * support for mandatory and extensible security protections. This notice |
| 34 | * is included in support of clause 2.2 (b) of the Apple Public License, |
| 35 | * Version 2.0. |
| 36 | */ |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/fcntl.h> |
| 40 | #include <sys/kernel.h> |
| 41 | #include <sys/lock.h> |
| 42 | #include <sys/namei.h> |
| 43 | #include <sys/proc_internal.h> |
| 44 | #include <sys/kauth.h> |
| 45 | #include <sys/queue.h> |
| 46 | #include <sys/systm.h> |
| 47 | #include <sys/time.h> |
| 48 | #include <sys/ucred.h> |
| 49 | #include <sys/uio.h> |
| 50 | #include <sys/unistd.h> |
| 51 | #include <sys/file_internal.h> |
| 52 | #include <sys/vnode_internal.h> |
| 53 | #include <sys/user.h> |
| 54 | #include <sys/syscall.h> |
| 55 | #include <sys/malloc.h> |
| 56 | #include <sys/un.h> |
| 57 | #include <sys/sysent.h> |
| 58 | #include <sys/sysproto.h> |
| 59 | #include <sys/vfs_context.h> |
| 60 | #include <sys/domain.h> |
| 61 | #include <sys/protosw.h> |
| 62 | #include <sys/socketvar.h> |
| 63 | |
| 64 | #include <bsm/audit.h> |
| 65 | #include <bsm/audit_internal.h> |
| 66 | #include <bsm/audit_kevents.h> |
| 67 | |
| 68 | #include <security/audit/audit.h> |
| 69 | #include <security/audit/audit_bsd.h> |
| 70 | #include <security/audit/audit_private.h> |
| 71 | |
| 72 | #include <mach/host_priv.h> |
| 73 | #include <mach/host_special_ports.h> |
| 74 | #include <mach/audit_triggers_server.h> |
| 75 | |
| 76 | #include <kern/host.h> |
| 77 | #include <kern/kalloc.h> |
| 78 | #include <kern/zalloc.h> |
| 79 | #include <kern/sched_prim.h> |
| 80 | |
| 81 | #include <net/route.h> |
| 82 | |
| 83 | #include <netinet/in.h> |
| 84 | #include <netinet/in_pcb.h> |
| 85 | |
| 86 | #if CONFIG_AUDIT |
| 87 | MALLOC_DEFINE(M_AUDITDATA, "audit_data" , "Audit data storage" ); |
| 88 | MALLOC_DEFINE(M_AUDITPATH, "audit_path" , "Audit path storage" ); |
| 89 | MALLOC_DEFINE(M_AUDITTEXT, "audit_text" , "Audit text storage" ); |
| 90 | |
| 91 | /* |
| 92 | * Audit control settings that are set/read by system calls and are hence |
| 93 | * non-static. |
| 94 | * |
| 95 | * Define the audit control flags. |
| 96 | */ |
| 97 | int audit_enabled; |
| 98 | int audit_suspended; |
| 99 | |
| 100 | int audit_syscalls; |
| 101 | au_class_t audit_kevent_mask; |
| 102 | |
| 103 | /* |
| 104 | * The audit control mode is used to ensure configuration settings are only |
| 105 | * accepted from appropriate sources based on the current mode. |
| 106 | */ |
| 107 | au_ctlmode_t audit_ctl_mode; |
| 108 | au_expire_after_t audit_expire_after; |
| 109 | |
| 110 | /* |
| 111 | * Flags controlling behavior in low storage situations. Should we panic if |
| 112 | * a write fails? Should we fail stop if we're out of disk space? |
| 113 | */ |
| 114 | int audit_panic_on_write_fail; |
| 115 | int audit_fail_stop; |
| 116 | int audit_argv; |
| 117 | int audit_arge; |
| 118 | |
| 119 | /* |
| 120 | * Are we currently "failing stop" due to out of disk space? |
| 121 | */ |
| 122 | int audit_in_failure; |
| 123 | |
| 124 | /* |
| 125 | * Global audit statistics. |
| 126 | */ |
| 127 | struct audit_fstat audit_fstat; |
| 128 | |
| 129 | /* |
| 130 | * Preselection mask for non-attributable events. |
| 131 | */ |
| 132 | struct au_mask audit_nae_mask; |
| 133 | |
| 134 | /* |
| 135 | * Mutex to protect global variables shared between various threads and |
| 136 | * processes. |
| 137 | */ |
| 138 | struct mtx audit_mtx; |
| 139 | |
| 140 | /* |
| 141 | * Queue of audit records ready for delivery to disk. We insert new records |
| 142 | * at the tail, and remove records from the head. Also, a count of the |
| 143 | * number of records used for checking queue depth. In addition, a counter |
| 144 | * of records that we have allocated but are not yet in the queue, which is |
| 145 | * needed to estimate the total size of the combined set of records |
| 146 | * outstanding in the system. |
| 147 | */ |
| 148 | struct kaudit_queue audit_q; |
| 149 | int audit_q_len; |
| 150 | int audit_pre_q_len; |
| 151 | |
| 152 | /* |
| 153 | * Audit queue control settings (minimum free, low/high water marks, etc.) |
| 154 | */ |
| 155 | struct au_qctrl audit_qctrl; |
| 156 | |
| 157 | /* |
| 158 | * Condition variable to signal to the worker that it has work to do: either |
| 159 | * new records are in the queue, or a log replacement is taking place. |
| 160 | */ |
| 161 | struct cv audit_worker_cv; |
| 162 | |
| 163 | /* |
| 164 | * Condition variable to signal when the worker is done draining the audit |
| 165 | * queue. |
| 166 | */ |
| 167 | struct cv audit_drain_cv; |
| 168 | |
| 169 | /* |
| 170 | * Condition variable to flag when crossing the low watermark, meaning that |
| 171 | * threads blocked due to hitting the high watermark can wake up and continue |
| 172 | * to commit records. |
| 173 | */ |
| 174 | struct cv audit_watermark_cv; |
| 175 | |
| 176 | /* |
| 177 | * Condition variable for auditing threads wait on when in fail-stop mode. |
| 178 | * Threads wait on this CV forever (and ever), never seeing the light of day |
| 179 | * again. |
| 180 | */ |
| 181 | static struct cv audit_fail_cv; |
| 182 | |
| 183 | static zone_t audit_record_zone; |
| 184 | |
| 185 | /* |
| 186 | * Kernel audit information. This will store the current audit address |
| 187 | * or host information that the kernel will use when it's generating |
| 188 | * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2) |
| 189 | * command. |
| 190 | */ |
| 191 | static struct auditinfo_addr audit_kinfo; |
| 192 | static struct rwlock audit_kinfo_lock; |
| 193 | |
| 194 | #define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \ |
| 195 | "audit_kinfo_lock") |
| 196 | #define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock) |
| 197 | #define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock) |
| 198 | #define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock) |
| 199 | #define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock) |
| 200 | |
| 201 | void |
| 202 | audit_set_kinfo(struct auditinfo_addr *ak) |
| 203 | { |
| 204 | |
| 205 | KASSERT(ak->ai_termid.at_type == AU_IPv4 || |
| 206 | ak->ai_termid.at_type == AU_IPv6, |
| 207 | ("audit_set_kinfo: invalid address type" )); |
| 208 | |
| 209 | KINFO_WLOCK(); |
| 210 | bcopy(ak, &audit_kinfo, sizeof(audit_kinfo)); |
| 211 | KINFO_WUNLOCK(); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | audit_get_kinfo(struct auditinfo_addr *ak) |
| 216 | { |
| 217 | |
| 218 | KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 || |
| 219 | audit_kinfo.ai_termid.at_type == AU_IPv6, |
| 220 | ("audit_set_kinfo: invalid address type" )); |
| 221 | |
| 222 | KINFO_RLOCK(); |
| 223 | bcopy(&audit_kinfo, ak, sizeof(*ak)); |
| 224 | KINFO_RUNLOCK(); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Construct an audit record for the passed thread. |
| 229 | */ |
| 230 | static void |
| 231 | audit_record_ctor(proc_t p, struct kaudit_record *ar) |
| 232 | { |
| 233 | kauth_cred_t cred; |
| 234 | |
| 235 | bzero(ar, sizeof(*ar)); |
| 236 | ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; |
| 237 | nanotime(&ar->k_ar.ar_starttime); |
| 238 | |
| 239 | if (PROC_NULL != p) { |
| 240 | cred = kauth_cred_proc_ref(p); |
| 241 | |
| 242 | /* |
| 243 | * Export the subject credential. |
| 244 | */ |
| 245 | cru2x(cred, &ar->k_ar.ar_subj_cred); |
| 246 | ar->k_ar.ar_subj_ruid = kauth_cred_getruid(cred); |
| 247 | ar->k_ar.ar_subj_rgid = kauth_cred_getrgid(cred); |
| 248 | ar->k_ar.ar_subj_egid = kauth_cred_getgid(cred); |
| 249 | ar->k_ar.ar_subj_pid = p->p_pid; |
| 250 | ar->k_ar.ar_subj_auid = cred->cr_audit.as_aia_p->ai_auid; |
| 251 | ar->k_ar.ar_subj_asid = cred->cr_audit.as_aia_p->ai_asid; |
| 252 | bcopy(&cred->cr_audit.as_mask, &ar->k_ar.ar_subj_amask, |
| 253 | sizeof(struct au_mask)); |
| 254 | bcopy(&cred->cr_audit.as_aia_p->ai_termid, |
| 255 | &ar->k_ar.ar_subj_term_addr, sizeof(struct au_tid_addr)); |
| 256 | kauth_cred_unref(&cred); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | audit_record_dtor(struct kaudit_record *ar) |
| 262 | { |
| 263 | |
| 264 | if (ar->k_ar.ar_arg_upath1 != NULL) |
| 265 | free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); |
| 266 | if (ar->k_ar.ar_arg_upath2 != NULL) |
| 267 | free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); |
| 268 | if (ar->k_ar.ar_arg_kpath1 != NULL) |
| 269 | free(ar->k_ar.ar_arg_kpath1, M_AUDITPATH); |
| 270 | if (ar->k_ar.ar_arg_kpath2 != NULL) |
| 271 | free(ar->k_ar.ar_arg_kpath2, M_AUDITPATH); |
| 272 | if (ar->k_ar.ar_arg_text != NULL) |
| 273 | free(ar->k_ar.ar_arg_text, M_AUDITTEXT); |
| 274 | if (ar->k_ar.ar_arg_opaque != NULL) |
| 275 | free(ar->k_ar.ar_arg_opaque, M_AUDITDATA); |
| 276 | if (ar->k_ar.ar_arg_data != NULL) |
| 277 | free(ar->k_ar.ar_arg_data, M_AUDITDATA); |
| 278 | if (ar->k_udata != NULL) |
| 279 | free(ar->k_udata, M_AUDITDATA); |
| 280 | if (ar->k_ar.ar_arg_argv != NULL) |
| 281 | free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); |
| 282 | if (ar->k_ar.ar_arg_envv != NULL) |
| 283 | free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); |
| 284 | audit_identity_info_destruct(&ar->k_ar.ar_arg_identity); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Initialize the Audit subsystem: configuration state, work queue, |
| 289 | * synchronization primitives, worker thread, and trigger device node. Also |
| 290 | * call into the BSM assembly code to initialize it. |
| 291 | */ |
| 292 | void |
| 293 | audit_init(void) |
| 294 | { |
| 295 | |
| 296 | audit_enabled = 0; |
| 297 | audit_syscalls = 0; |
| 298 | audit_kevent_mask = 0; |
| 299 | audit_suspended = 0; |
| 300 | audit_panic_on_write_fail = 0; |
| 301 | audit_fail_stop = 0; |
| 302 | audit_in_failure = 0; |
| 303 | audit_argv = 0; |
| 304 | audit_arge = 0; |
| 305 | audit_ctl_mode = AUDIT_CTLMODE_NORMAL; |
| 306 | audit_expire_after.age = 0; |
| 307 | audit_expire_after.size = 0; |
| 308 | audit_expire_after.op_type = AUDIT_EXPIRE_OP_AND; |
| 309 | |
| 310 | audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ |
| 311 | audit_fstat.af_currsz = 0; |
| 312 | audit_nae_mask.am_success = 0; |
| 313 | audit_nae_mask.am_failure = 0; |
| 314 | |
| 315 | TAILQ_INIT(&audit_q); |
| 316 | audit_q_len = 0; |
| 317 | audit_pre_q_len = 0; |
| 318 | audit_qctrl.aq_hiwater = AQ_HIWATER; |
| 319 | audit_qctrl.aq_lowater = AQ_LOWATER; |
| 320 | audit_qctrl.aq_bufsz = AQ_BUFSZ; |
| 321 | audit_qctrl.aq_minfree = AU_FS_MINFREE; |
| 322 | |
| 323 | audit_kinfo.ai_termid.at_type = AU_IPv4; |
| 324 | audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY; |
| 325 | |
| 326 | _audit_lck_grp_init(); |
| 327 | mtx_init(&audit_mtx, "audit_mtx" , NULL, MTX_DEF); |
| 328 | KINFO_LOCK_INIT(); |
| 329 | cv_init(&audit_worker_cv, "audit_worker_cv" ); |
| 330 | cv_init(&audit_drain_cv, "audit_drain_cv" ); |
| 331 | cv_init(&audit_watermark_cv, "audit_watermark_cv" ); |
| 332 | cv_init(&audit_fail_cv, "audit_fail_cv" ); |
| 333 | |
| 334 | audit_record_zone = zinit(sizeof(struct kaudit_record), |
| 335 | AQ_HIWATER*sizeof(struct kaudit_record), 8192, "audit_zone" ); |
| 336 | #if CONFIG_MACF |
| 337 | audit_mac_init(); |
| 338 | #endif |
| 339 | /* Init audit session subsystem. */ |
| 340 | audit_session_init(); |
| 341 | |
| 342 | /* Initialize the BSM audit subsystem. */ |
| 343 | kau_init(); |
| 344 | |
| 345 | /* audit_trigger_init(); */ |
| 346 | |
| 347 | /* Start audit worker thread. */ |
| 348 | (void) audit_pipe_init(); |
| 349 | |
| 350 | /* Start audit worker thread. */ |
| 351 | audit_worker_init(); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Drain the audit queue and close the log at shutdown. Note that this can |
| 356 | * be called both from the system shutdown path and also from audit |
| 357 | * configuration syscalls, so 'arg' and 'howto' are ignored. |
| 358 | */ |
| 359 | void |
| 360 | audit_shutdown(void) |
| 361 | { |
| 362 | |
| 363 | audit_rotate_vnode(NULL, NULL); |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Return the current thread's audit record, if any. |
| 368 | */ |
| 369 | struct kaudit_record * |
| 370 | currecord(void) |
| 371 | { |
| 372 | |
| 373 | return (curthread()->uu_ar); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * XXXAUDIT: There are a number of races present in the code below due to |
| 378 | * release and re-grab of the mutex. The code should be revised to become |
| 379 | * slightly less racy. |
| 380 | * |
| 381 | * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available |
| 382 | * pre_q space, suspending the system call until there is room? |
| 383 | */ |
| 384 | struct kaudit_record * |
| 385 | audit_new(int event, proc_t p, __unused struct uthread *uthread) |
| 386 | { |
| 387 | struct kaudit_record *ar; |
| 388 | int no_record; |
| 389 | int audit_override; |
| 390 | |
| 391 | /* |
| 392 | * Override the audit_suspended and audit_enabled if it always |
| 393 | * audits session events. |
| 394 | * |
| 395 | * XXXss - This really needs to be a generalized call to a filter |
| 396 | * interface so if other things that use the audit subsystem in the |
| 397 | * future can simply plugged in. |
| 398 | */ |
| 399 | audit_override = (AUE_SESSION_START == event || |
| 400 | AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || |
| 401 | AUE_SESSION_CLOSE == event); |
| 402 | |
| 403 | mtx_lock(&audit_mtx); |
| 404 | no_record = (audit_suspended || !audit_enabled); |
| 405 | mtx_unlock(&audit_mtx); |
| 406 | if (!audit_override && no_record) |
| 407 | return (NULL); |
| 408 | |
| 409 | /* |
| 410 | * Initialize the audit record header. |
| 411 | * XXX: We may want to fail-stop if allocation fails. |
| 412 | * |
| 413 | * Note: the number of outstanding uncommitted audit records is |
| 414 | * limited to the number of concurrent threads servicing system calls |
| 415 | * in the kernel. |
| 416 | */ |
| 417 | ar = zalloc(audit_record_zone); |
| 418 | if (ar == NULL) |
| 419 | return NULL; |
| 420 | audit_record_ctor(p, ar); |
| 421 | ar->k_ar.ar_event = event; |
| 422 | |
| 423 | #if CONFIG_MACF |
| 424 | if (PROC_NULL != p) { |
| 425 | if (audit_mac_new(p, ar) != 0) { |
| 426 | zfree(audit_record_zone, ar); |
| 427 | return (NULL); |
| 428 | } |
| 429 | } else |
| 430 | ar->k_ar.ar_mac_records = NULL; |
| 431 | #endif |
| 432 | |
| 433 | mtx_lock(&audit_mtx); |
| 434 | audit_pre_q_len++; |
| 435 | mtx_unlock(&audit_mtx); |
| 436 | |
| 437 | return (ar); |
| 438 | } |
| 439 | |
| 440 | void |
| 441 | audit_free(struct kaudit_record *ar) |
| 442 | { |
| 443 | |
| 444 | audit_record_dtor(ar); |
| 445 | #if CONFIG_MACF |
| 446 | if (NULL != ar->k_ar.ar_mac_records) |
| 447 | audit_mac_free(ar); |
| 448 | #endif |
| 449 | zfree(audit_record_zone, ar); |
| 450 | } |
| 451 | |
| 452 | void |
| 453 | audit_commit(struct kaudit_record *ar, int error, int retval) |
| 454 | { |
| 455 | au_event_t event; |
| 456 | au_class_t class; |
| 457 | au_id_t auid; |
| 458 | int sorf; |
| 459 | struct au_mask *aumask; |
| 460 | int audit_override; |
| 461 | |
| 462 | if (ar == NULL) |
| 463 | return; |
| 464 | |
| 465 | /* |
| 466 | * Decide whether to commit the audit record by checking the error |
| 467 | * value from the system call and using the appropriate audit mask. |
| 468 | */ |
| 469 | if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) |
| 470 | aumask = &audit_nae_mask; |
| 471 | else |
| 472 | aumask = &ar->k_ar.ar_subj_amask; |
| 473 | |
| 474 | if (error) |
| 475 | sorf = AU_PRS_FAILURE; |
| 476 | else |
| 477 | sorf = AU_PRS_SUCCESS; |
| 478 | |
| 479 | switch(ar->k_ar.ar_event) { |
| 480 | case AUE_OPEN_RWTC: |
| 481 | /* |
| 482 | * The open syscall always writes a AUE_OPEN_RWTC event; |
| 483 | * change it to the proper type of event based on the flags |
| 484 | * and the error value. |
| 485 | */ |
| 486 | ar->k_ar.ar_event = audit_flags_and_error_to_openevent( |
| 487 | ar->k_ar.ar_arg_fflags, error); |
| 488 | break; |
| 489 | |
| 490 | case AUE_OPEN_EXTENDED_RWTC: |
| 491 | /* |
| 492 | * The open_extended syscall always writes a |
| 493 | * AUE_OPEN_EXTENDEDRWTC event; change it to the proper type of |
| 494 | * event based on the flags and the error value. |
| 495 | */ |
| 496 | ar->k_ar.ar_event = audit_flags_and_error_to_openextendedevent( |
| 497 | ar->k_ar.ar_arg_fflags, error); |
| 498 | break; |
| 499 | |
| 500 | case AUE_OPENAT_RWTC: |
| 501 | /* |
| 502 | * The openat syscall always writes a |
| 503 | * AUE_OPENAT_RWTC event; change it to the proper type of |
| 504 | * event based on the flags and the error value. |
| 505 | */ |
| 506 | ar->k_ar.ar_event = audit_flags_and_error_to_openatevent( |
| 507 | ar->k_ar.ar_arg_fflags, error); |
| 508 | break; |
| 509 | |
| 510 | case AUE_OPENBYID_RWT: |
| 511 | /* |
| 512 | * The openbyid syscall always writes a |
| 513 | * AUE_OPENBYID_RWT event; change it to the proper type of |
| 514 | * event based on the flags and the error value. |
| 515 | */ |
| 516 | ar->k_ar.ar_event = audit_flags_and_error_to_openbyidevent( |
| 517 | ar->k_ar.ar_arg_fflags, error); |
| 518 | break; |
| 519 | |
| 520 | case AUE_SYSCTL: |
| 521 | ar->k_ar.ar_event = audit_ctlname_to_sysctlevent( |
| 522 | ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); |
| 523 | break; |
| 524 | |
| 525 | case AUE_AUDITON: |
| 526 | /* Convert the auditon() command to an event. */ |
| 527 | ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); |
| 528 | break; |
| 529 | |
| 530 | case AUE_FCNTL: |
| 531 | /* Convert some fcntl() commands to their own events. */ |
| 532 | ar->k_ar.ar_event = audit_fcntl_command_event( |
| 533 | ar->k_ar.ar_arg_cmd, ar->k_ar.ar_arg_fflags, error); |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | auid = ar->k_ar.ar_subj_auid; |
| 538 | event = ar->k_ar.ar_event; |
| 539 | class = au_event_class(event); |
| 540 | |
| 541 | /* |
| 542 | * See if we need to override the audit_suspend and audit_enabled |
| 543 | * flags. |
| 544 | * |
| 545 | * XXXss - This check needs to be generalized so new filters can |
| 546 | * easily be added. |
| 547 | */ |
| 548 | audit_override = (AUE_SESSION_START == event || |
| 549 | AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || |
| 550 | AUE_SESSION_CLOSE == event); |
| 551 | |
| 552 | ar->k_ar_commit |= AR_COMMIT_KERNEL; |
| 553 | if (au_preselect(event, class, aumask, sorf) != 0) |
| 554 | ar->k_ar_commit |= AR_PRESELECT_TRAIL; |
| 555 | if (audit_pipe_preselect(auid, event, class, sorf, |
| 556 | ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) |
| 557 | ar->k_ar_commit |= AR_PRESELECT_PIPE; |
| 558 | if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | |
| 559 | AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE | |
| 560 | AR_PRESELECT_FILTER)) == 0) { |
| 561 | mtx_lock(&audit_mtx); |
| 562 | audit_pre_q_len--; |
| 563 | mtx_unlock(&audit_mtx); |
| 564 | audit_free(ar); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | ar->k_ar.ar_errno = error; |
| 569 | ar->k_ar.ar_retval = retval; |
| 570 | nanotime(&ar->k_ar.ar_endtime); |
| 571 | |
| 572 | /* |
| 573 | * Note: it could be that some records initiated while audit was |
| 574 | * enabled should still be committed? |
| 575 | */ |
| 576 | mtx_lock(&audit_mtx); |
| 577 | if (!audit_override && (audit_suspended || !audit_enabled)) { |
| 578 | audit_pre_q_len--; |
| 579 | mtx_unlock(&audit_mtx); |
| 580 | audit_free(ar); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Constrain the number of committed audit records based on the |
| 586 | * configurable parameter. |
| 587 | */ |
| 588 | while (audit_q_len >= audit_qctrl.aq_hiwater) |
| 589 | cv_wait(&audit_watermark_cv, &audit_mtx); |
| 590 | |
| 591 | TAILQ_INSERT_TAIL(&audit_q, ar, k_q); |
| 592 | audit_q_len++; |
| 593 | audit_pre_q_len--; |
| 594 | cv_signal(&audit_worker_cv); |
| 595 | mtx_unlock(&audit_mtx); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * audit_syscall_enter() is called on entry to each system call. It is |
| 600 | * responsible for deciding whether or not to audit the call (preselection), |
| 601 | * and if so, allocating a per-thread audit record. audit_new() will fill in |
| 602 | * basic thread/credential properties. |
| 603 | */ |
| 604 | void |
| 605 | audit_syscall_enter(unsigned int code, proc_t proc, struct uthread *uthread) |
| 606 | { |
| 607 | struct au_mask *aumask; |
| 608 | au_class_t class; |
| 609 | au_event_t event; |
| 610 | au_id_t auid; |
| 611 | kauth_cred_t cred; |
| 612 | |
| 613 | /* |
| 614 | * In FreeBSD, each ABI has its own system call table, and hence |
| 615 | * mapping of system call codes to audit events. Convert the code to |
| 616 | * an audit event identifier using the process system call table |
| 617 | * reference. In Darwin, there's only one, so we use the global |
| 618 | * symbol for the system call table. No audit record is generated |
| 619 | * for bad system calls, as no operation has been performed. |
| 620 | * |
| 621 | * In Mac OS X, the audit events are stored in a table seperate from |
| 622 | * the syscall table(s). This table is generated by makesyscalls.sh |
| 623 | * from syscalls.master and stored in audit_kevents.c. |
| 624 | */ |
| 625 | if (code >= nsysent) |
| 626 | return; |
| 627 | event = sys_au_event[code]; |
| 628 | if (event == AUE_NULL) |
| 629 | return; |
| 630 | |
| 631 | KASSERT(uthread->uu_ar == NULL, |
| 632 | ("audit_syscall_enter: uthread->uu_ar != NULL" )); |
| 633 | |
| 634 | /* |
| 635 | * Check which audit mask to use; either the kernel non-attributable |
| 636 | * event mask or the process audit mask. |
| 637 | */ |
| 638 | cred = kauth_cred_proc_ref(proc); |
| 639 | auid = cred->cr_audit.as_aia_p->ai_auid; |
| 640 | if (auid == AU_DEFAUDITID) |
| 641 | aumask = &audit_nae_mask; |
| 642 | else |
| 643 | aumask = &cred->cr_audit.as_mask; |
| 644 | |
| 645 | /* |
| 646 | * Allocate an audit record, if preselection allows it, and store in |
| 647 | * the thread for later use. |
| 648 | */ |
| 649 | class = au_event_class(event); |
| 650 | #if CONFIG_MACF |
| 651 | /* |
| 652 | * Note: audit_mac_syscall_enter() may call audit_new() and allocate |
| 653 | * memory for the audit record (uu_ar). |
| 654 | */ |
| 655 | if (audit_mac_syscall_enter(code, proc, uthread, cred, event) == 0) |
| 656 | goto out; |
| 657 | #endif |
| 658 | if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { |
| 659 | /* |
| 660 | * If we're out of space and need to suspend unprivileged |
| 661 | * processes, do that here rather than trying to allocate |
| 662 | * another audit record. |
| 663 | * |
| 664 | * Note: we might wish to be able to continue here in the |
| 665 | * future, if the system recovers. That should be possible |
| 666 | * by means of checking the condition in a loop around |
| 667 | * cv_wait(). It might be desirable to reevaluate whether an |
| 668 | * audit record is still required for this event by |
| 669 | * re-calling au_preselect(). |
| 670 | */ |
| 671 | if (audit_in_failure && |
| 672 | suser(cred, &proc->p_acflag) != 0) { |
| 673 | cv_wait(&audit_fail_cv, &audit_mtx); |
| 674 | panic("audit_failing_stop: thread continued" ); |
| 675 | } |
| 676 | if (uthread->uu_ar == NULL) |
| 677 | uthread->uu_ar = audit_new(event, proc, uthread); |
| 678 | } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) { |
| 679 | if (uthread->uu_ar == NULL) |
| 680 | uthread->uu_ar = audit_new(event, proc, uthread); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * All audited events will contain an identity |
| 685 | * |
| 686 | * Note: Identity should be obtained prior to the syscall implementation |
| 687 | * being called to handle cases like execve(2) where the process changes |
| 688 | */ |
| 689 | AUDIT_ARG(identity); |
| 690 | |
| 691 | out: |
| 692 | kauth_cred_unref(&cred); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * audit_syscall_exit() is called from the return of every system call, or in |
| 697 | * the event of exit1(), during the execution of exit1(). It is responsible |
| 698 | * for committing the audit record, if any, along with return condition. |
| 699 | * |
| 700 | * Note: The audit_syscall_exit() parameter list was modified to support |
| 701 | * mac_audit_check_postselect(), which requires the syscall number. |
| 702 | */ |
| 703 | #if CONFIG_MACF |
| 704 | void |
| 705 | audit_syscall_exit(unsigned int code, int error, __unused proc_t proc, |
| 706 | struct uthread *uthread) |
| 707 | #else |
| 708 | void |
| 709 | audit_syscall_exit(int error, __unsed proc_t proc, struct uthread *uthread) |
| 710 | #endif |
| 711 | { |
| 712 | int retval; |
| 713 | |
| 714 | /* |
| 715 | * Commit the audit record as desired; once we pass the record into |
| 716 | * audit_commit(), the memory is owned by the audit subsystem. The |
| 717 | * return value from the system call is stored on the user thread. |
| 718 | * If there was an error, the return value is set to -1, imitating |
| 719 | * the behavior of the cerror routine. |
| 720 | */ |
| 721 | if (error) |
| 722 | retval = -1; |
| 723 | else |
| 724 | retval = uthread->uu_rval[0]; |
| 725 | |
| 726 | #if CONFIG_MACF |
| 727 | if (audit_mac_syscall_exit(code, uthread, error, retval) != 0) |
| 728 | goto out; |
| 729 | #endif |
| 730 | audit_commit(uthread->uu_ar, error, retval); |
| 731 | |
| 732 | out: |
| 733 | uthread->uu_ar = NULL; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Calls to set up and tear down audit structures used during Mach system |
| 738 | * calls. |
| 739 | */ |
| 740 | void |
| 741 | audit_mach_syscall_enter(unsigned short event) |
| 742 | { |
| 743 | struct uthread *uthread; |
| 744 | proc_t proc; |
| 745 | struct au_mask *aumask; |
| 746 | kauth_cred_t cred; |
| 747 | au_class_t class; |
| 748 | au_id_t auid; |
| 749 | |
| 750 | if (event == AUE_NULL) |
| 751 | return; |
| 752 | |
| 753 | uthread = curthread(); |
| 754 | if (uthread == NULL) |
| 755 | return; |
| 756 | |
| 757 | proc = current_proc(); |
| 758 | if (proc == NULL) |
| 759 | return; |
| 760 | |
| 761 | KASSERT(uthread->uu_ar == NULL, |
| 762 | ("audit_mach_syscall_enter: uthread->uu_ar != NULL" )); |
| 763 | |
| 764 | cred = kauth_cred_proc_ref(proc); |
| 765 | auid = cred->cr_audit.as_aia_p->ai_auid; |
| 766 | |
| 767 | /* |
| 768 | * Check which audit mask to use; either the kernel non-attributable |
| 769 | * event mask or the process audit mask. |
| 770 | */ |
| 771 | if (auid == AU_DEFAUDITID) |
| 772 | aumask = &audit_nae_mask; |
| 773 | else |
| 774 | aumask = &cred->cr_audit.as_mask; |
| 775 | |
| 776 | /* |
| 777 | * Allocate an audit record, if desired, and store in the BSD thread |
| 778 | * for later use. |
| 779 | */ |
| 780 | class = au_event_class(event); |
| 781 | if (au_preselect(event, class, aumask, AU_PRS_BOTH)) |
| 782 | uthread->uu_ar = audit_new(event, proc, uthread); |
| 783 | else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) |
| 784 | uthread->uu_ar = audit_new(event, proc, uthread); |
| 785 | else |
| 786 | uthread->uu_ar = NULL; |
| 787 | |
| 788 | kauth_cred_unref(&cred); |
| 789 | } |
| 790 | |
| 791 | void |
| 792 | audit_mach_syscall_exit(int retval, struct uthread *uthread) |
| 793 | { |
| 794 | /* |
| 795 | * The error code from Mach system calls is the same as the |
| 796 | * return value |
| 797 | */ |
| 798 | /* XXX Is the above statement always true? */ |
| 799 | audit_commit(uthread->uu_ar, retval, retval); |
| 800 | uthread->uu_ar = NULL; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * kau_will_audit can be used by a security policy to determine |
| 805 | * if an audit record will be stored, reducing wasted memory allocation |
| 806 | * and string handling. |
| 807 | */ |
| 808 | int |
| 809 | kau_will_audit(void) |
| 810 | { |
| 811 | |
| 812 | return (audit_enabled && currecord() != NULL); |
| 813 | } |
| 814 | |
| 815 | #if CONFIG_COREDUMP |
| 816 | void |
| 817 | audit_proc_coredump(proc_t proc, char *path, int errcode) |
| 818 | { |
| 819 | struct kaudit_record *ar; |
| 820 | struct au_mask *aumask; |
| 821 | au_class_t class; |
| 822 | int ret, sorf; |
| 823 | char **pathp; |
| 824 | au_id_t auid; |
| 825 | kauth_cred_t my_cred; |
| 826 | struct uthread *uthread; |
| 827 | |
| 828 | ret = 0; |
| 829 | |
| 830 | /* |
| 831 | * Make sure we are using the correct preselection mask. |
| 832 | */ |
| 833 | my_cred = kauth_cred_proc_ref(proc); |
| 834 | auid = my_cred->cr_audit.as_aia_p->ai_auid; |
| 835 | if (auid == AU_DEFAUDITID) |
| 836 | aumask = &audit_nae_mask; |
| 837 | else |
| 838 | aumask = &my_cred->cr_audit.as_mask; |
| 839 | kauth_cred_unref(&my_cred); |
| 840 | /* |
| 841 | * It's possible for coredump(9) generation to fail. Make sure that |
| 842 | * we handle this case correctly for preselection. |
| 843 | */ |
| 844 | if (errcode != 0) |
| 845 | sorf = AU_PRS_FAILURE; |
| 846 | else |
| 847 | sorf = AU_PRS_SUCCESS; |
| 848 | class = au_event_class(AUE_CORE); |
| 849 | if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 && |
| 850 | audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0) |
| 851 | return; |
| 852 | /* |
| 853 | * If we are interested in seeing this audit record, allocate it. |
| 854 | * Where possible coredump records should contain a pathname and arg32 |
| 855 | * (signal) tokens. |
| 856 | */ |
| 857 | uthread = curthread(); |
| 858 | ar = audit_new(AUE_CORE, proc, uthread); |
| 859 | if (path != NULL) { |
| 860 | pathp = &ar->k_ar.ar_arg_upath1; |
| 861 | *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); |
| 862 | if (audit_canon_path(vfs_context_cwd(vfs_context_current()), path, |
| 863 | *pathp)) |
| 864 | free(*pathp, M_AUDITPATH); |
| 865 | else |
| 866 | ARG_SET_VALID(ar, ARG_UPATH1); |
| 867 | } |
| 868 | ar->k_ar.ar_arg_signum = proc->p_sigacts->ps_sig; |
| 869 | ARG_SET_VALID(ar, ARG_SIGNUM); |
| 870 | if (errcode != 0) |
| 871 | ret = 1; |
| 872 | audit_commit(ar, errcode, ret); |
| 873 | } |
| 874 | #endif /* CONFIG_COREDUMP */ |
| 875 | #endif /* CONFIG_AUDIT */ |
| 876 | |