linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: cdns3:  Fix potential dereference of NULL pointer
@ 2022-05-18  8:12 Yongzhi Liu
  2022-05-19 16:07 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Yongzhi Liu @ 2022-05-18  8:12 UTC (permalink / raw)
  To: peter.chen, pawell, rogerq, a-govindraju, gregkh, felipe.balbi
  Cc: linux-usb, linux-kernel, fuyq, Yongzhi Liu

The return value of cdns3_gadget_ep_alloc_request()
needs to be checked to avoid use of NULL pointer
in case of an allocation failure.

Fixes: 7733f6c32e36f ("usb: cdns3: Add Cadence USB3 DRD Driver")

Signed-off-by: Yongzhi Liu <lyz_cs@pku.edu.cn>
---
 drivers/usb/cdns3/cdns3-gadget.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
index 5d8c982..7be328e 100644
--- a/drivers/usb/cdns3/cdns3-gadget.c
+++ b/drivers/usb/cdns3/cdns3-gadget.c
@@ -2568,6 +2568,10 @@ static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
 		struct cdns3_request *priv_req;
 
 		zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
+		if (!zlp_request) {
+			ret = -ENOMEM;
+			goto err;
+		}
 		zlp_request->buf = priv_dev->zlp_buf;
 		zlp_request->length = 0;
 
@@ -2578,7 +2582,7 @@ static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
 			priv_ep->name);
 		ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
 	}
-
+err:
 	spin_unlock_irqrestore(&priv_dev->lock, flags);
 	return ret;
 }
-- 
2.7.4


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

* Re: [PATCH] usb: cdns3:  Fix potential dereference of NULL pointer
  2022-05-18  8:12 [PATCH] usb: cdns3: Fix potential dereference of NULL pointer Yongzhi Liu
@ 2022-05-19 16:07 ` Greg KH
  2022-05-19 17:15   ` 刘永志
  0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2022-05-19 16:07 UTC (permalink / raw)
  To: Yongzhi Liu
  Cc: peter.chen, pawell, rogerq, a-govindraju, felipe.balbi, linux-usb,
	linux-kernel, fuyq

On Wed, May 18, 2022 at 01:12:50AM -0700, Yongzhi Liu wrote:
> The return value of cdns3_gadget_ep_alloc_request()
> needs to be checked to avoid use of NULL pointer
> in case of an allocation failure.
> 
> Fixes: 7733f6c32e36f ("usb: cdns3: Add Cadence USB3 DRD Driver")
> 
> Signed-off-by: Yongzhi Liu <lyz_cs@pku.edu.cn>

Again, no blank line and use the full width for your text.

> ---
>  drivers/usb/cdns3/cdns3-gadget.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> index 5d8c982..7be328e 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.c
> +++ b/drivers/usb/cdns3/cdns3-gadget.c
> @@ -2568,6 +2568,10 @@ static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
>  		struct cdns3_request *priv_req;
>  
>  		zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
> +		if (!zlp_request) {
> +			ret = -ENOMEM;
> +			goto err;
> +		}

How did you test this that the if the allocation fails this will clean
up properly?

thanks,

greg k-h

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

* Re: Re: [PATCH] usb: cdns3:  Fix potential dereference of NULL pointer
  2022-05-19 16:07 ` Greg KH
@ 2022-05-19 17:15   ` 刘永志
  0 siblings, 0 replies; 3+ messages in thread
From: 刘永志 @ 2022-05-19 17:15 UTC (permalink / raw)
  To: greg kh
  Cc: peter.chen, pawell, rogerq, a-govindraju, felipe.balbi, linux-usb,
	linux-kernel, fuyq




> -----Original Messages-----
> From: "Greg KH" <gregkh@linuxfoundation.org>
> Sent Time: 2022-05-20 00:07:17 (Friday)
> To: "Yongzhi Liu" <lyz_cs@pku.edu.cn>
> Cc: peter.chen@kernel.org, pawell@cadence.com, rogerq@kernel.org, a-govindraju@ti.com, felipe.balbi@linux.intel.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, fuyq@stu.pku.edu.cn
> Subject: Re: [PATCH] usb: cdns3:  Fix potential dereference of NULL pointer
> 
> On Wed, May 18, 2022 at 01:12:50AM -0700, Yongzhi Liu wrote:
> > The return value of cdns3_gadget_ep_alloc_request()
> > needs to be checked to avoid use of NULL pointer
> > in case of an allocation failure.
> > 
> > Fixes: 7733f6c32e36f ("usb: cdns3: Add Cadence USB3 DRD Driver")
> > 
> > Signed-off-by: Yongzhi Liu <lyz_cs@pku.edu.cn>
> 
> Again, no blank line and use the full width for your text.
> 
> > ---
> >  drivers/usb/cdns3/cdns3-gadget.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> > index 5d8c982..7be328e 100644
> > --- a/drivers/usb/cdns3/cdns3-gadget.c
> > +++ b/drivers/usb/cdns3/cdns3-gadget.c
> > @@ -2568,6 +2568,10 @@ static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
> >  		struct cdns3_request *priv_req;
> >  
> >  		zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
> > +		if (!zlp_request) {
> > +			ret = -ENOMEM;
> > +			goto err;
> > +		}
> 
> How did you test this that the if the allocation fails this will clean
> up properly?
> 

I find this by a static analyzer based on frequency and similarity, which report many null ptr deref bugs.
In cdns3/cdns3-gadget.c, I find that we usually check the return value when call function 'cdns3_gadget_ep_alloc_request'.
If 'zcalloc' in 'cdns3_gadget_ep_alloc_request' failed, the allocation will return null. Therefore, i think we should add null checks here.
I will resubmit a new patch if you think the bug is real.
Thanks for your reply and advice.

> thanks,
> 
> greg k-h

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

end of thread, other threads:[~2022-05-19 17:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-18  8:12 [PATCH] usb: cdns3: Fix potential dereference of NULL pointer Yongzhi Liu
2022-05-19 16:07 ` Greg KH
2022-05-19 17:15   ` 刘永志

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).