e3b3d0f549c1d drivers/tty/tty_audit.c (Greg Kroah-Hartman 2017-11-06 18:11:51 +0100 1) // SPDX-License-Identifier: GPL-2.0
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 2) /*
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 3) * Creating audit events from TTY input.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 4) *
e5656d43dce82 drivers/tty/tty_audit.c (Greg Kroah-Hartman 2017-11-06 18:11:55 +0100 5) * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 6) *
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 7) * Authors: Miloslav Trmac <mitr@redhat.com>
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 8) */
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 9)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 10) #include <linux/audit.h>
5a0e3ad6af866 drivers/char/tty_audit.c (Tejun Heo 2010-03-24 17:04:11 +0900 11) #include <linux/slab.h>
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 12) #include <linux/tty.h>
da5d669e00d2c drivers/tty/tty_audit.c (Greg Kroah-Hartman 2021-04-08 14:51:29 +0200 13) #include "tty.h"
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 14)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 15) struct tty_audit_buf {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 16) struct mutex mutex; /* Protects all data below */
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 17) dev_t dev; /* The TTY which the data is from */
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 18) unsigned icanon:1;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 19) size_t valid;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 20) unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 21) };
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 22)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 23) static struct tty_audit_buf *tty_audit_buf_ref(void)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 24) {
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 25) struct tty_audit_buf *buf;
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 26)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 27) buf = current->signal->tty_audit_buf;
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 28) WARN_ON(buf == ERR_PTR(-ESRCH));
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 29) return buf;
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 30) }
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 31)
a75c9b091226c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:28 -0800 32) static struct tty_audit_buf *tty_audit_buf_alloc(void)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 33) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 34) struct tty_audit_buf *buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 35)
66c6ceae39534 drivers/char/tty_audit.c (Alan Cox 2008-02-08 04:18:46 -0800 36) buf = kmalloc(sizeof(*buf), GFP_KERNEL);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 37) if (!buf)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 38) goto err;
c481c707fe4b0 drivers/char/tty_audit.c (Alan Cox 2009-06-11 13:04:27 +0100 39) buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 40) if (!buf->data)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 41) goto err_buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 42) mutex_init(&buf->mutex);
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 43) buf->dev = MKDEV(0, 0);
a75c9b091226c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:28 -0800 44) buf->icanon = 0;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 45) buf->valid = 0;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 46) return buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 47)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 48) err_buf:
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 49) kfree(buf);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 50) err:
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 51) return NULL;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 52) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 53)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 54) static void tty_audit_buf_free(struct tty_audit_buf *buf)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 55) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 56) WARN_ON(buf->valid != 0);
c481c707fe4b0 drivers/char/tty_audit.c (Alan Cox 2009-06-11 13:04:27 +0100 57) kfree(buf->data);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 58) kfree(buf);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 59) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 60)
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 61) static void tty_audit_log(const char *description, dev_t dev,
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 62) unsigned char *data, size_t size)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 63) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 64) struct audit_buffer *ab;
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 65) pid_t pid = task_pid_nr(current);
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 66) uid_t uid = from_kuid(&init_user_ns, task_uid(current));
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 67) uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 68) unsigned int sessionid = audit_get_sessionid(current);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 69)
18f5c1d567a5d drivers/tty/tty_audit.c (Richard Guy Briggs 2019-02-05 17:19:06 -0500 70) ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_TTY);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 71) if (ab) {
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 72) char name[sizeof(current->comm)];
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 73)
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 74) audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
f1dc4867ff41b drivers/tty/tty_audit.c (Richard Guy Briggs 2013-12-11 13:52:26 -0500 75) " minor=%d comm=", description, pid, uid,
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 76) loginuid, sessionid, MAJOR(dev), MINOR(dev));
2a1fe215e7300 drivers/tty/tty_audit.c (Paul Moore 2018-11-26 18:40:07 -0500 77) get_task_comm(name, current);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 78) audit_log_untrustedstring(ab, name);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 79) audit_log_format(ab, " data=");
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 80) audit_log_n_hex(ab, data, size);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 81) audit_log_end(ab);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 82) }
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 83) }
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 84)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 85) /*
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 86) * tty_audit_buf_push - Push buffered data out
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 87) *
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 88) * Generate an audit message from the contents of @buf, which is owned by
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 89) * the current task. @buf->mutex must be locked.
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 90) */
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 91) static void tty_audit_buf_push(struct tty_audit_buf *buf)
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 92) {
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 93) if (buf->valid == 0)
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 94) return;
f7859590d9761 drivers/tty/tty_audit.c (Richard Guy Briggs 2018-06-05 19:20:39 -0400 95) if (audit_enabled == AUDIT_OFF) {
00bff392c81e4 drivers/tty/tty_audit.c (Xiaotian Feng 2011-03-03 18:08:24 +0800 96) buf->valid = 0;
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 97) return;
00bff392c81e4 drivers/tty/tty_audit.c (Xiaotian Feng 2011-03-03 18:08:24 +0800 98) }
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 99) tty_audit_log("tty", buf->dev, buf->data, buf->valid);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 100) buf->valid = 0;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 101) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 102)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 103) /**
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 104) * tty_audit_exit - Handle a task exit
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 105) *
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 106) * Make sure all buffered data is written out and deallocate the buffer.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 107) * Only needs to be called if current->signal->tty_audit_buf != %NULL.
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 108) *
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 109) * The process is single-threaded at this point; no other threads share
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 110) * current->signal.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 111) */
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 112) void tty_audit_exit(void)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 113) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 114) struct tty_audit_buf *buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 115)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 116) buf = xchg(¤t->signal->tty_audit_buf, ERR_PTR(-ESRCH));
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 117) if (!buf)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 118) return;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 119)
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 120) tty_audit_buf_push(buf);
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 121) tty_audit_buf_free(buf);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 122) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 123)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 124) /*
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 125) * tty_audit_fork - Copy TTY audit state for a new task
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 126) *
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 127) * Set up TTY audit state in @sig from current. @sig needs no locking.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 128) */
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 129) void tty_audit_fork(struct signal_struct *sig)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 130) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 131) sig->audit_tty = current->signal->audit_tty;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 132) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 133)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 134) /*
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 135) * tty_audit_tiocsti - Log TIOCSTI
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 136) */
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 137) void tty_audit_tiocsti(struct tty_struct *tty, char ch)
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 138) {
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 139) dev_t dev;
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 140)
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 141) dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
82b5c93a00169 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:38 -0800 142) if (tty_audit_push())
82b5c93a00169 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:38 -0800 143) return;
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 144)
54555919e8a1b drivers/tty/tty_audit.c (Sudip Mukherjee 2016-02-24 16:45:09 +0530 145) if (audit_enabled)
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 146) tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 147) }
1e641743f055f drivers/char/tty_audit.c (Al Viro 2008-12-09 09:23:33 +0000 148)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 149) /*
37282a77954aa drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:31 -0800 150) * tty_audit_push - Flush current's pending audit data
3c80fe4ac9cfb drivers/char/tty_audit.c (Thomas Gleixner 2009-12-09 14:19:31 +0000 151) *
37282a77954aa drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:31 -0800 152) * Returns 0 if success, -EPERM if tty audit is disabled
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 153) */
37282a77954aa drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:31 -0800 154) int tty_audit_push(void)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 155) {
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 156) struct tty_audit_buf *buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 157)
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 158) if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 159) return -EPERM;
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 160)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 161) buf = tty_audit_buf_ref();
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 162) if (!IS_ERR_OR_NULL(buf)) {
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 163) mutex_lock(&buf->mutex);
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 164) tty_audit_buf_push(buf);
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 165) mutex_unlock(&buf->mutex);
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 166) }
3c80fe4ac9cfb drivers/char/tty_audit.c (Thomas Gleixner 2009-12-09 14:19:31 +0000 167) return 0;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 168) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 169)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 170) /*
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 171) * tty_audit_buf_get - Get an audit buffer.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 172) *
a75c9b091226c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:28 -0800 173) * Get an audit buffer, allocate it if necessary. Return %NULL
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 174) * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 175) * occurred. Otherwise, return a new reference to the buffer.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 176) */
a75c9b091226c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:28 -0800 177) static struct tty_audit_buf *tty_audit_buf_get(void)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 178) {
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 179) struct tty_audit_buf *buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 180)
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 181) buf = tty_audit_buf_ref();
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 182) if (buf)
5493090fc2341 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:35 -0800 183) return buf;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 184)
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 185) buf = tty_audit_buf_alloc();
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 186) if (buf == NULL) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 187) audit_log_lost("out of memory in TTY auditing");
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 188) return NULL;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 189) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 190)
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 191) /* Race to use this buffer, free it if another wins */
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 192) if (cmpxchg(¤t->signal->tty_audit_buf, NULL, buf) != NULL)
fbaa122718a12 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:36 -0800 193) tty_audit_buf_free(buf);
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 194) return tty_audit_buf_ref();
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 195) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 196)
ffb5d9cf650f5 drivers/tty/tty_audit.c (Lee Jones 2020-11-04 19:35:28 +0000 197) /*
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 198) * tty_audit_add_data - Add data for TTY auditing.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 199) *
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 200) * Audit @data of @size from @tty, if necessary.
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 201) */
309426ae69cdf drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:27 -0800 202) void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 203) {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 204) struct tty_audit_buf *buf;
309426ae69cdf drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:27 -0800 205) unsigned int icanon = !!L_ICANON(tty);
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 206) unsigned int audit_tty;
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 207) dev_t dev;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 208)
f17c3662745ea drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:37 -0800 209) audit_tty = READ_ONCE(current->signal->audit_tty);
f17c3662745ea drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:37 -0800 210) if (~audit_tty & AUDIT_TTY_ENABLE)
f17c3662745ea drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:37 -0800 211) return;
f17c3662745ea drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:37 -0800 212)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 213) if (unlikely(size == 0))
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 214) return;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 215)
d7c0ba40ebb32 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:25 -0800 216) if (tty->driver->type == TTY_DRIVER_TYPE_PTY
d7c0ba40ebb32 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:25 -0800 217) && tty->driver->subtype == PTY_TYPE_MASTER)
d7c0ba40ebb32 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:25 -0800 218) return;
d7c0ba40ebb32 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:25 -0800 219)
2e28d38ae1d9c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:33 -0800 220) if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
46e959ea2969c drivers/tty/tty_audit.c (Richard Guy Briggs 2013-05-03 14:03:50 -0400 221) return;
46e959ea2969c drivers/tty/tty_audit.c (Richard Guy Briggs 2013-05-03 14:03:50 -0400 222)
a75c9b091226c drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:28 -0800 223) buf = tty_audit_buf_get();
55b6314a17e8a drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:39 -0800 224) if (IS_ERR_OR_NULL(buf))
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 225) return;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 226)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 227) mutex_lock(&buf->mutex);
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 228) dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 229) if (buf->dev != dev || buf->icanon != icanon) {
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 230) tty_audit_buf_push(buf);
4d240b6442824 drivers/tty/tty_audit.c (Peter Hurley 2016-01-09 22:55:32 -0800 231) buf->dev = dev;
6c633f27ccf78 drivers/tty/tty_audit.c (Jiri Slaby 2012-10-18 22:26:37 +0200 232) buf->icanon = icanon;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 233) }
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 234) do {
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 235) size_t run;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 236)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 237) run = N_TTY_BUF_SIZE - buf->valid;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 238) if (run > size)
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 239) run = size;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 240) memcpy(buf->data + buf->valid, data, run);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 241) buf->valid += run;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 242) data += run;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 243) size -= run;
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 244) if (buf->valid == N_TTY_BUF_SIZE)
152f497b9b594 drivers/tty/tty_audit.c (Eric Paris 2013-04-19 13:56:11 -0400 245) tty_audit_buf_push(buf);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 246) } while (size != 0);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 247) mutex_unlock(&buf->mutex);
522ed7767e800 drivers/char/tty_audit.c (Miloslav Trmac 2007-07-15 23:40:56 -0700 248) }