From patchwork Fri Feb 9 16:49:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 199017 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp991862dyd; Fri, 9 Feb 2024 09:04:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUGfcyfaRBu7scvAEnhyGS8RXSMDLhWFI4sT9G0XhOqx1ZDz/HOohVR0r6ib/hvd7NSgeNd7d36Ghaqgb0AJ/rmyj7l2Q== X-Google-Smtp-Source: AGHT+IGPOBIM8Z0i7MPJ+NtH1GT7rRVqbpkMPROoOygxwDbs7NlfIvoE6P/6WO2eiJi1EczXJ+dI X-Received: by 2002:a17:903:249:b0:1d8:ae30:eddd with SMTP id j9-20020a170903024900b001d8ae30edddmr2514167plh.23.1707498246803; Fri, 09 Feb 2024 09:04:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707498246; cv=pass; d=google.com; s=arc-20160816; b=ju+cmqnzqa4rZMWOr7k/vt8oI6siwq3II0zQgXBKklRQIi5N1P8k7I5Hu0qQwTV3UW GqnlE7ESh5hocYP9m2yo7/GR38wHASrJXsXze/yKHq7Pfae27kuvtk8lV0qbjIR8EYxc FZEevSXkQ7u+uZro310H1Fbk4toBAL82apYpYN8m6x4qINOkar9EqvfSWQpJQP95dYFz trpKShP320kvmaPu6qOW4UFFxZDMESzd7eqZbOepmMvHv0mh1hzPAZUk1kagzouyMcBz skHH4NeP60fl0MMxTQIxsXrBiInSMoAfoNFMGQD/9PQSP2ROUIBQJyCK1xRnLPJT7ngN tIHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=ynfqeEaOI5g7zJp9rFKHbivlh40eLsPZgdwI7uHEjME=; fh=05/ykgsWAgc55yK7P5UIx5W7VZzFJiwiT3uD3+QPDGY=; b=fC0/Xo4XYSuzDkzbtnwo2A0saG1Sh4lcLsEKigaCEa1AZBL82kKwd5ikfG/FWvYVhC V5Cfk63xeW3CAlgFhvqTbCrRUKu1wQmjdXxG6x0UlspNs/SX95SK+KsEJevqCry+qB11 +gAUIAMBJiS5W+ppNcf3yx0AvNoDVwUrdr8qmS286CmJUBWhhVM0zAzTNBUH1ovX7Lad FgI04YseXiU8sFU70VXqkWHRdRKjprWrw3inodZDLu+hLPdZx1FJnwbujNDTTVfhhbfS 6IimTLGuq2baGd7gkPpP07dy4gdmRTTcvi6pJhS6vFMZ6gMi8hQN6oIg60uU4V6VVbtd XJdg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=HG3l0MQQ; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-59680-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59680-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com X-Forwarded-Encrypted: i=2; AJvYcCXPFFPqDC8TO2+kuEB58sxTDg7JeJg59HmdvAfW6stVD4d5SKTz+yc/pLZGp2oCt97Bbes9fJJNRK6F/JW5/dy8g/oEbw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 21-20020a170902c25500b001d963d963a9si1892676plg.2.2024.02.09.09.04.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 09:04:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59680-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=HG3l0MQQ; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-59680-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59680-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 99ED528C24E for ; Fri, 9 Feb 2024 16:57:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CE757EEFC; Fri, 9 Feb 2024 16:52:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="HG3l0MQQ" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23E18762CD; Fri, 9 Feb 2024 16:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707497571; cv=none; b=P9boCjeKLMlVWAMoBNIPs2NkOkIN6LHNbSoKTvcMol9ehPqYrA4TaFsSzFeR0NpmPUXcY83Oefy7xC4WB2iEzgyb63grZCuaPv5z2BR3Ge71CERBNGFBtqwOxkuzgSzwJR+jgbzfPgcC4S5mDUZTyqGqQbAmCWbKcQkvtot1Bz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707497571; c=relaxed/simple; bh=NmxkS+jMEl1UDH6K3hczdpAMvlCTTVjwBXbAsGC195I=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=QQrth0eJXD1KYQXU7VASZPnmvTmHk6AxC/kIo8lRl9J2RJOR2WnIQEIekyspVJzyV40j/cAx7Ya6D/KuvPBxQie4MHtBDntKifUyOU+jduRKo8WouK3lyxfBD8xtujoH64GLt9A7qkMoi64IYq6t/nJAhbKFab3jhxHVXohpmE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b=HG3l0MQQ; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 566E0C433F1; Fri, 9 Feb 2024 16:52:49 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="HG3l0MQQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1707497565; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ynfqeEaOI5g7zJp9rFKHbivlh40eLsPZgdwI7uHEjME=; b=HG3l0MQQMBB/izRV3H7Zc7JNrczDOpSpsAZXyzy5j/UH6cLoiMw5DTRxwVxAGeKSa7DRsk LL1NMS7VhaL8tbyNi2QFjNNIo+HcQuY9RgMxn3QuyfWQeF3/uoFHkb6hZ/VbFdy57kv3s7 PpuOkZxB3krT0n1j8Fn8X5nx/IkFDYg= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id f1048efb (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 9 Feb 2024 16:52:45 +0000 (UTC) From: "Jason A. Donenfeld" To: x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Borislav Petkov , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Dave Hansen , Elena Reshetova , "H . Peter Anvin" , Ingo Molnar , "Kirill A . Shutemov" , Theodore Ts'o , Thomas Gleixner Subject: [PATCH] x86/coco: Require seeding RNG with RDRAND on CoCo systems Date: Fri, 9 Feb 2024 17:49:15 +0100 Message-ID: <20240209164946.4164052-1-Jason@zx2c4.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790441681967950759 X-GMAIL-MSGID: 1790441681967950759 There are few uses of CoCo that don't rely on working cryptography and hence a working RNG. Unfortunately, the CoCo threat model means that the VM host cannot be trusted and may actively work against guests to extract secrets or manipulate computation. Since a malicious host can modify or observe nearly all inputs to guests, the only remaining source of entropy for CoCo guests is RDRAND. Unfortunately, RDRAND itself can be rendered unreliable by the host, since the host controls guest scheduling and can starve RDRAND's generation. A malicious host could also choose to simply terminate or not boot a CoCo guest. So, tie the starvation of RDRAND to a BUG_ON at boot time. Specifically, try at boot to seed the RNG using 256 bits of RDRAND output. If these fail, BUG(). This doesn't handle the more complicated case of reseeding later in boot, but that's fraught with its own difficulties, such as a malicious userspace starving the kernel. For now, simply make sure the RNG is initially seeded securely during boot, avoiding the worst of potential pitfalls. This patch is deliberately written to be "just a CoCo x86 driver feature" and not part of the RNG itself. Many device drivers and platforms have some desire to contribute something to the RNG, and add_device_randomness() is specifically meant for this purpose. Any driver can call this with seed data of any quality, or even garbage quality, and it can only possibly make the quality of the RNG better or have no effect, but can never make it worse. Rather than trying to build something into the core of the RNG, this patch interprets the particular CoCo issue as just a CoCo issue, and therefore separates this all out into driver (well, arch/platform) code. Cc: Borislav Petkov Cc: Daniel P. Berrangé Cc: Dave Hansen Cc: Elena Reshetova Cc: H. Peter Anvin Cc: Ingo Molnar Cc: Kirill A. Shutemov Cc: Theodore Ts'o Cc: Thomas Gleixner , Signed-off-by: Jason A. Donenfeld --- Probably this shouldn't be merged until Dave/Elena and others get back with regards to the full picture, with information from inside Intel. But I have a feeling this patch, or something like it, is ultimately what we'll wind up with, so I'm posting it now. I don't have a functional CoCo setup, so this patch has only been very lightly tested. arch/x86/coco/core.c | 36 ++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/coco.h | 2 ++ arch/x86/kernel/setup.c | 2 ++ 3 files changed, 40 insertions(+) diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c index eeec9986570e..4e3b1cfe0063 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -3,13 +3,16 @@ * Confidential Computing Platform Capability checks * * Copyright (C) 2021 Advanced Micro Devices, Inc. + * Copyright (C) 2024 Jason A. Donenfeld . All Rights Reserved. * * Author: Tom Lendacky */ #include #include +#include +#include #include #include @@ -153,3 +156,36 @@ __init void cc_set_mask(u64 mask) { cc_mask = mask; } + +__init void cc_random_init(void) +{ + unsigned long rng_seed[32 / sizeof(long)]; + size_t i, longs; + + if (cc_vendor == CC_VENDOR_NONE) + return; + + /* + * Since the CoCo threat model includes the host, the only reliable + * source of entropy that can be neither observed nor manipulated is + * RDRAND. Usually, RDRAND failure is considered tolerable, but since a + * host can possibly induce failures consistently, it's important to at + * least ensure the RNG gets some initial random seeds. + */ + for (i = 0; i < ARRAY_SIZE(rng_seed); i += longs) { + longs = arch_get_random_longs(&rng_seed[i], ARRAY_SIZE(rng_seed) - i); + + /* + * A zero return value means that the guest is under attack, + * the hardware is broken, or some other mishap has occurred + * that means the RNG cannot be properly rng_seeded, which also + * likely means most crypto inside of the CoCo instance will be + * broken, defeating the purpose of CoCo in the first place. So + * just panic here because it's absolutely unsafe to continue + * executing. + */ + BUG_ON(longs == 0); + } + add_device_randomness(rng_seed, sizeof(rng_seed)); + memzero_explicit(rng_seed, sizeof(rng_seed)); +} diff --git a/arch/x86/include/asm/coco.h b/arch/x86/include/asm/coco.h index 76c310b19b11..e9d059449885 100644 --- a/arch/x86/include/asm/coco.h +++ b/arch/x86/include/asm/coco.h @@ -15,6 +15,7 @@ extern enum cc_vendor cc_vendor; void cc_set_mask(u64 mask); u64 cc_mkenc(u64 val); u64 cc_mkdec(u64 val); +void cc_random_init(void); #else #define cc_vendor (CC_VENDOR_NONE) @@ -27,6 +28,7 @@ static inline u64 cc_mkdec(u64 val) { return val; } +static inline void cc_random_init(void) { } #endif #endif /* _ASM_X86_COCO_H */ diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 84201071dfac..30a653cfc7d2 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -994,6 +995,7 @@ void __init setup_arch(char **cmdline_p) * memory size. */ mem_encrypt_setup_arch(); + cc_random_init(); efi_fake_memmap(); efi_find_mirror();