Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3550225pxj; Mon, 24 May 2021 09:10:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyH4Yrgu5IPu+9/l9xwBrNlJRj5tcvyhlDeB5MKkPU4DwagOu32c4YBuQFeBOt8shoVh/ji X-Received: by 2002:a92:dcd1:: with SMTP id b17mr14260934ilr.145.1621872606650; Mon, 24 May 2021 09:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621872606; cv=none; d=google.com; s=arc-20160816; b=sJ36zthvys0A8giuVIHkNils694NrVLLA/7q4QCHfEfnnIsRewT0wv9I/LJr8hqWCc qtZ/uDU8x8WIBcZvbhCJa4qapmXdneC44ZF+Ow9OVwc3Cze45NwdKmoNeehjHrWzzaCJ i0XrR3+vyqOfV7oyg5BlJC5Eu/u7iiwZ7dTtHGzEk9HFT7knfvoCIgai5OxhCzEUewzO 8dn37QvCWeYjU7hHJ72dwyN9wXY6WN36nYe7zMB79hITc38LZmzdXhBL1DNqWDPHZBHv gNO6XABe/MLsuGK4f8w/at7Ui2Of0/g8PbcvOFGC4/npCRrE+f+b9BO6Fw5vHh1laJwK VgFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KOaNjbr9XVVM5D5WXhnY4wUSMMlq+jgE/sUOr1kT+Jw=; b=NbK8U1CZpbZr2ZCOkGJmULM/3VXlIDmG9QrG3KRlCggsDrQjLze4pB9bMlwUYhIzZ2 quo3fUH3IMiD4aSjbAZ/dveXfD4wXDFBs4U1yHGvs7eVAQTUzS+vJ0zmb53ZZ8sCGrS3 YJoVgKMb5ydnH1txW5zNw1ey/OmAPu8hXkK39k+Y3yQDlLJOz+t8Wm7zsvC0v+/SnggN 31aYnzVQEHljV2YbrceXDeEy5XNbBdV161NlugeX4vBZks/KmyndUDDKs/7DSmqzEeXF g46rrJUB0bxDDeCSLVD9he2lMKtiiK5WniavOhCb2vRMNIF/weA/38123V2EmW0cMmU9 YEDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=06wMu3kS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o2si15124645jaa.70.2021.05.24.09.09.53; Mon, 24 May 2021 09:10:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=06wMu3kS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238364AbhEXQKE (ORCPT + 99 others); Mon, 24 May 2021 12:10:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:47354 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235910AbhEXQBt (ORCPT ); Mon, 24 May 2021 12:01:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5EA23613DF; Mon, 24 May 2021 15:46:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621871219; bh=9jW5E5FsQGcrO2qsi4g2HM0gupFU/sDofvrZVbVMKsY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=06wMu3kS26CjkYXtgFO0fUxbkTGNdyis4NtCliOHVCDXz4d/FTSYSodcS7ZNk+gU+ TpkvDm1UJggArboAlWvOx+wV2MHCYUaty2pRLp1LkbGrRem8l/L1GbpSaNERI7GkQb O7pLdawX8DDtftdRJ+zt5kMjjB0lW0ZQRgXOW5HA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joerg Roedel , Borislav Petkov Subject: [PATCH 5.12 127/127] x86/boot/compressed/64: Check SEV encryption in the 32-bit boot-path Date: Mon, 24 May 2021 17:27:24 +0200 Message-Id: <20210524152339.154524875@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210524152334.857620285@linuxfoundation.org> References: <20210524152334.857620285@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel commit fef81c86262879d4b1176ef51a834c15b805ebb9 upstream. Check whether the hypervisor reported the correct C-bit when running as an SEV guest. Using a wrong C-bit position could be used to leak sensitive data from the guest to the hypervisor. Signed-off-by: Joerg Roedel Signed-off-by: Borislav Petkov Link: https://lkml.kernel.org/r/20210312123824.306-8-joro@8bytes.org Signed-off-by: Greg Kroah-Hartman --- arch/x86/boot/compressed/head_64.S | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -172,11 +172,21 @@ SYM_FUNC_START(startup_32) */ call get_sev_encryption_bit xorl %edx, %edx +#ifdef CONFIG_AMD_MEM_ENCRYPT testl %eax, %eax jz 1f subl $32, %eax /* Encryption bit is always above bit 31 */ bts %eax, %edx /* Set encryption mask for page tables */ + /* + * Mark SEV as active in sev_status so that startup32_check_sev_cbit() + * will do a check. The sev_status memory will be fully initialized + * with the contents of MSR_AMD_SEV_STATUS later in + * set_sev_encryption_mask(). For now it is sufficient to know that SEV + * is active. + */ + movl $1, rva(sev_status)(%ebp) 1: +#endif /* Initialize Page tables to 0 */ leal rva(pgtable)(%ebx), %edi @@ -261,6 +271,9 @@ SYM_FUNC_START(startup_32) movl %esi, %edx 1: #endif + /* Check if the C-bit position is correct when SEV is active */ + call startup32_check_sev_cbit + pushl $__KERNEL_CS pushl %eax @@ -787,6 +800,78 @@ SYM_DATA_END(loaded_image_proto) #endif /* + * Check for the correct C-bit position when the startup_32 boot-path is used. + * + * The check makes use of the fact that all memory is encrypted when paging is + * disabled. The function creates 64 bits of random data using the RDRAND + * instruction. RDRAND is mandatory for SEV guests, so always available. If the + * hypervisor violates that the kernel will crash right here. + * + * The 64 bits of random data are stored to a memory location and at the same + * time kept in the %eax and %ebx registers. Since encryption is always active + * when paging is off the random data will be stored encrypted in main memory. + * + * Then paging is enabled. When the C-bit position is correct all memory is + * still mapped encrypted and comparing the register values with memory will + * succeed. An incorrect C-bit position will map all memory unencrypted, so that + * the compare will use the encrypted random data and fail. + */ + __HEAD + .code32 +SYM_FUNC_START(startup32_check_sev_cbit) +#ifdef CONFIG_AMD_MEM_ENCRYPT + pushl %eax + pushl %ebx + pushl %ecx + pushl %edx + + /* Check for non-zero sev_status */ + movl rva(sev_status)(%ebp), %eax + testl %eax, %eax + jz 4f + + /* + * Get two 32-bit random values - Don't bail out if RDRAND fails + * because it is better to prevent forward progress if no random value + * can be gathered. + */ +1: rdrand %eax + jnc 1b +2: rdrand %ebx + jnc 2b + + /* Store to memory and keep it in the registers */ + movl %eax, rva(sev_check_data)(%ebp) + movl %ebx, rva(sev_check_data+4)(%ebp) + + /* Enable paging to see if encryption is active */ + movl %cr0, %edx /* Backup %cr0 in %edx */ + movl $(X86_CR0_PG | X86_CR0_PE), %ecx /* Enable Paging and Protected mode */ + movl %ecx, %cr0 + + cmpl %eax, rva(sev_check_data)(%ebp) + jne 3f + cmpl %ebx, rva(sev_check_data+4)(%ebp) + jne 3f + + movl %edx, %cr0 /* Restore previous %cr0 */ + + jmp 4f + +3: /* Check failed - hlt the machine */ + hlt + jmp 3b + +4: + popl %edx + popl %ecx + popl %ebx + popl %eax +#endif + ret +SYM_FUNC_END(startup32_check_sev_cbit) + +/* * Stack and heap for uncompression */ .bss