[PATCH] netlink: Add netns check on taps

From: Kevin Cernekee
Date: Tue Dec 05 2017 - 17:55:25 EST


Currently, a nlmon link inside a child namespace can observe systemwide
netlink activity. Filter the traffic so that in a non-init netns,
nlmon can only sniff netlink messages from its own netns.

Test case:

vpnns -- bash -c "ip link add nlmon0 type nlmon; \
ip link set nlmon0 up; \
tcpdump -i nlmon0 -q -w /tmp/nlmon.pcap -U" &
sudo ip xfrm state add src 10.1.1.1 dst 10.1.1.2 proto esp \
spi 0x1 mode transport \
auth sha1 0x6162633132330000000000000000000000000000 \
enc aes 0x00000000000000000000000000000000
grep abc123 /tmp/nlmon.pcap

Signed-off-by: Kevin Cernekee <cernekee@xxxxxxxxxxxx>
---
net/netlink/af_netlink.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index b9e0ee4..88381a2 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -253,6 +253,11 @@ static int __netlink_deliver_tap_skb(struct sk_buff *skb,
struct sock *sk = skb->sk;
int ret = -ENOMEM;

+ if (!net_eq(dev_net(dev), sock_net(sk)) &&
+ !net_eq(dev_net(dev), &init_net)) {
+ return 0;
+ }
+
dev_hold(dev);

if (is_vmalloc_addr(skb->head))
--
2.7.4