blob: df2d735338e216d0a398c50a7e4202afd21417e6 [file] [log] [blame]
Miloslav Trmac522ed772007-07-15 23:40:56 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070014#include <linux/tty.h>
15
16struct tty_audit_buf {
Miloslav Trmac522ed772007-07-15 23:40:56 -070017 struct mutex mutex; /* Protects all data below */
Peter Hurley4d240b62016-01-09 22:55:32 -080018 dev_t dev; /* The TTY which the data is from */
Miloslav Trmac522ed772007-07-15 23:40:56 -070019 unsigned icanon:1;
20 size_t valid;
21 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
22};
23
Peter Hurley55b63142016-01-09 22:55:39 -080024static struct tty_audit_buf *tty_audit_buf_ref(void)
25{
26 struct tty_audit_buf *buf;
27
28 buf = current->signal->tty_audit_buf;
29 WARN_ON(buf == ERR_PTR(-ESRCH));
30 return buf;
31}
32
Peter Hurleya75c9b02016-01-09 22:55:28 -080033static struct tty_audit_buf *tty_audit_buf_alloc(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -070034{
35 struct tty_audit_buf *buf;
36
Alan Cox66c6cea2008-02-08 04:18:46 -080037 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
Miloslav Trmac522ed772007-07-15 23:40:56 -070038 if (!buf)
39 goto err;
Alan Coxc481c702009-06-11 13:04:27 +010040 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
Miloslav Trmac522ed772007-07-15 23:40:56 -070041 if (!buf->data)
42 goto err_buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -070043 mutex_init(&buf->mutex);
Peter Hurley4d240b62016-01-09 22:55:32 -080044 buf->dev = MKDEV(0, 0);
Peter Hurleya75c9b02016-01-09 22:55:28 -080045 buf->icanon = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -070046 buf->valid = 0;
47 return buf;
48
49err_buf:
50 kfree(buf);
51err:
52 return NULL;
53}
54
55static void tty_audit_buf_free(struct tty_audit_buf *buf)
56{
57 WARN_ON(buf->valid != 0);
Alan Coxc481c702009-06-11 13:04:27 +010058 kfree(buf->data);
Miloslav Trmac522ed772007-07-15 23:40:56 -070059 kfree(buf);
60}
61
Peter Hurley4d240b62016-01-09 22:55:32 -080062static void tty_audit_log(const char *description, dev_t dev,
Eric Paris152f4972013-04-19 13:56:11 -040063 unsigned char *data, size_t size)
Al Viro1e641742008-12-09 09:23:33 +000064{
65 struct audit_buffer *ab;
Eric Paris152f4972013-04-19 13:56:11 -040066 struct task_struct *tsk = current;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -050067 pid_t pid = task_pid_nr(tsk);
Eric Paris152f4972013-04-19 13:56:11 -040068 uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
69 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
Eric Paris4440e852013-11-27 17:35:17 -050070 unsigned int sessionid = audit_get_sessionid(tsk);
Al Viro1e641742008-12-09 09:23:33 +000071
72 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
73 if (ab) {
74 char name[sizeof(tsk->comm)];
Al Viro1e641742008-12-09 09:23:33 +000075
Eric Paris152f4972013-04-19 13:56:11 -040076 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -050077 " minor=%d comm=", description, pid, uid,
Peter Hurley4d240b62016-01-09 22:55:32 -080078 loginuid, sessionid, MAJOR(dev), MINOR(dev));
Al Viro1e641742008-12-09 09:23:33 +000079 get_task_comm(name, tsk);
80 audit_log_untrustedstring(ab, name);
81 audit_log_format(ab, " data=");
82 audit_log_n_hex(ab, data, size);
83 audit_log_end(ab);
84 }
85}
86
Miloslav Trmac522ed772007-07-15 23:40:56 -070087/**
88 * tty_audit_buf_push - Push buffered data out
89 *
90 * Generate an audit message from the contents of @buf, which is owned by
Eric Paris152f4972013-04-19 13:56:11 -040091 * the current task. @buf->mutex must be locked.
Miloslav Trmac522ed772007-07-15 23:40:56 -070092 */
Eric Paris152f4972013-04-19 13:56:11 -040093static void tty_audit_buf_push(struct tty_audit_buf *buf)
Miloslav Trmac522ed772007-07-15 23:40:56 -070094{
Miloslav Trmac522ed772007-07-15 23:40:56 -070095 if (buf->valid == 0)
96 return;
Xiaotian Feng00bff392011-03-03 18:08:24 +080097 if (audit_enabled == 0) {
98 buf->valid = 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -070099 return;
Xiaotian Feng00bff392011-03-03 18:08:24 +0800100 }
Peter Hurley4d240b62016-01-09 22:55:32 -0800101 tty_audit_log("tty", buf->dev, buf->data, buf->valid);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700102 buf->valid = 0;
103}
104
105/**
Miloslav Trmac522ed772007-07-15 23:40:56 -0700106 * tty_audit_exit - Handle a task exit
107 *
108 * Make sure all buffered data is written out and deallocate the buffer.
109 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
Peter Hurley54930902016-01-09 22:55:35 -0800110 *
111 * The process is single-threaded at this point; no other threads share
112 * current->signal.
Miloslav Trmac522ed772007-07-15 23:40:56 -0700113 */
114void tty_audit_exit(void)
115{
116 struct tty_audit_buf *buf;
117
Peter Hurley55b63142016-01-09 22:55:39 -0800118 buf = xchg(&current->signal->tty_audit_buf, ERR_PTR(-ESRCH));
Miloslav Trmac522ed772007-07-15 23:40:56 -0700119 if (!buf)
120 return;
121
Eric Paris152f4972013-04-19 13:56:11 -0400122 tty_audit_buf_push(buf);
Peter Hurley54930902016-01-09 22:55:35 -0800123 tty_audit_buf_free(buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700124}
125
126/**
127 * tty_audit_fork - Copy TTY audit state for a new task
128 *
129 * Set up TTY audit state in @sig from current. @sig needs no locking.
130 */
131void tty_audit_fork(struct signal_struct *sig)
132{
Miloslav Trmac522ed772007-07-15 23:40:56 -0700133 sig->audit_tty = current->signal->audit_tty;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700134}
135
136/**
Al Viro1e641742008-12-09 09:23:33 +0000137 * tty_audit_tiocsti - Log TIOCSTI
138 */
139void tty_audit_tiocsti(struct tty_struct *tty, char ch)
140{
Peter Hurley4d240b62016-01-09 22:55:32 -0800141 dev_t dev;
Al Viro1e641742008-12-09 09:23:33 +0000142
Peter Hurley4d240b62016-01-09 22:55:32 -0800143 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
Peter Hurley82b5c932016-01-09 22:55:38 -0800144 if (tty_audit_push())
145 return;
Al Viro1e641742008-12-09 09:23:33 +0000146
Sudip Mukherjee54555912016-02-24 16:45:09 +0530147 if (audit_enabled)
Peter Hurley4d240b62016-01-09 22:55:32 -0800148 tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
Al Viro1e641742008-12-09 09:23:33 +0000149}
150
151/**
Peter Hurley37282a72016-01-09 22:55:31 -0800152 * tty_audit_push - Flush current's pending audit data
Thomas Gleixner3c80fe42009-12-09 14:19:31 +0000153 *
Peter Hurley37282a72016-01-09 22:55:31 -0800154 * Returns 0 if success, -EPERM if tty audit is disabled
Miloslav Trmac522ed772007-07-15 23:40:56 -0700155 */
Peter Hurley37282a72016-01-09 22:55:31 -0800156int tty_audit_push(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700157{
Peter Hurley2e28d382016-01-09 22:55:33 -0800158 struct tty_audit_buf *buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700159
Peter Hurley2e28d382016-01-09 22:55:33 -0800160 if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
161 return -EPERM;
162
Peter Hurley55b63142016-01-09 22:55:39 -0800163 buf = tty_audit_buf_ref();
164 if (!IS_ERR_OR_NULL(buf)) {
Peter Hurley2e28d382016-01-09 22:55:33 -0800165 mutex_lock(&buf->mutex);
166 tty_audit_buf_push(buf);
167 mutex_unlock(&buf->mutex);
Peter Hurley2e28d382016-01-09 22:55:33 -0800168 }
Thomas Gleixner3c80fe42009-12-09 14:19:31 +0000169 return 0;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700170}
171
172/**
173 * tty_audit_buf_get - Get an audit buffer.
174 *
Peter Hurleya75c9b02016-01-09 22:55:28 -0800175 * Get an audit buffer, allocate it if necessary. Return %NULL
Peter Hurley55b63142016-01-09 22:55:39 -0800176 * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
177 * occurred. Otherwise, return a new reference to the buffer.
Miloslav Trmac522ed772007-07-15 23:40:56 -0700178 */
Peter Hurleya75c9b02016-01-09 22:55:28 -0800179static struct tty_audit_buf *tty_audit_buf_get(void)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700180{
Peter Hurleyfbaa1222016-01-09 22:55:36 -0800181 struct tty_audit_buf *buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700182
Peter Hurley55b63142016-01-09 22:55:39 -0800183 buf = tty_audit_buf_ref();
Peter Hurley54930902016-01-09 22:55:35 -0800184 if (buf)
185 return buf;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700186
Peter Hurleyfbaa1222016-01-09 22:55:36 -0800187 buf = tty_audit_buf_alloc();
188 if (buf == NULL) {
Miloslav Trmac522ed772007-07-15 23:40:56 -0700189 audit_log_lost("out of memory in TTY auditing");
190 return NULL;
191 }
192
Peter Hurleyfbaa1222016-01-09 22:55:36 -0800193 /* Race to use this buffer, free it if another wins */
194 if (cmpxchg(&current->signal->tty_audit_buf, NULL, buf) != NULL)
195 tty_audit_buf_free(buf);
Peter Hurley55b63142016-01-09 22:55:39 -0800196 return tty_audit_buf_ref();
Miloslav Trmac522ed772007-07-15 23:40:56 -0700197}
198
199/**
200 * tty_audit_add_data - Add data for TTY auditing.
201 *
202 * Audit @data of @size from @tty, if necessary.
203 */
Peter Hurley309426a2016-01-09 22:55:27 -0800204void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700205{
206 struct tty_audit_buf *buf;
Peter Hurley309426a2016-01-09 22:55:27 -0800207 unsigned int icanon = !!L_ICANON(tty);
Peter Hurley2e28d382016-01-09 22:55:33 -0800208 unsigned int audit_tty;
Peter Hurley4d240b62016-01-09 22:55:32 -0800209 dev_t dev;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700210
Peter Hurleyf17c3662016-01-09 22:55:37 -0800211 audit_tty = READ_ONCE(current->signal->audit_tty);
212 if (~audit_tty & AUDIT_TTY_ENABLE)
213 return;
214
Miloslav Trmac522ed772007-07-15 23:40:56 -0700215 if (unlikely(size == 0))
216 return;
217
Peter Hurleyd7c0ba42016-01-09 22:55:25 -0800218 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
219 && tty->driver->subtype == PTY_TYPE_MASTER)
220 return;
221
Peter Hurley2e28d382016-01-09 22:55:33 -0800222 if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
Richard Guy Briggs46e959e2013-05-03 14:03:50 -0400223 return;
224
Peter Hurleya75c9b02016-01-09 22:55:28 -0800225 buf = tty_audit_buf_get();
Peter Hurley55b63142016-01-09 22:55:39 -0800226 if (IS_ERR_OR_NULL(buf))
Miloslav Trmac522ed772007-07-15 23:40:56 -0700227 return;
228
229 mutex_lock(&buf->mutex);
Peter Hurley4d240b62016-01-09 22:55:32 -0800230 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
231 if (buf->dev != dev || buf->icanon != icanon) {
Eric Paris152f4972013-04-19 13:56:11 -0400232 tty_audit_buf_push(buf);
Peter Hurley4d240b62016-01-09 22:55:32 -0800233 buf->dev = dev;
Jiri Slaby6c633f22012-10-18 22:26:37 +0200234 buf->icanon = icanon;
Miloslav Trmac522ed772007-07-15 23:40:56 -0700235 }
236 do {
237 size_t run;
238
239 run = N_TTY_BUF_SIZE - buf->valid;
240 if (run > size)
241 run = size;
242 memcpy(buf->data + buf->valid, data, run);
243 buf->valid += run;
244 data += run;
245 size -= run;
246 if (buf->valid == N_TTY_BUF_SIZE)
Eric Paris152f4972013-04-19 13:56:11 -0400247 tty_audit_buf_push(buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700248 } while (size != 0);
249 mutex_unlock(&buf->mutex);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700250}