Skip to contentSkip to conversation

net: hso: do not call unregister if not registered

PATCH In review Greg KH 4 participants 6 messages Oct 2, 2020 – Aug 17, 2021

Subsystems (5)
lore
Share & tools
lore

Greg KH

author maintainer
#0 lore raw

PATCH v2 1/1 net: hso: do not call unregister if not registered

From: Tuba Yavuz <[email protected]>

On an error path inside the hso_create_net_device function of the hso driver, hso_free_net_device gets called. This causes a use-after-free and a double-free if register_netdev has not been called yet as hso_free_net_device calls unregister_netdev regardless. I think the driver should distinguish these cases and call unregister_netdev only if register_netdev has been called.

---
v2: format cleaned up based on feedback from previous review
Forward to Greg to submit due to email problems on Tuba's side

drivers/net/usb/hso.c +2 -1
index 2bb28db89432..e6b56bdf691d 100644
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
 	remove_net_device(hso_net->parent);
 
-	if (hso_net->net)
+	if (hso_net->net &&
+	    hso_net->net->reg_state == NETREG_REGISTERED)
 		unregister_netdev(hso_net->net);
 
 	/* start freeing */
maintainer
#1 lore raw

From: Greg KH <[email protected]>
Date: Fri, 2 Oct 2020 13:43:23 +0200

drivers/net/usb/hso.c static void hso_free_net_device(struct hso_device *hso_dev, bool bailout) · line 2373

Quoted 10 lines
drivers/net/usb/hso.c +2 -1
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
 	remove_net_device(hso_net->parent);
 
-	if (hso_net->net)
+	if (hso_net->net &&
+	    hso_net->net->reg_state == NETREG_REGISTERED)
 		unregister_netdev(hso_net->net);
 
 	/* start freeing */

I really want to get out of the habit of drivers testing the internal netdev registration state to make decisions.

Instead, please track this internally. You know if you registered the device or not, therefore use that to control whether you try to unregister it or not.

Thank you.

author maintainer
#2 lore raw
Quoted 12 lines · David Miller

From: Greg KH <[email protected]>
Date: Fri, 2 Oct 2020 13:43:23 +0200

Quoted 9 lines
drivers/net/usb/hso.c +2 -1
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
 	remove_net_device(hso_net->parent);
 
-	if (hso_net->net)
+	if (hso_net->net &&
+	    hso_net->net->reg_state == NETREG_REGISTERED)
 		unregister_netdev(hso_net->net);
 
Quoted 8 lines

drivers/net/usb/hso.c static void hso_free_net_device(struct hso_device *hso_dev, bool bailout) · line 2373

Quoted 1 line
drivers/net/usb/hso.c +0 -0
 	/* start freeing */

I really want to get out of the habit of drivers testing the internal netdev registration state to make decisions.

Instead, please track this internally. You know if you registered the device or not, therefore use that to control whether you try to unregister it or not.

Fair enough. Tuba, do you want to fix this up in this way, or do you recommend that someone else do it?

thanks,

greg k-h

#3 lore raw

Hi Greg, Tuba,

Quoted 18 lines · Greg KH
Quoted 17 lines · David Miller

From: Greg KH <[email protected]>
Date: Fri, 2 Oct 2020 13:43:23 +0200

drivers/net/usb/hso.c static void hso_free_net_device(struct hso_device *hso_dev, bool bailout) · line 2373

Quoted 10 lines
drivers/net/usb/hso.c +2 -1
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
 	remove_net_device(hso_net->parent);
 
-	if (hso_net->net)
+	if (hso_net->net &&
+	    hso_net->net->reg_state == NETREG_REGISTERED)
 		unregister_netdev(hso_net->net);
 
 	/* start freeing */

I really want to get out of the habit of drivers testing the internal netdev registration state to make decisions.

Quoted 6 lines
Quoted 3 lines

Instead, please track this internally. You know if you registered the device or not, therefore use that to control whether you try to unregister it or not.

Fair enough. Tuba, do you want to fix this up in this way, or do you recommend that someone else do it?

Do I miss something, or did that possibly fall through the cracks?

I was checking some open issues on a downstream distro side and found htat this thread did not got a follow-up.

Regards,
Salvatore

author maintainer
#4 lore raw
Quoted 25 lines · Salvatore Bonaccorso

Hi Greg, Tuba,

Quoted 22 lines · Greg KH
Quoted 20 lines · David Miller

From: Greg KH <[email protected]>
Date: Fri, 2 Oct 2020 13:43:23 +0200

drivers/net/usb/hso.c static void hso_free_net_device(struct hso_device *hso_dev, bool bailout) · line 2373

Quoted 10 lines
drivers/net/usb/hso.c +2 -1
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
 	remove_net_device(hso_net->parent);
 
-	if (hso_net->net)
+	if (hso_net->net &&
+	    hso_net->net->reg_state == NETREG_REGISTERED)
 		unregister_netdev(hso_net->net);
 
 	/* start freeing */

I really want to get out of the habit of drivers testing the internal netdev registration state to make decisions.

Instead, please track this internally. You know if you registered the device or not, therefore use that to control whether you try to unregister it or not.

Quoted 7 lines
Quoted 2 lines

Fair enough. Tuba, do you want to fix this up in this way, or do you recommend that someone else do it?

Do I miss something, or did that possibly fall through the cracks?

I was checking some open issues on a downstream distro side and found htat this thread did not got a follow-up.

I did not see a follow-up patch :(

#5 lore raw
In reply to Salvatore Bonaccorso

Hi Salvatore,

I think it would be best if one of the developers of the hso driver could develop a patch along the lines of David Miller's earlier suggestion.

Best,

Tuba

________________________________________
From: Salvatore Bonaccorso <[email protected]> on behalf of Salvatore Bonaccorso <[email protected]>
Sent: Monday, August 16, 2021 2:52 AM
To: Greg KH
Cc: David Miller; Yavuz, Tuba; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2] net: hso: do not call unregister if not registered

[External Email]

Hi Greg, Tuba,

Quoted 18 lines · Greg KH
Quoted 17 lines · David Miller

From: Greg KH <[email protected]>
Date: Fri, 2 Oct 2020 13:43:23 +0200

drivers/net/usb/hso.c static void hso_free_net_device(struct hso_device *hso_dev, bool bailout) · line 2373

Quoted 10 lines
drivers/net/usb/hso.c +2 -1
@@ -2366,7 +2366,8 @@ static void hso_free_net_device(struct hso_device *hso_dev, bool bailout)
 
  remove_net_device(hso_net->parent);
 
- if (hso_net->net)
+ if (hso_net->net &&
+     hso_net->net->reg_state == NETREG_REGISTERED)
          unregister_netdev(hso_net->net);
 
  /* start freeing */

I really want to get out of the habit of drivers testing the internal netdev registration state to make decisions.

Quoted 6 lines
Quoted 3 lines

Instead, please track this internally. You know if you registered the device or not, therefore use that to control whether you try to unregister it or not.

Fair enough. Tuba, do you want to fix this up in this way, or do you recommend that someone else do it?

Do I miss something, or did that possibly fall through the cracks?

I was checking some open issues on a downstream distro side and found htat this thread did not got a follow-up.

Regards,
Salvatore