aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/audit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/audit.c')
-rw-r--r--kernel/audit.c380
1 files changed, 243 insertions, 137 deletions
diff --git a/kernel/audit.c b/kernel/audit.c
index 906ae5a0233a..3392d3e0254a 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 struct net *net;
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,24 @@ 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 = dest->net;
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
513 put_net(net);
495 kfree(dest); 514 kfree(dest);
496 515
497 return 0; 516 return 0;
498} 517}
499 518
500struct sk_buff *audit_make_reply(int pid, int seq, int type, int done, 519struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
501 int multi, const void *payload, int size) 520 int multi, const void *payload, int size)
502{ 521{
503 struct sk_buff *skb; 522 struct sk_buff *skb;
@@ -510,7 +529,7 @@ struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
510 if (!skb) 529 if (!skb)
511 return NULL; 530 return NULL;
512 531
513 nlh = nlmsg_put(skb, pid, seq, t, size, flags); 532 nlh = nlmsg_put(skb, portid, seq, t, size, flags);
514 if (!nlh) 533 if (!nlh)
515 goto out_kfree_skb; 534 goto out_kfree_skb;
516 data = nlmsg_data(nlh); 535 data = nlmsg_data(nlh);
@@ -525,19 +544,22 @@ out_kfree_skb:
525static int audit_send_reply_thread(void *arg) 544static int audit_send_reply_thread(void *arg)
526{ 545{
527 struct audit_reply *reply = (struct audit_reply *)arg; 546 struct audit_reply *reply = (struct audit_reply *)arg;
547 struct net *net = reply->net;
548 struct audit_net *aunet = net_generic(net, audit_net_id);
528 549
529 mutex_lock(&audit_cmd_mutex); 550 mutex_lock(&audit_cmd_mutex);
530 mutex_unlock(&audit_cmd_mutex); 551 mutex_unlock(&audit_cmd_mutex);
531 552
532 /* Ignore failure. It'll only happen if the sender goes away, 553 /* Ignore failure. It'll only happen if the sender goes away,
533 because our timeout is set to infinite. */ 554 because our timeout is set to infinite. */
534 netlink_unicast(audit_sock, reply->skb, reply->pid, 0); 555 netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0);
556 put_net(net);
535 kfree(reply); 557 kfree(reply);
536 return 0; 558 return 0;
537} 559}
538/** 560/**
539 * audit_send_reply - send an audit reply message via netlink 561 * audit_send_reply - send an audit reply message via netlink
540 * @pid: process id to send reply to 562 * @request_skb: skb of request we are replying to (used to target the reply)
541 * @seq: sequence number 563 * @seq: sequence number
542 * @type: audit message type 564 * @type: audit message type
543 * @done: done (last) flag 565 * @done: done (last) flag
@@ -545,12 +567,14 @@ static int audit_send_reply_thread(void *arg)
545 * @payload: payload data 567 * @payload: payload data
546 * @size: payload size 568 * @size: payload size
547 * 569 *
548 * Allocates an skb, builds the netlink message, and sends it to the pid. 570 * Allocates an skb, builds the netlink message, and sends it to the port id.
549 * No failure notifications. 571 * No failure notifications.
550 */ 572 */
551static void audit_send_reply(int pid, int seq, int type, int done, int multi, 573static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done,
552 const void *payload, int size) 574 int multi, const void *payload, int size)
553{ 575{
576 u32 portid = NETLINK_CB(request_skb).portid;
577 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
554 struct sk_buff *skb; 578 struct sk_buff *skb;
555 struct task_struct *tsk; 579 struct task_struct *tsk;
556 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), 580 struct audit_reply *reply = kmalloc(sizeof(struct audit_reply),
@@ -559,11 +583,12 @@ static void audit_send_reply(int pid, int seq, int type, int done, int multi,
559 if (!reply) 583 if (!reply)
560 return; 584 return;
561 585
562 skb = audit_make_reply(pid, seq, type, done, multi, payload, size); 586 skb = audit_make_reply(portid, seq, type, done, multi, payload, size);
563 if (!skb) 587 if (!skb)
564 goto out; 588 goto out;
565 589
566 reply->pid = pid; 590 reply->net = get_net(net);
591 reply->portid = portid;
567 reply->skb = skb; 592 reply->skb = skb;
568 593
569 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); 594 tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply");
@@ -652,8 +677,7 @@ static int audit_get_feature(struct sk_buff *skb)
652 677
653 seq = nlmsg_hdr(skb)->nlmsg_seq; 678 seq = nlmsg_hdr(skb)->nlmsg_seq;
654 679
655 audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_GET, 0, 0, 680 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &af, sizeof(af));
656 &af, sizeof(af));
657 681
658 return 0; 682 return 0;
659} 683}
@@ -663,8 +687,12 @@ static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature
663{ 687{
664 struct audit_buffer *ab; 688 struct audit_buffer *ab;
665 689
690 if (audit_enabled == AUDIT_OFF)
691 return;
692
666 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE); 693 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", 694 audit_log_task_info(ab, current);
695 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, 696 audit_feature_names[which], !!old_feature, !!new_feature,
669 !!old_lock, !!new_lock, res); 697 !!old_lock, !!new_lock, res);
670 audit_log_end(ab); 698 audit_log_end(ab);
@@ -694,7 +722,7 @@ static int audit_set_feature(struct sk_buff *skb)
694 old_lock = af.lock & feature; 722 old_lock = af.lock & feature;
695 723
696 /* are we changing a locked feature? */ 724 /* are we changing a locked feature? */
697 if ((af.lock & feature) && (new_feature != old_feature)) { 725 if (old_lock && (new_feature != old_feature)) {
698 audit_log_feature_change(i, old_feature, new_feature, 726 audit_log_feature_change(i, old_feature, new_feature,
699 old_lock, new_lock, 0); 727 old_lock, new_lock, 0);
700 return -EPERM; 728 return -EPERM;
@@ -732,7 +760,6 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
732{ 760{
733 u32 seq; 761 u32 seq;
734 void *data; 762 void *data;
735 struct audit_status *status_get, status_set;
736 int err; 763 int err;
737 struct audit_buffer *ab; 764 struct audit_buffer *ab;
738 u16 msg_type = nlh->nlmsg_type; 765 u16 msg_type = nlh->nlmsg_type;
@@ -758,48 +785,69 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
758 data = nlmsg_data(nlh); 785 data = nlmsg_data(nlh);
759 786
760 switch (msg_type) { 787 switch (msg_type) {
761 case AUDIT_GET: 788 case AUDIT_GET: {
762 memset(&status_set, 0, sizeof(status_set)); 789 struct audit_status s;
763 status_set.enabled = audit_enabled; 790 memset(&s, 0, sizeof(s));
764 status_set.failure = audit_failure; 791 s.enabled = audit_enabled;
765 status_set.pid = audit_pid; 792 s.failure = audit_failure;
766 status_set.rate_limit = audit_rate_limit; 793 s.pid = audit_pid;
767 status_set.backlog_limit = audit_backlog_limit; 794 s.rate_limit = audit_rate_limit;
768 status_set.lost = atomic_read(&audit_lost); 795 s.backlog_limit = audit_backlog_limit;
769 status_set.backlog = skb_queue_len(&audit_skb_queue); 796 s.lost = atomic_read(&audit_lost);
770 audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_GET, 0, 0, 797 s.backlog = skb_queue_len(&audit_skb_queue);
771 &status_set, sizeof(status_set)); 798 s.version = AUDIT_VERSION_LATEST;
799 s.backlog_wait_time = audit_backlog_wait_time;
800 audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s));
772 break; 801 break;
773 case AUDIT_SET: 802 }
774 if (nlmsg_len(nlh) < sizeof(struct audit_status)) 803 case AUDIT_SET: {
775 return -EINVAL; 804 struct audit_status s;
776 status_get = (struct audit_status *)data; 805 memset(&s, 0, sizeof(s));
777 if (status_get->mask & AUDIT_STATUS_ENABLED) { 806 /* guard against past and future API changes */
778 err = audit_set_enabled(status_get->enabled); 807 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
808 if (s.mask & AUDIT_STATUS_ENABLED) {
809 err = audit_set_enabled(s.enabled);
779 if (err < 0) 810 if (err < 0)
780 return err; 811 return err;
781 } 812 }
782 if (status_get->mask & AUDIT_STATUS_FAILURE) { 813 if (s.mask & AUDIT_STATUS_FAILURE) {
783 err = audit_set_failure(status_get->failure); 814 err = audit_set_failure(s.failure);
784 if (err < 0) 815 if (err < 0)
785 return err; 816 return err;
786 } 817 }
787 if (status_get->mask & AUDIT_STATUS_PID) { 818 if (s.mask & AUDIT_STATUS_PID) {
788 int new_pid = status_get->pid; 819 int new_pid = s.pid;
789 820
821 if ((!new_pid) && (task_tgid_vnr(current) != audit_pid))
822 return -EACCES;
790 if (audit_enabled != AUDIT_OFF) 823 if (audit_enabled != AUDIT_OFF)
791 audit_log_config_change("audit_pid", new_pid, audit_pid, 1); 824 audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
792 audit_pid = new_pid; 825 audit_pid = new_pid;
793 audit_nlk_portid = NETLINK_CB(skb).portid; 826 audit_nlk_portid = NETLINK_CB(skb).portid;
827 audit_sock = skb->sk;
794 } 828 }
795 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { 829 if (s.mask & AUDIT_STATUS_RATE_LIMIT) {
796 err = audit_set_rate_limit(status_get->rate_limit); 830 err = audit_set_rate_limit(s.rate_limit);
831 if (err < 0)
832 return err;
833 }
834 if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) {
835 err = audit_set_backlog_limit(s.backlog_limit);
836 if (err < 0)
837 return err;
838 }
839 if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) {
840 if (sizeof(s) > (size_t)nlh->nlmsg_len)
841 return -EINVAL;
842 if (s.backlog_wait_time < 0 ||
843 s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME)
844 return -EINVAL;
845 err = audit_set_backlog_wait_time(s.backlog_wait_time);
797 if (err < 0) 846 if (err < 0)
798 return err; 847 return err;
799 } 848 }
800 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
801 err = audit_set_backlog_limit(status_get->backlog_limit);
802 break; 849 break;
850 }
803 case AUDIT_GET_FEATURE: 851 case AUDIT_GET_FEATURE:
804 err = audit_get_feature(skb); 852 err = audit_get_feature(skb);
805 if (err) 853 if (err)
@@ -817,13 +865,14 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
817 return 0; 865 return 0;
818 866
819 err = audit_filter_user(msg_type); 867 err = audit_filter_user(msg_type);
820 if (err == 1) { 868 if (err == 1) { /* match or error */
821 err = 0; 869 err = 0;
822 if (msg_type == AUDIT_USER_TTY) { 870 if (msg_type == AUDIT_USER_TTY) {
823 err = tty_audit_push_current(); 871 err = tty_audit_push_current();
824 if (err) 872 if (err)
825 break; 873 break;
826 } 874 }
875 mutex_unlock(&audit_cmd_mutex);
827 audit_log_common_recv_msg(&ab, msg_type); 876 audit_log_common_recv_msg(&ab, msg_type);
828 if (msg_type != AUDIT_USER_TTY) 877 if (msg_type != AUDIT_USER_TTY)
829 audit_log_format(ab, " msg='%.*s'", 878 audit_log_format(ab, " msg='%.*s'",
@@ -839,8 +888,9 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
839 size--; 888 size--;
840 audit_log_n_untrustedstring(ab, data, size); 889 audit_log_n_untrustedstring(ab, data, size);
841 } 890 }
842 audit_set_pid(ab, NETLINK_CB(skb).portid); 891 audit_set_portid(ab, NETLINK_CB(skb).portid);
843 audit_log_end(ab); 892 audit_log_end(ab);
893 mutex_lock(&audit_cmd_mutex);
844 } 894 }
845 break; 895 break;
846 case AUDIT_ADD_RULE: 896 case AUDIT_ADD_RULE:
@@ -853,11 +903,12 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
853 audit_log_end(ab); 903 audit_log_end(ab);
854 return -EPERM; 904 return -EPERM;
855 } 905 }
856 /* fallthrough */ 906 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)); 907 seq, data, nlmsg_len(nlh));
860 break; 908 break;
909 case AUDIT_LIST_RULES:
910 err = audit_list_rules_send(skb, seq);
911 break;
861 case AUDIT_TRIM: 912 case AUDIT_TRIM:
862 audit_trim_trees(); 913 audit_trim_trees();
863 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); 914 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
@@ -921,8 +972,8 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
921 memcpy(sig_data->ctx, ctx, len); 972 memcpy(sig_data->ctx, ctx, len);
922 security_release_secctx(ctx, len); 973 security_release_secctx(ctx, len);
923 } 974 }
924 audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_SIGNAL_INFO, 975 audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0,
925 0, 0, sig_data, sizeof(*sig_data) + len); 976 sig_data, sizeof(*sig_data) + len);
926 kfree(sig_data); 977 kfree(sig_data);
927 break; 978 break;
928 case AUDIT_TTY_GET: { 979 case AUDIT_TTY_GET: {
@@ -934,25 +985,37 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
934 s.log_passwd = tsk->signal->audit_tty_log_passwd; 985 s.log_passwd = tsk->signal->audit_tty_log_passwd;
935 spin_unlock(&tsk->sighand->siglock); 986 spin_unlock(&tsk->sighand->siglock);
936 987
937 audit_send_reply(NETLINK_CB(skb).portid, seq, 988 audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
938 AUDIT_TTY_GET, 0, 0, &s, sizeof(s));
939 break; 989 break;
940 } 990 }
941 case AUDIT_TTY_SET: { 991 case AUDIT_TTY_SET: {
942 struct audit_tty_status s; 992 struct audit_tty_status s, old;
943 struct task_struct *tsk = current; 993 struct task_struct *tsk = current;
994 struct audit_buffer *ab;
944 995
945 memset(&s, 0, sizeof(s)); 996 memset(&s, 0, sizeof(s));
946 /* guard against past and future API changes */ 997 /* guard against past and future API changes */
947 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); 998 memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh)));
999 /* check if new data is valid */
948 if ((s.enabled != 0 && s.enabled != 1) || 1000 if ((s.enabled != 0 && s.enabled != 1) ||
949 (s.log_passwd != 0 && s.log_passwd != 1)) 1001 (s.log_passwd != 0 && s.log_passwd != 1))
950 return -EINVAL; 1002 err = -EINVAL;
951 1003
952 spin_lock(&tsk->sighand->siglock); 1004 spin_lock(&tsk->sighand->siglock);
953 tsk->signal->audit_tty = s.enabled; 1005 old.enabled = tsk->signal->audit_tty;
954 tsk->signal->audit_tty_log_passwd = s.log_passwd; 1006 old.log_passwd = tsk->signal->audit_tty_log_passwd;
1007 if (!err) {
1008 tsk->signal->audit_tty = s.enabled;
1009 tsk->signal->audit_tty_log_passwd = s.log_passwd;
1010 }
955 spin_unlock(&tsk->sighand->siglock); 1011 spin_unlock(&tsk->sighand->siglock);
1012
1013 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
1014 audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
1015 " old-log_passwd=%d new-log_passwd=%d res=%d",
1016 old.enabled, s.enabled, old.log_passwd,
1017 s.log_passwd, !err);
1018 audit_log_end(ab);
956 break; 1019 break;
957 } 1020 }
958 default: 1021 default:
@@ -998,24 +1061,55 @@ static void audit_receive(struct sk_buff *skb)
998 mutex_unlock(&audit_cmd_mutex); 1061 mutex_unlock(&audit_cmd_mutex);
999} 1062}
1000 1063
1001/* Initialize audit support at boot time. */ 1064static int __net_init audit_net_init(struct net *net)
1002static int __init audit_init(void)
1003{ 1065{
1004 int i;
1005 struct netlink_kernel_cfg cfg = { 1066 struct netlink_kernel_cfg cfg = {
1006 .input = audit_receive, 1067 .input = audit_receive,
1007 }; 1068 };
1008 1069
1070 struct audit_net *aunet = net_generic(net, audit_net_id);
1071
1072 aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
1073 if (aunet->nlsk == NULL) {
1074 audit_panic("cannot initialize netlink socket in namespace");
1075 return -ENOMEM;
1076 }
1077 aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1078 return 0;
1079}
1080
1081static void __net_exit audit_net_exit(struct net *net)
1082{
1083 struct audit_net *aunet = net_generic(net, audit_net_id);
1084 struct sock *sock = aunet->nlsk;
1085 if (sock == audit_sock) {
1086 audit_pid = 0;
1087 audit_sock = NULL;
1088 }
1089
1090 rcu_assign_pointer(aunet->nlsk, NULL);
1091 synchronize_net();
1092 netlink_kernel_release(sock);
1093}
1094
1095static struct pernet_operations audit_net_ops __net_initdata = {
1096 .init = audit_net_init,
1097 .exit = audit_net_exit,
1098 .id = &audit_net_id,
1099 .size = sizeof(struct audit_net),
1100};
1101
1102/* Initialize audit support at boot time. */
1103static int __init audit_init(void)
1104{
1105 int i;
1106
1009 if (audit_initialized == AUDIT_DISABLED) 1107 if (audit_initialized == AUDIT_DISABLED)
1010 return 0; 1108 return 0;
1011 1109
1012 printk(KERN_INFO "audit: initializing netlink socket (%s)\n", 1110 pr_info("initializing netlink subsys (%s)\n",
1013 audit_default ? "enabled" : "disabled"); 1111 audit_default ? "enabled" : "disabled");
1014 audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, &cfg); 1112 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 1113
1020 skb_queue_head_init(&audit_skb_queue); 1114 skb_queue_head_init(&audit_skb_queue);
1021 skb_queue_head_init(&audit_skb_hold_queue); 1115 skb_queue_head_init(&audit_skb_hold_queue);
@@ -1039,22 +1133,32 @@ static int __init audit_enable(char *str)
1039 if (!audit_default) 1133 if (!audit_default)
1040 audit_initialized = AUDIT_DISABLED; 1134 audit_initialized = AUDIT_DISABLED;
1041 1135
1042 printk(KERN_INFO "audit: %s", audit_default ? "enabled" : "disabled"); 1136 pr_info("%s\n", audit_default ?
1137 "enabled (after initialization)" : "disabled (until reboot)");
1043 1138
1044 if (audit_initialized == AUDIT_INITIALIZED) { 1139 return 1;
1045 audit_enabled = audit_default; 1140}
1046 audit_ever_enabled |= !!audit_default; 1141__setup("audit=", audit_enable);
1047 } else if (audit_initialized == AUDIT_UNINITIALIZED) { 1142
1048 printk(" (after initialization)"); 1143/* Process kernel command-line parameter at boot time.
1049 } else { 1144 * audit_backlog_limit=<n> */
1050 printk(" (until reboot)"); 1145static int __init audit_backlog_limit_set(char *str)
1146{
1147 u32 audit_backlog_limit_arg;
1148
1149 pr_info("audit_backlog_limit: ");
1150 if (kstrtouint(str, 0, &audit_backlog_limit_arg)) {
1151 pr_cont("using default of %u, unable to parse %s\n",
1152 audit_backlog_limit, str);
1153 return 1;
1051 } 1154 }
1052 printk("\n"); 1155
1156 audit_backlog_limit = audit_backlog_limit_arg;
1157 pr_cont("%d\n", audit_backlog_limit);
1053 1158
1054 return 1; 1159 return 1;
1055} 1160}
1056 1161__setup("audit_backlog_limit=", audit_backlog_limit_set);
1057__setup("audit=", audit_enable);
1058 1162
1059static void audit_buffer_free(struct audit_buffer *ab) 1163static void audit_buffer_free(struct audit_buffer *ab)
1060{ 1164{
@@ -1165,18 +1269,20 @@ static inline void audit_get_stamp(struct audit_context *ctx,
1165/* 1269/*
1166 * Wait for auditd to drain the queue a little 1270 * Wait for auditd to drain the queue a little
1167 */ 1271 */
1168static void wait_for_auditd(unsigned long sleep_time) 1272static long wait_for_auditd(long sleep_time)
1169{ 1273{
1170 DECLARE_WAITQUEUE(wait, current); 1274 DECLARE_WAITQUEUE(wait, current);
1171 set_current_state(TASK_UNINTERRUPTIBLE); 1275 set_current_state(TASK_UNINTERRUPTIBLE);
1172 add_wait_queue(&audit_backlog_wait, &wait); 1276 add_wait_queue_exclusive(&audit_backlog_wait, &wait);
1173 1277
1174 if (audit_backlog_limit && 1278 if (audit_backlog_limit &&
1175 skb_queue_len(&audit_skb_queue) > audit_backlog_limit) 1279 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
1176 schedule_timeout(sleep_time); 1280 sleep_time = schedule_timeout(sleep_time);
1177 1281
1178 __set_current_state(TASK_RUNNING); 1282 __set_current_state(TASK_RUNNING);
1179 remove_wait_queue(&audit_backlog_wait, &wait); 1283 remove_wait_queue(&audit_backlog_wait, &wait);
1284
1285 return sleep_time;
1180} 1286}
1181 1287
1182/** 1288/**
@@ -1200,7 +1306,8 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
1200 struct audit_buffer *ab = NULL; 1306 struct audit_buffer *ab = NULL;
1201 struct timespec t; 1307 struct timespec t;
1202 unsigned int uninitialized_var(serial); 1308 unsigned int uninitialized_var(serial);
1203 int reserve; 1309 int reserve = 5; /* Allow atomic callers to go up to five
1310 entries over the normal backlog limit */
1204 unsigned long timeout_start = jiffies; 1311 unsigned long timeout_start = jiffies;
1205 1312
1206 if (audit_initialized != AUDIT_INITIALIZED) 1313 if (audit_initialized != AUDIT_INITIALIZED)
@@ -1209,36 +1316,37 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
1209 if (unlikely(audit_filter_type(type))) 1316 if (unlikely(audit_filter_type(type)))
1210 return NULL; 1317 return NULL;
1211 1318
1212 if (gfp_mask & __GFP_WAIT) 1319 if (gfp_mask & __GFP_WAIT) {
1213 reserve = 0; 1320 if (audit_pid && audit_pid == current->pid)
1214 else 1321 gfp_mask &= ~__GFP_WAIT;
1215 reserve = 5; /* Allow atomic callers to go up to five 1322 else
1216 entries over the normal backlog limit */ 1323 reserve = 0;
1324 }
1217 1325
1218 while (audit_backlog_limit 1326 while (audit_backlog_limit
1219 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { 1327 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
1220 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) { 1328 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) {
1221 unsigned long sleep_time; 1329 long sleep_time;
1222 1330
1223 sleep_time = timeout_start + audit_backlog_wait_time - 1331 sleep_time = timeout_start + audit_backlog_wait_time - jiffies;
1224 jiffies; 1332 if (sleep_time > 0) {
1225 if ((long)sleep_time > 0) { 1333 sleep_time = wait_for_auditd(sleep_time);
1226 wait_for_auditd(sleep_time); 1334 if (sleep_time > 0)
1227 continue; 1335 continue;
1228 } 1336 }
1229 } 1337 }
1230 if (audit_rate_check() && printk_ratelimit()) 1338 if (audit_rate_check() && printk_ratelimit())
1231 printk(KERN_WARNING 1339 pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n",
1232 "audit: audit_backlog=%d > " 1340 skb_queue_len(&audit_skb_queue),
1233 "audit_backlog_limit=%d\n", 1341 audit_backlog_limit);
1234 skb_queue_len(&audit_skb_queue),
1235 audit_backlog_limit);
1236 audit_log_lost("backlog limit exceeded"); 1342 audit_log_lost("backlog limit exceeded");
1237 audit_backlog_wait_time = audit_backlog_wait_overflow; 1343 audit_backlog_wait_time = audit_backlog_wait_overflow;
1238 wake_up(&audit_backlog_wait); 1344 wake_up(&audit_backlog_wait);
1239 return NULL; 1345 return NULL;
1240 } 1346 }
1241 1347
1348 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
1349
1242 ab = audit_buffer_alloc(ctx, gfp_mask, type); 1350 ab = audit_buffer_alloc(ctx, gfp_mask, type);
1243 if (!ab) { 1351 if (!ab) {
1244 audit_log_lost("out of memory in audit_log_start"); 1352 audit_log_lost("out of memory in audit_log_start");
@@ -1356,7 +1464,6 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
1356 int i, avail, new_len; 1464 int i, avail, new_len;
1357 unsigned char *ptr; 1465 unsigned char *ptr;
1358 struct sk_buff *skb; 1466 struct sk_buff *skb;
1359 static const unsigned char *hex = "0123456789ABCDEF";
1360 1467
1361 if (!ab) 1468 if (!ab)
1362 return; 1469 return;
@@ -1374,10 +1481,8 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
1374 } 1481 }
1375 1482
1376 ptr = skb_tail_pointer(skb); 1483 ptr = skb_tail_pointer(skb);
1377 for (i=0; i<len; i++) { 1484 for (i = 0; i < len; i++)
1378 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ 1485 ptr = hex_byte_pack_upper(ptr, buf[i]);
1379 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
1380 }
1381 *ptr = 0; 1486 *ptr = 0;
1382 skb_put(skb, len << 1); /* new string is twice the old string */ 1487 skb_put(skb, len << 1); /* new string is twice the old string */
1383} 1488}
@@ -1491,7 +1596,7 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1491 1596
1492void audit_log_session_info(struct audit_buffer *ab) 1597void audit_log_session_info(struct audit_buffer *ab)
1493{ 1598{
1494 u32 sessionid = audit_get_sessionid(current); 1599 unsigned int sessionid = audit_get_sessionid(current);
1495 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); 1600 uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1496 1601
1497 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid); 1602 audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
@@ -1716,7 +1821,7 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1716 audit_log_format(ab, 1821 audit_log_format(ab,
1717 " ppid=%ld pid=%d auid=%u uid=%u gid=%u" 1822 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1718 " euid=%u suid=%u fsuid=%u" 1823 " euid=%u suid=%u fsuid=%u"
1719 " egid=%u sgid=%u fsgid=%u ses=%u tty=%s", 1824 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1720 sys_getppid(), 1825 sys_getppid(),
1721 tsk->pid, 1826 tsk->pid,
1722 from_kuid(&init_user_ns, audit_get_loginuid(tsk)), 1827 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
@@ -1728,7 +1833,7 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1728 from_kgid(&init_user_ns, cred->egid), 1833 from_kgid(&init_user_ns, cred->egid),
1729 from_kgid(&init_user_ns, cred->sgid), 1834 from_kgid(&init_user_ns, cred->sgid),
1730 from_kgid(&init_user_ns, cred->fsgid), 1835 from_kgid(&init_user_ns, cred->fsgid),
1731 audit_get_sessionid(tsk), tty); 1836 tty, audit_get_sessionid(tsk));
1732 1837
1733 get_task_comm(name, tsk); 1838 get_task_comm(name, tsk);
1734 audit_log_format(ab, " comm="); 1839 audit_log_format(ab, " comm=");
@@ -1739,7 +1844,8 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1739 if (mm->exe_file) 1844 if (mm->exe_file)
1740 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path); 1845 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
1741 up_read(&mm->mmap_sem); 1846 up_read(&mm->mmap_sem);
1742 } 1847 } else
1848 audit_log_format(ab, " exe=(null)");
1743 audit_log_task_context(ab); 1849 audit_log_task_context(ab);
1744} 1850}
1745EXPORT_SYMBOL(audit_log_task_info); 1851EXPORT_SYMBOL(audit_log_task_info);