All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
@ 2022-01-04  2:34 Zhou Qingyang
  2022-01-04  2:47 ` Li Fei1
  0 siblings, 1 reply; 7+ messages in thread
From: Zhou Qingyang @ 2022-01-04  2:34 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, Fei Li, Reinette Chatre, Zhi Wang, Greg Kroah-Hartman,
	Shuo Liu, linux-kernel

In acrn_dev_ioctl(), vm_param is not released or passed out on the 
error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
which could lead to a memory leak.

Fix this bug by adding a kfree of vm_param on the error path.

This bug was found by a static analyzer.

Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
and our static analyzer no longer warns about this code.

Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
---
The analysis employs differential checking to identify inconsistent
security operations (e.g., checks or kfrees) between two code paths
and confirms that the inconsistent operations are not recovered in 
the current function or the callers, so they constitute bugs. 

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

 drivers/virt/acrn/hsm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/virt/acrn/hsm.c b/drivers/virt/acrn/hsm.c
index 5419794fccf1..205f4c637556 100644
--- a/drivers/virt/acrn/hsm.c
+++ b/drivers/virt/acrn/hsm.c
@@ -136,9 +136,11 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
 		if (IS_ERR(vm_param))
 			return PTR_ERR(vm_param);
 
-		if ((vm_param->reserved0 | vm_param->reserved1) != 0)
-			return -EINVAL;
-
+		if ((vm_param->reserved0 | vm_param->reserved1) != 0) {
+			ret = -EINVAL;
+			kfree(vm_param);
+			break;
+		}
 		vm = acrn_vm_create(vm, vm_param);
 		if (!vm) {
 			ret = -EINVAL;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  2:34 [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl() Zhou Qingyang
@ 2022-01-04  2:47 ` Li Fei1
  2022-01-04  3:45   ` Zhou Qingyang
  0 siblings, 1 reply; 7+ messages in thread
From: Li Fei1 @ 2022-01-04  2:47 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, reinette.chatre, zhi.a.wang, gregkh, linux-kernel, fei1.li

On Tue, Jan 04, 2022 at 10:34:39AM +0800, Zhou Qingyang wrote:
> In acrn_dev_ioctl(), vm_param is not released or passed out on the 
> error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
> which could lead to a memory leak.
> 
> Fix this bug by adding a kfree of vm_param on the error path.
> 
> This bug was found by a static analyzer.
> 
> Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
> and our static analyzer no longer warns about this code.
> 
> Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
> Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> ---
> The analysis employs differential checking to identify inconsistent
> security operations (e.g., checks or kfrees) between two code paths
> and confirms that the inconsistent operations are not recovered in 
> the current function or the callers, so they constitute bugs. 
> 
> Note that, as a bug found by static analysis, it can be a false
> positive or hard to trigger. Multiple researchers have cross-reviewed
> the bug.
> 
Hi Qingyang

Thanks a lot to fix this issue. Would you please to help to fix the same issue
in ACRN_IOCTL_SET_VCPU_REGS case ?


>  drivers/virt/acrn/hsm.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/virt/acrn/hsm.c b/drivers/virt/acrn/hsm.c
> index 5419794fccf1..205f4c637556 100644
> --- a/drivers/virt/acrn/hsm.c
> +++ b/drivers/virt/acrn/hsm.c
> @@ -136,9 +136,11 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
>  		if (IS_ERR(vm_param))
>  			return PTR_ERR(vm_param);
>  
> -		if ((vm_param->reserved0 | vm_param->reserved1) != 0)
> -			return -EINVAL;
> -
> +		if ((vm_param->reserved0 | vm_param->reserved1) != 0) {
> +			ret = -EINVAL;
> +			kfree(vm_param);
> +			break;
> +		}
>  		vm = acrn_vm_create(vm, vm_param);
>  		if (!vm) {
>  			ret = -EINVAL;
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  2:47 ` Li Fei1
@ 2022-01-04  3:45   ` Zhou Qingyang
  2022-01-04  3:50     ` Li Fei1
  0 siblings, 1 reply; 7+ messages in thread
From: Zhou Qingyang @ 2022-01-04  3:45 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, Fei Li, Greg Kroah-Hartman, Reinette Chatre, Zhi Wang,
	Shuo Liu, linux-kernel

In acrn_dev_ioctl(), vm_param is not released or passed out on the 
error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
which could lead to a memory leak.

Fix this bug by adding a kfree of vm_param on the error path.

This bug was found by a static analyzer.

Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
and our static analyzer no longer warns about this code.

Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
---
The analysis employs differential checking to identify inconsistent
security operations (e.g., checks or kfrees) between two code paths
and confirms that the inconsistent operations are not recovered in 
the current function or the callers, so they constitute bugs. 

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

Changes in v2:
  -  Fix the same bug in ACRN_IOCTL_SET_VCPU_REGS.

 drivers/virt/acrn/hsm.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/virt/acrn/hsm.c b/drivers/virt/acrn/hsm.c
index 5419794fccf1..7a8f722f431c 100644
--- a/drivers/virt/acrn/hsm.c
+++ b/drivers/virt/acrn/hsm.c
@@ -136,9 +136,11 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
 		if (IS_ERR(vm_param))
 			return PTR_ERR(vm_param);
 
-		if ((vm_param->reserved0 | vm_param->reserved1) != 0)
-			return -EINVAL;
-
+		if ((vm_param->reserved0 | vm_param->reserved1) != 0) {
+			ret = -EINVAL;
+			kfree(vm_param);
+			break;
+		}
 		vm = acrn_vm_create(vm, vm_param);
 		if (!vm) {
 			ret = -EINVAL;
@@ -182,21 +184,29 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
 			return PTR_ERR(cpu_regs);
 
 		for (i = 0; i < ARRAY_SIZE(cpu_regs->reserved); i++)
-			if (cpu_regs->reserved[i])
+			if (cpu_regs->reserved[i]) {
+				kfree(cpu_regs);
 				return -EINVAL;
+			}
 
 		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.reserved_32); i++)
-			if (cpu_regs->vcpu_regs.reserved_32[i])
+			if (cpu_regs->vcpu_regs.reserved_32[i]) {
+				kfree(cpu_regs);
 				return -EINVAL;
+			}
 
 		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.reserved_64); i++)
-			if (cpu_regs->vcpu_regs.reserved_64[i])
+			if (cpu_regs->vcpu_regs.reserved_64[i]) {
+				kfree(cpu_regs);
 				return -EINVAL;
+			}
 
 		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.gdt.reserved); i++)
 			if (cpu_regs->vcpu_regs.gdt.reserved[i] |
-			    cpu_regs->vcpu_regs.idt.reserved[i])
+			    cpu_regs->vcpu_regs.idt.reserved[i]) {
+				kfree(cpu_regs);
 				return -EINVAL;
+			}
 
 		ret = hcall_set_vcpu_regs(vm->vmid, virt_to_phys(cpu_regs));
 		if (ret < 0)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  3:45   ` Zhou Qingyang
@ 2022-01-04  3:50     ` Li Fei1
  2022-01-04  7:37       ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Li Fei1 @ 2022-01-04  3:50 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, gregkh, reinette.chatre, zhi.a.wang, linux-kernel, fei1.li

On Tue, Jan 04, 2022 at 11:45:01AM +0800, Zhou Qingyang wrote:
> In acrn_dev_ioctl(), vm_param is not released or passed out on the 
> error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
> which could lead to a memory leak.
> 
> Fix this bug by adding a kfree of vm_param on the error path.
> 
> This bug was found by a static analyzer.
> 
> Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
> and our static analyzer no longer warns about this code.
> 
> Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
> Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> ---
> The analysis employs differential checking to identify inconsistent
> security operations (e.g., checks or kfrees) between two code paths
> and confirms that the inconsistent operations are not recovered in 
> the current function or the callers, so they constitute bugs. 
> 
> Note that, as a bug found by static analysis, it can be a false
> positive or hard to trigger. Multiple researchers have cross-reviewed
> the bug.
> 
> Changes in v2:
>   -  Fix the same bug in ACRN_IOCTL_SET_VCPU_REGS.
> 

Signed-off-by: Fei Li <fei1.li@intel.com>

Thanks a lot.

>  drivers/virt/acrn/hsm.c | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/virt/acrn/hsm.c b/drivers/virt/acrn/hsm.c
> index 5419794fccf1..7a8f722f431c 100644
> --- a/drivers/virt/acrn/hsm.c
> +++ b/drivers/virt/acrn/hsm.c
> @@ -136,9 +136,11 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
>  		if (IS_ERR(vm_param))
>  			return PTR_ERR(vm_param);
>  
> -		if ((vm_param->reserved0 | vm_param->reserved1) != 0)
> -			return -EINVAL;
> -
> +		if ((vm_param->reserved0 | vm_param->reserved1) != 0) {
> +			ret = -EINVAL;
> +			kfree(vm_param);
> +			break;
> +		}
>  		vm = acrn_vm_create(vm, vm_param);
>  		if (!vm) {
>  			ret = -EINVAL;
> @@ -182,21 +184,29 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
>  			return PTR_ERR(cpu_regs);
>  
>  		for (i = 0; i < ARRAY_SIZE(cpu_regs->reserved); i++)
> -			if (cpu_regs->reserved[i])
> +			if (cpu_regs->reserved[i]) {
> +				kfree(cpu_regs);
>  				return -EINVAL;
> +			}
>  
>  		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.reserved_32); i++)
> -			if (cpu_regs->vcpu_regs.reserved_32[i])
> +			if (cpu_regs->vcpu_regs.reserved_32[i]) {
> +				kfree(cpu_regs);
>  				return -EINVAL;
> +			}
>  
>  		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.reserved_64); i++)
> -			if (cpu_regs->vcpu_regs.reserved_64[i])
> +			if (cpu_regs->vcpu_regs.reserved_64[i]) {
> +				kfree(cpu_regs);
>  				return -EINVAL;
> +			}
>  
>  		for (i = 0; i < ARRAY_SIZE(cpu_regs->vcpu_regs.gdt.reserved); i++)
>  			if (cpu_regs->vcpu_regs.gdt.reserved[i] |
> -			    cpu_regs->vcpu_regs.idt.reserved[i])
> +			    cpu_regs->vcpu_regs.idt.reserved[i]) {
> +				kfree(cpu_regs);
>  				return -EINVAL;
> +			}
>  
>  		ret = hcall_set_vcpu_regs(vm->vmid, virt_to_phys(cpu_regs));
>  		if (ret < 0)
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  3:50     ` Li Fei1
@ 2022-01-04  7:37       ` Greg KH
  2022-01-04  8:01         ` Li Fei1
  2022-01-04  8:09         ` Li Fei1
  0 siblings, 2 replies; 7+ messages in thread
From: Greg KH @ 2022-01-04  7:37 UTC (permalink / raw)
  To: Li Fei1; +Cc: Zhou Qingyang, kjlu, reinette.chatre, zhi.a.wang, linux-kernel

On Tue, Jan 04, 2022 at 11:50:33AM +0800, Li Fei1 wrote:
> On Tue, Jan 04, 2022 at 11:45:01AM +0800, Zhou Qingyang wrote:
> > In acrn_dev_ioctl(), vm_param is not released or passed out on the 
> > error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
> > which could lead to a memory leak.
> > 
> > Fix this bug by adding a kfree of vm_param on the error path.
> > 
> > This bug was found by a static analyzer.
> > 
> > Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
> > and our static analyzer no longer warns about this code.
> > 
> > Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
> > Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> > ---
> > The analysis employs differential checking to identify inconsistent
> > security operations (e.g., checks or kfrees) between two code paths
> > and confirms that the inconsistent operations are not recovered in 
> > the current function or the callers, so they constitute bugs. 
> > 
> > Note that, as a bug found by static analysis, it can be a false
> > positive or hard to trigger. Multiple researchers have cross-reviewed
> > the bug.
> > 
> > Changes in v2:
> >   -  Fix the same bug in ACRN_IOCTL_SET_VCPU_REGS.
> > 
> 
> Signed-off-by: Fei Li <fei1.li@intel.com>

For the multiple obvious reasons (not the least being you didn't even
run it through our normal testing scripts), I am not going to take this
change, sorry.

Fei, please be more careful in the future.

greg k-h

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  7:37       ` Greg KH
@ 2022-01-04  8:01         ` Li Fei1
  2022-01-04  8:09         ` Li Fei1
  1 sibling, 0 replies; 7+ messages in thread
From: Li Fei1 @ 2022-01-04  8:01 UTC (permalink / raw)
  To: Greg KH
  Cc: zhou1615, kjlu, reinette.chatre, zhi.a.wang, linux-kernel, fei1.li

On Tue, Jan 04, 2022 at 08:37:27AM +0100, Greg KH wrote:
> On Tue, Jan 04, 2022 at 11:50:33AM +0800, Li Fei1 wrote:
> > On Tue, Jan 04, 2022 at 11:45:01AM +0800, Zhou Qingyang wrote:
> > > In acrn_dev_ioctl(), vm_param is not released or passed out on the 
> > > error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
> > > which could lead to a memory leak.
> > > 
> > > Fix this bug by adding a kfree of vm_param on the error path.
> > > 
> > > This bug was found by a static analyzer.
> > > 
> > > Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
> > > and our static analyzer no longer warns about this code.
> > > 
> > > Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
> > > Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> > > ---
> > > The analysis employs differential checking to identify inconsistent
> > > security operations (e.g., checks or kfrees) between two code paths
> > > and confirms that the inconsistent operations are not recovered in 
> > > the current function or the callers, so they constitute bugs. 
> > > 
> > > Note that, as a bug found by static analysis, it can be a false
> > > positive or hard to trigger. Multiple researchers have cross-reviewed
> > > the bug.
> > > 
> > > Changes in v2:
> > >   -  Fix the same bug in ACRN_IOCTL_SET_VCPU_REGS.
> > > 
> > 
> > Signed-off-by: Fei Li <fei1.li@intel.com>
> 
> For the multiple obvious reasons (not the least being you didn't even
> run it through our normal testing scripts), I am not going to take this
> change, sorry.
> 
> Fei, please be more careful in the future.
> 
Hi Greg

Thanks for your kind reminder. I will.


> greg k-h

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl()
  2022-01-04  7:37       ` Greg KH
  2022-01-04  8:01         ` Li Fei1
@ 2022-01-04  8:09         ` Li Fei1
  1 sibling, 0 replies; 7+ messages in thread
From: Li Fei1 @ 2022-01-04  8:09 UTC (permalink / raw)
  To: zhou1615, kjlu, reinette.chatre, zhi.a.wang, gregkh,
	linux-kernel, fei1.li

On Tue, Jan 04, 2022 at 08:37:27AM +0100, Greg KH wrote:
> On Tue, Jan 04, 2022 at 11:50:33AM +0800, Li Fei1 wrote:
> > On Tue, Jan 04, 2022 at 11:45:01AM +0800, Zhou Qingyang wrote:
> > > In acrn_dev_ioctl(), vm_param is not released or passed out on the 
> > > error path of "if ((vm_param->reserved0 | vm_param->reserved1) != 0)", 
> > > which could lead to a memory leak.
> > > 
> > > Fix this bug by adding a kfree of vm_param on the error path.
> > > 
> > > This bug was found by a static analyzer.
> > > 
> > > Builds with CONFIG_ACRN_GUEST=y, CONFIG_ACRN_HSM=y show no new warnings, 
> > > and our static analyzer no longer warns about this code.
> > > 
> > > Fixes: 9c5137aedd11 (“9c5137aedd11 virt: acrn: Introduce VM management interfaces”)
> > > Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> > > ---
> > > The analysis employs differential checking to identify inconsistent
> > > security operations (e.g., checks or kfrees) between two code paths
> > > and confirms that the inconsistent operations are not recovered in 
> > > the current function or the callers, so they constitute bugs. 
> > > 
> > > Note that, as a bug found by static analysis, it can be a false
> > > positive or hard to trigger. Multiple researchers have cross-reviewed
> > > the bug.
> > > 
> > > Changes in v2:
> > >   -  Fix the same bug in ACRN_IOCTL_SET_VCPU_REGS.
Hi Qingyang

I think the "Changes in v2" was not fixes the code in commit "9c5137aedd11 virt: acrn: Introduce VM management interfaces", but in commit 2ad2aaee1bc9 "virt: acrn: Introduce an ioctl to set vCPU registers state".
Would you please to split this patch into two and please replace “...” with "..." in your commit message.

Thanks.


> > > 
> > 
> > Signed-off-by: Fei Li <fei1.li@intel.com>
Please ignore my "Signed-off" here. Sorry.
> 
> For the multiple obvious reasons (not the least being you didn't even
> run it through our normal testing scripts), I am not going to take this
> change, sorry.
> 
> Fei, please be more careful in the future.
> 
> greg k-h

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-01-04  8:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-04  2:34 [PATCH] virt: acrn: fix a memory leak bug in acrn_dev_ioctl() Zhou Qingyang
2022-01-04  2:47 ` Li Fei1
2022-01-04  3:45   ` Zhou Qingyang
2022-01-04  3:50     ` Li Fei1
2022-01-04  7:37       ` Greg KH
2022-01-04  8:01         ` Li Fei1
2022-01-04  8:09         ` Li Fei1

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.