Skip to contentSkip to conversation

kexec_file: ima: allow loading a kernel with its IMA signature verified

PATCH In review Coiby Xu 3 participants 7 messages Jul 11, 2023 – Jul 14, 2023

Subsystems (5)
lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

PATCH 1/1 kexec_file: ima: allow loading a kernel with its IMA signature verified

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")
---
 kernel/kexec_file.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +9 -5
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}
 
 		if (sig_enforce) {
 			pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
@@ -169,12 +176,9 @@ kimage_validate_signature(struct kimage *image)
 		}
 
 		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
+		 * When both IMA and KEXEC_SIG fail in lockdown mode, reject it.
 		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (security_locked_down(LOCKDOWN_KEXEC))
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
Signature · 7 lines
2.41.0


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

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

With this patch, only the IMA signature would be verified.

Quoted 37 lines
---
 kernel/kexec_file.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +9 -5
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}
 
 		if (sig_enforce) {
 			pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
@@ -169,12 +176,9 @@ kimage_validate_signature(struct kimage *image)
 		}
 
 		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
+		 * When both IMA and KEXEC_SIG fail in lockdown mode, reject it.
 		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (security_locked_down(LOCKDOWN_KEXEC))
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
#2 lore raw
Quoted 28 lines · Mimi Zohar

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

With this patch, only the IMA signature would be verified.

Quoted 4 lines

---
kernel/kexec_file.c | 14 +++++++++-----

1 file changed, 9 insertions(+), 5 deletions(-)
Quoted 15 lines
Quoted 5 lines
kernel/kexec_file.c +0 -0
index 881ba0d1714c..96fce001fbc0 100644

kernel/kexec_file.c kimage_validate_signature(struct kimage *image) · line 171

Quoted 10 lines
kernel/kexec_file.c +7 -0
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}

The issue I see here is ret could be many things, for example it could be -EKEYREJECTED, meaning it was contained on a revocation list. With this patch the revocation could be overruled if the image was IMA signed with a different key. Do we really want to add the ability to overrule a revocation?

Quoted 20 lines
Quoted 18 lines
kernel/kexec_file.c +2 -5
 		if (sig_enforce) {
 			pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
@@ -169,12 +176,9 @@ kimage_validate_signature(struct kimage *image)
 		}
 
 		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
+		 * When both IMA and KEXEC_SIG fail in lockdown mode, reject it.
 		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (security_locked_down(LOCKDOWN_KEXEC))
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
author
#3 lore raw
Quoted 40 lines · Eric Snowberg
Quoted 40 lines · Mimi Zohar

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

With this patch, only the IMA signature would be verified.

Quoted 16 lines

---
kernel/kexec_file.c | 14 +++++++++-----

1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +4 -0
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
Quoted 8 lines
Quoted 3 lines

kernel/kexec_file.c kimage_validate_signature(struct kimage *image) · line 171

Quoted 3 lines
kernel/kexec_file.c +3 -0
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}

The issue I see here is ret could be many things, for example it could be -EKEYREJECTED, meaning it was contained on a revocation list. With this patch the revocation could be overruled if the image was IMA signed with a different key. Do we really want to add the ability to overrule a revocation?

Thanks for raising the concern! I haven't thought about this case of the key being in a revocation list. If the IMA signature comes from a distribution, the distribution should also blocklist the IMA key when the PE-COFF signature key is added to the revocation list. If the IMA signature is from an end-user, I think the user wants to pass the verification in this case.

Or how about only allowing IMA signature when EFI runtime services are disabled? Another reason I am yet to add to the commit message is a real-time kernel currently disables EFI runtime services for latency issues so it can't access the MOK keys to verify the PECOFF signature.

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

#4 lore raw
Quoted 54 lines · Coiby Xu
Quoted 40 lines · Eric Snowberg
Quoted 40 lines · Mimi Zohar

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

With this patch, only the IMA signature would be verified.

Quoted 16 lines

---
kernel/kexec_file.c | 14 +++++++++-----

1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +4 -0
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
Quoted 8 lines
Quoted 3 lines

kernel/kexec_file.c kimage_validate_signature(struct kimage *image) · line 171

Quoted 3 lines
kernel/kexec_file.c +3 -0
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}

The issue I see here is ret could be many things, for example it could be -EKEYREJECTED, meaning it was contained on a revocation list. With this patch the revocation could be overruled if the image was IMA signed with a different key. Do we really want to add the ability to overrule a revocation?

Quoted 6 lines

Thanks for raising the concern! I haven't thought about this case of the key being in a revocation list. If the IMA signature comes from a distribution, the distribution should also blocklist the IMA key when the PE-COFF signature key is added to the revocation list. If the IMA signature is from an end-user, I think the user wants to pass the verification in this case.

Correct, the IMA signature should be on the revocation list in this case. However, the embedded signature in the kernel doesn’t have to be the same as the IMA signature in the extended attribute. If they don’t match up, IMA could be used as a bypass mechanism to kexec a kernel that shouldn’t load.

Quoted 4 lines

Or how about only allowing IMA signature when EFI runtime services are disabled? Another reason I am yet to add to the commit message is a real-time kernel currently disables EFI runtime services for latency issues so it can't access the MOK keys to verify the PECOFF signature.

Possibly? On a UEFI system, the IMA signature on a kernel is ignored during boot, GRUB2 doesn’t have a way to validate it. If GRUB2 is booting a kernel on a system with EFI runtime services disabled, not only will you be missing the MOK keys, but you will be missing the MOKX and DBX too.

author
#5 lore raw
In reply to Mimi Zohar
Quoted 14 lines · Mimi Zohar

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Quoted 5 lines

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

With this patch, only the IMA signature would be verified.

Thanks for correcting me! I thought it's already a consensus that we could use either signature to verify a kernel image because that's what the code of commit 29d3c1c8dfe7 has done and the code comment seems to confirm it. But if we just read the commit message, it indeed didn't give an answer on whether x86 and ARM are only allowed to use PE-COFF signature.

Quoted 40 lines
Quoted 38 lines
---
 kernel/kexec_file.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +9 -5
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}
 
 		if (sig_enforce) {
 			pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
@@ -169,12 +176,9 @@ kimage_validate_signature(struct kimage *image)
 		}
 
 		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
+		 * When both IMA and KEXEC_SIG fail in lockdown mode, reject it.
 		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (security_locked_down(LOCKDOWN_KEXEC))
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);
Signature · 8 lines
Best regards,
Coiby


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

[CC'ing Paul Moore]

Quoted 19 lines · Coiby Xu
Quoted 18 lines · Mimi Zohar

[Cc'ing the LSM mailing list.]

Quoted 5 lines · Coiby Xu

When IMA has verified the signature of the kernel image, kexec'ing this kernel should be allowed.

Fixes af16df54b89d ("ima: force signature verification when CONFIG_KEXEC_SIG is configured")

The original commit 29d3c1c8dfe7 ("kexec: Allow kexec_file() with appropriate IMA policy when locked down") was not in lieu of the PE- COFF signature, but allowed using the IMA signature on other architectures.

Currently on systems with both PE-COFF and IMA signatures, both signatures are verified, assuming the file is in the IMA policy. If either signature verification fails, the kexec fails.

Quoted 7 lines
Quoted 1 line

With this patch, only the IMA signature would be verified.

Thanks for correcting me! I thought it's already a consensus that we could use either signature to verify a kernel image because that's what the code of commit 29d3c1c8dfe7 has done and the code comment seems to confirm it. But if we just read the commit message, it indeed didn't give an answer on whether x86 and ARM are only allowed to use PE-COFF signature.

I'm not aware of any consensus one way or the other. Commit 29d3c1c8dfe7 continued to fail the kexec on failure, when CONFIG_KEXEC_SIG_FORCE was enabled.

As there isn't a lockdown maintainer, Paul are you ok with this change?

Quoted 42 lines
Quoted 41 lines
Quoted 39 lines

---
 kernel/kexec_file.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/kexec_file.c +9 -5
index 881ba0d1714c..96fce001fbc0 100644
@@ -162,6 +162,13 @@ kimage_validate_signature(struct kimage *image)
 	ret = kexec_image_verify_sig(image, image->kernel_buf,
 				     image->kernel_buf_len);
 	if (ret) {
+		/*
+		 * If the kernel image already has its IMA signature verified, permit it.
+		 */
+		if (ima_appraise_signature(READING_KEXEC_IMAGE)) {
+			pr_notice("The kernel image already has its IMA signature verified.\n");
+			return 0;
+		}
 
 		if (sig_enforce) {
 			pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
@@ -169,12 +176,9 @@ kimage_validate_signature(struct kimage *image)
 		}
 
 		/*
-		 * If IMA is guaranteed to appraise a signature on the kexec
-		 * image, permit it even if the kernel is otherwise locked
-		 * down.
+		 * When both IMA and KEXEC_SIG fail in lockdown mode, reject it.
 		 */
-		if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
-		    security_locked_down(LOCKDOWN_KEXEC))
+		if (security_locked_down(LOCKDOWN_KEXEC))
 			return -EPERM;
 
 		pr_debug("kernel signature verification failed (%d).\n", ret);