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