From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 616483B38AC for ; Fri, 26 Jun 2026 10:38:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782470339; cv=none; b=DXlwsRCtVdP6AhFGImo9j9G3oexXGmrkVciD71jb1yiVCBzlbw4WWFjfo57eS7FBr4Uc0C3QhQQLUy/8e5MN3KPz+YDqmbELyxWXNLl+/xMXoHHiBuaH2H8hNysBJ3oy3XX4UX/Av1lQFhHH95QeFsF2ELuHRex9iXZ9ce+RNJQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782470339; c=relaxed/simple; bh=d21F/jmMXz8n7tddwFZnpU9mN2PUUWdKMgoFTpQINnU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=UQ21XkCWwJMKMe7juf1Hoju4060us0gNGMvB033tnzK6q8DrLHdUL8Ivmb/QmFL3bjoLr+3xXjQ8H5vapHVuFt9em3urTtAVyYBPYezg/C9g5/kpz5NKVJ3Wjun6qkhj3YeGf/yX5knF0orglvbP8MLRPq6ujn/4KyzdOBSU0Ig= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=VFZxB0NK; arc=none smtp.client-ip=95.215.58.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="VFZxB0NK" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1782470324; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HWktgbq7NgGGnrTybk5KiUwxIP14lnbdKeXbMxrkJ18=; b=VFZxB0NK4gDXBNFmAR8CZaBKyj6pd2xcLQYfQ5Fn5yvaLUVp/IetpcjsxT/3LBnSegGWkp VJjtzydSbfPq4nQiJO8pDzb3gMxwkgGEoW/TrZZx1cEEJ7tZBIRYh2k6A+0lgROecb6w3b QvhhsxUAo76kAhh9zdarq1+CCsSlvnI= From: George Guo To: Huacai Chen Cc: WANG Xuerui , Qiang Ma , Kexin Liu , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] LoongArch: kexec: Fix address space mismatch in command line lookup Date: Fri, 26 Jun 2026 18:38:35 +0800 Message-Id: <20260626103835.353818-1-dongtai.guo@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: George Guo 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. Fixes: 4a03b2ac06a5 ("LoongArch: Add kexec support") Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202605051639.aEPioXdD-lkp@intel.com/ Co-developed-by: Kexin Liu Signed-off-by: Kexin Liu Signed-off-by: George Guo --- arch/loongarch/kernel/machine_kexec.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/loongarch/kernel/machine_kexec.c b/arch/loongarch/kernel/machine_kexec.c index d7fafda1d541..6f563175ddbf 100644 --- a/arch/loongarch/kernel/machine_kexec.c +++ b/arch/loongarch/kernel/machine_kexec.c @@ -42,7 +42,7 @@ static unsigned long first_ind_entry; int machine_kexec_prepare(struct kimage *kimage) { int i; - char *bootloader = "kexec"; + static const char bootloader[] = "kexec"; void *cmdline_ptr = (void *)KEXEC_CMDLINE_ADDR; kimage->arch.efi_boot = fw_arg0; @@ -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 tmp[sizeof(bootloader)]; + /* 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(tmp, kimage->segment[i].buf, strlen(bootloader))) + continue; + if (!strncmp(bootloader, tmp, strlen(bootloader))) { if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE)) kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr; break; -- 2.25.1