2017-04-04 04:52:54

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH v2 1/2] usb: gadget: udc: avoid use of freed pointer

Rewrite udc_free_dma_chain() function to avoid use of pointer after free.

Addresses-Coverity-ID: 1091172
Acked-by: Michal Nazarewicz <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
Changes in v2:
Remove 'td->next = 0x00' inside for loop.
Remove unnecessary pointer nullification after free.
Rename variable addr_aux to addr_next.

drivers/usb/gadget/udc/amd5536udc.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/drivers/usb/gadget/udc/amd5536udc.c b/drivers/usb/gadget/udc/amd5536udc.c
index ea03ca7..821d088 100644
--- a/drivers/usb/gadget/udc/amd5536udc.c
+++ b/drivers/usb/gadget/udc/amd5536udc.c
@@ -611,21 +611,20 @@ udc_alloc_request(struct usb_ep *usbep, gfp_t gfp)
static int udc_free_dma_chain(struct udc *dev, struct udc_request *req)
{
int ret_val = 0;
- struct udc_data_dma *td;
- struct udc_data_dma *td_last = NULL;
+ struct udc_data_dma *td = req->td_data;
unsigned int i;

+ dma_addr_t addr_next = 0x00;
+ dma_addr_t addr = (dma_addr_t)td->next;
+
DBG(dev, "free chain req = %p\n", req);

/* do not free first desc., will be done by free for request */
- td_last = req->td_data;
- td = phys_to_virt(td_last->next);
-
for (i = 1; i < req->chain_len; i++) {
- pci_pool_free(dev->data_requests, td,
- (dma_addr_t)td_last->next);
- td_last = td;
- td = phys_to_virt(td_last->next);
+ td = phys_to_virt(addr);
+ addr_next = (dma_addr_t)td->next;
+ pci_pool_free(dev->data_requests, td, addr);
+ addr = addr_next;
}

return ret_val;
--
2.5.0


2017-04-04 04:34:51

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH v2 2/2] usb: gadget: udc: remove unnecessary variable and update function prototype

Remove unnecessary variable and update function prototype.

Acked-by: Michal Nazarewicz <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
Changes in v2:
None.

drivers/usb/gadget/udc/amd5536udc.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/udc/amd5536udc.c b/drivers/usb/gadget/udc/amd5536udc.c
index 821d088..67dd209 100644
--- a/drivers/usb/gadget/udc/amd5536udc.c
+++ b/drivers/usb/gadget/udc/amd5536udc.c
@@ -608,9 +608,8 @@ udc_alloc_request(struct usb_ep *usbep, gfp_t gfp)
}

/* frees pci pool descriptors of a DMA chain */
-static int udc_free_dma_chain(struct udc *dev, struct udc_request *req)
+static void udc_free_dma_chain(struct udc *dev, struct udc_request *req)
{
- int ret_val = 0;
struct udc_data_dma *td = req->td_data;
unsigned int i;

@@ -626,8 +625,6 @@ static int udc_free_dma_chain(struct udc *dev, struct udc_request *req)
pci_pool_free(dev->data_requests, td, addr);
addr = addr_next;
}
-
- return ret_val;
}

/* Frees request packet, called by gadget driver */
--
2.5.0

2017-04-05 12:33:42

by Felipe Balbi

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] usb: gadget: udc: avoid use of freed pointer


Hi,

"Gustavo A. R. Silva" <[email protected]> writes:
> Rewrite udc_free_dma_chain() function to avoid use of pointer after free.
>
> Addresses-Coverity-ID: 1091172
> Acked-by: Michal Nazarewicz <[email protected]>
> Reviewed-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Gustavo A. R. Silva <[email protected]>

doesn't apply to testing/next. Care to rebase?

--
balbi


Attachments:
signature.asc (832.00 B)