audit.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1999-2005 Apple Inc.
  5. * Copyright (c) 2006-2007, 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/eventhandler.h>
  41. #include <sys/file.h>
  42. #include <sys/filedesc.h>
  43. #include <sys/fcntl.h>
  44. #include <sys/ipc.h>
  45. #include <sys/jail.h>
  46. #include <sys/kernel.h>
  47. #include <sys/kthread.h>
  48. #include <sys/malloc.h>
  49. #include <sys/mount.h>
  50. #include <sys/namei.h>
  51. #include <sys/priv.h>
  52. #include <sys/proc.h>
  53. #include <sys/queue.h>
  54. #include <sys/socket.h>
  55. #include <sys/socketvar.h>
  56. #include <sys/protosw.h>
  57. #include <sys/domain.h>
  58. #include <sys/sysctl.h>
  59. #include <sys/sysproto.h>
  60. #include <sys/sysent.h>
  61. #include <sys/systm.h>
  62. #include <sys/ucred.h>
  63. #include <sys/uio.h>
  64. #include <sys/un.h>
  65. #include <sys/unistd.h>
  66. #include <sys/vnode.h>
  67. #include <bsm/audit.h>
  68. #include <bsm/audit_internal.h>
  69. #include <bsm/audit_kevents.h>
  70. #include <netinet/in.h>
  71. #include <netinet/in_pcb.h>
  72. #include <security/audit/audit.h>
  73. #include <security/audit/audit_private.h>
  74. #include <vm/uma.h>
  75. FEATURE(audit, "BSM audit support");
  76. static uma_zone_t audit_record_zone;
  77. static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
  78. MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
  79. MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
  80. MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
  81. MALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
  82. static SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  83. "TrustedBSD audit controls");
  84. /*
  85. * Audit control settings that are set/read by system calls and are hence
  86. * non-static.
  87. *
  88. * Define the audit control flags.
  89. */
  90. int audit_trail_enabled;
  91. int audit_trail_suspended;
  92. #ifdef KDTRACE_HOOKS
  93. u_int audit_dtrace_enabled;
  94. #endif
  95. bool __read_frequently audit_syscalls_enabled;
  96. /*
  97. * Flags controlling behavior in low storage situations. Should we panic if
  98. * a write fails? Should we fail stop if we're out of disk space?
  99. */
  100. int audit_panic_on_write_fail;
  101. int audit_fail_stop;
  102. int audit_argv;
  103. int audit_arge;
  104. /*
  105. * Are we currently "failing stop" due to out of disk space?
  106. */
  107. int audit_in_failure;
  108. /*
  109. * Global audit statistics.
  110. */
  111. struct audit_fstat audit_fstat;
  112. /*
  113. * Preselection mask for non-attributable events.
  114. */
  115. struct au_mask audit_nae_mask;
  116. /*
  117. * Mutex to protect global variables shared between various threads and
  118. * processes.
  119. */
  120. struct mtx audit_mtx;
  121. /*
  122. * Queue of audit records ready for delivery to disk. We insert new records
  123. * at the tail, and remove records from the head. Also, a count of the
  124. * number of records used for checking queue depth. In addition, a counter
  125. * of records that we have allocated but are not yet in the queue, which is
  126. * needed to estimate the total size of the combined set of records
  127. * outstanding in the system.
  128. */
  129. struct kaudit_queue audit_q;
  130. int audit_q_len;
  131. int audit_pre_q_len;
  132. /*
  133. * Audit queue control settings (minimum free, low/high water marks, etc.)
  134. */
  135. struct au_qctrl audit_qctrl;
  136. /*
  137. * Condition variable to signal to the worker that it has work to do: either
  138. * new records are in the queue, or a log replacement is taking place.
  139. */
  140. struct cv audit_worker_cv;
  141. /*
  142. * Condition variable to flag when crossing the low watermark, meaning that
  143. * threads blocked due to hitting the high watermark can wake up and continue
  144. * to commit records.
  145. */
  146. struct cv audit_watermark_cv;
  147. /*
  148. * Condition variable for auditing threads wait on when in fail-stop mode.
  149. * Threads wait on this CV forever (and ever), never seeing the light of day
  150. * again.
  151. */
  152. static struct cv audit_fail_cv;
  153. /*
  154. * Optional DTrace audit provider support: function pointers for preselection
  155. * and commit events.
  156. */
  157. #ifdef KDTRACE_HOOKS
  158. void *(*dtaudit_hook_preselect)(au_id_t auid, au_event_t event,
  159. au_class_t class);
  160. int (*dtaudit_hook_commit)(struct kaudit_record *kar, au_id_t auid,
  161. au_event_t event, au_class_t class, int sorf);
  162. void (*dtaudit_hook_bsm)(struct kaudit_record *kar, au_id_t auid,
  163. au_event_t event, au_class_t class, int sorf,
  164. void *bsm_data, size_t bsm_lenlen);
  165. #endif
  166. /*
  167. * Kernel audit information. This will store the current audit address
  168. * or host information that the kernel will use when it's generating
  169. * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2)
  170. * command.
  171. */
  172. static struct auditinfo_addr audit_kinfo;
  173. static struct rwlock audit_kinfo_lock;
  174. #define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \
  175. "audit_kinfo_lock")
  176. #define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock)
  177. #define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock)
  178. #define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock)
  179. #define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock)
  180. /*
  181. * Check various policies to see if we should enable system-call audit hooks.
  182. * Note that despite the mutex being held, we want to assign a value exactly
  183. * once, as checks of the flag are performed lock-free for performance
  184. * reasons. The mutex is used to get a consistent snapshot of policy state --
  185. * e.g., safely accessing the two audit_trail flags.
  186. */
  187. void
  188. audit_syscalls_enabled_update(void)
  189. {
  190. mtx_lock(&audit_mtx);
  191. #ifdef KDTRACE_HOOKS
  192. if (audit_dtrace_enabled)
  193. audit_syscalls_enabled = true;
  194. else {
  195. #endif
  196. if (audit_trail_enabled && !audit_trail_suspended)
  197. audit_syscalls_enabled = true;
  198. else
  199. audit_syscalls_enabled = false;
  200. #ifdef KDTRACE_HOOKS
  201. }
  202. #endif
  203. mtx_unlock(&audit_mtx);
  204. }
  205. void
  206. audit_set_kinfo(struct auditinfo_addr *ak)
  207. {
  208. KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
  209. ak->ai_termid.at_type == AU_IPv6,
  210. ("audit_set_kinfo: invalid address type"));
  211. KINFO_WLOCK();
  212. audit_kinfo = *ak;
  213. KINFO_WUNLOCK();
  214. }
  215. void
  216. audit_get_kinfo(struct auditinfo_addr *ak)
  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. KINFO_RLOCK();
  222. *ak = audit_kinfo;
  223. KINFO_RUNLOCK();
  224. }
  225. /*
  226. * Construct an audit record for the passed thread.
  227. */
  228. static int
  229. audit_record_ctor(void *mem, int size, void *arg, int flags)
  230. {
  231. struct kaudit_record *ar;
  232. struct thread *td;
  233. struct ucred *cred;
  234. struct prison *pr;
  235. KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
  236. td = arg;
  237. ar = mem;
  238. bzero(ar, sizeof(*ar));
  239. ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
  240. nanotime(&ar->k_ar.ar_starttime);
  241. /*
  242. * Export the subject credential.
  243. */
  244. cred = td->td_ucred;
  245. cru2x(cred, &ar->k_ar.ar_subj_cred);
  246. ar->k_ar.ar_subj_ruid = cred->cr_ruid;
  247. ar->k_ar.ar_subj_rgid = cred->cr_rgid;
  248. ar->k_ar.ar_subj_egid = cred->cr_groups[0];
  249. ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
  250. ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
  251. ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
  252. ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
  253. ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
  254. /*
  255. * If this process is jailed, make sure we capture the name of the
  256. * jail so we can use it to generate a zonename token when we covert
  257. * this record to BSM.
  258. */
  259. if (jailed(cred)) {
  260. pr = cred->cr_prison;
  261. (void) strlcpy(ar->k_ar.ar_jailname, pr->pr_name,
  262. sizeof(ar->k_ar.ar_jailname));
  263. } else
  264. ar->k_ar.ar_jailname[0] = '\0';
  265. return (0);
  266. }
  267. static void
  268. audit_record_dtor(void *mem, int size, void *arg)
  269. {
  270. struct kaudit_record *ar;
  271. KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
  272. ar = mem;
  273. if (ar->k_ar.ar_arg_upath1 != NULL)
  274. free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
  275. if (ar->k_ar.ar_arg_upath2 != NULL)
  276. free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
  277. if (ar->k_ar.ar_arg_text != NULL)
  278. free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
  279. if (ar->k_udata != NULL)
  280. free(ar->k_udata, M_AUDITDATA);
  281. if (ar->k_ar.ar_arg_argv != NULL)
  282. free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
  283. if (ar->k_ar.ar_arg_envv != NULL)
  284. free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
  285. if (ar->k_ar.ar_arg_groups.gidset != NULL)
  286. free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
  287. }
  288. /*
  289. * Initialize the Audit subsystem: configuration state, work queue,
  290. * synchronization primitives, worker thread, and trigger device node. Also
  291. * call into the BSM assembly code to initialize it.
  292. */
  293. static void
  294. audit_init(void)
  295. {
  296. audit_trail_enabled = 0;
  297. audit_trail_suspended = 0;
  298. audit_syscalls_enabled = false;
  299. audit_panic_on_write_fail = 0;
  300. audit_fail_stop = 0;
  301. audit_in_failure = 0;
  302. audit_argv = 0;
  303. audit_arge = 0;
  304. audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */
  305. audit_fstat.af_currsz = 0;
  306. audit_nae_mask.am_success = 0;
  307. audit_nae_mask.am_failure = 0;
  308. TAILQ_INIT(&audit_q);
  309. audit_q_len = 0;
  310. audit_pre_q_len = 0;
  311. audit_qctrl.aq_hiwater = AQ_HIWATER;
  312. audit_qctrl.aq_lowater = AQ_LOWATER;
  313. audit_qctrl.aq_bufsz = AQ_BUFSZ;
  314. audit_qctrl.aq_minfree = AU_FS_MINFREE;
  315. audit_kinfo.ai_termid.at_type = AU_IPv4;
  316. audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
  317. mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
  318. KINFO_LOCK_INIT();
  319. cv_init(&audit_worker_cv, "audit_worker_cv");
  320. cv_init(&audit_watermark_cv, "audit_watermark_cv");
  321. cv_init(&audit_fail_cv, "audit_fail_cv");
  322. audit_record_zone = uma_zcreate("audit_record",
  323. sizeof(struct kaudit_record), audit_record_ctor,
  324. audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
  325. /* First initialisation of audit_syscalls_enabled. */
  326. audit_syscalls_enabled_update();
  327. /* Initialize the BSM audit subsystem. */
  328. kau_init();
  329. audit_trigger_init();
  330. /* Register shutdown handler. */
  331. EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
  332. SHUTDOWN_PRI_FIRST);
  333. /* Start audit worker thread. */
  334. audit_worker_init();
  335. }
  336. SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
  337. /*
  338. * Drain the audit queue and close the log at shutdown. Note that this can
  339. * be called both from the system shutdown path and also from audit
  340. * configuration syscalls, so 'arg' and 'howto' are ignored.
  341. *
  342. * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to
  343. * drain before returning, which could lead to lost records on shutdown.
  344. */
  345. void
  346. audit_shutdown(void *arg, int howto)
  347. {
  348. audit_rotate_vnode(NULL, NULL);
  349. }
  350. /*
  351. * Return the current thread's audit record, if any.
  352. */
  353. struct kaudit_record *
  354. currecord(void)
  355. {
  356. return (curthread->td_ar);
  357. }
  358. /*
  359. * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
  360. * pre_q space, suspending the system call until there is room?
  361. */
  362. struct kaudit_record *
  363. audit_new(int event, struct thread *td)
  364. {
  365. struct kaudit_record *ar;
  366. /*
  367. * Note: the number of outstanding uncommitted audit records is
  368. * limited to the number of concurrent threads servicing system calls
  369. * in the kernel.
  370. */
  371. ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
  372. ar->k_ar.ar_event = event;
  373. mtx_lock(&audit_mtx);
  374. audit_pre_q_len++;
  375. mtx_unlock(&audit_mtx);
  376. return (ar);
  377. }
  378. void
  379. audit_free(struct kaudit_record *ar)
  380. {
  381. uma_zfree(audit_record_zone, ar);
  382. }
  383. void
  384. audit_commit(struct kaudit_record *ar, int error, int retval)
  385. {
  386. au_event_t event;
  387. au_class_t class;
  388. au_id_t auid;
  389. int sorf;
  390. struct au_mask *aumask;
  391. if (ar == NULL)
  392. return;
  393. ar->k_ar.ar_errno = error;
  394. ar->k_ar.ar_retval = retval;
  395. nanotime(&ar->k_ar.ar_endtime);
  396. /*
  397. * Decide whether to commit the audit record by checking the error
  398. * value from the system call and using the appropriate audit mask.
  399. */
  400. if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
  401. aumask = &audit_nae_mask;
  402. else
  403. aumask = &ar->k_ar.ar_subj_amask;
  404. if (error)
  405. sorf = AU_PRS_FAILURE;
  406. else
  407. sorf = AU_PRS_SUCCESS;
  408. /*
  409. * syscalls.master sometimes contains a prototype event number, which
  410. * we will transform into a more specific event number now that we
  411. * have more complete information gathered during the system call.
  412. */
  413. switch(ar->k_ar.ar_event) {
  414. case AUE_OPEN_RWTC:
  415. ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
  416. ar->k_ar.ar_arg_fflags, error);
  417. break;
  418. case AUE_OPENAT_RWTC:
  419. ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
  420. ar->k_ar.ar_arg_fflags, error);
  421. break;
  422. case AUE_SYSCTL:
  423. ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
  424. ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
  425. break;
  426. case AUE_AUDITON:
  427. /* Convert the auditon() command to an event. */
  428. ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
  429. break;
  430. case AUE_MSGSYS:
  431. if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
  432. ar->k_ar.ar_event =
  433. audit_msgsys_to_event(ar->k_ar.ar_arg_svipc_which);
  434. break;
  435. case AUE_SEMSYS:
  436. if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
  437. ar->k_ar.ar_event =
  438. audit_semsys_to_event(ar->k_ar.ar_arg_svipc_which);
  439. break;
  440. case AUE_SHMSYS:
  441. if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
  442. ar->k_ar.ar_event =
  443. audit_shmsys_to_event(ar->k_ar.ar_arg_svipc_which);
  444. break;
  445. }
  446. auid = ar->k_ar.ar_subj_auid;
  447. event = ar->k_ar.ar_event;
  448. class = au_event_class(event);
  449. ar->k_ar_commit |= AR_COMMIT_KERNEL;
  450. if (au_preselect(event, class, aumask, sorf) != 0)
  451. ar->k_ar_commit |= AR_PRESELECT_TRAIL;
  452. if (audit_pipe_preselect(auid, event, class, sorf,
  453. ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
  454. ar->k_ar_commit |= AR_PRESELECT_PIPE;
  455. #ifdef KDTRACE_HOOKS
  456. /*
  457. * Expose the audit record to DTrace, both to allow the "commit" probe
  458. * to fire if it's desirable, and also to allow a decision to be made
  459. * about later firing with BSM in the audit worker.
  460. */
  461. if (dtaudit_hook_commit != NULL) {
  462. if (dtaudit_hook_commit(ar, auid, event, class, sorf) != 0)
  463. ar->k_ar_commit |= AR_PRESELECT_DTRACE;
  464. }
  465. #endif
  466. if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
  467. AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE |
  468. AR_PRESELECT_DTRACE)) == 0) {
  469. mtx_lock(&audit_mtx);
  470. audit_pre_q_len--;
  471. mtx_unlock(&audit_mtx);
  472. audit_free(ar);
  473. return;
  474. }
  475. /*
  476. * Note: it could be that some records initiated while audit was
  477. * enabled should still be committed?
  478. *
  479. * NB: The check here is not for audit_syscalls because any
  480. * DTrace-related obligations have been fulfilled above -- we're just
  481. * down to the trail and pipes now.
  482. */
  483. mtx_lock(&audit_mtx);
  484. if (audit_trail_suspended || !audit_trail_enabled) {
  485. audit_pre_q_len--;
  486. mtx_unlock(&audit_mtx);
  487. audit_free(ar);
  488. return;
  489. }
  490. /*
  491. * Constrain the number of committed audit records based on the
  492. * configurable parameter.
  493. */
  494. while (audit_q_len >= audit_qctrl.aq_hiwater)
  495. cv_wait(&audit_watermark_cv, &audit_mtx);
  496. TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
  497. audit_q_len++;
  498. audit_pre_q_len--;
  499. cv_signal(&audit_worker_cv);
  500. mtx_unlock(&audit_mtx);
  501. }
  502. /*
  503. * audit_syscall_enter() is called on entry to each system call. It is
  504. * responsible for deciding whether or not to audit the call (preselection),
  505. * and if so, allocating a per-thread audit record. audit_new() will fill in
  506. * basic thread/credential properties.
  507. *
  508. * This function will be entered only if audit_syscalls_enabled was set in the
  509. * macro wrapper for this function. It could be cleared by the time this
  510. * function runs, but that is an acceptable race.
  511. */
  512. void
  513. audit_syscall_enter(unsigned short code, struct thread *td)
  514. {
  515. struct au_mask *aumask;
  516. #ifdef KDTRACE_HOOKS
  517. void *dtaudit_state;
  518. #endif
  519. au_class_t class;
  520. au_event_t event;
  521. au_id_t auid;
  522. int record_needed;
  523. KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
  524. KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
  525. ("audit_syscall_enter: TDP_AUDITREC set"));
  526. /*
  527. * In FreeBSD, each ABI has its own system call table, and hence
  528. * mapping of system call codes to audit events. Convert the code to
  529. * an audit event identifier using the process system call table
  530. * reference. In Darwin, there's only one, so we use the global
  531. * symbol for the system call table. No audit record is generated
  532. * for bad system calls, as no operation has been performed.
  533. */
  534. if (code >= td->td_proc->p_sysent->sv_size)
  535. return;
  536. event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
  537. if (event == AUE_NULL)
  538. return;
  539. /*
  540. * Check which audit mask to use; either the kernel non-attributable
  541. * event mask or the process audit mask.
  542. */
  543. auid = td->td_ucred->cr_audit.ai_auid;
  544. if (auid == AU_DEFAUDITID)
  545. aumask = &audit_nae_mask;
  546. else
  547. aumask = &td->td_ucred->cr_audit.ai_mask;
  548. /*
  549. * Determine whether trail or pipe preselection would like an audit
  550. * record allocated for this system call.
  551. */
  552. class = au_event_class(event);
  553. if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
  554. /*
  555. * If we're out of space and need to suspend unprivileged
  556. * processes, do that here rather than trying to allocate
  557. * another audit record.
  558. *
  559. * Note: we might wish to be able to continue here in the
  560. * future, if the system recovers. That should be possible
  561. * by means of checking the condition in a loop around
  562. * cv_wait(). It might be desirable to reevaluate whether an
  563. * audit record is still required for this event by
  564. * re-calling au_preselect().
  565. */
  566. if (audit_in_failure &&
  567. priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
  568. cv_wait(&audit_fail_cv, &audit_mtx);
  569. panic("audit_failing_stop: thread continued");
  570. }
  571. record_needed = 1;
  572. } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
  573. record_needed = 1;
  574. } else {
  575. record_needed = 0;
  576. }
  577. /*
  578. * After audit trails and pipes have made their policy choices, DTrace
  579. * may request that records be generated as well. This is a slightly
  580. * complex affair, as the DTrace audit provider needs the audit
  581. * framework to maintain some state on the audit record, which has not
  582. * been allocated at the point where the decision has to be made.
  583. * This hook must run even if we are not changing the decision, as
  584. * DTrace may want to stick event state onto a record we were going to
  585. * produce due to the trail or pipes. The event state returned by the
  586. * DTrace provider must be safe without locks held between here and
  587. * below -- i.e., dtaudit_state must must refer to stable memory.
  588. */
  589. #ifdef KDTRACE_HOOKS
  590. dtaudit_state = NULL;
  591. if (dtaudit_hook_preselect != NULL) {
  592. dtaudit_state = dtaudit_hook_preselect(auid, event, class);
  593. if (dtaudit_state != NULL)
  594. record_needed = 1;
  595. }
  596. #endif
  597. /*
  598. * If a record is required, allocate it and attach it to the thread
  599. * for use throughout the system call. Also attach DTrace state if
  600. * required.
  601. *
  602. * XXXRW: If we decide to reference count the evname_elem underlying
  603. * dtaudit_state, we will need to free here if no record is allocated
  604. * or allocatable.
  605. */
  606. if (record_needed) {
  607. td->td_ar = audit_new(event, td);
  608. if (td->td_ar != NULL) {
  609. td->td_pflags |= TDP_AUDITREC;
  610. #ifdef KDTRACE_HOOKS
  611. td->td_ar->k_dtaudit_state = dtaudit_state;
  612. #endif
  613. }
  614. } else
  615. td->td_ar = NULL;
  616. }
  617. /*
  618. * audit_syscall_exit() is called from the return of every system call, or in
  619. * the event of exit1(), during the execution of exit1(). It is responsible
  620. * for committing the audit record, if any, along with return condition.
  621. */
  622. void
  623. audit_syscall_exit(int error, struct thread *td)
  624. {
  625. int retval;
  626. /*
  627. * Commit the audit record as desired; once we pass the record into
  628. * audit_commit(), the memory is owned by the audit subsystem. The
  629. * return value from the system call is stored on the user thread.
  630. * If there was an error, the return value is set to -1, imitating
  631. * the behavior of the cerror routine.
  632. */
  633. if (error)
  634. retval = -1;
  635. else
  636. retval = td->td_retval[0];
  637. audit_commit(td->td_ar, error, retval);
  638. td->td_ar = NULL;
  639. td->td_pflags &= ~TDP_AUDITREC;
  640. }
  641. void
  642. audit_cred_copy(struct ucred *src, struct ucred *dest)
  643. {
  644. bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
  645. }
  646. void
  647. audit_cred_destroy(struct ucred *cred)
  648. {
  649. }
  650. void
  651. audit_cred_init(struct ucred *cred)
  652. {
  653. bzero(&cred->cr_audit, sizeof(cred->cr_audit));
  654. }
  655. /*
  656. * Initialize audit information for the first kernel process (proc 0) and for
  657. * the first user process (init).
  658. */
  659. void
  660. audit_cred_kproc0(struct ucred *cred)
  661. {
  662. cred->cr_audit.ai_auid = AU_DEFAUDITID;
  663. cred->cr_audit.ai_termid.at_type = AU_IPv4;
  664. }
  665. void
  666. audit_cred_proc1(struct ucred *cred)
  667. {
  668. cred->cr_audit.ai_auid = AU_DEFAUDITID;
  669. cred->cr_audit.ai_termid.at_type = AU_IPv4;
  670. }
  671. void
  672. audit_thread_alloc(struct thread *td)
  673. {
  674. td->td_ar = NULL;
  675. }
  676. void
  677. audit_thread_free(struct thread *td)
  678. {
  679. KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
  680. KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
  681. ("audit_thread_free: TDP_AUDITREC set"));
  682. }
  683. void
  684. audit_proc_coredump(struct thread *td, char *path, int errcode)
  685. {
  686. struct kaudit_record *ar;
  687. struct au_mask *aumask;
  688. struct ucred *cred;
  689. au_class_t class;
  690. int ret, sorf;
  691. char **pathp;
  692. au_id_t auid;
  693. ret = 0;
  694. /*
  695. * Make sure we are using the correct preselection mask.
  696. */
  697. cred = td->td_ucred;
  698. auid = cred->cr_audit.ai_auid;
  699. if (auid == AU_DEFAUDITID)
  700. aumask = &audit_nae_mask;
  701. else
  702. aumask = &cred->cr_audit.ai_mask;
  703. /*
  704. * It's possible for coredump(9) generation to fail. Make sure that
  705. * we handle this case correctly for preselection.
  706. */
  707. if (errcode != 0)
  708. sorf = AU_PRS_FAILURE;
  709. else
  710. sorf = AU_PRS_SUCCESS;
  711. class = au_event_class(AUE_CORE);
  712. if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
  713. audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
  714. return;
  715. /*
  716. * If we are interested in seeing this audit record, allocate it.
  717. * Where possible coredump records should contain a pathname and arg32
  718. * (signal) tokens.
  719. */
  720. ar = audit_new(AUE_CORE, td);
  721. if (ar == NULL)
  722. return;
  723. if (path != NULL) {
  724. pathp = &ar->k_ar.ar_arg_upath1;
  725. *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
  726. audit_canon_path(td, AT_FDCWD, path, *pathp);
  727. ARG_SET_VALID(ar, ARG_UPATH1);
  728. }
  729. ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
  730. ARG_SET_VALID(ar, ARG_SIGNUM);
  731. if (errcode != 0)
  732. ret = 1;
  733. audit_commit(ar, errcode, ret);
  734. }