Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 42F35C3ABC3 for ; Tue, 13 May 2025 10:36:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:content-type: Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=ezEgbeLEMuSHR1q8FCSFIVQjHrFYFsK4QfsJ4BnHsWk=; b=oiEYgyLiwanJ6OQAVICYGn4bnR tf5pFg0+UMoJ6OexQAGFNXHI6deaLDKOcyyRnXwyeQLA2ZRmASiPwXQxchhrYW7iA5KZsoYMkgzg6 zryTycYs9t7qQZuKJSLK/KZsnelQubehb1CHK3TxKaR+nzfUJIT42mM+D/r0nktbjqXVEJWxHoc3B +19CnALhURQwAoNiDtLXvuHQnTvSKy/BK0iszNjz3WuR0REZJd2OPiq1NzlCAz5ZKoJf+Vap5PUR5 xiLX7D2ipdZ020Nkkj9kZu/v0/9jInaYNYQIMkKR4HdI568hyOPK85lt3D2HnJdzOLZoxLsDAeoJ8 nMeu+u0Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uEmzy-0000000C8d6-0lBm; Tue, 13 May 2025 10:36:34 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uEmwX-0000000C7xA-06KF for kexec@lists.infradead.org; Tue, 13 May 2025 10:33:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747132378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ezEgbeLEMuSHR1q8FCSFIVQjHrFYFsK4QfsJ4BnHsWk=; b=HhTmrAi+USupfK91zVl6yt8JFs9ih61d5V3ulvgYJXNeFsuy1oHm0jxUJJXKUdqGV1fRFb /zslWtotq4OXYTdsLTxaMRiZG1wHjcEt6uhIiiFobpE0Oq0iL8cCoFdrJmhhF+tTmKsjl1 Omps2Rp7O4xZ/zbH9dfFu6VkLLSDETk= Received: from mx-prod-mc-02.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-423--dPyDqwtMwC2yQrg4mppxA-1; Tue, 13 May 2025 06:32:57 -0400 X-MC-Unique: -dPyDqwtMwC2yQrg4mppxA-1 X-Mimecast-MFC-AGG-ID: -dPyDqwtMwC2yQrg4mppxA_1747132376 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CE2F119560BF; Tue, 13 May 2025 10:32:55 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.72.112.55]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8AFB71800879; Tue, 13 May 2025 10:32:52 +0000 (UTC) From: Pingfan Liu To: kexec@lists.infradead.org, horms@verge.net.au Cc: Pingfan Liu Subject: [PATCH] kexec/zboot: Add boundary check on PE header offset Date: Tue, 13 May 2025 18:32:40 +0800 Message-ID: <20250513103240.16803-1-piliu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: cF3-mIO0_gtXMw6M5UrNvVzAPN51SHGX2-xNH7Kfb8o_1747132376 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250513_033301_145149_4DF2F5F7 X-CRM114-Status: GOOD ( 14.13 ) X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" Errors-To: kexec-bounces+kexec=archiver.kernel.org@lists.infradead.org If a buildroot enviorment does not provide libzstd and kexec_file_load a zstd compressed kernel, it will suffer from coredump. The following is observed from the coredump Core was generated by `/sbin/kexec -s -p --command-line=BOOT_IMAGE=\(hd0,gpt2\)/vmlinuz-6.15.0-0.rc5.250509g9c69f8884904.47.fc43.aarch64\ ro\ rootflags=subvol=root\ irqpoll\ nr_cpus=1\ reset_devices\ cgroup_disable=memory\ udev.children-max=2\ panic=10\ swiotlb=noforce\ novmcoredd\ cma=0\ hugetlb_cma=0\ sbsa_gwdt.pretimeout=0 --initrd=/boot/initramfs-6.15.0-0.rc5.250509g9c69f8884904.47.fc43.aarch64kdump.img /boot/vmlinuz-6.15.0-0.rc5.250509g9c69f8884904.47.fc43.aarch64'. Program terminated with signal SIGSEGV, Segmentation fault. 143 if (!!memcmp(buf, "PE\0\0", 4)) { (gdb) Instead of coredump, it is better to exit elegantly by adding checkout on the PE header offset. Signed-off-by: Pingfan Liu --- include/pe.h | 4 ++++ kexec/kexec-pe-zboot.c | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pe.h b/include/pe.h index a1aa91e..9ab3e79 100644 --- a/include/pe.h +++ b/include/pe.h @@ -139,6 +139,10 @@ static int get_pehdr_offset(const char *buf) int pe_hdr_offset; pe_hdr_offset = *((int *)(buf + 0x3c)); + if (pe_hdr_offset < 0 || pe_hdr_offset > 4096) { + printf("PE header offset is not a reasonable value\n"); + return -1; + } buf += pe_hdr_offset; if (!!memcmp(buf, "PE\0\0", 4)) { printf("Not a PE file\n"); diff --git a/kexec/kexec-pe-zboot.c b/kexec/kexec-pe-zboot.c index 8e17b4e..c09f2ae 100644 --- a/kexec/kexec-pe-zboot.c +++ b/kexec/kexec-pe-zboot.c @@ -100,7 +100,10 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd, dbgprintf("%s: decompressed size %ld\n", __func__, decompressed_size); /* Makefile.zboot pads Image with zero, but the trailing zero is not part of PE file */ - parse = kernel_uncompressed_buf + get_pehdr_offset(kernel_uncompressed_buf); + ret = get_pehdr_offset(kernel_uncompressed_buf); + if (ret < 0) + goto fail_bad_header; + parse = kernel_uncompressed_buf + ret; parse += sizeof(struct pe_hdr); opt_hdr = (struct pe32plus_opt_hdr*)parse; parse += sizeof(struct pe32plus_opt_hdr); -- 2.49.0