mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yingjie Wang <x-professor@outlook.com>
To: Neill Kapron <nkapron@google.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Bingquan Chen <patzilla007@gmail.com>,
	Andrzej Pietrasiewicz <andrzej.p@collabora.com>,
	Felipe Balbi <balbi@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: [PATCH v2 1/2] usb: gadget: configfs: NUL-terminate extended property data
Date: Sat, 31 Oct 2026 18:13:58 +0000	[thread overview]
Message-ID: <7bb734dd2b0ef5f1ff7ffd9f1ffd8fd7b84e5073.1793470427.git.x-professor@outlook.com> (raw)
In-Reply-To: <cover.1793470427.git.x-professor@outlook.com>

Unicode extended properties include a UTF-16 terminator in their wire
length.  The configfs store path keeps the raw UTF-8 bytes, but the
configfs readback and descriptor conversion consume the source byte
implied by that terminator-inclusive length.

The allocation currently contains only the input bytes, so both paths
can read one byte past it.  Allocate the trailing NUL with kmemdup_nul()
and centralize the raw-to-wire length conversion used by the store path.
Avoid dereferencing page[-1] on an empty configfs write.

Fixes: 7419485f197c ("usb: gadget: configfs: OS Extended Properties descriptors support")
Reported-by: Bingquan Chen <patzilla007@gmail.com>
Link: https://lore.kernel.org/r/20260421141010.5607-1-patzilla007@gmail.com
Suggested-by: Neill Kapron <nkapron@google.com>
Link: https://lore.kernel.org/r/179338899796.2590875.15337207674245295434.b4-review@b4
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 drivers/usb/gadget/configfs.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 51df6d1d1487a..f5fff213d7952 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1283,6 +1283,21 @@ static inline struct usb_os_desc_ext_prop
 	return container_of(item, struct usb_os_desc_ext_prop, item);
 }
 
+static bool ext_prop_is_unicode_string(u8 type)
+{
+	return type == USB_EXT_PROP_UNICODE ||
+	       type == USB_EXT_PROP_UNICODE_ENV ||
+	       type == USB_EXT_PROP_UNICODE_LINK;
+}
+
+static int ext_prop_data_wire_len(u8 type, int raw_len)
+{
+	if (ext_prop_is_unicode_string(type))
+		return 2 * raw_len + 2;
+
+	return raw_len;
+}
+
 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
 {
 	return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
@@ -1332,9 +1347,7 @@ static ssize_t ext_prop_data_show(struct config_item *item, char *page)
 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
 	int len = ext_prop->data_len;
 
-	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
-	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
-	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
+	if (ext_prop_is_unicode_string(ext_prop->type))
 		len >>= 1;
 	memcpy(page, ext_prop->data, len);
 
@@ -1349,9 +1362,9 @@ static ssize_t ext_prop_data_store(struct config_item *item,
 	char *new_data;
 	size_t ret_len = len;
 
-	if (page[len - 1] == '\n' || page[len - 1] == '\0')
+	if (len && (page[len - 1] == '\n' || page[len - 1] == '\0'))
 		--len;
-	new_data = kmemdup(page, len, GFP_KERNEL);
+	new_data = kmemdup_nul(page, len, GFP_KERNEL);
 	if (!new_data)
 		return -ENOMEM;
 
@@ -1360,16 +1373,8 @@ static ssize_t ext_prop_data_store(struct config_item *item,
 	kfree(ext_prop->data);
 	ext_prop->data = new_data;
 	desc->ext_prop_len -= ext_prop->data_len;
-	ext_prop->data_len = len;
+	ext_prop->data_len = ext_prop_data_wire_len(ext_prop->type, len);
 	desc->ext_prop_len += ext_prop->data_len;
-	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
-	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
-	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
-		desc->ext_prop_len -= ext_prop->data_len;
-		ext_prop->data_len <<= 1;
-		ext_prop->data_len += 2;
-		desc->ext_prop_len += ext_prop->data_len;
-	}
 	if (desc->opts_mutex)
 		mutex_unlock(desc->opts_mutex);
 	return ret_len;
-- 
2.43.0


  reply	other threads:[~2026-10-31 18:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29  3:06 [PATCH] " Yingjie Wang
2026-10-30 19:36 ` Neill Kapron
2026-10-31 18:13   ` [PATCH v2 0/2] usb: gadget: configfs: fix extended property length handling Yingjie Wang
2026-10-31 18:13     ` Yingjie Wang [this message]
2026-10-31 18:13     ` [PATCH v2 2/2] usb: gadget: configfs: keep extended property lengths consistent Yingjie Wang

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=7bb734dd2b0ef5f1ff7ffd9f1ffd8fd7b84e5073.1793470427.git.x-professor@outlook.com \
    --to=x-professor@outlook.com \
    --cc=andrzej.p@collabora.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=patzilla007@gmail.com \
    --cc=stable@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®