Add static DEXCR support
Subsystems (4)
PATCH v2 2/12 powerpc/ptrace: Add missing <linux/regset.h> include
In reply to Benjamin Grayptrace-decl.h uses user_regset_get2_fn (among other things) from regset.h. While all current users of ptrace-decl.h include regset.h before it anyway, it adds an implicit ordering dependency and breaks source tooling that tries to inspect ptrace-decl.h by itself.
---
v2: * New in v2
--- arch/powerpc/kernel/ptrace/ptrace-decl.h | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kernel/ptrace/ptrace-decl.h +2 -0
@@ -1,5 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ +#include <linux/regset.h> +/* * Set of msr bits that gdb can change on behalf of a process. */
Signature · 1 line
2.39.2
PATCH v2 3/12 powerpc/dexcr: Add initial Dynamic Execution Control Register (DEXCR) support
In reply to Benjamin GrayISA 3.1B introduces the Dynamic Execution Control Register (DEXCR). It is a per-cpu register that allows control over various CPU behaviours including branch hint usage, indirect branch speculation, and hashst/hashchk support.
Add some definitions and basic support for the DEXCR in the kernel. Right now it just
* Zero initialises the DEXCR and HASHKEYR when a CPU onlines.
* Clears them in reset_sprs().
* Detects when the NPHIE aspect is supported (the others don't get looked at in this series, so there's no need to waste a CPU_FTR on them).
We initialise the HASHKEYR to ensure that all cores have the same key, so an HV enforced NPHIE + swapping cores doesn't randomly crash a process using hash instructions. The stores to HASHKEYR are unconditional because the ISA makes no mention of the SPR being missing if support for doing the hashes isn't present. So all that would happen is the HASHKEYR value gets ignored. This helps slightly if NPHIE detection fails; e.g., we currently only detect it on pseries.
---
v2: * More definitions for ptrace
v1: * Only make a CPU feature for NPHIE. We only need to know if the
hashst/hashchk functionality is supported for a static DEXCR.
* Initialise the DEXCR to 0 when each CPU comes online. Remove the dexcr_init() and get_thread_dexcr() functions.
* No longer track the DEXCR in a per-thread field.
* Remove the made-up Opal features
--- arch/powerpc/include/asm/book3s/64/kexec.h | 5 +++++ arch/powerpc/include/asm/cputable.h | 4 +++- arch/powerpc/include/asm/reg.h | 10 ++++++++++ arch/powerpc/kernel/cpu_setup_power.c | 8 ++++++++ arch/powerpc/kernel/prom.c | 1 + 5 files changed, 27 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/book3s/64/kexec.h +5 -0
@@ -21,6 +21,11 @@ static inline void reset_sprs(void)plpar_set_ciabr (0 ); } +if (cpu_has_feature (CPU_FTR_ARCH_31 )) { +mtspr (SPRN_DEXCR ,0 ); +mtspr (SPRN_HASHKEYR ,0 ); + } +/* Do we need isync()? We are going via a kexec reset */ isync (); }
arch/powerpc/include/asm/cputable.h +3 -1
@@ -192,6 +192,7 @@ static inline void cpu_feature_keys_init(void) { }#define CPU_FTR_P9_RADIX_PREFETCH_BUG LONG_ASM_CONST(0x0002000000000000)#define CPU_FTR_ARCH_31 LONG_ASM_CONST(0x0004000000000000)#define CPU_FTR_DAWR1 LONG_ASM_CONST(0x0008000000000000) +#define CPU_FTR_DEXCR_NPHIE LONG_ASM_CONST(0x0010000000000000)#ifndef __ASSEMBLY__
@@ -451,7 +452,8 @@ static inline void cpu_feature_keys_init(void) { } CPU_FTR_CFAR | CPU_FTR_HVMODE | CPU_FTR_VMX_COPY | \ CPU_FTR_DBELL | CPU_FTR_HAS_PPR | CPU_FTR_ARCH_207S | \ CPU_FTR_ARCH_300 | CPU_FTR_ARCH_31 | \ - CPU_FTR_DAWR | CPU_FTR_DAWR1) + CPU_FTR_DAWR | CPU_FTR_DAWR1 | \ + CPU_FTR_DEXCR_NPHIE)#define CPU_FTRS_CELL (CPU_FTR_LWSYNC | \ CPU_FTR_PPCAS_ARCH_V2 | CPU_FTR_CTRL | \ CPU_FTR_ALTIVEC_COMP | CPU_FTR_MMCRA | CPU_FTR_SMT | \
arch/powerpc/include/asm/reg.h +10 -0
@@ -384,7 +384,17 @@#define SPRN_HRMOR 0x139/* Real mode offset register */ #define SPRN_HSRR0 0x13A/* Hypervisor Save/Restore 0 */ #define SPRN_HSRR1 0x13B/* Hypervisor Save/Restore 1 */ +#define SPRN_HDEXCR_RO 0x1C7/* Hypervisor DEXCR (non-privileged, readonly) */ +#define SPRN_HASHKEYR 0x1D4/* Non-privileged hashst/hashchk key register */ +#define SPRN_HDEXCR 0x1D7/* Hypervisor dynamic execution control register */ +#define SPRN_DEXCR_RO 0x32C/* DEXCR (non-privileged, readonly) */ #define SPRN_ASDR 0x330/* Access segment descriptor register */ +#define SPRN_DEXCR 0x33C/* Dynamic execution control register */ +#define DEXCR_PR_BIT (aspect) PPC_BIT(32 + (aspect)) +#define DEXCR_PR_SBHE DEXCR_PR_BIT(0)/* Speculative Branch Hint Enable */ +#define DEXCR_PR_IBRTPD DEXCR_PR_BIT(3)/* Indirect Branch Recurrent Target Prediction Disable */ +#define DEXCR_PR_SRAPD DEXCR_PR_BIT(4)/* Subroutine Return Address Prediction Disable */ +#define DEXCR_PR_NPHIE DEXCR_PR_BIT(5)/* Non-Privileged Hash Instruction Enable */ #define SPRN_IC 0x350/* Virtual Instruction Count */ #define SPRN_VTB 0x351/* Virtual Time Base */ #define SPRN_LDBAR 0x352/* LD Base Address Register */
arch/powerpc/kernel/cpu_setup_power.c +8 -0
@@ -126,6 +126,12 @@ static void init_PMU_ISA31(void)mtspr (SPRN_MMCR0 ,MMCR0_FC |MMCR0_PMCCEXT ); } +static void init_DEXCR (void ) +{ +mtspr (SPRN_DEXCR ,0 ); +mtspr (SPRN_HASHKEYR ,0 ); +} +/* * Note that we can be called twice of pseudo-PVRs. * The parameter offset is not used.
@@ -241,6 +247,7 @@ void __setup_cpu_power10(unsigned long offset, struct cpu_spec *t)init_FSCR_power10 ();init_PMU ();init_PMU_ISA31 (); +init_DEXCR ();if (!init_hvmode_206 (t))return ;
@@ -263,6 +270,7 @@ void __restore_cpu_power10(void)init_FSCR_power10 ();init_PMU ();init_PMU_ISA31 (); +init_DEXCR (); msr =mfmsr ();if (!(msr &MSR_HV ))
arch/powerpc/kernel/prom.c +1 -0
@@ -182,6 +182,7 @@ static struct ibm_feature ibm_pa_features[] __initdata = { .cpu_user_ftrs2 = PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_HTM_NOSC_COMP }, { .pabyte = 64, .pabit = 0, .cpu_features = CPU_FTR_DAWR1 }, + { .pabyte = 68, .pabit = 5, .cpu_features = CPU_FTR_DEXCR_NPHIE }, };/*
Signature · 1 line
2.39.2
PATCH v2 4/12 powerpc/dexcr: Handle hashchk exception
In reply to Benjamin GrayRecognise and pass the appropriate signal to the user program when a hashchk instruction triggers. This is independent of allowing configuration of DEXCR[NPHIE], as a hypervisor can enforce this aspect regardless of the kernel.
The signal mirrors how ARM reports their similar check failure. For example, their FPAC handler in arch/arm64/kernel/traps.c do_el0_fpac() does this. When we fail to read the instruction that caused the fault we send a segfault, similar to how emulate_math() does it.
---
v1: * Refactor the hashchk check to return 0 on success, an error
code on failure. Determine what to do based on specific error code.
* Motivate signal and code
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/processor.h | 9 +++++++ arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/dexcr.c | 36 +++++++++++++++++++++++++++ arch/powerpc/kernel/traps.c | 10 ++++++++ 5 files changed, 57 insertions(+) create mode 100644 arch/powerpc/kernel/dexcr.c
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -215,6 +215,7 @@#define OP_31_XOP_STFSX 663#define OP_31_XOP_STFSUX 695#define OP_31_XOP_STFDX 727 +#define OP_31_XOP_HASHCHK 754#define OP_31_XOP_STFDUX 759#define OP_31_XOP_LHBRX 790#define OP_31_XOP_LFIWAX 855
arch/powerpc/include/asm/processor.h +9 -0
@@ -455,6 +455,15 @@ int exit_vmx_usercopy(void);int enter_vmx_ops (void );void *exit_vmx_ops (void *dest); +#ifdef CONFIG_PPC_BOOK3S_64 +int check_hashchk_trap (struct pt_regs const *regs); +#else +static inline int check_hashchk_trap (struct pt_regs const *regs) +{ +return -EINVAL ; +} +#endif /* CONFIG_PPC_BOOK3S_64 */ +#endif /* __KERNEL__ */ #endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_PROCESSOR_H */
arch/powerpc/kernel/Makefile +1 -0
@@ -88,6 +88,7 @@ obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_PPC_DAWR) += dawr.o obj-$(CONFIG_PPC_BOOK3S_64) += cpu_setup_ppc970.o cpu_setup_pa6t.o obj-$(CONFIG_PPC_BOOK3S_64) += cpu_setup_power.o +obj-$(CONFIG_PPC_BOOK3S_64) += dexcr.o obj-$(CONFIG_PPC_BOOK3S_64) += mce.o mce_power.o obj-$(CONFIG_PPC_BOOK3E_64) += exceptions-64e.o idle_64e.o obj-$(CONFIG_PPC_BARRIER_NOSPEC) += security.o
arch/powerpc/kernel/dexcr.c new file +36 -0
@@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* DEXCR infrastructure +* +* Copyright 2023, Benjamin Gray, IBM Corporation. +*/ +#include <linux/compiler_types.h> + +#include <asm/cpu_has_feature.h> +#include <asm/cputable.h> +#include <asm/disassemble.h> +#include <asm/errno.h> +#include <asm/inst.h> +#include <asm/ppc-opcode.h> +#include <asm/ptrace.h> +#include <asm/reg.h> + +int check_hashchk_trap (struct pt_regs const *regs) +{ +ppc_inst_t insn; + +if (!cpu_has_feature (CPU_FTR_DEXCR_NPHIE )) +return -EINVAL ; + +if (!user_mode (regs)) +return -EINVAL ; + +if (get_user_instr (insn, (void __user *)regs->nip)) +return -EFAULT ; + +if (ppc_inst_primary_opcode (insn) !=31 || +get_xop (ppc_inst_val (insn)) !=OP_31_XOP_HASHCHK ) +return -EINVAL ; + +return 0 ; +}
arch/powerpc/kernel/traps.c +10 -0
@@ -1516,6 +1516,16 @@ static void do_program_check(struct pt_regs *regs)return ; } } + +switch (check_hashchk_trap (regs)) { +case 0 : +_exception (SIGILL , regs,ILL_ILLOPN , regs->nip); +return ; +case -EFAULT : +_exception (SIGSEGV , regs,SEGV_MAPERR , regs->nip); +return ; + } +_exception (SIGTRAP , regs,TRAP_BRKPT , regs->nip);return ; }
Signature · 1 line
2.39.2
PATCH v2 1/12 powerpc/book3s: Add missing <linux/sched.h> include
In reply to Benjamin GrayThe functions here use struct thread_struct fields, so need to import the full definition from <linux/sched.h>. The <asm/current.h> header that defines current only forward declares struct thread_struct.
Failing to include this <linux/sched.h> header leads to a compilation error when a translation unit does not also include <linux/sched.h> indirectly.
---
v1: * Add npiggin reviewed-by
--- arch/powerpc/include/asm/book3s/64/kup.h | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/include/asm/book3s/64/kup.h +1 -0
@@ -194,6 +194,7 @@#else /* !__ASSEMBLY__ */ #include <linux/jump_label.h> +#include <linux/sched.h> DECLARE_STATIC_KEY_FALSE (uaccess_flush_key);
Signature · 1 line
2.39.2
PATCH v2 10/12 selftests/powerpc: Add more utility macros
In reply to Benjamin Gray* Include unistd.h for _exit()
* Include stdio.h for fprintf()
* Adds _MSG assertion variants to provide more context behind why a failure occurred.
* Move ARRAY_SIZE macro to utils.h
The _MSG variants and ARRAY_SIZE will be used by the following DEXCR selftests.
---
v1: * Remove the signal handler variants
* Describe why headers are included
--- .../testing/selftests/powerpc/include/utils.h | 27 ++++++++++++++++++- .../powerpc/pmu/sampling_tests/misc.h | 2 -- 2 files changed, 26 insertions(+), 3 deletions(-)
tools/testing/selftests/powerpc/include/utils.h +26 -1
@@ -9,11 +9,17 @@#define __cacheline_aligned __attribute__((aligned(128)))#include <stdint.h> +#include <stdio.h> #include <stdbool.h> #include <linux/auxvec.h> #include <linux/perf_event.h> #include <asm/cputable.h> #include "reg.h" +#include <unistd.h> + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) +#endif /* Avoid headaches with PRI?64 - just use %ll? always */ typedef unsigned long long u64 ;
@@ -64,7 +70,6 @@ struct perf_event_read { };#if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ (2 ,30 ) -#include <unistd.h> #include <sys/syscall.h> static inline pid_t gettid (void )
@@ -113,6 +118,16 @@ do { \ } \ } while (0) +#define FAIL_IF_MSG (x, msg) \ +do { \ + if ((x)) { \ + fprintf(stderr, \ + "[FAIL] Test FAILED on line %d: %s\n", \ + __LINE__, msg); \ + return 1; \ + } \ +} while (0) +#define FAIL_IF_EXIT (x) \ do { \ if ((x)) { \
@@ -122,6 +137,16 @@ do { \ } \ } while (0) +#define FAIL_IF_EXIT_MSG (x, msg) \ +do { \ + if ((x)) { \ + fprintf(stderr, \ + "[FAIL] Test FAILED on line %d: %s\n", \ + __LINE__, msg); \ + _exit(1); \ + } \ +} while (0) +/* The test harness uses this, yes it's gross */ #define MAGIC_SKIP_RETURN_VALUE 99
tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h +0 -2
@@ -18,8 +18,6 @@#define MMCR1_RSQ 0x200000000000ULL/* radix scope qual field */ #define BHRB_DISABLE 0x2000000000ULL/* MMCRA BHRB DISABLE bit */ -#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -extern int ev_mask_pmcxsel, ev_shift_pmcxsel;extern int ev_mask_marked, ev_shift_marked;extern int ev_mask_comb, ev_shift_comb;
Signature · 1 line
2.39.2
PATCH v2 6/12 powerpc/dexcr: Support custom default DEXCR value
In reply to Benjamin GrayMake the DEXCR value configurable at config time. Intentionally don't limit possible values to support future aspects without needing kernel updates.
The default config value enables hashst/hashchk in problem state. This should be safe, as generally software needs to request these instructions be included in the first place.
---
New in v1
Preface with: I'm note sure on the best place to put the config.
I also don't think there's any need to zero out unknown/unsupported bits. Reserved implies they are ignored by the hardware (from my understanding of the ISA). Current P10s boot with all bits set; lsdexcr (later patch) reports
uDEXCR: ff000000 (SBHE, IBRTPD, SRAPD, NPHIE, PHIE, unknown)
when you try to read it back. Leaving them be also makes it easier to support newer aspects without a kernel update.
If arbitrary value support isn't important, it's probably a nicer interface to make each aspect an entry in a menu.
Future work may include dynamic DEXCR controls via prctl() and sysfs. The dynamic controls would be able to override this default DEXCR on a per-process basis. A stronger "PPC_ENFORCE_USER_ROP_PROCTETION" config may be required at such a time to prevent dynamically disabling the hash checks.
--- arch/powerpc/Kconfig | 14 ++++++++++++++ arch/powerpc/kernel/cpu_setup_power.c | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-)
arch/powerpc/Kconfig +14 -0
@@ -1035,6 +1035,20 @@ config PPC_MEM_KEYS If unsure, say y. +config PPC_DEXCR_DEFAULT + hex "Default DEXCR value" + default 0x0000000004000000 + depends on PPC_BOOK3S_64 + help + Power10 introduces the Dynamic Execution Control Register (DEXCR) + to provide fine grained control over various speculation and + security capabilities. This is used as the default DEXCR value. + + It is a 64 bit value that splits into 32 bits for supervisor mode + and 32 bits for problem state. The default config value enables + the hashst/hashck instructions in userspace. See the ISA for + specifics of what each bit controls. + config PPC_SECURE_BOOT prompt "Enable secure boot support" bool
arch/powerpc/kernel/cpu_setup_power.c +2 -1
@@ -10,6 +10,7 @@#include <asm/reg.h> #include <asm/synch.h> #include <linux/bitops.h> +#include <linux/kconfig.h> #include <asm/cputable.h> #include <asm/cpu_setup.h>
@@ -128,7 +129,7 @@ static void init_PMU_ISA31(void)static void init_DEXCR (void ) { -mtspr (SPRN_DEXCR ,0 ); +mtspr (SPRN_DEXCR ,CONFIG_PPC_DEXCR_DEFAULT );mtspr (SPRN_HASHKEYR ,0 ); }
Signature · 1 line
2.39.2
PATCH v2 9/12 Documentation: Document PowerPC kernel DEXCR interface
In reply to Benjamin GrayDescribe the DEXCR and document how to configure it.
---
v2: * Document coredump & ptrace support
v1: * Remove the dynamic control docs, describe the static config
option
This documentation is a little bare for now, but will be expanded on when dynamic DEXCR control is added.
--- Documentation/powerpc/dexcr.rst | 58 +++++++++++++++++++++++++++++++++ Documentation/powerpc/index.rst | 1 + 2 files changed, 59 insertions(+) create mode 100644 Documentation/powerpc/dexcr.rst
Documentation/powerpc/dexcr.rst new file +58 -0
@@ -0,0 +1,58 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +========================================== +DEXCR (Dynamic Execution Control Register) +========================================== + +Overview +======== + +The DEXCR is a privileged special purpose register (SPR) introduced in +PowerPC ISA 3.1B (Power10) that allows per-cpu control over several dynamic +execution behaviours. These behaviours include speculation (e.g., indirect +branch target prediction) and enabling return-oriented programming (ROP) +protection instructions. + +The execution control is exposed in hardware as up to 32 bits ('aspects') in +the DEXCR. Each aspect controls a certain behaviour, and can be set or cleared +to enable/disable the aspect. There are several variants of the DEXCR for +different purposes: + +DEXCR + A privileged SPR that can control aspects for userspace and kernel space +HDEXCR + A hypervisor-privileged SPR that can control aspects for the hypervisor and + enforce aspects for the kernel and userspace. +UDEXCR + An optional ultravisor-privileged SPR that can control aspects for the ultravisor. + +Userspace can examine the current DEXCR state using a dedicated SPR that +provides a non-privileged read-only view of the userspace DEXCR aspects. +There is also an SPR that provides a read-only view of the hypervisor enforced +aspects, which ORed with the userspace DEXCR view gives the effective DEXCR +state for a process. + + +Kernel Config +============= + +The kernel supports a static default DEXCR value determined at config time. +Set the ``PPC_DEXCR_DEFAULT`` config to the value you want all processes to +use. + + +coredump and ptrace +=================== + +The userspace values of the DEXCR and HDEXCR (in this order) are exposed under +``NT_PPC_DEXCR``. These are each 32 bits and readonly, and are intended to +assist with core dumps. The DEXCR may be made writable in future. + +If the kernel config ``CONFIG_CHECKPOINT_RESTORE`` is enabled, then +``NT_PPC_HASHKEYR`` is available and exposes the HASHKEYR value of the process +for reading and writing. This is a tradeoff between increased security and +checkpoint/restore support: a process should normally have no need to know its +secret key, but restoring a process requires setting its original key. The key +therefore appears in core dumps, and an attacker may be able to retrieve it from +a coredump and effectively bypass ROP protection on any threads that share this +key (potentially all threads from the same parent that have not run ``exec()``).
Documentation/powerpc/index.rst +1 -0
@@ -15,6 +15,7 @@ powerpc cxl cxlflash dawr-power9 + dexcr dscr eeh-pci-error-recovery elf_hwcaps
Signature · 1 line
2.39.2
PATCH v2 12/12 selftests/powerpc/dexcr: Add DEXCR status utility lsdexcr
In reply to Benjamin GrayAdd a utility 'lsdexcr' to print the current DEXCR status. Useful for quickly checking the status such as when debugging test failures or verifying the new default DEXCR does what you want (for userspace at least). Example output:
# ./lsdexcr
uDEXCR: 04000000 (NPHIE)
HDEXCR: 00000000
Effective: 04000000 (NPHIE) SBHE (0): clear (Speculative branch hint enable)
IBRTPD (3): clear (Indirect branch recurrent target ...)
SRAPD (4): clear (Subroutine return address ...) NPHIE * (5): set (Non-privileged hash instruction enable)
PHIE (6): clear (Privileged hash instruction enable)
DEXCR[NPHIE] enabled: hashst/hashchk working
---
v1: * Report if hashst/hashchk actually does something
--- .../selftests/powerpc/dexcr/.gitignore | 1 + .../testing/selftests/powerpc/dexcr/Makefile | 2 + .../testing/selftests/powerpc/dexcr/lsdexcr.c | 141 ++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 tools/testing/selftests/powerpc/dexcr/lsdexcr.c
tools/testing/selftests/powerpc/dexcr/.gitignore +1 -0
@@ -1 +1,2 @@ hashchk_test +lsdexcr
tools/testing/selftests/powerpc/dexcr/Makefile +2 -0
@@ -1,7 +1,9 @@ TEST_GEN_PROGS := hashchk_test +TEST_GEN_FILES := lsdexcr include ../../lib.mk $(OUTPUT)/hashchk_test: CFLAGS += -fno-pie $(call cc-option,-mno-rop-protect) $(TEST_GEN_PROGS): ../harness.c ../utils.c ./dexcr.c +$(TEST_GEN_FILES): ../utils.c ./dexcr.c
tools/testing/selftests/powerpc/dexcr/lsdexcr.c new file +141 -0
141 lines not on this page · raw message
Signature · 1 line
2.39.2
v2 cover Add static DEXCR support
Respin with requested ptrace/coredump support.
The new support is tested by making a core dump. The result has the expected
LINUX 0x00000008 Unknown note type: (0x00000111) description data: 00 00 00 04 00 00 00 00 LINUX 0x00000008 Unknown note type: (0x00000112) description data: 53 6c dd 5e 23 eb 5e 03
Entries when the dump is read with readelf (DEXCR hash NPHIE, HDEXCR is clear, HASHKEYR is random). Endianness is the same as all the other register printers.
v2: * Add ptrace/coredump support
Previous versions:
v1: https://lore.kernel.org/all/[email protected]/
RFC: https://lore.kernel.org/all/[email protected]/
Benjamin Gray (12):
powerpc/book3s: Add missing <linux/sched.h> include powerpc/ptrace: Add missing <linux/regset.h> include powerpc/dexcr: Add initial Dynamic Execution Control Register (DEXCR)
support
powerpc/dexcr: Handle hashchk exception
powerpc/dexcr: Support userspace ROP protection powerpc/dexcr: Support custom default DEXCR value powerpc/ptrace: Expose DEXCR and HDEXCR registers to ptrace powerpc/ptrace: Expose HASHKEYR register to ptrace
Documentation: Document PowerPC kernel DEXCR interface selftests/powerpc: Add more utility macros selftests/powerpc/dexcr: Add hashst/hashchk test selftests/powerpc/dexcr: Add DEXCR status utility lsdexcr
Documentation/powerpc/dexcr.rst | 58 +++++ Documentation/powerpc/index.rst | 1 + arch/powerpc/Kconfig | 14 ++ arch/powerpc/include/asm/book3s/64/kexec.h | 5 + arch/powerpc/include/asm/book3s/64/kup.h | 1 + arch/powerpc/include/asm/cputable.h | 4 +- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/processor.h | 10 + arch/powerpc/include/asm/reg.h | 10 + arch/powerpc/include/uapi/asm/elf.h | 2 + arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/cpu_setup_power.c | 9 + arch/powerpc/kernel/dexcr.c | 36 +++ arch/powerpc/kernel/process.c | 17 ++ arch/powerpc/kernel/prom.c | 1 + arch/powerpc/kernel/ptrace/ptrace-decl.h | 6 + arch/powerpc/kernel/ptrace/ptrace-view.c | 67 +++++- arch/powerpc/kernel/traps.c | 10 + include/uapi/linux/elf.h | 2 + tools/testing/selftests/powerpc/Makefile | 1 + .../selftests/powerpc/dexcr/.gitignore | 2 + .../testing/selftests/powerpc/dexcr/Makefile | 9 + tools/testing/selftests/powerpc/dexcr/dexcr.c | 132 ++++++++++ tools/testing/selftests/powerpc/dexcr/dexcr.h | 49 ++++ .../selftests/powerpc/dexcr/hashchk_test.c | 227 ++++++++++++++++++ .../testing/selftests/powerpc/dexcr/lsdexcr.c | 141 +++++++++++ tools/testing/selftests/powerpc/include/reg.h | 4 + .../testing/selftests/powerpc/include/utils.h | 31 ++- .../powerpc/pmu/sampling_tests/misc.h | 2 - tools/testing/selftests/powerpc/utils.c | 24 ++ 30 files changed, 872 insertions(+), 5 deletions(-) create mode 100644 Documentation/powerpc/dexcr.rst create mode 100644 arch/powerpc/kernel/dexcr.c create mode 100644 tools/testing/selftests/powerpc/dexcr/.gitignore create mode 100644 tools/testing/selftests/powerpc/dexcr/Makefile create mode 100644 tools/testing/selftests/powerpc/dexcr/dexcr.c create mode 100644 tools/testing/selftests/powerpc/dexcr/dexcr.h create mode 100644 tools/testing/selftests/powerpc/dexcr/hashchk_test.c create mode 100644 tools/testing/selftests/powerpc/dexcr/lsdexcr.c
base-commit: 0f98241d5ef5c3bb4c5ca07ceee3a825d79999fd --
2.39.2
PATCH v2 7/12 powerpc/ptrace: Expose DEXCR and HDEXCR registers to ptrace
The DEXCR register is of interest when ptracing processes. Currently it is static, but eventually will be dynamically controllable by a process. If a process can control its own, then it is useful for it to be ptrace-able to (e.g., for checkpoint-restore functionality).
It is also relevant to core dumps (the NPHIE aspect in particular), which use the ptrace mechanism (or is it the other way around?) to decide what to dump. The HDEXCR is useful here too, as the NPHIE aspect may be set in the HDEXCR without being set in the DEXCR. Although the HDEXCR is per-cpu and we don't track it in the task struct (it's useless in normal operation), it would be difficult to imagine why a hypervisor would set it to different values within a guest. A hypervisor cannot safely set NPHIE differently at least, as that would break programs.
Expose a read-only view of the userspace DEXCR and HDEXCR to ptrace. The HDEXCR is always readonly, and is useful for diagnosing the core dumps (as the HDEXCR may set NPHIE without the DEXCR setting it).
---
v2: * New in v2
--- arch/powerpc/include/uapi/asm/elf.h | 1 + arch/powerpc/kernel/ptrace/ptrace-decl.h | 1 + arch/powerpc/kernel/ptrace/ptrace-view.c | 31 +++++++++++++++++++++++- include/uapi/linux/elf.h | 1 + 4 files changed, 33 insertions(+), 1 deletion(-)
arch/powerpc/include/uapi/asm/elf.h +1 -0
@@ -98,6 +98,7 @@#define ELF_NEBB 3/* includes ebbrr, ebbhr, bescr */ #define ELF_NPMU 5/* includes siar, sdar, sier, mmcr2, mmcr0 */ #define ELF_NPKEY 3/* includes amr, iamr, uamor */ +#define ELF_NDEXCR 2/* includes dexcr, hdexcr */ typedef unsigned long elf_greg_t64 ;typedef elf_greg_t64 elf_gregset_t64 [ELF_NGREG ];
arch/powerpc/kernel/ptrace/ptrace-decl.h +1 -0
@@ -57,6 +57,7 @@ enum powerpc_regset {REGSET_TAR ,/* TAR register */ REGSET_EBB ,/* EBB registers */ REGSET_PMR ,/* Performance Monitor Registers */ +REGSET_DEXCR ,/* DEXCR registers */ #endif #ifdef CONFIG_PPC_MEM_KEYS REGSET_PKEY, /* AMR register */
arch/powerpc/kernel/ptrace/ptrace-view.c +30 -1
@@ -454,7 +454,31 @@ static int pmu_set(struct task_struct *target, const struct user_regset *regset, 5 * sizeof(unsigned long)); return ret; } -#endif + +static int dexcr_active (struct task_struct *target,const struct user_regset *regset) +{ +if (!cpu_has_feature (CPU_FTR_ARCH_31 )) +return -ENODEV ; + +return regset->n; +} + +static int dexcr_get (struct task_struct *target,const struct user_regset *regset, +struct membuf to) +{ +if (!cpu_has_feature (CPU_FTR_ARCH_31 )) +return -ENODEV ; + +membuf_store (&to, (unsigned int )CONFIG_PPC_DEXCR_DEFAULT ); + +/* +* Technically the HDEXCR is per-cpu, but a hypervisor can't reasonably +* change it between CPUs of the same guest. +*/ +return membuf_store (&to, (unsigned int )mfspr (SPRN_HDEXCR_RO )); +} + +#endif /* CONFIG_PPC_BOOK3S_64 */ #ifdef CONFIG_PPC_MEM_KEYS static int pkey_active (struct task_struct *target,const struct user_regset *regset)
@@ -615,6 +639,11 @@ static const struct user_regset native_regsets[] = { .size = sizeof(u64), .align = sizeof(u64), .active = pmu_active, .regset_get = pmu_get, .set = pmu_set }, + [REGSET_DEXCR] = { + .core_note_type = NT_PPC_DEXCR, .n = ELF_NDEXCR, + .size = sizeof(u32), .align = sizeof(u32), + .active = dexcr_active, .regset_get = dexcr_get + },#endif #ifdef CONFIG_PPC_MEM_KEYS [REGSET_PKEY] = {
include/uapi/linux/elf.h +1 -0
@@ -403,6 +403,7 @@ typedef struct elf64_shdr {#define NT_PPC_TM_CPPR 0x10e/* TM checkpointed Program Priority Register */ #define NT_PPC_TM_CDSCR 0x10f/* TM checkpointed Data Stream Control Register */ #define NT_PPC_PKEY 0x110/* Memory Protection Keys registers */ +#define NT_PPC_DEXCR 0x111/* PowerPC DEXCR registers */ #define NT_386_TLS 0x200/* i386 TLS slots (struct user_desc) */ #define NT_386_IOPERM 0x201/* x86 io permission bitmap (1=deny) */ #define NT_X86_XSTATE 0x202/* x86 extended state using xsave */
Signature · 1 line
2.39.2
PATCH v2 8/12 powerpc/ptrace: Expose HASHKEYR register to ptrace
In reply to Benjamin GrayThe HASHKEYR register contains a secret per-process key to enable unique hashes per process. In general it should not be exposed to userspace at all and a regular process has no need to know its key.
However, checkpoint restore in userspace (CRIU) functionality requires that a process be able to set the HASHKEYR of another process, otherwise existing hashes on the stack would be invalidated by a new random key.
Exposing HASHKEYR in this way also makes it appear in core dumps, which is a security concern. Multiple threads may share a key, for example just after a fork() call, where the kernel cannot know if the child is going to return back along the parent's stack. If such a thread is coerced into making a core dump, then the HASHKEYR value will be readable and able to be used against all other threads sharing that key, effectively undoing any protection offered by hashst/hashchk.
Therefore we expose HASHKEYR to ptrace when CONFIG_CHECKPOINT_RESTORE is enabled, providing a choice of increased security or migratable ROP protected processes. This is similar to how ARM exposes its PAC keys.
---
v2: * New in v2
--- arch/powerpc/include/uapi/asm/elf.h | 1 + arch/powerpc/kernel/ptrace/ptrace-decl.h | 3 ++ arch/powerpc/kernel/ptrace/ptrace-view.c | 36 ++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 4 files changed, 41 insertions(+)
arch/powerpc/include/uapi/asm/elf.h +1 -0
@@ -99,6 +99,7 @@#define ELF_NPMU 5/* includes siar, sdar, sier, mmcr2, mmcr0 */ #define ELF_NPKEY 3/* includes amr, iamr, uamor */ #define ELF_NDEXCR 2/* includes dexcr, hdexcr */ +#define ELF_NHASHKEYR 1/* includes hashkeyr */ typedef unsigned long elf_greg_t64 ;typedef elf_greg_t64 elf_gregset_t64 [ELF_NGREG ];
arch/powerpc/kernel/ptrace/ptrace-decl.h +3 -0
@@ -58,6 +58,9 @@ enum powerpc_regset {REGSET_EBB ,/* EBB registers */ REGSET_PMR ,/* Performance Monitor Registers */ REGSET_DEXCR ,/* DEXCR registers */ +#ifdef CONFIG_CHECKPOINT_RESTORE +REGSET_HASHKEYR ,/* HASHKEYR register */ +#endif #endif #ifdef CONFIG_PPC_MEM_KEYS REGSET_PKEY, /* AMR register */
arch/powerpc/kernel/ptrace/ptrace-view.c +36 -0
@@ -478,6 +478,35 @@ static int dexcr_get(struct task_struct *target, const struct user_regset *regsereturn membuf_store (&to, (unsigned int )mfspr (SPRN_HDEXCR_RO )); } +#ifdef CONFIG_CHECKPOINT_RESTORE +static int hashkeyr_active (struct task_struct *target,const struct user_regset *regset) +{ +if (!cpu_has_feature (CPU_FTR_ARCH_31 )) +return -ENODEV ; + +return regset->n; +} + +static int hashkeyr_get (struct task_struct *target,const struct user_regset *regset, +struct membuf to) +{ +if (!cpu_has_feature (CPU_FTR_ARCH_31 )) +return -ENODEV ; + +return membuf_store (&to, target->thread.hashkeyr); +} + +static int hashkeyr_set (struct task_struct *target,const struct user_regset *regset, +unsigned int pos,unsigned int count,const void *kbuf, +const void __user *ubuf) +{ +if (!cpu_has_feature (CPU_FTR_ARCH_31 )) +return -ENODEV ; + +return user_regset_copyin (&pos, &count, &kbuf, &ubuf, &target->thread.hashkeyr, +0 ,sizeof (unsigned long )); +} +#endif /* CONFIG_CHECKPOINT_RESTORE */ #endif /* CONFIG_PPC_BOOK3S_64 */ #ifdef CONFIG_PPC_MEM_KEYS
@@ -644,6 +673,13 @@ static const struct user_regset native_regsets[] = { .size = sizeof(u32), .align = sizeof(u32), .active = dexcr_active, .regset_get = dexcr_get }, +#ifdef CONFIG_CHECKPOINT_RESTORE + [REGSET_HASHKEYR] = { + .core_note_type = NT_PPC_HASHKEYR, .n = ELF_NHASHKEYR, + .size = sizeof(u64), .align = sizeof(u64), + .active = hashkeyr_active, .regset_get = hashkeyr_get, .set = hashkeyr_set + }, +#endif #endif #ifdef CONFIG_PPC_MEM_KEYS [REGSET_PKEY] = {
include/uapi/linux/elf.h +1 -0
@@ -404,6 +404,7 @@ typedef struct elf64_shdr {#define NT_PPC_TM_CDSCR 0x10f/* TM checkpointed Data Stream Control Register */ #define NT_PPC_PKEY 0x110/* Memory Protection Keys registers */ #define NT_PPC_DEXCR 0x111/* PowerPC DEXCR registers */ +#define NT_PPC_HASHKEYR 0x112/* PowerPC HASHKEYR register */ #define NT_386_TLS 0x200/* i386 TLS slots (struct user_desc) */ #define NT_386_IOPERM 0x201/* x86 io permission bitmap (1=deny) */ #define NT_X86_XSTATE 0x202/* x86 extended state using xsave */
Signature · 1 line
2.39.2
PATCH v2 11/12 selftests/powerpc/dexcr: Add hashst/hashchk test
In reply to Benjamin GrayTest the kernel DEXCR[NPHIE] interface and hashchk exception handling.
Introduces with it a DEXCR utils library for common DEXCR operations.
Volatile is used to prevent the compiler optimising away the signal tests.
---
v1: * Clean up dexcr makefile
* Include kernel headers in CFLAGS
* Use numeric literals for hashst/hashchk to support older toolchains
* A lot of other refactoring
--- tools/testing/selftests/powerpc/Makefile | 1 + .../selftests/powerpc/dexcr/.gitignore | 1 + .../testing/selftests/powerpc/dexcr/Makefile | 7 + tools/testing/selftests/powerpc/dexcr/dexcr.c | 132 ++++++++++ tools/testing/selftests/powerpc/dexcr/dexcr.h | 49 ++++ .../selftests/powerpc/dexcr/hashchk_test.c | 227 ++++++++++++++++++ tools/testing/selftests/powerpc/include/reg.h | 4 + .../testing/selftests/powerpc/include/utils.h | 4 + tools/testing/selftests/powerpc/utils.c | 24 ++ 9 files changed, 449 insertions(+) create mode 100644 tools/testing/selftests/powerpc/dexcr/.gitignore create mode 100644 tools/testing/selftests/powerpc/dexcr/Makefile create mode 100644 tools/testing/selftests/powerpc/dexcr/dexcr.c create mode 100644 tools/testing/selftests/powerpc/dexcr/dexcr.h create mode 100644 tools/testing/selftests/powerpc/dexcr/hashchk_test.c
tools/testing/selftests/powerpc/Makefile +1 -0
@@ -17,6 +17,7 @@ SUB_DIRS = alignment \ benchmarks \ cache_shape \ copyloops \ + dexcr \ dscr \ mm \ nx-gzip \
tools/testing/selftests/powerpc/dexcr/.gitignore new file +1 -0
@@ -0,0 +1 @@ +hashchk_test
tools/testing/selftests/powerpc/dexcr/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +TEST_GEN_PROGS := hashchk_test + +include ../../lib.mk + +$(OUTPUT)/hashchk_test: CFLAGS += -fno-pie $(call cc-option,-mno-rop-protect) + +$(TEST_GEN_PROGS): ../harness.c ../utils.c ./dexcr.c
tools/testing/selftests/powerpc/dexcr/dexcr.c new file +132 -0
132 lines not on this page · raw message
tools/testing/selftests/powerpc/dexcr/dexcr.h new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/powerpc/dexcr/hashchk_test.c new file +227 -0
227 lines not on this page · raw message
tools/testing/selftests/powerpc/include/reg.h +4 -0
@@ -19,6 +19,8 @@#define mb () asm volatile("sync" : : : "memory");#define barrier () asm volatile("" : : : "memory"); +#define SPRN_HDEXCR_RO 455/* Userspace readonly view of SPRN_HDEXCR (471) */ +#define SPRN_MMCR2 769#define SPRN_MMCRA 770#define SPRN_MMCR0 779
@@ -47,6 +49,8 @@#define SPRN_SDAR 781#define SPRN_SIER 768 +#define SPRN_DEXCR_RO 812/* Userspace readonly view of SPRN_DEXCR (828) */ +#define SPRN_TEXASR 0x82/* Transaction Exception and Status Register */ #define SPRN_TFIAR 0x81/* Transaction Failure Inst Addr */ #define SPRN_TFHAR 0x80/* Transaction Failure Handler Addr */
tools/testing/selftests/powerpc/include/utils.h +4 -0
@@ -11,6 +11,7 @@#include <stdint.h> #include <stdio.h> #include <stdbool.h> +#include <sys/signal.h> #include <linux/auxvec.h> #include <linux/perf_event.h> #include <asm/cputable.h>
@@ -108,6 +109,9 @@ static inline char *auxv_platform(void)bool is_ppc64le (void );int using_hash_mmu (bool *using_hash); +struct sigaction push_signal_handler (int sig,void (*fn)(int ,siginfo_t *,void *)); +struct sigaction pop_signal_handler (int sig,struct sigaction old_handler); +/* Yes, this is evil */ #define FAIL_IF (x) \ do { \
tools/testing/selftests/powerpc/utils.c +24 -0
@@ -595,3 +595,27 @@ int using_hash_mmu(bool *using_hash)fclose (f);return rc; } + +struct sigaction push_signal_handler (int sig,void (*fn)(int ,siginfo_t *,void *)) +{ +struct sigaction sa; +struct sigaction old_handler; + + sa.sa_sigaction = fn; +sigemptyset (&sa.sa_mask); + sa.sa_flags =SA_SIGINFO ; +FAIL_IF_EXIT_MSG (sigaction (sig, &sa, &old_handler), +"failed to push signal handler" ); + +return old_handler; +} + +struct sigaction pop_signal_handler (int sig,struct sigaction old_handler) +{ +struct sigaction popped; + +FAIL_IF_EXIT_MSG (sigaction (sig, &old_handler, &popped), +"failed to pop signal handler" ); + +return popped; +}
Signature · 1 line
2.39.2
PATCH v2 5/12 powerpc/dexcr: Support userspace ROP protection
In reply to Benjamin GrayThe ISA 3.1B hashst and hashchk instructions use a per-cpu SPR HASHKEYR to hold a key used in the hash calculation. This key should be different for each process to make it harder for a malicious process to recreate valid hash values for a victim process.
Add support for storing a per-thread hash key, and setting/clearing HASHKEYR appropriately.
---
v1: * Guard HASHKEYR update behind change check
* HASHKEYR reset moved earlier to patch 2
--- arch/powerpc/include/asm/processor.h | 1 + arch/powerpc/kernel/process.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
arch/powerpc/include/asm/processor.h +1 -0
@@ -264,6 +264,7 @@ struct thread_struct {unsigned long mmcr3;unsigned long sier2;unsigned long sier3; +unsigned long hashkeyr;#endif };
arch/powerpc/kernel/process.c +17 -0
@@ -1182,6 +1182,9 @@ static inline void save_sprs(struct thread_struct *t)*/ t->tar = mfspr(SPRN_TAR); } + +if (cpu_has_feature (CPU_FTR_DEXCR_NPHIE )) + t->hashkeyr =mfspr (SPRN_HASHKEYR );#endif }
@@ -1260,6 +1263,10 @@ static inline void restore_sprs(struct thread_struct *old_thread,if (cpu_has_feature (CPU_FTR_P9_TIDR ) && old_thread->tidr != new_thread->tidr)mtspr (SPRN_TIDR , new_thread->tidr); + +if (cpu_has_feature (CPU_FTR_DEXCR_NPHIE ) && + old_thread->hashkeyr != new_thread->hashkeyr) +mtspr (SPRN_HASHKEYR , new_thread->hashkeyr);#endif }
@@ -1844,6 +1851,10 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->ppr =DEFAULT_PPR ; p->thread.tidr =0 ; +#endif +#ifdef CONFIG_PPC_BOOK3S_64 +if (cpu_has_feature (CPU_FTR_DEXCR_NPHIE )) + p->thread.hashkeyr = current->thread.hashkeyr;#endif /* * Run with the current AMR value of the kernel
@@ -1972,6 +1983,12 @@ void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp) current->thread.tm_tfiar =0 ; current->thread.load_tm =0 ;#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +#ifdef CONFIG_PPC_BOOK3S_64 +if (cpu_has_feature (CPU_FTR_DEXCR_NPHIE )) { + current->thread.hashkeyr =get_random_long (); +mtspr (SPRN_HASHKEYR , current->thread.hashkeyr); + } +#endif /* CONFIG_PPC_BOOK3S_64 */ }EXPORT_SYMBOL (start_thread);
Signature · 1 line
2.39.2
Re: [PATCH v2 02/12] powerpc/ptrace: Add missing <linux/regset.h> include
In reply to Benjamin GrayQuoted 6 lines · Benjamin Gray
ptrace-decl.husesuser_regset_get2_fn(among other things) fromregset.h. While all current users ofptrace-decl.hincluderegset.hbefore it anyway, it adds an implicit ordering dependency and breaks source tooling that tries to inspectptrace-decl.hby itself.Signed-off-by Benjamin Gray <[email protected]>
Re: [PATCH v2 03/12] powerpc/dexcr: Add initial Dynamic Execution Control Register (DEXCR) support
In reply to Benjamin GrayQuoted 26 lines · Benjamin Gray
ISA 3.1B introduces the Dynamic Execution Control Register (DEXCR). It
is a per-cpu register that allows control over various CPU behaviours including branch hint usage, indirect branch speculation, and hashst/hashchk support.Add some definitions and basic support for the DEXCR in the kernel. Right now it just
* Zero initialises the DEXCR and HASHKEYR when a CPU onlines.
* Clears them inreset_sprs().
* Detects when the NPHIE aspect is supported (the others don't get looked at in this series, so there's no need to waste aCPU_FTRon them).We initialise the HASHKEYR to ensure that all cores have the same key,
so an HV enforced NPHIE + swapping cores doesn't randomly crash a process using hash instructions. The stores to HASHKEYR are unconditional because the ISA makes no mention of the SPR being missing
if support for doing the hashes isn't present. So all that would happen
is the HASHKEYR value gets ignored. This helps slightly if NPHIE detection fails; e.g., we currently only detect it on pseries.
Quoted 2 lines
Signed-off-by Benjamin Gray <[email protected]>
LGTM.
Re: [PATCH v2 01/12] powerpc/book3s: Add missing <linux/sched.h> include
In reply to Benjamin GrayQuoted 10 lines · Benjamin Gray
The functions here use struct
thread_structfields, so need to import the full definition from <linux/sched.h>. The <asm/current.h> header that defines current only forward declares structthread_struct.Failing to include this <
linux/sched.h> header leads to a compilation error when a translation unit does not also include <linux/sched.h> indirectly.Signed-off-by Benjamin Gray <[email protected]>Reviewed-by Nicholas Piggin <[email protected]>
Re: [PATCH v2 04/12] powerpc/dexcr: Handle hashchk exception
In reply to Benjamin GrayQuoted 12 lines · Benjamin Gray
Recognise and pass the appropriate signal to the user program when a hashchk instruction triggers. This is independent of allowing configuration of DEXCR[NPHIE], as a hypervisor can enforce this aspect
regardless of the kernel.The signal mirrors how ARM reports their similar check failure. For example, their FPAC handler in
arch/arm64/kernel/traps.cdo_el0_fpac()
does this. When we fail to read the instruction that caused the fault we send a segfault, similar to howemulate_math()does it.
Quoted 8 lines
Signed-off-by Benjamin Gray <[email protected]>---
v1: * Refactor the hashchk check to return 0 on success, an error code on failure. Determine what to do based on specific error
code.
0 on success makes sense, but it's not exactly obvious what "success" is in this case.
Quoted 67 lines
* Motivate signal and code
---
arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/processor.h | 9 +++++++ arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/dexcr.c | 36
+++++++++++++++++++++++++++
arch/powerpc/kernel/traps.c | 10 ++++++++5 files changed, 57 insertions(+)create mode 100644
arch/powerpc/kernel/dexcr.carch/powerpc/include/asm/ppc-opcode.h +1
-0@@ -215,6 +215,7 @@ #define OP_31_XOP_STFSX 663 #define OP_31_XOP_STFSUX 695 #define OP_31_XOP_STFDX 727 +#define OP_31_XOP_HASHCHK 754 #define OP_31_XOP_STFDUX 759 #define OP_31_XOP_LHBRX 790 #define OP_31_XOP_LFIWAX 855arch/powerpc/include/asm/processor.h +0
-0@@ -455,6 +455,15 @@ int exit_vmx_usercopy(void); int enter_vmx_ops(void); void *exit_vmx_ops(void *dest);arch/powerpc/include/asm/processor.h +9
-0+#ifdef CONFIG_PPC_BOOK3S_64 +int check_hashchk_trap (struct pt_regs const *regs); +#else +static inline int check_hashchk_trap (struct pt_regs const *regs) +{ + return -EINVAL; +} +#endif /* CONFIG_PPC_BOOK3S_64 */ +#endif /*
__KERNEL__*/
#endif /*__ASSEMBLY__*/
#endif /*_ASM_POWERPC_PROCESSOR_H*/arch/powerpc/kernel/Makefile +0
-0@@ -88,6 +88,7 @@ obj-$(CONFIG_HAVE_HW_BREAKPOINT) +=hw_breakpoint.o
obj-$(CONFIG_PPC_DAWR) += dawr.o obj-$(CONFIG_PPC_BOOK3S_64) += cpu_setup_ppc970.o cpu_setup_pa6t.o
obj-$(CONFIG_PPC_BOOK3S_64) += cpu_setup_power.o +obj-$(CONFIG_PPC_BOOK3S_64) += dexcr.o obj-$(CONFIG_PPC_BOOK3S_64) += mce.o mce_power.o obj-$(CONFIG_PPC_BOOK3E_64) += exceptions-64e.o idle_64e.o obj-$(CONFIG_PPC_BARRIER_NOSPEC) += security.oarch/powerpc/kernel/dexcr.c new file +0
-0
arch/powerpc/kernel/dexcr.c line 3
Quoted 3 lines
arch/powerpc/kernel/dexcr.c new file +3
-0@@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* DEXCR infrastructure
May as well spell out DEXCR here
Quoted 23 lines
arch/powerpc/kernel/dexcr.c new file +23
-0+* +* Copyright 2023, Benjamin Gray, IBM Corporation. +*/ +#include <linux/compiler_types.h> + +#include <asm/cpu_has_feature.h> +#include <asm/cputable.h> +#include <asm/disassemble.h> +#include <asm/errno.h> +#include <asm/inst.h> +#include <asm/ppc-opcode.h> +#include <asm/ptrace.h> +#include <asm/reg.h> + +int check_hashchk_trap (struct pt_regs const *regs) +{ + ppc_inst_t insn; + + if (!cpu_has_feature(CPU_FTR_DEXCR_NPHIE)) + return -EINVAL; + + if (!user_mode(regs)) + return -EINVAL;
arch/powerpc/kernel/dexcr.c line 36
Quoted 10 lines
arch/powerpc/kernel/dexcr.c new file +10
-0+ + if (get_user_instr(insn, (void __user *)regs->nip)) + return -EFAULT; + + if (ppc_inst_primary_opcode(insn) != 31 || + get_xop(ppc_inst_val(insn)) != OP_31_XOP_HASHCHK) + return -EINVAL; + + return 0; +}
The return values here are quite confusing and only start to make sense in the context of the calling function, which isn't great for something living in a different file. So if we return 0 we SIGILL because of hashchk, return -EFAULT to SEGV_MAPERR and return -EINVAL to fall through to SIGTRAP. I would like a comment for that since it's not very intuitive.
Quoted 12 lines
arch/powerpc/kernel/traps.c +2
-0@@ -1516,6 +1516,16 @@ static void do_program_check(struct pt_regs *regs) return; } } + + switch (check_hashchk_trap(regs)) {
arch/powerpc/kernel/traps.c static void do_program_check(struct pt_regs · line 1531
Quoted 10 lines
arch/powerpc/kernel/traps.c +8
-0+ case 0: + _exception(SIGILL, regs, ILL_ILLOPN, regs- >nip); + return; + case -EFAULT: + _exception(SIGSEGV, regs, SEGV_MAPERR, regs- >nip); + return; + } +
I offer no constructive feedback on how to rearrange it, but this just looks gross.
Quoted 3 lines
_exception(SIGTRAP, regs,
TRAP_BRKPT,regs->nip); return;
}
Re: [PATCH v2 05/12] powerpc/dexcr: Support userspace ROP protection
In reply to Benjamin GrayQuoted 8 lines · Benjamin Gray
The ISA 3.1B hashst and hashchk instructions use a per-cpu SPR HASHKEYR
to hold a key used in the hash calculation. This key should be different
for each process to make it harder for a malicious process to recreate
valid hash values for a victim process.
Quoted 4 lines
Add support for storing a per-thread hash key, and setting/clearing HASHKEYR appropriately.
Signed-off-by Benjamin Gray <[email protected]>
Re: [PATCH v2 06/12] powerpc/dexcr: Support custom default DEXCR value
In reply to Benjamin GrayQuoted 11 lines · Benjamin Gray
Make the DEXCR value configurable at config time. Intentionally don't limit possible values to support future aspects without needing kernel
updates.The default config value enables hashst/hashchk in problem state. This should be safe, as generally software needs to request these instructions be included in the first place.
Signed-off-by Benjamin Gray <[email protected]>
Quoted 4 lines
---
New in v1Preface with: I'm note sure on the best place to put the config.
Me neither.
Quoted 32 lines
I also don't think there's any need to zero out unknown/unsupported bits. Reserved implies they are ignored by the hardware (from my understanding of the ISA). Current P10s boot with all bits set; lsdexcr
(later patch) reportsuDEXCR:
ff000000(SBHE, IBRTPD, SRAPD, NPHIE, PHIE, unknown)when you try to read it back. Leaving them be also makes it easier to support newer aspects without a kernel update.
If arbitrary value support isn't important, it's probably a nicer interface to make each aspect an entry in a menu.
Future work may include dynamic DEXCR controls via
prctl()and sysfs. The dynamic controls would be able to override this default DEXCR on a
per-process basis. A stronger "PPC_ENFORCE_USER_ROP_PROCTETION" config
may be required at such a time to prevent dynamically disabling the hash checks.
---
arch/powerpc/Kconfig | 14 ++++++++++++++arch/powerpc/kernel/cpu_setup_power.c| 3 ++-2 files changed, 16 insertions(+), 1 deletion(-)arch/powerpc/Kconfig +0
-0@@ -1035,6 +1035,20 @@ config PPC_MEM_KEYS
Quoted 4 lines
If unsure, say y.
+configPPC_DEXCR_DEFAULT
Calling it "default" could be slightly misleading since there's no other way to modify the DEXCR right now. It'll make more sense once it's user configurable though.
Quoted 9 lines
+ hex "Default DEXCR value"
+ default0x0000000004000000
+ depends onPPC_BOOK3S_64
+ help
+ Power10 introduces the Dynamic Execution Control Register (DEXCR)
+ to provide fine grained control over various speculation and
+ security capabilities. This is used as the default DEXCR
Quoted 8 lines
value.
+
+ It is a 64 bit value that splits into 32 bits for supervisor mode
+ and 32 bits for problem state. The default config value enables
+ the hashst/hashck instructions in userspace. See the ISA for
hashchk*
May also be useful to reference the ISA version here.
Quoted 27 lines
+ specifics of what each bit controls.
+
configPPC_SECURE_BOOT
prompt "Enable secure boot support"
boolarch/powerpc/kernel/cpu_setup_power.c +1
-0@@ -10,6 +10,7 @@ #include <asm/reg.h> #include <asm/synch.h> #include <linux/bitops.h> +#include <linux/kconfig.h> #include <asm/cputable.h> #include <asm/cpu_setup.h>arch/powerpc/kernel/cpu_setup_power.c +0
-0@@ -128,7 +129,7 @@ static void init_PMU_ISA31(void)
static voidinit_DEXCR(void)
{
- mtspr(SPRN_DEXCR, 0);
+ mtspr(SPRN_DEXCR,CONFIG_PPC_DEXCR_DEFAULT);mtspr(SPRN_HASHKEYR, 0);
}
Re: [PATCH v2 07/12] powerpc/ptrace: Expose DEXCR and HDEXCR registers to ptrace
In reply to Benjamin GrayQuoted 19 lines · Benjamin Gray
The DEXCR register is of interest when ptracing processes. Currently it
is static, but eventually will be dynamically controllable by a process.
If a process can control its own, then it is useful for it to be ptrace-able to (e.g., for checkpoint-restore functionality).It is also relevant to core dumps (the NPHIE aspect in particular), which use the ptrace mechanism (or is it the other way around?) to decide what to dump. The HDEXCR is useful here too, as the NPHIE aspect
may be set in the HDEXCR without being set in the DEXCR. Although the HDEXCR is per-cpu and we don't track it in the task struct (it's useless
in normal operation), it would be difficult to imagine why a hypervisor
would set it to different values within a guest. A hypervisor cannot safely set NPHIE differently at least, as that would break programs.
Quoted 6 lines
Expose a read-only view of the userspace DEXCR and HDEXCR to ptrace. The HDEXCR is always readonly, and is useful for diagnosing the core dumps (as the HDEXCR may set NPHIE without the DEXCR setting it).
Signed-off-by Benjamin Gray <[email protected]>
I don't know much about ptrace but this looks sane.
Re: [PATCH v2 08/12] powerpc/ptrace: Expose HASHKEYR register to ptrace
In reply to Benjamin GrayQuoted 23 lines · Benjamin Gray
The HASHKEYR register contains a secret per-process key to enable unique
hashes per process. In general it should not be exposed to userspace at all and a regular process has no need to know its key.However, checkpoint restore in userspace (CRIU) functionality requires
that a process be able to set the HASHKEYR of another process, otherwise
existing hashes on the stack would be invalidated by a new random key.Exposing HASHKEYR in this way also makes it appear in core dumps, which
is a security concern. Multiple threads may share a key, for example just after afork()call, where the kernel cannot know if the child is
going to return back along the parent's stack. If such a thread is coerced into making a core dump, then the HASHKEYR value will be readable and able to be used against all other threads sharing that key,
effectively undoing any protection offered by hashst/hashchk.
Quoted 6 lines
Therefore we expose HASHKEYR to ptrace when
CONFIG_CHECKPOINT_RESTOREis
enabled, providing a choice of increased security or migratable ROP protected processes. This is similar to how ARM exposes its PAC keys.Signed-off-by Benjamin Gray <[email protected]>
Seems sensible
Re: [PATCH v2 09/12] Documentation: Document PowerPC kernel DEXCR interface
In reply to Benjamin GrayQuoted 11 lines · Benjamin Gray
Describe the DEXCR and document how to configure it.
Signed-off-by Benjamin Gray <[email protected]>---
v2: * Document coredump & ptrace support
v1: * Remove the dynamic control docs, describe the static config optionThis documentation is a little bare for now, but will be expanded on when dynamic DEXCR control is added.
Re: [PATCH v2 10/12] selftests/powerpc: Add more utility macros
In reply to Benjamin GrayQuoted 11 lines · Benjamin Gray
* Include
unistd.hfor_exit()
* Includestdio.hforfprintf()
* Adds_MSGassertion variants to provide more context behind why a failure occurred.
* MoveARRAY_SIZEmacro toutils.hThe
_MSGvariants andARRAY_SIZEwill be used by the following DEXCR selftests.Signed-off-by Benjamin Gray <[email protected]>
I'd prefer if your commit message led with adding FAIL_IF_MSG etc and then mentioned the other changes to support it. It's unintuitive to read as-is.