|
63 | 63 | #include <linux/freezer.h> |
64 | 64 | #include <linux/tty.h> |
65 | 65 | #include <linux/pid_namespace.h> |
| 66 | +#include <net/netns/generic.h> |
66 | 67 |
|
67 | 68 | #include "audit.h" |
68 | 69 |
|
@@ -121,6 +122,7 @@ static atomic_t audit_lost = ATOMIC_INIT(0); |
121 | 122 |
|
122 | 123 | /* The netlink socket. */ |
123 | 124 | static struct sock *audit_sock; |
| 125 | +int audit_net_id; |
124 | 126 |
|
125 | 127 | /* Hash for inode-based rules */ |
126 | 128 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; |
@@ -176,6 +178,7 @@ struct audit_buffer { |
176 | 178 |
|
177 | 179 | struct audit_reply { |
178 | 180 | __u32 portid; |
| 181 | + pid_t pid; |
179 | 182 | struct sk_buff *skb; |
180 | 183 | }; |
181 | 184 |
|
@@ -401,6 +404,7 @@ static void kauditd_send_skb(struct sk_buff *skb) |
401 | 404 | printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); |
402 | 405 | audit_log_lost("auditd disappeared\n"); |
403 | 406 | audit_pid = 0; |
| 407 | + audit_sock = NULL; |
404 | 408 | /* we might get lucky and get this in the next auditd */ |
405 | 409 | audit_hold_skb(skb); |
406 | 410 | } else |
@@ -482,15 +486,16 @@ static int kauditd_thread(void *dummy) |
482 | 486 | int audit_send_list(void *_dest) |
483 | 487 | { |
484 | 488 | struct audit_netlink_list *dest = _dest; |
485 | | - __u32 portid = dest->portid; |
486 | 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 | 493 | /* wait for parent to finish and send an ACK */ |
489 | 494 | mutex_lock(&audit_cmd_mutex); |
490 | 495 | mutex_unlock(&audit_cmd_mutex); |
491 | 496 |
|
492 | 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 | 500 | kfree(dest); |
496 | 501 |
|
@@ -525,13 +530,15 @@ struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done, |
525 | 530 | static int audit_send_reply_thread(void *arg) |
526 | 531 | { |
527 | 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 | 536 | mutex_lock(&audit_cmd_mutex); |
530 | 537 | mutex_unlock(&audit_cmd_mutex); |
531 | 538 |
|
532 | 539 | /* Ignore failure. It'll only happen if the sender goes away, |
533 | 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 | 542 | kfree(reply); |
536 | 543 | return 0; |
537 | 544 | } |
@@ -564,6 +571,7 @@ static void audit_send_reply(__u32 portid, int seq, int type, int done, |
564 | 571 | goto out; |
565 | 572 |
|
566 | 573 | reply->portid = portid; |
| 574 | + reply->pid = task_pid_vnr(current); |
567 | 575 | reply->skb = skb; |
568 | 576 |
|
569 | 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 | 799 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); |
792 | 800 | audit_pid = new_pid; |
793 | 801 | audit_nlk_portid = NETLINK_CB(skb).portid; |
| 802 | + audit_sock = NETLINK_CB(skb).sk; |
794 | 803 | } |
795 | 804 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { |
796 | 805 | err = audit_set_rate_limit(status_get->rate_limit); |
@@ -998,24 +1007,58 @@ static void audit_receive(struct sk_buff *skb) |
998 | 1007 | mutex_unlock(&audit_cmd_mutex); |
999 | 1008 | } |
1000 | 1009 |
|
1001 | | -/* Initialize audit support at boot time. */ |
1002 | | -static int __init audit_init(void) |
| 1010 | +static int __net_init audit_net_init(struct net *net) |
1003 | 1011 | { |
1004 | | - int i; |
1005 | 1012 | struct netlink_kernel_cfg cfg = { |
1006 | 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 | 1056 | if (audit_initialized == AUDIT_DISABLED) |
1010 | 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 | 1060 | audit_default ? "enabled" : "disabled"); |
1014 | | - audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, &cfg); |
1015 | | - if (!audit_sock) |
1016 | | - audit_panic("cannot initialize netlink socket"); |
1017 | | - else |
1018 | | - audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; |
| 1061 | + register_pernet_subsys(&audit_net_ops); |
1019 | 1062 |
|
1020 | 1063 | skb_queue_head_init(&audit_skb_queue); |
1021 | 1064 | skb_queue_head_init(&audit_skb_hold_queue); |
|
0 commit comments