audit_pipe.c revision 174894
1296177Sjhibbits/*-
2296177Sjhibbits * Copyright (c) 2006 Robert N. M. Watson
3296177Sjhibbits * All rights reserved.
4296177Sjhibbits *
5296177Sjhibbits * This software was developed by Robert Watson for the TrustedBSD Project.
6296177Sjhibbits *
7296177Sjhibbits * Redistribution and use in source and binary forms, with or without
8296177Sjhibbits * modification, are permitted provided that the following conditions
9296177Sjhibbits * are met:
10296177Sjhibbits * 1. Redistributions of source code must retain the above copyright
11296177Sjhibbits *    notice, this list of conditions and the following disclaimer.
12296177Sjhibbits * 2. Redistributions in binary form must reproduce the above copyright
13296177Sjhibbits *    notice, this list of conditions and the following disclaimer in the
14296177Sjhibbits *    documentation and/or other materials provided with the distribution.
15296177Sjhibbits *
16296177Sjhibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17296177Sjhibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18296177Sjhibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19296177Sjhibbits * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20296177Sjhibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21296177Sjhibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22296177Sjhibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23296177Sjhibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24296177Sjhibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25296177Sjhibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26296177Sjhibbits * SUCH DAMAGE.
27296177Sjhibbits *
28296177Sjhibbits * $FreeBSD: head/sys/security/audit/audit_pipe.c 174894 2007-12-25 13:23:19Z wkoszek $
29296177Sjhibbits */
30296177Sjhibbits
31296177Sjhibbits#include <sys/param.h>
32296177Sjhibbits#include <sys/condvar.h>
33296177Sjhibbits#include <sys/conf.h>
34296177Sjhibbits#include <sys/eventhandler.h>
35296177Sjhibbits#include <sys/filio.h>
36296177Sjhibbits#include <sys/kernel.h>
37296177Sjhibbits#include <sys/lock.h>
38296177Sjhibbits#include <sys/malloc.h>
39296177Sjhibbits#include <sys/mutex.h>
40296177Sjhibbits#include <sys/poll.h>
41296177Sjhibbits#include <sys/proc.h>
42296177Sjhibbits#include <sys/queue.h>
43296177Sjhibbits#include <sys/selinfo.h>
44296177Sjhibbits#include <sys/sigio.h>
45296177Sjhibbits#include <sys/signal.h>
46296177Sjhibbits#include <sys/signalvar.h>
47296177Sjhibbits#include <sys/systm.h>
48296177Sjhibbits#include <sys/uio.h>
49296177Sjhibbits
50296177Sjhibbits#include <security/audit/audit.h>
51296177Sjhibbits#include <security/audit/audit_ioctl.h>
52296177Sjhibbits#include <security/audit/audit_private.h>
53296177Sjhibbits
54296177Sjhibbits/*
55296177Sjhibbits * Implementation of a clonable special device providing a live stream of BSM
56296177Sjhibbits * audit data.  This is a "tee" of the data going to the file.  It provides
57296177Sjhibbits * unreliable but timely access to audit events.  Consumers of this interface
58296177Sjhibbits * should be very careful to avoid introducing event cycles.  Consumers may
59296177Sjhibbits * express interest via a set of preselection ioctls.
60296177Sjhibbits */
61296177Sjhibbits
62296177Sjhibbits/*
63296177Sjhibbits * Memory types.
64296177Sjhibbits */
65296177Sjhibbitsstatic MALLOC_DEFINE(M_AUDIT_PIPE, "audit_pipe", "Audit pipes");
66296177Sjhibbitsstatic MALLOC_DEFINE(M_AUDIT_PIPE_ENTRY, "audit_pipeent",
67296177Sjhibbits    "Audit pipe entries and buffers");
68296177Sjhibbitsstatic MALLOC_DEFINE(M_AUDIT_PIPE_PRESELECT, "audit_pipe_presel",
69296177Sjhibbits    "Audit pipe preselection structure");
70296177Sjhibbits
71296177Sjhibbits/*
72296177Sjhibbits * Audit pipe buffer parameters.
73296177Sjhibbits */
74296177Sjhibbits#define	AUDIT_PIPE_QLIMIT_DEFAULT	(128)
75296177Sjhibbits#define	AUDIT_PIPE_QLIMIT_MIN		(0)
76296177Sjhibbits#define	AUDIT_PIPE_QLIMIT_MAX		(1024)
77296177Sjhibbits
78296177Sjhibbits/*
79296177Sjhibbits * Description of an entry in an audit_pipe.
80296177Sjhibbits */
81296177Sjhibbitsstruct audit_pipe_entry {
82296177Sjhibbits	void				*ape_record;
83296177Sjhibbits	u_int				 ape_record_len;
84296177Sjhibbits	TAILQ_ENTRY(audit_pipe_entry)	 ape_queue;
85296177Sjhibbits};
86296177Sjhibbits
87296177Sjhibbits/*
88296177Sjhibbits * Audit pipes allow processes to express "interest" in the set of records
89296177Sjhibbits * that are delivered via the pipe.  They do this in a similar manner to the
90296177Sjhibbits * mechanism for audit trail configuration, by expressing two global masks,
91296177Sjhibbits * and optionally expressing per-auid masks.  The following data structure is
92296177Sjhibbits * the per-auid mask description.  The global state is stored in the audit
93296177Sjhibbits * pipe data structure.
94296177Sjhibbits *
95296177Sjhibbits * We may want to consider a more space/time-efficient data structure once
96296177Sjhibbits * usage patterns for per-auid specifications are clear.
97296177Sjhibbits */
98296177Sjhibbitsstruct audit_pipe_preselect {
99296177Sjhibbits	au_id_t					 app_auid;
100296177Sjhibbits	au_mask_t				 app_mask;
101296177Sjhibbits	TAILQ_ENTRY(audit_pipe_preselect)	 app_list;
102296177Sjhibbits};
103296177Sjhibbits
104296177Sjhibbits/*
105296177Sjhibbits * Description of an individual audit_pipe.  Consists largely of a bounded
106296177Sjhibbits * length queue.
107296177Sjhibbits */
108296177Sjhibbits#define	AUDIT_PIPE_ASYNC	0x00000001
109296177Sjhibbits#define	AUDIT_PIPE_NBIO		0x00000002
110296177Sjhibbitsstruct audit_pipe {
111296177Sjhibbits	int				 ap_open;	/* Device open? */
112296177Sjhibbits	u_int				 ap_flags;
113296177Sjhibbits
114296177Sjhibbits	struct selinfo			 ap_selinfo;
115296177Sjhibbits	struct sigio			*ap_sigio;
116296177Sjhibbits
117296177Sjhibbits	u_int				 ap_qlen;
118296177Sjhibbits	u_int				 ap_qlimit;
119296177Sjhibbits
120296177Sjhibbits	u_int64_t			 ap_inserts;	/* Records added. */
121296177Sjhibbits	u_int64_t			 ap_reads;	/* Records read. */
122296177Sjhibbits	u_int64_t			 ap_drops;	/* Records dropped. */
123296177Sjhibbits	u_int64_t			 ap_truncates;	/* Records too long. */
124296177Sjhibbits
125296177Sjhibbits	/*
126296177Sjhibbits	 * Fields relating to pipe interest: global masks for unmatched
127296177Sjhibbits	 * processes (attributable, non-attributable), and a list of specific
128296177Sjhibbits	 * interest specifications by auid.
129296177Sjhibbits	 */
130296177Sjhibbits	int				 ap_preselect_mode;
131296177Sjhibbits	au_mask_t			 ap_preselect_flags;
132296177Sjhibbits	au_mask_t			 ap_preselect_naflags;
133296177Sjhibbits	TAILQ_HEAD(, audit_pipe_preselect)	ap_preselect_list;
134296177Sjhibbits
135296177Sjhibbits	/*
136296177Sjhibbits	 * Current pending record list.
137296177Sjhibbits	 */
138296177Sjhibbits	TAILQ_HEAD(, audit_pipe_entry)	 ap_queue;
139296177Sjhibbits
140296177Sjhibbits	/*
141296177Sjhibbits	 * Global pipe list.
142296177Sjhibbits	 */
143296177Sjhibbits	TAILQ_ENTRY(audit_pipe)		 ap_list;
144296177Sjhibbits};
145296177Sjhibbits
146296177Sjhibbits/*
147296177Sjhibbits * Global list of audit pipes, mutex to protect it and the pipes.  Finer
148296177Sjhibbits * grained locking may be desirable at some point.
149296177Sjhibbits */
150296177Sjhibbitsstatic TAILQ_HEAD(, audit_pipe)	 audit_pipe_list;
151296177Sjhibbitsstatic struct mtx		 audit_pipe_mtx;
152296177Sjhibbits
153296177Sjhibbits/*
154296177Sjhibbits * This CV is used to wakeup on an audit record write.  Eventually, it might
155296177Sjhibbits * be per-pipe to avoid unnecessary wakeups when several pipes with different
156296177Sjhibbits * preselection masks are present.
157296177Sjhibbits */
158296177Sjhibbitsstatic struct cv		 audit_pipe_cv;
159296177Sjhibbits
160296177Sjhibbits/*
161296177Sjhibbits * Cloning related variables and constants.
162296177Sjhibbits */
163296177Sjhibbits#define	AUDIT_PIPE_NAME		"auditpipe"
164296177Sjhibbitsstatic eventhandler_tag		 audit_pipe_eh_tag;
165296177Sjhibbitsstatic struct clonedevs		*audit_pipe_clones;
166296177Sjhibbits
167296177Sjhibbits/*
168296177Sjhibbits * Special device methods and definition.
169296177Sjhibbits */
170296177Sjhibbitsstatic d_open_t		audit_pipe_open;
171296177Sjhibbitsstatic d_close_t	audit_pipe_close;
172296177Sjhibbitsstatic d_read_t		audit_pipe_read;
173296177Sjhibbitsstatic d_ioctl_t	audit_pipe_ioctl;
174296177Sjhibbitsstatic d_poll_t		audit_pipe_poll;
175296177Sjhibbitsstatic d_kqfilter_t	audit_pipe_kqfilter;
176296177Sjhibbits
177296177Sjhibbitsstatic struct cdevsw	audit_pipe_cdevsw = {
178296177Sjhibbits	.d_version =	D_VERSION,
179296177Sjhibbits	.d_flags =	D_PSEUDO | D_NEEDGIANT,
180296177Sjhibbits	.d_open =	audit_pipe_open,
181296177Sjhibbits	.d_close =	audit_pipe_close,
182296177Sjhibbits	.d_read =	audit_pipe_read,
183296177Sjhibbits	.d_ioctl =	audit_pipe_ioctl,
184296177Sjhibbits	.d_poll =	audit_pipe_poll,
185296177Sjhibbits	.d_kqfilter =	audit_pipe_kqfilter,
186296177Sjhibbits	.d_name =	AUDIT_PIPE_NAME,
187296177Sjhibbits};
188296177Sjhibbits
189296177Sjhibbitsstatic int	audit_pipe_kqread(struct knote *note, long hint);
190296177Sjhibbitsstatic void	audit_pipe_kqdetach(struct knote *note);
191296177Sjhibbits
192296177Sjhibbitsstatic struct filterops audit_pipe_read_filterops = {
193296177Sjhibbits	.f_isfd =	1,
194296177Sjhibbits	.f_attach =	NULL,
195296177Sjhibbits	.f_detach =	audit_pipe_kqdetach,
196296177Sjhibbits	.f_event =	audit_pipe_kqread,
197296177Sjhibbits};
198296177Sjhibbits
199296177Sjhibbits/*
200296177Sjhibbits * Some global statistics on audit pipes.
201296177Sjhibbits */
202296177Sjhibbitsstatic int		audit_pipe_count;	/* Current number of pipes. */
203296177Sjhibbitsstatic u_int64_t	audit_pipe_ever;	/* Pipes ever allocated. */
204296177Sjhibbitsstatic u_int64_t	audit_pipe_records;	/* Records seen. */
205296177Sjhibbitsstatic u_int64_t	audit_pipe_drops;	/* Global record drop count. */
206296177Sjhibbits
207296177Sjhibbits/*
208296177Sjhibbits * Free an audit pipe entry.
209296177Sjhibbits */
210296177Sjhibbitsstatic void
211296177Sjhibbitsaudit_pipe_entry_free(struct audit_pipe_entry *ape)
212296177Sjhibbits{
213296177Sjhibbits
214296177Sjhibbits	free(ape->ape_record, M_AUDIT_PIPE_ENTRY);
215296177Sjhibbits	free(ape, M_AUDIT_PIPE_ENTRY);
216296177Sjhibbits}
217296177Sjhibbits
218296177Sjhibbits/*
219296177Sjhibbits * Find an audit pipe preselection specification for an auid, if any.
220296177Sjhibbits */
221296177Sjhibbitsstatic struct audit_pipe_preselect *
222296177Sjhibbitsaudit_pipe_preselect_find(struct audit_pipe *ap, au_id_t auid)
223296177Sjhibbits{
224296177Sjhibbits	struct audit_pipe_preselect *app;
225296177Sjhibbits
226296177Sjhibbits	mtx_assert(&audit_pipe_mtx, MA_OWNED);
227296177Sjhibbits
228296177Sjhibbits	TAILQ_FOREACH(app, &ap->ap_preselect_list, app_list) {
229296177Sjhibbits		if (app->app_auid == auid)
230296177Sjhibbits			return (app);
231296177Sjhibbits	}
232296177Sjhibbits	return (NULL);
233296177Sjhibbits}
234296177Sjhibbits
235296177Sjhibbits/*
236296177Sjhibbits * Query the per-pipe mask for a specific auid.
237296177Sjhibbits */
238296177Sjhibbitsstatic int
239296177Sjhibbitsaudit_pipe_preselect_get(struct audit_pipe *ap, au_id_t auid,
240296177Sjhibbits    au_mask_t *maskp)
241296177Sjhibbits{
242296177Sjhibbits	struct audit_pipe_preselect *app;
243296177Sjhibbits	int error;
244296177Sjhibbits
245296177Sjhibbits	mtx_lock(&audit_pipe_mtx);
246296177Sjhibbits	app = audit_pipe_preselect_find(ap, auid);
247296177Sjhibbits	if (app != NULL) {
248296177Sjhibbits		*maskp = app->app_mask;
249296177Sjhibbits		error = 0;
250296177Sjhibbits	} else
251296177Sjhibbits		error = ENOENT;
252296177Sjhibbits	mtx_unlock(&audit_pipe_mtx);
253296177Sjhibbits	return (error);
254296177Sjhibbits}
255296177Sjhibbits
256296177Sjhibbits/*
257296177Sjhibbits * Set the per-pipe mask for a specific auid.  Add a new entry if needed;
258296177Sjhibbits * otherwise, update the current entry.
259296177Sjhibbits */
260296177Sjhibbitsstatic void
261296177Sjhibbitsaudit_pipe_preselect_set(struct audit_pipe *ap, au_id_t auid, au_mask_t mask)
262296177Sjhibbits{
263296177Sjhibbits	struct audit_pipe_preselect *app, *app_new;
264296177Sjhibbits
265296177Sjhibbits	/*
266296177Sjhibbits	 * Pessimistically assume that the auid doesn't already have a mask
267296177Sjhibbits	 * set, and allocate.  We will free it if it is unneeded.
268296177Sjhibbits	 */
269296177Sjhibbits	app_new = malloc(sizeof(*app_new), M_AUDIT_PIPE_PRESELECT, M_WAITOK);
270296177Sjhibbits	mtx_lock(&audit_pipe_mtx);
271296177Sjhibbits	app = audit_pipe_preselect_find(ap, auid);
272296177Sjhibbits	if (app == NULL) {
273296177Sjhibbits		app = app_new;
274296177Sjhibbits		app_new = NULL;
275296177Sjhibbits		app->app_auid = auid;
276296177Sjhibbits		TAILQ_INSERT_TAIL(&ap->ap_preselect_list, app, app_list);
277296177Sjhibbits	}
278296177Sjhibbits	app->app_mask = mask;
279296177Sjhibbits	mtx_unlock(&audit_pipe_mtx);
280296177Sjhibbits	if (app_new != NULL)
281296177Sjhibbits		free(app_new, M_AUDIT_PIPE_PRESELECT);
282296177Sjhibbits}
283296177Sjhibbits
284296177Sjhibbits/*
285296177Sjhibbits * Delete a per-auid mask on an audit pipe.
286296177Sjhibbits */
287296177Sjhibbitsstatic int
288296177Sjhibbitsaudit_pipe_preselect_delete(struct audit_pipe *ap, au_id_t auid)
289296177Sjhibbits{
290296177Sjhibbits	struct audit_pipe_preselect *app;
291296177Sjhibbits	int error;
292296177Sjhibbits
293296177Sjhibbits	mtx_lock(&audit_pipe_mtx);
294296177Sjhibbits	app = audit_pipe_preselect_find(ap, auid);
295296177Sjhibbits	if (app != NULL) {
296296177Sjhibbits		TAILQ_REMOVE(&ap->ap_preselect_list, app, app_list);
297296177Sjhibbits		error = 0;
298296177Sjhibbits	} else
299296177Sjhibbits		error = ENOENT;
300296177Sjhibbits	mtx_unlock(&audit_pipe_mtx);
301296177Sjhibbits	if (app != NULL)
302296177Sjhibbits		free(app, M_AUDIT_PIPE_PRESELECT);
303296177Sjhibbits	return (error);
304296177Sjhibbits}
305296177Sjhibbits
306296177Sjhibbits/*
307296177Sjhibbits * Delete all per-auid masks on an audit pipe.
308296177Sjhibbits */
309296177Sjhibbitsstatic void
310296177Sjhibbitsaudit_pipe_preselect_flush_locked(struct audit_pipe *ap)
311296177Sjhibbits{
312296177Sjhibbits	struct audit_pipe_preselect *app;
313
314	mtx_assert(&audit_pipe_mtx, MA_OWNED);
315
316	while ((app = TAILQ_FIRST(&ap->ap_preselect_list)) != NULL) {
317		TAILQ_REMOVE(&ap->ap_preselect_list, app, app_list);
318		free(app, M_AUDIT_PIPE_PRESELECT);
319	}
320}
321
322static void
323audit_pipe_preselect_flush(struct audit_pipe *ap)
324{
325
326	mtx_lock(&audit_pipe_mtx);
327	audit_pipe_preselect_flush_locked(ap);
328	mtx_unlock(&audit_pipe_mtx);
329}
330
331/*-
332 * Determine whether a specific audit pipe matches a record with these
333 * properties.  Algorithm is as follows:
334 *
335 * - If the pipe is configured to track the default trail configuration, then
336 *   use the results of global preselection matching.
337 * - If not, search for a specifically configured auid entry matching the
338 *   event.  If an entry is found, use that.
339 * - Otherwise, use the default flags or naflags configured for the pipe.
340 */
341static int
342audit_pipe_preselect_check(struct audit_pipe *ap, au_id_t auid,
343    au_event_t event, au_class_t class, int sorf, int trail_preselect)
344{
345	struct audit_pipe_preselect *app;
346
347	mtx_assert(&audit_pipe_mtx, MA_OWNED);
348
349	switch (ap->ap_preselect_mode) {
350	case AUDITPIPE_PRESELECT_MODE_TRAIL:
351		return (trail_preselect);
352
353	case AUDITPIPE_PRESELECT_MODE_LOCAL:
354		app = audit_pipe_preselect_find(ap, auid);
355		if (app == NULL) {
356			if (auid == AU_DEFAUDITID)
357				return (au_preselect(event, class,
358				    &ap->ap_preselect_naflags, sorf));
359			else
360				return (au_preselect(event, class,
361				    &ap->ap_preselect_flags, sorf));
362		} else
363			return (au_preselect(event, class, &app->app_mask,
364			    sorf));
365
366	default:
367		panic("audit_pipe_preselect_check: mode %d",
368		    ap->ap_preselect_mode);
369	}
370
371	return (0);
372}
373
374/*
375 * Determine whether there exists a pipe interested in a record with specific
376 * properties.
377 */
378int
379audit_pipe_preselect(au_id_t auid, au_event_t event, au_class_t class,
380    int sorf, int trail_preselect)
381{
382	struct audit_pipe *ap;
383
384	mtx_lock(&audit_pipe_mtx);
385	TAILQ_FOREACH(ap, &audit_pipe_list, ap_list) {
386		if (audit_pipe_preselect_check(ap, auid, event, class, sorf,
387		    trail_preselect)) {
388			mtx_unlock(&audit_pipe_mtx);
389			return (1);
390		}
391	}
392	mtx_unlock(&audit_pipe_mtx);
393	return (0);
394}
395
396/*
397 * Append individual record to a queue -- allocate queue-local buffer, and
398 * add to the queue.  We try to drop from the head of the queue so that more
399 * recent events take precedence over older ones, but if allocation fails we
400 * do drop the new event.
401 */
402static void
403audit_pipe_append(struct audit_pipe *ap, void *record, u_int record_len)
404{
405	struct audit_pipe_entry *ape, *ape_remove;
406
407	mtx_assert(&audit_pipe_mtx, MA_OWNED);
408
409	ape = malloc(sizeof(*ape), M_AUDIT_PIPE_ENTRY, M_NOWAIT | M_ZERO);
410	if (ape == NULL) {
411		ap->ap_drops++;
412		audit_pipe_drops++;
413		return;
414	}
415
416	ape->ape_record = malloc(record_len, M_AUDIT_PIPE_ENTRY, M_NOWAIT);
417	if (ape->ape_record == NULL) {
418		free(ape, M_AUDIT_PIPE_ENTRY);
419		ap->ap_drops++;
420		audit_pipe_drops++;
421		return;
422	}
423
424	bcopy(record, ape->ape_record, record_len);
425	ape->ape_record_len = record_len;
426
427	if (ap->ap_qlen >= ap->ap_qlimit) {
428		ape_remove = TAILQ_FIRST(&ap->ap_queue);
429		TAILQ_REMOVE(&ap->ap_queue, ape_remove, ape_queue);
430		audit_pipe_entry_free(ape_remove);
431		ap->ap_qlen--;
432		ap->ap_drops++;
433		audit_pipe_drops++;
434	}
435
436	TAILQ_INSERT_TAIL(&ap->ap_queue, ape, ape_queue);
437	ap->ap_inserts++;
438	ap->ap_qlen++;
439	selwakeuppri(&ap->ap_selinfo, PSOCK);
440	KNOTE_LOCKED(&ap->ap_selinfo.si_note, 0);
441	if (ap->ap_flags & AUDIT_PIPE_ASYNC)
442		pgsigio(&ap->ap_sigio, SIGIO, 0);
443}
444
445/*
446 * audit_pipe_submit(): audit_worker submits audit records via this
447 * interface, which arranges for them to be delivered to pipe queues.
448 */
449void
450audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class, int sorf,
451    int trail_select, void *record, u_int record_len)
452{
453	struct audit_pipe *ap;
454
455	/*
456	 * Lockless read to avoid mutex overhead if pipes are not in use.
457	 */
458	if (TAILQ_FIRST(&audit_pipe_list) == NULL)
459		return;
460
461	mtx_lock(&audit_pipe_mtx);
462	TAILQ_FOREACH(ap, &audit_pipe_list, ap_list) {
463		if (audit_pipe_preselect_check(ap, auid, event, class, sorf,
464		    trail_select))
465			audit_pipe_append(ap, record, record_len);
466	}
467	audit_pipe_records++;
468	mtx_unlock(&audit_pipe_mtx);
469	cv_broadcastpri(&audit_pipe_cv, PSOCK);
470}
471
472/*
473 * audit_pipe_submit_user(): the same as audit_pipe_submit(), except that
474 * since we don't currently have selection information available, it is
475 * delivered to the pipe unconditionally.
476 *
477 * XXXRW: This is a bug.  The BSM check routine for submitting a user record
478 * should parse that information and return it.
479 */
480void
481audit_pipe_submit_user(void *record, u_int record_len)
482{
483	struct audit_pipe *ap;
484
485	/*
486	 * Lockless read to avoid mutex overhead if pipes are not in use.
487	 */
488	if (TAILQ_FIRST(&audit_pipe_list) == NULL)
489		return;
490
491	mtx_lock(&audit_pipe_mtx);
492	TAILQ_FOREACH(ap, &audit_pipe_list, ap_list)
493		audit_pipe_append(ap, record, record_len);
494	audit_pipe_records++;
495	mtx_unlock(&audit_pipe_mtx);
496	cv_broadcastpri(&audit_pipe_cv, PSOCK);
497}
498
499
500/*
501 * Pop the next record off of an audit pipe.
502 */
503static struct audit_pipe_entry *
504audit_pipe_pop(struct audit_pipe *ap)
505{
506	struct audit_pipe_entry *ape;
507
508	mtx_assert(&audit_pipe_mtx, MA_OWNED);
509
510	ape = TAILQ_FIRST(&ap->ap_queue);
511	KASSERT((ape == NULL && ap->ap_qlen == 0) ||
512	    (ape != NULL && ap->ap_qlen != 0), ("audit_pipe_pop: qlen"));
513	if (ape == NULL)
514		return (NULL);
515	TAILQ_REMOVE(&ap->ap_queue, ape, ape_queue);
516	ap->ap_qlen--;
517	return (ape);
518}
519
520/*
521 * Allocate a new audit pipe.  Connects the pipe, on success, to the global
522 * list and updates statistics.
523 */
524static struct audit_pipe *
525audit_pipe_alloc(void)
526{
527	struct audit_pipe *ap;
528
529	mtx_assert(&audit_pipe_mtx, MA_OWNED);
530
531	ap = malloc(sizeof(*ap), M_AUDIT_PIPE, M_NOWAIT | M_ZERO);
532	if (ap == NULL)
533		return (NULL);
534	ap->ap_qlimit = AUDIT_PIPE_QLIMIT_DEFAULT;
535	TAILQ_INIT(&ap->ap_queue);
536	knlist_init(&ap->ap_selinfo.si_note, &audit_pipe_mtx, NULL, NULL,
537	    NULL);
538
539	/*
540	 * Default flags, naflags, and auid-specific preselection settings to
541	 * 0.  Initialize the mode to the global trail so that if praudit(1)
542	 * is run on /dev/auditpipe, it sees events associated with the
543	 * default trail.  Pipe-aware application can clear the flag, set
544	 * custom masks, and flush the pipe as needed.
545	 */
546	bzero(&ap->ap_preselect_flags, sizeof(ap->ap_preselect_flags));
547	bzero(&ap->ap_preselect_naflags, sizeof(ap->ap_preselect_naflags));
548	TAILQ_INIT(&ap->ap_preselect_list);
549	ap->ap_preselect_mode = AUDITPIPE_PRESELECT_MODE_TRAIL;
550
551	/*
552	 * Add to global list and update global statistics.
553	 */
554	TAILQ_INSERT_HEAD(&audit_pipe_list, ap, ap_list);
555	audit_pipe_count++;
556	audit_pipe_ever++;
557
558	return (ap);
559}
560
561/*
562 * Flush all records currently present in an audit pipe; assume mutex is held.
563 */
564static void
565audit_pipe_flush(struct audit_pipe *ap)
566{
567	struct audit_pipe_entry *ape;
568
569	mtx_assert(&audit_pipe_mtx, MA_OWNED);
570
571	while ((ape = TAILQ_FIRST(&ap->ap_queue)) != NULL) {
572		TAILQ_REMOVE(&ap->ap_queue, ape, ape_queue);
573		audit_pipe_entry_free(ape);
574		ap->ap_qlen--;
575	}
576	KASSERT(ap->ap_qlen == 0, ("audit_pipe_free: ap_qlen"));
577}
578
579/*
580 * Free an audit pipe; this means freeing all preselection state and all
581 * records in the pipe.  Assumes mutex is held to prevent any new records
582 * from being inserted during the free, and that the audit pipe is still on
583 * the global list.
584 */
585static void
586audit_pipe_free(struct audit_pipe *ap)
587{
588
589	mtx_assert(&audit_pipe_mtx, MA_OWNED);
590
591	audit_pipe_preselect_flush_locked(ap);
592	audit_pipe_flush(ap);
593	knlist_destroy(&ap->ap_selinfo.si_note);
594	TAILQ_REMOVE(&audit_pipe_list, ap, ap_list);
595	free(ap, M_AUDIT_PIPE);
596	audit_pipe_count--;
597}
598
599/*
600 * Audit pipe clone routine -- provide specific requested audit pipe, or a
601 * fresh one if a specific one is not requested.
602 */
603static void
604audit_pipe_clone(void *arg, struct ucred *cred, char *name, int namelen,
605    struct cdev **dev)
606{
607	int i, u;
608
609	if (*dev != NULL)
610		return;
611
612	if (strcmp(name, AUDIT_PIPE_NAME) == 0)
613		u = -1;
614	else if (dev_stdclone(name, NULL, AUDIT_PIPE_NAME, &u) != 1)
615		return;
616
617	i = clone_create(&audit_pipe_clones, &audit_pipe_cdevsw, &u, dev, 0);
618	if (i) {
619		*dev = make_dev(&audit_pipe_cdevsw, unit2minor(u), UID_ROOT,
620		    GID_WHEEL, 0600, "%s%d", AUDIT_PIPE_NAME, u);
621		if (*dev != NULL) {
622			dev_ref(*dev);
623			(*dev)->si_flags |= SI_CHEAPCLONE;
624		}
625	}
626}
627
628/*
629 * Audit pipe open method.  Explicit privilege check isn't used as this
630 * allows file permissions on the special device to be used to grant audit
631 * review access.  Those file permissions should be managed carefully.
632 */
633static int
634audit_pipe_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
635{
636	struct audit_pipe *ap;
637
638	mtx_lock(&audit_pipe_mtx);
639	ap = dev->si_drv1;
640	if (ap == NULL) {
641		ap = audit_pipe_alloc();
642		if (ap == NULL) {
643			mtx_unlock(&audit_pipe_mtx);
644			return (ENOMEM);
645		}
646		dev->si_drv1 = ap;
647	} else {
648		KASSERT(ap->ap_open, ("audit_pipe_open: ap && !ap_open"));
649		mtx_unlock(&audit_pipe_mtx);
650		return (EBUSY);
651	}
652	ap->ap_open = 1;
653	mtx_unlock(&audit_pipe_mtx);
654	fsetown(td->td_proc->p_pid, &ap->ap_sigio);
655	return (0);
656}
657
658/*
659 * Close audit pipe, tear down all records, etc.
660 */
661static int
662audit_pipe_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
663{
664	struct audit_pipe *ap;
665
666	ap = dev->si_drv1;
667	KASSERT(ap != NULL, ("audit_pipe_close: ap == NULL"));
668	KASSERT(ap->ap_open, ("audit_pipe_close: !ap_open"));
669	funsetown(&ap->ap_sigio);
670	mtx_lock(&audit_pipe_mtx);
671	ap->ap_open = 0;
672	audit_pipe_free(ap);
673	dev->si_drv1 = NULL;
674	mtx_unlock(&audit_pipe_mtx);
675	return (0);
676}
677
678/*
679 * Audit pipe ioctl() routine.  Handle file descriptor and audit pipe layer
680 * commands.
681 *
682 * Would be desirable to support filtering, although perhaps something simple
683 * like an event mask, as opposed to something complicated like BPF.
684 */
685static int
686audit_pipe_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
687    struct thread *td)
688{
689	struct auditpipe_ioctl_preselect *aip;
690	struct audit_pipe *ap;
691	au_mask_t *maskp;
692	int error, mode;
693	au_id_t auid;
694
695	ap = dev->si_drv1;
696	KASSERT(ap != NULL, ("audit_pipe_ioctl: ap == NULL"));
697
698	/*
699	 * Audit pipe ioctls: first come standard device node ioctls, then
700	 * manipulation of pipe settings, and finally, statistics query
701	 * ioctls.
702	 */
703	switch (cmd) {
704	case FIONBIO:
705		mtx_lock(&audit_pipe_mtx);
706		if (*(int *)data)
707			ap->ap_flags |= AUDIT_PIPE_NBIO;
708		else
709			ap->ap_flags &= ~AUDIT_PIPE_NBIO;
710		mtx_unlock(&audit_pipe_mtx);
711		error = 0;
712		break;
713
714	case FIONREAD:
715		mtx_lock(&audit_pipe_mtx);
716		if (TAILQ_FIRST(&ap->ap_queue) != NULL)
717			*(int *)data =
718			    TAILQ_FIRST(&ap->ap_queue)->ape_record_len;
719		else
720			*(int *)data = 0;
721		mtx_unlock(&audit_pipe_mtx);
722		error = 0;
723		break;
724
725	case FIOASYNC:
726		mtx_lock(&audit_pipe_mtx);
727		if (*(int *)data)
728			ap->ap_flags |= AUDIT_PIPE_ASYNC;
729		else
730			ap->ap_flags &= ~AUDIT_PIPE_ASYNC;
731		mtx_unlock(&audit_pipe_mtx);
732		error = 0;
733		break;
734
735	case FIOSETOWN:
736		error = fsetown(*(int *)data, &ap->ap_sigio);
737		break;
738
739	case FIOGETOWN:
740		*(int *)data = fgetown(&ap->ap_sigio);
741		error = 0;
742		break;
743
744	case AUDITPIPE_GET_QLEN:
745		*(u_int *)data = ap->ap_qlen;
746		error = 0;
747		break;
748
749	case AUDITPIPE_GET_QLIMIT:
750		*(u_int *)data = ap->ap_qlimit;
751		error = 0;
752		break;
753
754	case AUDITPIPE_SET_QLIMIT:
755		/* Lockless integer write. */
756		if (*(u_int *)data >= AUDIT_PIPE_QLIMIT_MIN ||
757		    *(u_int *)data <= AUDIT_PIPE_QLIMIT_MAX) {
758			ap->ap_qlimit = *(u_int *)data;
759			error = 0;
760		} else
761			error = EINVAL;
762		break;
763
764	case AUDITPIPE_GET_QLIMIT_MIN:
765		*(u_int *)data = AUDIT_PIPE_QLIMIT_MIN;
766		error = 0;
767		break;
768
769	case AUDITPIPE_GET_QLIMIT_MAX:
770		*(u_int *)data = AUDIT_PIPE_QLIMIT_MAX;
771		error = 0;
772		break;
773
774	case AUDITPIPE_GET_PRESELECT_FLAGS:
775		mtx_lock(&audit_pipe_mtx);
776		maskp = (au_mask_t *)data;
777		*maskp = ap->ap_preselect_flags;
778		mtx_unlock(&audit_pipe_mtx);
779		error = 0;
780		break;
781
782	case AUDITPIPE_SET_PRESELECT_FLAGS:
783		mtx_lock(&audit_pipe_mtx);
784		maskp = (au_mask_t *)data;
785		ap->ap_preselect_flags = *maskp;
786		mtx_unlock(&audit_pipe_mtx);
787		error = 0;
788		break;
789
790	case AUDITPIPE_GET_PRESELECT_NAFLAGS:
791		mtx_lock(&audit_pipe_mtx);
792		maskp = (au_mask_t *)data;
793		*maskp = ap->ap_preselect_naflags;
794		mtx_unlock(&audit_pipe_mtx);
795		error = 0;
796		break;
797
798	case AUDITPIPE_SET_PRESELECT_NAFLAGS:
799		mtx_lock(&audit_pipe_mtx);
800		maskp = (au_mask_t *)data;
801		ap->ap_preselect_naflags = *maskp;
802		mtx_unlock(&audit_pipe_mtx);
803		error = 0;
804		break;
805
806	case AUDITPIPE_GET_PRESELECT_AUID:
807		aip = (struct auditpipe_ioctl_preselect *)data;
808		error = audit_pipe_preselect_get(ap, aip->aip_auid,
809		    &aip->aip_mask);
810		break;
811
812	case AUDITPIPE_SET_PRESELECT_AUID:
813		aip = (struct auditpipe_ioctl_preselect *)data;
814		audit_pipe_preselect_set(ap, aip->aip_auid, aip->aip_mask);
815		error = 0;
816		break;
817
818	case AUDITPIPE_DELETE_PRESELECT_AUID:
819		auid = *(au_id_t *)data;
820		error = audit_pipe_preselect_delete(ap, auid);
821		break;
822
823	case AUDITPIPE_FLUSH_PRESELECT_AUID:
824		audit_pipe_preselect_flush(ap);
825		error = 0;
826		break;
827
828	case AUDITPIPE_GET_PRESELECT_MODE:
829		mtx_lock(&audit_pipe_mtx);
830		*(int *)data = ap->ap_preselect_mode;
831		mtx_unlock(&audit_pipe_mtx);
832		error = 0;
833		break;
834
835	case AUDITPIPE_SET_PRESELECT_MODE:
836		mode = *(int *)data;
837		switch (mode) {
838		case AUDITPIPE_PRESELECT_MODE_TRAIL:
839		case AUDITPIPE_PRESELECT_MODE_LOCAL:
840			mtx_lock(&audit_pipe_mtx);
841			ap->ap_preselect_mode = mode;
842			mtx_unlock(&audit_pipe_mtx);
843			error = 0;
844			break;
845
846		default:
847			error = EINVAL;
848		}
849		break;
850
851	case AUDITPIPE_FLUSH:
852		mtx_lock(&audit_pipe_mtx);
853		audit_pipe_flush(ap);
854		mtx_unlock(&audit_pipe_mtx);
855		error = 0;
856		break;
857
858	case AUDITPIPE_GET_MAXAUDITDATA:
859		*(u_int *)data = MAXAUDITDATA;
860		error = 0;
861		break;
862
863	case AUDITPIPE_GET_INSERTS:
864		*(u_int *)data = ap->ap_inserts;
865		error = 0;
866		break;
867
868	case AUDITPIPE_GET_READS:
869		*(u_int *)data = ap->ap_reads;
870		error = 0;
871		break;
872
873	case AUDITPIPE_GET_DROPS:
874		*(u_int *)data = ap->ap_drops;
875		error = 0;
876		break;
877
878	case AUDITPIPE_GET_TRUNCATES:
879		*(u_int *)data = ap->ap_truncates;
880		error = 0;
881		break;
882
883	default:
884		error = ENOTTY;
885	}
886	return (error);
887}
888
889/*
890 * Audit pipe read.  Pull one record off the queue and copy to user space.
891 * On error, the record is dropped.
892 *
893 * Providing more sophisticated behavior, such as partial reads, is tricky
894 * due to the potential for parallel I/O.  If partial read support is
895 * required, it will require a per-pipe "current record being read" along
896 * with an offset into that trecord which has already been read.  Threads
897 * performing partial reads will need to allocate per-thread copies of the
898 * data so that if another thread completes the read of the record, it can be
899 * freed without adding reference count logic.  If this is added, a flag to
900 * indicate that only atomic record reads are desired would be useful, as if
901 * different threads are all waiting for records on the pipe, they will want
902 * independent record reads, which is currently the behavior.
903 */
904static int
905audit_pipe_read(struct cdev *dev, struct uio *uio, int flag)
906{
907	struct audit_pipe_entry *ape;
908	struct audit_pipe *ap;
909	int error;
910
911	ap = dev->si_drv1;
912	KASSERT(ap != NULL, ("audit_pipe_read: ap == NULL"));
913	mtx_lock(&audit_pipe_mtx);
914	do {
915		/*
916		 * Wait for a record that fits into the read buffer, dropping
917		 * records that would be truncated if actually passed to the
918		 * process.  This helps maintain the discreet record read
919		 * interface.
920		 */
921		while ((ape = audit_pipe_pop(ap)) == NULL) {
922			if (ap->ap_flags & AUDIT_PIPE_NBIO) {
923				mtx_unlock(&audit_pipe_mtx);
924				return (EAGAIN);
925			}
926			error = cv_wait_sig(&audit_pipe_cv, &audit_pipe_mtx);
927			if (error) {
928				mtx_unlock(&audit_pipe_mtx);
929				return (error);
930			}
931		}
932		if (ape->ape_record_len <= uio->uio_resid)
933			break;
934		audit_pipe_entry_free(ape);
935		ap->ap_truncates++;
936	} while (1);
937	ap->ap_reads++;
938	mtx_unlock(&audit_pipe_mtx);
939
940	/*
941	 * Now read record to user space memory.  Even if the read is short,
942	 * we abandon the remainder of the record, supporting only discreet
943	 * record reads.
944	 */
945	error = uiomove(ape->ape_record, ape->ape_record_len, uio);
946	audit_pipe_entry_free(ape);
947	return (error);
948}
949
950/*
951 * Audit pipe poll.
952 */
953static int
954audit_pipe_poll(struct cdev *dev, int events, struct thread *td)
955{
956	struct audit_pipe *ap;
957	int revents;
958
959	revents = 0;
960	ap = dev->si_drv1;
961	KASSERT(ap != NULL, ("audit_pipe_poll: ap == NULL"));
962	if (events & (POLLIN | POLLRDNORM)) {
963		mtx_lock(&audit_pipe_mtx);
964		if (TAILQ_FIRST(&ap->ap_queue) != NULL)
965			revents |= events & (POLLIN | POLLRDNORM);
966		else
967			selrecord(td, &ap->ap_selinfo);
968		mtx_unlock(&audit_pipe_mtx);
969	}
970	return (revents);
971}
972
973/*
974 * Audit pipe kqfilter.
975 */
976static int
977audit_pipe_kqfilter(struct cdev *dev, struct knote *kn)
978{
979	struct audit_pipe *ap;
980
981	ap = dev->si_drv1;
982	KASSERT(ap != NULL, ("audit_pipe_kqfilter: ap == NULL"));
983
984	if (kn->kn_filter != EVFILT_READ)
985		return (EINVAL);
986
987	kn->kn_fop = &audit_pipe_read_filterops;
988	kn->kn_hook = ap;
989
990	mtx_lock(&audit_pipe_mtx);
991	knlist_add(&ap->ap_selinfo.si_note, kn, 1);
992	mtx_unlock(&audit_pipe_mtx);
993	return (0);
994}
995
996/*
997 * Return true if there are records available for reading on the pipe.
998 */
999static int
1000audit_pipe_kqread(struct knote *kn, long hint)
1001{
1002	struct audit_pipe_entry *ape;
1003	struct audit_pipe *ap;
1004
1005	mtx_assert(&audit_pipe_mtx, MA_OWNED);
1006
1007	ap = (struct audit_pipe *)kn->kn_hook;
1008	KASSERT(ap != NULL, ("audit_pipe_kqread: ap == NULL"));
1009
1010	if (ap->ap_qlen != 0) {
1011		ape = TAILQ_FIRST(&ap->ap_queue);
1012		KASSERT(ape != NULL, ("audit_pipe_kqread: ape == NULL"));
1013
1014		kn->kn_data = ape->ape_record_len;
1015		return (1);
1016	} else {
1017		kn->kn_data = 0;
1018		return (0);
1019	}
1020}
1021
1022/*
1023 * Detach kqueue state from audit pipe.
1024 */
1025static void
1026audit_pipe_kqdetach(struct knote *kn)
1027{
1028	struct audit_pipe *ap;
1029
1030	ap = (struct audit_pipe *)kn->kn_hook;
1031	KASSERT(ap != NULL, ("audit_pipe_kqdetach: ap == NULL"));
1032
1033	mtx_lock(&audit_pipe_mtx);
1034	knlist_remove(&ap->ap_selinfo.si_note, kn, 1);
1035	mtx_unlock(&audit_pipe_mtx);
1036}
1037
1038/*
1039 * Initialize the audit pipe system.
1040 */
1041static void
1042audit_pipe_init(void *unused)
1043{
1044
1045	TAILQ_INIT(&audit_pipe_list);
1046	mtx_init(&audit_pipe_mtx, "audit_pipe_mtx", NULL, MTX_DEF);
1047	cv_init(&audit_pipe_cv, "audit_pipe_cv");
1048
1049	clone_setup(&audit_pipe_clones);
1050	audit_pipe_eh_tag = EVENTHANDLER_REGISTER(dev_clone,
1051	    audit_pipe_clone, 0, 1000);
1052	if (audit_pipe_eh_tag == NULL)
1053		panic("audit_pipe_init: EVENTHANDLER_REGISTER");
1054}
1055
1056SYSINIT(audit_pipe_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, audit_pipe_init,
1057    NULL);
1058