]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/char/tty_audit.c
Merge branch 'master' into next
[mv-sheeva.git] / drivers / char / tty_audit.c
1 /*
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>
13 #include <linux/file.h>
14 #include <linux/fdtable.h>
15 #include <linux/tty.h>
16
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 */
24 };
25
26 static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
27                                                  int icanon)
28 {
29         struct tty_audit_buf *buf;
30
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;
47
48 err_buf:
49         kfree(buf);
50 err:
51         return NULL;
52 }
53
54 static void tty_audit_buf_free(struct tty_audit_buf *buf)
55 {
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);
62 }
63
64 static void tty_audit_buf_put(struct tty_audit_buf *buf)
65 {
66         if (atomic_dec_and_test(&buf->count))
67                 tty_audit_buf_free(buf);
68 }
69
70 /**
71  *      tty_audit_buf_push      -       Push buffered data out
72  *
73  *      Generate an audit message from the contents of @buf, which is owned by
74  *      @tsk with @loginuid.  @buf->mutex must be locked.
75  */
76 static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
77                                unsigned int sessionid,
78                                struct tty_audit_buf *buf)
79 {
80         struct audit_buffer *ab;
81
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)];
89                 uid_t uid = task_uid(tsk);
90
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);
99                 audit_log_end(ab);
100         }
101         buf->valid = 0;
102 }
103
104 /**
105  *      tty_audit_buf_push_current      -       Push buffered data out
106  *
107  *      Generate an audit message from the contents of @buf, which is owned by
108  *      the current task.  @buf->mutex must be locked.
109  */
110 static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
111 {
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);
115 }
116
117 /**
118  *      tty_audit_exit  -       Handle a task exit
119  *
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.
122  */
123 void tty_audit_exit(void)
124 {
125         struct tty_audit_buf *buf;
126
127         spin_lock_irq(&current->sighand->siglock);
128         buf = current->signal->tty_audit_buf;
129         current->signal->tty_audit_buf = NULL;
130         spin_unlock_irq(&current->sighand->siglock);
131         if (!buf)
132                 return;
133
134         mutex_lock(&buf->mutex);
135         tty_audit_buf_push_current(buf);
136         mutex_unlock(&buf->mutex);
137
138         tty_audit_buf_put(buf);
139 }
140
141 /**
142  *      tty_audit_fork  -       Copy TTY audit state for a new task
143  *
144  *      Set up TTY audit state in @sig from current.  @sig needs no locking.
145  */
146 void tty_audit_fork(struct signal_struct *sig)
147 {
148         spin_lock_irq(&current->sighand->siglock);
149         sig->audit_tty = current->signal->audit_tty;
150         spin_unlock_irq(&current->sighand->siglock);
151         sig->tty_audit_buf = NULL;
152 }
153
154 /**
155  *      tty_audit_push_task     -       Flush task's pending audit data
156  */
157 void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
158 {
159         struct tty_audit_buf *buf;
160
161         spin_lock_irq(&tsk->sighand->siglock);
162         buf = tsk->signal->tty_audit_buf;
163         if (buf)
164                 atomic_inc(&buf->count);
165         spin_unlock_irq(&tsk->sighand->siglock);
166         if (!buf)
167                 return;
168
169         mutex_lock(&buf->mutex);
170         tty_audit_buf_push(tsk, loginuid, sessionid, buf);
171         mutex_unlock(&buf->mutex);
172
173         tty_audit_buf_put(buf);
174 }
175
176 /**
177  *      tty_audit_buf_get       -       Get an audit buffer.
178  *
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.
182  */
183 static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
184 {
185         struct tty_audit_buf *buf, *buf2;
186
187         buf = NULL;
188         buf2 = NULL;
189         spin_lock_irq(&current->sighand->siglock);
190         if (likely(!current->signal->audit_tty))
191                 goto out;
192         buf = current->signal->tty_audit_buf;
193         if (buf) {
194                 atomic_inc(&buf->count);
195                 goto out;
196         }
197         spin_unlock_irq(&current->sighand->siglock);
198
199         buf2 = tty_audit_buf_alloc(tty->driver->major,
200                                    tty->driver->minor_start + tty->index,
201                                    tty->icanon);
202         if (buf2 == NULL) {
203                 audit_log_lost("out of memory in TTY auditing");
204                 return NULL;
205         }
206
207         spin_lock_irq(&current->sighand->siglock);
208         if (!current->signal->audit_tty)
209                 goto out;
210         buf = current->signal->tty_audit_buf;
211         if (!buf) {
212                 current->signal->tty_audit_buf = buf2;
213                 buf = buf2;
214                 buf2 = NULL;
215         }
216         atomic_inc(&buf->count);
217         /* Fall through */
218  out:
219         spin_unlock_irq(&current->sighand->siglock);
220         if (buf2)
221                 tty_audit_buf_free(buf2);
222         return buf;
223 }
224
225 /**
226  *      tty_audit_add_data      -       Add data for TTY auditing.
227  *
228  *      Audit @data of @size from @tty, if necessary.
229  */
230 void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
231                         size_t size)
232 {
233         struct tty_audit_buf *buf;
234         int major, minor;
235
236         if (unlikely(size == 0))
237                 return;
238
239         if (tty->driver->type == TTY_DRIVER_TYPE_PTY
240             && tty->driver->subtype == PTY_TYPE_MASTER)
241                 return;
242
243         buf = tty_audit_buf_get(tty);
244         if (!buf)
245                 return;
246
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);
253                 buf->major = major;
254                 buf->minor = minor;
255                 buf->icanon = tty->icanon;
256         }
257         do {
258                 size_t run;
259
260                 run = N_TTY_BUF_SIZE - buf->valid;
261                 if (run > size)
262                         run = size;
263                 memcpy(buf->data + buf->valid, data, run);
264                 buf->valid += run;
265                 data += run;
266                 size -= run;
267                 if (buf->valid == N_TTY_BUF_SIZE)
268                         tty_audit_buf_push_current(buf);
269         } while (size != 0);
270         mutex_unlock(&buf->mutex);
271         tty_audit_buf_put(buf);
272 }
273
274 /**
275  *      tty_audit_push  -       Push buffered data out
276  *
277  *      Make sure no audit data is pending for @tty on the current process.
278  */
279 void tty_audit_push(struct tty_struct *tty)
280 {
281         struct tty_audit_buf *buf;
282
283         spin_lock_irq(&current->sighand->siglock);
284         if (likely(!current->signal->audit_tty)) {
285                 spin_unlock_irq(&current->sighand->siglock);
286                 return;
287         }
288         buf = current->signal->tty_audit_buf;
289         if (buf)
290                 atomic_inc(&buf->count);
291         spin_unlock_irq(&current->sighand->siglock);
292
293         if (buf) {
294                 int major, minor;
295
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);
303         }
304 }