Linux USB
 help / color / mirror / Atom feed
From: Lin Tzu Yang <rongh7426@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Neill Kapron <nkapron@google.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores
Date: Mon, 30 Nov 2026 19:50:55 +0800	[thread overview]
Message-ID: <20261130115055.842820-1-rongh7426@gmail.com> (raw)

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


             reply	other threads:[~2026-11-30 11:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:50 Lin Tzu Yang [this message]
2026-11-30 11:53 ` [PATCH] usb: gadget: f_uac1: fix return value of string attribute stores sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130115055.842820-1-rongh7426@gmail.com \
    --to=rongh7426@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox