initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / char / tty_audit.c
blobdd4691a7631b64ccd5c3931e05b79953dbe60484
1 /*
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
7 * Public License v.2.
9 * Authors: Miloslav Trmac <mitr@redhat.com>
12 #include <linux/audit.h>
13 #include <linux/tty.h>
15 struct 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 */
24 static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
25 int icanon)
27 struct tty_audit_buf *buf;
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->major = major;
38 buf->minor = minor;
39 buf->icanon = icanon;
40 buf->valid = 0;
41 return buf;
43 err_buf:
44 kfree(buf);
45 err:
46 return NULL;
49 static void tty_audit_buf_free(struct tty_audit_buf *buf)
51 WARN_ON(buf->valid != 0);
52 kfree(buf->data);
53 kfree(buf);
56 static void tty_audit_buf_put(struct tty_audit_buf *buf)
58 if (atomic_dec_and_test(&buf->count))
59 tty_audit_buf_free(buf);
62 static void tty_audit_log(const char *description, struct task_struct *tsk,
63 uid_t loginuid, unsigned sessionid, int major,
64 int minor, unsigned char *data, size_t size)
66 struct audit_buffer *ab;
68 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
69 if (ab) {
70 char name[sizeof(tsk->comm)];
71 uid_t uid = task_uid(tsk);
73 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
74 "major=%d minor=%d comm=", description,
75 tsk->pid, uid, loginuid, sessionid,
76 major, minor);
77 get_task_comm(name, tsk);
78 audit_log_untrustedstring(ab, name);
79 audit_log_format(ab, " data=");
80 audit_log_n_hex(ab, data, size);
81 audit_log_end(ab);
85 /**
86 * tty_audit_buf_push - Push buffered data out
88 * Generate an audit message from the contents of @buf, which is owned by
89 * @tsk with @loginuid. @buf->mutex must be locked.
91 static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
92 unsigned int sessionid,
93 struct tty_audit_buf *buf)
95 if (buf->valid == 0)
96 return;
97 if (audit_enabled == 0) {
98 buf->valid = 0;
99 return;
101 tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
102 buf->data, buf->valid);
103 buf->valid = 0;
107 * tty_audit_buf_push_current - Push buffered data out
109 * Generate an audit message from the contents of @buf, which is owned by
110 * the current task. @buf->mutex must be locked.
112 static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
114 uid_t auid = audit_get_loginuid(current);
115 unsigned int sessionid = audit_get_sessionid(current);
116 tty_audit_buf_push(current, auid, sessionid, buf);
120 * tty_audit_exit - Handle a task exit
122 * Make sure all buffered data is written out and deallocate the buffer.
123 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
125 void tty_audit_exit(void)
127 struct tty_audit_buf *buf;
129 spin_lock_irq(&current->sighand->siglock);
130 buf = current->signal->tty_audit_buf;
131 current->signal->tty_audit_buf = NULL;
132 spin_unlock_irq(&current->sighand->siglock);
133 if (!buf)
134 return;
136 mutex_lock(&buf->mutex);
137 tty_audit_buf_push_current(buf);
138 mutex_unlock(&buf->mutex);
140 tty_audit_buf_put(buf);
144 * tty_audit_fork - Copy TTY audit state for a new task
146 * Set up TTY audit state in @sig from current. @sig needs no locking.
148 void tty_audit_fork(struct signal_struct *sig)
150 spin_lock_irq(&current->sighand->siglock);
151 sig->audit_tty = current->signal->audit_tty;
152 spin_unlock_irq(&current->sighand->siglock);
153 sig->tty_audit_buf = NULL;
157 * tty_audit_tiocsti - Log TIOCSTI
159 void tty_audit_tiocsti(struct tty_struct *tty, char ch)
161 struct tty_audit_buf *buf;
162 int major, minor, should_audit;
164 spin_lock_irq(&current->sighand->siglock);
165 should_audit = current->signal->audit_tty;
166 buf = current->signal->tty_audit_buf;
167 if (buf)
168 atomic_inc(&buf->count);
169 spin_unlock_irq(&current->sighand->siglock);
171 major = tty->driver->major;
172 minor = tty->driver->minor_start + tty->index;
173 if (buf) {
174 mutex_lock(&buf->mutex);
175 if (buf->major == major && buf->minor == minor)
176 tty_audit_buf_push_current(buf);
177 mutex_unlock(&buf->mutex);
178 tty_audit_buf_put(buf);
181 if (should_audit && audit_enabled) {
182 uid_t auid;
183 unsigned int sessionid;
185 auid = audit_get_loginuid(current);
186 sessionid = audit_get_sessionid(current);
187 tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
188 minor, &ch, 1);
193 * tty_audit_push_task - Flush task's pending audit data
195 void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
197 struct tty_audit_buf *buf;
199 spin_lock_irq(&tsk->sighand->siglock);
200 buf = tsk->signal->tty_audit_buf;
201 if (buf)
202 atomic_inc(&buf->count);
203 spin_unlock_irq(&tsk->sighand->siglock);
204 if (!buf)
205 return;
207 mutex_lock(&buf->mutex);
208 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
209 mutex_unlock(&buf->mutex);
211 tty_audit_buf_put(buf);
215 * tty_audit_buf_get - Get an audit buffer.
217 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
218 * if TTY auditing is disabled or out of memory. Otherwise, return a new
219 * reference to the buffer.
221 static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
223 struct tty_audit_buf *buf, *buf2;
225 buf = NULL;
226 buf2 = NULL;
227 spin_lock_irq(&current->sighand->siglock);
228 if (likely(!current->signal->audit_tty))
229 goto out;
230 buf = current->signal->tty_audit_buf;
231 if (buf) {
232 atomic_inc(&buf->count);
233 goto out;
235 spin_unlock_irq(&current->sighand->siglock);
237 buf2 = tty_audit_buf_alloc(tty->driver->major,
238 tty->driver->minor_start + tty->index,
239 tty->icanon);
240 if (buf2 == NULL) {
241 audit_log_lost("out of memory in TTY auditing");
242 return NULL;
245 spin_lock_irq(&current->sighand->siglock);
246 if (!current->signal->audit_tty)
247 goto out;
248 buf = current->signal->tty_audit_buf;
249 if (!buf) {
250 current->signal->tty_audit_buf = buf2;
251 buf = buf2;
252 buf2 = NULL;
254 atomic_inc(&buf->count);
255 /* Fall through */
256 out:
257 spin_unlock_irq(&current->sighand->siglock);
258 if (buf2)
259 tty_audit_buf_free(buf2);
260 return buf;
264 * tty_audit_add_data - Add data for TTY auditing.
266 * Audit @data of @size from @tty, if necessary.
268 void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
269 size_t size)
271 struct tty_audit_buf *buf;
272 int major, minor;
274 if (unlikely(size == 0))
275 return;
277 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
278 && tty->driver->subtype == PTY_TYPE_MASTER)
279 return;
281 buf = tty_audit_buf_get(tty);
282 if (!buf)
283 return;
285 mutex_lock(&buf->mutex);
286 major = tty->driver->major;
287 minor = tty->driver->minor_start + tty->index;
288 if (buf->major != major || buf->minor != minor
289 || buf->icanon != tty->icanon) {
290 tty_audit_buf_push_current(buf);
291 buf->major = major;
292 buf->minor = minor;
293 buf->icanon = tty->icanon;
295 do {
296 size_t run;
298 run = N_TTY_BUF_SIZE - buf->valid;
299 if (run > size)
300 run = size;
301 memcpy(buf->data + buf->valid, data, run);
302 buf->valid += run;
303 data += run;
304 size -= run;
305 if (buf->valid == N_TTY_BUF_SIZE)
306 tty_audit_buf_push_current(buf);
307 } while (size != 0);
308 mutex_unlock(&buf->mutex);
309 tty_audit_buf_put(buf);
313 * tty_audit_push - Push buffered data out
315 * Make sure no audit data is pending for @tty on the current process.
317 void tty_audit_push(struct tty_struct *tty)
319 struct tty_audit_buf *buf;
321 spin_lock_irq(&current->sighand->siglock);
322 if (likely(!current->signal->audit_tty)) {
323 spin_unlock_irq(&current->sighand->siglock);
324 return;
326 buf = current->signal->tty_audit_buf;
327 if (buf)
328 atomic_inc(&buf->count);
329 spin_unlock_irq(&current->sighand->siglock);
331 if (buf) {
332 int major, minor;
334 major = tty->driver->major;
335 minor = tty->driver->minor_start + tty->index;
336 mutex_lock(&buf->mutex);
337 if (buf->major == major && buf->minor == minor)
338 tty_audit_buf_push_current(buf);
339 mutex_unlock(&buf->mutex);
340 tty_audit_buf_put(buf);