Skip to contentSkip to conversation

usb: gadget: rndis: Avoid dereference before NULL check

PATCH In review Jim Lin 4 participants 6 messages Sep 8, 2022 – Sep 22, 2022 USB

lore
Share & tools
lore

Jim Lin

author
#0 lore raw

PATCH 1/1 usb: gadget: rndis: Avoid dereference before NULL check

NULL check is performed after params->dev is dereferenced in dev_get_stats.
Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-----------------
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +15 -22
index 64de9f1b874c..d2f18f34c8e5 100644
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	outbuf = (__le32 *)&resp[1];
 	resp->InformationBufferOffset = cpu_to_le32(16);
 
+	if (!params->dev)
+		return -ENODEV;
+
 	net = params->dev;
 	stats = dev_get_stats(net, &temp);
 
@@ -246,10 +249,8 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	/* mandatory */
 	case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
 		pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
-		if (params->dev) {
-			*outbuf = cpu_to_le32(params->dev->mtu);
-			retval = 0;
-		}
+		*outbuf = cpu_to_le32(params->dev->mtu);
+		retval = 0;
 		break;
 
 	/* mandatory */
@@ -266,19 +267,15 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	/* mandatory */
 	case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
 		pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
-		if (params->dev) {
-			*outbuf = cpu_to_le32(params->dev->mtu);
-			retval = 0;
-		}
+		*outbuf = cpu_to_le32(params->dev->mtu);
+		retval = 0;
 		break;
 
 	/* mandatory */
 	case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
 		pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
-		if (params->dev) {
-			*outbuf = cpu_to_le32(params->dev->mtu);
-			retval = 0;
-		}
+		*outbuf = cpu_to_le32(params->dev->mtu);
+		retval = 0;
 		break;
 
 	/* mandatory */
@@ -405,21 +402,17 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	/* mandatory */
 	case RNDIS_OID_802_3_PERMANENT_ADDRESS:
 		pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
-		if (params->dev) {
-			length = ETH_ALEN;
-			memcpy(outbuf, params->host_mac, length);
-			retval = 0;
-		}
+		length = ETH_ALEN;
+		memcpy(outbuf, params->host_mac, length);
+		retval = 0;
 		break;
 
 	/* mandatory */
 	case RNDIS_OID_802_3_CURRENT_ADDRESS:
 		pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
-		if (params->dev) {
-			length = ETH_ALEN;
-			memcpy(outbuf, params->host_mac, length);
-			retval = 0;
-		}
+		length = ETH_ALEN;
+		memcpy(outbuf, params->host_mac, length);
+		retval = 0;
 		break;
 
 	/* mandatory */
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Jim Lin

NULL check is performed after params->dev is dereferenced in dev_get_stats.
Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

You've beaten me to send such a patch... :-)

Quoted 12 lines
---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-----------------
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +0 -0
index 64de9f1b874c..d2f18f34c8e5 100644

drivers/usb/gadget/function/rndis.c static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf, · line 203

Quoted 6 lines
drivers/usb/gadget/function/rndis.c +3 -0
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	outbuf = (__le32 *)&resp[1];
 	resp->InformationBufferOffset = cpu_to_le32(16);
 
+	if (!params->dev)
+		return -ENODEV;
+

Hm, isn't this checked at the start of rndis_query_response(), this function's
only caller?

[...]

MBR, Sergey

maintainer
#2 lore raw
In reply to Jim Lin
Quoted 2 lines · Jim Lin

NULL check is performed after params->dev is dereferenced in dev_get_stats.

I do not understand this statement.

Quoted 14 lines

Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-----------------
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +0 -0
index 64de9f1b874c..d2f18f34c8e5 100644

drivers/usb/gadget/function/rndis.c static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf, · line 203

Quoted 6 lines
drivers/usb/gadget/function/rndis.c +3 -0
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	outbuf = (__le32 *)&resp[1];
 	resp->InformationBufferOffset = cpu_to_le32(16);
 
+	if (!params->dev)
+		return -ENODEV;
+

As Sergey points out, this check is useless and the ones below should also be removed.

But, why make this check at all, how did you trigger a problem with the current code?

Are you using this driver? If so, why? It is totally broken (as per the specification) and we really really need to just delete it from the tree to prevent anyone else from ever using it.

thanks,

greg k-h

author
#3 lore raw
Quoted 40 lines · Greg KH

External email: Use caution opening links or attachments

Quoted 2 lines · Jim Lin

NULL check is performed after params->dev is dereferenced in dev_get_stats.

I do not understand this statement.

Quoted 16 lines

Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-------------
----
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +0 -0
index 64de9f1b874c..d2f18f34c8e5 100644

drivers/usb/gadget/function/rndis.c static int gen_ndis_query_resp(struct · line 204

Quoted 7 lines
drivers/usb/gadget/function/rndis.c +3 -0
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct
 rndis_params *params, u32 OID, u8 *buf,
      outbuf = (__le32 *)&resp[1];
      resp->InformationBufferOffset = cpu_to_le32(16);
 
+     if (!params->dev)
+             return -ENODEV;
+

As Sergey points out, this check is useless and the ones below should also be removed.

But, why make this check at all, how did you trigger a problem with the
current code?

Quoted 8 lines

Are you using this driver? If so, why? It is totally broken (as per the specification) and we really really need to just delete it from the
tree to prevent anyone else from ever using it.

thanks,

greg k-h

Thanks for review, please ignore this patch.
Issue was triggered by our internal tool (probably not smart enough, and cause false alarm).

--nvpublic

#4 lore raw
Quoted 46 lines · Jim Lin
Quoted 45 lines · Greg KH

External email: Use caution opening links or attachments

Quoted 2 lines · Jim Lin

NULL check is performed after params->dev is dereferenced in dev_get_stats.

I do not understand this statement.

Quoted 16 lines

Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-------------
----
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +0 -0
index 64de9f1b874c..d2f18f34c8e5 100644

drivers/usb/gadget/function/rndis.c static int gen_ndis_query_resp(struct · line 204

Quoted 7 lines
drivers/usb/gadget/function/rndis.c +3 -0
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct
 rndis_params *params, u32 OID, u8 *buf,
      outbuf = (__le32 *)&resp[1];
      resp->InformationBufferOffset = cpu_to_le32(16);
 
+     if (!params->dev)
+             return -ENODEV;
+

As Sergey points out, this check is useless and the ones below should also be removed.

But, why make this check at all, how did you trigger a problem with the
current code?

Are you using this driver? If so, why? It is totally broken (as per the specification) and we really really need to just delete it from the
tree to prevent anyone else from ever using it.

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

Thanks for review, please ignore this patch.
Issue was triggered by our internal tool (probably not smart enough, and cause false alarm).

Ah, so that means I HAVE to reject it as you did not properly follow the requirements when you use tools like this. Please read Documentation/process/researcher-guidelines.rst for how to do this properly.

That is now a requirement to make it so that we are not the ones doing the debugging of random tools like we just had to do here...

You owe me 3 patch reviews now :)

thanks,

greg k-h

#5 lore raw
In reply to Greg KH

Hello!

Quoted 22 lines · Greg KH
Quoted 2 lines

NULL check is performed after params->dev is dereferenced in dev_get_stats.

I do not understand this statement.

Quoted 17 lines

Fixed by adding a NULL check before dereferencing params->dev and removing subsequent NULL checks for it.

---
 drivers/usb/gadget/function/rndis.c | 37 ++++++++++++-----------------
 1 file changed, 15 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/rndis.c +0 -0
index 64de9f1b874c..d2f18f34c8e5 100644
@@ -198,6 +198,9 @@ static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
 	outbuf = (__le32 *)&resp[1];
 	resp->InformationBufferOffset = cpu_to_le32(16);
 
Quoted 6 lines

drivers/usb/gadget/function/rndis.c static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf, · line 203

Quoted 3 lines
drivers/usb/gadget/function/rndis.c +3 -0
+	if (!params->dev)
+		return -ENODEV;
+

As Sergey points out, this check is useless and the ones below should also be removed.

Would you accept this patch modulo the above check then? If not,
I'll just resolve the corresponding SVACE checker as "won't fix" here. :-)

Quoted 2 lines

But, why make this check at all, how did you trigger a problem with the current code?

There's no problem, just the redundant NULL checks...

Quoted 3 lines

Are you using this driver? If so, why? It is totally broken (as per the specification) and we really really need to just delete it from the tree to prevent anyone else from ever using it.

Well, delete it then... :-)

Quoted 3 lines

thanks,

greg k-h

MBR, Sergey