dmaengine: qcom: hidma: fix wild pointer stored in mchan->running when active list is empty
Subsystems (3)
PATCH 1/1 dmaengine: qcom: hidma: fix wild pointer stored in mchan->running when active list is empty
From: Wang Haoran <[email protected]>
While auditing list_first_entry() users we found two call sites in the HIDMA driver that invoke it on mchan->active without verifying the list is non-empty. list_first_entry() never returns NULL: on an empty list head->next == head, so container_of() produces a bogus non-NULL pointer into the enclosing hidma_chan structure instead. That wild pointer is stored into mchan->running, and is later dereferenced in hidma_tx_status() where "if (mchan->running) runcookie = mchan->running->desc.cookie" passes precisely because the pointer is not NULL, yielding an out-of-bounds read / memory corruption.
The list can be empty at both sites on entirely normal paths. In hidma_callback(), the completed descriptor is first moved from active to completed; when it was the last active descriptor, the subsequent list_first_entry() runs on the now-empty list. In hidma_issue_pending(), a client calling dma_async_issue_pending() without any queued descriptor leaves both queued and active empty, and the "if (!mchan->running)" branch then fetches the "first" entry of an empty active list.
Fix both sites by switching to list_first_entry_or_null(), which returns NULL for an empty list. This makes the existing NULL check on mchan->running in hidma_tx_status() effective, so the paused-channel cookie query takes the -EINVAL path instead of dereferencing a wild pointer. Both call sites already run under mchan->lock, so no additional locking is needed.
--- drivers/dma/qcom/hidma.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/dma/qcom/hidma.c +7 -5
@@ -176,8 +176,9 @@ static void hidma_callback(void *data) queued = true;/* calculate the next running descriptor */ - mchan->running =list_first_entry (&mchan->active, - struct hidma_desc, node); + mchan->running =list_first_entry_or_null (&mchan->active, + struct hidma_desc, + node); }spin_unlock_irqrestore (&mchan->lock, irqflags);
@@ -238,9 +239,10 @@ static void hidma_issue_pending(struct dma_chan *dmach) }if (!mchan->running) { -struct hidma_desc *desc =list_first_entry (&mchan->active, - struct hidma_desc, - node); +struct hidma_desc *desc; + + desc =list_first_entry_or_null (&mchan->active, + struct hidma_desc, node); mchan->running = desc; }spin_unlock_irqrestore (&mchan->lock, flags);
Signature · 1 line
2.30.2
Quoted 21 lines · Li Ximo
From: Wang Haoran <[email protected]>
While auditing
list_first_entry()users we found two call sites in the HIDMA driver that invoke it onmchan->activewithout verifying the list is non-empty.list_first_entry()never returns NULL: on an empty listhead->next== head, socontainer_of()produces a bogus non-NULL pointer into the enclosinghidma_chanstructure instead. That wild pointer is stored intomchan->running, and is later dereferenced inhidma_tx_status()where "if (mchan->running) runcookie =mchan->running->desc.cookie" passes precisely because the pointer is not NULL, yielding an out-of-bounds read / memory corruption.The list can be empty at both sites on entirely normal paths. In
hidma_callback(), the completed descriptor is first moved from active to completed; when it was the last active descriptor, the subsequentlist_first_entry()runs on the now-empty list. Inhidma_issue_pending(), a client callingdma_async_issue_pending()without any queued descriptor leaves both queued and active empty, and the "if (!mchan->running)" branch then fetches the "first" entry of an empty active list.
Quoted 7 lines
Fix both sites by switching to
list_first_entry_or_null(), which returns NULL for an empty list. This makes the existing NULL check onmchan->runninginhidma_tx_status()effective, so the paused-channel cookie query takes the-EINVALpath instead of dereferencing a wild pointer. Both call sites already run undermchan->lock, so no additional locking is needed.
Can you use simple words.
dmaengine: qcom: hidma: fix memory corruption when active list is empty
list_first_entry() does not check if the list is empty. When mchan->active is empty it returns a bogus pointer, which causing memory corruption.
Use list_first_entry_or_null() instead.
Frank
Quoted 39 lines
Reported-by Wang Haoran <[email protected]>--- drivers/dma/qcom/hidma.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)drivers/dma/qcom/hidma.c +7
-6@@ -176,8 +176,9 @@ static void hidma_callback(void *data) queued = true;/* calculate the next running descriptor */ - mchan->running =list_first_entry (&mchan->active, - struct hidma_desc, node); + mchan->running =list_first_entry_or_null (&mchan->active, + struct hidma_desc, + node); }spin_unlock_irqrestore (&mchan->lock, irqflags);@@ -238,9 +239,10 @@ static void hidma_issue_pending(struct dma_chan *dmach) }if (!mchan->running) { -struct hidma_desc *desc =list_first_entry (&mchan->active, - struct hidma_desc, - node); +struct hidma_desc *desc; + + desc =list_first_entry_or_null (&mchan->active, + struct hidma_desc, node); mchan->running = desc; }spin_unlock_irqrestore (&mchan->lock, flags); --2.30.2