Skip to contentSkip to conversation

LoongArch: kexec: Fix address space mismatch in command line lookup

PATCH New George Guo 1 participant 1 message Jul 1 LoongArch

lore
Share & tools
lore

George Guo

author
#0 lore raw

PATCH v2 1/1 LoongArch: kexec: Fix address space mismatch in command line lookup

From: George Guo <[email protected]>

When searching the loaded segments for the "kexec" command line marker, the kexec_load(2) path (file_mode == 0) passes the user-space segment buffer straight to strncmp() through a bogus (char __user *) cast. This dereferences a user pointer in kernel context, which is wrong and is flagged by sparse:

arch/loongarch/kernel/machine_kexec.c:84:51: sparse: incorrect type in argument 2 (different address spaces) @@ expected char const * @@ got char [noderef] __user *

Copy the marker-sized prefix of each segment into a small on-stack buffer with copy_from_user() before comparing, and skip segments that fault. The subsequent copy_from_user() that stages the full command line into the safe area is left unchanged.

---
Changes in v2:
- Keep bootloader unchanged and copy into a fixed char head[8] instead of turning bootloader into an array just to size the temporary buffer (Huacai).

v1: https://lore.kernel.org/all/[email protected]/

 arch/loongarch/kernel/machine_kexec.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/loongarch/kernel/machine_kexec.c +5 -1
index d7fafda1d541..ced49194d9de 100644
@@ -57,9 +57,13 @@ int machine_kexec_prepare(struct kimage *kimage)
 					strlen((char *)kimage->arch.cmdline_ptr) + 1);
 		kimage->arch.cmdline_ptr = (unsigned long)KEXEC_CMDLINE_ADDR;
 	} else {
+		char head[8];
+
 		/* Find the command line */
 		for (i = 0; i < kimage->nr_segments; i++) {
-			if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {
+			if (copy_from_user(head, kimage->segment[i].buf, strlen(bootloader)))
+				continue;
+			if (!strncmp(bootloader, head, strlen(bootloader))) {
 				if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))
 					kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;
 				break;
Signature · 1 line
2.25.1