Skip to contentSkip to conversation

pinctrl: check memory returned by devm_kasprintf()

PATCH Applied Claudiu Beznea 3 participants 9 messages Jun 15, 2023 – Jun 16, 2023

Subsystems (5)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover pinctrl: check memory returned by devm_kasprintf()

Hi,

While browsing some code I noticed that there are places where pointer returned by devm_kasprintf() or kasprintf() is not checked. Thus I've tooked the chance and fixed this (by updating kmerr.cocci script, changes published at [1]).

Thank you,
Claudiu Beznea

[1] https://lore.kernel.org/all/[email protected]/

Claudiu Beznea (3):
pinctrl: mcp23s08: check return value of {devm_}kasprintf()
pinctrl: microchip-sgpio: check return value of devm_kasprintf()
pinctrl: at91-pio4: check return value of devm_kasprintf()

 drivers/pinctrl/pinctrl-at91-pio4.c       | 2 ++
 drivers/pinctrl/pinctrl-mcp23s08_spi.c    | 3 +++
 drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++
 3 files changed, 8 insertions(+)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 pinctrl: mcp23s08: check return value of devm_kasprintf()

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Fixes 0f04a81784fe ("pinctrl: mcp23s08: Split to three parts: core, I²C, SPI")
---
 drivers/pinctrl/pinctrl-mcp23s08_spi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pinctrl/pinctrl-mcp23s08_spi.c +3 -0
index 9ae10318f6f3..4013ffbfa282 100644
@@ -119,6 +119,9 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
 		return -EINVAL;
 	}
 
+	if (!name || !mcp->chip.label)
+		return -ENOMEM;
+
 	copy = devm_kmemdup(dev, config, sizeof(*config), GFP_KERNEL);
 	if (!copy)
 		return -ENOMEM;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] pinctrl: mcp23s08: check return value of devm_kasprintf()

Quoted 3 lines · Claudiu Beznea

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Not that this may happen, but okay.

...

drivers/pinctrl/pinctrl-mcp23s08_spi.c static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev, · line 123

Quoted 6 lines
drivers/pinctrl/pinctrl-mcp23s08_spi.c +2 -0
@@ -119,6 +119,9 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
 		return -EINVAL;
 	}
 
+	if (!name || !mcp->chip.label)
+		return -ENOMEM;

I prefer to see 4 independent checks for each of the devm_kasprintf() calls.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH 2/3 pinctrl: microchip-sgpio: check return value of devm_kasprintf()

In reply to Claudiu Beznea

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Fixes 7e5ea974e61c ("pinctrl: pinctrl-microchip-sgpio: Add pinctrl driver for Microsemi Serial GPIO")
---
 drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pinctrl/pinctrl-microchip-sgpio.c +3 -0
index 4794602316e7..666d8b7cdbad 100644
@@ -818,6 +818,9 @@ static int microchip_sgpio_register_bank(struct device *dev,
 	pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
 					 dev_name(dev),
 					 bank->is_input ? "in" : "out");
+	if (!pctl_desc->name)
+		return -ENOMEM;
+
 	pctl_desc->pctlops = &sgpio_pctl_ops;
 	pctl_desc->pmxops = &sgpio_pmx_ops;
 	pctl_desc->confops = &sgpio_confops;
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] pinctrl: microchip-sgpio: check return value of devm_kasprintf()

Quoted 3 lines · Claudiu Beznea

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Quoted 23 lines
Fixes 7e5ea974e61c ("pinctrl: pinctrl-microchip-sgpio: Add pinctrl driver for Microsemi Serial GPIO")
---
 drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pinctrl/pinctrl-microchip-sgpio.c +3 -0
index 4794602316e7..666d8b7cdbad 100644
@@ -818,6 +818,9 @@ static int microchip_sgpio_register_bank(struct device *dev,
 	pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
 					 dev_name(dev),
 					 bank->is_input ? "in" : "out");
+	if (!pctl_desc->name)
+		return -ENOMEM;
+
 	pctl_desc->pctlops = &sgpio_pctl_ops;
 	pctl_desc->pmxops = &sgpio_pmx_ops;
 	pctl_desc->confops = &sgpio_confops;
Signature · 1 line
2.34.1
Signature · 9 lines
With Best Regards,
Andy Shevchenko



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

PATCH 3/3 pinctrl: at91-pio4: check return value of devm_kasprintf()

In reply to Claudiu Beznea

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Fixes 776180848b57 ("pinctrl: introduce driver for Atmel PIO4 controller")

Depends-on: 1c4e5c470a56 ("pinctrl: at91: use devm_kasprintf() to avoid potential leaks")
Depends-on: 5a8f9cf269e8 ("pinctrl: at91-pio4: use proper format specifier for unsigned int")

---
 drivers/pinctrl/pinctrl-at91-pio4.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pinctrl/pinctrl-at91-pio4.c +2 -0
index e40487be2038..fc7d7155bb8d 100644
@@ -1146,6 +1146,8 @@ static int atmel_pinctrl_probe(struct platform_device *pdev)
 		/* Pin naming convention: P(bank_name)(bank_pin_number). */
 		pin_desc[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "P%c%u",
 						  bank + 'A', line);
+		if (!pin_desc[i].name)
+			return -ENOMEM;
 
 		group->name = group_names[i] = pin_desc[i].name;
 		group->pin = pin_desc[i].number;
Signature · 1 line
2.34.1
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] pinctrl: at91-pio4: check return value of devm_kasprintf()

Quoted 3 lines · Claudiu Beznea

devm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).

Quoted 24 lines
Fixes 776180848b57 ("pinctrl: introduce driver for Atmel PIO4 controller")

Depends-on: 1c4e5c470a56 ("pinctrl: at91: use devm_kasprintf() to avoid potential leaks")
Depends-on: 5a8f9cf269e8 ("pinctrl: at91-pio4: use proper format specifier for unsigned int")

---
 drivers/pinctrl/pinctrl-at91-pio4.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pinctrl/pinctrl-at91-pio4.c +2 -0
index e40487be2038..fc7d7155bb8d 100644
@@ -1146,6 +1146,8 @@ static int atmel_pinctrl_probe(struct platform_device *pdev)
 		/* Pin naming convention: P(bank_name)(bank_pin_number). */
 		pin_desc[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "P%c%u",
 						  bank + 'A', line);
+		if (!pin_desc[i].name)
+			return -ENOMEM;
 
 		group->name = group_names[i] = pin_desc[i].name;
 		group->pin = pin_desc[i].number;
Signature · 1 line
2.34.1
Signature · 9 lines
With Best Regards,
Andy Shevchenko



_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#7 lore raw
In reply to Claudiu Beznea
Quoted 6 lines · Claudiu Beznea

Hi,

While browsing some code I noticed that there are places where pointer returned by devm_kasprintf() or kasprintf() is not checked. Thus I've tooked the chance and fixed this (by updating kmerr.cocci script, changes published at [1]).

Thank you for fixing this!
Individual patches are commented by me.

Quoted 15 lines

[1] https://lore.kernel.org/all/[email protected]/

Claudiu Beznea (3):
pinctrl: mcp23s08: check return value of {devm_}kasprintf()
pinctrl: microchip-sgpio: check return value of devm_kasprintf()
pinctrl: at91-pio4: check return value of devm_kasprintf()

 drivers/pinctrl/pinctrl-at91-pio4.c       | 2 ++
 drivers/pinctrl/pinctrl-mcp23s08_spi.c    | 3 +++
 drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++
 3 files changed, 8 insertions(+)
Signature · 1 line
2.34.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Claudiu Beznea

Hi Claudiu,

thanks for your patches!

Quoted 4 lines · Claudiu Beznea

While browsing some code I noticed that there are places where pointer returned by devm_kasprintf() or kasprintf() is not checked. Thus I've tooked the chance and fixed this (by updating kmerr.cocci script, changes published at [1]).

I applied patch 2/3 and 3/3 you only need to resent the first patch after looking into the comments from Andy.

Yours,
Linus Walleij