Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3439149lqp; Tue, 26 Mar 2024 09:09:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX0f4gAJkGpaZmhdLcfnCDIXMh9osrClHbhmR0N53IkSvzRjXUCCibYVPyo1B0T+/HkLx5QDjcdywQZjFJPCF2GeKoMoAC+KVffIpG3RQ== X-Google-Smtp-Source: AGHT+IHoUCszk89IHVSzP9ZA1ybWIUsFoJd45XtRE999TKrnzLbmnmVUSC/FhL/nqEpFDD3UBOOd X-Received: by 2002:a17:902:76c4:b0:1dd:7b1d:af7f with SMTP id j4-20020a17090276c400b001dd7b1daf7fmr138258plt.58.1711469343309; Tue, 26 Mar 2024 09:09:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711469343; cv=pass; d=google.com; s=arc-20160816; b=nyEDxeQjDy8mBNJzarqmnscZDSvxUne4aFqsI4MMUFk4mD606vWqAt46imkWeU35/F QrPp1tegpT+XAqJjqq6FQNWY5PNQ8uJLrwByizlHiOeqwZv5IfzV20fBDmeWabg8gnYC pdZVjtiH+sqHvgNktoUu6sZJiw4mKZZNEJDDCv1bGfrJtKuFWbNP32jkBopwiFqf/q1M KRevPB0x0rxnXk+TXuQDznsAOLIEtWpGaVA3wk+bfA4EWMhwQAvVmZVKYR1V7721xHLF SfDfA4Q2dN/GJTG/c5LxDkGICtOSAwz7NL2dZWexpC1XM3lokBDsXopEoEfjXNy+frBJ e2Pw== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=qRlhV/wNtR2/43VLUsHVHpymnq61r5ZGY83YpOPrN94=; fh=DHYudgMBHakKVtVik8uDMmhBNgXSBEariQz4m5SWPoY=; b=nxPFd5xQWRa6zWe+g5hqXTht0lEAz8qyez/kn0L30RrwD1B2wmarSien/Zk57ZY3rk CrenIe/XRPgxOFFtv7tNs+PXaMMlCKtXc12e98Q8FHNEPXmqyexdyc9uM4LlNTuudrV7 q9GsJjEfJTQqdfx8dC02yGsw++2TX0dF4TVmBYsQOH2Md8H4xwSC2Lcc75L3RLTOfg11 AHlRZNsMdmnKVW7FsdycKD4bcXc/88Jc5Qhe6x4SBcRgDPpWVmcZVg7i/a8Yflu1rqxc MwaGABeFMOHzlVWaLfIvfy7B/OipxKmWqTcjmx+zjuw3VzsyBF/jlJu8G45aV9Vja2Kj sSdA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=LA3vWYhI; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-119424-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119424-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id z16-20020a170903019000b001dc4b6c737dsi2801264plg.338.2024.03.26.09.09.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 09:09:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119424-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=LA3vWYhI; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-119424-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119424-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 34B5D30078A for ; Tue, 26 Mar 2024 16:08:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5BBA413C9B4; Tue, 26 Mar 2024 16:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="LA3vWYhI" 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 3C13B13C696; Tue, 26 Mar 2024 16:07:57 +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=1711469278; cv=none; b=XfuXH6HXJIiGsFOxTO2Az6yyTob/nMWHj7xah76wnMdL5hC3TGPkteHg7ziovOziZm2nsQ4hEhNest+paZ1EASNto98hkurMCKKCjPoIaSpw8+0gGbKWXAj32nIXW4J8UOm16698IaCIaWIMYYLDHf+swEK+hYRRmDX3ZoYdO3I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711469278; c=relaxed/simple; bh=30JIHtimg6n8W6Ta1n9cVF3rWKvRQkP+z9819UW55F0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BEAXgJZL1H5/ieHkf0t70l5X7jKosarUC6pQxIjAWagJoGXFkwn8wC/jTXZq6RWH0bUeaNhKf8wKUPjV55KAMlSVI08ATx8eID3fD5RgQaPajjmEtgCaknTXlRFzsCxWfCtlsOCNeLcQbEvBA/r27lnADaeWEGk157fYzDa/ikM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b=LA3vWYhI; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 576F1C433F1; Tue, 26 Mar 2024 16:07:56 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="LA3vWYhI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1711469273; 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: in-reply-to:in-reply-to:references:references; bh=qRlhV/wNtR2/43VLUsHVHpymnq61r5ZGY83YpOPrN94=; b=LA3vWYhI8E4ciTAaUxImG87k3puXPqFgsORrEg9fZYgFW/SG1E0KEx+Rx0H/C+11JQaZb/ YsKQZeDQnqdSrNM3co0GO+PVi6PzoTiPcQXlgqATXGbjFzfRIYAj5/DgV3jQCtMOwA474e E+6DyVgXy88k+fRqt8op4STygCX8Q4M= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 557207e9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 26 Mar 2024 16:07:53 +0000 (UTC) From: "Jason A. Donenfeld" To: Borislav Petkov Cc: x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Jason A. Donenfeld" , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , Thomas Gleixner , Tom Lendacky , stable@vger.kernel.org, Elena Reshetova , "Kirill A . Shutemov" , Theodore Ts'o Subject: [PATCH v6] x86/coco: Require seeding RNG with RDRAND on CoCo systems Date: Tue, 26 Mar 2024 17:07:35 +0100 Message-ID: <20240326160735.73531-1-Jason@zx2c4.com> In-Reply-To: <20240326112137.GDZgKvwRHD4yQs3Zm-@fat_crate.local> References: <20240326112137.GDZgKvwRHD4yQs3Zm-@fat_crate.local> 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. If RDRAND is broken -- due to CPU hardware fault -- the RNG as a whole is meant to gracefully continue on gathering entropy from other sources, but since there aren't other sources on CoCo, this is catastrophic. This is mostly a concern at boot time when initially seeding the RNG, as after that the consequences of a broken RDRAND are much more theoretical. So, try at boot to seed the RNG using 256 bits of RDRAND output. If this fails, panic(). This will also trigger if the system is booted without RDRAND, as RDRAND is essential for a safe CoCo boot. 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: H. Peter Anvin Cc: Ingo Molnar Cc: Thomas Gleixner Cc: Tom Lendacky Cc: stable@vger.kernel.org Reviewed-by: Elena Reshetova Reviewed-by: Kirill A. Shutemov Reviewed-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld --- Changes v5->v6: - Rebase on tip/master. - Add string.h include. arch/x86/coco/core.c | 41 +++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/coco.h | 2 ++ arch/x86/kernel/setup.c | 2 ++ 3 files changed, 45 insertions(+) diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c index d07be9d05cd0..ddd4efdc79d6 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -3,13 +3,17 @@ * 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 #include @@ -148,3 +152,40 @@ u64 cc_mkdec(u64 val) } } EXPORT_SYMBOL_GPL(cc_mkdec); + +__init void cc_random_init(void) +{ + /* + * The seed is 32 bytes (in units of longs), which is 256 bits, which + * is the security level that the RNG is targeting. + */ + unsigned long rng_seed[32 / sizeof(long)]; + size_t i, longs; + + if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) + 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 + * CoCo guests have no other unobservable source of entropy, 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 doesn't have RDRAND + * or the CPU is physically broken, and in both cases that + * 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. + */ + if (longs == 0) + panic("RDRAND is defective."); + } + 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 fb7388bbc212..c086699b0d0c 100644 --- a/arch/x86/include/asm/coco.h +++ b/arch/x86/include/asm/coco.h @@ -22,6 +22,7 @@ static inline 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) @@ -34,6 +35,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 f04cef846e51..e3f01caf104b 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -993,6 +994,7 @@ void __init setup_arch(char **cmdline_p) * memory size. */ mem_encrypt_setup_arch(); + cc_random_init(); efi_fake_memmap(); efi_find_mirror(); -- 2.44.0