Skip to contentSkip to conversation

iwlwifi: fix double free/complete in firmware loading

PATCH Superseded Johannes Berg 2 participants 5 messages Jun 5, 2012 Networking drivers (wireless)

lore
Share & tools
lore

Johannes Berg

author maintainer
#0 lore raw

PATCH 1/1 iwlwifi: fix double free/complete in firmware loading

From: Johannes Berg <[email protected]>

Linus reported that due to mac80211 failing to register the device (due to WoWLAN) his machine crashed etc. as we double-freed the vmalloc() firmware area. His patch to fix it was very similar to this one but I noticed that there's another bug in the area: we complete the completion before starting, so since we're running in a work struct context stop() could be called while in the middle of start() which will almost certainly lead to issues.

Make a modification similar to his to avoid the double- free but also move the completion to another spot so it is only done after start() either finished or failed so that stop() can have a consistent state.

---
 drivers/net/wireless/iwlwifi/iwl-drv.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/wireless/iwlwifi/iwl-drv.c +11 -3
index d742900..1f4d6af 100644
@@ -861,13 +861,20 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 
 	/* We have our copies now, allow OS release its copies */
 	release_firmware(ucode_raw);
-	complete(&drv->request_firmware_complete);
 
 	drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
 
-	if (!drv->op_mode)
-		goto out_free_fw;
+	if (!drv->op_mode) {
+		complete(&drv->request_firmware_complete);
+		goto out_release_driver;
+	}
 
+	/*
+	 * Complete the firmware request last so that
+	 * a driver unbind (stop) doesn't run while we
+	 * are doing the start() above.
+	 */
+	complete(&drv->request_firmware_complete);
 	return;
 
  try_again:
@@ -883,6 +890,7 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 	release_firmware(ucode_raw);
  out_unbind:
 	complete(&drv->request_firmware_complete);
+ out_release_driver:
 	device_release_driver(drv->trans->dev);
 }
 
Signature · 1 line
1.7.10
#1 lore raw
Quoted 4 lines · Johannes Berg
drivers/net/wireless/iwlwifi/iwl-drv.c +0 -0
@@ -861,13 +861,20 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 
        /* We have our copies now, allow OS release its copies */
        release_firmware(ucode_raw);

drivers/net/wireless/iwlwifi/iwl-drv.c static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context) · line 870

Quoted 10 lines
drivers/net/wireless/iwlwifi/iwl-drv.c +4 -3
-       complete(&drv->request_firmware_complete);
 
        drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
 
-       if (!drv->op_mode)
-               goto out_free_fw;
+       if (!drv->op_mode) {
+               complete(&drv->request_firmware_complete);
+               goto out_release_driver;
+       }

If you do it this way, you might as well replace the

   complete(&drv->request_firmware_complete);
   goto out_release_driver;

with just a "goto out_unbind".

Linus

author maintainer
#2 lore raw
Quoted 19 lines · Linus Torvalds
Quoted 4 lines · Johannes Berg
drivers/net/wireless/iwlwifi/iwl-drv.c +0 -0
@@ -861,13 +861,20 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 
       /* We have our copies now, allow OS release its copies */
       release_firmware(ucode_raw);

drivers/net/wireless/iwlwifi/iwl-drv.c static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context) · line 870

Quoted 10 lines
drivers/net/wireless/iwlwifi/iwl-drv.c +4 -3
-       complete(&drv->request_firmware_complete);
 
       drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
 
-       if (!drv->op_mode)
-               goto out_free_fw;
+       if (!drv->op_mode) {
+               complete(&drv->request_firmware_complete);
+               goto out_release_driver;
+       }

If you do it this way, you might as well replace the

Quoted 4 lines
   complete(&drv->request_firmware_complete);
   goto out_release_driver;

with just a "goto out_unbind".

Good point.

johannes

author maintainer
#3 lore raw

PATCH v2 1/1 iwlwifi: fix double free/complete in firmware loading

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

Linus reported that due to mac80211 failing to register the device (due to WoWLAN) his machine crashed etc. as we double-freed the vmalloc() firmware area. His patch to fix it was very similar to this one but I noticed that there's another bug in the area: we complete the completion before starting, so since we're running in a work struct context stop() could be called while in the middle of start() which will almost certainly lead to issues.

Make a modification similar to his to avoid the double- free but also move the completion to another spot so it is only done after start() either finished or failed so that stop() can have a consistent state.

---
 drivers/net/wireless/iwlwifi/iwl-drv.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/net/wireless/iwlwifi/iwl-drv.c +8 -2
index d742900..38fbbe7 100644
@@ -861,13 +861,18 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 
 	/* We have our copies now, allow OS release its copies */
 	release_firmware(ucode_raw);
-	complete(&drv->request_firmware_complete);
 
 	drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
 
 	if (!drv->op_mode)
-		goto out_free_fw;
+		goto out_unbind;
 
+	/*
+	 * Complete the firmware request last so that
+	 * a driver unbind (stop) doesn't run while we
+	 * are doing the start() above.
+	 */
+	complete(&drv->request_firmware_complete);
 	return;
 
  try_again:
@@ -883,6 +888,7 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 	release_firmware(ucode_raw);
  out_unbind:
 	complete(&drv->request_firmware_complete);
+ out_release_driver:
 	device_release_driver(drv->trans->dev);
 }
 
Signature · 1 line
1.7.10
author maintainer
#4 lore raw

PATCH v3 1/1 iwlwifi: fix double free/complete in firmware loading

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

Linus reported that due to mac80211 failing to register the device (due to WoWLAN) his machine crashed etc. as we double-freed the vmalloc() firmware area. His patch to fix it was very similar to this one but I noticed that there's another bug in the area: we complete the completion before starting, so since we're running in a work struct context stop() could be called while in the middle of start() which will almost certainly lead to issues.

Make a modification similar to his to avoid the double- free but also move the completion to another spot so it is only done after start() either finished or failed so that stop() can have a consistent state.

---
Wow, clearly I'm not paying enough attention today, sorry. Remove the stray label.

 drivers/net/wireless/iwlwifi/iwl-drv.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/wireless/iwlwifi/iwl-drv.c +7 -2
index d742900..fac67a5 100644
@@ -861,13 +861,18 @@ static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
 
 	/* We have our copies now, allow OS release its copies */
 	release_firmware(ucode_raw);
-	complete(&drv->request_firmware_complete);
 
 	drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
 
 	if (!drv->op_mode)
-		goto out_free_fw;
+		goto out_unbind;
 
+	/*
+	 * Complete the firmware request last so that
+	 * a driver unbind (stop) doesn't run while we
+	 * are doing the start() above.
+	 */
+	complete(&drv->request_firmware_complete);
 	return;
 
  try_again:
Signature · 1 line
1.7.10