drm/i915/guc: Use struct_size() helper in kmalloc()
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 25 Jan 2022 18:07:26 +0000 (12:07 -0600)
committerTvrtko Ursulin <tvrtko.ursulin@intel.com>
Wed, 26 Jan 2022 10:19:08 +0000 (10:19 +0000)
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.

Also, address the following sparse warnings:
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c:792:23: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20220125180726.GA68646@embeddedor
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c

index de89d40..2f7fc87 100644 (file)
@@ -784,7 +784,7 @@ static struct ct_incoming_msg *ct_alloc_msg(u32 num_dwords)
 {
        struct ct_incoming_msg *msg;
 
-       msg = kmalloc(sizeof(*msg) + sizeof(u32) * num_dwords, GFP_ATOMIC);
+       msg = kmalloc(struct_size(msg, msg, num_dwords), GFP_ATOMIC);
        if (msg)
                msg->size = num_dwords;
        return msg;