LinuxPPC-Dev Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle
@ 2019-02-08 11:11 Russell Currey
  2019-02-27 12:06 ` Akshay Adiga
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Russell Currey @ 2019-02-08 11:11 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Russell Currey

Without restoring the IAMR after idle, execution prevention on POWER9
with Radix MMU is overwritten and the kernel can freely execute userspace without
faulting.

This is necessary when returning from any stop state that modifies user
state, as well as hypervisor state.

To test how this fails without this patch, load the lkdtm driver and
do the following:

   echo EXEC_USERSPACE > /sys/kernel/debug/provoke-crash/DIRECT

which won't fault, then boot the kernel with powersave=off, where it
will fault.  Applying this patch will fix this.

Fixes: 3b10d0095a1e ("powerpc/mm/radix: Prevent kernel execution of user
space")
Cc: <stable@vger.kernel.org>
Signed-off-by: Russell Currey <ruscur@russell.cc>
---
Since v1:
      - no longer use paca to save IAMR, instead use _DAR (thanks mpe)
      - remove isync and pnv_wakeup_noloss section (thanks Nick)

 arch/powerpc/kernel/idle_book3s.S | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/arch/powerpc/kernel/idle_book3s.S b/arch/powerpc/kernel/idle_book3s.S
index 7f5ac2e8581b..551cc4649021 100644
--- a/arch/powerpc/kernel/idle_book3s.S
+++ b/arch/powerpc/kernel/idle_book3s.S
@@ -200,6 +200,13 @@ pnv_powersave_common:
 	/* Continue saving state */
 	SAVE_GPR(2, r1)
 	SAVE_NVGPRS(r1)
+
+BEGIN_FTR_SECTION
+	/* _DAR is unused here, so (ab)use it to save the IAMR */
+	mfspr	r5,SPRN_IAMR
+	std	r5,_DAR(r1)
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
+
 	mfcr	r5
 	std	r5,_CCR(r1)
 	std	r1,PACAR1(r13)
@@ -924,6 +931,17 @@ BEGIN_FTR_SECTION
 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
 	REST_NVGPRS(r1)
 	REST_GPR(2, r1)
+
+BEGIN_FTR_SECTION
+	/* IAMR was saved in regs->dar in pnv_powersave_common */
+	ld	r4,_DAR(r1)
+	mtspr	SPRN_IAMR,r4
+	/*
+	 * We don't need an isync here because the upcoming mtmsrd is
+	 * execution synchronizing.
+	 */
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
+
 	ld	r4,PACAKMSR(r13)
 	ld	r5,_LINK(r1)
 	ld	r6,_CCR(r1)
-- 
2.20.1


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

* Re: [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle
  2019-02-08 11:11 [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle Russell Currey
@ 2019-02-27 12:06 ` Akshay Adiga
  2019-02-27 14:53 ` Nicholas Piggin
  2019-02-28  9:54 ` Gautham R Shenoy
  2 siblings, 0 replies; 4+ messages in thread
From: Akshay Adiga @ 2019-02-27 12:06 UTC (permalink / raw)
  To: Russell Currey; +Cc: linuxppc-dev

The changes look good to me.

On Fri, Feb 08, 2019 at 10:11:03PM +1100, Russell Currey wrote:
> Without restoring the IAMR after idle, execution prevention on POWER9
> with Radix MMU is overwritten and the kernel can freely execute userspace without
> faulting.
> 
> This is necessary when returning from any stop state that modifies user
> state, as well as hypervisor state.
> 
> To test how this fails without this patch, load the lkdtm driver and
> do the following:
> 
>    echo EXEC_USERSPACE > /sys/kernel/debug/provoke-crash/DIRECT
> 
> which won't fault, then boot the kernel with powersave=off, where it
> will fault.  Applying this patch will fix this.
> 
> Fixes: 3b10d0095a1e ("powerpc/mm/radix: Prevent kernel execution of user
> space")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Russell Currey <ruscur@russell.cc>

Reviewed-by: Akshay Adiga <akshay.adiga@linux.vnet.ibm.com>


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

* Re: [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle
  2019-02-08 11:11 [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle Russell Currey
  2019-02-27 12:06 ` Akshay Adiga
@ 2019-02-27 14:53 ` Nicholas Piggin
  2019-02-28  9:54 ` Gautham R Shenoy
  2 siblings, 0 replies; 4+ messages in thread
From: Nicholas Piggin @ 2019-02-27 14:53 UTC (permalink / raw)
  To: linuxppc-dev, Russell Currey

Russell Currey's on February 8, 2019 9:11 pm:
> Without restoring the IAMR after idle, execution prevention on POWER9
> with Radix MMU is overwritten and the kernel can freely execute userspace without
> faulting.
> 
> This is necessary when returning from any stop state that modifies user
> state, as well as hypervisor state.
> 
> To test how this fails without this patch, load the lkdtm driver and
> do the following:
> 
>    echo EXEC_USERSPACE > /sys/kernel/debug/provoke-crash/DIRECT
> 
> which won't fault, then boot the kernel with powersave=off, where it
> will fault.  Applying this patch will fix this.
> 
> Fixes: 3b10d0095a1e ("powerpc/mm/radix: Prevent kernel execution of user
> space")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Russell Currey <ruscur@russell.cc>
> ---
> Since v1:
>       - no longer use paca to save IAMR, instead use _DAR (thanks mpe)
>       - remove isync and pnv_wakeup_noloss section (thanks Nick)

Thanks for that, looks good.

Reviewed-by: Nicholas Piggin <npiggin@gmail.com>


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

* Re: [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle
  2019-02-08 11:11 [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle Russell Currey
  2019-02-27 12:06 ` Akshay Adiga
  2019-02-27 14:53 ` Nicholas Piggin
@ 2019-02-28  9:54 ` Gautham R Shenoy
  2 siblings, 0 replies; 4+ messages in thread
From: Gautham R Shenoy @ 2019-02-28  9:54 UTC (permalink / raw)
  To: Russell Currey; +Cc: linuxppc-dev

Hello Russell,

On Fri, Feb 08, 2019 at 10:11:03PM +1100, Russell Currey wrote:
> Without restoring the IAMR after idle, execution prevention on POWER9
> with Radix MMU is overwritten and the kernel can freely execute userspace without
> faulting.
> 
> This is necessary when returning from any stop state that modifies user
> state, as well as hypervisor state.
> 
> To test how this fails without this patch, load the lkdtm driver and
> do the following:
> 
>    echo EXEC_USERSPACE > /sys/kernel/debug/provoke-crash/DIRECT
> 
> which won't fault, then boot the kernel with powersave=off, where it
> will fault.  Applying this patch will fix this.
> 
> Fixes: 3b10d0095a1e ("powerpc/mm/radix: Prevent kernel execution of user
> space")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Russell Currey <ruscur@russell.cc>
> ---
> Since v1:
>       - no longer use paca to save IAMR, instead use _DAR (thanks mpe)

Looks good to me. Once we move to Nick Piggin's C-based save/restore
code, we will be saving all these SPR values on the stack anyway.

Reviewed-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>

--
Thanks and Regards
gautham.


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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-08 11:11 [PATCH v2] powerpc/powernv/idle: Restore IAMR after idle Russell Currey
2019-02-27 12:06 ` Akshay Adiga
2019-02-27 14:53 ` Nicholas Piggin
2019-02-28  9:54 ` Gautham R Shenoy

LinuxPPC-Dev Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linuxppc-dev/0 linuxppc-dev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linuxppc-dev linuxppc-dev/ https://lore.kernel.org/linuxppc-dev \
		linuxppc-dev@lists.ozlabs.org linuxppc-dev@ozlabs.org linuxppc-dev@archiver.kernel.org
	public-inbox-index linuxppc-dev


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.ozlabs.lists.linuxppc-dev


AGPL code for this site: git clone https://public-inbox.org/ public-inbox