Re: [PATCH v2 2/2] VMCI: Fix memcpy() run-time warning in dg_dispatch_as_host()

From: Dan Carpenter
Date: Fri Jan 12 2024 - 00:35:55 EST


On Thu, Jan 11, 2024 at 10:13:44AM -0800, Kees Cook wrote:
> On Thu, Jan 11, 2024 at 10:15:40AM +0300, Dan Carpenter wrote:
> > On Wed, Jan 10, 2024 at 04:03:28PM -0800, Kees Cook wrote:
> > (I wrote a patch for this a few months back but didn't send it because
> > of the merge window. I forgot about it until now that we're in a merge
> > window again... :P)
>
> memcpy(&ivi->vlan, &bulletin->vlan, VLAN_HLEN);
>
> #define VLAN_HLEN 4
> ivi->vlan is u32
> bulletin has:
> u16 vlan;
> u8 vlan_padding[6];
>
> yeah, ew. Should it even be reading padding? i.e. should this be:
>
> ivi->vlan = bulletin->vlan << 16;
>

That seems reasonable. We don't care about endianness?

> ?
>
> Or should bulletin be:
>
> union {
> struct {
> u16 vlan;
> u8 vlan_padding[6];
> };
> struct {
> u32 vlan_header;
> u8 vlan_header_padding[4];
> };
> };
>
> with:
>
> ivi->vlan = bulletin->vlan_header;
>
> ?

My patch used a struct group.

struct_group(vlan_header,
u16 vlan;
u16 vlan_padding;
);

We don't need 6 bytes of padding, only 2. Using a shift seems like
possibly a better approach. Have to think about that.


>
> I've been finding that almost all memcpy()s and memset()s into non-array
> types are better just rewritten as a direct assignment. :P

That seems like a good rule of thumb, thanks.

regards,
dan carpenter