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.
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/faith/audit/
44 #include <linux/init.h>
45 #include <asm/atomic.h>
46 #include <asm/types.h>
48 #include <linux/module.h>
50 #include <linux/audit.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. */
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. */
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 DEFINE_SPINLOCK(audit_txlist_lock
);
102 static DEFINE_SPINLOCK(audit_freelist_lock
);
103 static int audit_freelist_count
= 0;
104 static LIST_HEAD(audit_txlist
);
105 static LIST_HEAD(audit_freelist
);
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
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
;
145 int count
; /* Times requeued */
148 void audit_set_type(struct audit_buffer
*ab
, int type
)
154 struct list_head list
;
155 struct audit_rule rule
;
158 static void audit_log_end_irq(struct audit_buffer
*ab
);
159 static void audit_log_end_fast(struct audit_buffer
*ab
);
161 static void audit_panic(const char *message
)
163 switch (audit_failure
)
165 case AUDIT_FAIL_SILENT
:
167 case AUDIT_FAIL_PRINTK
:
168 printk(KERN_ERR
"audit: %s\n", message
);
170 case AUDIT_FAIL_PANIC
:
171 panic("audit: %s\n", message
);
176 static inline int audit_rate_check(void)
178 static unsigned long last_check
= 0;
179 static int messages
= 0;
180 static DEFINE_SPINLOCK(lock
);
183 unsigned long elapsed
;
186 if (!audit_rate_limit
) return 1;
188 spin_lock_irqsave(&lock
, flags
);
189 if (++messages
< audit_rate_limit
) {
193 elapsed
= now
- last_check
;
200 spin_unlock_irqrestore(&lock
, flags
);
205 /* Emit at least 1 message per second, even if audit_rate_check is
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_backlog=%d"
232 " audit_rate_limit=%d audit_backlog_limit=%d\n",
233 atomic_read(&audit_lost
),
234 atomic_read(&audit_backlog
),
236 audit_backlog_limit
);
237 audit_panic(message
);
242 static int audit_set_rate_limit(int limit
)
244 int old
= audit_rate_limit
;
245 audit_rate_limit
= limit
;
246 audit_log(current
->audit_context
, "audit_rate_limit=%d old=%d",
247 audit_rate_limit
, old
);
251 static int audit_set_backlog_limit(int limit
)
253 int old
= audit_backlog_limit
;
254 audit_backlog_limit
= limit
;
255 audit_log(current
->audit_context
, "audit_backlog_limit=%d old=%d",
256 audit_backlog_limit
, old
);
260 static int audit_set_enabled(int state
)
262 int old
= audit_enabled
;
263 if (state
!= 0 && state
!= 1)
265 audit_enabled
= state
;
266 audit_log(current
->audit_context
, "audit_enabled=%d old=%d",
271 static int audit_set_failure(int state
)
273 int old
= audit_failure
;
274 if (state
!= AUDIT_FAIL_SILENT
275 && state
!= AUDIT_FAIL_PRINTK
276 && state
!= AUDIT_FAIL_PANIC
)
278 audit_failure
= state
;
279 audit_log(current
->audit_context
, "audit_failure=%d old=%d",
285 void audit_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
286 void *payload
, int size
)
289 struct nlmsghdr
*nlh
;
290 int len
= NLMSG_SPACE(size
);
292 int flags
= multi
? NLM_F_MULTI
: 0;
293 int t
= done
? NLMSG_DONE
: type
;
295 skb
= alloc_skb(len
, GFP_KERNEL
);
299 nlh
= NLMSG_PUT(skb
, pid
, seq
, t
, len
- sizeof(*nlh
));
300 nlh
->nlmsg_flags
= flags
;
301 data
= NLMSG_DATA(nlh
);
302 memcpy(data
, payload
, size
);
303 netlink_unicast(audit_sock
, skb
, pid
, MSG_DONTWAIT
);
306 nlmsg_failure
: /* Used by NLMSG_PUT */
312 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
315 static int audit_netlink_ok(kernel_cap_t eff_cap
, u16 msg_type
)
325 if (!cap_raised(eff_cap
, CAP_AUDIT_CONTROL
))
329 if (!cap_raised(eff_cap
, CAP_AUDIT_WRITE
))
332 default: /* bad msg */
339 static int audit_receive_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
343 struct audit_status
*status_get
, status_set
;
345 struct audit_buffer
*ab
;
346 u16 msg_type
= nlh
->nlmsg_type
;
348 err
= audit_netlink_ok(NETLINK_CB(skb
).eff_cap
, msg_type
);
352 pid
= NETLINK_CREDS(skb
)->pid
;
353 uid
= NETLINK_CREDS(skb
)->uid
;
354 seq
= nlh
->nlmsg_seq
;
355 data
= NLMSG_DATA(nlh
);
359 status_set
.enabled
= audit_enabled
;
360 status_set
.failure
= audit_failure
;
361 status_set
.pid
= audit_pid
;
362 status_set
.rate_limit
= audit_rate_limit
;
363 status_set
.backlog_limit
= audit_backlog_limit
;
364 status_set
.lost
= atomic_read(&audit_lost
);
365 status_set
.backlog
= atomic_read(&audit_backlog
);
366 audit_send_reply(NETLINK_CB(skb
).pid
, seq
, AUDIT_GET
, 0, 0,
367 &status_set
, sizeof(status_set
));
370 if (nlh
->nlmsg_len
< sizeof(struct audit_status
))
372 status_get
= (struct audit_status
*)data
;
373 if (status_get
->mask
& AUDIT_STATUS_ENABLED
) {
374 err
= audit_set_enabled(status_get
->enabled
);
375 if (err
< 0) return err
;
377 if (status_get
->mask
& AUDIT_STATUS_FAILURE
) {
378 err
= audit_set_failure(status_get
->failure
);
379 if (err
< 0) return err
;
381 if (status_get
->mask
& AUDIT_STATUS_PID
) {
383 audit_pid
= status_get
->pid
;
384 audit_log(current
->audit_context
,
385 "audit_pid=%d old=%d", audit_pid
, old
);
387 if (status_get
->mask
& AUDIT_STATUS_RATE_LIMIT
)
388 audit_set_rate_limit(status_get
->rate_limit
);
389 if (status_get
->mask
& AUDIT_STATUS_BACKLOG_LIMIT
)
390 audit_set_backlog_limit(status_get
->backlog_limit
);
393 ab
= audit_log_start(NULL
);
395 break; /* audit_panic has been called */
397 "user pid=%d uid=%d length=%d msg='%.1024s'",
400 - ((char *)data
- (char *)nlh
)),
402 ab
->type
= AUDIT_USER
;
408 if (nlh
->nlmsg_len
< sizeof(struct audit_rule
))
412 #ifdef CONFIG_AUDITSYSCALL
413 err
= audit_receive_filter(nlh
->nlmsg_type
, NETLINK_CB(skb
).pid
,
424 return err
< 0 ? err
: 0;
427 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
428 * processed by audit_receive_msg. Malformed skbs with wrong length are
429 * discarded silently. */
430 static int audit_receive_skb(struct sk_buff
*skb
)
433 struct nlmsghdr
*nlh
;
436 while (skb
->len
>= NLMSG_SPACE(0)) {
437 nlh
= (struct nlmsghdr
*)skb
->data
;
438 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
440 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
443 if ((err
= audit_receive_msg(skb
, nlh
))) {
444 netlink_ack(skb
, nlh
, err
);
445 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
446 netlink_ack(skb
, nlh
, 0);
452 /* Receive messages from netlink socket. */
453 static void audit_receive(struct sock
*sk
, int length
)
457 if (down_trylock(&audit_netlink_sem
))
460 /* FIXME: this must not cause starvation */
461 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
462 if (audit_receive_skb(skb
) && skb
->len
)
463 skb_queue_head(&sk
->sk_receive_queue
, skb
);
467 up(&audit_netlink_sem
);
470 /* Move data from tmp buffer into an skb. This is an extra copy, and
471 * that is unfortunate. However, the copy will only occur when a record
472 * is being written to user space, which is already a high-overhead
473 * operation. (Elimination of the copy is possible, for example, by
474 * writing directly into a pre-allocated skb, at the cost of wasting
476 static void audit_log_move(struct audit_buffer
*ab
)
480 int extra
= ab
->nlh
? 0 : NLMSG_SPACE(0);
482 /* possible resubmission */
486 skb
= skb_peek(&ab
->sklist
);
487 if (!skb
|| skb_tailroom(skb
) <= ab
->len
+ extra
) {
488 skb
= alloc_skb(2 * ab
->len
+ extra
, GFP_ATOMIC
);
490 ab
->len
= 0; /* Lose information in ab->tmp */
491 audit_log_lost("out of memory in audit_log_move");
494 __skb_queue_tail(&ab
->sklist
, skb
);
496 ab
->nlh
= (struct nlmsghdr
*)skb_put(skb
,
499 start
= skb_put(skb
, ab
->len
);
500 memcpy(start
, ab
->tmp
, ab
->len
);
504 /* Iterate over the skbuff in the audit_buffer, sending their contents
506 static inline int audit_log_drain(struct audit_buffer
*ab
)
510 while ((skb
= skb_dequeue(&ab
->sklist
))) {
515 ab
->nlh
->nlmsg_len
= ab
->total
;
516 ab
->nlh
->nlmsg_type
= ab
->type
;
517 ab
->nlh
->nlmsg_flags
= 0;
518 ab
->nlh
->nlmsg_seq
= 0;
519 ab
->nlh
->nlmsg_pid
= ab
->pid
;
521 skb_get(skb
); /* because netlink_* frees */
522 retval
= netlink_unicast(audit_sock
, skb
, audit_pid
,
525 if (retval
== -EAGAIN
&& ab
->count
< 5) {
527 skb_queue_tail(&ab
->sklist
, skb
);
528 audit_log_end_irq(ab
);
532 if (retval
== -ECONNREFUSED
) {
534 "audit: *NO* daemon at audit_pid=%d\n",
538 audit_log_lost("netlink socket too busy");
540 if (!audit_pid
) { /* No daemon */
541 int offset
= ab
->nlh
? NLMSG_SPACE(0) : 0;
542 int len
= skb
->len
- offset
;
543 printk(KERN_ERR
"%*.*s\n",
544 len
, len
, skb
->data
+ offset
);
552 /* Initialize audit support at boot time. */
553 static int __init
audit_init(void)
555 printk(KERN_INFO
"audit: initializing netlink socket (%s)\n",
556 audit_default
? "enabled" : "disabled");
557 audit_sock
= netlink_kernel_create(NETLINK_AUDIT
, audit_receive
);
559 audit_panic("cannot initialize netlink socket");
561 audit_initialized
= 1;
562 audit_enabled
= audit_default
;
563 audit_log(NULL
, "initialized");
568 /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
570 static void audit_log_move(struct audit_buffer
*ab
)
572 printk(KERN_ERR
"%*.*s\n", ab
->len
, ab
->len
, ab
->tmp
);
576 static inline int audit_log_drain(struct audit_buffer
*ab
)
581 /* Initialize audit support at boot time. */
582 int __init
audit_init(void)
584 printk(KERN_INFO
"audit: initializing WITHOUT netlink support\n");
588 audit_initialized
= 1;
589 audit_enabled
= audit_default
;
590 audit_log(NULL
, "initialized");
595 __initcall(audit_init
);
597 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
598 static int __init
audit_enable(char *str
)
600 audit_default
= !!simple_strtol(str
, NULL
, 0);
601 printk(KERN_INFO
"audit: %s%s\n",
602 audit_default
? "enabled" : "disabled",
603 audit_initialized
? "" : " (after initialization)");
604 if (audit_initialized
)
605 audit_enabled
= audit_default
;
609 __setup("audit=", audit_enable
);
612 /* Obtain an audit buffer. This routine does locking to obtain the
613 * audit buffer, but then no locking is required for calls to
614 * audit_log_*format. If the tsk is a task that is currently in a
615 * syscall, then the syscall is marked as auditable and an audit record
616 * will be written at syscall exit. If there is no associated task, tsk
618 struct audit_buffer
*audit_log_start(struct audit_context
*ctx
)
620 struct audit_buffer
*ab
= NULL
;
625 if (!audit_initialized
)
628 if (audit_backlog_limit
629 && atomic_read(&audit_backlog
) > audit_backlog_limit
) {
630 if (audit_rate_check())
632 "audit: audit_backlog=%d > "
633 "audit_backlog_limit=%d\n",
634 atomic_read(&audit_backlog
),
635 audit_backlog_limit
);
636 audit_log_lost("backlog limit exceeded");
640 spin_lock_irqsave(&audit_freelist_lock
, flags
);
641 if (!list_empty(&audit_freelist
)) {
642 ab
= list_entry(audit_freelist
.next
,
643 struct audit_buffer
, list
);
645 --audit_freelist_count
;
647 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
650 ab
= kmalloc(sizeof(*ab
), GFP_ATOMIC
);
652 audit_log_lost("out of memory in audit_log_start");
656 atomic_inc(&audit_backlog
);
657 skb_queue_head_init(&ab
->sklist
);
663 ab
->type
= AUDIT_KERNEL
;
667 #ifdef CONFIG_AUDITSYSCALL
669 audit_get_stamp(ab
->ctx
, &t
, &serial
);
674 audit_log_format(ab
, "audit(%lu.%03lu:%u): ",
675 t
.tv_sec
, t
.tv_nsec
/1000000, serial
);
680 /* Format an audit message into the audit buffer. If there isn't enough
681 * room in the audit buffer, more room will be allocated and vsnprint
682 * will be called a second time. Currently, we assume that a printk
683 * can't format message larger than 1024 bytes, so we don't either. */
684 static void audit_log_vformat(struct audit_buffer
*ab
, const char *fmt
,
692 avail
= sizeof(ab
->tmp
) - ab
->len
;
695 avail
= sizeof(ab
->tmp
) - ab
->len
;
697 len
= vsnprintf(ab
->tmp
+ ab
->len
, avail
, fmt
, args
);
699 /* The printk buffer is 1024 bytes long, so if we get
700 * here and AUDIT_BUFSIZ is at least 1024, then we can
701 * log everything that printk could have logged. */
703 avail
= sizeof(ab
->tmp
) - ab
->len
;
704 len
= vsnprintf(ab
->tmp
+ ab
->len
, avail
, fmt
, args
);
706 ab
->len
+= (len
< avail
) ? len
: avail
;
707 ab
->total
+= (len
< avail
) ? len
: avail
;
710 /* Format a message into the audit buffer. All the work is done in
711 * audit_log_vformat. */
712 void audit_log_format(struct audit_buffer
*ab
, const char *fmt
, ...)
719 audit_log_vformat(ab
, fmt
, args
);
723 /* This is a helper-function to print the d_path without using a static
724 * buffer or allocating another buffer in addition to the one in
726 void audit_log_d_path(struct audit_buffer
*ab
, const char *prefix
,
727 struct dentry
*dentry
, struct vfsmount
*vfsmnt
)
732 if (prefix
) audit_log_format(ab
, " %s", prefix
);
736 avail
= sizeof(ab
->tmp
) - ab
->len
;
737 p
= d_path(dentry
, vfsmnt
, ab
->tmp
+ ab
->len
, avail
);
739 /* FIXME: can we save some information here? */
740 audit_log_format(ab
, "<toolong>");
742 /* path isn't at start of buffer */
743 len
= (ab
->tmp
+ sizeof(ab
->tmp
) - 1) - p
;
744 memmove(ab
->tmp
+ ab
->len
, p
, len
);
750 /* Remove queued messages from the audit_txlist and send them to userspace. */
751 static void audit_tasklet_handler(unsigned long arg
)
754 struct audit_buffer
*ab
;
757 spin_lock_irqsave(&audit_txlist_lock
, flags
);
758 list_splice_init(&audit_txlist
, &list
);
759 spin_unlock_irqrestore(&audit_txlist_lock
, flags
);
761 while (!list_empty(&list
)) {
762 ab
= list_entry(list
.next
, struct audit_buffer
, list
);
764 audit_log_end_fast(ab
);
768 static DECLARE_TASKLET(audit_tasklet
, audit_tasklet_handler
, 0);
770 /* The netlink_* functions cannot be called inside an irq context, so
771 * the audit buffer is places on a queue and a tasklet is scheduled to
772 * remove them from the queue outside the irq context. May be called in
774 static void audit_log_end_irq(struct audit_buffer
*ab
)
780 spin_lock_irqsave(&audit_txlist_lock
, flags
);
781 list_add_tail(&ab
->list
, &audit_txlist
);
782 spin_unlock_irqrestore(&audit_txlist_lock
, flags
);
784 tasklet_schedule(&audit_tasklet
);
787 /* Send the message in the audit buffer directly to user space. May not
788 * be called in an irq context. */
789 static void audit_log_end_fast(struct audit_buffer
*ab
)
796 if (!audit_rate_check()) {
797 audit_log_lost("rate limit exceeded");
800 if (audit_log_drain(ab
))
804 atomic_dec(&audit_backlog
);
805 spin_lock_irqsave(&audit_freelist_lock
, flags
);
806 if (++audit_freelist_count
> AUDIT_MAXFREE
)
809 list_add(&ab
->list
, &audit_freelist
);
810 spin_unlock_irqrestore(&audit_freelist_lock
, flags
);
813 /* Send or queue the message in the audit buffer, depending on the
814 * current context. (A convenience function that may be called in any
816 void audit_log_end(struct audit_buffer
*ab
)
819 audit_log_end_irq(ab
);
821 audit_log_end_fast(ab
);
824 /* Log an audit record. This is a convenience function that calls
825 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
826 * called in any context. */
827 void audit_log(struct audit_context
*ctx
, const char *fmt
, ...)
829 struct audit_buffer
*ab
;
832 ab
= audit_log_start(ctx
);
835 audit_log_vformat(ab
, fmt
, args
);