Skip to contentSkip to conversation

tuntap: fix possible deadlock when fail to register netdev

PATCH Superseded Jason Wang 2 participants 3 messages Dec 8, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 tuntap: fix possible deadlock when fail to register netdev

Private destructor could be called when register_netdev() fail with rtnl lock held. This will lead deadlock in tun_free_netdev() who tries to hold rtnl_lock. Fixing this by switching to use spinlock to synchronize.

Fixes 96f84061620c ("tun: add eBPF based queue selection method")
Cc Eric Dumazet <[email protected]>
Cc Willem de Bruijn <[email protected]>
---
 drivers/net/tun.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/tun.c +4 -3
index 787cc35..f7ccd79 100644
@@ -2050,8 +2050,11 @@ static int __tun_set_steering_ebpf(struct tun_struct *tun,
 		new->prog = prog;
 	}
 
-	old = rtnl_dereference(tun->steering_prog);
+	spin_lock(&tun->lock);
+	old = rcu_dereference_protected(tun->steering_prog,
+					lock_is_held(&tun->lock));
 	rcu_assign_pointer(tun->steering_prog, new);
+	spin_unlock(&tun->lock);
 
 	if (old)
 		call_rcu(&old->rcu, tun_steering_prog_free);
@@ -2067,9 +2070,7 @@ static void tun_free_netdev(struct net_device *dev)
 	free_percpu(tun->pcpu_stats);
 	tun_flow_uninit(tun);
 	security_tun_dev_free_security(tun->security);
-	rtnl_lock();
 	__tun_set_steering_ebpf(tun, NULL);
-	rtnl_unlock();
 }
 
 static void tun_setup(struct net_device *dev)
Signature · 1 line
2.7.4
#1 lore raw
Quoted 30 lines · Jason Wang

Private destructor could be called when register_netdev() fail with rtnl lock held. This will lead deadlock in tun_free_netdev() who tries
to hold rtnl_lock. Fixing this by switching to use spinlock to synchronize.

Fixes 96f84061620c ("tun: add eBPF based queue selection method")
Cc Eric Dumazet <[email protected]>
Cc Willem de Bruijn <[email protected]>

---
 drivers/net/tun.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/tun.c +0 -0
index 787cc35..f7ccd79 100644
@@ -2050,8 +2050,11 @@ static int __tun_set_steering_ebpf(struct
 tun_struct *tun,
  		new->prog = prog;
  	}

drivers/net/tun.c +3 -1
-	old = rtnl_dereference(tun->steering_prog);
+	spin_lock(&tun->lock);
+	old = rcu_dereference_protected(tun->steering_prog,
+					lock_is_held(&tun->lock));

  rcu_assign_pointer(tun->steering_prog, new);

drivers/net/tun.c static int __tun_set_steering_ebpf(struct tun_struct *tun, · line 2057

Quoted 1 line
drivers/net/tun.c +1 -0
+	spin_unlock(&tun->lock);

Hi Jason, thank you for the following up.

Have you tested this code path with lockdep enabled ?

My gut feeling is that you need spin_lock_bh() here.

Thanks

author
#2 lore raw
Quoted 34 lines · Eric Dumazet
Quoted 21 lines · Jason Wang

Private destructor could be called when register_netdev() fail with rtnl lock held. This will lead deadlock in tun_free_netdev() who tries
to hold rtnl_lock. Fixing this by switching to use spinlock to synchronize.

Fixes 96f84061620c ("tun: add eBPF based queue selection method")
Cc Eric Dumazet <[email protected]>
Cc Willem de Bruijn <[email protected]>
---
  drivers/net/tun.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/tun.c +0 -0
index 787cc35..f7ccd79 100644
@@ -2050,8 +2050,11 @@ static int __tun_set_steering_ebpf(struct
 tun_struct *tun,

drivers/net/tun.c static int __tun_set_steering_ebpf(struct · line 2059

Quoted 10 lines
drivers/net/tun.c +4 -1
  		new->prog = prog;
  	}
 
-	old = rtnl_dereference(tun->steering_prog);
+	spin_lock(&tun->lock);
+	old = rcu_dereference_protected(tun->steering_prog,
+					lock_is_held(&tun->lock));
  	rcu_assign_pointer(tun->steering_prog, new);
+	spin_unlock(&tun->lock);
 

Hi Jason, thank you for the following up.

Quoted 1 line

Have you tested this code path with lockdep enabled ?

No I test without it.

Quoted 5 lines

My gut feeling is that you need spin_lock_bh() here.

Thanks

Yes, I miss the fact this the lock is used by e.g flow caches too. Will post V2.

Thanks