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>
59 /* No auditing will take place until audit_initialized != 0.
60 * (Initialization happens after skb_init is called.) */
61 static int audit_initialized;
63 /* No syscall auditing will take place unless audit_enabled != 0. */
66 /* Default state when kernel boots without any parameters. */
67 static int audit_default;
69 /* If auditing cannot proceed, audit_failure selects what happens. */
70 static int audit_failure = AUDIT_FAIL_PRINTK;
72 /* If audit records are to be written to the netlink socket, audit_pid
73 * contains the (non-zero) pid. */
76 /* If audit_rate_limit is non-zero, limit the rate of sending audit records
77 * to that number per second. This prevents DoS attacks, but results in
78 * audit records being dropped. */
79 static int audit_rate_limit;
81 /* Number of outstanding audit_buffers allowed. */
82 static int audit_backlog_limit = 64;
83 static int audit_backlog_wait_time = 60 * HZ;
84 static int audit_backlog_wait_overflow = 0;
86 /* The identity of the user shutting down the audit system. */
87 uid_t audit_sig_uid = -1;
88 pid_t audit_sig_pid = -1;
90 /* Records can be lost in several ways:
91 0) [suppressed in audit_alloc]
92 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
93 2) out of memory in audit_log_move [alloc_skb]
94 3) suppressed due to audit_rate_limit
95 4) suppressed due to audit_backlog_limit
97 static atomic_t audit_lost = ATOMIC_INIT(0);
99 /* The netlink socket. */
100 static struct sock *audit_sock;
102 /* The audit_freelist is a list of pre-allocated audit buffers (if more
103 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
104 * being placed on the freelist). */
105 static DEFINE_SPINLOCK(audit_freelist_lock);
106 static int audit_freelist_count;
107 static LIST_HEAD(audit_freelist);
109 static struct sk_buff_head audit_skb_queue;
110 static struct task_struct *kauditd_task;
111 static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
112 static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
114 /* The netlink socket is only to be read by 1 CPU, which lets us assume
115 * that list additions and deletions never happen simultaneously in
117 DEFINE_MUTEX(audit_netlink_mutex);
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 *skb; /* formatted skb ready to send */
136 struct audit_context *ctx; /* NULL or associated context */
140 static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
142 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
143 nlh->nlmsg_pid = pid;
146 void audit_panic(const char *message)
148 switch (audit_failure)
150 case AUDIT_FAIL_SILENT:
152 case AUDIT_FAIL_PRINTK:
153 printk(KERN_ERR "audit: %s\n", message);
155 case AUDIT_FAIL_PANIC:
156 panic("audit: %s\n", message);
161 static inline int audit_rate_check(void)
163 static unsigned long last_check = 0;
164 static int messages = 0;
165 static DEFINE_SPINLOCK(lock);
168 unsigned long elapsed;
171 if (!audit_rate_limit) return 1;
173 spin_lock_irqsave(&lock, flags);
174 if (++messages < audit_rate_limit) {
178 elapsed = now - last_check;
185 spin_unlock_irqrestore(&lock, flags);
191 * audit_log_lost - conditionally log lost audit message event
192 * @message: the message stating reason for lost audit message
194 * Emit at least 1 message per second, even if audit_rate_check is
196 * Always increment the lost messages counter.
198 void audit_log_lost(const char *message)
200 static unsigned long last_msg = 0;
201 static DEFINE_SPINLOCK(lock);
206 atomic_inc(&audit_lost);
208 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
211 spin_lock_irqsave(&lock, flags);
213 if (now - last_msg > HZ) {
217 spin_unlock_irqrestore(&lock, flags);
222 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
223 atomic_read(&audit_lost),
225 audit_backlog_limit);
226 audit_panic(message);
230 static int audit_set_rate_limit(int limit, uid_t loginuid)
232 int old = audit_rate_limit;
233 audit_rate_limit = limit;
234 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
235 "audit_rate_limit=%d old=%d by auid=%u",
236 audit_rate_limit, old, loginuid);
240 static int audit_set_backlog_limit(int limit, uid_t loginuid)
242 int old = audit_backlog_limit;
243 audit_backlog_limit = limit;
244 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
245 "audit_backlog_limit=%d old=%d by auid=%u",
246 audit_backlog_limit, old, loginuid);
250 static int audit_set_enabled(int state, uid_t loginuid)
252 int old = audit_enabled;
253 if (state != 0 && state != 1)
255 audit_enabled = state;
256 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
257 "audit_enabled=%d old=%d by auid=%u",
258 audit_enabled, old, loginuid);
262 static int audit_set_failure(int state, uid_t loginuid)
264 int old = audit_failure;
265 if (state != AUDIT_FAIL_SILENT
266 && state != AUDIT_FAIL_PRINTK
267 && state != AUDIT_FAIL_PANIC)
269 audit_failure = state;
270 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
271 "audit_failure=%d old=%d by auid=%u",
272 audit_failure, old, loginuid);
276 static int kauditd_thread(void *dummy)
281 skb = skb_dequeue(&audit_skb_queue);
282 wake_up(&audit_backlog_wait);
285 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
287 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
288 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
292 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
296 DECLARE_WAITQUEUE(wait, current);
297 set_current_state(TASK_INTERRUPTIBLE);
298 add_wait_queue(&kauditd_wait, &wait);
300 if (!skb_queue_len(&audit_skb_queue)) {
305 __set_current_state(TASK_RUNNING);
306 remove_wait_queue(&kauditd_wait, &wait);
313 * audit_send_reply - send an audit reply message via netlink
314 * @pid: process id to send reply to
315 * @seq: sequence number
316 * @type: audit message type
317 * @done: done (last) flag
318 * @multi: multi-part message flag
319 * @payload: payload data
320 * @size: payload size
322 * Allocates an skb, builds the netlink message, and sends it to the pid.
323 * No failure notifications.
325 void audit_send_reply(int pid, int seq, int type, int done, int multi,
326 void *payload, int size)
329 struct nlmsghdr *nlh;
330 int len = NLMSG_SPACE(size);
332 int flags = multi ? NLM_F_MULTI : 0;
333 int t = done ? NLMSG_DONE : type;
335 skb = alloc_skb(len, GFP_KERNEL);
339 nlh = NLMSG_PUT(skb, pid, seq, t, size);
340 nlh->nlmsg_flags = flags;
341 data = NLMSG_DATA(nlh);
342 memcpy(data, payload, size);
344 /* Ignore failure. It'll only happen if the sender goes away,
345 because our timeout is set to infinite. */
346 netlink_unicast(audit_sock, skb, pid, 0);
349 nlmsg_failure: /* Used by NLMSG_PUT */
355 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
358 static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
365 case AUDIT_LIST_RULES:
371 case AUDIT_SIGNAL_INFO:
372 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
376 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
377 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
378 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
381 default: /* bad msg */
388 static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
392 struct audit_status *status_get, status_set;
394 struct audit_buffer *ab;
395 u16 msg_type = nlh->nlmsg_type;
396 uid_t loginuid; /* loginuid of sender */
397 struct audit_sig_info sig_data;
399 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
403 /* As soon as there's any sign of userspace auditd,
404 * start kauditd to talk to it */
406 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
407 if (IS_ERR(kauditd_task)) {
408 err = PTR_ERR(kauditd_task);
413 pid = NETLINK_CREDS(skb)->pid;
414 uid = NETLINK_CREDS(skb)->uid;
415 loginuid = NETLINK_CB(skb).loginuid;
416 seq = nlh->nlmsg_seq;
417 data = NLMSG_DATA(nlh);
421 status_set.enabled = audit_enabled;
422 status_set.failure = audit_failure;
423 status_set.pid = audit_pid;
424 status_set.rate_limit = audit_rate_limit;
425 status_set.backlog_limit = audit_backlog_limit;
426 status_set.lost = atomic_read(&audit_lost);
427 status_set.backlog = skb_queue_len(&audit_skb_queue);
428 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
429 &status_set, sizeof(status_set));
432 if (nlh->nlmsg_len < sizeof(struct audit_status))
434 status_get = (struct audit_status *)data;
435 if (status_get->mask & AUDIT_STATUS_ENABLED) {
436 err = audit_set_enabled(status_get->enabled, loginuid);
437 if (err < 0) return err;
439 if (status_get->mask & AUDIT_STATUS_FAILURE) {
440 err = audit_set_failure(status_get->failure, loginuid);
441 if (err < 0) return err;
443 if (status_get->mask & AUDIT_STATUS_PID) {
445 audit_pid = status_get->pid;
446 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
447 "audit_pid=%d old=%d by auid=%u",
448 audit_pid, old, loginuid);
450 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
451 audit_set_rate_limit(status_get->rate_limit, loginuid);
452 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
453 audit_set_backlog_limit(status_get->backlog_limit,
457 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
458 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
459 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
462 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
465 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
468 "user pid=%d uid=%u auid=%u msg='%.1024s'",
469 pid, uid, loginuid, (char *)data);
470 audit_set_pid(ab, pid);
477 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
481 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
482 uid, seq, data, nlmsg_len(nlh),
487 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
490 case AUDIT_LIST_RULES:
491 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
492 uid, seq, data, nlmsg_len(nlh),
495 case AUDIT_SIGNAL_INFO:
496 sig_data.uid = audit_sig_uid;
497 sig_data.pid = audit_sig_pid;
498 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
499 0, 0, &sig_data, sizeof(sig_data));
506 return err < 0 ? err : 0;
510 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
511 * processed by audit_receive_msg. Malformed skbs with wrong length are
512 * discarded silently.
514 static void audit_receive_skb(struct sk_buff *skb)
517 struct nlmsghdr *nlh;
520 while (skb->len >= NLMSG_SPACE(0)) {
521 nlh = (struct nlmsghdr *)skb->data;
522 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
524 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
527 if ((err = audit_receive_msg(skb, nlh))) {
528 netlink_ack(skb, nlh, err);
529 } else if (nlh->nlmsg_flags & NLM_F_ACK)
530 netlink_ack(skb, nlh, 0);
535 /* Receive messages from netlink socket. */
536 static void audit_receive(struct sock *sk, int length)
541 mutex_lock(&audit_netlink_mutex);
543 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544 skb = skb_dequeue(&sk->sk_receive_queue);
545 audit_receive_skb(skb);
548 mutex_unlock(&audit_netlink_mutex);
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, 0, audit_receive,
560 audit_panic("cannot initialize netlink socket");
562 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
564 skb_queue_head_init(&audit_skb_queue);
565 audit_initialized = 1;
566 audit_enabled = audit_default;
567 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
570 __initcall(audit_init);
572 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
573 static int __init audit_enable(char *str)
575 audit_default = !!simple_strtol(str, NULL, 0);
576 printk(KERN_INFO "audit: %s%s\n",
577 audit_default ? "enabled" : "disabled",
578 audit_initialized ? "" : " (after initialization)");
579 if (audit_initialized)
580 audit_enabled = audit_default;
584 __setup("audit=", audit_enable);
586 static void audit_buffer_free(struct audit_buffer *ab)
596 spin_lock_irqsave(&audit_freelist_lock, flags);
597 if (++audit_freelist_count > AUDIT_MAXFREE)
600 list_add(&ab->list, &audit_freelist);
601 spin_unlock_irqrestore(&audit_freelist_lock, flags);
604 static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
605 gfp_t gfp_mask, int type)
608 struct audit_buffer *ab = NULL;
609 struct nlmsghdr *nlh;
611 spin_lock_irqsave(&audit_freelist_lock, flags);
612 if (!list_empty(&audit_freelist)) {
613 ab = list_entry(audit_freelist.next,
614 struct audit_buffer, list);
616 --audit_freelist_count;
618 spin_unlock_irqrestore(&audit_freelist_lock, flags);
621 ab = kmalloc(sizeof(*ab), gfp_mask);
626 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
631 ab->gfp_mask = gfp_mask;
632 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
633 nlh->nlmsg_type = type;
634 nlh->nlmsg_flags = 0;
639 audit_buffer_free(ab);
644 * audit_serial - compute a serial number for the audit record
646 * Compute a serial number for the audit record. Audit records are
647 * written to user-space as soon as they are generated, so a complete
648 * audit record may be written in several pieces. The timestamp of the
649 * record and this serial number are used by the user-space tools to
650 * determine which pieces belong to the same audit record. The
651 * (timestamp,serial) tuple is unique for each syscall and is live from
652 * syscall entry to syscall exit.
654 * NOTE: Another possibility is to store the formatted records off the
655 * audit context (for those records that have a context), and emit them
656 * all at syscall exit. However, this could delay the reporting of
657 * significant errors until syscall exit (or never, if the system
660 unsigned int audit_serial(void)
662 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
663 static unsigned int serial = 0;
668 spin_lock_irqsave(&serial_lock, flags);
671 } while (unlikely(!ret));
672 spin_unlock_irqrestore(&serial_lock, flags);
677 static inline void audit_get_stamp(struct audit_context *ctx,
678 struct timespec *t, unsigned int *serial)
681 auditsc_get_stamp(ctx, t, serial);
684 *serial = audit_serial();
688 /* Obtain an audit buffer. This routine does locking to obtain the
689 * audit buffer, but then no locking is required for calls to
690 * audit_log_*format. If the tsk is a task that is currently in a
691 * syscall, then the syscall is marked as auditable and an audit record
692 * will be written at syscall exit. If there is no associated task, tsk
696 * audit_log_start - obtain an audit buffer
697 * @ctx: audit_context (may be NULL)
698 * @gfp_mask: type of allocation
699 * @type: audit message type
701 * Returns audit_buffer pointer on success or NULL on error.
703 * Obtain an audit buffer. This routine does locking to obtain the
704 * audit buffer, but then no locking is required for calls to
705 * audit_log_*format. If the task (ctx) is a task that is currently in a
706 * syscall, then the syscall is marked as auditable and an audit record
707 * will be written at syscall exit. If there is no associated task, then
708 * task context (ctx) should be NULL.
710 struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
713 struct audit_buffer *ab = NULL;
717 unsigned long timeout_start = jiffies;
719 if (!audit_initialized)
722 if (unlikely(audit_filter_type(type)))
725 if (gfp_mask & __GFP_WAIT)
728 reserve = 5; /* Allow atomic callers to go up to five
729 entries over the normal backlog limit */
731 while (audit_backlog_limit
732 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
733 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
734 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
736 /* Wait for auditd to drain the queue a little */
737 DECLARE_WAITQUEUE(wait, current);
738 set_current_state(TASK_INTERRUPTIBLE);
739 add_wait_queue(&audit_backlog_wait, &wait);
741 if (audit_backlog_limit &&
742 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
743 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
745 __set_current_state(TASK_RUNNING);
746 remove_wait_queue(&audit_backlog_wait, &wait);
749 if (audit_rate_check())
751 "audit: audit_backlog=%d > "
752 "audit_backlog_limit=%d\n",
753 skb_queue_len(&audit_skb_queue),
754 audit_backlog_limit);
755 audit_log_lost("backlog limit exceeded");
756 audit_backlog_wait_time = audit_backlog_wait_overflow;
757 wake_up(&audit_backlog_wait);
761 ab = audit_buffer_alloc(ctx, gfp_mask, type);
763 audit_log_lost("out of memory in audit_log_start");
767 audit_get_stamp(ab->ctx, &t, &serial);
769 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
770 t.tv_sec, t.tv_nsec/1000000, serial);
775 * audit_expand - expand skb in the audit buffer
777 * @extra: space to add at tail of the skb
779 * Returns 0 (no space) on failed expansion, or available space if
782 static inline int audit_expand(struct audit_buffer *ab, int extra)
784 struct sk_buff *skb = ab->skb;
785 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
788 audit_log_lost("out of memory in audit_expand");
791 return skb_tailroom(skb);
795 * Format an audit message into the audit buffer. If there isn't enough
796 * room in the audit buffer, more room will be allocated and vsnprint
797 * will be called a second time. Currently, we assume that a printk
798 * can't format message larger than 1024 bytes, so we don't either.
800 static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
812 avail = skb_tailroom(skb);
814 avail = audit_expand(ab, AUDIT_BUFSIZ);
818 va_copy(args2, args);
819 len = vsnprintf(skb->tail, avail, fmt, args);
821 /* The printk buffer is 1024 bytes long, so if we get
822 * here and AUDIT_BUFSIZ is at least 1024, then we can
823 * log everything that printk could have logged. */
824 avail = audit_expand(ab,
825 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
828 len = vsnprintf(skb->tail, avail, fmt, args2);
837 * audit_log_format - format a message into the audit buffer.
839 * @fmt: format string
840 * @...: optional parameters matching @fmt string
842 * All the work is done in audit_log_vformat.
844 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
851 audit_log_vformat(ab, fmt, args);
856 * audit_log_hex - convert a buffer to hex and append it to the audit skb
857 * @ab: the audit_buffer
858 * @buf: buffer to convert to hex
859 * @len: length of @buf to be converted
861 * No return value; failure to expand is silently ignored.
863 * This function will take the passed buf and convert it into a string of
864 * ascii hex digits. The new string is placed onto the skb.
866 void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
869 int i, avail, new_len;
872 static const unsigned char *hex = "0123456789ABCDEF";
876 avail = skb_tailroom(skb);
878 if (new_len >= avail) {
879 /* Round the buffer request up to the next multiple */
880 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
881 avail = audit_expand(ab, new_len);
887 for (i=0; i<len; i++) {
888 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
889 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
892 skb_put(skb, len << 1); /* new string is twice the old string */
896 * audit_log_unstrustedstring - log a string that may contain random characters
898 * @string: string to be logged
900 * This code will escape a string that is passed to it if the string
901 * contains a control character, unprintable character, double quote mark,
902 * or a space. Unescaped strings will start and end with a double quote mark.
903 * Strings that are escaped are printed in hex (2 digits per char).
905 void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
907 const unsigned char *p = string;
910 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
911 audit_log_hex(ab, string, strlen(string));
916 audit_log_format(ab, "\"%s\"", string);
919 /* This is a helper-function to print the escaped d_path */
920 void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
921 struct dentry *dentry, struct vfsmount *vfsmnt)
926 audit_log_format(ab, " %s", prefix);
928 /* We will allow 11 spaces for ' (deleted)' to be appended */
929 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
931 audit_log_format(ab, "<no memory>");
934 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
935 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
936 /* FIXME: can we save some information here? */
937 audit_log_format(ab, "<too long>");
939 audit_log_untrustedstring(ab, p);
944 * audit_log_end - end one audit record
945 * @ab: the audit_buffer
947 * The netlink_* functions cannot be called inside an irq context, so
948 * the audit buffer is placed on a queue and a tasklet is scheduled to
949 * remove them from the queue outside the irq context. May be called in
952 void audit_log_end(struct audit_buffer *ab)
956 if (!audit_rate_check()) {
957 audit_log_lost("rate limit exceeded");
960 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
961 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
962 skb_queue_tail(&audit_skb_queue, ab->skb);
964 wake_up_interruptible(&kauditd_wait);
966 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
969 audit_buffer_free(ab);
973 * audit_log - Log an audit record
974 * @ctx: audit context
975 * @gfp_mask: type of allocation
976 * @type: audit message type
977 * @fmt: format string to use
978 * @...: variable parameters matching the format string
980 * This is a convenience function that calls audit_log_start,
981 * audit_log_vformat, and audit_log_end. It may be called
984 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
985 const char *fmt, ...)
987 struct audit_buffer *ab;
990 ab = audit_log_start(ctx, gfp_mask, type);
993 audit_log_vformat(ab, fmt, args);
999 EXPORT_SYMBOL(audit_log_start);
1000 EXPORT_SYMBOL(audit_log_end);
1001 EXPORT_SYMBOL(audit_log_format);
1002 EXPORT_SYMBOL(audit_log);