From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796324648; b=TXulpYx7uSMHmZNfsQg7EPC3TKO/4ZoY+cev4xNhukhV7Mm6uEea309KJ9rCr/GgOLzu JHB5rdp6kdCRX//rrTYA0DTxzNk+ha3A5Fc86QzyrzVFnqSzAV5eXEJRfLi9hlGwy+63l ay7dIH9MqTuhoEf411Jdgam1M9lDFDY+7nPhn0zwD0bs6jhM79HUODA+YC+Ae0dvv4jjo rAPJI9EzvwrAXsPugooUZY+Kafq//I/HLDhwtCzgUf7L1C2n+alf5mje+zs9AQMUE9Tl5 D+VBpnMiC0QRB9eklHeJnC8NSWRIsUfEJp5SBJCViIpawiZg6krVd5vAlXjpXiW4ATQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796324648; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=rLMCRqQaK2Z5O3OTyM5RAt06wrItgTKnQU5mrEcGnXI=; b=uYnICxwfcuW7+3wDkP0Qe1to3OOuwFrHQdaGKgC5o5UV20qp9pxfrjWgKWgzlu77XtnL 1UCgsdiW1HZYmr1yBgRl3ZqZEFZi3C8k3rHjX0YPlh/vjCY8g8goGRcrx3DeRZeNX5rDo ZO/2OxJiuZjEQzlY8tlAvZuBKfC0cEDaQ9v8Lo384V/kVXRnFnFdG9NB+iajkNSbwXJ8h flNQPgz5xAqPkOjWqU9Dr2oz0+ekYHy0vJL9tpRSbHf2Dw1Hyz5P1ZxD2B/f7hUxuCB8g 0VZJolXMGfXPBnl6pqFhtYhAopsohtLC4buiCHfAuXnURmqbI/BzxtExh3gEyHEEOHw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bm+wohBg; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 913DA606A330 for ; Thu, 3 Dec 2026 19:04:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bm+wohBg Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90CC21F00933; Thu, 3 Dec 2026 19:04:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796324648; bh=rLMCRqQaK2Z5O3OTyM5RAt06wrItgTKnQU5mrEcGnXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bm+wohBg/C3Zu1Bb7uGRXgVIDNspcvoKwDanlw8iiXvsZTvkg02ANMHagTkTZe73W TIwNiMdO9zGZ/RmQErBQkGp2eE1qAXluHD3PuVtV9ufzAV5UTqDmYwwS/cJ+TI+Lw+ 6xlk2OIRgFEcvlum44APfIfSQfVhm1CEchNNvJrz0aHOZrg16XTw4KwOGJcQzXeigv kmUSN5nh+VS3lvMnhpE0FZp0qI6BMgzN6++ISBBkW6ItJ2OVa/Jm1+U/UsHSqNdFn1 qOIHvRj5uzYCMfdubRjLB9i6by5ofQuRDlutwmlTO5L2+EvbaEF0/i+J/kZmxHIA85 XHLWgy/H8a9CQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Ben Cressey , Kees Cook , Catalin Marinas , Sasha Levin Subject: [PATCH 6.1.y 2/2] arm64: kexec: use a typed entry to fix kCFI oops Date: Thu, 3 Dec 2026 14:04:04 -0500 Message-ID: <20261203190353.570026-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261203190353.570026-1-sashal@kernel.org> References: <2026120258-brunt-heaviness-372e@gregkh> <20261203190353.570026-1-sashal@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ben Cressey [ Upstream commit b059e957d07b3cab92eef4656988244043f5769a ] An arm64 Linux kernel built with CONFIG_CFI=y oopses after "Bye!" on every kexec that relocates the image. Crash images load in place and are not affected. The oops reproduces under QEMU: Unable to handle kernel paging request at virtual address 00000000544bfffc pc : machine_kexec+0x104/0x274 Code: d5182008 d5033fdf aa1303e0 f9418a68 (b85fc110) b85fc110 is "ldur w16, [x8, #-4]", the kCFI check's load of the type hash in front of the callee. Commit efc2d0f20a9d ("arm64: kexec: keep MMU enabled during kexec relocation") made machine_kexec() call the control page's copy of arm64_relocate_new_kernel() indirectly, while TTBR0 maps only that page. The copy starts at the page's first byte, so the load faults. SYM_CODE_START() also gives the routine no hash. SYM_TYPED_FUNC_START() emits the hash inside .kexec_relocate.text, and machine_kexec_post_load() already copies that whole section, at most 4 KiB, to the page's start, so the mapped page holds both hash and entry. The ASSERT from commit dc4824faa265 ("arm64: avoid executing padding bytes during kexec / hibernation") kept the entry first, since C branched to the page's start. Relax it, so the entry need only be in the section. Fix by typing the routine with SYM_TYPED_FUNC_START() and a C prototype, and entering the copy at the symbol's offset in the section. The code is Kees Cook's, as posted in review, with two comments and the ASSERT message reworded. An LLM diagnosed the oops. Fixes: efc2d0f20a9d ("arm64: kexec: keep MMU enabled during kexec relocation") Cc: stable@vger.kernel.org Suggested-by: Kees Cook Link: https://lore.kernel.org/linux-arm-kernel/202609241804.BB23BF10@keescook/ Signed-off-by: Ben Cressey Signed-off-by: Catalin Marinas Signed-off-by: Sasha Levin --- arch/arm64/include/asm/kexec.h | 2 ++ arch/arm64/kernel/machine_kexec.c | 7 +++++-- arch/arm64/kernel/relocate_kernel.S | 11 +++++++---- arch/arm64/kernel/vmlinux.lds.S | 5 +++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 559bfae267153..f2596f1e6c3af 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -100,6 +100,8 @@ void cpu_soft_restart(unsigned long el2_switch, unsigned long entry, unsigned long arg0, unsigned long arg1, unsigned long arg2); +void arm64_relocate_new_kernel(struct kimage *kimage); + int machine_kexec_post_load(struct kimage *image); #define machine_kexec_post_load machine_kexec_post_load diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.c index 906bd97eeeb68..19ec86bb8b479 100644 --- a/arch/arm64/kernel/machine_kexec.c +++ b/arch/arm64/kernel/machine_kexec.c @@ -148,9 +148,12 @@ int machine_kexec_post_load(struct kimage *kimage) kimage->arch.ttbr1 = __pa(trans_pgd); kimage->arch.zero_page = __pa_symbol(empty_zero_page); + /* Copy the whole section: a kCFI hash may precede the entry point */ reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start; memcpy(reloc_code, __relocate_new_kernel_start, reloc_size); - kimage->arch.kern_reloc = __pa(reloc_code); + kimage->arch.kern_reloc = __pa(reloc_code) + + ((unsigned long)arm64_relocate_new_kernel - + (unsigned long)__relocate_new_kernel_start); rc = trans_pgd_idmap_page(&info, &kimage->arch.ttbr0, &kimage->arch.t0sz, reloc_code); if (rc) @@ -205,7 +208,7 @@ void machine_kexec(struct kimage *kimage) restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem, 0, 0); } else { - void (*kernel_reloc)(struct kimage *kimage); + typeof(arm64_relocate_new_kernel) *kernel_reloc; if (is_hyp_nvhe()) __hyp_set_vectors(kimage->arch.el2_vectors); diff --git a/arch/arm64/kernel/relocate_kernel.S b/arch/arm64/kernel/relocate_kernel.S index 413f899e4ac63..897d86994e3a0 100644 --- a/arch/arm64/kernel/relocate_kernel.S +++ b/arch/arm64/kernel/relocate_kernel.S @@ -8,6 +8,7 @@ * Pasha Tatashin */ +#include #include #include @@ -32,11 +33,13 @@ * new image to its final location. To assure that the * arm64_relocate_new_kernel routine which does that copy is not overwritten, * all code and data needed by arm64_relocate_new_kernel must be between the - * symbols arm64_relocate_new_kernel and arm64_relocate_new_kernel_end. The - * machine_kexec() routine will copy arm64_relocate_new_kernel to the kexec + * symbols __relocate_new_kernel_start and __relocate_new_kernel_end. The + * machine_kexec_post_load() routine will copy that whole region to the kexec * safe memory that has been set up to be preserved during the copy operation. + * + * Called indirectly by machine_kexec(), this routine needs a kCFI type hash. */ -SYM_CODE_START(arm64_relocate_new_kernel) +SYM_TYPED_FUNC_START(arm64_relocate_new_kernel) /* * The kimage structure isn't allocated specially and may be clobbered * during relocation. We must load any values we need from it prior to @@ -97,4 +100,4 @@ SYM_CODE_START(arm64_relocate_new_kernel) mov x2, xzr mov x3, xzr br x28 /* Jumps from el1 */ -SYM_CODE_END(arm64_relocate_new_kernel) +SYM_FUNC_END(arm64_relocate_new_kernel) diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 1eadea9aa758a..f3fa1ddfe91f8 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -373,6 +373,7 @@ ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET, ASSERT(__relocate_new_kernel_end - __relocate_new_kernel_start <= SZ_4K, "kexec relocation code is bigger than 4 KiB") ASSERT(KEXEC_CONTROL_PAGE_SIZE >= SZ_4K, "KEXEC_CONTROL_PAGE_SIZE is broken") -ASSERT(__relocate_new_kernel_start == arm64_relocate_new_kernel, - "kexec control page does not start with arm64_relocate_new_kernel") +ASSERT(arm64_relocate_new_kernel >= __relocate_new_kernel_start && + arm64_relocate_new_kernel < __relocate_new_kernel_end, + "kexec relocation code does not contain arm64_relocate_new_kernel") #endif -- 2.53.0