net: ethernet: mediatek: add NULL check on of_match_device() return value
Subsystems (4)
PATCH 1/1 net: ethernet: mediatek: add NULL check on of_match_device() return value
Check return value from call to of_match_device()
in order to prevent a NULL pointer dereference.
In case of NULL print error message and return -ENODEV
--- drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +5 -0
@@ -2408,6 +2408,11 @@ static int mtk_probe(struct platform_device *pdev)int i; match =of_match_device (of_mtk_match, &pdev->dev); +if (!match) { +dev_err (&pdev->dev,"failed to match device\n" ); +return -ENODEV ; + } + soc = (struct mtk_soc_data *)match->data; eth =devm_kzalloc (&pdev->dev,sizeof (*eth),GFP_KERNEL );
Signature · 1 line
2.5.0
Quoted 7 lines · Gustavo A. R. Silva
Check return value from call to
of_match_device()
in order to prevent a NULL pointer dereference.In case of NULL print error message and return
-ENODEVSigned-off-by Gustavo A. R. Silva <[email protected]>---
Quoted 20 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/net/ethernet/mediatek/mtk_eth_soc.c +5
-0@@ -2408,6 +2408,11 @@ static int mtk_probe(struct platform_device *pdev)int i; match =of_match_device (of_mtk_match, &pdev->dev); +if (!match) { +dev_err (&pdev->dev,"failed to match device\n" ); +return -ENODEV ; + } + soc = (struct mtk_soc_data *)match->data; eth =devm_kzalloc (&pdev->dev,sizeof (*eth),GFP_KERNEL );
Quoted 15 lines · Gustavo A. R. Silva
Check return value from call to
of_match_device()
in order to prevent a NULL pointer dereference.In case of NULL print error message and return
-ENODEVSigned-off-by Gustavo A. R. Silva <[email protected]>--- drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/net/ethernet/mediatek/mtk_eth_soc.c +0
-0
drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_probe(struct platform_device *pdev) · line 2416
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +5
-0@@ -2408,6 +2408,11 @@ static int mtk_probe(struct platform_device *pdev)int i; match =of_match_device (of_mtk_match, &pdev->dev); +if (!match) { +dev_err (&pdev->dev,"failed to match device\n" ); +return -ENODEV ; + } + soc = (struct mtk_soc_data *)match->data;
Hi Gustavo
I think you are fixing the wrong thing. The soc variable is unused. Also,
const struct of_device_id of_mtk_match[] = {
{ .compatible = "mediatek,mt2701-eth" },
{},
};So match->data is NULL. This code is all pointless and should be removed, rather than try to avoid a NULL pointer dereference which can not actually happen.
Andrew
Hi Andrew,
Quoting Andrew Lunn <[email protected]>:
Quoted 31 lines · Gustavo A. R. Silva
Quoted 16 lines
Check return value from call to
of_match_device()
in order to prevent a NULL pointer dereference.In case of NULL print error message and return
-ENODEVSigned-off-by Gustavo A. R. Silva <[email protected]>--- drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/net/ethernet/mediatek/mtk_eth_soc.c +0
-0drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_probe(struct platform_device *pdev) · line 2416
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +5
-0@@ -2408,6 +2408,11 @@ static int mtk_probe(struct platform_device *pdev)int i; match =of_match_device (of_mtk_match, &pdev->dev); +if (!match) { +dev_err (&pdev->dev,"failed to match device\n" ); +return -ENODEV ; + } + soc = (struct mtk_soc_data *)match->data;Hi Gustavo
I think you are fixing the wrong thing. The soc variable is unused. Also,
Quoted 9 lines
const struct of_device_id of_mtk_match[] = { { .compatible = "mediatek,mt2701-eth" }, {}, };So
match->datais NULL. This code is all pointless and should be removed, rather than try to avoid a NULL pointer dereference which can not actually happen.
You are right. Thank you for pointing it out. I'll remove that code and send a new patch shortly.
Thanks!
--
Gustavo A. R. Silva
PATCH 1/1 net: ethernet: mediatek: remove useless code in mtk_probe()
Remove useless local variables _match_, _soc_ and the code related.
Notice that
const struct of_device_id of_mtk_match[] = {
{ .compatible = "mediatek,mt2701-eth" },
{},
};So match->data is NULL.
--- drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 ----- 1 file changed, 5 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -5
@@ -2401,15 +2401,10 @@ static int mtk_probe(struct platform_device *pdev) {struct resource *res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );struct device_node *mac_np; -const struct of_device_id *match; -struct mtk_soc_data *soc;struct mtk_eth *eth;int err;int i; - match =of_match_device (of_mtk_match, &pdev->dev); - soc = (struct mtk_soc_data *)match->data; - eth =devm_kzalloc (&pdev->dev,sizeof (*eth),GFP_KERNEL );if (!eth)return -ENOMEM ;
Signature · 1 line
2.5.0
Re: [PATCH] net: ethernet: mediatek: remove useless code in mtk_probe()
Hi, Gustavo
It indeed is useless at the current time point.
but actually I will add new SoC support to the driver in the next week, which requires the variable match :-(
Sean
Quoted 37 lines · Gustavo A. R. Silva
Remove useless local variables _match_, _soc_ and the code related.
Notice that
const struct of_device_id of_mtk_match[] = { { .compatible = "mediatek,mt2701-eth" }, {}, };So
match->datais NULL.Suggested-by Andrew Lunn <[email protected]>Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/net/ethernet/mediatek/mtk_eth_soc.c | 5 ----- 1 file changed, 5 deletions(-)drivers/net/ethernet/mediatek/mtk_eth_soc.c +0
-5@@ -2401,15 +2401,10 @@ static int mtk_probe(struct platform_device *pdev) {struct resource *res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );struct device_node *mac_np; -const struct of_device_id *match; -struct mtk_soc_data *soc;struct mtk_eth *eth;int err;int i; - match =of_match_device (of_mtk_match, &pdev->dev); - soc = (struct mtk_soc_data *)match->data; - eth =devm_kzalloc (&pdev->dev,sizeof (*eth),GFP_KERNEL );if (!eth)return -ENOMEM ;
Re: [PATCH] net: ethernet: mediatek: remove useless code in mtk_probe()
In reply to Gustavo A. R. SilvaFrom: "Gustavo A. R. Silva" <[email protected]>
Date: Fri, 7 Jul 2017 15:23:34 -0500
Quoted 9 lines
Remove useless local variables _match_, _soc_ and the code related.
Notice that
const struct of_device_id of_mtk_match[] = { { .compatible = "mediatek,mt2701-eth" }, {}, };
Quoted 4 lines
So
match->datais NULL.Suggested-by Andrew Lunn <[email protected]>Signed-off-by Gustavo A. R. Silva <[email protected]>
Applied, thanks.
If someone needs this they can it back, in a less buggy form.