Received: by 10.192.165.156 with SMTP id m28csp268581imm; Tue, 17 Apr 2018 09:50:40 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/cdjaC633y0Vz0o4VeEl+EvUu0ESPdVo9xFCZ9TSPZege7KH8+gTBrKRWtGgjkqkZPfGad X-Received: by 2002:a17:902:4303:: with SMTP id i3-v6mr2819312pld.394.1523983840706; Tue, 17 Apr 2018 09:50:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523983840; cv=none; d=google.com; s=arc-20160816; b=o1nvLx+2WKQCgGkeUP0e0YXzafRs3d+gvuQ+2becPFlcsXbI6qKdEUuQmWoWtM9aRS jYoY/3kgTORxtrYScObKnXhbN9U0DZ+uraFKGDwAputGpPNUmHh0hSmFq4pGayQP//NG UHRNSSHezfn7RaWn/EcWgV5Yaiul/kG7i1f3QKulPN/aWAXch3JnDIL6OpCvbZ2eUBgD E/Kry8y39VOE6TwZJciUNsk12l2qLWd9fdC1/vSDvnuIQXZ3MxkYJbSD8OtXIJZH06YB sWEovl6DEdhYh1cxl73gzaac2od+aVJJzrehX15pvwPYsTNNuCl2Vw9kwqs4SVCX7Pol a73A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=mY3t+QYcZmKVRn7iliaQVJXl3Ub5YN0Rhqgp7i301jc=; b=Gp/RGReeL0FfY7AnXYme2BlRbiB6VnmX7Gn8mUENL4hbC2HcGxYIn1uwtxQ28bEbnT chZt6/+hcbbMDjiF+1yb4D7+T2PaWxLMl/bEfP089F1X7X17O0sA5nA0qfELCUBkN73/ uTaby56kfFAvopsdmTuVeNbGSHmVUw9big3Kn84LbpL+ED1JIBF9GTxH2UdLX2+0Yuek VZqM4lNVANCJ4Xa21dl+4WAVAFaH3kQ8GBiDHPid0oSUwxG1uqiGq/Zh1DkToPxyED4D j6Aebyc1IqGIcTOxbnDPEXTYdovD1sZOx3s3Tx1P0J40D/qcs+FMnTO+X6+XcA/KTDSB 15eg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w31-v6si14828959pla.193.2018.04.17.09.50.26; Tue, 17 Apr 2018 09:50:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754616AbeDQQEv (ORCPT + 99 others); Tue, 17 Apr 2018 12:04:51 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:34360 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753997AbeDQQEs (ORCPT ); Tue, 17 Apr 2018 12:04:48 -0400 Received: from localhost (unknown [46.44.180.42]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D6B9ABBE; Tue, 17 Apr 2018 16:04:47 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guillaume Nault , "David S. Miller" Subject: [PATCH 4.15 08/53] l2tp: fix race in duplicate tunnel detection Date: Tue, 17 Apr 2018 17:58:33 +0200 Message-Id: <20180417155723.427474067@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180417155723.091120060@linuxfoundation.org> References: <20180417155723.091120060@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Guillaume Nault [ Upstream commit f6cd651b056ffd3b4e8496afd44d4ed44bf69136 ] We can't use l2tp_tunnel_find() to prevent l2tp_nl_cmd_tunnel_create() from creating a duplicate tunnel. A tunnel can be concurrently registered after l2tp_tunnel_find() returns. Therefore, searching for duplicates must be done at registration time. Finally, remove l2tp_tunnel_find() entirely as it isn't use anywhere anymore. Fixes: 309795f4bec2 ("l2tp: Add netlink control API for L2TP") Signed-off-by: Guillaume Nault Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/l2tp/l2tp_core.c | 35 ++++++++++++++--------------------- net/l2tp/l2tp_core.h | 1 - net/l2tp/l2tp_netlink.c | 6 ------ 3 files changed, 14 insertions(+), 28 deletions(-) --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -335,26 +335,6 @@ err_tlock: } EXPORT_SYMBOL_GPL(l2tp_session_register); -/* Lookup a tunnel by id - */ -struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id) -{ - struct l2tp_tunnel *tunnel; - struct l2tp_net *pn = l2tp_pernet(net); - - rcu_read_lock_bh(); - list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { - if (tunnel->tunnel_id == tunnel_id) { - rcu_read_unlock_bh(); - return tunnel; - } - } - rcu_read_unlock_bh(); - - return NULL; -} -EXPORT_SYMBOL_GPL(l2tp_tunnel_find); - struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth) { struct l2tp_net *pn = l2tp_pernet(net); @@ -1510,6 +1490,7 @@ static int l2tp_validate_socket(const st int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, struct l2tp_tunnel_cfg *cfg) { + struct l2tp_tunnel *tunnel_walk; struct l2tp_net *pn; struct socket *sock; struct sock *sk; @@ -1538,7 +1519,16 @@ int l2tp_tunnel_register(struct l2tp_tun tunnel->l2tp_net = net; pn = l2tp_pernet(net); + spin_lock_bh(&pn->l2tp_tunnel_list_lock); + list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) { + if (tunnel_walk->tunnel_id == tunnel->tunnel_id) { + spin_unlock_bh(&pn->l2tp_tunnel_list_lock); + + ret = -EEXIST; + goto err_sock; + } + } list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); spin_unlock_bh(&pn->l2tp_tunnel_list_lock); @@ -1567,7 +1557,10 @@ int l2tp_tunnel_register(struct l2tp_tun return 0; err_sock: - sockfd_put(sock); + if (tunnel->fd < 0) + sock_release(sock); + else + sockfd_put(sock); err: return ret; } --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -225,7 +225,6 @@ struct l2tp_session *l2tp_session_get(co struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth); struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, const char *ifname); -struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id); struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth); int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, --- a/net/l2tp/l2tp_netlink.c +++ b/net/l2tp/l2tp_netlink.c @@ -236,12 +236,6 @@ static int l2tp_nl_cmd_tunnel_create(str if (info->attrs[L2TP_ATTR_DEBUG]) cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]); - tunnel = l2tp_tunnel_find(net, tunnel_id); - if (tunnel != NULL) { - ret = -EEXIST; - goto out; - } - ret = -EINVAL; switch (cfg.encap) { case L2TP_ENCAPTYPE_UDP: