From: Chen Zhongjin <chenzhongjin@huawei.com>
To: <x86@kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <tglx@linutronix.de>, <mingo@redhat.com>, <bp@alien8.de>,
<dave.hansen@linux.intel.com>, <hpa@zytor.com>,
<chenzhongjin@huawei.com>, <akpm@linux-foudation.org>,
<ben-linux@fluff.org>, <wuchi.zero@gmail.com>
Subject: [PATCH 2/2] profiling: Check prof_buffer in profile_tick()
Date: Mon, 10 Apr 2023 10:22:26 +0800 [thread overview]
Message-ID: <20230410022226.181812-3-chenzhongjin@huawei.com> (raw)
In-Reply-To: <20230410022226.181812-1-chenzhongjin@huawei.com>
KASAN reported a UAF problem in profile_tick():
BUG: KASAN: use-after-free in profile_tick+0x5c/0x80
Read of size 8 at addr ffff888100928aa0 by task bash/1108
CPU: 2 PID: 1108 Comm: bash Not tainted 5.10.0+ #72
Call Trace:
<IRQ>
dump_stack+0x93/0xc5
print_address_description.constprop.0+0x1c/0x3c0
kasan_report.cold+0x37/0x74
check_memory_region+0x161/0x1c0
profile_tick+0x5c/0x80
tick_sched_timer+0xcd/0x100
__hrtimer_run_queues+0x23e/0x480
hrtimer_interrupt+0x1c2/0x440
asm_call_irq_on_stack+0xf/0x20
</IRQ>
...
It is beacause in profiling_store(), profile_init() is possible to fail
and free prof_cpu_mask. However prof_cpu_mask is not set to NULL and
cpumask_available(prof_cpu_mask) will return true in profile_tick().
Then cpumask_test_cpu() will dereference prof_cpu_mask and trigger the
KASAN warning.
There is no interface to disable profile_tick() even though profile_init
has been already failed. And when CONFIG_CPUMASK_OFFSTACK=n, cpumask_var_t
is an array pointer which can not be set to NULL. So prof_cpu_mask can be
a freed pointner or uncleaned array here, and cpumask_available() can't
promise it is safe to use it.
To fix this, add check for prof_buffer in profile_tick() before check
cpumask_available(prof_cpu_mask). Because prof_cpu_mask is available only
after prof_buffer is allocated successfully.
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
kernel/profile.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/profile.c b/kernel/profile.c
index 8a77769bc4b4..cca47dc2b1db 100644
--- a/kernel/profile.c
+++ b/kernel/profile.c
@@ -334,7 +334,7 @@ void profile_tick(int type)
{
struct pt_regs *regs = get_irq_regs();
- if (!user_mode(regs) && cpumask_available(prof_cpu_mask) &&
+ if (!user_mode(regs) && prof_buffer && cpumask_available(prof_cpu_mask) &&
cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
profile_hit(type, (void *)profile_pc(regs));
}
--
2.17.1
prev parent reply other threads:[~2023-04-10 2:24 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-10 2:22 [PATCH 0/2] Fix two bugs for profile Chen Zhongjin
2023-04-10 2:22 ` [PATCH 1/2] x86: profiling: remove lock functions hack for !FRAME_POINTER Chen Zhongjin
2023-04-10 19:34 ` Dave Hansen
2023-04-12 7:01 ` Chen Zhongjin
2023-04-12 10:01 ` David Laight
2023-04-13 7:53 ` Chen Zhongjin
2023-04-19 16:17 ` Josh Poimboeuf
2023-04-19 15:43 ` Josh Poimboeuf
2023-04-10 2:22 ` Chen Zhongjin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230410022226.181812-3-chenzhongjin@huawei.com \
--to=chenzhongjin@huawei.com \
--cc=akpm@linux-foudation.org \
--cc=ben-linux@fluff.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=wuchi.zero@gmail.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®