Skip to contentSkip to conversation

flow_dissector: lookup netns by skb->sk if skb->dev is NULL

PATCH Applied Willem de Bruijn 4 participants 4 messages Sep 25, 2018 – Sep 27, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 flow_dissector: lookup netns by skb->sk if skb->dev is NULL

From: Willem de Bruijn <[email protected]>

BPF flow dissectors are configured per network namespace. __skb_flow_dissect looks up the netns through dev_net(skb->dev).

In some dissector paths skb->dev is NULL, such as for Unix sockets. In these cases fall back to looking up the netns by socket.

Analyzing the codepaths leading to __skb_flow_dissect I did not find a case where both skb->dev and skb->sk are NULL. Warn and fall back to standard flow dissector if one is found.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")
---
 net/core/flow_dissector.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/core/flow_dissector.c +9 -3
index 5c5dd74b5b3b..738c7562e1e0 100644
@@ -714,7 +714,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 	struct flow_dissector_key_vlan *key_vlan;
 	enum flow_dissect_ret fdret;
 	enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
-	struct bpf_prog *attached;
+	struct bpf_prog *attached = NULL;
 	int num_hdrs = 0;
 	u8 ip_proto = 0;
 	bool ret;
@@ -755,8 +755,14 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 					      target_container);
 
 	rcu_read_lock();
-	attached = skb ? rcu_dereference(dev_net(skb->dev)->flow_dissector_prog)
-		       : NULL;
+	if (skb) {
+		if (skb->dev)
+			attached = rcu_dereference(dev_net(skb->dev)->flow_dissector_prog);
+		else if (skb->sk)
+			attached = rcu_dereference(sock_net(skb->sk)->flow_dissector_prog);
+		else
+			WARN_ON_ONCE(1);
+	}
 	if (attached) {
 		/* Note that even though the const qualifier is discarded
 		 * throughout the execution of the BPF program, all changes(the
Signature · 1 line
2.19.0.444.g18242da7ef-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

BPF flow dissectors are configured per network namespace. __skb_flow_dissect looks up the netns through dev_net(skb->dev).

In some dissector paths skb->dev is NULL, such as for Unix sockets. In these cases fall back to looking up the netns by socket.

Quoted 7 lines

Analyzing the codepaths leading to __skb_flow_dissect I did not find a case where both skb->dev and skb->sk are NULL. Warn and fall back to standard flow dissector if one is found.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")
Quoted 37 lines
---
 net/core/flow_dissector.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/core/flow_dissector.c +9 -4
index 5c5dd74b5b3b..738c7562e1e0 100644
@@ -714,7 +714,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
        struct flow_dissector_key_vlan *key_vlan;
        enum flow_dissect_ret fdret;
        enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
-       struct bpf_prog *attached;
+       struct bpf_prog *attached = NULL;
        int num_hdrs = 0;
        u8 ip_proto = 0;
        bool ret;
@@ -755,8 +755,14 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
                                              target_container);
 
        rcu_read_lock();
-       attached = skb ? rcu_dereference(dev_net(skb->dev)->flow_dissector_prog)
-                      : NULL;
+       if (skb) {
+               if (skb->dev)
+                       attached = rcu_dereference(dev_net(skb->dev)->flow_dissector_prog);
+               else if (skb->sk)
+                       attached = rcu_dereference(sock_net(skb->sk)->flow_dissector_prog);
+               else
+                       WARN_ON_ONCE(1);
+       }
        if (attached) {
                /* Note that even though the const qualifier is discarded
                 * throughout the execution of the BPF program, all changes(the
--

2.19.0.444.g18242da7ef-goog

#2 lore raw
In reply to Willem de Bruijn
Quoted 8 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

BPF flow dissectors are configured per network namespace. __skb_flow_dissect looks up the netns through dev_net(skb->dev).

In some dissector paths skb->dev is NULL, such as for Unix sockets. In these cases fall back to looking up the netns by socket.

Quoted 7 lines

Analyzing the codepaths leading to __skb_flow_dissect I did not find a case where both skb->dev and skb->sk are NULL. Warn and fall back to standard flow dissector if one is found.

Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")

Applied to bpf-next, thanks Willem!

#3 lore raw
Quoted 13 lines · Daniel Borkmann
Quoted 12 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

BPF flow dissectors are configured per network namespace. __skb_flow_dissect looks up the netns through dev_net(skb->dev).

In some dissector paths skb->dev is NULL, such as for Unix sockets. In these cases fall back to looking up the netns by socket.

Analyzing the codepaths leading to __skb_flow_dissect I did not find a case where both skb->dev and skb->sk are NULL. Warn and fall back to standard flow dissector if one is found.

Quoted 6 lines
Quoted 3 lines
Fixes d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")

Applied to bpf-next, thanks Willem!

Sadly lib/test_bpf should still cause crashes, because populate_skb() populates an skb attached to a fake device, for which net pointer is NULL.