pinctrl: check memory returned by devm_kasprintf()
Subsystems (5)
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
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).
--- drivers/pinctrl/pinctrl-mcp23s08_spi.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/pinctrl/pinctrl-mcp23s08_spi.c +3 -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 ; + copy =devm_kmemdup (dev, config,sizeof (*config),GFP_KERNEL );if (!copy)return -ENOMEM ;
Signature · 1 line
2.34.1
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.cocciscript).
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
PATCH 2/3 pinctrl: microchip-sgpio: check return value of devm_kasprintf()
In reply to Claudiu Bezneadevm_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).
--- drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/pinctrl/pinctrl-microchip-sgpio.c +3 -0
@@ -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
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.cocciscript).
Quoted 23 lines
Fixes 7e5ea974e61c ("pinctrl: pinctrl-microchip-sgpio: Add pinctrl driver for Microsemi Serial GPIO")Signed-off-by Claudiu Beznea <[email protected]>--- drivers/pinctrl/pinctrl-microchip-sgpio.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pinctrl/pinctrl-microchip-sgpio.c +3
-0@@ -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
PATCH 3/3 pinctrl: at91-pio4: check return value of devm_kasprintf()
In reply to Claudiu Bezneadevm_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).
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
@@ -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
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.cocciscript).
Quoted 24 lines
Fixes 776180848b57 ("pinctrl: introduce driver for Atmel PIO4 controller")Depends-on:
1c4e5c470a56("pinctrl: at91: usedevm_kasprintf()to avoid potential leaks")
Depends-on:5a8f9cf269e8("pinctrl: at91-pio4: use proper format specifier for unsigned int")Signed-off-by Claudiu Beznea <[email protected]>--- drivers/pinctrl/pinctrl-at91-pio4.c | 2 ++ 1 file changed, 2 insertions(+)drivers/pinctrl/pinctrl-at91-pio4.c +2
-0@@ -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
Quoted 6 lines · Claudiu Beznea
Hi,
While browsing some code I noticed that there are places where pointer returned by
devm_kasprintf()orkasprintf()is not checked. Thus I've tooked the chance and fixed this (by updatingkmerr.cocciscript, 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 ofdevm_kasprintf()
pinctrl: at91-pio4: check return value ofdevm_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
Linus Walleij
AppliedHi 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()orkasprintf()is not checked. Thus I've tooked the chance and fixed this (by updatingkmerr.cocciscript, 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