xref: /freebsd/sys/security/audit/audit_bsm_db.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1709557d9SRobert Watson /*
2709557d9SRobert Watson  * Copyright (c) 1999-2009 Apple Inc.
3*deea362cSRobert Watson  * Copyright (c) 2005, 2016-2018 Robert N. M. Watson
4709557d9SRobert Watson  * All rights reserved.
5709557d9SRobert Watson  *
6709557d9SRobert Watson  * Portions of this software were developed by BAE Systems, the University of
7709557d9SRobert Watson  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
8709557d9SRobert Watson  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
9709557d9SRobert Watson  * Computing (TC) research program.
10709557d9SRobert Watson  *
11709557d9SRobert Watson  * Redistribution and use in source and binary forms, with or without
12709557d9SRobert Watson  * modification, are permitted provided that the following conditions
13709557d9SRobert Watson  * are met:
14709557d9SRobert Watson  * 1.  Redistributions of source code must retain the above copyright
15709557d9SRobert Watson  *     notice, this list of conditions and the following disclaimer.
16709557d9SRobert Watson  * 2.  Redistributions in binary form must reproduce the above copyright
17709557d9SRobert Watson  *     notice, this list of conditions and the following disclaimer in the
18709557d9SRobert Watson  *     documentation and/or other materials provided with the distribution.
19709557d9SRobert Watson  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
20709557d9SRobert Watson  *     its contributors may be used to endorse or promote products derived
21709557d9SRobert Watson  *     from this software without specific prior written permission.
22709557d9SRobert Watson  *
23709557d9SRobert Watson  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
24709557d9SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25709557d9SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26709557d9SRobert Watson  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
27709557d9SRobert Watson  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28709557d9SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29709557d9SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30709557d9SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31709557d9SRobert Watson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32709557d9SRobert Watson  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33709557d9SRobert Watson  * POSSIBILITY OF SUCH DAMAGE.
34709557d9SRobert Watson  */
35709557d9SRobert Watson 
36709557d9SRobert Watson #include <sys/param.h>
37709557d9SRobert Watson #include <sys/capsicum.h>
38709557d9SRobert Watson #include <sys/fcntl.h>
39709557d9SRobert Watson #include <sys/filedesc.h>
40709557d9SRobert Watson #include <sys/libkern.h>
41*deea362cSRobert Watson #include <sys/linker.h>
42709557d9SRobert Watson #include <sys/malloc.h>
43709557d9SRobert Watson #include <sys/mount.h>
44709557d9SRobert Watson #include <sys/proc.h>
45709557d9SRobert Watson #include <sys/rwlock.h>
46709557d9SRobert Watson #include <sys/sem.h>
47709557d9SRobert Watson #include <sys/sbuf.h>
48709557d9SRobert Watson #include <sys/sx.h>
49709557d9SRobert Watson #include <sys/syscall.h>
50709557d9SRobert Watson #include <sys/sysctl.h>
51709557d9SRobert Watson #include <sys/sysent.h>
52709557d9SRobert Watson #include <sys/vnode.h>
53709557d9SRobert Watson 
54709557d9SRobert Watson #include <bsm/audit.h>
55709557d9SRobert Watson #include <bsm/audit_kevents.h>
56709557d9SRobert Watson #include <security/audit/audit.h>
57709557d9SRobert Watson #include <security/audit/audit_private.h>
58709557d9SRobert Watson 
59709557d9SRobert Watson /*
60709557d9SRobert Watson  * Hash table functions for the audit event number to event class mask
61709557d9SRobert Watson  * mapping.
62709557d9SRobert Watson  */
63709557d9SRobert Watson #define	EVCLASSMAP_HASH_TABLE_SIZE	251
64709557d9SRobert Watson struct evclass_elem {
65709557d9SRobert Watson 	au_event_t event;
66709557d9SRobert Watson 	au_class_t class;
67709557d9SRobert Watson 	LIST_ENTRY(evclass_elem) entry;
68709557d9SRobert Watson };
69709557d9SRobert Watson struct evclass_list {
70709557d9SRobert Watson 	LIST_HEAD(, evclass_elem) head;
71709557d9SRobert Watson };
72709557d9SRobert Watson 
73709557d9SRobert Watson static MALLOC_DEFINE(M_AUDITEVCLASS, "audit_evclass", "Audit event class");
74709557d9SRobert Watson static struct rwlock		evclass_lock;
75709557d9SRobert Watson static struct evclass_list	evclass_hash[EVCLASSMAP_HASH_TABLE_SIZE];
76709557d9SRobert Watson 
77709557d9SRobert Watson #define	EVCLASS_LOCK_INIT()	rw_init(&evclass_lock, "evclass_lock")
78709557d9SRobert Watson #define	EVCLASS_RLOCK()		rw_rlock(&evclass_lock)
79709557d9SRobert Watson #define	EVCLASS_RUNLOCK()	rw_runlock(&evclass_lock)
80709557d9SRobert Watson #define	EVCLASS_WLOCK()		rw_wlock(&evclass_lock)
81709557d9SRobert Watson #define	EVCLASS_WUNLOCK()	rw_wunlock(&evclass_lock)
82709557d9SRobert Watson 
83709557d9SRobert Watson /*
84709557d9SRobert Watson  * Hash table maintaining a mapping from audit event numbers to audit event
85709557d9SRobert Watson  * names.  For now, used only by DTrace, but present always so that userspace
86709557d9SRobert Watson  * tools can register and inspect fields consistently even if DTrace is not
87709557d9SRobert Watson  * present.
88709557d9SRobert Watson  *
89709557d9SRobert Watson  * struct evname_elem is defined in audit_private.h so that audit_dtrace.c can
90709557d9SRobert Watson  * use the definition.
91709557d9SRobert Watson  */
92*deea362cSRobert Watson #define	EVNAMEMAP_HASH_TABLE_MODULE	"etc_security_audit_event"
93709557d9SRobert Watson #define	EVNAMEMAP_HASH_TABLE_SIZE	251
94709557d9SRobert Watson struct evname_list {
95709557d9SRobert Watson 	LIST_HEAD(, evname_elem)	enl_head;
96709557d9SRobert Watson };
97709557d9SRobert Watson 
98709557d9SRobert Watson static MALLOC_DEFINE(M_AUDITEVNAME, "audit_evname", "Audit event name");
99709557d9SRobert Watson static struct sx		evnamemap_lock;
100709557d9SRobert Watson static struct evname_list	evnamemap_hash[EVNAMEMAP_HASH_TABLE_SIZE];
101709557d9SRobert Watson 
102709557d9SRobert Watson #define	EVNAMEMAP_LOCK_INIT()	sx_init(&evnamemap_lock, "evnamemap_lock");
103709557d9SRobert Watson #define	EVNAMEMAP_RLOCK()	sx_slock(&evnamemap_lock)
104709557d9SRobert Watson #define	EVNAMEMAP_RUNLOCK()	sx_sunlock(&evnamemap_lock)
105709557d9SRobert Watson #define	EVNAMEMAP_WLOCK()	sx_xlock(&evnamemap_lock)
106709557d9SRobert Watson #define	EVNAMEMAP_WUNLOCK()	sx_xunlock(&evnamemap_lock)
107709557d9SRobert Watson 
108709557d9SRobert Watson /*
109709557d9SRobert Watson  * Look up the class for an audit event in the class mapping table.
110709557d9SRobert Watson  */
111709557d9SRobert Watson au_class_t
au_event_class(au_event_t event)112709557d9SRobert Watson au_event_class(au_event_t event)
113709557d9SRobert Watson {
114709557d9SRobert Watson 	struct evclass_list *evcl;
115709557d9SRobert Watson 	struct evclass_elem *evc;
116709557d9SRobert Watson 	au_class_t class;
117709557d9SRobert Watson 
118709557d9SRobert Watson 	EVCLASS_RLOCK();
119709557d9SRobert Watson 	evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
120709557d9SRobert Watson 	class = 0;
121709557d9SRobert Watson 	LIST_FOREACH(evc, &evcl->head, entry) {
122709557d9SRobert Watson 		if (evc->event == event) {
123709557d9SRobert Watson 			class = evc->class;
124709557d9SRobert Watson 			goto out;
125709557d9SRobert Watson 		}
126709557d9SRobert Watson 	}
127709557d9SRobert Watson out:
128709557d9SRobert Watson 	EVCLASS_RUNLOCK();
129709557d9SRobert Watson 	return (class);
130709557d9SRobert Watson }
131709557d9SRobert Watson 
132709557d9SRobert Watson /*
133709557d9SRobert Watson  * Insert a event to class mapping. If the event already exists in the
134709557d9SRobert Watson  * mapping, then replace the mapping with the new one.
135709557d9SRobert Watson  *
136709557d9SRobert Watson  * XXX There is currently no constraints placed on the number of mappings.
137709557d9SRobert Watson  * May want to either limit to a number, or in terms of memory usage.
138709557d9SRobert Watson  */
139709557d9SRobert Watson void
au_evclassmap_insert(au_event_t event,au_class_t class)140709557d9SRobert Watson au_evclassmap_insert(au_event_t event, au_class_t class)
141709557d9SRobert Watson {
142709557d9SRobert Watson 	struct evclass_list *evcl;
143709557d9SRobert Watson 	struct evclass_elem *evc, *evc_new;
144709557d9SRobert Watson 
145709557d9SRobert Watson 	/*
146709557d9SRobert Watson 	 * Pessimistically, always allocate storage before acquiring mutex.
147709557d9SRobert Watson 	 * Free if there is already a mapping for this event.
148709557d9SRobert Watson 	 */
149709557d9SRobert Watson 	evc_new = malloc(sizeof(*evc), M_AUDITEVCLASS, M_WAITOK);
150709557d9SRobert Watson 
151709557d9SRobert Watson 	EVCLASS_WLOCK();
152709557d9SRobert Watson 	evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
153709557d9SRobert Watson 	LIST_FOREACH(evc, &evcl->head, entry) {
154709557d9SRobert Watson 		if (evc->event == event) {
155709557d9SRobert Watson 			evc->class = class;
156709557d9SRobert Watson 			EVCLASS_WUNLOCK();
157709557d9SRobert Watson 			free(evc_new, M_AUDITEVCLASS);
158709557d9SRobert Watson 			return;
159709557d9SRobert Watson 		}
160709557d9SRobert Watson 	}
161709557d9SRobert Watson 	evc = evc_new;
162709557d9SRobert Watson 	evc->event = event;
163709557d9SRobert Watson 	evc->class = class;
164709557d9SRobert Watson 	LIST_INSERT_HEAD(&evcl->head, evc, entry);
165709557d9SRobert Watson 	EVCLASS_WUNLOCK();
166709557d9SRobert Watson }
167709557d9SRobert Watson 
168709557d9SRobert Watson void
au_evclassmap_init(void)169709557d9SRobert Watson au_evclassmap_init(void)
170709557d9SRobert Watson {
171709557d9SRobert Watson 	int i;
172709557d9SRobert Watson 
173709557d9SRobert Watson 	EVCLASS_LOCK_INIT();
174709557d9SRobert Watson 	for (i = 0; i < EVCLASSMAP_HASH_TABLE_SIZE; i++)
175709557d9SRobert Watson 		LIST_INIT(&evclass_hash[i].head);
176709557d9SRobert Watson 
177709557d9SRobert Watson 	/*
178709557d9SRobert Watson 	 * Set up the initial event to class mapping for system calls.
179709557d9SRobert Watson 	 *
180709557d9SRobert Watson 	 * XXXRW: Really, this should walk all possible audit events, not all
181709557d9SRobert Watson 	 * native ABI system calls, as there may be audit events reachable
182709557d9SRobert Watson 	 * only through non-native system calls.  It also seems a shame to
183709557d9SRobert Watson 	 * frob the mutex this early.
184709557d9SRobert Watson 	 */
185709557d9SRobert Watson 	for (i = 0; i < SYS_MAXSYSCALL; i++) {
186709557d9SRobert Watson 		if (sysent[i].sy_auevent != AUE_NULL)
187709557d9SRobert Watson 			au_evclassmap_insert(sysent[i].sy_auevent, 0);
188709557d9SRobert Watson 	}
189709557d9SRobert Watson }
190709557d9SRobert Watson 
191709557d9SRobert Watson /*
192709557d9SRobert Watson  * Look up the name for an audit event in the event-to-name mapping table.
193709557d9SRobert Watson  */
194709557d9SRobert Watson int
au_event_name(au_event_t event,char * name)195709557d9SRobert Watson au_event_name(au_event_t event, char *name)
196709557d9SRobert Watson {
197709557d9SRobert Watson 	struct evname_list *enl;
198709557d9SRobert Watson 	struct evname_elem *ene;
199709557d9SRobert Watson 	int error;
200709557d9SRobert Watson 
201709557d9SRobert Watson 	error = ENOENT;
202709557d9SRobert Watson 	EVNAMEMAP_RLOCK();
203709557d9SRobert Watson 	enl = &evnamemap_hash[event % EVNAMEMAP_HASH_TABLE_SIZE];
204709557d9SRobert Watson 	LIST_FOREACH(ene, &enl->enl_head, ene_entry) {
205709557d9SRobert Watson 		if (ene->ene_event == event) {
206709557d9SRobert Watson 			strlcpy(name, ene->ene_name, EVNAMEMAP_NAME_SIZE);
207709557d9SRobert Watson 			error = 0;
208709557d9SRobert Watson 			goto out;
209709557d9SRobert Watson 		}
210709557d9SRobert Watson 	}
211709557d9SRobert Watson out:
212709557d9SRobert Watson 	EVNAMEMAP_RUNLOCK();
213709557d9SRobert Watson 	return (error);
214709557d9SRobert Watson }
215709557d9SRobert Watson 
216709557d9SRobert Watson /*
217709557d9SRobert Watson  * Insert a event-to-name mapping.  If the event already exists in the
218709557d9SRobert Watson  * mapping, then replace the mapping with the new one.
219709557d9SRobert Watson  *
220709557d9SRobert Watson  * XXX There is currently no constraints placed on the number of mappings.
221709557d9SRobert Watson  * May want to either limit to a number, or in terms of memory usage.
222709557d9SRobert Watson  *
223709557d9SRobert Watson  * XXXRW: Accepts truncated name -- but perhaps should return failure instead?
224709557d9SRobert Watson  *
225709557d9SRobert Watson  * XXXRW: It could be we need a way to remove existing names...?
226709557d9SRobert Watson  *
227709557d9SRobert Watson  * XXXRW: We handle collisions between numbers, but I wonder if we also need a
228709557d9SRobert Watson  * way to handle name collisions, for DTrace, where probe names must be
229709557d9SRobert Watson  * unique?
230709557d9SRobert Watson  */
231709557d9SRobert Watson void
au_evnamemap_insert(au_event_t event,const char * name)232709557d9SRobert Watson au_evnamemap_insert(au_event_t event, const char *name)
233709557d9SRobert Watson {
234709557d9SRobert Watson 	struct evname_list *enl;
235709557d9SRobert Watson 	struct evname_elem *ene, *ene_new;
236709557d9SRobert Watson 
237709557d9SRobert Watson 	/*
238709557d9SRobert Watson 	 * Pessimistically, always allocate storage before acquiring lock.
239709557d9SRobert Watson 	 * Free if there is already a mapping for this event.
240709557d9SRobert Watson 	 */
241709557d9SRobert Watson 	ene_new = malloc(sizeof(*ene_new), M_AUDITEVNAME, M_WAITOK | M_ZERO);
242709557d9SRobert Watson 	EVNAMEMAP_WLOCK();
243709557d9SRobert Watson 	enl = &evnamemap_hash[event % EVNAMEMAP_HASH_TABLE_SIZE];
244709557d9SRobert Watson 	LIST_FOREACH(ene, &enl->enl_head, ene_entry) {
245709557d9SRobert Watson 		if (ene->ene_event == event) {
246709557d9SRobert Watson 			EVNAME_LOCK(ene);
247709557d9SRobert Watson 			(void)strlcpy(ene->ene_name, name,
248709557d9SRobert Watson 			    sizeof(ene->ene_name));
249709557d9SRobert Watson 			EVNAME_UNLOCK(ene);
250709557d9SRobert Watson 			EVNAMEMAP_WUNLOCK();
251709557d9SRobert Watson 			free(ene_new, M_AUDITEVNAME);
252709557d9SRobert Watson 			return;
253709557d9SRobert Watson 		}
254709557d9SRobert Watson 	}
255709557d9SRobert Watson 	ene = ene_new;
256709557d9SRobert Watson 	mtx_init(&ene->ene_lock, "au_evnamemap", NULL, MTX_DEF);
257709557d9SRobert Watson 	ene->ene_event = event;
258709557d9SRobert Watson 	(void)strlcpy(ene->ene_name, name, sizeof(ene->ene_name));
259709557d9SRobert Watson 	LIST_INSERT_HEAD(&enl->enl_head, ene, ene_entry);
260709557d9SRobert Watson 	EVNAMEMAP_WUNLOCK();
261709557d9SRobert Watson }
262709557d9SRobert Watson 
263*deea362cSRobert Watson /*
264*deea362cSRobert Watson  * If /etc/security/audit_event has been preloaded by the boot loader, parse
265*deea362cSRobert Watson  * it to build an initial set of event number<->name mappings.
266*deea362cSRobert Watson  */
267*deea362cSRobert Watson static void
au_evnamemap_init_preload(void)268*deea362cSRobert Watson au_evnamemap_init_preload(void)
269*deea362cSRobert Watson {
270*deea362cSRobert Watson 	caddr_t kmdp;
271*deea362cSRobert Watson 	char *endptr, *line, *nextline, *ptr;
272*deea362cSRobert Watson 	const char *evnum_str, *evname;
273*deea362cSRobert Watson 	size_t size;
274*deea362cSRobert Watson 	long evnum;
275*deea362cSRobert Watson 	u_int lineno;
276*deea362cSRobert Watson 
277*deea362cSRobert Watson 	kmdp = preload_search_by_type(EVNAMEMAP_HASH_TABLE_MODULE);
278*deea362cSRobert Watson 	if (kmdp == NULL)
279*deea362cSRobert Watson 		return;
280*deea362cSRobert Watson 	ptr = preload_fetch_addr(kmdp);
281*deea362cSRobert Watson 	size = preload_fetch_size(kmdp);
282*deea362cSRobert Watson 
283*deea362cSRobert Watson 	/*
284*deea362cSRobert Watson 	 * Parse preloaded configuration file "in place".  Assume that the
285*deea362cSRobert Watson 	 * last character is a new line, meaning that we can replace it with a
286*deea362cSRobert Watson 	 * nul byte safely.  We can then use strsep(3) to process the full
287*deea362cSRobert Watson 	 * buffer.
288*deea362cSRobert Watson 	 */
289*deea362cSRobert Watson 	ptr[size - 1] = '\0';
290*deea362cSRobert Watson 
291*deea362cSRobert Watson 	/*
292*deea362cSRobert Watson 	 * Process line by line.
293*deea362cSRobert Watson 	 */
294*deea362cSRobert Watson 	nextline = ptr;
295*deea362cSRobert Watson 	lineno = 0;
296*deea362cSRobert Watson 	while ((line = strsep(&nextline, "\n")) != NULL) {
297*deea362cSRobert Watson 		/*
298*deea362cSRobert Watson 		 * Skip any leading white space.
299*deea362cSRobert Watson 		 */
300*deea362cSRobert Watson 		while (line[0] == ' ' || line[0] == '\t')
301*deea362cSRobert Watson 			line++;
302*deea362cSRobert Watson 
303*deea362cSRobert Watson 		/*
304*deea362cSRobert Watson 		 * Skip blank lines and comment lines.
305*deea362cSRobert Watson 		 */
306*deea362cSRobert Watson 		if (line[0] == '\0' || line[0] == '#') {
307*deea362cSRobert Watson 			lineno++;
308*deea362cSRobert Watson 			continue;
309*deea362cSRobert Watson 		}
310*deea362cSRobert Watson 
311*deea362cSRobert Watson 		/*
312*deea362cSRobert Watson 		 * Parse each line -- ":"-separated tuple of event number,
313*deea362cSRobert Watson 		 * event name, and other material we are less interested in.
314*deea362cSRobert Watson 		 */
315*deea362cSRobert Watson 		evnum_str = strsep(&line, ":");
316*deea362cSRobert Watson 		if (evnum_str == NULL || *evnum_str == '\0') {
317*deea362cSRobert Watson 			printf("%s: Invalid line %u - evnum strsep\n",
318*deea362cSRobert Watson 			    __func__, lineno);
319*deea362cSRobert Watson 			lineno++;
320*deea362cSRobert Watson 			continue;
321*deea362cSRobert Watson 		}
322*deea362cSRobert Watson 		evnum = strtol(evnum_str, &endptr, 10);
323*deea362cSRobert Watson 		if (*evnum_str == '\0' || *endptr != '\0' ||
324*deea362cSRobert Watson 		    evnum <= 0 || evnum > UINT16_MAX) {
325*deea362cSRobert Watson 			printf("%s: Invalid line %u - evnum strtol\n",
326*deea362cSRobert Watson 			    __func__, lineno);
327*deea362cSRobert Watson 			lineno++;
328*deea362cSRobert Watson 			continue;
329*deea362cSRobert Watson 		}
330*deea362cSRobert Watson 		evname = strsep(&line, ":");
331*deea362cSRobert Watson 		if (evname == NULL || *evname == '\0') {
332*deea362cSRobert Watson 			printf("%s: Invalid line %u - evname strsp\n",
333*deea362cSRobert Watson 			    __func__, lineno);
334*deea362cSRobert Watson 			lineno++;
335*deea362cSRobert Watson 			continue;
336*deea362cSRobert Watson 		}
337*deea362cSRobert Watson 		au_evnamemap_insert(evnum, evname);
338*deea362cSRobert Watson 		lineno++;
339*deea362cSRobert Watson 	}
340*deea362cSRobert Watson }
341*deea362cSRobert Watson 
342709557d9SRobert Watson void
au_evnamemap_init(void)343709557d9SRobert Watson au_evnamemap_init(void)
344709557d9SRobert Watson {
345709557d9SRobert Watson 	int i;
346709557d9SRobert Watson 
347709557d9SRobert Watson 	EVNAMEMAP_LOCK_INIT();
348709557d9SRobert Watson 	for (i = 0; i < EVNAMEMAP_HASH_TABLE_SIZE; i++)
349709557d9SRobert Watson 		LIST_INIT(&evnamemap_hash[i].enl_head);
350*deea362cSRobert Watson 	au_evnamemap_init_preload();
351709557d9SRobert Watson }
352709557d9SRobert Watson 
353709557d9SRobert Watson /*
354709557d9SRobert Watson  * The DTrace audit provider occasionally needs to walk the entries in the
355709557d9SRobert Watson  * event-to-name mapping table, and uses this public interface to do so.  A
356709557d9SRobert Watson  * write lock is acquired so that the provider can safely update its fields in
357709557d9SRobert Watson  * table entries.
358709557d9SRobert Watson  */
359709557d9SRobert Watson void
au_evnamemap_foreach(au_evnamemap_callback_t callback)360709557d9SRobert Watson au_evnamemap_foreach(au_evnamemap_callback_t callback)
361709557d9SRobert Watson {
362709557d9SRobert Watson 	struct evname_list *enl;
363709557d9SRobert Watson 	struct evname_elem *ene;
364709557d9SRobert Watson 	int i;
365709557d9SRobert Watson 
366709557d9SRobert Watson 	EVNAMEMAP_WLOCK();
367709557d9SRobert Watson 	for (i = 0; i < EVNAMEMAP_HASH_TABLE_SIZE; i++) {
368709557d9SRobert Watson 		enl = &evnamemap_hash[i];
369709557d9SRobert Watson 		LIST_FOREACH(ene, &enl->enl_head, ene_entry)
370709557d9SRobert Watson 			callback(ene);
371709557d9SRobert Watson 	}
372709557d9SRobert Watson 	EVNAMEMAP_WUNLOCK();
373709557d9SRobert Watson }
374709557d9SRobert Watson 
375709557d9SRobert Watson #ifdef KDTRACE_HOOKS
376709557d9SRobert Watson /*
377709557d9SRobert Watson  * Look up an event-to-name mapping table entry by event number.  As evname
378709557d9SRobert Watson  * elements are stable in memory, we can return the pointer without the table
379709557d9SRobert Watson  * lock held -- but the caller will need to lock the element mutex before
380709557d9SRobert Watson  * accessing element fields.
381709557d9SRobert Watson  *
382709557d9SRobert Watson  * NB: the event identifier in elements is stable and can be read without
383709557d9SRobert Watson  * holding the evname_elem lock.
384709557d9SRobert Watson  */
385709557d9SRobert Watson struct evname_elem *
au_evnamemap_lookup(au_event_t event)386709557d9SRobert Watson au_evnamemap_lookup(au_event_t event)
387709557d9SRobert Watson {
388709557d9SRobert Watson 	struct evname_list *enl;
389709557d9SRobert Watson 	struct evname_elem *ene;
390709557d9SRobert Watson 
391709557d9SRobert Watson 	EVNAMEMAP_RLOCK();
392709557d9SRobert Watson 	enl = &evnamemap_hash[event % EVNAMEMAP_HASH_TABLE_SIZE];
393709557d9SRobert Watson 	LIST_FOREACH(ene, &enl->enl_head, ene_entry) {
394709557d9SRobert Watson 		if (ene->ene_event == event)
395709557d9SRobert Watson 			goto out;
396709557d9SRobert Watson 	}
397709557d9SRobert Watson 	ene = NULL;
398709557d9SRobert Watson out:
399709557d9SRobert Watson 	EVNAMEMAP_RUNLOCK();
400709557d9SRobert Watson 	return (ene);
401709557d9SRobert Watson }
402709557d9SRobert Watson #endif /* !KDTRACE_HOOKS */
403