1 From foo@baz Mon Oct 9 09:32:35 CEST 2017
2 From: Sabrina Dubroca <sd@queasysnail.net>
3 Date: Tue, 26 Sep 2017 16:16:43 +0200
4 Subject: l2tp: fix race condition in l2tp_tunnel_delete
6 From: Sabrina Dubroca <sd@queasysnail.net>
9 [ Upstream commit 62b982eeb4589b2e6d7c01a90590e3a4c2b2ca19 ]
11 If we try to delete the same tunnel twice, the first delete operation
12 does a lookup (l2tp_tunnel_get), finds the tunnel, calls
13 l2tp_tunnel_delete, which queues it for deletion by
16 The second delete operation also finds the tunnel and calls
17 l2tp_tunnel_delete. If the workqueue has already fired and started
18 running l2tp_tunnel_del_work, then l2tp_tunnel_delete will queue the
19 same tunnel a second time, and try to free the socket again.
21 Add a dead flag to prevent firing the workqueue twice. Then we can
22 remove the check of queue_work's result that was meant to prevent that
27 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
28 ip l2tp add session name l2tp1 tunnel_id 3000 session_id 1000 peer_session_id 2000
30 ip l2tp del tunnel tunnel_id 3000
31 ip l2tp del tunnel tunnel_id 3000
33 Fixes: f8ccac0e4493 ("l2tp: put tunnel socket release on a workqueue")
34 Reported-by: Jianlin Shi <jishi@redhat.com>
35 Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
36 Acked-by: Guillaume Nault <g.nault@alphalink.fr>
37 Signed-off-by: David S. Miller <davem@davemloft.net>
38 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
40 net/l2tp/l2tp_core.c | 10 ++++------
41 net/l2tp/l2tp_core.h | 5 ++++-
42 2 files changed, 8 insertions(+), 7 deletions(-)
44 --- a/net/l2tp/l2tp_core.c
45 +++ b/net/l2tp/l2tp_core.c
46 @@ -1665,14 +1665,12 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
48 /* This function is used by the netlink TUNNEL_DELETE command.
50 -int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
51 +void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
53 - l2tp_tunnel_inc_refcount(tunnel);
54 - if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
55 - l2tp_tunnel_dec_refcount(tunnel);
57 + if (!test_and_set_bit(0, &tunnel->dead)) {
58 + l2tp_tunnel_inc_refcount(tunnel);
59 + queue_work(l2tp_wq, &tunnel->del_work);
63 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
65 --- a/net/l2tp/l2tp_core.h
66 +++ b/net/l2tp/l2tp_core.h
67 @@ -160,6 +160,9 @@ struct l2tp_tunnel_cfg {
70 int magic; /* Should be L2TP_TUNNEL_MAGIC */
75 rwlock_t hlist_lock; /* protect session_hlist */
76 struct hlist_head session_hlist[L2TP_HASH_SIZE];
77 @@ -248,7 +251,7 @@ int l2tp_tunnel_create(struct net *net,
78 u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
79 struct l2tp_tunnel **tunnelp);
80 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
81 -int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
82 +void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
83 struct l2tp_session *l2tp_session_create(int priv_size,
84 struct l2tp_tunnel *tunnel,
85 u32 session_id, u32 peer_session_id,