mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sergey Shtylyov <s.shtylyov@omp.ru>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	<linux-usb@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <lvc-project@linuxtesting.org>,
	Fedor Pchelkin <pchelkin@ispras.ru>
Subject: [PATCH] usb: gadget: composite: fix possible kernel oops in composite_setup()
Date: Tue, 29 Apr 2025 23:32:36 +0300	[thread overview]
Message-ID: <0e61b364-8f26-4f98-9f10-9b9800b1cd41@omp.ru> (raw)

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;

             reply	other threads:[~2025-04-29 20:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-29 20:32 Sergey Shtylyov [this message]
2025-06-16 20:30 ` Sergey Shtylyov

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=0e61b364-8f26-4f98-9f10-9b9800b1cd41@omp.ru \
    --to=s.shtylyov@omp.ru \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=pchelkin@ispras.ru \
    /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®