Skip to contentSkip to conversation

tuntap: fix possible deadlock when fail to register netdev

PATCH Applied Jason Wang 4 participants 4 messages Dec 8, 2017

Subsystems (3)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH v2 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..8d85163 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_bh(&tun->lock);
+	old = rcu_dereference_protected(tun->steering_prog,
+					lockdep_is_held(&tun->lock));
 	rcu_assign_pointer(tun->steering_prog, new);
+	spin_unlock_bh(&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
Acked-by on v2 1/1
#1 lore raw
Quoted 10 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]>
Quoted 33 lines
---
 drivers/net/tun.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/tun.c +4 -3
index 787cc35..8d85163 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_bh(&tun->lock);
+	old = rcu_dereference_protected(tun->steering_prog,
+					lockdep_is_held(&tun->lock));
 	rcu_assign_pointer(tun->steering_prog, new);
+	spin_unlock_bh(&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
Reviewed-by on v2 1/1
#3 lore raw
In reply to Jason Wang
Quoted 6 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.

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

---

Thanks.

maintainer Applied
#2 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Fri, 8 Dec 2017 06:43:44 +0200

Quoted 6 lines · Jason Wang
Quoted 5 lines

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.

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

Applied, thanks.