Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1070684rdb; Fri, 9 Feb 2024 09:20:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVjzHCciJk0mO+iIGYOhiQueAAAIQq1wQZyaZx5Rr+Rm8Dftlva2BoBkH3fFSaB058Q0apwHq4t1T7LXGo9eIzkDm2Ls644Uo0cC85cRg== X-Google-Smtp-Source: AGHT+IFbIVYxAe/6VC6IXjU0VV9Gn3V1FfI0NjPi/BfxolUvlRpP8sl8SrpBaCg3gTL8NqCzfPds X-Received: by 2002:a17:902:a385:b0:1d9:b6a1:1ffe with SMTP id x5-20020a170902a38500b001d9b6a11ffemr2129934pla.54.1707499251835; Fri, 09 Feb 2024 09:20:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707499251; cv=pass; d=google.com; s=arc-20160816; b=Zw1BkGMWJQ5KoIPAu0kpr5Yh2TESNwh+j5uD9sFtTsvvyiAFUXRGM2c24bACv92IiH 5k/7XzPTnkiaMm+GOLzjNiqV0Dfkt9yrPrc6ypl0KTyotuENv0Em6Q1/nJlQSGEfmEO7 OK8GaxlzA1Oo77Wksf5jCGQ43Wae4avPUNVavoax52318RVcHA8fggYMM28yYHASmlMo jUWpvRkrM7XYBNSk/XDmXqa9swQrifo4uixgao4Yj4mSjQ/evUk12aqplr49HObLqZ1M AxyI1+EN9dndEnCY1QWTDTx7ElsBXdogN4pd3AW3Ha7hKQKcZlAT2GS1vmh8kfSsKlha W2cg== 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=Rh9Ya8JMaiYB/qQEhtyi9dkAKlniucuqn5HoYJYL7BA=; b=GzSNmSSPbsuh8MSeGvJG5CkQx98a9ViqgQ6BpjZDJ42HXFiNXP1ZMX+/RO2/4J3fcL VN3ENBFMFvf/DyCAeYtEJeAbFj0MMxaNIRce9JHD+gWvX++CJ39ClJl6VI2GvKyxnBfK Lmiepa9rw0YjJliUA6AVm6j1/1PMUwi8Skq5CKZ+cR4V1dLlWku4gdQ5goeyfGxOEvv3 BIC/f9v/tFFve4bynl1IIKDfwOsh+8PNtTNwjK14VicgJx9hrRwDyNUkiO7R5/LQqbZG p0moe486RwXZMILaP3lEaLl+A4xx3EblmI3LbPqFFhKyydK1hpi2FsyEH4b31C5d0KjZ IotA==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59680-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com X-Forwarded-Encrypted: i=2; AJvYcCVKBiN+rG3AWErRThLO7VBkkgjxyiQqnrjIk0MgFS8VbzpAkFn9WmiFIKPQYkG3OR5RhAEu/MCdfm3UCOvmCsznwLdw3qAUXz24Fzy8Mg== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id n6-20020a170903110600b001d8ded24c8esi1984005plh.143.2024.02.09.09.20.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 09:20:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59680-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59680-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 23329B2A86A for ; Fri, 9 Feb 2024 16:57:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4ABFE7CF37; Fri, 9 Feb 2024 16:52:52 +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=20P=20=2E=20Berrang=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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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(); -- 2.43.0