1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1999-2008 Apple Inc. 5 * Copyright (c) 2006-2008, 2016, 2018 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Portions of this software were developed by BAE Systems, the University of 9 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 10 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 11 * Computing (TC) research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of Apple Inc. ("Apple") nor the names of 22 * its contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS 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 APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 29 * 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, 33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/condvar.h> 40 #include <sys/conf.h> 41 #include <sys/file.h> 42 #include <sys/filedesc.h> 43 #include <sys/fcntl.h> 44 #include <sys/ipc.h> 45 #include <sys/kernel.h> 46 #include <sys/kthread.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/proc.h> 51 #include <sys/queue.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/protosw.h> 55 #include <sys/domain.h> 56 #include <sys/stdarg.h> 57 #include <sys/sx.h> 58 #include <sys/sysproto.h> 59 #include <sys/sysent.h> 60 #include <sys/systm.h> 61 #include <sys/ucred.h> 62 #include <sys/uio.h> 63 #include <sys/un.h> 64 #include <sys/unistd.h> 65 #include <sys/vnode.h> 66 67 #include <bsm/audit.h> 68 #include <bsm/audit_internal.h> 69 #include <bsm/audit_kevents.h> 70 71 #include <netinet/in.h> 72 #include <netinet/in_pcb.h> 73 74 #include <security/audit/audit.h> 75 #include <security/audit/audit_private.h> 76 77 #include <vm/uma.h> 78 79 /* 80 * Worker thread that will schedule disk I/O, etc. 81 */ 82 static struct proc *audit_thread; 83 84 /* 85 * audit_cred and audit_vp are the stored credential and vnode to use for 86 * active audit trail. They are protected by the audit worker lock, which 87 * will be held across all I/O and all rotation to prevent them from being 88 * replaced (rotated) while in use. The audit_file_rotate_wait flag is set 89 * when the kernel has delivered a trigger to auditd to rotate the trail, and 90 * is cleared when the next rotation takes place. It is also protected by 91 * the audit worker lock. 92 */ 93 static int audit_file_rotate_wait; 94 static struct ucred *audit_cred; 95 static struct vnode *audit_vp; 96 static off_t audit_size; 97 static struct sx audit_worker_lock; 98 99 #define AUDIT_WORKER_LOCK_INIT() sx_init(&audit_worker_lock, \ 100 "audit_worker_lock"); 101 #define AUDIT_WORKER_LOCK_ASSERT() sx_assert(&audit_worker_lock, \ 102 SA_XLOCKED) 103 #define AUDIT_WORKER_LOCK() sx_xlock(&audit_worker_lock) 104 #define AUDIT_WORKER_UNLOCK() sx_xunlock(&audit_worker_lock) 105 106 static void 107 audit_worker_sync_vp(struct vnode *vp, struct mount *mp, const char *fmt, ...) 108 { 109 struct mount *mp1; 110 int error; 111 va_list va; 112 113 va_start(va, fmt); 114 error = vn_start_write(vp, &mp1, 0); 115 if (error == 0) { 116 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY); 117 (void)VOP_FSYNC(vp, MNT_WAIT, curthread); 118 VOP_UNLOCK(vp); 119 vn_finished_write(mp1); 120 } 121 vfs_unbusy(mp); 122 vpanic(fmt, va); 123 va_end(va); 124 } 125 126 /* 127 * Write an audit record to a file, performed as the last stage after both 128 * preselection and BSM conversion. Both space management and write failures 129 * are handled in this function. 130 * 131 * No attempt is made to deal with possible failure to deliver a trigger to 132 * the audit daemon, since the message is asynchronous anyway. 133 */ 134 static void 135 audit_record_write(struct vnode *vp, struct ucred *cred, void *data, 136 size_t len) 137 { 138 static struct timeval last_lowspace_trigger; 139 static struct timeval last_fail; 140 static int cur_lowspace_trigger; 141 struct statfs *mnt_stat; 142 struct mount *mp; 143 int error; 144 static int cur_fail; 145 long temp; 146 147 AUDIT_WORKER_LOCK_ASSERT(); 148 149 if (vp == NULL) 150 return; 151 152 mp = vp->v_mount; 153 if (mp == NULL) { 154 error = EINVAL; 155 goto fail; 156 } 157 error = vfs_busy(mp, 0); 158 if (error != 0) { 159 mp = NULL; 160 goto fail; 161 } 162 mnt_stat = &mp->mnt_stat; 163 164 /* 165 * First, gather statistics on the audit log file and file system so 166 * that we know how we're doing on space. Consider failure of these 167 * operations to indicate a future inability to write to the file. 168 */ 169 error = VFS_STATFS(mp, mnt_stat); 170 if (error != 0) 171 goto fail; 172 173 /* 174 * We handle four different space-related limits: 175 * 176 * - A fixed (hard) limit on the minimum free blocks we require on 177 * the file system, and results in record loss, a trigger, and 178 * possible fail stop due to violating invariants. 179 * 180 * - An administrative (soft) limit, which when fallen below, results 181 * in the kernel notifying the audit daemon of low space. 182 * 183 * - An audit trail size limit, which when gone above, results in the 184 * kernel notifying the audit daemon that rotation is desired. 185 * 186 * - The total depth of the kernel audit record exceeding free space, 187 * which can lead to possible fail stop (with drain), in order to 188 * prevent violating invariants. Failure here doesn't halt 189 * immediately, but prevents new records from being generated. 190 * 191 * Possibly, the last of these should be handled differently, always 192 * allowing a full queue to be lost, rather than trying to prevent 193 * loss. 194 * 195 * First, handle the hard limit, which generates a trigger and may 196 * fail stop. This is handled in the same manner as ENOSPC from 197 * VOP_WRITE, and results in record loss. 198 */ 199 if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) { 200 error = ENOSPC; 201 goto fail_enospc; 202 } 203 204 /* 205 * Second, handle falling below the soft limit, if defined; we send 206 * the daemon a trigger and continue processing the record. Triggers 207 * are limited to 1/sec. 208 */ 209 if (audit_qctrl.aq_minfree != 0) { 210 temp = mnt_stat->f_blocks / (100 / audit_qctrl.aq_minfree); 211 if (mnt_stat->f_bfree < temp) { 212 if (ppsratecheck(&last_lowspace_trigger, 213 &cur_lowspace_trigger, 1)) { 214 (void)audit_send_trigger( 215 AUDIT_TRIGGER_LOW_SPACE); 216 printf("Warning: disk space low (< %d%% free) " 217 "on audit log file-system\n", 218 audit_qctrl.aq_minfree); 219 } 220 } 221 } 222 223 /* 224 * If the current file is getting full, generate a rotation trigger 225 * to the daemon. This is only approximate, which is fine as more 226 * records may be generated before the daemon rotates the file. 227 */ 228 if (audit_fstat.af_filesz != 0 && 229 audit_size >= audit_fstat.af_filesz * (audit_file_rotate_wait + 1)) { 230 AUDIT_WORKER_LOCK_ASSERT(); 231 232 audit_file_rotate_wait++; 233 (void)audit_send_trigger(AUDIT_TRIGGER_ROTATE_KERNEL); 234 } 235 236 /* 237 * If the estimated amount of audit data in the audit event queue 238 * (plus records allocated but not yet queued) has reached the amount 239 * of free space on the disk, then we need to go into an audit fail 240 * stop state, in which we do not permit the allocation/committing of 241 * any new audit records. We continue to process records but don't 242 * allow any activities that might generate new records. In the 243 * future, we might want to detect when space is available again and 244 * allow operation to continue, but this behavior is sufficient to 245 * meet fail stop requirements in CAPP. 246 */ 247 if (audit_fail_stop) { 248 if ((unsigned long)((audit_q_len + audit_pre_q_len + 1) * 249 MAX_AUDIT_RECORD_SIZE) / mnt_stat->f_bsize >= 250 (unsigned long)(mnt_stat->f_bfree)) { 251 if (ppsratecheck(&last_fail, &cur_fail, 1)) 252 printf("audit_record_write: free space " 253 "below size of audit queue, failing " 254 "stop\n"); 255 audit_in_failure = 1; 256 } else if (audit_in_failure) { 257 /* 258 * Note: if we want to handle recovery, this is the 259 * spot to do it: unset audit_in_failure, and issue a 260 * wakeup on the cv. 261 */ 262 } 263 } 264 265 error = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE, 266 IO_APPEND|IO_UNIT, cred, NULL, NULL, curthread); 267 if (error == ENOSPC) 268 goto fail_enospc; 269 else if (error) 270 goto fail; 271 AUDIT_WORKER_LOCK_ASSERT(); 272 audit_size += len; 273 274 /* 275 * Catch completion of a queue drain here; if we're draining and the 276 * queue is now empty, fail stop. That audit_fail_stop is implicitly 277 * true, since audit_in_failure can only be set of audit_fail_stop is 278 * set. 279 * 280 * Note: if we handle recovery from audit_in_failure, then we need to 281 * make panic here conditional. 282 */ 283 if (audit_in_failure) { 284 if (audit_q_len == 0 && audit_pre_q_len == 0) { 285 audit_worker_sync_vp(vp, mp, 286 "Audit store overflow; record queue drained."); 287 } 288 } 289 290 vfs_unbusy(mp); 291 return; 292 293 fail_enospc: 294 /* 295 * ENOSPC is considered a special case with respect to failures, as 296 * this can reflect either our preemptive detection of insufficient 297 * space, or ENOSPC returned by the vnode write call. 298 */ 299 if (audit_fail_stop) { 300 audit_worker_sync_vp(vp, mp, 301 "Audit log space exhausted and fail-stop set."); 302 } 303 (void)audit_send_trigger(AUDIT_TRIGGER_NO_SPACE); 304 audit_trail_suspended = 1; 305 audit_syscalls_enabled_update(); 306 307 /* FALLTHROUGH */ 308 fail: 309 /* 310 * We have failed to write to the file, so the current record is 311 * lost, which may require an immediate system halt. 312 */ 313 if (audit_panic_on_write_fail) { 314 audit_worker_sync_vp(vp, mp, 315 "audit_worker: write error %d\n", error); 316 } else if (ppsratecheck(&last_fail, &cur_fail, 1)) 317 printf("audit_worker: write error %d\n", error); 318 if (mp != NULL) 319 vfs_unbusy(mp); 320 } 321 322 /* 323 * Given a kernel audit record, process as required. Kernel audit records 324 * are converted to one, or possibly two, BSM records, depending on whether 325 * there is a user audit record present also. Kernel records need be 326 * converted to BSM before they can be written out. Both types will be 327 * written to disk, and audit pipes. 328 */ 329 static void 330 audit_worker_process_record(struct kaudit_record *ar) 331 { 332 struct au_record *bsm; 333 au_class_t class; 334 au_event_t event; 335 au_id_t auid; 336 int error, sorf; 337 int locked; 338 339 /* 340 * We hold the audit worker lock over both writes, if there are two, 341 * so that the two records won't be split across a rotation and end 342 * up in two different trail files. 343 */ 344 if (((ar->k_ar_commit & AR_COMMIT_USER) && 345 (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) || 346 (ar->k_ar_commit & AR_PRESELECT_TRAIL)) { 347 AUDIT_WORKER_LOCK(); 348 locked = 1; 349 } else 350 locked = 0; 351 352 /* 353 * First, handle the user record, if any: commit to the system trail 354 * and audit pipes as selected. 355 */ 356 if ((ar->k_ar_commit & AR_COMMIT_USER) && 357 (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) { 358 AUDIT_WORKER_LOCK_ASSERT(); 359 audit_record_write(audit_vp, audit_cred, ar->k_udata, 360 ar->k_ulen); 361 } 362 363 if ((ar->k_ar_commit & AR_COMMIT_USER) && 364 (ar->k_ar_commit & AR_PRESELECT_USER_PIPE)) 365 audit_pipe_submit_user(ar->k_udata, ar->k_ulen); 366 367 if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) || 368 ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 && 369 (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0 && 370 (ar->k_ar_commit & AR_PRESELECT_DTRACE) == 0)) 371 goto out; 372 373 auid = ar->k_ar.ar_subj_auid; 374 event = ar->k_ar.ar_event; 375 class = au_event_class(event); 376 if (ar->k_ar.ar_errno == 0) 377 sorf = AU_PRS_SUCCESS; 378 else 379 sorf = AU_PRS_FAILURE; 380 381 error = kaudit_to_bsm(ar, &bsm); 382 switch (error) { 383 case BSM_NOAUDIT: 384 goto out; 385 386 case BSM_FAILURE: 387 printf("audit_worker_process_record: BSM_FAILURE\n"); 388 goto out; 389 390 case BSM_SUCCESS: 391 break; 392 393 default: 394 panic("kaudit_to_bsm returned %d", error); 395 } 396 397 if (ar->k_ar_commit & AR_PRESELECT_TRAIL) { 398 AUDIT_WORKER_LOCK_ASSERT(); 399 audit_record_write(audit_vp, audit_cred, bsm->data, bsm->len); 400 } 401 402 if (ar->k_ar_commit & AR_PRESELECT_PIPE) 403 audit_pipe_submit(auid, event, class, sorf, 404 ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data, 405 bsm->len); 406 407 #ifdef KDTRACE_HOOKS 408 /* 409 * Version of the dtaudit commit hook that accepts BSM. 410 */ 411 if (ar->k_ar_commit & AR_PRESELECT_DTRACE) { 412 if (dtaudit_hook_bsm != NULL) 413 dtaudit_hook_bsm(ar, auid, event, class, sorf, 414 bsm->data, bsm->len); 415 } 416 #endif 417 418 kau_free(bsm); 419 out: 420 if (locked) 421 AUDIT_WORKER_UNLOCK(); 422 } 423 424 /* 425 * The audit_worker thread is responsible for watching the event queue, 426 * dequeueing records, converting them to BSM format, and committing them to 427 * disk. In order to minimize lock thrashing, records are dequeued in sets 428 * to a thread-local work queue. 429 * 430 * Note: this means that the effect bound on the size of the pending record 431 * queue is 2x the length of the global queue. 432 */ 433 static void 434 audit_worker(void *arg) 435 { 436 struct kaudit_queue ar_worklist; 437 struct kaudit_record *ar; 438 int lowater_signal; 439 440 TAILQ_INIT(&ar_worklist); 441 mtx_lock(&audit_mtx); 442 while (1) { 443 mtx_assert(&audit_mtx, MA_OWNED); 444 445 /* 446 * Wait for a record. 447 */ 448 while (TAILQ_EMPTY(&audit_q)) 449 cv_wait(&audit_worker_cv, &audit_mtx); 450 451 /* 452 * If there are records in the global audit record queue, 453 * transfer them to a thread-local queue and process them 454 * one by one. If we cross the low watermark threshold, 455 * signal any waiting processes that they may wake up and 456 * continue generating records. 457 */ 458 lowater_signal = 0; 459 while ((ar = TAILQ_FIRST(&audit_q))) { 460 TAILQ_REMOVE(&audit_q, ar, k_q); 461 audit_q_len--; 462 if (audit_q_len == audit_qctrl.aq_lowater) 463 lowater_signal++; 464 TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q); 465 } 466 if (lowater_signal) 467 cv_broadcast(&audit_watermark_cv); 468 469 mtx_unlock(&audit_mtx); 470 while ((ar = TAILQ_FIRST(&ar_worklist))) { 471 TAILQ_REMOVE(&ar_worklist, ar, k_q); 472 audit_worker_process_record(ar); 473 audit_free(ar); 474 } 475 mtx_lock(&audit_mtx); 476 } 477 } 478 479 /* 480 * audit_rotate_vnode() is called by a user or kernel thread to configure or 481 * de-configure auditing on a vnode. The arguments are the replacement 482 * credential (referenced) and vnode (referenced and opened) to substitute 483 * for the current credential and vnode, if any. If either is set to NULL, 484 * both should be NULL, and this is used to indicate that audit is being 485 * disabled. Any previous cred/vnode will be closed and freed. We re-enable 486 * generating rotation requests to auditd. 487 */ 488 void 489 audit_rotate_vnode(struct ucred *cred, struct vnode *vp) 490 { 491 struct ucred *old_audit_cred; 492 struct vnode *old_audit_vp; 493 struct vattr vattr; 494 495 KASSERT((cred != NULL && vp != NULL) || (cred == NULL && vp == NULL), 496 ("audit_rotate_vnode: cred %p vp %p", cred, vp)); 497 498 if (vp != NULL) { 499 vn_lock(vp, LK_SHARED | LK_RETRY); 500 if (VOP_GETATTR(vp, &vattr, cred) != 0) 501 vattr.va_size = 0; 502 VOP_UNLOCK(vp); 503 } else { 504 vattr.va_size = 0; 505 } 506 507 /* 508 * Rotate the vnode/cred, and clear the rotate flag so that we will 509 * send a rotate trigger if the new file fills. 510 */ 511 AUDIT_WORKER_LOCK(); 512 old_audit_cred = audit_cred; 513 old_audit_vp = audit_vp; 514 audit_cred = cred; 515 audit_vp = vp; 516 audit_size = vattr.va_size; 517 audit_file_rotate_wait = 0; 518 audit_trail_enabled = (audit_vp != NULL); 519 audit_syscalls_enabled_update(); 520 AUDIT_WORKER_UNLOCK(); 521 522 /* 523 * If there was an old vnode/credential, close and free. 524 */ 525 if (old_audit_vp != NULL) { 526 vn_close(old_audit_vp, AUDIT_CLOSE_FLAGS, old_audit_cred, 527 curthread); 528 crfree(old_audit_cred); 529 } 530 } 531 532 void 533 audit_worker_init(void) 534 { 535 int error; 536 537 AUDIT_WORKER_LOCK_INIT(); 538 error = kproc_create(audit_worker, NULL, &audit_thread, RFHIGHPID, 539 0, "audit"); 540 if (error) 541 panic("audit_worker_init: kproc_create returned %d", error); 542 } 543