Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1569398 > unrolled thread

[PATCH] Usb: host - Fix possible NULL derefrence.

Started byShailendra Verma <shailendra.v@samsung.com>
First post2017-01-30 06:20 +0100
Last post2017-01-30 08:20 +0100
Articles 3 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH] Usb: host - Fix possible NULL derefrence. Shailendra Verma <shailendra.v@samsung.com> - 2017-01-30 06:20 +0100
    Re: [PATCH] Usb: host - Fix possible NULL derefrence. Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-01-30 08:00 +0100
      Re: [PATCH] Usb: host - Fix possible NULL derefrence. Thierry Reding <thierry.reding@gmail.com> - 2017-01-30 08:20 +0100

#1569398 — [PATCH] Usb: host - Fix possible NULL derefrence.

FromShailendra Verma <shailendra.v@samsung.com>
Date2017-01-30 06:20 +0100
Subject[PATCH] Usb: host - Fix possible NULL derefrence.
Message-ID<t5cxI-8lg-5@gated-at.bofh.it>
of_device_get_match_data could return NULL, and so can cause
a NULL pointer dereference later.

Signed-off-by: Shailendra Verma <shailendra.v@samsung.com>
---
 drivers/usb/host/xhci-tegra.c |    4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/host/xhci-tegra.c b/drivers/usb/host/xhci-tegra.c
index a59fafb..890c778 100644
--- a/drivers/usb/host/xhci-tegra.c
+++ b/drivers/usb/host/xhci-tegra.c
@@ -903,6 +903,10 @@ static int tegra_xusb_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	tegra->soc = of_device_get_match_data(&pdev->dev);
+	if (!tegra->soc) {
+		dev_err(&pdev->dev, "no device match found\n");
+		return -ENODEV;
+	}
 	mutex_init(&tegra->lock);
 	tegra->dev = &pdev->dev;
 
-- 
1.7.9.5

[toc] | [next] | [standalone]


#1569425

FromGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Date2017-01-30 08:00 +0100
Message-ID<t5e6u-I2-1@gated-at.bofh.it>
In reply to#1569398
On Mon, Jan 30, 2017 at 10:36:29AM +0530, Shailendra Verma wrote:
> of_device_get_match_data could return NULL, and so can cause
> a NULL pointer dereference later.
> 
> Signed-off-by: Shailendra Verma <shailendra.v@samsung.com>
> ---
>  drivers/usb/host/xhci-tegra.c |    4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/usb/host/xhci-tegra.c b/drivers/usb/host/xhci-tegra.c
> index a59fafb..890c778 100644
> --- a/drivers/usb/host/xhci-tegra.c
> +++ b/drivers/usb/host/xhci-tegra.c
> @@ -903,6 +903,10 @@ static int tegra_xusb_probe(struct platform_device *pdev)
>  		return -ENOMEM;
>  
>  	tegra->soc = of_device_get_match_data(&pdev->dev);
> +	if (!tegra->soc) {

How would the driver be loaded and the probe function called if this
returns NULL?

Is this ever possible?

thanks,

greg k-h

[toc] | [prev] | [next] | [standalone]


#1569436

FromThierry Reding <thierry.reding@gmail.com>
Date2017-01-30 08:20 +0100
Message-ID<t5epP-15B-13@gated-at.bofh.it>
In reply to#1569425

[Multipart message — attachments visible in raw view] — view raw

On Mon, Jan 30, 2017 at 07:45:21AM +0100, Greg Kroah-Hartman wrote:
> On Mon, Jan 30, 2017 at 10:36:29AM +0530, Shailendra Verma wrote:
> > of_device_get_match_data could return NULL, and so can cause
> > a NULL pointer dereference later.
> > 
> > Signed-off-by: Shailendra Verma <shailendra.v@samsung.com>
> > ---
> >  drivers/usb/host/xhci-tegra.c |    4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/drivers/usb/host/xhci-tegra.c b/drivers/usb/host/xhci-tegra.c
> > index a59fafb..890c778 100644
> > --- a/drivers/usb/host/xhci-tegra.c
> > +++ b/drivers/usb/host/xhci-tegra.c
> > @@ -903,6 +903,10 @@ static int tegra_xusb_probe(struct platform_device *pdev)
> >  		return -ENOMEM;
> >  
> >  	tegra->soc = of_device_get_match_data(&pdev->dev);
> > +	if (!tegra->soc) {
> 
> How would the driver be loaded and the probe function called if this
> returns NULL?
> 
> Is this ever possible?

No, it isn't. I've been NAK'ing this kind of patch for a while now.
There are two variants of this patch going about:

  1) checking the return value of of_match_device()
  2) checking the return value of of_device_get_match_data()

The same may also apply to of_match_node(), but I haven't seen that used
very much lately.

For of_match_device() the problem could technically occur if used in non
OF setups, because the device could be instantiated by hand in board
setup code. Tegra has been OF-only for a couple of years now, so there
is no way this can happen today.

of_device_get_match_data() is somewhat more complicated because it could
still return NULL if the OF table entry had its .data field set to NULL.
However in all drivers that I know that would be considered a bug, so
might as well let things crash at this point to make it immediately
obvious.

I had once been tempted to write a checkpatch rule for this, but I'm not
sure it's as easy as just warning if there's a check, because there are
some legitimate cases, even if they're very rare.

Thierry

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web