fix arm64 kernel image not aligned on 64k boundary
This commit is contained in:
parent
3592d6e45e
commit
9e7b13f0a1
@ -0,0 +1,65 @@
|
|||||||
|
From 337b3d963d28b3544e8817428fb68ca559613a39 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Peter Jones <pjones@redhat.com>
|
||||||
|
Date: Thu, 9 Sep 2021 10:59:28 -0400
|
||||||
|
Subject: [PATCH 2/2] Arm: check for the PE magic for the compiled arch
|
||||||
|
|
||||||
|
In "arm64: Fix EFI loader kernel image allocation", Ben fixed the kernel
|
||||||
|
alignment to match the alignment given in the PE header. In doing so, a
|
||||||
|
check for valid PE magic was added, which was hard-coded to the value
|
||||||
|
seen on Aarch64 (GRUB_PE32_PE64_MAGIC).
|
||||||
|
|
||||||
|
Unfortunately, this code is shared between 64-bit and 32-bit, and so
|
||||||
|
that value broke 32-bit Arm systems.
|
||||||
|
|
||||||
|
This patch adds a constant definition for GRUB_PE32_PEXX_MAGIC, which is
|
||||||
|
either GRUB_PE32_PE64_MAGIC or GRUB_PE32_PE32_MAGIC, depending on which
|
||||||
|
platform is being built, and uses it in the header magic check.
|
||||||
|
|
||||||
|
Resolves: rhbz#2000756
|
||||||
|
|
||||||
|
Signed-off-by: Peter Jones <pjones@redhat.com>
|
||||||
|
---
|
||||||
|
grub-core/loader/arm64/linux.c | 2 +-
|
||||||
|
include/grub/arm/linux.h | 1 +
|
||||||
|
include/grub/arm64/linux.h | 1 +
|
||||||
|
3 files changed, 3 insertions(+), 1 deletion(-)
|
||||||
|
|
||||||
|
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
|
||||||
|
index 1da1886..f0ad052 100644
|
||||||
|
--- a/grub-core/loader/arm64/linux.c
|
||||||
|
+++ b/grub-core/loader/arm64/linux.c
|
||||||
|
@@ -342,7 +342,7 @@ parse_pe_header (void *kernel, grub_uint64_t *total_size,
|
||||||
|
|
||||||
|
pe = (void *)((unsigned long)kernel + lh->hdr_offset);
|
||||||
|
|
||||||
|
- if (pe->opt.magic != GRUB_PE32_PE64_MAGIC)
|
||||||
|
+ if (pe->opt.magic != GRUB_PE32_PEXX_MAGIC)
|
||||||
|
return grub_error(GRUB_ERR_BAD_OS, "Invalid PE optional header magic");
|
||||||
|
|
||||||
|
*total_size = pe->opt.image_size;
|
||||||
|
diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h
|
||||||
|
index b582f67..966a507 100644
|
||||||
|
--- a/include/grub/arm/linux.h
|
||||||
|
+++ b/include/grub/arm/linux.h
|
||||||
|
@@ -44,6 +44,7 @@ struct grub_arm_linux_pe_header
|
||||||
|
|
||||||
|
#if defined(__arm__)
|
||||||
|
# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE
|
||||||
|
+# define GRUB_PE32_PEXX_MAGIC GRUB_PE32_PE32_MAGIC
|
||||||
|
# define linux_arch_kernel_header linux_arm_kernel_header
|
||||||
|
# define grub_armxx_linux_pe_header grub_arm_linux_pe_header
|
||||||
|
#endif
|
||||||
|
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h
|
||||||
|
index a3be9dd..20828d9 100644
|
||||||
|
--- a/include/grub/arm64/linux.h
|
||||||
|
+++ b/include/grub/arm64/linux.h
|
||||||
|
@@ -47,6 +47,7 @@ struct grub_arm64_linux_pe_header
|
||||||
|
|
||||||
|
#if defined(__aarch64__)
|
||||||
|
# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE
|
||||||
|
+# define GRUB_PE32_PEXX_MAGIC GRUB_PE32_PE64_MAGIC
|
||||||
|
# define linux_arch_kernel_header linux_arm64_kernel_header
|
||||||
|
# define grub_armxx_linux_pe_header grub_arm64_linux_pe_header
|
||||||
|
#endif
|
||||||
|
--
|
||||||
|
2.23.0
|
||||||
192
backport-arm64-Fix-EFI-loader-kernel-image-allocation.patch
Normal file
192
backport-arm64-Fix-EFI-loader-kernel-image-allocation.patch
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
|
||||||
|
Date: Mon, 2 Aug 2021 23:10:01 +1000
|
||||||
|
Subject: [PATCH 1/2] arm64: Fix EFI loader kernel image allocation
|
||||||
|
|
||||||
|
We are currently allocating just enough memory for the file size,
|
||||||
|
which means that the kernel BSS is in limbo (and not even zeroed).
|
||||||
|
|
||||||
|
We are also not honoring the alignment specified in the image
|
||||||
|
PE header.
|
||||||
|
|
||||||
|
This makes us use the PE optional header in which the kernel puts the
|
||||||
|
actual size it needs, including BSS, and make sure we clear it, and
|
||||||
|
honors the specified alignment for the image.
|
||||||
|
|
||||||
|
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
|
||||||
|
---
|
||||||
|
grub-core/loader/arm64/linux.c | 102 ++++++++++++++++++++++-----------
|
||||||
|
1 file changed, 67 insertions(+), 35 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
|
||||||
|
index a18c487..a803aa9 100644
|
||||||
|
--- a/grub-core/loader/arm64/linux.c
|
||||||
|
+++ b/grub-core/loader/arm64/linux.c
|
||||||
|
@@ -40,6 +40,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
|
||||||
|
static grub_dl_t my_mod;
|
||||||
|
static int loaded;
|
||||||
|
|
||||||
|
+static void *kernel_alloc_addr;
|
||||||
|
+static grub_uint32_t kernel_alloc_pages;
|
||||||
|
static void *kernel_addr;
|
||||||
|
static grub_uint64_t kernel_size;
|
||||||
|
static grub_uint32_t handover_offset;
|
||||||
|
@@ -223,9 +225,8 @@ grub_linux_unload (void)
|
||||||
|
GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
|
||||||
|
initrd_start = initrd_end = 0;
|
||||||
|
grub_free (linux_args);
|
||||||
|
- if (kernel_addr)
|
||||||
|
- grub_efi_free_pages ((grub_addr_t) kernel_addr,
|
||||||
|
- GRUB_EFI_BYTES_TO_PAGES (kernel_size));
|
||||||
|
+ if (kernel_alloc_addr)
|
||||||
|
+ grub_efi_free_pages ((grub_addr_t) kernel_alloc_addr, kernel_alloc_pages);
|
||||||
|
grub_fdt_unload ();
|
||||||
|
return GRUB_ERR_NONE;
|
||||||
|
}
|
||||||
|
@@ -330,14 +331,35 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
|
||||||
|
return grub_errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
+static grub_err_t
|
||||||
|
+parse_pe_header (void *kernel, grub_uint64_t *total_size,
|
||||||
|
+ grub_uint32_t *entry_offset,
|
||||||
|
+ grub_uint32_t *alignment)
|
||||||
|
+{
|
||||||
|
+ struct linux_arch_kernel_header *lh = kernel;
|
||||||
|
+ struct grub_armxx_linux_pe_header *pe;
|
||||||
|
+
|
||||||
|
+ pe = (void *)((unsigned long)kernel + lh->hdr_offset);
|
||||||
|
+
|
||||||
|
+ if (pe->opt.magic != GRUB_PE32_PE64_MAGIC)
|
||||||
|
+ return grub_error(GRUB_ERR_BAD_OS, "Invalid PE optional header magic");
|
||||||
|
+
|
||||||
|
+ *total_size = pe->opt.image_size;
|
||||||
|
+ *entry_offset = pe->opt.entry_addr;
|
||||||
|
+ *alignment = pe->opt.section_alignment;
|
||||||
|
+
|
||||||
|
+ return GRUB_ERR_NONE;
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
static grub_err_t
|
||||||
|
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
||||||
|
int argc, char *argv[])
|
||||||
|
{
|
||||||
|
grub_file_t file = 0;
|
||||||
|
- struct linux_arch_kernel_header lh;
|
||||||
|
- struct grub_armxx_linux_pe_header *pe;
|
||||||
|
grub_err_t err;
|
||||||
|
+ grub_off_t filelen;
|
||||||
|
+ grub_uint32_t align;
|
||||||
|
+ void *kernel = NULL;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
grub_dl_ref (my_mod);
|
||||||
|
@@ -351,41 +373,25 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
||||||
|
file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
|
||||||
|
if (!file)
|
||||||
|
goto fail;
|
||||||
|
-
|
||||||
|
- kernel_size = grub_file_size (file);
|
||||||
|
-
|
||||||
|
- if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
|
||||||
|
- return grub_errno;
|
||||||
|
-
|
||||||
|
- if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
|
||||||
|
- goto fail;
|
||||||
|
-
|
||||||
|
- grub_loader_unset();
|
||||||
|
-
|
||||||
|
- grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
|
||||||
|
- kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
|
||||||
|
- grub_dprintf ("linux", "kernel numpages: %lld\n",
|
||||||
|
- (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
|
||||||
|
- if (!kernel_addr)
|
||||||
|
+
|
||||||
|
+ filelen = grub_file_size (file);
|
||||||
|
+ kernel = grub_malloc(filelen);
|
||||||
|
+ if (!kernel)
|
||||||
|
{
|
||||||
|
- grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
|
||||||
|
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel load buffer"));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
- grub_file_seek (file, 0);
|
||||||
|
- if (grub_file_read (file, kernel_addr, kernel_size)
|
||||||
|
- < (grub_int64_t) kernel_size)
|
||||||
|
+ if (grub_file_read (file, kernel, filelen) < (grub_ssize_t)filelen)
|
||||||
|
{
|
||||||
|
- if (!grub_errno)
|
||||||
|
- grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
|
||||||
|
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"),
|
||||||
|
+ argv[0]);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
- grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
|
||||||
|
-
|
||||||
|
if (grub_efi_secure_boot ())
|
||||||
|
{
|
||||||
|
- rc = grub_linuxefi_secure_validate (kernel_addr, kernel_size);
|
||||||
|
+ rc = grub_linuxefi_secure_validate (kernel, filelen);
|
||||||
|
if (rc <= 0)
|
||||||
|
{
|
||||||
|
grub_error (GRUB_ERR_INVALID_COMMAND,
|
||||||
|
@@ -394,8 +400,32 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- pe = (void *)((unsigned long)kernel_addr + lh.hdr_offset);
|
||||||
|
- handover_offset = pe->opt.entry_addr;
|
||||||
|
+ if (grub_arch_efi_linux_check_image (kernel) != GRUB_ERR_NONE)
|
||||||
|
+ goto fail;
|
||||||
|
+ if (parse_pe_header (kernel, &kernel_size, &handover_offset, &align) != GRUB_ERR_NONE)
|
||||||
|
+ goto fail;
|
||||||
|
+ grub_dprintf ("linux", "kernel mem size : %lld\n", (long long) kernel_size);
|
||||||
|
+ grub_dprintf ("linux", "kernel entry offset : %d\n", handover_offset);
|
||||||
|
+ grub_dprintf ("linux", "kernel alignment : 0x%x\n", align);
|
||||||
|
+
|
||||||
|
+ grub_loader_unset();
|
||||||
|
+
|
||||||
|
+ kernel_alloc_pages = GRUB_EFI_BYTES_TO_PAGES (kernel_size + align - 1);
|
||||||
|
+ kernel_alloc_addr = grub_efi_allocate_any_pages (kernel_alloc_pages);
|
||||||
|
+ grub_dprintf ("linux", "kernel numpages: %d\n", kernel_alloc_pages);
|
||||||
|
+ if (!kernel_alloc_addr)
|
||||||
|
+ {
|
||||||
|
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
|
||||||
|
+ goto fail;
|
||||||
|
+ }
|
||||||
|
+ kernel_addr = (void *)ALIGN_UP((grub_uint64_t)kernel_alloc_addr, align);
|
||||||
|
+
|
||||||
|
+ grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
|
||||||
|
+ grub_memcpy (kernel_addr, kernel, grub_min(filelen, kernel_size));
|
||||||
|
+ if (kernel_size > filelen)
|
||||||
|
+ grub_memset ((char *)kernel_addr + filelen, 0, kernel_size - filelen);
|
||||||
|
+ grub_free(kernel);
|
||||||
|
+ kernel = NULL;
|
||||||
|
|
||||||
|
cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
|
||||||
|
linux_args = grub_malloc (cmdline_size);
|
||||||
|
@@ -419,6 +449,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
||||||
|
}
|
||||||
|
|
||||||
|
fail:
|
||||||
|
+ if (kernel)
|
||||||
|
+ grub_free (kernel);
|
||||||
|
+
|
||||||
|
if (file)
|
||||||
|
grub_file_close (file);
|
||||||
|
|
||||||
|
@@ -431,9 +464,8 @@ fail:
|
||||||
|
if (linux_args && !loaded)
|
||||||
|
grub_free (linux_args);
|
||||||
|
|
||||||
|
- if (kernel_addr && !loaded)
|
||||||
|
- grub_efi_free_pages ((grub_addr_t) kernel_addr,
|
||||||
|
- GRUB_EFI_BYTES_TO_PAGES (kernel_size));
|
||||||
|
+ if (kernel_alloc_addr && !loaded)
|
||||||
|
+ grub_efi_free_pages ((grub_addr_t) kernel_alloc_addr, kernel_alloc_pages);
|
||||||
|
|
||||||
|
return grub_errno;
|
||||||
|
}
|
||||||
|
--
|
||||||
|
2.23.0
|
||||||
@ -356,3 +356,5 @@ Patch0355: backport-templates-Fix-bad-test-on-GRUB_DISABLE_SUBMENU.patch
|
|||||||
Patch0356: grub2-set-password-prompts-to-enter-the-current-pass.patch
|
Patch0356: grub2-set-password-prompts-to-enter-the-current-pass.patch
|
||||||
Patch0357: support-TPM2.0.patch
|
Patch0357: support-TPM2.0.patch
|
||||||
Patch0358: use-default-timestamp.patch
|
Patch0358: use-default-timestamp.patch
|
||||||
|
Patch0359: backport-arm64-Fix-EFI-loader-kernel-image-allocation.patch
|
||||||
|
Patch0360: backport-Arm-check-for-the-PE-magic-for-the-compiled-arch.patch
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
Name: grub2
|
Name: grub2
|
||||||
Epoch: 1
|
Epoch: 1
|
||||||
Version: 2.04
|
Version: 2.04
|
||||||
Release: 23
|
Release: 24
|
||||||
Summary: Bootloader with support for Linux, Multiboot and more
|
Summary: Bootloader with support for Linux, Multiboot and more
|
||||||
License: GPLv3+
|
License: GPLv3+
|
||||||
URL: http://www.gnu.org/software/grub/
|
URL: http://www.gnu.org/software/grub/
|
||||||
@ -450,6 +450,13 @@ rm -r /boot/grub2.tmp/ || :
|
|||||||
%{_datadir}/man/man*
|
%{_datadir}/man/man*
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Mon Feb 28 2022 fengtao <fengtao40@huawei.com> - 2.04-24
|
||||||
|
- Type:bugfix
|
||||||
|
- CVE:NA
|
||||||
|
- SUG:NA
|
||||||
|
- DESC:fix arm64 kernel image not aligned on 64k boundary
|
||||||
|
fix grub.patches file format to unix
|
||||||
|
|
||||||
* Sat Feb 26 2022 zhangqiumiao <zhangqiumiao1@huawei.com> - 2.04-23
|
* Sat Feb 26 2022 zhangqiumiao <zhangqiumiao1@huawei.com> - 2.04-23
|
||||||
- Type:bugfix
|
- Type:bugfix
|
||||||
- CVE:NA
|
- CVE:NA
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user