Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4724732pxj; Wed, 12 May 2021 11:46:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDK4CJqAzIs2LNqoX2sGm4HZy780UVmxifAUgJx3SdeMxI7AlVznDrqmlnWiXldlnsD9Qw X-Received: by 2002:aa7:c655:: with SMTP id z21mr45147463edr.283.1620845208083; Wed, 12 May 2021 11:46:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620845208; cv=none; d=google.com; s=arc-20160816; b=RWwSQd/kguhLV02nCteaWmvtWUUEfdHGwgoNIIJGsD4VVSXojFeyzISIdiekIUO/IQ eQ+POUl/VFklsZQzMUtDODE7hx0hSOq4zPlbARKMISvZVtgxPZMbqr4zxYuhgV1KSuQh APvWDxhkEtX8T99PwLr877OG+QBq9m4Y+9ZR3F2gl1HrCOJlDPZLdmIjC/WxL8xJL6ol sa2dKA3fHY2CvJ4j5LktXBPOvUYLOZ/22ZPxAcTOSKx6YB+xrxXzOXK0KAPKsfRbX6bD L573PBAYYAzlztdVTuu2DVxrU2m5fJfhEU+8Ha4XItq8k8NEoXD8yKe/Nw4LRP9/ol/V 3YMQ== 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=5mCp2ZDjBFiBGEROH4uTg27Sujk1ebvP/y/la5fCMCI=; b=TySK06bPEsbFQaErAJli58A45z19hecU12/l/15zVj9crdR5f1ctAqxdtJcjjb76vG uoS7lZRth3eVHG10djarMaKxkKPTBQrgZBX4qRH0DLpYk7BZF5h3Q5sANQVQdqzSad0s n44OcO639BiMG04WlG7finkNlLO8XAAZhCYMlNZPbSeXYhAFvgy8QzD644eJ4/Pbzd50 Aj6DGARwr/0gBUPy/2N5vR6Xh3+SkCCUzos4d1LpPDE2wqlMb/KoJ98ge1ThI4yUV8St 6hVwSvodQc1oOC06IWcyMpr1h2M9YulPinGBoQzqyP5abbvBKuT6hOCg0ocAJnjIWu7k Uuiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tp79N3eB; 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 u8si485617edq.102.2021.05.12.11.46.24; Wed, 12 May 2021 11:46:48 -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=tp79N3eB; 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 S1357729AbhELSjK (ORCPT + 99 others); Wed, 12 May 2021 14:39:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:57062 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239454AbhELQjO (ORCPT ); Wed, 12 May 2021 12:39:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 002D961E2B; Wed, 12 May 2021 16:03:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620835410; bh=zCWNzPnSjRIm15SvtBgEAAkQMnFavHcUKs4jMKgd/Jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tp79N3eBvv7ZwJnoFTnQi676zMIvJo7A5aNLDEZEvC6n8Z6/MhuRUKOdwzBFLGxyX 0p6D/rqBrAV0I2jpFKcF/BgK3DmK5OO/o7il8sZyoWUx9giQM52mVZZ16RV874uStQ 4h/AcbQvVpjZRlhS7bQscftvIfw5kOMSr0DWFkTM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Dexuan Cui , Chris von Recklinghausen , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.12 332/677] PM: hibernate: x86: Use crc32 instead of md5 for hibernation e820 integrity check Date: Wed, 12 May 2021 16:46:18 +0200 Message-Id: <20210512144848.297389097@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@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: Chris von Recklinghausen [ Upstream commit f5d1499ae2096d7ea301023c4cc54e427300eb0a ] Hibernation fails on a system in fips mode because md5 is used for the e820 integrity check and is not available. Use crc32 instead. The check is intended to detect whether the E820 memory map provided by the firmware after cold boot unexpectedly differs from the one that was in use when the hibernation image was created. In this case, the hibernation image cannot be restored, as it may cover memory regions that are no longer available to the OS. A non-cryptographic checksum such as CRC-32 is sufficient to detect such inadvertent deviations. Fixes: 62a03defeabd ("PM / hibernate: Verify the consistent of e820 memory map by md5 digest") Reviewed-by: Eric Biggers Tested-by: Dexuan Cui Reviewed-by: Dexuan Cui Signed-off-by: Chris von Recklinghausen [ rjw: Subject edit ] Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- arch/x86/kernel/e820.c | 4 +- arch/x86/power/hibernate.c | 89 ++++++-------------------------------- 2 files changed, 16 insertions(+), 77 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 22aad412f965..629c4994f165 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -31,8 +31,8 @@ * - inform the user about the firmware's notion of memory layout * via /sys/firmware/memmap * - * - the hibernation code uses it to generate a kernel-independent MD5 - * fingerprint of the physical memory layout of a system. + * - the hibernation code uses it to generate a kernel-independent CRC32 + * checksum of the physical memory layout of a system. * * - 'e820_table_kexec': a slightly modified (by the kernel) firmware version * passed to us by the bootloader - the major difference between diff --git a/arch/x86/power/hibernate.c b/arch/x86/power/hibernate.c index cd3914fc9f3d..e94e0050a583 100644 --- a/arch/x86/power/hibernate.c +++ b/arch/x86/power/hibernate.c @@ -13,8 +13,8 @@ #include #include #include - -#include +#include +#include #include #include @@ -54,95 +54,33 @@ int pfn_is_nosave(unsigned long pfn) return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn; } - -#define MD5_DIGEST_SIZE 16 - struct restore_data_record { unsigned long jump_address; unsigned long jump_address_phys; unsigned long cr3; unsigned long magic; - u8 e820_digest[MD5_DIGEST_SIZE]; + unsigned long e820_checksum; }; -#if IS_BUILTIN(CONFIG_CRYPTO_MD5) /** - * get_e820_md5 - calculate md5 according to given e820 table + * compute_e820_crc32 - calculate crc32 of a given e820 table * * @table: the e820 table to be calculated - * @buf: the md5 result to be stored to + * + * Return: the resulting checksum */ -static int get_e820_md5(struct e820_table *table, void *buf) +static inline u32 compute_e820_crc32(struct e820_table *table) { - struct crypto_shash *tfm; - struct shash_desc *desc; - int size; - int ret = 0; - - tfm = crypto_alloc_shash("md5", 0, 0); - if (IS_ERR(tfm)) - return -ENOMEM; - - desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm), - GFP_KERNEL); - if (!desc) { - ret = -ENOMEM; - goto free_tfm; - } - - desc->tfm = tfm; - - size = offsetof(struct e820_table, entries) + + int size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry) * table->nr_entries; - if (crypto_shash_digest(desc, (u8 *)table, size, buf)) - ret = -EINVAL; - - kfree_sensitive(desc); - -free_tfm: - crypto_free_shash(tfm); - return ret; -} - -static int hibernation_e820_save(void *buf) -{ - return get_e820_md5(e820_table_firmware, buf); -} - -static bool hibernation_e820_mismatch(void *buf) -{ - int ret; - u8 result[MD5_DIGEST_SIZE]; - - memset(result, 0, MD5_DIGEST_SIZE); - /* If there is no digest in suspend kernel, let it go. */ - if (!memcmp(result, buf, MD5_DIGEST_SIZE)) - return false; - - ret = get_e820_md5(e820_table_firmware, result); - if (ret) - return true; - - return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false; -} -#else -static int hibernation_e820_save(void *buf) -{ - return 0; -} - -static bool hibernation_e820_mismatch(void *buf) -{ - /* If md5 is not builtin for restore kernel, let it go. */ - return false; + return ~crc32_le(~0, (unsigned char const *)table, size); } -#endif #ifdef CONFIG_X86_64 -#define RESTORE_MAGIC 0x23456789ABCDEF01UL +#define RESTORE_MAGIC 0x23456789ABCDEF02UL #else -#define RESTORE_MAGIC 0x12345678UL +#define RESTORE_MAGIC 0x12345679UL #endif /** @@ -179,7 +117,8 @@ int arch_hibernation_header_save(void *addr, unsigned int max_size) */ rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK; - return hibernation_e820_save(rdr->e820_digest); + rdr->e820_checksum = compute_e820_crc32(e820_table_firmware); + return 0; } /** @@ -200,7 +139,7 @@ int arch_hibernation_header_restore(void *addr) jump_address_phys = rdr->jump_address_phys; restore_cr3 = rdr->cr3; - if (hibernation_e820_mismatch(rdr->e820_digest)) { + if (rdr->e820_checksum != compute_e820_crc32(e820_table_firmware)) { pr_crit("Hibernate inconsistent memory map detected!\n"); return -ENODEV; } -- 2.30.2