MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / audit.c
blobd813b7aa4b4cadb2ec9f61d9426ba0bb66fb4b55
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
5 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
6 * All Rights Reserved.
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.
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.
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
22 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
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.
41 * Example user-space utilities: http://people.redhat.com/faith/audit/
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>
50 #include <linux/audit.h>
52 #include <net/sock.h>
53 #include <linux/skbuff.h>
54 #include <linux/netlink.h>
56 /* No auditing will take place until audit_initialized != 0.
57 * (Initialization happens after skb_init is called.) */
58 static int audit_initialized;
60 /* No syscall auditing will take place unless audit_enabled != 0. */
61 int audit_enabled;
63 /* Default state when kernel boots without any parameters. */
64 static int audit_default;
66 /* If auditing cannot proceed, audit_failure selects what happens. */
67 static int audit_failure = AUDIT_FAIL_PRINTK;
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;
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;
78 /* Number of outstanding audit_buffers allowed. */
79 static int audit_backlog_limit = 64;
80 static atomic_t audit_backlog = ATOMIC_INIT(0);
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
89 static atomic_t audit_lost = ATOMIC_INIT(0);
91 /* The netlink socket. */
92 static struct sock *audit_sock;
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).
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 spinlock_t audit_txlist_lock = SPIN_LOCK_UNLOCKED;
102 static spinlock_t audit_freelist_lock = SPIN_LOCK_UNLOCKED;
103 static int audit_freelist_count = 0;
104 static LIST_HEAD(audit_txlist);
105 static LIST_HEAD(audit_freelist);
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);
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);
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
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)
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];
140 /* Pointer to header and contents */
141 struct nlmsghdr *nlh;
142 int total;
143 int type;
144 int pid;
145 int count; /* Times requeued */
148 struct audit_entry {
149 struct list_head list;
150 struct audit_rule rule;
153 static void audit_panic(const char *message)
155 switch (audit_failure)
157 case AUDIT_FAIL_SILENT:
158 break;
159 case AUDIT_FAIL_PRINTK:
160 printk(KERN_ERR "audit: %s\n", message);
161 break;
162 case AUDIT_FAIL_PANIC:
163 panic(message);
164 break;
168 static inline int audit_rate_check(void)
170 static unsigned long last_check = 0;
171 static int messages = 0;
172 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
173 unsigned long flags;
174 unsigned long now;
175 unsigned long elapsed;
176 int retval = 0;
178 if (!audit_rate_limit) return 1;
180 spin_lock_irqsave(&lock, flags);
181 if (++messages < audit_rate_limit) {
182 retval = 1;
183 } else {
184 now = jiffies;
185 elapsed = now - last_check;
186 if (elapsed > HZ) {
187 last_check = now;
188 messages = 0;
189 retval = 1;
192 spin_unlock_irqrestore(&lock, flags);
194 return retval;
197 /* Emit at least 1 message per second, even if audit_rate_check is
198 * throttling. */
199 void audit_log_lost(const char *message)
201 static unsigned long last_msg = 0;
202 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
203 unsigned long flags;
204 unsigned long now;
205 int print;
207 atomic_inc(&audit_lost);
209 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
211 if (!print) {
212 spin_lock_irqsave(&lock, flags);
213 now = jiffies;
214 if (now - last_msg > HZ) {
215 print = 1;
216 last_msg = now;
218 spin_unlock_irqrestore(&lock, flags);
221 if (print) {
222 printk(KERN_WARNING
223 "audit: audit_lost=%d audit_backlog=%d"
224 " audit_rate_limit=%d audit_backlog_limit=%d\n",
225 atomic_read(&audit_lost),
226 atomic_read(&audit_backlog),
227 audit_rate_limit,
228 audit_backlog_limit);
229 audit_panic(message);
234 int audit_set_rate_limit(int limit)
236 int old = audit_rate_limit;
237 audit_rate_limit = limit;
238 audit_log(current->audit_context, "audit_rate_limit=%d old=%d",
239 audit_rate_limit, old);
240 return old;
243 int audit_set_backlog_limit(int limit)
245 int old = audit_backlog_limit;
246 audit_backlog_limit = limit;
247 audit_log(current->audit_context, "audit_backlog_limit=%d old=%d",
248 audit_backlog_limit, old);
249 return old;
252 int audit_set_enabled(int state)
254 int old = audit_enabled;
255 if (state != 0 && state != 1)
256 return -EINVAL;
257 audit_enabled = state;
258 audit_log(current->audit_context, "audit_enabled=%d old=%d",
259 audit_enabled, old);
260 return old;
263 int audit_set_failure(int state)
265 int old = audit_failure;
266 if (state != AUDIT_FAIL_SILENT
267 && state != AUDIT_FAIL_PRINTK
268 && state != AUDIT_FAIL_PANIC)
269 return -EINVAL;
270 audit_failure = state;
271 audit_log(current->audit_context, "audit_failure=%d old=%d",
272 audit_failure, old);
273 return old;
276 #ifdef CONFIG_NET
277 void audit_send_reply(int pid, int seq, int type, int done, int multi,
278 void *payload, int size)
280 struct sk_buff *skb;
281 struct nlmsghdr *nlh;
282 int len = NLMSG_SPACE(size);
283 void *data;
284 int flags = multi ? NLM_F_MULTI : 0;
285 int t = done ? NLMSG_DONE : type;
287 skb = alloc_skb(len, GFP_KERNEL);
288 if (!skb)
289 goto nlmsg_failure;
291 nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
292 nlh->nlmsg_flags = flags;
293 data = NLMSG_DATA(nlh);
294 memcpy(data, payload, size);
295 netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
296 return;
298 nlmsg_failure: /* Used by NLMSG_PUT */
299 if (skb)
300 kfree_skb(skb);
303 static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
305 u32 uid, pid, seq;
306 void *data;
307 struct audit_status *status_get, status_set;
308 struct audit_login *login;
309 int err = 0;
310 struct audit_buffer *ab;
312 pid = NETLINK_CREDS(skb)->pid;
313 uid = NETLINK_CREDS(skb)->uid;
314 seq = nlh->nlmsg_seq;
315 data = NLMSG_DATA(nlh);
317 switch (nlh->nlmsg_type) {
318 case AUDIT_GET:
319 status_set.enabled = audit_enabled;
320 status_set.failure = audit_failure;
321 status_set.pid = audit_pid;
322 status_set.rate_limit = audit_rate_limit;
323 status_set.backlog_limit = audit_backlog_limit;
324 status_set.lost = atomic_read(&audit_lost);
325 status_set.backlog = atomic_read(&audit_backlog);
326 audit_send_reply(pid, seq, AUDIT_GET, 0, 0,
327 &status_set, sizeof(status_set));
328 break;
329 case AUDIT_SET:
330 if (!capable(CAP_SYS_ADMIN))
331 return -EPERM;
332 status_get = (struct audit_status *)data;
333 if (status_get->mask & AUDIT_STATUS_ENABLED) {
334 err = audit_set_enabled(status_get->enabled);
335 if (err < 0) return err;
337 if (status_get->mask & AUDIT_STATUS_FAILURE) {
338 err = audit_set_failure(status_get->failure);
339 if (err < 0) return err;
341 if (status_get->mask & AUDIT_STATUS_PID) {
342 int old = audit_pid;
343 audit_pid = status_get->pid;
344 audit_log(current->audit_context,
345 "audit_pid=%d old=%d", audit_pid, old);
347 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
348 audit_set_rate_limit(status_get->rate_limit);
349 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
350 audit_set_backlog_limit(status_get->backlog_limit);
351 break;
352 case AUDIT_USER:
353 ab = audit_log_start(NULL);
354 if (!ab)
355 break; /* audit_panic has been called */
356 audit_log_format(ab,
357 "user pid=%d uid=%d length=%d msg='%.1024s'",
358 pid, uid,
359 (int)(nlh->nlmsg_len
360 - ((char *)data - (char *)nlh)),
361 (char *)data);
362 ab->type = AUDIT_USER;
363 ab->pid = pid;
364 audit_log_end(ab);
365 break;
366 case AUDIT_LOGIN:
367 if (!capable(CAP_SYS_ADMIN))
368 return -EPERM;
369 login = (struct audit_login *)data;
370 ab = audit_log_start(NULL);
371 if (ab) {
372 audit_log_format(ab, "login pid=%d uid=%d loginuid=%d"
373 " length=%d msg='%.1024s'",
374 pid, uid,
375 login->loginuid,
376 login->msglen,
377 login->msg);
378 ab->type = AUDIT_LOGIN;
379 ab->pid = pid;
380 audit_log_end(ab);
382 #ifdef CONFIG_AUDITSYSCALL
383 err = audit_set_loginuid(current->audit_context,
384 login->loginuid);
385 #endif
386 break;
387 case AUDIT_LIST:
388 case AUDIT_ADD:
389 case AUDIT_DEL:
390 #ifdef CONFIG_AUDITSYSCALL
391 err = audit_receive_filter(nlh->nlmsg_type, pid, uid, seq,
392 data);
393 #else
394 err = -EOPNOTSUPP;
395 #endif
396 break;
397 default:
398 err = -EINVAL;
399 break;
402 return err < 0 ? err : 0;
405 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
406 * processed by audit_receive_msg. Malformed skbs with wrong length are
407 * discarded silently. */
408 static int audit_receive_skb(struct sk_buff *skb)
410 int err;
411 struct nlmsghdr *nlh;
412 u32 rlen;
414 while (skb->len >= NLMSG_SPACE(0)) {
415 nlh = (struct nlmsghdr *)skb->data;
416 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
417 return 0;
418 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
419 if (rlen > skb->len)
420 rlen = skb->len;
421 if ((err = audit_receive_msg(skb, nlh))) {
422 netlink_ack(skb, nlh, -err);
423 } else if (nlh->nlmsg_flags & NLM_F_ACK)
424 netlink_ack(skb, nlh, 0);
425 skb_pull(skb, rlen);
427 return 0;
430 /* Receive messages from netlink socket. */
431 static void audit_receive(struct sock *sk, int length)
433 struct sk_buff *skb;
435 if (down_trylock(&audit_netlink_sem))
436 return;
438 /* FIXME: this must not cause starvation */
439 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
440 if (audit_receive_skb(skb) && skb->len)
441 skb_queue_head(&sk->sk_receive_queue, skb);
442 else
443 kfree_skb(skb);
445 up(&audit_netlink_sem);
448 /* Move data from tmp buffer into an skb. This is an extra copy, and
449 * that is unfortunate. However, the copy will only occur when a record
450 * is being written to user space, which is already a high-overhead
451 * operation. (Elimination of the copy is possible, for example, by
452 * writing directly into a pre-allocated skb, at the cost of wasting
453 * memory. */
454 static void audit_log_move(struct audit_buffer *ab)
456 struct sk_buff *skb;
457 char *start;
458 int extra = ab->nlh ? 0 : NLMSG_SPACE(0);
460 skb = skb_peek(&ab->sklist);
461 if (!skb || skb_tailroom(skb) <= ab->len + extra) {
462 skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
463 if (!skb) {
464 ab->len = 0; /* Lose information in ab->tmp */
465 audit_log_lost("out of memory in audit_log_move");
466 return;
468 __skb_queue_tail(&ab->sklist, skb);
469 if (!ab->nlh)
470 ab->nlh = (struct nlmsghdr *)skb_put(skb,
471 NLMSG_SPACE(0));
473 start = skb_put(skb, ab->len);
474 memcpy(start, ab->tmp, ab->len);
475 ab->len = 0;
478 /* Iterate over the skbuff in the audit_buffer, sending their contents
479 * to user space. */
480 static inline int audit_log_drain(struct audit_buffer *ab)
482 struct sk_buff *skb;
484 while ((skb = skb_dequeue(&ab->sklist))) {
485 int retval = 0;
487 if (audit_pid) {
488 if (ab->nlh) {
489 ab->nlh->nlmsg_len = ab->total;
490 ab->nlh->nlmsg_type = ab->type;
491 ab->nlh->nlmsg_flags = 0;
492 ab->nlh->nlmsg_seq = 0;
493 ab->nlh->nlmsg_pid = ab->pid;
495 skb_get(skb); /* because netlink_* frees */
496 retval = netlink_unicast(audit_sock, skb, audit_pid,
497 MSG_DONTWAIT);
499 if (retval == -EAGAIN && ab->count < 5) {
500 ++ab->count;
501 audit_log_end_irq(ab);
502 return 1;
504 if (retval < 0) {
505 if (retval == -ECONNREFUSED) {
506 printk(KERN_ERR
507 "audit: *NO* daemon at audit_pid=%d\n",
508 audit_pid);
509 audit_pid = 0;
510 } else
511 audit_log_lost("netlink socket too busy");
513 if (!audit_pid) { /* No daemon */
514 int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
515 int len = skb->len - offset;
516 printk(KERN_ERR "%*.*s\n",
517 len, len, skb->data + offset);
519 kfree_skb(skb);
520 ab->nlh = NULL;
522 return 0;
525 /* Initialize audit support at boot time. */
526 int __init audit_init(void)
528 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
529 audit_default ? "enabled" : "disabled");
530 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
531 if (!audit_sock)
532 audit_panic("cannot initialize netlink socket");
534 audit_initialized = 1;
535 audit_enabled = audit_default;
536 audit_log(NULL, "initialized");
537 return 0;
540 #else
541 /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
542 * in the buffer. */
543 static void audit_log_move(struct audit_buffer *ab)
545 printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
546 ab->len = 0;
549 static inline int audit_log_drain(struct audit_buffer *ab)
551 return 0;
554 /* Initialize audit support at boot time. */
555 int __init audit_init(void)
557 printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
558 audit_sock = NULL;
559 audit_pid = 0;
561 audit_initialized = 1;
562 audit_enabled = audit_default;
563 audit_log(NULL, "initialized");
564 return 0;
566 #endif
568 __initcall(audit_init);
570 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
571 static int __init audit_enable(char *str)
573 audit_default = !!simple_strtol(str, NULL, 0);
574 printk(KERN_INFO "audit: %s%s\n",
575 audit_default ? "enabled" : "disabled",
576 audit_initialized ? "" : " (after initialization)");
577 if (audit_initialized)
578 audit_enabled = audit_default;
579 return 0;
582 __setup("audit=", audit_enable);
585 /* Obtain an audit buffer. This routine does locking to obtain the
586 * audit buffer, but then no locking is required for calls to
587 * audit_log_*format. If the tsk is a task that is currently in a
588 * syscall, then the syscall is marked as auditable and an audit record
589 * will be written at syscall exit. If there is no associated task, tsk
590 * should be NULL. */
591 struct audit_buffer *audit_log_start(struct audit_context *ctx)
593 struct audit_buffer *ab = NULL;
594 unsigned long flags;
595 struct timespec t;
596 int serial = 0;
598 if (!audit_initialized)
599 return NULL;
601 if (audit_backlog_limit
602 && atomic_read(&audit_backlog) > audit_backlog_limit) {
603 if (audit_rate_check())
604 printk(KERN_WARNING
605 "audit: audit_backlog=%d > "
606 "audit_backlog_limit=%d\n",
607 atomic_read(&audit_backlog),
608 audit_backlog_limit);
609 audit_log_lost("backlog limit exceeded");
610 return NULL;
613 spin_lock_irqsave(&audit_freelist_lock, flags);
614 if (!list_empty(&audit_freelist)) {
615 ab = list_entry(audit_freelist.next,
616 struct audit_buffer, list);
617 list_del(&ab->list);
618 --audit_freelist_count;
620 spin_unlock_irqrestore(&audit_freelist_lock, flags);
622 if (!ab)
623 ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
624 if (!ab)
625 audit_log_lost("audit: out of memory in audit_log_start");
626 if (!ab)
627 return NULL;
629 atomic_inc(&audit_backlog);
630 skb_queue_head_init(&ab->sklist);
632 ab->ctx = ctx;
633 ab->len = 0;
634 ab->nlh = NULL;
635 ab->total = 0;
636 ab->type = AUDIT_KERNEL;
637 ab->pid = 0;
638 ab->count = 0;
640 #ifdef CONFIG_AUDITSYSCALL
641 if (ab->ctx)
642 audit_get_stamp(ab->ctx, &t, &serial);
643 else
644 #endif
645 t = CURRENT_TIME;
647 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
648 t.tv_sec, t.tv_nsec/1000000, serial);
649 return ab;
653 /* Format an audit message into the audit buffer. If there isn't enough
654 * room in the audit buffer, more room will be allocated and vsnprint
655 * will be called a second time. Currently, we assume that a printk
656 * can't format message larger than 1024 bytes, so we don't either. */
657 static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
658 va_list args)
660 int len, avail;
662 if (!ab)
663 return;
665 avail = sizeof(ab->tmp) - ab->len;
666 if (avail <= 0) {
667 audit_log_move(ab);
668 avail = sizeof(ab->tmp) - ab->len;
670 len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
671 if (len >= avail) {
672 /* The printk buffer is 1024 bytes long, so if we get
673 * here and AUDIT_BUFSIZ is at least 1024, then we can
674 * log everything that printk could have logged. */
675 audit_log_move(ab);
676 avail = sizeof(ab->tmp) - ab->len;
677 len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
679 ab->len += (len < avail) ? len : avail;
680 ab->total += (len < avail) ? len : avail;
683 /* Format a message into the audit buffer. All the work is done in
684 * audit_log_vformat. */
685 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
687 va_list args;
689 if (!ab)
690 return;
691 va_start(args, fmt);
692 audit_log_vformat(ab, fmt, args);
693 va_end(args);
696 /* This is a helper-function to print the d_path without using a static
697 * buffer or allocating another buffer in addition to the one in
698 * audit_buffer. */
699 void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
700 struct dentry *dentry, struct vfsmount *vfsmnt)
702 char *p;
703 int len, avail;
705 if (prefix) audit_log_format(ab, " %s", prefix);
707 if (ab->len > 128)
708 audit_log_move(ab);
709 avail = sizeof(ab->tmp) - ab->len;
710 p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
711 if (IS_ERR(p)) {
712 /* FIXME: can we save some information here? */
713 audit_log_format(ab, "<toolong>");
714 } else {
715 /* path isn't at start of buffer */
716 len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
717 memmove(ab->tmp + ab->len, p, len);
718 ab->len += len;
719 ab->total += len;
723 /* Remove queued messages from the audit_txlist and send them to userspace. */
724 static void audit_tasklet_handler(unsigned long arg)
726 LIST_HEAD(list);
727 struct audit_buffer *ab;
728 unsigned long flags;
730 spin_lock_irqsave(&audit_txlist_lock, flags);
731 list_splice_init(&audit_txlist, &list);
732 spin_unlock_irqrestore(&audit_txlist_lock, flags);
734 while (!list_empty(&list)) {
735 ab = list_entry(list.next, struct audit_buffer, list);
736 list_del(&ab->list);
737 audit_log_end_fast(ab);
741 static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
743 /* The netlink_* functions cannot be called inside an irq context, so
744 * the audit buffer is places on a queue and a tasklet is scheduled to
745 * remove them from the queue outside the irq context. May be called in
746 * any context. */
747 void audit_log_end_irq(struct audit_buffer *ab)
749 unsigned long flags;
751 if (!ab)
752 return;
753 spin_lock_irqsave(&audit_txlist_lock, flags);
754 list_add_tail(&ab->list, &audit_txlist);
755 spin_unlock_irqrestore(&audit_txlist_lock, flags);
757 tasklet_schedule(&audit_tasklet);
760 /* Send the message in the audit buffer directly to user space. May not
761 * be called in an irq context. */
762 void audit_log_end_fast(struct audit_buffer *ab)
764 unsigned long flags;
766 BUG_ON(in_irq());
767 if (!ab)
768 return;
769 if (!audit_rate_check()) {
770 audit_log_lost("rate limit exceeded");
771 } else {
772 audit_log_move(ab);
773 if (audit_log_drain(ab))
774 return;
777 atomic_dec(&audit_backlog);
778 spin_lock_irqsave(&audit_freelist_lock, flags);
779 if (++audit_freelist_count > AUDIT_MAXFREE)
780 kfree(ab);
781 else
782 list_add(&ab->list, &audit_freelist);
783 spin_unlock_irqrestore(&audit_freelist_lock, flags);
786 /* Send or queue the message in the audit buffer, depending on the
787 * current context. (A convenience function that may be called in any
788 * context.) */
789 void audit_log_end(struct audit_buffer *ab)
791 if (in_irq())
792 audit_log_end_irq(ab);
793 else
794 audit_log_end_fast(ab);
797 /* Log an audit record. This is a convenience function that calls
798 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
799 * called in any context. */
800 void audit_log(struct audit_context *ctx, const char *fmt, ...)
802 struct audit_buffer *ab;
803 va_list args;
805 ab = audit_log_start(ctx);
806 if (ab) {
807 va_start(args, fmt);
808 audit_log_vformat(ab, fmt, args);
809 va_end(args);
810 audit_log_end(ab);