Skip to contentSkip to conversation

wifi: rtlwifi: usb probe error path fixes

PATCH In review Thadeu Lima de Souza Cascardo 3 participants 19 messages Nov 7, 2024 – Nov 18, 2024 Realtek wireless driver (rtlwifi family)

lore
Share & tools
lore
#0 lore raw

cover wifi: rtlwifi: usb probe error path fixes

These are fixes that affect mostly the usb probe error path. It fixes UAF due to firmware loading touching freed memory by waiting for the load completion before releasing that memory. It also fixes a couple of identified memory leaks.

Thadeu Lima de Souza Cascardo (5):
wifi: rtlwifi: do not complete firmware loading needlessly
wifi: rtlwifi: rtl8192se: rise completion of firmware loading as last
step
wifi: rtlwifi: wait for firmware loading before releasing memory
wifi: rtlwifi: fix init_sw_vars leak when probe fails
wifi: rtlwifi: usb: fix workqueue leak when probe fails

 drivers/net/wireless/realtek/rtlwifi/pci.c          | 1 -
 drivers/net/wireless/realtek/rtlwifi/rtl8192se/sw.c | 7 ++++---
 drivers/net/wireless/realtek/rtlwifi/usb.c          | 7 +++++--
 3 files changed, 9 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/5 wifi: rtlwifi: do not complete firmware loading needlessly

The only code waiting for completion is driver removal, which will not be called when probe returns a failure. So this completion is unnecessary.

Fixes b0302aba812b ("rtlwifi: Convert to asynchronous firmware load")
---
 drivers/net/wireless/realtek/rtlwifi/pci.c | 1 -
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 -
 2 files changed, 2 deletions(-)
drivers/net/wireless/realtek/rtlwifi/pci.c +0 -1
index 11709b6c83f1..40fc3c297a8a 100644
@@ -2266,7 +2266,6 @@ int rtl_pci_probe(struct pci_dev *pdev,
 		pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
 
 	pci_release_regions(pdev);
-	complete(&rtlpriv->firmware_loading_complete);
 
 fail1:
 	if (hw)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -1
index d37a017b2b81..c3aa0cd9ff21 100644
@@ -1040,7 +1040,6 @@ int rtl_usb_probe(struct usb_interface *intf,
 error_out2:
 	_rtl_usb_io_handler_release(hw);
 	usb_put_dev(udev);
-	complete(&rtlpriv->firmware_loading_complete);
 	kfree(rtlpriv->usb_data);
 	ieee80211_free_hw(hw);
 	return -ENODEV;
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/5
#2 lore raw

RE: [PATCH net 1/5] wifi: rtlwifi: do not complete firmware loading needlessly

Quoted 5 lines · Thadeu Lima de Souza Cascardo

The only code waiting for completion is driver removal, which will not be called when probe returns a failure. So this completion is unnecessary.

Fixes b0302aba812b ("rtlwifi: Convert to asynchronous firmware load")
maintainer
#3 lore raw

Re: [PATCH net 1/5] wifi: rtlwifi: do not complete firmware loading needlessly

In reply to Thadeu Lima de Souza Cascardo
Quoted 6 lines · Thadeu Lima de Souza Cascardo

The only code waiting for completion is driver removal, which will not be called when probe returns a failure. So this completion is unnecessary.

Fixes b0302aba812b ("rtlwifi: Convert to asynchronous firmware load")

5 patch(es) applied to rtw-next branch of rtw.git, thanks.

e73e11d30394 wifi: rtlwifi: do not complete firmware loading needlessly 8559a9e0c457 wifi: rtlwifi: rtl8192se: rise completion of firmware loading as last step b4b26642b31e wifi: rtlwifi: wait for firmware loading before releasing memory 00260350aed8 wifi: rtlwifi: fix init_sw_vars leak when probe fails f79bc5c67867 wifi: rtlwifi: usb: fix workqueue leak when probe fails

---
https://github.com/pkshih/rtw.git

#4 lore raw

PATCH 2/5 wifi: rtlwifi: rtl8192se: rise completion of firmware loading as last step

In reply to Thadeu Lima de Souza Cascardo

Just like in commit 4dfde294b979 ("rtlwifi: rise completion at the last step of firmware callback"), only signal completion once the function is finished. Otherwise, the module removal waiting for the completion could free the memory that the callback will still use before returning.

Fixes b0302aba812b ("rtlwifi: Convert to asynchronous firmware load")
---
 drivers/net/wireless/realtek/rtlwifi/rtl8192se/sw.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/wireless/realtek/rtlwifi/rtl8192se/sw.c +4 -3
index bbf8ff63dced..e63c67b1861b 100644
@@ -64,22 +64,23 @@ static void rtl92se_fw_cb(const struct firmware *firmware, void *context)
 
 	rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
 		"Firmware callback routine entered!\n");
-	complete(&rtlpriv->firmware_loading_complete);
 	if (!firmware) {
 		pr_err("Firmware %s not available\n", fw_name);
 		rtlpriv->max_fw_size = 0;
-		return;
+		goto exit;
 	}
 	if (firmware->size > rtlpriv->max_fw_size) {
 		pr_err("Firmware is too big!\n");
 		rtlpriv->max_fw_size = 0;
 		release_firmware(firmware);
-		return;
+		goto exit;
 	}
 	pfirmware = (struct rt_firmware *)rtlpriv->rtlhal.pfirmware;
 	memcpy(pfirmware->sz_fw_tmpbuffer, firmware->data, firmware->size);
 	pfirmware->sz_fw_tmpbufferlen = firmware->size;
 	release_firmware(firmware);
+exit:
+	complete(&rtlpriv->firmware_loading_complete);
 }
 
 static int rtl92s_init_sw_vars(struct ieee80211_hw *hw)
Signature · 1 line
2.34.1
maintainer Acked-by on v1 2/5
#5 lore raw

RE: [PATCH net 2/5] wifi: rtlwifi: rtl8192se: rise completion of firmware loading as last step

Quoted 7 lines · Thadeu Lima de Souza Cascardo

Just like in commit 4dfde294b979 ("rtlwifi: rise completion at the last step of firmware callback"), only signal completion once the function is finished. Otherwise, the module removal waiting for the completion could free the memory that the callback will still use before returning.

Fixes b0302aba812b ("rtlwifi: Convert to asynchronous firmware load")
#6 lore raw

PATCH 3/5 wifi: rtlwifi: wait for firmware loading before releasing memory

In reply to Thadeu Lima de Souza Cascardo

At probe error path, the firmware loading work may have already been queued. In such a case, it will try to access memory allocated by the probe function, which is about to be released. In such paths, wait for the firmware worker to finish before releasing memory.

Fixes a7f7c15e945a ("rtlwifi: rtl8192cu: Free ieee80211_hw if probing fails")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
index c3aa0cd9ff21..c27b116ccdff 100644
@@ -1028,13 +1028,15 @@ int rtl_usb_probe(struct usb_interface *intf,
 	err = ieee80211_register_hw(hw);
 	if (err) {
 		pr_err("Can't register mac80211 hw.\n");
-		goto error_out;
+		goto error_init_vars;
 	}
 	rtlpriv->mac80211.mac80211_registered = 1;
 
 	set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
 	return 0;
 
+error_init_vars:
+	wait_for_completion(&rtlpriv->firmware_loading_complete);
 error_out:
 	rtl_deinit_core(hw);
 error_out2:
Signature · 1 line
2.34.1
maintainer
#7 lore raw

RE: [PATCH net 3/5] wifi: rtlwifi: wait for firmware loading before releasing memory

Quoted 19 lines · Thadeu Lima de Souza Cascardo

At probe error path, the firmware loading work may have already been queued. In such a case, it will try to access memory allocated by the probe function, which is about to be released. In such paths, wait for the firmware worker to finish before releasing memory.

Fixes a7f7c15e945a ("rtlwifi: rtl8192cu: Free ieee80211_hw if probing fails")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -0
index c3aa0cd9ff21..c27b116ccdff 100644
@@ -1028,13 +1028,15 @@ int rtl_usb_probe(struct usb_interface *intf,
        err = ieee80211_register_hw(hw);
        if (err) {
                pr_err("Can't register mac80211 hw.\n");

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1039

Quoted 10 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
-               goto error_out;
+               goto error_init_vars;
        }
        rtlpriv->mac80211.mac80211_registered = 1;
 
        set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
        return 0;
 
+error_init_vars:
+       wait_for_completion(&rtlpriv->firmware_loading_complete);

The firmware request is trigged by rtlpriv->cfg->ops->init_sw_vars(hw), and here is wait for filling rtlpriv->rtlhal.pfirmware and rtlpriv->rtlhal.wowlan_firmware.

The rtlpriv->cfg->ops->deinit_sw_vars(hw) is to free firmware. Shouldn't we call it here? Also shouldn't PCI need this?

Quoted 5 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -1
 error_out:
        rtl_deinit_core(hw);
 error_out2:
--

2.34.1

#8 lore raw

Re: [PATCH net 3/5] wifi: rtlwifi: wait for firmware loading before releasing memory

Quoted 35 lines · Ping-Ke Shih
Quoted 19 lines · Thadeu Lima de Souza Cascardo

At probe error path, the firmware loading work may have already been queued. In such a case, it will try to access memory allocated by the probe function, which is about to be released. In such paths, wait for the firmware worker to finish before releasing memory.

Fixes a7f7c15e945a ("rtlwifi: rtl8192cu: Free ieee80211_hw if probing fails")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -0
index c3aa0cd9ff21..c27b116ccdff 100644
@@ -1028,13 +1028,15 @@ int rtl_usb_probe(struct usb_interface *intf,
        err = ieee80211_register_hw(hw);
        if (err) {
                pr_err("Can't register mac80211 hw.\n");

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1039

Quoted 10 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
-               goto error_out;
+               goto error_init_vars;
        }
        rtlpriv->mac80211.mac80211_registered = 1;
 
        set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
        return 0;
 
+error_init_vars:
+       wait_for_completion(&rtlpriv->firmware_loading_complete);

The firmware request is trigged by rtlpriv->cfg->ops->init_sw_vars(hw), and here is wait for filling rtlpriv->rtlhal.pfirmware and rtlpriv->rtlhal.wowlan_firmware.

Quoted 3 lines

The rtlpriv->cfg->ops->deinit_sw_vars(hw) is to free firmware. Shouldn't we call it here? Also shouldn't PCI need this?

About PCI, as I was testing with a USB emulator, I couldn't test it, though I found a few fixes that it would need as well. I can send a patchset for the PCI fixes, though I won't be able to test them.

As for merging the following patch with this one, it would make sense. But I found out the memory leak after coming up with this fix. And instead of explaining two bugs (one UAF and one memory leak) in the same commit, I thought it more simple to have two commits. Besides, given the Fixes line I came up with for each one of them, they would apply to different trees.

Cascardo.

Quoted 6 lines
Quoted 5 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -1
 error_out:
        rtl_deinit_core(hw);
 error_out2:
--

2.34.1

maintainer
#9 lore raw

RE: [PATCH net 3/5] wifi: rtlwifi: wait for firmware loading before releasing memory

Quoted 36 lines · Thadeu Lima de Souza Cascardo
Quoted 35 lines · Ping-Ke Shih
Quoted 19 lines · Thadeu Lima de Souza Cascardo

At probe error path, the firmware loading work may have already been queued. In such a case, it will try to access memory allocated by the probe function, which is about to be released. In such paths, wait for the firmware worker to finish before releasing memory.

Fixes a7f7c15e945a ("rtlwifi: rtl8192cu: Free ieee80211_hw if probing fails")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -0
index c3aa0cd9ff21..c27b116ccdff 100644
@@ -1028,13 +1028,15 @@ int rtl_usb_probe(struct usb_interface *intf,
        err = ieee80211_register_hw(hw);
        if (err) {
                pr_err("Can't register mac80211 hw.\n");

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1039

Quoted 10 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
-               goto error_out;
+               goto error_init_vars;
        }
        rtlpriv->mac80211.mac80211_registered = 1;
 
        set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
        return 0;
 
+error_init_vars:
+       wait_for_completion(&rtlpriv->firmware_loading_complete);

The firmware request is trigged by rtlpriv->cfg->ops->init_sw_vars(hw), and here is wait for filling rtlpriv->rtlhal.pfirmware and rtlpriv->rtlhal.wowlan_firmware.

Quoted 7 lines
Quoted 3 lines

The rtlpriv->cfg->ops->deinit_sw_vars(hw) is to free firmware. Shouldn't we call it here? Also shouldn't PCI need this?

About PCI, as I was testing with a USB emulator, I couldn't test it, though I found a few fixes that it would need as well. I can send a patchset for the PCI fixes, though I won't be able to test them.

Thanks for the PCI fixes in advance.

Quoted 6 lines

As for merging the following patch with this one, it would make sense. But I found out the memory leak after coming up with this fix. And instead of explaining two bugs (one UAF and one memory leak) in the same commit, I thought it more simple to have two commits. Besides, given the Fixes line I came up with for each one of them, they would apply to different trees.

I feel these two commits are dependent. It is hard to pick latter one without taking this first. But I think it is fine to keep it as was.

#10 lore raw

PATCH 4/5 wifi: rtlwifi: fix init_sw_vars leak when probe fails

In reply to Thadeu Lima de Souza Cascardo

If ieee80211_register_hw fails, the memory allocated for the firmware will not be released. Call deinit_sw_vars as the function that undoes the allocationes done by init_sw_vars.

Fixes cefe3dfdb9f5 ("rtl8192cu: Call ieee80211_register_hw from rtl_usb_probe")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
index c27b116ccdff..8ec687fab572 100644
@@ -1037,6 +1037,7 @@ int rtl_usb_probe(struct usb_interface *intf,
 
 error_init_vars:
 	wait_for_completion(&rtlpriv->firmware_loading_complete);
+	rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
 	rtl_deinit_core(hw);
 error_out2:
Signature · 1 line
2.34.1
maintainer
#11 lore raw

RE: [PATCH net 4/5] wifi: rtlwifi: fix init_sw_vars leak when probe fails

Quoted 15 lines · Thadeu Lima de Souza Cascardo

If ieee80211_register_hw fails, the memory allocated for the firmware will not be released. Call deinit_sw_vars as the function that undoes the allocationes done by init_sw_vars.

Fixes cefe3dfdb9f5 ("rtl8192cu: Call ieee80211_register_hw from rtl_usb_probe")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -0
index c27b116ccdff..8ec687fab572 100644

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1040

Quoted 4 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
@@ -1037,6 +1037,7 @@ int rtl_usb_probe(struct usb_interface *intf,
 
 error_init_vars:
        wait_for_completion(&rtlpriv->firmware_loading_complete);
+       rtlpriv->cfg->ops->deinit_sw_vars(hw);

Ah. You did this in this patch. Seemingly we can combine these two small patches.

Quoted 5 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -1
 error_out:
        rtl_deinit_core(hw);
 error_out2:
--

2.34.1

#12 lore raw

PATCH 5/5 wifi: rtlwifi: usb: fix workqueue leak when probe fails

In reply to Thadeu Lima de Souza Cascardo

rtl_init_core creates a workqueue that is then assigned to rtl_wq. rtl_deinit_core does not destroy it. It is left to rtl_usb_deinit, which must be called in the probe error path.

Fixes 2ca20f79e0d8 ("rtlwifi: Add usb driver")
Fixes 851639fdaeac ("rtlwifi: Modify some USB de-initialize code.")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
index 8ec687fab572..0368ecea2e81 100644
@@ -1039,6 +1039,7 @@ int rtl_usb_probe(struct usb_interface *intf,
 	wait_for_completion(&rtlpriv->firmware_loading_complete);
 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
+	rtl_usb_deinit(hw);
 	rtl_deinit_core(hw);
 error_out2:
 	_rtl_usb_io_handler_release(hw);
Signature · 1 line
2.34.1
maintainer
#13 lore raw

RE: [PATCH net 5/5] wifi: rtlwifi: usb: fix workqueue leak when probe fails

Quoted 16 lines · Thadeu Lima de Souza Cascardo

rtl_init_core creates a workqueue that is then assigned to rtl_wq. rtl_deinit_core does not destroy it. It is left to rtl_usb_deinit, which must be called in the probe error path.

Fixes 2ca20f79e0d8 ("rtlwifi: Add usb driver")
Fixes 851639fdaeac ("rtlwifi: Modify some USB de-initialize code.")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -0
index 8ec687fab572..0368ecea2e81 100644

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1045

Quoted 7 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
@@ -1039,6 +1039,7 @@ int rtl_usb_probe(struct usb_interface *intf,
        wait_for_completion(&rtlpriv->firmware_loading_complete);
        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
+       rtl_usb_deinit(hw);
        rtl_deinit_core(hw);
 error_out2:
        _rtl_usb_io_handler_release(hw);

I think deinit should be in reverse order of init step by step:

drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
@@ -1017,7 +1017,7 @@ int rtl_usb_probe(struct usb_interface *intf,
        err = rtl_init_core(hw);
        if (err) {
                pr_err("Can't allocate sw for mac80211\n");
-               goto error_out2;
+               goto error_out_usb_deinit;
        }
        if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
                pr_err("Can't init_sw_vars\n");
@@ -1040,6 +1040,8 @@ int rtl_usb_probe(struct usb_interface *intf,
        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
        rtl_deinit_core(hw);
+error_out_usb_deinit:
+       rtl_usb_deinit(hw);
 error_out2:
        _rtl_usb_io_handler_release(hw);
        usb_put_dev(udev);

Have you also considered PCI? It seems that rtl_pci_deinit() isn't called if PCI probe fails.

#14 lore raw

Re: [PATCH net 5/5] wifi: rtlwifi: usb: fix workqueue leak when probe fails

Quoted 12 lines · Ping-Ke Shih
Quoted 11 lines · Thadeu Lima de Souza Cascardo

rtl_init_core creates a workqueue that is then assigned to rtl_wq. rtl_deinit_core does not destroy it. It is left to rtl_usb_deinit, which must be called in the probe error path.

Fixes 2ca20f79e0d8 ("rtlwifi: Add usb driver")
Fixes 851639fdaeac ("rtlwifi: Modify some USB de-initialize code.")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 14 lines

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1045

Quoted 12 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
index 8ec687fab572..0368ecea2e81 100644
@@ -1039,6 +1039,7 @@ int rtl_usb_probe(struct usb_interface *intf,
        wait_for_completion(&rtlpriv->firmware_loading_complete);
        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
+       rtl_usb_deinit(hw);
        rtl_deinit_core(hw);
 error_out2:
        _rtl_usb_io_handler_release(hw);

I think deinit should be in reverse order of init step by step:

Well, I kept the order that they appear in the remove path. Also, I checked that they are not exactly independent and rtl_usb_deinit does not need to be called when rtl_init_core fails. I have just checked and it wouldn't cause any harm either if rtl_usb_deinit is called in case rtl_init_core fails. So either way should be fine.

Cascardo.

Quoted 26 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +3 -1
@@ -1017,7 +1017,7 @@ int rtl_usb_probe(struct usb_interface *intf,
        err = rtl_init_core(hw);
        if (err) {
                pr_err("Can't allocate sw for mac80211\n");
-               goto error_out2;
+               goto error_out_usb_deinit;
        }
        if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
                pr_err("Can't init_sw_vars\n");
@@ -1040,6 +1040,8 @@ int rtl_usb_probe(struct usb_interface *intf,
        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
        rtl_deinit_core(hw);
+error_out_usb_deinit:
+       rtl_usb_deinit(hw);
 error_out2:
        _rtl_usb_io_handler_release(hw);
        usb_put_dev(udev);

Have you also considered PCI? It seems that rtl_pci_deinit() isn't called if PCI probe fails.

maintainer
#15 lore raw

RE: [PATCH net 5/5] wifi: rtlwifi: usb: fix workqueue leak when probe fails

Quoted 28 lines · Thadeu Lima de Souza Cascardo
Quoted 12 lines · Ping-Ke Shih
Quoted 11 lines · Thadeu Lima de Souza Cascardo

rtl_init_core creates a workqueue that is then assigned to rtl_wq. rtl_deinit_core does not destroy it. It is left to rtl_usb_deinit, which must be called in the probe error path.

Fixes 2ca20f79e0d8 ("rtlwifi: Add usb driver")
Fixes 851639fdaeac ("rtlwifi: Modify some USB de-initialize code.")
---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 14 lines

drivers/net/wireless/realtek/rtlwifi/usb.c int rtl_usb_probe(struct usb_interface *intf, · line 1045

Quoted 12 lines
drivers/net/wireless/realtek/rtlwifi/usb.c +1 -0
index 8ec687fab572..0368ecea2e81 100644
@@ -1039,6 +1039,7 @@ int rtl_usb_probe(struct usb_interface *intf,
        wait_for_completion(&rtlpriv->firmware_loading_complete);
        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 error_out:
+       rtl_usb_deinit(hw);
        rtl_deinit_core(hw);
 error_out2:
        _rtl_usb_io_handler_release(hw);

I think deinit should be in reverse order of init step by step:

Quoted 5 lines

Well, I kept the order that they appear in the remove path. Also, I checked that they are not exactly independent and rtl_usb_deinit does not need to be called when rtl_init_core fails. I have just checked and it wouldn't cause any harm either if rtl_usb_deinit is called in case rtl_init_core fails. So either way should be fine.

Well. Original implementation of the workqueue is not symmetric. Without real hardware verification, we can't move the code of destroy queue from rtl_usb_deinit() to rtl_deinit_core().

maintainer
#16 lore raw
In reply to Thadeu Lima de Souza Cascardo
Quoted 4 lines · Thadeu Lima de Souza Cascardo

These are fixes that affect mostly the usb probe error path. It fixes UAF due to firmware loading touching freed memory by waiting for the load completion before releasing that memory. It also fixes a couple of identified memory leaks.

This goes via wireless tree, not net. Just send to linux-wireless (you have done). No need "net" in patch subject.

I would quickly check if you did really encounter problems and have tested this patchset with real hardware?

#17 lore raw
Quoted 6 lines · Ping-Ke Shih
Quoted 4 lines · Thadeu Lima de Souza Cascardo

These are fixes that affect mostly the usb probe error path. It fixes UAF due to firmware loading touching freed memory by waiting for the load completion before releasing that memory. It also fixes a couple of identified memory leaks.

Quoted 7 lines

This goes via wireless tree, not net. Just send to linux-wireless (you have done). No need "net" in patch subject.

I would quickly check if you did really encounter problems and have tested this patchset with real hardware?

Yeah, I was playing it safe here, in case some of the same rules apply, and "PATCH net" was required.

If found this with a reproducer emulating a usb gadget device (by using /dev/raw-gadget), and then injecting memory allocation failures at different points in the probe path (at ieee80211_register_hw and then at init_sw_vars).

I haven't tested this with real hardware, but given this lies in the probe error path, I suppose it would be harder to test for the bugs that they fix. On the other hand, it would be nice to at least confirm that it doesn't break them, though I find it hard that it would.

Thanks.
Cascardo.

#18 lore raw
Quoted 23 lines · Thadeu Lima de Souza Cascardo
Quoted 6 lines · Ping-Ke Shih
Quoted 4 lines · Thadeu Lima de Souza Cascardo

These are fixes that affect mostly the usb probe error path. It fixes UAF due to firmware loading touching freed memory by waiting for the load completion before releasing that memory. It also fixes a couple of identified memory leaks.

Quoted 7 lines

This goes via wireless tree, not net. Just send to linux-wireless (you have done). No need "net" in patch subject.

I would quickly check if you did really encounter problems and have tested this patchset with real hardware?

Yeah, I was playing it safe here, in case some of the same rules apply, and "PATCH net" was required.

If found this with a reproducer emulating a usb gadget device (by using /dev/raw-gadget), and then injecting memory allocation failures at different points in the probe path (at ieee80211_register_hw and then at init_sw_vars).

Quoted 4 lines

I haven't tested this with real hardware, but given this lies in the probe error path, I suppose it would be harder to test for the bugs that they fix. On the other hand, it would be nice to at least confirm that it doesn't break them, though I find it hard that it would.

Yeah, regressions are what we maintainers are most worried. We certainly do not want cleanup patches breaking existing setups.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches