treewide: Use struct_size() for vmalloc()-family
authorKees Cook <keescook@chromium.org>
Tue, 8 May 2018 22:56:34 +0000 (15:56 -0700)
committerKees Cook <keescook@chromium.org>
Wed, 6 Jun 2018 18:15:43 +0000 (11:15 -0700)
This only finds one hit in the entire tree, but here's the Coccinelle:

// Directly refer to structure's field
@@
identifier alloc =~ "vmalloc|vzalloc";
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT))
+ alloc(struct_size(VAR, ELEMENT, COUNT))

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "vmalloc|vzalloc";
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]))
+ alloc(struct_size(VAR, ELEMENT, COUNT))

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "vmalloc|vzalloc";
expression SOMETHING, COUNT, ELEMENT;
@@

- alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT))
+ alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT))

Signed-off-by: Kees Cook <keescook@chromium.org>
drivers/gpu/drm/nouveau/nvkm/core/ramht.c

index ccba4ae73cc59210d387d64a765b3712b287c306..8162e3d2359cb7df9373736a2b1d63c021266ab5 100644 (file)
@@ -144,8 +144,7 @@ nvkm_ramht_new(struct nvkm_device *device, u32 size, u32 align,
        struct nvkm_ramht *ramht;
        int ret, i;
 
-       if (!(ramht = *pramht = vzalloc(sizeof(*ramht) +
-                                       (size >> 3) * sizeof(*ramht->data))))
+       if (!(ramht = *pramht = vzalloc(struct_size(ramht, data, (size >> 3)))))
                return -ENOMEM;
 
        ramht->device = device;