All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Felipe Balbi <balbi@kernel.org>,
	Jakob Koschel <jakobkoschel@gmail.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Ira Weiny <ira.weiny@intel.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH v2] USB: gadget: dummy_hcd: switch char * to u8 *
Date: Tue, 22 Nov 2022 17:12:14 +0100	[thread overview]
Message-ID: <Y3z03sS65YKgvI5R@kroah.com> (raw)
In-Reply-To: <Y2vBWJOgEBDn0NzU@rowland.harvard.edu>

On Wed, Nov 09, 2022 at 10:03:52AM -0500, Alan Stern wrote:
> On Wed, Nov 09, 2022 at 09:44:50AM +0100, Greg Kroah-Hartman wrote:
> > The function handle_control_request() casts the urb buffer to a char *,
> > and then treats it like a unsigned char buffer when assigning data to
> > it.  On some architectures, "char" is really signed, so let's just
> > properly set this pointer to a u8 to take away any potential problems as
> > that's what is really wanted here.
> > 
> > Use put_unaligned_le16() to copy the full 16bits into the buffer,
> > it's not a problem just yet as only 7 bits are being used here, but this
> > protects us when/if the USB spec changes in the future to define more of
> > these bits.
> > 
> > Cc: Felipe Balbi <balbi@kernel.org>
> > Cc: Jakob Koschel <jakobkoschel@gmail.com>
> > Cc: Randy Dunlap <rdunlap@infradead.org>
> > Cc: Ira Weiny <ira.weiny@intel.com>
> > Cc: Alan Stern <stern@rowland.harvard.edu>
> > Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > ---
> > v2: - use put_unaligned_le16() on Linus's recommendation as a simpler and
> >       more obvious way to describe the data being copied here.
> >     - update device: comment based on Alan's review
> > 
> >  drivers/usb/gadget/udc/dummy_hcd.c | 13 ++++++-------
> >  1 file changed, 6 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c
> > index 899ac9f9c279..7c59c20c8623 100644
> > --- a/drivers/usb/gadget/udc/dummy_hcd.c
> > +++ b/drivers/usb/gadget/udc/dummy_hcd.c
> > @@ -1740,13 +1740,13 @@ static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
> >  		if (setup->bRequestType == Dev_InRequest
> >  				|| setup->bRequestType == Intf_InRequest
> >  				|| setup->bRequestType == Ep_InRequest) {
> > -			char *buf;
> > +			u8 *buf;
> >  			/*
> > -			 * device: remote wakeup, selfpowered
> > +			 * device: remote wakeup, selfpowered, LTM, U1, or U2
> >  			 * interface: nothing
> >  			 * endpoint: halt
> >  			 */
> > -			buf = (char *)urb->transfer_buffer;
> > +			buf = urb->transfer_buffer;
> >  			if (urb->transfer_buffer_length > 0) {
> >  				if (setup->bRequestType == Ep_InRequest) {
> >  					ep2 = find_endpoint(dum, w_index);
> > @@ -1755,10 +1755,9 @@ static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
> >  						break;
> >  					}
> >  					buf[0] = ep2->halted;
> > -				} else if (setup->bRequestType ==
> > -					   Dev_InRequest) {
> > -					buf[0] = (u8)dum->devstatus;
> > -				} else
> > +				} else if (setup->bRequestType == Dev_InRequest)
> > +					put_unaligned_le16(dum->devstatus, buf);
> 
> This is tempting but wrong.  At this point we haven't checked how big 
> the transfer_buffer is; if it is only one byte long then this call will 
> overflow the buffer.  Notice the "if" statement three lines below.

Ick, good catch.  I'll redo this again...

thanks,

greg k-h

      reply	other threads:[~2022-11-22 16:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-09  8:44 [PATCH v2] USB: gadget: dummy_hcd: switch char * to u8 * Greg Kroah-Hartman
2022-11-09 15:03 ` Alan Stern
2022-11-22 16:12   ` 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=Y3z03sS65YKgvI5R@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=balbi@kernel.org \
    --cc=ira.weiny@intel.com \
    --cc=jakobkoschel@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=stern@rowland.harvard.edu \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.