Skip to contentSkip to conversation

usb: gadget: pch_udc: fix use after free

PATCH In review Pan Bian 3 participants 3 messages Nov 6, 2019 USB

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH 1/1 usb: gadget: pch_udc: fix use after free

The next field of the DMA descriptor is written after releasing the descriptor, which may result in a use-after-free issue. Set the value of the field before it is released to fix the bug.

---
 drivers/usb/gadget/udc/pch_udc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/pch_udc.c +1 -1
index 265dab2bbfac..c5b8ec908aab 100644
@@ -1518,8 +1518,8 @@ static void pch_udc_free_dma_chain(struct pch_udc_dev *dev,
 		/* do not free first desc., will be done by free for request */
 		td = phys_to_virt(addr);
 		addr2 = (dma_addr_t)td->next;
-		dma_pool_free(dev->data_requests, td, addr);
 		td->next = 0x00;
+		dma_pool_free(dev->data_requests, td, addr);
 		addr = addr2;
 	}
 	req->chain_len = 1;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · Pan Bian

The next field of the DMA descriptor is written after releasing the descriptor, which may result in a use-after-free issue. Set the value of the field before it is released to fix the bug.

Had you chance to read the discussion [1]?
I Cc to Gustavo to hear from him about destiny of the change.

[1]: http://lkml.iu.edu/hypermail/linux/kernel/1702.1/00843.html

Quoted 22 lines
---
 drivers/usb/gadget/udc/pch_udc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/pch_udc.c +1 -2
index 265dab2bbfac..c5b8ec908aab 100644
@@ -1518,8 +1518,8 @@ static void pch_udc_free_dma_chain(struct pch_udc_dev *dev,
                /* do not free first desc., will be done by free for request */
                td = phys_to_virt(addr);
                addr2 = (dma_addr_t)td->next;
-               dma_pool_free(dev->data_requests, td, addr);
                td->next = 0x00;
+               dma_pool_free(dev->data_requests, td, addr);
                addr = addr2;
        }
        req->chain_len = 1;
--

2.7.4

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Pan Bian

The next field of the DMA descriptor is written after releasing the descriptor, which may result in a use-after-free issue. Set the value of the field before it is released to fix the bug.

Had you chance to read the discussion [1]?
I Cc to Gustavo to hear from him about destiny of the change.

Wow, thanks for reminding me about this thread, Andy. I had totally forgotten about this for more than two years... :|

Here is the final version:

https://lore.kernel.org/lkml/20191106202821.GA20347@embeddedor/

Thanks
--
Gustavo