]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/tty/tty_audit.c
tty: audit: Handle tty audit enable atomically
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / tty_audit.c
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>
13 #include <linux/slab.h>
14 #include <linux/tty.h>
15
16 struct tty_audit_buf {
17 atomic_t count;
18 struct mutex mutex; /* Protects all data below */
19 dev_t dev; /* 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(void)
26 {
27 struct tty_audit_buf *buf;
28
29 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
30 if (!buf)
31 goto err;
32 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
33 if (!buf->data)
34 goto err_buf;
35 atomic_set(&buf->count, 1);
36 mutex_init(&buf->mutex);
37 buf->dev = MKDEV(0, 0);
38 buf->icanon = 0;
39 buf->valid = 0;
40 return buf;
41
42 err_buf:
43 kfree(buf);
44 err:
45 return NULL;
46 }
47
48 static void tty_audit_buf_free(struct tty_audit_buf *buf)
49 {
50 WARN_ON(buf->valid != 0);
51 kfree(buf->data);
52 kfree(buf);
53 }
54
55 static void tty_audit_buf_put(struct tty_audit_buf *buf)
56 {
57 if (atomic_dec_and_test(&buf->count))
58 tty_audit_buf_free(buf);
59 }
60
61 static void tty_audit_log(const char *description, dev_t dev,
62 unsigned char *data, size_t size)
63 {
64 struct audit_buffer *ab;
65 struct task_struct *tsk = current;
66 pid_t pid = task_pid_nr(tsk);
67 uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
68 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
69 unsigned int sessionid = audit_get_sessionid(tsk);
70
71 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
72 if (ab) {
73 char name[sizeof(tsk->comm)];
74
75 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
76 " minor=%d comm=", description, pid, uid,
77 loginuid, sessionid, MAJOR(dev), MINOR(dev));
78 get_task_comm(name, tsk);
79 audit_log_untrustedstring(ab, name);
80 audit_log_format(ab, " data=");
81 audit_log_n_hex(ab, data, size);
82 audit_log_end(ab);
83 }
84 }
85
86 /**
87 * tty_audit_buf_push - Push buffered data out
88 *
89 * Generate an audit message from the contents of @buf, which is owned by
90 * the current task. @buf->mutex must be locked.
91 */
92 static void tty_audit_buf_push(struct tty_audit_buf *buf)
93 {
94 if (buf->valid == 0)
95 return;
96 if (audit_enabled == 0) {
97 buf->valid = 0;
98 return;
99 }
100 tty_audit_log("tty", buf->dev, buf->data, buf->valid);
101 buf->valid = 0;
102 }
103
104 /**
105 * tty_audit_exit - Handle a task exit
106 *
107 * Make sure all buffered data is written out and deallocate the buffer.
108 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
109 */
110 void tty_audit_exit(void)
111 {
112 struct tty_audit_buf *buf;
113
114 buf = current->signal->tty_audit_buf;
115 current->signal->tty_audit_buf = NULL;
116 if (!buf)
117 return;
118
119 mutex_lock(&buf->mutex);
120 tty_audit_buf_push(buf);
121 mutex_unlock(&buf->mutex);
122
123 tty_audit_buf_put(buf);
124 }
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 */
131 void tty_audit_fork(struct signal_struct *sig)
132 {
133 sig->audit_tty = current->signal->audit_tty;
134 }
135
136 /**
137 * tty_audit_tiocsti - Log TIOCSTI
138 */
139 void tty_audit_tiocsti(struct tty_struct *tty, char ch)
140 {
141 struct tty_audit_buf *buf;
142 dev_t dev;
143 unsigned long flags;
144
145 spin_lock_irqsave(&current->sighand->siglock, flags);
146 buf = current->signal->tty_audit_buf;
147 if (buf)
148 atomic_inc(&buf->count);
149 spin_unlock_irqrestore(&current->sighand->siglock, flags);
150
151 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
152 if (buf) {
153 mutex_lock(&buf->mutex);
154 if (buf->dev == dev)
155 tty_audit_buf_push(buf);
156 mutex_unlock(&buf->mutex);
157 tty_audit_buf_put(buf);
158 }
159
160 if (audit_enabled && (current->signal->audit_tty & AUDIT_TTY_ENABLE)) {
161 kuid_t auid;
162 unsigned int sessionid;
163
164 auid = audit_get_loginuid(current);
165 sessionid = audit_get_sessionid(current);
166 tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
167 }
168 }
169
170 /**
171 * tty_audit_push - Flush current's pending audit data
172 *
173 * Returns 0 if success, -EPERM if tty audit is disabled
174 */
175 int tty_audit_push(void)
176 {
177 struct tty_audit_buf *buf;
178 unsigned long flags;
179
180 if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
181 return -EPERM;
182
183 spin_lock_irqsave(&current->sighand->siglock, flags);
184 buf = current->signal->tty_audit_buf;
185 if (buf)
186 atomic_inc(&buf->count);
187 spin_unlock_irqrestore(&current->sighand->siglock, flags);
188
189 if (buf) {
190 mutex_lock(&buf->mutex);
191 tty_audit_buf_push(buf);
192 mutex_unlock(&buf->mutex);
193
194 tty_audit_buf_put(buf);
195 }
196 return 0;
197 }
198
199 /**
200 * tty_audit_buf_get - Get an audit buffer.
201 *
202 * Get an audit buffer, allocate it if necessary. Return %NULL
203 * if TTY auditing is disabled or out of memory. Otherwise, return a new
204 * reference to the buffer.
205 */
206 static struct tty_audit_buf *tty_audit_buf_get(void)
207 {
208 struct tty_audit_buf *buf, *buf2;
209 unsigned long flags;
210
211 buf = NULL;
212 buf2 = NULL;
213 spin_lock_irqsave(&current->sighand->siglock, flags);
214 buf = current->signal->tty_audit_buf;
215 if (buf) {
216 atomic_inc(&buf->count);
217 goto out;
218 }
219 spin_unlock_irqrestore(&current->sighand->siglock, flags);
220
221 buf2 = tty_audit_buf_alloc();
222 if (buf2 == NULL) {
223 audit_log_lost("out of memory in TTY auditing");
224 return NULL;
225 }
226
227 if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
228 goto out;
229
230 spin_lock_irqsave(&current->sighand->siglock, flags);
231 buf = current->signal->tty_audit_buf;
232 if (!buf) {
233 current->signal->tty_audit_buf = buf2;
234 buf = buf2;
235 buf2 = NULL;
236 }
237 atomic_inc(&buf->count);
238 /* Fall through */
239 out:
240 spin_unlock_irqrestore(&current->sighand->siglock, flags);
241 if (buf2)
242 tty_audit_buf_free(buf2);
243 return buf;
244 }
245
246 /**
247 * tty_audit_add_data - Add data for TTY auditing.
248 *
249 * Audit @data of @size from @tty, if necessary.
250 */
251 void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
252 {
253 struct tty_audit_buf *buf;
254 unsigned int icanon = !!L_ICANON(tty);
255 unsigned int audit_tty;
256 dev_t dev;
257
258 if (unlikely(size == 0))
259 return;
260
261 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
262 && tty->driver->subtype == PTY_TYPE_MASTER)
263 return;
264
265 audit_tty = READ_ONCE(current->signal->audit_tty);
266 if (~audit_tty & AUDIT_TTY_ENABLE)
267 return;
268 if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
269 return;
270
271 buf = tty_audit_buf_get();
272 if (!buf)
273 return;
274
275 mutex_lock(&buf->mutex);
276 dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
277 if (buf->dev != dev || buf->icanon != icanon) {
278 tty_audit_buf_push(buf);
279 buf->dev = dev;
280 buf->icanon = icanon;
281 }
282 do {
283 size_t run;
284
285 run = N_TTY_BUF_SIZE - buf->valid;
286 if (run > size)
287 run = size;
288 memcpy(buf->data + buf->valid, data, run);
289 buf->valid += run;
290 data += run;
291 size -= run;
292 if (buf->valid == N_TTY_BUF_SIZE)
293 tty_audit_buf_push(buf);
294 } while (size != 0);
295 mutex_unlock(&buf->mutex);
296 tty_audit_buf_put(buf);
297 }