Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp131660ybm; Tue, 26 May 2020 12:33:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuHc55Z//HiKYNt7HNYzYmi3qMBOD4n5Hheh1vFzvV8ogGkLMeeu4DnzRhOjuk8cLYP7rm X-Received: by 2002:a17:906:2e50:: with SMTP id r16mr2449019eji.305.1590521622888; Tue, 26 May 2020 12:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590521622; cv=none; d=google.com; s=arc-20160816; b=m1mtdbk8/20Bpa26CnY7S5ujhQv9wtMzSW/clD3/kxOJZq6Myb0tLwhgLB/0gplMSW mIBFBuRwOa+eSL25O+qqic/sXwGkdln0yIFmIl7qXW83zTmEA2uvbgRz5gpFDV3ui3Hk ko9mORglKlMSuCq/wfrAAJ+iVETU3SpQoPEWnV3YldSprTovUbDmcszi911J0QC7RRLz X/fdxB8IGW8xjQ0Knf6DLO15LN/if2aO4Pvs58kCiG33/+sI92iawXpqOPE6TtLKxy3m h67XhAnoSYCHRK2pzCq1OhjLqJKVVITGDdjvPiKCh7/nM9tUnkQaO2loUM/EfQKFnxmA Gy0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=24HrpnVYpJ1mqAV2e1FqMuqD8fZ2lAPX3e/dwdVx7NQ=; b=kL6xlzeWAHaisbETjyEBdIrzhWx+Ej1e+lJGm7lG4Hxe4ntYd0T1g2ROfGLCxuK7JZ /0fp5d2eupTqGbwOV2U7ea0qjnNLF3C2SJ1nhyDVWlqGE9Ix4ce2rO+rWfFlkDGE2lPf eCQVEkeZ2FiLHA2mtbR9W3+ZSp6L1Pu2WDJPeBT9k8VzZ88tEVPEpgHuomUfjBjgj3ZC Wxr4pRTu/sCZ/oNVkZe/Q2m9eWOvrlfebZgZ+HgfEQ1Yugz0CjybjmL4exrOjFGeFiY9 IKinEysoXBaEe9xlmy7sj2SYUMUUVMm2IwsBGzntO8DD1AnuIV5TkmSOSsxDlbJEB6f9 Kl2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2kK3TJEp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p20si438740ejf.49.2020.05.26.12.33.19; Tue, 26 May 2020 12:33:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2kK3TJEp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390677AbgEZTAP (ORCPT + 99 others); Tue, 26 May 2020 15:00:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:54198 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390649AbgEZTAL (ORCPT ); Tue, 26 May 2020 15:00:11 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5E86720849; Tue, 26 May 2020 19:00:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590519610; bh=604pv/P1CxLz+KFT5nkJZQWGW6l8CZCsjlrFeAwmbmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2kK3TJEpSD01Ue0Shu+kbiT69/sAp7VjtfpQ/SO8sl85Fy9sFAgMTM9Yjb/jhNEsU ZC+mpsGuIcHMqUbgx7AYo/+TU3ngdgS1SGVuqhhXUnqyZOuisC/ixVIVoIGVRPsGmX 3wlAF9gzVZ0Hp45wbuUQnZ4O1aUxHAxNvx6DE7H4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, greg@kroah.com Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guillaume Nault , "David S. Miller" , Giuliano Procida Subject: [PATCH 4.9 43/64] l2tp: pass tunnel pointer to ->session_create() Date: Tue, 26 May 2020 20:53:12 +0200 Message-Id: <20200526183928.058472735@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200526183913.064413230@linuxfoundation.org> References: <20200526183913.064413230@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guillaume Nault commit f026bc29a8e093edfbb2a77700454b285c97e8ad upstream. Using l2tp_tunnel_find() in pppol2tp_session_create() and l2tp_eth_create() is racy, because no reference is held on the returned session. These functions are only used to implement the ->session_create callback which is run by l2tp_nl_cmd_session_create(). Therefore searching for the parent tunnel isn't necessary because l2tp_nl_cmd_session_create() already has a pointer to it and holds a reference. This patch modifies ->session_create()'s prototype to directly pass the the parent tunnel as parameter, thus avoiding searching for it in pppol2tp_session_create() and l2tp_eth_create(). Since we have to touch the ->session_create() call in l2tp_nl_cmd_session_create(), let's also remove the useless conditional: we know that ->session_create isn't NULL at this point because it's already been checked earlier in this same function. Finally, one might be tempted to think that the removed l2tp_tunnel_find() calls were harmless because they would return the same tunnel as the one held by l2tp_nl_cmd_session_create() anyway. But that tunnel might be removed and a new one created with same tunnel Id before the l2tp_tunnel_find() call. In this case l2tp_tunnel_find() would return the new tunnel which wouldn't be protected by the reference held by l2tp_nl_cmd_session_create(). Fixes: 309795f4bec2 ("l2tp: Add netlink control API for L2TP") Fixes: d9e31d17ceba ("l2tp: Add L2TP ethernet pseudowire support") Signed-off-by: Guillaume Nault Signed-off-by: David S. Miller Signed-off-by: Giuliano Procida Signed-off-by: Greg Kroah-Hartman --- net/l2tp/l2tp_core.h | 4 +++- net/l2tp/l2tp_eth.c | 11 +++-------- net/l2tp/l2tp_netlink.c | 8 ++++---- net/l2tp/l2tp_ppp.c | 19 +++++++------------ 4 files changed, 17 insertions(+), 25 deletions(-) --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -201,7 +201,9 @@ struct l2tp_tunnel { }; struct l2tp_nl_cmd_ops { - int (*session_create)(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg); + int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel, + u32 session_id, u32 peer_session_id, + struct l2tp_session_cfg *cfg); int (*session_delete)(struct l2tp_session *session); }; --- a/net/l2tp/l2tp_eth.c +++ b/net/l2tp/l2tp_eth.c @@ -256,23 +256,18 @@ static void l2tp_eth_adjust_mtu(struct l dev->needed_headroom += session->hdr_len; } -static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) +static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel, + u32 session_id, u32 peer_session_id, + struct l2tp_session_cfg *cfg) { struct net_device *dev; char name[IFNAMSIZ]; - struct l2tp_tunnel *tunnel; struct l2tp_session *session; struct l2tp_eth *priv; struct l2tp_eth_sess *spriv; int rc; struct l2tp_eth_net *pn; - tunnel = l2tp_tunnel_find(net, tunnel_id); - if (!tunnel) { - rc = -ENODEV; - goto out; - } - if (cfg->ifname) { dev = dev_get_by_name(net, cfg->ifname); if (dev) { --- a/net/l2tp/l2tp_netlink.c +++ b/net/l2tp/l2tp_netlink.c @@ -632,10 +632,10 @@ static int l2tp_nl_cmd_session_create(st break; } - ret = -EPROTONOSUPPORT; - if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create) - ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id, - session_id, peer_session_id, &cfg); + ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel, + session_id, + peer_session_id, + &cfg); if (ret >= 0) { session = l2tp_session_get(net, tunnel, session_id, false); --- a/net/l2tp/l2tp_ppp.c +++ b/net/l2tp/l2tp_ppp.c @@ -795,25 +795,20 @@ end: #ifdef CONFIG_L2TP_V3 -/* Called when creating sessions via the netlink interface. - */ -static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) +/* Called when creating sessions via the netlink interface. */ +static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel, + u32 session_id, u32 peer_session_id, + struct l2tp_session_cfg *cfg) { int error; - struct l2tp_tunnel *tunnel; struct l2tp_session *session; struct pppol2tp_session *ps; - tunnel = l2tp_tunnel_find(net, tunnel_id); - - /* Error if we can't find the tunnel */ - error = -ENOENT; - if (tunnel == NULL) - goto out; - /* Error if tunnel socket is not prepped */ - if (tunnel->sock == NULL) + if (!tunnel->sock) { + error = -ENOENT; goto out; + } /* Default MTU values. */ if (cfg->mtu == 0)