Skip to contentSkip to conversation

ASoC: test-component: fix null pointer dereference.

PATCH Superseded Ameer Hamza 3 participants 15 messages Dec 5, 2021 – Dec 9, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: test-component: fix null pointer dereference.

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

---
 sound/soc/generic/test-component.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
sound/soc/generic/test-component.c +4 -1
index 85385a771d80..8fc97d3ff011 100644
@@ -532,13 +532,16 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
 	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata = of_id->data;
+	const struct test_adata *adata;
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
+	if (!of_id)
+		return -EINVAL;
+	adata = of_id->data;
 	num = of_graph_get_endpoint_count(node);
 	if (!num) {
 		dev_err(dev, "no port exits\n");
Signature · 1 line
2.25.1
#1 lore raw

Hi Ameer

Thank you for your patch.

Quoted 3 lines

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

(snip)

Quoted 5 lines
sound/soc/generic/test-component.c +0 -1
@@ -532,13 +532,16 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
 	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata = of_id->data;

sound/soc/generic/test-component.c static int test_driver_probe(struct platform_device *pdev) · line 544

Quoted 10 lines
sound/soc/generic/test-component.c +4 -0
+	const struct test_adata *adata;
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
+	if (!of_id)
+		return -EINVAL;
+	adata = of_id->data;

But hmm...
Probing this driver without adata is strange for me. How did probe this driver ??

Best regards
---
Kuninori Morimoto

#2 lore raw
Quoted 21 lines · Kuninori Morimoto

Hi Ameer

Thank you for your patch.

Quoted 3 lines

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

(snip)

Quoted 12 lines
sound/soc/generic/test-component.c +1 -1
@@ -532,13 +532,16 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
 	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata = of_id->data;
+	const struct test_adata *adata;
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
Quoted 7 lines

sound/soc/generic/test-component.c static int test_driver_probe(struct platform_device *pdev) · line 544

Quoted 3 lines
sound/soc/generic/test-component.c +3 -0
+	if (!of_id)
+		return -EINVAL;
+	adata = of_id->data;

But hmm...
Probing this driver without adata is strange for me. How did probe this driver ??

Thank you for your response. Unfortunately, I am not aware of implementation details of this component. Coverity suggested that there can be a potential NULL pointer access which seems logical to me. Do you agree with coverity here?

Quoted 4 lines

Best regards
---
Kuninori Morimoto

#3 lore raw

Hi Ameer

Quoted 7 lines
Quoted 2 lines

Probing this driver without adata is strange for me. How did probe this driver ??

Thank you for your response. Unfortunately, I am not aware of implementation details of this component. Coverity suggested that there can be a potential NULL pointer access which seems logical to me. Do you agree with coverity here?

I think no potential NULL pointer access, because this driver can't be called without of_id->data.
But, potential NULL pointer check itself is good idea. It seems your patch was already accepted :)

I noticed that we can replace it to use of_device_get_match_data()

- const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
- const struct test_adata *adata = of_id->data;
+ const struct test_adata *adata = of_device_get_match_data(&pdev->dev);

Best regards
---
Kuninori Morimoto

#4 lore raw
Quoted 11 lines · Kuninori Morimoto

Hi Ameer

Quoted 7 lines
Quoted 2 lines

Probing this driver without adata is strange for me. How did probe this driver ??

Thank you for your response. Unfortunately, I am not aware of implementation details of this component. Coverity suggested that there can be a potential NULL pointer access which seems logical to me. Do you agree with coverity here?

Quoted 4 lines

I think no potential NULL pointer access, because this driver can't be called without of_id->data.
But, potential NULL pointer check itself is good idea. It seems your patch was already accepted :)

Yes, indeed.

Review

Quoted 5 lines

I noticed that we can replace it to use of_device_get_match_data()

(quoted diff) +1 -2
-	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata = of_id->data;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);

Thanks, that's a cleaner way. Can you advise how should proceed with this since previous patch is already applied. Should I send a updated version of patch, e.g., v2 or a new patch.

Quoted 4 lines

Best regards
---
Kuninori Morimoto

maintainer
#5 lore raw
Quoted 8 lines · Ameer Hamza

Review

Quoted 2 lines · Kuninori Morimoto
(quoted diff) +1 -1
-	const struct test_adata *adata = of_id->data;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);

Thanks, that's a cleaner way. Can you advise how should proceed with this since previous patch is already applied. Should I send a updated version of patch, e.g., v2 or a new patch.

Please send an incremental patch on top of what is already applied as covered in the message saying the patch was applied.

#6 lore raw

PATCH v2 1/1 ASoC: test-component: fix null pointer dereference.

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

---
changes in v2:
Replace of_match_device() with of_device_get_match_data to simplify the logic as suggested by Kuninori Morimoto

---
 sound/soc/generic/test-component.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/soc/generic/test-component.c +2 -6
index 8fc97d3ff011..5da4725d9e16 100644
@@ -531,17 +531,13 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
-	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
-	if (!of_id)
-		return -EINVAL;
-	adata = of_id->data;
 	num = of_graph_get_endpoint_count(node);
 	if (!num) {
 		dev_err(dev, "no port exits\n");
@@ -552,7 +548,7 @@ static int test_driver_probe(struct platform_device *pdev)
 	cdriv	= devm_kzalloc(dev, sizeof(*cdriv),		GFP_KERNEL);
 	ddriv	= devm_kzalloc(dev, sizeof(*ddriv) * num,	GFP_KERNEL);
 	dname	= devm_kzalloc(dev, sizeof(*dname) * num,	GFP_KERNEL);
-	if (!priv || !cdriv || !ddriv || !dname)
+	if (!priv || !cdriv || !ddriv || !dname || !adata)
 		return -EINVAL;
 
 	priv->dev		= dev;
Signature · 1 line
2.25.1
#7 lore raw

Hi Ameer

Ah, you posted the patch :)

Quoted 5 lines

Subject: [PATCH v2] ASoC: test-component: fix null pointer dereference.

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

Previous your patch was already accepted,
thus this is not v2 patch.
git log should indicate is replace of_match_device() to of_device_get_match_data()

Best regards
---
Kuninori Morimoto

#8 lore raw
Quoted 7 lines · Kuninori Morimoto

Hi Ameer

Ah, you posted the patch :)

Quoted 2 lines

Subject: [PATCH v2] ASoC: test-component: fix null pointer dereference.

Quoted 7 lines
Quoted 3 lines

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

Previous your patch was already accepted,
thus this is not v2 patch.
git log should indicate is replace of_match_device() to of_device_get_match_data()

Thank you for your kind response and clarifying the process. Let me send the updated patch. :)

Best Regards,
Ameer Hamza.

#9 lore raw

PATCH 1/1 ASoC: test-component: replace of_match_device() to of_device_get_match_data()

Getting device data from of_device_get_match_data() for a cleaner implementation.

---
 sound/soc/generic/test-component.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/soc/generic/test-component.c +2 -6
index 8fc97d3ff011..5da4725d9e16 100644
@@ -531,17 +531,13 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
-	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
-	if (!of_id)
-		return -EINVAL;
-	adata = of_id->data;
 	num = of_graph_get_endpoint_count(node);
 	if (!num) {
 		dev_err(dev, "no port exits\n");
@@ -552,7 +548,7 @@ static int test_driver_probe(struct platform_device *pdev)
 	cdriv	= devm_kzalloc(dev, sizeof(*cdriv),		GFP_KERNEL);
 	ddriv	= devm_kzalloc(dev, sizeof(*ddriv) * num,	GFP_KERNEL);
 	dname	= devm_kzalloc(dev, sizeof(*dname) * num,	GFP_KERNEL);
-	if (!priv || !cdriv || !ddriv || !dname)
+	if (!priv || !cdriv || !ddriv || !dname || !adata)
 		return -EINVAL;
 
 	priv->dev		= dev;
Signature · 1 line
2.25.1
#10 lore raw

Re: [PATCH] ASoC: test-component: replace of_match_device() to of_device_get_match_data()

Quoted 33 lines · Ameer Hamza

Getting device data from of_device_get_match_data() for a cleaner implementation.

---
 sound/soc/generic/test-component.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/soc/generic/test-component.c +1 -5
index 8fc97d3ff011..5da4725d9e16 100644
@@ -531,17 +531,13 @@ static int test_driver_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct device_node *node = dev->of_node;
 	struct device_node *ep;
-	const struct of_device_id *of_id = of_match_device(test_of_match, &pdev->dev);
-	const struct test_adata *adata;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);
 	struct snd_soc_component_driver *cdriv;
 	struct snd_soc_dai_driver *ddriv;
 	struct test_dai_name *dname;
 	struct test_priv *priv;
 	int num, ret, i;
 
-	if (!of_id)
-		return -EINVAL;
-	adata = of_id->data;
 	num = of_graph_get_endpoint_count(node);
 	if (!num) {
 		dev_err(dev, "no port exits\n");

sound/soc/generic/test-component.c static int test_driver_probe(struct platform_device *pdev) · line 554

Quoted 8 lines
sound/soc/generic/test-component.c +1 -1
@@ -552,7 +548,7 @@ static int test_driver_probe(struct platform_device *pdev)
 	cdriv	= devm_kzalloc(dev, sizeof(*cdriv),		GFP_KERNEL);
 	ddriv	= devm_kzalloc(dev, sizeof(*ddriv) * num,	GFP_KERNEL);
 	dname	= devm_kzalloc(dev, sizeof(*dname) * num,	GFP_KERNEL);
-	if (!priv || !cdriv || !ddriv || !dname)
+	if (!priv || !cdriv || !ddriv || !dname || !adata)
 		return -EINVAL;
 
 	priv->dev		= dev;

Hi Kuninori,
Would be really appreciated if you can review the patch please.

Best Regards,
Ameer Hamza.

maintainer
#12 lore raw
In reply to Ameer Hamza
Quoted 5 lines · Ameer Hamza

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: test-component: fix null pointer dereference.

      commit: c686316ec1210d43653c91e104c1e4cd0156dc89

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark

#13 lore raw
In reply to Mark Brown

Hi Ameer, Mark

Quoted 9 lines
Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-	const struct test_adata *adata = of_id->data;
+	const struct test_adata *adata = of_device_get_match_data(&pdev->dev);

Thanks, that's a cleaner way. Can you advise how should proceed with this since previous patch is already applied. Should I send a updated version of patch, e.g., v2 or a new patch.

Please send an incremental patch on top of what is already applied as covered in the message saying the patch was applied.

No worry.
I will post that patch

Best regards
---
Kuninori Morimoto

maintainer
#14 lore raw
In reply to Ameer Hamza
Quoted 5 lines · Ameer Hamza

Dereferncing of_id pointer will result in exception in current implementation since of_match_device() will assign it to NULL. Adding NULL check for protection.

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: test-component: fix null pointer dereference.

      commit: c686316ec1210d43653c91e104c1e4cd0156dc89

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark