Skip to contentSkip to conversation

Enforce buffer boundaries on RNDIS USB Gadget

PATCH In review Szymon Heidrich 3 participants 9 messages Nov 17, 2022 – Dec 9, 2022 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Enforce buffer boundaries on RNDIS USB Gadget

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Lack of validation of BufOffset could be exploited to dump arbitrary memory contents via NDIS packet filter.

---
 drivers/usb/gadget/rndis.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +6 -3
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
 	rndis_set_cmplt_type	*resp;
 	rndis_resp_t		*r;
 
+	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+	if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+	    (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+		return -EINVAL;
+
 	r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
 	if (!r)
 		return -ENOMEM;
 	resp = (rndis_set_cmplt_type *) r->buf;
 
-	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
 #ifdef	VERBOSE
 	debug("%s: Length: %d\n", __func__, BufLength);
 	debug("%s: Offset: %d\n", __func__, BufOffset);
Signature · 1 line
2.38.1
#1 lore raw

Szymon,

Quoted 7 lines · Szymon Heidrich

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Lack of validation of BufOffset could be exploited to dump arbitrary memory contents via NDIS packet filter.

Please run ./scripts/get_maintainer.pl on your patch and copy the maintainers.

Quoted 32 lines
---
 drivers/usb/gadget/rndis.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +6 -4
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
        rndis_set_cmplt_type    *resp;
        rndis_resp_t            *r;
 
+       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+       if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+           (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+               return -EINVAL;
+
        r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
        if (!r)
                return -ENOMEM;
        resp = (rndis_set_cmplt_type *) r->buf;
 
-       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
 #ifdef VERBOSE
        debug("%s: Length: %d\n", __func__, BufLength);
        debug("%s: Offset: %d\n", __func__, BufOffset);
--

2.38.1

#2 lore raw
Quoted 5 lines · Fabio Estevam

Szymon,

Quoted 1 line · Szymon Heidrich
Quoted 9 lines
Quoted 6 lines

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Lack of validation of BufOffset could be exploited to dump arbitrary memory contents via NDIS packet filter.

Please run ./scripts/get_maintainer.pl on your patch and copy the maintainers.

Hello Fabio,

Sorry I missed adding Lukasz and Marek - I'll keep that in mind for future.

Is there anything else missing from my side?

Best regards,
Szymon

Quoted 33 lines
Quoted 33 lines
---
 drivers/usb/gadget/rndis.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +6 -4
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
        rndis_set_cmplt_type    *resp;
        rndis_resp_t            *r;
 
+       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+       if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+           (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+               return -EINVAL;
+
        r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
        if (!r)
                return -ENOMEM;
        resp = (rndis_set_cmplt_type *) r->buf;
 
-       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
 #ifdef VERBOSE
        debug("%s: Length: %d\n", __func__, BufLength);
        debug("%s: Offset: %d\n", __func__, BufOffset);
--

2.38.1

#3 lore raw
Quoted 13 lines · Szymon Heidrich
Quoted 12 lines · Fabio Estevam

Szymon,

Quoted 7 lines · Szymon Heidrich

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Lack of validation of BufOffset could be exploited to dump arbitrary memory contents via NDIS packet filter.

Quoted 8 lines
Quoted 2 lines

Please run ./scripts/get_maintainer.pl on your patch and copy the maintainers.

Hello Fabio,

Sorry I missed adding Lukasz and Marek - I'll keep that in mind for future.

Is there anything else missing from my side?

There have been various security fixes recently which broke other things, so I am being careful now.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/rndis.c +6 -0
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
         rndis_set_cmplt_type    *resp;
         rndis_resp_t            *r;
 
+       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+       if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+           (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+               return -EINVAL;
+
         r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
         if (!r)
                 return -ENOMEM;
         resp = (rndis_set_cmplt_type *) r->buf;
 
Quoted 3 lines
Quoted 3 lines

drivers/usb/gadget/rndis.c static int rndis_set_response(int configNr, rndis_set_msg_type *buf) · line 865

Quoted 3 lines
drivers/usb/gadget/rndis.c +0 -3
-       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-

Reading through the RNDIS code, do you think the rndis_query_response and others which use buffer/offset data from the message should also be sanitized the same way ? I can imagine the query can be used to do test for 1bit of data all over the memory too.

#4 lore raw
Quoted 17 lines · Marek Vasut
Quoted 16 lines · Szymon Heidrich
Quoted 14 lines · Fabio Estevam

Szymon,

Quoted 7 lines · Szymon Heidrich

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Lack of validation of BufOffset could be exploited to dump arbitrary memory contents via NDIS packet filter.

Please run ./scripts/get_maintainer.pl on your patch and copy the maintainers.

Quoted 8 lines
Quoted 5 lines

Hello Fabio,

Sorry I missed adding Lukasz and Marek - I'll keep that in mind for future.

Is there anything else missing from my side?

There have been various security fixes recently which broke other things, so I am being careful now.

Sure, I completely understand that.
Thank you for your time and review.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/rndis.c +6 -0
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
          rndis_set_cmplt_type    *resp;
          rndis_resp_t            *r;
 
+       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+       if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+           (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+               return -EINVAL;
+
          r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
          if (!r)
                  return -ENOMEM;
          resp = (rndis_set_cmplt_type *) r->buf;
 
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

drivers/usb/gadget/rndis.c static int rndis_set_response(int configNr, rndis_set_msg_type *buf) · line 865

Quoted 3 lines
drivers/usb/gadget/rndis.c +0 -3
-       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-

Reading through the RNDIS code, do you think the rndis_query_response and others which use buffer/offset data from the message should also be sanitized the same way ? I can imagine the query can be used to do test for 1bit of data all over the memory too.

I added the extra validation in rndis_set_response as with the current implementation it is possible to manipulate InformationBufferOffset to exploit OID_GEN_CURRENT_PACKET_FILTER to set arbitrary memory contents within a 32byte offset as the devices packet filter. This value may be next retrieved using gen_ndis_query_resp so one may extract specific memory regions two bytes a time.

As for rndis_query_response I didn't touch it as the buffer offset and length passed to gen_ndis_query_resp are not used. Please let me know in case I'm missing something.

#5 lore raw

On 12/4/22 21:36, Szymon Heidrich wrote:

Hi,

sorry for the delays.

Quoted 26 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/rndis.c +6 -0
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
          rndis_set_cmplt_type    *resp;
          rndis_resp_t            *r;
 
+       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+       if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+           (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+               return -EINVAL;
+
          r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
          if (!r)
                  return -ENOMEM;
          resp = (rndis_set_cmplt_type *) r->buf;
 
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

drivers/usb/gadget/rndis.c static int rndis_set_response(int configNr, rndis_set_msg_type *buf) · line 865

Quoted 3 lines
drivers/usb/gadget/rndis.c +0 -3
-       BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-       BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-

Reading through the RNDIS code, do you think the rndis_query_response and others which use buffer/offset data from the message should also be sanitized the same way ? I can imagine the query can be used to do test for 1bit of data all over the memory too.

Quoted 5 lines

I added the extra validation in rndis_set_response as with the current implementation it is possible to manipulate InformationBufferOffset to exploit OID_GEN_CURRENT_PACKET_FILTER to set arbitrary memory contents within a 32byte offset as the devices packet filter. This value may be next retrieved using gen_ndis_query_resp so one may extract specific memory regions two bytes a time.

I see, can you please add that ^ explanation into the commit message and send V2 ? I'll pick it into the current MR.

Thanks

Quoted 2 lines

As for rndis_query_response I didn't touch it as the buffer offset and length passed to gen_ndis_query_resp are not used. Please let me know in case I'm missing something.

[...]

#6 lore raw

PATCH v2 1/1 usb: gadget: rndis: Prevent InformationBufferOffset manipulation

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Original implementation permits manipulation of InformationBufferOffset to exploit OID_GEN_CURRENT_PACKET_FILTER to set arbitrary memory contents within a 32byte offset as the devices packet filter. The packet filter value may be next retrieved using gen_ndis_query_resp so it is possible to extract specific memory regions two bytes a time.

The rndis_query_response was not modified as neither the buffer offset nor length passed to gen_ndis_query_resp is used.

---
V1 -> V2: Updated commit message

 drivers/usb/gadget/rndis.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +6 -3
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
 	rndis_set_cmplt_type	*resp;
 	rndis_resp_t		*r;
 
+	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+	if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+	    (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+		return -EINVAL;
+
 	r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
 	if (!r)
 		return -ENOMEM;
 	resp = (rndis_set_cmplt_type *) r->buf;
 
-	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
 #ifdef	VERBOSE
 	debug("%s: Length: %d\n", __func__, BufLength);
 	debug("%s: Offset: %d\n", __func__, BufOffset);
Signature · 1 line
2.38.1
Applied
#7 lore raw

Re: [PATCH v2] usb: gadget: rndis: Prevent InformationBufferOffset manipulation

Quoted 33 lines · Szymon Heidrich

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Original implementation permits manipulation of InformationBufferOffset to exploit OID_GEN_CURRENT_PACKET_FILTER to set arbitrary memory contents within a 32byte offset as the devices packet filter. The packet filter value may be next retrieved using gen_ndis_query_resp so it is possible to extract specific memory regions two bytes a time.

The rndis_query_response was not modified as neither the buffer offset nor length passed to gen_ndis_query_resp is used.

---
V1 -> V2: Updated commit message

  drivers/usb/gadget/rndis.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +6 -0
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
  	rndis_set_cmplt_type	*resp;
  	rndis_resp_t		*r;
 
+	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+	if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+	    (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+		return -EINVAL;
+
  	r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));

drivers/usb/gadget/rndis.c static int rndis_set_response(int configNr, rndis_set_msg_type *buf) · line 871

Quoted 10 lines
drivers/usb/gadget/rndis.c +0 -3
  	if (!r)
  		return -ENOMEM;
  	resp = (rndis_set_cmplt_type *) r->buf;
 
-	BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-	BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
  #ifdef	VERBOSE
  	debug("%s: Length: %d\n", __func__, BufLength);
  	debug("%s: Offset: %d\n", __func__, BufOffset);

Applied to usb/master, thanks

#8 lore raw

Re: [PATCH v2] usb: gadget: rndis: Prevent InformationBufferOffset manipulation

Quoted 36 lines · Marek Vasut
Quoted 35 lines · Szymon Heidrich

Prevent access to arbitrary memory locations in gen_ndis_set_resp via manipulation of buf->InformationBufferOffset. Original implementation permits manipulation of InformationBufferOffset to exploit OID_GEN_CURRENT_PACKET_FILTER to set arbitrary memory contents within a 32byte offset as the devices packet filter. The packet filter value may be next retrieved using gen_ndis_query_resp so it is possible to extract specific memory regions two bytes a time.

The rndis_query_response was not modified as neither the buffer offset nor length passed to gen_ndis_query_resp is used.

---
V1 -> V2: Updated commit message

  drivers/usb/gadget/rndis.c | 9 ++++++---

  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/rndis.c +5 -0
index 13c327ea38..3948f2cc9a 100644
@@ -855,14 +855,17 @@ static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
       rndis_set_cmplt_type    *resp;
       rndis_resp_t        *r;
   +    BufLength = get_unaligned_le32(&buf->InformationBufferLength);
+    BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
+    if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
+        (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
+        return -EINVAL;
+
       r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
       if (!r)
           return -ENOMEM;
       resp = (rndis_set_cmplt_type *) r->buf;
Quoted 8 lines

drivers/usb/gadget/rndis.c static int rndis_set_response(int configNr, rndis_set_msg_type *buf) · line 871

Quoted 6 lines
drivers/usb/gadget/rndis.c +0 -2
   -    BufLength = get_unaligned_le32(&buf->InformationBufferLength);
-    BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
-
   #ifdef    VERBOSE
       debug("%s: Length: %d\n", __func__, BufLength);
       debug("%s: Offset: %d\n", __func__, BufOffset);

Applied to usb/master, thanks

Thank you very much for your time and review.