Re: [PATCH] usb: xhci: fix a possible null-pointer dereference in xhci_setup_device()
From: Michał Pecio
Date: Wed Oct 02 2024 - 06:04:57 EST
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