From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1okrvA-0007YE-1z for mharc-grub-devel@gnu.org; Tue, 18 Oct 2022 15:06:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47136) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okruW-0007NU-Nl for grub-devel@gnu.org; Tue, 18 Oct 2022 15:05:58 -0400 Received: from ams.source.kernel.org ([145.40.68.75]:54308) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okru9-0000bl-Ss for grub-devel@gnu.org; Tue, 18 Oct 2022 15:05:39 -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 4CC0BB820EF; Tue, 18 Oct 2022 19:05:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6DB9C433B5; Tue, 18 Oct 2022 19:05:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666119928; bh=yG8OQ0QK7Te5gTLHaEkOwwmBodIcLow7jVk5f3W9hLA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QPkN22DhNb+AR/3EDX0wN3ZQCcr6Hl6rP6iVZyvPokMk2vSL+oBVmqrxEed1OVxB1 b1DxlpgVOfnyk4yQEdQUsBUmDlZCDgFbZJkkXl2Zdgc3FkrahqTeoE3Dcu61k8+ugx mIaJa7c51IVmfc5h6srWdqI3tg3JLuGb+In+0Mqy4R7ZVpRFIkUn/atUeoZE+jjkTi Yt9dZJN+wu66T78N/fmsfm8q0UE0PrgoMMDVc1mSD576yQ1kSvT73Gh8FYoKOZpORL AsA0ktwQJYwa+AivNc2h3w4foB8e6qBJCV70ngu0uaWxHzxyoqX8fNWLpEslm2wnS1 nf1LlzZj+fBdQ== 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 , Ilias Apalodimas Subject: [PATCH v5 3/6] linux/arm: account for COFF headers appearing at unexpected offsets Date: Tue, 18 Oct 2022 21:05:04 +0200 Message-Id: <20221018190508.177568-4-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018190508.177568-1-ardb@kernel.org> References: <20221018190508.177568-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1779; i=ardb@kernel.org; h=from:subject; bh=yG8OQ0QK7Te5gTLHaEkOwwmBodIcLow7jVk5f3W9hLA=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTvjdelFFLbIGJDvqIl6rxYID+g8b5J8gUdOMGOGw +ewYxE6JAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY0743QAKCRDDTyI5ktmPJDpCC/ 0XQKVMMIIUGW39KaXFZ8bQLPB891em0CTQXEDPpg9WppNYIIYW3TXI0zpNBm5HraWMDgZgjsa1Mm8C kO6ovxz9XihV93g7y9zSjW7H8gXT8t4HC1Te4MjOOdUEVgyg7XAThpsA+qet50mpRU1Nvgk390FOqU JYZmwgry+oXvmiZQozWREgPJvrfXL5CYp/GyhPvdTqJXNDOyPM7DtpYUqV62O78ioNXYPypW6ZpZyH 4a7pMFYKIFPf8NzPPfPhOO537QvNbU+R402RSFEe7p4x1p9jYqmS1fT67H4sag6zJg5QTFN8TAHj5f xPt5ZPoejJq7t0YdBGxRIh9GtSvlpOODJS8YlTMl1DmSkSfg9SnCAIEH7cs+oAUjZs0q1ROmIaPkO+ w05C/wIw23jDNi7Ffb1tg2xWr/lcXUBXm/4Tr50xuK8niXoS4G95BP9QD9kztamiNC7oL+9Qu7qmGX gwZnd2C/yUL/H7ppjpd0lGq9n/+CAHnLFD1vFuhzuisVg= 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: -73 X-Spam_score: -7.4 X-Spam_bar: ------- X-Spam_report: (-7.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.256, 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 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: Tue, 18 Oct 2022 19:06:00 -0000 X-List-Received-Date: Tue, 18 Oct 2022 19:06:00 -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 3733a69d6e36..754db84a96cf 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 pe_image_header field of + * struct linux_arch_kernel_header. + */ + if ((grub_uint8_t *) lh + lh->hdr_offset != (grub_uint8_t *) &lh->pe_image_header) + { + if (grub_file_seek (file, lh->hdr_offset) == (grub_off_t) -1 + || grub_file_read (file, &lh->pe_image_header, + sizeof (struct grub_pe_image_header)) + != sizeof (struct grub_pe_image_header)) + return grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header"); + } + return GRUB_ERR_NONE; } -- 2.35.1