Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp550893rdb; Mon, 29 Jan 2024 10:06:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFCxvfP5sPjgDfMxM1L+QFAddjutKknXYlBeEJmoliajCZFNBRHAMoqygyGJILdkXjHcxzd X-Received: by 2002:a17:906:8417:b0:a35:689b:dbca with SMTP id n23-20020a170906841700b00a35689bdbcamr4133494ejx.18.1706551570734; Mon, 29 Jan 2024 10:06:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706551570; cv=pass; d=google.com; s=arc-20160816; b=DhTqoQxR9hoiKnh1ToDy2GOdYG95VyG7r4DpGuEoG7Wqjuq7vXQ8KPDE9UF/FFhv28 HtB1QQKsT/wZxmT2aunPxGGGQ1b0oRqK2SYbwe62IBHKhfXZQIDOcpXhJVcfnfcd5bhm qfdq45+WYA6eFoJ1M/fkUmfkCVD49FNoYTpOyISdOBTa9FNr34BM+jh5pNs5PvTGgRI7 YafZLSCTKajntlz7/a0T/30FXint5O8ahVCJoKXX36Nn+nxYRt2Yc7kpXSLO0KQYwbFS F2h7ARcfADQbERe+SoneGrtCZLeQ7pwJKBozi+AapfLjubPXURAW0rNSb+olW+3mbidD p98g== 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=p3FWGTV10qie8rRiz9nelg+HW9vFOvPrRVG9vffZcTs=; fh=fMhMPvo8duafGkM4xZHdH7fULbau3etefTGsKa+Qy7I=; b=1A1j6ZfW5ltFVXduwATzJfy0mOhvRBEbLtDwuYXAfK9pq0a+E+SV1vHfm6y9nbW4QN POy4m4RA/21Ssc62nRVwvPoVlsJoo//v1NxKeixLyLrig0sZGzNboQ2eulK+aaM3iTpl PEQZ10WI4nYbWSoc9PTqwXvI2U/HIhIX1uo4nrBO94zGl4YDDAsKCAj1f5MA7RjDMhY8 dJijFA/qdr4rWf7c/hEk4SL9uqGYMKD7GVdcUm2sEvdqyNamEz8WNpIETPVYdwoJn4Vi Fin/F/aDVaqI0jE+egEPyZSdmOeYIpHE9dzxkPj/j1wwTprPrdkjebo85YasSMF7GZ6b 6vWg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="g4paj4/2"; 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-43269-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43269-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t23-20020a1709064f1700b00a35b9bfb08csi1120744eju.30.2024.01.29.10.06.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 10:06:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43269-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="g4paj4/2"; 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-43269-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43269-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 4CB871F2453B for ; Mon, 29 Jan 2024 18:06:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C103C76023; Mon, 29 Jan 2024 18:05:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="g4paj4/2" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 90BFA6F073 for ; Mon, 29 Jan 2024 18:05:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706551536; cv=none; b=EYXiJmo2f3qJueKibgCPCmoyntHkQOwy37ss9HUvgug28oPPaJHkcuxbubHc5CamoC7chfojs3UkdVBjgcOmiME7iC/jYE8jbhoRs0wTjyxuYZyTWvGUevltqzJ8lGeqmkTtczFoHO60sjp6Bw73X3s6D0T1CC8li+GOZ7nmwLY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706551536; c=relaxed/simple; bh=HFn1spOv4KPR4R1zHjTL4BDpzlPynxVt+ZjcueLDXCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LvX+iqANvo0n7w6xXzGkd7LxTuRxRCEzVghiUiofv49OdpYucet7ckt0n9fyJFvP70mu6b0C0yfiMxD+e2kD4iKnRT8CJcuSX7qjqVRww1yiRAQjEU0wh/l6DTGkf6KMSm/W2ymF+MsXE+1HwzIzHHrlTCO5Rwh/EFOkEoB7z4o= 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=g4paj4/2; arc=none smtp.client-ip=209.85.128.202 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-yw1-f202.google.com with SMTP id 00721157ae682-5ff93902762so55570337b3.2 for ; Mon, 29 Jan 2024 10:05:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706551533; x=1707156333; 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=p3FWGTV10qie8rRiz9nelg+HW9vFOvPrRVG9vffZcTs=; b=g4paj4/287fNNvwYS7k+HS4qAaVswZNsu8T+4cLZSy+eXyl6tD/ouoK2Wslz4zIX/m KBU8+2MLvR5WGs25D+LZ6xTtUWnHlKt54Xp1ELBSJKyQiziiMoWi7Ybi3Y5hSGr7vh44 m2yTCoTwZ0LVLM9uEfgM45GyIoYkj0s6e4wURlotgBff5NfWo5b6RakFS1OV+RqViXFU uOmx1wLzQtZiuD5Q5zRr+JATpaq2jnXNnzxA9VQfCWsGaT6ejCZ6a0KQurR66r1skaRa OirpC0RIU3yegLWVK+yZT7tXuwsWWvrIPtVxcxtYmwCPB8/ZMfj9yv5p2eh8+3nEwK9P ecyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706551533; x=1707156333; 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=p3FWGTV10qie8rRiz9nelg+HW9vFOvPrRVG9vffZcTs=; b=Bm+dvzP3R67hzbG5QoAIY188tJV5vkUNEmR2mDgRbl2BdGJSO2uX15MTXrymMwP3sd p2sz1Dd3Wkdl6XXSfPogx6lS8UbVBFHKGUHUQ34dJZiZRAT9Xq5v1TB2lPBXY5VQYiOL Y99QrwhPaS6mxezIgPGIzYRjMRgxBBbXwHGS9idtejQfZPUMhLYcSmQAj3DjcG4G+rT8 y4dO/4/nJ8PUC9qgJPrIIM5WtNRqA0bYHXIxL7wUSVRU1KsBoMvXN3hYPAUBMmVZzJVF yJUoFYzKVc+NX2UyIeUNo7dLPchGr9Hq88xzyeihbsRZ6b/Xb5RJykAwu0K3pb/7Hj9n 6Qwg== X-Gm-Message-State: AOJu0YzHvYGKiRTQ4WVTPpM/icBVFosM6jttEEt95Cn+Eir+2Tqe5G6c xOBQHhN18KsREDANLsBL9lX0HLR2WulFs/qBXt4qhUrf4rYKiExWT1oNrll9bKN2xpf3T27j+8k ULNzEGKCnB4gUt48S0NpLNxfp3t+hwhhmFbMd0vbZ1pau8PCtIilcguWoe+khfxduc1Oz3U81+8 jlPY1TjurXk27Cx/ehikE5pA2DiXb2CQ== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:690c:ed4:b0:5d8:4274:bae2 with SMTP id cs20-20020a05690c0ed400b005d84274bae2mr2074182ywb.6.1706551533566; Mon, 29 Jan 2024 10:05:33 -0800 (PST) Date: Mon, 29 Jan 2024 19:05:05 +0100 In-Reply-To: <20240129180502.4069817-21-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: <20240129180502.4069817-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7925; i=ardb@kernel.org; h=from:subject; bh=M0vxsf0gTqIKMZpFhFJ2BBWB91v5R63xIeoeRQ3uSVE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX7i4vMAZebr19uPjtNTGWy4aWMzFNMrYUL3pafZBPbu /6AHf+KjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRHYUM/0zmcif/jDiQ3vfl ukmjmedu7Q9+7mufCvEZl7/vklXvs2D4Z7mhl60oe8btsotvDwd9qtTNC7TYFrvMi1F2Z/gmS/V oNgA= X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240129180502.4069817-23-ardb+git@google.com> Subject: [PATCH v3 02/19] 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 pair of new xloadflags in the setup header to indicate that a) mem_encrypt= was provided, and b) whether it was set to on or off. What this actually means in terms of default behavior when the command line parameter is omitted is left up to the existing logic - this permits the same flags to be reused if the need arises. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/misc.c | 22 ++++++++++ arch/x86/include/uapi/asm/bootparam.h | 2 + arch/x86/lib/Makefile | 13 ------ arch/x86/mm/mem_encrypt_identity.c | 45 +++----------------- drivers/firmware/efi/libstub/x86-stub.c | 6 +++ 5 files changed, 37 insertions(+), 51 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index b99e08e6815b..d63a2dc7d0b1 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -357,6 +357,26 @@ 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. If not, the consumer of the flag decides what the + * default behavior should be. + */ +static void set_mem_encrypt_flag(struct setup_header *hdr) +{ + hdr->xloadflags &= ~(XLF_MEM_ENCRYPTION | XLF_MEM_ENCRYPTION_ENABLED); + + if (IS_ENABLED(CONFIG_ARCH_HAS_MEM_ENCRYPT)) { + 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; + if (on > off) + hdr->xloadflags |= XLF_MEM_ENCRYPTION_ENABLED; + } +} + /* * 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 +407,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; + set_mem_encrypt_flag(&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..316784e17d38 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -38,6 +38,8 @@ #define XLF_EFI_KEXEC (1<<4) #define XLF_5LEVEL (1<<5) #define XLF_5LEVEL_ENABLED (1<<6) +#define XLF_MEM_ENCRYPTION (1<<7) +#define XLF_MEM_ENCRYPTION_ENABLED (1<<8) #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 7f72472a34d6..06466f6d5966 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,10 +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 char sme_cmdline_off[] __initdata = "off"; - static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd) { unsigned long pgd_start, pgd_end, pgd_size; @@ -504,11 +499,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, *cmdline_off; unsigned int eax, ebx, ecx, edx; unsigned long feature_mask; unsigned long me_mask; - char buffer[16]; bool snp; u64 msr; @@ -570,42 +563,18 @@ void __init sme_enable(struct boot_params *bp) msr = __rdmsr(MSR_AMD64_SYSCFG); if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) return; + + if (bp->hdr.xloadflags & XLF_MEM_ENCRYPTION) { + if (bp->hdr.xloadflags & XLF_MEM_ENCRYPTION_ENABLED) + sme_me_mask = me_mask; + } else if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT)) { + sme_me_mask = me_mask; + } } else { /* SEV state cannot be controlled by a command line option */ sme_me_mask = me_mask; - 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)); - asm ("lea sme_cmdline_off(%%rip), %0" - : "=r" (cmdline_off) - : "p" (sme_cmdline_off)); - - if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT)) - sme_me_mask = me_mask; - - 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) - goto out; - - if (!strncmp(buffer, cmdline_on, sizeof(buffer))) - sme_me_mask = me_mask; - else if (!strncmp(buffer, cmdline_off, sizeof(buffer))) - sme_me_mask = 0; - -out: if (sme_me_mask) { physical_mask &= ~sme_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..66e336cca0cc 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -879,6 +879,12 @@ void __noreturn efi_stub_entry(efi_handle_t handle, } } + if (IS_ENABLED(CONFIG_ARCH_HAS_MEM_ENCRYPT) && efi_mem_encrypt) { + hdr->xloadflags |= XLF_MEM_ENCRYPTION; + if (efi_mem_encrypt > 0) + hdr->xloadflags |= XLF_MEM_ENCRYPTION_ENABLED; + } + status = efi_decompress_kernel(&kernel_entry); if (status != EFI_SUCCESS) { efi_err("Failed to decompress kernel\n"); -- 2.43.0.429.g432eaa2c6b-goog