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