[v2,1/2] VMCI: Use struct_size() in kmalloc()

Message ID 20240105164001.2129796-1-harshit.m.mogalapalli@oracle.com
State New
Headers
Series [v2,1/2] VMCI: Use struct_size() in kmalloc() |

Commit Message

Harshit Mogalapalli Jan. 5, 2024, 4:39 p.m. UTC
  Use struct_size() instead of open coding.

Suggested-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
 drivers/misc/vmw_vmci/vmci_datagram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Gustavo A. R. Silva Jan. 5, 2024, 4:57 p.m. UTC | #1
On 1/5/24 10:39, Harshit Mogalapalli wrote:
> Use struct_size() instead of open coding.
> 
> Suggested-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

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);
  
Kees Cook Jan. 8, 2024, 10:28 p.m. UTC | #2
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 <gustavoars@kernel.org>
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>

Yeah, clear replacement.

Reviewed-by: Kees Cook <keescook@chromium.org>
  
Kees Cook Feb. 1, 2024, 6:06 p.m. UTC | #3
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,
  

Patch

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);