Skip to contentSkip to conversation

Fix NULL pointer dereference and use struct_size()

PATCH Applied Gustavo A. R. Silva 3 participants 8 messages Jan 22, 2019 – Jan 31, 2019 Multifunction devices (MFD)

lore
Share & tools
lore
#0 lore raw

cover Fix NULL pointer dereference and use struct_size()

Hi,

The first patch in this series fixes a potential NULL pointer dereference by adding a NULL check. A tag for stable has been added for this patch.

The second patch promotes the use of struct_size() in devm_kzalloc().

Both issues were detected with the help of Coccinelle.

Gustavo A. R. Silva (2):
mfd: sm501: Fix potential NULL pointer dereference
mfd: sm501: Use struct_size() in devm_kzalloc()

 drivers/mfd/sm501.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 mfd: sm501: Fix potential NULL pointer dereference

There is a potential NULL pointer dereference in case devm_kzalloc() fails and returns NULL.

Fix this by adding a NULL check on *lookup*

This bug was detected with the help of Coccinelle.

Fixes b2e63555592f ("i2c: gpio: Convert to use descriptors")
---
 drivers/mfd/sm501.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mfd/sm501.c +3 -0
index a530972c5a7e..e0173bf4b0dc 100644
@@ -1145,6 +1145,9 @@ static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
 	lookup = devm_kzalloc(&pdev->dev,
 			      sizeof(*lookup) + 3 * sizeof(struct gpiod_lookup),
 			      GFP_KERNEL);
+	if (!lookup)
+		return -ENOMEM;
+
 	lookup->dev_id = "i2c-gpio";
 	if (iic->pin_sda < 32)
 		lookup->table[0].chip_label = "SM501-LOW";
Signature · 1 line
2.20.1
Applied
#2 lore raw

Re: [PATCH 1/2] mfd: sm501: Fix potential NULL pointer dereference

Quoted 5 lines · Gustavo A. R. Silva

There is a potential NULL pointer dereference in case devm_kzalloc() fails and returns NULL.

Fix this by adding a NULL check on *lookup*

Quoted 8 lines

This bug was detected with the help of Coccinelle.

Fixes b2e63555592f ("i2c: gpio: Convert to use descriptors")
---
 drivers/mfd/sm501.c | 3 +++
 1 file changed, 3 insertions(+)

Applied, thanks.

Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#3 lore raw

Re: [PATCH 1/2] mfd: sm501: Fix potential NULL pointer dereference

Quoted 9 lines · Lee Jones
Quoted 7 lines · Gustavo A. R. Silva

There is a potential NULL pointer dereference in case devm_kzalloc() fails and returns NULL.

Fix this by adding a NULL check on *lookup*

This bug was detected with the help of Coccinelle.

Quoted 9 lines
Quoted 6 lines
Fixes b2e63555592f ("i2c: gpio: Convert to use descriptors")
---
 drivers/mfd/sm501.c | 3 +++
 1 file changed, 3 insertions(+)

Applied, thanks.

Thanks, Lee.

--
Gustavo

#4 lore raw

PATCH 2/2 mfd: sm501: Use struct_size() in devm_kzalloc()

In reply to Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    struct boo entry[];
};

instance = devm_kzalloc(dev, sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

---
 drivers/mfd/sm501.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/mfd/sm501.c +1 -2
index e0173bf4b0dc..d217debf382e 100644
@@ -1142,8 +1142,7 @@ static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
 		return -ENOMEM;
 
 	/* Create a gpiod lookup using gpiochip-local offsets */
-	lookup = devm_kzalloc(&pdev->dev,
-			      sizeof(*lookup) + 3 * sizeof(struct gpiod_lookup),
+	lookup = devm_kzalloc(&pdev->dev, struct_size(lookup, table, 3),
 			      GFP_KERNEL);
 	if (!lookup)
 		return -ENOMEM;
Signature · 1 line
2.20.1
Applied
#5 lore raw

Re: [PATCH 2/2] mfd: sm501: Use struct_size() in devm_kzalloc()

Quoted 14 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    struct boo entry[];
};

instance = devm_kzalloc(dev, sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

Quoted 8 lines

instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

---
 drivers/mfd/sm501.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Applied, thanks.

Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
Reviewed-by on the v1 series
#6 lore raw
In reply to Gustavo A. R. Silva
Quoted 5 lines · Gustavo A. R. Silva

The first patch in this series fixes a potential NULL pointer dereference by adding a NULL check. A tag for stable has been added for this patch.

The second patch promotes the use of struct_size() in devm_kzalloc().

Both patches:

Yours,
Linus Walleij

#7 lore raw
Quoted 12 lines · Linus Walleij
Quoted 5 lines · Gustavo A. R. Silva

The first patch in this series fixes a potential NULL pointer dereference by adding a NULL check. A tag for stable has been added for this patch.

The second patch promotes the use of struct_size() in devm_kzalloc().

Both patches:

Thanks, Linus.

--
Gustavo