Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oOgu2-00052g-37 for mharc-grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39296) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oOgu0-0004vm-Ii for grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:44 -0400 Received: from ams.source.kernel.org ([145.40.68.75]:58040) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oOgtu-0001K8-23 for grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:42 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 33CD5B821C8; Thu, 18 Aug 2022 14:53:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D532EC433C1; Thu, 18 Aug 2022 14:53:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660834414; bh=a+eFr/WWVWSZD8tqmOLBJll8Nv1OKzhzVxe2gHJjaUY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=erPH73DtnjfTCRgvwz0eaOE7IGcKM766bE/5HuKLJvgHJgJScBAcYp/5iVBjpJRhC r5eSAbfvqdvO6nqQx+X+D3J6+wl5u02DIOGS1/drY46PaUJIPAbZWYFVYS1/M68gV6 kjO8ZT/+DaeIaOEald2alxyD/0slT8M9RIyMmpChXXt8zpteNVu9/k/O+Ux1FjRozV rpdz5FYf6LPCHTDTF6kk6LP18A0lKT0O86wS/Wfh5zR+bgfIONnhS7NHok78zeQ6R7 gtbyrycKO0gj81xCFezeO6Ly4Hw2+rl3kN33osIXqqhcO0R3Wfsj9Y/1wO15SAQrtU cvRxdCkjJvgjg== From: Ard Biesheuvel To: grub-devel@gnu.org Cc: Ard Biesheuvel , Daniel Kiper , Leif Lindholm , Nikita Ermakov , Atish Patra , Huacai Chen , Heinrich Schuchardt , dann frazier , Julian Andres Klode Subject: [PATCH v3 5/8] linux/arm: account for COFF headers appearing at unexpected offsets Date: Thu, 18 Aug 2022 16:51:32 +0200 Message-Id: <20220818145135.2208251-6-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220818145135.2208251-1-ardb@kernel.org> References: <20220818145135.2208251-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1728; i=ardb@kernel.org; h=from:subject; bh=a+eFr/WWVWSZD8tqmOLBJll8Nv1OKzhzVxe2gHJjaUY=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/lHufuSyB/c1hJQvT4WLgOeT3CQgUVdcUMdP0I4U 6vQwkCWJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv5R7gAKCRDDTyI5ktmPJJqRDA C7xMGYsZRFS39mlF1KVNgTynJ1r00rfR1G5cLY2UFqbcbPGGgxxeoy2NFGJWm0uOo2moEf9HkvP5N0 aiMrY+xv17l3QNXFNIsh5zxL5ULTNqWXnnz327tHM39TgguGaHVc33ELXQu70/X1yWxC8FYL+zRo5I hsbMUsB10ip7jGdxVduv9k3xpZlUMyqxI7isOSslRiEBFJdXLG9BJP3gVcuBHjL+gb+DQcCOzZ1uNk 1ZoKrAzoIk4rhavV3IW/JXwP6SYkxZt2UYe79bdy4RrtctIkxC2+BEgw0A9l2DyqmLnPTyYy8SUqs9 oTMjZXH8k3wMk+gU59E6b/t/xZnseQ1VOh1KfGD84s2mZuFvg+ofoynNSHY4/hQ+6N3emZeeWy8YgK 6gHwPUtt5DOpqcvvKOHiExLVAXdXImBzkJcqMB+4g45fWEzimpojEWac44fW9w25I0InG7yoDuKE3F Bq+K6Rpvwm4DPpHApKVbeEXeI95+e5SHJqvPufcELjk2M= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=145.40.68.75; envelope-from=ardb@kernel.org; helo=ams.source.kernel.org X-Spam_score_int: -71 X-Spam_score: -7.2 X-Spam_bar: ------- X-Spam_report: (-7.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Aug 2022 14:53:44 -0000 The way we load the Linux and PE/COFF image headers depends on a fixed placement of the COFF header at offset 0x40 into the file. This is a reasonable default, given that this is where Linux emits it today. However, in order to comply with the PE/COFF spec, which allows this header to appear anywhere in the file, let's ensure that we read the header from where it actually appears in the file if it is not located at offset 0x40. Signed-off-by: Ard Biesheuvel --- grub-core/loader/arm64/linux.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 7c0f17cf933d..56ba8d0a6ea3 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -63,6 +63,21 @@ grub_arch_efi_linux_load_image_header (grub_file_t file, grub_dprintf ("linux", "UEFI stub kernel:\n"); grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); + /* + * The PE/COFF spec permits the COFF header to appear anywhere in the file, so + * we need to double check whether it was where we expected it, and if not, we + * must load it from the correct offset into the coff_image_header field of + * struct linux_arch_kernel_header. + */ + if ((grub_uint8_t *) lh + lh->hdr_offset != (grub_uint8_t *) &lh->coff_image_header) + { + grub_file_seek (file, lh->hdr_offset); + + if (grub_file_read (file, &lh->coff_image_header, sizeof(struct grub_coff_image_header)) + != sizeof(struct grub_coff_image_header)) + return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header"); + } + return GRUB_ERR_NONE; } -- 2.35.1