Skip to contentSkip to conversation

usb: gadget: function: Fix returning incorrect PNP string

PATCH In review Wei Ming Chen 2 participants 4 messages Jan 5, 2022 – Jan 26, 2022 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: function: Fix returning incorrect PNP string

There will be 2 leading bytes indicating the total length of the PNP string, so I think we should add value by 2, otherwise the PNP string copied to user will not contain the last 2 bytes

---
 drivers/usb/gadget/function/f_printer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_printer.c +3 -1
index abec5c58f525..3fb00fd0b5ee 100644
@@ -1005,9 +1005,11 @@ static int printer_func_setup(struct usb_function *f,
 				break;
 			}
 			value = strlen(dev->pnp_string);
+			memcpy(buf + 2, dev->pnp_string, value);
+
+			value += 2;
 			buf[0] = (value >> 8) & 0xFF;
 			buf[1] = value & 0xFF;
-			memcpy(buf + 2, dev->pnp_string, value);
 			DBG(dev, "1284 PNP String: %x %s\n", value,
 			    dev->pnp_string);
 			break;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 29 lines · Wei Ming Chen

There will be 2 leading bytes indicating the total length of the PNP string, so I think we should add value by 2, otherwise the PNP string copied to user will not contain the last 2 bytes

---
 drivers/usb/gadget/function/f_printer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_printer.c +3 -1
index abec5c58f525..3fb00fd0b5ee 100644
@@ -1005,9 +1005,11 @@ static int printer_func_setup(struct usb_function *f,
 				break;
 			}
 			value = strlen(dev->pnp_string);
+			memcpy(buf + 2, dev->pnp_string, value);
+
+			value += 2;
 			buf[0] = (value >> 8) & 0xFF;
 			buf[1] = value & 0xFF;
-			memcpy(buf + 2, dev->pnp_string, value);
 			DBG(dev, "1284 PNP String: %x %s\n", value,
 			    dev->pnp_string);
 			break;
Signature · 1 line
2.25.1

Are you sure this is correct?

How is this related to this recent thread:
https://lore.kernel.org/all/CAKjGFBUdjXcZoVV4jdrgTz4rKThTfZAK4CqreKmBZ4KHE+K1GA@mail.gmail.com/#t

your change is different from what is proposed there, why?

thanks,

greg k-h

author
#2 lore raw

Greg KH <[email protected]> 於 2022年1月6日 週四 下午10:35寫道:

Quoted 34 lines · Wei Ming Chen
Quoted 27 lines

There will be 2 leading bytes indicating the total length of the PNP string, so I think we should add value by 2, otherwise the PNP string copied to user will not contain the last 2 bytes

---
 drivers/usb/gadget/function/f_printer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_printer.c +3 -2
index abec5c58f525..3fb00fd0b5ee 100644
@@ -1005,9 +1005,11 @@ static int printer_func_setup(struct usb_function *f,
                              break;
                      }
                      value = strlen(dev->pnp_string);
+                     memcpy(buf + 2, dev->pnp_string, value);
+
+                     value += 2;
                      buf[0] = (value >> 8) & 0xFF;
                      buf[1] = value & 0xFF;
-                     memcpy(buf + 2, dev->pnp_string, value);
                      DBG(dev, "1284 PNP String: %x %s\n", value,
                          dev->pnp_string);
                      break;
--
Quoted 2 lines

2.25.1

Are you sure this is correct?

Quoted 4 lines

How is this related to this recent thread:
https://lore.kernel.org/all/CAKjGFBUdjXcZoVV4jdrgTz4rKThTfZAK4CqreKmBZ4KHE+K1GA@mail.gmail.com/#t

your change is different from what is proposed there, why?

I didn’t notice this thread before I send this patch, I think the concept of my change is similar to Volodymyr Lisivka’s change, he/she introduced a separate variable for the PNP string length, I think it may be clearer and more readable than just “value += 2”

Another thing that I am not too sure whether I am correct is this line of code

DBG(dev, "1284 PNP String: %x %s\n", value,

                             dev->pnp_string);

What Volodymyr Lisivka changed is like this

DBG(dev, "1284 PNP String: %x %s\n", pnp_length,

                             dev->pnp_string);

In my change, “value” equals to “pnp_length + 2” in Volodymyr Lisivka’s change, and I think we should print “the PNP string length + 2” instead of “the PNP string length”?

Quoted 4 lines

thanks,

greg k-h

thanks,

Wei Ming Chen

maintainer
#3 lore raw
Quoted 51 lines · 陳偉銘

Greg KH <[email protected]> 於 2022年1月6日 週四 下午10:35寫道:

Quoted 34 lines · Wei Ming Chen
Quoted 27 lines

There will be 2 leading bytes indicating the total length of the PNP string, so I think we should add value by 2, otherwise the PNP string copied to user will not contain the last 2 bytes

---
 drivers/usb/gadget/function/f_printer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_printer.c +3 -2
index abec5c58f525..3fb00fd0b5ee 100644
@@ -1005,9 +1005,11 @@ static int printer_func_setup(struct usb_function *f,
                              break;
                      }
                      value = strlen(dev->pnp_string);
+                     memcpy(buf + 2, dev->pnp_string, value);
+
+                     value += 2;
                      buf[0] = (value >> 8) & 0xFF;
                      buf[1] = value & 0xFF;
-                     memcpy(buf + 2, dev->pnp_string, value);
                      DBG(dev, "1284 PNP String: %x %s\n", value,
                          dev->pnp_string);
                      break;
--
Quoted 2 lines

2.25.1

Are you sure this is correct?

Quoted 4 lines

How is this related to this recent thread:
https://lore.kernel.org/all/CAKjGFBUdjXcZoVV4jdrgTz4rKThTfZAK4CqreKmBZ4KHE+K1GA@mail.gmail.com/#t

your change is different from what is proposed there, why?

I didn’t notice this thread before I send this patch, I think the concept of my change is similar to Volodymyr Lisivka’s change, he/she introduced a separate variable for the PNP string length, I think it may be clearer and more readable than just “value += 2”

Another thing that I am not too sure whether I am correct is this line of code

DBG(dev, "1284 PNP String: %x %s\n", value,

                             dev->pnp_string);
Quoted 9 lines

What Volodymyr Lisivka changed is like this

DBG(dev, "1284 PNP String: %x %s\n", pnp_length,

                             dev->pnp_string);

In my change, “value” equals to “pnp_length + 2” in Volodymyr Lisivka’s change, and I think we should print “the PNP string length + 2” instead of “the PNP string length”?

I do not know, I suggest you two work together to get the correct fix submitted.

thanks,

greg k-h