xref: /freebsd/sys/security/audit/audit_trigger.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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/param.h>
30 #include <sys/conf.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 #include <sys/uio.h>
39 
40 #include <security/audit/audit.h>
41 #include <security/audit/audit_private.h>
42 
43 /*
44  * Structures and operations to support the basic character special device
45  * used to communicate with userland.  /dev/audit reliably delivers one-byte
46  * messages to a listening application (or discards them if there is no
47  * listening application).
48  *
49  * Currently, select/poll are not supported on the trigger device.
50  */
51 struct trigger_info {
52 	unsigned int			trigger;
53 	TAILQ_ENTRY(trigger_info)	list;
54 };
55 
56 static MALLOC_DEFINE(M_AUDITTRIGGER, "audit_trigger", "Audit trigger events");
57 static struct cdev *audit_dev;
58 static int audit_isopen = 0;
59 static TAILQ_HEAD(, trigger_info) trigger_list;
60 static struct mtx audit_trigger_mtx;
61 
62 static int
63 audit_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
64 {
65 	int error;
66 
67 	/* Only one process may open the device at a time. */
68 	mtx_lock(&audit_trigger_mtx);
69 	if (!audit_isopen) {
70 		error = 0;
71 		audit_isopen = 1;
72 	} else
73 		error = EBUSY;
74 	mtx_unlock(&audit_trigger_mtx);
75 
76 	return (error);
77 }
78 
79 static int
80 audit_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
81 {
82 	struct trigger_info *ti;
83 
84 	/* Flush the queue of pending trigger events. */
85 	mtx_lock(&audit_trigger_mtx);
86 	audit_isopen = 0;
87 	while (!TAILQ_EMPTY(&trigger_list)) {
88 		ti = TAILQ_FIRST(&trigger_list);
89 		TAILQ_REMOVE(&trigger_list, ti, list);
90 		free(ti, M_AUDITTRIGGER);
91 	}
92 	mtx_unlock(&audit_trigger_mtx);
93 
94 	return (0);
95 }
96 
97 static int
98 audit_read(struct cdev *dev, struct uio *uio, int ioflag)
99 {
100 	int error = 0;
101 	struct trigger_info *ti = NULL;
102 
103 	mtx_lock(&audit_trigger_mtx);
104 	while (TAILQ_EMPTY(&trigger_list)) {
105 		error = msleep(&trigger_list, &audit_trigger_mtx,
106 		    PSOCK | PCATCH, "auditd", 0);
107 		if (error)
108 			break;
109 	}
110 	if (!error) {
111 		ti = TAILQ_FIRST(&trigger_list);
112 		TAILQ_REMOVE(&trigger_list, ti, list);
113 	}
114 	mtx_unlock(&audit_trigger_mtx);
115 	if (!error) {
116 		error = uiomove(&ti->trigger, sizeof(ti->trigger), uio);
117 		free(ti, M_AUDITTRIGGER);
118 	}
119 	return (error);
120 }
121 
122 static int
123 audit_write(struct cdev *dev, struct uio *uio, int ioflag)
124 {
125 
126 	/* Communication is kernel->userspace only. */
127 	return (EOPNOTSUPP);
128 }
129 
130 int
131 audit_send_trigger(unsigned int trigger)
132 {
133 	struct trigger_info *ti;
134 
135 	ti = malloc(sizeof *ti, M_AUDITTRIGGER, M_WAITOK);
136 	mtx_lock(&audit_trigger_mtx);
137 	if (!audit_isopen) {
138 		/* If nobody's listening, we ain't talking. */
139 		mtx_unlock(&audit_trigger_mtx);
140 		free(ti, M_AUDITTRIGGER);
141 		return (ENODEV);
142 	}
143 	ti->trigger = trigger;
144 	TAILQ_INSERT_TAIL(&trigger_list, ti, list);
145 	wakeup(&trigger_list);
146 	mtx_unlock(&audit_trigger_mtx);
147 	return (0);
148 }
149 
150 static struct cdevsw audit_cdevsw = {
151 	.d_version =	D_VERSION,
152 	.d_open =	audit_open,
153 	.d_close =	audit_close,
154 	.d_read =	audit_read,
155 	.d_write =	audit_write,
156 	.d_name =	"audit"
157 };
158 
159 void
160 audit_trigger_init(void)
161 {
162 
163 	TAILQ_INIT(&trigger_list);
164 	mtx_init(&audit_trigger_mtx, "audit_trigger_mtx", NULL, MTX_DEF);
165 }
166 
167 static void
168 audit_trigger_cdev_init(void *unused)
169 {
170 
171 	/* Create the special device file. */
172 	audit_dev = make_dev(&audit_cdevsw, 0, UID_ROOT, GID_KMEM, 0600,
173 	    AUDITDEV_FILENAME);
174 }
175 
176 SYSINIT(audit_trigger_cdev_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
177     audit_trigger_cdev_init, NULL);
178