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/fdtable.h>
15 #include <linux/tty.h>
17 struct tty_audit_buf {
19 struct mutex mutex; /* Protects all data below */
20 int major, minor; /* The TTY which the data is from */
23 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
26 static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
29 struct tty_audit_buf *buf;
31 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
34 if (PAGE_SIZE != N_TTY_BUF_SIZE)
35 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
37 buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
40 atomic_set(&buf->count, 1);
41 mutex_init(&buf->mutex);
54 static void tty_audit_buf_free(struct tty_audit_buf *buf)
56 WARN_ON(buf->valid != 0);
57 if (PAGE_SIZE != N_TTY_BUF_SIZE)
60 free_page((unsigned long)buf->data);
64 static void tty_audit_buf_put(struct tty_audit_buf *buf)
66 if (atomic_dec_and_test(&buf->count))
67 tty_audit_buf_free(buf);
71 * tty_audit_buf_push - Push buffered data out
73 * Generate an audit message from the contents of @buf, which is owned by
74 * @tsk with @loginuid. @buf->mutex must be locked.
76 static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
77 unsigned int sessionid,
78 struct tty_audit_buf *buf)
80 struct audit_buffer *ab;
84 if (audit_enabled == 0)
86 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
88 char name[sizeof(tsk->comm)];
89 uid_t uid = task_uid(tsk);
91 audit_log_format(ab, "tty pid=%u uid=%u auid=%u ses=%u "
92 "major=%d minor=%d comm=",
93 tsk->pid, uid, loginuid, sessionid,
94 buf->major, buf->minor);
95 get_task_comm(name, tsk);
96 audit_log_untrustedstring(ab, name);
97 audit_log_format(ab, " data=");
98 audit_log_n_hex(ab, buf->data, buf->valid);
105 * tty_audit_buf_push_current - Push buffered data out
107 * Generate an audit message from the contents of @buf, which is owned by
108 * the current task. @buf->mutex must be locked.
110 static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
112 uid_t auid = audit_get_loginuid(current);
113 unsigned int sessionid = audit_get_sessionid(current);
114 tty_audit_buf_push(current, auid, sessionid, buf);
118 * tty_audit_exit - Handle a task exit
120 * Make sure all buffered data is written out and deallocate the buffer.
121 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
123 void tty_audit_exit(void)
125 struct tty_audit_buf *buf;
127 spin_lock_irq(¤t->sighand->siglock);
128 buf = current->signal->tty_audit_buf;
129 current->signal->tty_audit_buf = NULL;
130 spin_unlock_irq(¤t->sighand->siglock);
134 mutex_lock(&buf->mutex);
135 tty_audit_buf_push_current(buf);
136 mutex_unlock(&buf->mutex);
138 tty_audit_buf_put(buf);
142 * tty_audit_fork - Copy TTY audit state for a new task
144 * Set up TTY audit state in @sig from current. @sig needs no locking.
146 void tty_audit_fork(struct signal_struct *sig)
148 spin_lock_irq(¤t->sighand->siglock);
149 sig->audit_tty = current->signal->audit_tty;
150 spin_unlock_irq(¤t->sighand->siglock);
151 sig->tty_audit_buf = NULL;
155 * tty_audit_push_task - Flush task's pending audit data
157 void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
159 struct tty_audit_buf *buf;
161 spin_lock_irq(&tsk->sighand->siglock);
162 buf = tsk->signal->tty_audit_buf;
164 atomic_inc(&buf->count);
165 spin_unlock_irq(&tsk->sighand->siglock);
169 mutex_lock(&buf->mutex);
170 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
171 mutex_unlock(&buf->mutex);
173 tty_audit_buf_put(buf);
177 * tty_audit_buf_get - Get an audit buffer.
179 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
180 * if TTY auditing is disabled or out of memory. Otherwise, return a new
181 * reference to the buffer.
183 static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
185 struct tty_audit_buf *buf, *buf2;
189 spin_lock_irq(¤t->sighand->siglock);
190 if (likely(!current->signal->audit_tty))
192 buf = current->signal->tty_audit_buf;
194 atomic_inc(&buf->count);
197 spin_unlock_irq(¤t->sighand->siglock);
199 buf2 = tty_audit_buf_alloc(tty->driver->major,
200 tty->driver->minor_start + tty->index,
203 audit_log_lost("out of memory in TTY auditing");
207 spin_lock_irq(¤t->sighand->siglock);
208 if (!current->signal->audit_tty)
210 buf = current->signal->tty_audit_buf;
212 current->signal->tty_audit_buf = buf2;
216 atomic_inc(&buf->count);
219 spin_unlock_irq(¤t->sighand->siglock);
221 tty_audit_buf_free(buf2);
226 * tty_audit_add_data - Add data for TTY auditing.
228 * Audit @data of @size from @tty, if necessary.
230 void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
233 struct tty_audit_buf *buf;
236 if (unlikely(size == 0))
239 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
240 && tty->driver->subtype == PTY_TYPE_MASTER)
243 buf = tty_audit_buf_get(tty);
247 mutex_lock(&buf->mutex);
248 major = tty->driver->major;
249 minor = tty->driver->minor_start + tty->index;
250 if (buf->major != major || buf->minor != minor
251 || buf->icanon != tty->icanon) {
252 tty_audit_buf_push_current(buf);
255 buf->icanon = tty->icanon;
260 run = N_TTY_BUF_SIZE - buf->valid;
263 memcpy(buf->data + buf->valid, data, run);
267 if (buf->valid == N_TTY_BUF_SIZE)
268 tty_audit_buf_push_current(buf);
270 mutex_unlock(&buf->mutex);
271 tty_audit_buf_put(buf);
275 * tty_audit_push - Push buffered data out
277 * Make sure no audit data is pending for @tty on the current process.
279 void tty_audit_push(struct tty_struct *tty)
281 struct tty_audit_buf *buf;
283 spin_lock_irq(¤t->sighand->siglock);
284 if (likely(!current->signal->audit_tty)) {
285 spin_unlock_irq(¤t->sighand->siglock);
288 buf = current->signal->tty_audit_buf;
290 atomic_inc(&buf->count);
291 spin_unlock_irq(¤t->sighand->siglock);
296 major = tty->driver->major;
297 minor = tty->driver->minor_start + tty->index;
298 mutex_lock(&buf->mutex);
299 if (buf->major == major && buf->minor == minor)
300 tty_audit_buf_push_current(buf);
301 mutex_unlock(&buf->mutex);
302 tty_audit_buf_put(buf);