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")
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>
                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);
 
 
        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)
                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
 
                         "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);