• Gustavo A. R. Silva's avatar
    platform/chrome: cros_ec_vbc: Fix -Warray-bounds warnings · 59a9ccf1
    Gustavo A. R. Silva authored
    GCC-13 (and Clang) does not like having a partially allocated object,
    since it cannot reason about it for bounds checking.
    
    Notice that the compiler is legitimately complaining about accessing
    an object (params, in this case) for which not enough memory was
    allocated.
    
    The object is of size 20 bytes:
    
    struct ec_params_vbnvcontext {
    	uint32_t                   op;                   /*     0     4 */
    	uint8_t                    block[16];            /*     4    16 */
    
    	/* size: 20, cachelines: 1, members: 2 */
    	/* last cacheline: 20 bytes */
    };
    
    but only 16 bytes are allocated:
    
    sizeof(struct ec_response_vbnvcontext) == 16
    
    In this case, as only enough space for the op field is allocated,
    we can use an object of type uint32_t instead of a whole
    struct ec_params_vbnvcontext (for which not enough memory is
    allocated).
    
    Fix the following warning seen under GCC 13:
    drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
    drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
       36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
          |               ^~
    In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
    In function ‘kmalloc’,
        inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
    ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
      580 |                 return kmalloc_trace(
          |                        ^~~~~~~~~~~~~~
      581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
          |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      582 |                                 flags, size);
          |                                 ~~~~~~~~~~~~
    
    Link: https://github.com/KSPP/linux/issues/278Signed-off-by: default avatar"Gustavo A. R. Silva" <gustavoars@kernel.org>
    Reviewed-by: default avatarKees Cook <keescook@chromium.org>
    Link: https://lore.kernel.org/r/ZCTrutoN+9TiJM8u@workSigned-off-by: default avatarTzung-Bi Shih <tzungbi@kernel.org>
    59a9ccf1
cros_ec_vbc.c 3.68 KB