From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Hui Peng <benquike@gmail.com>
Cc: Andrzej Pietrasiewicz <andrzej.p@collabora.com>,
Felipe Balbi <balbi@kernel.org>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH v4] usb: gadget: f_printer: bound PNP string copy length in printer_func_setup()
Date: Fri, 20 Nov 2026 11:47:29 +0100 [thread overview]
Message-ID: <2026112009-smudge-chaste-0ef0@gregkh> (raw)
In-Reply-To: <20260930075920.483189-1-benquike@gmail.com>
On Wed, Sep 30, 2026 at 07:59:20AM +0000, Hui Peng wrote:
> In printer_func_setup(), when processing a GET_DEVICE_ID class setup
> request, the IEEE-1284 PNP string length value = strlen(*dev->pnp_string)
> is copied into buf without checking wLength (the host's requested buffer
> size):
>
> value = strlen(*dev->pnp_string);
> memcpy(buf + 2, *dev->pnp_string, value);
>
> When the host issues a GET_DEVICE_ID request with wLength smaller than
> value + 2, memcpy() writes past the end of the transfer buffer, triggering
> a slab out-of-bounds heap buffer overflow in printer_func_setup():
>
> BUG: KASAN: slab-out-of-bounds in printer_func_setup+0x280/0x450
> Write of size 3074 at addr ffff8880024f8000 by task init/1
> Call Trace:
> <TASK>
> dump_stack_lvl+0x70/0xa0
> print_report+0x153/0x4c6
> kasan_report+0xf1/0x120
> __asan_memcpy+0x3c/0x60
> printer_func_setup+0x280/0x450
>
> Bound the PNP string copy length using min_t(u16, wLength, strlen() + 2) in
> printer_func_setup() so memcpy() never exceeds wLength.
>
> Tested in QEMU against Linux 7.3.0-rc3 with KASAN enabled by binding
> f_printer with a 3074-byte PNP string and issuing a GET_DEVICE_ID setup
> request with wLength = 64: on the unfixed kernel this triggers a KASAN
> slab-out-of-bounds write, whereas on the fixed kernel value is bounded to
> wLength and 0 KASAN faults occur.
>
> Fixes: b185f01a9ab7 ("usb: gadget: printer: factor out f_printer")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Hui Peng <benquike@gmail.com>
> ---
> Changes in v4:
> - Re-target fix strictly to bound memcpy() size against wLength in
> GET_DEVICE_ID handling in printer_func_setup().
>
> drivers/usb/gadget/function/f_printer.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
> index a27792966287..f8726e3d849f 100644
> --- a/drivers/usb/gadget/function/f_printer.c
> +++ b/drivers/usb/gadget/function/f_printer.c
> @@ -1033,9 +1033,13 @@ static int printer_func_setup(struct usb_function *f,
> if (!*dev->pnp_string) {
> value = 0;
> break;
> }
> - value = strlen(*dev->pnp_string);
> - buf[0] = (value >> 8) & 0xFF;
> - buf[1] = value & 0xFF;
> - memcpy(buf + 2, *dev->pnp_string, value);
> + value = min_t(u16, wLength, strlen(*dev->pnp_string) + 2);
min_t() is not needed, correct? Just use min().
thanks,
greg k-h
prev parent reply other threads:[~2026-11-20 10:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-30 7:59 [PATCH v4] usb: gadget: f_printer: bound PNP string copy length in printer_func_setup() Hui Peng
2026-11-20 10:47 ` Greg Kroah-Hartman [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=2026112009-smudge-chaste-0ef0@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=andrzej.p@collabora.com \
--cc=balbi@kernel.org \
--cc=benquike@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--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