Skip to contentSkip to conversation

arm64: support HAVE_ARCH_RARE_WRITE

RFC Superseded Hoeun Ryu 3 participants 5 messages Mar 2, 2017 – Mar 4, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hoeun Ryu

author
#0 lore raw

RFC 1/1 arm64: support HAVE_ARCH_RARE_WRITE

This RFC is a quick and dirty arm64 implementation for Kees Cook's RFC for
rare_write infrastructure [1].

This implementation is based on Mark Rutland's suggestions, which is that
a special userspace mm that maps only __start/end_rodata as RW permission is prepared during early boot time (paging_init) and __arch_rare_write_map() switches to the mm [2].

Due to the limit of implementation (the mm having RW mapping is userspace
mm), we need a new arch-specific __arch_rare_write_ptr() to convert RO address to RW address (CONFIG_HAVE_RARE_WRITE_PTR is added), which is general for all architectures (__rare_write_ptr()) in Kees's RFC . So all writes should be instrumented by __rare_write().

One caveat for arm64 is CONFIG_ARM64_SW_TTBR0_PAN.
Because __arch_rare_write_map() installes a special user mm to ttbr0, usercopy inside __arch_rare_write_map/unmap() pair will break rare_write. (uaccess_enable() replaces the special mm and RW alias is no longer valid.)

A similar problem could rise in general usercopy inside
__arch_rare_write_map/unmap(). __arch_rare_write_map() replaces current->mm, so we loose the address space of the current process.

It passes LKDTM's rare write test.

[1] : http://www.openwall.com/lists/kernel-hardening/2017/02/27/5
[2] : https://lkml.org/lkml/2017/2/22/254

---
 arch/Kconfig                     |  4 ++
 arch/arm64/Kconfig               |  2 +
 arch/arm64/include/asm/pgtable.h | 12 ++++++
 arch/arm64/mm/mmu.c              | 90 ++++++++++++++++++++++++++++++++++++++++
 include/linux/compiler.h         |  6 ++-
 5 files changed, 113 insertions(+), 1 deletion(-)
arch/Kconfig +4 -0
index b1bae4c..0d7b82d 100644
@@ -902,4 +902,8 @@ config HAVE_ARCH_RARE_WRITE
 	  - the routines must validate expected state (e.g. when enabling
 	    writes, BUG() if writes are already be enabled).
 
+config HAVE_ARCH_RARE_WRITE_PTR
+	def_bool n
+	help
+
 source "kernel/gcov/Kconfig"
arch/arm64/Kconfig +2 -0
index 896eba6..e6845ca 100644
@@ -70,6 +70,8 @@ config ARM64
 	select HAVE_ARCH_SECCOMP_FILTER
 	select HAVE_ARCH_TRACEHOOK
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
+	select HAVE_ARCH_RARE_WRITE
+	select HAVE_ARCH_RARE_WRITE_PTR
 	select HAVE_ARM_SMCCC
 	select HAVE_EBPF_JIT
 	select HAVE_C_RECORDMCOUNT
arch/arm64/include/asm/pgtable.h +12 -0
index 0eef606..0d4974d 100644
@@ -731,6 +731,18 @@ static inline void update_mmu_cache(struct vm_area_struct *vma,
 #define kc_vaddr_to_offset(v)	((v) & ~VA_START)
 #define kc_offset_to_vaddr(o)	((o) | VA_START)
 
+extern unsigned long __rare_write_rw_alias_start;
+
+#define __arch_rare_write_ptr(__var) ({				\
+	unsigned long __addr = (unsigned long)&__var;		\
+	__addr -= (unsigned long)__start_rodata;		\
+	__addr += __rare_write_rw_alias_start;			\
+	(typeof(__var) *)__addr;				\
+})
+
+unsigned long __arch_rare_write_map(void);
+unsigned long __arch_rare_write_unmap(void);
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __ASM_PGTABLE_H */
arch/arm64/mm/mmu.c +90 -0

102 lines not on this page · raw message

include/linux/compiler.h +5 -1
index c8c684c..a610ef2 100644
@@ -355,7 +355,11 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
 # define __wr_rare		__ro_after_init
 # define __wr_rare_type		const
 # define __rare_write_type(v)	typeof((typeof(v))0)
-# define __rare_write_ptr(v)	((__rare_write_type(v) *)&(v))
+# ifndef CONFIG_HAVE_ARCH_RARE_WRITE_PTR
+#  define __rare_write_ptr(v)	((__rare_write_type(v) *)&(v))
+# else
+#  define __rare_write_ptr(v)	__arch_rare_write_ptr(v)
+# endif
 # define __rare_write(__var, __val) ({			\
 	__rare_write_type(__var) *__rw_var;		\
 							\
Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Hoeun Ryu

This RFC is a quick and dirty arm64 implementation for Kees Cook's RFC for
rare_write infrastructure [1].

Awesome! :)

Quoted 10 lines

This implementation is based on Mark Rutland's suggestions, which is that
a special userspace mm that maps only __start/end_rodata as RW permission is prepared during early boot time (paging_init) and __arch_rare_write_map() switches to the mm [2].

Due to the limit of implementation (the mm having RW mapping is userspace
mm), we need a new arch-specific __arch_rare_write_ptr() to convert RO address to RW address (CONFIG_HAVE_RARE_WRITE_PTR is added), which is general for all architectures (__rare_write_ptr()) in Kees's RFC . So all writes should be instrumented by __rare_write().

Cool, yeah, I'll get all this fixed up in my next version.

Quoted 4 lines

One caveat for arm64 is CONFIG_ARM64_SW_TTBR0_PAN.
Because __arch_rare_write_map() installes a special user mm to ttbr0, usercopy inside __arch_rare_write_map/unmap() pair will break rare_write. (uaccess_enable() replaces the special mm and RW alias is no longer valid.)

That's totally fine constraint: this case should never happen for so many reasons. :)

Quoted 10 lines

A similar problem could rise in general usercopy inside
__arch_rare_write_map/unmap(). __arch_rare_write_map() replaces current->mm, so we loose the address space of the current process.

It passes LKDTM's rare write test.

[1] : http://www.openwall.com/lists/kernel-hardening/2017/02/27/5
[2] : https://lkml.org/lkml/2017/2/22/254

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
author
#2 lore raw
Quoted 14 lines · Kees Cook
Quoted 4 lines · Hoeun Ryu

This RFC is a quick and dirty arm64 implementation for Kees Cook's RFC for rare_write infrastructure [1].

Awesome! :)

Quoted 5 lines

This implementation is based on Mark Rutland's suggestions, which is that a special userspace mm that maps only __start/end_rodata as RW permission is prepared during early boot time (paging_init) and __arch_rare_write_map() switches to the mm [2].

Quoted 7 lines
Quoted 5 lines

Due to the limit of implementation (the mm having RW mapping is userspace mm), we need a new arch-specific __arch_rare_write_ptr() to convert RO address to RW address (CONFIG_HAVE_RARE_WRITE_PTR is added), which is general for all architectures (__rare_write_ptr()) in Kees's RFC . So all writes should be instrumented by __rare_write().

Cool, yeah, I'll get all this fixed up in my next version.

I'll send the next version of this when you send yours.

Quoted 8 lines
Quoted 4 lines

One caveat for arm64 is CONFIG_ARM64_SW_TTBR0_PAN.
Because __arch_rare_write_map() installes a special user mm to ttbr0, usercopy inside __arch_rare_write_map/unmap() pair will break rare_write. (uaccess_enable() replaces the special mm and RW alias is no longer valid.)

That's totally fine constraint: this case should never happen for so many reasons. :)

OK. Thank you for the review.

Quoted 16 lines
Quoted 10 lines

A similar problem could rise in general usercopy inside __arch_rare_write_map/unmap(). __arch_rare_write_map() replaces current->mm, so we loose the address space of the current process.

It passes LKDTM's rare write test.

[1] : http://www.openwall.com/lists/kernel-hardening/2017/02/27/5
[2] : https://lkml.org/lkml/2017/2/22/254

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#3 lore raw
In reply to Hoeun Ryu
Quoted 9 lines · Hoeun Ryu
arch/arm64/mm/mmu.c +9 -0
+unsigned long __rare_write_rw_alias_start = TASK_SIZE_64 / 4;
+
+__always_inline unsigned long __arch_rare_write_map(void)
+{
+       struct mm_struct *mm = &rare_write_mm;
+
+       preempt_disable();
+
+       __switch_mm(mm);

...

arch/arm64/mm/mmu.c static void __init map_kernel(pgd_t *pgd) · line 560

Quoted 6 lines
arch/arm64/mm/mmu.c +6 -0
+__always_inline unsigned long __arch_rare_write_unmap(void)
+{
+       struct mm_struct *mm = current->active_mm;
+
+       __switch_mm(mm);
+

This reminds me: this code imposes constraints on the context in which it's called. I'd advise making it very explicit, asserting correctness, and putting the onus on the caller to set things up. For example:

DEBUG_LOCKS_WARN_ON(preemptible() || in_interrupt() || in_nmi());

in both the map and unmap functions, along with getting rid of the preempt_disable(). I don't think we want the preempt-disabledness to depend on the arch. The generic non-arch rare_write helpers can do the preempt_disable().

This code also won't work if the mm is wacky when called. On x86, we could do:

DEBUG_LOCKS_WARN_ON(read_cr3() != current->active_mm->pgd);

or similar (since that surely doesn't compile as is).

--Andy

author
#4 lore raw
Quoted 28 lines · Andy Lutomirski
Quoted 11 lines · Hoeun Ryu
arch/arm64/mm/mmu.c +9 -0
+unsigned long __rare_write_rw_alias_start = TASK_SIZE_64 / 4;
+
+__always_inline unsigned long __arch_rare_write_map(void)
+{
+       struct mm_struct *mm = &rare_write_mm;
+
+       preempt_disable();
+
+       __switch_mm(mm);

...

arch/arm64/mm/mmu.c static void __init map_kernel(pgd_t *pgd) · line 560

Quoted 6 lines
arch/arm64/mm/mmu.c +6 -0
+__always_inline unsigned long __arch_rare_write_unmap(void)
+{
+       struct mm_struct *mm = current->active_mm;
+
+       __switch_mm(mm);
+

This reminds me: this code imposes constraints on the context in which it's called. I'd advise making it very explicit, asserting correctness, and putting the onus on the caller to set things up. For example:

Quoted 2 lines

DEBUG_LOCKS_WARN_ON(preemptible() || in_interrupt() || in_nmi());

OK. I will add some onus in the next version.

Quoted 5 lines

in both the map and unmap functions, along with getting rid of the preempt_disable(). I don't think we want the preempt-disabledness to depend on the arch. The generic non-arch rare_write helpers can do the preempt_disable().

I think I can fix this in the next version when Kees send the next version of RFC.

Quoted 7 lines

This code also won't work if the mm is wacky when called. On x86, we could do:

DEBUG_LOCKS_WARN_ON(read_cr3() != current->active_mm->pgd);

or similar (since that surely doesn't compile as is).

--Andy

Thank you for the review.