Skip to content

Commit c1c8550

Browse files
GoodLuck612gregkh
authored andcommitted
usb: xhci: Fix memory leak in xhci_disable_slot()
xhci_alloc_command() allocates a command structure and, when the second argument is true, also allocates a completion structure. Currently, the error handling path in xhci_disable_slot() only frees the command structure using kfree(), causing the completion structure to leak. Use xhci_free_command() instead of kfree(). xhci_free_command() correctly frees both the command structure and the associated completion structure. Since the command structure is allocated with zero-initialization, command->in_ctx is NULL and will not be erroneously freed by xhci_free_command(). This bug was found using an experimental static analysis tool we are developing. The tool is based on the LLVM framework and is specifically designed to detect memory management issues. It is currently under active development and not yet publicly available, but we plan to open-source it after our research is published. The bug was originally detected on v6.13-rc1 using our static analysis tool, and we have verified that the issue persists in the latest mainline kernel. We performed build testing on x86_64 with allyesconfig using GCC=11.4.0. Since triggering these error paths in xhci_disable_slot() requires specific hardware conditions or abnormal state, we were unable to construct a test case to reliably trigger these specific error paths at runtime. Fixes: 7faac19 ("xhci: avoid race between disable slot command and host runtime suspend") CC: stable@vger.kernel.org Signed-off-by: Zilin Guan <zilin@seu.edu.cn> Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com> Link: https://patch.msgid.link/20260304223639.3882398-2-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8df672b commit c1c8550

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

drivers/usb/host/xhci.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,15 +4146,15 @@ int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
41464146
if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
41474147
(xhci->xhc_state & XHCI_STATE_HALTED)) {
41484148
spin_unlock_irqrestore(&xhci->lock, flags);
4149-
kfree(command);
4149+
xhci_free_command(xhci, command);
41504150
return -ENODEV;
41514151
}
41524152

41534153
ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
41544154
slot_id);
41554155
if (ret) {
41564156
spin_unlock_irqrestore(&xhci->lock, flags);
4157-
kfree(command);
4157+
xhci_free_command(xhci, command);
41584158
return ret;
41594159
}
41604160
xhci_ring_cmd_db(xhci);

0 commit comments

Comments
 (0)