Skip to contentSkip to conversation

use crc32 instead of md5 for hibernation e820 integrity check

PATCH Superseded Chris von Recklinghausen 2 participants 3 messages Apr 13, 2021

Subsystems (3)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 use crc32 instead of md5 for hibernation e820 integrity check

Suspend fails on a system in fips mode because md5 is used for the e820 integrity check and is not available. Use crc32 instead.

This patch changes the integrity check algorithm from md5 to crc32.

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.

---
v1 -> v2
bump up RESTORE_MAGIC
v2 -> v3
move embelishment from cover letter to commit comments (no code change)
v3 -> v4
add note to comments that md5 isn't used for encryption here.
v4 -> v5
reword comment per Simo's suggestion
v5 -> v6
use wording from Eric Biggers, use crc32_le instead of crc32 from crypto
framework (crc32_le is in the core API and removes need for #defines)
v6 -> v7
reword with input from Eric/Ard/Simo, code changed per Eric's feedback

 arch/x86/power/hibernate.c | 87 +++++++++-----------------------------
 1 file changed, 21 insertions(+), 66 deletions(-)
arch/x86/power/hibernate.c +21 -66
index cd3914fc9f3d..0c42400d30b3 100644
@@ -14,7 +14,8 @@
 #include <linux/cpu.h>
 #include <linux/pgtable.h>
 
-#include <crypto/hash.h>
+#include <linux/types.h>
+#include <linux/crc32.h>
 
 #include <asm/e820/api.h>
 #include <asm/init.h>
@@ -55,94 +56,47 @@ int pfn_is_nosave(unsigned long 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_digest;
 };
 
-#if IS_BUILTIN(CONFIG_CRYPTO_MD5)
 /**
- * get_e820_md5 - calculate md5 according to given e820 table
+ * get_e820_crc32 - calculate crc32 according to given e820 table
  *
  * @table: the e820 table to be calculated
- * @buf: the md5 result to be stored to
+ * @buf: the crc32 result to be stored to
  */
-static int get_e820_md5(struct e820_table *table, void *buf)
+static inline void get_e820_crc32(struct e820_table *table, void *buf)
 {
-	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;
+	u32 ret;
 
-	if (crypto_shash_digest(desc, (u8 *)table, size, buf))
-		ret = -EINVAL;
-
-	kfree_sensitive(desc);
-
-free_tfm:
-	crypto_free_shash(tfm);
-	return ret;
+	ret = ~crc32_le(~0, (unsigned char const *)table, size);
+	*(unsigned long *)buf = ret;
 }
 
-static int hibernation_e820_save(void *buf)
+static inline void hibernation_e820_save(void *buf)
 {
-	return get_e820_md5(e820_table_firmware, buf);
+	get_e820_crc32(e820_table_firmware, buf);
 }
 
-static bool hibernation_e820_mismatch(void *buf)
+static inline bool hibernation_e820_mismatch(void *buf)
 {
-	int ret;
-	u8 result[MD5_DIGEST_SIZE];
+	unsigned long result;
 
-	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;
+	get_e820_crc32(e820_table_firmware, &result);
+	return result != *(unsigned long *)buf;
 }
 
-static bool hibernation_e820_mismatch(void *buf)
-{
-	/* If md5 is not builtin for restore kernel, let it go. */
-	return false;
-}
-#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 +133,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);
+	hibernation_e820_save(&rdr->e820_digest);
+	return 0;
 }
 
 /**
@@ -200,7 +155,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 (hibernation_e820_mismatch(&rdr->e820_digest)) {
 		pr_crit("Hibernate inconsistent memory map detected!\n");
 		return -ENODEV;
 	}
Signature · 1 line
2.18.1
#1 lore raw
Quoted 2 lines · Chris von Recklinghausen

+static inline void get_e820_crc32(struct e820_table *table, void *buf)
{

This should just return the CRC-32 value as a u32. There's no need for the 'void *buf' argument.

Also like I said, compute_e820_crc32() would be a more logical name.

arch/x86/power/hibernate.c int arch_hibernation_header_save(void *addr, unsigned int max_size) · line 137

Quoted 7 lines
arch/x86/power/hibernate.c +2 -1
@@ -179,7 +133,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);
+	hibernation_e820_save(&rdr->e820_digest);
+	return 0;

Like I said, hibernation_e820_save() should just be inlined into here:

	rdr->e820_digest = compute_e820_crc32(e820_table_firmware)

Having the helper function doesn't add anything.

arch/x86/power/hibernate.c int arch_hibernation_header_restore(void *addr) · line 158

Quoted 7 lines
arch/x86/power/hibernate.c +1 -1
 /**
@@ -200,7 +155,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 (hibernation_e820_mismatch(&rdr->e820_digest)) {

Likewise, this should be just

	if (rdr->e820_digest != compute_e820_crc32(e820_table_firmware)) {

- Eric

#2 lore raw
In reply to Chris von Recklinghausen
Quoted 1 line · Chris von Recklinghausen

Suspend fails on a system in fips mode because md5 is used for the e820

Suspend to disk (hibernation), or any suspend?

arch/x86/power/hibernate.c int pfn_is_nosave(unsigned long pfn) · line 65

Quoted 8 lines

struct restore_data_record {

arch/x86/power/hibernate.c +1 -1
 	unsigned long jump_address;
 	unsigned long jump_address_phys;
 	unsigned long cr3;
 	unsigned long magic;
-	u8 e820_digest[MD5_DIGEST_SIZE];
+	unsigned long e820_digest;
 };

This field should probably be renamed to 'e820_crc' or 'e820_checksum', since "digest" normally means a cryptographic digest.

- Eric