[PATCH] bus: imx-weim: fix to avoid potential NULL pointer dereference

Zhang Shurong posted 1 patch 2 years, 6 months ago
drivers/bus/imx-weim.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
[PATCH] bus: imx-weim: fix to avoid potential NULL pointer dereference
Posted by Zhang Shurong 2 years, 6 months ago
of_match_device() may fail and returns a NULL pointer.

Fix this by checking the return value of of_match_device.

Fixes: 3f98b6baad63 ("drivers: bus: imx-weim: Add support for i.MX1/21/25/27/31/35/50/51/53")
Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
---
 drivers/bus/imx-weim.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/bus/imx-weim.c b/drivers/bus/imx-weim.c
index 52a5d0447390..65402bb60c18 100644
--- a/drivers/bus/imx-weim.c
+++ b/drivers/bus/imx-weim.c
@@ -202,15 +202,19 @@ static int weim_timing_setup(struct device *dev, struct device_node *np,
 
 static int weim_parse_dt(struct platform_device *pdev)
 {
-	const struct of_device_id *of_id = of_match_device(weim_id_table,
-							   &pdev->dev);
-	const struct imx_weim_devtype *devtype = of_id->data;
+	const struct of_device_id *of_id;
+	const struct imx_weim_devtype *devtype;
 	int ret = 0, have_child = 0;
 	struct device_node *child;
 	struct weim_priv *priv;
 	void __iomem *base;
 	u32 reg;
 
+	of_id = of_match_device(weim_id_table, &pdev->dev);
+	if (!of_id)
+		return -EINVAL;
+
+	devtype = of_id->data;
 	if (devtype == &imx50_weim_devtype) {
 		ret = imx_weim_gpr_setup(pdev);
 		if (ret)
-- 
2.30.2
Re: [PATCH] bus: imx-weim: fix to avoid potential NULL pointer dereference
Posted by Fabio Estevam 2 years, 6 months ago
Hi Zhang,

On Sat, Jul 15, 2023 at 12:21 PM Zhang Shurong
<zhang_shurong@foxmail.com> wrote:
>
> of_match_device() may fail and returns a NULL pointer.
>
> Fix this by checking the return value of of_match_device.
>
> Fixes: 3f98b6baad63 ("drivers: bus: imx-weim: Add support for i.MX1/21/25/27/31/35/50/51/53")
> Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
> ---
>  drivers/bus/imx-weim.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/bus/imx-weim.c b/drivers/bus/imx-weim.c
> index 52a5d0447390..65402bb60c18 100644
> --- a/drivers/bus/imx-weim.c
> +++ b/drivers/bus/imx-weim.c
> @@ -202,15 +202,19 @@ static int weim_timing_setup(struct device *dev, struct device_node *np,
>
>  static int weim_parse_dt(struct platform_device *pdev)
>  {
> -       const struct of_device_id *of_id = of_match_device(weim_id_table,
> -                                                          &pdev->dev);
> -       const struct imx_weim_devtype *devtype = of_id->data;
> +       const struct of_device_id *of_id;
> +       const struct imx_weim_devtype *devtype;
>         int ret = 0, have_child = 0;
>         struct device_node *child;
>         struct weim_priv *priv;
>         void __iomem *base;
>         u32 reg;
>
> +       of_id = of_match_device(weim_id_table, &pdev->dev);
> +       if (!of_id)
> +               return -EINVAL;

If there is no match the driver will not probe in the first place.

I don't think this check is needed.
Re: [PATCH] bus: imx-weim: fix to avoid potential NULL pointer dereference
Posted by Zhang Shurong 2 years, 5 months ago
在 2023年7月16日星期日 CST 上午12:38:09,Fabio Estevam 写道:
> Hi Zhang,
> 
> On Sat, Jul 15, 2023 at 12:21 PM Zhang Shurong
> 
> <zhang_shurong@foxmail.com> wrote:
> > of_match_device() may fail and returns a NULL pointer.
> > 
> > Fix this by checking the return value of of_match_device.
> > 
> > Fixes: 3f98b6baad63 ("drivers: bus: imx-weim: Add support for
> > i.MX1/21/25/27/31/35/50/51/53") Signed-off-by: Zhang Shurong
> > <zhang_shurong@foxmail.com>
> > ---
> > 
> >  drivers/bus/imx-weim.c | 10 +++++++---
> >  1 file changed, 7 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/bus/imx-weim.c b/drivers/bus/imx-weim.c
> > index 52a5d0447390..65402bb60c18 100644
> > --- a/drivers/bus/imx-weim.c
> > +++ b/drivers/bus/imx-weim.c
> > @@ -202,15 +202,19 @@ static int weim_timing_setup(struct device *dev,
> > struct device_node *np,> 
> >  static int weim_parse_dt(struct platform_device *pdev)
> >  {
> > 
> > -       const struct of_device_id *of_id = of_match_device(weim_id_table,
> > -                                                          &pdev->dev);
> > -       const struct imx_weim_devtype *devtype = of_id->data;
> > +       const struct of_device_id *of_id;
> > +       const struct imx_weim_devtype *devtype;
> > 
> >         int ret = 0, have_child = 0;
> >         struct device_node *child;
> >         struct weim_priv *priv;
> >         void __iomem *base;
> >         u32 reg;
> > 
> > +       of_id = of_match_device(weim_id_table, &pdev->dev);
> > +       if (!of_id)
> > +               return -EINVAL;
> 
> If there is no match the driver will not probe in the first place.
> 
> I don't think this check is needed.

Hello Fabio,

I think we can make it happen by designing the platform device in a way that 
its name aligns with that of the driver. In such a scenario, when the driver 
is probed, the of_match_device function will return null. You can verify this 
functionality by reviewing the following function:

static int platform_match(struct device *dev, struct device_driver *drv)

Best regards,
Shurong
Re: [PATCH] bus: imx-weim: fix to avoid potential NULL pointer dereference
Posted by Fabio Estevam 2 years, 5 months ago
Hi Shurong,

On Mon, Aug 28, 2023 at 12:03 PM Zhang Shurong
<zhang_shurong@foxmail.com> wrote:

> Hello Fabio,
>
> I think we can make it happen by designing the platform device in a way that
> its name aligns with that of the driver. In such a scenario, when the driver
> is probed, the of_match_device function will return null. You can verify this
> functionality by reviewing the following function:
>
> static int platform_match(struct device *dev, struct device_driver *drv)

This cannot happen in practice as explained by Laurent in your other patch:
https://lore.kernel.org/dri-devel/20230828183748.GK14596@pendragon.ideasonboard.com/T/#t

Please do not introduce these checks.