1 // SPDX-License-Identifier: GPL-2.0
3 * Creating audit events from TTY input.
5 * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted
6 * material is made available to anyone wishing to use, modify, copy, or
7 * redistribute it subject to the terms and conditions of the GNU General
10 * Authors: Miloslav Trmac <mitr@redhat.com>
13 #include <linux/audit.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
17 struct tty_audit_buf {
18 struct mutex mutex; /* Protects all data below */
19 dev_t dev; /* The TTY which the data is from */
22 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
25 static struct tty_audit_buf *tty_audit_buf_ref(void)
27 struct tty_audit_buf *buf;
29 buf = current->signal->tty_audit_buf;
30 WARN_ON(buf == ERR_PTR(-ESRCH));
34 static struct tty_audit_buf *tty_audit_buf_alloc(void)
36 struct tty_audit_buf *buf;
38 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
41 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
44 mutex_init(&buf->mutex);
45 buf->dev = MKDEV(0, 0);
56 static void tty_audit_buf_free(struct tty_audit_buf *buf)
58 WARN_ON(buf->valid != 0);
63 static void tty_audit_log(const char *description, dev_t dev,
64 unsigned char *data, size_t size)
66 struct audit_buffer *ab;
67 struct task_struct *tsk = current;
68 pid_t pid = task_pid_nr(tsk);
69 uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
70 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
71 unsigned int sessionid = audit_get_sessionid(tsk);
73 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
75 char name[sizeof(tsk->comm)];
77 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
78 " minor=%d comm=", description, pid, uid,
79 loginuid, sessionid, MAJOR(dev), MINOR(dev));
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);
89 * tty_audit_buf_push - Push buffered data out
91 * Generate an audit message from the contents of @buf, which is owned by
92 * the current task. @buf->mutex must be locked.
94 static void tty_audit_buf_push(struct tty_audit_buf *buf)
98 if (audit_enabled == 0) {
102 tty_audit_log("tty", buf->dev, buf->data, buf->valid);
107 * tty_audit_exit - Handle a task exit
109 * Make sure all buffered data is written out and deallocate the buffer.
110 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
112 * The process is single-threaded at this point; no other threads share
115 void tty_audit_exit(void)
117 struct tty_audit_buf *buf;
119 buf = xchg(¤t->signal->tty_audit_buf, ERR_PTR(-ESRCH));
123 tty_audit_buf_push(buf);
124 tty_audit_buf_free(buf);
128 * tty_audit_fork - Copy TTY audit state for a new task
130 * Set up TTY audit state in @sig from current. @sig needs no locking.
132 void tty_audit_fork(struct signal_struct *sig)
134 sig->audit_tty = current->signal->audit_tty;
138 * tty_audit_tiocsti - Log TIOCSTI
140 void tty_audit_tiocsti(struct tty_struct *tty, char ch)
144 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
145 if (tty_audit_push())
149 tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
153 * tty_audit_push - Flush current's pending audit data
155 * Returns 0 if success, -EPERM if tty audit is disabled
157 int tty_audit_push(void)
159 struct tty_audit_buf *buf;
161 if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
164 buf = tty_audit_buf_ref();
165 if (!IS_ERR_OR_NULL(buf)) {
166 mutex_lock(&buf->mutex);
167 tty_audit_buf_push(buf);
168 mutex_unlock(&buf->mutex);
174 * tty_audit_buf_get - Get an audit buffer.
176 * Get an audit buffer, allocate it if necessary. Return %NULL
177 * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
178 * occurred. Otherwise, return a new reference to the buffer.
180 static struct tty_audit_buf *tty_audit_buf_get(void)
182 struct tty_audit_buf *buf;
184 buf = tty_audit_buf_ref();
188 buf = tty_audit_buf_alloc();
190 audit_log_lost("out of memory in TTY auditing");
194 /* Race to use this buffer, free it if another wins */
195 if (cmpxchg(¤t->signal->tty_audit_buf, NULL, buf) != NULL)
196 tty_audit_buf_free(buf);
197 return tty_audit_buf_ref();
201 * tty_audit_add_data - Add data for TTY auditing.
203 * Audit @data of @size from @tty, if necessary.
205 void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
207 struct tty_audit_buf *buf;
208 unsigned int icanon = !!L_ICANON(tty);
209 unsigned int audit_tty;
212 audit_tty = READ_ONCE(current->signal->audit_tty);
213 if (~audit_tty & AUDIT_TTY_ENABLE)
216 if (unlikely(size == 0))
219 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
220 && tty->driver->subtype == PTY_TYPE_MASTER)
223 if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
226 buf = tty_audit_buf_get();
227 if (IS_ERR_OR_NULL(buf))
230 mutex_lock(&buf->mutex);
231 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
232 if (buf->dev != dev || buf->icanon != icanon) {
233 tty_audit_buf_push(buf);
235 buf->icanon = icanon;
240 run = N_TTY_BUF_SIZE - buf->valid;
243 memcpy(buf->data + buf->valid, data, run);
247 if (buf->valid == N_TTY_BUF_SIZE)
248 tty_audit_buf_push(buf);
250 mutex_unlock(&buf->mutex);