From: WANG Rui <r@hev.cc>
To: Huacai Chen <chenhuacai@kernel.org>, Ard Biesheuvel <ardb@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Haiyong Sun <sunhaiyong@loongson.cn>,
Lisa Robinson <lisa@bytefly.space>,
loongarch@lists.linux.dev, linux-efi@vger.kernel.org,
linux-kernel@vger.kernel.org, WANG Rui <r@hev.cc>
Subject: [PATCH v3 3/3] LoongArch: Avoid initrd overlap during kernel relocation
Date: Wed, 29 Apr 2026 13:13:18 +0800 [thread overview]
Message-ID: <20260429051318.1581350-4-r@hev.cc> (raw)
In-Reply-To: <20260429051318.1581350-1-r@hev.cc>
Validate the relocation address against the initrd region specified
via "initrd=" or "initrdmem=" on the command line. Reject relocation
targets that overlap the initrd to prevent memory corruption during
early boot.
Signed-off-by: WANG Rui <r@hev.cc>
---
arch/loongarch/kernel/relocate.c | 45 ++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/arch/loongarch/kernel/relocate.c b/arch/loongarch/kernel/relocate.c
index c36604a81d08..c303c0be8f06 100644
--- a/arch/loongarch/kernel/relocate.c
+++ b/arch/loongarch/kernel/relocate.c
@@ -214,14 +214,59 @@ static inline void __init *determine_relocation_address(void)
return RELOCATED_KASLR(destination);
}
+static unsigned long __init try_get_initrd(unsigned long *size)
+{
+ unsigned long start = 0;
+ unsigned int key_length;
+ const char *key;
+ char *p;
+
+ key = "initrd=";
+ key_length = strlen(key);
+ p = strstr(boot_command_line, key);
+
+ if (!p) {
+ key = "initrdmem=";
+ key_length = strlen(key);
+ p = strstr(boot_command_line, key);
+ }
+
+ if (p == boot_command_line || (p > boot_command_line && *(p - 1) == ' ')) {
+ char *endp;
+
+ p += key_length;
+ start = memparse(p, &endp);
+ if (*endp == ',')
+ *size = memparse(endp + 1, NULL);
+ }
+
+ return start;
+}
+
static inline int __init relocation_addr_valid(void *location_new)
{
+ unsigned long initrd_start;
+ unsigned long initrd_size = 0;
+
if ((unsigned long)location_new & 0x00000ffff)
return 0; /* Inappropriately aligned new location */
if ((unsigned long)location_new < (unsigned long)_end)
return 0; /* New location overlaps original kernel */
+ initrd_start = try_get_initrd(&initrd_size);
+ if (initrd_start && initrd_size) {
+ unsigned long kernel_start;
+ unsigned long kernel_size;
+
+ kernel_start = PHYSADDR(location_new);
+ kernel_size = (unsigned long)_end - (unsigned long)_text;
+
+ if (kernel_start < (initrd_start + initrd_size) &&
+ initrd_start < (kernel_start + kernel_size))
+ return 0; /* Initrd overlaps kernel */
+ }
+
return 1;
}
#endif
--
2.54.0
next prev parent reply other threads:[~2026-04-29 5:14 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-29 5:13 [PATCH v3 0/3] LoongArch: Move KASLR to EFI stub to avoid initrd overlap WANG Rui
2026-04-29 5:13 ` [PATCH v3 1/3] efi/loongarch: Randomize kernel preferred address for KASLR WANG Rui
2026-04-29 7:51 ` Huacai Chen
2026-04-29 8:55 ` Ard Biesheuvel
2026-04-29 9:04 ` WANG Rui
2026-04-29 9:15 ` Ard Biesheuvel
2026-04-29 5:13 ` [PATCH v3 2/3] LoongArch: Skip relocation-time KASLR if it has already been applied WANG Rui
2026-04-29 5:13 ` WANG Rui [this message]
2026-04-29 7:39 ` [PATCH v3 3/3] LoongArch: Avoid initrd overlap during kernel relocation Huacai Chen
2026-04-29 9:16 ` [PATCH v3 0/3] LoongArch: Move KASLR to EFI stub to avoid initrd overlap Ard Biesheuvel
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=20260429051318.1581350-4-r@hev.cc \
--to=r@hev.cc \
--cc=ardb@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=ilias.apalodimas@linaro.org \
--cc=kernel@xen0n.name \
--cc=linux-efi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lisa@bytefly.space \
--cc=loongarch@lists.linux.dev \
--cc=sunhaiyong@loongson.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox