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;
92 u32 audit_sig_sid
= 0;
94 /* Records can be lost in several ways:
95 0) [suppressed in audit_alloc]
96 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
97 2) out of memory in audit_log_move [alloc_skb]
98 3) suppressed due to audit_rate_limit
99 4) suppressed due to audit_backlog_limit
101 static atomic_t audit_lost
= ATOMIC_INIT(0);
103 /* The netlink socket. */
104 static struct sock
*audit_sock
;
106 /* The audit_freelist is a list of pre-allocated audit buffers (if more
107 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
108 * being placed on the freelist). */
109 static DEFINE_SPINLOCK(audit_freelist_lock
);
110 static int audit_freelist_count
;
111 static LIST_HEAD(audit_freelist
);
113 static struct sk_buff_head audit_skb_queue
;
114 static struct task_struct
*kauditd_task
;
115 static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait
);
116 static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait
);
118 /* The netlink socket is only to be read by 1 CPU, which lets us assume
119 * that list additions and deletions never happen simultaneously in
121 DEFINE_MUTEX(audit_netlink_mutex
);
123 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
124 * audit records. Since printk uses a 1024 byte buffer, this buffer
125 * should be at least that large. */
126 #define AUDIT_BUFSIZ 1024
128 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
129 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
130 #define AUDIT_MAXFREE (2*NR_CPUS)
132 /* The audit_buffer is used when formatting an audit record. The caller
133 * locks briefly to get the record off the freelist or to allocate the
134 * buffer, and locks briefly to send the buffer to the netlink layer or
135 * to place it on a transmit queue. Multiple audit_buffers can be in
136 * use simultaneously. */
137 struct audit_buffer
{
138 struct list_head list
;
139 struct sk_buff
*skb
; /* formatted skb ready to send */
140 struct audit_context
*ctx
; /* NULL or associated context */
144 static void audit_set_pid(struct audit_buffer
*ab
, pid_t pid
)
146 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
147 nlh
->nlmsg_pid
= pid
;
150 void audit_panic(const char *message
)
152 switch (audit_failure
)
154 case AUDIT_FAIL_SILENT
:
156 case AUDIT_FAIL_PRINTK
:
157 printk(KERN_ERR
"audit: %s\n", message
);
159 case AUDIT_FAIL_PANIC
:
160 panic("audit: %s\n", message
);
165 static inline int audit_rate_check(void)
167 static unsigned long last_check
= 0;
168 static int messages
= 0;
169 static DEFINE_SPINLOCK(lock
);
172 unsigned long elapsed
;
175 if (!audit_rate_limit
) return 1;
177 spin_lock_irqsave(&lock
, flags
);
178 if (++messages
< audit_rate_limit
) {
182 elapsed
= now
- last_check
;
189 spin_unlock_irqrestore(&lock
, flags
);
195 * audit_log_lost - conditionally log lost audit message event
196 * @message: the message stating reason for lost audit message
198 * Emit at least 1 message per second, even if audit_rate_check is
200 * Always increment the lost messages counter.
202 void audit_log_lost(const char *message
)
204 static unsigned long last_msg
= 0;
205 static DEFINE_SPINLOCK(lock
);
210 atomic_inc(&audit_lost
);
212 print
= (audit_failure
== AUDIT_FAIL_PANIC
|| !audit_rate_limit
);
215 spin_lock_irqsave(&lock
, flags
);
217 if (now
- last_msg
> HZ
) {
221 spin_unlock_irqrestore(&lock
, flags
);
226 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
227 atomic_read(&audit_lost
),
229 audit_backlog_limit
);
230 audit_panic(message
);
234 static int audit_set_rate_limit(int limit
, uid_t loginuid
, u32 sid
)
236 int old
= audit_rate_limit
;
242 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
245 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
246 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
247 limit
, old
, loginuid
, ctx
);
250 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
251 "audit_rate_limit=%d old=%d by auid=%u",
252 limit
, old
, loginuid
);
253 audit_rate_limit
= limit
;
257 static int audit_set_backlog_limit(int limit
, uid_t loginuid
, u32 sid
)
259 int old
= audit_backlog_limit
;
265 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
268 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
269 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
270 limit
, old
, loginuid
, ctx
);
273 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
274 "audit_backlog_limit=%d old=%d by auid=%u",
275 limit
, old
, loginuid
);
276 audit_backlog_limit
= limit
;
280 static int audit_set_enabled(int state
, uid_t loginuid
, u32 sid
)
282 int old
= audit_enabled
;
284 if (state
!= 0 && state
!= 1)
291 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
294 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
295 "audit_enabled=%d old=%d by auid=%u subj=%s",
296 state
, old
, loginuid
, ctx
);
299 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
300 "audit_enabled=%d old=%d by auid=%u",
301 state
, old
, loginuid
);
302 audit_enabled
= state
;
306 static int audit_set_failure(int state
, uid_t loginuid
, u32 sid
)
308 int old
= audit_failure
;
310 if (state
!= AUDIT_FAIL_SILENT
311 && state
!= AUDIT_FAIL_PRINTK
312 && state
!= AUDIT_FAIL_PANIC
)
319 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
322 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
323 "audit_failure=%d old=%d by auid=%u subj=%s",
324 state
, old
, loginuid
, ctx
);
327 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
328 "audit_failure=%d old=%d by auid=%u",
329 state
, old
, loginuid
);
330 audit_failure
= state
;
334 static int kauditd_thread(void *dummy
)
339 skb
= skb_dequeue(&audit_skb_queue
);
340 wake_up(&audit_backlog_wait
);
343 int err
= netlink_unicast(audit_sock
, skb
, audit_pid
, 0);
345 BUG_ON(err
!= -ECONNREFUSED
); /* Shoudn't happen */
346 printk(KERN_ERR
"audit: *NO* daemon at audit_pid=%d\n", audit_pid
);
350 printk(KERN_NOTICE
"%s\n", skb
->data
+ NLMSG_SPACE(0));
354 DECLARE_WAITQUEUE(wait
, current
);
355 set_current_state(TASK_INTERRUPTIBLE
);
356 add_wait_queue(&kauditd_wait
, &wait
);
358 if (!skb_queue_len(&audit_skb_queue
)) {
363 __set_current_state(TASK_RUNNING
);
364 remove_wait_queue(&kauditd_wait
, &wait
);
369 int audit_send_list(void *_dest
)
371 struct audit_netlink_list
*dest
= _dest
;
375 /* wait for parent to finish and send an ACK */
376 mutex_lock(&audit_netlink_mutex
);
377 mutex_unlock(&audit_netlink_mutex
);
379 while ((skb
= __skb_dequeue(&dest
->q
)) != NULL
)
380 netlink_unicast(audit_sock
, skb
, pid
, 0);
387 struct sk_buff
*audit_make_reply(int pid
, int seq
, int type
, int done
,
388 int multi
, void *payload
, int size
)
391 struct nlmsghdr
*nlh
;
392 int len
= NLMSG_SPACE(size
);
394 int flags
= multi
? NLM_F_MULTI
: 0;
395 int t
= done
? NLMSG_DONE
: type
;
397 skb
= alloc_skb(len
, GFP_KERNEL
);
401 nlh
= NLMSG_PUT(skb
, pid
, seq
, t
, size
);
402 nlh
->nlmsg_flags
= flags
;
403 data
= NLMSG_DATA(nlh
);
404 memcpy(data
, payload
, size
);
407 nlmsg_failure
: /* Used by NLMSG_PUT */
414 * audit_send_reply - send an audit reply message via netlink
415 * @pid: process id to send reply to
416 * @seq: sequence number
417 * @type: audit message type
418 * @done: done (last) flag
419 * @multi: multi-part message flag
420 * @payload: payload data
421 * @size: payload size
423 * Allocates an skb, builds the netlink message, and sends it to the pid.
424 * No failure notifications.
426 void audit_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
427 void *payload
, int size
)
430 skb
= audit_make_reply(pid
, seq
, type
, done
, multi
, payload
, size
);
433 /* Ignore failure. It'll only happen if the sender goes away,
434 because our timeout is set to infinite. */
435 netlink_unicast(audit_sock
, skb
, pid
, 0);
440 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
443 static int audit_netlink_ok(kernel_cap_t eff_cap
, u16 msg_type
)
450 case AUDIT_LIST_RULES
:
456 case AUDIT_SIGNAL_INFO
:
457 if (!cap_raised(eff_cap
, CAP_AUDIT_CONTROL
))
461 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
462 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
463 if (!cap_raised(eff_cap
, CAP_AUDIT_WRITE
))
466 default: /* bad msg */
473 static int audit_receive_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
475 u32 uid
, pid
, seq
, sid
;
477 struct audit_status
*status_get
, status_set
;
479 struct audit_buffer
*ab
;
480 u16 msg_type
= nlh
->nlmsg_type
;
481 uid_t loginuid
; /* loginuid of sender */
482 struct audit_sig_info
*sig_data
;
486 err
= audit_netlink_ok(NETLINK_CB(skb
).eff_cap
, msg_type
);
490 /* As soon as there's any sign of userspace auditd,
491 * start kauditd to talk to it */
493 kauditd_task
= kthread_run(kauditd_thread
, NULL
, "kauditd");
494 if (IS_ERR(kauditd_task
)) {
495 err
= PTR_ERR(kauditd_task
);
500 pid
= NETLINK_CREDS(skb
)->pid
;
501 uid
= NETLINK_CREDS(skb
)->uid
;
502 loginuid
= NETLINK_CB(skb
).loginuid
;
503 sid
= NETLINK_CB(skb
).sid
;
504 seq
= nlh
->nlmsg_seq
;
505 data
= NLMSG_DATA(nlh
);
509 status_set
.enabled
= audit_enabled
;
510 status_set
.failure
= audit_failure
;
511 status_set
.pid
= audit_pid
;
512 status_set
.rate_limit
= audit_rate_limit
;
513 status_set
.backlog_limit
= audit_backlog_limit
;
514 status_set
.lost
= atomic_read(&audit_lost
);
515 status_set
.backlog
= skb_queue_len(&audit_skb_queue
);
516 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_GET
, 0, 0,
517 &status_set
, sizeof(status_set
));
520 if (nlh
->nlmsg_len
< sizeof(struct audit_status
))
522 status_get
= (struct audit_status
*)data
;
523 if (status_get
->mask
& AUDIT_STATUS_ENABLED
) {
524 err
= audit_set_enabled(status_get
->enabled
,
526 if (err
< 0) return err
;
528 if (status_get
->mask
& AUDIT_STATUS_FAILURE
) {
529 err
= audit_set_failure(status_get
->failure
,
531 if (err
< 0) return err
;
533 if (status_get
->mask
& AUDIT_STATUS_PID
) {
536 if ((err
= selinux_ctxid_to_string(
540 audit_log(NULL
, GFP_KERNEL
,
542 "audit_pid=%d old=%d by auid=%u subj=%s",
543 status_get
->pid
, old
,
547 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
548 "audit_pid=%d old=%d by auid=%u",
549 status_get
->pid
, old
, loginuid
);
550 audit_pid
= status_get
->pid
;
552 if (status_get
->mask
& AUDIT_STATUS_RATE_LIMIT
)
553 err
= audit_set_rate_limit(status_get
->rate_limit
,
555 if (status_get
->mask
& AUDIT_STATUS_BACKLOG_LIMIT
)
556 err
= audit_set_backlog_limit(status_get
->backlog_limit
,
560 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
561 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
562 if (!audit_enabled
&& msg_type
!= AUDIT_USER_AVC
)
565 err
= audit_filter_user(&NETLINK_CB(skb
), msg_type
);
568 ab
= audit_log_start(NULL
, GFP_KERNEL
, msg_type
);
571 "user pid=%d uid=%u auid=%u",
574 if (selinux_ctxid_to_string(
578 /* Maybe call audit_panic? */
584 audit_log_format(ab
, " msg='%.1024s'",
586 audit_set_pid(ab
, pid
);
593 if (nlmsg_len(nlh
) < sizeof(struct audit_rule
))
597 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
598 uid
, seq
, data
, nlmsg_len(nlh
),
603 if (nlmsg_len(nlh
) < sizeof(struct audit_rule_data
))
606 case AUDIT_LIST_RULES
:
607 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
608 uid
, seq
, data
, nlmsg_len(nlh
),
611 case AUDIT_SIGNAL_INFO
:
612 err
= selinux_ctxid_to_string(audit_sig_sid
, &ctx
, &len
);
615 sig_data
= kmalloc(sizeof(*sig_data
) + len
, GFP_KERNEL
);
620 sig_data
->uid
= audit_sig_uid
;
621 sig_data
->pid
= audit_sig_pid
;
622 memcpy(sig_data
->ctx
, ctx
, len
);
624 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_SIGNAL_INFO
,
625 0, 0, sig_data
, sizeof(*sig_data
) + len
);
633 return err
< 0 ? err
: 0;
637 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
638 * processed by audit_receive_msg. Malformed skbs with wrong length are
639 * discarded silently.
641 static void audit_receive_skb(struct sk_buff
*skb
)
644 struct nlmsghdr
*nlh
;
647 while (skb
->len
>= NLMSG_SPACE(0)) {
648 nlh
= (struct nlmsghdr
*)skb
->data
;
649 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
651 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
654 if ((err
= audit_receive_msg(skb
, nlh
))) {
655 netlink_ack(skb
, nlh
, err
);
656 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
657 netlink_ack(skb
, nlh
, 0);
662 /* Receive messages from netlink socket. */
663 static void audit_receive(struct sock
*sk
, int length
)
668 mutex_lock(&audit_netlink_mutex
);
670 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
671 skb
= skb_dequeue(&sk
->sk_receive_queue
);
672 audit_receive_skb(skb
);
675 mutex_unlock(&audit_netlink_mutex
);
679 /* Initialize audit support at boot time. */
680 static int __init
audit_init(void)
682 printk(KERN_INFO
"audit: initializing netlink socket (%s)\n",
683 audit_default
? "enabled" : "disabled");
684 audit_sock
= netlink_kernel_create(NETLINK_AUDIT
, 0, audit_receive
,
687 audit_panic("cannot initialize netlink socket");
689 audit_sock
->sk_sndtimeo
= MAX_SCHEDULE_TIMEOUT
;
691 skb_queue_head_init(&audit_skb_queue
);
692 audit_initialized
= 1;
693 audit_enabled
= audit_default
;
695 /* Register the callback with selinux. This callback will be invoked
696 * when a new policy is loaded. */
697 selinux_audit_set_callback(&selinux_audit_rule_update
);
699 audit_log(NULL
, GFP_KERNEL
, AUDIT_KERNEL
, "initialized");
702 __initcall(audit_init
);
704 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
705 static int __init
audit_enable(char *str
)
707 audit_default
= !!simple_strtol(str
, NULL
, 0);
708 printk(KERN_INFO
"audit: %s%s\n",
709 audit_default
? "enabled" : "disabled",
710 audit_initialized
? "" : " (after initialization)");
711 if (audit_initialized
)
712 audit_enabled
= audit_default
;
716 __setup("audit=", audit_enable
);
718 static void audit_buffer_free(struct audit_buffer
*ab
)
728 spin_lock_irqsave(&audit_freelist_lock
, flags
);
729 if (audit_freelist_count
> AUDIT_MAXFREE
)
732 audit_freelist_count
++;
733 list_add(&ab
->list
, &audit_freelist
);
735 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
738 static struct audit_buffer
* audit_buffer_alloc(struct audit_context
*ctx
,
739 gfp_t gfp_mask
, int type
)
742 struct audit_buffer
*ab
= NULL
;
743 struct nlmsghdr
*nlh
;
745 spin_lock_irqsave(&audit_freelist_lock
, flags
);
746 if (!list_empty(&audit_freelist
)) {
747 ab
= list_entry(audit_freelist
.next
,
748 struct audit_buffer
, list
);
750 --audit_freelist_count
;
752 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
755 ab
= kmalloc(sizeof(*ab
), gfp_mask
);
760 ab
->skb
= alloc_skb(AUDIT_BUFSIZ
, gfp_mask
);
765 ab
->gfp_mask
= gfp_mask
;
766 nlh
= (struct nlmsghdr
*)skb_put(ab
->skb
, NLMSG_SPACE(0));
767 nlh
->nlmsg_type
= type
;
768 nlh
->nlmsg_flags
= 0;
773 audit_buffer_free(ab
);
778 * audit_serial - compute a serial number for the audit record
780 * Compute a serial number for the audit record. Audit records are
781 * written to user-space as soon as they are generated, so a complete
782 * audit record may be written in several pieces. The timestamp of the
783 * record and this serial number are used by the user-space tools to
784 * determine which pieces belong to the same audit record. The
785 * (timestamp,serial) tuple is unique for each syscall and is live from
786 * syscall entry to syscall exit.
788 * NOTE: Another possibility is to store the formatted records off the
789 * audit context (for those records that have a context), and emit them
790 * all at syscall exit. However, this could delay the reporting of
791 * significant errors until syscall exit (or never, if the system
794 unsigned int audit_serial(void)
796 static spinlock_t serial_lock
= SPIN_LOCK_UNLOCKED
;
797 static unsigned int serial
= 0;
802 spin_lock_irqsave(&serial_lock
, flags
);
805 } while (unlikely(!ret
));
806 spin_unlock_irqrestore(&serial_lock
, flags
);
811 static inline void audit_get_stamp(struct audit_context
*ctx
,
812 struct timespec
*t
, unsigned int *serial
)
815 auditsc_get_stamp(ctx
, t
, serial
);
818 *serial
= audit_serial();
822 /* Obtain an audit buffer. This routine does locking to obtain the
823 * audit buffer, but then no locking is required for calls to
824 * audit_log_*format. If the tsk is a task that is currently in a
825 * syscall, then the syscall is marked as auditable and an audit record
826 * will be written at syscall exit. If there is no associated task, tsk
830 * audit_log_start - obtain an audit buffer
831 * @ctx: audit_context (may be NULL)
832 * @gfp_mask: type of allocation
833 * @type: audit message type
835 * Returns audit_buffer pointer on success or NULL on error.
837 * Obtain an audit buffer. This routine does locking to obtain the
838 * audit buffer, but then no locking is required for calls to
839 * audit_log_*format. If the task (ctx) is a task that is currently in a
840 * syscall, then the syscall is marked as auditable and an audit record
841 * will be written at syscall exit. If there is no associated task, then
842 * task context (ctx) should be NULL.
844 struct audit_buffer
*audit_log_start(struct audit_context
*ctx
, gfp_t gfp_mask
,
847 struct audit_buffer
*ab
= NULL
;
851 unsigned long timeout_start
= jiffies
;
853 if (!audit_initialized
)
856 if (unlikely(audit_filter_type(type
)))
859 if (gfp_mask
& __GFP_WAIT
)
862 reserve
= 5; /* Allow atomic callers to go up to five
863 entries over the normal backlog limit */
865 while (audit_backlog_limit
866 && skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
+ reserve
) {
867 if (gfp_mask
& __GFP_WAIT
&& audit_backlog_wait_time
868 && time_before(jiffies
, timeout_start
+ audit_backlog_wait_time
)) {
870 /* Wait for auditd to drain the queue a little */
871 DECLARE_WAITQUEUE(wait
, current
);
872 set_current_state(TASK_INTERRUPTIBLE
);
873 add_wait_queue(&audit_backlog_wait
, &wait
);
875 if (audit_backlog_limit
&&
876 skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
)
877 schedule_timeout(timeout_start
+ audit_backlog_wait_time
- jiffies
);
879 __set_current_state(TASK_RUNNING
);
880 remove_wait_queue(&audit_backlog_wait
, &wait
);
883 if (audit_rate_check())
885 "audit: audit_backlog=%d > "
886 "audit_backlog_limit=%d\n",
887 skb_queue_len(&audit_skb_queue
),
888 audit_backlog_limit
);
889 audit_log_lost("backlog limit exceeded");
890 audit_backlog_wait_time
= audit_backlog_wait_overflow
;
891 wake_up(&audit_backlog_wait
);
895 ab
= audit_buffer_alloc(ctx
, gfp_mask
, type
);
897 audit_log_lost("out of memory in audit_log_start");
901 audit_get_stamp(ab
->ctx
, &t
, &serial
);
903 audit_log_format(ab
, "audit(%lu.%03lu:%u): ",
904 t
.tv_sec
, t
.tv_nsec
/1000000, serial
);
909 * audit_expand - expand skb in the audit buffer
911 * @extra: space to add at tail of the skb
913 * Returns 0 (no space) on failed expansion, or available space if
916 static inline int audit_expand(struct audit_buffer
*ab
, int extra
)
918 struct sk_buff
*skb
= ab
->skb
;
919 int ret
= pskb_expand_head(skb
, skb_headroom(skb
), extra
,
922 audit_log_lost("out of memory in audit_expand");
925 return skb_tailroom(skb
);
929 * Format an audit message into the audit buffer. If there isn't enough
930 * room in the audit buffer, more room will be allocated and vsnprint
931 * will be called a second time. Currently, we assume that a printk
932 * can't format message larger than 1024 bytes, so we don't either.
934 static void audit_log_vformat(struct audit_buffer
*ab
, const char *fmt
,
946 avail
= skb_tailroom(skb
);
948 avail
= audit_expand(ab
, AUDIT_BUFSIZ
);
952 va_copy(args2
, args
);
953 len
= vsnprintf(skb
->tail
, avail
, fmt
, args
);
955 /* The printk buffer is 1024 bytes long, so if we get
956 * here and AUDIT_BUFSIZ is at least 1024, then we can
957 * log everything that printk could have logged. */
958 avail
= audit_expand(ab
,
959 max_t(unsigned, AUDIT_BUFSIZ
, 1+len
-avail
));
962 len
= vsnprintf(skb
->tail
, avail
, fmt
, args2
);
971 * audit_log_format - format a message into the audit buffer.
973 * @fmt: format string
974 * @...: optional parameters matching @fmt string
976 * All the work is done in audit_log_vformat.
978 void audit_log_format(struct audit_buffer
*ab
, const char *fmt
, ...)
985 audit_log_vformat(ab
, fmt
, args
);
990 * audit_log_hex - convert a buffer to hex and append it to the audit skb
991 * @ab: the audit_buffer
992 * @buf: buffer to convert to hex
993 * @len: length of @buf to be converted
995 * No return value; failure to expand is silently ignored.
997 * This function will take the passed buf and convert it into a string of
998 * ascii hex digits. The new string is placed onto the skb.
1000 void audit_log_hex(struct audit_buffer
*ab
, const unsigned char *buf
,
1003 int i
, avail
, new_len
;
1005 struct sk_buff
*skb
;
1006 static const unsigned char *hex
= "0123456789ABCDEF";
1010 avail
= skb_tailroom(skb
);
1012 if (new_len
>= avail
) {
1013 /* Round the buffer request up to the next multiple */
1014 new_len
= AUDIT_BUFSIZ
*(((new_len
-avail
)/AUDIT_BUFSIZ
) + 1);
1015 avail
= audit_expand(ab
, new_len
);
1021 for (i
=0; i
<len
; i
++) {
1022 *ptr
++ = hex
[(buf
[i
] & 0xF0)>>4]; /* Upper nibble */
1023 *ptr
++ = hex
[buf
[i
] & 0x0F]; /* Lower nibble */
1026 skb_put(skb
, len
<< 1); /* new string is twice the old string */
1030 * audit_log_unstrustedstring - log a string that may contain random characters
1032 * @string: string to be logged
1034 * This code will escape a string that is passed to it if the string
1035 * contains a control character, unprintable character, double quote mark,
1036 * or a space. Unescaped strings will start and end with a double quote mark.
1037 * Strings that are escaped are printed in hex (2 digits per char).
1039 const char *audit_log_untrustedstring(struct audit_buffer
*ab
, const char *string
)
1041 const unsigned char *p
= string
;
1042 size_t len
= strlen(string
);
1045 if (*p
== '"' || *p
< 0x21 || *p
> 0x7f) {
1046 audit_log_hex(ab
, string
, len
);
1047 return string
+ len
+ 1;
1051 audit_log_format(ab
, "\"%s\"", string
);
1055 /* This is a helper-function to print the escaped d_path */
1056 void audit_log_d_path(struct audit_buffer
*ab
, const char *prefix
,
1057 struct dentry
*dentry
, struct vfsmount
*vfsmnt
)
1062 audit_log_format(ab
, " %s", prefix
);
1064 /* We will allow 11 spaces for ' (deleted)' to be appended */
1065 path
= kmalloc(PATH_MAX
+11, ab
->gfp_mask
);
1067 audit_log_format(ab
, "<no memory>");
1070 p
= d_path(dentry
, vfsmnt
, path
, PATH_MAX
+11);
1071 if (IS_ERR(p
)) { /* Should never happen since we send PATH_MAX */
1072 /* FIXME: can we save some information here? */
1073 audit_log_format(ab
, "<too long>");
1075 audit_log_untrustedstring(ab
, p
);
1080 * audit_log_end - end one audit record
1081 * @ab: the audit_buffer
1083 * The netlink_* functions cannot be called inside an irq context, so
1084 * the audit buffer is placed on a queue and a tasklet is scheduled to
1085 * remove them from the queue outside the irq context. May be called in
1088 void audit_log_end(struct audit_buffer
*ab
)
1092 if (!audit_rate_check()) {
1093 audit_log_lost("rate limit exceeded");
1096 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
1097 nlh
->nlmsg_len
= ab
->skb
->len
- NLMSG_SPACE(0);
1098 skb_queue_tail(&audit_skb_queue
, ab
->skb
);
1100 wake_up_interruptible(&kauditd_wait
);
1102 printk(KERN_NOTICE
"%s\n", ab
->skb
->data
+ NLMSG_SPACE(0));
1105 audit_buffer_free(ab
);
1109 * audit_log - Log an audit record
1110 * @ctx: audit context
1111 * @gfp_mask: type of allocation
1112 * @type: audit message type
1113 * @fmt: format string to use
1114 * @...: variable parameters matching the format string
1116 * This is a convenience function that calls audit_log_start,
1117 * audit_log_vformat, and audit_log_end. It may be called
1120 void audit_log(struct audit_context
*ctx
, gfp_t gfp_mask
, int type
,
1121 const char *fmt
, ...)
1123 struct audit_buffer
*ab
;
1126 ab
= audit_log_start(ctx
, gfp_mask
, type
);
1128 va_start(args
, fmt
);
1129 audit_log_vformat(ab
, fmt
, args
);
1135 EXPORT_SYMBOL(audit_log_start
);
1136 EXPORT_SYMBOL(audit_log_end
);
1137 EXPORT_SYMBOL(audit_log_format
);
1138 EXPORT_SYMBOL(audit_log
);