virtchnl: fix fake 1-elem arrays in structs allocated as `nents + 1` - 1
authorAlexander Lobakin <aleksander.lobakin@intel.com>
Fri, 28 Jul 2023 15:52:05 +0000 (17:52 +0200)
committerTony Nguyen <anthony.l.nguyen@intel.com>
Wed, 16 Aug 2023 15:56:56 +0000 (08:56 -0700)
commitdd2e84bb3804103ad1c26a21deb4b35b0e166746
treebedd61883fc4fe94c276ce356399187ba6ea3f04
parent950fe35831af0c1f9d87d4105843c3b7f1fbf09b
virtchnl: fix fake 1-elem arrays in structs allocated as `nents + 1` - 1

The two most problematic virtchnl structures are virtchnl_rss_key and
virtchnl_rss_lut. Their "flex" arrays have the type of u8, thus, when
allocating / checking, the actual size is calculated as `sizeof +
nents - 1 byte`. But their sizeof() is not 1 byte larger than the size
of such structure with proper flex array, it's two bytes larger due to
the padding. That said, their size is always 1 byte larger unless
there are no tail elements -- then it's +2 bytes.
Add virtchnl_struct_size() macro which will handle this case (and later
other cases as well). Make its calling conv the same as we call
struct_size() to allow it to be drop-in, even though it's unlikely to
become possible to switch to generic API. The macro will calculate a
proper size of a structure with a flex array at the end, so that it
becomes transparent for the compilers, but add the difference from the
old values, so that the real size of sorta-ABI-messages doesn't change.
Use it on the allocation side in IAVF and the receiving side (defined
as static inline in virtchnl.h) for the mentioned two structures.

Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Tested-by: Rafal Romanowski <rafal.romanowski@intel.com>
Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
drivers/net/ethernet/intel/iavf/iavf_virtchnl.c
include/linux/avf/virtchnl.h