From nobody Mon Feb 9 15:09:25 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 424A61DC9B3 for ; Mon, 19 Jan 2026 03:25:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768793159; cv=none; b=eDjU98q6HNAEucl9M2A5AgAHeAuZYg9W5ztM3cHvjaj2hreH6D3aqNnF9F+DEWlYattlGW4E9EEh8l3ANbTIv/Xjz1sJNcieekfZqiqyYfdnSDnm65hLYtDDHFwAcyaClakhWGH/yhvaWA37S+kf1/mCIQmYG/1Th5N4OGNm3FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768793159; c=relaxed/simple; bh=+ecQ5gK8JeAjJGmk6QHcpXOGTwCDwRZN/RM0zrGbals=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KFXfBh79cVIRq7UU5MQBJds8q577JRyEtLVLhLe4QN0/AMS19DSINkCxxIDnRnjCvoDF0XtX7+zQ+WxO5sd8xqIKqrRxsO4tyL/v4Ns67arj9XW+5gMCyfv065XjRv5KE3scR2bq0YWYMTSEiaXY7j5VYoogAJMzKpQHSLbqtts= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Sr/WbDdh; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Sr/WbDdh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768793157; 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: in-reply-to:in-reply-to:references:references; bh=PWLya1dhQNEc2PDrSyTdG72OD3leiyaAynZDfpPJDd8=; b=Sr/WbDdh2I1uXlSQU3IFGZv1TUjRho8lhPqy53DJv8aodXfiKoNR8Qmz6HPF9y3OxDWgT4 vQWWCaEw/lszjUBOCgOARqj/P143HdNihdfPmsBLHFYlJN9zdBT0Lu+pzJbeSJVcMCDr3Y S5yF7fr+omOI4mCzZK7kTKQrtCZQROI= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-utvPLdATNoeFcSBB5BRvfw-1; Sun, 18 Jan 2026 22:25:54 -0500 X-MC-Unique: utvPLdATNoeFcSBB5BRvfw-1 X-Mimecast-MFC-AGG-ID: utvPLdATNoeFcSBB5BRvfw_1768793151 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EFC471956096; Mon, 19 Jan 2026 03:25:50 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.72.112.74]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 906531955F22; Mon, 19 Jan 2026 03:25:39 +0000 (UTC) From: Pingfan Liu To: kexec@lists.infradead.org Cc: Pingfan Liu , "David S. Miller" , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jeremy Linton , Catalin Marinas , Will Deacon , Ard Biesheuvel , Simon Horman , Gerd Hoffmann , Vitaly Kuznetsov , Philipp Rudo , Viktor Malik , Jan Hendrik Farr , Baoquan He , Dave Young , Andrew Morton , bpf@vger.kernel.org, systemd-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCHv6 03/13] kexec_file: Introduce routines to parse PE file Date: Mon, 19 Jan 2026 11:24:14 +0800 Message-ID: <20260119032424.10781-4-piliu@redhat.com> In-Reply-To: <20260119032424.10781-1-piliu@redhat.com> References: <20260119032424.10781-1-piliu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" The incoming patches need to handle UEFI format image, which is in PE format. Hence introducing some routines here. Signed-off-by: Pingfan Liu Cc: Baoquan He Cc: Dave Young Cc: Andrew Morton Cc: Philipp Rudo To: kexec@lists.infradead.org --- kernel/Makefile | 1 + kernel/kexec_internal.h | 3 ++ kernel/kexec_uefi_app.c | 81 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 kernel/kexec_uefi_app.c diff --git a/kernel/Makefile b/kernel/Makefile index e83669841b8cc..f9e85c4a0622b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -83,6 +83,7 @@ obj-$(CONFIG_CRASH_DUMP_KUNIT_TEST) +=3D crash_core_test.o obj-$(CONFIG_KEXEC) +=3D kexec.o obj-$(CONFIG_KEXEC_FILE) +=3D kexec_file.o obj-$(CONFIG_KEXEC_ELF) +=3D kexec_elf.o +obj-$(CONFIG_KEXEC_BPF) +=3D kexec_uefi_app.o obj-$(CONFIG_BACKTRACE_SELF_TEST) +=3D backtracetest.o obj-$(CONFIG_COMPAT) +=3D compat.o obj-$(CONFIG_CGROUPS) +=3D cgroup/ diff --git a/kernel/kexec_internal.h b/kernel/kexec_internal.h index 228bb88c018bc..8e5e5c1237732 100644 --- a/kernel/kexec_internal.h +++ b/kernel/kexec_internal.h @@ -36,6 +36,9 @@ static inline void kexec_unlock(void) void kimage_file_post_load_cleanup(struct kimage *image); extern char kexec_purgatory[]; extern size_t kexec_purgatory_size; +extern bool pe_has_bpf_section(const char *file_buf, unsigned long pe_sz); +extern int pe_get_section(const char *file_buf, const char *sect_name, + char **sect_start, unsigned long *sect_sz); #else /* CONFIG_KEXEC_FILE */ static inline void kimage_file_post_load_cleanup(struct kimage *image) { } #endif /* CONFIG_KEXEC_FILE */ diff --git a/kernel/kexec_uefi_app.c b/kernel/kexec_uefi_app.c new file mode 100644 index 0000000000000..dbe6d76d47ffa --- /dev/null +++ b/kernel/kexec_uefi_app.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * UEFI appilication file helpers + * + * Copyright (C) 2025, 2026 Red Hat, Inc + */ + +#include +#include +#include +#include +#include +#include +#include "kexec_internal.h" + +/* + * The UEFI Terse Executable (TE) image has MZ header. + */ +static bool is_valid_pe(const char *kernel_buf, unsigned long kernel_len) +{ + struct mz_hdr *mz; + struct pe_hdr *pe; + + if (!kernel_buf) + return false; + mz =3D (struct mz_hdr *)kernel_buf; + if (mz->magic !=3D IMAGE_DOS_SIGNATURE) + return false; + pe =3D (struct pe_hdr *)(kernel_buf + mz->peaddr); + if (pe->magic !=3D IMAGE_NT_SIGNATURE) + return false; + if (pe->opt_hdr_size =3D=3D 0) { + pr_err("optional header is missing\n"); + return false; + } + return true; +} + +bool pe_has_bpf_section(const char *file_buf, unsigned long pe_sz) +{ + char *sect_start =3D NULL; + unsigned long sect_sz =3D 0; + int ret; + + if (!is_valid_pe(file_buf, pe_sz)) + return false; + ret =3D pe_get_section(file_buf, ".bpf", §_start, §_sz); + if (ret < 0) + return false; + return true; +} + +int pe_get_section(const char *file_buf, const char *sect_name, + char **sect_start, unsigned long *sect_sz) +{ + struct pe_hdr *pe_hdr; + struct pe32plus_opt_hdr *opt_hdr; + struct section_header *sect_hdr; + int section_nr, i; + struct mz_hdr *mz =3D (struct mz_hdr *)file_buf; + + *sect_start =3D NULL; + *sect_sz =3D 0; + pe_hdr =3D (struct pe_hdr *)(file_buf + mz->peaddr); + section_nr =3D pe_hdr->sections; + opt_hdr =3D (struct pe32plus_opt_hdr *)(file_buf + mz->peaddr + + sizeof(struct pe_hdr)); + sect_hdr =3D (struct section_header *)((char *)opt_hdr + + pe_hdr->opt_hdr_size); + + for (i =3D 0; i < section_nr; i++) { + if (strcmp(sect_hdr->name, sect_name) =3D=3D 0) { + *sect_start =3D (char *)file_buf + sect_hdr->data_addr; + *sect_sz =3D sect_hdr->raw_data_size; + return 0; + } + sect_hdr++; + } + + return -1; +} --=20 2.49.0