Skip to contentSkip to conversation

Fix NULL pointer dereference and use struct_size

PATCH In review Gustavo A. R. Silva 2 participants 4 messages Jan 5, 2019 – Jan 11, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 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.

Thanks

Changes in v2:
- Fix bug in patch 2/2 reported by kbuild test robot.

Gustavo A. R. Silva (2):
ARM: integrator: impd1: fix NULL pointer dereference
ARM: integrator: impd1: use struct_size() in devm_kzalloc()

 arch/arm/mach-integrator/impd1.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/2 ARM: integrator: impd1: fix 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 issue was detected with the help of Coccinelle.

Fixes 684284b64aae ("ARM: integrator: add MMCI device to IM-PD1")

---
Changes in v2:
- None.

 arch/arm/mach-integrator/impd1.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm/mach-integrator/impd1.c +3 -0
index a109f6482413..eb0149561be2 100644
@@ -392,6 +392,9 @@ static int __ref impd1_probe(struct lm_device *dev)
 			lookup = devm_kzalloc(&dev->dev,
 					      sizeof(*lookup) + 3 * sizeof(struct gpiod_lookup),
 					      GFP_KERNEL);
+			if (!lookup)
+				return -ENOMEM;
+
 			chipname = devm_kstrdup(&dev->dev, devname, GFP_KERNEL);
 			mmciname = kasprintf(GFP_KERNEL, "lm%x:00700", dev->id);
 			lookup->dev_id = mmciname;
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/2 ARM: integrator: impd1: 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;
    void *entry[];
};

instance = devm_kzalloc(dev, sizeof(struct foo) + sizeof(void *) * count, 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.

---
Changes in v2:
- Fix devm_kzalloc parameter reported by kbuild test robot.

 arch/arm/mach-integrator/impd1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-integrator/impd1.c +1 -1
index eb0149561be2..a0a1e2acdb5e 100644
@@ -390,7 +390,7 @@ static int __ref impd1_probe(struct lm_device *dev)
 			char *mmciname;
 
 			lookup = devm_kzalloc(&dev->dev,
-					      sizeof(*lookup) + 3 * sizeof(struct gpiod_lookup),
+					      struct_size(lookup, table, 3),
 					      GFP_KERNEL);
 			if (!lookup)
 				return -ENOMEM;
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v2 2/2] ARM: integrator: impd1: 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;
    void *entry[];
};

instance = devm_kzalloc(dev, sizeof(struct foo) + sizeof(void *) * count, 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.

---
Changes in v2:
- Fix devm_kzalloc parameter reported by kbuild test robot.

Patch applied.

Yours,
Linus Walleij