Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tanuj Sharma" <ertanuj96@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: configfs: fix OOB read on ext_prop type toggle
Date: Mon, 30 Nov 2026 21:44:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-242037@kernel.org> (raw)
In-Reply-To: <20261130212459.85640-1-ertanuj96@gmail.com>

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

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-242037@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ertanuj96@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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