From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oWHca-00088h-Cs for mharc-grub-devel@gnu.org; Thu, 08 Sep 2022 09:31:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56056) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWHcN-0007xh-FE for grub-devel@gnu.org; Thu, 08 Sep 2022 09:30:57 -0400 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]:53486) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWHcL-00037r-QX for grub-devel@gnu.org; Thu, 08 Sep 2022 09:30:55 -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 2B4ACB820F5; Thu, 8 Sep 2022 13:30:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0CEAC433B5; Thu, 8 Sep 2022 13:30:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662643850; bh=a+eFr/WWVWSZD8tqmOLBJll8Nv1OKzhzVxe2gHJjaUY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nAm6j42M2QV0EzAWZlMIh4B2PFAqtHqasQGCQi4QBCj53r8ud2gzEaTPqcmXetgg8 ueJIp7j6t3RiNEG8zkV2Mwkyn6czgPtVpWKSCQXSSmqlH0PkNtQVE2dEwzdJfZBsOm yJWdneoG7tzNcnP+DMIU2CW/NbdQTGd8bZIZ8sf31fMu0wyLZGFTtLemVhVKQBCu7a C5YnKOWFvOfES92+nU5WdbgZ8C3zi7fDMx1rd4mPgR7Z0bHJB2S22jra9rOiB1XGMx Qoj8ODl3ru+QlchhfUux2AkM5mqn815/3oIP89ILyGIAugZDwgy1LUr1WuXS3Wlu4t mmRjwKP49j7fg== 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 v4 3/6] linux/arm: account for COFF headers appearing at unexpected offsets Date: Thu, 8 Sep 2022 15:30:14 +0200 Message-Id: <20220908133017.1464494-4-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220908133017.1464494-1-ardb@kernel.org> References: <20220908133017.1464494-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/pANAwAKAcNPIjmS2Y8kAcsmYgBjGe5jfuSyB/c1hJQvT4WLgOeT3CQgUVdcUMdP0I4U 6vQwkCWJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYxnuYwAKCRDDTyI5ktmPJKuXDA ClSCybaPDMnp43AAqqnjJ2/SThtaYq59wsT1c6NHU46V/7VZ5wWXU7zDjOYd4DFRy6OkFMzwvIkUCZ iXdJn5O2ruV9psZgFTeHwgwxM7vBd5b03PcS07cExdevph71zCIyE+IQEay1N856jG2Uyj0fjPzdmS gZ9v7dsiqsGyeknbtfsnQcGIbo0h2GvPTSNPc7Xv2lCtzpjEtFlMxF2kr1Inwu16O1kKhG9yQEJNfn PP50vgCoyulgKF7P1OeKre/JNXM0qP5NvABqmzr8l7ftQjIFZh3w1NaWax1ZGvUwXIbT8l6NENrqmp G8Gz8dSHyiJAqXKaGqFESYMTPXrX5PtVMKCngj/t919IEv4I5M+tmCdLdVBGclWmamcC76OHIzjsSD +DzQHfy39neRZ6lGUbIT5vyBuoSPt30krWdMRb7NFh3I9lYBvfGN16ZonCD9nd7UPctrvojOpPyhPy mefJLl+zG6hdFl4ARcgdXk5TAY8ggyk72DYM3VvQhLFsg= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2604:1380:4601:e00::1; envelope-from=ardb@kernel.org; helo=ams.source.kernel.org X-Spam_score_int: -70 X-Spam_score: -7.1 X-Spam_bar: ------- X-Spam_report: (-7.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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, 08 Sep 2022 13:30:57 -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