Skip to content

Commit 06ec27a

Browse files
chleroympe
authored andcommitted
powerpc/64: add stack protector support
On PPC64, as register r13 points to the paca_struct at all time, this patch adds a copy of the canary there, which is copied at task_switch. That new canary is then used by using the following GCC options: -mstack-protector-guard=tls -mstack-protector-guard-reg=r13 -mstack-protector-guard-offset=offsetof(struct paca_struct, canary)) Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent c3ff2a5 commit 06ec27a

File tree

6 files changed

+23
-1
lines changed

6 files changed

+23
-1
lines changed

arch/powerpc/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ config PPC
180180
select HAVE_ARCH_SECCOMP_FILTER
181181
select HAVE_ARCH_TRACEHOOK
182182
select HAVE_CBPF_JIT if !PPC64
183-
select HAVE_STACKPROTECTOR if $(cc-option,-mstack-protector-guard=tls) && PPC32
183+
select HAVE_STACKPROTECTOR if $(cc-option,-mstack-protector-guard=tls)
184184
select HAVE_CONTEXT_TRACKING if PPC64
185185
select HAVE_DEBUG_KMEMLEAK
186186
select HAVE_DEBUG_STACKOVERFLOW

arch/powerpc/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ KBUILD_ARFLAGS += --target=elf$(BITS)-$(GNUTARGET)
113113
endif
114114

115115
cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard=tls
116+
ifdef CONFIG_PPC64
117+
cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard-reg=r13
118+
else
116119
cflags-$(CONFIG_STACKPROTECTOR) += -mstack-protector-guard-reg=r2
120+
endif
117121

118122
LDFLAGS_vmlinux-y := -Bstatic
119123
LDFLAGS_vmlinux-$(CONFIG_RELOCATABLE) := -pie
@@ -411,8 +415,12 @@ ifdef CONFIG_STACKPROTECTOR
411415
prepare: stack_protector_prepare
412416

413417
stack_protector_prepare: prepare0
418+
ifdef CONFIG_PPC64
419+
$(eval KBUILD_CFLAGS += -mstack-protector-guard-offset=$(shell awk '{if ($$2 == "PACA_CANARY") print $$3;}' include/generated/asm-offsets.h))
420+
else
414421
$(eval KBUILD_CFLAGS += -mstack-protector-guard-offset=$(shell awk '{if ($$2 == "TASK_CANARY") print $$3;}' include/generated/asm-offsets.h))
415422
endif
423+
endif
416424

417425
# Use the file '.tmp_gas_check' for binutils tests, as gas won't output
418426
# to stdout and these checks are run even on install targets.

arch/powerpc/include/asm/paca.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ struct paca_struct {
256256
struct slb_entry *mce_faulty_slbs;
257257
u16 slb_save_cache_ptr;
258258
#endif /* CONFIG_PPC_BOOK3S_64 */
259+
#ifdef CONFIG_STACKPROTECTOR
260+
unsigned long canary;
261+
#endif
259262
} ____cacheline_aligned;
260263

261264
extern void copy_mm_to_paca(struct mm_struct *mm);

arch/powerpc/include/asm/stackprotector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/version.h>
1212
#include <asm/reg.h>
1313
#include <asm/current.h>
14+
#include <asm/paca.h>
1415

1516
/*
1617
* Initialize the stackprotector canary value.
@@ -29,6 +30,9 @@ static __always_inline void boot_init_stack_canary(void)
2930
canary &= CANARY_MASK;
3031

3132
current->stack_canary = canary;
33+
#ifdef CONFIG_PPC64
34+
get_paca()->canary = canary;
35+
#endif
3236
}
3337

3438
#endif /* _ASM_STACKPROTECTOR_H */

arch/powerpc/kernel/asm-offsets.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ int main(void)
8181
OFFSET(MM, task_struct, mm);
8282
#ifdef CONFIG_STACKPROTECTOR
8383
OFFSET(TASK_CANARY, task_struct, stack_canary);
84+
#ifdef CONFIG_PPC64
85+
OFFSET(PACA_CANARY, paca_struct, canary);
86+
#endif
8487
#endif
8588
OFFSET(MMCONTEXTID, mm_struct, context.id);
8689
#ifdef CONFIG_PPC64

arch/powerpc/kernel/entry_64.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ _GLOBAL(_switch)
624624

625625
addi r6,r4,-THREAD /* Convert THREAD to 'current' */
626626
std r6,PACACURRENT(r13) /* Set new 'current' */
627+
#if defined(CONFIG_STACKPROTECTOR)
628+
ld r6, TASK_CANARY(r6)
629+
std r6, PACA_CANARY(r13)
630+
#endif
627631

628632
ld r8,KSP(r4) /* new stack pointer */
629633
#ifdef CONFIG_PPC_BOOK3S_64

0 commit comments

Comments
 (0)