Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores
@ 2026-11-30 11:50 Lin Tzu Yang
  2026-11-30 11:53 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Tzu Yang @ 2026-11-30 11:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Neill Kapron, linux-usb, linux-kernel

The configfs store for the f_uac1 string attributes (function_name,
p_it_name, c_it_name and the others) returns the result of scnprintf()
with a size of min(sizeof(opts->name), len). That is at most len - 1,
so every write is reported as short. The writer then writes the rest
again; for the last byte the size is 1, nothing is stored and 0 is
returned, and a writer that retries on a short write never finishes.
Each retry also overwrites the name with what is left, so the name ends
up empty.

With busybox in a QEMU arm64 guest (dummy_hcd), all of these hang until
they are killed, and the name reads back empty afterwards:

  echo MyAudio > functions/uac1.0/function_name
  printf NoNewline > functions/uac1.0/function_name
  echo Mic > functions/uac1.0/p_it_name

This started when snprintf() was replaced by scnprintf(): snprintf()
returned the full length of the string, which is len.

Do what f_uac2 does since commit 9499327714de ("usb: gadget: f_uac2: fix
return value for UAC2_ATTRIBUTE_STRING store") and commit e60284b63245
("usb: gadget: f_uac2: fix non-newline-terminated function name"):
return len, and strip only a trailing newline, so a name written without
one keeps its last character.

Fixes: c1a371866db9 ("usb: gadget: f_uac1: Replace snprintf() with the safer scnprintf() variant")
Cc: stable@vger.kernel.org
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
Tested on configfs-next 2e0fad9d2eae in a QEMU/KVM arm64 guest with
dummy_hcd and busybox: before, the writes above hang; after, they return
and read back MyAudio, NoNewline and Mic, a single-character name and a
70-character name are stored as written, and writes while the function
is linked into a configuration still fail with -EBUSY. I could not check
the strings on the host side, since dummy_hcd has no isochronous
endpoints and uac1 does not bind there. Build-tested with W=1 on
usb-next.

The same return value change is part of a syzbot RFC from July, "usb:
gadget: f_uac: fix multiple bugs in configfs attributes", which as far
as I can tell was never sent to the lists.

This patch was written with the help of an AI coding assistant
(Claude). I ran the tests above and reviewed the change.

 drivers/usb/gadget/function/f_uac1.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c
index 7539817cd4..656be855e4 100644
--- a/drivers/usb/gadget/function/f_uac1.c
+++ b/drivers/usb/gadget/function/f_uac1.c
@@ -1649,7 +1649,7 @@ static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
 					  const char *page, size_t len)	\
 {									\
 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
-	int ret = 0;							\
+	int ret = len;							\
 									\
 	mutex_lock(&opts->lock);					\
 	if (opts->refcnt) {						\
@@ -1657,8 +1657,11 @@ static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
 		goto end;						\
 	}								\
 									\
-	ret = scnprintf(opts->name, min(sizeof(opts->name), len),	\
-			"%s", page);					\
+	if (len && page[len - 1] == '\n')				\
+		len--;							\
+									\
+	scnprintf(opts->name, min(sizeof(opts->name), len + 1),		\
+		  "%s", page);						\
 									\
 end:									\
 	mutex_unlock(&opts->lock);					\
-- 
2.47.3


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

* Re: [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores
  2026-11-30 11:50 [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores Lin Tzu Yang
@ 2026-11-30 11:53 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 11:53 UTC (permalink / raw)
  To: Lin Tzu Yang; +Cc: linux-usb

> The configfs store for the f_uac1 string attributes (function_name,
> p_it_name, c_it_name and the others) returns the result of scnprintf()
> with a size of min(sizeof(opts->name), len). That is at most len - 1,
> so every write is reported as short. The writer then writes the rest
> again; for the last byte the size is 1, nothing is stored and 0 is
> returned, and a writer that retries on a short write never finishes.
> Each retry also overwrites the name with what is left, so the name ends
> up empty.
> 
> With busybox in a QEMU arm64 guest (dummy_hcd), all of these hang until
> they are killed, and the name reads back empty afterwards:
> 
>   echo MyAudio > functions/uac1.0/function_name
>   printf NoNewline > functions/uac1.0/function_name
>   echo Mic > functions/uac1.0/p_it_name
> 
> This started when snprintf() was replaced by scnprintf(): snprintf()
> returned the full length of the string, which is len.
> 
> Do what f_uac2 does since commit 9499327714de ("usb: gadget: f_uac2: fix
> return value for UAC2_ATTRIBUTE_STRING store") and commit e60284b63245
> ("usb: gadget: f_uac2: fix non-newline-terminated function name"):
> return len, and strip only a trailing newline, so a name written without
> one keeps its last character.
> 
> Fixes: c1a371866db9 ("usb: gadget: f_uac1: Replace snprintf() with the safer scnprintf() variant")
> Cc: stable@vger.kernel.org
> Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130115055.842820-1-rongh7426@gmail.com?part=1

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

end of thread, other threads:[~2026-11-30 11:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 11:50 [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores Lin Tzu Yang
2026-11-30 11:53 ` sashiko-bot

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