2 * Creating audit events from TTY input.
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
9 * Authors: Miloslav Trmac <mitr@redhat.com>
12 #include <linux/audit.h>
13 #include <linux/file.h>
14 #include <linux/tty.h>
16 struct tty_audit_buf {
18 struct mutex mutex; /* Protects all data below */
19 int major, minor; /* 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_alloc(int major, int minor,
28 struct tty_audit_buf *buf;
30 buf = kmalloc(sizeof (*buf), GFP_KERNEL);
33 if (PAGE_SIZE != N_TTY_BUF_SIZE)
34 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
36 buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
39 atomic_set(&buf->count, 1);
40 mutex_init(&buf->mutex);
53 static void tty_audit_buf_free(struct tty_audit_buf *buf)
55 WARN_ON(buf->valid != 0);
56 if (PAGE_SIZE != N_TTY_BUF_SIZE)
59 free_page((unsigned long)buf->data);
63 static void tty_audit_buf_put(struct tty_audit_buf *buf)
65 if (atomic_dec_and_test(&buf->count))
66 tty_audit_buf_free(buf);
70 * tty_audit_buf_push - Push buffered data out
72 * Generate an audit message from the contents of @buf, which is owned by
73 * @tsk with @loginuid. @buf->mutex must be locked.
75 static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
76 struct tty_audit_buf *buf)
78 struct audit_buffer *ab;
82 if (audit_enabled == 0)
84 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
86 char name[sizeof(tsk->comm)];
88 audit_log_format(ab, "tty pid=%u uid=%u auid=%u major=%d "
89 "minor=%d comm=", tsk->pid, tsk->uid,
90 loginuid, buf->major, buf->minor);
91 get_task_comm(name, tsk);
92 audit_log_untrustedstring(ab, name);
93 audit_log_format(ab, " data=");
94 audit_log_n_untrustedstring(ab, buf->valid, buf->data);
101 * tty_audit_buf_push_current - Push buffered data out
103 * Generate an audit message from the contents of @buf, which is owned by
104 * the current task. @buf->mutex must be locked.
106 static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
108 tty_audit_buf_push(current, audit_get_loginuid(current->audit_context),
113 * tty_audit_exit - Handle a task exit
115 * Make sure all buffered data is written out and deallocate the buffer.
116 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
118 void tty_audit_exit(void)
120 struct tty_audit_buf *buf;
122 spin_lock_irq(¤t->sighand->siglock);
123 buf = current->signal->tty_audit_buf;
124 current->signal->tty_audit_buf = NULL;
125 spin_unlock_irq(¤t->sighand->siglock);
129 mutex_lock(&buf->mutex);
130 tty_audit_buf_push_current(buf);
131 mutex_unlock(&buf->mutex);
133 tty_audit_buf_put(buf);
137 * tty_audit_fork - Copy TTY audit state for a new task
139 * Set up TTY audit state in @sig from current. @sig needs no locking.
141 void tty_audit_fork(struct signal_struct *sig)
143 spin_lock_irq(¤t->sighand->siglock);
144 sig->audit_tty = current->signal->audit_tty;
145 spin_unlock_irq(¤t->sighand->siglock);
146 sig->tty_audit_buf = NULL;
150 * tty_audit_push_task - Flush task's pending audit data
152 void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid)
154 struct tty_audit_buf *buf;
156 spin_lock_irq(&tsk->sighand->siglock);
157 buf = tsk->signal->tty_audit_buf;
159 atomic_inc(&buf->count);
160 spin_unlock_irq(&tsk->sighand->siglock);
164 mutex_lock(&buf->mutex);
165 tty_audit_buf_push(tsk, loginuid, buf);
166 mutex_unlock(&buf->mutex);
168 tty_audit_buf_put(buf);
172 * tty_audit_buf_get - Get an audit buffer.
174 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
175 * if TTY auditing is disabled or out of memory. Otherwise, return a new
176 * reference to the buffer.
178 static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
180 struct tty_audit_buf *buf, *buf2;
184 spin_lock_irq(¤t->sighand->siglock);
185 if (likely(!current->signal->audit_tty))
187 buf = current->signal->tty_audit_buf;
189 atomic_inc(&buf->count);
192 spin_unlock_irq(¤t->sighand->siglock);
194 buf2 = tty_audit_buf_alloc(tty->driver->major,
195 tty->driver->minor_start + tty->index,
198 audit_log_lost("out of memory in TTY auditing");
202 spin_lock_irq(¤t->sighand->siglock);
203 if (!current->signal->audit_tty)
205 buf = current->signal->tty_audit_buf;
207 current->signal->tty_audit_buf = buf2;
211 atomic_inc(&buf->count);
214 spin_unlock_irq(¤t->sighand->siglock);
216 tty_audit_buf_free(buf2);
221 * tty_audit_add_data - Add data for TTY auditing.
223 * Audit @data of @size from @tty, if necessary.
225 void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
228 struct tty_audit_buf *buf;
231 if (unlikely(size == 0))
234 buf = tty_audit_buf_get(tty);
238 mutex_lock(&buf->mutex);
239 major = tty->driver->major;
240 minor = tty->driver->minor_start + tty->index;
241 if (buf->major != major || buf->minor != minor
242 || buf->icanon != tty->icanon) {
243 tty_audit_buf_push_current(buf);
246 buf->icanon = tty->icanon;
251 run = N_TTY_BUF_SIZE - buf->valid;
254 memcpy(buf->data + buf->valid, data, run);
258 if (buf->valid == N_TTY_BUF_SIZE)
259 tty_audit_buf_push_current(buf);
261 mutex_unlock(&buf->mutex);
262 tty_audit_buf_put(buf);
266 * tty_audit_push - Push buffered data out
268 * Make sure no audit data is pending for @tty on the current process.
270 void tty_audit_push(struct tty_struct *tty)
272 struct tty_audit_buf *buf;
274 spin_lock_irq(¤t->sighand->siglock);
275 if (likely(!current->signal->audit_tty)) {
276 spin_unlock_irq(¤t->sighand->siglock);
279 buf = current->signal->tty_audit_buf;
281 atomic_inc(&buf->count);
282 spin_unlock_irq(¤t->sighand->siglock);
287 major = tty->driver->major;
288 minor = tty->driver->minor_start + tty->index;
289 mutex_lock(&buf->mutex);
290 if (buf->major == major && buf->minor == minor)
291 tty_audit_buf_push_current(buf);
292 mutex_unlock(&buf->mutex);
293 tty_audit_buf_put(buf);
298 * tty_audit_opening - A TTY is being opened.
300 * As a special hack, tasks that close all their TTYs and open new ones
301 * are assumed to be system daemons (e.g. getty) and auditing is
302 * automatically disabled for them.
304 void tty_audit_opening(void)
309 spin_lock_irq(¤t->sighand->siglock);
310 if (current->signal->audit_tty == 0)
312 spin_unlock_irq(¤t->sighand->siglock);
317 if (current->files) {
322 * We don't take a ref to the file, so we must hold ->file_lock
325 spin_lock(¤t->files->file_lock);
326 fdt = files_fdtable(current->files);
327 for (i = 0; i < fdt->max_fds; i++) {
330 filp = fcheck_files(current->files, i);
331 if (filp && is_tty(filp)) {
336 spin_unlock(¤t->files->file_lock);
338 task_unlock(current);
342 spin_lock_irq(¤t->sighand->siglock);
343 current->signal->audit_tty = 0;
344 spin_unlock_irq(¤t->sighand->siglock);