LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH] irqchip/gic-v3-its: Fix double free on error
@ 2024-03-21 11:04 Guanrui Huang
  2024-03-21 12:43 ` Zenghui Yu
  0 siblings, 1 reply; 4+ messages in thread
From: Guanrui Huang @ 2024-03-21 11:04 UTC (permalink / raw
  To: maz; +Cc: 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 checking if bitmap is empty in its_vpe_irq_domain_alloc.
If bitmap is empty and i > 0, means that bitmap have been clear and free
in its_vpe_irq_domain_free.

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

diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index fca888b36680..98a1be90caef 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -4562,9 +4562,14 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
 	}
 
 	if (err) {
-		if (i > 0)
+		if (i > 0) {
 			its_vpe_irq_domain_free(domain, virq, i);
 
+			/* bitmap and vprop_page be freed in its_vpe_irq_domain_free */
+			if (bitmap_empty(bitmap, nr_ids))
+				return err;
+		}
+
 		its_lpi_free(bitmap, base, nr_ids);
 		its_free_prop_table(vprop_page);
 	}
-- 
2.36.1


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

* Re: [PATCH] irqchip/gic-v3-its: Fix double free on error
  2024-03-21 11:04 [PATCH] irqchip/gic-v3-its: Fix double free on error Guanrui Huang
@ 2024-03-21 12:43 ` Zenghui Yu
  2024-03-22  2:38   ` huangguanrui.hgr
  0 siblings, 1 reply; 4+ messages in thread
From: Zenghui Yu @ 2024-03-21 12:43 UTC (permalink / raw
  To: Guanrui Huang; +Cc: maz, shannon.zhao, tglx, linux-arm-kernel, linux-kernel

On 2024/3/21 19:04, Guanrui Huang 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 checking if bitmap is empty in its_vpe_irq_domain_alloc.
> If bitmap is empty and i > 0, means that bitmap have been clear and free
> in its_vpe_irq_domain_free.
> 
> Signed-off-by: Guanrui Huang <guanrui.huang@linux.alibaba.com>
> ---
>  drivers/irqchip/irq-gic-v3-its.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
> index fca888b36680..98a1be90caef 100644
> --- a/drivers/irqchip/irq-gic-v3-its.c
> +++ b/drivers/irqchip/irq-gic-v3-its.c
> @@ -4562,9 +4562,14 @@ static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq
>  	}
>  
>  	if (err) {
> -		if (i > 0)
> +		if (i > 0) {
>  			its_vpe_irq_domain_free(domain, virq, i);
>  
> +			/* bitmap and vprop_page be freed in its_vpe_irq_domain_free */
> +			if (bitmap_empty(bitmap, nr_ids))

It looks like that 'bitmap' can't be non-empty if you managed to get
here. Right?

> +				return err;
> +		}
> +
>  		its_lpi_free(bitmap, base, nr_ids);
>  		its_free_prop_table(vprop_page);
>  	}

Is it possible that we handle these 2 cases together? I.e., does the
following approach help?

if (err)
	its_vpe_irq_domain_free(domain, virq, i);

Thanks,
Zenghui

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

* Re: [PATCH] irqchip/gic-v3-its: Fix double free on error
  2024-03-21 12:43 ` Zenghui Yu
@ 2024-03-22  2:38   ` huangguanrui.hgr
  2024-03-26  9:43     ` Zenghui Yu
  0 siblings, 1 reply; 4+ messages in thread
From: huangguanrui.hgr @ 2024-03-22  2:38 UTC (permalink / raw
  To: yuzenghui; +Cc: linux-arm-kernel, linux-kernel, maz, shannon.zhao, tglx

We notice that:
In its_vpe_irq_domain_alloc, its_vm is from args, But in its_vpe_irq_domain_free,
its_vm is from domain->host_data.

Will these two values be different? For example, when other bugs occur?
If these two values is different, I think we can't handle these 2 cases together.

	if (err)
		its_vpe_irq_domain_free(domain, virq, i);
Because these will lead a memleak.

So I think that check the bitmap is a safe way to fix this. If bitmap is empty, that means 
its_vpe_irq_domain_free handles same its_vm which comes from its_vpe_irq_domain_alloc.

Thanks,
Guanrui

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

* Re: [PATCH] irqchip/gic-v3-its: Fix double free on error
  2024-03-22  2:38   ` huangguanrui.hgr
@ 2024-03-26  9:43     ` Zenghui Yu
  0 siblings, 0 replies; 4+ messages in thread
From: Zenghui Yu @ 2024-03-26  9:43 UTC (permalink / raw
  To: huangguanrui.hgr; +Cc: linux-arm-kernel, linux-kernel, maz, shannon.zhao, tglx

On 2024/3/22 10:38, huangguanrui.hgr wrote:
> We notice that:
> In its_vpe_irq_domain_alloc, its_vm is from args, But in its_vpe_irq_domain_free,
> its_vm is from domain->host_data.

They *should* point to the same its_vm structure.

> Will these two values be different? For example, when other bugs occur?

Of course they can be different if there are *bugs* but I guess you
won't get any further (e.g., you hit BUG_ON(vm != vpe->its_vm) in
its_vpe_irq_domain_free()). The right thing to do is fixing the bug
rather than papering over it.

Zenghui

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

end of thread, other threads:[~2024-03-26  9:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-21 11:04 [PATCH] irqchip/gic-v3-its: Fix double free on error Guanrui Huang
2024-03-21 12:43 ` Zenghui Yu
2024-03-22  2:38   ` huangguanrui.hgr
2024-03-26  9:43     ` Zenghui Yu

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 read-only IMAP folder(s) and NNTP newsgroup(s).