linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] irqchip/gic-v3-its: Fix double free on error
@ 2024-04-11 10:56 Guanrui Huang
  2024-04-11 11:19 ` Marc Zyngier
  0 siblings, 1 reply; 4+ messages in thread
From: Guanrui Huang @ 2024-04-11 10:56 UTC (permalink / raw)
  To: maz
  Cc: yuzenghui, shannon.zhao, tglx, linux-arm-kernel, linux-kernel,
	Guanrui Huang

In its_vpe_irq_domain_alloc, when its_vpe_init() returns an error
with i > 0, its_vpe_irq_domain_free may free bitmap and vprop_page,
and then there is a double free in its_vpe_irq_domain_alloc.

Fix it by calling its_vpe_irq_domain_free directly, bitmap and
vprop_page will be freed in this function.

And check whether its_vm is equal to domain->host_data to make sure
its_vpe_irq_domain_free handle right its_vm.

Signed-off-by: Guanrui Huang <guanrui.huang@linux.alibaba.com>
---
 drivers/irqchip/irq-gic-v3-its.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index fca888b36680..72c44e555c88 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -4523,6 +4523,9 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
 
 	BUG_ON(!vm);
 
+	if (vm != domain->host_data)
+		return -EINVAL;
+
 	bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
 	if (!bitmap)
 		return -ENOMEM;
@@ -4561,13 +4564,8 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
 		irqd_set_resend_when_in_progress(irq_get_irq_data(virq + i));
 	}
 
-	if (err) {
-		if (i > 0)
-			its_vpe_irq_domain_free(domain, virq, i);
-
-		its_lpi_free(bitmap, base, nr_ids);
-		its_free_prop_table(vprop_page);
-	}
+	if (err)
+		its_vpe_irq_domain_free(domain, virq, i);
 
 	return err;
 }
-- 
2.36.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH v2] irqchip/gic-v3-its: Fix double free on error
  2024-04-11 10:56 [PATCH v2] irqchip/gic-v3-its: Fix double free on error Guanrui Huang
@ 2024-04-11 11:19 ` Marc Zyngier
  2024-04-11 11:58   ` Guanrui Huang
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Zyngier @ 2024-04-11 11:19 UTC (permalink / raw)
  To: Guanrui Huang
  Cc: yuzenghui, shannon.zhao, tglx, linux-arm-kernel, linux-kernel

On Thu, 11 Apr 2024 11:56:30 +0100,
Guanrui Huang <guanrui.huang@linux.alibaba.com> wrote:
> 
> In its_vpe_irq_domain_alloc, when its_vpe_init() returns an error
> with i > 0, its_vpe_irq_domain_free may free bitmap and vprop_page,
> and then there is a double free in its_vpe_irq_domain_alloc.
> 
> Fix it by calling its_vpe_irq_domain_free directly, bitmap and
> vprop_page will be freed in this function.
> 
> And check whether its_vm is equal to domain->host_data to make sure
> its_vpe_irq_domain_free handle right its_vm.
> 
> Signed-off-by: Guanrui Huang <guanrui.huang@linux.alibaba.com>
> ---
>  drivers/irqchip/irq-gic-v3-its.c | 12 +++++-------
>  1 file changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
> index fca888b36680..72c44e555c88 100644
> --- a/drivers/irqchip/irq-gic-v3-its.c
> +++ b/drivers/irqchip/irq-gic-v3-its.c
> @@ -4523,6 +4523,9 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
>  
>  	BUG_ON(!vm);
>  
> +	if (vm != domain->host_data)
> +		return -EINVAL;
> +

How can this happen?

>  	bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
>  	if (!bitmap)
>  		return -ENOMEM;
> @@ -4561,13 +4564,8 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
>  		irqd_set_resend_when_in_progress(irq_get_irq_data(virq + i));
>  	}
>  
> -	if (err) {
> -		if (i > 0)
> -			its_vpe_irq_domain_free(domain, virq, i);
> -
> -		its_lpi_free(bitmap, base, nr_ids);
> -		its_free_prop_table(vprop_page);
> -	}
> +	if (err)
> +		its_vpe_irq_domain_free(domain, virq, i);
>  
>  	return err;
>  }

This otherwise looks reasonable.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH v2] irqchip/gic-v3-its: Fix double free on error
  2024-04-11 11:19 ` Marc Zyngier
@ 2024-04-11 11:58   ` Guanrui Huang
  2024-04-11 13:00     ` Marc Zyngier
  0 siblings, 1 reply; 4+ messages in thread
From: Guanrui Huang @ 2024-04-11 11:58 UTC (permalink / raw)
  To: maz
  Cc: guanrui.huang, linux-arm-kernel, linux-kernel, shannon.zhao, tglx,
	yuzenghui

if (vm != domain->host_data)

This is just a safety check. It looks unlikely happened.
After all, vm is obtained from the args. If the caller has a bug, can we check here to avoid possible problems that may arise in later code?

Thanks,
Guanrui

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH v2] irqchip/gic-v3-its: Fix double free on error
  2024-04-11 11:58   ` Guanrui Huang
@ 2024-04-11 13:00     ` Marc Zyngier
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2024-04-11 13:00 UTC (permalink / raw)
  To: Guanrui Huang
  Cc: linux-arm-kernel, linux-kernel, shannon.zhao, tglx, yuzenghui

On Thu, 11 Apr 2024 12:58:05 +0100,
Guanrui Huang <guanrui.huang@linux.alibaba.com> wrote:
> 
> if (vm != domain->host_data)
> 
> This is just a safety check. It looks unlikely happened.

It's not a safety check. It is just papering over problems. If you
don't trust the vm pointer, why do you trust the domain pointer? Why
do you trust *anything* at all?

> After all, vm is obtained from the args. If the caller has a bug,
> can we check here to avoid possible problems that may arise in later
> code?

If the caller has a bug, please fix the caller. Don't paper over it.

And you can remove the BUG_ON(!vm) which is equally useless.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2024-04-11 13:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-11 10:56 [PATCH v2] irqchip/gic-v3-its: Fix double free on error Guanrui Huang
2024-04-11 11:19 ` Marc Zyngier
2024-04-11 11:58   ` Guanrui Huang
2024-04-11 13:00     ` Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).