Skip to contentSkip to conversation

use more system keyrings to verify arm64 kdump kernel image signature

PATCH Superseded Coiby Xu 3 participants 15 messages Apr 1, 2022 – Apr 14, 2022

Subsystems (5)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

v5:
- improve commit message [Baoquan]

v4:
- fix commit reference format issue and other checkpatch.pl warnings [Baoquan]

v3:
- s/arch_kexec_kernel_verify_pe_sig/kexec_kernel_verify_pe_sig [Eric]
- clean up arch_kexec_kernel_verify_sig [Eric]

v2:
- only x86_64 and arm64 need to enable PE file signature check [Dave]

Coiby Xu (3):
kexec: clean up arch_kexec_kernel_verify_sig
kexec, KEYS: make the code in bzImage64_verify_sig generic
arm64: kexec_file: use more system keyrings to verify kernel image
signature

 arch/arm64/kernel/kexec_image.c   |  4 +--
 arch/x86/kernel/kexec-bzimage64.c | 13 +-------
 include/linux/kexec.h             |  7 +++--
 kernel/kexec_file.c               | 51 ++++++++++++++++++-------------
 4 files changed, 37 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v5 1/3 kexec: clean up arch_kexec_kernel_verify_sig

Currently there is no arch-specific implementation of arch_kexec_kernel_verify_sig. Even if we want to add an implementation for an architecture in the future, we can simply use "(struct kexec_file_ops*)->verify_sig". So clean it up.

---
 include/linux/kexec.h |  4 ----
 kernel/kexec_file.c   | 34 +++++++++++++---------------------
 2 files changed, 13 insertions(+), 25 deletions(-)
include/linux/kexec.h +0 -4
index 0c994ae37729..755fed183224 100644
@@ -196,10 +196,6 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi,
 				 const Elf_Shdr *relsec,
 				 const Elf_Shdr *symtab);
 int arch_kimage_file_post_load_cleanup(struct kimage *image);
-#ifdef CONFIG_KEXEC_SIG
-int arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
-				 unsigned long buf_len);
-#endif
 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
kernel/kexec_file.c +13 -21
index 8347fc158d2b..3720435807eb 100644
@@ -89,25 +89,6 @@ int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
 	return kexec_image_post_load_cleanup_default(image);
 }
 
-#ifdef CONFIG_KEXEC_SIG
-static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
-					  unsigned long buf_len)
-{
-	if (!image->fops || !image->fops->verify_sig) {
-		pr_debug("kernel loader does not support signature verification.\n");
-		return -EKEYREJECTED;
-	}
-
-	return image->fops->verify_sig(buf, buf_len);
-}
-
-int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
-					unsigned long buf_len)
-{
-	return kexec_image_verify_sig_default(image, buf, buf_len);
-}
-#endif
-
 /*
  * arch_kexec_apply_relocations_add - apply relocations of type RELA
  * @pi:		Purgatory to be relocated.
@@ -184,13 +165,24 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 }
 
 #ifdef CONFIG_KEXEC_SIG
+static int kexec_image_verify_sig(struct kimage *image, void *buf,
+		unsigned long buf_len)
+{
+	if (!image->fops || !image->fops->verify_sig) {
+		pr_debug("kernel loader does not support signature verification.\n");
+		return -EKEYREJECTED;
+	}
+
+	return image->fops->verify_sig(buf, buf_len);
+}
+
 static int
 kimage_validate_signature(struct kimage *image)
 {
 	int ret;
 
-	ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
-					   image->kernel_buf_len);
+	ret = kexec_image_verify_sig(image, image->kernel_buf,
+			image->kernel_buf_len);
 	if (ret) {
 
 		if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v5 2/3 kexec, KEYS: make the code in bzImage64_verify_sig generic

In reply to Coiby Xu

The code in bzImage64_verify_sig could make use of system keyrings including .buitin_trusted_keys, .secondary_trusted_keys and .platform keyring to verify signed kernel image as PE file. Make it generic so both x86_64 and arm64 can use it.

---
 arch/x86/kernel/kexec-bzimage64.c | 13 +------------
 include/linux/kexec.h             |  7 +++++++
 kernel/kexec_file.c               | 17 +++++++++++++++++
 3 files changed, 25 insertions(+), 12 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +1 -12
index 170d0fd68b1f..f73aab3fde33 100644
@@ -17,7 +17,6 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/efi.h>
-#include <linux/verification.h>
 
 #include <asm/bootparam.h>
 #include <asm/setup.h>
@@ -531,17 +530,7 @@ 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)
 {
-	int ret;
-
-	ret = verify_pefile_signature(kernel, kernel_len,
-				      VERIFY_USE_SECONDARY_KEYRING,
-				      VERIFYING_KEXEC_PE_SIGNATURE);
-	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
-		ret = verify_pefile_signature(kernel, kernel_len,
-					      VERIFY_USE_PLATFORM_KEYRING,
-					      VERIFYING_KEXEC_PE_SIGNATURE);
-	}
-	return ret;
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }
 #endif
 
include/linux/kexec.h +7 -0
index 755fed183224..2fe39e946988 100644
@@ -19,6 +19,7 @@
 #include <asm/io.h>
 
 #include <uapi/linux/kexec.h>
+#include <linux/verification.h>
 
 #ifdef CONFIG_KEXEC_CORE
 #include <linux/list.h>
@@ -196,6 +197,12 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi,
 				 const Elf_Shdr *relsec,
 				 const Elf_Shdr *symtab);
 int arch_kimage_file_post_load_cleanup(struct kimage *image);
+#ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel,
+				    unsigned long kernel_len);
+#endif
+#endif
 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
kernel/kexec_file.c +17 -0
index 3720435807eb..754885b96aab 100644
@@ -165,6 +165,23 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 }
 
 #ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
+{
+	int ret;
+
+	ret = verify_pefile_signature(kernel, kernel_len,
+				      VERIFY_USE_SECONDARY_KEYRING,
+				      VERIFYING_KEXEC_PE_SIGNATURE);
+	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
+		ret = verify_pefile_signature(kernel, kernel_len,
+					      VERIFY_USE_PLATFORM_KEYRING,
+					      VERIFYING_KEXEC_PE_SIGNATURE);
+	}
+	return ret;
+}
+#endif
+
 static int kexec_image_verify_sig(struct kimage *image, void *buf,
 		unsigned long buf_len)
 {
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v5 3/3 arm64: kexec_file: use more system keyrings to verify kernel image signature

In reply to Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This patch allows to verify arm64 kernel image signature using not only .builtin_trusted_keys but also .platform and .secondary_trusted_key keyring.

---
 arch/arm64/kernel/kexec_image.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm64/kernel/kexec_image.c +1 -3
index 9ec34690e255..51af1c22d6da 100644
@@ -14,7 +14,6 @@
 #include <linux/kexec.h>
 #include <linux/pe.h>
 #include <linux/string.h>
-#include <linux/verification.h>
 #include <asm/byteorder.h>
 #include <asm/cpufeature.h>
 #include <asm/image.h>
@@ -133,8 +132,7 @@ static void *image_load(struct kimage *image,
 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
 static int image_verify_sig(const char *kernel, unsigned long kernel_len)
 {
-	return verify_pefile_signature(kernel, kernel_len, NULL,
-				       VERIFYING_KEXEC_PE_SIGNATURE);
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }
 #endif
 
Signature · 1 line
2.34.1
#9 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

In reply to Coiby Xu

Hi Coiby,

Quoted 7 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

Meanwhile, I noticed Michal also posted a patchset to address the same issue, while he tries to make it work on s390 too. Could you check and consider if these two patches can be integrated?

[PATCH 0/4] Unifrom keyring support across architectures and functions
https://lore.kernel.org/lkml/cover.1644953683.git.msuchanek at suse.de/

Thanks
Baoquan

Quoted 29 lines

v5:
- improve commit message [Baoquan]

v4:
- fix commit reference format issue and other checkpatch.pl warnings [Baoquan]

v3:
- s/arch_kexec_kernel_verify_pe_sig/kexec_kernel_verify_pe_sig [Eric]
- clean up arch_kexec_kernel_verify_sig [Eric]

v2:
- only x86_64 and arm64 need to enable PE file signature check [Dave]

Coiby Xu (3):
kexec: clean up arch_kexec_kernel_verify_sig
kexec, KEYS: make the code in bzImage64_verify_sig generic
arm64: kexec_file: use more system keyrings to verify kernel image
signature

 arch/arm64/kernel/kexec_image.c   |  4 +--
 arch/x86/kernel/kexec-bzimage64.c | 13 +-------
 include/linux/kexec.h             |  7 +++--
 kernel/kexec_file.c               | 51 ++++++++++++++++++-------------
 4 files changed, 37 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
#10 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

Quoted 8 lines · Baoquan He

Hi Coiby,

Quoted 5 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

Quoted 5 lines
Quoted 2 lines

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

The reason is that MOK keys are (if anywhere) linked to the secondary keyring, and only primary keyring is used on arm64.

Thanks

Michal

Quoted 41 lines

Meanwhile, I noticed Michal also posted a patchset to address the same issue, while he tries to make it work on s390 too. Could you check and consider if these two patches can be integrated?

[PATCH 0/4] Unifrom keyring support across architectures and functions
https://lore.kernel.org/lkml/cover.1644953683.git.msuchanek at suse.de/

Thanks
Baoquan

Quoted 29 lines

v5:
- improve commit message [Baoquan]

v4:
- fix commit reference format issue and other checkpatch.pl warnings [Baoquan]

v3:
- s/arch_kexec_kernel_verify_pe_sig/kexec_kernel_verify_pe_sig [Eric]
- clean up arch_kexec_kernel_verify_sig [Eric]

v2:
- only x86_64 and arm64 need to enable PE file signature check [Dave]

Coiby Xu (3):
kexec: clean up arch_kexec_kernel_verify_sig
kexec, KEYS: make the code in bzImage64_verify_sig generic
arm64: kexec_file: use more system keyrings to verify kernel image
signature

 arch/arm64/kernel/kexec_image.c   |  4 +--
 arch/x86/kernel/kexec-bzimage64.c | 13 +-------
 include/linux/kexec.h             |  7 +++--
 kernel/kexec_file.c               | 51 ++++++++++++++++++-------------
 4 files changed, 37 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
#11 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

Quoted 9 lines · Michal Such?nek
Quoted 8 lines · Baoquan He

Hi Coiby,

Quoted 5 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

The reason is that MOK keys are (if anywhere) linked to the secondary keyring, and only primary keyring is used on arm64.

Thanks for explaining. This is valuable information and should be put into log for better understanding when reviewing or reading code later.

author
#12 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

Quoted 10 lines · Baoquan He
Quoted 9 lines · Michal Such?nek
Quoted 8 lines · Baoquan He

Hi Coiby,

Quoted 5 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

The reason is that MOK keys are (if anywhere) linked to the secondary

                                                                ^^^^^^^^^
                                                                platform?
Quoted 1 line
Quoted 1 line

keyring, and only primary keyring is used on arm64.

Thanks Michal for providing the info! Btw, I think you made a typo because MOK keys are linked to the platform keyring, right?

Quoted 4 lines

Thanks for explaining. This is valuable information and should be put into log for better understanding when reviewing or reading code later.

Thanks for the reminder! I'll include this info in the commit message.

Quoted 1 line
Signature · 2 lines
Best regards,
Coiby
#13 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

Quoted 17 lines · Coiby Xu
Quoted 16 lines · Baoquan He
Quoted 15 lines · Michal Such?nek
Quoted 13 lines · Baoquan He

Hi Coiby,

Quoted 7 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

The reason is that MOK keys are (if anywhere) linked to the secondary

                                                               ^^^^^^^^^
                                                               platform?
Quoted 1 line
Quoted 1 line

keyring, and only primary keyring is used on arm64.

Thanks Michal for providing the info! Btw, I think you made a typo because MOK keys are linked to the platform keyring, right?

No, I mean secondary, through this patchset:
https://lore.kernel.org/lkml/YhKP12KEmyqyS8rj at iki.fi/

Apparently support for importing the MOK keys into the platform keyring also exists but I am not sure if this is upstream or downstream feature.

At any rate the MOK keys are not included in the primary keyring which is the only keyring currently in use for kexec on arm64.

Thanks

Michal

Reviewed-by on v5 2/3
#4 lore raw

Re: [PATCH v5 2/3] kexec, KEYS: make the code in bzImage64_verify_sig generic

In reply to Coiby Xu
Quoted 1 line · Coiby Xu

The code in bzImage64_verify_sig could make use of system keyrings

s/could make/makes/

Quoted 30 lines

including .buitin_trusted_keys, .secondary_trusted_keys and .platform keyring to verify signed kernel image as PE file. Make it generic so both x86_64 and arm64 can use it.

---
 arch/x86/kernel/kexec-bzimage64.c | 13 +------------
 include/linux/kexec.h             |  7 +++++++
 kernel/kexec_file.c               | 17 +++++++++++++++++
 3 files changed, 25 insertions(+), 12 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +0 -4
index 170d0fd68b1f..f73aab3fde33 100644
@@ -17,7 +17,6 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/efi.h>
-#include <linux/verification.h>
 
 #include <asm/bootparam.h>
 #include <asm/setup.h>
@@ -531,17 +530,7 @@ 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)
 {
-	int ret;
-
-	ret = verify_pefile_signature(kernel, kernel_len,

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

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c +1 -8
-				      VERIFY_USE_SECONDARY_KEYRING,
-				      VERIFYING_KEXEC_PE_SIGNATURE);
-	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
-		ret = verify_pefile_signature(kernel, kernel_len,
-					      VERIFY_USE_PLATFORM_KEYRING,
-					      VERIFYING_KEXEC_PE_SIGNATURE);
-	}
-	return ret;
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }

Maybe you can completely eliminate bzImage64_verify_sig and directly assign kexec_kernel_verify_pe_sig to the fops?

Other than that

Quoted 58 lines
arch/x86/kernel/kexec-bzimage64.c +0 -0
 #endif
 
include/linux/kexec.h +7 -0
index 755fed183224..2fe39e946988 100644
@@ -19,6 +19,7 @@
 #include <asm/io.h>
 
 #include <uapi/linux/kexec.h>
+#include <linux/verification.h>
 
 #ifdef CONFIG_KEXEC_CORE
 #include <linux/list.h>
@@ -196,6 +197,12 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi,
 				 const Elf_Shdr *relsec,
 				 const Elf_Shdr *symtab);
 int arch_kimage_file_post_load_cleanup(struct kimage *image);
+#ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel,
+				    unsigned long kernel_len);
+#endif
+#endif
 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
kernel/kexec_file.c +17 -0
index 3720435807eb..754885b96aab 100644
@@ -165,6 +165,23 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 }
 
 #ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
+{
+	int ret;
+
+	ret = verify_pefile_signature(kernel, kernel_len,
+				      VERIFY_USE_SECONDARY_KEYRING,
+				      VERIFYING_KEXEC_PE_SIGNATURE);
+	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
+		ret = verify_pefile_signature(kernel, kernel_len,
+					      VERIFY_USE_PLATFORM_KEYRING,
+					      VERIFYING_KEXEC_PE_SIGNATURE);
+	}
+	return ret;
+}
+#endif
+
 static int kexec_image_verify_sig(struct kimage *image, void *buf,
 		unsigned long buf_len)
 {
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v5 3/3] arm64: kexec_file: use more system keyrings to verify kernel image signature

In reply to Coiby Xu
Quoted 4 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This is because image_verify_sig uses only the primary keyring that contains only kernel built-in keys to verify the kexec image.

Quoted 23 lines

This patch allows to verify arm64 kernel image signature using not only .builtin_trusted_keys but also .platform and .secondary_trusted_key keyring.

---
 arch/arm64/kernel/kexec_image.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm64/kernel/kexec_image.c +0 -1
index 9ec34690e255..51af1c22d6da 100644
@@ -14,7 +14,6 @@
 #include <linux/kexec.h>
 #include <linux/pe.h>
 #include <linux/string.h>
-#include <linux/verification.h>
 #include <asm/byteorder.h>
 #include <asm/cpufeature.h>
 #include <asm/image.h>

arch/arm64/kernel/kexec_image.c static void *image_load(struct kimage *image, · line 137

Quoted 8 lines
arch/arm64/kernel/kexec_image.c +1 -2
@@ -133,8 +132,7 @@ static void *image_load(struct kimage *image,
 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
 static int image_verify_sig(const char *kernel, unsigned long kernel_len)
 {
-	return verify_pefile_signature(kernel, kernel_len, NULL,
-				       VERIFYING_KEXEC_PE_SIGNATURE);
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }
 #endif

You can eliminate image_verify_sig here aswell and directly assign kexec_kernel_verify_pe_sig to fops.

Thanks

Michal

Reviewed-by on v5 1/3
#2 lore raw

Re: [PATCH v5 1/3] kexec: clean up arch_kexec_kernel_verify_sig

In reply to Coiby Xu
Quoted 4 lines · Coiby Xu

Currently there is no arch-specific implementation of arch_kexec_kernel_verify_sig. Even if we want to add an implementation for an architecture in the future, we can simply use "(struct kexec_file_ops*)->verify_sig". So clean it up.

Quoted 83 lines
---
 include/linux/kexec.h |  4 ----
 kernel/kexec_file.c   | 34 +++++++++++++---------------------
 2 files changed, 13 insertions(+), 25 deletions(-)
include/linux/kexec.h +0 -4
index 0c994ae37729..755fed183224 100644
@@ -196,10 +196,6 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi,
 				 const Elf_Shdr *relsec,
 				 const Elf_Shdr *symtab);
 int arch_kimage_file_post_load_cleanup(struct kimage *image);
-#ifdef CONFIG_KEXEC_SIG
-int arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
-				 unsigned long buf_len);
-#endif
 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
kernel/kexec_file.c +13 -21
index 8347fc158d2b..3720435807eb 100644
@@ -89,25 +89,6 @@ int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
 	return kexec_image_post_load_cleanup_default(image);
 }
 
-#ifdef CONFIG_KEXEC_SIG
-static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
-					  unsigned long buf_len)
-{
-	if (!image->fops || !image->fops->verify_sig) {
-		pr_debug("kernel loader does not support signature verification.\n");
-		return -EKEYREJECTED;
-	}
-
-	return image->fops->verify_sig(buf, buf_len);
-}
-
-int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
-					unsigned long buf_len)
-{
-	return kexec_image_verify_sig_default(image, buf, buf_len);
-}
-#endif
-
 /*
  * arch_kexec_apply_relocations_add - apply relocations of type RELA
  * @pi:		Purgatory to be relocated.
@@ -184,13 +165,24 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 }
 
 #ifdef CONFIG_KEXEC_SIG
+static int kexec_image_verify_sig(struct kimage *image, void *buf,
+		unsigned long buf_len)
+{
+	if (!image->fops || !image->fops->verify_sig) {
+		pr_debug("kernel loader does not support signature verification.\n");
+		return -EKEYREJECTED;
+	}
+
+	return image->fops->verify_sig(buf, buf_len);
+}
+
 static int
 kimage_validate_signature(struct kimage *image)
 {
 	int ret;
 
-	ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
-					   image->kernel_buf_len);
+	ret = kexec_image_verify_sig(image, image->kernel_buf,
+			image->kernel_buf_len);
 	if (ret) {
 
 		if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
Signature · 1 line
2.34.1
author
#14 lore raw

v5 cover use more system keyrings to verify arm64 kdump kernel image signature

In reply to Michal Suchánek
Quoted 21 lines · Michal Such?nek
Quoted 20 lines · Coiby Xu
Quoted 10 lines · Baoquan He
Quoted 9 lines · Michal Such?nek
Quoted 8 lines · Baoquan He

Hi Coiby,

Quoted 5 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This patch set allows arm64 to use more system keyrings to verify kdump kernel image signature by making the existing code in x64 public.

Thanks for updating. It would be great to tell why the problem is met, then allow arm64 to use more system keyrings can solve it.

The reason is that MOK keys are (if anywhere) linked to the secondary

                                                               ^^^^^^^^^
                                                               platform?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

keyring, and only primary keyring is used on arm64.

Thanks Michal for providing the info! Btw, I think you made a typo because MOK keys are linked to the platform keyring, right?

No, I mean secondary, through this patchset:
https://lore.kernel.org/lkml/YhKP12KEmyqyS8rj at iki.fi/

Thanks for the info! This provides another approach to verify kernel image's signature via the secondary keyring once the end-use chooses to trust MOK keys by setting MokListTrustedRT.

Quoted 3 lines

Apparently support for importing the MOK keys into the platform keyring also exists but I am not sure if this is upstream or downstream feature.

This is actually an upstream feature,

commit 15ea0e1e3e185040bed6119f815096f2e4326242

Author: Josh Boyer <[email protected]>
Date:   Thu Dec 13 01:37:56 2018 +0530

efi: Import certificates from UEFI Secure Boot

Secure Boot stores a list of allowed certificates in the 'db' variable. This patch imports those certificates into the platform keyring. The shim UEFI bootloader has a similar certificate list stored in the 'MokListRT' variable. We import those as well.

Secure Boot also maintains a list of disallowed certificates in the 'dbx' variable. We load those certificates into the system blacklist keyring and forbid any kernel signed with those from loading.

[zohar at linux.ibm.com: dropped Josh's original patch description]
Quoted 3 lines

At any rate the MOK keys are not included in the primary keyring which is the only keyring currently in use for kexec on arm64.

Good summary, thanks!

Quoted 5 lines

Thanks

Michal

Signature · 2 lines
Best regards,
Coiby
author
#8 lore raw

Re: [PATCH v5 3/3] arm64: kexec_file: use more system keyrings to verify kernel image signature

In reply to Michal Suchánek
Quoted 35 lines · Michal Such?nek
Quoted 4 lines · Coiby Xu

Currently, a problem faced by arm64 is if a kernel image is signed by a MOK key, loading it via the kexec_file_load() system call would be rejected with the error "Lockdown: kexec: kexec of unsigned images is restricted; see man kernel_lockdown.7".

This is because image_verify_sig uses only the primary keyring that contains only kernel built-in keys to verify the kexec image.

Quoted 26 lines

This patch allows to verify arm64 kernel image signature using not only .builtin_trusted_keys but also .platform and .secondary_trusted_key keyring.

---
 arch/arm64/kernel/kexec_image.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm64/kernel/kexec_image.c +0 -1
index 9ec34690e255..51af1c22d6da 100644
@@ -14,7 +14,6 @@
 #include <linux/kexec.h>
 #include <linux/pe.h>
 #include <linux/string.h>
-#include <linux/verification.h>
 #include <asm/byteorder.h>
 #include <asm/cpufeature.h>
 #include <asm/image.h>
@@ -133,8 +132,7 @@ static void *image_load(struct kimage *image,
 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
 static int image_verify_sig(const char *kernel, unsigned long kernel_len)
 {
Quoted 8 lines

arch/arm64/kernel/kexec_image.c static void *image_load(struct kimage *image, · line 137

Quoted 5 lines
arch/arm64/kernel/kexec_image.c +1 -2
-	return verify_pefile_signature(kernel, kernel_len, NULL,
-				       VERIFYING_KEXEC_PE_SIGNATURE);
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }
 #endif

You can eliminate image_verify_sig here aswell and directly assign kexec_kernel_verify_pe_sig to fops.

Good suggestions, thanks!

Quoted 5 lines

Thanks

Michal

Signature · 2 lines
Best regards,
Coiby
author Applied
#5 lore raw

Re: [PATCH v5 2/3] kexec, KEYS: make the code in bzImage64_verify_sig generic

In reply to Michal Suchánek
Quoted 47 lines · Michal Such?nek
Quoted 1 line · Coiby Xu

The code in bzImage64_verify_sig could make use of system keyrings

s/could make/makes/

Quoted 30 lines

including .buitin_trusted_keys, .secondary_trusted_keys and .platform keyring to verify signed kernel image as PE file. Make it generic so both x86_64 and arm64 can use it.

---
 arch/x86/kernel/kexec-bzimage64.c | 13 +------------
 include/linux/kexec.h             |  7 +++++++
 kernel/kexec_file.c               | 17 +++++++++++++++++
 3 files changed, 25 insertions(+), 12 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +0 -4
index 170d0fd68b1f..f73aab3fde33 100644
@@ -17,7 +17,6 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/efi.h>
-#include <linux/verification.h>
 
 #include <asm/bootparam.h>
 #include <asm/setup.h>
@@ -531,17 +530,7 @@ 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)
 {
-	int ret;
-
-	ret = verify_pefile_signature(kernel, kernel_len,

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

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c +1 -8
-				      VERIFY_USE_SECONDARY_KEYRING,
-				      VERIFYING_KEXEC_PE_SIGNATURE);
-	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
-		ret = verify_pefile_signature(kernel, kernel_len,
-					      VERIFY_USE_PLATFORM_KEYRING,
-					      VERIFYING_KEXEC_PE_SIGNATURE);
-	}
-	return ret;
+	return kexec_kernel_verify_pe_sig(kernel, kernel_len);
 }

Maybe you can completely eliminate bzImage64_verify_sig and directly assign kexec_kernel_verify_pe_sig to the fops?

Quoted 3 lines

Other than that

Applied, thanks for the suggestion and reviewing the patch!

Quoted 61 lines
Quoted 60 lines
arch/x86/kernel/kexec-bzimage64.c +0 -0
 #endif
 
include/linux/kexec.h +7 -0
index 755fed183224..2fe39e946988 100644
@@ -19,6 +19,7 @@
 #include <asm/io.h>
 
 #include <uapi/linux/kexec.h>
+#include <linux/verification.h>
 
 #ifdef CONFIG_KEXEC_CORE
 #include <linux/list.h>
@@ -196,6 +197,12 @@ int arch_kexec_apply_relocations(struct purgatory_info *pi,
 				 const Elf_Shdr *relsec,
 				 const Elf_Shdr *symtab);
 int arch_kimage_file_post_load_cleanup(struct kimage *image);
+#ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel,
+				    unsigned long kernel_len);
+#endif
+#endif
 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
kernel/kexec_file.c +17 -1
index 3720435807eb..754885b96aab 100644
@@ -165,6 +165,23 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 }
 
 #ifdef CONFIG_KEXEC_SIG
+#ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
+int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
+{
+	int ret;
+
+	ret = verify_pefile_signature(kernel, kernel_len,
+				      VERIFY_USE_SECONDARY_KEYRING,
+				      VERIFYING_KEXEC_PE_SIGNATURE);
+	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
+		ret = verify_pefile_signature(kernel, kernel_len,
+					      VERIFY_USE_PLATFORM_KEYRING,
+					      VERIFYING_KEXEC_PE_SIGNATURE);
+	}
+	return ret;
+}
+#endif
+
 static int kexec_image_verify_sig(struct kimage *image, void *buf,
 		unsigned long buf_len)
 {
--

2.34.1

Signature · 2 lines
Best regards,
Coiby