Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp125484iof; Sun, 5 Jun 2022 23:07:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnCyjNyEZO0JNMpLw4nJAeXuGQxM2l9kxCknAIUhv3jkLkPaRma+3tVAFdLWzmLvZV5nIg X-Received: by 2002:a17:902:ea0b:b0:164:81f:682f with SMTP id s11-20020a170902ea0b00b00164081f682fmr22675285plg.39.1654495652648; Sun, 05 Jun 2022 23:07:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495652; cv=none; d=google.com; s=arc-20160816; b=RTGMsQxbkeQ6NRQdrcPcq8HFgfE8a7Gdqkb6wyEOPTb1UQVtS5QF2ZqPvrwV0mKI3f 42djhPXMfAUBPYBqqBNsdDck2N3FrHL+OZNertrJT8tUmvNBxxjVADrj3TrnHt40mCmZ iETpfggrX0L/LDZZpLohZWe/eSigmPDSOoCYqLQEfVYhpKZwsSuil2mGDtM3QP3/nBY6 nbPAv73Za/LjFEsxl94zYml9TznJ7/QUDag0HMAZATI/QOW1lxyLzbOF00lsbvU/4EwN VhXce7CePdCo8PAqSY3NqESOweN1LFxqNhr0g3tSMfQ76F6Bq0RzqPe7nKYzO7Q7PuFJ Egvw== 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:dkim-signature; bh=TktEAKzMXlckYPb1wmtHCGJBPjvUjlyBnCJZah6s7k8=; b=DMyeTn3h1Mz4zoyrMuc+4QwgwltXyWi5aUds06aUEs55dYKvq2YNNSb4D1QedN/T5f y5ZCJMZnOTrciiL6Zxv91dC5LcuBZjB8K62ltD4nDQRIb7uihvlLYcPQD/sKMdmZcXPS L+PAnGqT8Ae2vib0/NjZF26k2d6lnrBldhrLrnt3rC7WTnse+Xhyx3Iha4oBbxw+m2Wu HhcFL0fuHapdC8inJiQgNpPiM2uDgZSkKo1dZ7UHX3/carRy1zEcrTLmbyhg1lTnS/Ux +Wpbg1DwVv/qRQPfe+CFOZX2UUCndXLf1CNMFSFfYRaWRKBYdnqDcC+5cLkIpWaR1O2r LP9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=UxJRsh6Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s64-20020a17090a2f4600b001e2d6f186fasi24516126pjd.183.2022.06.05.23.07.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:07:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=UxJRsh6Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8A8283125A0; Sun, 5 Jun 2022 21:57:24 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245581AbiFELzt (ORCPT + 99 others); Sun, 5 Jun 2022 07:55:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232695AbiFELzr (ORCPT ); Sun, 5 Jun 2022 07:55:47 -0400 Received: from m12-12.163.com (m12-12.163.com [220.181.12.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BC6D54B41C for ; Sun, 5 Jun 2022 04:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id; bh=TktEAKzMXlckYPb1wm tHCGJBPjvUjlyBnCJZah6s7k8=; b=UxJRsh6Y88e0gYNya3P3ilkERz5ekqih9r rcqfowJPBQM3QbHBg1y1IT4ydejusEZZ0Zr+tX8eWgUv1r+LTQRon0Si+DfpB3qN 3+Zo/EYJ+0pd0Gu7bEWwOreVi7HNCHusC5xOdlB2yFmobUy4XYAIPQHEN7nDhZW1 cUGepyHWg= Received: from localhost.localdomain (unknown [202.112.113.212]) by smtp8 (Coremail) with SMTP id DMCowAD3Eh1lmZxiknruGA--.26778S4; Sun, 05 Jun 2022 19:54:21 +0800 (CST) From: Xiaohui Zhang To: Xiaohui Zhang , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.or, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] l2tp: fix possible use-after-free Date: Sun, 5 Jun 2022 19:54:12 +0800 Message-Id: <20220605115412.16004-1-ruc_zhangxiaohui@163.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: DMCowAD3Eh1lmZxiknruGA--.26778S4 X-Coremail-Antispam: 1Uf129KBjvdXoW7JF1xKF18ZF1UWrWfAw1fWFg_yoWkWFX_Zr y8X3WrWrn5Xan7Gr42vw4YqrWkJrZxCrWrCrn7tFsFya4ftF93Kr97JFWvv347urWqgr9a qrsYqw4UKwnrujkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7sRiApn5UUUUU== X-Originating-IP: [202.112.113.212] X-CM-SenderInfo: puxfs6pkdqw5xldrx3rl6rljoofrz/1tbiVg0XMFqzri88igAAsi X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: xiaohuizhang98 We detected a suspected bug with our code clone detection tool. 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: xiaohuizhang98 --- net/l2tp/l2tp_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index b759fbd09b65..c5de6d4e0818 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -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; -- 2.17.1