vserver 1.9.5.x5
[linux-2.6.git] / kernel / audit.c
1 /* audit.c -- Auditing support -*- linux-c -*-
2  * Gateway between the kernel (e.g., selinux) and the user-space audit daemon.
3  * System-call specific features have moved to auditsc.c
4  *
5  * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
6  * All Rights Reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23  *
24  * Goals: 1) Integrate fully with SELinux.
25  *        2) Minimal run-time overhead:
26  *           a) Minimal when syscall auditing is disabled (audit_enable=0).
27  *           b) Small when syscall auditing is enabled and no audit record
28  *              is generated (defer as much work as possible to record
29  *              generation time):
30  *              i) context is allocated,
31  *              ii) names from getname are stored without a copy, and
32  *              iii) inode information stored from path_lookup.
33  *        3) Ability to disable syscall auditing at boot time (audit=0).
34  *        4) Usable by other parts of the kernel (if audit_log* is called,
35  *           then a syscall record will be generated automatically for the
36  *           current syscall).
37  *        5) Netlink interface to user-space.
38  *        6) Support low-overhead kernel-based filtering to minimize the
39  *           information that must be passed to user-space.
40  *
41  * Example user-space utilities: http://people.redhat.com/faith/audit/
42  */
43
44 #include <linux/init.h>
45 #include <asm/atomic.h>
46 #include <asm/types.h>
47 #include <linux/mm.h>
48 #include <linux/module.h>
49
50 #include <linux/audit.h>
51
52 #include <net/sock.h>
53 #include <linux/skbuff.h>
54 #include <linux/netlink.h>
55
56 /* No auditing will take place until audit_initialized != 0.
57  * (Initialization happens after skb_init is called.) */
58 static int      audit_initialized;
59
60 /* No syscall auditing will take place unless audit_enabled != 0. */
61 int             audit_enabled;
62
63 /* Default state when kernel boots without any parameters. */
64 static int      audit_default;
65
66 /* If auditing cannot proceed, audit_failure selects what happens. */
67 static int      audit_failure = AUDIT_FAIL_PRINTK;
68
69 /* If audit records are to be written to the netlink socket, audit_pid
70  * contains the (non-zero) pid. */
71 static int      audit_pid;
72
73 /* If audit_limit is non-zero, limit the rate of sending audit records
74  * to that number per second.  This prevents DoS attacks, but results in
75  * audit records being dropped. */
76 static int      audit_rate_limit;
77
78 /* Number of outstanding audit_buffers allowed. */
79 static int      audit_backlog_limit = 64;
80 static atomic_t audit_backlog       = ATOMIC_INIT(0);
81
82 /* Records can be lost in several ways:
83    0) [suppressed in audit_alloc]
84    1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
85    2) out of memory in audit_log_move [alloc_skb]
86    3) suppressed due to audit_rate_limit
87    4) suppressed due to audit_backlog_limit
88 */
89 static atomic_t    audit_lost = ATOMIC_INIT(0);
90
91 /* The netlink socket. */
92 static struct sock *audit_sock;
93
94 /* There are two lists of audit buffers.  The txlist contains audit
95  * buffers that cannot be sent immediately to the netlink device because
96  * we are in an irq context (these are sent later in a tasklet).
97  *
98  * The second list is a list of pre-allocated audit buffers (if more
99  * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
100  * being placed on the freelist). */
101 static DEFINE_SPINLOCK(audit_txlist_lock);
102 static DEFINE_SPINLOCK(audit_freelist_lock);
103 static int         audit_freelist_count = 0;
104 static LIST_HEAD(audit_txlist);
105 static LIST_HEAD(audit_freelist);
106
107 /* There are three lists of rules -- one to search at task creation
108  * time, one to search at syscall entry time, and another to search at
109  * syscall exit time. */
110 static LIST_HEAD(audit_tsklist);
111 static LIST_HEAD(audit_entlist);
112 static LIST_HEAD(audit_extlist);
113
114 /* The netlink socket is only to be read by 1 CPU, which lets us assume
115  * that list additions and deletions never happen simultaneiously in
116  * auditsc.c */
117 static DECLARE_MUTEX(audit_netlink_sem);
118
119 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
120  * audit records.  Since printk uses a 1024 byte buffer, this buffer
121  * should be at least that large. */
122 #define AUDIT_BUFSIZ 1024
123
124 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
125  * audit_freelist.  Doing so eliminates many kmalloc/kfree calls. */
126 #define AUDIT_MAXFREE  (2*NR_CPUS)
127
128 /* The audit_buffer is used when formatting an audit record.  The caller
129  * locks briefly to get the record off the freelist or to allocate the
130  * buffer, and locks briefly to send the buffer to the netlink layer or
131  * to place it on a transmit queue.  Multiple audit_buffers can be in
132  * use simultaneously. */
133 struct audit_buffer {
134         struct list_head     list;
135         struct sk_buff_head  sklist;    /* formatted skbs ready to send */
136         struct audit_context *ctx;      /* NULL or associated context */
137         int                  len;       /* used area of tmp */
138         char                 tmp[AUDIT_BUFSIZ];
139
140                                 /* Pointer to header and contents */
141         struct nlmsghdr      *nlh;
142         int                  total;
143         int                  type;
144         int                  pid;
145         int                  count; /* Times requeued */
146 };
147
148 void audit_set_type(struct audit_buffer *ab, int type)
149 {
150         ab->type = type;
151 }
152
153 struct audit_entry {
154         struct list_head  list;
155         struct audit_rule rule;
156 };
157
158 static void audit_panic(const char *message)
159 {
160         switch (audit_failure)
161         {
162         case AUDIT_FAIL_SILENT:
163                 break;
164         case AUDIT_FAIL_PRINTK:
165                 printk(KERN_ERR "audit: %s\n", message);
166                 break;
167         case AUDIT_FAIL_PANIC:
168                 panic("audit: %s\n", message);
169                 break;
170         }
171 }
172
173 static inline int audit_rate_check(void)
174 {
175         static unsigned long    last_check = 0;
176         static int              messages   = 0;
177         static DEFINE_SPINLOCK(lock);
178         unsigned long           flags;
179         unsigned long           now;
180         unsigned long           elapsed;
181         int                     retval     = 0;
182
183         if (!audit_rate_limit) return 1;
184
185         spin_lock_irqsave(&lock, flags);
186         if (++messages < audit_rate_limit) {
187                 retval = 1;
188         } else {
189                 now     = jiffies;
190                 elapsed = now - last_check;
191                 if (elapsed > HZ) {
192                         last_check = now;
193                         messages   = 0;
194                         retval     = 1;
195                 }
196         }
197         spin_unlock_irqrestore(&lock, flags);
198
199         return retval;
200 }
201
202 /* Emit at least 1 message per second, even if audit_rate_check is
203  * throttling. */
204 void audit_log_lost(const char *message)
205 {
206         static unsigned long    last_msg = 0;
207         static DEFINE_SPINLOCK(lock);
208         unsigned long           flags;
209         unsigned long           now;
210         int                     print;
211
212         atomic_inc(&audit_lost);
213
214         print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
215
216         if (!print) {
217                 spin_lock_irqsave(&lock, flags);
218                 now = jiffies;
219                 if (now - last_msg > HZ) {
220                         print = 1;
221                         last_msg = now;
222                 }
223                 spin_unlock_irqrestore(&lock, flags);
224         }
225
226         if (print) {
227                 printk(KERN_WARNING
228                        "audit: audit_lost=%d audit_backlog=%d"
229                        " audit_rate_limit=%d audit_backlog_limit=%d\n",
230                        atomic_read(&audit_lost),
231                        atomic_read(&audit_backlog),
232                        audit_rate_limit,
233                        audit_backlog_limit);
234                 audit_panic(message);
235         }
236
237 }
238
239 int audit_set_rate_limit(int limit)
240 {
241         int old          = audit_rate_limit;
242         audit_rate_limit = limit;
243         audit_log(current->audit_context, "audit_rate_limit=%d old=%d",
244                   audit_rate_limit, old);
245         return old;
246 }
247
248 int audit_set_backlog_limit(int limit)
249 {
250         int old          = audit_backlog_limit;
251         audit_backlog_limit = limit;
252         audit_log(current->audit_context, "audit_backlog_limit=%d old=%d",
253                   audit_backlog_limit, old);
254         return old;
255 }
256
257 int audit_set_enabled(int state)
258 {
259         int old          = audit_enabled;
260         if (state != 0 && state != 1)
261                 return -EINVAL;
262         audit_enabled = state;
263         audit_log(current->audit_context, "audit_enabled=%d old=%d",
264                   audit_enabled, old);
265         return old;
266 }
267
268 int audit_set_failure(int state)
269 {
270         int old          = audit_failure;
271         if (state != AUDIT_FAIL_SILENT
272             && state != AUDIT_FAIL_PRINTK
273             && state != AUDIT_FAIL_PANIC)
274                 return -EINVAL;
275         audit_failure = state;
276         audit_log(current->audit_context, "audit_failure=%d old=%d",
277                   audit_failure, old);
278         return old;
279 }
280
281 #ifdef CONFIG_NET
282 void audit_send_reply(int pid, int seq, int type, int done, int multi,
283                       void *payload, int size)
284 {
285         struct sk_buff  *skb;
286         struct nlmsghdr *nlh;
287         int             len = NLMSG_SPACE(size);
288         void            *data;
289         int             flags = multi ? NLM_F_MULTI : 0;
290         int             t     = done  ? NLMSG_DONE  : type;
291
292         skb = alloc_skb(len, GFP_KERNEL);
293         if (!skb)
294                 goto nlmsg_failure;
295
296         nlh              = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
297         nlh->nlmsg_flags = flags;
298         data             = NLMSG_DATA(nlh);
299         memcpy(data, payload, size);
300         netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
301         return;
302
303 nlmsg_failure:                  /* Used by NLMSG_PUT */
304         if (skb)
305                 kfree_skb(skb);
306 }
307
308 /*
309  * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
310  * control messages.
311  */
312 static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
313 {
314         int err = 0;
315
316         switch (msg_type) {
317         case AUDIT_GET:
318         case AUDIT_LIST:
319         case AUDIT_SET:
320         case AUDIT_ADD:
321         case AUDIT_DEL:
322                 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
323                         err = -EPERM;
324                 break;
325         case AUDIT_USER:
326                 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
327                         err = -EPERM;
328                 break;
329         default:  /* bad msg */
330                 err = -EINVAL;
331         }
332
333         return err;
334 }
335
336 static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
337 {
338         u32                     uid, pid, seq;
339         void                    *data;
340         struct audit_status     *status_get, status_set;
341         int                     err;
342         struct audit_buffer     *ab;
343         u16                     msg_type = nlh->nlmsg_type;
344
345         err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
346         if (err)
347                 return err;
348
349         pid  = NETLINK_CREDS(skb)->pid;
350         uid  = NETLINK_CREDS(skb)->uid;
351         seq  = nlh->nlmsg_seq;
352         data = NLMSG_DATA(nlh);
353
354         switch (msg_type) {
355         case AUDIT_GET:
356                 status_set.enabled       = audit_enabled;
357                 status_set.failure       = audit_failure;
358                 status_set.pid           = audit_pid;
359                 status_set.rate_limit    = audit_rate_limit;
360                 status_set.backlog_limit = audit_backlog_limit;
361                 status_set.lost          = atomic_read(&audit_lost);
362                 status_set.backlog       = atomic_read(&audit_backlog);
363                 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
364                                  &status_set, sizeof(status_set));
365                 break;
366         case AUDIT_SET:
367                 if (nlh->nlmsg_len < sizeof(struct audit_status))
368                         return -EINVAL;
369                 status_get   = (struct audit_status *)data;
370                 if (status_get->mask & AUDIT_STATUS_ENABLED) {
371                         err = audit_set_enabled(status_get->enabled);
372                         if (err < 0) return err;
373                 }
374                 if (status_get->mask & AUDIT_STATUS_FAILURE) {
375                         err = audit_set_failure(status_get->failure);
376                         if (err < 0) return err;
377                 }
378                 if (status_get->mask & AUDIT_STATUS_PID) {
379                         int old   = audit_pid;
380                         audit_pid = status_get->pid;
381                         audit_log(current->audit_context,
382                                   "audit_pid=%d old=%d", audit_pid, old);
383                 }
384                 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
385                         audit_set_rate_limit(status_get->rate_limit);
386                 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
387                         audit_set_backlog_limit(status_get->backlog_limit);
388                 break;
389         case AUDIT_USER:
390                 ab = audit_log_start(NULL);
391                 if (!ab)
392                         break;  /* audit_panic has been called */
393                 audit_log_format(ab,
394                                  "user pid=%d uid=%d length=%d msg='%.1024s'",
395                                  pid, uid,
396                                  (int)(nlh->nlmsg_len
397                                        - ((char *)data - (char *)nlh)),
398                                  (char *)data);
399                 ab->type = AUDIT_USER;
400                 ab->pid  = pid;
401                 audit_log_end(ab);
402                 break;
403         case AUDIT_ADD:
404         case AUDIT_DEL:
405                 if (nlh->nlmsg_len < sizeof(struct audit_rule))
406                         return -EINVAL;
407                 /* fallthrough */
408         case AUDIT_LIST:
409 #ifdef CONFIG_AUDITSYSCALL
410                 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
411                                            uid, seq, data);
412 #else
413                 err = -EOPNOTSUPP;
414 #endif
415                 break;
416         default:
417                 err = -EINVAL;
418                 break;
419         }
420
421         return err < 0 ? err : 0;
422 }
423
424 /* Get message from skb (based on rtnetlink_rcv_skb).  Each message is
425  * processed by audit_receive_msg.  Malformed skbs with wrong length are
426  * discarded silently.  */
427 static int audit_receive_skb(struct sk_buff *skb)
428 {
429         int             err;
430         struct nlmsghdr *nlh;
431         u32             rlen;
432
433         while (skb->len >= NLMSG_SPACE(0)) {
434                 nlh = (struct nlmsghdr *)skb->data;
435                 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
436                         return 0;
437                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
438                 if (rlen > skb->len)
439                         rlen = skb->len;
440                 if ((err = audit_receive_msg(skb, nlh))) {
441                         netlink_ack(skb, nlh, err);
442                 } else if (nlh->nlmsg_flags & NLM_F_ACK)
443                         netlink_ack(skb, nlh, 0);
444                 skb_pull(skb, rlen);
445         }
446         return 0;
447 }
448
449 /* Receive messages from netlink socket. */
450 static void audit_receive(struct sock *sk, int length)
451 {
452         struct sk_buff  *skb;
453
454         if (down_trylock(&audit_netlink_sem))
455                 return;
456
457                                 /* FIXME: this must not cause starvation */
458         while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
459                 if (audit_receive_skb(skb) && skb->len)
460                         skb_queue_head(&sk->sk_receive_queue, skb);
461                 else
462                         kfree_skb(skb);
463         }
464         up(&audit_netlink_sem);
465 }
466
467 /* Move data from tmp buffer into an skb.  This is an extra copy, and
468  * that is unfortunate.  However, the copy will only occur when a record
469  * is being written to user space, which is already a high-overhead
470  * operation.  (Elimination of the copy is possible, for example, by
471  * writing directly into a pre-allocated skb, at the cost of wasting
472  * memory. */
473 static void audit_log_move(struct audit_buffer *ab)
474 {
475         struct sk_buff  *skb;
476         char            *start;
477         int             extra = ab->nlh ? 0 : NLMSG_SPACE(0);
478
479         /* possible resubmission */
480         if (ab->len == 0)
481                 return;
482
483         skb = skb_peek(&ab->sklist);
484         if (!skb || skb_tailroom(skb) <= ab->len + extra) {
485                 skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
486                 if (!skb) {
487                         ab->len = 0; /* Lose information in ab->tmp */
488                         audit_log_lost("out of memory in audit_log_move");
489                         return;
490                 }
491                 __skb_queue_tail(&ab->sklist, skb);
492                 if (!ab->nlh)
493                         ab->nlh = (struct nlmsghdr *)skb_put(skb,
494                                                              NLMSG_SPACE(0));
495         }
496         start = skb_put(skb, ab->len);
497         memcpy(start, ab->tmp, ab->len);
498         ab->len = 0;
499 }
500
501 /* Iterate over the skbuff in the audit_buffer, sending their contents
502  * to user space. */
503 static inline int audit_log_drain(struct audit_buffer *ab)
504 {
505         struct sk_buff *skb;
506
507         while ((skb = skb_dequeue(&ab->sklist))) {
508                 int retval = 0;
509
510                 if (audit_pid) {
511                         if (ab->nlh) {
512                                 ab->nlh->nlmsg_len   = ab->total;
513                                 ab->nlh->nlmsg_type  = ab->type;
514                                 ab->nlh->nlmsg_flags = 0;
515                                 ab->nlh->nlmsg_seq   = 0;
516                                 ab->nlh->nlmsg_pid   = ab->pid;
517                         }
518                         skb_get(skb); /* because netlink_* frees */
519                         retval = netlink_unicast(audit_sock, skb, audit_pid,
520                                                  MSG_DONTWAIT);
521                 }
522                 if (retval == -EAGAIN && ab->count < 5) {
523                         ++ab->count;
524                         skb_queue_tail(&ab->sklist, skb);
525                         audit_log_end_irq(ab);
526                         return 1;
527                 }
528                 if (retval < 0) {
529                         if (retval == -ECONNREFUSED) {
530                                 printk(KERN_ERR
531                                        "audit: *NO* daemon at audit_pid=%d\n",
532                                        audit_pid);
533                                 audit_pid = 0;
534                         } else
535                                 audit_log_lost("netlink socket too busy");
536                 }
537                 if (!audit_pid) { /* No daemon */
538                         int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
539                         int len    = skb->len - offset;
540                         printk(KERN_ERR "%*.*s\n",
541                                len, len, skb->data + offset);
542                 }
543                 kfree_skb(skb);
544                 ab->nlh = NULL;
545         }
546         return 0;
547 }
548
549 /* Initialize audit support at boot time. */
550 int __init audit_init(void)
551 {
552         printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
553                audit_default ? "enabled" : "disabled");
554         audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
555         if (!audit_sock)
556                 audit_panic("cannot initialize netlink socket");
557
558         audit_initialized = 1;
559         audit_enabled = audit_default;
560         audit_log(NULL, "initialized");
561         return 0;
562 }
563
564 #else
565 /* Without CONFIG_NET, we have no skbuffs.  For now, print what we have
566  * in the buffer. */
567 static void audit_log_move(struct audit_buffer *ab)
568 {
569         printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
570         ab->len = 0;
571 }
572
573 static inline int audit_log_drain(struct audit_buffer *ab)
574 {
575         return 0;
576 }
577
578 /* Initialize audit support at boot time. */
579 int __init audit_init(void)
580 {
581         printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
582         audit_sock = NULL;
583         audit_pid  = 0;
584
585         audit_initialized = 1;
586         audit_enabled = audit_default;
587         audit_log(NULL, "initialized");
588         return 0;
589 }
590 #endif
591
592 __initcall(audit_init);
593
594 /* Process kernel command-line parameter at boot time.  audit=0 or audit=1. */
595 static int __init audit_enable(char *str)
596 {
597         audit_default = !!simple_strtol(str, NULL, 0);
598         printk(KERN_INFO "audit: %s%s\n",
599                audit_default ? "enabled" : "disabled",
600                audit_initialized ? "" : " (after initialization)");
601         if (audit_initialized)
602                 audit_enabled = audit_default;
603         return 0;
604 }
605
606 __setup("audit=", audit_enable);
607
608
609 /* Obtain an audit buffer.  This routine does locking to obtain the
610  * audit buffer, but then no locking is required for calls to
611  * audit_log_*format.  If the tsk is a task that is currently in a
612  * syscall, then the syscall is marked as auditable and an audit record
613  * will be written at syscall exit.  If there is no associated task, tsk
614  * should be NULL. */
615 struct audit_buffer *audit_log_start(struct audit_context *ctx)
616 {
617         struct audit_buffer     *ab     = NULL;
618         unsigned long           flags;
619         struct timespec         t;
620         int                     serial  = 0;
621
622         if (!audit_initialized)
623                 return NULL;
624
625         if (audit_backlog_limit
626             && atomic_read(&audit_backlog) > audit_backlog_limit) {
627                 if (audit_rate_check())
628                         printk(KERN_WARNING
629                                "audit: audit_backlog=%d > "
630                                "audit_backlog_limit=%d\n",
631                                atomic_read(&audit_backlog),
632                                audit_backlog_limit);
633                 audit_log_lost("backlog limit exceeded");
634                 return NULL;
635         }
636
637         spin_lock_irqsave(&audit_freelist_lock, flags);
638         if (!list_empty(&audit_freelist)) {
639                 ab = list_entry(audit_freelist.next,
640                                 struct audit_buffer, list);
641                 list_del(&ab->list);
642                 --audit_freelist_count;
643         }
644         spin_unlock_irqrestore(&audit_freelist_lock, flags);
645
646         if (!ab)
647                 ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
648         if (!ab) {
649                 audit_log_lost("out of memory in audit_log_start");
650                 return NULL;
651         }
652
653         atomic_inc(&audit_backlog);
654         skb_queue_head_init(&ab->sklist);
655
656         ab->ctx   = ctx;
657         ab->len   = 0;
658         ab->nlh   = NULL;
659         ab->total = 0;
660         ab->type  = AUDIT_KERNEL;
661         ab->pid   = 0;
662         ab->count = 0;
663
664 #ifdef CONFIG_AUDITSYSCALL
665         if (ab->ctx)
666                 audit_get_stamp(ab->ctx, &t, &serial);
667         else
668 #endif
669                 t = CURRENT_TIME;
670
671         audit_log_format(ab, "audit(%lu.%03lu:%u): ",
672                          t.tv_sec, t.tv_nsec/1000000, serial);
673         return ab;
674 }
675
676
677 /* Format an audit message into the audit buffer.  If there isn't enough
678  * room in the audit buffer, more room will be allocated and vsnprint
679  * will be called a second time.  Currently, we assume that a printk
680  * can't format message larger than 1024 bytes, so we don't either. */
681 static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
682                               va_list args)
683 {
684         int len, avail;
685
686         if (!ab)
687                 return;
688
689         avail = sizeof(ab->tmp) - ab->len;
690         if (avail <= 0) {
691                 audit_log_move(ab);
692                 avail = sizeof(ab->tmp) - ab->len;
693         }
694         len   = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
695         if (len >= avail) {
696                 /* The printk buffer is 1024 bytes long, so if we get
697                  * here and AUDIT_BUFSIZ is at least 1024, then we can
698                  * log everything that printk could have logged. */
699                 audit_log_move(ab);
700                 avail = sizeof(ab->tmp) - ab->len;
701                 len   = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
702         }
703         ab->len   += (len < avail) ? len : avail;
704         ab->total += (len < avail) ? len : avail;
705 }
706
707 /* Format a message into the audit buffer.  All the work is done in
708  * audit_log_vformat. */
709 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
710 {
711         va_list args;
712
713         if (!ab)
714                 return;
715         va_start(args, fmt);
716         audit_log_vformat(ab, fmt, args);
717         va_end(args);
718 }
719
720 /* This is a helper-function to print the d_path without using a static
721  * buffer or allocating another buffer in addition to the one in
722  * audit_buffer. */
723 void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
724                       struct dentry *dentry, struct vfsmount *vfsmnt)
725 {
726         char *p;
727         int  len, avail;
728
729         if (prefix) audit_log_format(ab, " %s", prefix);
730
731         if (ab->len > 128)
732                 audit_log_move(ab);
733         avail = sizeof(ab->tmp) - ab->len;
734         p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
735         if (IS_ERR(p)) {
736                 /* FIXME: can we save some information here? */
737                 audit_log_format(ab, "<toolong>");
738         } else {
739                                 /* path isn't at start of buffer */
740                 len        = (ab->tmp + sizeof(ab->tmp) - 1) - p;
741                 memmove(ab->tmp + ab->len, p, len);
742                 ab->len   += len;
743                 ab->total += len;
744         }
745 }
746
747 /* Remove queued messages from the audit_txlist and send them to userspace. */
748 static void audit_tasklet_handler(unsigned long arg)
749 {
750         LIST_HEAD(list);
751         struct audit_buffer *ab;
752         unsigned long       flags;
753
754         spin_lock_irqsave(&audit_txlist_lock, flags);
755         list_splice_init(&audit_txlist, &list);
756         spin_unlock_irqrestore(&audit_txlist_lock, flags);
757
758         while (!list_empty(&list)) {
759                 ab = list_entry(list.next, struct audit_buffer, list);
760                 list_del(&ab->list);
761                 audit_log_end_fast(ab);
762         }
763 }
764
765 static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
766
767 /* The netlink_* functions cannot be called inside an irq context, so
768  * the audit buffer is places on a queue and a tasklet is scheduled to
769  * remove them from the queue outside the irq context.  May be called in
770  * any context. */
771 void audit_log_end_irq(struct audit_buffer *ab)
772 {
773         unsigned long flags;
774
775         if (!ab)
776                 return;
777         spin_lock_irqsave(&audit_txlist_lock, flags);
778         list_add_tail(&ab->list, &audit_txlist);
779         spin_unlock_irqrestore(&audit_txlist_lock, flags);
780
781         tasklet_schedule(&audit_tasklet);
782 }
783
784 /* Send the message in the audit buffer directly to user space.  May not
785  * be called in an irq context. */
786 void audit_log_end_fast(struct audit_buffer *ab)
787 {
788         unsigned long flags;
789
790         BUG_ON(in_irq());
791         if (!ab)
792                 return;
793         if (!audit_rate_check()) {
794                 audit_log_lost("rate limit exceeded");
795         } else {
796                 audit_log_move(ab);
797                 if (audit_log_drain(ab))
798                         return;
799         }
800
801         atomic_dec(&audit_backlog);
802         spin_lock_irqsave(&audit_freelist_lock, flags);
803         if (++audit_freelist_count > AUDIT_MAXFREE)
804                 kfree(ab);
805         else
806                 list_add(&ab->list, &audit_freelist);
807         spin_unlock_irqrestore(&audit_freelist_lock, flags);
808 }
809
810 /* Send or queue the message in the audit buffer, depending on the
811  * current context.  (A convenience function that may be called in any
812  * context.) */
813 void audit_log_end(struct audit_buffer *ab)
814 {
815         if (in_irq())
816                 audit_log_end_irq(ab);
817         else
818                 audit_log_end_fast(ab);
819 }
820
821 /* Log an audit record.  This is a convenience function that calls
822  * audit_log_start, audit_log_vformat, and audit_log_end.  It may be
823  * called in any context. */
824 void audit_log(struct audit_context *ctx, const char *fmt, ...)
825 {
826         struct audit_buffer *ab;
827         va_list args;
828
829         ab = audit_log_start(ctx);
830         if (ab) {
831                 va_start(args, fmt);
832                 audit_log_vformat(ab, fmt, args);
833                 va_end(args);
834                 audit_log_end(ab);
835         }
836 }