1/*-
2 * Copyright (c) 2005 Wayne J. Salamon
3 * All rights reserved.
4 *
5 * This software was developed by Wayne Salamon for the TrustedBSD Project.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/conf.h>
34#include <sys/kernel.h>
35#include <sys/lock.h>
36#include <sys/malloc.h>
37#include <sys/mutex.h>
38#include <sys/proc.h>
39#include <sys/queue.h>
40#include <sys/systm.h>
41#include <sys/uio.h>
42
43#include <security/audit/audit.h>
44#include <security/audit/audit_private.h>
45
46/*
47 * Structures and operations to support the basic character special device
48 * used to communicate with userland.  /dev/audit reliably delivers one-byte
49 * messages to a listening application (or discards them if there is no
50 * listening application).
51 *
52 * Currently, select/poll are not supported on the trigger device.
53 */
54struct trigger_info {
55	unsigned int			trigger;
56	TAILQ_ENTRY(trigger_info)	list;
57};
58
59static MALLOC_DEFINE(M_AUDITTRIGGER, "audit_trigger", "Audit trigger events");
60static struct cdev *audit_dev;
61static int audit_isopen = 0;
62static TAILQ_HEAD(, trigger_info) trigger_list;
63static struct mtx audit_trigger_mtx;
64
65static int
66audit_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
67{
68	int error;
69
70	/* Only one process may open the device at a time. */
71	mtx_lock(&audit_trigger_mtx);
72	if (!audit_isopen) {
73		error = 0;
74		audit_isopen = 1;
75	} else
76		error = EBUSY;
77	mtx_unlock(&audit_trigger_mtx);
78
79	return (error);
80}
81
82static int
83audit_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
84{
85	struct trigger_info *ti;
86
87	/* Flush the queue of pending trigger events. */
88	mtx_lock(&audit_trigger_mtx);
89	audit_isopen = 0;
90	while (!TAILQ_EMPTY(&trigger_list)) {
91		ti = TAILQ_FIRST(&trigger_list);
92		TAILQ_REMOVE(&trigger_list, ti, list);
93		free(ti, M_AUDITTRIGGER);
94	}
95	mtx_unlock(&audit_trigger_mtx);
96
97	return (0);
98}
99
100static int
101audit_read(struct cdev *dev, struct uio *uio, int ioflag)
102{
103	int error = 0;
104	struct trigger_info *ti = NULL;
105
106	mtx_lock(&audit_trigger_mtx);
107	while (TAILQ_EMPTY(&trigger_list)) {
108		error = msleep(&trigger_list, &audit_trigger_mtx,
109		    PSOCK | PCATCH, "auditd", 0);
110		if (error)
111			break;
112	}
113	if (!error) {
114		ti = TAILQ_FIRST(&trigger_list);
115		TAILQ_REMOVE(&trigger_list, ti, list);
116	}
117	mtx_unlock(&audit_trigger_mtx);
118	if (!error) {
119		error = uiomove(&ti->trigger, sizeof(ti->trigger), uio);
120		free(ti, M_AUDITTRIGGER);
121	}
122	return (error);
123}
124
125static int
126audit_write(struct cdev *dev, struct uio *uio, int ioflag)
127{
128
129	/* Communication is kernel->userspace only. */
130	return (EOPNOTSUPP);
131}
132
133int
134audit_send_trigger(unsigned int trigger)
135{
136	struct trigger_info *ti;
137
138	ti = malloc(sizeof *ti, M_AUDITTRIGGER, M_WAITOK);
139	mtx_lock(&audit_trigger_mtx);
140	if (!audit_isopen) {
141		/* If nobody's listening, we ain't talking. */
142		mtx_unlock(&audit_trigger_mtx);
143		free(ti, M_AUDITTRIGGER);
144		return (ENODEV);
145	}
146	ti->trigger = trigger;
147	TAILQ_INSERT_TAIL(&trigger_list, ti, list);
148	wakeup(&trigger_list);
149	mtx_unlock(&audit_trigger_mtx);
150	return (0);
151}
152
153static struct cdevsw audit_cdevsw = {
154	.d_version =	D_VERSION,
155	.d_open =	audit_open,
156	.d_close =	audit_close,
157	.d_read =	audit_read,
158	.d_write =	audit_write,
159	.d_name =	"audit"
160};
161
162void
163audit_trigger_init(void)
164{
165
166	TAILQ_INIT(&trigger_list);
167	mtx_init(&audit_trigger_mtx, "audit_trigger_mtx", NULL, MTX_DEF);
168}
169
170static void
171audit_trigger_cdev_init(void *unused)
172{
173
174	/* Create the special device file. */
175	audit_dev = make_dev(&audit_cdevsw, 0, UID_ROOT, GID_KMEM, 0600,
176	    AUDITDEV_FILENAME);
177}
178
179SYSINIT(audit_trigger_cdev_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
180    audit_trigger_cdev_init, NULL);
181