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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 14 | #include <linux/tty.h> |
| 15 | |
| 16 | struct tty_audit_buf { |
| 17 | atomic_t count; |
| 18 | struct mutex mutex; /* Protects all data below */ |
| 19 | int major, minor; /* The TTY which the data is from */ |
| 20 | unsigned icanon:1; |
| 21 | size_t valid; |
| 22 | unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */ |
| 23 | }; |
| 24 | |
| 25 | static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor, |
| 26 | int icanon) |
| 27 | { |
| 28 | struct tty_audit_buf *buf; |
| 29 | |
Alan Cox | 66c6cea | 2008-02-08 04:18:46 -0800 | [diff] [blame] | 30 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 31 | if (!buf) |
| 32 | goto err; |
Alan Cox | c481c70 | 2009-06-11 13:04:27 +0100 | [diff] [blame] | 33 | buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 34 | if (!buf->data) |
| 35 | goto err_buf; |
| 36 | atomic_set(&buf->count, 1); |
| 37 | mutex_init(&buf->mutex); |
| 38 | buf->major = major; |
| 39 | buf->minor = minor; |
| 40 | buf->icanon = icanon; |
| 41 | buf->valid = 0; |
| 42 | return buf; |
| 43 | |
| 44 | err_buf: |
| 45 | kfree(buf); |
| 46 | err: |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | static void tty_audit_buf_free(struct tty_audit_buf *buf) |
| 51 | { |
| 52 | WARN_ON(buf->valid != 0); |
Alan Cox | c481c70 | 2009-06-11 13:04:27 +0100 | [diff] [blame] | 53 | kfree(buf->data); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 54 | kfree(buf); |
| 55 | } |
| 56 | |
| 57 | static void tty_audit_buf_put(struct tty_audit_buf *buf) |
| 58 | { |
| 59 | if (atomic_dec_and_test(&buf->count)) |
| 60 | tty_audit_buf_free(buf); |
| 61 | } |
| 62 | |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 63 | static void tty_audit_log(const char *description, struct task_struct *tsk, |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 64 | kuid_t loginuid, unsigned sessionid, int major, |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 65 | int minor, unsigned char *data, size_t size) |
| 66 | { |
| 67 | struct audit_buffer *ab; |
| 68 | |
| 69 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY); |
| 70 | if (ab) { |
| 71 | char name[sizeof(tsk->comm)]; |
| 72 | uid_t uid = task_uid(tsk); |
| 73 | |
| 74 | audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u " |
| 75 | "major=%d minor=%d comm=", description, |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 76 | tsk->pid, uid, |
| 77 | from_kuid(&init_user_ns, loginuid), |
| 78 | sessionid, |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 79 | major, minor); |
| 80 | get_task_comm(name, tsk); |
| 81 | audit_log_untrustedstring(ab, name); |
| 82 | audit_log_format(ab, " data="); |
| 83 | audit_log_n_hex(ab, data, size); |
| 84 | audit_log_end(ab); |
| 85 | } |
| 86 | } |
| 87 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 88 | /** |
| 89 | * tty_audit_buf_push - Push buffered data out |
| 90 | * |
| 91 | * Generate an audit message from the contents of @buf, which is owned by |
| 92 | * @tsk with @loginuid. @buf->mutex must be locked. |
| 93 | */ |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 94 | static void tty_audit_buf_push(struct task_struct *tsk, kuid_t loginuid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 95 | unsigned int sessionid, |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 96 | struct tty_audit_buf *buf) |
| 97 | { |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 98 | if (buf->valid == 0) |
| 99 | return; |
Xiaotian Feng | 00bff39 | 2011-03-03 18:08:24 +0800 | [diff] [blame] | 100 | if (audit_enabled == 0) { |
| 101 | buf->valid = 0; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 102 | return; |
Xiaotian Feng | 00bff39 | 2011-03-03 18:08:24 +0800 | [diff] [blame] | 103 | } |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 104 | tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor, |
| 105 | buf->data, buf->valid); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 106 | buf->valid = 0; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * tty_audit_buf_push_current - Push buffered data out |
| 111 | * |
| 112 | * Generate an audit message from the contents of @buf, which is owned by |
| 113 | * the current task. @buf->mutex must be locked. |
| 114 | */ |
| 115 | static void tty_audit_buf_push_current(struct tty_audit_buf *buf) |
| 116 | { |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 117 | kuid_t auid = audit_get_loginuid(current); |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 118 | unsigned int sessionid = audit_get_sessionid(current); |
| 119 | tty_audit_buf_push(current, auid, sessionid, buf); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * tty_audit_exit - Handle a task exit |
| 124 | * |
| 125 | * Make sure all buffered data is written out and deallocate the buffer. |
| 126 | * Only needs to be called if current->signal->tty_audit_buf != %NULL. |
| 127 | */ |
| 128 | void tty_audit_exit(void) |
| 129 | { |
| 130 | struct tty_audit_buf *buf; |
| 131 | |
| 132 | spin_lock_irq(¤t->sighand->siglock); |
| 133 | buf = current->signal->tty_audit_buf; |
| 134 | current->signal->tty_audit_buf = NULL; |
| 135 | spin_unlock_irq(¤t->sighand->siglock); |
| 136 | if (!buf) |
| 137 | return; |
| 138 | |
| 139 | mutex_lock(&buf->mutex); |
| 140 | tty_audit_buf_push_current(buf); |
| 141 | mutex_unlock(&buf->mutex); |
| 142 | |
| 143 | tty_audit_buf_put(buf); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * tty_audit_fork - Copy TTY audit state for a new task |
| 148 | * |
| 149 | * Set up TTY audit state in @sig from current. @sig needs no locking. |
| 150 | */ |
| 151 | void tty_audit_fork(struct signal_struct *sig) |
| 152 | { |
| 153 | spin_lock_irq(¤t->sighand->siglock); |
| 154 | sig->audit_tty = current->signal->audit_tty; |
| 155 | spin_unlock_irq(¤t->sighand->siglock); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 159 | * tty_audit_tiocsti - Log TIOCSTI |
| 160 | */ |
| 161 | void tty_audit_tiocsti(struct tty_struct *tty, char ch) |
| 162 | { |
| 163 | struct tty_audit_buf *buf; |
| 164 | int major, minor, should_audit; |
| 165 | |
| 166 | spin_lock_irq(¤t->sighand->siglock); |
| 167 | should_audit = current->signal->audit_tty; |
| 168 | buf = current->signal->tty_audit_buf; |
| 169 | if (buf) |
| 170 | atomic_inc(&buf->count); |
| 171 | spin_unlock_irq(¤t->sighand->siglock); |
| 172 | |
| 173 | major = tty->driver->major; |
| 174 | minor = tty->driver->minor_start + tty->index; |
| 175 | if (buf) { |
| 176 | mutex_lock(&buf->mutex); |
| 177 | if (buf->major == major && buf->minor == minor) |
| 178 | tty_audit_buf_push_current(buf); |
| 179 | mutex_unlock(&buf->mutex); |
| 180 | tty_audit_buf_put(buf); |
| 181 | } |
| 182 | |
| 183 | if (should_audit && audit_enabled) { |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 184 | kuid_t auid; |
Al Viro | 1e64174 | 2008-12-09 09:23:33 +0000 | [diff] [blame] | 185 | unsigned int sessionid; |
| 186 | |
| 187 | auid = audit_get_loginuid(current); |
| 188 | sessionid = audit_get_sessionid(current); |
| 189 | tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major, |
| 190 | minor, &ch, 1); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Thomas Gleixner | 3c80fe4 | 2009-12-09 14:19:31 +0000 | [diff] [blame] | 195 | * tty_audit_push_task - Flush task's pending audit data |
| 196 | * @tsk: task pointer |
| 197 | * @loginuid: sender login uid |
| 198 | * @sessionid: sender session id |
| 199 | * |
| 200 | * Called with a ref on @tsk held. Try to lock sighand and get a |
| 201 | * reference to the tty audit buffer if available. |
| 202 | * Flush the buffer or return an appropriate error code. |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 203 | */ |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame^] | 204 | int tty_audit_push_task(struct task_struct *tsk, kuid_t loginuid, u32 sessionid) |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 205 | { |
Thomas Gleixner | 3c80fe4 | 2009-12-09 14:19:31 +0000 | [diff] [blame] | 206 | struct tty_audit_buf *buf = ERR_PTR(-EPERM); |
| 207 | unsigned long flags; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 208 | |
Thomas Gleixner | 3c80fe4 | 2009-12-09 14:19:31 +0000 | [diff] [blame] | 209 | if (!lock_task_sighand(tsk, &flags)) |
| 210 | return -ESRCH; |
| 211 | |
| 212 | if (tsk->signal->audit_tty) { |
| 213 | buf = tsk->signal->tty_audit_buf; |
| 214 | if (buf) |
| 215 | atomic_inc(&buf->count); |
| 216 | } |
| 217 | unlock_task_sighand(tsk, &flags); |
| 218 | |
| 219 | /* |
| 220 | * Return 0 when signal->audit_tty set |
| 221 | * but tsk->signal->tty_audit_buf == NULL. |
| 222 | */ |
| 223 | if (!buf || IS_ERR(buf)) |
| 224 | return PTR_ERR(buf); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 225 | |
| 226 | mutex_lock(&buf->mutex); |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 227 | tty_audit_buf_push(tsk, loginuid, sessionid, buf); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 228 | mutex_unlock(&buf->mutex); |
| 229 | |
| 230 | tty_audit_buf_put(buf); |
Thomas Gleixner | 3c80fe4 | 2009-12-09 14:19:31 +0000 | [diff] [blame] | 231 | return 0; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * tty_audit_buf_get - Get an audit buffer. |
| 236 | * |
| 237 | * Get an audit buffer for @tty, allocate it if necessary. Return %NULL |
| 238 | * if TTY auditing is disabled or out of memory. Otherwise, return a new |
| 239 | * reference to the buffer. |
| 240 | */ |
| 241 | static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty) |
| 242 | { |
| 243 | struct tty_audit_buf *buf, *buf2; |
| 244 | |
| 245 | buf = NULL; |
| 246 | buf2 = NULL; |
| 247 | spin_lock_irq(¤t->sighand->siglock); |
| 248 | if (likely(!current->signal->audit_tty)) |
| 249 | goto out; |
| 250 | buf = current->signal->tty_audit_buf; |
| 251 | if (buf) { |
| 252 | atomic_inc(&buf->count); |
| 253 | goto out; |
| 254 | } |
| 255 | spin_unlock_irq(¤t->sighand->siglock); |
| 256 | |
| 257 | buf2 = tty_audit_buf_alloc(tty->driver->major, |
| 258 | tty->driver->minor_start + tty->index, |
| 259 | tty->icanon); |
| 260 | if (buf2 == NULL) { |
| 261 | audit_log_lost("out of memory in TTY auditing"); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | spin_lock_irq(¤t->sighand->siglock); |
| 266 | if (!current->signal->audit_tty) |
| 267 | goto out; |
| 268 | buf = current->signal->tty_audit_buf; |
| 269 | if (!buf) { |
| 270 | current->signal->tty_audit_buf = buf2; |
| 271 | buf = buf2; |
| 272 | buf2 = NULL; |
| 273 | } |
| 274 | atomic_inc(&buf->count); |
| 275 | /* Fall through */ |
| 276 | out: |
| 277 | spin_unlock_irq(¤t->sighand->siglock); |
| 278 | if (buf2) |
| 279 | tty_audit_buf_free(buf2); |
| 280 | return buf; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * tty_audit_add_data - Add data for TTY auditing. |
| 285 | * |
| 286 | * Audit @data of @size from @tty, if necessary. |
| 287 | */ |
| 288 | void tty_audit_add_data(struct tty_struct *tty, unsigned char *data, |
| 289 | size_t size) |
| 290 | { |
| 291 | struct tty_audit_buf *buf; |
| 292 | int major, minor; |
| 293 | |
| 294 | if (unlikely(size == 0)) |
| 295 | return; |
| 296 | |
Miloslav Trmac | 4112622 | 2008-04-18 13:30:14 -0700 | [diff] [blame] | 297 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY |
| 298 | && tty->driver->subtype == PTY_TYPE_MASTER) |
| 299 | return; |
| 300 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 301 | buf = tty_audit_buf_get(tty); |
| 302 | if (!buf) |
| 303 | return; |
| 304 | |
| 305 | mutex_lock(&buf->mutex); |
| 306 | major = tty->driver->major; |
| 307 | minor = tty->driver->minor_start + tty->index; |
| 308 | if (buf->major != major || buf->minor != minor |
| 309 | || buf->icanon != tty->icanon) { |
| 310 | tty_audit_buf_push_current(buf); |
| 311 | buf->major = major; |
| 312 | buf->minor = minor; |
| 313 | buf->icanon = tty->icanon; |
| 314 | } |
| 315 | do { |
| 316 | size_t run; |
| 317 | |
| 318 | run = N_TTY_BUF_SIZE - buf->valid; |
| 319 | if (run > size) |
| 320 | run = size; |
| 321 | memcpy(buf->data + buf->valid, data, run); |
| 322 | buf->valid += run; |
| 323 | data += run; |
| 324 | size -= run; |
| 325 | if (buf->valid == N_TTY_BUF_SIZE) |
| 326 | tty_audit_buf_push_current(buf); |
| 327 | } while (size != 0); |
| 328 | mutex_unlock(&buf->mutex); |
| 329 | tty_audit_buf_put(buf); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * tty_audit_push - Push buffered data out |
| 334 | * |
| 335 | * Make sure no audit data is pending for @tty on the current process. |
| 336 | */ |
| 337 | void tty_audit_push(struct tty_struct *tty) |
| 338 | { |
| 339 | struct tty_audit_buf *buf; |
| 340 | |
| 341 | spin_lock_irq(¤t->sighand->siglock); |
| 342 | if (likely(!current->signal->audit_tty)) { |
| 343 | spin_unlock_irq(¤t->sighand->siglock); |
| 344 | return; |
| 345 | } |
| 346 | buf = current->signal->tty_audit_buf; |
| 347 | if (buf) |
| 348 | atomic_inc(&buf->count); |
| 349 | spin_unlock_irq(¤t->sighand->siglock); |
| 350 | |
| 351 | if (buf) { |
| 352 | int major, minor; |
| 353 | |
| 354 | major = tty->driver->major; |
| 355 | minor = tty->driver->minor_start + tty->index; |
| 356 | mutex_lock(&buf->mutex); |
| 357 | if (buf->major == major && buf->minor == minor) |
| 358 | tty_audit_buf_push_current(buf); |
| 359 | mutex_unlock(&buf->mutex); |
| 360 | tty_audit_buf_put(buf); |
| 361 | } |
| 362 | } |