1 /* audit.c -- Auditing support
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.
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
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
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/sgrubb/audit/
44 #include <linux/init.h>
45 #include <asm/types.h>
46 #include <asm/atomic.h>
48 #include <linux/module.h>
49 #include <linux/err.h>
50 #include <linux/kthread.h>
52 #include <linux/audit.h>
55 #include <net/netlink.h>
56 #include <linux/skbuff.h>
57 #include <linux/netlink.h>
58 #include <linux/selinux.h>
62 /* No auditing will take place until audit_initialized != 0.
63 * (Initialization happens after skb_init is called.) */
64 static int audit_initialized
;
66 /* No syscall auditing will take place unless audit_enabled != 0. */
69 /* Default state when kernel boots without any parameters. */
70 static int audit_default
;
72 /* If auditing cannot proceed, audit_failure selects what happens. */
73 static int audit_failure
= AUDIT_FAIL_PRINTK
;
75 /* If audit records are to be written to the netlink socket, audit_pid
76 * contains the (non-zero) pid. */
79 /* If audit_rate_limit is non-zero, limit the rate of sending audit records
80 * to that number per second. This prevents DoS attacks, but results in
81 * audit records being dropped. */
82 static int audit_rate_limit
;
84 /* Number of outstanding audit_buffers allowed. */
85 static int audit_backlog_limit
= 64;
86 static int audit_backlog_wait_time
= 60 * HZ
;
87 static int audit_backlog_wait_overflow
= 0;
89 /* The identity of the user shutting down the audit system. */
90 uid_t audit_sig_uid
= -1;
91 pid_t audit_sig_pid
= -1;
93 /* Records can be lost in several ways:
94 0) [suppressed in audit_alloc]
95 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
96 2) out of memory in audit_log_move [alloc_skb]
97 3) suppressed due to audit_rate_limit
98 4) suppressed due to audit_backlog_limit
100 static atomic_t audit_lost
= ATOMIC_INIT(0);
102 /* The netlink socket. */
103 static struct sock
*audit_sock
;
105 /* The audit_freelist is a list of pre-allocated audit buffers (if more
106 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
107 * being placed on the freelist). */
108 static DEFINE_SPINLOCK(audit_freelist_lock
);
109 static int audit_freelist_count
;
110 static LIST_HEAD(audit_freelist
);
112 static struct sk_buff_head audit_skb_queue
;
113 static struct task_struct
*kauditd_task
;
114 static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait
);
115 static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait
);
117 /* The netlink socket is only to be read by 1 CPU, which lets us assume
118 * that list additions and deletions never happen simultaneously in
120 DEFINE_MUTEX(audit_netlink_mutex
);
122 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
123 * audit records. Since printk uses a 1024 byte buffer, this buffer
124 * should be at least that large. */
125 #define AUDIT_BUFSIZ 1024
127 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
128 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
129 #define AUDIT_MAXFREE (2*NR_CPUS)
131 /* The audit_buffer is used when formatting an audit record. The caller
132 * locks briefly to get the record off the freelist or to allocate the
133 * buffer, and locks briefly to send the buffer to the netlink layer or
134 * to place it on a transmit queue. Multiple audit_buffers can be in
135 * use simultaneously. */
136 struct audit_buffer
{
137 struct list_head list
;
138 struct sk_buff
*skb
; /* formatted skb ready to send */
139 struct audit_context
*ctx
; /* NULL or associated context */
143 static void audit_set_pid(struct audit_buffer
*ab
, pid_t pid
)
145 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
146 nlh
->nlmsg_pid
= pid
;
149 void audit_panic(const char *message
)
151 switch (audit_failure
)
153 case AUDIT_FAIL_SILENT
:
155 case AUDIT_FAIL_PRINTK
:
156 printk(KERN_ERR
"audit: %s\n", message
);
158 case AUDIT_FAIL_PANIC
:
159 panic("audit: %s\n", message
);
164 static inline int audit_rate_check(void)
166 static unsigned long last_check
= 0;
167 static int messages
= 0;
168 static DEFINE_SPINLOCK(lock
);
171 unsigned long elapsed
;
174 if (!audit_rate_limit
) return 1;
176 spin_lock_irqsave(&lock
, flags
);
177 if (++messages
< audit_rate_limit
) {
181 elapsed
= now
- last_check
;
188 spin_unlock_irqrestore(&lock
, flags
);
194 * audit_log_lost - conditionally log lost audit message event
195 * @message: the message stating reason for lost audit message
197 * Emit at least 1 message per second, even if audit_rate_check is
199 * Always increment the lost messages counter.
201 void audit_log_lost(const char *message
)
203 static unsigned long last_msg
= 0;
204 static DEFINE_SPINLOCK(lock
);
209 atomic_inc(&audit_lost
);
211 print
= (audit_failure
== AUDIT_FAIL_PANIC
|| !audit_rate_limit
);
214 spin_lock_irqsave(&lock
, flags
);
216 if (now
- last_msg
> HZ
) {
220 spin_unlock_irqrestore(&lock
, flags
);
225 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
226 atomic_read(&audit_lost
),
228 audit_backlog_limit
);
229 audit_panic(message
);
233 static int audit_set_rate_limit(int limit
, uid_t loginuid
, u32 sid
)
235 int old
= audit_rate_limit
;
241 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
244 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
245 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
246 limit
, old
, loginuid
, ctx
);
249 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
250 "audit_rate_limit=%d old=%d by auid=%u",
251 limit
, old
, loginuid
);
252 audit_rate_limit
= limit
;
256 static int audit_set_backlog_limit(int limit
, uid_t loginuid
, u32 sid
)
258 int old
= audit_backlog_limit
;
264 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
267 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
268 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
269 limit
, old
, loginuid
, ctx
);
272 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
273 "audit_backlog_limit=%d old=%d by auid=%u",
274 limit
, old
, loginuid
);
275 audit_backlog_limit
= limit
;
279 static int audit_set_enabled(int state
, uid_t loginuid
, u32 sid
)
281 int old
= audit_enabled
;
283 if (state
!= 0 && state
!= 1)
290 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
293 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
294 "audit_enabled=%d old=%d by auid=%u subj=%s",
295 state
, old
, loginuid
, ctx
);
298 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
299 "audit_enabled=%d old=%d by auid=%u",
300 state
, old
, loginuid
);
301 audit_enabled
= state
;
305 static int audit_set_failure(int state
, uid_t loginuid
, u32 sid
)
307 int old
= audit_failure
;
309 if (state
!= AUDIT_FAIL_SILENT
310 && state
!= AUDIT_FAIL_PRINTK
311 && state
!= AUDIT_FAIL_PANIC
)
318 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
321 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
322 "audit_failure=%d old=%d by auid=%u subj=%s",
323 state
, old
, loginuid
, ctx
);
326 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
327 "audit_failure=%d old=%d by auid=%u",
328 state
, old
, loginuid
);
329 audit_failure
= state
;
333 static int kauditd_thread(void *dummy
)
338 skb
= skb_dequeue(&audit_skb_queue
);
339 wake_up(&audit_backlog_wait
);
342 int err
= netlink_unicast(audit_sock
, skb
, audit_pid
, 0);
344 BUG_ON(err
!= -ECONNREFUSED
); /* Shoudn't happen */
345 printk(KERN_ERR
"audit: *NO* daemon at audit_pid=%d\n", audit_pid
);
349 printk(KERN_NOTICE
"%s\n", skb
->data
+ NLMSG_SPACE(0));
353 DECLARE_WAITQUEUE(wait
, current
);
354 set_current_state(TASK_INTERRUPTIBLE
);
355 add_wait_queue(&kauditd_wait
, &wait
);
357 if (!skb_queue_len(&audit_skb_queue
)) {
362 __set_current_state(TASK_RUNNING
);
363 remove_wait_queue(&kauditd_wait
, &wait
);
370 * audit_send_reply - send an audit reply message via netlink
371 * @pid: process id to send reply to
372 * @seq: sequence number
373 * @type: audit message type
374 * @done: done (last) flag
375 * @multi: multi-part message flag
376 * @payload: payload data
377 * @size: payload size
379 * Allocates an skb, builds the netlink message, and sends it to the pid.
380 * No failure notifications.
382 void audit_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
383 void *payload
, int size
)
386 struct nlmsghdr
*nlh
;
387 int len
= NLMSG_SPACE(size
);
389 int flags
= multi
? NLM_F_MULTI
: 0;
390 int t
= done
? NLMSG_DONE
: type
;
392 skb
= alloc_skb(len
, GFP_KERNEL
);
396 nlh
= NLMSG_PUT(skb
, pid
, seq
, t
, size
);
397 nlh
->nlmsg_flags
= flags
;
398 data
= NLMSG_DATA(nlh
);
399 memcpy(data
, payload
, size
);
401 /* Ignore failure. It'll only happen if the sender goes away,
402 because our timeout is set to infinite. */
403 netlink_unicast(audit_sock
, skb
, pid
, 0);
406 nlmsg_failure
: /* Used by NLMSG_PUT */
412 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
415 static int audit_netlink_ok(kernel_cap_t eff_cap
, u16 msg_type
)
422 case AUDIT_LIST_RULES
:
428 case AUDIT_SIGNAL_INFO
:
429 if (!cap_raised(eff_cap
, CAP_AUDIT_CONTROL
))
433 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
434 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
435 if (!cap_raised(eff_cap
, CAP_AUDIT_WRITE
))
438 default: /* bad msg */
445 static int audit_receive_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
447 u32 uid
, pid
, seq
, sid
;
449 struct audit_status
*status_get
, status_set
;
451 struct audit_buffer
*ab
;
452 u16 msg_type
= nlh
->nlmsg_type
;
453 uid_t loginuid
; /* loginuid of sender */
454 struct audit_sig_info sig_data
;
456 err
= audit_netlink_ok(NETLINK_CB(skb
).eff_cap
, msg_type
);
460 /* As soon as there's any sign of userspace auditd,
461 * start kauditd to talk to it */
463 kauditd_task
= kthread_run(kauditd_thread
, NULL
, "kauditd");
464 if (IS_ERR(kauditd_task
)) {
465 err
= PTR_ERR(kauditd_task
);
470 pid
= NETLINK_CREDS(skb
)->pid
;
471 uid
= NETLINK_CREDS(skb
)->uid
;
472 loginuid
= NETLINK_CB(skb
).loginuid
;
473 sid
= NETLINK_CB(skb
).sid
;
474 seq
= nlh
->nlmsg_seq
;
475 data
= NLMSG_DATA(nlh
);
479 status_set
.enabled
= audit_enabled
;
480 status_set
.failure
= audit_failure
;
481 status_set
.pid
= audit_pid
;
482 status_set
.rate_limit
= audit_rate_limit
;
483 status_set
.backlog_limit
= audit_backlog_limit
;
484 status_set
.lost
= atomic_read(&audit_lost
);
485 status_set
.backlog
= skb_queue_len(&audit_skb_queue
);
486 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_GET
, 0, 0,
487 &status_set
, sizeof(status_set
));
490 if (nlh
->nlmsg_len
< sizeof(struct audit_status
))
492 status_get
= (struct audit_status
*)data
;
493 if (status_get
->mask
& AUDIT_STATUS_ENABLED
) {
494 err
= audit_set_enabled(status_get
->enabled
,
496 if (err
< 0) return err
;
498 if (status_get
->mask
& AUDIT_STATUS_FAILURE
) {
499 err
= audit_set_failure(status_get
->failure
,
501 if (err
< 0) return err
;
503 if (status_get
->mask
& AUDIT_STATUS_PID
) {
509 if ((rc
= selinux_ctxid_to_string(
513 audit_log(NULL
, GFP_KERNEL
,
515 "audit_pid=%d old=%d by auid=%u subj=%s",
516 status_get
->pid
, old
,
520 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
521 "audit_pid=%d old=%d by auid=%u",
522 status_get
->pid
, old
, loginuid
);
523 audit_pid
= status_get
->pid
;
525 if (status_get
->mask
& AUDIT_STATUS_RATE_LIMIT
)
526 audit_set_rate_limit(status_get
->rate_limit
,
528 if (status_get
->mask
& AUDIT_STATUS_BACKLOG_LIMIT
)
529 audit_set_backlog_limit(status_get
->backlog_limit
,
533 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
534 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
535 if (!audit_enabled
&& msg_type
!= AUDIT_USER_AVC
)
538 err
= audit_filter_user(&NETLINK_CB(skb
), msg_type
);
541 ab
= audit_log_start(NULL
, GFP_KERNEL
, msg_type
);
544 "user pid=%d uid=%u auid=%u",
549 if (selinux_ctxid_to_string(
553 /* Maybe call audit_panic? */
559 audit_log_format(ab
, " msg='%.1024s'",
561 audit_set_pid(ab
, pid
);
568 if (nlmsg_len(nlh
) < sizeof(struct audit_rule
))
572 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
573 uid
, seq
, data
, nlmsg_len(nlh
),
578 if (nlmsg_len(nlh
) < sizeof(struct audit_rule_data
))
581 case AUDIT_LIST_RULES
:
582 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
583 uid
, seq
, data
, nlmsg_len(nlh
),
586 case AUDIT_SIGNAL_INFO
:
587 sig_data
.uid
= audit_sig_uid
;
588 sig_data
.pid
= audit_sig_pid
;
589 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_SIGNAL_INFO
,
590 0, 0, &sig_data
, sizeof(sig_data
));
597 return err
< 0 ? err
: 0;
601 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
602 * processed by audit_receive_msg. Malformed skbs with wrong length are
603 * discarded silently.
605 static void audit_receive_skb(struct sk_buff
*skb
)
608 struct nlmsghdr
*nlh
;
611 while (skb
->len
>= NLMSG_SPACE(0)) {
612 nlh
= (struct nlmsghdr
*)skb
->data
;
613 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
615 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
618 if ((err
= audit_receive_msg(skb
, nlh
))) {
619 netlink_ack(skb
, nlh
, err
);
620 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
621 netlink_ack(skb
, nlh
, 0);
626 /* Receive messages from netlink socket. */
627 static void audit_receive(struct sock
*sk
, int length
)
632 mutex_lock(&audit_netlink_mutex
);
634 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
635 skb
= skb_dequeue(&sk
->sk_receive_queue
);
636 audit_receive_skb(skb
);
639 mutex_unlock(&audit_netlink_mutex
);
643 /* Initialize audit support at boot time. */
644 static int __init
audit_init(void)
646 printk(KERN_INFO
"audit: initializing netlink socket (%s)\n",
647 audit_default
? "enabled" : "disabled");
648 audit_sock
= netlink_kernel_create(NETLINK_AUDIT
, 0, audit_receive
,
651 audit_panic("cannot initialize netlink socket");
653 audit_sock
->sk_sndtimeo
= MAX_SCHEDULE_TIMEOUT
;
655 skb_queue_head_init(&audit_skb_queue
);
656 audit_initialized
= 1;
657 audit_enabled
= audit_default
;
659 /* Register the callback with selinux. This callback will be invoked
660 * when a new policy is loaded. */
661 selinux_audit_set_callback(&selinux_audit_rule_update
);
663 audit_log(NULL
, GFP_KERNEL
, AUDIT_KERNEL
, "initialized");
666 __initcall(audit_init
);
668 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
669 static int __init
audit_enable(char *str
)
671 audit_default
= !!simple_strtol(str
, NULL
, 0);
672 printk(KERN_INFO
"audit: %s%s\n",
673 audit_default
? "enabled" : "disabled",
674 audit_initialized
? "" : " (after initialization)");
675 if (audit_initialized
)
676 audit_enabled
= audit_default
;
680 __setup("audit=", audit_enable
);
682 static void audit_buffer_free(struct audit_buffer
*ab
)
692 spin_lock_irqsave(&audit_freelist_lock
, flags
);
693 if (++audit_freelist_count
> AUDIT_MAXFREE
)
696 list_add(&ab
->list
, &audit_freelist
);
697 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
700 static struct audit_buffer
* audit_buffer_alloc(struct audit_context
*ctx
,
701 gfp_t gfp_mask
, int type
)
704 struct audit_buffer
*ab
= NULL
;
705 struct nlmsghdr
*nlh
;
707 spin_lock_irqsave(&audit_freelist_lock
, flags
);
708 if (!list_empty(&audit_freelist
)) {
709 ab
= list_entry(audit_freelist
.next
,
710 struct audit_buffer
, list
);
712 --audit_freelist_count
;
714 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
717 ab
= kmalloc(sizeof(*ab
), gfp_mask
);
722 ab
->skb
= alloc_skb(AUDIT_BUFSIZ
, gfp_mask
);
727 ab
->gfp_mask
= gfp_mask
;
728 nlh
= (struct nlmsghdr
*)skb_put(ab
->skb
, NLMSG_SPACE(0));
729 nlh
->nlmsg_type
= type
;
730 nlh
->nlmsg_flags
= 0;
735 audit_buffer_free(ab
);
740 * audit_serial - compute a serial number for the audit record
742 * Compute a serial number for the audit record. Audit records are
743 * written to user-space as soon as they are generated, so a complete
744 * audit record may be written in several pieces. The timestamp of the
745 * record and this serial number are used by the user-space tools to
746 * determine which pieces belong to the same audit record. The
747 * (timestamp,serial) tuple is unique for each syscall and is live from
748 * syscall entry to syscall exit.
750 * NOTE: Another possibility is to store the formatted records off the
751 * audit context (for those records that have a context), and emit them
752 * all at syscall exit. However, this could delay the reporting of
753 * significant errors until syscall exit (or never, if the system
756 unsigned int audit_serial(void)
758 static spinlock_t serial_lock
= SPIN_LOCK_UNLOCKED
;
759 static unsigned int serial
= 0;
764 spin_lock_irqsave(&serial_lock
, flags
);
767 } while (unlikely(!ret
));
768 spin_unlock_irqrestore(&serial_lock
, flags
);
773 static inline void audit_get_stamp(struct audit_context
*ctx
,
774 struct timespec
*t
, unsigned int *serial
)
777 auditsc_get_stamp(ctx
, t
, serial
);
780 *serial
= audit_serial();
784 /* Obtain an audit buffer. This routine does locking to obtain the
785 * audit buffer, but then no locking is required for calls to
786 * audit_log_*format. If the tsk is a task that is currently in a
787 * syscall, then the syscall is marked as auditable and an audit record
788 * will be written at syscall exit. If there is no associated task, tsk
792 * audit_log_start - obtain an audit buffer
793 * @ctx: audit_context (may be NULL)
794 * @gfp_mask: type of allocation
795 * @type: audit message type
797 * Returns audit_buffer pointer on success or NULL on error.
799 * Obtain an audit buffer. This routine does locking to obtain the
800 * audit buffer, but then no locking is required for calls to
801 * audit_log_*format. If the task (ctx) is a task that is currently in a
802 * syscall, then the syscall is marked as auditable and an audit record
803 * will be written at syscall exit. If there is no associated task, then
804 * task context (ctx) should be NULL.
806 struct audit_buffer
*audit_log_start(struct audit_context
*ctx
, gfp_t gfp_mask
,
809 struct audit_buffer
*ab
= NULL
;
813 unsigned long timeout_start
= jiffies
;
815 if (!audit_initialized
)
818 if (unlikely(audit_filter_type(type
)))
821 if (gfp_mask
& __GFP_WAIT
)
824 reserve
= 5; /* Allow atomic callers to go up to five
825 entries over the normal backlog limit */
827 while (audit_backlog_limit
828 && skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
+ reserve
) {
829 if (gfp_mask
& __GFP_WAIT
&& audit_backlog_wait_time
830 && time_before(jiffies
, timeout_start
+ audit_backlog_wait_time
)) {
832 /* Wait for auditd to drain the queue a little */
833 DECLARE_WAITQUEUE(wait
, current
);
834 set_current_state(TASK_INTERRUPTIBLE
);
835 add_wait_queue(&audit_backlog_wait
, &wait
);
837 if (audit_backlog_limit
&&
838 skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
)
839 schedule_timeout(timeout_start
+ audit_backlog_wait_time
- jiffies
);
841 __set_current_state(TASK_RUNNING
);
842 remove_wait_queue(&audit_backlog_wait
, &wait
);
845 if (audit_rate_check())
847 "audit: audit_backlog=%d > "
848 "audit_backlog_limit=%d\n",
849 skb_queue_len(&audit_skb_queue
),
850 audit_backlog_limit
);
851 audit_log_lost("backlog limit exceeded");
852 audit_backlog_wait_time
= audit_backlog_wait_overflow
;
853 wake_up(&audit_backlog_wait
);
857 ab
= audit_buffer_alloc(ctx
, gfp_mask
, type
);
859 audit_log_lost("out of memory in audit_log_start");
863 audit_get_stamp(ab
->ctx
, &t
, &serial
);
865 audit_log_format(ab
, "audit(%lu.%03lu:%u): ",
866 t
.tv_sec
, t
.tv_nsec
/1000000, serial
);
871 * audit_expand - expand skb in the audit buffer
873 * @extra: space to add at tail of the skb
875 * Returns 0 (no space) on failed expansion, or available space if
878 static inline int audit_expand(struct audit_buffer
*ab
, int extra
)
880 struct sk_buff
*skb
= ab
->skb
;
881 int ret
= pskb_expand_head(skb
, skb_headroom(skb
), extra
,
884 audit_log_lost("out of memory in audit_expand");
887 return skb_tailroom(skb
);
891 * Format an audit message into the audit buffer. If there isn't enough
892 * room in the audit buffer, more room will be allocated and vsnprint
893 * will be called a second time. Currently, we assume that a printk
894 * can't format message larger than 1024 bytes, so we don't either.
896 static void audit_log_vformat(struct audit_buffer
*ab
, const char *fmt
,
908 avail
= skb_tailroom(skb
);
910 avail
= audit_expand(ab
, AUDIT_BUFSIZ
);
914 va_copy(args2
, args
);
915 len
= vsnprintf(skb
->tail
, avail
, fmt
, args
);
917 /* The printk buffer is 1024 bytes long, so if we get
918 * here and AUDIT_BUFSIZ is at least 1024, then we can
919 * log everything that printk could have logged. */
920 avail
= audit_expand(ab
,
921 max_t(unsigned, AUDIT_BUFSIZ
, 1+len
-avail
));
924 len
= vsnprintf(skb
->tail
, avail
, fmt
, args2
);
933 * audit_log_format - format a message into the audit buffer.
935 * @fmt: format string
936 * @...: optional parameters matching @fmt string
938 * All the work is done in audit_log_vformat.
940 void audit_log_format(struct audit_buffer
*ab
, const char *fmt
, ...)
947 audit_log_vformat(ab
, fmt
, args
);
952 * audit_log_hex - convert a buffer to hex and append it to the audit skb
953 * @ab: the audit_buffer
954 * @buf: buffer to convert to hex
955 * @len: length of @buf to be converted
957 * No return value; failure to expand is silently ignored.
959 * This function will take the passed buf and convert it into a string of
960 * ascii hex digits. The new string is placed onto the skb.
962 void audit_log_hex(struct audit_buffer
*ab
, const unsigned char *buf
,
965 int i
, avail
, new_len
;
968 static const unsigned char *hex
= "0123456789ABCDEF";
972 avail
= skb_tailroom(skb
);
974 if (new_len
>= avail
) {
975 /* Round the buffer request up to the next multiple */
976 new_len
= AUDIT_BUFSIZ
*(((new_len
-avail
)/AUDIT_BUFSIZ
) + 1);
977 avail
= audit_expand(ab
, new_len
);
983 for (i
=0; i
<len
; i
++) {
984 *ptr
++ = hex
[(buf
[i
] & 0xF0)>>4]; /* Upper nibble */
985 *ptr
++ = hex
[buf
[i
] & 0x0F]; /* Lower nibble */
988 skb_put(skb
, len
<< 1); /* new string is twice the old string */
992 * audit_log_unstrustedstring - log a string that may contain random characters
994 * @string: string to be logged
996 * This code will escape a string that is passed to it if the string
997 * contains a control character, unprintable character, double quote mark,
998 * or a space. Unescaped strings will start and end with a double quote mark.
999 * Strings that are escaped are printed in hex (2 digits per char).
1001 void audit_log_untrustedstring(struct audit_buffer
*ab
, const char *string
)
1003 const unsigned char *p
= string
;
1006 if (*p
== '"' || *p
< 0x21 || *p
> 0x7f) {
1007 audit_log_hex(ab
, string
, strlen(string
));
1012 audit_log_format(ab
, "\"%s\"", string
);
1015 /* This is a helper-function to print the escaped d_path */
1016 void audit_log_d_path(struct audit_buffer
*ab
, const char *prefix
,
1017 struct dentry
*dentry
, struct vfsmount
*vfsmnt
)
1022 audit_log_format(ab
, " %s", prefix
);
1024 /* We will allow 11 spaces for ' (deleted)' to be appended */
1025 path
= kmalloc(PATH_MAX
+11, ab
->gfp_mask
);
1027 audit_log_format(ab
, "<no memory>");
1030 p
= d_path(dentry
, vfsmnt
, path
, PATH_MAX
+11);
1031 if (IS_ERR(p
)) { /* Should never happen since we send PATH_MAX */
1032 /* FIXME: can we save some information here? */
1033 audit_log_format(ab
, "<too long>");
1035 audit_log_untrustedstring(ab
, p
);
1040 * audit_log_end - end one audit record
1041 * @ab: the audit_buffer
1043 * The netlink_* functions cannot be called inside an irq context, so
1044 * the audit buffer is placed on a queue and a tasklet is scheduled to
1045 * remove them from the queue outside the irq context. May be called in
1048 void audit_log_end(struct audit_buffer
*ab
)
1052 if (!audit_rate_check()) {
1053 audit_log_lost("rate limit exceeded");
1056 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
1057 nlh
->nlmsg_len
= ab
->skb
->len
- NLMSG_SPACE(0);
1058 skb_queue_tail(&audit_skb_queue
, ab
->skb
);
1060 wake_up_interruptible(&kauditd_wait
);
1062 printk(KERN_NOTICE
"%s\n", ab
->skb
->data
+ NLMSG_SPACE(0));
1065 audit_buffer_free(ab
);
1069 * audit_log - Log an audit record
1070 * @ctx: audit context
1071 * @gfp_mask: type of allocation
1072 * @type: audit message type
1073 * @fmt: format string to use
1074 * @...: variable parameters matching the format string
1076 * This is a convenience function that calls audit_log_start,
1077 * audit_log_vformat, and audit_log_end. It may be called
1080 void audit_log(struct audit_context
*ctx
, gfp_t gfp_mask
, int type
,
1081 const char *fmt
, ...)
1083 struct audit_buffer
*ab
;
1086 ab
= audit_log_start(ctx
, gfp_mask
, type
);
1088 va_start(args
, fmt
);
1089 audit_log_vformat(ab
, fmt
, args
);
1095 EXPORT_SYMBOL(audit_log_start
);
1096 EXPORT_SYMBOL(audit_log_end
);
1097 EXPORT_SYMBOL(audit_log_format
);
1098 EXPORT_SYMBOL(audit_log
);