aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/audit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/audit.c')
-rw-r--r--kernel/audit.c134
1 files changed, 106 insertions, 28 deletions
diff --git a/kernel/audit.c b/kernel/audit.c
index 0a813d2883e5..973ca5a9e0d6 100644
--- a/kernel/audit.c
+++ b/kernel/audit.c
@@ -72,7 +72,7 @@ static int audit_failure = AUDIT_FAIL_PRINTK;
72 * contains the (non-zero) pid. */ 72 * contains the (non-zero) pid. */
73int audit_pid; 73int audit_pid;
74 74
75/* If audit_limit is non-zero, limit the rate of sending audit records 75/* If audit_rate_limit is non-zero, limit the rate of sending audit records
76 * to that number per second. This prevents DoS attacks, but results in 76 * to that number per second. This prevents DoS attacks, but results in
77 * audit records being dropped. */ 77 * audit records being dropped. */
78static int audit_rate_limit; 78static int audit_rate_limit;
@@ -102,7 +102,7 @@ static struct sock *audit_sock;
102 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of 102 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
103 * being placed on the freelist). */ 103 * being placed on the freelist). */
104static DEFINE_SPINLOCK(audit_freelist_lock); 104static DEFINE_SPINLOCK(audit_freelist_lock);
105static int audit_freelist_count = 0; 105static int audit_freelist_count;
106static LIST_HEAD(audit_freelist); 106static LIST_HEAD(audit_freelist);
107 107
108static struct sk_buff_head audit_skb_queue; 108static struct sk_buff_head audit_skb_queue;
@@ -186,8 +186,14 @@ static inline int audit_rate_check(void)
186 return retval; 186 return retval;
187} 187}
188 188
189/* Emit at least 1 message per second, even if audit_rate_check is 189/**
190 * throttling. */ 190 * audit_log_lost - conditionally log lost audit message event
191 * @message: the message stating reason for lost audit message
192 *
193 * Emit at least 1 message per second, even if audit_rate_check is
194 * throttling.
195 * Always increment the lost messages counter.
196*/
191void audit_log_lost(const char *message) 197void audit_log_lost(const char *message)
192{ 198{
193 static unsigned long last_msg = 0; 199 static unsigned long last_msg = 0;
@@ -218,7 +224,6 @@ void audit_log_lost(const char *message)
218 audit_backlog_limit); 224 audit_backlog_limit);
219 audit_panic(message); 225 audit_panic(message);
220 } 226 }
221
222} 227}
223 228
224static int audit_set_rate_limit(int limit, uid_t loginuid) 229static int audit_set_rate_limit(int limit, uid_t loginuid)
@@ -302,6 +307,19 @@ static int kauditd_thread(void *dummy)
302 } 307 }
303} 308}
304 309
310/**
311 * audit_send_reply - send an audit reply message via netlink
312 * @pid: process id to send reply to
313 * @seq: sequence number
314 * @type: audit message type
315 * @done: done (last) flag
316 * @multi: multi-part message flag
317 * @payload: payload data
318 * @size: payload size
319 *
320 * Allocates an skb, builds the netlink message, and sends it to the pid.
321 * No failure notifications.
322 */
305void audit_send_reply(int pid, int seq, int type, int done, int multi, 323void audit_send_reply(int pid, int seq, int type, int done, int multi,
306 void *payload, int size) 324 void *payload, int size)
307{ 325{
@@ -376,7 +394,8 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
376 if (err) 394 if (err)
377 return err; 395 return err;
378 396
379 /* As soon as there's any sign of userspace auditd, start kauditd to talk to it */ 397 /* As soon as there's any sign of userspace auditd,
398 * start kauditd to talk to it */
380 if (!kauditd_task) 399 if (!kauditd_task)
381 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); 400 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
382 if (IS_ERR(kauditd_task)) { 401 if (IS_ERR(kauditd_task)) {
@@ -469,9 +488,11 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
469 return err < 0 ? err : 0; 488 return err < 0 ? err : 0;
470} 489}
471 490
472/* Get message from skb (based on rtnetlink_rcv_skb). Each message is 491/*
492 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
473 * processed by audit_receive_msg. Malformed skbs with wrong length are 493 * processed by audit_receive_msg. Malformed skbs with wrong length are
474 * discarded silently. */ 494 * discarded silently.
495 */
475static void audit_receive_skb(struct sk_buff *skb) 496static void audit_receive_skb(struct sk_buff *skb)
476{ 497{
477 int err; 498 int err;
@@ -600,7 +621,10 @@ err:
600 return NULL; 621 return NULL;
601} 622}
602 623
603/* Compute a serial number for the audit record. Audit records are 624/**
625 * audit_serial - compute a serial number for the audit record
626 *
627 * Compute a serial number for the audit record. Audit records are
604 * written to user-space as soon as they are generated, so a complete 628 * written to user-space as soon as they are generated, so a complete
605 * audit record may be written in several pieces. The timestamp of the 629 * audit record may be written in several pieces. The timestamp of the
606 * record and this serial number are used by the user-space tools to 630 * record and this serial number are used by the user-space tools to
@@ -612,8 +636,8 @@ err:
612 * audit context (for those records that have a context), and emit them 636 * audit context (for those records that have a context), and emit them
613 * all at syscall exit. However, this could delay the reporting of 637 * all at syscall exit. However, this could delay the reporting of
614 * significant errors until syscall exit (or never, if the system 638 * significant errors until syscall exit (or never, if the system
615 * halts). */ 639 * halts).
616 640 */
617unsigned int audit_serial(void) 641unsigned int audit_serial(void)
618{ 642{
619 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED; 643 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
@@ -649,6 +673,21 @@ static inline void audit_get_stamp(struct audit_context *ctx,
649 * will be written at syscall exit. If there is no associated task, tsk 673 * will be written at syscall exit. If there is no associated task, tsk
650 * should be NULL. */ 674 * should be NULL. */
651 675
676/**
677 * audit_log_start - obtain an audit buffer
678 * @ctx: audit_context (may be NULL)
679 * @gfp_mask: type of allocation
680 * @type: audit message type
681 *
682 * Returns audit_buffer pointer on success or NULL on error.
683 *
684 * Obtain an audit buffer. This routine does locking to obtain the
685 * audit buffer, but then no locking is required for calls to
686 * audit_log_*format. If the task (ctx) is a task that is currently in a
687 * syscall, then the syscall is marked as auditable and an audit record
688 * will be written at syscall exit. If there is no associated task, then
689 * task context (ctx) should be NULL.
690 */
652struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, 691struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
653 int type) 692 int type)
654{ 693{
@@ -713,6 +752,7 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
713/** 752/**
714 * audit_expand - expand skb in the audit buffer 753 * audit_expand - expand skb in the audit buffer
715 * @ab: audit_buffer 754 * @ab: audit_buffer
755 * @extra: space to add at tail of the skb
716 * 756 *
717 * Returns 0 (no space) on failed expansion, or available space if 757 * Returns 0 (no space) on failed expansion, or available space if
718 * successful. 758 * successful.
@@ -729,10 +769,12 @@ static inline int audit_expand(struct audit_buffer *ab, int extra)
729 return skb_tailroom(skb); 769 return skb_tailroom(skb);
730} 770}
731 771
732/* Format an audit message into the audit buffer. If there isn't enough 772/*
773 * Format an audit message into the audit buffer. If there isn't enough
733 * room in the audit buffer, more room will be allocated and vsnprint 774 * room in the audit buffer, more room will be allocated and vsnprint
734 * will be called a second time. Currently, we assume that a printk 775 * will be called a second time. Currently, we assume that a printk
735 * can't format message larger than 1024 bytes, so we don't either. */ 776 * can't format message larger than 1024 bytes, so we don't either.
777 */
736static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, 778static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
737 va_list args) 779 va_list args)
738{ 780{
@@ -757,7 +799,8 @@ static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
757 /* The printk buffer is 1024 bytes long, so if we get 799 /* The printk buffer is 1024 bytes long, so if we get
758 * here and AUDIT_BUFSIZ is at least 1024, then we can 800 * here and AUDIT_BUFSIZ is at least 1024, then we can
759 * log everything that printk could have logged. */ 801 * log everything that printk could have logged. */
760 avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); 802 avail = audit_expand(ab,
803 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
761 if (!avail) 804 if (!avail)
762 goto out; 805 goto out;
763 len = vsnprintf(skb->tail, avail, fmt, args2); 806 len = vsnprintf(skb->tail, avail, fmt, args2);
@@ -768,8 +811,14 @@ out:
768 return; 811 return;
769} 812}
770 813
771/* Format a message into the audit buffer. All the work is done in 814/**
772 * audit_log_vformat. */ 815 * audit_log_format - format a message into the audit buffer.
816 * @ab: audit_buffer
817 * @fmt: format string
818 * @...: optional parameters matching @fmt string
819 *
820 * All the work is done in audit_log_vformat.
821 */
773void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) 822void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
774{ 823{
775 va_list args; 824 va_list args;
@@ -781,9 +830,18 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
781 va_end(args); 830 va_end(args);
782} 831}
783 832
784/* This function will take the passed buf and convert it into a string of 833/**
785 * ascii hex digits. The new string is placed onto the skb. */ 834 * audit_log_hex - convert a buffer to hex and append it to the audit skb
786void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, 835 * @ab: the audit_buffer
836 * @buf: buffer to convert to hex
837 * @len: length of @buf to be converted
838 *
839 * No return value; failure to expand is silently ignored.
840 *
841 * This function will take the passed buf and convert it into a string of
842 * ascii hex digits. The new string is placed onto the skb.
843 */
844void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
787 size_t len) 845 size_t len)
788{ 846{
789 int i, avail, new_len; 847 int i, avail, new_len;
@@ -812,10 +870,16 @@ void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
812 skb_put(skb, len << 1); /* new string is twice the old string */ 870 skb_put(skb, len << 1); /* new string is twice the old string */
813} 871}
814 872
815/* This code will escape a string that is passed to it if the string 873/**
816 * contains a control character, unprintable character, double quote mark, 874 * audit_log_unstrustedstring - log a string that may contain random characters
875 * @ab: audit_buffer
876 * @string: string to be logged
877 *
878 * This code will escape a string that is passed to it if the string
879 * contains a control character, unprintable character, double quote mark,
817 * or a space. Unescaped strings will start and end with a double quote mark. 880 * or a space. Unescaped strings will start and end with a double quote mark.
818 * Strings that are escaped are printed in hex (2 digits per char). */ 881 * Strings that are escaped are printed in hex (2 digits per char).
882 */
819void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) 883void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
820{ 884{
821 const unsigned char *p = string; 885 const unsigned char *p = string;
@@ -854,10 +918,15 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
854 kfree(path); 918 kfree(path);
855} 919}
856 920
857/* The netlink_* functions cannot be called inside an irq context, so 921/**
858 * the audit buffer is places on a queue and a tasklet is scheduled to 922 * audit_log_end - end one audit record
923 * @ab: the audit_buffer
924 *
925 * The netlink_* functions cannot be called inside an irq context, so
926 * the audit buffer is placed on a queue and a tasklet is scheduled to
859 * remove them from the queue outside the irq context. May be called in 927 * remove them from the queue outside the irq context. May be called in
860 * any context. */ 928 * any context.
929 */
861void audit_log_end(struct audit_buffer *ab) 930void audit_log_end(struct audit_buffer *ab)
862{ 931{
863 if (!ab) 932 if (!ab)
@@ -878,9 +947,18 @@ void audit_log_end(struct audit_buffer *ab)
878 audit_buffer_free(ab); 947 audit_buffer_free(ab);
879} 948}
880 949
881/* Log an audit record. This is a convenience function that calls 950/**
882 * audit_log_start, audit_log_vformat, and audit_log_end. It may be 951 * audit_log - Log an audit record
883 * called in any context. */ 952 * @ctx: audit context
953 * @gfp_mask: type of allocation
954 * @type: audit message type
955 * @fmt: format string to use
956 * @...: variable parameters matching the format string
957 *
958 * This is a convenience function that calls audit_log_start,
959 * audit_log_vformat, and audit_log_end. It may be called
960 * in any context.
961 */
884void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, 962void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
885 const char *fmt, ...) 963 const char *fmt, ...)
886{ 964{