public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: "Michał Pecio" <michal.pecio@gmail.com>
To: islituo@gmail.com
Cc: baijiaju1990@gmail.com, gregkh@linuxfoundation.org,
	linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org,
	mathias.nyman@intel.com
Subject: Re: [PATCH] usb: xhci: fix a possible null-pointer dereference in xhci_setup_device()
Date: Wed, 2 Oct 2024 12:04:39 +0200	[thread overview]
Message-ID: <20241002120439.077afc90@foxbook> (raw)
In-Reply-To: <20241001194526.25757-1-islituo@gmail.com>

Hi,

> There is a possible null-pointer dereference related to the
> wait-completion synchronization mechanism in the function
> xhci_setup_device().
> 
> Consider the following execution scenario:
> 
> in drivers/usb/host/xhci-mem.c:
>   xhci_mem_init()       // 2381
>     if (!xhci->dcbaa)   // 2431  xhci->dcbaa can be NULL
>     xhci_mem_cleanup()  // 2548
>       xhci_cleanup_command_queue()        // 1888
> in drivers/usb/host/xhci-ring.c
>         xhci_complete_del_and_free_cmd()  // 1619
>           complete(cmd->completion);      // 1608
> 
> The variable xhci->dcbaa is checked by an if statement at Line 2431.
> If xhci->dcbaa is NULL, xhci_mem_cleanup() will be called at Line
> 2548, which eventually leads to complete() at Line 1608 that wakes up
> the wait_for_completion().
> 
> Consider the wait_for_completion() in drivers/usb/host/xhci.c
>   xhci_setup_device()
>     wait_for_completion(command->completion);       // 4179
>     le64_to_cpu(xhci->dcbaa->dev_context_ptrs...)); // 4237
> 
> The variable xhci->dcbaa is dereferenced (without being rechecked)
> after the wait_for_completion(), which can introduce a possible
> null-pointer dereference.

I think it's a false positive, because xhci_mem_init() is only called
on driver initialization and on resume from suspend, immediately after
an explicit xhci_mem_cleanup(), which would have woken up any waiting
tasks (and likely made them crash), but there shouldn't be any.

By the way, is your analyzer not finding the issue that any call to
xhci_mem_cleanup() wakes up everybody waiting on the command queue and
then sets a bunch of things (including xhci->dcbaa) to NULL shortly
thereafter? This race looks like it shouldn't be harder to detect than
the things you are doing already.

Of course, all of that would bring more false positives too. Basically,
you discovered that calling a cleanup function while something else is
still pending, or having some work already pending while initialization
isn't yet complete, may not end well.

> To address this issue, a NULL check is added to ensure the variable
> xhci->dcbaa is not NULL when xhci_dbg_trace() is called.

That's still just bandaid and not a real fix. With static analysis one
must always review the output and ask if the problem is real, what it
really means for the code and what to do about it. Simply ignoring the
missing pointer is rarely the right solution.

Regards,
Michal

  reply	other threads:[~2024-10-02 10:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-01 19:45 [PATCH] usb: xhci: fix a possible null-pointer dereference in xhci_setup_device() Tuo Li
2024-10-02 10:04 ` Michał Pecio [this message]
2024-10-02 12:14   ` Michał Pecio

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=20241002120439.077afc90@foxbook \
    --to=michal.pecio@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=islituo@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@intel.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