From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 E2B5245DF73; Tue, 21 Jul 2026 22:24:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784672662; cv=none; b=lWBle+XWwlvVaus301iVWkmUtGSdt6A3ofUOaJIKI8DBqq1za9ylHxPdXOrfi/SB0e8Vs5NoxDURmNtABRl2f0PkhmYy5V+aBpNFEN4TmTLr0aDS61IFoBz0ff+Yg/F3Ilm973YdxVYoL9Vfjz8X9NnSVq4u1xrh4Xupx9pIsC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784672662; c=relaxed/simple; bh=CUP6oW2dl/bUmGLfeBHv5YhW/HiDSAdY3QmJl/3TZNc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rbQuQOIXLvGdiRM0URPzd6+xcC1bMII7iJM8C+8rhmGawiFI/kTEWYwyVhxL5RFSZhQsm5MaWs5+ANCA6eQ5t5CdDacztwsB3nPKE5XH/kNiOwc3KhpIbhVyR44pjy0QoV8R9AtX2Sw7r1L+EFwx/dVaBWBgoEW1VCe+yg+dRBc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=cSPmP9O+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="cSPmP9O+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53F091F000E9; Tue, 21 Jul 2026 22:24:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784672660; bh=RH0Phi9h+Lv547lNTrLZRJMRQP3vpt7XnSRu0OPAI/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cSPmP9O+aXTE6na38/DOj8ThlpwrNc0U8nQerERonDM7qOcQQMtN75eKgUj8+l4Jt Gut35E9sHA4C23JzVjEPWSWaN0FUKFMTWyVCVq1WiF+5th+/vus2n/W1yzKyu6HAzN JOqUnaHMHLCANFSPbOZXGdAhSUDBkhj9D69u/1xo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Baoquan He , Pratyush Yadav , Nutty Liu , Tao Liu , Paul Walmsley Subject: [PATCH 5.15 708/843] riscv: Prevent NULL pointer dereference in machine_kexec_prepare() Date: Tue, 21 Jul 2026 17:25:43 +0200 Message-ID: <20260721152421.977414532@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152405.946368001@linuxfoundation.org> References: <20260721152405.946368001@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tao Liu commit 81bbcff0c053c4f5c711c31a9b72fc492bd96c3f upstream. A NULL pointer dereference issue is noticed in riscv's machine_kexec_prepare(), where image->segment[i].buf might be NULL and copied unchecked. The NULL buf comes from ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL, then it is copied without a check in machine_kexec_prepare(): kexec_file_load -> kimage_file_alloc_init() -> kimage_file_prepare_segments() -> ima_add_kexec_buffer() -> kexec_add_buffer() -> machine_kexec_prepare() -> memcpy() Address this by adding a check before the data copy attempt. Fixes: b7fb4d78a6ad ("RISC-V: use memcpy for kexec_file mode") Cc: stable@vger.kernel.org Closes: https://lore.kernel.org/kexec/CAO7dBbVftLUhd2qrh7hmijTB3PEPfZAhykCGqEfrPoOcSrrj-w@mail.gmail.com/ Acked-by: Baoquan He Acked-by: Pratyush Yadav Reviewed-by: Nutty Liu Signed-off-by: Tao Liu Link: https://patch.msgid.link/20260705232706.30265-2-ltao@redhat.com Signed-off-by: Paul Walmsley Signed-off-by: Greg Kroah-Hartman --- arch/riscv/kernel/machine_kexec.c | 3 +++ 1 file changed, 3 insertions(+) --- a/arch/riscv/kernel/machine_kexec.c +++ b/arch/riscv/kernel/machine_kexec.c @@ -67,6 +67,9 @@ machine_kexec_prepare(struct kimage *ima if (image->segment[i].memsz <= sizeof(fdt)) continue; + if (!image->segment[i].buf) + continue; + if (image->file_mode) memcpy(&fdt, image->segment[i].buf, sizeof(fdt)); else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))