Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp2858543rdb; Tue, 12 Sep 2023 14:34:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHga1W5aJIHIUplCmnRe7B/UsxDJljF3rjsq/c1HhpoIg9gRCgfifPFJDqCwmYDfUq+fIMA X-Received: by 2002:a17:902:b7c3:b0:1c3:a1a8:9699 with SMTP id v3-20020a170902b7c300b001c3a1a89699mr885659plz.11.1694554496078; Tue, 12 Sep 2023 14:34:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694554496; cv=none; d=google.com; s=arc-20160816; b=Zfyn+HzfNF41RybggxU5Y5tZCJseBb1P7fxTvSFu+TlHZxb1tmnqBUJFte/Gg+KexL Pg7j5GwodwsPM7AuUXWRkJBfik9LHr6lgdwwuipaUJFRPsQdp1Hcz2BQHLO9MtS0Nws7 AmlqQthPull4NCAxU0kjZPohyAmc8uwV7s28uXuZTsnjvLt59uA/gECKAFkao7qMDg2H 4U5mxH7Pm1JEUPKzenlSiUo76tmfMFxC3F+knGXUtubO7JTTrLQR0yEfBbmelNUF0D+Z EFWl+317bvtie3ZyvY73zvl6ytBdEQN+uBHi2NgLQG+pRb/b3csspIv2cjzhmHpZcbaL DPiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=fe+/cU9QRFOgu9GR9ilVdqMZbBq1lerXnHnsmRtv0H4=; fh=WGxnbchXB1nh8WtfYTQqAYq5ggcl91wEfddYAfjE7VI=; b=FpopLQCaKB7ov+lgGAYHFc2oBVNWmTW/i0BlZmqGVCIL/tA1ArNmXS9vJNntKUX5U/ 27d5m0qf43aLpMxYvATDCgWgnSJCLdBmm+/ZML5crZFN+RUVVxIKUsZcWK6rQopR3CJV dCuRkd5o7fzSP/+/V7HmL80GC66oNa4VwyTvFxnLnSed7r3hbR6Iie7+y+wnLArlP+3d d3TBPQ0a2ZEMHBSl4y5k9lsFh689SkewIeVYJ5NEHFrcPJA8gxJUiYoeOlsepu4w1r5y 62a02kOBPCEIidXqLOS+044Y5G1NalpT+pUmng/lwOcTt3rZezwlaiJwHG1VkAbr5Vp4 zVXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YJoGIzx2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id ij5-20020a170902ab4500b001b9e38b8167si6767559plb.169.2023.09.12.14.34.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 14:34:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YJoGIzx2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 78545840FE10; Tue, 12 Sep 2023 02:02:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233498AbjILJCd (ORCPT + 99 others); Tue, 12 Sep 2023 05:02:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233606AbjILJBm (ORCPT ); Tue, 12 Sep 2023 05:01:42 -0400 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9787910EF for ; Tue, 12 Sep 2023 02:01:38 -0700 (PDT) Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-401d9abf21cso44365825e9.0 for ; Tue, 12 Sep 2023 02:01:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694509297; x=1695114097; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fe+/cU9QRFOgu9GR9ilVdqMZbBq1lerXnHnsmRtv0H4=; b=YJoGIzx2xv6fF76kQnFEf5B6eO1nIDRBvinjs+npO2z2r+8M1YLlMAQFgRTpqQtG5M AacTY4SiXUmoX48LTyEUzvjY3Koirci1ab5ALEvgm1JIOX++MD7T3pi/R35hLw4whhO/ 2o6I8FdViErsJa7CKVO+RNSDvKChQIKsZqYBc1SY7hJyWB447o8/joWvzzf7ZjDm0rYN 4Lrh7RkvCJ2tQrrUKLp1BH2oMUs4TVWtTTbC0gpYBrjOK+J7KH/mSc8ANTHHXAFTTKVT hTDZb3eH+FE/ZVQeqvKXuAaWPUIclgPPLq6VAXS1NKl/7XLpsc8HCkgsBihVF3rHEeKJ sfew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694509297; x=1695114097; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fe+/cU9QRFOgu9GR9ilVdqMZbBq1lerXnHnsmRtv0H4=; b=fbXWQBucjk++kQc4hA1DaOB4TAeAvGQ74NBWuQbRKdMPtuCT4GtQo4VaVRvsEdgwjJ rqRtw3LXYhbUiUN3rmbYsESIxVU3fRe3465l+uwWiaCazly9mqJpqbVVTrsG0NyfoSjt VjehuAZLmy6fiukOV3FKk0kSRymihNh9elokJv2J/uz7TuTt6J+3dPPtPOeX4EvsVunq 4ToLUZgVDsiUCFAgeP/0aAX3btmcrRm/HFEkNkXx9b3BBeI95JoeBY7sjn6fOVV7dW7X SFIuLrwpCI6fs1h2RD581cy4f4MStwoASoQqN6KlXL6rNQ/72kNqMt4hkb4IB8kjNTIW +N9Q== X-Gm-Message-State: AOJu0YyvCvwknyY+CTqeJNAdtkapjP0zgWDVt1unvA42y2xJj6XhuCU/ gpbjAJ7kpmdcYkaHLixtFCViD9WP X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6000:180b:b0:31d:3669:1c57 with SMTP id m11-20020a056000180b00b0031d36691c57mr136857wrh.7.1694509297239; Tue, 12 Sep 2023 02:01:37 -0700 (PDT) Date: Tue, 12 Sep 2023 09:00:56 +0000 In-Reply-To: <20230912090051.4014114-17-ardb@google.com> Mime-Version: 1.0 References: <20230912090051.4014114-17-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5240; i=ardb@kernel.org; h=from:subject; bh=wBN5p9iqf+zXksMG8YvWV0u77eSyD0wh6+R0/IxKHqc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZVB4yT3BsEV2T3uV2as43gkvCqwcY+CmPKvCv15TaUtb +60Wfp3lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgInskGJkeDgxQPP6+rfbDx88 kqpnGlQiJ6uiGLdrkX7l/PNzO4WYvjMyzBQ8Eh5bp/5KecW+kKzti8Wn6Kuvspny6kWFVlL9nhQ /HgA= X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912090051.4014114-22-ardb@google.com> Subject: [PATCH v2 05/15] x86/boot: Omit compression buffer from PE/COFF image memory footprint From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Dave Hansen , Ingo Molnar , Thomas Gleixner , Peter Jones , Matthew Garrett , Gerd Hoffmann , Kees Cook , "H. Peter Anvin" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 02:02:36 -0700 (PDT) From: Ard Biesheuvel Now that the EFI stub decompresses the kernel and hands over to the decompressed image directly, there is no longer a need to provide a decompression buffer as part of the .BSS allocation of the PE/COFF image. It also means the PE/COFF image can be loaded anywhere in memory, and setting the preferred image base is unnecessary. So drop the handling of this from the header and from the build tool. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/header.S | 6 +-- arch/x86/boot/tools/build.c | 50 +++----------------- 2 files changed, 8 insertions(+), 48 deletions(-) diff --git a/arch/x86/boot/header.S b/arch/x86/boot/header.S index b24fa50a9898..a87d9133384b 100644 --- a/arch/x86/boot/header.S +++ b/arch/x86/boot/header.S @@ -90,12 +90,10 @@ optional_header: #endif extra_header_fields: - # PE specification requires ImageBase to be 64k aligned - .set image_base, (LOAD_PHYSICAL_ADDR + 0xffff) & ~0xffff #ifdef CONFIG_X86_32 - .long image_base # ImageBase + .long 0 # ImageBase #else - .quad image_base # ImageBase + .quad 0 # ImageBase #endif .long 0x20 # SectionAlignment .long 0x20 # FileAlignment diff --git a/arch/x86/boot/tools/build.c b/arch/x86/boot/tools/build.c index bd247692b701..0354c223e354 100644 --- a/arch/x86/boot/tools/build.c +++ b/arch/x86/boot/tools/build.c @@ -65,7 +65,6 @@ static unsigned long efi_pe_entry; static unsigned long efi32_pe_entry; static unsigned long kernel_info; static unsigned long startup_64; -static unsigned long _ehead; static unsigned long _end; /*----------------------------------------------------------------------*/ @@ -229,27 +228,14 @@ static void update_pecoff_setup_and_reloc(unsigned int size) #endif } -static void update_pecoff_text(unsigned int text_start, unsigned int file_sz, - unsigned int init_sz) +static void update_pecoff_text(unsigned int text_start, unsigned int file_sz) { unsigned int pe_header; unsigned int text_sz = file_sz - text_start; - unsigned int bss_sz = init_sz - file_sz; + unsigned int bss_sz = _end - text_sz; pe_header = get_unaligned_le32(&buf[0x3c]); - /* - * The PE/COFF loader may load the image at an address which is - * misaligned with respect to the kernel_alignment field in the setup - * header. - * - * In order to avoid relocating the kernel to correct the misalignment, - * add slack to allow the buffer to be aligned within the declared size - * of the image. - */ - bss_sz += CONFIG_PHYSICAL_ALIGN; - init_sz += CONFIG_PHYSICAL_ALIGN; - /* * Size of code: Subtract the size of the first sector (512 bytes) * which includes the header. @@ -257,7 +243,7 @@ static void update_pecoff_text(unsigned int text_start, unsigned int file_sz, put_unaligned_le32(file_sz - 512 + bss_sz, &buf[pe_header + 0x1c]); /* Size of image */ - put_unaligned_le32(init_sz, &buf[pe_header + 0x50]); + put_unaligned_le32(file_sz + bss_sz, &buf[pe_header + 0x50]); /* * Address of entry point for PE/COFF executable @@ -308,8 +294,7 @@ static void efi_stub_entry_update(void) static inline void update_pecoff_setup_and_reloc(unsigned int size) {} static inline void update_pecoff_text(unsigned int text_start, - unsigned int file_sz, - unsigned int init_sz) {} + unsigned int file_sz) {} static inline void efi_stub_defaults(void) {} static inline void efi_stub_entry_update(void) {} @@ -360,7 +345,6 @@ static void parse_zoffset(char *fname) PARSE_ZOFS(p, efi32_pe_entry); PARSE_ZOFS(p, kernel_info); PARSE_ZOFS(p, startup_64); - PARSE_ZOFS(p, _ehead); PARSE_ZOFS(p, _end); p = strchr(p, '\n'); @@ -371,7 +355,7 @@ static void parse_zoffset(char *fname) int main(int argc, char ** argv) { - unsigned int i, sz, setup_sectors, init_sz; + unsigned int i, sz, setup_sectors; int c; u32 sys_size; struct stat sb; @@ -442,31 +426,9 @@ int main(int argc, char ** argv) buf[0x1f1] = setup_sectors-1; put_unaligned_le32(sys_size, &buf[0x1f4]); - init_sz = get_unaligned_le32(&buf[0x260]); -#ifdef CONFIG_EFI_STUB - /* - * The decompression buffer will start at ImageBase. When relocating - * the compressed kernel to its end, we must ensure that the head - * section does not get overwritten. The head section occupies - * [i, i + _ehead), and the destination is [init_sz - _end, init_sz). - * - * At present these should never overlap, because 'i' is at most 32k - * because of SETUP_SECT_MAX, '_ehead' is less than 1k, and the - * calculation of INIT_SIZE in boot/header.S ensures that - * 'init_sz - _end' is at least 64k. - * - * For future-proofing, increase init_sz if necessary. - */ - - if (init_sz - _end < i + _ehead) { - init_sz = (i + _ehead + _end + 4095) & ~4095; - put_unaligned_le32(init_sz, &buf[0x260]); - } -#endif - update_pecoff_text(setup_sectors * 512, i + (sys_size * 16), init_sz); + update_pecoff_text(setup_sectors * 512, i + (sys_size * 16)); efi_stub_entry_update(); - /* Update kernel_info offset. */ put_unaligned_le32(kernel_info, &buf[0x268]); -- 2.42.0.283.g2d96d420d3-goog