Skip to contentSkip to conversation

usb: gadget: f_midi: Replace strlcpy() with strscpy()

PATCH New Kees Cook 1 participant 1 message Nov 16, 2023 USB

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 usb: gadget: f_midi: Replace strlcpy() with strscpy()

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated[1]. Additionally, it returns the size of the source string, not the resulting size of the destination string. In an effort to remove strlcpy() completely[2], replace strlcpy() here with strscpy().

In the unlikely (impossible?) case where opts->id was larger than PAGE_SIZE, this will now correctly report truncation errors.

---
 drivers/usb/gadget/function/f_midi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_midi.c +2 -2
index 5335845d697b..20c6fbd94f32 100644
@@ -1177,11 +1177,11 @@ F_MIDI_OPT(out_ports, true, MAX_PORTS);
 static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
 {
 	struct f_midi_opts *opts = to_f_midi_opts(item);
-	int result;
+	ssize_t result;
 
 	mutex_lock(&opts->lock);
 	if (opts->id) {
-		result = strlcpy(page, opts->id, PAGE_SIZE);
+		result = strscpy(page, opts->id, PAGE_SIZE);
 	} else {
 		page[0] = 0;
 		result = 0;
Signature · 1 line
2.34.1