aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/audit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/audit.c')
-rw-r--r--kernel/audit.c365
1 files changed, 235 insertions, 130 deletions
diff --git a/kernel/audit.c b/kernel/audit.c
index 906ae5a0233a..34c5a2310fbf 100644
--- a/kernel/audit.c
+++ b/kernel/audit.c
@@ -41,6 +41,8 @@
41 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ 41 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
42 */ 42 */
43 43
44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
44#include <linux/init.h> 46#include <linux/init.h>
45#include <asm/types.h> 47#include <asm/types.h>
46#include <linux/atomic.h> 48#include <linux/atomic.h>
@@ -63,6 +65,7 @@
63#include <linux/freezer.h> 65#include <linux/freezer.h>
64#include <linux/tty.h> 66#include <linux/tty.h>
65#include <linux/pid_namespace.h> 67#include <linux/pid_namespace.h>
68#include <net/netns/generic.h>
66 69
67#include "audit.h" 70#include "audit.h"
68 71
@@ -76,16 +79,16 @@ static int audit_initialized;
76#define AUDIT_OFF 0 79#define AUDIT_OFF 0
77#define AUDIT_ON 1 80#define AUDIT_ON 1
78#define AUDIT_LOCKED 2 81#define AUDIT_LOCKED 2
79int audit_enabled; 82u32 audit_enabled;
80int audit_ever_enabled; 83u32 audit_ever_enabled;
81 84
82EXPORT_SYMBOL_GPL(audit_enabled); 85EXPORT_SYMBOL_GPL(audit_enabled);
83 86
84/* Default state when kernel boots without any parameters. */ 87/* Default state when kernel boots without any parameters. */
85static int audit_default; 88static u32 audit_default;
86 89
87/* If auditing cannot proceed, audit_failure selects what happens. */ 90/* If auditing cannot proceed, audit_failure selects what happens. */
88static int audit_failure = AUDIT_FAIL_PRINTK; 91static u32 audit_failure = AUDIT_FAIL_PRINTK;
89 92
90/* 93/*
91 * If audit records are to be written to the netlink socket, audit_pid 94 * If audit records are to be written to the netlink socket, audit_pid
@@ -93,17 +96,19 @@ static int audit_failure = AUDIT_FAIL_PRINTK;
93 * the portid to use to send netlink messages to that process. 96 * the portid to use to send netlink messages to that process.
94 */ 97 */
95int audit_pid; 98int audit_pid;
96static int audit_nlk_portid; 99static __u32 audit_nlk_portid;
97 100
98/* If audit_rate_limit is non-zero, limit the rate of sending audit records 101/* If audit_rate_limit is non-zero, limit the rate of sending audit records
99 * to that number per second. This prevents DoS attacks, but results in 102 * to that number per second. This prevents DoS attacks, but results in
100 * audit records being dropped. */ 103 * audit records being dropped. */
101static int audit_rate_limit; 104static u32 audit_rate_limit;
102 105
103/* Number of outstanding audit_buffers allowed. */ 106/* Number of outstanding audit_buffers allowed.
104static int audit_backlog_limit = 64; 107 * When set to zero, this means unlimited. */
105static int audit_backlog_wait_time = 60 * HZ; 108static u32 audit_backlog_limit = 64;
106static int audit_backlog_wait_overflow = 0; 109#define AUDIT_BACKLOG_WAIT_TIME (60 * HZ)
110static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
111static u32 audit_backlog_wait_overflow = 0;
107 112
108/* The identity of the user shutting down the audit system. */ 113/* The identity of the user shutting down the audit system. */
109kuid_t audit_sig_uid = INVALID_UID; 114kuid_t audit_sig_uid = INVALID_UID;
@@ -121,6 +126,7 @@ static atomic_t audit_lost = ATOMIC_INIT(0);
121 126
122/* The netlink socket. */ 127/* The netlink socket. */
123static struct sock *audit_sock; 128static struct sock *audit_sock;
129int audit_net_id;
124 130
125/* Hash for inode-based rules */ 131/* Hash for inode-based rules */
126struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; 132struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
@@ -175,27 +181,27 @@ struct audit_buffer {
175}; 181};
176 182
177struct audit_reply { 183struct audit_reply {
178 int pid; 184 __u32 portid;
185 pid_t pid;
179 struct sk_buff *skb; 186 struct sk_buff *skb;
180}; 187};
181 188
182static void audit_set_pid(struct audit_buffer *ab, pid_t pid) 189static void audit_set_portid(struct audit_buffer *ab, __u32 portid)
183{ 190{
184 if (ab) { 191 if (ab) {
185 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); 192 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
186 nlh->nlmsg_pid = pid; 193 nlh->nlmsg_pid = portid;
187 } 194 }
188} 195}
189 196
190void audit_panic(const char *message) 197void audit_panic(const char *message)
191{ 198{
192 switch (audit_failure) 199 switch (audit_failure) {
193 {
194 case AUDIT_FAIL_SILENT: 200 case AUDIT_FAIL_SILENT:
195 break; 201 break;
196 case AUDIT_FAIL_PRINTK: 202 case AUDIT_FAIL_PRINTK:
197 if (printk_ratelimit()) 203 if (printk_ratelimit())
198 printk(KERN_ERR "audit: %s\n", message); 204 pr_err("%s\n", message);
199 break; 205 break;
200 case AUDIT_FAIL_PANIC: 206 case AUDIT_FAIL_PANIC:
201 /* test audit_pid since printk is always losey, why bother? */ 207 /* test audit_pid since printk is always losey, why bother? */
@@ -266,9 +272,7 @@ void audit_log_lost(const char *message)
266 272
267 if (print) { 273 if (print) {
268 if (printk_ratelimit()) 274 if (printk_ratelimit())
269 printk(KERN_WARNING 275 pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n",
270 "audit: audit_lost=%d audit_rate_limit=%d "
271 "audit_backlog_limit=%d\n",
272 atomic_read(&audit_lost), 276 atomic_read(&audit_lost),
273 audit_rate_limit, 277 audit_rate_limit,
274 audit_backlog_limit); 278 audit_backlog_limit);
@@ -276,7 +280,7 @@ void audit_log_lost(const char *message)
276 } 280 }
277} 281}
278 282
279static int audit_log_config_change(char *function_name, int new, int old, 283static int audit_log_config_change(char *function_name, u32 new, u32 old,
280 int allow_changes) 284 int allow_changes)
281{ 285{
282 struct audit_buffer *ab; 286 struct audit_buffer *ab;
@@ -285,7 +289,7 @@ static int audit_log_config_change(char *function_name, int new, int old,
285 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); 289 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
286 if (unlikely(!ab)) 290 if (unlikely(!ab))
287 return rc; 291 return rc;
288 audit_log_format(ab, "%s=%d old=%d", function_name, new, old); 292 audit_log_format(ab, "%s=%u old=%u", function_name, new, old);
289 audit_log_session_info(ab); 293 audit_log_session_info(ab);
290 rc = audit_log_task_context(ab); 294 rc = audit_log_task_context(ab);
291 if (rc) 295 if (rc)
@@ -295,9 +299,10 @@ static int audit_log_config_change(char *function_name, int new, int old,
295 return rc; 299 return rc;
296} 300}
297 301
298static int audit_do_config_change(char *function_name, int *to_change, int new) 302static int audit_do_config_change(char *function_name, u32 *to_change, u32 new)
299{ 303{
300 int allow_changes, rc = 0, old = *to_change; 304 int allow_changes, rc = 0;
305 u32 old = *to_change;
301 306
302 /* check if we are locked */ 307 /* check if we are locked */
303 if (audit_enabled == AUDIT_LOCKED) 308 if (audit_enabled == AUDIT_LOCKED)
@@ -320,17 +325,23 @@ static int audit_do_config_change(char *function_name, int *to_change, int new)
320 return rc; 325 return rc;
321} 326}
322 327
323static int audit_set_rate_limit(int limit) 328static int audit_set_rate_limit(u32 limit)
324{ 329{
325 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit); 330 return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit);
326} 331}
327 332
328static int audit_set_backlog_limit(int limit) 333static int audit_set_backlog_limit(u32 limit)
329{ 334{
330 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit); 335 return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit);
331} 336}
332 337
333static int audit_set_enabled(int state) 338static int audit_set_backlog_wait_time(u32 timeout)
339{
340 return audit_do_config_change("audit_backlog_wait_time",
341 &audit_backlog_wait_time, timeout);
342}
343
344static int audit_set_enabled(u32 state)
334{ 345{
335 int rc; 346 int rc;
336 if (state < AUDIT_OFF || state > AUDIT_LOCKED) 347 if (state < AUDIT_OFF || state > AUDIT_LOCKED)
@@ -343,7 +354,7 @@ static int audit_set_enabled(int state)
343 return rc; 354 return rc;
344} 355}
345 356
346static int audit_set_failure(int state) 357static int audit_set_failure(u32 state)
347{ 358{
348 if (state != AUDIT_FAIL_SILENT 359 if (state != AUDIT_FAIL_SILENT
349 && state != AUDIT_FAIL_PRINTK 360 && state != AUDIT_FAIL_PRINTK
@@ -365,7 +376,8 @@ static int audit_set_failure(int state)
365static void audit_hold_skb(struct sk_buff *skb) 376static void audit_hold_skb(struct sk_buff *skb)
366{ 377{
367 if (audit_default && 378 if (audit_default &&
368 skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit) 379 (!audit_backlog_limit ||
380 skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit))
369 skb_queue_tail(&audit_skb_hold_queue, skb); 381 skb_queue_tail(&audit_skb_hold_queue, skb);
370 else 382 else
371 kfree_skb(skb); 383 kfree_skb(skb);
@@ -382,7 +394,7 @@ static void audit_printk_skb(struct sk_buff *skb)
382 394
383 if (nlh->nlmsg_type != AUDIT_EOE) { 395 if (nlh->nlmsg_type != AUDIT_EOE) {
384 if (printk_ratelimit()) 396 if (printk_ratelimit())
385 printk(KERN_NOTICE "type=%d %s\n", nlh->nlmsg_type, data); 397 pr_notice("type=%d %s\n", nlh->nlmsg_type, data);
386 else 398 else
387 audit_log_lost("printk limit exceeded\n"); 399 audit_log_lost("printk limit exceeded\n");
388 } 400 }
@@ -398,9 +410,12 @@ static void kauditd_send_skb(struct sk_buff *skb)
398 err = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); 410 err = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0);
399 if (err < 0) { 411 if (err < 0) {
400 BUG_ON(err != -ECONNREFUSED); /* Shouldn't happen */ 412 BUG_ON(err != -ECONNREFUSED); /* Shouldn't happen */
401 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); 413 if (audit_pid) {
402 audit_log_lost("auditd disappeared\n"); 414 pr_err("*NO* daemon at audit_pid=%d\n", audit_pid);
403 audit_pid = 0; 415 audit_log_lost("auditd disappeared\n");
416 audit_pid = 0;
417 audit_sock = NULL;
418 }
404 /* we might get lucky and get this in the next auditd */ 419 /* we might get lucky and get this in the next auditd */
405 audit_hold_skb(skb); 420 audit_hold_skb(skb);
406 } else 421 } else
@@ -457,8 +472,10 @@ static int kauditd_thread(void *dummy)
457 flush_hold_queue(); 472 flush_hold_queue();
458 473
459 skb = skb_dequeue(&audit_skb_queue); 474 skb = skb_dequeue(&audit_skb_queue);
460 wake_up(&audit_backlog_wait); 475
461 if (skb) { 476 if (skb) {
477 if (skb_queue_len(&audit_skb_queue) <= audit_backlog_limit)
478 wake_up(&audit_backlog_wait);
462 if (audit_pid) 479 if (audit_pid)
463 kauditd_send_skb(skb); 480 kauditd_send_skb(skb);
464 else 481 else
@@ -482,22 +499,23 @@ static int kauditd_thread(void *dummy)
482int audit_send_list(void *_dest) 499int audit_send_list(void *_dest)
483{ 500{
484 struct audit_netlink_list *dest = _dest; 501 struct audit_netlink_list *dest = _dest;
485 int pid = dest->pid;
486 struct sk_buff *skb; 502 struct sk_buff *skb;
503 struct net *net = get_net_ns_by_pid(dest->pid);
504 struct audit_net *aunet = net_generic(net, audit_net_id);
487 505
488 /* wait for parent to finish and send an ACK */ 506 /* wait for parent to finish and send an ACK */
489 mutex_lock(&audit_cmd_mutex); 507 mutex_lock(&audit_cmd_mutex);
490 mutex_unlock(&audit_cmd_mutex); 508 mutex_unlock(&audit_cmd_mutex);
491 509
492 while ((skb = __skb_dequeue(&dest->q)) != NULL) 510 while ((skb = __skb_dequeue(&dest->q)) != NULL)
493 netlink_unicast(audit_sock, skb, pid, 0); 511 netlink_unicast(aunet->nlsk, skb, dest->portid, 0);
494 512
495 kfree(dest); 513 kfree(dest);
496 514
497 return 0; 515 return 0;
498} 516}
499 517
500struct sk_buff *audit_make_reply(int pid, int seq, int type, int done, 518struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
501 int multi, const void *payload, int size) 519 int multi, const void *payload, int size)
502{ 520{
503 struct sk_buff *skb; 521 struct sk_buff *skb;
@@ -510,7 +528,7 @@ struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
510 if (!skb) 528 if (!skb)
511 return NULL; 529 return NULL;
512 530
513 nlh = nlmsg_put(skb, pid, seq, t, size, flags); 531 nlh = nlmsg_put(skb, portid, seq, t, size, flags);
514 if (!nlh) 532 if (!nlh)
515 goto out_kfree_skb; 533 goto out_kfree_skb;
516 data = nlmsg_data(nlh); 534 data = nlmsg_data(nlh);
@@ -525,19 +543,21 @@ out_kfree_skb:
525static int audit_send_reply_thread(void *arg) 543static int audit_send_reply_thread(void *arg)
526{ 544{
527 struct audit_reply *reply = (struct audit_reply *)arg; 545 struct audit_reply *reply = (struct audit_reply *)arg;
546 struct net *net = get_net_ns_by_pid(reply->pid);
547 struct audit_net *aunet = net_generic(net, audit_net_id);
528 548
529 mutex_lock(&audit_cmd_mutex); 549 mutex_lock(&audit_cmd_mutex);
530 mutex_unlock(&audit_cmd_mutex); 550 mutex_unlock(&audit_cmd_mutex);
531 551
532 /* Ignore failure. It'll only happen if the sender goes away, 552 /* Ignore failure. It'll only happen if the sender goes away,
533 because our timeout is set to infinite. */ 553 because our timeout is set to infinite. */
534 netlink_unicast(audit_sock, reply->skb, reply->pid, 0); 554 netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0);
535 kfree(reply); 555 kfree(reply);
536 return 0; 556 return 0;
537} 557}
538/** 558/**
539 * audit_send_reply - send an audit reply message via netlink 559 * audit_send_reply - send an audit reply message via netlink
540 * @pid: process id to send reply to 560 * @portid: netlink port to which to send reply
541 * @seq: sequence number 561 * @seq: sequence number
542 * @type: audit message type 562 * @type: audit message type
543 * @done: done (last) flag 563 * @done: done (last) flag
@@ -545,11 +565,11 @@ static int audit_send_reply_thread(void *arg)
545 * @payload: payload data 565 * @payload: payload data
546 * @size: payload size 566 * @size: payload size
547 * 567 *
548 * Allocates an skb, builds the netlink message, and sends it to the pid. 568 * Allocates an skb, builds the netlink message, and sends it to the port id.
549 * No failure notifications. 569 * No failure notifications.
550 */ 570 */
551static void audit_send_reply(int pid, int seq, int type, int done, int multi, 571static void audit_send_reply(__u32 portid, int seq, int type, int done,
552 const void *payload, int size) 572 int multi, const void *payload, int size)
553{ 573{
554 struct sk_buff *skb; 574 struct sk_buff *skb;
555 struct task_struct *tsk; 575 struct task_struct *tsk;
@@ -559,11 +579,12 @@ static void audit_send_reply(int pid, int seq, int type, int done, int multi,
559 if (!reply) 579 if (!reply)
560 return; 580 return;
561 581
562 skb = audit_make_reply(pid, seq, type, done, multi, payload, size); 582 skb = audit_make_reply(portid, seq, type, done, multi, payload, size);
563 if (!skb) 583 if (!skb)
564 goto out; 584 goto out;
565 585
566 reply->pid = pid; 586 reply->portid = portid;
587 reply->pid = task_pid_vnr(current);
567 reply->skb = skb; 588 reply->skb = skb;
568 589
569 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); 590 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
@@ -663,8 +684,12 @@ static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature
663{ 684{
664 struct audit_buffer *ab; 685 struct audit_buffer *ab;
665 686
687 if (audit_enabled == AUDIT_OFF)
688 return;
689
666 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE); 690 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE);
667 audit_log_format(ab, "feature=%s new=%d old=%d old_lock=%d new_lock=%d res=%d", 691 audit_log_task_info(ab, current);
692 audit_log_format(ab, "feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d",
668 audit_feature_names[which], !!old_feature, !!new_feature, 693 audit_feature_names[which], !!old_feature, !!new_feature,
669 !!old_lock, !!new_lock, res); 694 !!old_lock, !!new_lock, res);
670 audit_log_end(ab); 695 audit_log_end(ab);
@@ -694,7 +719,7 @@ static int audit_set_feature(struct sk_buff *skb)
694 old_lock = af.lock & feature; 719 old_lock = af.lock & feature;
695 720
696 /* are we changing a locked feature? */ 721 /* are we changing a locked feature? */
697 if ((af.lock & feature) && (new_feature != old_feature)) { 722 if (old_lock && (new_feature != old_feature)) {
698 audit_log_feature_change(i, old_feature, new_feature, 723 audit_log_feature_change(i, old_feature, new_feature,
699 old_lock, new_lock, 0); 724 old_lock, new_lock, 0);
700 return -EPERM; 725 return -EPERM;
@@ -732,7 +757,6 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
732{ 757{
733 u32 seq; 758 u32 seq;
734 void *data; 759 void *data;
735 struct audit_status *status_get, status_set;
736 int err; 760 int err;
737 struct audit_buffer *ab; 761 struct audit_buffer *ab;
738 u16 msg_type = nlh->nlmsg_type; 762 u16 msg_type = nlh->nlmsg_type;
@@ -758,48 +782,70 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
758 data = nlmsg_data(nlh); 782 data = nlmsg_data(nlh);
759 783
760 switch (msg_type) { 784 switch (msg_type) {
761 case AUDIT_GET: 785 case AUDIT_GET: {
762 memset(&status_set, 0, sizeof(status_set)); 786 struct audit_status s;
763 status_set.enabled = audit_enabled; 787 memset(&s, 0, sizeof(s));
764 status_set.failure = audit_failure; 788 s.enabled = audit_enabled;
765 status_set.pid = audit_pid; 789 s.failure = audit_failure;
766 status_set.rate_limit = audit_rate_limit; 790 s.pid = audit_pid;
767 status_set.backlog_limit = audit_backlog_limit; 791 s.rate_limit = audit_rate_limit;
768 status_set.lost = atomic_read(&audit_lost); 792 s.backlog_limit = audit_backlog_limit;
769 status_set.backlog = skb_queue_len(&audit_skb_queue); 793 s.lost = atomic_read(&audit_lost);
794 s.backlog = skb_queue_len(&audit_skb_queue);
795 s.version = AUDIT_VERSION_LATEST;
796 s.backlog_wait_time = audit_backlog_wait_time;
770 audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_GET, 0, 0, 797 audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_GET, 0, 0,
771 &status_set, sizeof(status_set)); 798 &s, sizeof(s));
772 break; 799 break;
773 case AUDIT_SET: 800 }
774 if (nlmsg_len(nlh) < sizeof(struct audit_status)) 801 case AUDIT_SET: {
775 return -EINVAL; 802 struct audit_status s;
776 status_get = (struct audit_status *)data; 803 memset(&s, 0, sizeof(s));
777 if (status_get->mask & AUDIT_STATUS_ENABLED) { 804 /* guard against past and future API changes */
778 err = audit_set_enabled(status_get->enabled); 805 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
806 if (s.mask & AUDIT_STATUS_ENABLED) {
807 err = audit_set_enabled(s.enabled);
779 if (err < 0) 808 if (err < 0)
780 return err; 809 return err;
781 } 810 }
782 if (status_get->mask & AUDIT_STATUS_FAILURE) { 811 if (s.mask & AUDIT_STATUS_FAILURE) {
783 err = audit_set_failure(status_get->failure); 812 err = audit_set_failure(s.failure);
784 if (err < 0) 813 if (err < 0)
785 return err; 814 return err;
786 } 815 }
787 if (status_get->mask & AUDIT_STATUS_PID) { 816 if (s.mask & AUDIT_STATUS_PID) {
788 int new_pid = status_get->pid; 817 int new_pid = s.pid;
789 818
819 if ((!new_pid) && (task_tgid_vnr(current) != audit_pid))
820 return -EACCES;
790 if (audit_enabled != AUDIT_OFF) 821 if (audit_enabled != AUDIT_OFF)
791 audit_log_config_change("audit_pid", new_pid, audit_pid, 1); 822 audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
792 audit_pid = new_pid; 823 audit_pid = new_pid;
793 audit_nlk_portid = NETLINK_CB(skb).portid; 824 audit_nlk_portid = NETLINK_CB(skb).portid;
825 audit_sock = skb->sk;
794 } 826 }
795 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { 827 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
796 err = audit_set_rate_limit(status_get->rate_limit); 828 err = audit_set_rate_limit(s.rate_limit);
829 if (err < 0)
830 return err;
831 }
832 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
833 err = audit_set_backlog_limit(s.backlog_limit);
834 if (err < 0)
835 return err;
836 }
837 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
838 if (sizeof(s) > (size_t)nlh->nlmsg_len)
839 return -EINVAL;
840 if (s.backlog_wait_time < 0 ||
841 s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
842 return -EINVAL;
843 err = audit_set_backlog_wait_time(s.backlog_wait_time);
797 if (err < 0) 844 if (err < 0)
798 return err; 845 return err;
799 } 846 }
800 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
801 err = audit_set_backlog_limit(status_get->backlog_limit);
802 break; 847 break;
848 }
803 case AUDIT_GET_FEATURE: 849 case AUDIT_GET_FEATURE:
804 err = audit_get_feature(skb); 850 err = audit_get_feature(skb);
805 if (err) 851 if (err)
@@ -817,13 +863,14 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
817 return 0; 863 return 0;
818 864
819 err = audit_filter_user(msg_type); 865 err = audit_filter_user(msg_type);
820 if (err == 1) { 866 if (err == 1) { /* match or error */
821 err = 0; 867 err = 0;
822 if (msg_type == AUDIT_USER_TTY) { 868 if (msg_type == AUDIT_USER_TTY) {
823 err = tty_audit_push_current(); 869 err = tty_audit_push_current();
824 if (err) 870 if (err)
825 break; 871 break;
826 } 872 }
873 mutex_unlock(&audit_cmd_mutex);
827 audit_log_common_recv_msg(&ab, msg_type); 874 audit_log_common_recv_msg(&ab, msg_type);
828 if (msg_type != AUDIT_USER_TTY) 875 if (msg_type != AUDIT_USER_TTY)
829 audit_log_format(ab, " msg='%.*s'", 876 audit_log_format(ab, " msg='%.*s'",
@@ -839,8 +886,9 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
839 size--; 886 size--;
840 audit_log_n_untrustedstring(ab, data, size); 887 audit_log_n_untrustedstring(ab, data, size);
841 } 888 }
842 audit_set_pid(ab, NETLINK_CB(skb).portid); 889 audit_set_portid(ab, NETLINK_CB(skb).portid);
843 audit_log_end(ab); 890 audit_log_end(ab);
891 mutex_lock(&audit_cmd_mutex);
844 } 892 }
845 break; 893 break;
846 case AUDIT_ADD_RULE: 894 case AUDIT_ADD_RULE:
@@ -853,11 +901,12 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
853 audit_log_end(ab); 901 audit_log_end(ab);
854 return -EPERM; 902 return -EPERM;
855 } 903 }
856 /* fallthrough */ 904 err = audit_rule_change(msg_type, NETLINK_CB(skb).portid,
857 case AUDIT_LIST_RULES:
858 err = audit_receive_filter(msg_type, NETLINK_CB(skb).portid,
859 seq, data, nlmsg_len(nlh)); 905 seq, data, nlmsg_len(nlh));
860 break; 906 break;
907 case AUDIT_LIST_RULES:
908 err = audit_list_rules_send(NETLINK_CB(skb).portid, seq);
909 break;
861 case AUDIT_TRIM: 910 case AUDIT_TRIM:
862 audit_trim_trees(); 911 audit_trim_trees();
863 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); 912 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
@@ -939,20 +988,33 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
939 break; 988 break;
940 } 989 }
941 case AUDIT_TTY_SET: { 990 case AUDIT_TTY_SET: {
942 struct audit_tty_status s; 991 struct audit_tty_status s, old;
943 struct task_struct *tsk = current; 992 struct task_struct *tsk = current;
993 struct audit_buffer *ab;
944 994
945 memset(&s, 0, sizeof(s)); 995 memset(&s, 0, sizeof(s));
946 /* guard against past and future API changes */ 996 /* guard against past and future API changes */
947 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); 997 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
998 /* check if new data is valid */
948 if ((s.enabled != 0 && s.enabled != 1) || 999 if ((s.enabled != 0 && s.enabled != 1) ||
949 (s.log_passwd != 0 && s.log_passwd != 1)) 1000 (s.log_passwd != 0 && s.log_passwd != 1))
950 return -EINVAL; 1001 err = -EINVAL;
951 1002
952 spin_lock(&tsk->sighand->siglock); 1003 spin_lock(&tsk->sighand->siglock);
953 tsk->signal->audit_tty = s.enabled; 1004 old.enabled = tsk->signal->audit_tty;
954 tsk->signal->audit_tty_log_passwd = s.log_passwd; 1005 old.log_passwd = tsk->signal->audit_tty_log_passwd;
1006 if (!err) {
1007 tsk->signal->audit_tty = s.enabled;
1008 tsk->signal->audit_tty_log_passwd = s.log_passwd;
1009 }
955 spin_unlock(&tsk->sighand->siglock); 1010 spin_unlock(&tsk->sighand->siglock);
1011
1012 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1013 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1014 " old-log_passwd=%d new-log_passwd=%d res=%d",
1015 old.enabled, s.enabled, old.log_passwd,
1016 s.log_passwd, !err);
1017 audit_log_end(ab);
956 break; 1018 break;
957 } 1019 }
958 default: 1020 default:
@@ -998,24 +1060,55 @@ static void audit_receive(struct sk_buff *skb)
998 mutex_unlock(&audit_cmd_mutex); 1060 mutex_unlock(&audit_cmd_mutex);
999} 1061}
1000 1062
1001/* Initialize audit support at boot time. */ 1063static int __net_init audit_net_init(struct net *net)
1002static int __init audit_init(void)
1003{ 1064{
1004 int i;
1005 struct netlink_kernel_cfg cfg = { 1065 struct netlink_kernel_cfg cfg = {
1006 .input = audit_receive, 1066 .input = audit_receive,
1007 }; 1067 };
1008 1068
1069 struct audit_net *aunet = net_generic(net, audit_net_id);
1070
1071 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
1072 if (aunet->nlsk == NULL) {
1073 audit_panic("cannot initialize netlink socket in namespace");
1074 return -ENOMEM;
1075 }
1076 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1077 return 0;
1078}
1079
1080static void __net_exit audit_net_exit(struct net *net)
1081{
1082 struct audit_net *aunet = net_generic(net, audit_net_id);
1083 struct sock *sock = aunet->nlsk;
1084 if (sock == audit_sock) {
1085 audit_pid = 0;
1086 audit_sock = NULL;
1087 }
1088
1089 rcu_assign_pointer(aunet->nlsk, NULL);
1090 synchronize_net();
1091 netlink_kernel_release(sock);
1092}
1093
1094static struct pernet_operations audit_net_ops __net_initdata = {
1095 .init = audit_net_init,
1096 .exit = audit_net_exit,
1097 .id = &audit_net_id,
1098 .size = sizeof(struct audit_net),
1099};
1100
1101/* Initialize audit support at boot time. */
1102static int __init audit_init(void)
1103{
1104 int i;
1105
1009 if (audit_initialized == AUDIT_DISABLED) 1106 if (audit_initialized == AUDIT_DISABLED)
1010 return 0; 1107 return 0;
1011 1108
1012 printk(KERN_INFO "audit: initializing netlink socket (%s)\n", 1109 pr_info("initializing netlink subsys (%s)\n",
1013 audit_default ? "enabled" : "disabled"); 1110 audit_default ? "enabled" : "disabled");
1014 audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, &cfg); 1111 register_pernet_subsys(&audit_net_ops);
1015 if (!audit_sock)
1016 audit_panic("cannot initialize netlink socket");
1017 else
1018 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1019 1112
1020 skb_queue_head_init(&audit_skb_queue); 1113 skb_queue_head_init(&audit_skb_queue);
1021 skb_queue_head_init(&audit_skb_hold_queue); 1114 skb_queue_head_init(&audit_skb_hold_queue);
@@ -1039,22 +1132,32 @@ static int __init audit_enable(char *str)
1039 if (!audit_default) 1132 if (!audit_default)
1040 audit_initialized = AUDIT_DISABLED; 1133 audit_initialized = AUDIT_DISABLED;
1041 1134
1042 printk(KERN_INFO "audit: %s", audit_default ? "enabled" : "disabled"); 1135 pr_info("%s\n", audit_default ?
1136 "enabled (after initialization)" : "disabled (until reboot)");
1043 1137
1044 if (audit_initialized == AUDIT_INITIALIZED) { 1138 return 1;
1045 audit_enabled = audit_default; 1139}
1046 audit_ever_enabled |= !!audit_default; 1140__setup("audit=", audit_enable);
1047 } else if (audit_initialized == AUDIT_UNINITIALIZED) { 1141
1048 printk(" (after initialization)"); 1142/* Process kernel command-line parameter at boot time.
1049 } else { 1143 * audit_backlog_limit=<n> */
1050 printk(" (until reboot)"); 1144static int __init audit_backlog_limit_set(char *str)
1145{
1146 u32 audit_backlog_limit_arg;
1147
1148 pr_info("audit_backlog_limit: ");
1149 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1150 pr_cont("using default of %u, unable to parse %s\n",
1151 audit_backlog_limit, str);
1152 return 1;
1051 } 1153 }
1052 printk("\n"); 1154
1155 audit_backlog_limit = audit_backlog_limit_arg;
1156 pr_cont("%d\n", audit_backlog_limit);
1053 1157
1054 return 1; 1158 return 1;
1055} 1159}
1056 1160__setup("audit_backlog_limit=", audit_backlog_limit_set);
1057__setup("audit=", audit_enable);
1058 1161
1059static void audit_buffer_free(struct audit_buffer *ab) 1162static void audit_buffer_free(struct audit_buffer *ab)
1060{ 1163{
@@ -1165,18 +1268,20 @@ static inline void audit_get_stamp(struct audit_context *ctx,
1165/* 1268/*
1166 * Wait for auditd to drain the queue a little 1269 * Wait for auditd to drain the queue a little
1167 */ 1270 */
1168static void wait_for_auditd(unsigned long sleep_time) 1271static long wait_for_auditd(long sleep_time)
1169{ 1272{
1170 DECLARE_WAITQUEUE(wait, current); 1273 DECLARE_WAITQUEUE(wait, current);
1171 set_current_state(TASK_UNINTERRUPTIBLE); 1274 set_current_state(TASK_UNINTERRUPTIBLE);
1172 add_wait_queue(&audit_backlog_wait, &wait); 1275 add_wait_queue_exclusive(&audit_backlog_wait, &wait);
1173 1276
1174 if (audit_backlog_limit && 1277 if (audit_backlog_limit &&
1175 skb_queue_len(&audit_skb_queue) > audit_backlog_limit) 1278 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
1176 schedule_timeout(sleep_time); 1279 sleep_time = schedule_timeout(sleep_time);
1177 1280
1178 __set_current_state(TASK_RUNNING); 1281 __set_current_state(TASK_RUNNING);
1179 remove_wait_queue(&audit_backlog_wait, &wait); 1282 remove_wait_queue(&audit_backlog_wait, &wait);
1283
1284 return sleep_time;
1180} 1285}
1181 1286
1182/** 1287/**
@@ -1200,7 +1305,8 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
1200 struct audit_buffer *ab = NULL; 1305 struct audit_buffer *ab = NULL;
1201 struct timespec t; 1306 struct timespec t;
1202 unsigned int uninitialized_var(serial); 1307 unsigned int uninitialized_var(serial);
1203 int reserve; 1308 int reserve = 5; /* Allow atomic callers to go up to five
1309 entries over the normal backlog limit */
1204 unsigned long timeout_start = jiffies; 1310 unsigned long timeout_start = jiffies;
1205 1311
1206 if (audit_initialized != AUDIT_INITIALIZED) 1312 if (audit_initialized != AUDIT_INITIALIZED)
@@ -1209,36 +1315,37 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
1209 if (unlikely(audit_filter_type(type))) 1315 if (unlikely(audit_filter_type(type)))
1210 return NULL; 1316 return NULL;
1211 1317
1212 if (gfp_mask & __GFP_WAIT) 1318 if (gfp_mask & __GFP_WAIT) {
1213 reserve = 0; 1319 if (audit_pid && audit_pid == current->pid)
1214 else 1320 gfp_mask &= ~__GFP_WAIT;
1215 reserve = 5; /* Allow atomic callers to go up to five 1321 else
1216 entries over the normal backlog limit */ 1322 reserve = 0;
1323 }
1217 1324
1218 while (audit_backlog_limit 1325 while (audit_backlog_limit
1219 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { 1326 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
1220 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) { 1327 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) {
1221 unsigned long sleep_time; 1328 long sleep_time;
1222 1329
1223 sleep_time = timeout_start + audit_backlog_wait_time - 1330 sleep_time = timeout_start + audit_backlog_wait_time - jiffies;
1224 jiffies; 1331 if (sleep_time > 0) {
1225 if ((long)sleep_time > 0) { 1332 sleep_time = wait_for_auditd(sleep_time);
1226 wait_for_auditd(sleep_time); 1333 if (sleep_time > 0)
1227 continue; 1334 continue;
1228 } 1335 }
1229 } 1336 }
1230 if (audit_rate_check() && printk_ratelimit()) 1337 if (audit_rate_check() && printk_ratelimit())
1231 printk(KERN_WARNING 1338 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1232 "audit: audit_backlog=%d > " 1339 skb_queue_len(&audit_skb_queue),
1233 "audit_backlog_limit=%d\n", 1340 audit_backlog_limit);
1234 skb_queue_len(&audit_skb_queue),
1235 audit_backlog_limit);
1236 audit_log_lost("backlog limit exceeded"); 1341 audit_log_lost("backlog limit exceeded");
1237 audit_backlog_wait_time = audit_backlog_wait_overflow; 1342 audit_backlog_wait_time = audit_backlog_wait_overflow;
1238 wake_up(&audit_backlog_wait); 1343 wake_up(&audit_backlog_wait);
1239 return NULL; 1344 return NULL;
1240 } 1345 }
1241 1346
1347 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
1348
1242 ab = audit_buffer_alloc(ctx, gfp_mask, type); 1349 ab = audit_buffer_alloc(ctx, gfp_mask, type);
1243 if (!ab) { 1350 if (!ab) {
1244 audit_log_lost("out of memory in audit_log_start"); 1351 audit_log_lost("out of memory in audit_log_start");
@@ -1356,7 +1463,6 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
1356 int i, avail, new_len; 1463 int i, avail, new_len;
1357 unsigned char *ptr; 1464 unsigned char *ptr;
1358 struct sk_buff *skb; 1465 struct sk_buff *skb;
1359 static const unsigned char *hex = "0123456789ABCDEF";
1360 1466
1361 if (!ab) 1467 if (!ab)
1362 return; 1468 return;
@@ -1374,10 +1480,8 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
1374 } 1480 }
1375 1481
1376 ptr = skb_tail_pointer(skb); 1482 ptr = skb_tail_pointer(skb);
1377 for (i=0; i<len; i++) { 1483 for (i = 0; i < len; i++)
1378 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ 1484 ptr = hex_byte_pack_upper(ptr, buf[i]);
1379 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
1380 }
1381 *ptr = 0; 1485 *ptr = 0;
1382 skb_put(skb, len << 1); /* new string is twice the old string */ 1486 skb_put(skb, len << 1); /* new string is twice the old string */
1383} 1487}
@@ -1491,7 +1595,7 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1491 1595
1492void audit_log_session_info(struct audit_buffer *ab) 1596void audit_log_session_info(struct audit_buffer *ab)
1493{ 1597{
1494 u32 sessionid = audit_get_sessionid(current); 1598 unsigned int sessionid = audit_get_sessionid(current);
1495 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); 1599 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1496 1600
1497 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid); 1601 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
@@ -1716,7 +1820,7 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1716 audit_log_format(ab, 1820 audit_log_format(ab,
1717 " ppid=%ld pid=%d auid=%u uid=%u gid=%u" 1821 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1718 " euid=%u suid=%u fsuid=%u" 1822 " euid=%u suid=%u fsuid=%u"
1719 " egid=%u sgid=%u fsgid=%u ses=%u tty=%s", 1823 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1720 sys_getppid(), 1824 sys_getppid(),
1721 tsk->pid, 1825 tsk->pid,
1722 from_kuid(&init_user_ns, audit_get_loginuid(tsk)), 1826 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
@@ -1728,7 +1832,7 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1728 from_kgid(&init_user_ns, cred->egid), 1832 from_kgid(&init_user_ns, cred->egid),
1729 from_kgid(&init_user_ns, cred->sgid), 1833 from_kgid(&init_user_ns, cred->sgid),
1730 from_kgid(&init_user_ns, cred->fsgid), 1834 from_kgid(&init_user_ns, cred->fsgid),
1731 audit_get_sessionid(tsk), tty); 1835 tty, audit_get_sessionid(tsk));
1732 1836
1733 get_task_comm(name, tsk); 1837 get_task_comm(name, tsk);
1734 audit_log_format(ab, " comm="); 1838 audit_log_format(ab, " comm=");
@@ -1739,7 +1843,8 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1739 if (mm->exe_file) 1843 if (mm->exe_file)
1740 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path); 1844 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
1741 up_read(&mm->mmap_sem); 1845 up_read(&mm->mmap_sem);
1742 } 1846 } else
1847 audit_log_format(ab, " exe=(null)");
1743 audit_log_task_context(ab); 1848 audit_log_task_context(ab);
1744} 1849}
1745EXPORT_SYMBOL(audit_log_task_info); 1850EXPORT_SYMBOL(audit_log_task_info);