Skip to contentSkip to conversation

let kexec_file_load use platform keyring to verify the kernel image

RFC PATCH Superseded Kairui Song 2 participants 8 messages Jan 15, 2019

Subsystems (10)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover let kexec_file_load use platform keyring to verify the kernel image

Hi,

This patch series adds a .platform_trusted_keys in system_keyring as the reference to .platform keyring in integrity subsystem, when platform keyring is being initialized it will be updated. So other component could use this keyring as well.

This patch series also let kexec_file_load use platform keyring as fall back if it failed to verify the image against secondary keyring, make it possible to load kernel signed by third part key if third party key is imported in the firmware.

After this patch kexec_file_load will be able to verify a signed PE bzImage using keys in platform keyring.

Tested in a VM with locally signed kernel with pesign and imported the cert to EFI's MokList variable.

Kairui Song (2):
integrity, KEYS: add a reference to platform keyring kexec, KEYS: Make use of platform keyring for signature verify

Update from V1:
- Make platform_trusted_keys static, and update commit message as suggested by Mimi Zohar
- Always check if platform keyring is initialized before use it

 arch/x86/kernel/kexec-bzimage64.c | 15 ++++++++++++---
 certs/system_keyring.c            | 20 +++++++++++++++++++-
 include/keys/system_keyring.h     |  5 +++++
 include/linux/verification.h      |  3 +++
 security/integrity/digsig.c       |  6 ++++++
 5 files changed, 45 insertions(+), 4 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

RFC PATCH v2 1/2 integrity, KEYS: add a reference to platform keyring

Currently when loading new kernel via kexec_file_load syscall, it is able to verify the signed PE bzimage against .builtin_trusted_keys or .secondary_trusted_keys. But the image could be signed with third part keys which will be provided by platform or firmware as EFI variable (eg. stored in MokListRT EFI variable), and the keys won't be available in keyrings mentioned above.

After commit 9dc92c45177a ('integrity: Define a trusted platform keyring') a .platform keyring is introduced to store the keys provided by platform or firmware, this keyring is intended to be used for verifying kernel images being loaded by kexec_file_load syscall. And with a few following up commits, keys provided by firmware is being loaded into this keyring, and IMA-appraisal is able to use the keyring to verify kernel images. IMA is the currently the only user of that keyring.

This patch exposes the .platform, and makes it useable for other components. For example, kexec_file_load could use this .platform keyring to verify the kernel image's image.

---
 certs/system_keyring.c        | 9 +++++++++
 include/keys/system_keyring.h | 5 +++++
 security/integrity/digsig.c   | 6 ++++++
 3 files changed, 20 insertions(+)
certs/system_keyring.c +9 -0
index 81728717523d..4690ef9cda8a 100644
@@ -24,6 +24,9 @@ static struct key *builtin_trusted_keys;
 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
 static struct key *secondary_trusted_keys;
 #endif
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+static struct key *platform_trusted_keys;
+#endif
 
 extern __initconst const u8 system_certificate_list[];
 extern __initconst const unsigned long system_certificate_list_size;
@@ -265,4 +268,10 @@ int verify_pkcs7_signature(const void *data, size_t len,
 }
 EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+void __init set_platform_trusted_keys(struct key *keyring) {
+	platform_trusted_keys = keyring;
+}
+#endif
+
 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
include/keys/system_keyring.h +5 -0
index 359c2f936004..9e1b7849b6aa 100644
@@ -61,5 +61,10 @@ static inline struct key *get_ima_blacklist_keyring(void)
 }
 #endif /* CONFIG_IMA_BLACKLIST_KEYRING */
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+
+extern void __init set_platform_trusted_keys(struct key* keyring);
+
+#endif /* CONFIG_INTEGRITY_PLATFORM_KEYRING */
 
 #endif /* _KEYS_SYSTEM_KEYRING_H */
security/integrity/digsig.c +6 -0
index f45d6edecf99..bfabc2a8111d 100644
@@ -89,6 +89,12 @@ static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
 		keyring[id] = NULL;
 	}
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+	if (id = INTEGRITY_KEYRING_PLATFORM) {
+		set_platform_trusted_keys(keyring[id]);
+	}
+#endif
+
 	return err;
 }
 
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [RFC PATCH v2 1/2] integrity, KEYS: add a reference to platform keyring

On Tue, 2019-01-15 at 17:45 +0800, Kairui Song wrote:
[snip]

Quoted 7 lines
security/integrity/digsig.c +0 -0
index f45d6edecf99..bfabc2a8111d 100644
@@ -89,6 +89,12 @@ static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
 		keyring[id] = NULL;
 	}

security/integrity/digsig.c static int __integrity_init_keyring(const unsigned int id, key_perm_t perm, · line 100

Quoted 10 lines
security/integrity/digsig.c +6 -0
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+	if (id = INTEGRITY_KEYRING_PLATFORM) {
+		set_platform_trusted_keys(keyring[id]);
+	}
+#endif
+
 	return err;
 }
 

Any reason for setting it here as opposed to in the caller platform_keyring_init()?

Mimi

#3 lore raw

Re: [RFC PATCH v2 1/2] integrity, KEYS: add a reference to platform keyring

Quoted 25 lines · Mimi Zohar

On Tue, 2019-01-15 at 17:45 +0800, Kairui Song wrote:
[snip]

Quoted 7 lines
security/integrity/digsig.c +0 -0
index f45d6edecf99..bfabc2a8111d 100644
@@ -89,6 +89,12 @@ static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
              keyring[id] = NULL;
      }

security/integrity/digsig.c static int __integrity_init_keyring(const unsigned int id, key_perm_t perm, · line 100

Quoted 10 lines
security/integrity/digsig.c +6 -0
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+     if (id = INTEGRITY_KEYRING_PLATFORM) {
+             set_platform_trusted_keys(keyring[id]);
+     }
+#endif
+
      return err;
 }
 

Any reason for setting it here as opposed to in the caller platform_keyring_init()?

Quoted 2 lines

Mimi

Yes, "keyring" is static so unless I expose it to other files, it is only accessible here. And I think there should be no problem to put the set_platform_trusted_keys here.

Signature · 2 lines
Best Regards,
Kairui Song
maintainer
#4 lore raw

Re: [RFC PATCH v2 1/2] integrity, KEYS: add a reference to platform keyring

Quoted 26 lines · Kairui Song
Quoted 25 lines · Mimi Zohar

On Tue, 2019-01-15 at 17:45 +0800, Kairui Song wrote:
[snip]

Quoted 7 lines
security/integrity/digsig.c +0 -0
index f45d6edecf99..bfabc2a8111d 100644
@@ -89,6 +89,12 @@ static int __integrity_init_keyring(const unsigned int id, key_perm_t perm,
              keyring[id] = NULL;
      }

security/integrity/digsig.c static int __integrity_init_keyring(const unsigned int id, key_perm_t perm, · line 100

Quoted 10 lines
security/integrity/digsig.c +6 -0
 
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+     if (id = INTEGRITY_KEYRING_PLATFORM) {
+             set_platform_trusted_keys(keyring[id]);
+     }
+#endif
+
      return err;
 }
 

Any reason for setting it here as opposed to in the caller platform_keyring_init()?

Quoted 6 lines
Quoted 2 lines

Mimi

Yes, "keyring" is static so unless I expose it to other files, it is only accessible here. And I think there should be no problem to put the set_platform_trusted_keys here.

Right, that's a really good reason.

Mimi

#5 lore raw

RFC PATCH v2 2/2 kexec, KEYS: Make use of platform keyring for signature verify

In reply to Kairui Song

With KEXEC_BZIMAGE_VERIFY_SIG enabled, kexec_file_load will need to verify the kernel image. The image might be signed with third part keys, and the keys could be stored in firmware, then got loaded into the .platform keyring. Now we have a symbol .platform_trusted_keyring as the reference to .platform keyring, this patch makes use if it and allow kexec_file_load to verify the image against keys in .platform keyring.

This commit adds a VERIFY_USE_PLATFORM_KEYRING similar to previous VERIFY_USE_SECONDARY_KEYRING indicating that verify_pkcs7_signature should verify the signature using platform keyring. Also, decrease the error message log level when verification failed with -ENOKEY, so that if called tried multiple time with different keyring it won't generate extra noises.

---
 arch/x86/kernel/kexec-bzimage64.c | 15 ++++++++++++---
 certs/system_keyring.c            | 11 ++++++++++-
 include/linux/verification.h      |  3 +++
 3 files changed, 25 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +12 -3
index 7d97e432cbbc..a06b04065bb1 100644
@@ -534,9 +534,18 @@ static int bzImage64_cleanup(void *loader_data)
 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
 {
-	return verify_pefile_signature(kernel, kernel_len,
-				       VERIFY_USE_SECONDARY_KEYRING,
-				       VERIFYING_KEXEC_PE_SIGNATURE);
+	int ret;
+	ret = verify_pefile_signature(kernel, kernel_len,
+			VERIFY_USE_SECONDARY_KEYRING,
+			VERIFYING_KEXEC_PE_SIGNATURE);
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+	if (ret = -ENOKEY) {
+		ret = verify_pefile_signature(kernel, kernel_len,
+				VERIFY_USE_PLATFORM_KEYRING,
+				VERIFYING_KEXEC_PE_SIGNATURE);
+	}
+#endif
+	return ret;
 }
 #endif
 
certs/system_keyring.c +10 -1
index 4690ef9cda8a..df2a09599bd8 100644
@@ -239,12 +239,21 @@ int verify_pkcs7_signature(const void *data, size_t len,
 		trusted_keys = secondary_trusted_keys;
 #else
 		trusted_keys = builtin_trusted_keys;
+#endif
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+	} else if (trusted_keys = VERIFY_USE_PLATFORM_KEYRING) {
+		trusted_keys = platform_trusted_keys;
+		if (!trusted_keys) {
+			ret = -ENOKEY;
+			pr_devel("PKCS#7 platform keyring is not available\n");
+			goto error;
+		}
 #endif
 	}
 	ret = pkcs7_validate_trust(pkcs7, trusted_keys);
 	if (ret < 0) {
 		if (ret = -ENOKEY)
-			pr_err("PKCS#7 signature not signed with a trusted key\n");
+			pr_devel("PKCS#7 signature not signed with a trusted key\n");
 		goto error;
 	}
 
include/linux/verification.h +3 -0
index cfa4730d607a..dbd1686e9bc4 100644
@@ -17,6 +17,9 @@
  * should be used.
  */
 #define VERIFY_USE_SECONDARY_KEYRING ((struct key *)1UL)
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
+#define VERIFY_USE_PLATFORM_KEYRING  ((struct key *)2UL)
+#endif
 
 /*
  * The use to which an asymmetric key is being put.
Signature · 1 line
2.20.1
maintainer
#6 lore raw

Re: [RFC PATCH v2 2/2] kexec, KEYS: Make use of platform keyring for signature verify

Quoted 6 lines · Kairui Song
arch/x86/kernel/kexec-bzimage64.c +0 -0
index 7d97e432cbbc..a06b04065bb1 100644
@@ -534,9 +534,18 @@ static int bzImage64_cleanup(void *loader_data)
 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG

arch/x86/kernel/kexec-bzimage64.c static int bzImage64_cleanup(void *loader_data) · line 541

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c +5 -3
 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
 {
-	return verify_pefile_signature(kernel, kernel_len,
-				       VERIFY_USE_SECONDARY_KEYRING,
-				       VERIFYING_KEXEC_PE_SIGNATURE);
+	int ret;
+	ret = verify_pefile_signature(kernel, kernel_len,
+			VERIFY_USE_SECONDARY_KEYRING,
+			VERIFYING_KEXEC_PE_SIGNATURE);
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING

Consider using IS_ENABLED() or IS_BUILTIN().

Mimi

Quoted 9 lines
arch/x86/kernel/kexec-bzimage64.c +7 -0
+	if (ret = -ENOKEY) {
+		ret = verify_pefile_signature(kernel, kernel_len,
+				VERIFY_USE_PLATFORM_KEYRING,
+				VERIFYING_KEXEC_PE_SIGNATURE);
+	}
+#endif
+	return ret;
 }
 #endif
#7 lore raw

Re: [RFC PATCH v2 2/2] kexec, KEYS: Make use of platform keyring for signature verify

Quoted 22 lines · Mimi Zohar
Quoted 6 lines · Kairui Song
arch/x86/kernel/kexec-bzimage64.c +0 -0
index 7d97e432cbbc..a06b04065bb1 100644
@@ -534,9 +534,18 @@ static int bzImage64_cleanup(void *loader_data)
 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG

arch/x86/kernel/kexec-bzimage64.c static int bzImage64_cleanup(void *loader_data) · line 541

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c +5 -3
 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
 {
-     return verify_pefile_signature(kernel, kernel_len,
-                                    VERIFY_USE_SECONDARY_KEYRING,
-                                    VERIFYING_KEXEC_PE_SIGNATURE);
+     int ret;
+     ret = verify_pefile_signature(kernel, kernel_len,
+                     VERIFY_USE_SECONDARY_KEYRING,
+                     VERIFYING_KEXEC_PE_SIGNATURE);
+#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING

Consider using IS_ENABLED() or IS_BUILTIN().

Quoted 1 line

Mimi

Thanks for the suggestion, will update the patch later if there are no other comments.

Quoted 11 lines

arch/x86/kernel/kexec-bzimage64.c static int bzImage64_cleanup(void *loader_data) · line 550

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c +7 -0
+     if (ret = -ENOKEY) {
+             ret = verify_pefile_signature(kernel, kernel_len,
+                             VERIFY_USE_PLATFORM_KEYRING,
+                             VERIFYING_KEXEC_PE_SIGNATURE);
+     }
+#endif
+     return ret;
 }
 #endif

--
Best Regards,
Kairui Song