linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds
@ 2018-12-12 14:02 Diana Craciun
  2018-12-12 14:03 ` [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush Diana Craciun
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:02 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

Implement Spectre variant 2 workarounds for NXP PowerPC Book3E
processors.

Diana Craciun (11):
  Add infrastructure to fixup branch predictor flush
  Add macro to flush the branch predictor
  Fix spectre_v2 mitigations reporting
  Emulate SPRN_BUCSR register
  Add nospectre_v2 command line argument
  Flush the branch predictor at each kernel entry (64bit)
  Flush the branch predictor at each kernel entry (32 bit)
  Flush branch predictor when entering KVM
  Enable runtime patching if nospectre_v2 boot arg is used
  Update Spectre v2 reporting
  Add FSL_PPC_BOOK3E as supported arch for nospectre_v2 boot arg

 Documentation/admin-guide/kernel-parameters.txt |  2 +-
 arch/powerpc/include/asm/feature-fixups.h       | 12 +++++++++++
 arch/powerpc/include/asm/ppc_asm.h              | 10 +++++++++
 arch/powerpc/include/asm/setup.h                |  7 +++++++
 arch/powerpc/kernel/entry_64.S                  |  5 +++++
 arch/powerpc/kernel/exceptions-64e.S            | 26 ++++++++++++++++++++++-
 arch/powerpc/kernel/head_booke.h                |  6 ++++++
 arch/powerpc/kernel/head_fsl_booke.S            | 15 +++++++++++++
 arch/powerpc/kernel/security.c                  | 28 +++++++++++++++++++++++--
 arch/powerpc/kernel/setup-common.c              |  1 +
 arch/powerpc/kernel/vmlinux.lds.S               |  8 +++++++
 arch/powerpc/kvm/bookehv_interrupts.S           |  4 ++++
 arch/powerpc/kvm/e500_emulate.c                 |  5 +++++
 arch/powerpc/lib/feature-fixups.c               | 21 +++++++++++++++++++
 arch/powerpc/mm/tlb_low_64e.S                   |  7 +++++++
 15 files changed, 153 insertions(+), 4 deletions(-)

-- 
2.5.5


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-22  9:54   ` [01/11] " Michael Ellerman
  2018-12-12 14:03 ` [PATCH 02/11] powerpc/fsl: Add macro to flush the branch predictor Diana Craciun
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

In order to protect against speculation attacks (Spectre
variant 2) on NXP PowerPC platforms, the branch predictor
should be flushed when the privillege level is changed.
This patch is adding the infrastructure to fixup at runtime
the code sections that are performing the branch predictor flush
depending on a boot arg parameter which is added later in a
separate patch.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/include/asm/feature-fixups.h | 12 ++++++++++++
 arch/powerpc/include/asm/setup.h          |  2 ++
 arch/powerpc/kernel/vmlinux.lds.S         |  8 ++++++++
 arch/powerpc/lib/feature-fixups.c         | 21 +++++++++++++++++++++
 4 files changed, 43 insertions(+)

diff --git a/arch/powerpc/include/asm/feature-fixups.h b/arch/powerpc/include/asm/feature-fixups.h
index 33b6f9c..40a6c926 100644
--- a/arch/powerpc/include/asm/feature-fixups.h
+++ b/arch/powerpc/include/asm/feature-fixups.h
@@ -221,6 +221,17 @@ label##3:					       	\
 	FTR_ENTRY_OFFSET 953b-954b;			\
 	.popsection;
 
+#define START_BTB_FLUSH_SECTION			\
+955:							\
+
+#define END_BTB_FLUSH_SECTION			\
+956:							\
+	.pushsection __btb_flush_fixup,"a";	\
+	.align 2;							\
+957:						\
+	FTR_ENTRY_OFFSET 955b-957b;			\
+	FTR_ENTRY_OFFSET 956b-957b;			\
+	.popsection;
 
 #ifndef __ASSEMBLY__
 #include <linux/types.h>
@@ -230,6 +241,7 @@ extern long __start___stf_entry_barrier_fixup, __stop___stf_entry_barrier_fixup;
 extern long __start___stf_exit_barrier_fixup, __stop___stf_exit_barrier_fixup;
 extern long __start___rfi_flush_fixup, __stop___rfi_flush_fixup;
 extern long __start___barrier_nospec_fixup, __stop___barrier_nospec_fixup;
+extern long __start__btb_flush_fixup, __stop__btb_flush_fixup;
 
 void apply_feature_fixups(void);
 void setup_feature_keys(void);
diff --git a/arch/powerpc/include/asm/setup.h b/arch/powerpc/include/asm/setup.h
index 1fffbba..c941c8c 100644
--- a/arch/powerpc/include/asm/setup.h
+++ b/arch/powerpc/include/asm/setup.h
@@ -67,6 +67,8 @@ void do_barrier_nospec_fixups_range(bool enable, void *start, void *end);
 static inline void do_barrier_nospec_fixups_range(bool enable, void *start, void *end) { };
 #endif
 
+void do_btb_flush_fixups(void);
+
 #endif /* !__ASSEMBLY__ */
 
 #endif	/* _ASM_POWERPC_SETUP_H */
diff --git a/arch/powerpc/kernel/vmlinux.lds.S b/arch/powerpc/kernel/vmlinux.lds.S
index 434581b..254b757 100644
--- a/arch/powerpc/kernel/vmlinux.lds.S
+++ b/arch/powerpc/kernel/vmlinux.lds.S
@@ -170,6 +170,14 @@ SECTIONS
 	}
 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
 
+#ifdef CONFIG_PPC_FSL_BOOK3E
+	. = ALIGN(8);
+	__spec_btb_flush_fixup : AT(ADDR(__spec_btb_flush_fixup) - LOAD_OFFSET) {
+		__start__btb_flush_fixup = .;
+		*(__btb_flush_fixup)
+		__stop__btb_flush_fixup = .;
+	}
+#endif
 	EXCEPTION_TABLE(0)
 
 	NOTES :kernel :notes
diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index e613b02..02a213c 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -347,6 +347,27 @@ void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_
 
 	printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
 }
+static void patch_btb_flush_section(long *curr)
+{
+	unsigned int *start, *end;
+
+	start = (void *)curr + *curr;
+	end = (void *)curr + *(curr + 1);
+	for (; start < end; start++) {
+		pr_devel("patching dest %lx\n", (unsigned long)start);
+		patch_instruction(start, PPC_INST_NOP);
+	}
+}
+void do_btb_flush_fixups(void)
+{
+	long *start, *end;
+
+	start = PTRRELOC(&__start__btb_flush_fixup);
+	end = PTRRELOC(&__stop__btb_flush_fixup);
+
+	for (; start < end; start += 2)
+		patch_btb_flush_section(start);
+}
 #endif /* CONFIG_PPC_FSL_BOOK3E */
 
 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 02/11] powerpc/fsl: Add macro to flush the branch predictor
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
  2018-12-12 14:03 ` [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 03/11] powerpc/fsl: Fix spectre_v2 mitigations reporting Diana Craciun
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

The BUCSR register can be used to invalidate the entries in the
branch prediction mechanisms.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/include/asm/ppc_asm.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/powerpc/include/asm/ppc_asm.h b/arch/powerpc/include/asm/ppc_asm.h
index b5d0236..5c901bf 100644
--- a/arch/powerpc/include/asm/ppc_asm.h
+++ b/arch/powerpc/include/asm/ppc_asm.h
@@ -821,4 +821,14 @@ END_FTR_SECTION_IFCLR(CPU_FTR_601)
 	stringify_in_c(.long (_target) - . ;)	\
 	stringify_in_c(.previous)
 
+#ifdef CONFIG_PPC_FSL_BOOK3E
+#define BTB_FLUSH(reg)			\
+	lis reg,BUCSR_INIT@h;		\
+	ori reg,reg,BUCSR_INIT@l;	\
+	mtspr SPRN_BUCSR,reg;		\
+	isync;
+#else
+#define BTB_FLUSH(reg)
+#endif /* CONFIG_PPC_FSL_BOOK3E */
+
 #endif /* _ASM_POWERPC_PPC_ASM_H */
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 03/11] powerpc/fsl: Fix spectre_v2 mitigations reporting
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
  2018-12-12 14:03 ` [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush Diana Craciun
  2018-12-12 14:03 ` [PATCH 02/11] powerpc/fsl: Add macro to flush the branch predictor Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 04/11] powerpc/fsl: Emulate SPRN_BUCSR register Diana Craciun
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

Currently for CONFIG_PPC_FSL_BOOK3E
cat /sys/devices/system/cpu/vulnerabilities/spectre_v2 reports:
"Mitigation: Software count cache flush" which is wrong. Fix it
to report vulnerable for now.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kernel/security.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/security.c b/arch/powerpc/kernel/security.c
index f6f469f..1b395b8 100644
--- a/arch/powerpc/kernel/security.c
+++ b/arch/powerpc/kernel/security.c
@@ -22,7 +22,7 @@ enum count_cache_flush_type {
 	COUNT_CACHE_FLUSH_SW	= 0x2,
 	COUNT_CACHE_FLUSH_HW	= 0x4,
 };
-static enum count_cache_flush_type count_cache_flush_type;
+static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
 
 bool barrier_nospec_enabled;
 static bool no_nospec;
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 04/11] powerpc/fsl: Emulate SPRN_BUCSR register
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (2 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 03/11] powerpc/fsl: Fix spectre_v2 mitigations reporting Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 05/11] powerpc/fsl: Add nospectre_v2 command line argument Diana Craciun
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

In order to flush the branch predictor the guest kernel
performs writes to the BUCSR register which is hypervisor
privilleged. However, the branch predictor is flushed at
each KVM entry, so the branch predictor has been already
flushed, so just return as soon as possible to guest.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kvm/e500_emulate.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/powerpc/kvm/e500_emulate.c b/arch/powerpc/kvm/e500_emulate.c
index 3f8189e..d0eb670 100644
--- a/arch/powerpc/kvm/e500_emulate.c
+++ b/arch/powerpc/kvm/e500_emulate.c
@@ -276,6 +276,11 @@ int kvmppc_core_emulate_mtspr_e500(struct kvm_vcpu *vcpu, int sprn, ulong spr_va
 		 */
 		vcpu->arch.pwrmgtcr0 = spr_val;
 		break;
+		/* if we are here, it means that we have already flushed the
+		 * branch predictor, so just return to guest
+		 */
+	case SPRN_BUCSR:
+		break;
 
 	/* extra exceptions */
 #ifdef CONFIG_SPE_POSSIBLE
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 05/11] powerpc/fsl: Add nospectre_v2 command line argument
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (3 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 04/11] powerpc/fsl: Emulate SPRN_BUCSR register Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 06/11] powerpc/fsl: Flush the branch predictor at each kernel entry (64bit) Diana Craciun
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

When the command line argument is present, the Spectre variant 2
mitigations are disabled.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/include/asm/setup.h |  5 +++++
 arch/powerpc/kernel/security.c   | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/arch/powerpc/include/asm/setup.h b/arch/powerpc/include/asm/setup.h
index c941c8c..65676e2 100644
--- a/arch/powerpc/include/asm/setup.h
+++ b/arch/powerpc/include/asm/setup.h
@@ -67,6 +67,11 @@ void do_barrier_nospec_fixups_range(bool enable, void *start, void *end);
 static inline void do_barrier_nospec_fixups_range(bool enable, void *start, void *end) { };
 #endif
 
+#ifdef CONFIG_PPC_FSL_BOOK3E
+void setup_spectre_v2(void);
+#else
+static inline void setup_spectre_v2(void) {};
+#endif
 void do_btb_flush_fixups(void);
 
 #endif /* !__ASSEMBLY__ */
diff --git a/arch/powerpc/kernel/security.c b/arch/powerpc/kernel/security.c
index 1b395b8..4393a38 100644
--- a/arch/powerpc/kernel/security.c
+++ b/arch/powerpc/kernel/security.c
@@ -26,6 +26,10 @@ static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NO
 
 bool barrier_nospec_enabled;
 static bool no_nospec;
+static bool btb_flush_enabled;
+#ifdef CONFIG_PPC_FSL_BOOK3E
+static bool no_spectrev2;
+#endif
 
 static void enable_barrier_nospec(bool enable)
 {
@@ -101,6 +105,23 @@ static __init int barrier_nospec_debugfs_init(void)
 device_initcall(barrier_nospec_debugfs_init);
 #endif /* CONFIG_DEBUG_FS */
 
+#ifdef CONFIG_PPC_FSL_BOOK3E
+static int __init handle_nospectre_v2(char *p)
+{
+	no_spectrev2 = true;
+
+	return 0;
+}
+early_param("nospectre_v2", handle_nospectre_v2);
+void setup_spectre_v2(void)
+{
+	if (no_spectrev2)
+		do_btb_flush_fixups();
+	else
+		btb_flush_enabled = true;
+}
+#endif /* CONFIG_PPC_FSL_BOOK3E */
+
 #ifdef CONFIG_PPC_BOOK3S_64
 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
 {
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 06/11] powerpc/fsl: Flush the branch predictor at each kernel entry (64bit)
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (4 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 05/11] powerpc/fsl: Add nospectre_v2 command line argument Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit) Diana Craciun
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

In order to protect against speculation attacks on
indirect branches, the branch predictor is flushed at
kernel entry to protect for the following situations:
- userspace process attacking another userspace process
- userspace process attacking the kernel
Basically when the privillege level change (i.e. the
kernel is entered), the branch predictor state is flushed.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kernel/entry_64.S       |  5 +++++
 arch/powerpc/kernel/exceptions-64e.S | 26 +++++++++++++++++++++++++-
 arch/powerpc/mm/tlb_low_64e.S        |  7 +++++++
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 7b1693a..7c2032e 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -80,6 +80,11 @@ END_FTR_SECTION_IFSET(CPU_FTR_TM)
 	std	r0,GPR0(r1)
 	std	r10,GPR1(r1)
 	beq	2f			/* if from kernel mode */
+#ifdef CONFIG_PPC_FSL_BOOK3E
+START_BTB_FLUSH_SECTION
+	BTB_FLUSH(r10)
+END_BTB_FLUSH_SECTION
+#endif
 	ACCOUNT_CPU_USER_ENTRY(r13, r10, r11)
 2:	std	r2,GPR2(r1)
 	std	r3,GPR3(r1)
diff --git a/arch/powerpc/kernel/exceptions-64e.S b/arch/powerpc/kernel/exceptions-64e.S
index 6d6e144..afb6387 100644
--- a/arch/powerpc/kernel/exceptions-64e.S
+++ b/arch/powerpc/kernel/exceptions-64e.S
@@ -296,7 +296,8 @@ ret_from_mc_except:
 	andi.	r10,r11,MSR_PR;		/* save stack pointer */	    \
 	beq	1f;			/* branch around if supervisor */   \
 	ld	r1,PACAKSAVE(r13);	/* get kernel stack coming from usr */\
-1:	cmpdi	cr1,r1,0;		/* check if SP makes sense */	    \
+1:	type##_BTB_FLUSH		\
+	cmpdi	cr1,r1,0;		/* check if SP makes sense */	    \
 	bge-	cr1,exc_##n##_bad_stack;/* bad stack (TODO: out of line) */ \
 	mfspr	r10,SPRN_##type##_SRR0;	/* read SRR0 before touching stack */
 
@@ -328,6 +329,29 @@ ret_from_mc_except:
 #define SPRN_MC_SRR0	SPRN_MCSRR0
 #define SPRN_MC_SRR1	SPRN_MCSRR1
 
+#ifdef CONFIG_PPC_FSL_BOOK3E
+#define GEN_BTB_FLUSH			\
+	START_BTB_FLUSH_SECTION		\
+		beq 1f;			\
+		BTB_FLUSH(r10)			\
+		1:		\
+	END_BTB_FLUSH_SECTION
+
+#define CRIT_BTB_FLUSH			\
+	START_BTB_FLUSH_SECTION		\
+		BTB_FLUSH(r10)		\
+	END_BTB_FLUSH_SECTION
+
+#define DBG_BTB_FLUSH CRIT_BTB_FLUSH
+#define MC_BTB_FLUSH CRIT_BTB_FLUSH
+#define GDBELL_BTB_FLUSH GEN_BTB_FLUSH
+#else
+#define GEN_BTB_FLUSH
+#define CRIT_BTB_FLUSH
+#define DBG_BTB_FLUSH
+#define GDBELL_BTB_FLUSH
+#endif
+
 #define NORMAL_EXCEPTION_PROLOG(n, intnum, addition)			    \
 	EXCEPTION_PROLOG(n, intnum, GEN, addition##_GEN(n))
 
diff --git a/arch/powerpc/mm/tlb_low_64e.S b/arch/powerpc/mm/tlb_low_64e.S
index 7fd20c5..9ed9006 100644
--- a/arch/powerpc/mm/tlb_low_64e.S
+++ b/arch/powerpc/mm/tlb_low_64e.S
@@ -70,6 +70,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
 	std	r15,EX_TLB_R15(r12)
 	std	r10,EX_TLB_CR(r12)
 #ifdef CONFIG_PPC_FSL_BOOK3E
+START_BTB_FLUSH_SECTION
+	mfspr r11, SPRN_SRR1
+	andi. r10,r11,MSR_PR
+	beq 1f
+	BTB_FLUSH(r10)
+1:
+END_BTB_FLUSH_SECTION
 	std	r7,EX_TLB_R7(r12)
 #endif
 	TLB_MISS_PROLOG_STATS
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit)
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (5 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 06/11] powerpc/fsl: Flush the branch predictor at each kernel entry (64bit) Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-19  9:42   ` kbuild test robot
  2018-12-12 14:03 ` [PATCH 08/11] powerpc/fsl: Flush branch predictor when entering KVM Diana Craciun
                   ` (3 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

In order to protect against speculation attacks on
indirect branches, the branch predictor is flushed at
kernel entry to protect for the following situations:
- userspace process attacking another userspace process
- userspace process attacking the kernel
Basically when the privillege level change (i.e.the kernel
is entered), the branch predictor state is flushed.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kernel/head_booke.h     |  6 ++++++
 arch/powerpc/kernel/head_fsl_booke.S | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/arch/powerpc/kernel/head_booke.h b/arch/powerpc/kernel/head_booke.h
index d0862a1..15ac510 100644
--- a/arch/powerpc/kernel/head_booke.h
+++ b/arch/powerpc/kernel/head_booke.h
@@ -43,6 +43,9 @@
 	andi.	r11, r11, MSR_PR;	/* check whether user or kernel    */\
 	mr	r11, r1;						     \
 	beq	1f;							     \
+START_BTB_FLUSH_SECTION					\
+	BTB_FLUSH(r11)						\
+END_BTB_FLUSH_SECTION					\
 	/* if from user, start at top of this thread's kernel stack */       \
 	lwz	r11, THREAD_INFO-THREAD(r10);				     \
 	ALLOC_STACK_FRAME(r11, THREAD_SIZE);				     \
@@ -128,6 +131,9 @@
 	stw	r9,_CCR(r8);		/* save CR on stack		   */\
 	mfspr	r11,exc_level_srr1;	/* check whether user or kernel    */\
 	DO_KVM	BOOKE_INTERRUPT_##intno exc_level_srr1;		             \
+START_BTB_FLUSH_SECTION								\
+	BTB_FLUSH(r10)									\
+END_BTB_FLUSH_SECTION								\
 	andi.	r11,r11,MSR_PR;						     \
 	mfspr	r11,SPRN_SPRG_THREAD;	/* if from user, start at top of   */\
 	lwz	r11,THREAD_INFO-THREAD(r11); /* this thread's kernel stack */\
diff --git a/arch/powerpc/kernel/head_fsl_booke.S b/arch/powerpc/kernel/head_fsl_booke.S
index e2750b8..2386ce2 100644
--- a/arch/powerpc/kernel/head_fsl_booke.S
+++ b/arch/powerpc/kernel/head_fsl_booke.S
@@ -453,6 +453,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
 	mfcr	r13
 	stw	r13, THREAD_NORMSAVE(3)(r10)
 	DO_KVM	BOOKE_INTERRUPT_DTLB_MISS SPRN_SRR1
+START_BTB_FLUSH_SECTION
+	mfspr r11, SPRN_SRR1
+	andi. r10,r11,MSR_PR
+	beq 1f
+	BTB_FLUSH(r10)
+1:
+END_BTB_FLUSH_SECTION
 	mfspr	r10, SPRN_DEAR		/* Get faulting address */
 
 	/* If we are faulting a kernel address, we have to use the
@@ -547,6 +554,14 @@ END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
 	mfcr	r13
 	stw	r13, THREAD_NORMSAVE(3)(r10)
 	DO_KVM	BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR1
+START_BTB_FLUSH_SECTION
+	mfspr r11, SPRN_SRR1
+	andi. r10,r11,MSR_PR
+	beq 1f
+	BTB_FLUSH(r10)
+1:
+END_BTB_FLUSH_SECTION
+
 	mfspr	r10, SPRN_SRR0		/* Get faulting address */
 
 	/* If we are faulting a kernel address, we have to use the
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 08/11] powerpc/fsl: Flush branch predictor when entering KVM
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (6 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit) Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 09/11] powerpc/fsl: Enable runtime patching if nospectre_v2 boot arg is used Diana Craciun
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

Switching from the guest to host is another place
where the speculative accesses can be exploited.
Flush the branch predictor when entering KVM.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kvm/bookehv_interrupts.S | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/powerpc/kvm/bookehv_interrupts.S b/arch/powerpc/kvm/bookehv_interrupts.S
index 051af7d..4e5081e 100644
--- a/arch/powerpc/kvm/bookehv_interrupts.S
+++ b/arch/powerpc/kvm/bookehv_interrupts.S
@@ -75,6 +75,10 @@
 	PPC_LL	r1, VCPU_HOST_STACK(r4)
 	PPC_LL	r2, HOST_R2(r1)
 
+START_BTB_FLUSH_SECTION
+	BTB_FLUSH(r10)
+END_BTB_FLUSH_SECTION
+
 	mfspr	r10, SPRN_PID
 	lwz	r8, VCPU_HOST_PID(r4)
 	PPC_LL	r11, VCPU_SHARED(r4)
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 09/11] powerpc/fsl: Enable runtime patching if nospectre_v2 boot arg is used
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (7 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 08/11] powerpc/fsl: Flush branch predictor when entering KVM Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 10/11] powerpc/fsl: Update Spectre v2 reporting Diana Craciun
  2018-12-12 14:03 ` [PATCH 11/11] powerpc/fsl: Add FSL_PPC_BOOK3E as supported arch for nospectre_v2 boot arg Diana Craciun
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

If the user choses not to use the mitigations, replace
the code sequence with nops.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kernel/setup-common.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
index 93ee370..f27eeda 100644
--- a/arch/powerpc/kernel/setup-common.c
+++ b/arch/powerpc/kernel/setup-common.c
@@ -974,6 +974,7 @@ void __init setup_arch(char **cmdline_p)
 		ppc_md.setup_arch();
 
 	setup_barrier_nospec();
+	setup_spectre_v2();
 
 	paging_init();
 
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 10/11] powerpc/fsl: Update Spectre v2 reporting
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (8 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 09/11] powerpc/fsl: Enable runtime patching if nospectre_v2 boot arg is used Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  2018-12-12 14:03 ` [PATCH 11/11] powerpc/fsl: Add FSL_PPC_BOOK3E as supported arch for nospectre_v2 boot arg Diana Craciun
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

Report branch predictor state flush as a mitigation for
Spectre variant 2.

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 arch/powerpc/kernel/security.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/security.c b/arch/powerpc/kernel/security.c
index 4393a38..861fab3 100644
--- a/arch/powerpc/kernel/security.c
+++ b/arch/powerpc/kernel/security.c
@@ -212,8 +212,11 @@ ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, c
 
 		if (count_cache_flush_type == COUNT_CACHE_FLUSH_HW)
 			seq_buf_printf(&s, "(hardware accelerated)");
-	} else
+	} else if (btb_flush_enabled) {
+		seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
+	} else {
 		seq_buf_printf(&s, "Vulnerable");
+	}
 
 	seq_buf_printf(&s, "\n");
 
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 11/11] powerpc/fsl: Add FSL_PPC_BOOK3E as supported arch for nospectre_v2 boot arg
  2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
                   ` (9 preceding siblings ...)
  2018-12-12 14:03 ` [PATCH 10/11] powerpc/fsl: Update Spectre v2 reporting Diana Craciun
@ 2018-12-12 14:03 ` Diana Craciun
  10 siblings, 0 replies; 14+ messages in thread
From: Diana Craciun @ 2018-12-12 14:03 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Diana Craciun

Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
---
 Documentation/admin-guide/kernel-parameters.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index aefd358..cf6b4c5 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2827,7 +2827,7 @@
 			check bypass). With this option data leaks are possible
 			in the system.
 
-	nospectre_v2	[X86] Disable all mitigations for the Spectre variant 2
+	nospectre_v2	[X86,PPC_FSL_BOOK3E] Disable all mitigations for the Spectre variant 2
 			(indirect branch prediction) vulnerability. System may
 			allow data leaks with this option, which is equivalent
 			to spectre_v2=off.
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit)
  2018-12-12 14:03 ` [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit) Diana Craciun
@ 2018-12-19  9:42   ` kbuild test robot
  0 siblings, 0 replies; 14+ messages in thread
From: kbuild test robot @ 2018-12-19  9:42 UTC (permalink / raw)
  To: Diana Craciun; +Cc: Diana Craciun, linuxppc-dev, kbuild-all

[-- Attachment #1: Type: text/plain, Size: 1500 bytes --]

Hi Diana,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on powerpc/next]
[also build test WARNING on v4.20-rc7 next-20181218]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Diana-Craciun/powerpc-fsl-NXP-PowerPC-Spectre-variant-2-workarounds/20181213-015503
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc-icon_defconfig (attached as .config)
compiler: powerpc-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        GCC_VERSION=7.2.0 make.cross ARCH=powerpc 

All warnings (new ones prefixed by >>):

>> powerpc-linux-gnu-ld: warning: orphan section `__btb_flush_fixup' from `arch/powerpc/kernel/head_44x.o' being placed in section `__btb_flush_fixup'.
>> powerpc-linux-gnu-ld: warning: orphan section `__btb_flush_fixup' from `arch/powerpc/kernel/head_44x.o' being placed in section `__btb_flush_fixup'.
>> powerpc-linux-gnu-ld: warning: orphan section `__btb_flush_fixup' from `arch/powerpc/kernel/head_44x.o' being placed in section `__btb_flush_fixup'.

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 15113 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush
  2018-12-12 14:03 ` [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush Diana Craciun
@ 2018-12-22  9:54   ` Michael Ellerman
  0 siblings, 0 replies; 14+ messages in thread
From: Michael Ellerman @ 2018-12-22  9:54 UTC (permalink / raw)
  To: Diana Craciun, linuxppc-dev; +Cc: Diana Craciun

On Wed, 2018-12-12 at 14:03:00 UTC, Diana Craciun wrote:
> In order to protect against speculation attacks (Spectre
> variant 2) on NXP PowerPC platforms, the branch predictor
> should be flushed when the privillege level is changed.
> This patch is adding the infrastructure to fixup at runtime
> the code sections that are performing the branch predictor flush
> depending on a boot arg parameter which is added later in a
> separate patch.
> 
> Signed-off-by: Diana Craciun <diana.craciun@nxp.com>

Series applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/76a5eaa38b15dda92cd6964248c39b

cheers

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-12-22 10:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-12 14:02 [PATCH 00/11] powerpc/fsl: NXP PowerPC Spectre variant 2 workarounds Diana Craciun
2018-12-12 14:03 ` [PATCH 01/11] powerpc/fsl: Add infrastructure to fixup branch predictor flush Diana Craciun
2018-12-22  9:54   ` [01/11] " Michael Ellerman
2018-12-12 14:03 ` [PATCH 02/11] powerpc/fsl: Add macro to flush the branch predictor Diana Craciun
2018-12-12 14:03 ` [PATCH 03/11] powerpc/fsl: Fix spectre_v2 mitigations reporting Diana Craciun
2018-12-12 14:03 ` [PATCH 04/11] powerpc/fsl: Emulate SPRN_BUCSR register Diana Craciun
2018-12-12 14:03 ` [PATCH 05/11] powerpc/fsl: Add nospectre_v2 command line argument Diana Craciun
2018-12-12 14:03 ` [PATCH 06/11] powerpc/fsl: Flush the branch predictor at each kernel entry (64bit) Diana Craciun
2018-12-12 14:03 ` [PATCH 07/11] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit) Diana Craciun
2018-12-19  9:42   ` kbuild test robot
2018-12-12 14:03 ` [PATCH 08/11] powerpc/fsl: Flush branch predictor when entering KVM Diana Craciun
2018-12-12 14:03 ` [PATCH 09/11] powerpc/fsl: Enable runtime patching if nospectre_v2 boot arg is used Diana Craciun
2018-12-12 14:03 ` [PATCH 10/11] powerpc/fsl: Update Spectre v2 reporting Diana Craciun
2018-12-12 14:03 ` [PATCH 11/11] powerpc/fsl: Add FSL_PPC_BOOK3E as supported arch for nospectre_v2 boot arg Diana Craciun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).