Skip to content

Commit b5f689d

Browse files
qsngregkh
authored andcommitted
l2tp: fix race condition in l2tp_tunnel_delete
[ Upstream commit 62b982eeb4589b2e6d7c01a90590e3a4c2b2ca19 ] If we try to delete the same tunnel twice, the first delete operation does a lookup (l2tp_tunnel_get), finds the tunnel, calls l2tp_tunnel_delete, which queues it for deletion by l2tp_tunnel_del_work. The second delete operation also finds the tunnel and calls l2tp_tunnel_delete. If the workqueue has already fired and started running l2tp_tunnel_del_work, then l2tp_tunnel_delete will queue the same tunnel a second time, and try to free the socket again. Add a dead flag to prevent firing the workqueue twice. Then we can remove the check of queue_work's result that was meant to prevent that race but doesn't. Reproducer: ip l2tp add tunnel tunnel_id 3000 peer_tunnel_id 4000 local 192.168.0.2 remote 192.168.0.1 encap udp udp_sport 5000 udp_dport 6000 ip l2tp add session name l2tp1 tunnel_id 3000 session_id 1000 peer_session_id 2000 ip link set l2tp1 up ip l2tp del tunnel tunnel_id 3000 ip l2tp del tunnel tunnel_id 3000 Fixes: f8ccac0 ("l2tp: put tunnel socket release on a workqueue") Reported-by: Jianlin Shi <jishi@redhat.com> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net> Acked-by: Guillaume Nault <g.nault@alphalink.fr> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 110cf3d commit b5f689d

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

net/l2tp/l2tp_core.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,14 +1643,12 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
16431643

16441644
/* This function is used by the netlink TUNNEL_DELETE command.
16451645
*/
1646-
int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1646+
void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
16471647
{
1648-
l2tp_tunnel_inc_refcount(tunnel);
1649-
if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1650-
l2tp_tunnel_dec_refcount(tunnel);
1651-
return 1;
1648+
if (!test_and_set_bit(0, &tunnel->dead)) {
1649+
l2tp_tunnel_inc_refcount(tunnel);
1650+
queue_work(l2tp_wq, &tunnel->del_work);
16521651
}
1653-
return 0;
16541652
}
16551653
EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
16561654

net/l2tp/l2tp_core.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ struct l2tp_tunnel_cfg {
169169

170170
struct l2tp_tunnel {
171171
int magic; /* Should be L2TP_TUNNEL_MAGIC */
172+
173+
unsigned long dead;
174+
172175
struct rcu_head rcu;
173176
rwlock_t hlist_lock; /* protect session_hlist */
174177
struct hlist_head session_hlist[L2TP_HASH_SIZE];
@@ -253,7 +256,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
253256
u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
254257
struct l2tp_tunnel **tunnelp);
255258
void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
256-
int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
259+
void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
257260
struct l2tp_session *l2tp_session_create(int priv_size,
258261
struct l2tp_tunnel *tunnel,
259262
u32 session_id, u32 peer_session_id,

0 commit comments

Comments
 (0)