blob: 55ba6f142883f6de16cc0460b540bbc64b932738 [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>
Miloslav Trmac522ed772007-07-15 23:40:56 -070013#include <linux/tty.h>
14
15struct tty_audit_buf {
16 atomic_t count;
17 struct mutex mutex; /* Protects all data below */
18 int major, minor; /* The TTY which the data is from */
19 unsigned icanon:1;
20 size_t valid;
21 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
22};
23
24static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
25 int icanon)
26{
27 struct tty_audit_buf *buf;
28
Alan Cox66c6cea2008-02-08 04:18:46 -080029 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
Miloslav Trmac522ed772007-07-15 23:40:56 -070030 if (!buf)
31 goto err;
32 if (PAGE_SIZE != N_TTY_BUF_SIZE)
33 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
34 else
35 buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
36 if (!buf->data)
37 goto err_buf;
38 atomic_set(&buf->count, 1);
39 mutex_init(&buf->mutex);
40 buf->major = major;
41 buf->minor = minor;
42 buf->icanon = icanon;
43 buf->valid = 0;
44 return buf;
45
46err_buf:
47 kfree(buf);
48err:
49 return NULL;
50}
51
52static void tty_audit_buf_free(struct tty_audit_buf *buf)
53{
54 WARN_ON(buf->valid != 0);
55 if (PAGE_SIZE != N_TTY_BUF_SIZE)
56 kfree(buf->data);
57 else
58 free_page((unsigned long)buf->data);
59 kfree(buf);
60}
61
62static void tty_audit_buf_put(struct tty_audit_buf *buf)
63{
64 if (atomic_dec_and_test(&buf->count))
65 tty_audit_buf_free(buf);
66}
67
Al Viro1e641742008-12-09 09:23:33 +000068static void tty_audit_log(const char *description, struct task_struct *tsk,
69 uid_t loginuid, unsigned sessionid, int major,
70 int minor, unsigned char *data, size_t size)
71{
72 struct audit_buffer *ab;
73
74 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
75 if (ab) {
76 char name[sizeof(tsk->comm)];
77 uid_t uid = task_uid(tsk);
78
79 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
80 "major=%d minor=%d comm=", description,
81 tsk->pid, uid, loginuid, sessionid,
82 major, minor);
83 get_task_comm(name, tsk);
84 audit_log_untrustedstring(ab, name);
85 audit_log_format(ab, " data=");
86 audit_log_n_hex(ab, data, size);
87 audit_log_end(ab);
88 }
89}
90
Miloslav Trmac522ed772007-07-15 23:40:56 -070091/**
92 * tty_audit_buf_push - Push buffered data out
93 *
94 * Generate an audit message from the contents of @buf, which is owned by
95 * @tsk with @loginuid. @buf->mutex must be locked.
96 */
97static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
Eric Paris4746ec52008-01-08 10:06:53 -050098 unsigned int sessionid,
Miloslav Trmac522ed772007-07-15 23:40:56 -070099 struct tty_audit_buf *buf)
100{
Miloslav Trmac522ed772007-07-15 23:40:56 -0700101 if (buf->valid == 0)
102 return;
103 if (audit_enabled == 0)
104 return;
Al Viro1e641742008-12-09 09:23:33 +0000105 tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
106 buf->data, buf->valid);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700107 buf->valid = 0;
108}
109
110/**
111 * tty_audit_buf_push_current - Push buffered data out
112 *
113 * Generate an audit message from the contents of @buf, which is owned by
114 * the current task. @buf->mutex must be locked.
115 */
116static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
117{
Eric Paris4746ec52008-01-08 10:06:53 -0500118 uid_t auid = audit_get_loginuid(current);
119 unsigned int sessionid = audit_get_sessionid(current);
120 tty_audit_buf_push(current, auid, sessionid, buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700121}
122
123/**
124 * tty_audit_exit - Handle a task exit
125 *
126 * Make sure all buffered data is written out and deallocate the buffer.
127 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
128 */
129void tty_audit_exit(void)
130{
131 struct tty_audit_buf *buf;
132
133 spin_lock_irq(&current->sighand->siglock);
134 buf = current->signal->tty_audit_buf;
135 current->signal->tty_audit_buf = NULL;
136 spin_unlock_irq(&current->sighand->siglock);
137 if (!buf)
138 return;
139
140 mutex_lock(&buf->mutex);
141 tty_audit_buf_push_current(buf);
142 mutex_unlock(&buf->mutex);
143
144 tty_audit_buf_put(buf);
145}
146
147/**
148 * tty_audit_fork - Copy TTY audit state for a new task
149 *
150 * Set up TTY audit state in @sig from current. @sig needs no locking.
151 */
152void tty_audit_fork(struct signal_struct *sig)
153{
154 spin_lock_irq(&current->sighand->siglock);
155 sig->audit_tty = current->signal->audit_tty;
156 spin_unlock_irq(&current->sighand->siglock);
157 sig->tty_audit_buf = NULL;
158}
159
160/**
Al Viro1e641742008-12-09 09:23:33 +0000161 * tty_audit_tiocsti - Log TIOCSTI
162 */
163void tty_audit_tiocsti(struct tty_struct *tty, char ch)
164{
165 struct tty_audit_buf *buf;
166 int major, minor, should_audit;
167
168 spin_lock_irq(&current->sighand->siglock);
169 should_audit = current->signal->audit_tty;
170 buf = current->signal->tty_audit_buf;
171 if (buf)
172 atomic_inc(&buf->count);
173 spin_unlock_irq(&current->sighand->siglock);
174
175 major = tty->driver->major;
176 minor = tty->driver->minor_start + tty->index;
177 if (buf) {
178 mutex_lock(&buf->mutex);
179 if (buf->major == major && buf->minor == minor)
180 tty_audit_buf_push_current(buf);
181 mutex_unlock(&buf->mutex);
182 tty_audit_buf_put(buf);
183 }
184
185 if (should_audit && audit_enabled) {
186 uid_t auid;
187 unsigned int sessionid;
188
189 auid = audit_get_loginuid(current);
190 sessionid = audit_get_sessionid(current);
191 tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
192 minor, &ch, 1);
193 }
194}
195
196/**
Miloslav Trmac522ed772007-07-15 23:40:56 -0700197 * tty_audit_push_task - Flush task's pending audit data
198 */
Eric Paris25323862008-04-18 10:09:25 -0400199void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
Miloslav Trmac522ed772007-07-15 23:40:56 -0700200{
201 struct tty_audit_buf *buf;
202
203 spin_lock_irq(&tsk->sighand->siglock);
204 buf = tsk->signal->tty_audit_buf;
205 if (buf)
206 atomic_inc(&buf->count);
207 spin_unlock_irq(&tsk->sighand->siglock);
208 if (!buf)
209 return;
210
211 mutex_lock(&buf->mutex);
Eric Paris4746ec52008-01-08 10:06:53 -0500212 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700213 mutex_unlock(&buf->mutex);
214
215 tty_audit_buf_put(buf);
216}
217
218/**
219 * tty_audit_buf_get - Get an audit buffer.
220 *
221 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
222 * if TTY auditing is disabled or out of memory. Otherwise, return a new
223 * reference to the buffer.
224 */
225static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
226{
227 struct tty_audit_buf *buf, *buf2;
228
229 buf = NULL;
230 buf2 = NULL;
231 spin_lock_irq(&current->sighand->siglock);
232 if (likely(!current->signal->audit_tty))
233 goto out;
234 buf = current->signal->tty_audit_buf;
235 if (buf) {
236 atomic_inc(&buf->count);
237 goto out;
238 }
239 spin_unlock_irq(&current->sighand->siglock);
240
241 buf2 = tty_audit_buf_alloc(tty->driver->major,
242 tty->driver->minor_start + tty->index,
243 tty->icanon);
244 if (buf2 == NULL) {
245 audit_log_lost("out of memory in TTY auditing");
246 return NULL;
247 }
248
249 spin_lock_irq(&current->sighand->siglock);
250 if (!current->signal->audit_tty)
251 goto out;
252 buf = current->signal->tty_audit_buf;
253 if (!buf) {
254 current->signal->tty_audit_buf = buf2;
255 buf = buf2;
256 buf2 = NULL;
257 }
258 atomic_inc(&buf->count);
259 /* Fall through */
260 out:
261 spin_unlock_irq(&current->sighand->siglock);
262 if (buf2)
263 tty_audit_buf_free(buf2);
264 return buf;
265}
266
267/**
268 * tty_audit_add_data - Add data for TTY auditing.
269 *
270 * Audit @data of @size from @tty, if necessary.
271 */
272void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
273 size_t size)
274{
275 struct tty_audit_buf *buf;
276 int major, minor;
277
278 if (unlikely(size == 0))
279 return;
280
Miloslav Trmac41126222008-04-18 13:30:14 -0700281 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
282 && tty->driver->subtype == PTY_TYPE_MASTER)
283 return;
284
Miloslav Trmac522ed772007-07-15 23:40:56 -0700285 buf = tty_audit_buf_get(tty);
286 if (!buf)
287 return;
288
289 mutex_lock(&buf->mutex);
290 major = tty->driver->major;
291 minor = tty->driver->minor_start + tty->index;
292 if (buf->major != major || buf->minor != minor
293 || buf->icanon != tty->icanon) {
294 tty_audit_buf_push_current(buf);
295 buf->major = major;
296 buf->minor = minor;
297 buf->icanon = tty->icanon;
298 }
299 do {
300 size_t run;
301
302 run = N_TTY_BUF_SIZE - buf->valid;
303 if (run > size)
304 run = size;
305 memcpy(buf->data + buf->valid, data, run);
306 buf->valid += run;
307 data += run;
308 size -= run;
309 if (buf->valid == N_TTY_BUF_SIZE)
310 tty_audit_buf_push_current(buf);
311 } while (size != 0);
312 mutex_unlock(&buf->mutex);
313 tty_audit_buf_put(buf);
314}
315
316/**
317 * tty_audit_push - Push buffered data out
318 *
319 * Make sure no audit data is pending for @tty on the current process.
320 */
321void tty_audit_push(struct tty_struct *tty)
322{
323 struct tty_audit_buf *buf;
324
325 spin_lock_irq(&current->sighand->siglock);
326 if (likely(!current->signal->audit_tty)) {
327 spin_unlock_irq(&current->sighand->siglock);
328 return;
329 }
330 buf = current->signal->tty_audit_buf;
331 if (buf)
332 atomic_inc(&buf->count);
333 spin_unlock_irq(&current->sighand->siglock);
334
335 if (buf) {
336 int major, minor;
337
338 major = tty->driver->major;
339 minor = tty->driver->minor_start + tty->index;
340 mutex_lock(&buf->mutex);
341 if (buf->major == major && buf->minor == minor)
342 tty_audit_buf_push_current(buf);
343 mutex_unlock(&buf->mutex);
344 tty_audit_buf_put(buf);
345 }
346}