2 * Copyright (c) 2006 Robert N. M. Watson
3 * Copyright (c) 2008-2009 Apple, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/fcntl.h>
38 #include <sys/signalvar.h>
39 #include <miscfs/devfs/devfs.h>
41 #include <bsm/audit.h>
42 #include <security/audit/audit.h>
43 #include <security/audit/audit_ioctl.h>
44 #include <security/audit/audit_bsd.h>
45 #include <security/audit/audit_private.h>
49 * Implementation of a clonable special device providing a live stream of BSM
50 * audit data. Consumers receive a "tee" of the system audit trail by
51 * default, but may also define alternative event selections using ioctls.
52 * This interface provides unreliable but timely access to audit events.
53 * Consumers should be very careful to avoid introducing event cycles.
59 static MALLOC_DEFINE(M_AUDIT_PIPE
, "audit_pipe", "Audit pipes");
60 static MALLOC_DEFINE(M_AUDIT_PIPE_ENTRY
, "audit_pipeent",
61 "Audit pipe entries and buffers");
62 static MALLOC_DEFINE(M_AUDIT_PIPE_PRESELECT
, "audit_pipe_presel",
63 "Audit pipe preselection structure");
66 * Audit pipe buffer parameters.
68 #define AUDIT_PIPE_QLIMIT_DEFAULT (128)
69 #define AUDIT_PIPE_QLIMIT_MIN (1)
70 #define AUDIT_PIPE_QLIMIT_MAX (1024)
73 * Description of an entry in an audit_pipe.
75 struct audit_pipe_entry
{
78 TAILQ_ENTRY(audit_pipe_entry
) ape_queue
;
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.
89 * We may want to consider a more space/time-efficient data structure once
90 * usage patterns for per-auid specifications are clear.
92 struct audit_pipe_preselect
{
95 TAILQ_ENTRY(audit_pipe_preselect
) app_list
;
99 * Description of an individual audit_pipe. Consists largely of a bounded
102 #define AUDIT_PIPE_ASYNC 0x00000001
103 #define AUDIT_PIPE_NBIO 0x00000002
105 int ap_open
; /* Device open? */
108 struct selinfo ap_selinfo
;
112 * Per-pipe mutex protecting most fields in this data structure.
117 * Per-pipe sleep lock serializing user-generated reads and flushes.
118 * uiomove() is called to copy out the current head record's data
119 * while the record remains in the queue, so we prevent other threads
120 * from removing it using this lock.
125 * Condition variable to signal when data has been delivered to a
131 * Various queue-related variables: qlen and qlimit are a count of
132 * records in the queue; qbyteslen is the number of bytes of data
133 * across all records, and qoffset is the amount read so far of the
134 * first record in the queue. The number of bytes available for
135 * reading in the queue is qbyteslen - qoffset.
143 * Per-pipe operation statistics.
145 u_int64_t ap_inserts
; /* Records added. */
146 u_int64_t ap_reads
; /* Records read. */
147 u_int64_t ap_drops
; /* Records dropped. */
150 * Fields relating to pipe interest: global masks for unmatched
151 * processes (attributable, non-attributable), and a list of specific
152 * interest specifications by auid.
154 int ap_preselect_mode
;
155 au_mask_t ap_preselect_flags
;
156 au_mask_t ap_preselect_naflags
;
157 TAILQ_HEAD(, audit_pipe_preselect
) ap_preselect_list
;
160 * Current pending record list. Protected by a combination of ap_mtx
161 * and ap_sx. Note particularly that *both* locks are required to
162 * remove a record from the head of the queue, as an in-progress read
163 * may sleep while copying and therefore cannot hold ap_mtx.
165 TAILQ_HEAD(, audit_pipe_entry
) ap_queue
;
170 TAILQ_ENTRY(audit_pipe
) ap_list
;
173 #define AUDIT_PIPE_LOCK(ap) mtx_lock(&(ap)->ap_mtx)
174 #define AUDIT_PIPE_LOCK_ASSERT(ap) mtx_assert(&(ap)->ap_mtx, MA_OWNED)
175 #define AUDIT_PIPE_LOCK_DESTROY(ap) mtx_destroy(&(ap)->ap_mtx)
176 #define AUDIT_PIPE_LOCK_INIT(ap) mtx_init(&(ap)->ap_mtx, \
177 "audit_pipe_mtx", NULL, MTX_DEF)
178 #define AUDIT_PIPE_UNLOCK(ap) mtx_unlock(&(ap)->ap_mtx)
179 #define AUDIT_PIPE_MTX(ap) (&(ap)->ap_mtx)
181 #define AUDIT_PIPE_SX_LOCK_DESTROY(ap) slck_destroy(&(ap)->ap_sx)
182 #define AUDIT_PIPE_SX_LOCK_INIT(ap) slck_init(&(ap)->ap_sx, "audit_pipe_sx")
183 #define AUDIT_PIPE_SX_XLOCK_ASSERT(ap) slck_assert(&(ap)->ap_sx, SA_XLOCKED)
184 #define AUDIT_PIPE_SX_XLOCK_SIG(ap) slck_lock_sig(&(ap)->ap_sx)
185 #define AUDIT_PIPE_SX_XUNLOCK(ap) slck_unlock(&(ap)->ap_sx)
189 * Global list of audit pipes, rwlock to protect it. Individual record
190 * queues on pipes are protected by per-pipe locks; these locks synchronize
191 * between threads walking the list to deliver to individual pipes and add/
192 * remove of pipes, and are mostly acquired for read.
194 static TAILQ_HEAD(, audit_pipe
) audit_pipe_list
;
195 static struct rwlock audit_pipe_lock
;
197 #define AUDIT_PIPE_LIST_LOCK_INIT() rw_init(&audit_pipe_lock, \
198 "audit_pipe_list_lock")
199 #define AUDIT_PIPE_LIST_RLOCK() rw_rlock(&audit_pipe_lock)
200 #define AUDIT_PIPE_LIST_RUNLOCK() rw_runlock(&audit_pipe_lock)
201 #define AUDIT_PIPE_LIST_WLOCK() rw_wlock(&audit_pipe_lock)
202 #define AUDIT_PIPE_LIST_WLOCK_ASSERT() rw_assert(&audit_pipe_lock, \
204 #define AUDIT_PIPE_LIST_WUNLOCK() rw_wunlock(&audit_pipe_lock)
207 * Cloning related variables and constants.
209 #define AUDIT_PIPE_NAME "auditpipe"
210 #define MAX_AUDIT_PIPES 32
211 static int audit_pipe_major
;
214 * dev_t doesn't have a pointer for "softc" data. So we have to keep track of
215 * it with the following global array (indexed by the minor number).
217 * XXX We may want to dynamically grow this as needed.
219 static struct audit_pipe
*audit_pipe_dtab
[MAX_AUDIT_PIPES
];
223 * Special device methods and definition.
225 static open_close_fcn_t audit_pipe_open
;
226 static open_close_fcn_t audit_pipe_close
;
227 static read_write_fcn_t audit_pipe_read
;
228 static ioctl_fcn_t audit_pipe_ioctl
;
229 static select_fcn_t audit_pipe_poll
;
231 static struct cdevsw audit_pipe_cdevsw
= {
232 .d_open
= audit_pipe_open
,
233 .d_close
= audit_pipe_close
,
234 .d_read
= audit_pipe_read
,
235 .d_write
= eno_rdwrt
,
236 .d_ioctl
= audit_pipe_ioctl
,
238 .d_reset
= eno_reset
,
240 .d_select
= audit_pipe_poll
,
242 .d_strategy
= eno_strat
,
247 * Some global statistics on audit pipes.
249 static int audit_pipe_count
; /* Current number of pipes. */
250 static u_int64_t audit_pipe_ever
; /* Pipes ever allocated. */
251 static u_int64_t audit_pipe_records
; /* Records seen. */
252 static u_int64_t audit_pipe_drops
; /* Global record drop count. */
255 * Free an audit pipe entry.
258 audit_pipe_entry_free(struct audit_pipe_entry
*ape
)
261 free(ape
->ape_record
, M_AUDIT_PIPE_ENTRY
);
262 free(ape
, M_AUDIT_PIPE_ENTRY
);
266 * Find an audit pipe preselection specification for an auid, if any.
268 static struct audit_pipe_preselect
*
269 audit_pipe_preselect_find(struct audit_pipe
*ap
, au_id_t auid
)
271 struct audit_pipe_preselect
*app
;
273 AUDIT_PIPE_LOCK_ASSERT(ap
);
275 TAILQ_FOREACH(app
, &ap
->ap_preselect_list
, app_list
) {
276 if (app
->app_auid
== auid
)
283 * Query the per-pipe mask for a specific auid.
286 audit_pipe_preselect_get(struct audit_pipe
*ap
, au_id_t auid
,
289 struct audit_pipe_preselect
*app
;
293 app
= audit_pipe_preselect_find(ap
, auid
);
295 *maskp
= app
->app_mask
;
299 AUDIT_PIPE_UNLOCK(ap
);
304 * Set the per-pipe mask for a specific auid. Add a new entry if needed;
305 * otherwise, update the current entry.
308 audit_pipe_preselect_set(struct audit_pipe
*ap
, au_id_t auid
, au_mask_t mask
)
310 struct audit_pipe_preselect
*app
, *app_new
;
313 * Pessimistically assume that the auid doesn't already have a mask
314 * set, and allocate. We will free it if it is unneeded.
316 app_new
= malloc(sizeof(*app_new
), M_AUDIT_PIPE_PRESELECT
, M_WAITOK
);
318 app
= audit_pipe_preselect_find(ap
, auid
);
322 app
->app_auid
= auid
;
323 TAILQ_INSERT_TAIL(&ap
->ap_preselect_list
, app
, app_list
);
325 app
->app_mask
= mask
;
326 AUDIT_PIPE_UNLOCK(ap
);
328 free(app_new
, M_AUDIT_PIPE_PRESELECT
);
332 * Delete a per-auid mask on an audit pipe.
335 audit_pipe_preselect_delete(struct audit_pipe
*ap
, au_id_t auid
)
337 struct audit_pipe_preselect
*app
;
341 app
= audit_pipe_preselect_find(ap
, auid
);
343 TAILQ_REMOVE(&ap
->ap_preselect_list
, app
, app_list
);
347 AUDIT_PIPE_UNLOCK(ap
);
349 free(app
, M_AUDIT_PIPE_PRESELECT
);
354 * Delete all per-auid masks on an audit pipe.
357 audit_pipe_preselect_flush_locked(struct audit_pipe
*ap
)
359 struct audit_pipe_preselect
*app
;
361 AUDIT_PIPE_LOCK_ASSERT(ap
);
363 while ((app
= TAILQ_FIRST(&ap
->ap_preselect_list
)) != NULL
) {
364 TAILQ_REMOVE(&ap
->ap_preselect_list
, app
, app_list
);
365 free(app
, M_AUDIT_PIPE_PRESELECT
);
370 audit_pipe_preselect_flush(struct audit_pipe
*ap
)
374 audit_pipe_preselect_flush_locked(ap
);
375 AUDIT_PIPE_UNLOCK(ap
);
379 * Determine whether a specific audit pipe matches a record with these
380 * properties. Algorithm is as follows:
382 * - If the pipe is configured to track the default trail configuration, then
383 * use the results of global preselection matching.
384 * - If not, search for a specifically configured auid entry matching the
385 * event. If an entry is found, use that.
386 * - Otherwise, use the default flags or naflags configured for the pipe.
389 audit_pipe_preselect_check(struct audit_pipe
*ap
, au_id_t auid
,
390 au_event_t event
, au_class_t
class, int sorf
, int trail_preselect
)
392 struct audit_pipe_preselect
*app
;
394 AUDIT_PIPE_LOCK_ASSERT(ap
);
396 switch (ap
->ap_preselect_mode
) {
397 case AUDITPIPE_PRESELECT_MODE_TRAIL
:
398 return (trail_preselect
);
400 case AUDITPIPE_PRESELECT_MODE_LOCAL
:
401 app
= audit_pipe_preselect_find(ap
, auid
);
403 if (auid
== (uid_t
)AU_DEFAUDITID
)
404 return (au_preselect(event
, class,
405 &ap
->ap_preselect_naflags
, sorf
));
407 return (au_preselect(event
, class,
408 &ap
->ap_preselect_flags
, sorf
));
410 return (au_preselect(event
, class, &app
->app_mask
,
414 panic("audit_pipe_preselect_check: mode %d",
415 ap
->ap_preselect_mode
);
422 * Determine whether there exists a pipe interested in a record with specific
426 audit_pipe_preselect(au_id_t auid
, au_event_t event
, au_class_t
class,
427 int sorf
, int trail_preselect
)
429 struct audit_pipe
*ap
;
431 /* Lockless read to avoid acquiring the global lock if not needed. */
432 if (TAILQ_EMPTY(&audit_pipe_list
))
435 AUDIT_PIPE_LIST_RLOCK();
436 TAILQ_FOREACH(ap
, &audit_pipe_list
, ap_list
) {
438 if (audit_pipe_preselect_check(ap
, auid
, event
, class, sorf
,
440 AUDIT_PIPE_UNLOCK(ap
);
441 AUDIT_PIPE_LIST_RUNLOCK();
444 AUDIT_PIPE_UNLOCK(ap
);
446 AUDIT_PIPE_LIST_RUNLOCK();
451 * Append individual record to a queue -- allocate queue-local buffer, and
452 * add to the queue. If the queue is full or we can't allocate memory, drop
456 audit_pipe_append(struct audit_pipe
*ap
, void *record
, u_int record_len
)
458 struct audit_pipe_entry
*ape
;
460 AUDIT_PIPE_LOCK_ASSERT(ap
);
462 if (ap
->ap_qlen
>= ap
->ap_qlimit
) {
468 ape
= malloc(sizeof(*ape
), M_AUDIT_PIPE_ENTRY
, M_NOWAIT
| M_ZERO
);
475 ape
->ape_record
= malloc(record_len
, M_AUDIT_PIPE_ENTRY
, M_NOWAIT
);
476 if (ape
->ape_record
== NULL
) {
477 free(ape
, M_AUDIT_PIPE_ENTRY
);
483 bcopy(record
, ape
->ape_record
, record_len
);
484 ape
->ape_record_len
= record_len
;
486 TAILQ_INSERT_TAIL(&ap
->ap_queue
, ape
, ape_queue
);
489 ap
->ap_qbyteslen
+= ape
->ape_record_len
;
490 selwakeup(&ap
->ap_selinfo
);
491 if (ap
->ap_flags
& AUDIT_PIPE_ASYNC
)
492 pgsigio(ap
->ap_sigio
, SIGIO
);
493 #if 0 /* XXX - fix select */
494 selwakeuppri(&ap
->ap_selinfo
, PSOCK
);
495 KNOTE_LOCKED(&ap
->ap_selinfo
.si_note
, 0);
496 if (ap
->ap_flags
& AUDIT_PIPE_ASYNC
)
497 pgsigio(&ap
->ap_sigio
, SIGIO
, 0);
499 cv_broadcast(&ap
->ap_cv
);
503 * audit_pipe_submit(): audit_worker submits audit records via this
504 * interface, which arranges for them to be delivered to pipe queues.
507 audit_pipe_submit(au_id_t auid
, au_event_t event
, au_class_t
class, int sorf
,
508 int trail_select
, void *record
, u_int record_len
)
510 struct audit_pipe
*ap
;
513 * Lockless read to avoid lock overhead if pipes are not in use.
515 if (TAILQ_FIRST(&audit_pipe_list
) == NULL
)
518 AUDIT_PIPE_LIST_RLOCK();
519 TAILQ_FOREACH(ap
, &audit_pipe_list
, ap_list
) {
521 if (audit_pipe_preselect_check(ap
, auid
, event
, class, sorf
,
523 audit_pipe_append(ap
, record
, record_len
);
524 AUDIT_PIPE_UNLOCK(ap
);
526 AUDIT_PIPE_LIST_RUNLOCK();
528 /* Unlocked increment. */
529 audit_pipe_records
++;
533 * audit_pipe_submit_user(): the same as audit_pipe_submit(), except that
534 * since we don't currently have selection information available, it is
535 * delivered to the pipe unconditionally.
537 * XXXRW: This is a bug. The BSM check routine for submitting a user record
538 * should parse that information and return it.
541 audit_pipe_submit_user(void *record
, u_int record_len
)
543 struct audit_pipe
*ap
;
546 * Lockless read to avoid lock overhead if pipes are not in use.
548 if (TAILQ_FIRST(&audit_pipe_list
) == NULL
)
551 AUDIT_PIPE_LIST_RLOCK();
552 TAILQ_FOREACH(ap
, &audit_pipe_list
, ap_list
) {
554 audit_pipe_append(ap
, record
, record_len
);
555 AUDIT_PIPE_UNLOCK(ap
);
557 AUDIT_PIPE_LIST_RUNLOCK();
559 /* Unlocked increment. */
560 audit_pipe_records
++;
564 * Allocate a new audit pipe. Connects the pipe, on success, to the global
565 * list and updates statistics.
567 static struct audit_pipe
*
568 audit_pipe_alloc(void)
570 struct audit_pipe
*ap
;
572 AUDIT_PIPE_LIST_WLOCK_ASSERT();
574 ap
= malloc(sizeof(*ap
), M_AUDIT_PIPE
, M_NOWAIT
| M_ZERO
);
578 ap
->ap_qlimit
= AUDIT_PIPE_QLIMIT_DEFAULT
;
579 TAILQ_INIT(&ap
->ap_queue
);
581 knlist_init(&ap
->ap_selinfo
.si_note
, AUDIT_PIPE_MTX(ap
), NULL
, NULL
,
584 AUDIT_PIPE_LOCK_INIT(ap
);
585 AUDIT_PIPE_SX_LOCK_INIT(ap
);
586 cv_init(&ap
->ap_cv
, "audit_pipe");
589 * Default flags, naflags, and auid-specific preselection settings to
590 * 0. Initialize the mode to the global trail so that if praudit(1)
591 * is run on /dev/auditpipe, it sees events associated with the
592 * default trail. Pipe-aware application can clear the flag, set
593 * custom masks, and flush the pipe as needed.
595 bzero(&ap
->ap_preselect_flags
, sizeof(ap
->ap_preselect_flags
));
596 bzero(&ap
->ap_preselect_naflags
, sizeof(ap
->ap_preselect_naflags
));
597 TAILQ_INIT(&ap
->ap_preselect_list
);
598 ap
->ap_preselect_mode
= AUDITPIPE_PRESELECT_MODE_TRAIL
;
601 * Add to global list and update global statistics.
603 TAILQ_INSERT_HEAD(&audit_pipe_list
, ap
, ap_list
);
611 * Flush all records currently present in an audit pipe; assume mutex is held.
614 audit_pipe_flush(struct audit_pipe
*ap
)
616 struct audit_pipe_entry
*ape
;
618 AUDIT_PIPE_LOCK_ASSERT(ap
);
620 while ((ape
= TAILQ_FIRST(&ap
->ap_queue
)) != NULL
) {
621 TAILQ_REMOVE(&ap
->ap_queue
, ape
, ape_queue
);
622 ap
->ap_qbyteslen
-= ape
->ape_record_len
;
623 audit_pipe_entry_free(ape
);
628 KASSERT(ap
->ap_qlen
== 0, ("audit_pipe_free: ap_qbyteslen"));
629 KASSERT(ap
->ap_qbyteslen
== 0, ("audit_pipe_flush: ap_qbyteslen"));
633 * Free an audit pipe; this means freeing all preselection state and all
634 * records in the pipe. Assumes global write lock and pipe mutex are held to
635 * revent any new records from being inserted during the free, and that the
636 * audit pipe is still on the global list.
639 audit_pipe_free(struct audit_pipe
*ap
)
642 AUDIT_PIPE_LIST_WLOCK_ASSERT();
643 AUDIT_PIPE_LOCK_ASSERT(ap
);
645 audit_pipe_preselect_flush_locked(ap
);
646 audit_pipe_flush(ap
);
647 cv_destroy(&ap
->ap_cv
);
648 AUDIT_PIPE_SX_LOCK_DESTROY(ap
);
649 AUDIT_PIPE_LOCK_DESTROY(ap
);
651 knlist_destroy(&ap
->ap_selinfo
.si_note
);
653 TAILQ_REMOVE(&audit_pipe_list
, ap
, ap_list
);
654 free(ap
, M_AUDIT_PIPE
);
659 * Audit pipe clone routine -- provides a new minor number, or to return (-1),
660 * if one can't be provided. Called with DEVFS_LOCK held.
663 audit_pipe_clone(__unused dev_t dev
, int action
)
667 if (action
== DEVFS_CLONE_ALLOC
) {
668 for(i
= 0; i
< MAX_AUDIT_PIPES
; i
++)
669 if (audit_pipe_dtab
[i
] == NULL
)
673 * XXX Should really return -1 here but that seems to hang
674 * things in devfs. Instead return 0 and let _open() tell
675 * userland the bad news.
684 * Audit pipe open method. Explicit privilege check isn't used as this
685 * allows file permissions on the special device to be used to grant audit
686 * review access. Those file permissions should be managed carefully.
689 audit_pipe_open(dev_t dev
, __unused
int flags
, __unused
int devtype
,
692 struct audit_pipe
*ap
;
696 if (u
< 0 || u
> MAX_AUDIT_PIPES
)
699 AUDIT_PIPE_LIST_WLOCK();
700 ap
= audit_pipe_dtab
[u
];
702 ap
= audit_pipe_alloc();
704 AUDIT_PIPE_LIST_WUNLOCK();
707 audit_pipe_dtab
[u
] = ap
;
709 KASSERT(ap
->ap_open
, ("audit_pipe_open: ap && !ap_open"));
710 AUDIT_PIPE_LIST_WUNLOCK();
714 AUDIT_PIPE_LIST_WUNLOCK();
716 fsetown(td
->td_proc
->p_pid
, &ap
->ap_sigio
);
722 * Close audit pipe, tear down all records, etc.
725 audit_pipe_close(dev_t dev
, __unused
int flags
, __unused
int devtype
,
728 struct audit_pipe
*ap
;
732 ap
= audit_pipe_dtab
[u
];
733 KASSERT(ap
!= NULL
, ("audit_pipe_close: ap == NULL"));
734 KASSERT(ap
->ap_open
, ("audit_pipe_close: !ap_open"));
737 funsetown(&ap
->ap_sigio
);
739 AUDIT_PIPE_LIST_WLOCK();
743 audit_pipe_dtab
[u
] = NULL
;
744 AUDIT_PIPE_LIST_WUNLOCK();
749 * Audit pipe ioctl() routine. Handle file descriptor and audit pipe layer
753 audit_pipe_ioctl(dev_t dev
, u_long cmd
, caddr_t data
,
754 __unused
int flag
, __unused proc_t p
)
756 struct auditpipe_ioctl_preselect
*aip
;
757 struct audit_pipe
*ap
;
762 ap
= audit_pipe_dtab
[minor(dev
)];
763 KASSERT(ap
!= NULL
, ("audit_pipe_ioctl: ap == NULL"));
766 * Audit pipe ioctls: first come standard device node ioctls, then
767 * manipulation of pipe settings, and finally, statistics query
774 ap
->ap_flags
|= AUDIT_PIPE_NBIO
;
776 ap
->ap_flags
&= ~AUDIT_PIPE_NBIO
;
777 AUDIT_PIPE_UNLOCK(ap
);
783 *(int *)data
= ap
->ap_qbyteslen
- ap
->ap_qoffset
;
784 AUDIT_PIPE_UNLOCK(ap
);
791 ap
->ap_flags
|= AUDIT_PIPE_ASYNC
;
793 ap
->ap_flags
&= ~AUDIT_PIPE_ASYNC
;
794 AUDIT_PIPE_UNLOCK(ap
);
800 error
= fsetown(*(int *)data
, &ap
->ap_sigio
);
804 *(int *)data
= fgetown(&ap
->ap_sigio
);
807 #endif /* !__APPLE__ */
809 case AUDITPIPE_GET_QLEN
:
810 *(u_int
*)data
= ap
->ap_qlen
;
814 case AUDITPIPE_GET_QLIMIT
:
815 *(u_int
*)data
= ap
->ap_qlimit
;
819 case AUDITPIPE_SET_QLIMIT
:
820 /* Lockless integer write. */
821 if (*(u_int
*)data
>= AUDIT_PIPE_QLIMIT_MIN
||
822 *(u_int
*)data
<= AUDIT_PIPE_QLIMIT_MAX
) {
823 ap
->ap_qlimit
= *(u_int
*)data
;
829 case AUDITPIPE_GET_QLIMIT_MIN
:
830 *(u_int
*)data
= AUDIT_PIPE_QLIMIT_MIN
;
834 case AUDITPIPE_GET_QLIMIT_MAX
:
835 *(u_int
*)data
= AUDIT_PIPE_QLIMIT_MAX
;
839 case AUDITPIPE_GET_PRESELECT_FLAGS
:
841 maskp
= (au_mask_t
*)data
;
842 *maskp
= ap
->ap_preselect_flags
;
843 AUDIT_PIPE_UNLOCK(ap
);
847 case AUDITPIPE_SET_PRESELECT_FLAGS
:
849 maskp
= (au_mask_t
*)data
;
850 ap
->ap_preselect_flags
= *maskp
;
851 AUDIT_CHECK_IF_KEVENTS_MASK(ap
->ap_preselect_flags
);
852 AUDIT_PIPE_UNLOCK(ap
);
856 case AUDITPIPE_GET_PRESELECT_NAFLAGS
:
858 maskp
= (au_mask_t
*)data
;
859 *maskp
= ap
->ap_preselect_naflags
;
860 AUDIT_PIPE_UNLOCK(ap
);
864 case AUDITPIPE_SET_PRESELECT_NAFLAGS
:
866 maskp
= (au_mask_t
*)data
;
867 ap
->ap_preselect_naflags
= *maskp
;
868 AUDIT_CHECK_IF_KEVENTS_MASK(ap
->ap_preselect_naflags
);
869 AUDIT_PIPE_UNLOCK(ap
);
873 case AUDITPIPE_GET_PRESELECT_AUID
:
874 aip
= (struct auditpipe_ioctl_preselect
*)data
;
875 error
= audit_pipe_preselect_get(ap
, aip
->aip_auid
,
879 case AUDITPIPE_SET_PRESELECT_AUID
:
880 aip
= (struct auditpipe_ioctl_preselect
*)data
;
881 audit_pipe_preselect_set(ap
, aip
->aip_auid
, aip
->aip_mask
);
885 case AUDITPIPE_DELETE_PRESELECT_AUID
:
886 auid
= *(au_id_t
*)data
;
887 error
= audit_pipe_preselect_delete(ap
, auid
);
890 case AUDITPIPE_FLUSH_PRESELECT_AUID
:
891 audit_pipe_preselect_flush(ap
);
895 case AUDITPIPE_GET_PRESELECT_MODE
:
897 *(int *)data
= ap
->ap_preselect_mode
;
898 AUDIT_PIPE_UNLOCK(ap
);
902 case AUDITPIPE_SET_PRESELECT_MODE
:
905 case AUDITPIPE_PRESELECT_MODE_TRAIL
:
906 case AUDITPIPE_PRESELECT_MODE_LOCAL
:
908 ap
->ap_preselect_mode
= mode
;
909 AUDIT_PIPE_UNLOCK(ap
);
918 case AUDITPIPE_FLUSH
:
919 if (AUDIT_PIPE_SX_XLOCK_SIG(ap
) != 0)
922 audit_pipe_flush(ap
);
923 AUDIT_PIPE_UNLOCK(ap
);
924 AUDIT_PIPE_SX_XUNLOCK(ap
);
928 case AUDITPIPE_GET_MAXAUDITDATA
:
929 *(u_int
*)data
= MAXAUDITDATA
;
933 case AUDITPIPE_GET_INSERTS
:
934 *(u_int
*)data
= ap
->ap_inserts
;
938 case AUDITPIPE_GET_READS
:
939 *(u_int
*)data
= ap
->ap_reads
;
943 case AUDITPIPE_GET_DROPS
:
944 *(u_int
*)data
= ap
->ap_drops
;
948 case AUDITPIPE_GET_TRUNCATES
:
960 * Audit pipe read. Read one or more partial or complete records to user
964 audit_pipe_read(dev_t dev
, struct uio
*uio
, __unused
int flag
)
966 struct audit_pipe_entry
*ape
;
967 struct audit_pipe
*ap
;
971 ap
= audit_pipe_dtab
[minor(dev
)];
972 KASSERT(ap
!= NULL
, ("audit_pipe_read: ap == NULL"));
975 * We hold an sleep lock over read and flush because we rely on the
976 * stability of a record in the queue during uiomove(9).
978 if (AUDIT_PIPE_SX_XLOCK_SIG(ap
) != 0)
981 while (TAILQ_EMPTY(&ap
->ap_queue
)) {
982 if (ap
->ap_flags
& AUDIT_PIPE_NBIO
) {
983 AUDIT_PIPE_UNLOCK(ap
);
984 AUDIT_PIPE_SX_XUNLOCK(ap
);
987 error
= cv_wait_sig(&ap
->ap_cv
, AUDIT_PIPE_MTX(ap
));
989 AUDIT_PIPE_UNLOCK(ap
);
990 AUDIT_PIPE_SX_XUNLOCK(ap
);
996 * Copy as many remaining bytes from the current record to userspace
997 * as we can. Keep processing records until we run out of records in
998 * the queue, or until the user buffer runs out of space.
1000 * Note: we rely on the sleep lock to maintain ape's stability here.
1003 while ((ape
= TAILQ_FIRST(&ap
->ap_queue
)) != NULL
&&
1004 uio_resid(uio
) > 0) {
1005 AUDIT_PIPE_LOCK_ASSERT(ap
);
1007 KASSERT(ape
->ape_record_len
> ap
->ap_qoffset
,
1008 ("audit_pipe_read: record_len > qoffset (1)"));
1009 toread
= MIN(ape
->ape_record_len
- ap
->ap_qoffset
,
1011 AUDIT_PIPE_UNLOCK(ap
);
1012 error
= uiomove((char *)ape
->ape_record
+ ap
->ap_qoffset
,
1015 AUDIT_PIPE_SX_XUNLOCK(ap
);
1020 * If the copy succeeded, update book-keeping, and if no
1021 * bytes remain in the current record, free it.
1023 AUDIT_PIPE_LOCK(ap
);
1024 KASSERT(TAILQ_FIRST(&ap
->ap_queue
) == ape
,
1025 ("audit_pipe_read: queue out of sync after uiomove"));
1026 ap
->ap_qoffset
+= toread
;
1027 KASSERT(ape
->ape_record_len
>= ap
->ap_qoffset
,
1028 ("audit_pipe_read: record_len >= qoffset (2)"));
1029 if (ap
->ap_qoffset
== ape
->ape_record_len
) {
1030 TAILQ_REMOVE(&ap
->ap_queue
, ape
, ape_queue
);
1031 ap
->ap_qbyteslen
-= ape
->ape_record_len
;
1032 audit_pipe_entry_free(ape
);
1037 AUDIT_PIPE_UNLOCK(ap
);
1038 AUDIT_PIPE_SX_XUNLOCK(ap
);
1046 audit_pipe_poll(dev_t dev
, int events
, void *wql
, struct proc
*p
)
1048 struct audit_pipe
*ap
;
1052 ap
= audit_pipe_dtab
[minor(dev
)];
1053 KASSERT(ap
!= NULL
, ("audit_pipe_poll: ap == NULL"));
1055 if (events
& (POLLIN
| POLLRDNORM
)) {
1056 AUDIT_PIPE_LOCK(ap
);
1057 if (TAILQ_FIRST(&ap
->ap_queue
) != NULL
)
1058 revents
|= events
& (POLLIN
| POLLRDNORM
);
1060 selrecord(p
, &ap
->ap_selinfo
, wql
);
1061 AUDIT_PIPE_UNLOCK(ap
);
1068 * Return true if there are records available for reading on the pipe.
1071 audit_pipe_kqread(struct knote
*kn
, long hint
)
1073 struct audit_pipe
*ap
;
1075 ap
= (struct audit_pipe
*)kn
->kn_hook
;
1076 KASSERT(ap
!= NULL
, ("audit_pipe_kqread: ap == NULL"));
1077 AUDIT_PIPE_LOCK_ASSERT(ap
);
1079 if (ap
->ap_qlen
!= 0) {
1080 kn
->kn_data
= ap
->ap_qbyteslen
- ap
->ap_qoffset
;
1089 * Detach kqueue state from audit pipe.
1092 audit_pipe_kqdetach(struct knote
*kn
)
1094 struct audit_pipe
*ap
;
1096 ap
= (struct audit_pipe
*)kn
->kn_hook
;
1097 KASSERT(ap
!= NULL
, ("audit_pipe_kqdetach: ap == NULL"));
1099 AUDIT_PIPE_LOCK(ap
);
1100 knlist_remove(&ap
->ap_selinfo
.si_note
, kn
, 1);
1101 AUDIT_PIPE_UNLOCK(ap
);
1103 #endif /* !__APPLE__ */
1105 static void *devnode
;
1108 audit_pipe_init(void)
1112 TAILQ_INIT(&audit_pipe_list
);
1113 AUDIT_PIPE_LIST_LOCK_INIT();
1115 audit_pipe_major
= cdevsw_add(-1, &audit_pipe_cdevsw
);
1116 if (audit_pipe_major
< 0)
1117 return (KERN_FAILURE
);
1119 dev
= makedev(audit_pipe_major
, 0);
1120 devnode
= devfs_make_node_clone(dev
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
,
1121 0600, audit_pipe_clone
, "auditpipe", 0);
1123 if (devnode
== NULL
)
1124 return (KERN_FAILURE
);
1126 return (KERN_SUCCESS
);
1130 audit_pipe_shutdown(void)
1133 /* unwind everything */
1134 devfs_remove(devnode
);
1135 (void) cdevsw_remove(audit_pipe_major
, &audit_pipe_cdevsw
);
1137 return (KERN_SUCCESS
);
1140 #endif /* CONFIG_AUDIT */