From: Jiangfeng Xiao <xiaojiangfeng@huawei.com>
To: <gustavoars@kernel.org>, <akpm@linux-foundation.org>,
<jpoimboe@kernel.org>, <peterz@infradead.org>,
<dave.hansen@linux.intel.com>, <kirill.shutemov@linux.intel.com>,
<keescook@chromium.org>, <xiaojiangfeng@huawei.com>
Cc: <linux-kernel@vger.kernel.org>, <linux-hardening@vger.kernel.org>,
<linux-mm@kvack.org>, <nixiaoming@huawei.com>,
<kepler.chenxin@huawei.com>, <wangbing6@huawei.com>,
<wangfangpeng1@huawei.com>, <douzhaolei@huawei.com>
Subject: [PATCH] usercopy: delete __noreturn from usercopy_abort
Date: Mon, 4 Mar 2024 09:39:45 +0800 [thread overview]
Message-ID: <1709516385-7778-1-git-send-email-xiaojiangfeng@huawei.com> (raw)
When the last instruction of a noreturn function is a call
to another function, the return address falls outside
of the function boundary. This seems to cause kernel
to interrupt the backtrace.
My testcase is as follow:
```
static volatile size_t unconst = 0;
/*
check_object_size
__check_object_size
check_kernel_text_object
usercopy_abort("kernel text", ...)
*/
void test_usercopy_kernel(void)
{
check_object_size(schedule, unconst + PAGE_SIZE, 1);
}
static int __init test_usercopy_init(void)
{
test_usercopy_kernel();
return 0;
}
static void __exit test_usercopy_exit(void)
{
}
module_init(test_usercopy_init);
module_exit(test_usercopy_exit);
MODULE_LICENSE("GPL");
```
Running the testcase cause kernel oops,
and then the call stack is incorrect:
```
usercopy: Kernel memory exposure attempt detected from kernel text
Kernel BUG at usercopy_abort+0x98/0x9c
Internal error: Oops - undefined instruction: 0 [#1] SMP ARM
Modules linked in: test_usercopy(O+) usbcore usb_common
CPU: 0 PID: 609 Comm: insmod Tainted: G O 5.10.0 #11
Hardware name: Hisilicon A9
PC is at usercopy_abort+0x98/0x9c
LR is at usercopy_abort+0x98/0x9c
[...]
(usercopy_abort) from (memfd_fcntl+0x0/0x654)
(memfd_fcntl) from (0xef7368f8)
Code: e1a01004 e58dc004 e58de000 eb108378 (e7f001f2)
---[ end trace e5fdc684259b0883 ]---
Kernel panic - not syncing: Fatal exception
```
Why Does the kernel backtrace cause errors?
Because usercopy_abort is marked as __noreturn.
You can see the related disassembling:
```
c02f24ac <__check_object_size>:
static_key_count():
[...]
check_kernel_text_object():
linux/mm/usercopy.c:125
usercopy_abort("kernel text", NULL, to_user, ptr - textlow, n);
c02f26c4: e3040110 movw r0, #16656 ; 0x4110
c02f26c8: e58d5000 str r5, [sp]
c02f26cc: e0443003 sub r3, r4, r3
c02f26d0: e1a02007 mov r2, r7
c02f26d4: e34c0096 movt r0, #49302 ; 0xc096
c02f26d8: ebffff4c bl c02f2410 <usercopy_abort>
c02f26dc <memfd_fcntl>:
memfd_fcntl():
[...]
```
The last instruction of __check_object_size is a call to usercopy_abort,
which is a noreturn function, the return address falls outside of the
__check_object_size function boundary, the return address falls into
the next function memfd_fcntl, therefore,
an error occurs when the kernel backtrace.
Delete __noreturn from usercopy_abort,
the correct call stack is as follow:
```
(usercopy_abort) from (__check_object_size+0x170/0x234)
(__check_object_size) from (test_usercopy_init+0x8/0xc0 [test_usercopy])
(test_usercopy_init [test_usercopy]) from (do_one_initcall+0xac/0x204)
(do_one_initcall) from (do_init_module+0x44/0x1c8)
(do_init_module) from (load_module+0x1d48/0x2434)
(load_module) from (sys_finit_module+0xc0/0xf4)
(sys_finit_module) from (ret_fast_syscall+0x0/0x50)
```
Fixes: b394d468e7d7 ("usercopy: Enhance and rename report_usercopy()")
Signed-off-by: Jiangfeng Xiao <xiaojiangfeng@huawei.com>
---
include/linux/uaccess.h | 2 +-
mm/usercopy.c | 2 +-
tools/objtool/noreturns.h | 1 -
3 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 3064314..c37af70 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -437,7 +437,7 @@ static inline void user_access_restore(unsigned long flags) { }
#endif
#ifdef CONFIG_HARDENED_USERCOPY
-void __noreturn usercopy_abort(const char *name, const char *detail,
+void usercopy_abort(const char *name, const char *detail,
bool to_user, unsigned long offset,
unsigned long len);
#endif
diff --git a/mm/usercopy.c b/mm/usercopy.c
index 83c164a..ca1b22e 100644
--- a/mm/usercopy.c
+++ b/mm/usercopy.c
@@ -83,7 +83,7 @@ static noinline int check_stack_object(const void *obj, unsigned long len)
* kmem_cache_create_usercopy() function to create the cache (and
* carefully audit the whitelist range).
*/
-void __noreturn usercopy_abort(const char *name, const char *detail,
+void usercopy_abort(const char *name, const char *detail,
bool to_user, unsigned long offset,
unsigned long len)
{
diff --git a/tools/objtool/noreturns.h b/tools/objtool/noreturns.h
index 1685d7e..c7e341c 100644
--- a/tools/objtool/noreturns.h
+++ b/tools/objtool/noreturns.h
@@ -40,7 +40,6 @@
NORETURN(snp_abort)
NORETURN(start_kernel)
NORETURN(stop_this_cpu)
-NORETURN(usercopy_abort)
NORETURN(x86_64_start_kernel)
NORETURN(x86_64_start_reservations)
NORETURN(xen_cpu_bringup_again)
--
1.8.5.6
next reply other threads:[~2024-03-04 2:02 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-04 1:39 Jiangfeng Xiao [this message]
2024-03-04 15:15 ` Jann Horn
2024-03-04 17:40 ` Kees Cook
2024-03-05 3:31 ` Jiangfeng Xiao
2024-03-05 9:32 ` Kees Cook
2024-03-05 11:38 ` Jiangfeng Xiao
2024-03-05 17:58 ` Josh Poimboeuf
2024-03-06 4:00 ` Jiangfeng Xiao
2024-03-06 9:52 ` Russell King (Oracle)
2024-03-06 16:02 ` Josh Poimboeuf
2024-03-09 14:58 ` David Laight
2024-03-18 4:01 ` Jiangfeng Xiao
2024-03-05 2:54 ` Jiangfeng Xiao
2024-03-05 3:12 ` Jiangfeng Xiao
2024-03-20 2:19 ` [PATCH] ARM: unwind: improve unwinders for noreturn case Jiangfeng Xiao
2024-03-20 2:46 ` Kees Cook
2024-03-20 3:30 ` Jiangfeng Xiao
2024-03-20 3:34 ` Matthew Wilcox
2024-03-20 3:46 ` Jiangfeng Xiao
2024-03-20 3:44 ` [PATCH v2] " Jiangfeng Xiao
2024-03-20 8:45 ` Russell King (Oracle)
2024-03-20 15:30 ` Jiangfeng Xiao
2024-03-20 19:40 ` Russell King (Oracle)
2024-03-21 9:44 ` Jiangfeng Xiao
2024-03-21 10:22 ` David Laight
2024-03-21 11:23 ` Russell King (Oracle)
2024-03-21 12:07 ` David Laight
2024-03-21 12:22 ` Russell King (Oracle)
2024-03-21 12:57 ` David Laight
2024-03-21 13:08 ` Russell King (Oracle)
2024-03-21 14:37 ` David Laight
2024-03-21 14:56 ` Russell King (Oracle)
2024-03-21 15:20 ` David Laight
2024-03-21 15:33 ` Russell King (Oracle)
2024-03-21 22:43 ` Ard Biesheuvel
2024-03-22 0:08 ` Russell King (Oracle)
2024-03-22 9:24 ` David Laight
2024-03-22 9:52 ` Russell King (Oracle)
2024-03-22 12:54 ` Jiangfeng Xiao
2024-03-22 14:16 ` David Laight
2024-03-20 15:41 ` [PATCH v3] " Jiangfeng Xiao
2024-03-20 19:42 ` Russell King (Oracle)
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=1709516385-7778-1-git-send-email-xiaojiangfeng@huawei.com \
--to=xiaojiangfeng@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=dave.hansen@linux.intel.com \
--cc=douzhaolei@huawei.com \
--cc=gustavoars@kernel.org \
--cc=jpoimboe@kernel.org \
--cc=keescook@chromium.org \
--cc=kepler.chenxin@huawei.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nixiaoming@huawei.com \
--cc=peterz@infradead.org \
--cc=wangbing6@huawei.com \
--cc=wangfangpeng1@huawei.com \
/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