From: Alexey Ignatov <lexszero@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Evgeniy Polyakov <zbr@ioremap.net>, Alexey Ignatov <lexszero@gmail.com>
Subject: [PATCH] w1: w1-gpio: Fix double-free of platform_data
Date: Sat, 25 Mar 2017 19:06:45 +0200 [thread overview]
Message-ID: <20170325170645.16073-1-lexszero@gmail.com> (raw)
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
next reply other threads:[~2017-03-25 17:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-25 17:06 Alexey Ignatov [this message]
2017-04-29 16:38 ` [PATCH] w1: w1-gpio: Fix double-free of platform_data Evgeniy Polyakov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170325170645.16073-1-lexszero@gmail.com \
--to=lexszero@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=zbr@ioremap.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox