Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1060968iol; Thu, 9 Jun 2022 22:26:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEqHw2yBtHTjbyFal0zd0DwGfv+C6UMMp2lskJtUYENTgP3lsOBnyW/PIdtk9X1zTch0dV X-Received: by 2002:a17:906:804a:b0:70c:1dac:9bb8 with SMTP id x10-20020a170906804a00b0070c1dac9bb8mr36565800ejw.92.1654838763265; Thu, 09 Jun 2022 22:26:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654838763; cv=none; d=google.com; s=arc-20160816; b=H4dp+tQnHqWMvIKkRzIIn1LMN3Oleoi179/xELPuphBLINvsGksumi8ntCy/WwNzXJ OjQ/9xzPOtfPPs2Go6Fl2ubYngDLrlFjZnBb6y4+k3aTQdB9nDWD3Kd4MZ+Ha+r/IuD1 INvBQhAMgb4Sk0l1TmqYuTrMEULLYjVX1ZRgNIC9OwRVxC46lGRH3fN25GpIon6vxyul eHniiWdGnRPdtlfUird8JUmgXaV1xceI38VAP9s+JHTdsYSEIQAhFWM2gfoyRcGEiZMo Z5N4JsFCwAWMWjuQMKOoNbmQ5gDZfX+bS4aGZwGAttXwxFBYEz/CjoVS1AwuoH9UtBNA zBUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:to:from; bh=G90ZlidpP3LUoSVF4XL28Q8LlSMzmm0RqYhdY7Nk1pA=; b=E0WjiiCbEE6i8FfzrOfINeE5CnPcCaYEggTbvvLNf5Ivw8bhY1z3aaGRrspqAbwOlT rwYouS8+EdVNpgsc4N5jgCJsL8HrnJEAkmyJHEnnDXQXP6qXs/bZgmM7NkV8O/SJB5ip dgD8eA/YmOS9UfdjJ2nqf+y4OGbElDukdzKSx/m+GktH7TVV0thqaJ8cvfT8qc8KEWQi 4BMKFpsJsYRsE34XXcYIZPshGwbp4Y1oU30aBzLozZcH81ECvxy5J+sfB6p29kT/sZfu thQpTDTTii/FMngVAQU/lYlJSeHzkaeeOLKU6FmjpCmQqSdb0eRTR4z9V/C0BzJNUubd Ezxw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ruc.edu.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p22-20020a170906b21600b00711ff9cf4e2si2756689ejz.636.2022.06.09.22.25.30; Thu, 09 Jun 2022 22:26:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ruc.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243604AbiFJFQs (ORCPT + 99 others); Fri, 10 Jun 2022 01:16:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235342AbiFJFQp (ORCPT ); Fri, 10 Jun 2022 01:16:45 -0400 Received: from smtp.ruc.edu.cn (m177126.mail.qiye.163.com [123.58.177.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9307765C; Thu, 9 Jun 2022 22:16:40 -0700 (PDT) Received: from localhost.localdomain (unknown [202.112.113.212]) by smtp.ruc.edu.cn (Hmail) with ESMTPSA id 337D98009D; Fri, 10 Jun 2022 13:16:38 +0800 (CST) From: Xiaohui Zhang To: Xiaohui Zhang , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Xiyu Yang , Xin Tan , Xin Xiong , Tom Parkin , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/1] l2tp: fix possible use-after-free Date: Fri, 10 Jun 2022 13:16:33 +0800 Message-Id: <20220610051633.8582-1-xiaohuizhang@ruc.edu.cn> X-Mailer: git-send-email 2.17.1 X-HM-Spam-Status: e1kfGhgUHx5ZQUtXWQgPGg8OCBgUHx5ZQUlOS1dZCBgUCR5ZQVlLVUtZV1 kWDxoPAgseWUFZKDYvK1lXWShZQUhPN1dZLVlBSVdZDwkaFQgSH1lBWUJOT05WTEgZQ05CSUwfGR 9JVRMBExYaEhckFA4PWVdZFhoPEhUdFFlBWU9LSFVKSktISkNVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6Pgw6Syo*Cj00HwoBEAJNLRxN HyIaCjdVSlVKTU5PQ0hDSkJCS0lJVTMWGhIXVQMSGhQTDhIBExoVHDsJDhhVHh8OVRgVRVlXWRIL WUFZSUtJVUpKSVVKSkhVSUpJWVdZCAFZQUhOQ0o3Bg++ X-HM-Tid: 0a814c0b01282c20kusn337d98009d X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similar to the handling of l2tp_tunnel_get in commit a622b40035d1 ("l2ip: fix possible use-after-free"), we thought a patch might be needed here as well. Before taking a refcount on a rcu protected structure, we need to make sure the refcount is not zero. Signed-off-by: Xiaohui Zhang --- net/l2tp/l2tp_core.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index 7499c51b1850..96e8966f900d 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -252,8 +252,8 @@ struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel, rcu_read_lock_bh(); hlist_for_each_entry_rcu(session, session_list, hlist) - if (session->session_id == session_id) { - l2tp_session_inc_refcount(session); + if (session->session_id == session_id && + refcount_inc_not_zero(&session->ref_count)) { rcu_read_unlock_bh(); return session; @@ -273,8 +273,8 @@ struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id) rcu_read_lock_bh(); hlist_for_each_entry_rcu(session, session_list, global_hlist) - if (session->session_id == session_id) { - l2tp_session_inc_refcount(session); + if (session->session_id == session_id && + refcount_inc_not_zero(&session->ref_count)) { rcu_read_unlock_bh(); return session; @@ -294,8 +294,8 @@ struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth) rcu_read_lock_bh(); for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) { - if (++count > nth) { - l2tp_session_inc_refcount(session); + if (++count > nth && + refcount_inc_not_zero(&session->ref_count)) { rcu_read_unlock_bh(); return session; } @@ -321,8 +321,8 @@ struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, rcu_read_lock_bh(); for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { - if (!strcmp(session->ifname, ifname)) { - l2tp_session_inc_refcount(session); + if (!strcmp(session->ifname, ifname) && + refcount_inc_not_zero(&session->ref_count)) { rcu_read_unlock_bh(); return session; -- 2.17.1