Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp6897068rwl; Thu, 29 Dec 2022 23:42:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXvZtUopkbfIKGJzBIiqz/5cFRPJMPlAU31OkJ01mP/3pNvljma3fZLfuvP21Hro5vJYmlQU X-Received: by 2002:a05:6a20:bfc3:b0:ad:94d0:ac97 with SMTP id gs3-20020a056a20bfc300b000ad94d0ac97mr36115449pzb.48.1672386160534; Thu, 29 Dec 2022 23:42:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672386160; cv=none; d=google.com; s=arc-20160816; b=J0OPCuA/t+5BT/QsMOh0YtyYa6xbkoJQlaS85mvJH5WfCrWq2o3BFiwBxtVxm9jitd Uj92glLuZF0xfLX2doK2u5QrWobCRCwhTL5cWE1z1cTS/kVg9wRk+AJdfojokyuLau1Z 3mkW5etBMDozO6RN+pNlHcWYrxP+h7qqeKCBf7GoUdVx5l2962KU3DxVzuGRp/ADrvaQ Y+pbayK/JYPdMeFAESaHMSjMYaXtiGAyqqkas6xQKP/2V0vUe4OPKonDC/1ZZ/ecX0MX WvjagOAnDFhrOjB+9IWqaWD+I2f8CLvHjx97OaqapQ2qFz2cc+Rod4e2sMq9QYV/vdbI 0XVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/eX4/CTCtjyiWC4V7OvOX6dzNZbFQNR2QZZFW5KPupw=; b=yCeRfTwhahiwmK99hciyULbT5lU9Tlb8L9wn+dbHYrAsal2XMf7+PHPqlkQ8H9B1eP 48crkF4OCou771yQ1x5yZhzFk/vyYpkZ2YQ2h6lkGg90bCmXhntSe64IZsUy9RMW23DM 4FUg9HA05nLVQQ/qDrLswpZApLEJpetahXN3PT9iV/VQ2GHF2SVT9F7ongVCGQCXtjUR PQ6AVUJyeiiROsOX22FUuEd3dbEhB/B6js6jEbz6AsU6tdgXNg4SXdsbJTYtgm6HlHOA ycX3R6G5EZgU6ddVul/z9VS7AUeAxh/BaOQVaQC9RzUCau5E4Us6irPOsyLyPsPnpQ6p hxZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nbd.name header.s=20160729 header.b=imvlYXXL; 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=nbd.name Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p14-20020a63e64e000000b00476c46ff6e9si22251393pgj.572.2022.12.29.23.42.30; Thu, 29 Dec 2022 23:42:40 -0800 (PST) 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; dkim=fail header.i=@nbd.name header.s=20160729 header.b=imvlYXXL; 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=nbd.name Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234687AbiL3HcL (ORCPT + 65 others); Fri, 30 Dec 2022 02:32:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234562AbiL3HcG (ORCPT ); Fri, 30 Dec 2022 02:32:06 -0500 Received: from nbd.name (nbd.name [46.4.11.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0A9B2BD9; Thu, 29 Dec 2022 23:32:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nbd.name; s=20160729; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=/eX4/CTCtjyiWC4V7OvOX6dzNZbFQNR2QZZFW5KPupw=; b=imvlYXXLQzkhx3C9WX7lII+acM ixTx1f6ndSof5qCXODx7uSKtLWVAarD7z6YBuQVrZv1DOGx3pKP3polA2qD6VwyZ6uixGTBZx/xhU jrd1yuzyJqQxUBqabRCr2U/9MvyZMcxmyh3NluE5k1hN/UlyZ0RXpBT5lA9p/uYtuoM8=; Received: from p200300daa720fc00fd7bb9014adaf597.dip0.t-ipconnect.de ([2003:da:a720:fc00:fd7b:b901:4ada:f597] helo=Maecks.lan) by ds12 with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.94.2) (envelope-from ) id 1pB9rn-00CcyM-UX; Fri, 30 Dec 2022 08:31:48 +0100 From: Felix Fietkau To: netdev@vger.kernel.org, John Crispin , Sean Wang , Mark Lee , Lorenzo Bianconi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Matthias Brugger , Pablo Neira Ayuso Cc: linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH net v3 3/5] net: ethernet: mtk_eth_soc: fix flow_offload related refcount bug Date: Fri, 30 Dec 2022 08:31:43 +0100 Message-Id: <20221230073145.53386-3-nbd@nbd.name> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221230073145.53386-1-nbd@nbd.name> References: <20221230073145.53386-1-nbd@nbd.name> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE 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 Since we call flow_block_cb_decref on FLOW_BLOCK_UNBIND, we need to call flow_block_cb_incref unconditionally, even for a newly allocated cb. Fixes a use-after-free bug Fixes: 502e84e2382d ("net: ethernet: mtk_eth_soc: add flow offloading support") Signed-off-by: Felix Fietkau --- .../net/ethernet/mediatek/mtk_ppe_offload.c | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c index 81afd5ee3fbf..412d215b9571 100644 --- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c @@ -554,6 +554,7 @@ mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f) struct mtk_eth *eth = mac->hw; static LIST_HEAD(block_cb_list); struct flow_block_cb *block_cb; + bool register_block = false; flow_setup_cb_t *cb; if (!eth->soc->offload_version) @@ -568,16 +569,20 @@ mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f) switch (f->command) { case FLOW_BLOCK_BIND: block_cb = flow_block_cb_lookup(f->block, cb, dev); - if (block_cb) { - flow_block_cb_incref(block_cb); - return 0; + if (!block_cb) { + block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); + if (IS_ERR(block_cb)) + return PTR_ERR(block_cb); + + register_block = true; } - block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); - if (IS_ERR(block_cb)) - return PTR_ERR(block_cb); - flow_block_cb_add(block_cb, f); - list_add_tail(&block_cb->driver_list, &block_cb_list); + flow_block_cb_incref(block_cb); + + if (register_block) { + flow_block_cb_add(block_cb, f); + list_add_tail(&block_cb->driver_list, &block_cb_list); + } return 0; case FLOW_BLOCK_UNBIND: block_cb = flow_block_cb_lookup(f->block, cb, dev); -- 2.38.1