mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: contact@arnaud-lcm.com
Cc: manas18244@iiitd.ac.in,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Pete Zaitcev <zaitcev@redhat.com>,
	Paolo Abeni <paolo.abeni@email.it>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@suse.de>,
	syzbot+86b6d7c8bcc66747c505@syzkaller.appspotmail.com
Subject: Re: [PATCH] usbmon: Fix out-of-bounds read in mon_copy_to_buff
Date: Wed, 2 Jul 2025 22:29:49 -0400	[thread overview]
Message-ID: <22cdd2ea-9240-45ed-8667-cc315eccf0e0@rowland.harvard.edu> (raw)
In-Reply-To: <32d213c565df4c2a4bdb15b8c5b7c1ba@arnaud-lcm.com>

On Wed, Jul 02, 2025 at 10:42:42PM +0100, contact@arnaud-lcm.com wrote:
> Hi Manas, I just noticed your patch while I was working on it and we had the
> same idea. I think you are not covering every cases of the issue.
> I've done it this way:

Why do both of you guys think that mon_copy_to_buff() is trying to 
transfer data from an empty source buffer?  Maybe the destination buffer 
is the cause of the problem instead.

> From 49f4d231a1c4407d52fee83e956b1d40b3221e37 Mon Sep 17 00:00:00 2001
> From: Arnaud Lecomte <contact@arnaud-lcm.com>
> Date: Wed, 2 Jul 2025 22:39:08 +0100
> Subject: [PATCH] usb: mon: Add read length checking in mon_copy_to_buff
> 
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> ---
>  drivers/usb/mon/mon_bin.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
> index c93b43f5bc46..e7b390439743 100644
> --- a/drivers/usb/mon/mon_bin.c
> +++ b/drivers/usb/mon/mon_bin.c
> @@ -249,6 +249,8 @@ static unsigned int mon_copy_to_buff(const struct
> mon_reader_bin *this,
>  		 * Copy data and advance pointers.
>  		 */
>  		buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
> +		if(strlen(from) < step_len)

strlen() is absolutely the wrong thing to use here for both of your 
patches.  "from" is not a NUL-terminated string but rather a general 
memory buffer.  The caller is supposed to guarantee that "from" contains 
at least "length" bytes of data.  If something is going wrong here, it 
is the caller's fault.

Alan Stern

> +			return -ENOMEM;
>  		memcpy(buf, from, step_len);
>  		if ((off += step_len) >= this->b_size) off = 0;
>  		from += step_len;
> -- 
> 2.43.0
> 
> 
> On 2025-07-02 22:27, Manas Gupta via B4 Relay wrote:
> > From: Manas Gupta <manas18244@iiitd.ac.in>
> > 
> > memcpy tries to copy buffer 'from' when it is empty. This leads to
> > out-of-bounds crash.
> > 
> > This checks if the buffer is already empty and throws an appropriate
> > error message before bailing out.
> > 
> > Fixes: 6f23ee1fefdc1 ("USB: add binary API to usbmon")
> > Reported-by: syzbot+86b6d7c8bcc66747c505@syzkaller.appspotmail.com
> > Closes: https://syzkaller.appspot.com/bug?extid=86b6d7c8bcc66747c505
> > Signed-off-by: Manas Gupta <manas18244@iiitd.ac.in>
> > ---
> > I have used printk(KERN_ERR ... to keep things consistent in usbmon.
> > dev_err and pr_err are not used anywhere else in usbmon.
> > ---
> >  drivers/usb/mon/mon_bin.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> > 
> > diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
> > index c93b43f5bc4614ad75568b601c47a1ae51f01fa5..bd945052f6fb821ba814fab96eba5a82e5d161e2
> > 100644
> > --- a/drivers/usb/mon/mon_bin.c
> > +++ b/drivers/usb/mon/mon_bin.c
> > @@ -249,6 +249,11 @@ static unsigned int mon_copy_to_buff(const struct
> > mon_reader_bin *this,
> >  		 * Copy data and advance pointers.
> >  		 */
> >  		buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
> > +		if (!strlen(from)) {
> > +			printk(KERN_ERR TAG
> > +			       ": src buffer is empty, cannot copy from it\n");
> > +			return -ENOMEM;
> > +		}
> >  		memcpy(buf, from, step_len);
> >  		if ((off += step_len) >= this->b_size) off = 0;
> >  		from += step_len;
> > 
> > ---
> > base-commit: d0b3b7b22dfa1f4b515fd3a295b3fd958f9e81af
> > change-id: 20250703-fix-oob-mon_copy_to_buff-7cfe26e819b9
> > 
> > Best regards,
> 

  reply	other threads:[~2025-07-03  2:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-02 21:27 Manas Gupta via B4 Relay
2025-07-02 21:42 ` contact
2025-07-03  2:29   ` Alan Stern [this message]
2025-07-03  5:12 ` Greg Kroah-Hartman
2025-07-03  5:40   ` Manas
2025-07-03 14:51     ` Alan Stern

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=22cdd2ea-9240-45ed-8667-cc315eccf0e0@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=contact@arnaud-lcm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gregkh@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=manas18244@iiitd.ac.in \
    --cc=paolo.abeni@email.it \
    --cc=syzbot+86b6d7c8bcc66747c505@syzkaller.appspotmail.com \
    --cc=zaitcev@redhat.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

all inboxes | Powered by JetHome®