Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: configfs: fix OOB read on ext_prop type toggle
@ 2026-11-30 21:24 Tanuj Sharma
  2026-11-30 21:44 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tanuj Sharma @ 2026-11-30 21:24 UTC (permalink / raw)
  To: gregkh; +Cc: linux-usb, stable

In ext_prop_type_store(), toggling the property type from BINARY to
UNICODE doubles data_len without reallocating the data buffer. When
ext_prop_data_show() subsequently reads data_len bytes from the
original buffer, it reads past the allocation into adjacent slab data,
leaking kernel heap contents to userspace via the configfs attribute
read interface.

Fix this by reallocating the data buffer with krealloc when the type
change requires a larger buffer. Zero-fill the newly allocated space to
prevent information leaks. The halving path (UNICODE to BINARY) is safe
as it reads fewer bytes than allocated.

Fixes: 7419485f197c ("usb: gadget: configfs: OS Extended Properties descriptors support")
Cc: stable@vger.kernel.org
Signed-off-by: Tanuj Sharma <ertanuj96@gmail.com>
---
 drivers/usb/gadget/configfs.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 51df6d1..1fcc010 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1311,9 +1311,24 @@ static ssize_t ext_prop_type_store(struct config_item *item,
 	    ext_prop->type == USB_EXT_PROP_BE32) &&
 	    (type == USB_EXT_PROP_UNICODE ||
 	    type == USB_EXT_PROP_UNICODE_ENV ||
-	    type == USB_EXT_PROP_UNICODE_LINK))
+	    type == USB_EXT_PROP_UNICODE_LINK)) {
+		if (ext_prop->data) {
+			char *new_data;
+			size_t new_len = ext_prop->data_len << 1;
+
+			new_data = krealloc(ext_prop->data, new_len,
+					    GFP_KERNEL);
+			if (!new_data) {
+				if (desc->opts_mutex)
+					mutex_unlock(desc->opts_mutex);
+				return -ENOMEM;
+			}
+			memset(new_data + ext_prop->data_len, 0,
+			       ext_prop->data_len);
+			ext_prop->data = new_data;
+		}
 		ext_prop->data_len <<= 1;
-	else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
+	} else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
 		   ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
 		   ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
 		   (type == USB_EXT_PROP_BINARY ||
-- 
2.43.0


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

* Re: [PATCH] usb: gadget: configfs: fix OOB read on ext_prop type toggle
  2026-11-30 21:24 [PATCH] usb: gadget: configfs: fix OOB read on ext_prop type toggle Tanuj Sharma
@ 2026-11-30 21:44 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 21:44 UTC (permalink / raw)
  To: Tanuj Sharma; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: gadget: configfs: OOB read and page overflow in
  ext_prop_type_store()
- [Medium] usb: gadget: configfs: zero-size krealloc() in
  ext_prop_type_store()
- [Medium] usb: gadget: configfs: race and UAF on data buffer in
  ext_prop_type_store()
--

commit 369cb44f3d37f1e467b4fc47f9afd4f859e6fcdf
Author: Tanuj Sharma <ertanuj96@gmail.com>

usb: gadget: configfs: fix OOB read on ext_prop type toggle

This commit reallocates and zero-fills ext_prop->data in
ext_prop_type_store() when toggling an extended property from a binary or
32-bit type to a Unicode type.

> diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
> index 568b2a0a6df81..5ba45da8e6b89 100644
> --- a/drivers/usb/gadget/configfs.c
> +++ b/drivers/usb/gadget/configfs.c
> @@ -1311,9 +1311,24 @@ static ssize_t ext_prop_type_store(struct config_item *item,
>  	    ext_prop->type == USB_EXT_PROP_BE32) &&
>  	    (type == USB_EXT_PROP_UNICODE ||
>  	    type == USB_EXT_PROP_UNICODE_ENV ||
> -	    type == USB_EXT_PROP_UNICODE_LINK))
> +	    type == USB_EXT_PROP_UNICODE_LINK)) {
> +		if (ext_prop->data) {
> +			char *new_data;
> +			size_t new_len = ext_prop->data_len << 1;
> +
> +			new_data = krealloc(ext_prop->data, new_len,
> +					    GFP_KERNEL);

[Severity: Medium]
What happens if ext_prop->data is non-NULL while ext_prop->data_len is 0?

Writing a single newline or NUL byte in ext_prop_data_store() while the
type is USB_EXT_PROP_BINARY decrements len to 0, allocates a 1-byte buffer
for ext_prop->data, and sets ext_prop->data_len to 0.  Toggling the type
to USB_EXT_PROP_UNICODE then calls krealloc(ext_prop->data, 0, GFP_KERNEL),
which frees ext_prop->data and returns ZERO_SIZE_PTR.  Because
ZERO_SIZE_PTR is non-NULL, ext_prop->data is set to ZERO_SIZE_PTR with
ext_prop->data_len still 0, and fill_ext_prop() ->
usb_ext_prop_put_unicode() later writes the 2-byte UTF-16LE terminator at
offset data_len - 2 (-2), corrupting dwPropertyDataLength in the
descriptor buffer.

> +			if (!new_data) {
> +				if (desc->opts_mutex)
> +					mutex_unlock(desc->opts_mutex);
> +				return -ENOMEM;
> +			}
> +			memset(new_data + ext_prop->data_len, 0,
> +			       ext_prop->data_len);
> +			ext_prop->data = new_data;
> +		}
>  		ext_prop->data_len <<= 1;

[Severity: Medium]
Can reallocating ext_prop->data here race with ext_prop_data_show()?

ext_prop_data_show() reads ext_prop->data_len, ext_prop->type, and
ext_prop->data without taking desc->opts_mutex:

CPU0                                  CPU1
ext_prop_type_store()                 ext_prop_data_show()
  mutex_lock(desc->opts_mutex)
  new_data = krealloc(ext_prop->data,
                      new_len, ...)
    /* frees old ext_prop->data */
                                        memcpy(page, ext_prop->data, len)
                                          <-- use-after-free
  ext_prop->data = new_data

Even if krealloc() does not move the buffer, updating ext_prop->data_len
before ext_prop->type lets a concurrent ext_prop_data_show() see the
doubled ext_prop->data_len with the old binary ext_prop->type, skipping
len >>= 1 and reading past the allocation.  In addition, both in-tree
callers of usb_os_desc_prepare_interf_dir() (ncm_alloc_inst() and
rndis_alloc_inst()) leave desc->opts_mutex as NULL, allowing two
concurrent ext_prop_type_store() calls to pass the same ext_prop->data
pointer to krealloc().

> -	else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
> +	} else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
>  		   ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
>  		   ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
>  		   (type == USB_EXT_PROP_BINARY ||

[Severity: High]
Does this still allow an out-of-bounds read and page overflow when
transitioning through USB_EXT_PROP_UNICODE_MULTI?

When ext_prop->type is already USB_EXT_PROP_UNICODE, ext_prop_data_store()
allocates only len + 1 bytes for ext_prop->data while setting
ext_prop->data_len to (len << 1) + 2:

drivers/usb/gadget/configfs.c:ext_prop_data_store() {
    ...
	new_data = kzalloc(len + 1, GFP_KERNEL);
	if (!new_data)
		return -ENOMEM;
	memcpy(new_data, page, 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)
		ext_prop->data_len = (len << 1) + 2;
    ...
}

If userspace writes len bytes while the type is USB_EXT_PROP_UNICODE and
then changes the type to USB_EXT_PROP_UNICODE_MULTI (or cycles 1 -> 7 ->
3), neither branch in ext_prop_type_store() runs.  ext_prop->data remains
len + 1 bytes while ext_prop->data_len remains (len << 1) + 2, so
ext_prop_data_show() does not halve len and copies (len << 1) + 2 bytes
from a (len + 1)-byte slab buffer.

If userspace then transitions 1 -> 7 -> 3 -> 1, krealloc() grows the
(len + 1)-byte buffer to (len << 2) + 4 bytes, but memset() only zeroes
the upper half starting at (len << 1) + 2, leaving bytes [len + 1 ..
(len << 1) + 1] uninitialized when ext_prop_data_show() reads them.
Repeating the cycle (or writing more than 2048 bytes before toggling 3 ->
1 -> 7) also grows ext_prop->data_len past PAGE_SIZE, causing
memcpy(page, ext_prop->data, len) in ext_prop_data_show() to overflow the
configfs page buffer.

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

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 21:24 [PATCH] usb: gadget: configfs: fix OOB read on ext_prop type toggle Tanuj Sharma
2026-11-30 21:44 ` sashiko-bot

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