diff mbox

i2c: tegra: fix a possible NULL dereference

Message ID 1447313163-23848-1-git-send-email-clabbe.montjoie@gmail.com
State Deferred
Headers show

Commit Message

Corentin Labbe Nov. 12, 2015, 7:26 a.m. UTC
of_match_device could return NULL, and so cause a NULL pointer
dereference later at line 809:
i2c_dev->hw = match->data;

Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
---
 drivers/i2c/busses/i2c-tegra.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Thierry Reding Nov. 12, 2015, 12:29 p.m. UTC | #1
On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> of_match_device could return NULL, and so cause a NULL pointer

No. There is no way that of_match_device() can ever fail. The driver
core uses the same table to match the OF device to the driver, so the
only case where of_match_device() would return NULL is if no match was
found, in which case the tegra_i2c_probe() function would never have
been called in the first place.

Thierry

> dereference later at line 809:
> i2c_dev->hw = match->data;
> 
> Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
> ---
>  drivers/i2c/busses/i2c-tegra.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
> index a0522fc..c803551 100644
> --- a/drivers/i2c/busses/i2c-tegra.c
> +++ b/drivers/i2c/busses/i2c-tegra.c
> @@ -806,7 +806,10 @@ static int tegra_i2c_probe(struct platform_device *pdev)
>  
>  	if (pdev->dev.of_node) {
>  		const struct of_device_id *match;
> +
>  		match = of_match_device(tegra_i2c_of_match, &pdev->dev);
> +		if (!match)
> +			return -ENODEV;
>  		i2c_dev->hw = match->data;
>  		i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
>  						"nvidia,tegra20-i2c-dvc");
> -- 
> 2.4.10
>
LABBE Corentin Nov. 12, 2015, 12:54 p.m. UTC | #2
On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > of_match_device could return NULL, and so cause a NULL pointer
> 
> No. There is no way that of_match_device() can ever fail. The driver
> core uses the same table to match the OF device to the driver, so the
> only case where of_match_device() would return NULL is if no match was
> found, in which case the tegra_i2c_probe() function would never have
> been called in the first place.
> 
> Thierry
> 

In a parallel thread for i2c-rcar, the conclusion was different.
https://lkml.org/lkml/2015/11/12/83

Regards

--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Thierry Reding Nov. 12, 2015, 1:28 p.m. UTC | #3
On Thu, Nov 12, 2015 at 01:54:22PM +0100, LABBE Corentin wrote:
> On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > > of_match_device could return NULL, and so cause a NULL pointer
> > 
> > No. There is no way that of_match_device() can ever fail. The driver
> > core uses the same table to match the OF device to the driver, so the
> > only case where of_match_device() would return NULL is if no match was
> > found, in which case the tegra_i2c_probe() function would never have
> > been called in the first place.
> > 
> > Thierry
> > 
> 
> In a parallel thread for i2c-rcar, the conclusion was different.
> https://lkml.org/lkml/2015/11/12/83

The conclusion was the same: there should be no case where this happens.
The example that Uwe gave is hypothetical and not valid DT in the first
place. So instead of chickening out I think it'd be better to just crash
to make sure people fix the DT.

On a side-note I think that platform_match() should be stricter and do
something like this instead:

	if (dev->of_node) {
		if (of_driver_match_device(dev, drv))
			return 1;

		return 0;
	}

Thierry
Jon Hunter Nov. 12, 2015, 1:40 p.m. UTC | #4
On 12/11/15 12:29, Thierry Reding wrote:
> * PGP Signed by an unknown key
> 
> On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
>> of_match_device could return NULL, and so cause a NULL pointer
> 
> No. There is no way that of_match_device() can ever fail. The driver
> core uses the same table to match the OF device to the driver, so the
> only case where of_match_device() would return NULL is if no match was
> found, in which case the tegra_i2c_probe() function would never have
> been called in the first place.

Right and so ...

>> dereference later at line 809:
>> i2c_dev->hw = match->data;
>>
>> Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
>> ---
>>  drivers/i2c/busses/i2c-tegra.c | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
>> index a0522fc..c803551 100644
>> --- a/drivers/i2c/busses/i2c-tegra.c
>> +++ b/drivers/i2c/busses/i2c-tegra.c
>> @@ -806,7 +806,10 @@ static int tegra_i2c_probe(struct platform_device *pdev)
>>  
>>  	if (pdev->dev.of_node) {

Can we get rid of this if-statement?

Jon
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Uwe Kleine-König Nov. 12, 2015, 1:45 p.m. UTC | #5
On Thu, Nov 12, 2015 at 02:28:37PM +0100, Thierry Reding wrote:
> On Thu, Nov 12, 2015 at 01:54:22PM +0100, LABBE Corentin wrote:
> > On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> > > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > > > of_match_device could return NULL, and so cause a NULL pointer
> > > 
> > > No. There is no way that of_match_device() can ever fail. The driver
> > > core uses the same table to match the OF device to the driver, so the
> > > only case where of_match_device() would return NULL is if no match was
> > > found, in which case the tegra_i2c_probe() function would never have
> > > been called in the first place.
> > > 
> > > Thierry
> > > 
> > 
> > In a parallel thread for i2c-rcar, the conclusion was different.
> > https://lkml.org/lkml/2015/11/12/83
> 
> The conclusion was the same: there should be no case where this happens.
> The example that Uwe gave is hypothetical and not valid DT in the first
> place. So instead of chickening out I think it'd be better to just crash
> to make sure people fix the DT.

It depends in your trust in the DT. Just because it's not advisable to
do something that is not documented usually isn't a good excuse to not
handle broken input. That't the case for webserver requests, arguments
to system calls and several more. I admit DT is a bit special because
you have to assume it's trusted, but still handling errors in a sane way
is IMHO nice.

> On a side-note I think that platform_match() should be stricter and do
> something like this instead:
> 
> 	if (dev->of_node) {
> 		if (of_driver_match_device(dev, drv))
> 			return 1;
> 
> 		return 0;
> 	}
That's equivalent to

	if (dev->of_node)
		return of_driver_match_device(dev, drv);

and was already suggested in the thread referenced from my reply to
http://article.gmane.org/gmane.linux.kernel/2083641 :-)

Best regards
Uwe
Thierry Reding Nov. 12, 2015, 1:55 p.m. UTC | #6
On Thu, Nov 12, 2015 at 02:45:20PM +0100, Uwe Kleine-König wrote:
> On Thu, Nov 12, 2015 at 02:28:37PM +0100, Thierry Reding wrote:
> > On Thu, Nov 12, 2015 at 01:54:22PM +0100, LABBE Corentin wrote:
> > > On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> > > > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > > > > of_match_device could return NULL, and so cause a NULL pointer
> > > > 
> > > > No. There is no way that of_match_device() can ever fail. The driver
> > > > core uses the same table to match the OF device to the driver, so the
> > > > only case where of_match_device() would return NULL is if no match was
> > > > found, in which case the tegra_i2c_probe() function would never have
> > > > been called in the first place.
> > > > 
> > > > Thierry
> > > > 
> > > 
> > > In a parallel thread for i2c-rcar, the conclusion was different.
> > > https://lkml.org/lkml/2015/11/12/83
> > 
> > The conclusion was the same: there should be no case where this happens.
> > The example that Uwe gave is hypothetical and not valid DT in the first
> > place. So instead of chickening out I think it'd be better to just crash
> > to make sure people fix the DT.
> 
> It depends in your trust in the DT. Just because it's not advisable to
> do something that is not documented usually isn't a good excuse to not
> handle broken input. That't the case for webserver requests, arguments
> to system calls and several more. I admit DT is a bit special because
> you have to assume it's trusted, but still handling errors in a sane way
> is IMHO nice.

Given that it's supposed to be provided by firmware and possibly from a
ROM, crashing might be a better motivation for fixing it than erroring
out, which people might just ignore or not notice until it's too late.

> > On a side-note I think that platform_match() should be stricter and do
> > something like this instead:
> > 
> > 	if (dev->of_node) {
> > 		if (of_driver_match_device(dev, drv))
> > 			return 1;
> > 
> > 		return 0;
> > 	}
> That's equivalent to
> 
> 	if (dev->of_node)
> 		return of_driver_match_device(dev, drv);
> 
> and was already suggested in the thread referenced from my reply to
> http://article.gmane.org/gmane.linux.kernel/2083641 :-)

Ah, too many cross-reference =) FWIW:

Acked-by: Thierry Reding <treding@nvidia.com>

If we want to gracefully handle this, then let's do it in the core by
making sure that drivers where it would return NULL are never probed,
rather than coding this check in every single driver.

Thierry
Thierry Reding Nov. 12, 2015, 1:55 p.m. UTC | #7
On Thu, Nov 12, 2015 at 01:40:56PM +0000, Jon Hunter wrote:
> 
> On 12/11/15 12:29, Thierry Reding wrote:
> > * PGP Signed by an unknown key
> > 
> > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> >> of_match_device could return NULL, and so cause a NULL pointer
> > 
> > No. There is no way that of_match_device() can ever fail. The driver
> > core uses the same table to match the OF device to the driver, so the
> > only case where of_match_device() would return NULL is if no match was
> > found, in which case the tegra_i2c_probe() function would never have
> > been called in the first place.
> 
> Right and so ...
> 
> >> dereference later at line 809:
> >> i2c_dev->hw = match->data;
> >>
> >> Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
> >> ---
> >>  drivers/i2c/busses/i2c-tegra.c | 3 +++
> >>  1 file changed, 3 insertions(+)
> >>
> >> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
> >> index a0522fc..c803551 100644
> >> --- a/drivers/i2c/busses/i2c-tegra.c
> >> +++ b/drivers/i2c/busses/i2c-tegra.c
> >> @@ -806,7 +806,10 @@ static int tegra_i2c_probe(struct platform_device *pdev)
> >>  
> >>  	if (pdev->dev.of_node) {
> 
> Can we get rid of this if-statement?

Yeah, I guess we can drop that, too. It's been a long time since Tegra
was converted to OF only.

Thierry
Corentin Labbe Nov. 12, 2015, 2:54 p.m. UTC | #8
On Thu, Nov 12, 2015 at 02:55:00PM +0100, Thierry Reding wrote:
> On Thu, Nov 12, 2015 at 02:45:20PM +0100, Uwe Kleine-König wrote:
> > On Thu, Nov 12, 2015 at 02:28:37PM +0100, Thierry Reding wrote:
> > > On Thu, Nov 12, 2015 at 01:54:22PM +0100, LABBE Corentin wrote:
> > > > On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> > > > > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > > > > > of_match_device could return NULL, and so cause a NULL pointer
> > > > > 
> > > > > No. There is no way that of_match_device() can ever fail. The driver
> > > > > core uses the same table to match the OF device to the driver, so the
> > > > > only case where of_match_device() would return NULL is if no match was
> > > > > found, in which case the tegra_i2c_probe() function would never have
> > > > > been called in the first place.
> > > > > 
> > > > > Thierry
> > > > > 
> > > > 
> > > > In a parallel thread for i2c-rcar, the conclusion was different.
> > > > https://lkml.org/lkml/2015/11/12/83
> > > 
> > > The conclusion was the same: there should be no case where this happens.
> > > The example that Uwe gave is hypothetical and not valid DT in the first
> > > place. So instead of chickening out I think it'd be better to just crash
> > > to make sure people fix the DT.
> > 
> > It depends in your trust in the DT. Just because it's not advisable to
> > do something that is not documented usually isn't a good excuse to not
> > handle broken input. That't the case for webserver requests, arguments
> > to system calls and several more. I admit DT is a bit special because
> > you have to assume it's trusted, but still handling errors in a sane way
> > is IMHO nice.
> 
> Given that it's supposed to be provided by firmware and possibly from a
> ROM, crashing might be a better motivation for fixing it than erroring
> out, which people might just ignore or not notice until it's too late.
> 
> > > On a side-note I think that platform_match() should be stricter and do
> > > something like this instead:
> > > 
> > > 	if (dev->of_node) {
> > > 		if (of_driver_match_device(dev, drv))
> > > 			return 1;
> > > 
> > > 		return 0;
> > > 	}
> > That's equivalent to
> > 
> > 	if (dev->of_node)
> > 		return of_driver_match_device(dev, drv);
> > 
> > and was already suggested in the thread referenced from my reply to
> > http://article.gmane.org/gmane.linux.kernel/2083641 :-)
> 
> Ah, too many cross-reference =) FWIW:
> 
> Acked-by: Thierry Reding <treding@nvidia.com>
> 

Just for be sure, since the thread goes in lot of direction, you ack my patch ?
Perhaps is it better that I resent a version which use of_device_get_match_data() ?

Regards

--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Thierry Reding Nov. 12, 2015, 4:14 p.m. UTC | #9
On Thu, Nov 12, 2015 at 03:54:58PM +0100, LABBE Corentin wrote:
> On Thu, Nov 12, 2015 at 02:55:00PM +0100, Thierry Reding wrote:
> > On Thu, Nov 12, 2015 at 02:45:20PM +0100, Uwe Kleine-König wrote:
> > > On Thu, Nov 12, 2015 at 02:28:37PM +0100, Thierry Reding wrote:
> > > > On Thu, Nov 12, 2015 at 01:54:22PM +0100, LABBE Corentin wrote:
> > > > > On Thu, Nov 12, 2015 at 01:29:23PM +0100, Thierry Reding wrote:
> > > > > > On Thu, Nov 12, 2015 at 08:26:03AM +0100, LABBE Corentin wrote:
> > > > > > > of_match_device could return NULL, and so cause a NULL pointer
> > > > > > 
> > > > > > No. There is no way that of_match_device() can ever fail. The driver
> > > > > > core uses the same table to match the OF device to the driver, so the
> > > > > > only case where of_match_device() would return NULL is if no match was
> > > > > > found, in which case the tegra_i2c_probe() function would never have
> > > > > > been called in the first place.
> > > > > > 
> > > > > > Thierry
> > > > > > 
> > > > > 
> > > > > In a parallel thread for i2c-rcar, the conclusion was different.
> > > > > https://lkml.org/lkml/2015/11/12/83
> > > > 
> > > > The conclusion was the same: there should be no case where this happens.
> > > > The example that Uwe gave is hypothetical and not valid DT in the first
> > > > place. So instead of chickening out I think it'd be better to just crash
> > > > to make sure people fix the DT.
> > > 
> > > It depends in your trust in the DT. Just because it's not advisable to
> > > do something that is not documented usually isn't a good excuse to not
> > > handle broken input. That't the case for webserver requests, arguments
> > > to system calls and several more. I admit DT is a bit special because
> > > you have to assume it's trusted, but still handling errors in a sane way
> > > is IMHO nice.
> > 
> > Given that it's supposed to be provided by firmware and possibly from a
> > ROM, crashing might be a better motivation for fixing it than erroring
> > out, which people might just ignore or not notice until it's too late.
> > 
> > > > On a side-note I think that platform_match() should be stricter and do
> > > > something like this instead:
> > > > 
> > > > 	if (dev->of_node) {
> > > > 		if (of_driver_match_device(dev, drv))
> > > > 			return 1;
> > > > 
> > > > 		return 0;
> > > > 	}
> > > That's equivalent to
> > > 
> > > 	if (dev->of_node)
> > > 		return of_driver_match_device(dev, drv);
> > > 
> > > and was already suggested in the thread referenced from my reply to
> > > http://article.gmane.org/gmane.linux.kernel/2083641 :-)
> > 
> > Ah, too many cross-reference =) FWIW:
> > 
> > Acked-by: Thierry Reding <treding@nvidia.com>
> > 
> 
> Just for be sure, since the thread goes in lot of direction, you ack my patch ?
> Perhaps is it better that I resent a version which use of_device_get_match_data() ?

No, the Acked-by was for Uwe's proposal to modify platform_match(). I
think if we want to gracefully handle these cases, then the right way to
do so is by having the driver core not fallback to name matches for
devices instantiated from device tree.

Sorry for being unclear.

Thierry
Wolfram Sang Jan. 23, 2016, 10:56 a.m. UTC | #10
Hi Uwe,

> That's equivalent to
> 
> 	if (dev->of_node)
> 		return of_driver_match_device(dev, drv);
> 
> and was already suggested in the thread referenced from my reply to
> http://article.gmane.org/gmane.linux.kernel/2083641 :-)

Did you make a proper patch out of it? Can't find it, though I also
think this would be useful.

Acked-by: Wolfram Sang <wsa@the-dreams.de>

All the best,

   Wolfram
Wolfram Sang Jan. 23, 2016, 11:07 a.m. UTC | #11
> Perhaps is it better that I resent a version which use of_device_get_match_data() ?

I'd like that for both of your patches anyhow. It is more concise and
better to read IMO.
diff mbox

Patch

diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index a0522fc..c803551 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -806,7 +806,10 @@  static int tegra_i2c_probe(struct platform_device *pdev)
 
 	if (pdev->dev.of_node) {
 		const struct of_device_id *match;
+
 		match = of_match_device(tegra_i2c_of_match, &pdev->dev);
+		if (!match)
+			return -ENODEV;
 		i2c_dev->hw = match->data;
 		i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
 						"nvidia,tegra20-i2c-dvc");