linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.10 69/96] efi/libstub: arm64: Force Image reallocation if BSS was not reserved
Date: Mon, 16 Aug 2021 15:02:19 +0200	[thread overview]
Message-ID: <20210816125437.267104668@linuxfoundation.org> (raw)
In-Reply-To: <20210816125434.948010115@linuxfoundation.org>

From: Ard Biesheuvel <ardb@kernel.org>

[ Upstream commit 5b94046efb4706b3429c9c8e7377bd8d1621d588 ]

Distro versions of GRUB replace the usual LoadImage/StartImage calls
used to load the kernel image with some local code that fails to honor
the allocation requirements described in the PE/COFF header, as it
does not account for the image's BSS section at all: it fails to
allocate space for it, and fails to zero initialize it.

Since the EFI stub itself is allocated in the .init segment, which is
in the middle of the image, its BSS section is not impacted by this,
and the main consequence of this omission is that the BSS section may
overlap with memory regions that are already used by the firmware.

So let's warn about this condition, and force image reallocation to
occur in this case, which works around the problem.

Fixes: 82046702e288 ("efi/libstub/arm64: Replace 'preferred' offset with alignment check")
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Tested-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/firmware/efi/libstub/arm64-stub.c | 49 ++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c
index 22ece1ad68a8..3dc54b9db054 100644
--- a/drivers/firmware/efi/libstub/arm64-stub.c
+++ b/drivers/firmware/efi/libstub/arm64-stub.c
@@ -34,6 +34,51 @@ efi_status_t check_platform_features(void)
 	return EFI_SUCCESS;
 }
 
+/*
+ * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
+ * to provide space, and fail to zero it). Check for this condition by double
+ * checking that the first and the last byte of the image are covered by the
+ * same EFI memory map entry.
+ */
+static bool check_image_region(u64 base, u64 size)
+{
+	unsigned long map_size, desc_size, buff_size;
+	efi_memory_desc_t *memory_map;
+	struct efi_boot_memmap map;
+	efi_status_t status;
+	bool ret = false;
+	int map_offset;
+
+	map.map =	&memory_map;
+	map.map_size =	&map_size;
+	map.desc_size =	&desc_size;
+	map.desc_ver =	NULL;
+	map.key_ptr =	NULL;
+	map.buff_size =	&buff_size;
+
+	status = efi_get_memory_map(&map);
+	if (status != EFI_SUCCESS)
+		return false;
+
+	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
+		efi_memory_desc_t *md = (void *)memory_map + map_offset;
+		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
+
+		/*
+		 * Find the region that covers base, and return whether
+		 * it covers base+size bytes.
+		 */
+		if (base >= md->phys_addr && base < end) {
+			ret = (base + size) <= end;
+			break;
+		}
+	}
+
+	efi_bs_call(free_pool, memory_map);
+
+	return ret;
+}
+
 /*
  * Although relocatable kernels can fix up the misalignment with respect to
  * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
@@ -92,7 +137,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	}
 
 	if (status != EFI_SUCCESS) {
-		if (IS_ALIGNED((u64)_text, min_kimg_align())) {
+		if (!check_image_region((u64)_text, kernel_memsize)) {
+			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
+		} else if (IS_ALIGNED((u64)_text, min_kimg_align())) {
 			/*
 			 * Just execute from wherever we were loaded by the
 			 * UEFI PE/COFF loader if the alignment is suitable.
-- 
2.30.2




  parent reply	other threads:[~2021-08-16 13:14 UTC|newest]

Thread overview: 112+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-16 13:01 [PATCH 5.10 00/96] 5.10.60-rc1 review Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 01/96] iio: adc: ti-ads7950: Ensure CS is deasserted after reading channels Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 02/96] iio: adis: set GPIO reset pin direction Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 03/96] iio: humidity: hdc100x: Add margin to the conversion time Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 04/96] iio: adc: Fix incorrect exit of for-loop Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 05/96] ASoC: amd: Fix reference to PCM buffer address Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 06/96] ASoC: xilinx: " Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 07/96] ASoC: uniphier: " Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 08/96] ASoC: tlv320aic31xx: Fix jack detection after suspend Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 09/96] ASoC: intel: atom: Fix reference to PCM buffer address Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 10/96] i2c: dev: zero out array used for i2c reads from userspace Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 11/96] cifs: create sd context must be a multiple of 8 Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 12/96] scsi: lpfc: Move initialization of phba->poll_list earlier to avoid crash Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 13/96] seccomp: Fix setting loaded filter count during TSYNC Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 14/96] net: ethernet: ti: cpsw: fix min eth packet size for non-switch use-cases Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 15/96] ARC: fp: set FPU_STATUS.FWE to enable FPU_STATUS update on context switch Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 16/96] ceph: reduce contention in ceph_check_delayed_caps() Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 17/96] ACPI: NFIT: Fix support for virtual SPA ranges Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 18/96] libnvdimm/region: Fix label activation vs errors Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 19/96] drm/amd/display: Remove invalid assert for ODM + MPC case Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 20/96] drm/amd/display: use GFP_ATOMIC in amdgpu_dm_irq_schedule_work Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 21/96] drm/amdgpu: dont enable baco on boco platforms in runpm Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 22/96] ieee802154: hwsim: fix GPF in hwsim_set_edge_lqi Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 23/96] ieee802154: hwsim: fix GPF in hwsim_new_edge_nl Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 24/96] pinctrl: mediatek: Fix fallback behavior for bias_set_combo Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 25/96] ASoC: cs42l42: Correct definition of ADC Volume control Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 26/96] ASoC: cs42l42: Dont allow SND_SOC_DAIFMT_LEFT_J Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 27/96] interconnect: qcom: icc-rpmh: Add BCMs to commit list in pre_aggregate Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 28/96] ASoC: SOF: Intel: hda-ipc: fix reply size checking Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 29/96] ASoC: cs42l42: Fix inversion of ADC Notch Switch control Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 30/96] ASoC: cs42l42: Remove duplicate control for WNF filter frequency Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 31/96] netfilter: nf_conntrack_bridge: Fix memory leak when error Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 32/96] pinctrl: tigerlake: Fix GPIO mapping for newer version of software Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 33/96] ASoC: cs42l42: Fix LRCLK frame start edge Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 34/96] net: dsa: mt7530: add the missing RxUnicast MIB counter Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 35/96] net: mvvp2: fix short frame size on s390 Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 36/96] platform/x86: pcengines-apuv2: Add missing terminating entries to gpio-lookup tables Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 37/96] libbpf: Fix probe for BPF_PROG_TYPE_CGROUP_SOCKOPT Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 38/96] bpf: Fix integer overflow involving bucket_size Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 39/96] net: phy: micrel: Fix link detection on ksz87xx switch" Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 40/96] ppp: Fix generating ifname when empty IFLA_IFNAME is specified Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 41/96] net/smc: fix wait on already cleared link Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 42/96] net: sched: act_mirred: Reset ct info when mirror/redirect skb Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 43/96] ice: Prevent probing virtual functions Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 44/96] ice: dont remove netdev->dev_addr from uc sync list Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 45/96] iavf: Set RSS LUT and key in reset handle path Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 46/96] psample: Add a fwd declaration for skbuff Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 47/96] bareudp: Fix invalid read beyond skbs linear data Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 48/96] net/mlx5: Synchronize correct IRQ when destroying CQ Greg Kroah-Hartman
2021-08-16 13:01 ` [PATCH 5.10 49/96] net/mlx5: Fix return value from tracer initialization Greg Kroah-Hartman
2021-08-17 17:51   ` Pavel Machek
2021-08-18  7:44     ` Saeed Mahameed
2021-08-18 19:28       ` Pavel Machek
2021-08-16 13:02 ` [PATCH 5.10 50/96] drm/meson: fix colour distortion from HDR set during vendor u-boot Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 51/96] net: dsa: microchip: Fix ksz_read64() Greg Kroah-Hartman
2021-08-17 17:56   ` Pavel Machek
2021-08-17 18:20     ` Ben Hutchings
2021-08-18 19:26       ` Pavel Machek
2021-08-16 13:02 ` [PATCH 5.10 52/96] net: dsa: microchip: ksz8795: Fix VLAN filtering Greg Kroah-Hartman
2021-08-16 13:28   ` Ben Hutchings
2021-08-16 16:27     ` Greg Kroah-Hartman
2021-08-16 17:49       ` Ben Hutchings
2021-08-16 18:09         ` [PATCH 5.10 1/5] net: dsa: microchip: Fix probing KSZ87xx switch with DT node for host port Ben Hutchings
2021-08-16 18:09         ` [PATCH 5.10 2/5] net: dsa: microchip: ksz8795: Fix PVID tag insertion Ben Hutchings
2021-08-16 18:09         ` [PATCH 5.10 3/5] net: dsa: microchip: ksz8795: Reject unsupported VLAN configuration Ben Hutchings
2021-08-16 18:09         ` [PATCH 5.10 4/5] net: dsa: microchip: ksz8795: Fix VLAN untagged flag change on deletion Ben Hutchings
2021-08-16 18:09         ` [PATCH 5.10 5/5] net: dsa: microchip: ksz8795: Use software untagging on CPU port Ben Hutchings
2021-08-16 19:22         ` [PATCH 5.10 52/96] net: dsa: microchip: ksz8795: Fix VLAN filtering Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 53/96] net: Fix memory leak in ieee802154_raw_deliver Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 54/96] net: igmp: fix data-race in igmp_ifc_timer_expire() Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 55/96] net: dsa: lan9303: fix broken backpressure in .port_fdb_dump Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 56/96] net: dsa: lantiq: " Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 57/96] net: dsa: sja1105: " Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 58/96] net: bridge: validate the NUD_PERMANENT bit when adding an extern_learn FDB entry Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 59/96] net: bridge: fix flags interpretation for extern learn fdb entries Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 60/96] net: bridge: fix memleak in br_add_if() Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 61/96] net: linkwatch: fix failure to restore device state across suspend/resume Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 62/96] tcp_bbr: fix u32 wrap bug in round logic if bbr_init() called after 2B packets Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 63/96] net: igmp: increase size of mr_ifc_count Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 64/96] drm/i915: Only access SFC_DONE when media domain is not fused off Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 65/96] xen/events: Fix race in set_evtchn_to_irq Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 66/96] vsock/virtio: avoid potential deadlock when vsock device remove Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 67/96] nbd: Aovid double completion of a request Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 68/96] arm64: efi: kaslr: Fix occasional random alloc (and boot) failure Greg Kroah-Hartman
2021-08-16 13:02 ` Greg Kroah-Hartman [this message]
2021-08-16 13:02 ` [PATCH 5.10 70/96] efi/libstub: arm64: Relax 2M alignment again for relocatable kernels Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 71/96] powerpc/kprobes: Fix kprobe Oops happens in booke Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 72/96] x86/tools: Fix objdump version check again Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 73/96] genirq: Provide IRQCHIP_AFFINITY_PRE_STARTUP Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 74/96] x86/msi: Force affinity setup before startup Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 75/96] x86/ioapic: " Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 76/96] x86/resctrl: Fix default monitoring groups reporting Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 77/96] genirq/msi: Ensure deactivation on teardown Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 78/96] genirq/timings: Prevent potential array overflow in __irq_timings_store() Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 79/96] PCI/MSI: Enable and mask MSI-X early Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 80/96] PCI/MSI: Mask all unused MSI-X entries Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 81/96] PCI/MSI: Enforce that MSI-X table entry is masked for update Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 82/96] PCI/MSI: Enforce MSI[X] entry updates to be visible Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 83/96] PCI/MSI: Do not set invalid bits in MSI mask Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 84/96] PCI/MSI: Correct misleading comments Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 85/96] PCI/MSI: Use msi_mask_irq() in pci_msi_shutdown() Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 86/96] PCI/MSI: Protect msi_desc::masked for multi-MSI Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 87/96] powerpc/smp: Fix OOPS in topology_init() Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 88/96] efi/libstub: arm64: Double check image alignment at entry Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 89/96] KVM: VMX: Use current VMCS to query WAITPKG support for MSR emulation Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 90/96] KVM: nVMX: Use vmx_need_pf_intercept() when deciding if L0 wants a #PF Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 91/96] vboxsf: Add vboxsf_[create|release]_sf_handle() helpers Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 92/96] vboxsf: Add support for the atomic_open directory-inode op Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 93/96] ceph: add some lockdep assertions around snaprealm handling Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 94/96] ceph: clean up locking annotation for ceph_get_snap_realm and __lookup_snap_realm Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 95/96] ceph: take snap_empty_lock atomically with snaprealm refcount change Greg Kroah-Hartman
2021-08-16 13:02 ` [PATCH 5.10 96/96] vmlinux.lds.h: Handle clangs module.{c,d}tor sections Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210816125437.267104668@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ardb@kernel.org \
    --cc=benh@kernel.crashing.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).