lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Tue, 1 Aug 2023 12:30:08 +0200
From:   Borislav Petkov <bp@...en8.de>
To:     Ard Biesheuvel <ardb@...nel.org>
Cc:     linux-efi@...r.kernel.org, linux-kernel@...r.kernel.org,
        Evgeniy Baskov <baskov@...ras.ru>,
        Andy Lutomirski <luto@...nel.org>,
        Dave Hansen <dave.hansen@...ux.intel.com>,
        Ingo Molnar <mingo@...hat.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Thomas Gleixner <tglx@...utronix.de>,
        Alexey Khoroshilov <khoroshilov@...ras.ru>,
        Peter Jones <pjones@...hat.com>,
        Gerd Hoffmann <kraxel@...hat.com>,
        Dave Young <dyoung@...hat.com>,
        Mario Limonciello <mario.limonciello@....com>,
        Kees Cook <keescook@...omium.org>,
        Tom Lendacky <thomas.lendacky@....com>,
        "Kirill A . Shutemov" <kirill.shutemov@...ux.intel.com>,
        Linus Torvalds <torvalds@...ux-foundation.org>,
        Joerg Roedel <jroedel@...e.de>
Subject: Re: [PATCH v7 11/22] x86/decompressor: Avoid the need for a stack in
 the 32-bit trampoline

On Fri, Jul 28, 2023 at 11:09:05AM +0200, Ard Biesheuvel wrote:
> The 32-bit trampoline no longer uses the stack for anything except
> performing a far return back to long mode. Currently, this stack is
> placed in the same page that carries the trampoline code, which means
> this page must be mapped writable and executable, and the stack is
> therefore executable as well.
> 
> Replace the far return with a far jump, so that the return address can
> be pre-calculated and patched into the code before it is called. This
> removes the need for a stack entirely, and in a later patch, this will
> be taken advantage of by removing writable permissions from (and adding
> executable permissions to) this code page explicitly when booting via
> the EFI stub.
> 
> Not touching the stack pointer also makes it more straight-forward to
> call the trampoline code as an ordinary 64-bit function from C code.
> 
> Note that we need to preserve the value of RSP across the switch into
	    ^^

Passive voice pls.

> compatibility mode: the stack pointer may get truncated to 32 bits.
> 
> Acked-by: Kirill A. Shutemov <kirill.shutemov@...ux.intel.com>
> Signed-off-by: Ard Biesheuvel <ardb@...nel.org>
> ---
>  arch/x86/boot/compressed/head_64.S    | 64 ++++++++++----------
>  arch/x86/boot/compressed/pgtable.h    |  4 +-
>  arch/x86/boot/compressed/pgtable_64.c | 12 +++-
>  3 files changed, 44 insertions(+), 36 deletions(-)
> 
> diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
> index 491d985be75fd5b0..1b0c61d1b389fd37 100644
> --- a/arch/x86/boot/compressed/head_64.S
> +++ b/arch/x86/boot/compressed/head_64.S
> @@ -449,9 +449,6 @@ SYM_CODE_START(startup_64)
>  	leaq	TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax
>  	call	*%rax
>  
> -	/* Restore the stack, the 32-bit trampoline uses its own stack */
> -	leaq	rva(boot_stack_end)(%rbx), %rsp
> -
>  	/*
>  	 * cleanup_trampoline() would restore trampoline memory.
>  	 *
> @@ -537,32 +534,37 @@ SYM_FUNC_END(.Lrelocated)
>   * EDI contains the base address of the trampoline memory.
>   * Non-zero ESI means trampoline needs to enable 5-level paging.
>   */
> +	.section ".rodata", "a", @progbits
>  SYM_CODE_START(trampoline_32bit_src)
> -	/* Grab return address */
> -	movq	(%rsp), %rax
> -
> -	/* Set up 32-bit addressable stack */
> -	leaq	TRAMPOLINE_32BIT_STACK_END(%rdi), %rsp
> -
> -	/* Preserve return address and other live 64-bit registers */
> -	pushq	%rax
> +	/* Preserve live 64-bit registers */
>  	pushq	%r15
>  	pushq	%rbp
>  	pushq	%rbx
>  
> +	/* Preserve top half of RSP in a legacy mode GPR to avoid truncation */
> +	movq	%rsp, %rbx
> +	shrq	$32, %rbx
> +
>  	/* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */
>  	pushq	$__KERNEL32_CS
>  	leaq	0f(%rip), %rax
>  	pushq	%rax
>  	lretq
>  
> +	/*
> +	 * The 32-bit code below will do a far jump back to long mode and end
> +	 * up here after reconfiguring the number of paging levels.
> +	 */
> +.Lret:	shlq	$32, %rbx		// Reconstruct stack pointer

No side comments pls.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ