Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: configfs: Prevent buffer overrun in usb_string_copy
@ 2023-06-30 11:04 Yiyuan Guo
  2023-06-30 12:17 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Yiyuan Guo @ 2023-06-30 11:04 UTC (permalink / raw)
  To: gregkh, dan.scally, andriy.shevchenko
  Cc: frank.li, christophe.jaillet, jgilab, chanh, linux-usb, yguoaz

In usb_string_copy(), when `strlen(s) == 0`, `str[ret - 1]` accesses at
index -1. Add a check to prevent buffer overrun when `s` is empty.

Signed-off-by: Yiyuan Guo <yguoaz@gmail.com>
---
 drivers/usb/gadget/configfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 4c639e9ddedc..457dbc267964 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -127,7 +127,7 @@ static int usb_string_copy(const char *s, char **s_copy)
 			return -ENOMEM;
 	}
 	strcpy(str, s);
-	if (str[ret - 1] == '\n')
+	if (ret && str[ret - 1] == '\n')
 		str[ret - 1] = '\0';
 	*s_copy = str;
 	return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-01  3:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-30 11:04 [PATCH] usb: gadget: configfs: Prevent buffer overrun in usb_string_copy Yiyuan Guo
2023-06-30 12:17 ` Greg KH
2023-06-30 13:13   ` yguoaz
2023-06-30 19:48     ` Greg KH
2023-07-01  3:48       ` yguoaz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox