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