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>
59 #include <linux/inotify.h>
63 /* No auditing will take place until audit_initialized != 0.
64 * (Initialization happens after skb_init is called.) */
65 static int audit_initialized
;
67 /* No syscall auditing will take place unless audit_enabled != 0. */
70 /* Default state when kernel boots without any parameters. */
71 static int audit_default
;
73 /* If auditing cannot proceed, audit_failure selects what happens. */
74 static int audit_failure
= AUDIT_FAIL_PRINTK
;
76 /* If audit records are to be written to the netlink socket, audit_pid
77 * contains the (non-zero) pid. */
80 /* If audit_rate_limit is non-zero, limit the rate of sending audit records
81 * to that number per second. This prevents DoS attacks, but results in
82 * audit records being dropped. */
83 static int audit_rate_limit
;
85 /* Number of outstanding audit_buffers allowed. */
86 static int audit_backlog_limit
= 64;
87 static int audit_backlog_wait_time
= 60 * HZ
;
88 static int audit_backlog_wait_overflow
= 0;
90 /* The identity of the user shutting down the audit system. */
91 uid_t audit_sig_uid
= -1;
92 pid_t audit_sig_pid
= -1;
93 u32 audit_sig_sid
= 0;
95 /* Records can be lost in several ways:
96 0) [suppressed in audit_alloc]
97 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
98 2) out of memory in audit_log_move [alloc_skb]
99 3) suppressed due to audit_rate_limit
100 4) suppressed due to audit_backlog_limit
102 static atomic_t audit_lost
= ATOMIC_INIT(0);
104 /* The netlink socket. */
105 static struct sock
*audit_sock
;
107 /* Inotify handle. */
108 struct inotify_handle
*audit_ih
;
110 /* Hash for inode-based rules */
111 struct list_head audit_inode_hash
[AUDIT_INODE_BUCKETS
];
113 /* The audit_freelist is a list of pre-allocated audit buffers (if more
114 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
115 * being placed on the freelist). */
116 static DEFINE_SPINLOCK(audit_freelist_lock
);
117 static int audit_freelist_count
;
118 static LIST_HEAD(audit_freelist
);
120 static struct sk_buff_head audit_skb_queue
;
121 static struct task_struct
*kauditd_task
;
122 static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait
);
123 static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait
);
125 /* Serialize requests from userspace. */
126 static DEFINE_MUTEX(audit_cmd_mutex
);
128 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
129 * audit records. Since printk uses a 1024 byte buffer, this buffer
130 * should be at least that large. */
131 #define AUDIT_BUFSIZ 1024
133 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
134 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
135 #define AUDIT_MAXFREE (2*NR_CPUS)
137 /* The audit_buffer is used when formatting an audit record. The caller
138 * locks briefly to get the record off the freelist or to allocate the
139 * buffer, and locks briefly to send the buffer to the netlink layer or
140 * to place it on a transmit queue. Multiple audit_buffers can be in
141 * use simultaneously. */
142 struct audit_buffer
{
143 struct list_head list
;
144 struct sk_buff
*skb
; /* formatted skb ready to send */
145 struct audit_context
*ctx
; /* NULL or associated context */
149 static void audit_set_pid(struct audit_buffer
*ab
, pid_t pid
)
151 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
152 nlh
->nlmsg_pid
= pid
;
155 void audit_panic(const char *message
)
157 switch (audit_failure
)
159 case AUDIT_FAIL_SILENT
:
161 case AUDIT_FAIL_PRINTK
:
162 printk(KERN_ERR
"audit: %s\n", message
);
164 case AUDIT_FAIL_PANIC
:
165 panic("audit: %s\n", message
);
170 static inline int audit_rate_check(void)
172 static unsigned long last_check
= 0;
173 static int messages
= 0;
174 static DEFINE_SPINLOCK(lock
);
177 unsigned long elapsed
;
180 if (!audit_rate_limit
) return 1;
182 spin_lock_irqsave(&lock
, flags
);
183 if (++messages
< audit_rate_limit
) {
187 elapsed
= now
- last_check
;
194 spin_unlock_irqrestore(&lock
, flags
);
200 * audit_log_lost - conditionally log lost audit message event
201 * @message: the message stating reason for lost audit message
203 * Emit at least 1 message per second, even if audit_rate_check is
205 * Always increment the lost messages counter.
207 void audit_log_lost(const char *message
)
209 static unsigned long last_msg
= 0;
210 static DEFINE_SPINLOCK(lock
);
215 atomic_inc(&audit_lost
);
217 print
= (audit_failure
== AUDIT_FAIL_PANIC
|| !audit_rate_limit
);
220 spin_lock_irqsave(&lock
, flags
);
222 if (now
- last_msg
> HZ
) {
226 spin_unlock_irqrestore(&lock
, flags
);
231 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
232 atomic_read(&audit_lost
),
234 audit_backlog_limit
);
235 audit_panic(message
);
239 static int audit_set_rate_limit(int limit
, uid_t loginuid
, u32 sid
)
241 int old
= audit_rate_limit
;
247 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
250 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
251 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
252 limit
, old
, loginuid
, ctx
);
255 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
256 "audit_rate_limit=%d old=%d by auid=%u",
257 limit
, old
, loginuid
);
258 audit_rate_limit
= limit
;
262 static int audit_set_backlog_limit(int limit
, uid_t loginuid
, u32 sid
)
264 int old
= audit_backlog_limit
;
270 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
273 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
274 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
275 limit
, old
, loginuid
, ctx
);
278 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
279 "audit_backlog_limit=%d old=%d by auid=%u",
280 limit
, old
, loginuid
);
281 audit_backlog_limit
= limit
;
285 static int audit_set_enabled(int state
, uid_t loginuid
, u32 sid
)
287 int old
= audit_enabled
;
289 if (state
!= 0 && state
!= 1)
296 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
299 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
300 "audit_enabled=%d old=%d by auid=%u subj=%s",
301 state
, old
, loginuid
, ctx
);
304 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
305 "audit_enabled=%d old=%d by auid=%u",
306 state
, old
, loginuid
);
307 audit_enabled
= state
;
311 static int audit_set_failure(int state
, uid_t loginuid
, u32 sid
)
313 int old
= audit_failure
;
315 if (state
!= AUDIT_FAIL_SILENT
316 && state
!= AUDIT_FAIL_PRINTK
317 && state
!= AUDIT_FAIL_PANIC
)
324 if ((rc
= selinux_ctxid_to_string(sid
, &ctx
, &len
)))
327 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
328 "audit_failure=%d old=%d by auid=%u subj=%s",
329 state
, old
, loginuid
, ctx
);
332 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
333 "audit_failure=%d old=%d by auid=%u",
334 state
, old
, loginuid
);
335 audit_failure
= state
;
339 static int kauditd_thread(void *dummy
)
344 skb
= skb_dequeue(&audit_skb_queue
);
345 wake_up(&audit_backlog_wait
);
348 int err
= netlink_unicast(audit_sock
, skb
, audit_pid
, 0);
350 BUG_ON(err
!= -ECONNREFUSED
); /* Shoudn't happen */
351 printk(KERN_ERR
"audit: *NO* daemon at audit_pid=%d\n", audit_pid
);
355 printk(KERN_NOTICE
"%s\n", skb
->data
+ NLMSG_SPACE(0));
359 DECLARE_WAITQUEUE(wait
, current
);
360 set_current_state(TASK_INTERRUPTIBLE
);
361 add_wait_queue(&kauditd_wait
, &wait
);
363 if (!skb_queue_len(&audit_skb_queue
)) {
368 __set_current_state(TASK_RUNNING
);
369 remove_wait_queue(&kauditd_wait
, &wait
);
374 int audit_send_list(void *_dest
)
376 struct audit_netlink_list
*dest
= _dest
;
380 /* wait for parent to finish and send an ACK */
381 mutex_lock(&audit_cmd_mutex
);
382 mutex_unlock(&audit_cmd_mutex
);
384 while ((skb
= __skb_dequeue(&dest
->q
)) != NULL
)
385 netlink_unicast(audit_sock
, skb
, pid
, 0);
392 struct sk_buff
*audit_make_reply(int pid
, int seq
, int type
, int done
,
393 int multi
, void *payload
, int size
)
396 struct nlmsghdr
*nlh
;
397 int len
= NLMSG_SPACE(size
);
399 int flags
= multi
? NLM_F_MULTI
: 0;
400 int t
= done
? NLMSG_DONE
: type
;
402 skb
= alloc_skb(len
, GFP_KERNEL
);
406 nlh
= NLMSG_PUT(skb
, pid
, seq
, t
, size
);
407 nlh
->nlmsg_flags
= flags
;
408 data
= NLMSG_DATA(nlh
);
409 memcpy(data
, payload
, size
);
412 nlmsg_failure
: /* Used by NLMSG_PUT */
419 * audit_send_reply - send an audit reply message via netlink
420 * @pid: process id to send reply to
421 * @seq: sequence number
422 * @type: audit message type
423 * @done: done (last) flag
424 * @multi: multi-part message flag
425 * @payload: payload data
426 * @size: payload size
428 * Allocates an skb, builds the netlink message, and sends it to the pid.
429 * No failure notifications.
431 void audit_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
432 void *payload
, int size
)
435 skb
= audit_make_reply(pid
, seq
, type
, done
, multi
, payload
, size
);
438 /* Ignore failure. It'll only happen if the sender goes away,
439 because our timeout is set to infinite. */
440 netlink_unicast(audit_sock
, skb
, pid
, 0);
445 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
448 static int audit_netlink_ok(struct sk_buff
*skb
, u16 msg_type
)
455 case AUDIT_LIST_RULES
:
461 case AUDIT_SIGNAL_INFO
:
462 if (security_netlink_recv(skb
, CAP_AUDIT_CONTROL
))
466 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
467 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
468 if (security_netlink_recv(skb
, CAP_AUDIT_WRITE
))
471 default: /* bad msg */
478 static int audit_receive_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
480 u32 uid
, pid
, seq
, sid
;
482 struct audit_status
*status_get
, status_set
;
484 struct audit_buffer
*ab
;
485 u16 msg_type
= nlh
->nlmsg_type
;
486 uid_t loginuid
; /* loginuid of sender */
487 struct audit_sig_info
*sig_data
;
491 err
= audit_netlink_ok(skb
, msg_type
);
495 /* As soon as there's any sign of userspace auditd,
496 * start kauditd to talk to it */
498 kauditd_task
= kthread_run(kauditd_thread
, NULL
, "kauditd");
499 if (IS_ERR(kauditd_task
)) {
500 err
= PTR_ERR(kauditd_task
);
505 pid
= NETLINK_CREDS(skb
)->pid
;
506 uid
= NETLINK_CREDS(skb
)->uid
;
507 loginuid
= NETLINK_CB(skb
).loginuid
;
508 sid
= NETLINK_CB(skb
).sid
;
509 seq
= nlh
->nlmsg_seq
;
510 data
= NLMSG_DATA(nlh
);
514 status_set
.enabled
= audit_enabled
;
515 status_set
.failure
= audit_failure
;
516 status_set
.pid
= audit_pid
;
517 status_set
.rate_limit
= audit_rate_limit
;
518 status_set
.backlog_limit
= audit_backlog_limit
;
519 status_set
.lost
= atomic_read(&audit_lost
);
520 status_set
.backlog
= skb_queue_len(&audit_skb_queue
);
521 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_GET
, 0, 0,
522 &status_set
, sizeof(status_set
));
525 if (nlh
->nlmsg_len
< sizeof(struct audit_status
))
527 status_get
= (struct audit_status
*)data
;
528 if (status_get
->mask
& AUDIT_STATUS_ENABLED
) {
529 err
= audit_set_enabled(status_get
->enabled
,
531 if (err
< 0) return err
;
533 if (status_get
->mask
& AUDIT_STATUS_FAILURE
) {
534 err
= audit_set_failure(status_get
->failure
,
536 if (err
< 0) return err
;
538 if (status_get
->mask
& AUDIT_STATUS_PID
) {
541 if ((err
= selinux_ctxid_to_string(
545 audit_log(NULL
, GFP_KERNEL
,
547 "audit_pid=%d old=%d by auid=%u subj=%s",
548 status_get
->pid
, old
,
552 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
553 "audit_pid=%d old=%d by auid=%u",
554 status_get
->pid
, old
, loginuid
);
555 audit_pid
= status_get
->pid
;
557 if (status_get
->mask
& AUDIT_STATUS_RATE_LIMIT
)
558 err
= audit_set_rate_limit(status_get
->rate_limit
,
560 if (status_get
->mask
& AUDIT_STATUS_BACKLOG_LIMIT
)
561 err
= audit_set_backlog_limit(status_get
->backlog_limit
,
565 case AUDIT_FIRST_USER_MSG
...AUDIT_LAST_USER_MSG
:
566 case AUDIT_FIRST_USER_MSG2
...AUDIT_LAST_USER_MSG2
:
567 if (!audit_enabled
&& msg_type
!= AUDIT_USER_AVC
)
570 err
= audit_filter_user(&NETLINK_CB(skb
), msg_type
);
573 ab
= audit_log_start(NULL
, GFP_KERNEL
, msg_type
);
576 "user pid=%d uid=%u auid=%u",
579 if (selinux_ctxid_to_string(
583 /* Maybe call audit_panic? */
589 audit_log_format(ab
, " msg='%.1024s'",
591 audit_set_pid(ab
, pid
);
598 if (nlmsg_len(nlh
) < sizeof(struct audit_rule
))
602 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
603 uid
, seq
, data
, nlmsg_len(nlh
),
608 if (nlmsg_len(nlh
) < sizeof(struct audit_rule_data
))
611 case AUDIT_LIST_RULES
:
612 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
613 uid
, seq
, data
, nlmsg_len(nlh
),
616 case AUDIT_SIGNAL_INFO
:
617 err
= selinux_ctxid_to_string(audit_sig_sid
, &ctx
, &len
);
620 sig_data
= kmalloc(sizeof(*sig_data
) + len
, GFP_KERNEL
);
625 sig_data
->uid
= audit_sig_uid
;
626 sig_data
->pid
= audit_sig_pid
;
627 memcpy(sig_data
->ctx
, ctx
, len
);
629 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_SIGNAL_INFO
,
630 0, 0, sig_data
, sizeof(*sig_data
) + len
);
638 return err
< 0 ? err
: 0;
642 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
643 * processed by audit_receive_msg. Malformed skbs with wrong length are
644 * discarded silently.
646 static void audit_receive_skb(struct sk_buff
*skb
)
649 struct nlmsghdr
*nlh
;
652 while (skb
->len
>= NLMSG_SPACE(0)) {
653 nlh
= (struct nlmsghdr
*)skb
->data
;
654 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
656 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
659 if ((err
= audit_receive_msg(skb
, nlh
))) {
660 netlink_ack(skb
, nlh
, err
);
661 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
662 netlink_ack(skb
, nlh
, 0);
667 /* Receive messages from netlink socket. */
668 static void audit_receive(struct sock
*sk
, int length
)
673 mutex_lock(&audit_cmd_mutex
);
675 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
676 skb
= skb_dequeue(&sk
->sk_receive_queue
);
677 audit_receive_skb(skb
);
680 mutex_unlock(&audit_cmd_mutex
);
683 #ifdef CONFIG_AUDITSYSCALL
684 static const struct inotify_operations audit_inotify_ops
= {
685 .handle_event
= audit_handle_ievent
,
686 .destroy_watch
= audit_free_parent
,
690 /* Initialize audit support at boot time. */
691 static int __init
audit_init(void)
693 #ifdef CONFIG_AUDITSYSCALL
697 printk(KERN_INFO
"audit: initializing netlink socket (%s)\n",
698 audit_default
? "enabled" : "disabled");
699 audit_sock
= netlink_kernel_create(NETLINK_AUDIT
, 0, audit_receive
,
702 audit_panic("cannot initialize netlink socket");
704 audit_sock
->sk_sndtimeo
= MAX_SCHEDULE_TIMEOUT
;
706 skb_queue_head_init(&audit_skb_queue
);
707 audit_initialized
= 1;
708 audit_enabled
= audit_default
;
710 /* Register the callback with selinux. This callback will be invoked
711 * when a new policy is loaded. */
712 selinux_audit_set_callback(&selinux_audit_rule_update
);
714 audit_log(NULL
, GFP_KERNEL
, AUDIT_KERNEL
, "initialized");
716 #ifdef CONFIG_AUDITSYSCALL
717 audit_ih
= inotify_init(&audit_inotify_ops
);
718 if (IS_ERR(audit_ih
))
719 audit_panic("cannot initialize inotify handle");
721 for (i
= 0; i
< AUDIT_INODE_BUCKETS
; i
++)
722 INIT_LIST_HEAD(&audit_inode_hash
[i
]);
727 __initcall(audit_init
);
729 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
730 static int __init
audit_enable(char *str
)
732 audit_default
= !!simple_strtol(str
, NULL
, 0);
733 printk(KERN_INFO
"audit: %s%s\n",
734 audit_default
? "enabled" : "disabled",
735 audit_initialized
? "" : " (after initialization)");
736 if (audit_initialized
)
737 audit_enabled
= audit_default
;
741 __setup("audit=", audit_enable
);
743 static void audit_buffer_free(struct audit_buffer
*ab
)
753 spin_lock_irqsave(&audit_freelist_lock
, flags
);
754 if (audit_freelist_count
> AUDIT_MAXFREE
)
757 audit_freelist_count
++;
758 list_add(&ab
->list
, &audit_freelist
);
760 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
763 static struct audit_buffer
* audit_buffer_alloc(struct audit_context
*ctx
,
764 gfp_t gfp_mask
, int type
)
767 struct audit_buffer
*ab
= NULL
;
768 struct nlmsghdr
*nlh
;
770 spin_lock_irqsave(&audit_freelist_lock
, flags
);
771 if (!list_empty(&audit_freelist
)) {
772 ab
= list_entry(audit_freelist
.next
,
773 struct audit_buffer
, list
);
775 --audit_freelist_count
;
777 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
780 ab
= kmalloc(sizeof(*ab
), gfp_mask
);
785 ab
->skb
= alloc_skb(AUDIT_BUFSIZ
, gfp_mask
);
790 ab
->gfp_mask
= gfp_mask
;
791 nlh
= (struct nlmsghdr
*)skb_put(ab
->skb
, NLMSG_SPACE(0));
792 nlh
->nlmsg_type
= type
;
793 nlh
->nlmsg_flags
= 0;
798 audit_buffer_free(ab
);
803 * audit_serial - compute a serial number for the audit record
805 * Compute a serial number for the audit record. Audit records are
806 * written to user-space as soon as they are generated, so a complete
807 * audit record may be written in several pieces. The timestamp of the
808 * record and this serial number are used by the user-space tools to
809 * determine which pieces belong to the same audit record. The
810 * (timestamp,serial) tuple is unique for each syscall and is live from
811 * syscall entry to syscall exit.
813 * NOTE: Another possibility is to store the formatted records off the
814 * audit context (for those records that have a context), and emit them
815 * all at syscall exit. However, this could delay the reporting of
816 * significant errors until syscall exit (or never, if the system
819 unsigned int audit_serial(void)
821 static DEFINE_SPINLOCK(serial_lock
);
822 static unsigned int serial
= 0;
827 spin_lock_irqsave(&serial_lock
, flags
);
830 } while (unlikely(!ret
));
831 spin_unlock_irqrestore(&serial_lock
, flags
);
836 static inline void audit_get_stamp(struct audit_context
*ctx
,
837 struct timespec
*t
, unsigned int *serial
)
840 auditsc_get_stamp(ctx
, t
, serial
);
843 *serial
= audit_serial();
847 /* Obtain an audit buffer. This routine does locking to obtain the
848 * audit buffer, but then no locking is required for calls to
849 * audit_log_*format. If the tsk is a task that is currently in a
850 * syscall, then the syscall is marked as auditable and an audit record
851 * will be written at syscall exit. If there is no associated task, tsk
855 * audit_log_start - obtain an audit buffer
856 * @ctx: audit_context (may be NULL)
857 * @gfp_mask: type of allocation
858 * @type: audit message type
860 * Returns audit_buffer pointer on success or NULL on error.
862 * Obtain an audit buffer. This routine does locking to obtain the
863 * audit buffer, but then no locking is required for calls to
864 * audit_log_*format. If the task (ctx) is a task that is currently in a
865 * syscall, then the syscall is marked as auditable and an audit record
866 * will be written at syscall exit. If there is no associated task, then
867 * task context (ctx) should be NULL.
869 struct audit_buffer
*audit_log_start(struct audit_context
*ctx
, gfp_t gfp_mask
,
872 struct audit_buffer
*ab
= NULL
;
876 unsigned long timeout_start
= jiffies
;
878 if (!audit_initialized
)
881 if (unlikely(audit_filter_type(type
)))
884 if (gfp_mask
& __GFP_WAIT
)
887 reserve
= 5; /* Allow atomic callers to go up to five
888 entries over the normal backlog limit */
890 while (audit_backlog_limit
891 && skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
+ reserve
) {
892 if (gfp_mask
& __GFP_WAIT
&& audit_backlog_wait_time
893 && time_before(jiffies
, timeout_start
+ audit_backlog_wait_time
)) {
895 /* Wait for auditd to drain the queue a little */
896 DECLARE_WAITQUEUE(wait
, current
);
897 set_current_state(TASK_INTERRUPTIBLE
);
898 add_wait_queue(&audit_backlog_wait
, &wait
);
900 if (audit_backlog_limit
&&
901 skb_queue_len(&audit_skb_queue
) > audit_backlog_limit
)
902 schedule_timeout(timeout_start
+ audit_backlog_wait_time
- jiffies
);
904 __set_current_state(TASK_RUNNING
);
905 remove_wait_queue(&audit_backlog_wait
, &wait
);
908 if (audit_rate_check())
910 "audit: audit_backlog=%d > "
911 "audit_backlog_limit=%d\n",
912 skb_queue_len(&audit_skb_queue
),
913 audit_backlog_limit
);
914 audit_log_lost("backlog limit exceeded");
915 audit_backlog_wait_time
= audit_backlog_wait_overflow
;
916 wake_up(&audit_backlog_wait
);
920 ab
= audit_buffer_alloc(ctx
, gfp_mask
, type
);
922 audit_log_lost("out of memory in audit_log_start");
926 audit_get_stamp(ab
->ctx
, &t
, &serial
);
928 audit_log_format(ab
, "audit(%lu.%03lu:%u): ",
929 t
.tv_sec
, t
.tv_nsec
/1000000, serial
);
934 * audit_expand - expand skb in the audit buffer
936 * @extra: space to add at tail of the skb
938 * Returns 0 (no space) on failed expansion, or available space if
941 static inline int audit_expand(struct audit_buffer
*ab
, int extra
)
943 struct sk_buff
*skb
= ab
->skb
;
944 int ret
= pskb_expand_head(skb
, skb_headroom(skb
), extra
,
947 audit_log_lost("out of memory in audit_expand");
950 return skb_tailroom(skb
);
954 * Format an audit message into the audit buffer. If there isn't enough
955 * room in the audit buffer, more room will be allocated and vsnprint
956 * will be called a second time. Currently, we assume that a printk
957 * can't format message larger than 1024 bytes, so we don't either.
959 static void audit_log_vformat(struct audit_buffer
*ab
, const char *fmt
,
971 avail
= skb_tailroom(skb
);
973 avail
= audit_expand(ab
, AUDIT_BUFSIZ
);
977 va_copy(args2
, args
);
978 len
= vsnprintf(skb
->tail
, avail
, fmt
, args
);
980 /* The printk buffer is 1024 bytes long, so if we get
981 * here and AUDIT_BUFSIZ is at least 1024, then we can
982 * log everything that printk could have logged. */
983 avail
= audit_expand(ab
,
984 max_t(unsigned, AUDIT_BUFSIZ
, 1+len
-avail
));
987 len
= vsnprintf(skb
->tail
, avail
, fmt
, args2
);
996 * audit_log_format - format a message into the audit buffer.
998 * @fmt: format string
999 * @...: optional parameters matching @fmt string
1001 * All the work is done in audit_log_vformat.
1003 void audit_log_format(struct audit_buffer
*ab
, const char *fmt
, ...)
1009 va_start(args
, fmt
);
1010 audit_log_vformat(ab
, fmt
, args
);
1015 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1016 * @ab: the audit_buffer
1017 * @buf: buffer to convert to hex
1018 * @len: length of @buf to be converted
1020 * No return value; failure to expand is silently ignored.
1022 * This function will take the passed buf and convert it into a string of
1023 * ascii hex digits. The new string is placed onto the skb.
1025 void audit_log_hex(struct audit_buffer
*ab
, const unsigned char *buf
,
1028 int i
, avail
, new_len
;
1030 struct sk_buff
*skb
;
1031 static const unsigned char *hex
= "0123456789ABCDEF";
1035 avail
= skb_tailroom(skb
);
1037 if (new_len
>= avail
) {
1038 /* Round the buffer request up to the next multiple */
1039 new_len
= AUDIT_BUFSIZ
*(((new_len
-avail
)/AUDIT_BUFSIZ
) + 1);
1040 avail
= audit_expand(ab
, new_len
);
1046 for (i
=0; i
<len
; i
++) {
1047 *ptr
++ = hex
[(buf
[i
] & 0xF0)>>4]; /* Upper nibble */
1048 *ptr
++ = hex
[buf
[i
] & 0x0F]; /* Lower nibble */
1051 skb_put(skb
, len
<< 1); /* new string is twice the old string */
1055 * Format a string of no more than slen characters into the audit buffer,
1056 * enclosed in quote marks.
1058 static void audit_log_n_string(struct audit_buffer
*ab
, size_t slen
,
1063 struct sk_buff
*skb
;
1067 avail
= skb_tailroom(skb
);
1068 new_len
= slen
+ 3; /* enclosing quotes + null terminator */
1069 if (new_len
> avail
) {
1070 avail
= audit_expand(ab
, new_len
);
1076 memcpy(ptr
, string
, slen
);
1080 skb_put(skb
, slen
+ 2); /* don't include null terminator */
1084 * audit_log_n_unstrustedstring - log a string that may contain random characters
1086 * @len: lenth of string (not including trailing null)
1087 * @string: string to be logged
1089 * This code will escape a string that is passed to it if the string
1090 * contains a control character, unprintable character, double quote mark,
1091 * or a space. Unescaped strings will start and end with a double quote mark.
1092 * Strings that are escaped are printed in hex (2 digits per char).
1094 * The caller specifies the number of characters in the string to log, which may
1095 * or may not be the entire string.
1097 const char *audit_log_n_untrustedstring(struct audit_buffer
*ab
, size_t len
,
1100 const unsigned char *p
= string
;
1103 if (*p
== '"' || *p
< 0x21 || *p
> 0x7f) {
1104 audit_log_hex(ab
, string
, len
);
1105 return string
+ len
+ 1;
1109 audit_log_n_string(ab
, len
, string
);
1114 * audit_log_unstrustedstring - log a string that may contain random characters
1116 * @string: string to be logged
1118 * Same as audit_log_n_unstrustedstring(), except that strlen is used to
1119 * determine string length.
1121 const char *audit_log_untrustedstring(struct audit_buffer
*ab
, const char *string
)
1123 return audit_log_n_untrustedstring(ab
, strlen(string
), string
);
1126 /* This is a helper-function to print the escaped d_path */
1127 void audit_log_d_path(struct audit_buffer
*ab
, const char *prefix
,
1128 struct dentry
*dentry
, struct vfsmount
*vfsmnt
)
1133 audit_log_format(ab
, " %s", prefix
);
1135 /* We will allow 11 spaces for ' (deleted)' to be appended */
1136 path
= kmalloc(PATH_MAX
+11, ab
->gfp_mask
);
1138 audit_log_format(ab
, "<no memory>");
1141 p
= d_path(dentry
, vfsmnt
, path
, PATH_MAX
+11);
1142 if (IS_ERR(p
)) { /* Should never happen since we send PATH_MAX */
1143 /* FIXME: can we save some information here? */
1144 audit_log_format(ab
, "<too long>");
1146 audit_log_untrustedstring(ab
, p
);
1151 * audit_log_end - end one audit record
1152 * @ab: the audit_buffer
1154 * The netlink_* functions cannot be called inside an irq context, so
1155 * the audit buffer is placed on a queue and a tasklet is scheduled to
1156 * remove them from the queue outside the irq context. May be called in
1159 void audit_log_end(struct audit_buffer
*ab
)
1163 if (!audit_rate_check()) {
1164 audit_log_lost("rate limit exceeded");
1167 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)ab
->skb
->data
;
1168 nlh
->nlmsg_len
= ab
->skb
->len
- NLMSG_SPACE(0);
1169 skb_queue_tail(&audit_skb_queue
, ab
->skb
);
1171 wake_up_interruptible(&kauditd_wait
);
1173 printk(KERN_NOTICE
"%s\n", ab
->skb
->data
+ NLMSG_SPACE(0));
1176 audit_buffer_free(ab
);
1180 * audit_log - Log an audit record
1181 * @ctx: audit context
1182 * @gfp_mask: type of allocation
1183 * @type: audit message type
1184 * @fmt: format string to use
1185 * @...: variable parameters matching the format string
1187 * This is a convenience function that calls audit_log_start,
1188 * audit_log_vformat, and audit_log_end. It may be called
1191 void audit_log(struct audit_context
*ctx
, gfp_t gfp_mask
, int type
,
1192 const char *fmt
, ...)
1194 struct audit_buffer
*ab
;
1197 ab
= audit_log_start(ctx
, gfp_mask
, type
);
1199 va_start(args
, fmt
);
1200 audit_log_vformat(ab
, fmt
, args
);
1206 EXPORT_SYMBOL(audit_log_start
);
1207 EXPORT_SYMBOL(audit_log_end
);
1208 EXPORT_SYMBOL(audit_log_format
);
1209 EXPORT_SYMBOL(audit_log
);