mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: "Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
	"Eric Biederman" <ebiederm@xmission.com>,
	"David Woodhouse" <dwmw@amazon.co.uk>,
	"Sourabh Jain" <sourabhjain@linux.ibm.com>,
	"Hari Bathini" <hbathini@linux.ibm.com>,
	"Michael Ellerman" <mpe@ellerman.id.au>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Baoquan He" <bhe@redhat.com>, "Yuntao Wang" <ytcoode@gmail.com>,
	"David Kaplan" <david.kaplan@amd.com>,
	"Tao Liu" <ltao@redhat.com>,
	"Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>,
	"Kai Huang" <kai.huang@intel.com>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Josh Poimboeuf" <jpoimboe@kernel.org>,
	"Breno Leitao" <leitao@debian.org>,
	"Wei Yang" <richard.weiyang@gmail.com>,
	"Rong Xu" <xur@google.com>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	linux-kernel@vger.kernel.org, kexec@lists.infradead.org,
	"Simon Horman" <horms@kernel.org>,
	"Dave Young" <dyoung@redhat.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	bsz@amazon.de, nathan@kernel.org
Subject: [PATCH v2 4/9] x86/kexec: Fix stack and handling of re-entry point for ::preserve_context
Date: Thu,  9 Jan 2025 14:04:16 +0000	[thread overview]
Message-ID: <20250109140757.2841269-5-dwmw2@infradead.org> (raw)
In-Reply-To: <20250109140757.2841269-1-dwmw2@infradead.org>

From: David Woodhouse <dwmw@amazon.co.uk>

A ::preserve_context kimage can be invoked more than once, and the entry
point can be different every time. When the callee returns to the kernel,
it leaves the address of its entry point for next time on the stack.

That being the case, one might reasonably assume that the caller would
allocate space for it on the stack fram before actually performing the
'call' into the callee.

Apparently not, though. Ever since the kjump code was first added in
2009, it has set up a *new* stack at the top of the swap_page scratch
page, then just performed the 'call' without allocating any space for
the re-entry address to be returned. It then reads the re-entry point
for next time from 0(%rsp) which is actually the first qword of the page
*after* the swap page, which might not exist at all! And if the callee
has written to that, then it will have corrupted memory it doesn't own.

Correct this by pushing the entry point of the callee onto the stack
before calling it. The callee may then adjust it, or not, as it sees fit,
and subsequent invocations should work correctly either way.

Remove a stray push of zero to the *relocate_kernel* stack, which may
have been intended for this purpose, but which was actually just noise.

Also, loading the stack for the callee relied on the address of the swap
page being in %r10 without ever documenting that fact. Recent code
changes made that no longer true, so load it directly from the local
kexec_pa_swap_page variable instead.

Fixes: b3adabae8a96 ("x86/kexec: Drop page_list argument from relocate_kernel()")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 arch/x86/kernel/relocate_kernel_64.S | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocate_kernel_64.S
index 3ca3bf6b3f49..a95691b42c5c 100644
--- a/arch/x86/kernel/relocate_kernel_64.S
+++ b/arch/x86/kernel/relocate_kernel_64.S
@@ -113,8 +113,6 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
 	 * %r13 original CR4 when relocate_kernel() was invoked
 	 */
 
-	/* set return address to 0 if not preserving context */
-	pushq	$0
 	/* store the start address on the stack */
 	pushq   %rdx
 
@@ -208,12 +206,19 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
 
 .Lrelocate:
 	popq	%rdx
+
+	/* Use the swap page for the callee's stack */
+	movq	kexec_pa_swap_page(%rip), %r10
 	leaq	PAGE_SIZE(%r10), %rsp
+
+	/* push the existing entry point onto the callee's stack */
+	pushq	%rdx
+
 	ANNOTATE_RETPOLINE_SAFE
 	call	*%rdx
 
 	/* get the re-entry point of the peer system */
-	movq	0(%rsp), %rbp
+	popq	%rbp
 	leaq	relocate_kernel(%rip), %r8
 	movq	kexec_pa_swap_page(%rip), %r10
 	movq	pa_backup_pages_map(%rip), %rdi
@@ -247,6 +252,7 @@ SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
 	lgdt    saved_context_gdt_desc(%rax)
 #endif
 
+	/* relocate_kernel() returns the re-entry point for next time */
 	movq	%rbp, %rax
 
 	popf
-- 
2.47.0


  parent reply	other threads:[~2025-01-09 14:08 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-09 14:04 [PATCH v2 0/9] x86/kexec: Fixes for tip/x86/boot David Woodhouse
2025-01-09 14:04 ` [PATCH v2 1/9] x86/kexec: Disable global pages before writing to control page David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-09 14:04 ` [PATCH v2 2/9] x86/kexec: Ensure preserve_context flag is set on return to kernel David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-09 14:04 ` [PATCH v2 3/9] x86/kexec: Use correct swap page in swap_pages function David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-09 14:04 ` David Woodhouse [this message]
2025-01-15  9:17   ` [tip: x86/boot] x86/kexec: Fix stack and handling of re-entry point for ::preserve_context tip-bot2 for David Woodhouse
2025-01-09 14:04 ` [PATCH v2 5/9] x86/kexec: Fix location of relocate_kernel with -ffunction-sections David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for Nathan Chancellor
2025-01-09 14:04 ` [PATCH v2 6/9] x86/kexec: Mark machine_kexec() with __nocfi David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-09 14:04 ` [PATCH v2 7/9] kexec_core: Add and update comments regarding the KEXEC_JUMP flow David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for Rafael J. Wysocki
2025-01-09 14:04 ` [PATCH v2 8/9] x86/kexec: Cope with relocate_kernel() not being at the start of the page David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-09 14:04 ` [PATCH v2 9/9] x86/kexec: Use typedef for relocate_kernel_fn function prototype David Woodhouse
2025-01-15  9:17   ` [tip: x86/boot] " tip-bot2 for David Woodhouse
2025-01-14 11:34 ` [PATCH v2 0/9] x86/kexec: Fixes for tip/x86/boot David Woodhouse
2025-01-14 11:51   ` Borislav Petkov
2025-01-14 11:53     ` David Woodhouse
2025-01-14 12:11       ` Borislav Petkov

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=20250109140757.2841269-5-dwmw2@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=ardb@kernel.org \
    --cc=bhe@redhat.com \
    --cc=bp@alien8.de \
    --cc=bsz@amazon.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david.kaplan@amd.com \
    --cc=dwmw@amazon.co.uk \
    --cc=dyoung@redhat.com \
    --cc=ebiederm@xmission.com \
    --cc=hbathini@linux.ibm.com \
    --cc=horms@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@kernel.org \
    --cc=kai.huang@intel.com \
    --cc=kexec@lists.infradead.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ltao@redhat.com \
    --cc=mingo@redhat.com \
    --cc=mpe@ellerman.id.au \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=richard.weiyang@gmail.com \
    --cc=sourabhjain@linux.ibm.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=tzimmermann@suse.de \
    --cc=x86@kernel.org \
    --cc=xur@google.com \
    --cc=ytcoode@gmail.com \
    /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

all inboxes | Powered by JetHome®