linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: Add data checks in usbtmc_disconnect
@ 2021-03-23  3:47 Lv Yunlong
  2021-03-23  7:16 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Lv Yunlong @ 2021-03-23  3:47 UTC (permalink / raw)
  To: gregkh, linux-usb; +Cc: linux-kernel, Lv Yunlong

In usbtmc_disconnect, data is got from intf with the
initial reference. There is no refcount inc operation
before usbmc_free_int(data). In usbmc_free_int(data),
the data may be freed.

But later in usbtmc_disconnect, there is another put
function of data. I think it is better to add necessary
checks to avoid the data being put twice. It could cause
errors in race.

Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
---
 drivers/usb/class/usbtmc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index 74d5a9c5238a..e0438cb46386 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -2494,7 +2494,9 @@ static void usbtmc_disconnect(struct usb_interface *intf)
 	}
 	mutex_unlock(&data->io_mutex);
 	usbtmc_free_int(data);
-	kref_put(&data->kref, usbtmc_delete);
+
+	if (data->iin_ep_present && data->iin_urb)
+		kref_put(&data->kref, usbtmc_delete);
 }
 
 static void usbtmc_draw_down(struct usbtmc_file_data *file_data)
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: Add data checks in usbtmc_disconnect
  2021-03-23  3:47 [PATCH] usb: Add data checks in usbtmc_disconnect Lv Yunlong
@ 2021-03-23  7:16 ` Greg KH
  2021-03-23  9:10   ` lyl2019
  0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2021-03-23  7:16 UTC (permalink / raw)
  To: Lv Yunlong; +Cc: linux-usb, linux-kernel

On Mon, Mar 22, 2021 at 08:47:17PM -0700, Lv Yunlong wrote:
> In usbtmc_disconnect, data is got from intf with the
> initial reference. There is no refcount inc operation
> before usbmc_free_int(data). In usbmc_free_int(data),
> the data may be freed.
> 
> But later in usbtmc_disconnect, there is another put
> function of data. I think it is better to add necessary
> checks to avoid the data being put twice. It could cause
> errors in race.
> 
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> ---
>  drivers/usb/class/usbtmc.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
> index 74d5a9c5238a..e0438cb46386 100644
> --- a/drivers/usb/class/usbtmc.c
> +++ b/drivers/usb/class/usbtmc.c
> @@ -2494,7 +2494,9 @@ static void usbtmc_disconnect(struct usb_interface *intf)
>  	}
>  	mutex_unlock(&data->io_mutex);
>  	usbtmc_free_int(data);
> -	kref_put(&data->kref, usbtmc_delete);
> +
> +	if (data->iin_ep_present && data->iin_urb)
> +		kref_put(&data->kref, usbtmc_delete);

What protects the data from changing right after the check and right
before the kref_put() call?

krefs need a lock somewhere to protect from races like this, please fix
that logic instead.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Re: [PATCH] usb: Add data checks in usbtmc_disconnect
  2021-03-23  7:16 ` Greg KH
@ 2021-03-23  9:10   ` lyl2019
  0 siblings, 0 replies; 3+ messages in thread
From: lyl2019 @ 2021-03-23  9:10 UTC (permalink / raw)
  To: Greg KH; +Cc: linux-usb, linux-kernel




> -----原始邮件-----
> 发件人: "Greg KH" <gregkh@linuxfoundation.org>
> 发送时间: 2021-03-23 15:16:55 (星期二)
> 收件人: "Lv Yunlong" <lyl2019@mail.ustc.edu.cn>
> 抄送: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
> 主题: Re: [PATCH] usb: Add data checks in usbtmc_disconnect
> 
> On Mon, Mar 22, 2021 at 08:47:17PM -0700, Lv Yunlong wrote:
> > In usbtmc_disconnect, data is got from intf with the
> > initial reference. There is no refcount inc operation
> > before usbmc_free_int(data). In usbmc_free_int(data),
> > the data may be freed.
> > 
> > But later in usbtmc_disconnect, there is another put
> > function of data. I think it is better to add necessary
> > checks to avoid the data being put twice. It could cause
> > errors in race.
> > 
> > Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> > ---
> >  drivers/usb/class/usbtmc.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
> > index 74d5a9c5238a..e0438cb46386 100644
> > --- a/drivers/usb/class/usbtmc.c
> > +++ b/drivers/usb/class/usbtmc.c
> > @@ -2494,7 +2494,9 @@ static void usbtmc_disconnect(struct usb_interface *intf)
> >  	}
> >  	mutex_unlock(&data->io_mutex);
> >  	usbtmc_free_int(data);
> > -	kref_put(&data->kref, usbtmc_delete);
> > +
> > +	if (data->iin_ep_present && data->iin_urb)
> > +		kref_put(&data->kref, usbtmc_delete);
> 
> What protects the data from changing right after the check and right
> before the kref_put() call?
> 
> krefs need a lock somewhere to protect from races like this, please fix
> that logic instead.
> 
> thanks,
> 
> greg k-h

Thanks for your reminder. I think there is something wrong with my patch.

The check condition before kref_put() added is because the data will not be
freed  only when (data->iin_ep_present && data->iin_urb) is true in 
usbtmc_free_int(data). But i ignored that the data may be already be freed
in usbtmc_free_int().

I will submit a PATCH v2 later. Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-23  9:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23  3:47 [PATCH] usb: Add data checks in usbtmc_disconnect Lv Yunlong
2021-03-23  7:16 ` Greg KH
2021-03-23  9:10   ` lyl2019

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).