usercopy: reimplement arch_within_stack_frames
Subsystems (5)
v4 cover usercopy: reimplement arch_within_stack_frames
From: Sahara <[email protected]>
This series of patches introduce the arm64 arch_within_stack_frames implementation using stacktrace functions. Also the base code is moved from thread_info.h to stacktrace.h. x86 code is reimplemented to use frame pointer unwinder functions.
Note: The code is still missing in case of using x86 ORC unwinder and guess unwinder.
v2 changes:
- Remove 'arm64: usercopy: consider dynamic array stack variable'
- minor fix in x86 arch_within_stack_frames code.
v3 changes:
- Fix build problem caused by circular inclusion of header in x86 UP config
v4 changes:
- Fix build problem caused by page_ext.h change
James Morse (1):
arm64: usercopy: implement arch_within_stack_frames
Sahara (2):
stacktrace: move arch_within_stack_frames from thread_info.h
x86: usercopy: reimplement arch_within_stack_frames with unwinder
arch/arm64/Kconfig | 1 + arch/arm64/kernel/stacktrace.c | 76 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/thread_info.h | 51 +--------------------- arch/x86/include/asm/unwind.h | 5 +++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/stacktrace.c | 87 ++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/unwind_frame.c | 4 +- include/linux/stacktrace.h | 24 +++++++++++ include/linux/thread_info.h | 21 --------- mm/usercopy.c | 2 +- 10 files changed, 197 insertions(+), 76 deletions(-)
Signature · 1 line
2.7.4
PATCH v4 1/3 stacktrace: move arch_within_stack_frames from thread_info.h
From: Sahara <[email protected]>
Since the inlined arch_within_stack_frames function was placed within asm/thread_info.h, using stacktrace functions to unwind stack was restricted. Now in order to have this function use more abundant apis, it is moved to architecture's stacktrace.c. And, it is changed from inline to noinline function to clearly have three depth calls like:
do_usercopy_stack()
copy_[to|from]_user() : inline
check_copy_size() : inline
__check_object_size()
check_stack_object()
arch_within_stack_frames()
With this change, the x86's implementation was slightly changed also.
--- arch/x86/include/asm/thread_info.h | 51 +------------------------------------- arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/stacktrace.c | 49 ++++++++++++++++++++++++++++++++++++ include/linux/stacktrace.h | 24 ++++++++++++++++++ include/linux/thread_info.h | 21 ---------------- mm/usercopy.c | 2 +- 6 files changed, 76 insertions(+), 73 deletions(-)
arch/x86/include/asm/thread_info.h +1 -50
@@ -156,59 +156,10 @@ struct thread_info {* * preempt_count needs to be 1 initially, until the scheduler is functional. */ -#ifndef __ASSEMBLY__ - -/* -* Walks up the stack frames to make sure that the specified object is -* entirely contained by a single stack frame. -* -* Returns: -* GOOD_FRAME if within a frame -* BAD_STACK if placed across a frame boundary (or outside stack) -* NOT_STACK unable to determine (no frame pointers, etc) -*/ -static inline int arch_within_stack_frames (const void *const stack, -const void *const stackend, -const void *obj,unsigned long len) -{ -#if defined(CONFIG_FRAME_POINTER ) -const void *frame =NULL ; -const void *oldframe; - - oldframe =__builtin_frame_address (1 ); -if (oldframe) - frame =__builtin_frame_address (2 ); -/* -* low ----------------------------------------------> high -* [saved bp][saved ip][args][local vars][saved bp][saved ip] -* ^----------------^ -* allow copies only within here -*/ -while (stack <= frame && frame < stackend) { -/* -* If obj + len extends past the last frame, this -* check won't pass and the next frame will be 0, -* causing us to bail out and correctly report -* the copy as invalid. -*/ -if (obj + len <= frame) -return obj >= oldframe +2 *sizeof (void *) ? -GOOD_FRAME :BAD_STACK ; - oldframe = frame; - frame = *(const void *const *)frame; - } -return BAD_STACK ; -#else -return NOT_STACK ; -#endif -} - -#else /* !__ASSEMBLY__ */ - +#ifdef __ASSEMBLY__#ifdef CONFIG_X86_64 # define cpu_current_top_of_stack (cpu_tss_rw + TSS_sp1)#endif -#endif #ifdef CONFIG_COMPAT
arch/x86/kernel/Makefile +1 -1
@@ -70,7 +70,7 @@ obj-$(CONFIG_IA32_EMULATION) += tls.o obj-y += step.o obj-$(CONFIG_INTEL_TXT) += tboot.o obj-$(CONFIG_ISA_DMA_API) += i8237.o -obj-$(CONFIG_STACKTRACE) += stacktrace.o +obj-y += stacktrace.o obj-y += cpu/ obj-y += acpi/ obj-y += reboot.o
arch/x86/kernel/stacktrace.c +49 -0
58 lines not on this page · raw message
include/linux/stacktrace.h +24 -0
@@ -7,6 +7,30 @@struct task_struct ;struct pt_regs ; +/* +* For per-arch arch_within_stack_frames() implementations, defined in +* kernel/stacktrace.c. +*/ +enum { +BAD_STACK =-1 , +NOT_STACK =0 , +GOOD_FRAME , +GOOD_STACK , +}; + +#ifdef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES +extern int arch_within_stack_frames (const void *const stack, +const void *const stackend, +const void *obj,unsigned long len); +#else +static inline int arch_within_stack_frames (const void *const stack, +const void *const stackend, +const void *obj,unsigned long len) +{ +return NOT_STACK ; +} +#endif + #ifdef CONFIG_STACKTRACE struct stack_trace { unsigned int nr_entries, max_entries;
include/linux/thread_info.h +0 -21
@@ -23,18 +23,6 @@#endif #include <linux/bitops.h> - -/* -* For per-arch arch_within_stack_frames() implementations, defined in -* asm/thread_info.h. -*/ -enum { -BAD_STACK =-1 , -NOT_STACK =0 , -GOOD_FRAME , -GOOD_STACK , -}; -#include <asm/thread_info.h> #ifdef __KERNEL__
@@ -92,15 +80,6 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)#define tif_need_resched () test_thread_flag(TIF_NEED_RESCHED) -#ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES -static inline int arch_within_stack_frames (const void *const stack, -const void *const stackend, -const void *obj,unsigned long len) -{ -return 0 ; -} -#endif -#ifdef CONFIG_HARDENED_USERCOPY extern void __check_object_size (const void *ptr,unsigned long n,bool to_user);
mm/usercopy.c +1 -1
@@ -19,7 +19,7 @@#include <linux/sched.h> #include <linux/sched/task.h> #include <linux/sched/task_stack.h> -#include <linux/thread_info.h> +#include <linux/stacktrace.h> #include <asm/sections.h> /*
Signature · 1 line
2.7.4
PATCH v4 2/3 arm64: usercopy: implement arch_within_stack_frames
From: James Morse <[email protected]>
This implements arch_within_stack_frames() for arm64 that should validate if a given object is contained by a kernel stack frame.
--- arch/arm64/Kconfig | 1 + arch/arm64/kernel/stacktrace.c | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -128,6 +128,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES + select HAVE_ARCH_WITHIN_STACK_FRAMES select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING
arch/arm64/kernel/stacktrace.c +76 -0
88 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 3/3 x86: usercopy: reimplement arch_within_stack_frames with unwinder
From: Sahara <[email protected]>
The old arch_within_stack_frames which used the frame pointer is now reimplemented to use frame pointer unwinder apis. So the main functionality is same as before.
--- arch/x86/include/asm/unwind.h | 5 ++++ arch/x86/kernel/stacktrace.c | 64 +++++++++++++++++++++++++++++++++--------- arch/x86/kernel/unwind_frame.c | 4 +-- 3 files changed, 57 insertions(+), 16 deletions(-)
arch/x86/include/asm/unwind.h +5 -0
@@ -87,6 +87,11 @@ void unwind_init(void);void unwind_module_init (struct module *mod,void *orc_ip,size_t orc_ip_size,void *orc,size_t orc_size);#else +#ifdef CONFIG_UNWINDER_FRAME_POINTER +#define FRAME_HEADER_SIZE (sizeof(long) * 2) +size_t regs_size (struct pt_regs *regs); +#endif +static inline void unwind_init (void ) {} static inline void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
arch/x86/kernel/stacktrace.c +51 -13
88 lines not on this page · raw message
arch/x86/kernel/unwind_frame.c +1 -3
@@ -8,8 +8,6 @@#include <asm/stacktrace.h> #include <asm/unwind.h> -#define FRAME_HEADER_SIZE (sizeof(long) * 2) -unsigned long unwind_get_return_address (struct unwind_state *state) {if (unwind_done (state))
@@ -69,7 +67,7 @@ static void unwind_dump(struct unwind_state *state) } } -static size_t regs_size (struct pt_regs *regs) +size_t regs_size (struct pt_regs *regs) {/* x86_32 regs from kernel mode are two words shorter: */ if (IS_ENABLED (CONFIG_X86_32 ) && !user_mode (regs))
Signature · 1 line
2.7.4