audit_pipe.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*-
  2. * Copyright (c) 2006 Robert N. M. Watson
  3. * Copyright (c) 2008-2009 Apple, Inc.
  4. * All rights reserved.
  5. *
  6. * This software was developed by Robert Watson for the TrustedBSD Project.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/condvar.h>
  31. #include <sys/conf.h>
  32. #include <sys/eventhandler.h>
  33. #include <sys/filio.h>
  34. #include <sys/kernel.h>
  35. #include <sys/lock.h>
  36. #include <sys/malloc.h>
  37. #include <sys/mutex.h>
  38. #include <sys/poll.h>
  39. #include <sys/proc.h>
  40. #include <sys/queue.h>
  41. #include <sys/rwlock.h>
  42. #include <sys/selinfo.h>
  43. #include <sys/sigio.h>
  44. #include <sys/signal.h>
  45. #include <sys/signalvar.h>
  46. #include <sys/sx.h>
  47. #include <sys/systm.h>
  48. #include <sys/uio.h>
  49. #include <security/audit/audit.h>
  50. #include <security/audit/audit_ioctl.h>
  51. #include <security/audit/audit_private.h>
  52. /*
  53. * Implementation of a clonable special device providing a live stream of BSM
  54. * audit data. Consumers receive a "tee" of the system audit trail by
  55. * default, but may also define alternative event selections using ioctls.
  56. * This interface provides unreliable but timely access to audit events.
  57. * Consumers should be very careful to avoid introducing event cycles.
  58. */
  59. /*
  60. * Memory types.
  61. */
  62. static MALLOC_DEFINE(M_AUDIT_PIPE, "audit_pipe", "Audit pipes");
  63. static MALLOC_DEFINE(M_AUDIT_PIPE_ENTRY, "audit_pipeent",
  64. "Audit pipe entries and buffers");
  65. static MALLOC_DEFINE(M_AUDIT_PIPE_PRESELECT, "audit_pipe_presel",
  66. "Audit pipe preselection structure");
  67. /*
  68. * Audit pipe buffer parameters.
  69. */
  70. #define AUDIT_PIPE_QLIMIT_DEFAULT (128)
  71. #define AUDIT_PIPE_QLIMIT_MIN (1)
  72. #define AUDIT_PIPE_QLIMIT_MAX (1024)
  73. /*
  74. * Description of an entry in an audit_pipe.
  75. */
  76. struct audit_pipe_entry {
  77. void *ape_record;
  78. u_int ape_record_len;
  79. TAILQ_ENTRY(audit_pipe_entry) ape_queue;
  80. };
  81. /*
  82. * Audit pipes allow processes to express "interest" in the set of records
  83. * that are delivered via the pipe. They do this in a similar manner to the
  84. * mechanism for audit trail configuration, by expressing two global masks,
  85. * and optionally expressing per-auid masks. The following data structure is
  86. * the per-auid mask description. The global state is stored in the audit
  87. * pipe data structure.
  88. *
  89. * We may want to consider a more space/time-efficient data structure once
  90. * usage patterns for per-auid specifications are clear.
  91. */
  92. struct audit_pipe_preselect {
  93. au_id_t app_auid;
  94. au_mask_t app_mask;
  95. TAILQ_ENTRY(audit_pipe_preselect) app_list;
  96. };
  97. /*
  98. * Description of an individual audit_pipe. Consists largely of a bounded
  99. * length queue.
  100. */
  101. #define AUDIT_PIPE_ASYNC 0x00000001
  102. #define AUDIT_PIPE_NBIO 0x00000002
  103. struct audit_pipe {
  104. u_int ap_flags;
  105. struct selinfo ap_selinfo;
  106. struct sigio *ap_sigio;
  107. /*
  108. * Per-pipe mutex protecting most fields in this data structure.
  109. */
  110. struct mtx ap_mtx;
  111. /*
  112. * Per-pipe sleep lock serializing user-generated reads and flushes.
  113. * uiomove() is called to copy out the current head record's data
  114. * while the record remains in the queue, so we prevent other threads
  115. * from removing it using this lock.
  116. */
  117. struct sx ap_sx;
  118. /*
  119. * Condition variable to signal when data has been delivered to a
  120. * pipe.
  121. */
  122. struct cv ap_cv;
  123. /*
  124. * Various queue-reated variables: qlen and qlimit are a count of
  125. * records in the queue; qbyteslen is the number of bytes of data
  126. * across all records, and qoffset is the amount read so far of the
  127. * first record in the queue. The number of bytes available for
  128. * reading in the queue is qbyteslen - qoffset.
  129. */
  130. u_int ap_qlen;
  131. u_int ap_qlimit;
  132. u_int ap_qbyteslen;
  133. u_int ap_qoffset;
  134. /*
  135. * Per-pipe operation statistics.
  136. */
  137. u_int64_t ap_inserts; /* Records added. */
  138. u_int64_t ap_reads; /* Records read. */
  139. u_int64_t ap_drops; /* Records dropped. */
  140. /*
  141. * Fields relating to pipe interest: global masks for unmatched
  142. * processes (attributable, non-attributable), and a list of specific
  143. * interest specifications by auid.
  144. */
  145. int ap_preselect_mode;
  146. au_mask_t ap_preselect_flags;
  147. au_mask_t ap_preselect_naflags;
  148. TAILQ_HEAD(, audit_pipe_preselect) ap_preselect_list;
  149. /*
  150. * Current pending record list. Protected by a combination of ap_mtx
  151. * and ap_sx. Note particularly that *both* locks are required to
  152. * remove a record from the head of the queue, as an in-progress read
  153. * may sleep while copying and therefore cannot hold ap_mtx.
  154. */
  155. TAILQ_HEAD(, audit_pipe_entry) ap_queue;
  156. /*
  157. * Global pipe list.
  158. */
  159. TAILQ_ENTRY(audit_pipe) ap_list;
  160. };
  161. #define AUDIT_PIPE_LOCK(ap) mtx_lock(&(ap)->ap_mtx)
  162. #define AUDIT_PIPE_LOCK_ASSERT(ap) mtx_assert(&(ap)->ap_mtx, MA_OWNED)
  163. #define AUDIT_PIPE_LOCK_DESTROY(ap) mtx_destroy(&(ap)->ap_mtx)
  164. #define AUDIT_PIPE_LOCK_INIT(ap) mtx_init(&(ap)->ap_mtx, \
  165. "audit_pipe_mtx", NULL, MTX_DEF)
  166. #define AUDIT_PIPE_UNLOCK(ap) mtx_unlock(&(ap)->ap_mtx)
  167. #define AUDIT_PIPE_MTX(ap) (&(ap)->ap_mtx)
  168. #define AUDIT_PIPE_SX_LOCK_DESTROY(ap) sx_destroy(&(ap)->ap_sx)
  169. #define AUDIT_PIPE_SX_LOCK_INIT(ap) sx_init(&(ap)->ap_sx, "audit_pipe_sx")
  170. #define AUDIT_PIPE_SX_XLOCK_ASSERT(ap) sx_assert(&(ap)->ap_sx, SA_XLOCKED)
  171. #define AUDIT_PIPE_SX_XLOCK_SIG(ap) sx_xlock_sig(&(ap)->ap_sx)
  172. #define AUDIT_PIPE_SX_XUNLOCK(ap) sx_xunlock(&(ap)->ap_sx)
  173. /*
  174. * Global list of audit pipes, rwlock to protect it. Individual record
  175. * queues on pipes are protected by per-pipe locks; these locks synchronize
  176. * between threads walking the list to deliver to individual pipes and add/
  177. * remove of pipes, and are mostly acquired for read.
  178. */
  179. static TAILQ_HEAD(, audit_pipe) audit_pipe_list;
  180. static struct rwlock audit_pipe_lock;
  181. #define AUDIT_PIPE_LIST_LOCK_INIT() rw_init(&audit_pipe_lock, \
  182. "audit_pipe_list_lock")
  183. #define AUDIT_PIPE_LIST_LOCK_DESTROY() rw_destroy(&audit_pipe_lock)
  184. #define AUDIT_PIPE_LIST_RLOCK() rw_rlock(&audit_pipe_lock)
  185. #define AUDIT_PIPE_LIST_RUNLOCK() rw_runlock(&audit_pipe_lock)
  186. #define AUDIT_PIPE_LIST_WLOCK() rw_wlock(&audit_pipe_lock)
  187. #define AUDIT_PIPE_LIST_WLOCK_ASSERT() rw_assert(&audit_pipe_lock, \
  188. RA_WLOCKED)
  189. #define AUDIT_PIPE_LIST_WUNLOCK() rw_wunlock(&audit_pipe_lock)
  190. /*
  191. * Audit pipe device.
  192. */
  193. static struct cdev *audit_pipe_dev;
  194. #define AUDIT_PIPE_NAME "auditpipe"
  195. /*
  196. * Special device methods and definition.
  197. */
  198. static d_open_t audit_pipe_open;
  199. static d_read_t audit_pipe_read;
  200. static d_ioctl_t audit_pipe_ioctl;
  201. static d_poll_t audit_pipe_poll;
  202. static d_kqfilter_t audit_pipe_kqfilter;
  203. static struct cdevsw audit_pipe_cdevsw = {
  204. .d_version = D_VERSION,
  205. .d_open = audit_pipe_open,
  206. .d_read = audit_pipe_read,
  207. .d_ioctl = audit_pipe_ioctl,
  208. .d_poll = audit_pipe_poll,
  209. .d_kqfilter = audit_pipe_kqfilter,
  210. .d_name = AUDIT_PIPE_NAME,
  211. };
  212. static int audit_pipe_kqread(struct knote *note, long hint);
  213. static void audit_pipe_kqdetach(struct knote *note);
  214. static struct filterops audit_pipe_read_filterops = {
  215. .f_isfd = 1,
  216. .f_attach = NULL,
  217. .f_detach = audit_pipe_kqdetach,
  218. .f_event = audit_pipe_kqread,
  219. };
  220. /*
  221. * Some global statistics on audit pipes.
  222. */
  223. static int audit_pipe_count; /* Current number of pipes. */
  224. static u_int64_t audit_pipe_ever; /* Pipes ever allocated. */
  225. static u_int64_t audit_pipe_records; /* Records seen. */
  226. static u_int64_t audit_pipe_drops; /* Global record drop count. */
  227. /*
  228. * Free an audit pipe entry.
  229. */
  230. static void
  231. audit_pipe_entry_free(struct audit_pipe_entry *ape)
  232. {
  233. free(ape->ape_record, M_AUDIT_PIPE_ENTRY);
  234. free(ape, M_AUDIT_PIPE_ENTRY);
  235. }
  236. /*
  237. * Find an audit pipe preselection specification for an auid, if any.
  238. */
  239. static struct audit_pipe_preselect *
  240. audit_pipe_preselect_find(struct audit_pipe *ap, au_id_t auid)
  241. {
  242. struct audit_pipe_preselect *app;
  243. AUDIT_PIPE_LOCK_ASSERT(ap);
  244. TAILQ_FOREACH(app, &ap->ap_preselect_list, app_list) {
  245. if (app->app_auid == auid)
  246. return (app);
  247. }
  248. return (NULL);
  249. }
  250. /*
  251. * Query the per-pipe mask for a specific auid.
  252. */
  253. static int
  254. audit_pipe_preselect_get(struct audit_pipe *ap, au_id_t auid,
  255. au_mask_t *maskp)
  256. {
  257. struct audit_pipe_preselect *app;
  258. int error;
  259. AUDIT_PIPE_LOCK(ap);
  260. app = audit_pipe_preselect_find(ap, auid);
  261. if (app != NULL) {
  262. *maskp = app->app_mask;
  263. error = 0;
  264. } else
  265. error = ENOENT;
  266. AUDIT_PIPE_UNLOCK(ap);
  267. return (error);
  268. }
  269. /*
  270. * Set the per-pipe mask for a specific auid. Add a new entry if needed;
  271. * otherwise, update the current entry.
  272. */
  273. static void
  274. audit_pipe_preselect_set(struct audit_pipe *ap, au_id_t auid, au_mask_t mask)
  275. {
  276. struct audit_pipe_preselect *app, *app_new;
  277. /*
  278. * Pessimistically assume that the auid doesn't already have a mask
  279. * set, and allocate. We will free it if it is unneeded.
  280. */
  281. app_new = malloc(sizeof(*app_new), M_AUDIT_PIPE_PRESELECT, M_WAITOK);
  282. AUDIT_PIPE_LOCK(ap);
  283. app = audit_pipe_preselect_find(ap, auid);
  284. if (app == NULL) {
  285. app = app_new;
  286. app_new = NULL;
  287. app->app_auid = auid;
  288. TAILQ_INSERT_TAIL(&ap->ap_preselect_list, app, app_list);
  289. }
  290. app->app_mask = mask;
  291. AUDIT_PIPE_UNLOCK(ap);
  292. if (app_new != NULL)
  293. free(app_new, M_AUDIT_PIPE_PRESELECT);
  294. }
  295. /*
  296. * Delete a per-auid mask on an audit pipe.
  297. */
  298. static int
  299. audit_pipe_preselect_delete(struct audit_pipe *ap, au_id_t auid)
  300. {
  301. struct audit_pipe_preselect *app;
  302. int error;
  303. AUDIT_PIPE_LOCK(ap);
  304. app = audit_pipe_preselect_find(ap, auid);
  305. if (app != NULL) {
  306. TAILQ_REMOVE(&ap->ap_preselect_list, app, app_list);
  307. error = 0;
  308. } else
  309. error = ENOENT;
  310. AUDIT_PIPE_UNLOCK(ap);
  311. if (app != NULL)
  312. free(app, M_AUDIT_PIPE_PRESELECT);
  313. return (error);
  314. }
  315. /*
  316. * Delete all per-auid masks on an audit pipe.
  317. */
  318. static void
  319. audit_pipe_preselect_flush_locked(struct audit_pipe *ap)
  320. {
  321. struct audit_pipe_preselect *app;
  322. AUDIT_PIPE_LOCK_ASSERT(ap);
  323. while ((app = TAILQ_FIRST(&ap->ap_preselect_list)) != NULL) {
  324. TAILQ_REMOVE(&ap->ap_preselect_list, app, app_list);
  325. free(app, M_AUDIT_PIPE_PRESELECT);
  326. }
  327. }
  328. static void
  329. audit_pipe_preselect_flush(struct audit_pipe *ap)
  330. {
  331. AUDIT_PIPE_LOCK(ap);
  332. audit_pipe_preselect_flush_locked(ap);
  333. AUDIT_PIPE_UNLOCK(ap);
  334. }
  335. /*-
  336. * Determine whether a specific audit pipe matches a record with these
  337. * properties. Algorithm is as follows:
  338. *
  339. * - If the pipe is configured to track the default trail configuration, then
  340. * use the results of global preselection matching.
  341. * - If not, search for a specifically configured auid entry matching the
  342. * event. If an entry is found, use that.
  343. * - Otherwise, use the default flags or naflags configured for the pipe.
  344. */
  345. static int
  346. audit_pipe_preselect_check(struct audit_pipe *ap, au_id_t auid,
  347. au_event_t event, au_class_t class, int sorf, int trail_preselect)
  348. {
  349. struct audit_pipe_preselect *app;
  350. AUDIT_PIPE_LOCK_ASSERT(ap);
  351. switch (ap->ap_preselect_mode) {
  352. case AUDITPIPE_PRESELECT_MODE_TRAIL:
  353. return (trail_preselect);
  354. case AUDITPIPE_PRESELECT_MODE_LOCAL:
  355. app = audit_pipe_preselect_find(ap, auid);
  356. if (app == NULL) {
  357. if (auid == AU_DEFAUDITID)
  358. return (au_preselect(event, class,
  359. &ap->ap_preselect_naflags, sorf));
  360. else
  361. return (au_preselect(event, class,
  362. &ap->ap_preselect_flags, sorf));
  363. } else
  364. return (au_preselect(event, class, &app->app_mask,
  365. sorf));
  366. default:
  367. panic("audit_pipe_preselect_check: mode %d",
  368. ap->ap_preselect_mode);
  369. }
  370. return (0);
  371. }
  372. /*
  373. * Determine whether there exists a pipe interested in a record with specific
  374. * properties.
  375. */
  376. int
  377. audit_pipe_preselect(au_id_t auid, au_event_t event, au_class_t class,
  378. int sorf, int trail_preselect)
  379. {
  380. struct audit_pipe *ap;
  381. /* Lockless read to avoid acquiring the global lock if not needed. */
  382. if (TAILQ_EMPTY(&audit_pipe_list))
  383. return (0);
  384. AUDIT_PIPE_LIST_RLOCK();
  385. TAILQ_FOREACH(ap, &audit_pipe_list, ap_list) {
  386. AUDIT_PIPE_LOCK(ap);
  387. if (audit_pipe_preselect_check(ap, auid, event, class, sorf,
  388. trail_preselect)) {
  389. AUDIT_PIPE_UNLOCK(ap);
  390. AUDIT_PIPE_LIST_RUNLOCK();
  391. return (1);
  392. }
  393. AUDIT_PIPE_UNLOCK(ap);
  394. }
  395. AUDIT_PIPE_LIST_RUNLOCK();
  396. return (0);
  397. }
  398. /*
  399. * Append individual record to a queue -- allocate queue-local buffer, and
  400. * add to the queue. If the queue is full or we can't allocate memory, drop
  401. * the newest record.
  402. */
  403. static void
  404. audit_pipe_append(struct audit_pipe *ap, void *record, u_int record_len)
  405. {
  406. struct audit_pipe_entry *ape;
  407. AUDIT_PIPE_LOCK_ASSERT(ap);
  408. if (ap->ap_qlen >= ap->ap_qlimit) {
  409. ap->ap_drops++;
  410. audit_pipe_drops++;
  411. return;
  412. }
  413. ape = malloc(sizeof(*ape), M_AUDIT_PIPE_ENTRY, M_NOWAIT | M_ZERO);
  414. if (ape == NULL) {
  415. ap->ap_drops++;
  416. audit_pipe_drops++;
  417. return;
  418. }
  419. ape->ape_record = malloc(record_len, M_AUDIT_PIPE_ENTRY, M_NOWAIT);
  420. if (ape->ape_record == NULL) {
  421. free(ape, M_AUDIT_PIPE_ENTRY);
  422. ap->ap_drops++;
  423. audit_pipe_drops++;
  424. return;
  425. }
  426. bcopy(record, ape->ape_record, record_len);
  427. ape->ape_record_len = record_len;
  428. TAILQ_INSERT_TAIL(&ap->ap_queue, ape, ape_queue);
  429. ap->ap_inserts++;
  430. ap->ap_qlen++;
  431. ap->ap_qbyteslen += ape->ape_record_len;
  432. selwakeuppri(&ap->ap_selinfo, PSOCK);
  433. KNOTE_LOCKED(&ap->ap_selinfo.si_note, 0);
  434. if (ap->ap_flags & AUDIT_PIPE_ASYNC)
  435. pgsigio(&ap->ap_sigio, SIGIO, 0);
  436. cv_broadcast(&ap->ap_cv);
  437. }
  438. /*
  439. * audit_pipe_submit(): audit_worker submits audit records via this
  440. * interface, which arranges for them to be delivered to pipe queues.
  441. */
  442. void
  443. audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class, int sorf,
  444. int trail_select, void *record, u_int record_len)
  445. {
  446. struct audit_pipe *ap;
  447. /*
  448. * Lockless read to avoid lock overhead if pipes are not in use.
  449. */
  450. if (TAILQ_FIRST(&audit_pipe_list) == NULL)
  451. return;
  452. AUDIT_PIPE_LIST_RLOCK();
  453. TAILQ_FOREACH(ap, &audit_pipe_list, ap_list) {
  454. AUDIT_PIPE_LOCK(ap);
  455. if (audit_pipe_preselect_check(ap, auid, event, class, sorf,
  456. trail_select))
  457. audit_pipe_append(ap, record, record_len);
  458. AUDIT_PIPE_UNLOCK(ap);
  459. }
  460. AUDIT_PIPE_LIST_RUNLOCK();
  461. /* Unlocked increment. */
  462. audit_pipe_records++;
  463. }
  464. /*
  465. * audit_pipe_submit_user(): the same as audit_pipe_submit(), except that
  466. * since we don't currently have selection information available, it is
  467. * delivered to the pipe unconditionally.
  468. *
  469. * XXXRW: This is a bug. The BSM check routine for submitting a user record
  470. * should parse that information and return it.
  471. */
  472. void
  473. audit_pipe_submit_user(void *record, u_int record_len)
  474. {
  475. struct audit_pipe *ap;
  476. /*
  477. * Lockless read to avoid lock overhead if pipes are not in use.
  478. */
  479. if (TAILQ_FIRST(&audit_pipe_list) == NULL)
  480. return;
  481. AUDIT_PIPE_LIST_RLOCK();
  482. TAILQ_FOREACH(ap, &audit_pipe_list, ap_list) {
  483. AUDIT_PIPE_LOCK(ap);
  484. audit_pipe_append(ap, record, record_len);
  485. AUDIT_PIPE_UNLOCK(ap);
  486. }
  487. AUDIT_PIPE_LIST_RUNLOCK();
  488. /* Unlocked increment. */
  489. audit_pipe_records++;
  490. }
  491. /*
  492. * Allocate a new audit pipe. Connects the pipe, on success, to the global
  493. * list and updates statistics.
  494. */
  495. static struct audit_pipe *
  496. audit_pipe_alloc(void)
  497. {
  498. struct audit_pipe *ap;
  499. ap = malloc(sizeof(*ap), M_AUDIT_PIPE, M_NOWAIT | M_ZERO);
  500. if (ap == NULL)
  501. return (NULL);
  502. ap->ap_qlimit = AUDIT_PIPE_QLIMIT_DEFAULT;
  503. TAILQ_INIT(&ap->ap_queue);
  504. knlist_init_mtx(&ap->ap_selinfo.si_note, AUDIT_PIPE_MTX(ap));
  505. AUDIT_PIPE_LOCK_INIT(ap);
  506. AUDIT_PIPE_SX_LOCK_INIT(ap);
  507. cv_init(&ap->ap_cv, "audit_pipe");
  508. /*
  509. * Default flags, naflags, and auid-specific preselection settings to
  510. * 0. Initialize the mode to the global trail so that if praudit(1)
  511. * is run on /dev/auditpipe, it sees events associated with the
  512. * default trail. Pipe-aware application can clear the flag, set
  513. * custom masks, and flush the pipe as needed.
  514. */
  515. bzero(&ap->ap_preselect_flags, sizeof(ap->ap_preselect_flags));
  516. bzero(&ap->ap_preselect_naflags, sizeof(ap->ap_preselect_naflags));
  517. TAILQ_INIT(&ap->ap_preselect_list);
  518. ap->ap_preselect_mode = AUDITPIPE_PRESELECT_MODE_TRAIL;
  519. /*
  520. * Add to global list and update global statistics.
  521. */
  522. AUDIT_PIPE_LIST_WLOCK();
  523. TAILQ_INSERT_HEAD(&audit_pipe_list, ap, ap_list);
  524. audit_pipe_count++;
  525. audit_pipe_ever++;
  526. AUDIT_PIPE_LIST_WUNLOCK();
  527. return (ap);
  528. }
  529. /*
  530. * Flush all records currently present in an audit pipe; assume mutex is held.
  531. */
  532. static void
  533. audit_pipe_flush(struct audit_pipe *ap)
  534. {
  535. struct audit_pipe_entry *ape;
  536. AUDIT_PIPE_LOCK_ASSERT(ap);
  537. while ((ape = TAILQ_FIRST(&ap->ap_queue)) != NULL) {
  538. TAILQ_REMOVE(&ap->ap_queue, ape, ape_queue);
  539. ap->ap_qbyteslen -= ape->ape_record_len;
  540. audit_pipe_entry_free(ape);
  541. ap->ap_qlen--;
  542. }
  543. ap->ap_qoffset = 0;
  544. KASSERT(ap->ap_qlen == 0, ("audit_pipe_free: ap_qbyteslen"));
  545. KASSERT(ap->ap_qbyteslen == 0, ("audit_pipe_flush: ap_qbyteslen"));
  546. }
  547. /*
  548. * Free an audit pipe; this means freeing all preselection state and all
  549. * records in the pipe. Assumes global write lock and pipe mutex are held to
  550. * prevent any new records from being inserted during the free, and that the
  551. * audit pipe is still on the global list.
  552. */
  553. static void
  554. audit_pipe_free(struct audit_pipe *ap)
  555. {
  556. AUDIT_PIPE_LIST_WLOCK_ASSERT();
  557. AUDIT_PIPE_LOCK_ASSERT(ap);
  558. audit_pipe_preselect_flush_locked(ap);
  559. audit_pipe_flush(ap);
  560. cv_destroy(&ap->ap_cv);
  561. AUDIT_PIPE_SX_LOCK_DESTROY(ap);
  562. AUDIT_PIPE_LOCK_DESTROY(ap);
  563. seldrain(&ap->ap_selinfo);
  564. knlist_destroy(&ap->ap_selinfo.si_note);
  565. TAILQ_REMOVE(&audit_pipe_list, ap, ap_list);
  566. free(ap, M_AUDIT_PIPE);
  567. audit_pipe_count--;
  568. }
  569. static void
  570. audit_pipe_dtor(void *arg)
  571. {
  572. struct audit_pipe *ap;
  573. ap = arg;
  574. funsetown(&ap->ap_sigio);
  575. AUDIT_PIPE_LIST_WLOCK();
  576. AUDIT_PIPE_LOCK(ap);
  577. audit_pipe_free(ap);
  578. AUDIT_PIPE_LIST_WUNLOCK();
  579. }
  580. /*
  581. * Audit pipe open method. Explicit privilege check isn't used as this
  582. * allows file permissions on the special device to be used to grant audit
  583. * review access. Those file permissions should be managed carefully.
  584. */
  585. static int
  586. audit_pipe_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  587. {
  588. struct audit_pipe *ap;
  589. int error;
  590. ap = audit_pipe_alloc();
  591. if (ap == NULL)
  592. return (ENOMEM);
  593. fsetown(td->td_proc->p_pid, &ap->ap_sigio);
  594. error = devfs_set_cdevpriv(ap, audit_pipe_dtor);
  595. if (error != 0)
  596. audit_pipe_dtor(ap);
  597. return (error);
  598. }
  599. /*
  600. * Audit pipe ioctl() routine. Handle file descriptor and audit pipe layer
  601. * commands.
  602. */
  603. static int
  604. audit_pipe_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
  605. struct thread *td)
  606. {
  607. struct auditpipe_ioctl_preselect *aip;
  608. struct audit_pipe *ap;
  609. au_mask_t *maskp;
  610. int error, mode;
  611. au_id_t auid;
  612. error = devfs_get_cdevpriv((void **)&ap);
  613. if (error != 0)
  614. return (error);
  615. /*
  616. * Audit pipe ioctls: first come standard device node ioctls, then
  617. * manipulation of pipe settings, and finally, statistics query
  618. * ioctls.
  619. */
  620. switch (cmd) {
  621. case FIONBIO:
  622. AUDIT_PIPE_LOCK(ap);
  623. if (*(int *)data)
  624. ap->ap_flags |= AUDIT_PIPE_NBIO;
  625. else
  626. ap->ap_flags &= ~AUDIT_PIPE_NBIO;
  627. AUDIT_PIPE_UNLOCK(ap);
  628. error = 0;
  629. break;
  630. case FIONREAD:
  631. AUDIT_PIPE_LOCK(ap);
  632. *(int *)data = ap->ap_qbyteslen - ap->ap_qoffset;
  633. AUDIT_PIPE_UNLOCK(ap);
  634. error = 0;
  635. break;
  636. case FIOASYNC:
  637. AUDIT_PIPE_LOCK(ap);
  638. if (*(int *)data)
  639. ap->ap_flags |= AUDIT_PIPE_ASYNC;
  640. else
  641. ap->ap_flags &= ~AUDIT_PIPE_ASYNC;
  642. AUDIT_PIPE_UNLOCK(ap);
  643. error = 0;
  644. break;
  645. case FIOSETOWN:
  646. error = fsetown(*(int *)data, &ap->ap_sigio);
  647. break;
  648. case FIOGETOWN:
  649. *(int *)data = fgetown(&ap->ap_sigio);
  650. error = 0;
  651. break;
  652. case AUDITPIPE_GET_QLEN:
  653. *(u_int *)data = ap->ap_qlen;
  654. error = 0;
  655. break;
  656. case AUDITPIPE_GET_QLIMIT:
  657. *(u_int *)data = ap->ap_qlimit;
  658. error = 0;
  659. break;
  660. case AUDITPIPE_SET_QLIMIT:
  661. /* Lockless integer write. */
  662. if (*(u_int *)data >= AUDIT_PIPE_QLIMIT_MIN &&
  663. *(u_int *)data <= AUDIT_PIPE_QLIMIT_MAX) {
  664. ap->ap_qlimit = *(u_int *)data;
  665. error = 0;
  666. } else
  667. error = EINVAL;
  668. break;
  669. case AUDITPIPE_GET_QLIMIT_MIN:
  670. *(u_int *)data = AUDIT_PIPE_QLIMIT_MIN;
  671. error = 0;
  672. break;
  673. case AUDITPIPE_GET_QLIMIT_MAX:
  674. *(u_int *)data = AUDIT_PIPE_QLIMIT_MAX;
  675. error = 0;
  676. break;
  677. case AUDITPIPE_GET_PRESELECT_FLAGS:
  678. AUDIT_PIPE_LOCK(ap);
  679. maskp = (au_mask_t *)data;
  680. *maskp = ap->ap_preselect_flags;
  681. AUDIT_PIPE_UNLOCK(ap);
  682. error = 0;
  683. break;
  684. case AUDITPIPE_SET_PRESELECT_FLAGS:
  685. AUDIT_PIPE_LOCK(ap);
  686. maskp = (au_mask_t *)data;
  687. ap->ap_preselect_flags = *maskp;
  688. AUDIT_PIPE_UNLOCK(ap);
  689. error = 0;
  690. break;
  691. case AUDITPIPE_GET_PRESELECT_NAFLAGS:
  692. AUDIT_PIPE_LOCK(ap);
  693. maskp = (au_mask_t *)data;
  694. *maskp = ap->ap_preselect_naflags;
  695. AUDIT_PIPE_UNLOCK(ap);
  696. error = 0;
  697. break;
  698. case AUDITPIPE_SET_PRESELECT_NAFLAGS:
  699. AUDIT_PIPE_LOCK(ap);
  700. maskp = (au_mask_t *)data;
  701. ap->ap_preselect_naflags = *maskp;
  702. AUDIT_PIPE_UNLOCK(ap);
  703. error = 0;
  704. break;
  705. case AUDITPIPE_GET_PRESELECT_AUID:
  706. aip = (struct auditpipe_ioctl_preselect *)data;
  707. error = audit_pipe_preselect_get(ap, aip->aip_auid,
  708. &aip->aip_mask);
  709. break;
  710. case AUDITPIPE_SET_PRESELECT_AUID:
  711. aip = (struct auditpipe_ioctl_preselect *)data;
  712. audit_pipe_preselect_set(ap, aip->aip_auid, aip->aip_mask);
  713. error = 0;
  714. break;
  715. case AUDITPIPE_DELETE_PRESELECT_AUID:
  716. auid = *(au_id_t *)data;
  717. error = audit_pipe_preselect_delete(ap, auid);
  718. break;
  719. case AUDITPIPE_FLUSH_PRESELECT_AUID:
  720. audit_pipe_preselect_flush(ap);
  721. error = 0;
  722. break;
  723. case AUDITPIPE_GET_PRESELECT_MODE:
  724. AUDIT_PIPE_LOCK(ap);
  725. *(int *)data = ap->ap_preselect_mode;
  726. AUDIT_PIPE_UNLOCK(ap);
  727. error = 0;
  728. break;
  729. case AUDITPIPE_SET_PRESELECT_MODE:
  730. mode = *(int *)data;
  731. switch (mode) {
  732. case AUDITPIPE_PRESELECT_MODE_TRAIL:
  733. case AUDITPIPE_PRESELECT_MODE_LOCAL:
  734. AUDIT_PIPE_LOCK(ap);
  735. ap->ap_preselect_mode = mode;
  736. AUDIT_PIPE_UNLOCK(ap);
  737. error = 0;
  738. break;
  739. default:
  740. error = EINVAL;
  741. }
  742. break;
  743. case AUDITPIPE_FLUSH:
  744. if (AUDIT_PIPE_SX_XLOCK_SIG(ap) != 0)
  745. return (EINTR);
  746. AUDIT_PIPE_LOCK(ap);
  747. audit_pipe_flush(ap);
  748. AUDIT_PIPE_UNLOCK(ap);
  749. AUDIT_PIPE_SX_XUNLOCK(ap);
  750. error = 0;
  751. break;
  752. case AUDITPIPE_GET_MAXAUDITDATA:
  753. *(u_int *)data = MAXAUDITDATA;
  754. error = 0;
  755. break;
  756. case AUDITPIPE_GET_INSERTS:
  757. *(u_int *)data = ap->ap_inserts;
  758. error = 0;
  759. break;
  760. case AUDITPIPE_GET_READS:
  761. *(u_int *)data = ap->ap_reads;
  762. error = 0;
  763. break;
  764. case AUDITPIPE_GET_DROPS:
  765. *(u_int *)data = ap->ap_drops;
  766. error = 0;
  767. break;
  768. case AUDITPIPE_GET_TRUNCATES:
  769. *(u_int *)data = 0;
  770. error = 0;
  771. break;
  772. default:
  773. error = ENOTTY;
  774. }
  775. return (error);
  776. }
  777. /*
  778. * Audit pipe read. Read one or more partial or complete records to user
  779. * memory.
  780. */
  781. static int
  782. audit_pipe_read(struct cdev *dev, struct uio *uio, int flag)
  783. {
  784. struct audit_pipe_entry *ape;
  785. struct audit_pipe *ap;
  786. u_int toread;
  787. int error;
  788. error = devfs_get_cdevpriv((void **)&ap);
  789. if (error != 0)
  790. return (error);
  791. /*
  792. * We hold an sx(9) lock over read and flush because we rely on the
  793. * stability of a record in the queue during uiomove(9).
  794. */
  795. if (AUDIT_PIPE_SX_XLOCK_SIG(ap) != 0)
  796. return (EINTR);
  797. AUDIT_PIPE_LOCK(ap);
  798. while (TAILQ_EMPTY(&ap->ap_queue)) {
  799. if (ap->ap_flags & AUDIT_PIPE_NBIO) {
  800. AUDIT_PIPE_UNLOCK(ap);
  801. AUDIT_PIPE_SX_XUNLOCK(ap);
  802. return (EAGAIN);
  803. }
  804. error = cv_wait_sig(&ap->ap_cv, AUDIT_PIPE_MTX(ap));
  805. if (error) {
  806. AUDIT_PIPE_UNLOCK(ap);
  807. AUDIT_PIPE_SX_XUNLOCK(ap);
  808. return (error);
  809. }
  810. }
  811. /*
  812. * Copy as many remaining bytes from the current record to userspace
  813. * as we can. Keep processing records until we run out of records in
  814. * the queue, or until the user buffer runs out of space.
  815. *
  816. * Note: we rely on the SX lock to maintain ape's stability here.
  817. */
  818. ap->ap_reads++;
  819. while ((ape = TAILQ_FIRST(&ap->ap_queue)) != NULL &&
  820. uio->uio_resid > 0) {
  821. AUDIT_PIPE_LOCK_ASSERT(ap);
  822. KASSERT(ape->ape_record_len > ap->ap_qoffset,
  823. ("audit_pipe_read: record_len > qoffset (1)"));
  824. toread = MIN(ape->ape_record_len - ap->ap_qoffset,
  825. uio->uio_resid);
  826. AUDIT_PIPE_UNLOCK(ap);
  827. error = uiomove((char *)ape->ape_record + ap->ap_qoffset,
  828. toread, uio);
  829. if (error) {
  830. AUDIT_PIPE_SX_XUNLOCK(ap);
  831. return (error);
  832. }
  833. /*
  834. * If the copy succeeded, update book-keeping, and if no
  835. * bytes remain in the current record, free it.
  836. */
  837. AUDIT_PIPE_LOCK(ap);
  838. KASSERT(TAILQ_FIRST(&ap->ap_queue) == ape,
  839. ("audit_pipe_read: queue out of sync after uiomove"));
  840. ap->ap_qoffset += toread;
  841. KASSERT(ape->ape_record_len >= ap->ap_qoffset,
  842. ("audit_pipe_read: record_len >= qoffset (2)"));
  843. if (ap->ap_qoffset == ape->ape_record_len) {
  844. TAILQ_REMOVE(&ap->ap_queue, ape, ape_queue);
  845. ap->ap_qbyteslen -= ape->ape_record_len;
  846. audit_pipe_entry_free(ape);
  847. ap->ap_qlen--;
  848. ap->ap_qoffset = 0;
  849. }
  850. }
  851. AUDIT_PIPE_UNLOCK(ap);
  852. AUDIT_PIPE_SX_XUNLOCK(ap);
  853. return (0);
  854. }
  855. /*
  856. * Audit pipe poll.
  857. */
  858. static int
  859. audit_pipe_poll(struct cdev *dev, int events, struct thread *td)
  860. {
  861. struct audit_pipe *ap;
  862. int error, revents;
  863. revents = 0;
  864. error = devfs_get_cdevpriv((void **)&ap);
  865. if (error != 0)
  866. return (error);
  867. if (events & (POLLIN | POLLRDNORM)) {
  868. AUDIT_PIPE_LOCK(ap);
  869. if (TAILQ_FIRST(&ap->ap_queue) != NULL)
  870. revents |= events & (POLLIN | POLLRDNORM);
  871. else
  872. selrecord(td, &ap->ap_selinfo);
  873. AUDIT_PIPE_UNLOCK(ap);
  874. }
  875. return (revents);
  876. }
  877. /*
  878. * Audit pipe kqfilter.
  879. */
  880. static int
  881. audit_pipe_kqfilter(struct cdev *dev, struct knote *kn)
  882. {
  883. struct audit_pipe *ap;
  884. int error;
  885. error = devfs_get_cdevpriv((void **)&ap);
  886. if (error != 0)
  887. return (error);
  888. if (kn->kn_filter != EVFILT_READ)
  889. return (EINVAL);
  890. kn->kn_fop = &audit_pipe_read_filterops;
  891. kn->kn_hook = ap;
  892. AUDIT_PIPE_LOCK(ap);
  893. knlist_add(&ap->ap_selinfo.si_note, kn, 1);
  894. AUDIT_PIPE_UNLOCK(ap);
  895. return (0);
  896. }
  897. /*
  898. * Return true if there are records available for reading on the pipe.
  899. */
  900. static int
  901. audit_pipe_kqread(struct knote *kn, long hint)
  902. {
  903. struct audit_pipe *ap;
  904. ap = (struct audit_pipe *)kn->kn_hook;
  905. AUDIT_PIPE_LOCK_ASSERT(ap);
  906. if (ap->ap_qlen != 0) {
  907. kn->kn_data = ap->ap_qbyteslen - ap->ap_qoffset;
  908. return (1);
  909. } else {
  910. kn->kn_data = 0;
  911. return (0);
  912. }
  913. }
  914. /*
  915. * Detach kqueue state from audit pipe.
  916. */
  917. static void
  918. audit_pipe_kqdetach(struct knote *kn)
  919. {
  920. struct audit_pipe *ap;
  921. ap = (struct audit_pipe *)kn->kn_hook;
  922. AUDIT_PIPE_LOCK(ap);
  923. knlist_remove(&ap->ap_selinfo.si_note, kn, 1);
  924. AUDIT_PIPE_UNLOCK(ap);
  925. }
  926. /*
  927. * Initialize the audit pipe system.
  928. */
  929. static void
  930. audit_pipe_init(void *unused)
  931. {
  932. TAILQ_INIT(&audit_pipe_list);
  933. AUDIT_PIPE_LIST_LOCK_INIT();
  934. audit_pipe_dev = make_dev(&audit_pipe_cdevsw, 0, UID_ROOT,
  935. GID_WHEEL, 0600, "%s", AUDIT_PIPE_NAME);
  936. if (audit_pipe_dev == NULL) {
  937. AUDIT_PIPE_LIST_LOCK_DESTROY();
  938. panic("Can't initialize audit pipe subsystem");
  939. }
  940. }
  941. SYSINIT(audit_pipe_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, audit_pipe_init,
  942. NULL);