diff mbox series

[next] Drivers: hv: vmbus: Use struct_size() helper in kmalloc()

Message ID 20220125180131.GA67746@embeddedor (mailing list archive)
State Mainlined
Commit a70d298c44592b95ae962e698c434f2b04e78805
Headers show
Series [next] Drivers: hv: vmbus: Use struct_size() helper in kmalloc() | expand

Commit Message

Gustavo A. R. Silva Jan. 25, 2022, 6:01 p.m. UTC
Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/hv/vmbus_drv.c:1132:31: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/hv/vmbus_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Michael Kelley (LINUX) Jan. 27, 2022, 4:01 p.m. UTC | #1
From: Gustavo A. R. Silva <gustavoars@kernel.org> Sent: Tuesday, January 25, 2022 10:02 AM
> 
> Make use of the struct_size() helper instead of an open-coded version,
> in order to avoid any potential type mistakes or integer overflows that,
> in the worst scenario, could lead to heap overflows.
> 
> Also, address the following sparse warnings:
> drivers/hv/vmbus_drv.c:1132:31: warning: using sizeof on a flexible structure
> 
> Link: https://github.com/KSPP/linux/issues/174
> ---
>  drivers/hv/vmbus_drv.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> index 17bf55fe3169..cd193456cd84 100644
> --- a/drivers/hv/vmbus_drv.c
> +++ b/drivers/hv/vmbus_drv.c
> @@ -1129,7 +1129,7 @@ void vmbus_on_msg_dpc(unsigned long data)
>  	}
> 
>  	if (entry->handler_type	== VMHT_BLOCKING) {
> -		ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
> +		ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
>  		if (ctx == NULL)
>  			return;
> 
> --
> 2.27.0

Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Wei Liu Feb. 3, 2022, 12:42 p.m. UTC | #2
On Thu, Jan 27, 2022 at 04:01:28PM +0000, Michael Kelley (LINUX) wrote:
> From: Gustavo A. R. Silva <gustavoars@kernel.org> Sent: Tuesday, January 25, 2022 10:02 AM
> > 
> > Make use of the struct_size() helper instead of an open-coded version,
> > in order to avoid any potential type mistakes or integer overflows that,
> > in the worst scenario, could lead to heap overflows.
> > 
> > Also, address the following sparse warnings:
> > drivers/hv/vmbus_drv.c:1132:31: warning: using sizeof on a flexible structure
> > 
> > Link: https://github.com/KSPP/linux/issues/174
> > ---
> >  drivers/hv/vmbus_drv.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> > index 17bf55fe3169..cd193456cd84 100644
> > --- a/drivers/hv/vmbus_drv.c
> > +++ b/drivers/hv/vmbus_drv.c
> > @@ -1129,7 +1129,7 @@ void vmbus_on_msg_dpc(unsigned long data)
> >  	}
> > 
> >  	if (entry->handler_type	== VMHT_BLOCKING) {
> > -		ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
> > +		ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
> >  		if (ctx == NULL)
> >  			return;
> > 
> > --
> > 2.27.0
> 
> Reviewed-by: Michael Kelley <mikelley@microsoft.com>
> 

Applied to hyperv-next. Thanks.
diff mbox series

Patch

diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index 17bf55fe3169..cd193456cd84 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -1129,7 +1129,7 @@  void vmbus_on_msg_dpc(unsigned long data)
 	}
 
 	if (entry->handler_type	== VMHT_BLOCKING) {
-		ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
+		ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
 		if (ctx == NULL)
 			return;