diff options
author | Richard Guy Briggs <rgb@redhat.com> | 2013-07-16 13:18:45 -0400 |
---|---|---|
committer | Eric Paris <eparis@redhat.com> | 2014-01-13 22:27:24 -0500 |
commit | 33faba7fa7f2288d2f8aaea95958b2c97bf9ebfb (patch) | |
tree | fd6dd66d190aed6c01e974b9c07be163c10a5e65 | |
parent | 2f2ad1013322c8f6c40fc6dafdbd32442fa730ad (diff) |
audit: listen in all network namespaces
Convert audit from only listening in init_net to use register_pernet_subsys()
to dynamically manage the netlink socket list.
Signed-off-by: Richard Guy Briggs <rgb@redhat.com>
Signed-off-by: Eric Paris <eparis@redhat.com>
-rw-r--r-- | kernel/audit.c | 67 | ||||
-rw-r--r-- | kernel/audit.h | 5 | ||||
-rw-r--r-- | kernel/auditfilter.c | 1 |
3 files changed, 61 insertions, 12 deletions
diff --git a/kernel/audit.c b/kernel/audit.c index e88f599a2020..dd18747dde2b 100644 --- a/kernel/audit.c +++ b/kernel/audit.c | |||
@@ -63,6 +63,7 @@ | |||
63 | #include <linux/freezer.h> | 63 | #include <linux/freezer.h> |
64 | #include <linux/tty.h> | 64 | #include <linux/tty.h> |
65 | #include <linux/pid_namespace.h> | 65 | #include <linux/pid_namespace.h> |
66 | #include <net/netns/generic.h> | ||
66 | 67 | ||
67 | #include "audit.h" | 68 | #include "audit.h" |
68 | 69 | ||
@@ -121,6 +122,7 @@ static atomic_t audit_lost = ATOMIC_INIT(0); | |||
121 | 122 | ||
122 | /* The netlink socket. */ | 123 | /* The netlink socket. */ |
123 | static struct sock *audit_sock; | 124 | static struct sock *audit_sock; |
125 | int audit_net_id; | ||
124 | 126 | ||
125 | /* Hash for inode-based rules */ | 127 | /* Hash for inode-based rules */ |
126 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; | 128 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; |
@@ -176,6 +178,7 @@ struct audit_buffer { | |||
176 | 178 | ||
177 | struct audit_reply { | 179 | struct audit_reply { |
178 | __u32 portid; | 180 | __u32 portid; |
181 | pid_t pid; | ||
179 | struct sk_buff *skb; | 182 | struct sk_buff *skb; |
180 | }; | 183 | }; |
181 | 184 | ||
@@ -401,6 +404,7 @@ static void kauditd_send_skb(struct sk_buff *skb) | |||
401 | printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); | 404 | printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); |
402 | audit_log_lost("auditd disappeared\n"); | 405 | audit_log_lost("auditd disappeared\n"); |
403 | audit_pid = 0; | 406 | audit_pid = 0; |
407 | audit_sock = NULL; | ||
404 | /* we might get lucky and get this in the next auditd */ | 408 | /* we might get lucky and get this in the next auditd */ |
405 | audit_hold_skb(skb); | 409 | audit_hold_skb(skb); |
406 | } else | 410 | } else |
@@ -482,15 +486,16 @@ static int kauditd_thread(void *dummy) | |||
482 | int audit_send_list(void *_dest) | 486 | int audit_send_list(void *_dest) |
483 | { | 487 | { |
484 | struct audit_netlink_list *dest = _dest; | 488 | struct audit_netlink_list *dest = _dest; |
485 | __u32 portid = dest->portid; | ||
486 | struct sk_buff *skb; | 489 | struct sk_buff *skb; |
490 | struct net *net = get_net_ns_by_pid(dest->pid); | ||
491 | struct audit_net *aunet = net_generic(net, audit_net_id); | ||
487 | 492 | ||
488 | /* wait for parent to finish and send an ACK */ | 493 | /* wait for parent to finish and send an ACK */ |
489 | mutex_lock(&audit_cmd_mutex); | 494 | mutex_lock(&audit_cmd_mutex); |
490 | mutex_unlock(&audit_cmd_mutex); | 495 | mutex_unlock(&audit_cmd_mutex); |
491 | 496 | ||
492 | while ((skb = __skb_dequeue(&dest->q)) != NULL) | 497 | while ((skb = __skb_dequeue(&dest->q)) != NULL) |
493 | netlink_unicast(audit_sock, skb, portid, 0); | 498 | netlink_unicast(aunet->nlsk, skb, dest->portid, 0); |
494 | 499 | ||
495 | kfree(dest); | 500 | kfree(dest); |
496 | 501 | ||
@@ -525,13 +530,15 @@ out_kfree_skb: | |||
525 | static int audit_send_reply_thread(void *arg) | 530 | static int audit_send_reply_thread(void *arg) |
526 | { | 531 | { |
527 | struct audit_reply *reply = (struct audit_reply *)arg; | 532 | struct audit_reply *reply = (struct audit_reply *)arg; |
533 | struct net *net = get_net_ns_by_pid(reply->pid); | ||
534 | struct audit_net *aunet = net_generic(net, audit_net_id); | ||
528 | 535 | ||
529 | mutex_lock(&audit_cmd_mutex); | 536 | mutex_lock(&audit_cmd_mutex); |
530 | mutex_unlock(&audit_cmd_mutex); | 537 | mutex_unlock(&audit_cmd_mutex); |
531 | 538 | ||
532 | /* Ignore failure. It'll only happen if the sender goes away, | 539 | /* Ignore failure. It'll only happen if the sender goes away, |
533 | because our timeout is set to infinite. */ | 540 | because our timeout is set to infinite. */ |
534 | netlink_unicast(audit_sock, reply->skb, reply->portid, 0); | 541 | netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0); |
535 | kfree(reply); | 542 | kfree(reply); |
536 | return 0; | 543 | return 0; |
537 | } | 544 | } |
@@ -564,6 +571,7 @@ static void audit_send_reply(__u32 portid, int seq, int type, int done, | |||
564 | goto out; | 571 | goto out; |
565 | 572 | ||
566 | reply->portid = portid; | 573 | reply->portid = portid; |
574 | reply->pid = task_pid_vnr(current); | ||
567 | reply->skb = skb; | 575 | reply->skb = skb; |
568 | 576 | ||
569 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); | 577 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); |
@@ -791,6 +799,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |||
791 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); | 799 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); |
792 | audit_pid = new_pid; | 800 | audit_pid = new_pid; |
793 | audit_nlk_portid = NETLINK_CB(skb).portid; | 801 | audit_nlk_portid = NETLINK_CB(skb).portid; |
802 | audit_sock = NETLINK_CB(skb).sk; | ||
794 | } | 803 | } |
795 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { | 804 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { |
796 | err = audit_set_rate_limit(status_get->rate_limit); | 805 | err = audit_set_rate_limit(status_get->rate_limit); |
@@ -998,24 +1007,58 @@ static void audit_receive(struct sk_buff *skb) | |||
998 | mutex_unlock(&audit_cmd_mutex); | 1007 | mutex_unlock(&audit_cmd_mutex); |
999 | } | 1008 | } |
1000 | 1009 | ||
1001 | /* Initialize audit support at boot time. */ | 1010 | static int __net_init audit_net_init(struct net *net) |
1002 | static int __init audit_init(void) | ||
1003 | { | 1011 | { |
1004 | int i; | ||
1005 | struct netlink_kernel_cfg cfg = { | 1012 | struct netlink_kernel_cfg cfg = { |
1006 | .input = audit_receive, | 1013 | .input = audit_receive, |
1007 | }; | 1014 | }; |
1008 | 1015 | ||
1016 | struct audit_net *aunet = net_generic(net, audit_net_id); | ||
1017 | |||
1018 | pr_info("audit: initializing netlink socket in namespace\n"); | ||
1019 | |||
1020 | aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg); | ||
1021 | if (aunet->nlsk == NULL) | ||
1022 | return -ENOMEM; | ||
1023 | if (!aunet->nlsk) | ||
1024 | audit_panic("cannot initialize netlink socket in namespace"); | ||
1025 | else | ||
1026 | aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | ||
1027 | return 0; | ||
1028 | } | ||
1029 | |||
1030 | static void __net_exit audit_net_exit(struct net *net) | ||
1031 | { | ||
1032 | struct audit_net *aunet = net_generic(net, audit_net_id); | ||
1033 | struct sock *sock = aunet->nlsk; | ||
1034 | if (sock == audit_sock) { | ||
1035 | audit_pid = 0; | ||
1036 | audit_sock = NULL; | ||
1037 | } | ||
1038 | |||
1039 | rcu_assign_pointer(aunet->nlsk, NULL); | ||
1040 | synchronize_net(); | ||
1041 | netlink_kernel_release(sock); | ||
1042 | } | ||
1043 | |||
1044 | static struct pernet_operations __net_initdata audit_net_ops = { | ||
1045 | .init = audit_net_init, | ||
1046 | .exit = audit_net_exit, | ||
1047 | .id = &audit_net_id, | ||
1048 | .size = sizeof(struct audit_net), | ||
1049 | }; | ||
1050 | |||
1051 | /* Initialize audit support at boot time. */ | ||
1052 | static int __init audit_init(void) | ||
1053 | { | ||
1054 | int i; | ||
1055 | |||
1009 | if (audit_initialized == AUDIT_DISABLED) | 1056 | if (audit_initialized == AUDIT_DISABLED) |
1010 | return 0; | 1057 | return 0; |
1011 | 1058 | ||
1012 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", | 1059 | pr_info("audit: initializing netlink subsys (%s)\n", |
1013 | audit_default ? "enabled" : "disabled"); | 1060 | audit_default ? "enabled" : "disabled"); |
1014 | audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, &cfg); | 1061 | 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 | 1062 | ||
1020 | skb_queue_head_init(&audit_skb_queue); | 1063 | skb_queue_head_init(&audit_skb_queue); |
1021 | skb_queue_head_init(&audit_skb_hold_queue); | 1064 | skb_queue_head_init(&audit_skb_hold_queue); |
diff --git a/kernel/audit.h b/kernel/audit.h index c7282444caea..0719b4547221 100644 --- a/kernel/audit.h +++ b/kernel/audit.h | |||
@@ -247,11 +247,16 @@ extern void audit_panic(const char *message); | |||
247 | 247 | ||
248 | struct audit_netlink_list { | 248 | struct audit_netlink_list { |
249 | __u32 portid; | 249 | __u32 portid; |
250 | pid_t pid; | ||
250 | struct sk_buff_head q; | 251 | struct sk_buff_head q; |
251 | }; | 252 | }; |
252 | 253 | ||
253 | int audit_send_list(void *); | 254 | int audit_send_list(void *); |
254 | 255 | ||
256 | struct audit_net { | ||
257 | struct sock *nlsk; | ||
258 | }; | ||
259 | |||
255 | extern int selinux_audit_rule_update(void); | 260 | extern int selinux_audit_rule_update(void); |
256 | 261 | ||
257 | extern struct mutex audit_filter_mutex; | 262 | extern struct mutex audit_filter_mutex; |
diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c index 08f7f7bef26c..d085cfbe416e 100644 --- a/kernel/auditfilter.c +++ b/kernel/auditfilter.c | |||
@@ -1050,6 +1050,7 @@ int audit_receive_filter(int type, __u32 portid, int seq, void *data, | |||
1050 | if (!dest) | 1050 | if (!dest) |
1051 | return -ENOMEM; | 1051 | return -ENOMEM; |
1052 | dest->portid = portid; | 1052 | dest->portid = portid; |
1053 | dest->pid = task_pid_vnr(current); | ||
1053 | skb_queue_head_init(&dest->q); | 1054 | skb_queue_head_init(&dest->q); |
1054 | 1055 | ||
1055 | mutex_lock(&audit_filter_mutex); | 1056 | mutex_lock(&audit_filter_mutex); |