kcalloc: remove runtime division
[linux-2.6/mini2440.git] / drivers / char / tty_audit.c
blob3582f43345a8c1d40397d2284dd2bc11d77a9fd4
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/file.h>
14 #include <linux/fdtable.h>
15 #include <linux/tty.h>
17 struct tty_audit_buf {
18 atomic_t count;
19 struct mutex mutex; /* Protects all data below */
20 int major, minor; /* The TTY which the data is from */
21 unsigned icanon:1;
22 size_t valid;
23 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
26 static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
27 int icanon)
29 struct tty_audit_buf *buf;
31 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
32 if (!buf)
33 goto err;
34 if (PAGE_SIZE != N_TTY_BUF_SIZE)
35 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
36 else
37 buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
38 if (!buf->data)
39 goto err_buf;
40 atomic_set(&buf->count, 1);
41 mutex_init(&buf->mutex);
42 buf->major = major;
43 buf->minor = minor;
44 buf->icanon = icanon;
45 buf->valid = 0;
46 return buf;
48 err_buf:
49 kfree(buf);
50 err:
51 return NULL;
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)
58 kfree(buf->data);
59 else
60 free_page((unsigned long)buf->data);
61 kfree(buf);
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);
70 /**
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;
82 if (buf->valid == 0)
83 return;
84 if (audit_enabled == 0)
85 return;
86 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
87 if (ab) {
88 char name[sizeof(tsk->comm)];
90 audit_log_format(ab, "tty pid=%u uid=%u auid=%u ses=%u "
91 "major=%d minor=%d comm=", tsk->pid, tsk->uid,
92 loginuid, sessionid, buf->major, buf->minor);
93 get_task_comm(name, tsk);
94 audit_log_untrustedstring(ab, name);
95 audit_log_format(ab, " data=");
96 audit_log_n_untrustedstring(ab, buf->data, buf->valid);
97 audit_log_end(ab);
99 buf->valid = 0;
103 * tty_audit_buf_push_current - Push buffered data out
105 * Generate an audit message from the contents of @buf, which is owned by
106 * the current task. @buf->mutex must be locked.
108 static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
110 uid_t auid = audit_get_loginuid(current);
111 unsigned int sessionid = audit_get_sessionid(current);
112 tty_audit_buf_push(current, auid, sessionid, buf);
116 * tty_audit_exit - Handle a task exit
118 * Make sure all buffered data is written out and deallocate the buffer.
119 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
121 void tty_audit_exit(void)
123 struct tty_audit_buf *buf;
125 spin_lock_irq(&current->sighand->siglock);
126 buf = current->signal->tty_audit_buf;
127 current->signal->tty_audit_buf = NULL;
128 spin_unlock_irq(&current->sighand->siglock);
129 if (!buf)
130 return;
132 mutex_lock(&buf->mutex);
133 tty_audit_buf_push_current(buf);
134 mutex_unlock(&buf->mutex);
136 tty_audit_buf_put(buf);
140 * tty_audit_fork - Copy TTY audit state for a new task
142 * Set up TTY audit state in @sig from current. @sig needs no locking.
144 void tty_audit_fork(struct signal_struct *sig)
146 spin_lock_irq(&current->sighand->siglock);
147 sig->audit_tty = current->signal->audit_tty;
148 spin_unlock_irq(&current->sighand->siglock);
149 sig->tty_audit_buf = NULL;
153 * tty_audit_push_task - Flush task's pending audit data
155 void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
157 struct tty_audit_buf *buf;
159 spin_lock_irq(&tsk->sighand->siglock);
160 buf = tsk->signal->tty_audit_buf;
161 if (buf)
162 atomic_inc(&buf->count);
163 spin_unlock_irq(&tsk->sighand->siglock);
164 if (!buf)
165 return;
167 mutex_lock(&buf->mutex);
168 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
169 mutex_unlock(&buf->mutex);
171 tty_audit_buf_put(buf);
175 * tty_audit_buf_get - Get an audit buffer.
177 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
178 * if TTY auditing is disabled or out of memory. Otherwise, return a new
179 * reference to the buffer.
181 static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
183 struct tty_audit_buf *buf, *buf2;
185 buf = NULL;
186 buf2 = NULL;
187 spin_lock_irq(&current->sighand->siglock);
188 if (likely(!current->signal->audit_tty))
189 goto out;
190 buf = current->signal->tty_audit_buf;
191 if (buf) {
192 atomic_inc(&buf->count);
193 goto out;
195 spin_unlock_irq(&current->sighand->siglock);
197 buf2 = tty_audit_buf_alloc(tty->driver->major,
198 tty->driver->minor_start + tty->index,
199 tty->icanon);
200 if (buf2 == NULL) {
201 audit_log_lost("out of memory in TTY auditing");
202 return NULL;
205 spin_lock_irq(&current->sighand->siglock);
206 if (!current->signal->audit_tty)
207 goto out;
208 buf = current->signal->tty_audit_buf;
209 if (!buf) {
210 current->signal->tty_audit_buf = buf2;
211 buf = buf2;
212 buf2 = NULL;
214 atomic_inc(&buf->count);
215 /* Fall through */
216 out:
217 spin_unlock_irq(&current->sighand->siglock);
218 if (buf2)
219 tty_audit_buf_free(buf2);
220 return buf;
224 * tty_audit_add_data - Add data for TTY auditing.
226 * Audit @data of @size from @tty, if necessary.
228 void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
229 size_t size)
231 struct tty_audit_buf *buf;
232 int major, minor;
234 if (unlikely(size == 0))
235 return;
237 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
238 && tty->driver->subtype == PTY_TYPE_MASTER)
239 return;
241 buf = tty_audit_buf_get(tty);
242 if (!buf)
243 return;
245 mutex_lock(&buf->mutex);
246 major = tty->driver->major;
247 minor = tty->driver->minor_start + tty->index;
248 if (buf->major != major || buf->minor != minor
249 || buf->icanon != tty->icanon) {
250 tty_audit_buf_push_current(buf);
251 buf->major = major;
252 buf->minor = minor;
253 buf->icanon = tty->icanon;
255 do {
256 size_t run;
258 run = N_TTY_BUF_SIZE - buf->valid;
259 if (run > size)
260 run = size;
261 memcpy(buf->data + buf->valid, data, run);
262 buf->valid += run;
263 data += run;
264 size -= run;
265 if (buf->valid == N_TTY_BUF_SIZE)
266 tty_audit_buf_push_current(buf);
267 } while (size != 0);
268 mutex_unlock(&buf->mutex);
269 tty_audit_buf_put(buf);
273 * tty_audit_push - Push buffered data out
275 * Make sure no audit data is pending for @tty on the current process.
277 void tty_audit_push(struct tty_struct *tty)
279 struct tty_audit_buf *buf;
281 spin_lock_irq(&current->sighand->siglock);
282 if (likely(!current->signal->audit_tty)) {
283 spin_unlock_irq(&current->sighand->siglock);
284 return;
286 buf = current->signal->tty_audit_buf;
287 if (buf)
288 atomic_inc(&buf->count);
289 spin_unlock_irq(&current->sighand->siglock);
291 if (buf) {
292 int major, minor;
294 major = tty->driver->major;
295 minor = tty->driver->minor_start + tty->index;
296 mutex_lock(&buf->mutex);
297 if (buf->major == major && buf->minor == minor)
298 tty_audit_buf_push_current(buf);
299 mutex_unlock(&buf->mutex);
300 tty_audit_buf_put(buf);