2024-01-05 16:47:53

by Harshit Mogalapalli

[permalink] [raw]
Subject: [PATCH v2 1/2] VMCI: Use struct_size() in kmalloc()

Use struct_size() instead of open coding.

Suggested-by: Gustavo A. R. Silva <[email protected]>
Signed-off-by: Harshit Mogalapalli <[email protected]>
---
drivers/misc/vmw_vmci/vmci_datagram.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/vmw_vmci/vmci_datagram.c b/drivers/misc/vmw_vmci/vmci_datagram.c
index f50d22882476..ac6cb0c8d99b 100644
--- a/drivers/misc/vmw_vmci/vmci_datagram.c
+++ b/drivers/misc/vmw_vmci/vmci_datagram.c
@@ -224,8 +224,8 @@ static int dg_dispatch_as_host(u32 context_id, struct vmci_datagram *dg)
return VMCI_ERROR_NO_MEM;
}

- dg_info = kmalloc(sizeof(*dg_info) +
- (size_t) dg->payload_size, GFP_ATOMIC);
+ dg_info = kmalloc(struct_size(dg_info, msg_payload, dg->payload_size),
+ GFP_ATOMIC);
if (!dg_info) {
atomic_dec(&delayed_dg_host_queue_size);
vmci_resource_put(resource);
--
2.42.0



2024-01-05 16:57:37

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] VMCI: Use struct_size() in kmalloc()



On 1/5/24 10:39, Harshit Mogalapalli wrote:
> Use struct_size() instead of open coding.
>
> Suggested-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Harshit Mogalapalli <[email protected]>

Reviewed-by: Gustavo A. R. Silva <[email protected]>

Thanks!
--
Gustavo

> ---
> drivers/misc/vmw_vmci/vmci_datagram.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/misc/vmw_vmci/vmci_datagram.c b/drivers/misc/vmw_vmci/vmci_datagram.c
> index f50d22882476..ac6cb0c8d99b 100644
> --- a/drivers/misc/vmw_vmci/vmci_datagram.c
> +++ b/drivers/misc/vmw_vmci/vmci_datagram.c
> @@ -224,8 +224,8 @@ static int dg_dispatch_as_host(u32 context_id, struct vmci_datagram *dg)
> return VMCI_ERROR_NO_MEM;
> }
>
> - dg_info = kmalloc(sizeof(*dg_info) +
> - (size_t) dg->payload_size, GFP_ATOMIC);
> + dg_info = kmalloc(struct_size(dg_info, msg_payload, dg->payload_size),
> + GFP_ATOMIC);
> if (!dg_info) {
> atomic_dec(&delayed_dg_host_queue_size);
> vmci_resource_put(resource);

2024-01-08 22:28:43

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] VMCI: Use struct_size() in kmalloc()

On Fri, Jan 05, 2024 at 08:39:59AM -0800, Harshit Mogalapalli wrote:
> Use struct_size() instead of open coding.
>
> Suggested-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Harshit Mogalapalli <[email protected]>

Yeah, clear replacement.

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2024-02-01 18:39:45

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] VMCI: Use struct_size() in kmalloc()

On Fri, 05 Jan 2024 08:39:59 -0800, Harshit Mogalapalli wrote:
> Use struct_size() instead of open coding.
>
>

Applied to for-next/hardening, thanks!

[1/2] VMCI: Use struct_size() in kmalloc()
https://git.kernel.org/kees/c/e03d4910e6e4
[2/2] VMCI: Fix memcpy() run-time warning in dg_dispatch_as_host()
https://git.kernel.org/kees/c/19b070fefd0d

Take care,

--
Kees Cook