[PATCH v3 0/5] Fix coverity bugs and add checks for elf values in grub-core

2022-04-20 Thread Alec Brown
v3: Added check for e_shoff, made starting words lowercase in error messages, and added comment to why return pointers are set to 0. Coverity identified several untrusted loop bounds and untrusted allocation size bugs in grub-core/loader/i386/bsdXX.c and grub-core/loader/multiboot_elfXX.c. Upon re

[PATCH v3 4/5] elf: Validate number of elf program header table entries

2022-04-20 Thread Alec Brown
In bsdXX.c and multiboot_elfXX.c, e_phnum is used to obtain the number of program header table entries, but it wasn't being checked if the value was there. According to the elf(5) manual page, "If the number of entries in the program header table is larger than or equal to PN_XNUM (0x), this m

[PATCH v3 2/5] elf: Validate number of elf section header table entries

2022-04-20 Thread Alec Brown
In bsdXX.c and multiboot_elfXX.c, e_shnum is used to obtain the number of section header table entries, but it wasn't being checked if the value was there. According to the elf(5) manual page, "If the number of entries in the section header table is larger than or equal to SHN_LORESERVE (0xff00),

[PATCH v3 1/5] grub-core/loader/i386/bsdXX.c: Avoid downcasting (char *) to (Elf_Shdr *)

2022-04-20 Thread Alec Brown
In bsdXX.c, a couple of untrusted loop bound and untrusted allocation size bugs were flagged by Coverity in the functions grub_openbsd_find_ramdisk() and grub_freebsd_load_elfmodule(). These bugs were flagged by coverity because the variable shdr was downcasting from a char pointer to an Elf_Shdr p

[PATCH v3 5/5] util/grub-module-verifierXX.c: Add e_shoff check in get_shdr()

2022-04-20 Thread Alec Brown
In util/grub-module-verifierXX.c, the function get_shdr() is used to obtain the section header at a given index but isn't checking that there is an offset for the section header table. To validate that there is, we can check that e_shoff isn't 0. Signed-off-by: Alec Brown --- util/grub-module-ve

[PATCH v3 3/5] elf: Validate elf section header table index for section name string table

2022-04-20 Thread Alec Brown
In multiboot_elfXX.c, e_shstrndx is used to obtain the section header table index of the section name string table, but it wasn't being checked if the value was there. According to the elf(5) manual page, "If the index of section name string table section is larger than or equal to SHN_LORESERVE (

[PATCH v3 00/15] Dynamic allocation of memory regions and IBM vTPM v2

2022-04-20 Thread Daniel Axtens
Hi, This is a small update to v2 (https://lists.gnu.org/archive/html/grub-devel/2022-03/msg00242.html) Changes since v2: - A fix to patch 11 that does IBM CAS, as it turns out my old approach caused some more extreme configurations to fail to boot. Oops. - Code style fixes (tabs vs spaces).

[PATCH v3 01/15] grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms

2022-04-20 Thread Daniel Axtens
This breaks the tests on pseries - just restrict it to x86 platforms that don't specify a BIOS. Signed-off-by: Daniel Axtens Reviewed-by: Daniel Kiper --- v2: Thanks Daniel K and Glenn for feedback. --- tests/util/grub-shell.in | 6 +- 1 file changed, 5 insertions(+), 1 deletion(-) diff --

[PATCH v3 06/15] mm: Allow dynamically requesting additional memory regions

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt Currently, all platforms will set up their heap on initialization of the platform code. While this works mostly fine, it poses some limitations on memory management on us. Most notably, allocating big chunks of memory in the gigabyte range would require us to pre-request

[PATCH v3 03/15] mm: when adding a region, merge with region after as well as before

2022-04-20 Thread Daniel Axtens
On x86_64-efi (at least) regions seem to be added from top down. The mm code will merge a new region with an existing region that comes immediately before the new region. This allows larger allocations to be satisfied that would otherwise be the case. On powerpc-ieee1275, however, regions are adde

[PATCH v3 02/15] mm: assert that we preserve header vs region alignment

2022-04-20 Thread Daniel Axtens
grub_mm_region_init() does: h = (grub_mm_header_t) (r + 1); where h is a grub_mm_header_t and r is a grub_mm_region_t. Cells are supposed to be GRUB_MM_ALIGN aligned, but while grub_mm_dump ensures this vs the region header, grub_mm_region_init() does not. It's better to be explicit than impl

[PATCH v3 15/15] ibmvtpm: Add support for trusted boot using a vTPM 2.0

2022-04-20 Thread Daniel Axtens
From: Stefan Berger Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275 PowerPC platform. With this patch grub now measures text and binary data into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform does. This patch requires Daniel Axtens's patches for claiming more me

[PATCH v3 04/15] mm: debug support for region operations

2022-04-20 Thread Daniel Axtens
This is handy for debugging. Enable with `set debug=regions`. Signed-off-by: Daniel Axtens Reviewed-by: Daniel Kiper --- grub-core/kern/mm.c | 19 --- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 6e4e8f325a05..a1f

[PATCH v3 08/15] efi: mm: Extract function to add memory regions

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt In preparation of support for runtime-allocating additional memory region, this patch extracts the function to retrieve the EFI memory map and add a subset of it to GRUB's own memory regions. Signed-off-by: Patrick Steinhardt Signed-off-by: Daniel Axtens Reviewed-by: D

[PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()`

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt The function `add_memory_regions ()` is currently only called on system initialization to allocate a fixed amount of pages. As such, it didn't need to return any errors: in case it failed, we cannot proceed anyway. This will change with the upcoming support for requesting

[PATCH v3 11/15] ieee1275: request memory with ibm, client-architecture-support

2022-04-20 Thread Daniel Axtens
On PowerVM, the first time we boot a Linux partition, we may only get 256MB of real memory area, even if the partition has more memory. This isn't enough to reliably verify a kernel. Fortunately, the Power Architecture Platform Reference (PAPR) defines a method we can call to ask for more memory:

[PATCH v3 05/15] mm: Drop unused unloading of modules on OOM

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt In `grub_memalign ()`, there's a commented section which would allow for unloading of unneeded modules in case where there is not enough free memory available to satisfy a request. Given that this code is never compiled in, let's remove it together with `grub_dl_unload_un

[PATCH v3 12/15] ieee1275: drop len -= 1 quirk in heap_init

2022-04-20 Thread Daniel Axtens
This was apparently 'required by some firmware': commit dc9468500919 ("2007-02-12 Hollis Blanchard "). It's not clear what firmware that was, and what platform from 14 years ago which exhibited the bug then is still both in use and buggy now. It doesn't cause issues on qemu (mac99 or pseries) o

[PATCH v3 07/15] efi: mm: Always request a fixed number of pages on init

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt When initializing the EFI memory subsytem, we will by default request a quarter of the available memory, bounded by a minimum/maximum value. Given that we're about to extend the EFI memory system to dynamically request additional pages from the firmware as required, this

[PATCH v3 14/15] [RFC] Add memtool module with memory allocation stress-test

2022-04-20 Thread Daniel Axtens
When working on memory, it's nice to be able to test your work. Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands: * lsmem - print all allocations and free space in all regions * lsfreemem - print free space in all regions * stress_big_allocs - stress test larg

[PATCH v3 13/15] ieee1275: support runtime memory claiming

2022-04-20 Thread Daniel Axtens
On powerpc-ieee1275, we are running out of memory trying to verify anything. This is because: - we have to load an entire file into memory to verify it. This is difficult to change with appended signatures. - We only have 32MB of heap. - Distro kernels are now often around 30MB. So we want

[PATCH v3 10/15] efi: mm: Implement runtime addition of pages

2022-04-20 Thread Daniel Axtens
From: Patrick Steinhardt Adjust the interface of `grub_efi_mm_add_regions ()` to take a set of `GRUB_MM_ADD_REGION_*` flags, which most notably is currently only the `CONSECUTVE` flag. This allows us to set the function up as callback for the memory subsystem and have it call out to us in case th

Re: [PATCH v2 13/22] libtasn1: changes for grub compatibility

2022-04-20 Thread Daniel Axtens
Stefan Berger writes: > On 6/30/21 4:40 AM, Daniel Axtens wrote: >> Do a few things to make libtasn1 compile as part of grub: >> >> - redefine _asn1_strcat. grub removed strcat so replace it with the >> appropriate calls to memcpy and strlen. Use this internally where >> strcat was used

Re: [PATCH v2 18/22] appended signatures: parse PKCS#7 signedData and X.509 certificates

2022-04-20 Thread Daniel Axtens
Stefan Berger writes: > On 6/30/21 4:40 AM, Daniel Axtens wrote: > >> This code allows us to parse: >> >> - PKCS#7 signedData messages. Only a single signerInfo is supported, >> which is all that the Linux sign-file utility supports creating >> out-of-the-box. Only RSA, SHA-256 and SHA-