2 * Copyright (c) 2016 Robert N. M. Watson
5 * This software was developed by BAE Systems, the University of Cambridge
6 * Computer Laboratory, and Memorial University under DARPA/AFRL contract
7 * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
8 * (TC) research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/ctype.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/queue.h>
42 #include <sys/refcount.h>
44 #include <sys/dtrace.h>
45 #include <sys/dtrace_bsd.h>
47 #include <bsm/audit.h>
48 #include <bsm/audit_internal.h>
49 #include <bsm/audit_kevents.h>
51 #include <security/audit/audit.h>
52 #include <security/audit/audit_private.h>
55 * Audit DTrace provider: allow DTrace to request that audit records be
56 * generated for various audit events, and then expose those records (in
57 * various forms) to probes. The model is that each event type has two
58 * probes, which use the event's name to create the probe:
60 * - "commit" passes the kernel-internal (unserialised) kaudit_record
61 * synchronously (from the originating thread) of the record as we prepare
62 * to "commit" the record to the audit queue.
64 * - "bsm" also passes generated BSM, and executes asynchronously in the audit
65 * worker thread, once it has been extracted from the audit queue. This is
66 * the point at which an audit record would be enqueued to the trail on
69 * These probes support very different goals. The former executes in the
70 * thread originating the record, making it easier to correlate other DTrace
71 * probe activity with the event described in the record. The latter gives
72 * access to BSM-formatted events (at a cost) allowing DTrace to extract BSM
73 * directly an alternative mechanism to the formal audit trail and audit
76 * To generate names for numeric event IDs, userspace will push the contents
77 * of /etc/security/audit_event into the kernel during audit setup, much as it
78 * does /etc/security/audit_class. We then create the probes for each of
79 * those mappings. If one (or both) of the probes are enabled, then we cause
80 * a record to be generated (as both normal audit preselection and audit pipes
81 * do), and catch it on the way out during commit. There are suitable hook
82 * functions in the audit code that this provider can register to catch
83 * various events in the audit-record life cycle.
87 * - How do we want to handle events for which there are not names -- perhaps
88 * a catch-all probe for those events without mappings?
90 * - Should the evname code really be present even if DTrace isn't loaded...?
91 * Right now, we arrange that it is so that userspace can usefully maintain
92 * the list in case DTrace is later loaded (and to prevent userspace
95 * - Should we add an additional set of audit:class::commit probes that use
96 * event class names to match broader categories of events as specified in
97 * /etc/security/event_class?
99 * - If we pursue that last point, we will want to pass the name of the event
100 * into the probe explicitly (e.g., as arg0), since it would no longer be
101 * available as the probe function name.
104 static int dtaudit_unload(void);
105 static void dtaudit_getargdesc(void *, dtrace_id_t, void *,
107 static void dtaudit_provide(void *, dtrace_probedesc_t *);
108 static void dtaudit_destroy(void *, dtrace_id_t, void *);
109 static void dtaudit_enable(void *, dtrace_id_t, void *);
110 static void dtaudit_disable(void *, dtrace_id_t, void *);
111 static void dtaudit_load(void *);
113 static dtrace_pattr_t dtaudit_attr = {
114 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
115 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
116 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
117 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
118 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
122 * Strings for the "module" and "name" portions of the probe. The name of the
123 * audit event will be the "function" portion of the probe. All dtaudit
124 * probes therefore take the form audit:event:<event name>:commit.
126 static char *dtaudit_module_str = "event";
127 static char *dtaudit_name_commit_str = "commit";
128 static char *dtaudit_name_bsm_str = "bsm";
130 static dtrace_pops_t dtaudit_pops = {
131 .dtps_provide = dtaudit_provide,
132 .dtps_provide_module = NULL,
133 .dtps_enable = dtaudit_enable,
134 .dtps_disable = dtaudit_disable,
135 .dtps_suspend = NULL,
137 .dtps_getargdesc = dtaudit_getargdesc,
138 .dtps_getargval = NULL,
139 .dtps_usermode = NULL,
140 .dtps_destroy = dtaudit_destroy
143 static dtrace_provider_id_t dtaudit_id;
146 * Because looking up entries in the event-to-name mapping is quite expensive,
147 * maintain a global flag tracking whether any dtaudit probes are enabled. If
148 * not, don't bother doing all that work whenever potential queries about
149 * events turn up during preselection or commit.
151 static uint_t dtaudit_probes_enabled;
154 * Check dtaudit policy for the event to see whether this is an event we would
155 * like to preselect (i.e., cause an audit record to be generated for). To
156 * minimise probe effect when not used at all, we not only check for the probe
157 * on the individual event, but also a global flag indicating that at least
158 * one probe is enabled, before acquiring locks, searching lists, etc.
160 * If the event is selected, return an evname_elem reference to be stored in
161 * the audit record, which we can use later to avoid further lookups. The
162 * contents of the evname_elem must be sufficiently stable so as to not risk
163 * race conditions here.
165 * Currently, we take an interest only in the 'event' argument, but in the
166 * future might want to support other types of record selection tied to
167 * additional probe types (e.g., event clases).
169 * XXXRW: Should we have a catch-all probe here for events without registered
173 dtaudit_preselect(au_id_t auid, au_event_t event, au_class_t class)
175 struct evname_elem *ene;
179 * NB: Lockless reads here may return a slightly stale value; this is
180 * considered better than acquiring a lock, however.
182 if (!dtaudit_probes_enabled)
184 ene = au_evnamemap_lookup(event);
189 * See if either of the two probes for the audit event are enabled.
191 * NB: Lock also not acquired here -- but perhaps it wouldn't matter
192 * given that we've already used the list lock above?
194 * XXXRW: Alternatively, au_evnamemap_lookup() could return these
195 * values while holding the list lock...?
197 probe_enabled = ene->ene_commit_probe_enabled ||
198 ene->ene_bsm_probe_enabled;
201 return ((void *)ene);
205 * Commit probe pre-BSM. Fires the probe but also checks to see if we should
206 * ask the audit framework to call us again with BSM arguments in the audit
209 * XXXRW: Should we have a catch-all probe here for events without registered
213 dtaudit_commit(struct kaudit_record *kar, au_id_t auid, au_event_t event,
214 au_class_t class, int sorf)
216 char ene_name_lower[EVNAMEMAP_NAME_SIZE];
217 struct evname_elem *ene;
220 ene = (struct evname_elem *)kar->k_dtaudit_state;
225 * Process a possibly registered commit probe.
227 if (ene->ene_commit_probe_enabled) {
229 * XXXRW: Lock ene to provide stability to the name string. A
230 * bit undesirable! We may want another locking strategy
231 * here. At least we don't run the DTrace probe under the
234 * XXXRW: We provide the struct audit_record pointer -- but
235 * perhaps should provide the kaudit_record pointer?
238 for (i = 0; i < sizeof(ene_name_lower); i++)
239 ene_name_lower[i] = tolower(ene->ene_name[i]);
241 dtrace_probe(ene->ene_commit_probe_id,
242 (uintptr_t)ene_name_lower, (uintptr_t)&kar->k_ar, 0, 0, 0);
246 * Return the state of the BSM probe to the caller.
248 return (ene->ene_bsm_probe_enabled);
252 * Commit probe post-BSM.
254 * XXXRW: Should we have a catch-all probe here for events without registered
258 dtaudit_bsm(struct kaudit_record *kar, au_id_t auid, au_event_t event,
259 au_class_t class, int sorf, void *bsm_data, size_t bsm_len)
261 char ene_name_lower[EVNAMEMAP_NAME_SIZE];
262 struct evname_elem *ene;
265 ene = (struct evname_elem *)kar->k_dtaudit_state;
268 if (!(ene->ene_bsm_probe_enabled))
272 * XXXRW: Lock ene to provide stability to the name string. A bit
273 * undesirable! We may want another locking strategy here. At least
274 * we don't run the DTrace probe under the lock.
276 * XXXRW: We provide the struct audit_record pointer -- but perhaps
277 * should provide the kaudit_record pointer?
280 for (i = 0; i < sizeof(ene_name_lower); i++)
281 ene_name_lower[i] = tolower(ene->ene_name[i]);
283 dtrace_probe(ene->ene_bsm_probe_id, (uintptr_t)ene_name_lower,
284 (uintptr_t)&kar->k_ar, (uintptr_t)bsm_data, (uintptr_t)bsm_len,
289 * A very simple provider: argument types are identical across all probes: the
290 * kaudit_record, plus a BSM pointer and length.
293 dtaudit_getargdesc(void *arg, dtrace_id_t id, void *parg,
294 dtrace_argdesc_t *desc)
296 struct evname_elem *ene;
299 ene = (struct evname_elem *)parg;
301 switch (desc->dtargd_ndx) {
303 /* Audit event name. */
308 /* In-kernel audit record. */
309 p = "struct audit_record *";
313 /* BSM data, if present. */
314 if (id == ene->ene_bsm_probe_id)
317 desc->dtargd_ndx = DTRACE_ARGNONE;
321 /* BSM length, if present. */
322 if (id == ene->ene_bsm_probe_id)
325 desc->dtargd_ndx = DTRACE_ARGNONE;
329 desc->dtargd_ndx = DTRACE_ARGNONE;
333 strlcpy(desc->dtargd_native, p, sizeof(desc->dtargd_native));
337 * Callback from the event-to-name mapping code when performing
338 * evname_foreach(). Note that we may update the entry, so the foreach code
339 * must have a write lock. However, as the synchronisation model is private
340 * to the evname code, we cannot easily assert it here.
342 * XXXRW: How do we want to handle event rename / collision issues here --
343 * e.g., if userspace was using a name to point to one event number, and then
344 * changes it so that the name points at another? For now, paper over this by
345 * skipping event numbers that are already registered, and likewise skipping
346 * names that are already registered. However, this could lead to confusing
347 * behaviour so possibly needs to be resolved in the longer term.
350 dtaudit_au_evnamemap_callback(struct evname_elem *ene)
352 char ene_name_lower[EVNAMEMAP_NAME_SIZE];
356 * DTrace, by convention, has lower-case probe names. However, the
357 * in-kernel event-to-name mapping table must maintain event-name case
358 * as submitted by userspace. Create a temporary lower-case version
359 * here, away from the fast path, to use when exposing the event name
360 * to DTrace as part of the name of a probe.
362 * NB: Convert the entire array, including the terminating nul,
363 * because these strings are short and it's more work not to. If they
364 * become long, we might feel more guilty about this sloppiness!
366 for (i = 0; i < sizeof(ene_name_lower); i++)
367 ene_name_lower[i] = tolower(ene->ene_name[i]);
370 * Don't register a new probe if this event number already has an
371 * associated commit probe -- or if another event has already
372 * registered this name.
374 * XXXRW: There is an argument that if multiple numeric events match
375 * a single name, they should all be exposed to the same named probe.
376 * In particular, we should perhaps use a probe ID returned by this
377 * lookup and just stick that in the saved probe ID?
379 if ((ene->ene_commit_probe_id == 0) &&
380 (dtrace_probe_lookup(dtaudit_id, dtaudit_module_str,
381 ene_name_lower, dtaudit_name_commit_str) == 0)) {
384 * Create the commit probe.
386 * NB: We don't declare any extra stack frames because stack()
387 * will just return the path to the audit commit code, which
388 * is not really interesting anyway.
390 * We pass in the pointer to the evnam_elem entry so that we
391 * can easily change its enabled flag in the probe
392 * enable/disable interface.
394 ene->ene_commit_probe_id = dtrace_probe_create(dtaudit_id,
395 dtaudit_module_str, ene_name_lower,
396 dtaudit_name_commit_str, 0, ene);
400 * Don't register a new probe if this event number already has an
401 * associated bsm probe -- or if another event has already
402 * registered this name.
404 * XXXRW: There is an argument that if multiple numeric events match
405 * a single name, they should all be exposed to the same named probe.
406 * In particular, we should perhaps use a probe ID returned by this
407 * lookup and just stick that in the saved probe ID?
409 if ((ene->ene_bsm_probe_id == 0) &&
410 (dtrace_probe_lookup(dtaudit_id, dtaudit_module_str,
411 ene_name_lower, dtaudit_name_bsm_str) == 0)) {
414 * Create the bsm probe.
416 * NB: We don't declare any extra stack frames because stack()
417 * will just return the path to the audit commit code, which
418 * is not really interesting anyway.
420 * We pass in the pointer to the evnam_elem entry so that we
421 * can easily change its enabled flag in the probe
422 * enable/disable interface.
424 ene->ene_bsm_probe_id = dtrace_probe_create(dtaudit_id,
425 dtaudit_module_str, ene_name_lower, dtaudit_name_bsm_str,
431 dtaudit_provide(void *arg, dtrace_probedesc_t *desc)
435 * Walk all registered number-to-name mapping entries, and ensure each
436 * is properly registered.
438 au_evnamemap_foreach(dtaudit_au_evnamemap_callback);
442 dtaudit_destroy(void *arg, dtrace_id_t id, void *parg)
447 dtaudit_enable(void *arg, dtrace_id_t id, void *parg)
449 struct evname_elem *ene;
452 KASSERT(ene->ene_commit_probe_id == id || ene->ene_bsm_probe_id == id,
453 ("%s: probe ID mismatch (%u, %u != %u)", __func__,
454 ene->ene_commit_probe_id, ene->ene_bsm_probe_id, id));
456 if (id == ene->ene_commit_probe_id)
457 ene->ene_commit_probe_enabled = 1;
459 ene->ene_bsm_probe_enabled = 1;
460 refcount_acquire(&dtaudit_probes_enabled);
464 dtaudit_disable(void *arg, dtrace_id_t id, void *parg)
466 struct evname_elem *ene;
469 KASSERT(ene->ene_commit_probe_id == id || ene->ene_bsm_probe_id == id,
470 ("%s: probe ID mismatch (%u, %u != %u)", __func__,
471 ene->ene_commit_probe_id, ene->ene_bsm_probe_id, id));
473 if (id == ene->ene_commit_probe_id)
474 ene->ene_commit_probe_enabled = 0;
476 ene->ene_bsm_probe_enabled = 0;
477 (void)refcount_release(&dtaudit_probes_enabled);
481 dtaudit_load(void *dummy)
484 if (dtrace_register("audit", &dtaudit_attr, DTRACE_PRIV_USER, NULL,
485 &dtaudit_pops, NULL, &dtaudit_id) != 0)
487 dtaudit_hook_preselect = dtaudit_preselect;
488 dtaudit_hook_commit = dtaudit_commit;
489 dtaudit_hook_bsm = dtaudit_bsm;
497 dtaudit_hook_preselect = NULL;
498 dtaudit_hook_commit = NULL;
499 dtaudit_hook_bsm = NULL;
500 if ((error = dtrace_unregister(dtaudit_id)) != 0)
506 dtaudit_modevent(module_t mod __unused, int type, void *data __unused)
524 SYSINIT(dtaudit_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, dtaudit_load,
526 SYSUNINIT(dtaudit_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
527 dtaudit_unload, NULL);
529 DEV_MODULE(dtaudit, dtaudit_modevent, NULL);
530 MODULE_VERSION(dtaudit, 1);
531 MODULE_DEPEND(dtaudit, dtrace, 1, 1, 1);
532 MODULE_DEPEND(dtaudit, opensolaris, 1, 1, 1);