linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: composite: fix possible kernel oops in composite_setup()
@ 2025-04-29 20:32 Sergey Shtylyov
  2025-06-16 20:30 ` Sergey Shtylyov
  0 siblings, 1 reply; 2+ messages in thread
From: Sergey Shtylyov @ 2025-04-29 20:32 UTC (permalink / raw)
  To: Greg Kroah-Hartman, linux-usb; +Cc: linux-kernel, lvc-project, Fedor Pchelkin

list_first_entry() should never return NULL -- which makes Svace complain
about the next *if* statement's condition being always false. What's worse,
list_first_entry() won't work correctly when the list is empty -- in that
case, passing config->descriptors[0] to memcpy() further below will cause
dereferencing of a garbage pointer read from cdev->qw_sign[] and so (most
probably) a kernel oops. Use list_first_entry_or_null() that returns NULL
if the list is empty; fix the strange indentation below, while at it...

TTBOMK, this situation shouldn't happen with the current gadget drivers --
however there's no guarantee that usb_add_config[_only]() is called by any
gadget driver; and anyway, Svace's complaints would be silenced...

Found by Linux Verification Center (linuxtesting.org) with the Svace static
analysis tool.

Fixes: 53e6242db8d6 ("usb: gadget: composite: add USB_DT_OTG request handling")
Suggested-by: Fedor Pchelkin <pchelkin@ispras.ru>
Signed-off-by: Sergey Shtylyov <s.shtylyov@omp.ru>

---
This patch is against the usb-linus branch of Greg KH's usb.git repo.

 drivers/usb/gadget/composite.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Index: usb/drivers/usb/gadget/composite.c
===================================================================
--- usb.orig/drivers/usb/gadget/composite.c
+++ usb/drivers/usb/gadget/composite.c
@@ -1887,8 +1887,8 @@ composite_setup(struct usb_gadget *gadge
 				if (cdev->config)
 					config = cdev->config;
 				else
-					config = list_first_entry(
-							&cdev->configs,
+					config = list_first_entry_or_null(
+						&cdev->configs,
 						struct usb_configuration, list);
 				if (!config)
 					goto done;

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

* Re: [PATCH] usb: gadget: composite: fix possible kernel oops in composite_setup()
  2025-04-29 20:32 [PATCH] usb: gadget: composite: fix possible kernel oops in composite_setup() Sergey Shtylyov
@ 2025-06-16 20:30 ` Sergey Shtylyov
  0 siblings, 0 replies; 2+ messages in thread
From: Sergey Shtylyov @ 2025-06-16 20:30 UTC (permalink / raw)
  To: Greg Kroah-Hartman, linux-usb; +Cc: linux-kernel, lvc-project, Fedor Pchelkin

On 4/29/25 11:32 PM, Sergey Shtylyov wrote:

   Hm, 1.5 months passed and no reaction whatsoever... :-/

> list_first_entry() should never return NULL -- which makes Svace complain
> about the next *if* statement's condition being always false. What's worse,
> list_first_entry() won't work correctly when the list is empty -- in that
> case, passing config->descriptors[0] to memcpy() further below will cause
> dereferencing of a garbage pointer read from cdev->qw_sign[] and so (most
> probably) a kernel oops. Use list_first_entry_or_null() that returns NULL
> if the list is empty; fix the strange indentation below, while at it...
> 
> TTBOMK, this situation shouldn't happen with the current gadget drivers --
> however there's no guarantee that usb_add_config[_only]() is called by any
> gadget driver; and anyway, Svace's complaints would be silenced...
> 
> Found by Linux Verification Center (linuxtesting.org) with the Svace static
> analysis tool.
> 
> Fixes: 53e6242db8d6 ("usb: gadget: composite: add USB_DT_OTG request handling")
> Suggested-by: Fedor Pchelkin <pchelkin@ispras.ru>
> Signed-off-by: Sergey Shtylyov <s.shtylyov@omp.ru>
> 
> ---
> This patch is against the usb-linus branch of Greg KH's usb.git repo.
> 
>  drivers/usb/gadget/composite.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> Index: usb/drivers/usb/gadget/composite.c
> ===================================================================
> --- usb.orig/drivers/usb/gadget/composite.c
> +++ usb/drivers/usb/gadget/composite.c
> @@ -1887,8 +1887,8 @@ composite_setup(struct usb_gadget *gadge
>  				if (cdev->config)
>  					config = cdev->config;
>  				else
> -					config = list_first_entry(
> -							&cdev->configs,
> +					config = list_first_entry_or_null(

   So we either need to do this...

> +						&cdev->configs,
>  						struct usb_configuration, list);
>  				if (!config)

   ... or this check needs to be removed.

>  					goto done;

   OK, I've tried (unsuccessfully!) reaching Greg on IRC, both privately and
on the OFTC's IRC channel #kernelnewbies -- and now I can't even connect to
irc.oftc.net:6697 for some reason. Thought I should try following up on the
linux-usb ML, just in case...
   So Greg, I'd still like to know: do you ignore all patches from .ru, all
patches from omp.ru or just all patches from me? :-)

MBR, Sergey

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

end of thread, other threads:[~2025-06-16 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-29 20:32 [PATCH] usb: gadget: composite: fix possible kernel oops in composite_setup() Sergey Shtylyov
2025-06-16 20:30 ` Sergey Shtylyov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).