linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] w1: w1-gpio: Fix double-free of platform_data
@ 2017-03-25 17:06 Alexey Ignatov
  2017-04-29 16:38 ` Evgeniy Polyakov
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Ignatov @ 2017-03-25 17:06 UTC (permalink / raw)
  To: linux-kernel; +Cc: Evgeniy Polyakov, Alexey Ignatov

struct w1_gpio_platform_data was allocated using devres when using
device tree. Then it was assigned to dev.platform_data, which leaded
to double free on device removal by devres and by direct
kfree(platform_data) in platform_device_release)

Signed-off-by: Alexey Ignatov <lexszero@gmail.com>
---
 drivers/w1/masters/w1-gpio.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/w1/masters/w1-gpio.c b/drivers/w1/masters/w1-gpio.c
index dc5f6a2008bb..14e1a54f9f1f 100644
--- a/drivers/w1/masters/w1-gpio.c
+++ b/drivers/w1/masters/w1-gpio.c
@@ -95,15 +95,15 @@ static const struct of_device_id w1_gpio_dt_ids[] = {
 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
 #endif
 
-static int w1_gpio_probe_dt(struct platform_device *pdev)
+static struct w1_gpio_platform_data *w1_gpio_probe_dt(struct platform_device *pdev)
 {
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct w1_gpio_platform_data *pdata;
 	struct device_node *np = pdev->dev.of_node;
 	int gpio;
 
 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 	if (!pdata)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	if (of_get_property(np, "linux,open-drain", NULL))
 		pdata->is_open_drain = 1;
@@ -115,25 +115,23 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
 					"Failed to parse gpio property for data pin (%d)\n",
 					gpio);
 
-		return gpio;
+		return ERR_PTR(gpio);
 	}
 	pdata->pin = gpio;
 
 	gpio = of_get_gpio(np, 1);
 	if (gpio == -EPROBE_DEFER)
-		return gpio;
+		return ERR_PTR(gpio);
 	/* ignore other errors as the pullup gpio is optional */
 	pdata->ext_pullup_enable_pin = gpio;
 
 	gpio = of_get_named_gpio(np, "pu-gpios", 0);
 	if (gpio == -EPROBE_DEFER)
-		return gpio;
+		return ERR_PTR(gpio);
 	/* ignore other errors as the strong pullup gpio is optional */
 	pdata->strong_pullup_enable_pin = gpio;
 
-	pdev->dev.platform_data = pdata;
-
-	return 0;
+	return pdata;
 }
 
 static int w1_gpio_probe(struct platform_device *pdev)
@@ -143,12 +141,12 @@ static int w1_gpio_probe(struct platform_device *pdev)
 	int err;
 
 	if (of_have_populated_dt()) {
-		err = w1_gpio_probe_dt(pdev);
-		if (err < 0)
-			return err;
+		pdata = w1_gpio_probe_dt(pdev);
+		if (IS_ERR(pdata) < 0)
+			return PTR_ERR(pdata);
 	}
-
-	pdata = dev_get_platdata(&pdev->dev);
+	else
+		pdata = dev_get_platdata(&pdev->dev);
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "No configuration data\n");
@@ -229,8 +227,9 @@ static int w1_gpio_probe(struct platform_device *pdev)
 static int w1_gpio_remove(struct platform_device *pdev)
 {
 	struct w1_bus_master *master = platform_get_drvdata(pdev);
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
+	pr_debug("%s: pdev=%p, master=%p\n", __func__, pdev, master);
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(0);
 
@@ -244,7 +243,8 @@ static int w1_gpio_remove(struct platform_device *pdev)
 
 static int __maybe_unused w1_gpio_suspend(struct device *dev)
 {
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
+	struct w1_bus_master *master = dev_get_drvdata(dev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(0);
@@ -254,7 +254,8 @@ static int __maybe_unused w1_gpio_suspend(struct device *dev)
 
 static int __maybe_unused w1_gpio_resume(struct device *dev)
 {
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
+	struct w1_bus_master *master = dev_get_drvdata(dev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(1);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] w1: w1-gpio: Fix double-free of platform_data
  2017-03-25 17:06 [PATCH] w1: w1-gpio: Fix double-free of platform_data Alexey Ignatov
@ 2017-04-29 16:38 ` Evgeniy Polyakov
  0 siblings, 0 replies; 2+ messages in thread
From: Evgeniy Polyakov @ 2017-04-29 16:38 UTC (permalink / raw)
  To: Alexey Ignatov, linux-kernel

Hi Alexey

25.03.2017, 20:08, "Alexey Ignatov" <lexszero@gmail.com>:
> struct w1_gpio_platform_data was allocated using devres when using
> device tree. Then it was assigned to dev.platform_data, which leaded
> to double free on device removal by devres and by direct
> kfree(platform_data) in platform_device_release)

If this patch is still relevant, please add someone from device-tree into copy, I would think this bug
affect anyone and we see alot of bug reports since probe failure ends up with double free.

Also a nit:

> @@ -143,12 +141,12 @@ static int w1_gpio_probe(struct platform_device *pdev)
>          int err;
>
>          if (of_have_populated_dt()) {
> - err = w1_gpio_probe_dt(pdev);
> - if (err < 0)
> - return err;
> + pdata = w1_gpio_probe_dt(pdev);
> + if (IS_ERR(pdata) < 0)
> + return PTR_ERR(pdata);

IS_ERR() should not be used this way

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-04-29 16:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-25 17:06 [PATCH] w1: w1-gpio: Fix double-free of platform_data Alexey Ignatov
2017-04-29 16:38 ` Evgeniy Polyakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).