selftests: i915: Use struct_size() helper in kmalloc()
authorluoqing <luoqing@kylinos.cn>
Sat, 8 Feb 2025 01:35:39 +0000 (09:35 +0800)
committerAndi Shyti <andi.shyti@linux.intel.com>
Mon, 10 Feb 2025 23:26:02 +0000 (00:26 +0100)
Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Signed-off-by: luoqing <luoqing@kylinos.cn>
Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com>
Signed-off-by: Andi Shyti <andi.shyti@linux.intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250208013539.3586855-1-l1138897701@163.com
drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c

index 075657018739220393d1a8db24f2581bf5717630..5cd58e0f0dcf6499aba7aa5fe7f2d6e8a1270db4 100644 (file)
@@ -103,8 +103,7 @@ static struct dma_buf *mock_dmabuf(int npages)
        struct dma_buf *dmabuf;
        int i;
 
-       mock = kmalloc(sizeof(*mock) + npages * sizeof(struct page *),
-                      GFP_KERNEL);
+       mock = kmalloc(struct_size(mock, pages, npages), GFP_KERNEL);
        if (!mock)
                return ERR_PTR(-ENOMEM);