All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb@kernel.org>
To: linux-arm-kernel@lists.infradead.org
Cc: Ard Biesheuvel <ardb@kernel.org>, Marc Zyngier <maz@kernel.org>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Kees Cook <keescook@chromium.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Mark Brown <broonie@kernel.org>,
	Anshuman Khandual <anshuman.khandual@arm.com>
Subject: [PATCH v7 22/33] arm64: head: move dynamic shadow call stack patching into early C runtime
Date: Fri, 11 Nov 2022 18:11:50 +0100	[thread overview]
Message-ID: <20221111171201.2088501-23-ardb@kernel.org> (raw)
In-Reply-To: <20221111171201.2088501-1-ardb@kernel.org>

Once we update the early kernel mapping code to only map the kernel once
with the right permissions, we can no longer perform code patching via
this mapping.

So move this code to an earlier stage of the boot, right after applying
the relocations.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/arm64/include/asm/scs.h           |  2 +-
 arch/arm64/kernel/Makefile             |  2 --
 arch/arm64/kernel/head.S               |  8 ++++---
 arch/arm64/kernel/module.c             |  2 +-
 arch/arm64/kernel/pi/Makefile          | 10 ++++----
 arch/arm64/kernel/{ => pi}/patch-scs.c | 24 ++++++++++----------
 6 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/arch/arm64/include/asm/scs.h b/arch/arm64/include/asm/scs.h
index ff7da1268a52ab79..d20dcb2a1a5284ce 100644
--- a/arch/arm64/include/asm/scs.h
+++ b/arch/arm64/include/asm/scs.h
@@ -71,7 +71,7 @@ static inline void dynamic_scs_init(void)
 static inline void dynamic_scs_init(void) {}
 #endif
 
-int scs_patch(const u8 eh_frame[], int size);
+int __pi_scs_patch(const u8 eh_frame[], int size);
 
 #endif /* __ASSEMBLY __ */
 
diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile
index 2b003834c320c20e..35354365f5f39f3f 100644
--- a/arch/arm64/kernel/Makefile
+++ b/arch/arm64/kernel/Makefile
@@ -79,8 +79,6 @@ obj-$(CONFIG_ARM64_PTR_AUTH)		+= pointer_auth.o
 obj-$(CONFIG_ARM64_MTE)			+= mte.o
 obj-y					+= vdso-wrap.o
 obj-$(CONFIG_COMPAT_VDSO)		+= vdso32-wrap.o
-obj-$(CONFIG_UNWIND_PATCH_PAC_INTO_SCS)	+= patch-scs.o
-CFLAGS_patch-scs.o			+= -mbranch-protection=none
 
 # Force dependency (vdso*-wrap.S includes vdso.so through incbin)
 $(obj)/vdso-wrap.o: $(obj)/vdso/vdso.so
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index 9ea7f4e355ef5849..5f1476c0f3a33d75 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -438,9 +438,6 @@ SYM_FUNC_START_LOCAL(__primary_switched)
 #endif
 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
 	bl	kasan_early_init
-#endif
-#ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS
-	bl	scs_patch_vmlinux
 #endif
 	mov	x0, x20
 	bl	finalise_el2			// Prefer VHE if possible
@@ -720,6 +717,11 @@ SYM_FUNC_START_LOCAL(__primary_switch)
 #ifdef CONFIG_RELOCATABLE
 	mov	x0, x23
 	bl	__pi_relocate_kernel
+#endif
+#ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS
+	ldr	x0, =__eh_frame_start
+	ldr	x1, =__eh_frame_end
+	bl	__pi_scs_patch_vmlinux
 #endif
 	ldr	x8, =__primary_switched
 	adrp	x0, KERNEL_START		// __pa(KERNEL_START)
diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c
index fa7b3228944b325e..ea505022737d531d 100644
--- a/arch/arm64/kernel/module.c
+++ b/arch/arm64/kernel/module.c
@@ -519,7 +519,7 @@ int module_finalize(const Elf_Ehdr *hdr,
 	if (scs_is_dynamic()) {
 		s = find_section(hdr, sechdrs, ".init.eh_frame");
 		if (s)
-			scs_patch((void *)s->sh_addr, s->sh_size);
+			__pi_scs_patch((void *)s->sh_addr, s->sh_size);
 	}
 
 	return module_init_ftrace_plt(hdr, sechdrs, me);
diff --git a/arch/arm64/kernel/pi/Makefile b/arch/arm64/kernel/pi/Makefile
index 47d3ffcff3ac9a03..293a04a5dc3ef516 100644
--- a/arch/arm64/kernel/pi/Makefile
+++ b/arch/arm64/kernel/pi/Makefile
@@ -38,7 +38,9 @@ $(obj)/lib-%.pi.o: OBJCOPYFLAGS += --prefix-alloc-sections=.init
 $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-obj-y				:= idreg-override.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
-obj-$(CONFIG_RELOCATABLE)	+= relocate.pi.o
-obj-$(CONFIG_RANDOMIZE_BASE)	+= kaslr_early.pi.o
-extra-y				:= $(patsubst %.pi.o,%.o,$(obj-y))
+obj-y					:= idreg-override.pi.o \
+					   lib-fdt.pi.o lib-fdt_ro.pi.o
+obj-$(CONFIG_RELOCATABLE)		+= relocate.pi.o
+obj-$(CONFIG_RANDOMIZE_BASE)		+= kaslr_early.pi.o
+obj-$(CONFIG_UNWIND_PATCH_PAC_INTO_SCS)	+= patch-scs.pi.o
+extra-y					:= $(patsubst %.pi.o,%.o,$(obj-y))
diff --git a/arch/arm64/kernel/patch-scs.c b/arch/arm64/kernel/pi/patch-scs.c
similarity index 91%
rename from arch/arm64/kernel/patch-scs.c
rename to arch/arm64/kernel/pi/patch-scs.c
index 1b3da02d5b741bc3..d15833df10d3d4c6 100644
--- a/arch/arm64/kernel/patch-scs.c
+++ b/arch/arm64/kernel/pi/patch-scs.c
@@ -4,14 +4,11 @@
  * Author: Ard Biesheuvel <ardb@google.com>
  */
 
-#include <linux/bug.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/linkage.h>
-#include <linux/printk.h>
 #include <linux/types.h>
 
-#include <asm/cacheflush.h>
 #include <asm/scs.h>
 
 //
@@ -81,7 +78,11 @@ static void __always_inline scs_patch_loc(u64 loc)
 		 */
 		return;
 	}
-	dcache_clean_pou(loc, loc + sizeof(u32));
+	if (IS_ENABLED(CONFIG_ARM64_WORKAROUND_CLEAN_CACHE))
+		asm("dc civac, %0" :: "r"(loc));
+	else
+		asm(ALTERNATIVE("dc cvau, %0", "nop", ARM64_HAS_CACHE_IDC)
+		    :: "r"(loc));
 }
 
 /*
@@ -128,9 +129,9 @@ struct eh_frame {
 	};
 };
 
-static int noinstr scs_handle_fde_frame(const struct eh_frame *frame,
-					bool fde_has_augmentation_data,
-					int code_alignment_factor)
+static int scs_handle_fde_frame(const struct eh_frame *frame,
+				bool fde_has_augmentation_data,
+				int code_alignment_factor)
 {
 	int size = frame->size - offsetof(struct eh_frame, opcodes) + 4;
 	u64 loc = (u64)offset_to_ptr(&frame->initial_loc);
@@ -196,14 +197,13 @@ static int noinstr scs_handle_fde_frame(const struct eh_frame *frame,
 			break;
 
 		default:
-			pr_err("unhandled opcode: %02x in FDE frame %lx\n", opcode[-1], (uintptr_t)frame);
 			return -ENOEXEC;
 		}
 	}
 	return 0;
 }
 
-int noinstr scs_patch(const u8 eh_frame[], int size)
+int scs_patch(const u8 eh_frame[], int size)
 {
 	const u8 *p = eh_frame;
 
@@ -246,12 +246,12 @@ int noinstr scs_patch(const u8 eh_frame[], int size)
 	return 0;
 }
 
-asmlinkage void __init scs_patch_vmlinux(void)
+asmlinkage void __init scs_patch_vmlinux(const u8 start[], const u8 end[])
 {
 	if (!should_patch_pac_into_scs())
 		return;
 
-	WARN_ON(scs_patch(__eh_frame_start, __eh_frame_end - __eh_frame_start));
-	icache_inval_all_pou();
+	scs_patch(start, end - start);
+	asm("ic ialluis");
 	isb();
 }
-- 
2.35.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2022-11-11 17:29 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-11 17:11 [PATCH v7 00/33] arm64: robustify boot sequence and add support for WXN Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 01/33] arm64: mm: Avoid SWAPPER_BLOCK_xxx constants in FDT fixmap logic Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 02/33] arm64: mm: Avoid swapper block size when choosing vmemmap granularity Ard Biesheuvel
2022-11-24  5:11   ` Anshuman Khandual
2022-11-11 17:11 ` [PATCH v7 03/33] arm64: kaslr: don't pretend KASLR is enabled if offset < MIN_KIMG_ALIGN Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 04/33] arm64: kaslr: drop special case for ThunderX in kaslr_requires_kpti() Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 05/33] arm64: kernel: Disable latent_entropy GCC plugin in early C runtime Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 06/33] arm64: kernel: Add relocation check to code built under pi/ Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 07/33] arm64: kernel: Don't rely on objcopy to make code under pi/ __init Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 08/33] arm64: head: move relocation handling to C code Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 09/33] arm64: Turn kaslr_feature_override into a generic SW feature override Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 10/33] arm64: idreg-override: Omit non-NULL checks for override pointer Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 11/33] arm64: idreg-override: Use relative references to override variables Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 12/33] arm64: idreg-override: Use relative references to filter routines Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 13/33] arm64: idreg-override: Avoid parameq() and parameqn() Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 14/33] arm64: idreg-override: avoid strlen() to check for empty strings Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 15/33] arm64: idreg-override: Avoid sprintf() for simple string concatenation Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 16/33] arm64: idreg_override: Avoid kstrtou64() to parse a single hex digit Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 17/33] arm64: idreg-override: Move to early mini C runtime Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 18/33] arm64: kernel: Remove early fdt remap code Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 19/33] arm64: head: Clear BSS and the kernel page tables in one go Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 20/33] arm64: Move feature overrides into the BSS section Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 21/33] arm64: head: Run feature override detection before mapping the kernel Ard Biesheuvel
2022-11-11 17:11 ` Ard Biesheuvel [this message]
2022-11-11 17:11 ` [PATCH v7 23/33] arm64: kaslr: Use feature override instead of parsing the cmdline again Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 24/33] arm64: idreg-override: Create a pseudo feature for rodata=off Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 25/33] arm64: head: allocate more pages for the kernel mapping Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 26/33] arm64: head: move memstart_offset_seed handling to C code Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 27/33] arm64: head: Move early kernel mapping routines into " Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 28/33] arm64: mm: avoid fixmap for early swapper_pg_dir updates Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 29/33] arm64: mm: omit redundant remap of kernel image Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 30/33] arm64: Revert "mm: provide idmap pointer to cpu_replace_ttbr1()" Ard Biesheuvel
2022-11-11 17:11 ` [PATCH v7 31/33] arm64: mmu: Retire SWAPPER_BLOCK_xxx and related constants Ard Biesheuvel
2022-11-11 17:12 ` [PATCH v7 32/33] mm: add arch hook to validate mmap() prot flags Ard Biesheuvel
2022-11-11 17:12 ` [PATCH v7 33/33] arm64: mm: add support for WXN memory translation attribute Ard Biesheuvel

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=20221111171201.2088501-23-ardb@kernel.org \
    --to=ardb@kernel.org \
    --cc=anshuman.khandual@arm.com \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=keescook@chromium.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=will@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.