Skip to contentSkip to conversation

rtl8150: set memory to all 0xFFs on failed register reads

PATCH In review Anant Thazhemadam 3 participants 6 messages Sep 16, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtl8150: set memory to all 0xFFs on failed register reads

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

---
 drivers/net/usb/rtl8150.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/usb/rtl8150.c +7 -2
index 733f120c852b..04fca7bfcbcb 100644
@@ -162,8 +162,13 @@ static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 			      RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
 			      indx, 0, buf, size, 500);
-	if (ret > 0 && ret <= size)
+
+	if (ret < 0)
+		memset(data, 0xff, size);
+
+	else
 		memcpy(data, buf, ret);
+
 	kfree(buf);
 	return ret;
 }
@@ -276,7 +281,7 @@ static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
 
 static inline void set_ethernet_addr(rtl8150_t * dev)
 {
-	u8 node_id[6];
+	u8 node_id[6] = {0};
 
 	get_registers(dev, IDR, sizeof(node_id), node_id);
 	memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
maintainer
#1 lore raw
Quoted 10 lines · Anant Thazhemadam

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

It would be better to use this new api call instead of usb_control_msg():

	https://lore.kernel.org/r/[email protected]

How about porting this patch to run on top of that series instead? That should make this logic much simpler.

Quoted 29 lines
---
 drivers/net/usb/rtl8150.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/usb/rtl8150.c +6 -1
index 733f120c852b..04fca7bfcbcb 100644
@@ -162,8 +162,13 @@ static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 			      RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
 			      indx, 0, buf, size, 500);
-	if (ret > 0 && ret <= size)
+
+	if (ret < 0)
+		memset(data, 0xff, size);
+
+	else
 		memcpy(data, buf, ret);
+
 	kfree(buf);
 	return ret;
 }

drivers/net/usb/rtl8150.c static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg) · line 284

Quoted 5 lines
drivers/net/usb/rtl8150.c +1 -1
@@ -276,7 +281,7 @@ static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
 
 static inline void set_ethernet_addr(rtl8150_t * dev)
 {
-	u8 node_id[6];
+	u8 node_id[6] = {0};

This should not be needed to be done.

thanks,

greg k-h
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

maintainer
#2 lore raw
Quoted 9 lines · Greg KH
Quoted 8 lines · Anant Thazhemadam

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

Quoted 6 lines
Quoted 2 lines

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

It would be better to use this new api call instead of usb_control_msg():

	https://lore.kernel.org/r/[email protected]

Heh, wasn't aware of the new api.

Quoted 2 lines

How about porting this patch to run on top of that series instead? That should make this logic much simpler.

I'll need to check if in this case 'size' is the right amount of bytes expected and not an upper limit. Then i'll convert it to the new api.

cheers,
Petko

Quoted 36 lines
Quoted 28 lines
---
 drivers/net/usb/rtl8150.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/usb/rtl8150.c +6 -1
index 733f120c852b..04fca7bfcbcb 100644
@@ -162,8 +162,13 @@ static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 			      RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
 			      indx, 0, buf, size, 500);
-	if (ret > 0 && ret <= size)
+
+	if (ret < 0)
+		memset(data, 0xff, size);
+
+	else
 		memcpy(data, buf, ret);
+
 	kfree(buf);
 	return ret;
 }

drivers/net/usb/rtl8150.c static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg) · line 284

Quoted 5 lines
drivers/net/usb/rtl8150.c +1 -1
@@ -276,7 +281,7 @@ static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
 
 static inline void set_ethernet_addr(rtl8150_t * dev)
 {
-	u8 node_id[6];
+	u8 node_id[6] = {0};

This should not be needed to be done.

Quoted 4 lines

thanks,

greg k-h

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
In reply to Greg KH
Quoted 9 lines · Greg KH
Quoted 8 lines · Anant Thazhemadam

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

Quoted 8 lines
Quoted 2 lines

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

It would be better to use this new api call instead of usb_control_msg():

	https://lore.kernel.org/r/[email protected]

How about porting this patch to run on top of that series instead? That should make this logic much simpler.

This looks viable to me. I'll be sure to try this out.

Quoted 29 lines
Quoted 29 lines
---
 drivers/net/usb/rtl8150.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/usb/rtl8150.c +6 -1
index 733f120c852b..04fca7bfcbcb 100644
@@ -162,8 +162,13 @@ static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 			      RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
 			      indx, 0, buf, size, 500);
-	if (ret > 0 && ret <= size)
+
+	if (ret < 0)
+		memset(data, 0xff, size);
+
+	else
 		memcpy(data, buf, ret);
+
 	kfree(buf);
 	return ret;
 }
@@ -276,7 +281,7 @@ static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
 
Quoted 5 lines

drivers/net/usb/rtl8150.c static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg) · line 284

Quoted 4 lines
drivers/net/usb/rtl8150.c +1 -1
 static inline void set_ethernet_addr(rtl8150_t * dev)
 {
-	u8 node_id[6];
+	u8 node_id[6] = {0};

This should not be needed to be done.

Noted.

Thank you for your time.

Thanks,
Anant

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

maintainer
#4 lore raw
In reply to Anant Thazhemadam
Quoted 2 lines · Anant Thazhemadam

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

Not true, memcpy() is only called if "ret" is positive.

Quoted 4 lines

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

usb_control_msg() returns negative on error (look up usb_internal_control_msg() to see for yourself) so it does not go unnoticed. If for some reason it return zero, nothing is copied. Also, if usb transfer fail no register values are being copied anywhere.

Your patch also allows for memcpy() to be called with 'size' either zero or greater than the allocated buffer size. Please, look at the code carefully.

Quoted 2 lines

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

I wasn't aware drivers are now responsible for filling up the memory with anything. Does not sound like a good idea to me.

Quoted 4 lines

Well, NACK from me.

cheers,
Petko

Quoted 36 lines
---
 drivers/net/usb/rtl8150.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/usb/rtl8150.c +7 -2
index 733f120c852b..04fca7bfcbcb 100644
@@ -162,8 +162,13 @@ static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
 			      RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
 			      indx, 0, buf, size, 500);
-	if (ret > 0 && ret <= size)
+
+	if (ret < 0)
+		memset(data, 0xff, size);
+
+	else
 		memcpy(data, buf, ret);
+
 	kfree(buf);
 	return ret;
 }
@@ -276,7 +281,7 @@ static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
 
 static inline void set_ethernet_addr(rtl8150_t * dev)
 {
-	u8 node_id[6];
+	u8 node_id[6] = {0};
 
 	get_registers(dev, IDR, sizeof(node_id), node_id);
 	memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
Signature · 1 line
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#5 lore raw
Quoted 4 lines · Petko Manolov
Quoted 2 lines · Anant Thazhemadam

get_registers() copies whatever memory is written by the usb_control_msg() call even if the underlying urb call ends up failing.

Not true, memcpy() is only called if "ret" is positive.

Right. I'm really sorry I fumbled and messed up the commit message there. Thank you for pointing that out.

Quoted 6 lines
Quoted 4 lines

If get_registers() fails, or ends up reading 0 bytes, meaningless and junk register values would end up being copied over (and eventually read by the driver), and since most of the callers of get_registers() don't check the return values of get_registers() either, this would go unnoticed.

usb_control_msg() returns negative on error (look up usb_internal_control_msg() to see for yourself) so it does not go unnoticed.

When I said "this would go unnoticed", I meant get_register() failing would go unnoticed, not that usb_control_msg() failing would go unnoticed. I agree that get_registers() notices usb_control_msg() failing, and appropriately returns the return value from usb_control_msg().
But there are many instances where get_registers() is called but the return value of get_registers() is not checked, to see if it failed or not; hence, "this would go unnoticed".

Quoted 2 lines

If for some reason it return zero, nothing is copied. Also, if usb transfer fail no register values are being copied anywhere.

True.
Now consider set_ethernet_addr(), and suppose get_register() fails when invoked from inside set_ethernet_addr().
As you said, no value is copied back, which means no value is copied back into node_id, which leaves node_id uninitialized. This node_id (still uninitialized) is then blindly copied into dev->netdev->dev_addr; which is less than ideal and could also quickly prove to become an issue, right?

Quoted 2 lines

Your patch also allows for memcpy() to be called with 'size' either zero or greater than the allocated buffer size. Please, look at the code carefully.

Oh. I apologize for this. This can be reverted relatively easily.

Quoted 4 lines
Quoted 2 lines

It might be a better idea to try and mirror the PCI master abort termination and set memory to 0xFFs instead in such cases.

I wasn't aware drivers are now responsible for filling up the memory with anything. Does not sound like a good idea to me.

Since we copy the correct register values when get_register() doesn't fail, I thought it might be a slightly better alternative to fill node_id with 0xFFs, instead of leaving it go uninitialized in case get_registers() fails.

Also, what are the odds that a successful get_register() call would see 0xFFs being copied?
If that's very real scenario, then I admit this doesn't work at all.

The only other alternative approach I can think of that can handle the issue I highlighted above, is to introduce checking for get_registers()'s return values nearly everywhere it gets called.
Would that be a more preferable and welcome approach?

Thank you for your time.

Thanks,
Anant

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees