Skip to contentSkip to conversation

bpf: devmap: fix wrong interface selection in notifier_call

PATCH Superseded Taehee Yoo 2 participants 3 messages Oct 23, 2018 – Oct 24, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Taehee Yoo

author
#0 lore raw

PATCH 1/1 bpf: devmap: fix wrong interface selection in notifier_call

The dev_map_notification() removes interface in devmap if unregistering interface's ifindex is same.
But only checking ifindex is not enough because other netns can have same ifindex. so that wrong interface selection could occurred. Hence the net_eq() is needed.

Fixes 2ddf71e23cc2 ("net: add notifier hooks for devmap bpf map")
---
 kernel/bpf/devmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/devmap.c +2 -1
index 141710b82a6c..0d9211e49a4a 100644
@@ -496,6 +496,7 @@ static int dev_map_notification(struct notifier_block *notifier,
 				ulong event, void *ptr)
 {
 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
+	struct net *net = dev_net(netdev);
 	struct bpf_dtab *dtab;
 	int i;
 
@@ -512,7 +513,7 @@ static int dev_map_notification(struct notifier_block *notifier,
 				struct bpf_dtab_netdev *dev, *odev;
 
 				dev = READ_ONCE(dtab->netdev_map[i]);
-				if (!dev ||
+				if (!dev || !net_eq(net, dev_net(dev->dev)) ||
 				    dev->dev->ifindex != netdev->ifindex)
 					continue;
 				odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
Signature · 1 line
2.17.1
maintainer
#1 lore raw

[ +John ]

Quoted 25 lines · Taehee Yoo

The dev_map_notification() removes interface in devmap if unregistering interface's ifindex is same.
But only checking ifindex is not enough because other netns can have same ifindex. so that wrong interface selection could occurred. Hence the net_eq() is needed.

Fixes 2ddf71e23cc2 ("net: add notifier hooks for devmap bpf map")
---
 kernel/bpf/devmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/devmap.c +1 -0
index 141710b82a6c..0d9211e49a4a 100644
@@ -496,6 +496,7 @@ static int dev_map_notification(struct notifier_block *notifier,
 				ulong event, void *ptr)
 {
 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
+	struct net *net = dev_net(netdev);
 	struct bpf_dtab *dtab;
 	int i;
 

kernel/bpf/devmap.c static int dev_map_notification(struct notifier_block *notifier, · line 517

Quoted 6 lines
kernel/bpf/devmap.c +1 -1
@@ -512,7 +513,7 @@ static int dev_map_notification(struct notifier_block *notifier,
 				struct bpf_dtab_netdev *dev, *odev;
 
 				dev = READ_ONCE(dtab->netdev_map[i]);
-				if (!dev ||
+				if (!dev || !net_eq(net, dev_net(dev->dev)) ||
 				    dev->dev->ifindex != netdev->ifindex)

Can't we just compare netdev pointers directly here?

Quoted 3 lines
kernel/bpf/devmap.c +0 -0
 					continue;
 				odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
author
#2 lore raw

Hi Daniel!

Quoted 31 lines · Daniel Borkmann

[ +John ]

Quoted 27 lines · Taehee Yoo

The dev_map_notification() removes interface in devmap if unregistering interface's ifindex is same.
But only checking ifindex is not enough because other netns can have same ifindex. so that wrong interface selection could occurred. Hence the net_eq() is needed.

Fixes 2ddf71e23cc2 ("net: add notifier hooks for devmap bpf map")
---
 kernel/bpf/devmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/devmap.c +1 -0
index 141710b82a6c..0d9211e49a4a 100644
@@ -496,6 +496,7 @@ static int dev_map_notification(struct notifier_block *notifier,
                              ulong event, void *ptr)
 {
      struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
+     struct net *net = dev_net(netdev);
      struct bpf_dtab *dtab;
      int i;
 
@@ -512,7 +513,7 @@ static int dev_map_notification(struct notifier_block *notifier,
                              struct bpf_dtab_netdev *dev, *odev;
 
Quoted 7 lines

kernel/bpf/devmap.c static int dev_map_notification(struct notifier_block *notifier, · line 517

Quoted 4 lines
kernel/bpf/devmap.c +1 -1
                              dev = READ_ONCE(dtab->netdev_map[i]);
-                             if (!dev ||
+                             if (!dev || !net_eq(net, dev_net(dev->dev)) ||
                                  dev->dev->ifindex != netdev->ifindex)

Can't we just compare netdev pointers directly here?

Thank you for review!
I tested what you suggested, It works well and it's more simple. So that I will send v2 patch that just compares netdev pointer instead of using ifindex and net_eq().

Quoted 4 lines

kernel/bpf/devmap.c static int dev_map_notification(struct notifier_block *notifier, · line 519

Quoted 3 lines
kernel/bpf/devmap.c +0 -0
                                      continue;
                              odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);

Thanks!