linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
@ 2022-11-21  7:29 Coiby Xu
  2022-11-21 18:23 ` Mimi Zohar
  2022-11-22  2:28 ` [PATCH v2] " Coiby Xu
  0 siblings, 2 replies; 6+ messages in thread
From: Coiby Xu @ 2022-11-21  7:29 UTC (permalink / raw)
  To: kexec
  Cc: Matthew Garrett, Jiri Bohac, David Howells, linux-integrity,
	Eric Biederman, James Morris, Matthew Garrett, open list

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 <mjg59@srcf.ucam.org>
Cc: Jiri Bohac <jbohac@suse.cz>
Cc: David Howells <dhowells@redhat.com>
Cc: kexec@lists.infradead.org
Cc: linux-integrity@vger.kernel.org
Signed-off-by: Coiby Xu <coxu@redhat.com>
---
 kernel/kexec_file.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 45637511e0de..04d56b6e6459 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -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,
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
  2022-11-21  7:29 [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled Coiby Xu
@ 2022-11-21 18:23 ` Mimi Zohar
  2022-11-22  2:36   ` Coiby Xu
  2022-11-22  2:28 ` [PATCH v2] " Coiby Xu
  1 sibling, 1 reply; 6+ messages in thread
From: Mimi Zohar @ 2022-11-21 18:23 UTC (permalink / raw)
  To: Coiby Xu, kexec
  Cc: Matthew Garrett, Jiri Bohac, David Howells, linux-integrity,
	Eric Biederman, James Morris, Matthew Garrett, open list

Hi Coiby,

On Mon, 2022-11-21 at 15:29 +0800, Coiby Xu wrote:
> 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 <mjg59@srcf.ucam.org>
> Cc: Jiri Bohac <jbohac@suse.cz>
> Cc: David Howells <dhowells@redhat.com>
> Cc: kexec@lists.infradead.org
> Cc: linux-integrity@vger.kernel.org
> Signed-off-by: Coiby Xu <coxu@redhat.com>

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

Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
  2022-11-21  7:29 [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled Coiby Xu
  2022-11-21 18:23 ` Mimi Zohar
@ 2022-11-22  2:28 ` Coiby Xu
  1 sibling, 0 replies; 6+ messages in thread
From: Coiby Xu @ 2022-11-22  2:28 UTC (permalink / raw)
  To: kexec
  Cc: Mimi Zohar, Matthew Garrett, Jiri Bohac, David Howells,
	linux-integrity, Eric Biederman, Matthew Garrett, James Morris,
	open list

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 <mjg59@srcf.ucam.org>
Cc: Jiri Bohac <jbohac@suse.cz>
Cc: David Howells <dhowells@redhat.com>
Cc: kexec@lists.infradead.org
Cc: linux-integrity@vger.kernel.org
Signed-off-by: Coiby Xu <coxu@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
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(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 45637511e0de..dcde7a50fbeb 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -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,
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
  2022-11-21 18:23 ` Mimi Zohar
@ 2022-11-22  2:36   ` Coiby Xu
  2022-11-28 17:16     ` Mimi Zohar
  0 siblings, 1 reply; 6+ messages in thread
From: Coiby Xu @ 2022-11-22  2:36 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: kexec, Matthew Garrett, Jiri Bohac, David Howells,
	linux-integrity, Eric Biederman, James Morris, Matthew Garrett,
	open list

Hi Mimi,

On Mon, Nov 21, 2022 at 01:23:57PM -0500, Mimi Zohar wrote:
>Hi Coiby,
>
>On Mon, 2022-11-21 at 15:29 +0800, Coiby Xu wrote:
>> 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 <mjg59@srcf.ucam.org>
>> Cc: Jiri Bohac <jbohac@suse.cz>
>> Cc: David Howells <dhowells@redhat.com>
>> Cc: kexec@lists.infradead.org
>> Cc: linux-integrity@vger.kernel.org
>> Signed-off-by: Coiby Xu <coxu@redhat.com>
>
>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.

>
>Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>

And thanks for the review!

-- 
Best regards,
Coiby

-- 
Best regards,
Coiby


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
  2022-11-22  2:36   ` Coiby Xu
@ 2022-11-28 17:16     ` Mimi Zohar
  2022-12-30  7:00       ` Coiby Xu
  0 siblings, 1 reply; 6+ messages in thread
From: Mimi Zohar @ 2022-11-28 17:16 UTC (permalink / raw)
  To: Coiby Xu
  Cc: kexec, Matthew Garrett, Jiri Bohac, David Howells,
	linux-integrity, Eric Biederman, James Morris, Matthew Garrett,
	open list

On Tue, 2022-11-22 at 10:36 +0800, Coiby Xu wrote:
> Hi Mimi,
> 
> On Mon, Nov 21, 2022 at 01:23:57PM -0500, Mimi Zohar wrote:
> >Hi Coiby,
> >
> >On Mon, 2022-11-21 at 15:29 +0800, Coiby Xu wrote:
> >> 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 <mjg59@srcf.ucam.org>
> >> Cc: Jiri Bohac <jbohac@suse.cz>
> >> Cc: David Howells <dhowells@redhat.com>
> >> Cc: kexec@lists.infradead.org
> >> Cc: linux-integrity@vger.kernel.org
> >> Signed-off-by: Coiby Xu <coxu@redhat.com>
> >
> >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.
> 
> >
> >Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> 
> 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,

Mimi


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled
  2022-11-28 17:16     ` Mimi Zohar
@ 2022-12-30  7:00       ` Coiby Xu
  0 siblings, 0 replies; 6+ messages in thread
From: Coiby Xu @ 2022-12-30  7:00 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: kexec, Matthew Garrett, Jiri Bohac, David Howells,
	linux-integrity, Eric Biederman, James Morris, Matthew Garrett,
	open list

On Mon, Nov 28, 2022 at 12:16:08PM -0500, Mimi Zohar wrote:
>On Tue, 2022-11-22 at 10:36 +0800, Coiby Xu wrote:
>> Hi Mimi,
>>
>> On Mon, Nov 21, 2022 at 01:23:57PM -0500, Mimi Zohar wrote:
>> >Hi Coiby,
>> >
>> >On Mon, 2022-11-21 at 15:29 +0800, Coiby Xu wrote:
>> >> 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 <mjg59@srcf.ucam.org>
>> >> Cc: Jiri Bohac <jbohac@suse.cz>
>> >> Cc: David Howells <dhowells@redhat.com>
>> >> Cc: kexec@lists.infradead.org
>> >> Cc: linux-integrity@vger.kernel.org
>> >> Signed-off-by: Coiby Xu <coxu@redhat.com>
>> >
>> >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.
>>
>> >
>> >Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
>>
>> 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]".

>
>-- 
>thanks,
>
>Mimi
>

-- 
Best regards,
Coiby


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-12-30  7:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-21  7:29 [PATCH] lockdown: kexec_file: prevent unsigned kernel image when KEXEC_SIG not enabled Coiby Xu
2022-11-21 18:23 ` Mimi Zohar
2022-11-22  2:36   ` Coiby Xu
2022-11-28 17:16     ` Mimi Zohar
2022-12-30  7:00       ` Coiby Xu
2022-11-22  2:28 ` [PATCH v2] " Coiby Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).