2024-05-24 09:20:05

by dicken.ding

[permalink] [raw]
Subject: [PATCH v2] genirq: Fix uaf issue in irq_find_at_or_after

irq_find_at_or_after() is at the risk of use-after-free due to lack of
any locks. irq_find_at_or_after() dereferences the interrupt descriptor
which is returned by mt_find() while neither holding sparse_irq_lock nor
RCU read lock, which means the descriptor can be freed between mt_find()
and the deference. Here is an example::

CPU0 CPU1
mt_find()
delayed_free_desc()
irq_desc_get_irq()

The use-after-free issue is reported by KASAN, as shown in the following
log::

Call trace:
dump_backtrace+0xec/0x138
show_stack+0x18/0x24
dump_stack_lvl+0x50/0x6c
print_report+0x1b0/0x714
kasan_report+0xc4/0x124
__do_kernel_fault+0xc0/0x368
do_bad_area+0x30/0xdc
do_tag_check_fault+0x20/0x34
do_mem_abort+0x58/0x118
el1_abort+0x3c/0x5c
el1h_64_sync_handler+0x54/0x90
el1h_64_sync+0x68/0x6c
irq_get_next_irq+0x58/0x84
show_stat+0x638/0x824
seq_read_iter+0x158/0x4ec
proc_reg_read_iter+0x94/0x12c
vfs_read+0x1e0/0x2c8
__arm64_sys_pread64+0x84/0xcc
invoke_syscall+0x58/0x114
el0_svc_common+0x80/0xe0
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x68
el0t_64_sync_handler+0x68/0xbc
el0t_64_sync+0x1a8/0x1ac

Freed by task 4471:
kasan_save_stack+0x40/0x70
save_stack_info+0x34/0x128
kasan_save_free_info+0x18/0x28
____kasan_slab_free+0x254/0x25c
__kasan_slab_free+0x10/0x20
slab_free_freelist_hook+0x174/0x1e0
__kmem_cache_free+0xa4/0x1dc
kfree+0x64/0x128
irq_kobj_release+0x28/0x3c
kobject_put+0xcc/0x1e0
delayed_free_desc+0x14/0x2c
rcu_do_batch+0x214/0x720
rcu_core+0x1b0/0x408
rcu_core_si+0x10/0x20
__do_softirq+0x154/0x470

Guard the access with a RCU read lock section.

Fixes: 721255b9826b ("genirq: Use a maple tree for interrupt descriptor management")
Signed-off-by: dicken.ding <[email protected]>
---
Changes since v1:
- Use guard(rcu)() to guard the access based on Thomas Gleixner's suggestion.
- Modify the commit message.
---
kernel/irq/irqdesc.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index 88ac3652fcf2..07e99c936ba5 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -160,7 +160,10 @@ static int irq_find_free_area(unsigned int from, unsigned int cnt)
static unsigned int irq_find_at_or_after(unsigned int offset)
{
unsigned long index = offset;
- struct irq_desc *desc = mt_find(&sparse_irqs, &index, nr_irqs);
+ struct irq_desc *desc;
+
+ guard(rcu)();
+ desc = mt_find(&sparse_irqs, &index, nr_irqs);

return desc ? irq_desc_get_irq(desc) : nr_irqs;
}
--
2.18.0



Subject: [tip: irq/urgent] genirq/irqdesc: Prevent use-after-free in irq_find_at_or_after()

The following commit has been merged into the irq/urgent branch of tip:

Commit-ID: b84a8aba806261d2f759ccedf4a2a6a80a5e55ba
Gitweb: https://git.kernel.org/tip/b84a8aba806261d2f759ccedf4a2a6a80a5e55ba
Author: dicken.ding <[email protected]>
AuthorDate: Fri, 24 May 2024 17:17:39 +08:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Fri, 24 May 2024 12:49:35 +02:00

genirq/irqdesc: Prevent use-after-free in irq_find_at_or_after()

irq_find_at_or_after() dereferences the interrupt descriptor which is
returned by mt_find() while neither holding sparse_irq_lock nor RCU read
lock, which means the descriptor can be freed between mt_find() and the
dereference:

CPU0 CPU1
desc = mt_find()
delayed_free_desc(desc)
irq_desc_get_irq(desc)

The use-after-free is reported by KASAN:

Call trace:
irq_get_next_irq+0x58/0x84
show_stat+0x638/0x824
seq_read_iter+0x158/0x4ec
proc_reg_read_iter+0x94/0x12c
vfs_read+0x1e0/0x2c8

Freed by task 4471:
slab_free_freelist_hook+0x174/0x1e0
__kmem_cache_free+0xa4/0x1dc
kfree+0x64/0x128
irq_kobj_release+0x28/0x3c
kobject_put+0xcc/0x1e0
delayed_free_desc+0x14/0x2c
rcu_do_batch+0x214/0x720

Guard the access with a RCU read lock section.

Fixes: 721255b9826b ("genirq: Use a maple tree for interrupt descriptor management")
Signed-off-by: dicken.ding <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Cc: [email protected]
Link: https://lore.kernel.org/r/[email protected]
---
kernel/irq/irqdesc.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index 88ac365..07e99c9 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -160,7 +160,10 @@ static int irq_find_free_area(unsigned int from, unsigned int cnt)
static unsigned int irq_find_at_or_after(unsigned int offset)
{
unsigned long index = offset;
- struct irq_desc *desc = mt_find(&sparse_irqs, &index, nr_irqs);
+ struct irq_desc *desc;
+
+ guard(rcu)();
+ desc = mt_find(&sparse_irqs, &index, nr_irqs);

return desc ? irq_desc_get_irq(desc) : nr_irqs;
}

2024-05-25 10:22:48

by Markus Elfring

[permalink] [raw]
Subject: Re: [tip: irq/urgent] genirq/irqdesc: Prevent use-after-free in irq_find_at_or_after()


> Fixes: 721255b9826b ("genirq: Use a maple tree for interrupt descriptor management")
> Signed-off-by: dicken.ding <[email protected]>


Would a slightly different author name be preferred here
according to the Developer's Certificate of Origin?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.9#n438

Regards,
Markus