All of lore.kernel.org
 help / color / mirror / Atom feed
From: Youling Tang <youling.tang@linux.dev>
To: Simon Horman <horms@verge.net.au>
Cc: kexec@lists.infradead.org, Huacai Chen <chenhuacai@kernel.org>,
	youling.tang@linux.dev, Youling Tang <tangyouling@kylinios.cn>
Subject: [PATCH 7/7] LoongArch: Add pe_hdr->machine check for pei format images
Date: Fri, 29 Aug 2025 17:10:40 +0800	[thread overview]
Message-ID: <20250829091040.35827-8-youling.tang@linux.dev> (raw)
In-Reply-To: <20250829091040.35827-1-youling.tang@linux.dev>

From: Youling Tang <tangyouling@kylinios.cn>

Add the pe_hdr->machine check to ensure that the kernel image is of the 64-bit
LoongArch architecture.

Signed-off-by: Youling Tang <tangyouling@kylinios.cn>
---
 kexec/arch/loongarch/image-header.h        |  3 +++
 kexec/arch/loongarch/kexec-pei-loongarch.c | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/kexec/arch/loongarch/image-header.h b/kexec/arch/loongarch/image-header.h
index 0db7615..fc71ff2 100644
--- a/kexec/arch/loongarch/image-header.h
+++ b/kexec/arch/loongarch/image-header.h
@@ -35,6 +35,9 @@ struct loongarch_image_header {
 static const uint8_t loongarch_image_pe_sig[2] = {'M', 'Z'};
 static const uint8_t loongarch_pe_machtype[6] = {'P','E', 0x0, 0x0, 0x64, 0x62};
 
+#define IMAGE_FILE_MACHINE_LOONGARCH64  0x6264 /* LoongArch 64-bit processor family */
+
+
 /**
  * loongarch_header_check_pe_sig - Helper to check the loongarch image header.
  *
diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c b/kexec/arch/loongarch/kexec-pei-loongarch.c
index 1a19a39..e0a82b6 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -24,9 +24,20 @@
 #include "kexec-loongarch.h"
 #include "arch/options.h"
 
+#include <pe.h>
+
+static inline int loongarch_pe_check_machine(const struct pe_hdr *pe_hdr)
+{
+	if (!pe_hdr)
+		return 0;
+
+	return (pe_hdr->machine == IMAGE_FILE_MACHINE_LOONGARCH64);
+}
+
 int pei_loongarch_probe(const char *kernel_buf, off_t kernel_size)
 {
 	const struct loongarch_image_header *h;
+	const struct pe_hdr *pe_hdr;
 
 	if (kernel_size < sizeof(struct loongarch_image_header)) {
 		dbgprintf("%s: No loongarch image header.\n", __func__);
@@ -40,6 +51,12 @@ int pei_loongarch_probe(const char *kernel_buf, off_t kernel_size)
 		return -1;
 	}
 
+	pe_hdr = (const struct pe_hdr *)(kernel_buf + get_pehdr_offset(kernel_buf));
+	if (!loongarch_pe_check_machine(pe_hdr)) {
+		dbgprintf("%s: Bad loongarch pe_hdr machine.\n", __func__);
+		return -1;
+	}
+
 	return 0;
 }
 
-- 
2.34.1



  parent reply	other threads:[~2025-08-29 10:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-29  9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
2025-08-29  9:10 ` [PATCH 1/7] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
2025-08-29  9:10 ` [PATCH 2/7] LoongArch: Fix comments Youling Tang
2025-08-29  9:10 ` [PATCH 3/7] LoongArch: Terminate the cmdline string using '\0' Youling Tang
2025-08-29  9:10 ` [PATCH 4/7] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
2025-08-29  9:10 ` [PATCH 5/7] LoongArch: Change initrd allocation to top-down Youling Tang
2025-08-29  9:10 ` [PATCH 6/7] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
2025-08-29  9:10 ` Youling Tang [this message]
2025-08-29  9:19 ` [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250829091040.35827-8-youling.tang@linux.dev \
    --to=youling.tang@linux.dev \
    --cc=chenhuacai@kernel.org \
    --cc=horms@verge.net.au \
    --cc=kexec@lists.infradead.org \
    --cc=tangyouling@kylinios.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.