From: Christophe Leroy <christophe.leroy@c-s.fr>
To: Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Paul Mackerras <paulus@samba.org>,
Michael Ellerman <mpe@ellerman.id.au>,
ruscur@russell.cc
Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 06/11] powerpc/8xx: Add Kernel Userspace Access Protection
Date: Wed, 28 Nov 2018 09:27:14 +0000 (UTC) [thread overview]
Message-ID: <9a7dc3917c7c865c0eef66a671439afe92165bba.1543356926.git.christophe.leroy@c-s.fr> (raw)
In-Reply-To: <76d777b36e54e7b8d4c196405decc712fc5eaf45.1543356926.git.christophe.leroy@c-s.fr>
This patch adds Kernel Userspace Access Protection on the 8xx.
When a page is RO or RW, it is set RO or RW for Key 0 and NA
for Key 1.
Up to now, the User group is defined with Key 0 for both User and
Supervisor.
By changing the group to Key 0 for User and Key 1 for Supervisor,
this patch prevents the Kernel from being able to access user data.
At exception entry, the kernel saves SPRN_MD_AP in the regs struct,
and reapply the protection. At exception exit it restore SPRN_MD_AP
with the value it had on exception entry.
For the time being, the unused mq field of pt_regs struct is used for
that.
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
---
arch/powerpc/include/asm/kup.h | 4 ++++
arch/powerpc/include/asm/mmu-8xx.h | 6 +++++
arch/powerpc/include/asm/nohash/32/kup-8xx.h | 34 ++++++++++++++++++++++++++++
arch/powerpc/mm/8xx_mmu.c | 12 ++++++++++
arch/powerpc/platforms/Kconfig.cputype | 1 +
5 files changed, 57 insertions(+)
create mode 100644 arch/powerpc/include/asm/nohash/32/kup-8xx.h
diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h
index 2ac540fb488f..f7262f4c427e 100644
--- a/arch/powerpc/include/asm/kup.h
+++ b/arch/powerpc/include/asm/kup.h
@@ -2,6 +2,10 @@
#ifndef _ASM_POWERPC_KUP_H_
#define _ASM_POWERPC_KUP_H_
+#ifdef CONFIG_PPC_8xx
+#include <asm/nohash/32/kup-8xx.h>
+#endif
+
#ifndef __ASSEMBLY__
#include <asm/pgtable.h>
diff --git a/arch/powerpc/include/asm/mmu-8xx.h b/arch/powerpc/include/asm/mmu-8xx.h
index 53dbf0788fce..01a0a1694ebd 100644
--- a/arch/powerpc/include/asm/mmu-8xx.h
+++ b/arch/powerpc/include/asm/mmu-8xx.h
@@ -120,6 +120,12 @@
*/
#define MD_APG_INIT 0x44444444
+/*
+ * 0 => No user => 01 (all accesses performed according to page definition)
+ * 1 => User => 10 (all accesses performed according to swaped page definition)
+ */
+#define MD_APG_KUAP 0x66666666
+
/* The effective page number register. When read, contains the information
* about the last instruction TLB miss. When MD_RPN is written, bits in
* this register are used to create the TLB entry.
diff --git a/arch/powerpc/include/asm/nohash/32/kup-8xx.h b/arch/powerpc/include/asm/nohash/32/kup-8xx.h
new file mode 100644
index 000000000000..8f4975c0de22
--- /dev/null
+++ b/arch/powerpc/include/asm/nohash/32/kup-8xx.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_KUP_8XX_H_
+#define _ASM_POWERPC_KUP_8XX_H_
+
+#ifdef CONFIG_PPC_KUAP
+#define LOCK_USER_ACCESS(val, sp, sr, srmax, current) \
+ mfspr val, SPRN_MD_AP; \
+ stw val, _KUAP(sp); \
+ lis val, MD_APG_KUAP@h; \
+ ori val, val, MD_APG_KUAP@l; \
+ mtspr SPRN_MD_AP, val
+
+#define REST_USER_ACCESS(val, sp, sr, srmax, current) \
+ lwz val, _KUAP(sp); \
+ mtspr SPRN_MD_AP, val
+
+#define KUAP_START MD_APG_KUAP
+
+#ifndef __ASSEMBLY__
+static inline void lock_user_access(void __user *to, const void __user *from,
+ unsigned long size)
+{
+ mtspr(SPRN_MD_AP, MD_APG_KUAP);
+}
+
+static inline void unlock_user_access(void __user *to, const void __user *from,
+ unsigned long size)
+{
+ mtspr(SPRN_MD_AP, MD_APG_INIT);
+}
+#endif /* !__ASSEMBLY__ */
+#endif /* CONFIG_PPC_KUAP */
+
+#endif /* _ASM_POWERPC_KUP_8XX_H_ */
diff --git a/arch/powerpc/mm/8xx_mmu.c b/arch/powerpc/mm/8xx_mmu.c
index f14ceb507d98..2bba4fd2eed7 100644
--- a/arch/powerpc/mm/8xx_mmu.c
+++ b/arch/powerpc/mm/8xx_mmu.c
@@ -206,3 +206,15 @@ void __init setup_kuep(bool disabled)
mtspr(SPRN_MI_AP, MI_APG_KUEP);
}
#endif
+
+#ifdef CONFIG_PPC_KUAP
+void __init setup_kuap(bool disabled)
+{
+ pr_info("Activating Kernel Userspace Access Protection\n");
+
+ if (disabled)
+ pr_warn("KUAP cannot be disabled yet on 8xx when compiled in\n");
+
+ mtspr(SPRN_MD_AP, MD_APG_KUAP);
+}
+#endif
diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype
index d1757cedf60b..a20669a9ec13 100644
--- a/arch/powerpc/platforms/Kconfig.cputype
+++ b/arch/powerpc/platforms/Kconfig.cputype
@@ -34,6 +34,7 @@ config PPC_8xx
select FSL_SOC
select SYS_SUPPORTS_HUGETLBFS
select PPC_HAVE_KUEP
+ select PPC_HAVE_KUAP
config 40x
bool "AMCC 40x"
--
2.13.3
next prev parent reply other threads:[~2018-11-28 9:41 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-28 9:27 [PATCH v2 01/11] powerpc/mm: Fix reporting of kernel execute faults on the 8xx Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 02/11] powerpc: Add framework for Kernel Userspace Protection Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 03/11] powerpc: Add skeleton for Kernel Userspace Execution Prevention Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 04/11] powerpc/mm: Add a framework for Kernel Userspace Access Protection Christophe Leroy
2018-12-21 5:07 ` Michael Ellerman
2018-12-21 6:48 ` Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 05/11] powerpc/8xx: Add Kernel Userspace Execution Prevention Christophe Leroy
2018-11-28 9:27 ` Christophe Leroy [this message]
2018-11-28 9:27 ` [RFC PATCH v2 07/11] powerpc/mm/radix: Use KUEP API for Radix MMU Russell Currey
2018-11-28 9:43 ` Christophe Leroy
2018-11-28 9:46 ` Christophe LEROY
2018-11-28 9:27 ` [RFC PATCH v2 08/11] powerpc/64s: Implement KUAP " Russell Currey
2018-11-28 9:43 ` Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 09/11] powerpc/32: add helper to write into segment registers Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 10/11] powerpc/book3s32: Prepare Kernel Userspace Access Protection Christophe Leroy
2018-11-28 9:27 ` [RFC PATCH v2 11/11] powerpc/book3s32: Implement " Christophe Leroy
2018-12-11 5:25 ` Russell Currey
2018-12-11 20:46 ` Christophe Leroy
2018-12-23 13:27 ` [v2, 01/11] powerpc/mm: Fix reporting of kernel execute faults on the 8xx Michael Ellerman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9a7dc3917c7c865c0eef66a671439afe92165bba.1543356926.git.christophe.leroy@c-s.fr \
--to=christophe.leroy@c-s.fr \
--cc=benh@kernel.crashing.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mpe@ellerman.id.au \
--cc=paulus@samba.org \
--cc=ruscur@russell.cc \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).