Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Creating audit events from TTY input. |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted |
| 5 | * material is made available to anyone wishing to use, modify, copy, or |
| 6 | * redistribute it subject to the terms and conditions of the GNU General |
| 7 | * Public License v.2. |
| 8 | * |
| 9 | * Authors: Miloslav Trmac <mitr@redhat.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/audit.h> |
| 13 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 14 | #include <linux/fdtable.h> |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 15 | #include <linux/tty.h> |
| 16 | |
| 17 | struct tty_audit_buf { |
| 18 | atomic_t count; |
| 19 | struct mutex mutex; /* Protects all data below */ |
| 20 | int major, minor; /* The TTY which the data is from */ |
| 21 | unsigned icanon:1; |
| 22 | size_t valid; |
| 23 | unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */ |
| 24 | }; |
| 25 | |
| 26 | static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor, |
| 27 | int icanon) |
| 28 | { |
| 29 | struct tty_audit_buf *buf; |
| 30 | |
Alan Cox | 66c6cea | 2008-02-08 04:18:46 -0800 | [diff] [blame] | 31 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 32 | if (!buf) |
| 33 | goto err; |
| 34 | if (PAGE_SIZE != N_TTY_BUF_SIZE) |
| 35 | buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
| 36 | else |
| 37 | buf->data = (unsigned char *)__get_free_page(GFP_KERNEL); |
| 38 | if (!buf->data) |
| 39 | goto err_buf; |
| 40 | atomic_set(&buf->count, 1); |
| 41 | mutex_init(&buf->mutex); |
| 42 | buf->major = major; |
| 43 | buf->minor = minor; |
| 44 | buf->icanon = icanon; |
| 45 | buf->valid = 0; |
| 46 | return buf; |
| 47 | |
| 48 | err_buf: |
| 49 | kfree(buf); |
| 50 | err: |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | static void tty_audit_buf_free(struct tty_audit_buf *buf) |
| 55 | { |
| 56 | WARN_ON(buf->valid != 0); |
| 57 | if (PAGE_SIZE != N_TTY_BUF_SIZE) |
| 58 | kfree(buf->data); |
| 59 | else |
| 60 | free_page((unsigned long)buf->data); |
| 61 | kfree(buf); |
| 62 | } |
| 63 | |
| 64 | static void tty_audit_buf_put(struct tty_audit_buf *buf) |
| 65 | { |
| 66 | if (atomic_dec_and_test(&buf->count)) |
| 67 | tty_audit_buf_free(buf); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * tty_audit_buf_push - Push buffered data out |
| 72 | * |
| 73 | * Generate an audit message from the contents of @buf, which is owned by |
| 74 | * @tsk with @loginuid. @buf->mutex must be locked. |
| 75 | */ |
| 76 | static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 77 | unsigned int sessionid, |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 78 | struct tty_audit_buf *buf) |
| 79 | { |
| 80 | struct audit_buffer *ab; |
| 81 | |
| 82 | if (buf->valid == 0) |
| 83 | return; |
| 84 | if (audit_enabled == 0) |
| 85 | return; |
| 86 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY); |
| 87 | if (ab) { |
| 88 | char name[sizeof(tsk->comm)]; |
| 89 | |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 90 | audit_log_format(ab, "tty pid=%u uid=%u auid=%u ses=%u " |
| 91 | "major=%d minor=%d comm=", tsk->pid, tsk->uid, |
| 92 | loginuid, sessionid, buf->major, buf->minor); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 93 | get_task_comm(name, tsk); |
| 94 | audit_log_untrustedstring(ab, name); |
| 95 | audit_log_format(ab, " data="); |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 96 | audit_log_n_untrustedstring(ab, buf->data, buf->valid); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 97 | audit_log_end(ab); |
| 98 | } |
| 99 | buf->valid = 0; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * tty_audit_buf_push_current - Push buffered data out |
| 104 | * |
| 105 | * Generate an audit message from the contents of @buf, which is owned by |
| 106 | * the current task. @buf->mutex must be locked. |
| 107 | */ |
| 108 | static void tty_audit_buf_push_current(struct tty_audit_buf *buf) |
| 109 | { |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 110 | uid_t auid = audit_get_loginuid(current); |
| 111 | unsigned int sessionid = audit_get_sessionid(current); |
| 112 | tty_audit_buf_push(current, auid, sessionid, buf); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * tty_audit_exit - Handle a task exit |
| 117 | * |
| 118 | * Make sure all buffered data is written out and deallocate the buffer. |
| 119 | * Only needs to be called if current->signal->tty_audit_buf != %NULL. |
| 120 | */ |
| 121 | void tty_audit_exit(void) |
| 122 | { |
| 123 | struct tty_audit_buf *buf; |
| 124 | |
| 125 | spin_lock_irq(¤t->sighand->siglock); |
| 126 | buf = current->signal->tty_audit_buf; |
| 127 | current->signal->tty_audit_buf = NULL; |
| 128 | spin_unlock_irq(¤t->sighand->siglock); |
| 129 | if (!buf) |
| 130 | return; |
| 131 | |
| 132 | mutex_lock(&buf->mutex); |
| 133 | tty_audit_buf_push_current(buf); |
| 134 | mutex_unlock(&buf->mutex); |
| 135 | |
| 136 | tty_audit_buf_put(buf); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * tty_audit_fork - Copy TTY audit state for a new task |
| 141 | * |
| 142 | * Set up TTY audit state in @sig from current. @sig needs no locking. |
| 143 | */ |
| 144 | void tty_audit_fork(struct signal_struct *sig) |
| 145 | { |
| 146 | spin_lock_irq(¤t->sighand->siglock); |
| 147 | sig->audit_tty = current->signal->audit_tty; |
| 148 | spin_unlock_irq(¤t->sighand->siglock); |
| 149 | sig->tty_audit_buf = NULL; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * tty_audit_push_task - Flush task's pending audit data |
| 154 | */ |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 155 | void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid) |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 156 | { |
| 157 | struct tty_audit_buf *buf; |
| 158 | |
| 159 | spin_lock_irq(&tsk->sighand->siglock); |
| 160 | buf = tsk->signal->tty_audit_buf; |
| 161 | if (buf) |
| 162 | atomic_inc(&buf->count); |
| 163 | spin_unlock_irq(&tsk->sighand->siglock); |
| 164 | if (!buf) |
| 165 | return; |
| 166 | |
| 167 | mutex_lock(&buf->mutex); |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 168 | tty_audit_buf_push(tsk, loginuid, sessionid, buf); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 169 | mutex_unlock(&buf->mutex); |
| 170 | |
| 171 | tty_audit_buf_put(buf); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * tty_audit_buf_get - Get an audit buffer. |
| 176 | * |
| 177 | * Get an audit buffer for @tty, allocate it if necessary. Return %NULL |
| 178 | * if TTY auditing is disabled or out of memory. Otherwise, return a new |
| 179 | * reference to the buffer. |
| 180 | */ |
| 181 | static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty) |
| 182 | { |
| 183 | struct tty_audit_buf *buf, *buf2; |
| 184 | |
| 185 | buf = NULL; |
| 186 | buf2 = NULL; |
| 187 | spin_lock_irq(¤t->sighand->siglock); |
| 188 | if (likely(!current->signal->audit_tty)) |
| 189 | goto out; |
| 190 | buf = current->signal->tty_audit_buf; |
| 191 | if (buf) { |
| 192 | atomic_inc(&buf->count); |
| 193 | goto out; |
| 194 | } |
| 195 | spin_unlock_irq(¤t->sighand->siglock); |
| 196 | |
| 197 | buf2 = tty_audit_buf_alloc(tty->driver->major, |
| 198 | tty->driver->minor_start + tty->index, |
| 199 | tty->icanon); |
| 200 | if (buf2 == NULL) { |
| 201 | audit_log_lost("out of memory in TTY auditing"); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | spin_lock_irq(¤t->sighand->siglock); |
| 206 | if (!current->signal->audit_tty) |
| 207 | goto out; |
| 208 | buf = current->signal->tty_audit_buf; |
| 209 | if (!buf) { |
| 210 | current->signal->tty_audit_buf = buf2; |
| 211 | buf = buf2; |
| 212 | buf2 = NULL; |
| 213 | } |
| 214 | atomic_inc(&buf->count); |
| 215 | /* Fall through */ |
| 216 | out: |
| 217 | spin_unlock_irq(¤t->sighand->siglock); |
| 218 | if (buf2) |
| 219 | tty_audit_buf_free(buf2); |
| 220 | return buf; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * tty_audit_add_data - Add data for TTY auditing. |
| 225 | * |
| 226 | * Audit @data of @size from @tty, if necessary. |
| 227 | */ |
| 228 | void tty_audit_add_data(struct tty_struct *tty, unsigned char *data, |
| 229 | size_t size) |
| 230 | { |
| 231 | struct tty_audit_buf *buf; |
| 232 | int major, minor; |
| 233 | |
| 234 | if (unlikely(size == 0)) |
| 235 | return; |
| 236 | |
Miloslav Trmac | 4112622 | 2008-04-18 13:30:14 -0700 | [diff] [blame] | 237 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY |
| 238 | && tty->driver->subtype == PTY_TYPE_MASTER) |
| 239 | return; |
| 240 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 241 | buf = tty_audit_buf_get(tty); |
| 242 | if (!buf) |
| 243 | return; |
| 244 | |
| 245 | mutex_lock(&buf->mutex); |
| 246 | major = tty->driver->major; |
| 247 | minor = tty->driver->minor_start + tty->index; |
| 248 | if (buf->major != major || buf->minor != minor |
| 249 | || buf->icanon != tty->icanon) { |
| 250 | tty_audit_buf_push_current(buf); |
| 251 | buf->major = major; |
| 252 | buf->minor = minor; |
| 253 | buf->icanon = tty->icanon; |
| 254 | } |
| 255 | do { |
| 256 | size_t run; |
| 257 | |
| 258 | run = N_TTY_BUF_SIZE - buf->valid; |
| 259 | if (run > size) |
| 260 | run = size; |
| 261 | memcpy(buf->data + buf->valid, data, run); |
| 262 | buf->valid += run; |
| 263 | data += run; |
| 264 | size -= run; |
| 265 | if (buf->valid == N_TTY_BUF_SIZE) |
| 266 | tty_audit_buf_push_current(buf); |
| 267 | } while (size != 0); |
| 268 | mutex_unlock(&buf->mutex); |
| 269 | tty_audit_buf_put(buf); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * tty_audit_push - Push buffered data out |
| 274 | * |
| 275 | * Make sure no audit data is pending for @tty on the current process. |
| 276 | */ |
| 277 | void tty_audit_push(struct tty_struct *tty) |
| 278 | { |
| 279 | struct tty_audit_buf *buf; |
| 280 | |
| 281 | spin_lock_irq(¤t->sighand->siglock); |
| 282 | if (likely(!current->signal->audit_tty)) { |
| 283 | spin_unlock_irq(¤t->sighand->siglock); |
| 284 | return; |
| 285 | } |
| 286 | buf = current->signal->tty_audit_buf; |
| 287 | if (buf) |
| 288 | atomic_inc(&buf->count); |
| 289 | spin_unlock_irq(¤t->sighand->siglock); |
| 290 | |
| 291 | if (buf) { |
| 292 | int major, minor; |
| 293 | |
| 294 | major = tty->driver->major; |
| 295 | minor = tty->driver->minor_start + tty->index; |
| 296 | mutex_lock(&buf->mutex); |
| 297 | if (buf->major == major && buf->minor == minor) |
| 298 | tty_audit_buf_push_current(buf); |
| 299 | mutex_unlock(&buf->mutex); |
| 300 | tty_audit_buf_put(buf); |
| 301 | } |
| 302 | } |