Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp3019380rdb; Tue, 13 Feb 2024 04:44:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWFYanlqo3aYhRg9s4WL37NkyxLlT30bNhpO18ikbiuxp1yOv167US10F5kw9iPLVihkHPlt98gdttNz+CQ8gXqH5Lt6FeQ9cpwBJlQXg== X-Google-Smtp-Source: AGHT+IGyF7lgBDxKpPStEXn/5GpHuCLT+H1bm57zBsj4NU+6Tdcqyn3GySG46nK2iTYKqiCwuTuS X-Received: by 2002:a17:906:6952:b0:a3c:f91d:ee10 with SMTP id c18-20020a170906695200b00a3cf91dee10mr1072879ejs.5.1707828286143; Tue, 13 Feb 2024 04:44:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707828286; cv=pass; d=google.com; s=arc-20160816; b=nSd5f3xRwE++Qhp3Joo09x9oZeEzF9nnyyF3wWCGOo9R51ksL8mfY2WVIxJJzHNYSz IH6/N4/P+ps+GlgD9yQuaudICkLjepPtMtCqt4cTl8tfvmjvzM52JR3QZOW/tv9x8MBC 3Rn9UBF26FwkJlkS86rgr+ED9Xf7wTfdDgeldNpzX7en0cv1hAZKxPyOB367XECmMDjW ZdYRxldYt5tOjiexkN8fyAKuTal3tYLHH57V7cLm6w3rPfZ62Yxixpz14cRETEO0dEbm b/gEd6/YSyt+oYQdR4qoxSkHrfO9VOev4nLFGZ3TPIUQcl/hZyq+IcBjjkRS0qeNwSNJ vE5g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=3FN139qBzPwhjMtuDTCrSTJijK28dahNsbuGrqs5W2w=; fh=hPKxlro8cqb+KXcJ8zUgN6N7/YZNlo4DaF6MVEYfu1U=; b=QA8rM1j/vvWrkINVNdm8vjKyYHWmjmP+syDvTFYRkbZYpxqAnxlNK5bABsCwKMNkzT ZWb2EY3YQrmSP7eCc5I8DMQbjBGFMyNNQsZsrV5d2Z3niCo8ryG1/ZkuTyagH/eZpwjP g26huEe7wDSnA7Qyx6WfnqLK2Hw4atFz3iole7891INFHASxT4NBDPZFaHJ1wQY57yzF sdMdiC3FdpslDO7W83fkqZtGYhoIhBL60z1ea2zbWYk+ZXciFZDZy2D0Et/xlhbCSRO3 aqWfSmZy/vd+JGuX7JvpHHPjiBEKW+yXPrkVGOrcVyT/MEiRxn21mSI8Tx/FesldJRnE pjgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=D9650BW9; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63529-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63529-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCW/O6CMcH6ujKiqVu4sxPS7C0zg7lznvaon+70P5DaRWYfYzCmm0j6tJHXNYC+cLoRKTzTrySdZCd2oey83E6WjilnesJdiT4Q7P/DlJQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j2-20020a170906830200b00a3c31cdc93esi1170512ejx.408.2024.02.13.04.44.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 04:44:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63529-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=D9650BW9; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63529-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63529-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B66D91F25365 for ; Tue, 13 Feb 2024 12:44:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BDA825467C; Tue, 13 Feb 2024 12:42:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="D9650BW9" Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFE9653811 for ; Tue, 13 Feb 2024 12:42:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828143; cv=none; b=Soppk17JlduCE6TQDGfFFw87DH/FCUMXPEt+BSorlGpU+dimD/Lyg7a1wEE8T7DanpMa8Tk/1LKKJBbD0w4zrZqrieXm1Zd+kFVPBrhACNvFBi8fKaOen4vUlxfJp06JNL0GvaGmeuPrZqjKmdGqnZIp+TlI/1L9mFyo72WVYsE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828143; c=relaxed/simple; bh=oVU6VFoULzmc4tmrHb0t9imKrLlrN7N2PGEhiPIi8jM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=djYjUUSPVY8PmnT6u5SB4RPHsw+5Uqdsz8pTXc8mVmoE9GXof86zEBQ9lbTzUP4q7n2e1M6eaPbGx10gYbUtAhIUHHLa0UHtxhrkWZdK4B+0unt9KVjVB0oF9FjPcydgXGP5owQ2Dg9T+khj9uO94ZZmVJFVyO6OZCG9pmGpFDE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=D9650BW9; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-33ce2ba4ee6so51197f8f.0 for ; Tue, 13 Feb 2024 04:42:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707828140; x=1708432940; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3FN139qBzPwhjMtuDTCrSTJijK28dahNsbuGrqs5W2w=; b=D9650BW9nvHJBvDG3w5z5agP99e1GyWoNebyRFm3n2zNwNNTGu7TqrlDK+6MUtiimv 50xohzKBTPRzSusQNknYU52de9/GkPVFssgssRmwylYdxxuf/sqsmXhWC0EFePyRtPdE YV2XPNUqlhE+zrDaGhlmsOvpUAHdIbjJvhNHI4tCQMqPHUsPuLIYhbWk0Yn/3dKhuEJn +ylJpk/l2aVk+dXa+cycsFL7vmJiK0KzDCTKyz/qXyR4gSeyys8MVYhNJ/hXmspmyuJ4 mhxotzHYJ7g1vKB+okIUvPuRExOh0AROXX/a3vBZ6eEhkmDKmrLjVZus2e/IrC93415U FIXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707828140; x=1708432940; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3FN139qBzPwhjMtuDTCrSTJijK28dahNsbuGrqs5W2w=; b=lnTTcQ/AkpktCZ1i9DJeM32wUvmyb7ZcWgye6XOeKaaByfsmtOL7EHHKG843f2KoHf boiTFhtFuaMSODFV6n0W0rCc+0PRLi3yBLfEeQB+KpcBYym5SYKYyUph+W+sltE6w85g EDYxZh/ZFYQtT2pSOMH+wGP3DfLF1gK1p9D4SUeXee/JoVDljID7J5M5q20u3jCtAYOe pVuP9scgiG9jzwfzvTd6+SQwLusFrW3o+xjJMFjHR+xnF7RRQu6nfry8vxeLPlowmWC4 oyZaghzgP+FLKtL1x1SDf23j4nG4fWS3LozkuX2k9SxK6BF+HKPsNtXymwgwt7T3AkPG IdRA== X-Gm-Message-State: AOJu0YwQdLBObTa2BG2p+4xOtd5KLk8CVsTdZx5AVnmxHnMfEm45462C pPCzs1tYmD3x1UcuPsqCNBRTQ+FMPoEJ1KhOpbCBh1HP3O67YNctbKdrVDQevRN5TtK8GQRkQTr yEEOQu4xjBdNhBg+etxkJrOjPff+O/D2GMhmqme9qhAtOKjmy6oReDhkjYGqVD8oxwWpWUCjZAl vm5uCLPWqoiEUXvowr/BcVrBX3hLjEbg== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:314a:b0:411:c45a:3926 with SMTP id h10-20020a05600c314a00b00411c45a3926mr8396wmo.1.1707828139786; Tue, 13 Feb 2024 04:42:19 -0800 (PST) Date: Tue, 13 Feb 2024 13:41:52 +0100 In-Reply-To: <20240213124143.1484862-13-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240213124143.1484862-13-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6958; i=ardb@kernel.org; h=from:subject; bh=eogXneMNGSkhcOk/JligRmO4hko1csHM8X0odsNJfPM=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfV08sT9Mq9jwnQj33kvK+1V0T2b9qdvVTGvDIu686KCs 2dlypZ0lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgImsM2BkuP3m2rY5zR8F1V7L zPoSYH/o+LKd+g+vzHeZvmv7zxiJGdmMDB/5Eifo76v4l/zn0v7iHrcei5thvHeYdy98bS/52eg nHz8A X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240213124143.1484862-21-ardb+git@google.com> Subject: [PATCH v4 08/11] x86/boot: Move mem_encrypt= parsing to the decompressor From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Kevin Loughlin , Tom Lendacky , Dionna Glaze , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Andy Lutomirski , Arnd Bergmann , Nathan Chancellor , Nick Desaulniers , Justin Stitt , Kees Cook , Brian Gerst , linux-arch@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" From: Ard Biesheuvel The early SME/SEV code parses the command line very early, in order to decide whether or not memory encryption should be enabled, which needs to occur even before the initial page tables are created. This is problematic for a number of reasons: - this early code runs from the 1:1 mapping provided by the decompressor or firmware, which uses a different translation than the one assumed by the linker, and so the code needs to be built in a special way; - parsing external input while the entire kernel image is still mapped writable is a bad idea in general, and really does not belong in security minded code; - the current code ignores the built-in command line entirely (although this appears to be the case for the entire decompressor) Given that the decompressor/EFI stub is an intrinsic part of the x86 bootable kernel image, move the command line parsing there and out of the core kernel. This removes the need to build lib/cmdline.o in a special way, or to use RIP-relative LEA instructions in inline asm blocks. This involves a new xloadflag in the setup header to indicate that mem_encrypt=on appeared on the kernel command line. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/misc.c | 15 +++++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + arch/x86/lib/Makefile | 13 -------- arch/x86/mm/mem_encrypt_identity.c | 32 ++------------------ drivers/firmware/efi/libstub/x86-stub.c | 3 ++ 5 files changed, 22 insertions(+), 42 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index b99e08e6815b..6c5c190a4d86 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -357,6 +357,19 @@ unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, return entry; } +/* + * Set the memory encryption xloadflag based on the mem_encrypt= command line + * parameter, if provided. + */ +static void parse_mem_encrypt(struct setup_header *hdr) +{ + int on = cmdline_find_option_bool("mem_encrypt=on"); + int off = cmdline_find_option_bool("mem_encrypt=off"); + + if (on > off) + hdr->xloadflags |= XLF_MEM_ENCRYPTION; +} + /* * The compressed kernel image (ZO), has been moved so that its position * is against the end of the buffer used to hold the uncompressed kernel @@ -387,6 +400,8 @@ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) /* Clear flags intended for solely in-kernel use. */ boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG; + parse_mem_encrypt(&boot_params_ptr->hdr); + sanitize_boot_params(boot_params_ptr); if (boot_params_ptr->screen_info.orig_video_mode == 7) { diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h index 01d19fc22346..eeea058cf602 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -38,6 +38,7 @@ #define XLF_EFI_KEXEC (1<<4) #define XLF_5LEVEL (1<<5) #define XLF_5LEVEL_ENABLED (1<<6) +#define XLF_MEM_ENCRYPTION (1<<7) #ifndef __ASSEMBLY__ diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile index ea3a28e7b613..f0dae4fb6d07 100644 --- a/arch/x86/lib/Makefile +++ b/arch/x86/lib/Makefile @@ -14,19 +14,6 @@ ifdef CONFIG_KCSAN CFLAGS_REMOVE_delay.o = $(CC_FLAGS_FTRACE) endif -# Early boot use of cmdline; don't instrument it -ifdef CONFIG_AMD_MEM_ENCRYPT -KCOV_INSTRUMENT_cmdline.o := n -KASAN_SANITIZE_cmdline.o := n -KCSAN_SANITIZE_cmdline.o := n - -ifdef CONFIG_FUNCTION_TRACER -CFLAGS_REMOVE_cmdline.o = -pg -endif - -CFLAGS_cmdline.o := -fno-stack-protector -fno-jump-tables -endif - inat_tables_script = $(srctree)/arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = $(srctree)/arch/x86/lib/x86-opcode-map.txt quiet_cmd_inat_tables = GEN $@ diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_identity.c index 0166ab1780cc..d210c7fc8fa2 100644 --- a/arch/x86/mm/mem_encrypt_identity.c +++ b/arch/x86/mm/mem_encrypt_identity.c @@ -43,7 +43,6 @@ #include #include -#include #include #include @@ -95,9 +94,6 @@ struct sme_populate_pgd_data { */ static char sme_workarea[2 * PMD_SIZE] __section(".init.scratch"); -static char sme_cmdline_arg[] __initdata = "mem_encrypt"; -static char sme_cmdline_on[] __initdata = "on"; - static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd) { unsigned long pgd_start, pgd_end, pgd_size; @@ -504,11 +500,9 @@ void __init sme_encrypt_kernel(struct boot_params *bp) void __init sme_enable(struct boot_params *bp) { - const char *cmdline_ptr, *cmdline_arg, *cmdline_on; unsigned int eax, ebx, ecx, edx; unsigned long feature_mask; unsigned long me_mask; - char buffer[16]; bool snp; u64 msr; @@ -551,6 +545,9 @@ void __init sme_enable(struct boot_params *bp) /* Check if memory encryption is enabled */ if (feature_mask == AMD_SME_BIT) { + if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION)) + return; + /* * No SME if Hypervisor bit is set. This check is here to * prevent a guest from trying to enable SME. For running as a @@ -570,31 +567,8 @@ void __init sme_enable(struct boot_params *bp) msr = __rdmsr(MSR_AMD64_SYSCFG); if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) return; - } else { - /* SEV state cannot be controlled by a command line option */ - goto out; } - /* - * Fixups have not been applied to phys_base yet and we're running - * identity mapped, so we must obtain the address to the SME command - * line argument data using rip-relative addressing. - */ - asm ("lea sme_cmdline_arg(%%rip), %0" - : "=r" (cmdline_arg) - : "p" (sme_cmdline_arg)); - asm ("lea sme_cmdline_on(%%rip), %0" - : "=r" (cmdline_on) - : "p" (sme_cmdline_on)); - - cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr | - ((u64)bp->ext_cmd_line_ptr << 32)); - - if (cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer)) < 0 || - strncmp(buffer, cmdline_on, sizeof(buffer))) - return; - -out: RIP_REL_REF(sme_me_mask) = me_mask; physical_mask &= ~me_mask; cc_vendor = CC_VENDOR_AMD; diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 0d510c9a06a4..9a25ec16b344 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -879,6 +879,9 @@ void __noreturn efi_stub_entry(efi_handle_t handle, } } + if (efi_mem_encrypt > 0) + hdr->xloadflags |= XLF_MEM_ENCRYPTION; + status = efi_decompress_kernel(&kernel_entry); if (status != EFI_SUCCESS) { efi_err("Failed to decompress kernel\n"); -- 2.43.0.687.g38aa6559b0-goog