Compile-time stack validation
Subsystems (13)
v8 cover Compile-time stack validation
This is v8 of the compile-time stack validation patch set, based on the tip/master branch.
The frame pointer macros are still called FRAME and ENDFRAME because I don't think we converged on anything else yet. Otherwise I tried to address all the other review comments from v7.
v7 can be found here:
https://lkml.kernel.org/r/[email protected]
For more information about the motivation behind this patch set, and more details about what it does, please see the changelog in patch 3.
Patches 1-5 are the stackvalidate tool and some related macros.
Patches 6-21 are some proposed fixes for several of the warnings reported by stackvalidate. They've been compile-tested and boot tested in a VM, but I haven't attempted any meaningful testing for many of them.
v8:
- add proposed fixes for warnings
- fix all memory leaks
- process ignores earlier and add more ignore checks
- always assume POPCNT alternative is enabled
- drop hweight inline asm fix
- drop __schedule() ignore patch
- change .Ltemp_\@ to .Lstackvalidate_ignore_\@ in asm macro
- fix CONFIG_* checks in asm macros
- add C versions of ignore macros and frame macros
- change ";" to "\n" in C macros
- add ifdef CONFIG_STACK_VALIDATION checks in C ignore macros
- use numbered label in C ignore macro
- add missing break in switch case statement in arch-x86.c
v7:
- sibling call support
- document proposed solution for inline asm() frame pointer issues
- say "kernel entry/exit" instead of "context switch"
- clarify the checking of switch statement jump tables
- discard __stackvalidate_ignore_* sections in linker script
- use .Ltemp_\@ to get a unique label instead of static 3-digit number
- change STACKVALIDATE_IGNORE_FUNC variable to a static
- move STACKVALIDATE_IGNORE_INSN to arch-specific .h file
v6:
- rename asmvalidate -> stackvalidate (again)
- gcc-generated object file support
- recursive branch state analysis
- external jump support
- fixup/exception table support
- jump label support
- switch statement jump table support
- added documentation
- detection of "noreturn" dead end functions
- added a Kbuild mechanism for skipping files and dirs
- moved frame pointer macros to arch/x86/include/asm/frame.h
- moved ignore macros to include/linux/stackvalidate.h
v5:
- stackvalidate -> asmvalidate
- frame pointers only required for non-leaf functions
- check for the use of the FP_SAVE/RESTORE macros instead of manually analyzing code to detect frame pointer usage
- additional checks to ensure each function doesn't leave its boundaries
- make the macros simpler and more flexible
- support for analyzing ALTERNATIVE macros
- simplified the arch interfaces in scripts/asmvalidate/arch.h
- fixed some asmvalidate warnings
- rebased onto latest tip asm cleanups
- many more small changes
v4:
- Changed the default to CONFIG_STACK_VALIDATION=n, until all the asm code can get cleaned up.
- Fixed a stackvalidate error path exit code issue found by Michal Marek.
v3:
- Added a patch to make the push/pop CFI macros arch-independent, as suggested by H. Peter Anvin
v2:
- Fixed memory leaks reported by Petr Mladek
Josh Poimboeuf (21):
x86/asm: Frame pointer macro cleanup
x86/asm: Add C versions of FRAME and ENDFRAME macros x86/stackvalidate: Compile-time stack validation x86/stackvalidate: Add file and directory ignores x86/stackvalidate: Add ignore macros
x86/xen: Add stack frame dependency to hypercall inline asm calls x86/paravirt: Add stack frame dependency to PVOP inline asm calls x86/paravirt: Fix frame pointer usage in PV_CALLEE_SAVE_REGS_THUNK x86/paravirt: Align paravirt thunk functions at 16-byte boundaries x86/amd: Set ELF function type for vide()
x86/reboot: Add ljmp instructions to stackvalidate whitelist x86/xen: Add xen_cpuid() and xen_setup_gdt() to stackvalidate
whitelists
x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S x86/asm/crypto: Move .Lbswap_mask data to .rodata section x86/asm/crypto: Move jump_table to .rodata section x86/asm/crypto: Fix frame pointer usage in clmul_ghash_mul/update() x86/asm/entry: Fix frame pointer usage in thunk functions x86/asm/acpi: Fix frame pointer usage in do_suspend_lowlevel() x86/asm: Fix frame pointer usage in rwsem functions x86/asm/efi: Fix frame pointer usage in efi_call() x86/asm/power: Fix frame pointer usage in hibernate_asm_64.S
Documentation/stack-validation.txt | 193 ++++++ MAINTAINERS | 8 + arch/Kconfig | 6 + arch/x86/Kconfig | 1 + arch/x86/Makefile | 6 +- arch/x86/boot/Makefile | 3 +- arch/x86/boot/compressed/Makefile | 3 +- arch/x86/crypto/aesni-intel_asm.S | 21 + arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 8 +- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 + arch/x86/entry/thunk_64.S | 4 + arch/x86/entry/vdso/Makefile | 5 +- arch/x86/include/asm/frame.h | 51 +- arch/x86/include/asm/paravirt.h | 10 +- arch/x86/include/asm/paravirt_types.h | 18 +- arch/x86/include/asm/stackvalidate.h | 45 ++ arch/x86/include/asm/xen/hypercall.h | 5 +- arch/x86/kernel/acpi/wakeup_64.S | 3 + arch/x86/kernel/cpu/amd.c | 5 +- arch/x86/kernel/reboot.c | 7 +- arch/x86/kernel/vmlinux.lds.S | 5 +- arch/x86/lib/rwsem.S | 11 +- arch/x86/platform/efi/efi_stub_64.S | 3 + arch/x86/power/hibernate_asm_64.S | 7 + arch/x86/purgatory/Makefile | 2 + arch/x86/realmode/Makefile | 4 +- arch/x86/realmode/rm/Makefile | 3 +- arch/x86/xen/enlighten.c | 4 +- drivers/firmware/efi/libstub/Makefile | 1 + include/linux/stackvalidate.h | 28 + lib/Kconfig.debug | 11 + scripts/Makefile | 1 + scripts/Makefile.build | 34 +- scripts/stackvalidate/Makefile | 24 + scripts/stackvalidate/arch-x86.c | 160 +++++ scripts/stackvalidate/arch.h | 44 ++ scripts/stackvalidate/elf.c | 427 ++++++++++++++ scripts/stackvalidate/elf.h | 92 +++ scripts/stackvalidate/list.h | 217 +++++++ scripts/stackvalidate/special.c | 197 +++++++ scripts/stackvalidate/special.h | 41 ++ scripts/stackvalidate/stackvalidate.c | 949 ++++++++++++++++++++++++++++++ 42 files changed, 2624 insertions(+), 48 deletions(-) create mode 100644 Documentation/stack-validation.txt create mode 100644 arch/x86/include/asm/stackvalidate.h create mode 100644 include/linux/stackvalidate.h create mode 100644 scripts/stackvalidate/Makefile create mode 100644 scripts/stackvalidate/arch-x86.c create mode 100644 scripts/stackvalidate/arch.h create mode 100644 scripts/stackvalidate/elf.c create mode 100644 scripts/stackvalidate/elf.h create mode 100644 scripts/stackvalidate/list.h create mode 100644 scripts/stackvalidate/special.c create mode 100644 scripts/stackvalidate/special.h create mode 100644 scripts/stackvalidate/stackvalidate.c
Signature · 1 line
2.1.0
PATCH v8 1/21 x86/asm: Frame pointer macro cleanup
The FRAME/ENDFRAME asm macros for setting up and restoring the frame pointer aren't currently being used. However, they will be needed soon to help asm functions to comply with stackvalidate.
Make the code more readable and improve the comments.
--- arch/x86/include/asm/frame.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
arch/x86/include/asm/frame.h +18 -15
@@ -1,23 +1,26 @@ +#ifndef _ASM_X86_FRAME_H +#define _ASM_X86_FRAME_H + #ifdef __ASSEMBLY__ #include <asm/asm.h> -/* The annotation hides the frame from the unwinder and makes it look -like a ordinary ebp save/restore. This avoids some special cases for -frame pointer later */ +/* + * These are stack frame creation macros. They should be used by every + * callable non-leaf asm function to make kernel stack traces more reliable. + */ +.macro FRAME +#ifdef CONFIG_FRAME_POINTER + push %_ASM_BP + _ASM_MOV %_ASM_SP, %_ASM_BP +#endif +.endm + +.macro ENDFRAME #ifdef CONFIG_FRAME_POINTER - .macro FRAME - __ASM_SIZE(push,) %__ASM_REG(bp) - __ASM_SIZE(mov) %__ASM_REG(sp), %__ASM_REG(bp) - .endm - .macro ENDFRAME - __ASM_SIZE(pop,) %__ASM_REG(bp) - .endm -#else - .macro FRAME - .endm - .macro ENDFRAME - .endm + pop %_ASM_BP #endif +.endm #endif /* __ASSEMBLY__ */ +#endif /* _ASM_X86_FRAME_H */
Signature · 1 line
2.1.0
PATCH v8 2/21 x86/asm: Add C versions of FRAME and ENDFRAME macros
In reply to Josh PoimboeufAdd C versions of the FRAME and ENDFRAME macros which can be used to create a stack frame in inline assembly.
--- arch/x86/include/asm/frame.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/include/asm/frame.h +18 -2
@@ -1,10 +1,10 @@#ifndef _ASM_X86_FRAME_H#define _ASM_X86_FRAME_H -#ifdef __ASSEMBLY__ -#include <asm/asm.h> +#ifdef __ASSEMBLY__ +/* * These are stack frame creation macros. They should be used by every * callable non-leaf asm function to make kernel stack traces more reliable.
@@ -22,5 +22,21 @@#endif .endm +#else /* !__ASSEMBLY__ */ + +#ifdef CONFIG_FRAME_POINTER + +#define FRAME \ + "push %" _ASM_BP "\n" \ + _ASM_MOV "%" _ASM_SP ", %" _ASM_BP "\n" + +#define ENDFRAME "pop %" _ASM_BP "\n" + +#else /* !CONFIG_FRAME_POINTER */ + +#define FRAME "" +#define ENDFRAME "" + +#endif /* CONFIG_FRAME_POINTER */ #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_FRAME_H */
Signature · 1 line
2.1.0
PATCH v8 3/21 x86/stackvalidate: Compile-time stack validation
In reply to Josh PoimboeufThis adds a CONFIG_STACK_VALIDATION option which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.
Currently it checks frame pointer usage. I also plan to add DWARF CFI validation for C .o files and CFI generation for asm .o files.
For each function, it recursively follows all possible code paths and validates the correct frame pointer state at each instruction.
It also follows code paths involving special sections, like .altinstructions, __jump_table, and __ex_table, which can add alternative execution paths to a given instruction (or set of instructions). Similarly, it knows how to follow switch statements, for which gcc sometimes uses jump tables.
To achieve the validation, stackvalidate enforces the following rules:
1. Each callable function must be annotated as such with the ELF function type. In asm code, this is typically done using the ENTRY/ENDPROC macros. If stackvalidate finds a return instruction outside of a function, it flags an error since that usually indicates callable code which should be annotated accordingly.
2. Conversely, each section of code which is *not* callable should *not* be annotated as an ELF function. The ENDPROC macro shouldn't be used in this case.
3. Each callable function which calls another function must have the correct frame pointer logic, if required by CONFIG_FRAME_POINTER or the architecture's back chain rules. This can by done in asm code with the FRAME/ENDFRAME macros.
4. Dynamic jumps and jumps to undefined symbols are only allowed if:
a) the jump is part of a switch statement; or
b) the jump matches sibling call semantics and the frame pointer has the same value it had on function entry.
5. A callable function may not execute kernel entry/exit instructions. The only code which needs such instructions is kernel entry code, which shouldn't be in callable functions anyway.
It currently only supports x86_64. I tried to make the code generic so that support for other architectures can hopefully be plugged in relatively easily.
As a first step, CONFIG_STACK_VALIDATION is disabled by default, and all reported non-compliances result in warnings. Once we get them all cleaned up, we can change the default to CONFIG_STACK_VALIDATION=y and change the warnings to errors to keep the stack metadata clean.
On my Lenovo T430s 4-core laptop, building the kernel with it enabled adds about 3.6 seconds (14.4 seconds of total CPU). It hasn't been optimized for performance yet, so there are probably some opportunities for better build performance.
--- Documentation/stack-validation.txt | 193 +++++++ MAINTAINERS | 8 + arch/Kconfig | 6 + arch/x86/Kconfig | 1 + arch/x86/Makefile | 6 +- lib/Kconfig.debug | 11 + scripts/Makefile | 1 + scripts/Makefile.build | 34 +- scripts/stackvalidate/Makefile | 24 + scripts/stackvalidate/arch-x86.c | 160 ++++++ scripts/stackvalidate/arch.h | 44 ++ scripts/stackvalidate/elf.c | 427 +++++++++++++++ scripts/stackvalidate/elf.h | 92 ++++ scripts/stackvalidate/list.h | 217 ++++++++ scripts/stackvalidate/special.c | 197 +++++++ scripts/stackvalidate/special.h | 41 ++ scripts/stackvalidate/stackvalidate.c | 949 ++++++++++++++++++++++++++++++++++ 17 files changed, 2406 insertions(+), 5 deletions(-) create mode 100644 Documentation/stack-validation.txt create mode 100644 scripts/stackvalidate/Makefile create mode 100644 scripts/stackvalidate/arch-x86.c create mode 100644 scripts/stackvalidate/arch.h create mode 100644 scripts/stackvalidate/elf.c create mode 100644 scripts/stackvalidate/elf.h create mode 100644 scripts/stackvalidate/list.h create mode 100644 scripts/stackvalidate/special.c create mode 100644 scripts/stackvalidate/special.h create mode 100644 scripts/stackvalidate/stackvalidate.c
Documentation/stack-validation.txt new file +193 -0
@@ -0,0 +1,193 @@ +Compile-time stack validation +============================= + + +Overview +-------- + +The CONFIG_STACK_VALIDATION option enables a host tool named +stackvalidate which runs at compile time. It analyzes every .o file and +ensures the validity of its stack metadata. It enforces a set of rules +on asm code and C inline assembly code so that stack traces can be +reliable. + +Currently it only checks frame pointer usage, but there are plans to add +CFI validation for C files and CFI generation for asm files. + +For each function, it recursively follows all possible code paths and +validates the correct frame pointer state at each instruction. + +It also follows code paths involving special sections, like +.altinstructions, __jump_table, and __ex_table, which can add +alternative execution paths to a given instruction (or set of +instructions). Similarly, it knows how to follow switch statements, for +which gcc sometimes uses jump tables. + + +Rules +----- + +To achieve the validation, stackvalidate enforces the following rules: + +1. Each callable function must be annotated as such with the ELF + function type. In asm code, this is typically done using the + ENTRY/ENDPROC macros. If stackvalidate finds a return instruction + outside of a function, it flags an error since that usually indicates + callable code which should be annotated accordingly. + +2. Conversely, each section of code which is *not* callable should *not* + be annotated as an ELF function. The ENDPROC macro shouldn't be used + in this case. + +3. Each callable function which calls another function must have the + correct frame pointer logic, if required by CONFIG_FRAME_POINTER or + the architecture's back chain rules. This can by done in asm code + with the FRAME/ENDFRAME macros. + +4. Dynamic jumps and jumps to undefined symbols are only allowed if: + + a) the jump is part of a switch statement; or + + b) the jump matches sibling call semantics and the frame pointer has + the same value it had on function entry. + +5. A callable function may not execute kernel entry/exit instructions. + The only code which needs such instructions is kernel entry code, + which shouldn't be be in callable functions anyway. + + +Errors in .S files +------------------ + +If you're getting an error in a compiled .S file which you don't +understand, first make sure that the affected code follows the above +rules. + +Here are some examples of common problems and suggestions for how to fix +them. + + +1. stackvalidate: asm_file.o: func()+0x128: call without frame pointer save/setup + + The func() function made a function call without first saving and/or + updating the frame pointer. + + If func() is indeed a callable function, add proper frame pointer + logic using the FP_SAVE and FP_RESTORE macros. Otherwise, remove its + ELF function annotation by changing ENDPROC to END. + + If you're getting this error in a .c file, see the "Errors in .c + files" section. + + +2. stackvalidate: asm_file.o: .text+0x53: return instruction outside of a callable function + + A return instruction was detected, but stackvalidate couldn't find a + way for a callable function to reach the instruction. + + If the return instruction is inside (or reachable from) a callable + function, the function needs to be annotated with the ENTRY/ENDPROC + macros. + + If you _really_ need a return instruction outside of a function, and + are 100% sure that it won't affect stack traces, you can tell + stackvalidate to ignore it. See the "Adding exceptions" section + below. + + +3. stackvalidate: asm_file.o: func()+0x9: function has unreachable instruction + + The instruction lives inside of a callable function, but there's no + possible control flow path from the beginning of the function to the + instruction. + + If the instruction is actually needed, and it's actually in a + callable function, ensure that its function is properly annotated + with ENTRY/ENDPROC. + + If it's not actually in a callable function (e.g. kernel entry code), + change ENDPROC to END. + + +4. stackvalidate: asm_file.o: func(): can't find starting instruction + or + stackvalidate: asm_file.o: func()+0x11dd: can't decode instruction + + Did you put data in a text section? If so, that can confuse + stackvalidate's instruction decoder. Move the data to a more + appropriate section like .data or .rodata. + + +5. stackvalidate: asm_file.o: func()+0x6: kernel entry/exit from callable instruction + + This is a kernel entry/exit instruction like sysenter or sysret. + Such instructions aren't allowed in a callable function, and are most + likely part of the kernel entry code. + + If the instruction isn't actually in a callable function, change + ENDPROC to END. + + +6. stackvalidate: asm_file.o: func()+0x26: sibling call from callable instruction with changed frame pointer + + This is a dynamic jump or a jump to an undefined symbol. + Stackvalidate assumed it's a sibling call and detected that the frame + pointer wasn't first restored to its original state. + + If it's not really a sibling call, you may need to move the + destination code to the local file. + + If the instruction is not actually in a callable function (e.g. + kernel entry code), change ENDPROC to END. + + +7. stackvalidate: asm_file: func()+0x5c: frame pointer state mismatch + + The instruction's frame pointer state is inconsistent, depending on + which execution path was taken to reach the instruction. + + Make sure the function pushes and sets up the frame pointer (for + x86_64, this means rbp) at the beginning of the function and pops it + at the end of the function. Also make sure that no other code in the + function touches the frame pointer. + + +Errors in .c files +------------------ + +If you're getting a stackvalidate error in a compiled .c file, chances +are the file uses an asm() statement which has a "call" instruction. An +asm() statement with a call instruction must declare the use of the +stack pointer in its output operand. For example, on x86_64: + + register void *__sp asm("rsp"); + asm volatile("call func" : "+r" (__sp)); + +Otherwise the stack frame may not get created before the call. + +Another possible cause for errors in C code is if the Makefile removes +-fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options. + +Also see the above section for .S file errors for more information what +the individual error messages mean. + + + +Adding exceptions +----------------- + +If you _really_ need stackvalidate to ignore something, and are 100% +sure that it won't affect kernel stack traces, you can tell +stackvalidate to ignore it: + +- To skip validation of an instruction, use the + STACKVALIDATE_IGNORE_INSN macro immediately before the instruction. + +- To skip validation of a function, use the STACKVALIDATE_IGNORE_FUNC + macro. + +- To skip validation of a file, add "STACKVALIDATE_filename.o := n" to + the Makefile. + +- To skip validation of a directory, add "STACKVALIDATE := n" to the + Makefile.
MAINTAINERS +8 -0
@@ -9675,6 +9675,14 @@ L: [email protected] S: Supported F: Documentation/stable_kernel_rules.txt +STACK VALIDATION +M: Josh Poimboeuf <[email protected]> +S: Supported +F: scripts/stackvalidate/ +F: include/linux/stackvalidate.h +F: arch/x86/include/asm/stackvalidate.h +F: Documentation/stack-validation.txt + STAGING SUBSYSTEM M: Greg Kroah-Hartman <[email protected]> T: git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git
arch/Kconfig +6 -0
@@ -519,6 +519,12 @@ config HAVE_COPY_THREAD_TLS normal C parameter passing, rather than extracting the syscall argument from pt_regs. +config HAVE_STACK_VALIDATION + bool + help + Architecture supports the stackvalidate host tool, which adds + compile-time stack metadata validation. + # # ABI hall of shame #
arch/x86/Kconfig +1 -0
@@ -151,6 +151,7 @@ config X86 select VIRT_TO_BUS select X86_DEV_DMA_OPS if X86_64 select X86_FEATURE_NAMES if PROC_FS + select HAVE_STACK_VALIDATION if X86_64 config INSTRUCTION_DECODER def_bool y
arch/x86/Makefile +5 -1
@@ -181,9 +181,13 @@ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables KBUILD_CFLAGS += $(mflags-y) KBUILD_AFLAGS += $(mflags-y) -archscripts: scripts_basic +archscripts: scripts_basic $(objtree)/arch/x86/lib/inat-tables.c $(Q)$(MAKE) $(build)=arch/x86/tools relocs +# this file is needed early by scripts/stackvalidate +$(objtree)/arch/x86/lib/inat-tables.c: + $(Q)$(MAKE) $(build)=arch/x86/lib $@ + ### # Syscall table generation
lib/Kconfig.debug +11 -0
@@ -332,6 +332,17 @@ config FRAME_POINTER larger and slower, but it gives very useful debugging information in case of kernel bugs. (precise oopses/stacktraces/warnings) +config STACK_VALIDATION + bool "Enable compile-time stack metadata validation" + depends on HAVE_STACK_VALIDATION + default n + help + Add compile-time checks to validate stack metadata, including frame + pointers and back chain pointers. This helps ensure that runtime + stack traces are more reliable. + + For more information, see Documentation/stack-validation.txt. + config DEBUG_FORCE_WEAK_PER_CPU bool "Force weak per-cpu definitions" depends on DEBUG_KERNEL
scripts/Makefile +1 -0
@@ -37,6 +37,7 @@ subdir-y += mod subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb +subdir-$(CONFIG_STACK_VALIDATION) += stackvalidate # Let clean descend into subdirs subdir- += basic kconfig package
scripts/Makefile.build +30 -4
@@ -241,9 +241,26 @@ cmd_record_mcount = \ fi; endif +ifdef CONFIG_STACK_VALIDATION + +stackvalidate = $(objtree)/scripts/stackvalidate/stackvalidate + +ifndef CONFIG_FRAME_POINTER +nofp = --no-frame-pointer +endif + +# Set STACKVALIDATE_foo.o=n to skip stack validation for a file. +# Set STACKVALIDATE=n to skip stack validation for a directory. +cmd_stackvalidate = $(if $(patsubst n%,, \ + $(STACKVALIDATE_$(basetarget).o)$(STACKVALIDATE)y), \ + $(stackvalidate) $(nofp) "$(@)";) + +endif # CONFIG_STACK_VALIDATION + define rule_cc_o_c $(call echo-cmd,checksrc) $(cmd_checksrc) \ $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \ + $(cmd_stackvalidate) \ $(cmd_modversions) \ $(call echo-cmd,record_mcount) \ $(cmd_record_mcount) \
@@ -253,14 +270,23 @@ define rule_cc_o_c mv -f $(dot-target).tmp $(dot-target).cmd endef +define rule_as_o_S + $(call echo-cmd,as_o_S) $(cmd_as_o_S); \ + $(cmd_stackvalidate) \ + scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,as_o_S)' > \ + $(dot-target).tmp; \ + rm -f $(depfile); \ + mv -f $(dot-target).tmp $(dot-target).cmd +endef + # Built-in and composite module parts -$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE +$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(stackvalidate) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c) # Single-part modules are special since we need to mark them in $(MODVERDIR) -$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE +$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(stackvalidate) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c) @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
@@ -290,8 +316,8 @@ $(obj)/%.s: $(src)/%.S FORCE quiet_cmd_as_o_S = AS $(quiet_modtag) $@ cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< -$(obj)/%.o: $(src)/%.S FORCE - $(call if_changed_dep,as_o_S) +$(obj)/%.o: $(src)/%.S $(stackvalidate) FORCE + $(call if_changed_rule,as_o_S) targets += $(real-objs-y) $(real-objs-m) $(lib-y) targets += $(extra-y) $(MAKECMDGOALS) $(always)
scripts/stackvalidate/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +hostprogs-y := stackvalidate +always := $(hostprogs-y) + +stackvalidate-objs := stackvalidate.o elf.o special.o + +HOSTCFLAGS += -Werror +HOSTLOADLIBES_stackvalidate := -lelf + +ifdef CONFIG_X86 + +stackvalidate-objs += arch-x86.o + +HOSTCFLAGS_arch-x86.o := -I$(objtree)/arch/x86/lib/ \ + -I$(srctree)/arch/x86/include/ \ + -I$(srctree)/arch/x86/lib/ + +$(obj)/arch-x86.o: $(srctree)/arch/x86/lib/insn.c \ + $(srctree)/arch/x86/lib/inat.c \ + $(srctree)/arch/x86/include/asm/inat_types.h \ + $(srctree)/arch/x86/include/asm/inat.h \ + $(srctree)/arch/x86/include/asm/insn.h \ + $(objtree)/arch/x86/lib/inat-tables.c + +endif
scripts/stackvalidate/arch-x86.c new file +160 -0
160 lines not on this page · raw message
scripts/stackvalidate/arch.h new file +44 -0
44 lines not on this page · raw message
scripts/stackvalidate/elf.c new file +427 -0
427 lines not on this page · raw message
scripts/stackvalidate/elf.h new file +92 -0
92 lines not on this page · raw message
scripts/stackvalidate/list.h new file +217 -0
217 lines not on this page · raw message
scripts/stackvalidate/special.c new file +197 -0
197 lines not on this page · raw message
scripts/stackvalidate/special.h new file +41 -0
41 lines not on this page · raw message
scripts/stackvalidate/stackvalidate.c new file +949 -0
949 lines not on this page · raw message
Signature · 1 line
2.1.0
PATCH v8 4/21 x86/stackvalidate: Add file and directory ignores
In reply to Josh PoimboeufTell stackvalidate to skip validation of the following code:
- boot image
- vdso image
- kexec purgatory
- realmode
- efi libstub
They all run outside the kernel's normal mode of operation and they don't affect runtime kernel stack traces, so they're free to skirt the stackvalidate rules.
--- arch/x86/boot/Makefile | 3 ++- arch/x86/boot/compressed/Makefile | 3 ++- arch/x86/entry/vdso/Makefile | 5 ++++- arch/x86/purgatory/Makefile | 2 ++ arch/x86/realmode/Makefile | 4 +++- arch/x86/realmode/rm/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 1 + 7 files changed, 16 insertions(+), 5 deletions(-)
arch/x86/boot/Makefile +2 -1
@@ -14,7 +14,8 @@ # Set it to -DSVGA_MODE=NORMAL_VGA if you just want the EGA/VGA mode. # The number is the same as you would ordinarily press at bootup. -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n SVGA_MODE := -DSVGA_MODE=NORMAL_VGA
arch/x86/boot/compressed/Makefile +2 -1
@@ -16,7 +16,8 @@ # (see scripts/Makefile.lib size_append) # compressed vmlinux.bin.all + u32 size of vmlinux.bin.all -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \ vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4
arch/x86/entry/vdso/Makefile +4 -1
@@ -3,7 +3,9 @@ # KBUILD_CFLAGS += $(DISABLE_LTO) -KASAN_SANITIZE := n + +KASAN_SANITIZE := n +STACKVALIDATE := n VDSO64-$(CONFIG_X86_64) := y VDSOX32-$(CONFIG_X86_X32_ABI) := y
@@ -15,6 +17,7 @@ vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o # files to link into kernel obj-y += vma.o +STACKVALIDATE_vma.o := y # vDSO images to build vdso_img-$(VDSO64-y) += 64
arch/x86/purgatory/Makefile +2 -0
@@ -1,3 +1,5 @@ +STACKVALIDATE := n + purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string.o targets += $(purgatory-y)
arch/x86/realmode/Makefile +3 -1
@@ -6,7 +6,9 @@ # for more details. # # -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n + subdir- := rm obj-y += init.o
arch/x86/realmode/rm/Makefile +2 -1
@@ -6,7 +6,8 @@ # for more details. # # -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n always := realmode.bin realmode.relocs
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -20,6 +20,7 @@ KBUILD_CFLAGS := $(cflags-y) \ GCOV_PROFILE := n KASAN_SANITIZE := n +STACKVALIDATE := n lib-y := efi-stub-helper.o lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o
Signature · 1 line
2.1.0
PATCH v8 5/21 x86/stackvalidate: Add ignore macros
In reply to Josh PoimboeufAdd new stackvalidate ignore macros: STACKVALIDATE_IGNORE_INSN and STACKVALIDATE_IGNORE_FUNC. These can be used to tell stackvalidate to skip validation of an instruction or a function, respectively.
--- arch/x86/include/asm/stackvalidate.h | 45 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/vmlinux.lds.S | 5 +++- include/linux/stackvalidate.h | 28 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/stackvalidate.h create mode 100644 include/linux/stackvalidate.h
arch/x86/include/asm/stackvalidate.h new file +45 -0
@@ -0,0 +1,45 @@ +#ifndef _ASM_X86_STACKVALIDATE_H +#define _ASM_X86_STACKVALIDATE_H + +#include <asm/asm.h> + +#ifdef __ASSEMBLY__ + +/* +* This asm macro tells the stack validation script to ignore the instruction +* immediately after the macro. It should only be used in special cases where +* you're 100% sure it won't affect the reliability of frame pointers and +* kernel stack traces. +* +* For more information, see Documentation/stack-validation.txt. +*/ +.macro STACKVALIDATE_IGNORE_INSN +#ifdef CONFIG_STACK_VALIDATION + .Lstackvalidate_ignore_\@: + .pushsection __stackvalidate_ignore_insn, "a" + _ASM_ALIGN + .long .Lstackvalidate_ignore_\@ - . + .popsection +#endif +.endm + +#else /* !__ASSEMBLY__ */ + +#ifdef CONFIG_STACK_VALIDATION + +#define STACKVALIDATE_IGNORE_INSN \ + "1:\n" \ + ".pushsection __stackvalidate_ignore_insn, \"a\"\n" \ + _ASM_ALIGN "\n" \ + ".long 1b - .\n" \ + ".popsection\n" + +#else /* !CONFIG_STACK_VALIDATION */ + +#define STACKVALIDATE_IGNORE_INSN "" + +#endif /* CONFIG_STACK_VALIDATION */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_STACKVALIDATE_H */
arch/x86/kernel/vmlinux.lds.S +4 -1
@@ -332,7 +332,10 @@ SECTIONS /* Sections to be discarded */ DISCARDS - /DISCARD/ : { *(.eh_frame) } + /DISCARD/ : { + *(.eh_frame) + *(__stackvalidate_ignore_*) + } }
include/linux/stackvalidate.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef _LINUX_STACKVALIDATE_H +#define _LINUX_STACKVALIDATE_H + +#include <asm/stackvalidate.h> + +#ifndef __ASSEMBLY__ + +#ifdef CONFIG_STACK_VALIDATION +/* +* This C macro tells the stack validation script to ignore the function. It +* should only be used in special cases where you're 100% sure it won't affect +* the reliability of frame pointers and kernel stack traces. +* +* For more information, see Documentation/stack-validation.txt. +*/ +#define STACKVALIDATE_IGNORE_FUNC (_func) \ + static void __used __section(__stackvalidate_ignore_func) \ + *__stackvalidate_ignore_func_##_func = _func + +#else /* !CONFIG_STACK_VALIDATION */ + +#define STACKVALIDATE_IGNORE_FUNC (_func) + +#endif /* CONFIG_STACK_VALIDATION */ + +#endif /* !__ASSEMBLY__ */ + +#endif /* _LINUX_STACKVALIDATE_H */
Signature · 1 line
2.1.0
PATCH v8 6/21 x86/xen: Add stack frame dependency to hypercall inline asm calls
In reply to Josh PoimboeufIf a hypercall is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace.
Force a stack frame to be created if CONFIG_FRAME_POINTER is enabled by listing the stack pointer as an output operand for the hypercall inline asm statements.
--- arch/x86/include/asm/xen/hypercall.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/xen/hypercall.h +3 -2
@@ -110,9 +110,10 @@ extern struct { char _entry[32]; } hypercall_page[]; registerunsigned long __arg2 asm (__HYPERCALL_ARG2REG ) = __arg2; \ registerunsigned long __arg3 asm (__HYPERCALL_ARG3REG ) = __arg3; \ registerunsigned long __arg4 asm (__HYPERCALL_ARG4REG ) = __arg4; \ - registerunsigned long __arg5 asm (__HYPERCALL_ARG5REG ) = __arg5; + registerunsigned long __arg5 asm (__HYPERCALL_ARG5REG ) = __arg5; \ + register void *__sp asm(_ASM_SP); -#define __HYPERCALL_0PARAM "=r" (__res) +#define __HYPERCALL_0PARAM "=r" (__res), "+r" (__sp)#define __HYPERCALL_1PARAM __HYPERCALL_0PARAM, "+r" (__arg1)#define __HYPERCALL_2PARAM __HYPERCALL_1PARAM, "+r" (__arg2)#define __HYPERCALL_3PARAM __HYPERCALL_2PARAM, "+r" (__arg3)
Signature · 1 line
2.1.0
PATCH v8 7/21 x86/paravirt: Add stack frame dependency to PVOP inline asm calls
In reply to Josh PoimboeufIf a PVOP call macro is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace.
Force a stack frame to be created by listing the stack pointer as an output operand for the PVOP inline asm statements.
--- arch/x86/include/asm/paravirt_types.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/include/asm/paravirt_types.h +10 -8
@@ -497,8 +497,9 @@ int paravirt_disable_iospace(void);* makes sure the incoming and outgoing types are always correct. */ #ifdef CONFIG_X86_32 -#define PVOP_VCALL_ARGS \ - unsigned long __eax = __eax, __edx = __edx, __ecx = __ecx +#define PVOP_VCALL_ARGS \ + unsigned long __eax = __eax, __edx = __edx, __ecx = __ecx; \ + register void *__sp asm("esp")#define PVOP_CALL_ARGS PVOP_VCALL_ARGS#define PVOP_CALL_ARG1 (x) "a" ((unsigned long)(x))
@@ -516,9 +517,10 @@ int paravirt_disable_iospace(void);#define VEXTRA_CLOBBERS #else /* CONFIG_X86_64 */ /* [re]ax isn't an arg, but the return val */ -#define PVOP_VCALL_ARGS \ - unsigned long __edi = __edi, __esi = __esi, \ - __edx = __edx, __ecx = __ecx, __eax = __eax +#define PVOP_VCALL_ARGS \ + unsigned long __edi = __edi, __esi = __esi, \ + __edx = __edx, __ecx = __ecx, __eax = __eax; \ + register void *__sp asm("rsp")#define PVOP_CALL_ARGS PVOP_VCALL_ARGS#define PVOP_CALL_ARG1 (x) "D" ((unsigned long)(x))
@@ -557,7 +559,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
@@ -567,7 +569,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
@@ -594,7 +596,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
Signature · 1 line
2.1.0
PATCH v8 8/21 x86/paravirt: Fix frame pointer usage in PV_CALLEE_SAVE_REGS_THUNK
In reply to Josh PoimboeufA function created with the PV_CALLEE_SAVE_REGS_THUNK macro doesn't set up a new stack frame before the call instruction, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace. Also, the thunk functions aren't annotated as ELF callable functions.
Create a stack frame when CONFIG_FRAME_POINTER is enabled and add the ELF function type.
--- arch/x86/include/asm/paravirt.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/include/asm/paravirt.h +7 -2
@@ -13,6 +13,7 @@#include <linux/bug.h> #include <linux/types.h> #include <linux/cpumask.h> +#include <asm/frame.h> static inline int paravirt_enabled (void ) {
@@ -772,15 +773,19 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock,* call. The return value in rax/eax will not be saved, even for void * functions. */ +#define PV_THUNK_NAME (func) "__raw_callee_save_" #func#define PV_CALLEE_SAVE_REGS_THUNK (func) \ extern typeof(func) __raw_callee_save_##func; \ \ asm(".pushsection .text;" \ - ".globl __raw_callee_save_" #func " ; " \ - "__raw_callee_save_" #func ": " \ + ".globl " PV_THUNK_NAME(func) ";" \ + ".type " PV_THUNK_NAME(func) ", @function;" \ + PV_THUNK_NAME(func) ":" \ + FRAME \ PV_SAVE_ALL_CALLER_REGS \ "call " #func ";" \ PV_RESTORE_ALL_CALLER_REGS \ + ENDFRAME \ "ret;" \ ".popsection")
Signature · 1 line
2.1.0
PATCH v8 9/21 x86/paravirt: Align paravirt thunk functions at 16-byte boundaries
In reply to Josh PoimboeufParavirt thunk functions aren't aligned, which can impact performance and is inconsistent with gcc-generated functions.
Align them at 16-byte boundaries to be consistent with gcc functions.
--- arch/x86/include/asm/paravirt.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/paravirt.h +1 -0
@@ -778,6 +778,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, extern typeof(func) __raw_callee_save_##func; \ \ asm(".pushsection .text;" \ + ".align 16;" \ ".globl " PV_THUNK_NAME(func) ";" \ ".type " PV_THUNK_NAME(func) ", @function;" \ PV_THUNK_NAME(func) ":" \
Signature · 1 line
2.1.0
Re: [PATCH v8 09/21] x86/paravirt: Align paravirt thunk functions at 16-byte boundaries
Quoted 4 lines · Josh Poimboeuf
Paravirt thunk functions aren't aligned, which can impact performance and is inconsistent with gcc-generated functions.
Align them at 16-byte boundaries to be consistent with gcc functions.
IMO stackvalidate shouldn't warn about this. We've discussed dropping the alignment requirement entirely, since it seems to have little benefit on modern CPUs.
--Andy
Re: [PATCH v8 09/21] x86/paravirt: Align paravirt thunk functions at 16-byte boundaries
Quoted 9 lines · Andy Lutomirski
Quoted 4 lines · Josh Poimboeuf
Paravirt thunk functions aren't aligned, which can impact performance and is inconsistent with gcc-generated functions.
Align them at 16-byte boundaries to be consistent with gcc functions.
IMO stackvalidate shouldn't warn about this. We've discussed dropping the alignment requirement entirely, since it seems to have little benefit on modern CPUs.
Stackvalidate didn't actually find this alignment issue. I just noticed it when fixing the frame pointer issue. If alignment no longer helps with performance on modern CPUS, it's fine to drop this patch.
Signature · 1 line
Josh
PATCH v8 10/21 x86/amd: Set ELF function type for vide()
In reply to Josh Poimboeufvide() is a callable function, but is missing the ELF function type, which confuses tools like stackvalidate.
Properly annotate it to be a callable function. The generated code is unchanged.
--- arch/x86/kernel/cpu/amd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +4 -1
@@ -74,7 +74,10 @@ static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)*/ extern __visible void vide(void); -__asm__(".globl vide\n\t.align 4\nvide: ret" ); +__asm__(".globl vide;" +".type vide, @function;" +".align 4;" +"vide: ret;" );static void init_amd_k5 (struct cpuinfo_x86 *c) {
Signature · 1 line
2.1.0
PATCH v8 11/21 x86/reboot: Add ljmp instructions to stackvalidate whitelist
In reply to Josh Poimboeufstackvalidate reports a false positive warning for the ljmp instruction in machine_real_restart(). Normally, ljmp isn't allowed in a function, but this is a special case where it's jumping into real mode.
Add the jumps to a whitelist which tells stackvalidate to ignore them.
--- arch/x86/kernel/reboot.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +5 -2
@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/tboot.h> #include <linux/delay.h> +#include <linux/stackvalidate.h> #include <acpi/reboot.h> #include <asm/io.h> #include <asm/apic.h>
@@ -97,11 +98,13 @@ void __noreturn machine_real_restart(unsigned int type)/* Jump to the identity-mapped low memory code */ #ifdef CONFIG_X86_32 - asmvolatile ("jmpl *%0" : : + asmvolatile (STACKVALIDATE_IGNORE_INSN +"jmpl *%0;" : :"rm" (real_mode_header->machine_real_restart_asm),"a" (type));#else - asmvolatile ("ljmpl *%0" : : + asmvolatile (STACKVALIDATE_IGNORE_INSN +"ljmpl *%0" : :"m" (real_mode_header->machine_real_restart_asm),"D" (type));#endif
Signature · 1 line
2.1.0
PATCH v8 12/21 x86/xen: Add xen_cpuid() and xen_setup_gdt() to stackvalidate whitelists
In reply to Josh Poimboeufstackvalidate reports the following false positive warnings:
stackvalidate: arch/x86/xen/enlighten.o: xen_cpuid()+0x41: can't find jump dest instruction at .text+0x108
stackvalidate: arch/x86/xen/enlighten.o: xen_setup_gdt.constprop.23()+0x2e: kernel entry/exit from callable instruction
The first warning is due to xen_cpuid()'s use of XEN_EMULATE_PREFIX to insert some fake instructions which stackvalidate doesn't know how to decode.
The second warning is due to xen_setup_gdt()'s use of an lret instruction, which stackvalidate normally doesn't allow in callable functions. But this seems to be a valid use of the instruction.
Add both functions to the stackvalidate whitelist. This results in no actual changes to the generated code.
--- arch/x86/xen/enlighten.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/xen/enlighten.c +3 -1
@@ -32,6 +32,7 @@#include <linux/gfp.h> #include <linux/memblock.h> #include <linux/edd.h> +#include <linux/stackvalidate.h> #include <xen/xen.h> #include <xen/events.h>
@@ -345,8 +346,8 @@ static void xen_cpuid(unsigned int *ax, unsigned int *bx, *cx &= maskecx; *cx |= setecx; *dx &= maskedx; - } +STACKVALIDATE_IGNORE_FUNC (xen_cpuid);static bool __init xen_check_mwait (void ) {
@@ -1403,6 +1404,7 @@ static void __ref xen_setup_gdt(int cpu) pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry; pv_cpu_ops.load_gdt = xen_load_gdt; } +STACKVALIDATE_IGNORE_FUNC (xen_setup_gdt);#ifdef CONFIG_XEN_PVH /*
Signature · 1 line
2.1.0
PATCH v8 13/21 x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
In reply to Josh Poimboeufaesni-intel_asm.S has several callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/crypto/aesni-intel_asm.S | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/crypto/aesni-intel_asm.S +19 -0
@@ -31,6 +31,7 @@ #include <linux/linkage.h> #include <asm/inst.h> +#include <asm/frame.h> /* * The following macros are used to move an (un)aligned 16 byte value to/from
@@ -1800,6 +1801,7 @@ ENDPROC(_key_expansion_256b) * unsigned int key_len) */ ENTRY(aesni_set_key) + FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx
@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)
@@ -1912,6 +1915,7 @@ ENDPROC(aesni_set_key) * void aesni_enc(struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src) */ ENTRY(aesni_enc) + FRAME #ifndef __x86_64__ pushl KEYP pushl KLEN
@@ -1927,6 +1931,7 @@ ENTRY(aesni_enc) popl KLEN popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_enc)
@@ -2101,6 +2106,7 @@ ENDPROC(_aesni_enc4) * void aesni_dec (struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src) */ ENTRY(aesni_dec) + FRAME #ifndef __x86_64__ pushl KEYP pushl KLEN
@@ -2117,6 +2123,7 @@ ENTRY(aesni_dec) popl KLEN popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_dec)
@@ -2292,6 +2299,7 @@ ENDPROC(_aesni_dec4) * size_t len) */ ENTRY(aesni_ecb_enc) + FRAME #ifndef __x86_64__ pushl LEN pushl KEYP
@@ -2342,6 +2350,7 @@ ENTRY(aesni_ecb_enc) popl KEYP popl LEN #endif + ENDFRAME ret ENDPROC(aesni_ecb_enc)
@@ -2350,6 +2359,7 @@ ENDPROC(aesni_ecb_enc) * size_t len); */ ENTRY(aesni_ecb_dec) + FRAME #ifndef __x86_64__ pushl LEN pushl KEYP
@@ -2401,6 +2411,7 @@ ENTRY(aesni_ecb_dec) popl KEYP popl LEN #endif + ENDFRAME ret ENDPROC(aesni_ecb_dec)
@@ -2409,6 +2420,7 @@ ENDPROC(aesni_ecb_dec) * size_t len, u8 *iv) */ ENTRY(aesni_cbc_enc) + FRAME #ifndef __x86_64__ pushl IVP pushl LEN
@@ -2443,6 +2455,7 @@ ENTRY(aesni_cbc_enc) popl LEN popl IVP #endif + ENDFRAME ret ENDPROC(aesni_cbc_enc)
@@ -2451,6 +2464,7 @@ ENDPROC(aesni_cbc_enc) * size_t len, u8 *iv) */ ENTRY(aesni_cbc_dec) + FRAME #ifndef __x86_64__ pushl IVP pushl LEN
@@ -2534,6 +2548,7 @@ ENTRY(aesni_cbc_dec) popl LEN popl IVP #endif + ENDFRAME ret ENDPROC(aesni_cbc_dec)
@@ -2598,6 +2613,7 @@ ENDPROC(_aesni_inc) * size_t len, u8 *iv) */ ENTRY(aesni_ctr_enc) + FRAME cmp $16, LEN jb .Lctr_enc_just_ret mov 480(KEYP), KLEN
@@ -2651,6 +2667,7 @@ ENTRY(aesni_ctr_enc) .Lctr_enc_ret: movups IV, (IVP) .Lctr_enc_just_ret: + ENDFRAME ret ENDPROC(aesni_ctr_enc)
@@ -2677,6 +2694,7 @@ ENDPROC(aesni_ctr_enc) * bool enc, u8 *iv) */ ENTRY(aesni_xts_crypt8) + FRAME cmpb $0, %cl movl $0, %ecx movl $240, %r10d
@@ -2777,6 +2795,7 @@ ENTRY(aesni_xts_crypt8) pxor INC, STATE4 movdqu STATE4, 0x70(OUTP) + ENDFRAME ret ENDPROC(aesni_xts_crypt8)
Signature · 1 line
2.1.0
PATCH v8 14/21 x86/asm/crypto: Move .Lbswap_mask data to .rodata section
In reply to Josh Poimboeufstackvalidate reports the following warning:
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: _aesni_inc_init(): can't find starting instruction
stackvalidate gets confused when it tries to disassemble the following data in the .text section:
.Lbswap_mask:
.byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
Move it to .rodata which is a more appropriate section for read-only data.
--- arch/x86/crypto/aesni-intel_asm.S | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/crypto/aesni-intel_asm.S +2 -0
@@ -2553,9 +2553,11 @@ ENTRY(aesni_cbc_dec) ENDPROC(aesni_cbc_dec) #ifdef __x86_64__ +.pushsection .rodata .align 16 .Lbswap_mask: .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +.popsection /* * _aesni_inc_init: internal ABI
Signature · 1 line
2.1.0
PATCH v8 15/21 x86/asm/crypto: Move jump_table to .rodata section
In reply to Josh Poimboeufstackvalidate reports the following warning:
stackvalidate: arch/x86/crypto/crc32c-pcl-intel-asm_64.o: crc_pcl()+0x11dd: can't decode instruction
It gets confused when trying to decode jump_table data. Move jump_table to the .rodata section which is a more appropriate home for read-only data.
--- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/crypto/crc32c-pcl-intel-asm_64.S +4 -4
@@ -170,8 +170,8 @@ continue_block: ## branch into array lea jump_table(%rip), bufp movzxw (bufp, %rax, 2), len - offset=crc_array-jump_table - lea offset(bufp, len, 1), bufp + lea crc_array(%rip), bufp + lea (bufp, len, 1), bufp jmp *bufp ################################################################
@@ -310,7 +310,9 @@ do_return: popq %rdi popq %rbx ret +ENDPROC(crc_pcl) +.section .rodata, "a", %progbits ################################################################ ## jump table Table is 129 entries x 2 bytes each ################################################################
@@ -324,13 +326,11 @@ JMPTBL_ENTRY %i i=i+1 .endr -ENDPROC(crc_pcl) ################################################################ ## PCLMULQDQ tables ## Table is 128 entries x 2 words (8 bytes) each ################################################################ -.section .rotata, "a", %progbits .align 8 K_table: .long 0x493c7d27, 0x00000001
Signature · 1 line
2.1.0
PATCH v8 16/21 x86/asm/crypto: Fix frame pointer usage in clmul_ghash_mul/update()
In reply to Josh Poimboeufclmul_ghash_mul() and clmul_ghash_update() are callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/crypto/ghash-clmulni-intel_asm.S +5 -0
@@ -18,6 +18,7 @@ #include <linux/linkage.h> #include <asm/inst.h> +#include <asm/frame.h> .data
@@ -94,6 +95,7 @@ ENDPROC(__clmul_gf128mul_ble) /* void clmul_ghash_mul(char *dst, const u128 *shash) */ ENTRY(clmul_ghash_mul) + FRAME movups (%rdi), DATA movups (%rsi), SHASH movaps .Lbswap_mask, BSWAP
@@ -101,6 +103,7 @@ ENTRY(clmul_ghash_mul) call __clmul_gf128mul_ble PSHUFB_XMM BSWAP DATA movups DATA, (%rdi) + ENDFRAME ret ENDPROC(clmul_ghash_mul)
@@ -109,6 +112,7 @@ ENDPROC(clmul_ghash_mul) * const u128 *shash); */ ENTRY(clmul_ghash_update) + FRAME cmp $16, %rdx jb .Lupdate_just_ret # check length movaps .Lbswap_mask, BSWAP
@@ -128,5 +132,6 @@ ENTRY(clmul_ghash_update) PSHUFB_XMM BSWAP DATA movups DATA, (%rdi) .Lupdate_just_ret: + ENDFRAME ret ENDPROC(clmul_ghash_update)
Signature · 1 line
2.1.0
PATCH v8 17/21 x86/asm/entry: Fix frame pointer usage in thunk functions
In reply to Josh PoimboeufThunk functions are callable non-leaf functions that don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces. Also they aren't annotated as ELF callable functions which can confuse tooling.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled and add the ELF function type.
--- arch/x86/entry/thunk_64.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/entry/thunk_64.S +4 -0
@@ -8,11 +8,14 @@ #include <linux/linkage.h> #include "calling.h" #include <asm/asm.h> +#include <asm/frame.h> /* rdi: arg1 ... normal C conventions. rax is saved/restored. */ .macro THUNK name, func, put_ret_addr_in_rdi=0 .globl \name + .type \name, @function \name: + FRAME /* this one pushes 9 elems, the next one would be %rIP */ pushq %rdi
@@ -62,6 +65,7 @@ restore: popq %rdx popq %rsi popq %rdi + ENDFRAME ret _ASM_NOKPROBE(restore) #endif
Signature · 1 line
2.1.0
PATCH v8 18/21 x86/asm/acpi: Fix frame pointer usage in do_suspend_lowlevel()
In reply to Josh Poimboeufdo_suspend_lowlevel() is a callable non-leaf function which doesn't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create a stack frame for it when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/kernel/acpi/wakeup_64.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kernel/acpi/wakeup_64.S +3 -0
@@ -5,6 +5,7 @@ #include <asm/page_types.h> #include <asm/msr.h> #include <asm/asm-offsets.h> +#include <asm/frame.h> # Copyright 2003 Pavel Machek <[email protected]>, distribute under GPLv2
@@ -39,6 +40,7 @@ bogus_64_magic: jmp bogus_64_magic ENTRY(do_suspend_lowlevel) + FRAME subq $8, %rsp xorl %eax, %eax call save_processor_state
@@ -109,6 +111,7 @@ ENTRY(do_suspend_lowlevel) xorl %eax, %eax addq $8, %rsp + ENDFRAME jmp restore_processor_state ENDPROC(do_suspend_lowlevel)
Signature · 1 line
2.1.0
PATCH v8 19/21 x86/asm: Fix frame pointer usage in rwsem functions
In reply to Josh Poimboeufrwsem.S has several callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/lib/rwsem.S | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/lib/rwsem.S +10 -1
@@ -15,6 +15,7 @@ #include <linux/linkage.h> #include <asm/alternative-asm.h> +#include <asm/frame.h> #define __ASM_HALF_REG(reg) __ASM_SEL(reg, e##reg) #define __ASM_HALF_SIZE(inst) __ASM_SEL(inst##w, inst##l)
@@ -84,24 +85,29 @@ /* Fix up special calling conventions */ ENTRY(call_rwsem_down_read_failed) + FRAME save_common_regs __ASM_SIZE(push,) %__ASM_REG(dx) movq %rax,%rdi call rwsem_down_read_failed __ASM_SIZE(pop,) %__ASM_REG(dx) restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_down_read_failed) ENTRY(call_rwsem_down_write_failed) + FRAME save_common_regs movq %rax,%rdi call rwsem_down_write_failed restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_down_write_failed) ENTRY(call_rwsem_wake) + FRAME /* do nothing if still outstanding active readers */ __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx) jnz 1f
@@ -109,15 +115,18 @@ ENTRY(call_rwsem_wake) movq %rax,%rdi call rwsem_wake restore_common_regs -1: ret +1: ENDFRAME + ret ENDPROC(call_rwsem_wake) ENTRY(call_rwsem_downgrade_wake) + FRAME save_common_regs __ASM_SIZE(push,) %__ASM_REG(dx) movq %rax,%rdi call rwsem_downgrade_wake __ASM_SIZE(pop,) %__ASM_REG(dx) restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_downgrade_wake)
Signature · 1 line
2.1.0
PATCH v8 20/21 x86/asm/efi: Fix frame pointer usage in efi_call()
In reply to Josh Poimboeufefi_call() is a callable non-leaf function which doesn't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create a stack frame for it when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/platform/efi/efi_stub_64.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/platform/efi/efi_stub_64.S +3 -0
@@ -11,6 +11,7 @@ #include <asm/msr.h> #include <asm/processor-flags.h> #include <asm/page_types.h> +#include <asm/frame.h> #define SAVE_XMM \ mov %rsp, %rax; \
@@ -74,6 +75,7 @@ .endm ENTRY(efi_call) + FRAME SAVE_XMM mov (%rsp), %rax mov 8(%rax), %rax
@@ -88,6 +90,7 @@ ENTRY(efi_call) RESTORE_PGT addq $48, %rsp RESTORE_XMM + ENDFRAME ret ENDPROC(efi_call)
Signature · 1 line
2.1.0
PATCH v8 21/21 x86/asm/power: Fix frame pointer usage in hibernate_asm_64.S
In reply to Josh Poimboeufswsusp_arch_suspend() and restore_registers() are callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces. Also they aren't annotated as ELF callable functions which can confuse tooling.
Create a stack frame for them when CONFIG_FRAME_POINTER is enabled and give them proper ELF function annotations.
--- arch/x86/power/hibernate_asm_64.S | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/power/hibernate_asm_64.S +7 -0
@@ -21,8 +21,10 @@ #include <asm/page_types.h> #include <asm/asm-offsets.h> #include <asm/processor-flags.h> +#include <asm/frame.h> ENTRY(swsusp_arch_suspend) + FRAME movq $saved_context, %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax)
@@ -50,7 +52,9 @@ ENTRY(swsusp_arch_suspend) movq %rax, restore_cr3(%rip) call swsusp_save + ENDFRAME ret +ENDPROC(swsusp_arch_suspend) ENTRY(restore_image) /* switch to temporary page tables */
@@ -107,6 +111,7 @@ ENTRY(core_restore_code) */ ENTRY(restore_registers) + FRAME /* go back to the original page tables */ movq %rbx, %cr3
@@ -147,4 +152,6 @@ ENTRY(restore_registers) /* tell the hibernation core that we've just restored the memory */ movq %rax, in_suspend(%rip) + ENDFRAME ret +ENDPROC(restore_registers)
Signature · 1 line
2.1.0
Quoted 6 lines · Josh Poimboeuf
This is v8 of the compile-time stack validation patch set, based on the tip/master branch.
The frame pointer macros are still called FRAME and ENDFRAME because I don't think we converged on anything else yet. Otherwise I tried to address all the other review comments from v7.
So I think:
FRAME_START
FRAME_END
would be OK.
They could be added as new aliases, keeping the old ones as well, while in the new patches you should use this new form.
Thanks,
Ingo
Quoted 10 lines · Ingo Molnar
Quoted 6 lines · Josh Poimboeuf
This is v8 of the compile-time stack validation patch set, based on the tip/master branch.
The frame pointer macros are still called FRAME and ENDFRAME because I don't think we converged on anything else yet. Otherwise I tried to address all the other review comments from v7.
Quoted 6 lines
So I think:
FRAME_START
FRAME_ENDwould be OK.
Thanks, that sounds good to me.
Quoted 2 lines
They could be added as new aliases, keeping the old ones as well, while in the new patches you should use this new form.
Turns out nobody uses the existing FRAME/ENDFRAME macros, so I'll just rename them.
Signature · 1 line
Josh
Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Josh Poimboeuf
This is v8 of the compile-time stack validation patch set, based on the tip/master branch.
Quoted 8 lines
Quoted 3 lines
The frame pointer macros are still called FRAME and ENDFRAME because I don't think we converged on anything else yet. Otherwise I tried to address all the other review comments from v7.
So I think:
FRAME_START
FRAME_END
(One last tweak to the bikeshed)
The antonym of END is BEGIN. So how about:
FRAME_BEGIN
FRAME_END
Signature · 1 line
Josh
Quoted 16 lines · Josh Poimboeuf
Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Josh Poimboeuf
This is v8 of the compile-time stack validation patch set, based on the tip/master branch.
Quoted 8 lines
Quoted 3 lines
The frame pointer macros are still called FRAME and ENDFRAME because I don't think we converged on anything else yet. Otherwise I tried to address all the other review comments from v7.
So I think:
FRAME_START
FRAME_END
Quoted 6 lines
(One last tweak to the bikeshed)
The antonym of END is BEGIN. So how about:
FRAME_BEGIN
FRAME_END
Ok, so thesaurus.com reminds me that words can have more than one antonym, and that START and BEGIN are both antonyms of END.
Personally, when I START something I usually FINISH it or even STOP it.
So I'll stop the bikeshedding I started and go with BEGIN/END.
Signature · 1 line
Josh