Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oU2vW-00064g-OH for mharc-grub-devel@gnu.org; Fri, 02 Sep 2022 05:25:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59816) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oU2vV-000628-9B for grub-devel@gnu.org; Fri, 02 Sep 2022 05:25:25 -0400 Received: from smtp-relay-internal-1.canonical.com ([185.125.188.123]:55066) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oU2vR-0000O7-TY for grub-devel@gnu.org; Fri, 02 Sep 2022 05:25:24 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (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 smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 3347E3F0E8 for ; Fri, 2 Sep 2022 09:25:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1662110718; bh=uJSnJ8vHN4eahCv0Prb52yhRGul/sCHF8zgHZnGBGPE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=n6Z+J8DJBWjiUNDWJCspclGyrXk8dQfKBjfRWKOGjL2O6dbZBwW+cOnq34suueUT/ HlbSlXcYATXEdF10IQwoO+HeXmdTi9zTampy6SYs79wxUvckkb8G2RPnWQtrjylM6v ofS/dQduZ07cJAGvqUtoYZJ7ev4FkeNvqdDo9khj9b1rjB18XERk41e6ej7eF0jvp+ aWnnyBv2PBtTNoCwnbosnVUR1faaEC+fa+oB+6V9Zd+j6xplc+sQSc2wKKBcESE08v iUWZVPVrNC85ksi+iMKXmA5G28zhVzCu/ggsFu6MV7YSLN2pPSQDacl29Dvi9RlfW8 zRVaHofU2WA6A== Received: by mail-ej1-f69.google.com with SMTP id dr17-20020a170907721100b00741a1ef8a20so805095ejc.0 for ; Fri, 02 Sep 2022 02:25:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=uJSnJ8vHN4eahCv0Prb52yhRGul/sCHF8zgHZnGBGPE=; b=VezbVnHR54G7Sr0ddq5HN+lRQDN6IZ2xbYhNYFFdwCiqCJ9w/vvTcz4cUW5lwZXwKH DDI/ln2GWbGGhRJJCRCtjumZZpbIzlzxoErg2ExT0y8V3E7LIxVx7WZXzXZuP3ehtm7L +EFyAkwR6t9MP9UHiwHSnCxqVO2e/Ov/7MnLySgZr8FZko7VnYMar/Mi5GhP42KgEY41 FakR6XfMZk1KRmn7NjymfPVRWnh4sY4/6n4+PIp9WezA1zu3k9JysxnvsQ7EVD3EkEXN YWjvJaYNMShrOPfFLA/Og9xIlhf+bmtRC4d8OwsU7uk/EGBboDNvHOaWP+A+f7WLHSo1 pHSw== X-Gm-Message-State: ACgBeo0+2e9DNfAwn2kYB1+ofFquqglX9+y5Z+f2yxzkTsUhs/lfMcpD 1nhiS0cfG9F4NFd1KMMNKFje2m1FuZs0az46C6/l9rgiVyU4VFBiJ5hiXVCXcgc92nHlsL+zMY/ sgiQUwu7Sf05g3u+e1BLdrawOXvyG X-Received: by 2002:aa7:cb87:0:b0:43b:e650:6036 with SMTP id r7-20020aa7cb87000000b0043be6506036mr32850131edt.350.1662110717532; Fri, 02 Sep 2022 02:25:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR7ByMYk4TGJKT25q6qYDo2ge6igL5INEgRIL7Z8RcqmQc0wWAguA/C3a1mMkIRB9pY5YzMYaw== X-Received: by 2002:aa7:cb87:0:b0:43b:e650:6036 with SMTP id r7-20020aa7cb87000000b0043be6506036mr32850114edt.350.1662110717279; Fri, 02 Sep 2022 02:25:17 -0700 (PDT) Received: from [192.168.123.67] (ip-084-118-157-002.um23.pools.vodafone-ip.de. [84.118.157.2]) by smtp.gmail.com with ESMTPSA id q7-20020a170906144700b007389c5a45f0sm953261ejc.148.2022.09.02.02.25.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Sep 2022 02:25:16 -0700 (PDT) Message-ID: <4b110e0d-32bb-4167-c5cd-8a43bfb24562@canonical.com> Date: Fri, 2 Sep 2022 11:25:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.1 Subject: Re: [PATCH v3 2/8] efi: move MS-DOS stub out of generic PE header definition Content-Language: en-US To: Ard Biesheuvel Cc: Daniel Kiper , Leif Lindholm , Nikita Ermakov , Atish Patra , Huacai Chen , dann frazier , Julian Andres Klode , grub-devel@gnu.org References: <20220818145135.2208251-1-ardb@kernel.org> <20220818145135.2208251-3-ardb@kernel.org> From: Heinrich Schuchardt In-Reply-To: <20220818145135.2208251-3-ardb@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=185.125.188.123; envelope-from=heinrich.schuchardt@canonical.com; helo=smtp-relay-internal-1.canonical.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_MED=-2.3, 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: Fri, 02 Sep 2022 09:25:25 -0000 On 8/18/22 16:51, Ard Biesheuvel wrote: > The PE/COFF spec permits the COFF signature and file header to appear > anywhere in the file, and the actual offset is recorded in 4 byte > little endian field at offset 0x3c of the image. > > When GRUB is emitted as a PE/COFF binary, we reuse the 128 byte MS-DOS > stub (even for non-x86 architectures), putting the COFF signature and > file header at offset 0x80. However, other PE/COFF images may use > different values, and non-x86 Linux kernels use an offset of 0x40 > instead. > > So let's get rid of the grub_pe32_header struct from pe32.h, given that > it does not represent anything defined by the PE/COFF spec. Instead, > use the GRUB_PE32_MSDOS_STUB_SIZE macro explicitly to reference the > COFF header in the only place in the code where we rely on this. > > The remaining fields are moved into a struct grub_coff_image_header, > which we will use later to access COFF header fields of arbitrary > images (and which may therefore appear at different offsets) > > Signed-off-by: Ard Biesheuvel > --- > grub-core/kern/efi/efi.c | 5 +++-- > include/grub/efi/pe32.h | 5 +---- > 2 files changed, 4 insertions(+), 6 deletions(-) > > diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c > index e8a976a22f15..8bef81663853 100644 > --- a/grub-core/kern/efi/efi.c > +++ b/grub-core/kern/efi/efi.c > @@ -302,7 +302,7 @@ grub_addr_t > grub_efi_modules_addr (void) > { > grub_efi_loaded_image_t *image; > - struct grub_pe32_header *header; > + struct grub_coff_image_header *header; > struct grub_pe32_coff_header *coff_header; > struct grub_pe32_section_table *sections; > struct grub_pe32_section_table *section; > @@ -313,7 +313,8 @@ grub_efi_modules_addr (void) > if (! image) > return 0; > > - header = image->image_base; > + header = (struct grub_coff_image_header *) ((char *) image->image_base > + + GRUB_PE32_MSDOS_STUB_SIZE); As you described above the offset to the PE-COFF header isn't a constant. Please, cast image->image_base to struct IMAGE_DOS_HEADER * and use the value of e_lfanew instead of GRUB_PE32_MSDOS_STUB_SIZE here. You can copy the definjtion of struct IMAGE_DOS_HEADER from gnu-efi's /usr/include/efi/x86_64/pe.h. Best regards Heinrich > coff_header = &(header->coff_header); > sections > = (struct grub_pe32_section_table *) ((char *) coff_header > diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h > index 0ed8781f0376..a2da4b318c85 100644 > --- a/include/grub/efi/pe32.h > +++ b/include/grub/efi/pe32.h > @@ -254,11 +254,8 @@ struct grub_pe32_section_table > > #define GRUB_PE32_SIGNATURE_SIZE 4 > > -struct grub_pe32_header > +struct grub_coff_image_header > { > - /* This should be filled in with GRUB_PE32_MSDOS_STUB. */ > - grub_uint8_t msdos_stub[GRUB_PE32_MSDOS_STUB_SIZE]; > - > /* This is always PE\0\0. */ > char signature[GRUB_PE32_SIGNATURE_SIZE]; >