Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp148508rwj; Thu, 22 Dec 2022 00:37:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXsn8PKsprc5tiE9PoaYtOrvBcgdSeeKUGMuNo4IsZtd4d2VuB4xseUeUAvVdQGCVKyYEIIS X-Received: by 2002:a17:906:6d47:b0:7c0:c312:acaa with SMTP id a7-20020a1709066d4700b007c0c312acaamr3550626ejt.49.1671698255616; Thu, 22 Dec 2022 00:37:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671698255; cv=none; d=google.com; s=arc-20160816; b=vnq/tW1py4unfvmmMwNGYyhkdyyvw7YR110q97/v6H91xejT6HRuF8OO1XzZr9Z6yO 4J0bvvhHvzfT1qn/8pxrz5k2nFlQXIeFDnf90gl8ceCdJv3yuRm8zeDpObORim2JpLdA TG+fMzqCHDIge19yqOPJ3quNj7l/L24lHcGEiFMDszKu9bhJ0HUgRsb4t0oFkYP/doxB 0eVNeBrwP89k1muv3I4uvEGj9K0pAPyNV8xJIEznB8fVX0wiJE5dEbbK1vN++S8VAJdV gNQcSbrmFBri5i/VTkS6mLeC60f7dMaIaCUycMqqcXTE4BTnHlXhL9GRHTK7kf1iM8MP om/A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=Nh3QNQcJTJIK+UvLH0pvvJJvrDHgmqpRNGYuaw8yRFk=; b=YBBNlrMnfLM/etnEbW7R5T59dagzmQ2yrlTsBA9MbECRmD9MDO1tLj/lXV5yidHYqx uHT4M8mFEPH06yDG3QgHKWEdp1RE4/lQM/3K6ycIi+YX5stYNFz+2gVcslCQxjdBs9Xy q8jHJ4FaIWKIQbvTdwkTPUB5+49AiSgPf1S7Xzd4GYknAEQKS+uMp6R8phEK0ledJBog PVSRQnXFMkLlihC2JXtZ0l1kJ5hJyQ1ZAK1lir3gq9F19sOA9eTTci+6kieyKazbyvFv DaEBZV71lGTfpzWelRlSExaGJ0HpWesJ7W6BwCF1FTOSpdhhGZzv52weyBQH93vqob6C 6Yzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=Stl8KRPv; 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=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hs4-20020a1709073e8400b007c11b8a1d74si14117ejc.41.2022.12.22.00.37.20; Thu, 22 Dec 2022 00:37:35 -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=pass header.i=@yandex-team.ru header.s=default header.b=Stl8KRPv; 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=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234972AbiLVHmm (ORCPT + 67 others); Thu, 22 Dec 2022 02:42:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229922AbiLVHmj (ORCPT ); Thu, 22 Dec 2022 02:42:39 -0500 Received: from forwardcorp1a.mail.yandex.net (forwardcorp1a.mail.yandex.net [178.154.239.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1D061A394; Wed, 21 Dec 2022 23:42:34 -0800 (PST) Received: from vla1-81430ab5870b.qloud-c.yandex.net (vla1-81430ab5870b.qloud-c.yandex.net [IPv6:2a02:6b8:c0d:35a1:0:640:8143:ab5]) by forwardcorp1a.mail.yandex.net (Yandex) with ESMTP id 6A8A55FF19; Thu, 22 Dec 2022 10:42:30 +0300 (MSK) Received: from d-tatianin-nix.yandex-team.ru (unknown [2a02:6b8:b081:7202::1:0]) by vla1-81430ab5870b.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id Qghs000QW4Y1-EePMyAkm; Thu, 22 Dec 2022 10:42:29 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1671694949; bh=Nh3QNQcJTJIK+UvLH0pvvJJvrDHgmqpRNGYuaw8yRFk=; h=Message-Id:Date:Cc:Subject:To:From; b=Stl8KRPvG9O1nfxTBE8+9fUvWngCVamt99XZQECWDA2j3nnHYdQKKaHXN4e7AabYN YMN6NB4Qq8pNTvB4aIRnr6kxwgI+tLNWwKsM8rn6nI4PzzDUkFkMlMoyE3h3YC6Vlt 3K//owqxRua6yVtBxL2R/9tGZG/0ChJbCCgfSDq8= Authentication-Results: vla1-81430ab5870b.qloud-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Daniil Tatianin To: Shahed Shaikh Cc: Daniil Tatianin , Manish Chopra , GR-Linux-NIC-Dev@marvell.com, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net v2] qlcnic: prevent ->dcb use-after-free on qlcnic_dcb_enable() failure Date: Thu, 22 Dec 2022 10:42:23 +0300 Message-Id: <20221222074223.1746072-1-d-tatianin@yandex-team.ru> X-Mailer: git-send-email 2.25.1 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_PASS 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 adapter->dcb would get silently freed inside qlcnic_dcb_enable() in case qlcnic_dcb_attach() would return an error, which always happens under OOM conditions. This would lead to use-after-free because both of the existing callers invoke qlcnic_dcb_get_info() on the obtained pointer, which is potentially freed at that point. Propagate errors from qlcnic_dcb_enable(), and instead free the dcb pointer at callsite using qlcnic_dcb_free(). This also removes the now unused qlcnic_clear_dcb_ops() helper, which was a simple wrapper around kfree() also causing memory leaks for partially initialized dcb. Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool. Fixes: 3c44bba1d270 ("qlcnic: Disable DCB operations from SR-IOV VFs") Signed-off-by: Daniil Tatianin --- Changes since v1: - Add a fixes tag + net as a target - Remove qlcnic_clear_dcb_ops entirely & use qlcnic_dcb_free instead --- drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c | 9 ++++++++- drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h | 10 ++-------- drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c | 9 ++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c index dbb800769cb6..774f2c6875ec 100644 --- a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c +++ b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c @@ -2505,7 +2505,14 @@ int qlcnic_83xx_init(struct qlcnic_adapter *adapter) goto disable_mbx_intr; qlcnic_83xx_clear_function_resources(adapter); - qlcnic_dcb_enable(adapter->dcb); + + err = qlcnic_dcb_enable(adapter->dcb); + if (err) { + qlcnic_dcb_free(adapter->dcb); + adapter->dcb = NULL; + goto disable_mbx_intr; + } + qlcnic_83xx_initialize_nic(adapter, 1); qlcnic_dcb_get_info(adapter->dcb); diff --git a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h index 7519773eaca6..22afa2be85fd 100644 --- a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h +++ b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h @@ -41,11 +41,6 @@ struct qlcnic_dcb { unsigned long state; }; -static inline void qlcnic_clear_dcb_ops(struct qlcnic_dcb *dcb) -{ - kfree(dcb); -} - static inline int qlcnic_dcb_get_hw_capability(struct qlcnic_dcb *dcb) { if (dcb && dcb->ops->get_hw_capability) @@ -112,9 +107,8 @@ static inline void qlcnic_dcb_init_dcbnl_ops(struct qlcnic_dcb *dcb) dcb->ops->init_dcbnl_ops(dcb); } -static inline void qlcnic_dcb_enable(struct qlcnic_dcb *dcb) +static inline int qlcnic_dcb_enable(struct qlcnic_dcb *dcb) { - if (dcb && qlcnic_dcb_attach(dcb)) - qlcnic_clear_dcb_ops(dcb); + return dcb ? qlcnic_dcb_attach(dcb) : 0; } #endif diff --git a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c index 28476b982bab..288eae201a31 100644 --- a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c +++ b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c @@ -2599,7 +2599,14 @@ qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) "Device does not support MSI interrupts\n"); if (qlcnic_82xx_check(adapter)) { - qlcnic_dcb_enable(adapter->dcb); + err = qlcnic_dcb_enable(adapter->dcb); + if (err) { + qlcnic_dcb_free(adapter->dcb); + adapter->dcb = NULL; + dev_err(&pdev->dev, "Failed to enable DCB\n"); + goto err_out_free_hw; + } + qlcnic_dcb_get_info(adapter->dcb); err = qlcnic_setup_intr(adapter); -- 2.25.1