]> git.baikalelectronics.ru Git - kernel.git/commitdiff
qlcnic: prevent ->dcb use-after-free on qlcnic_dcb_enable() failure
authorDaniil Tatianin <d-tatianin@yandex-team.ru>
Thu, 22 Dec 2022 11:52:28 +0000 (14:52 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 18 Jan 2023 10:41:57 +0000 (11:41 +0100)
[ Upstream commit 13a7c8964afcd8ca43c0b6001ebb0127baa95362 ]

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: 76eb33da729c ("qlcnic: Disable DCB operations from SR-IOV VFs")
Reviewed-by: Michal Swiatkowski <michal.swiatkowski@linux.intel.com>
Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_init.c
drivers/net/ethernet/qlogic/qlcnic/qlcnic_dcb.h
drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c

index 10286215092f6a5ac45df72a954c92e71362a6a5..85419b8258b592f1499f22dd3c78ccb95ba54d41 100644 (file)
@@ -2525,7 +2525,13 @@ int qlcnic_83xx_init(struct qlcnic_adapter *adapter, int pci_using_dac)
                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);
+               goto disable_mbx_intr;
+       }
+
        qlcnic_83xx_initialize_nic(adapter, 1);
        qlcnic_dcb_get_info(adapter->dcb);
 
index 0a9d24e86715d1358f4abdd59bebd94b590edab8..eb8000d9b6d0e615d836cab0d5cb0943567e399a 100644 (file)
@@ -42,11 +42,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)
@@ -113,9 +108,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
index 3a96fd6deef72cf442764f1356d87189753bf45d..9d5b74c804b5eb10ce8600e7f23a29d093d4d880 100644 (file)
@@ -2639,7 +2639,13 @@ 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);
+                       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);