Re: [PATCH] usb: xhci: Check kcalloc_node() when allocating interrupter array in xhci_mem_init()
From: Michal Pecio
Date: Mon Nov 03 2025 - 03:40:56 EST
On Thu, 18 Sep 2025 21:08:38 +0800, Guangshuo Li wrote:
> kcalloc_node() may fail. When the interrupter array allocation returns
> NULL, subsequent code uses xhci->interrupters (e.g. in xhci_add_interrupter()
> and in cleanup paths), leading to a potential NULL pointer dereference.
>
> Check the allocation and bail out to the existing fail path to avoid
> the NULL dereference.
>
> Fixes: c99b38c412343 ("xhci: add support to allocate several interrupters")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
> ---
> drivers/usb/host/xhci-mem.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
> index d698095fc88d..da257856e864 100644
> --- a/drivers/usb/host/xhci-mem.c
> +++ b/drivers/usb/host/xhci-mem.c
> @@ -2505,7 +2505,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
> "Allocating primary event ring");
> xhci->interrupters = kcalloc_node(xhci->max_interrupters, sizeof(*xhci->interrupters),
> flags, dev_to_node(dev));
> -
> + if (!xhci->interrupters)
> + goto fail;
> ir = xhci_alloc_interrupter(xhci, 0, flags);
> if (!ir)
> goto fail;
> --
> 2.43.0
Hi Greg and Mathias,
I noticed that this bug still exists in current 6.6 and 6.12 releases,
what would be the sensible course of action to fix it?
The patch from Guangshuo Li is a specific fix and it applies cleanly on
those branches. By simulating allocation failure, I confirmed the bug
and the fix on 6.6.113, which is identical to the current 6.6.116.
Mainline added an identical check in 83d98dea48eb ("usb: xhci: add
individual allocation checks in xhci_mem_init()") which is a cleanup
that has a merge conflict at least with 6.6.
The conflict seems superficial and probably the cleanup would have no
side effects on 6.6/6.12, but I haven't really reviewed this and maybe
it would be simpler to just take the targeted fix?
Regards,
Michal