Patch "l2tp: fix duplicate session creation" has been added to the 4.9-stable tree
Subsystems (3)
This is a note to let you know that I've just added the patch titled
l2tp: fix duplicate session creation
to the 4.9-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
l2tp-fix-duplicate-session-creation.patch
and it can be found in the queue-4.9 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
Quoted 1 line
From
dbdbc73b44782e22b3b4b6e8b51e7a3d245f3086Mon Sep 17 00:00:00 2001
From: Guillaume Nault <[email protected]>
Date: Fri, 31 Mar 2017 13:02:27 +0200
Subject: l2tp: fix duplicate session creation
From: Guillaume Nault <[email protected]>
commit dbdbc73b44782e22b3b4b6e8b51e7a3d245f3086 upstream.
l2tp_session_create() relies on its caller for checking for duplicate sessions. This is racy since a session can be concurrently inserted after the caller's verification.
Fix this by letting l2tp_session_create() verify sessions uniqueness upon insertion. Callers need to be adapted to check for l2tp_session_create()'s return code instead of calling l2tp_session_find().
pppol2tp_connect() is a bit special because it has to work on existing sessions (if they're not connected) or to create a new session if none is found. When acting on a preexisting session, a reference must be held or it could go away on us. So we have to use l2tp_session_get() instead of l2tp_session_find() and drop the reference before exiting.
--- net/l2tp/l2tp_core.c | 70 ++++++++++++++++++++++++++++++++++++++------------- net/l2tp/l2tp_eth.c | 10 +------ net/l2tp/l2tp_ppp.c | 60 +++++++++++++++++++++---------------------- 3 files changed, 84 insertions(+), 56 deletions(-)
net/l2tp/l2tp_core.c +53 -17
@@ -378,6 +378,48 @@ struct l2tp_session *l2tp_session_find_b }EXPORT_SYMBOL_GPL (l2tp_session_find_by_ifname); +static int l2tp_session_add_to_tunnel (struct l2tp_tunnel *tunnel, +struct l2tp_session *session) +{ +struct l2tp_session *session_walk; +struct hlist_head *g_head; +struct hlist_head *head; +struct l2tp_net *pn; + + head =l2tp_session_id_hash (tunnel, session->session_id); + +write_lock_bh (&tunnel->hlist_lock); +hlist_for_each_entry (session_walk, head, hlist) +if (session_walk->session_id == session->session_id) + goto exist; + +if (tunnel->version ==L2TP_HDR_VER_3 ) { + pn =l2tp_pernet (tunnel->l2tp_net); + g_head =l2tp_session_id_hash_2 (l2tp_pernet (tunnel->l2tp_net), + session->session_id); + +spin_lock_bh (&pn->l2tp_session_hlist_lock); +hlist_for_each_entry (session_walk, g_head, global_hlist) +if (session_walk->session_id == session->session_id) + goto exist_glob; + +hlist_add_head_rcu (&session->global_hlist, g_head); +spin_unlock_bh (&pn->l2tp_session_hlist_lock); + } + +hlist_add_head (&session->hlist, head); +write_unlock_bh (&tunnel->hlist_lock); + +return 0 ; + +exist_glob: +spin_unlock_bh (&pn->l2tp_session_hlist_lock); +exist: +write_unlock_bh (&tunnel->hlist_lock); + +return -EEXIST ; +} +/* Lookup a tunnel by id */ struct l2tp_tunnel *l2tp_tunnel_find (struct net *net,u32 tunnel_id)
@@ -1787,6 +1829,7 @@ EXPORT_SYMBOL_GPL(l2tp_session_set_headestruct l2tp_session *l2tp_session_create (int priv_size,struct l2tp_tunnel *tunnel,u32 session_id,u32 peer_session_id,struct l2tp_session_cfg *cfg) {struct l2tp_session *session; +int err; session =kzalloc (sizeof (struct l2tp_session ) + priv_size,GFP_KERNEL );if (session !=NULL ) {
@@ -1842,6 +1885,13 @@ struct l2tp_session *l2tp_session_createl2tp_session_set_header_len (session, tunnel->version); + err =l2tp_session_add_to_tunnel (tunnel, session); +if (err) { +kfree (session); + +return ERR_PTR (err); + } +/* Bump the reference count. The session context is deleted * only when this drops to zero. */
@@ -1851,28 +1901,14 @@ struct l2tp_session *l2tp_session_create/* Ensure tunnel socket isn't deleted */ sock_hold (tunnel->sock); -/* Add session to the tunnel's hash list */ -write_lock_bh (&tunnel->hlist_lock); -hlist_add_head (&session->hlist, -l2tp_session_id_hash (tunnel, session_id)); -write_unlock_bh (&tunnel->hlist_lock); - -/* And to the global session list if L2TPv3 */ -if (tunnel->version !=L2TP_HDR_VER_2 ) { -struct l2tp_net *pn =l2tp_pernet (tunnel->l2tp_net); - -spin_lock_bh (&pn->l2tp_session_hlist_lock); -hlist_add_head_rcu (&session->global_hlist, -l2tp_session_id_hash_2 (pn, session_id)); -spin_unlock_bh (&pn->l2tp_session_hlist_lock); - } -/* Ignore management session in session count value */ if (session->session_id !=0 )atomic_inc (&l2tp_session_count); + +return session; } -return session; +return ERR_PTR (-ENOMEM ); }EXPORT_SYMBOL_GPL (l2tp_session_create);
net/l2tp/l2tp_eth.c +2 -8
@@ -223,12 +223,6 @@ static int l2tp_eth_create(struct net *n goto out; } - session =l2tp_session_find (net, tunnel, session_id); -if (session) { - rc = -EEXIST ; - goto out; - } -if (cfg->ifname) { dev =dev_get_by_name (net, cfg->ifname);if (dev) {
@@ -242,8 +236,8 @@ static int l2tp_eth_create(struct net *n session =l2tp_session_create (sizeof (*spriv), tunnel, session_id, peer_session_id, cfg); -if (!session) { - rc = -ENOMEM ; +if (IS_ERR (session)) { + rc =PTR_ERR (session); goto out; }
net/l2tp/l2tp_ppp.c +29 -31
101 lines not on this page · raw message
Patches currently in stable-queue which might be from [email protected] are
queue-4.9/l2tp-take-a-reference-on-sessions-used-in-genetlink-handlers.patch queue-4.9/l2tp-ensure-session-can-t-get-removed-during-pppol2tp_session_ioctl.patch queue-4.9/l2tp-fix-race-in-l2tp_recv_common.patch queue-4.9/l2tp-hold-session-while-sending-creation-notifications.patch queue-4.9/l2tp-fix-duplicate-session-creation.patch