Skip to contentSkip to conversation

drm/imx: fix memory leak in imx_pd_bind

PATCH In review Navid Emamdoost 4 participants 15 messages Oct 4, 2019 – Nov 22, 2019

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

PATCH 1/1 drm/imx: fix memory leak in imx_pd_bind

In imx_pd_bind, the duplicated memory for imxpd->edid via kmemdup should be released in drm_of_find_panel_or_bridge or imx_pd_register fail.

Fixes ebc944613567 ("drm: convert drivers to use drm_of_find_panel_or_bridge")
Fixes 19022aaae677 ("staging: drm/imx: Add parallel display support")
---
 drivers/gpu/drm/imx/parallel-display.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/imx/parallel-display.c +6 -2
index e7ce17503ae1..9522d2cb0ad5 100644
@@ -227,14 +227,18 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 
 	/* port@1 is the output port */
 	ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge);
-	if (ret && ret != -ENODEV)
+	if (ret && ret != -ENODEV) {
+		kfree(imxpd->edid);
 		return ret;
+	}
 
 	imxpd->dev = dev;
 
 	ret = imx_pd_register(drm, imxpd);
-	if (ret)
+	if (ret) {
+		kfree(imxpd->edid);
 		return ret;
+	}
 
 	dev_set_drvdata(dev, imxpd);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines

In imx_pd_bind, the duplicated memory for imxpd->edid via kmemdup should be released in drm_of_find_panel_or_bridge or imx_pd_register fail.

Please improve this change description.

drivers/gpu/drm/imx/parallel-display.c static int imx_pd_bind(struct device *dev, struct device *master, void *data) · line 235

Quoted 12 lines

+++ b/drivers/gpu/drm/imx/parallel-display.c

drivers/gpu/drm/imx/parallel-display.c +3 -1
@@ -227,14 +227,18 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 
 	/* port@1 is the output port */
 	ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge);
-	if (ret && ret != -ENODEV)
+	if (ret && ret != -ENODEV) {
+		kfree(imxpd->edid);
 		return ret;
+	}
 
 	imxpd->dev = dev;

Please use a jump target here instead of adding duplicate source code for the completion of exception handling.

if (ret && ret != -ENODEV)
- return ret;
+ goto free_edid;

…

+free_edid:
+ kfree(imxpd->edid);
+ return ret;

Regards,
Markus

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw

Review

Quoted 3 lines
(quoted diff) +3 -0
+free_edid:
+ 	kfree(imxpd->edid);
+ 	return ret;

I have taken another look at this change idea.
Can the function call “devm_kfree(dev, imxpd)” become relevant also at this place?

Would you like to combine it with the update suggestion “Fix error handling for a kmemdup() call in imx_pd_bind()”?
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/patchwork/patch/1138912/
https://lkml.org/lkml/2019/10/12/87

Regards,
Markus

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw

No, that is not correct! You should not try to free imxpd here as it is a resource-managed allocation via devm_kzalloc(). It means memory allocated with this function is automatically freed on driver detach. So, this patch introduces a double-free.

Quoted 17 lines · Markus Elfring

Review

Quoted 4 lines
(quoted diff) +3 -0
+free_edid:
+     kfree(imxpd->edid);
+     return ret;

I have taken another look at this change idea.
Can the function call “devm_kfree(dev, imxpd)” become relevant also at this place?

Would you like to combine it with the update suggestion “Fix error handling for a kmemdup() call in imx_pd_bind()”?
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/patchwork/patch/1138912/
https://lkml.org/lkml/2019/10/12/87

Regards,
Markus

Signature · 1 line
Navid.
#4 lore raw

Re: drm/imx: Checking a kmemdup() call in imx_pd_bind()

In reply to Navid Emamdoost

I have taken another look also at the implementation of the function “imx_pd_bind”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/imx/parallel-display.c?id=43b815c6a8e7dbccb5b8bd9c4b099c24bc22d135#n197
https://elixir.bootlin.com/linux/v5.4-rc1/source/drivers/gpu/drm/imx/parallel-display.c#L197

Now I find an unchecked call of the function “kmemdup” suspicious. Will this detail trigger further software development considerations?

Regards,
Markus

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#5 lore raw

Re: drm/imx: Checking a kmemdup() call in imx_pd_bind()

Hi Markus,

I agree with you, kmemdup may fail so a null check seems necessary there.

Quoted 10 lines · Markus Elfring

I have taken another look also at the implementation of the function “imx_pd_bind”.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/imx/parallel-display.c?id=43b815c6a8e7dbccb5b8bd9c4b099c24bc22d135#n197
https://elixir.bootlin.com/linux/v5.4-rc1/source/drivers/gpu/drm/imx/parallel-display.c#L197

Now I find an unchecked call of the function “kmemdup” suspicious. Will this detail trigger further software development considerations?

Regards,
Markus

Signature · 6 lines
Navid.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#6 lore raw

Re: drm/imx: Checking a kmemdup() call in imx_pd_bind()

Quoted 1 line

I agree with you, kmemdup may fail so a null check seems necessary there.

Would this place (and similar ones) be pointed out for further considerations also by the source code analysis tool which your software research group seems to be developing?
https://github.com/umnsec/cheq/

Regards,
Markus

#7 lore raw

cover drm/imx: Adjustments for two functions

In reply to Navid Emamdoost

From: Markus Elfring <[email protected]>
Date: Sat, 12 Oct 2019 10:45:45 +0200

Two update suggestions were taken into account
from static source code analysis.

Markus Elfring (2):
Fix error handling for a kmemdup() call in imx_pd_bind() Fix error handling for a kmemdup() call in imx_ldb_panel_ddc()

 drivers/gpu/drm/imx/imx-ldb.c          | 2 ++
 drivers/gpu/drm/imx/parallel-display.c | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

--
2.23.0

#8 lore raw

PATCH 1/2 drm/imx: Fix error handling for a kmemdup() call in imx_pd_bind()

From: Markus Elfring <[email protected]>
Date: Sat, 12 Oct 2019 10:30:21 +0200

The return value from a call of the function “kmemdup” was not checked in this function implementation. Thus add the corresponding error handling.

Fixes 19022aaae677dfa171a719e9d1ff04823ce65a65 ("staging: drm/imx: Add parallel display support")
---
 drivers/gpu/drm/imx/parallel-display.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/parallel-display.c +6 -1
index 35518e5de356..39c4798f56b6 100644
@@ -210,8 +210,13 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 		return -ENOMEM;
 
 	edidp = of_get_property(np, "edid", &imxpd->edid_len);
-	if (edidp)
+	if (edidp) {
 		imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
+		if (!imxpd->edid) {
+			devm_kfree(dev, imxpd);
+			return -ENOMEM;
+		}
+	}
 
 	ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
 	if (!ret) {

--
2.23.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#9 lore raw

Re: [PATCH 1/2] drm/imx: Fix error handling for a kmemdup() call in imx_pd_bind()

Quoted 18 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 12 Oct 2019 10:30:21 +0200

The return value from a call of the function “kmemdup” was not checked in this function implementation. Thus add the corresponding error handling.

Fixes 19022aaae677dfa171a719e9d1ff04823ce65a65 ("staging: drm/imx: Add parallel display support")
---
 drivers/gpu/drm/imx/parallel-display.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/parallel-display.c +0 -0
index 35518e5de356..39c4798f56b6 100644

drivers/gpu/drm/imx/parallel-display.c static int imx_pd_bind(struct device *dev, struct device *master, void *data) · line 216

Quoted 8 lines
drivers/gpu/drm/imx/parallel-display.c +3 -1
@@ -210,8 +210,13 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
                return -ENOMEM;
 
        edidp = of_get_property(np, "edid", &imxpd->edid_len);
-       if (edidp)
+       if (edidp) {
                imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
+               if (!imxpd->edid) {
+                       devm_kfree(dev, imxpd);

You should not try to free imxpd here as it is a resource-managed allocation via devm_kzalloc(). It means memory allocated with this function is
automatically freed on driver detach. So, this patch introduces a double-free.

Quoted 9 lines
drivers/gpu/drm/imx/parallel-display.c +3 -1
+                       return -ENOMEM;
+               }
+       }
 
        ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
        if (!ret) {
--

2.23.0

Signature · 1 line
Navid.
#10 lore raw

Re: [PATCH 1/2] drm/imx: Fix error handling for a kmemdup() call in imx_pd_bind()

Quoted 21 lines · Navid Emamdoost
Quoted 20 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 12 Oct 2019 10:30:21 +0200

The return value from a call of the function “kmemdup” was not checked in this function implementation. Thus add the corresponding error handling.

Fixes 19022aaae677dfa171a719e9d1ff04823ce65a65 ("staging: drm/imx: Add parallel display support")
---
 drivers/gpu/drm/imx/parallel-display.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/parallel-display.c +0 -0
index 35518e5de356..39c4798f56b6 100644
@@ -210,8 +210,13 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
                return -ENOMEM;
 
Quoted 11 lines

drivers/gpu/drm/imx/parallel-display.c static int imx_pd_bind(struct device *dev, struct device *master, void *data) · line 216

Quoted 6 lines
drivers/gpu/drm/imx/parallel-display.c +3 -1
        edidp = of_get_property(np, "edid", &imxpd->edid_len);
-       if (edidp)
+       if (edidp) {
                imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
+               if (!imxpd->edid) {
+                       devm_kfree(dev, imxpd);

You should not try to free imxpd here as it is a resource-managed allocation via devm_kzalloc(). It means memory allocated with this function is
automatically freed on driver detach. So, this patch introduces a double-free.

No, it's not double freed since the proposed code frees it with a devm function, removing it from the list of things to free later. One can wonder why the free has to be made apparent, though.

julia

Quoted 15 lines
Quoted 10 lines
drivers/gpu/drm/imx/parallel-display.c +3 -1
+                       return -ENOMEM;
+               }
+       }
 
        ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
        if (!ret) {
--

2.23.0

--
Navid.

#11 lore raw

PATCH 2/2 drm/imx: Fix error handling for a kmemdup() call in imx_ldb_panel_ddc()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 12 Oct 2019 10:33:47 +0200

The return value from a call of the function “kmemdup” was not checked in this function implementation. Thus add the corresponding error handling.

This issue was detected by using the Coccinelle software.

Fixes dc80d7038883feca2abd08975165bc0d83c84762 ("drm/imx-ldb: Add support to drm-bridge")
---
 drivers/gpu/drm/imx/imx-ldb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/imx/imx-ldb.c +2 -0
index 208069faf183..801a2265dd11 100644
@@ -569,6 +569,8 @@ static int imx_ldb_panel_ddc(struct device *dev,
 			channel->edid = kmemdup(edidp,
 						channel->edid_len,
 						GFP_KERNEL);
+			if (!channel->edid)
+				return -ENOMEM;
 		} else if (!channel->panel) {
 			/* fallback to display-timings node */
 			ret = of_get_drm_display_mode(child,

--
2.23.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#12 lore raw
In reply to Navid Emamdoost
Quoted 8 lines · Navid Emamdoost

In imx_pd_bind, the duplicated memory for imxpd->edid via kmemdup should be released in drm_of_find_panel_or_bridge or imx_pd_register fail.

Fixes ebc944613567 ("drm: convert drivers to use drm_of_find_panel_or_bridge")
Fixes 19022aaae677 ("staging: drm/imx: Add parallel display support")

---

Would you please review this patch?

Thanks,

Quoted 31 lines
 drivers/gpu/drm/imx/parallel-display.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/imx/parallel-display.c +6 -3
index e7ce17503ae1..9522d2cb0ad5 100644
@@ -227,14 +227,18 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 
        /* port@1 is the output port */
        ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge);
-       if (ret && ret != -ENODEV)
+       if (ret && ret != -ENODEV) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        imxpd->dev = dev;
 
        ret = imx_pd_register(drm, imxpd);
-       if (ret)
+       if (ret) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        dev_set_drvdata(dev, imxpd);
 
--

2.17.1

Signature · 1 line
Navid.
#13 lore raw

Hi Navid,

Quoted 6 lines · Navid Emamdoost
Quoted 4 lines · Navid Emamdoost

In imx_pd_bind, the duplicated memory for imxpd->edid via kmemdup should be released in drm_of_find_panel_or_bridge or imx_pd_register fail.

Quoted 8 lines
Quoted 4 lines
Fixes ebc944613567 ("drm: convert drivers to use drm_of_find_panel_or_bridge")
Fixes 19022aaae677 ("staging: drm/imx: Add parallel display support")

---

Would you please review this patch?

Thanks,

I currently work on the drm/imx driver(s) to avoid errors like [1]. Hopefully I have a working version till next week. There I fixed this issue by using the devm_kmemdup() and dropped the explicit kfree() within unbind().

[1] https://www.spinics.net/lists/dri-devel/msg189388.html

Regards,
Marco

Quoted 38 lines
Quoted 32 lines
 drivers/gpu/drm/imx/parallel-display.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/imx/parallel-display.c +6 -3
index e7ce17503ae1..9522d2cb0ad5 100644
@@ -227,14 +227,18 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 
        /* port@1 is the output port */
        ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge);
-       if (ret && ret != -ENODEV)
+       if (ret && ret != -ENODEV) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        imxpd->dev = dev;
 
        ret = imx_pd_register(drm, imxpd);
-       if (ret)
+       if (ret) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        dev_set_drvdata(dev, imxpd);
 
--

2.17.1

Signature · 1 line
Navid.
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#14 lore raw

Thanks for the update.

Quoted 73 lines · Marco Felsch

Hi Navid,

Quoted 6 lines · Navid Emamdoost
Quoted 4 lines · Navid Emamdoost

In imx_pd_bind, the duplicated memory for imxpd->edid via kmemdup should be released in drm_of_find_panel_or_bridge or imx_pd_register fail.

Quoted 8 lines
Quoted 4 lines
Fixes ebc944613567 ("drm: convert drivers to use drm_of_find_panel_or_bridge")
Fixes 19022aaae677 ("staging: drm/imx: Add parallel display support")

---

Would you please review this patch?

Thanks,

I currently work on the drm/imx driver(s) to avoid errors like [1]. Hopefully I have a working version till next week. There I fixed this issue by using the devm_kmemdup() and dropped the explicit kfree() within unbind().

[1] https://www.spinics.net/lists/dri-devel/msg189388.html

Regards,
Marco

Quoted 29 lines
Quoted 29 lines
 drivers/gpu/drm/imx/parallel-display.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/imx/parallel-display.c +6 -2
index e7ce17503ae1..9522d2cb0ad5 100644
@@ -227,14 +227,18 @@ static int imx_pd_bind(struct device *dev, struct device *master, void *data)
 
        /* port@1 is the output port */
        ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel, &imxpd->bridge);
-       if (ret && ret != -ENODEV)
+       if (ret && ret != -ENODEV) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        imxpd->dev = dev;
 
        ret = imx_pd_register(drm, imxpd);
-       if (ret)
+       if (ret) {
+               kfree(imxpd->edid);
                return ret;
+       }
 
        dev_set_drvdata(dev, imxpd);
 
Quoted 9 lines
Quoted 3 lines

--
2.17.1

--
Navid.

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 1 line
Navid.