History

arm64: Basic Branch Target Identification support

This patch adds the bare minimum required to expose the ARMv8.5 Branch Target Identification feature to userspace. By itself, this does _not_ automatically enable BTI for any initial executable pages mapped by execve(). This will come later, but for now it should be possible to enable BTI manually on those pages by using mprotect() from within the target process. Other arches already using the generic mman.h are already using 0x10 for arch-specific prot flags, so we use that for PROT_BTI here. For consistency, signal handler entry points in BTI guarded pages are required to be annotated as such, just like any other function. This blocks a relatively minor attack vector, but comforming userspace will have the annotations anyway, so we may as well enforce them.

Dave Martin committed 6 years ago

Commit 8ef8f361 parent 00e19ce23 files · +172 −4Browse files

Unified · Split

23 files
  1. Documentation/arm64/cpu-feature-registers.rst +2 −0
  2. Documentation/arm64/elf_hwcaps.rst +5 −0
  3. arch/arm64/include/asm/cpucaps.h +2 −1
  4. arch/arm64/include/asm/cpufeature.h +6 −0
  5. arch/arm64/include/asm/esr.h +1 −1
  6. arch/arm64/include/asm/exception.h +1 −0
  7. arch/arm64/include/asm/hwcap.h +1 −0
  8. arch/arm64/include/asm/mman.h +37 −0
  9. arch/arm64/include/asm/pgtable-hwdef.h +1 −0
  10. arch/arm64/include/asm/pgtable.h +1 −1
  11. arch/arm64/include/asm/ptrace.h +1 −0
  12. arch/arm64/include/asm/sysreg.h +4 −0
  13. arch/arm64/include/uapi/asm/hwcap.h +1 −0
  14. arch/arm64/include/uapi/asm/mman.h +9 −0
  15. arch/arm64/include/uapi/asm/ptrace.h +9 −0
  16. arch/arm64/kernel/cpufeature.c +33 −0
  17. arch/arm64/kernel/cpuinfo.c +1 −0
  18. arch/arm64/kernel/entry-common.c +11 −0
  19. arch/arm64/kernel/ptrace.c +1 −1
  20. arch/arm64/kernel/signal.c +16 −0
  21. arch/arm64/kernel/syscall.c +18 −0
  22. arch/arm64/kernel/traps.c +8 −0
  23. include/linux/mm.h +3 −0
Documentation/arm64/cpu-feature-registers.rst+2−0
@@ -176,6 +176,8 @@ infrastructure: · 175 unmodified lines
176176 +------------------------------+---------+---------+
177177 | SSBS | [7-4] | y |
178178 +------------------------------+---------+---------+
179+ | BT | [3-0] | y |
180+ +------------------------------+---------+---------+
179181
180182
181183 4) MIDR_EL1 - Main ID Register
Documentation/arm64/elf_hwcaps.rst+5−0
@@ -236,6 +236,11 @@ HWCAP2_RNG · 235 unmodified lines
236236
237237 Functionality implied by ID_AA64ISAR0_EL1.RNDR == 0b0001.
238238
239+HWCAP2_BTI
240+
241+ Functionality implied by ID_AA64PFR0_EL1.BT == 0b0001.
242+
243+
239244 4. Unused AT_HWCAP bits
240245 -----------------------
241246
arch/arm64/include/asm/cpucaps.h+2−1
@@ -58,7 +58,8 @@ · 57 unmodified lines
5858 #define ARM64_WORKAROUND_SPECULATIVE_AT_NVHE 48
5959 #define ARM64_HAS_E0PD 49
6060 #define ARM64_HAS_RNG 50
61+#define ARM64_BTI 51
6162
62−#define ARM64_NCAPS 51
63+#define ARM64_NCAPS 52
6364
6465 #endif /* __ASM_CPUCAPS_H */
arch/arm64/include/asm/cpufeature.h+6−0
@@ -613,6 +613,12 @@ static inline bool system_has_prio_mask_debugging(void) · 612 unmodified lines
613613 system_uses_irq_prio_masking();
614614 }
615615
616+static inline bool system_supports_bti(void)
617+{
618+ return IS_ENABLED(CONFIG_ARM64_BTI) &&
619+ cpus_have_const_cap(ARM64_BTI);
620+}
621+
616622 static inline bool system_capabilities_finalized(void)
617623 {
618624 return static_branch_likely(&arm64_const_caps_ready);
arch/arm64/include/asm/esr.h+1−1
@@ -22,7 +22,7 @@ · 21 unmodified lines
2222 #define ESR_ELx_EC_PAC (0x09) /* EL2 and above */
2323 /* Unallocated EC: 0x0A - 0x0B */
2424 #define ESR_ELx_EC_CP14_64 (0x0C)
25−/* Unallocated EC: 0x0d */
25+#define ESR_ELx_EC_BTI (0x0D)
2626 #define ESR_ELx_EC_ILL (0x0E)
2727 /* Unallocated EC: 0x0F - 0x10 */
2828 #define ESR_ELx_EC_SVC32 (0x11)
arch/arm64/include/asm/exception.h+1−0
@@ -34,6 +34,7 @@ static inline u32 disr_to_esr(u64 disr) · 33 unmodified lines
3434 asmlinkage void enter_from_user_mode(void);
3535 void do_mem_abort(unsigned long addr, unsigned int esr, struct pt_regs *regs);
3636 void do_undefinstr(struct pt_regs *regs);
37+void do_bti(struct pt_regs *regs);
3738 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr);
3839 void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr,
3940 struct pt_regs *regs);
arch/arm64/include/asm/hwcap.h+1−0
@@ -94,6 +94,7 @@ · 93 unmodified lines
9494 #define KERNEL_HWCAP_BF16 __khwcap2_feature(BF16)
9595 #define KERNEL_HWCAP_DGH __khwcap2_feature(DGH)
9696 #define KERNEL_HWCAP_RNG __khwcap2_feature(RNG)
97+#define KERNEL_HWCAP_BTI __khwcap2_feature(BTI)
9798
9899 /*
99100 * This yields a mask that user programs can use to figure out what
arch/arm64/include/asm/mman.h+37−0
@@ -0,0 +1,37 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+#ifndef __ASM_MMAN_H__
3+#define __ASM_MMAN_H__
4+
5+#include <linux/compiler.h>
6+#include <linux/types.h>
7+#include <uapi/asm/mman.h>
8+
9+static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
10+ unsigned long pkey __always_unused)
11+{
12+ if (system_supports_bti() && (prot & PROT_BTI))
13+ return VM_ARM64_BTI;
14+
15+ return 0;
16+}
17+#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
18+
19+static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)
20+{
21+ return (vm_flags & VM_ARM64_BTI) ? __pgprot(PTE_GP) : __pgprot(0);
22+}
23+#define arch_vm_get_page_prot(vm_flags) arch_vm_get_page_prot(vm_flags)
24+
25+static inline bool arch_validate_prot(unsigned long prot,
26+ unsigned long addr __always_unused)
27+{
28+ unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
29+
30+ if (system_supports_bti())
31+ supported |= PROT_BTI;
32+
33+ return (prot & ~supported) == 0;
34+}
35+#define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
36+
37+#endif /* ! __ASM_MMAN_H__ */
arch/arm64/include/asm/pgtable-hwdef.h+1−0
@@ -151,6 +151,7 @@ · 150 unmodified lines
151151 #define PTE_SHARED (_AT(pteval_t, 3) << 8) /* SH[1:0], inner shareable */
152152 #define PTE_AF (_AT(pteval_t, 1) << 10) /* Access Flag */
153153 #define PTE_NG (_AT(pteval_t, 1) << 11) /* nG */
154+#define PTE_GP (_AT(pteval_t, 1) << 50) /* BTI guarded */
154155 #define PTE_DBM (_AT(pteval_t, 1) << 51) /* Dirty Bit Management */
155156 #define PTE_CONT (_AT(pteval_t, 1) << 52) /* Contiguous range */
156157 #define PTE_PXN (_AT(pteval_t, 1) << 53) /* Privileged XN */
arch/arm64/include/asm/pgtable.h+1−1
@@ -660,7 +660,7 @@ static inline phys_addr_t pgd_page_paddr(pgd_t pgd) · 659 unmodified lines
660660 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
661661 {
662662 const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
663− PTE_PROT_NONE | PTE_VALID | PTE_WRITE;
663+ PTE_PROT_NONE | PTE_VALID | PTE_WRITE | PTE_GP;
664664 /* preserve the hardware dirty information */
665665 if (pte_hw_dirty(pte))
666666 pte = pte_mkdirty(pte);
arch/arm64/include/asm/ptrace.h+1−0
@@ -35,6 +35,7 @@ · 34 unmodified lines
3535 #define GIC_PRIO_PSR_I_SET (1 << 4)
3636
3737 /* Additional SPSR bits not exposed in the UABI */
38+
3839 #define PSR_IL_BIT (1 << 20)
3940
4041 /* AArch32-specific ptrace requests */
arch/arm64/include/asm/sysreg.h+4−0
@@ -514,6 +514,8 @@ · 513 unmodified lines
514514 #endif
515515
516516 /* SCTLR_EL1 specific flags. */
517+#define SCTLR_EL1_BT1 (BIT(36))
518+#define SCTLR_EL1_BT0 (BIT(35))
517519 #define SCTLR_EL1_UCI (BIT(26))
518520 #define SCTLR_EL1_E0E (BIT(24))
519521 #define SCTLR_EL1_SPAN (BIT(23))
@@ -620,10 +622,12 @@ · 100 unmodified lines
620622
621623 /* id_aa64pfr1 */
622624 #define ID_AA64PFR1_SSBS_SHIFT 4
625+#define ID_AA64PFR1_BT_SHIFT 0
623626
624627 #define ID_AA64PFR1_SSBS_PSTATE_NI 0
625628 #define ID_AA64PFR1_SSBS_PSTATE_ONLY 1
626629 #define ID_AA64PFR1_SSBS_PSTATE_INSNS 2
630+#define ID_AA64PFR1_BT_BTI 0x1
627631
628632 /* id_aa64zfr0 */
629633 #define ID_AA64ZFR0_F64MM_SHIFT 56
arch/arm64/include/uapi/asm/hwcap.h+1−0
@@ -73,5 +73,6 @@ · 72 unmodified lines
7373 #define HWCAP2_BF16 (1 << 14)
7474 #define HWCAP2_DGH (1 << 15)
7575 #define HWCAP2_RNG (1 << 16)
76+#define HWCAP2_BTI (1 << 17)
7677
7778 #endif /* _UAPI__ASM_HWCAP_H */
arch/arm64/include/uapi/asm/mman.h+9−0
@@ -0,0 +1,9 @@
1+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+#ifndef _UAPI__ASM_MMAN_H
3+#define _UAPI__ASM_MMAN_H
4+
5+#include <asm-generic/mman.h>
6+
7+#define PROT_BTI 0x10 /* BTI guarded page */
8+
9+#endif /* ! _UAPI__ASM_MMAN_H */
arch/arm64/include/uapi/asm/ptrace.h+9−0
@@ -46,6 +46,7 @@ · 45 unmodified lines
4646 #define PSR_I_BIT 0x00000080
4747 #define PSR_A_BIT 0x00000100
4848 #define PSR_D_BIT 0x00000200
49+#define PSR_BTYPE_MASK 0x00000c00
4950 #define PSR_SSBS_BIT 0x00001000
5051 #define PSR_PAN_BIT 0x00400000
5152 #define PSR_UAO_BIT 0x00800000
@@ -55,6 +56,8 @@ · 3 unmodified lines
5556 #define PSR_Z_BIT 0x40000000
5657 #define PSR_N_BIT 0x80000000
5758
59+#define PSR_BTYPE_SHIFT 10
60+
5861 /*
5962 * Groups of PSR bits
6063 */
@@ -63,6 +66,12 @@ · 2 unmodified lines
6366 #define PSR_x 0x0000ff00 /* Extension */
6467 #define PSR_c 0x000000ff /* Control */
6568
69+/* Convenience names for the values of PSTATE.BTYPE */
70+#define PSR_BTYPE_NONE (0b00 << PSR_BTYPE_SHIFT)
71+#define PSR_BTYPE_JC (0b01 << PSR_BTYPE_SHIFT)
72+#define PSR_BTYPE_C (0b10 << PSR_BTYPE_SHIFT)
73+#define PSR_BTYPE_J (0b11 << PSR_BTYPE_SHIFT)
74+
6675 /* syscall emulation path in ptrace */
6776 #define PTRACE_SYSEMU 31
6877 #define PTRACE_SYSEMU_SINGLESTEP 32
arch/arm64/kernel/cpufeature.c+33−0
@@ -179,6 +179,8 @@ static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = { · 178 unmodified lines
179179
180180 static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
181181 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
182+ ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_BTI),
183+ FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_BT_SHIFT, 4, 0),
182184 ARM64_FTR_END,
183185 };
184186
@@ -1347,6 +1349,21 @@ static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry, · 1162 unmodified lines
13471349 }
13481350 #endif
13491351
1352+#ifdef CONFIG_ARM64_BTI
1353+static void bti_enable(const struct arm64_cpu_capabilities *__unused)
1354+{
1355+ /*
1356+ * Use of X16/X17 for tail-calls and trampolines that jump to
1357+ * function entry points using BR is a requirement for
1358+ * marking binaries with GNU_PROPERTY_AARCH64_FEATURE_1_BTI.
1359+ * So, be strict and forbid other BRs using other registers to
1360+ * jump onto a PACIxSP instruction:
1361+ */
1362+ sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_BT0 | SCTLR_EL1_BT1);
1363+ isb();
1364+}
1365+#endif /* CONFIG_ARM64_BTI */
1366+
13501367 static const struct arm64_cpu_capabilities arm64_features[] = {
13511368 {
13521369 .desc = "GIC system register CPU interface",
@@ -1671,6 +1688,19 @@ static const struct arm64_cpu_capabilities arm64_features[] = { · 318 unmodified lines
16711688 .sign = FTR_UNSIGNED,
16721689 .min_field_value = 1,
16731690 },
1691+#endif
1692+#ifdef CONFIG_ARM64_BTI
1693+ {
1694+ .desc = "Branch Target Identification",
1695+ .capability = ARM64_BTI,
1696+ .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1697+ .matches = has_cpuid_feature,
1698+ .cpu_enable = bti_enable,
1699+ .sys_reg = SYS_ID_AA64PFR1_EL1,
1700+ .field_pos = ID_AA64PFR1_BT_SHIFT,
1701+ .min_field_value = ID_AA64PFR1_BT_BTI,
1702+ .sign = FTR_UNSIGNED,
1703+ },
16741704 #endif
16751705 {},
16761706 };
@@ -1781,6 +1811,9 @@ static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = { · 104 unmodified lines
17811811 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F64MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F64MM, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM),
17821812 #endif
17831813 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_SSBS_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_SSBS_PSTATE_INSNS, CAP_HWCAP, KERNEL_HWCAP_SSBS),
1814+#ifdef CONFIG_ARM64_BTI
1815+ HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_BT_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_BT_BTI, CAP_HWCAP, KERNEL_HWCAP_BTI),
1816+#endif
17841817 #ifdef CONFIG_ARM64_PTR_AUTH
17851818 HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
17861819 HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
arch/arm64/kernel/cpuinfo.c+1−0
@@ -92,6 +92,7 @@ static const char *const hwcap_str[] = { · 91 unmodified lines
9292 "bf16",
9393 "dgh",
9494 "rng",
95+ "bti",
9596 NULL
9697 };
9798
arch/arm64/kernel/entry-common.c+11−0
@@ -188,6 +188,14 @@ static void notrace el0_undef(struct pt_regs *regs) · 187 unmodified lines
188188 }
189189 NOKPROBE_SYMBOL(el0_undef);
190190
191+static void notrace el0_bti(struct pt_regs *regs)
192+{
193+ user_exit_irqoff();
194+ local_daif_restore(DAIF_PROCCTX);
195+ do_bti(regs);
196+}
197+NOKPROBE_SYMBOL(el0_bti);
198+
191199 static void notrace el0_inv(struct pt_regs *regs, unsigned long esr)
192200 {
193201 user_exit_irqoff();
@@ -255,6 +263,9 @@ asmlinkage void notrace el0_sync_handler(struct pt_regs *regs) · 61 unmodified lines
255263 case ESR_ELx_EC_UNKNOWN:
256264 el0_undef(regs);
257265 break;
266+ case ESR_ELx_EC_BTI:
267+ el0_bti(regs);
268+ break;
258269 case ESR_ELx_EC_BREAKPT_LOW:
259270 case ESR_ELx_EC_SOFTSTP_LOW:
260271 case ESR_ELx_EC_WATCHPT_LOW:
arch/arm64/kernel/ptrace.c+1−1
@@ -1874,7 +1874,7 @@ void syscall_trace_exit(struct pt_regs *regs) · 1873 unmodified lines
18741874 */
18751875 #define SPSR_EL1_AARCH64_RES0_BITS \
18761876 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
1877− GENMASK_ULL(20, 13) | GENMASK_ULL(11, 10) | GENMASK_ULL(5, 5))
1877+ GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
18781878 #define SPSR_EL1_AARCH32_RES0_BITS \
18791879 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
18801880
arch/arm64/kernel/signal.c+16−0
@@ -732,6 +732,22 @@ static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, · 731 unmodified lines
732732 regs->regs[29] = (unsigned long)&user->next_frame->fp;
733733 regs->pc = (unsigned long)ka->sa.sa_handler;
734734
735+ /*
736+ * Signal delivery is a (wacky) indirect function call in
737+ * userspace, so simulate the same setting of BTYPE as a BLR
738+ * <register containing the signal handler entry point>.
739+ * Signal delivery to a location in a PROT_BTI guarded page
740+ * that is not a function entry point will now trigger a
741+ * SIGILL in userspace.
742+ *
743+ * If the signal handler entry point is not in a PROT_BTI
744+ * guarded page, this is harmless.
745+ */
746+ if (system_supports_bti()) {
747+ regs->pstate &= ~PSR_BTYPE_MASK;
748+ regs->pstate |= PSR_BTYPE_C;
749+ }
750+
735751 if (ka->sa.sa_flags & SA_RESTORER)
736752 sigtramp = ka->sa.sa_restorer;
737753 else
arch/arm64/kernel/syscall.c+18−0
@@ -98,6 +98,24 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr, · 97 unmodified lines
9898 regs->orig_x0 = regs->regs[0];
9999 regs->syscallno = scno;
100100
101+ /*
102+ * BTI note:
103+ * The architecture does not guarantee that SPSR.BTYPE is zero
104+ * on taking an SVC, so we could return to userspace with a
105+ * non-zero BTYPE after the syscall.
106+ *
107+ * This shouldn't matter except when userspace is explicitly
108+ * doing something stupid, such as setting PROT_BTI on a page
109+ * that lacks conforming BTI/PACIxSP instructions, falling
110+ * through from one executable page to another with differing
111+ * PROT_BTI, or messing with BTYPE via ptrace: in such cases,
112+ * userspace should not be surprised if a SIGILL occurs on
113+ * syscall return.
114+ *
115+ * So, don't touch regs->pstate & PSR_BTYPE_MASK here.
116+ * (Similarly for HVC and SMC elsewhere.)
117+ */
118+
101119 cortex_a76_erratum_1463225_svc_handler();
102120 local_daif_restore(DAIF_PROCCTX);
103121 user_exit();
arch/arm64/kernel/traps.c+8−0
@@ -411,6 +411,13 @@ void do_undefinstr(struct pt_regs *regs) · 410 unmodified lines
411411 }
412412 NOKPROBE_SYMBOL(do_undefinstr);
413413
414+void do_bti(struct pt_regs *regs)
415+{
416+ BUG_ON(!user_mode(regs));
417+ force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
418+}
419+NOKPROBE_SYMBOL(do_bti);
420+
414421 #define __user_cache_maint(insn, address, res) \
415422 if (address >= user_addr_max()) { \
416423 res = -EFAULT; \
@@ -753,6 +760,7 @@ static const char *esr_class_str[] = { · 336 unmodified lines
753760 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
754761 [ESR_ELx_EC_PAC] = "PAC",
755762 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
763+ [ESR_ELx_EC_BTI] = "BTI",
756764 [ESR_ELx_EC_ILL] = "PSTATE.IL",
757765 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
758766 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
include/linux/mm.h+3−0
@@ -324,6 +324,9 @@ extern unsigned int kobjsize(const void *objp); · 323 unmodified lines
324324 #elif defined(CONFIG_SPARC64)
325325 # define VM_SPARC_ADI VM_ARCH_1 /* Uses ADI tag for access control */
326326 # define VM_ARCH_CLEAR VM_SPARC_ADI
327+#elif defined(CONFIG_ARM64)
328+# define VM_ARM64_BTI VM_ARCH_1 /* BTI guarded page, a.k.a. GP bit */
329+# define VM_ARCH_CLEAR VM_ARM64_BTI
327330 #elif !defined(CONFIG_MMU)
328331 # define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
329332 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t