Skip to content

Commit 33faba7

Browse files
rgbriggseparis
authored andcommitted
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>
1 parent 2f2ad10 commit 33faba7

3 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎kernel/audit.c‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include <linux/freezer.h>
6464
#include <linux/tty.h>
6565
#include <linux/pid_namespace.h>
66+
#include <net/netns/generic.h>
6667

6768
#include "audit.h"
6869

@@ -121,6 +122,7 @@ static atomic_t audit_lost = ATOMIC_INIT(0);
121122

122123
/* The netlink socket. */
123124
static struct sock *audit_sock;
125+
int audit_net_id;
124126

125127
/* Hash for inode-based rules */
126128
struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
@@ -176,6 +178,7 @@ struct audit_buffer {
176178

177179
struct audit_reply {
178180
__u32 portid;
181+
pid_t pid;
179182
struct sk_buff *skb;
180183
};
181184

@@ -401,6 +404,7 @@ static void kauditd_send_skb(struct sk_buff *skb)
401404
printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
402405
audit_log_lost("auditd disappeared\n");
403406
audit_pid = 0;
407+
audit_sock = NULL;
404408
/* we might get lucky and get this in the next auditd */
405409
audit_hold_skb(skb);
406410
} else
@@ -482,15 +486,16 @@ static int kauditd_thread(void *dummy)
482486
int audit_send_list(void *_dest)
483487
{
484488
struct audit_netlink_list *dest = _dest;
485-
__u32 portid = dest->portid;
486489
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);
487492

488493
/* wait for parent to finish and send an ACK */
489494
mutex_lock(&audit_cmd_mutex);
490495
mutex_unlock(&audit_cmd_mutex);
491496

492497
while ((skb = __skb_dequeue(&dest->q)) != NULL)
493-
netlink_unicast(audit_sock, skb, portid, 0);
498+
netlink_unicast(aunet->nlsk, skb, dest->portid, 0);
494499

495500
kfree(dest);
496501

@@ -525,13 +530,15 @@ struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done,
525530
static int audit_send_reply_thread(void *arg)
526531
{
527532
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);
528535

529536
mutex_lock(&audit_cmd_mutex);
530537
mutex_unlock(&audit_cmd_mutex);
531538

532539
/* Ignore failure. It'll only happen if the sender goes away,
533540
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);
535542
kfree(reply);
536543
return 0;
537544
}
@@ -564,6 +571,7 @@ static void audit_send_reply(__u32 portid, int seq, int type, int done,
564571
goto out;
565572

566573
reply->portid = portid;
574+
reply->pid = task_pid_vnr(current);
567575
reply->skb = skb;
568576

569577
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)
791799
audit_log_config_change("audit_pid", new_pid, audit_pid, 1);
792800
audit_pid = new_pid;
793801
audit_nlk_portid = NETLINK_CB(skb).portid;
802+
audit_sock = NETLINK_CB(skb).sk;
794803
}
795804
if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) {
796805
err = audit_set_rate_limit(status_get->rate_limit);
@@ -998,24 +1007,58 @@ static void audit_receive(struct sk_buff *skb)
9981007
mutex_unlock(&audit_cmd_mutex);
9991008
}
10001009

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)
10031011
{
1004-
int i;
10051012
struct netlink_kernel_cfg cfg = {
10061013
.input = audit_receive,
10071014
};
10081015

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+
10091056
if (audit_initialized == AUDIT_DISABLED)
10101057
return 0;
10111058

1012-
printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
1059+
pr_info("audit: initializing netlink subsys (%s)\n",
10131060
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);
10191062

10201063
skb_queue_head_init(&audit_skb_queue);
10211064
skb_queue_head_init(&audit_skb_hold_queue);

‎kernel/audit.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,16 @@ extern void audit_panic(const char *message);
247247

248248
struct audit_netlink_list {
249249
__u32 portid;
250+
pid_t pid;
250251
struct sk_buff_head q;
251252
};
252253

253254
int audit_send_list(void *);
254255

256+
struct audit_net {
257+
struct sock *nlsk;
258+
};
259+
255260
extern int selinux_audit_rule_update(void);
256261

257262
extern struct mutex audit_filter_mutex;

‎kernel/auditfilter.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ int audit_receive_filter(int type, __u32 portid, int seq, void *data,
10501050
if (!dest)
10511051
return -ENOMEM;
10521052
dest->portid = portid;
1053+
dest->pid = task_pid_vnr(current);
10531054
skb_queue_head_init(&dest->q);
10541055

10551056
mutex_lock(&audit_filter_mutex);

0 commit comments

Comments
 (0)