Skip to contentSkip to conversation

lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled

PATCH Superseded Coiby Xu 2 participants 6 messages Nov 21, 2022 – Dec 30, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

PATCH 1/1 lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>
---
 kernel/kexec_file.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
kernel/kexec_file.c +21 -10
index 45637511e0de..04d56b6e6459 100644
@@ -125,6 +125,20 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 	image->image_loader_data = NULL;
 }
 
+static int mandate_signatute_verification(void)
+{
+	/*
+	 * If IMA is guaranteed to appraise a signature on the kexec
+	 * image, permit it even if the kernel is otherwise locked
+	 * down.
+	 */
+	if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
+	    security_locked_down(LOCKDOWN_KEXEC))
+		return -EPERM;
+
+	return 0;
+}
+
 #ifdef CONFIG_KEXEC_SIG
 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
@@ -168,14 +182,9 @@ kimage_validate_signature(struct kimage *image)
 			return ret;
 		}
 
-		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
-		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
-			return -EPERM;
+		ret = mandate_signatute_verification();
+		if (ret)
+			return ret;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
 	}
@@ -211,10 +220,12 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 
 #ifdef CONFIG_KEXEC_SIG
 	ret = kimage_validate_signature(image);
-
+#else
+	ret = mandate_signatute_verification();
+#endif
 	if (ret)
 		goto out;
-#endif
+
 	/* It is possible that there no initramfs is being loaded */
 	if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
 		ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
Signature · 1 line
2.38.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Coiby,

Quoted 7 lines · Coiby Xu

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Quoted 7 lines
Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>

Other than correcting the function name to mandate_signature_verificati on(),

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

author Applied
#2 lore raw

Hi Mimi,

Quoted 18 lines · Mimi Zohar

Hi Coiby,

Quoted 7 lines · Coiby Xu

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Quoted 7 lines
Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>
Quoted 2 lines

Other than correcting the function name to mandate_signature_verificati on(),

Applied to v2, thanks for correcting me! Btw, I realize I overwrote the return code of kexec_image_verify_sig with mandate_signature_verification's. v2 has fixed this issue as well.

Quoted 2 lines

And thanks for the review!

Signature · 12 lines
Best regards,
Coiby

-- 
Best regards,
Coiby


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#3 lore raw
Quoted 24 lines · Coiby Xu

Hi Mimi,

Quoted 18 lines · Mimi Zohar

Hi Coiby,

Quoted 7 lines · Coiby Xu

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Quoted 7 lines
Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>
Quoted 2 lines

Other than correcting the function name to mandate_signature_verificati on(),

Quoted 8 lines

Applied to v2, thanks for correcting me! Btw, I realize I overwrote the return code of kexec_image_verify_sig with mandate_signature_verification's. v2 has fixed this issue as well.

Quoted 2 lines

And thanks for the review!

You're welcome.

Without either IMA_ARCH or KEXEC_SIG enabled, the kexec selftest test_kexec_file_load.sh properly failed with "kexec_file_load failed [PASS]", but from the informational messages output, it isn't clear why it failed. This should be corrected.

Signature · 9 lines
thanks,

Mimi


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#4 lore raw
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Coiby Xu

Hi Mimi,

Quoted 18 lines · Mimi Zohar

Hi Coiby,

Quoted 7 lines · Coiby Xu

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Quoted 7 lines
Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>
Quoted 2 lines

Other than correcting the function name to mandate_signature_verificati on(),

Applied to v2, thanks for correcting me! Btw, I realize I overwrote the return code of kexec_image_verify_sig with mandate_signature_verification's. v2 has fixed this issue as well.

Quoted 2 lines
Quoted 8 lines
Quoted 1 line

And thanks for the review!

You're welcome.

Without either IMA_ARCH or KEXEC_SIG enabled, the kexec selftest test_kexec_file_load.sh properly failed with "kexec_file_load failed [PASS]", but from the informational messages output, it isn't clear why it failed. This should be corrected.

Thanks for the suggestion! I've added some tests in v3 and now the message is "# kexec_file_load failed (missing IMA sig) [PASS]".

Quoted 6 lines
Signature · 4 lines
-- 
thanks,

Mimi
Signature · 8 lines
Best regards,
Coiby


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#5 lore raw

PATCH v2 1/1 lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled

In reply to Coiby Xu

A kernel builder may not enable KEXEC_SIG and some architectures like ppc64 simply don't have KEXEC_SIG. In these cases, unless both IMA_ARCH_POLICY and secure boot also enabled, lockdown doesn't prevent unsigned kernel image from being kexec'ed via the kexec_file_load syscall whereas it could prevent one via the kexec_load syscall. Mandate signature verification for those cases.

Fixes 155bdd30af17 ("kexec_file: Restrict at runtime if the kernel is locked down")
Cc Matthew Garrett <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc David Howells <[email protected]>

---
v2
- collect reviewed-by tag from Mimi
- s/mandate_signatute_verification/mandate_signature_verification [Mimi]
- return the status of kexec_image_verify_sig correctly when signature verification is not mandated

---
 kernel/kexec_file.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
kernel/kexec_file.c +17 -9
index 45637511e0de..dcde7a50fbeb 100644
@@ -125,6 +125,17 @@ void kimage_file_post_load_cleanup(struct kimage *image)
 	image->image_loader_data = NULL;
 }
 
+static bool mandate_signature_verification(void)
+{
+	/*
+	 * If IMA is guaranteed to appraise a signature on the kexec
+	 * image, permit it even if the kernel is otherwise locked
+	 * down.
+	 */
+	return !ima_appraise_signature(READING_KEXEC_IMAGE) &&
+	       security_locked_down(LOCKDOWN_KEXEC);
+}
+
 #ifdef CONFIG_KEXEC_SIG
 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
@@ -168,13 +179,7 @@ kimage_validate_signature(struct kimage *image)
 			return ret;
 		}
 
-		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
-		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (mandate_signature_verification())
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
@@ -211,10 +216,13 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 
 #ifdef CONFIG_KEXEC_SIG
 	ret = kimage_validate_signature(image);
-
+#else
+	if (mandate_signature_verification())
+		ret = -EPERM;
+#endif
 	if (ret)
 		goto out;
-#endif
+
 	/* It is possible that there no initramfs is being loaded */
 	if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
 		ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
Signature · 7 lines
2.38.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec