2019-10-21 14:26:21

by Suwan Kim

[permalink] [raw]
Subject: [PATCH] usbip: Fix free of unallocated mem ory in vhci tx

iso_buffer should be set to NULL after use and free in the while loop.
In the case of isochronous URB in the while loop, iso_buffer is
allocated and after sending it to server, buffer is deallocated. And
then, if the next URB in the while loop is not a isochronous pipe,
iso_buffer still holds the previously deallocated buffer address and
kfree tries to free wrong buffer address.

Fixes: ea44d190764b (“usbip: Implement SG support to vhci-hcd and stub driver”)
Reported-by: kbuild test robot <[email protected]>
Reported-by: Julia Lawall <[email protected]>
Signed-off-by: Suwan Kim <[email protected]>
---
drivers/usb/usbip/vhci_tx.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/usb/usbip/vhci_tx.c b/drivers/usb/usbip/vhci_tx.c
index c3803785f6ef..b290e810d11b 100644
--- a/drivers/usb/usbip/vhci_tx.c
+++ b/drivers/usb/usbip/vhci_tx.c
@@ -73,6 +73,7 @@ static int vhci_send_cmd_submit(struct vhci_device *vdev)
memset(&pdu_header, 0, sizeof(pdu_header));
memset(&msg, 0, sizeof(msg));
memset(&iov, 0, sizeof(iov));
+ iso_buffer = NULL;

usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
priv->seqnum);
--
2.21.0


2019-10-21 15:09:33

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] usbip: Fix free of unallocated memory in vhci tx



On Mon, 21 Oct 2019, Suwan Kim wrote:

> iso_buffer should be set to NULL after use and free in the while loop.
> In the case of isochronous URB in the while loop, iso_buffer is
> allocated and after sending it to server, buffer is deallocated. And
> then, if the next URB in the while loop is not a isochronous pipe,
> iso_buffer still holds the previously deallocated buffer address and
> kfree tries to free wrong buffer address.
>
> Fixes: ea44d190764b (“usbip: Implement SG support to vhci-hcd and stub driver”)
> Reported-by: kbuild test robot <[email protected]>
> Reported-by: Julia Lawall <[email protected]>
> Signed-off-by: Suwan Kim <[email protected]>
> ---
> drivers/usb/usbip/vhci_tx.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/usb/usbip/vhci_tx.c b/drivers/usb/usbip/vhci_tx.c
> index c3803785f6ef..b290e810d11b 100644
> --- a/drivers/usb/usbip/vhci_tx.c
> +++ b/drivers/usb/usbip/vhci_tx.c
> @@ -73,6 +73,7 @@ static int vhci_send_cmd_submit(struct vhci_device *vdev)
> memset(&pdu_header, 0, sizeof(pdu_header));
> memset(&msg, 0, sizeof(msg));
> memset(&iov, 0, sizeof(iov));
> + iso_buffer = NULL;

Somehow I would have put it after the kfree, since the kfree makes the
value invalid. iso_buffer is already initialized to NULL for the first
iteration. If you want to put the setting to NULL at the top of the loop,
maybe the = NULL in the first line should be removed.

julia

>
> usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
> priv->seqnum);
> --
> 2.21.0
>
>

2019-10-21 19:29:41

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH] usbip: Fix free of unallocated m emory in vhci tx

On 10/21/19 9:08 AM, Julia Lawall wrote:
>
>
> On Mon, 21 Oct 2019, Suwan Kim wrote:
>
>> iso_buffer should be set to NULL after use and free in the while loop.
>> In the case of isochronous URB in the while loop, iso_buffer is
>> allocated and after sending it to server, buffer is deallocated. And
>> then, if the next URB in the while loop is not a isochronous pipe,
>> iso_buffer still holds the previously deallocated buffer address and
>> kfree tries to free wrong buffer address.
>>
>> Fixes: ea44d190764b (“usbip: Implement SG support to vhci-hcd and stub driver”)
>> Reported-by: kbuild test robot <[email protected]>
>> Reported-by: Julia Lawall <[email protected]>
>> Signed-off-by: Suwan Kim <[email protected]>
>> ---
>> drivers/usb/usbip/vhci_tx.c | 1 +
>> 1 file changed, 1 insertion(+)
>>
>> diff --git a/drivers/usb/usbip/vhci_tx.c b/drivers/usb/usbip/vhci_tx.c
>> index c3803785f6ef..b290e810d11b 100644
>> --- a/drivers/usb/usbip/vhci_tx.c
>> +++ b/drivers/usb/usbip/vhci_tx.c
>> @@ -73,6 +73,7 @@ static int vhci_send_cmd_submit(struct vhci_device *vdev)
>> memset(&pdu_header, 0, sizeof(pdu_header));
>> memset(&msg, 0, sizeof(msg));
>> memset(&iov, 0, sizeof(iov));
>> + iso_buffer = NULL;
>
> Somehow I would have put it after the kfree, since the kfree makes the
> value invalid. iso_buffer is already initialized to NULL for the first
> iteration. If you want to put the setting to NULL at the top of the loop,
> maybe the = NULL in the first line should be removed.
>

It makes sense to clear it after kfree() on line 150.

This kfree() and clearing iso_buffer are necessary only for
PIPE_ISOCHRONOUS case.

Please add a comment above that this is for isoc case.

thanks,
-- Shuah


2019-10-22 09:27:53

by Suwan Kim

[permalink] [raw]
Subject: Re: [PATCH] usbip: Fix free of unallocated memory in vhci tx

On Mon, Oct 21, 2019 at 01:28:54PM -0600, shuah wrote:
> On 10/21/19 9:08 AM, Julia Lawall wrote:
> >
> >
> > On Mon, 21 Oct 2019, Suwan Kim wrote:
> >
> > > iso_buffer should be set to NULL after use and free in the while loop.
> > > In the case of isochronous URB in the while loop, iso_buffer is
> > > allocated and after sending it to server, buffer is deallocated. And
> > > then, if the next URB in the while loop is not a isochronous pipe,
> > > iso_buffer still holds the previously deallocated buffer address and
> > > kfree tries to free wrong buffer address.
> > >
> > > Fixes: ea44d190764b (“usbip: Implement SG support to vhci-hcd and stub driver”)
> > > Reported-by: kbuild test robot <[email protected]>
> > > Reported-by: Julia Lawall <[email protected]>
> > > Signed-off-by: Suwan Kim <[email protected]>
> > > ---
> > > drivers/usb/usbip/vhci_tx.c | 1 +
> > > 1 file changed, 1 insertion(+)
> > >
> > > diff --git a/drivers/usb/usbip/vhci_tx.c b/drivers/usb/usbip/vhci_tx.c
> > > index c3803785f6ef..b290e810d11b 100644
> > > --- a/drivers/usb/usbip/vhci_tx.c
> > > +++ b/drivers/usb/usbip/vhci_tx.c
> > > @@ -73,6 +73,7 @@ static int vhci_send_cmd_submit(struct vhci_device *vdev)
> > > memset(&pdu_header, 0, sizeof(pdu_header));
> > > memset(&msg, 0, sizeof(msg));
> > > memset(&iov, 0, sizeof(iov));
> > > + iso_buffer = NULL;
> >
> > Somehow I would have put it after the kfree, since the kfree makes the
> > value invalid. iso_buffer is already initialized to NULL for the first
> > iteration. If you want to put the setting to NULL at the top of the loop,
> > maybe the = NULL in the first line should be removed.
> >
>
> It makes sense to clear it after kfree() on line 150.
>
> This kfree() and clearing iso_buffer are necessary only for
> PIPE_ISOCHRONOUS case.
>
> Please add a comment above that this is for isoc case.

Ok. I will send v2.

Regards
Suwan Kim