audit_worker.c 16 KB

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