Skip to content

Commit 265e60a

Browse files
cyrilbur-ibmmpe
authored andcommitted
powerpc/64s: Use emergency stack for kernel TM Bad Thing program checks
When using transactional memory (TM), the CPU can be in one of six states as far as TM is concerned, encoded in the Machine State Register (MSR). Certain state transitions are illegal and if attempted trigger a "TM Bad Thing" type program check exception. If we ever hit one of these exceptions it's treated as a bug, ie. we oops, and kill the process and/or panic, depending on configuration. One case where we can trigger a TM Bad Thing, is when returning to userspace after a system call or interrupt, using RFID. When this happens the CPU first restores the user register state, in particular r1 (the stack pointer) and then attempts to update the MSR. However the MSR update is not allowed and so we take the program check with the user register state, but the kernel MSR. This tricks the exception entry code into thinking we have a bad kernel stack pointer, because the MSR says we're coming from the kernel, but r1 is pointing to userspace. To avoid this we instead always switch to the emergency stack if we take a TM Bad Thing from the kernel. That way none of the user register values are used, other than for printing in the oops message. This is the fix for CVE-2017-1000255. Fixes: 5d176f7 ("powerpc: tm: Enable transactional memory (TM) lazily for userspace") Cc: stable@vger.kernel.org # v4.9+ Signed-off-by: Cyril Bur <cyrilbur@gmail.com> [mpe: Rewrite change log & comments, tweak asm slightly] Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 53ecde0 commit 265e60a

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

‎arch/powerpc/kernel/exceptions-64s.S‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,29 @@ EXC_REAL(program_check, 0x700, 0x100)
734734
EXC_VIRT(program_check, 0x4700, 0x100, 0x700)
735735
TRAMP_KVM(PACA_EXGEN, 0x700)
736736
EXC_COMMON_BEGIN(program_check_common)
737-
EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
737+
/*
738+
* It's possible to receive a TM Bad Thing type program check with
739+
* userspace register values (in particular r1), but with SRR1 reporting
740+
* that we came from the kernel. Normally that would confuse the bad
741+
* stack logic, and we would report a bad kernel stack pointer. Instead
742+
* we switch to the emergency stack if we're taking a TM Bad Thing from
743+
* the kernel.
744+
*/
745+
li r10,MSR_PR /* Build a mask of MSR_PR .. */
746+
oris r10,r10,0x200000@h /* .. and SRR1_PROGTM */
747+
and r10,r10,r12 /* Mask SRR1 with that. */
748+
srdi r10,r10,8 /* Shift it so we can compare */
749+
cmpldi r10,(0x200000 >> 8) /* .. with an immediate. */
750+
bne 1f /* If != go to normal path. */
751+
752+
/* SRR1 had PR=0 and SRR1_PROGTM=1, so use the emergency stack */
753+
andi. r10,r12,MSR_PR; /* Set CR0 correctly for label */
754+
/* 3 in EXCEPTION_PROLOG_COMMON */
755+
mr r10,r1 /* Save r1 */
756+
ld r1,PACAEMERGSP(r13) /* Use emergency stack */
757+
subi r1,r1,INT_FRAME_SIZE /* alloc stack frame */
758+
b 3f /* Jump into the macro !! */
759+
1: EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
738760
bl save_nvgprs
739761
RECONCILE_IRQ_STATE(r10, r11)
740762
addi r3,r1,STACK_FRAME_OVERHEAD

0 commit comments

Comments
 (0)