Skip to content

Commit 2cacf7f

Browse files
dwmw2bp3tk0v
authored andcommitted
x86/kexec: Fix stack and handling of re-entry point for ::preserve_context
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 frame 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: b3adaba ("x86/kexec: Drop page_list argument from relocate_kernel()") Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 85d724d commit 2cacf7f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

arch/x86/kernel/relocate_kernel_64.S

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
113113
* %r13 original CR4 when relocate_kernel() was invoked
114114
*/
115115

116-
/* set return address to 0 if not preserving context */
117-
pushq $0
118116
/* store the start address on the stack */
119117
pushq %rdx
120118

@@ -208,12 +206,19 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
208206

209207
.Lrelocate:
210208
popq %rdx
209+
210+
/* Use the swap page for the callee's stack */
211+
movq kexec_pa_swap_page(%rip), %r10
211212
leaq PAGE_SIZE(%r10), %rsp
213+
214+
/* push the existing entry point onto the callee's stack */
215+
pushq %rdx
216+
212217
ANNOTATE_RETPOLINE_SAFE
213218
call *%rdx
214219

215220
/* get the re-entry point of the peer system */
216-
movq 0(%rsp), %rbp
221+
popq %rbp
217222
leaq relocate_kernel(%rip), %r8
218223
movq kexec_pa_swap_page(%rip), %r10
219224
movq pa_backup_pages_map(%rip), %rdi
@@ -247,6 +252,7 @@ SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped)
247252
lgdt saved_context_gdt_desc(%rax)
248253
#endif
249254

255+
/* relocate_kernel() returns the re-entry point for next time */
250256
movq %rbp, %rax
251257

252258
popf

0 commit comments

Comments
 (0)