blob: e30aa6bf9ff9b269db368ff8fc67609c3bdd3736 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Miloslav Trmac522ed772007-07-15 23:40:56 -07002/*
3 * Creating audit events from TTY input.
4 *
Greg Kroah-Hartmane5656d42017-11-06 18:11:55 +01005 * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
Miloslav Trmac522ed772007-07-15 23:40:56 -07006 *
7 * Authors: Miloslav Trmac <mitr@redhat.com>
8 */
9
10#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070012#include <linux/tty.h>
13
14struct tty_audit_buf {
Miloslav Trmac522ed772007-07-15 23:40:56 -070015 struct mutex mutex; /* Protects all data below */
Peter Hurley4d240b62016-01-09 22:55:32 -080016 dev_t dev; /* The TTY which the data is from */
Miloslav Trmac522ed772007-07-15 23:40:56 -070017 unsigned icanon:1;
18 size_t valid;
19 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
20};
21
Peter Hurley55b63142016-01-09 22:55:39 -080022static struct tty_audit_buf *tty_audit_buf_ref(void)
23{
24 struct tty_audit_buf *buf;
25
26 buf = current->signal->tty_audit_buf;
27 WARN_ON(buf == ERR_PTR(-ESRCH));
28 return buf;
29}
30
Peter Hurleya75c9b02016-01-09 22:55:28 -080031static struct tty_audit_buf *tty_audit_buf_alloc(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -070032{
33 struct tty_audit_buf *buf;
34
Alan Cox66c6cea2008-02-08 04:18:46 -080035 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
Miloslav Trmac522ed772007-07-15 23:40:56 -070036 if (!buf)
37 goto err;
Alan Coxc481c702009-06-11 13:04:27 +010038 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
Miloslav Trmac522ed772007-07-15 23:40:56 -070039 if (!buf->data)
40 goto err_buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -070041 mutex_init(&buf->mutex);
Peter Hurley4d240b62016-01-09 22:55:32 -080042 buf->dev = MKDEV(0, 0);
Peter Hurleya75c9b02016-01-09 22:55:28 -080043 buf->icanon = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -070044 buf->valid = 0;
45 return buf;
46
47err_buf:
48 kfree(buf);
49err:
50 return NULL;
51}
52
53static void tty_audit_buf_free(struct tty_audit_buf *buf)
54{
55 WARN_ON(buf->valid != 0);
Alan Coxc481c702009-06-11 13:04:27 +010056 kfree(buf->data);
Miloslav Trmac522ed772007-07-15 23:40:56 -070057 kfree(buf);
58}
59
Peter Hurley4d240b62016-01-09 22:55:32 -080060static void tty_audit_log(const char *description, dev_t dev,
Eric Paris152f4972013-04-19 13:56:11 -040061 unsigned char *data, size_t size)
Al Viro1e641742008-12-09 09:23:33 +000062{
63 struct audit_buffer *ab;
Eric Paris152f4972013-04-19 13:56:11 -040064 struct task_struct *tsk = current;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -050065 pid_t pid = task_pid_nr(tsk);
Eric Paris152f4972013-04-19 13:56:11 -040066 uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
67 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
Eric Paris4440e852013-11-27 17:35:17 -050068 unsigned int sessionid = audit_get_sessionid(tsk);
Al Viro1e641742008-12-09 09:23:33 +000069
70 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
71 if (ab) {
72 char name[sizeof(tsk->comm)];
Al Viro1e641742008-12-09 09:23:33 +000073
Eric Paris152f4972013-04-19 13:56:11 -040074 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -050075 " minor=%d comm=", description, pid, uid,
Peter Hurley4d240b62016-01-09 22:55:32 -080076 loginuid, sessionid, MAJOR(dev), MINOR(dev));
Al Viro1e641742008-12-09 09:23:33 +000077 get_task_comm(name, tsk);
78 audit_log_untrustedstring(ab, name);
79 audit_log_format(ab, " data=");
80 audit_log_n_hex(ab, data, size);
81 audit_log_end(ab);
82 }
83}
84
Miloslav Trmac522ed772007-07-15 23:40:56 -070085/**
86 * tty_audit_buf_push - Push buffered data out
87 *
88 * Generate an audit message from the contents of @buf, which is owned by
Eric Paris152f4972013-04-19 13:56:11 -040089 * the current task. @buf->mutex must be locked.
Miloslav Trmac522ed772007-07-15 23:40:56 -070090 */
Eric Paris152f4972013-04-19 13:56:11 -040091static void tty_audit_buf_push(struct tty_audit_buf *buf)
Miloslav Trmac522ed772007-07-15 23:40:56 -070092{
Miloslav Trmac522ed772007-07-15 23:40:56 -070093 if (buf->valid == 0)
94 return;
Xiaotian Feng00bff392011-03-03 18:08:24 +080095 if (audit_enabled == 0) {
96 buf->valid = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -070097 return;
Xiaotian Feng00bff392011-03-03 18:08:24 +080098 }
Peter Hurley4d240b62016-01-09 22:55:32 -080099 tty_audit_log("tty", buf->dev, buf->data, buf->valid);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700100 buf->valid = 0;
101}
102
103/**
Miloslav Trmac522ed772007-07-15 23:40:56 -0700104 * tty_audit_exit - Handle a task exit
105 *
106 * Make sure all buffered data is written out and deallocate the buffer.
107 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
Peter Hurley54930902016-01-09 22:55:35 -0800108 *
109 * The process is single-threaded at this point; no other threads share
110 * current->signal.
Miloslav Trmac522ed772007-07-15 23:40:56 -0700111 */
112void tty_audit_exit(void)
113{
114 struct tty_audit_buf *buf;
115
Peter Hurley55b63142016-01-09 22:55:39 -0800116 buf = xchg(&current->signal->tty_audit_buf, ERR_PTR(-ESRCH));
Miloslav Trmac522ed772007-07-15 23:40:56 -0700117 if (!buf)
118 return;
119
Eric Paris152f4972013-04-19 13:56:11 -0400120 tty_audit_buf_push(buf);
Peter Hurley54930902016-01-09 22:55:35 -0800121 tty_audit_buf_free(buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700122}
123
124/**
125 * tty_audit_fork - Copy TTY audit state for a new task
126 *
127 * Set up TTY audit state in @sig from current. @sig needs no locking.
128 */
129void tty_audit_fork(struct signal_struct *sig)
130{
Miloslav Trmac522ed772007-07-15 23:40:56 -0700131 sig->audit_tty = current->signal->audit_tty;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700132}
133
134/**
Al Viro1e641742008-12-09 09:23:33 +0000135 * tty_audit_tiocsti - Log TIOCSTI
136 */
137void tty_audit_tiocsti(struct tty_struct *tty, char ch)
138{
Peter Hurley4d240b62016-01-09 22:55:32 -0800139 dev_t dev;
Al Viro1e641742008-12-09 09:23:33 +0000140
Peter Hurley4d240b62016-01-09 22:55:32 -0800141 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
Peter Hurley82b5c932016-01-09 22:55:38 -0800142 if (tty_audit_push())
143 return;
Al Viro1e641742008-12-09 09:23:33 +0000144
Sudip Mukherjee54555912016-02-24 16:45:09 +0530145 if (audit_enabled)
Peter Hurley4d240b62016-01-09 22:55:32 -0800146 tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
Al Viro1e641742008-12-09 09:23:33 +0000147}
148
149/**
Peter Hurley37282a72016-01-09 22:55:31 -0800150 * tty_audit_push - Flush current's pending audit data
Thomas Gleixner3c80fe42009-12-09 14:19:31 +0000151 *
Peter Hurley37282a72016-01-09 22:55:31 -0800152 * Returns 0 if success, -EPERM if tty audit is disabled
Miloslav Trmac522ed772007-07-15 23:40:56 -0700153 */
Peter Hurley37282a72016-01-09 22:55:31 -0800154int tty_audit_push(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700155{
Peter Hurley2e28d382016-01-09 22:55:33 -0800156 struct tty_audit_buf *buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700157
Peter Hurley2e28d382016-01-09 22:55:33 -0800158 if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
159 return -EPERM;
160
Peter Hurley55b63142016-01-09 22:55:39 -0800161 buf = tty_audit_buf_ref();
162 if (!IS_ERR_OR_NULL(buf)) {
Peter Hurley2e28d382016-01-09 22:55:33 -0800163 mutex_lock(&buf->mutex);
164 tty_audit_buf_push(buf);
165 mutex_unlock(&buf->mutex);
Peter Hurley2e28d382016-01-09 22:55:33 -0800166 }
Thomas Gleixner3c80fe42009-12-09 14:19:31 +0000167 return 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700168}
169
170/**
171 * tty_audit_buf_get - Get an audit buffer.
172 *
Peter Hurleya75c9b02016-01-09 22:55:28 -0800173 * Get an audit buffer, allocate it if necessary. Return %NULL
Peter Hurley55b63142016-01-09 22:55:39 -0800174 * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
175 * occurred. Otherwise, return a new reference to the buffer.
Miloslav Trmac522ed772007-07-15 23:40:56 -0700176 */
Peter Hurleya75c9b02016-01-09 22:55:28 -0800177static struct tty_audit_buf *tty_audit_buf_get(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700178{
Peter Hurleyfbaa12272016-01-09 22:55:36 -0800179 struct tty_audit_buf *buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700180
Peter Hurley55b63142016-01-09 22:55:39 -0800181 buf = tty_audit_buf_ref();
Peter Hurley54930902016-01-09 22:55:35 -0800182 if (buf)
183 return buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700184
Peter Hurleyfbaa12272016-01-09 22:55:36 -0800185 buf = tty_audit_buf_alloc();
186 if (buf == NULL) {
Miloslav Trmac522ed772007-07-15 23:40:56 -0700187 audit_log_lost("out of memory in TTY auditing");
188 return NULL;
189 }
190
Peter Hurleyfbaa12272016-01-09 22:55:36 -0800191 /* Race to use this buffer, free it if another wins */
192 if (cmpxchg(&current->signal->tty_audit_buf, NULL, buf) != NULL)
193 tty_audit_buf_free(buf);
Peter Hurley55b63142016-01-09 22:55:39 -0800194 return tty_audit_buf_ref();
Miloslav Trmac522ed772007-07-15 23:40:56 -0700195}
196
197/**
198 * tty_audit_add_data - Add data for TTY auditing.
199 *
200 * Audit @data of @size from @tty, if necessary.
201 */
Peter Hurley309426a2016-01-09 22:55:27 -0800202void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700203{
204 struct tty_audit_buf *buf;
Peter Hurley309426a2016-01-09 22:55:27 -0800205 unsigned int icanon = !!L_ICANON(tty);
Peter Hurley2e28d382016-01-09 22:55:33 -0800206 unsigned int audit_tty;
Peter Hurley4d240b62016-01-09 22:55:32 -0800207 dev_t dev;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700208
Peter Hurleyf17c3662016-01-09 22:55:37 -0800209 audit_tty = READ_ONCE(current->signal->audit_tty);
210 if (~audit_tty & AUDIT_TTY_ENABLE)
211 return;
212
Miloslav Trmac522ed772007-07-15 23:40:56 -0700213 if (unlikely(size == 0))
214 return;
215
Peter Hurleyd7c0ba42016-01-09 22:55:25 -0800216 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
217 && tty->driver->subtype == PTY_TYPE_MASTER)
218 return;
219
Peter Hurley2e28d382016-01-09 22:55:33 -0800220 if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
Richard Guy Briggs46e959e2013-05-03 14:03:50 -0400221 return;
222
Peter Hurleya75c9b02016-01-09 22:55:28 -0800223 buf = tty_audit_buf_get();
Peter Hurley55b63142016-01-09 22:55:39 -0800224 if (IS_ERR_OR_NULL(buf))
Miloslav Trmac522ed772007-07-15 23:40:56 -0700225 return;
226
227 mutex_lock(&buf->mutex);
Peter Hurley4d240b62016-01-09 22:55:32 -0800228 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
229 if (buf->dev != dev || buf->icanon != icanon) {
Eric Paris152f4972013-04-19 13:56:11 -0400230 tty_audit_buf_push(buf);
Peter Hurley4d240b62016-01-09 22:55:32 -0800231 buf->dev = dev;
Jiri Slaby6c633f22012-10-18 22:26:37 +0200232 buf->icanon = icanon;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700233 }
234 do {
235 size_t run;
236
237 run = N_TTY_BUF_SIZE - buf->valid;
238 if (run > size)
239 run = size;
240 memcpy(buf->data + buf->valid, data, run);
241 buf->valid += run;
242 data += run;
243 size -= run;
244 if (buf->valid == N_TTY_BUF_SIZE)
Eric Paris152f4972013-04-19 13:56:11 -0400245 tty_audit_buf_push(buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700246 } while (size != 0);
247 mutex_unlock(&buf->mutex);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700248}