diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2017-09-22 09:39:24 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-09-25 17:44:41 -0400 |
commit | b228a94066406b6c456321d69643b0d7ce11cfa6 (patch) | |
tree | bff46ce62caf374bf2bf49e8f8a8110696c2a6d2 | |
parent | cdd10c9627496ad25c87ce6394e29752253c69d3 (diff) |
l2tp: fix race between l2tp_session_delete() and l2tp_tunnel_closeall()
There are several ways to remove L2TP sessions:
* deleting a session explicitly using the netlink interface (with
L2TP_CMD_SESSION_DELETE),
* deleting the session's parent tunnel (either by closing the
tunnel's file descriptor or using the netlink interface),
* closing the PPPOL2TP file descriptor of a PPP pseudo-wire.
In some cases, when these methods are used concurrently on the same
session, the session can be removed twice, leading to use-after-free
bugs.
This patch adds a 'dead' flag, used by l2tp_session_delete() and
l2tp_tunnel_closeall() to prevent them from stepping on each other's
toes.
The session deletion path used when closing a PPPOL2TP file descriptor
doesn't need to be adapted. It already has to ensure that a session
remains valid for the lifetime of its PPPOL2TP file descriptor.
So it takes an extra reference on the session in the ->session_close()
callback (pppol2tp_session_close()), which is eventually dropped
in the ->sk_destruct() callback of the PPPOL2TP socket
(pppol2tp_session_destruct()).
Still, __l2tp_session_unhash() and l2tp_session_queue_purge() can be
called twice and even concurrently for a given session, but thanks to
proper locking and re-initialisation of list fields, this is not an
issue.
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | net/l2tp/l2tp_core.c | 6 | ||||
-rw-r--r-- | net/l2tp/l2tp_core.h | 1 |
2 files changed, 7 insertions, 0 deletions
diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index ee485df73ccd..d8c2a89a76e1 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c | |||
@@ -1314,6 +1314,9 @@ again: | |||
1314 | 1314 | ||
1315 | hlist_del_init(&session->hlist); | 1315 | hlist_del_init(&session->hlist); |
1316 | 1316 | ||
1317 | if (test_and_set_bit(0, &session->dead)) | ||
1318 | goto again; | ||
1319 | |||
1317 | if (session->ref != NULL) | 1320 | if (session->ref != NULL) |
1318 | (*session->ref)(session); | 1321 | (*session->ref)(session); |
1319 | 1322 | ||
@@ -1750,6 +1753,9 @@ EXPORT_SYMBOL_GPL(__l2tp_session_unhash); | |||
1750 | */ | 1753 | */ |
1751 | int l2tp_session_delete(struct l2tp_session *session) | 1754 | int l2tp_session_delete(struct l2tp_session *session) |
1752 | { | 1755 | { |
1756 | if (test_and_set_bit(0, &session->dead)) | ||
1757 | return 0; | ||
1758 | |||
1753 | if (session->ref) | 1759 | if (session->ref) |
1754 | (*session->ref)(session); | 1760 | (*session->ref)(session); |
1755 | __l2tp_session_unhash(session); | 1761 | __l2tp_session_unhash(session); |
diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index a305e0c5925a..70a12df40a5f 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h | |||
@@ -76,6 +76,7 @@ struct l2tp_session_cfg { | |||
76 | struct l2tp_session { | 76 | struct l2tp_session { |
77 | int magic; /* should be | 77 | int magic; /* should be |
78 | * L2TP_SESSION_MAGIC */ | 78 | * L2TP_SESSION_MAGIC */ |
79 | long dead; | ||
79 | 80 | ||
80 | struct l2tp_tunnel *tunnel; /* back pointer to tunnel | 81 | struct l2tp_tunnel *tunnel; /* back pointer to tunnel |
81 | * context */ | 82 | * context */ |