]> git.ipfire.org Git - thirdparty/grub.git/log
thirdparty/grub.git
2 years agonormal/help: Add paging instructions to normal and help prompts
Robbie Harwood [Fri, 4 Nov 2022 17:05:37 +0000 (13:05 -0400)] 
normal/help: Add paging instructions to normal and help prompts

This is not an ideal solution, as interactive users must always run
a command in order to get the behavior they want, but it avoids
problematic interactions between prompting and sourcing files.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agocommands/tpm: Don't propagate measurement failures to the verifiers layer
Robbie Harwood [Fri, 4 Nov 2022 16:13:36 +0000 (12:13 -0400)] 
commands/tpm: Don't propagate measurement failures to the verifiers layer

Currently if an EFI firmware fails to do a TPM measurement for a file,
the error will be propagated to the verifiers framework which will
prevent it to be opened. This mean that buggy firmwares will lead to
the system not booting because files won't be allowed to be loaded. But
a failure to do a TPM measurement isn't expected to be a fatal error
that causes the system to be unbootable.

To avoid this, don't return errors from .write and .verify_string
callbacks and just print a debug message in the case of a TPM
measurement failure. Add an environment variable, tpm_fail_fatal, to
restore the previous behavior.

Also-authored-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agokern/env: Add function for retrieving variables as booleans
Robbie Harwood [Fri, 4 Nov 2022 16:13:35 +0000 (12:13 -0400)] 
kern/env: Add function for retrieving variables as booleans

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotypes: Make bool generally available
Robbie Harwood [Fri, 4 Nov 2022 16:13:34 +0000 (12:13 -0400)] 
types: Make bool generally available

Add an include on stdbool.h, making the bool type generally available
within the GRUB without needing to add a file-specific include every
time it would be used.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoloader: Add support for grub-emu to kexec Linux menu entries
Raymund Will [Mon, 24 Oct 2022 18:33:50 +0000 (14:33 -0400)] 
loader: Add support for grub-emu to kexec Linux menu entries

The GRUB emulator is used as a debugging utility but it could also be
used as a user-space bootloader if there is support to boot an operating
system.

The Linux kernel is already able to (re)boot another kernel via the
kexec boot mechanism. So the grub-emu tool could rely on this feature
and have linux and initrd commands that are used to pass a kernel,
initramfs image and command line parameters to kexec for booting
a selected menu entry.

By default the systemctl kexec option is used so systemd can shutdown
all of the running services before doing a reboot using kexec. But if
this is not present, it can fall back to executing the kexec user-space
tool directly. The ability to force a kexec-reboot when systemctl kexec
fails must only be used in controlled environments to avoid possible
filesystem corruption and data loss.

Signed-off-by: Raymund Will <rw@suse.com>
Signed-off-by: John Jolly <jjolly@suse.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates: Introduce GRUB_TOP_LEVEL_* vars
Denton Liu [Mon, 24 Oct 2022 10:46:42 +0000 (03:46 -0700)] 
templates: Introduce GRUB_TOP_LEVEL_* vars

A user may wish to use an image that is not sorted as the "latest"
version as the top-level entry. For example, in Arch Linux, if a user
has the LTS and regular kernels installed, "/boot/vmlinuz-linux-lts"
gets sorted as the "latest" compared to "/boot/vmlinuz-linux", meaning
the LTS kernel becomes the top-level entry. However, a user may wish to
use the regular kernel as the top-level default with the LTS only
existing as a backup.

This need can be seen in Arch Linux's AUR with two user-submitted
packages[0][1] providing an update hook which patches /etc/grub.d/10_linux
to move the desired kernel to the top-level. This patch serves to solve
this in a more generic way.

Introduce the GRUB_TOP_LEVEL, GRUB_TOP_LEVEL_XEN and GRUB_TOP_LEVEL_OS_PROBER
variables to allow users to specify the top-level entry.

Create grub_move_to_front() as a helper function which moves entries to
the front of a list. This function does the heavy lifting of moving
the menu entry to the front in each script.

In 10_netbsd, since there isn't an explicit list variable, extract the
items that are being iterated through into a list so that we can
optionally apply grub_move_to_front() to the list before the loop.

[0]: https://aur.archlinux.org/packages/grub-linux-default-hook
[1]: https://aur.archlinux.org/packages/grub-linux-rt-default-hook

Signed-off-by: Denton Liu <liu.denton@gmail.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agovideo/readers: Add artificial limit to image dimensions
Alec Brown [Thu, 27 Oct 2022 00:16:44 +0000 (20:16 -0400)] 
video/readers: Add artificial limit to image dimensions

In grub-core/video/readers/jpeg.c, the height and width of a JPEG image don't
have an upper limit for how big the JPEG image can be. In Coverity, this is
getting flagged as an untrusted loop bound. This issue can also seen in PNG and
TGA format images as well but Coverity isn't flagging it. To prevent this, the
constant IMAGE_HW_MAX_PX is being added to include/grub/bitmap.h, which has
a value of 16384, to act as an artificial limit and restrict the height and
width of images. This value was picked as it is double the current max
resolution size, which is 8K.

Fixes: CID 292450
Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/diskfilter: Don't make a RAID array with more than 1024 disks
Daniel Axtens [Wed, 19 Oct 2022 09:23:22 +0000 (20:23 +1100)] 
disk/diskfilter: Don't make a RAID array with more than 1024 disks

This is "belt and braces" with commit 12e20a6a695f (disk/diskfilter:
Check calloc() result for NULL): we end up trying to use too much memory
in situations like corrupted Linux software RAID setups purporting to
use a huge number of disks. Simply refuse to permit such configurations.

1024 is a bit arbitrary, yes, and I feel a bit like I'm tempting fate
here, but I think 1024 disks in an array (that GRUB has to read to boot!)
should be enough for anyone.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoarm64/efi/linux: Ignore FDT unless we need to modify it
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:07 +0000 (21:05 +0200)] 
arm64/efi/linux: Ignore FDT unless we need to modify it

Now that we implemented support for the LoadFile2 protocol for initrd
loading, there is no longer a need to pass the initrd parameters via
the device tree. This means that when the LoadFile2 protocol is being
used, there is no reason to update the device tree in the first place,
and so we can ignore it entirely.

The only remaining reason to deal with the devicetree is if we are
using the "devicetree" command to load one from disk, so tweak the
logic in grub_fdt_install() to take that into account.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoarm64/efi/linux: Implement LoadFile2 initrd loading protocol for Linux
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:06 +0000 (21:05 +0200)] 
arm64/efi/linux: Implement LoadFile2 initrd loading protocol for Linux

Recent Linux kernels will invoke the LoadFile2 protocol installed on
a well-known vendor media path to load the initrd if it is exposed by
the firmware. Using this method is preferred for two reasons:
  - the Linux kernel is in charge of allocating the memory, and so it can
    implement any placement policy it wants (given that these tend to
    change between kernel versions),
  - it is no longer necessary to modify the device tree provided by the
    firmware.

So let's install this protocol when handling the "initrd" command if
such a recent kernel was detected (based on the PE/COFF image version),
and defer loading the initrd contents until the point where the kernel
invokes the LoadFile2 protocol.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi/efinet: Don't close connections at fini_hw() time
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:05 +0000 (21:05 +0200)] 
efi/efinet: Don't close connections at fini_hw() time

When GRUB runs on top of EFI firmware, it only has access to block and
network device abstractions exposed by the firmware, and it is up to the
firmware to quiesce the underlying hardware when exiting boot services
and handing over to the OS.

This is especially important for network devices, to prevent incoming
packets from being DMA'd straight into memory after the OS has taken
over but before it has managed to reconfigure the network hardware.

GRUB handles this by means of the grub_net_fini_hw() preboot hook, which
is executed before calling into the booted image. This means that all
network devices disappear or become inoperable before the EFI stub
executes on EFI targeted builds. This is problematic as it prevents the
EFI stub from calling back into GRUB provided protocols such as
LoadFile2 for the initrd, which we will provide in a subsequent patch.

So add a flag that indicates to the network core that EFI network
devices should not be closed when grub_net_fini_hw() is called.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoloader/arm64/linux: Account for COFF headers appearing at unexpected offsets
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:04 +0000 (21:05 +0200)] 
loader/arm64/linux: Account for COFF headers appearing at unexpected offsets

The way we load the Linux and PE/COFF image headers depends on a fixed
placement of the COFF header at offset 0x40 into the file. This is
a reasonable default, given that this is where Linux emits it today.
However, in order to comply with the PE/COFF spec, which allows this
header to appear anywhere in the file, let's ensure that we read the
header from where it actually appears in the file if it is not located
at offset 0x40.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoarm/linux: Unify ARM/arm64 vs Xen PE/COFF header handling
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:03 +0000 (21:05 +0200)] 
arm/linux: Unify ARM/arm64 vs Xen PE/COFF header handling

Xen has its own version of the image header, to account for the
additional PE/COFF header fields. Since we are adding references to
those in the shared EFI loader code, update the common definitions
and drop the Xen specific one which no longer has a purpose.

Since in both cases, the call to grub_arch_efi_linux_check_image() is
preceded by a load of the image header, let's move the load into that
function, and rename it to grub_arch_efi_linux_load_image_header().

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Move MS-DOS stub out of generic PE header definition
Ard Biesheuvel [Tue, 18 Oct 2022 19:05:02 +0000 (21:05 +0200)] 
efi: Move MS-DOS stub out of generic PE header definition

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,
introduce a minimal struct grub_msdos_image_header type based on the
PE/COFF spec's description of the image header, and use the offset
recorded at file position 0x3c to discover the actual location of the PE
signature and the COFF image header.

The remaining fields are moved into a struct grub_pe_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 <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agokern/buffer: Handle NULL input pointer in grub_buffer_free()
Jagannathan Raman [Mon, 17 Oct 2022 14:04:39 +0000 (14:04 +0000)] 
kern/buffer: Handle NULL input pointer in grub_buffer_free()

The grub_buffer_free() should handle NULL input pointer, similar to
grub_free(). If the pointer is not referencing any memory location,
grub_buffer_free() need not perform any function.

Fixes: CID 396931
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agofs/zfs/zfs: Update dangling dn_new pointer in dnode_get_path()
Jagannathan Raman [Mon, 17 Oct 2022 14:04:38 +0000 (14:04 +0000)] 
fs/zfs/zfs: Update dangling dn_new pointer in dnode_get_path()

The dnode_get_path() traverses dnode structures to locate the dnode leaf
of a given path. When the leaf is a symlink to another path, it restarts
the traversal either from root or from a different path. In such cases,
dn_new must be re-initialized

Passes "make check".

Fixes: CID 86750
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agobuild: Update to reflect minimum clang version 8.0
Darren Kenny [Fri, 21 Oct 2022 13:33:01 +0000 (13:33 +0000)] 
build: Update to reflect minimum clang version 8.0

After doing some validation with clang from versions 3.8 and up, the
builds prior to version 8.0.0 fail due to the use of safemath functions
at link time.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoconfigure: Fix building with clang
Darren Kenny [Fri, 21 Oct 2022 13:33:00 +0000 (13:33 +0000)] 
configure: Fix building with clang

Building the current code with clang and the latest gnulib fails due to
the use of a variable-length-array (vla) warning, which turns in to an
error due to the presence of the -Werror during the build.

The gnulib team stated that their code should not be built with -Werror.

At present, the only way to do this is for the complete code-base, by
using the --disable-werror option to configure.

Rather than doing this, and failing to gain any benefit that it provides,
instead, if building with clang, this patch makes it possible to specifically
not error on vlas, while retaining the -Werror functionality otherwise.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agognulib: Provide abort() implementation for gnulib
Darren Kenny [Fri, 21 Oct 2022 13:32:59 +0000 (13:32 +0000)] 
gnulib: Provide abort() implementation for gnulib

The recent gnulib updates require an implementation of abort(), but the
current macro provided by changeset:

  cd37d3d3916c gnulib: Drop no-abort.patch

to config.h.in does not work with the clang compiler since it doesn't
provide a __builtin_trap() implementation, so this element of the
changeset needs to be reverted, and replaced.

After some discussion with Vladimir 'phcoder' Serbinenko and Daniel Kiper
it was suggested to bring back in the change from the changeset:

  db7337a3d353 * grub-core/gnulib/regcomp.c (regerror): ...

Which implements abort() as an inline call to grub_abort(), but since
that was made static by changeset:

  a8f15bceeafe * grub-core/kern/misc.c (grub_abort): Make static

it is also necessary to revert the specific part that makes it a static
function too.

Another implementation of abort() was found in grub-core/kern/compiler-rt.c
which needs to also be removed to be consistent.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/cryptodisk: Fix unintentional integer overflow
Alec Brown [Fri, 14 Oct 2022 21:47:08 +0000 (17:47 -0400)] 
disk/cryptodisk: Fix unintentional integer overflow

In the function grub_cryptodisk_endecrypt(), a for loop is incrementing the
variable i by (1U << log_sector_size). The variable i is of type grub_size_t
which is a 64-bit unsigned integer on x86_64 architecture. On the other hand, 1U
is a 32-bit unsigned integer. By performing a left shift on a 32-bit value and
assigning it to a 64-bit variable, the 64-bit variable may have incorrect values
in the high 32-bits if the shift has an overflow. To avoid this, we replace 1U
with (grub_size_t)1.

Fixes: CID 307788
Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agomm: Try invalidate disk caches last when out of memory
Zhang Boyang [Sat, 15 Oct 2022 14:15:11 +0000 (22:15 +0800)] 
mm: Try invalidate disk caches last when out of memory

Every heap grow will cause all disk caches invalidated which decreases
performance severely. This patch moves disk cache invalidation code to
the last of memory squeezing measures. So, disk caches are released only
when there are no other ways to get free memory.

Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
2 years agoutil/grub-mkfont: Use valid conversion specifiers in printf() and fprintf()
Qiumiao Zhang [Tue, 11 Oct 2022 12:31:02 +0000 (20:31 +0800)] 
util/grub-mkfont: Use valid conversion specifiers in printf() and fprintf()

For printf()/fprintf() functions, unsigned integers should use %u as the
valid conversion specifier instead of %d.

Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Compile kernel.img with -fshort-wchar on all EFI targets
Chris Coulson [Tue, 11 Oct 2022 12:26:33 +0000 (13:26 +0100)] 
efi: Compile kernel.img with -fshort-wchar on all EFI targets

The stack check logs a console message on failure, and the EFI API expects
a NULL terminated UCS-2 string. In order to define a UCS-2 string literal,
kernel.img on amd64 and i386 EFI targets is built with -fshort-wchar.

Also compile kernel.img on other EFI targets with -fshort-wchar.

Fixes: 37ddd94 (kern/efi/init: Log a console error during a stack check failure)
Reported-by: Glenn Washburn <development@efficientek.com>
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agonormal/menu: Add Ctrl-L to refresh the menu
Benjamin Herrenschmidt [Fri, 7 Oct 2022 02:35:59 +0000 (13:35 +1100)] 
normal/menu: Add Ctrl-L to refresh the menu

This is useful on cloud instances with remote serial ports as it can be
difficult to connect "fast enough" to get the initial menu display

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil/grub-install: Set point of no return for powerpc-ieee1275 install
Michael Chang [Thu, 25 Aug 2022 06:05:01 +0000 (14:05 +0800)] 
util/grub-install: Set point of no return for powerpc-ieee1275 install

The point of no return is used to define a point where no change should
be reverted in a wake of fatal error that consequently aborts the
process. The powerpc-ieee1275 install apparently missed this point of no
return definition that newly installed modules could be inadvertently
reverted after successful image embedding so that boot failure is
incurred due to inconsistent state.

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/diskfilter: Check calloc() result for NULL
Daniel Axtens [Sun, 21 Aug 2022 12:22:35 +0000 (22:22 +1000)] 
disk/diskfilter: Check calloc() result for NULL

With wildly corrupt inputs, we can end up trying to calloc a very
large amount of memory, which will fail and give us a NULL pointer.
We need to check that to avoid a crash. (And, even if we blocked
such inputs, it is good practice to check the results of allocations
anyway.)

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner
Glenn Washburn [Fri, 19 Aug 2022 23:06:15 +0000 (18:06 -0500)] 
disk/cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner

A user can now specify UUID strings with dashes, instead of having to remove
dashes. This is backwards-compatibility preserving and also fixes a source
of user confusion over the inconsistency with how UUIDs are specified
between file system UUIDs and cryptomount UUIDs. Since cryptsetup, the
reference implementation for LUKS, displays and generates UUIDs with dashes
there has been additional confusion when using the UUID strings from
cryptsetup as exact input into GRUB does not find the expected cryptodisk.

A new function grub_uuidcasecmp() is added that is general enough to be used
other places where UUIDs are being compared.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agokern/corecmd: Quote variable values when displayed by the set command
Glenn Washburn [Fri, 26 Aug 2022 00:53:51 +0000 (19:53 -0500)] 
kern/corecmd: Quote variable values when displayed by the set command

Variable values may contain spaces at the end or newlines. However, when
displayed without quotes this is not obvious and can lead to confusion as
to the actual contents of variables. Also for some variables grub_env_get()
returns a NULL pointer instead of a pointer to an empty string and
previously would be printed as "var=(null)". Now such variables will be
displayed as "var=''".

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates: Add support for acpi on Hurd
Samuel Thibault [Mon, 26 Sep 2022 19:51:32 +0000 (21:51 +0200)] 
templates: Add support for acpi on Hurd

This adds acpi as bootstrap module whenever it is available. This opens the
path for proper IRQ routing for fully-userland disk drivers.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil/grub-module-verifierXX: Enable running standalone checkers
Peter Jones [Wed, 5 Oct 2022 13:48:37 +0000 (09:48 -0400)] 
util/grub-module-verifierXX: Enable running standalone checkers

Allow treating util/grub-module-verifierXX.c as a file you can build
directly so syntax checkers like vim's "syntastic" plugin, which uses
"gcc -x c -fsyntax-only" to build it, will work.

One still has to do whatever setup is required to make it pick the
right include dirs, which -I options we use, etc., but this makes
it so you can do the checking on the file you're editing, rather
than on a different file.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agokern/compiler-rt: Fix __clzsi2() logic
Tuan Phan [Mon, 3 Oct 2022 18:29:21 +0000 (11:29 -0700)] 
kern/compiler-rt: Fix __clzsi2() logic

Fix the incorrect return value of __clzsi2() function.

Fixes: e795b90 (RISC-V: Add libgcc helpers for clz)
Signed-off-by: Tuan Phan <tphan@ventanamicro.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Increase default memory allocation to 32 MiB
Daniel Axtens [Mon, 19 Sep 2022 14:30:30 +0000 (00:30 +1000)] 
efi: Increase default memory allocation to 32 MiB

We have multiple reports of things being slower with a 1 MiB initial static
allocation, and a report (more difficult to nail down) of a boot failure
as a result of the smaller initial allocation.

Make the initial memory allocation 32 MiB.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates: Filter C.UTF-8 locale for translation
Christian Hesse [Mon, 19 Sep 2022 13:31:28 +0000 (15:31 +0200)] 
templates: Filter C.UTF-8 locale for translation

In addition to C locale there is also C.UTF-8 locale now. Filter that as
well, by using ${grub_lang}, which contains a stripped value.
This fixes the following message and resulting boot failure:

    error: file `/boot/grub/locale/C.gmo' not found.

Signed-off-by: Christian Hesse <mail@eworm.de>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotests: Explicitly unset SOURCE_DATE_EPOCH before running fs tests
Steve McIntyre [Sun, 18 Sep 2022 22:12:02 +0000 (23:12 +0100)] 
tests: Explicitly unset SOURCE_DATE_EPOCH before running fs tests

In some filesystem utils like mksquashfs, they will silently change
behaviour and cause timestamps to unexpectedly change. Build
environments like Debian's set SOURCE_DATE_EPOCH in the environment,
so remove it. Reproducible builds are good and useful for shipped
artifacts, but this causes build-time tests to fail.

Signed-off-by: Steve McIntyre <steve@einval.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agocommands/efi/lsefisystab: Short text for EFI_CONFORMANCE_PROFILES_TABLE
Heinrich Schuchardt [Fri, 2 Sep 2022 12:58:45 +0000 (14:58 +0200)] 
commands/efi/lsefisystab: Short text for EFI_CONFORMANCE_PROFILES_TABLE

The EFI_CONFORMANCE_PROFILES_TABLE_GUID is used for a table of GUIDs for conformance
profiles (cf. UEFI specification 2.10, 4.6.5 EFI_CONFORMANCE_PROFILE_TABLE).

The lsefisystab command is used to display installed EFI configuration tables.
Currently it only shows the GUID but not a short text for the table.

Provide a short text for the EFI_CONFORMANCE_PROFILES_TABLE_GUID.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agofs/ext2: Ignore the large_dir incompat feature
Theodore Ts'o [Wed, 31 Aug 2022 02:41:59 +0000 (22:41 -0400)] 
fs/ext2: Ignore the large_dir incompat feature

Recently, ext4 added the large_dir feature, which adds support for
a 3 level htree directory support.

The GRUB supports existing file systems with htree directories by
ignoring their existence, and since the index nodes for the hash tree
look like deleted directory entries (by design), the GRUB can simply do
a brute force O(n) linear search of directories. The same is true for
3 level deep htrees indicated by large_dir feature flag.

Hence, it is safe for the GRUB to ignore the large_dir incompat feature.

Fixes: https://savannah.gnu.org/bugs/?61606
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/loopback: Support transparent decompression of backing file
Glenn Washburn [Fri, 26 Aug 2022 04:08:57 +0000 (23:08 -0500)] 
disk/loopback: Support transparent decompression of backing file

A new option is added to the loopback command, -D or --decompress, which
when specified transparently decompresses the backing file. This allows
compressed images to be used as if they were uncompressed.

Add documentation to support this change.

Suggested-by: Li Gen <ligenlive@gmail.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoconfigure: Add -DGRUB_HAS_PCI when compiling C/C++ files on targets that support PCI
Glenn Washburn [Fri, 26 Aug 2022 03:52:26 +0000 (22:52 -0500)] 
configure: Add -DGRUB_HAS_PCI when compiling C/C++ files on targets that support PCI

The list of targets that support PCI is in gentpl.py. However, there is no
support for generating makefile script from a .def file that will apply
globally to the makefile, but on a per target basis. So instead, use
gentpl.py in configure to get the list of targets and check if the current
build target is one of them. If it is, set the automake conditional
COND_HAVE_PCI. Then in conf/Makefile.common add -DGRUB_HAS_PCI for the
platform if COND_HAVE_PCI is true.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agocommands/read: Fix overflow in grub_getline()
Li Gen [Fri, 26 Aug 2022 00:59:09 +0000 (19:59 -0500)] 
commands/read: Fix overflow in grub_getline()

Store returned value from grub_getkey() in int instead of char to
prevent throwing away the extended bits. This was a problem because,
for instance, the left arrow key press would return
(GRUB_TERM_EXTENDED | 0x4b), which would have the GRUB_TERM_EXTENDED
thrown away leaving 0x4b or 'K'. These extended keys should either
work as intended or do nothing. This change has them do nothing,
instead of inserting a key not pressed by the user.

Signed-off-by: Li Gen <ligenlive@gmail.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Correct function prototype for register_key_notify() method of grub_efi_simple_t...
Li Gen [Thu, 25 Aug 2022 05:12:17 +0000 (00:12 -0500)] 
efi: Correct function prototype for register_key_notify() method of grub_efi_simple_text_input_ex_interface

The register_key_notify() method should have an output parameter which is
a pointer to the unique handle assigned to the registered notification.

Signed-off-by: Li Gen <ligenlive@gmail.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agonet/drivers/ieee1275/ofnet: Fix incorrect netmask
Masahiro Matsuya [Wed, 24 Aug 2022 17:36:37 +0000 (13:36 -0400)] 
net/drivers/ieee1275/ofnet: Fix incorrect netmask

The netmask configured in firmware is not respected on ppc64 (big endian).
When 255.255.252.0 is set as netmask in firmware, the following is the
value of bootpath string in grub_ieee1275_parse_bootpath():

  /vdevice/l-lan@30000002:speed=auto,duplex=auto,192.168.88.10,,192.168.89.113,192.168.88.1,5,5,255.255.252.0,512

The netmask in this bootpath is not a problem, since it's a value specified
in firmware. But the value of subnet_mask.ipv4 was set with 0xfffffc00, and
__builtin_ctz(~grub_le_to_cpu32(subnet_mask.ipv4)) returned 16 (not 22).
As a result, 16 was used for netmask wrongly:

  1111 1111 1111 1111 1111 1100 0000 0000 # subnet_mask.ipv4(=0xfffffc00)
  0000 0000 1111 1100 1111 1111 1111 1111 # grub_le_to_cpu32(subnet_mask.ipv4)
  1111 1111 0000 0011 0000 0000 0000 0000 # ~grub_le_to_cpu32(subnet_mask.ipv4)

and the count of zero with __builtin_ctz() can be 16. This patch changes
it as below:

  1111 1111 1111 1111 1111 1100 0000 0000 # subnet_mask.ipv4(=0xfffffc00)
  0000 0000 1111 1100 1111 1111 1111 1111 # grub_le_to_cpu32(subnet_mask.ipv4)
  1111 1111 1111 1111 1111 1100 0000 0000 # grub_be_to_cpu32(subnet_mask.ipv4)
  0000 0000 0000 0000 0000 0011 1111 1111 # ~grub_be_to_cpu32(subnet_mask.ipv4)

The count of zero with __builtin_clz() can be 22 (clz counts the number
of one bits preceding the most significant zero bit).

Signed-off-by: Masahiro Matsuya <mmatsuya@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoloader/i386/bsd: Initialize BSD relocator state variables
Ross Philipson [Wed, 24 Aug 2022 14:47:02 +0000 (10:47 -0400)] 
loader/i386/bsd: Initialize BSD relocator state variables

Numerous register fields in the relocator state are simply not
used depending on the relocator. This causes Coverity to flag
these fields but there is no real bug here. Simply initializing
the variable to {0} solves the issue. Fixed in the else case too
for consistency.

Fixes: CID 396932
Signed-off-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodocs: Add a link to environment variables
Andrea G. Monaco [Sat, 9 Jan 2021 18:30:34 +0000 (19:30 +0100)] 
docs: Add a link to environment variables

This is trivial, but it might save some time to beginners.

Signed-off-by: Andrea G. Monaco <andrea.monaco@autistici.org>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodocs: Fix mismatched brackets in halt command
Robbie Harwood [Fri, 19 Aug 2022 21:02:27 +0000 (17:02 -0400)] 
docs: Fix mismatched brackets in halt command

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodocs: Document fwsetup command
Robbie Harwood [Fri, 19 Aug 2022 21:02:26 +0000 (17:02 -0400)] 
docs: Document fwsetup command

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Don't display a uefi-firmware entry if it's not supported
Robbie Harwood [Thu, 18 Aug 2022 17:50:13 +0000 (13:50 -0400)] 
efi: Don't display a uefi-firmware entry if it's not supported

Add a new --is-supported option to commands/efi/efifwsetup and
conditionalize display on it.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agocommands/efi/efifwsetup: Print an error if boot to firmware setup is not supported
Javier Martinez Canillas [Thu, 18 Aug 2022 17:50:12 +0000 (13:50 -0400)] 
commands/efi/efifwsetup: Print an error if boot to firmware setup is not supported

The "fwsetup" command is only registered if the firmware supports booting
to the firmware setup UI. But it could be possible that the GRUB config
already contains a "fwsetup" entry, because it was generated in a machine
that has support for this feature.

To prevent users getting an error like:

    error: ../../grub-core/script/function.c:109:can't find command `fwsetup'.

if it is not supported by the firmware, let's just always register the
command but print a more accurate message if the firmware doesn't
support this option.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates: Check for EFI at runtime instead of config generation time
Javier Martinez Canillas [Thu, 18 Aug 2022 17:50:11 +0000 (13:50 -0400)] 
templates: Check for EFI at runtime instead of config generation time

The 30_uefi-firmware template checks if an OsIndicationsSupported UEFI var
exists and EFI_OS_INDICATIONS_BOOT_TO_FW_UI bit is set, to decide whether
a "fwsetup" menu entry would be added or not to the GRUB menu.

But this has the problem that it will only work if the configuration file
was created on an UEFI machine that supports booting to a firmware UI.

This for example doesn't support creating GRUB config files when executing
on systems that support both UEFI and legacy BIOS booting. Since creating
the config file from legacy BIOS wouldn't allow to access the firmware UI.

To prevent this, make the template to unconditionally create the grub.cfg
snippet but check at runtime if was booted through UEFI to decide if this
entry should be added. That way it won't be added when booting with BIOS.

There's no need to check if EFI_OS_INDICATIONS_BOOT_TO_FW_UI bit is set,
since that's already done by the "fwsetup" command when is executed.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Make all grub_efi_guid_t variables static
Robbie Harwood [Thu, 18 Aug 2022 17:50:10 +0000 (13:50 -0400)] 
efi: Make all grub_efi_guid_t variables static

This is believed to result in smaller code.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agocommands/efi/efifwsetup: Add missing grub_free()s
Robbie Harwood [Thu, 18 Aug 2022 17:50:09 +0000 (13:50 -0400)] 
commands/efi/efifwsetup: Add missing grub_free()s

Each call of grub_efi_get_variable() needs a grub_free().

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agofs/zfs/zfs: Pass pointer to dnode_end_t instead of value to fill_fs_info()
Jagannathan Raman [Fri, 19 Aug 2022 14:57:22 +0000 (10:57 -0400)] 
fs/zfs/zfs: Pass pointer to dnode_end_t instead of value to fill_fs_info()

Coverity reports that dnode_end_t argument of fill_fs_info() is too
large to pass-by-value. Therefore, replace the argument with a pointer.

Fixes: CID 73631
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/luks2: Fix decoding of digests and salts with escaped chars
Patrick Steinhardt [Mon, 15 Aug 2022 15:53:06 +0000 (17:53 +0200)] 
disk/luks2: Fix decoding of digests and salts with escaped chars

It was reported in the #grub IRC channel on Libera that decryption of
LUKS2 partitions fails with errors about invalid digests and/or salts.
In all of these cases, what failed was decoding the Base64
representation of these, where the encoded data contained invalid
characters.

As it turns out, the root cause is that json-c, which is used by
cryptsetup to read and write the JSON header, will escape some
characters by prepending a backslash when writing JSON strings by
default. Most importantly, json-c also escapes the forward slash, which
is part of the Base64 alphabet. Because GRUB doesn't know to unescape
such characters, decoding this string will rightfully fail.

Interestingly, this issue has until now only been reported by users of
Ubuntu 18.04. And a bit of digging in fact reveals that cryptsetup has
changed the logic in a054206d (Suppress useless slash escaping in json
lib, 2018-04-20), which has been released with cryptsetup v2.0.3. Ubuntu
18.04 is still shipping with cryptsetup v2.0.2 though, which explains
why this is not a more frequent issue.

Fix the issue by using our new grub_json_unescape() helper function
that handles unescaping for us.

Reported-by: Afdal
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <dkiper@net-space.pl>
2 years agolib/json/json: Add function to unescape JSON-encoded strings
Patrick Steinhardt [Mon, 15 Aug 2022 15:52:59 +0000 (17:52 +0200)] 
lib/json/json: Add function to unescape JSON-encoded strings

JSON strings require certain characters to be encoded, either by using
a single reverse solidus character "\" for a set of popular characters,
or by using a Unicode representation of "\uXXXXX". The jsmn library
doesn't handle unescaping for us, so we must implement this functionality
for ourselves.

Add a new function grub_json_unescape() that takes a potentially
escaped JSON string as input and returns a new unescaped string.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <dkiper@net-space.pl>
2 years agoloader: Drop argv[] argument in grub_initrd_load()
Nikita Ermakov [Thu, 18 Aug 2022 14:51:28 +0000 (16:51 +0200)] 
loader: Drop argv[] argument in grub_initrd_load()

In the case of an error grub_initrd_load() uses argv[] to print the
filename that caused the error. It is also possible to obtain the
filename from the file handles and there is no need to duplicate that
information in argv[], so let's drop it.

Signed-off-by: Nikita Ermakov <arei@altlinux.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoloader: Update error conditionals to use enums
Alec Brown [Fri, 12 Aug 2022 22:25:49 +0000 (18:25 -0400)] 
loader: Update error conditionals to use enums

In grub-core/loader/i386/bsdXX.c and grub-core/loader/multiboot_elfxx.c, error
conditionals are simplified to statements such as "if (err)". Even though the
assumption that non-zero values give errors is correct, it would be clearer and
more consistent to compare these conditionals to GRUB_ERR_NONE.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil/grub-module-verifierXX: Changed get_shnum() return type
Alec Brown [Fri, 12 Aug 2022 22:25:48 +0000 (18:25 -0400)] 
util/grub-module-verifierXX: Changed get_shnum() return type

In util/grub-module-verifierXX.c, the function get_shnum() returns the variable
shnum, which is of the type Elf_Word. In the function, shnum can be obtained by
the e_shnum member of an Elf_Ehdr or the sh_size member of an Elf_Shdr. The
sh_size member can either be grub_uint32_t or grub_uint64_t, depending on the
architecture, but Elf_Word is only grub_uint32_t. To account for when sh_size is
grub_uint64_t, we can set shnum to have type Elf_Shnum and have get_shnum()
return an Elf_Shnum.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoelf: Validate number of elf program header table entries
Alec Brown [Fri, 12 Aug 2022 22:25:47 +0000 (18:25 -0400)] 
elf: Validate number of elf program header table entries

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 (0xffff), this member holds PN_XNUM (0xffff) and the real number of
entries in the program header table is held in the sh_info member of the
initial entry in section header table.  Otherwise, the sh_info member of the
initial entry contains the value zero."

Since this check wasn't being made, grub_elfXX_get_phnum() is being added to
elfXX.c to make this check and use e_phnum if it doesn't have PN_XNUM as a
value, else use sh_info. We also need to make sure e_phnum isn't greater than
PN_XNUM and sh_info isn't less than PN_XNUM.

Note that even though elf.c and elfXX.c are located in grub-core/kern, they are
compiled as modules and don't need the EXPORT_FUNC() macro to define the functions
in elf.h.

Also, changed casts of phnum to match variables being set as well as dropped
casts when unnecessary.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoelf: Validate elf section header table index for section name string table
Alec Brown [Fri, 12 Aug 2022 22:25:46 +0000 (18:25 -0400)] 
elf: Validate elf section header table index for section name string table

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 (0xff00), this member holds SHN_XINDEX (0xffff) and the real
index of the section name string table section is held in the sh_link member of
the initial entry in section header table. Otherwise, the sh_link member of the
initial entry in section header table contains the value zero."

Since this check wasn't being made, grub_elfXX_get_shstrndx() is being added to
elfXX.c to make this check and use e_shstrndx if it doesn't have SHN_XINDEX as a
value, else use sh_link. We also need to make sure e_shstrndx isn't greater than
or equal to SHN_LORESERVE and sh_link isn't less than SHN_LORESERVE.

Note that even though elf.c and elfXX.c are located in grub-core/kern, they are
compiled as modules and don't need the EXPORT_FUNC() macro to define the functions
in elf.h.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoelf: Validate number of elf section header table entries
Alec Brown [Fri, 12 Aug 2022 22:25:45 +0000 (18:25 -0400)] 
elf: Validate number of elf section header table entries

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), e_shnum holds the value zero and the real number of
entries in the section header table is held in the sh_size member of the initial
entry in section header table. Otherwise, the sh_size member of the initial
entry in the section header table holds the value zero."

Since this check wasn't being made, grub_elfXX_get_shnum() is being added to
elfXX.c to make this check and use whichever member doesn't have a value of
zero. If both are zero, then we must return an error. We also need to make sure
that e_shnum doesn't have a value greater than or equal to SHN_LORESERVE and
sh_size isn't less than SHN_LORESERVE.

In order to get this function to work, the type ElfXX_Shnum is being added where
Elf32_Shnum defines Elf32_Word and Elf64_Shnum defines Elf64_Xword. This new
type is needed because if shnum obtains a value from sh_size, sh_size could be
of type El32_Word for Elf32_Shdr structures or Elf64_Xword for Elf64_Shdr
structures.

Note that even though elf.c and elfXX.c are located in grub-core/kern, they are
compiled as modules and don't need the EXPORT_FUNC() macro to define the functions
in elf.h.

For a few smaller changes, changed casts of shnum to match variables being set
as well as dropped casts when unnecessary and fixed spacing errors in bsdXX.c.
Also, shnum is an unsigned integer and is compared to int i in multiboot_elfxx.c,
it should be unsigned to match shnum.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates/linux_xen: Properly order the multiple initrd files
Mauricio Faria de Oliveira [Thu, 11 Aug 2022 19:10:14 +0000 (16:10 -0300)] 
templates/linux_xen: Properly order the multiple initrd files

The linux_xen template orders the "early" initrd file(s) _first_
(i.e., before the "real" initrd files) and that seems reasonable,
as microcode updates usually come first.

However, this usually breaks Linux boot with initrd under Xen
because Xen assumes the real initrd is the first multiboot[2]
module after the kernel, passing its address over to Linux in
Xen's start_info struct.

So, if a microcode-only initrd (i.e., without init/userspace)
is found by grub-mkconfig, it ends up considered as a normal
initrd by the Linux kernel, which cannot do anything with it
(as it has no other files) and panic()s unable to mount root
if it depends on a initrd to do that (e.g., root=UUID=...).

...

Well, since Xen doesn't actually use the provided microcode
by default / unless the 'ucode=<module number|scan>' option
is enabled, this isn't used in the general case (and breaks).

Additionally, if an user enables the 'ucode=' option, that
either specifies which module is to be used for microcode,
or scans all modules (regardless of being first) for that.

Thus, for Xen:
- it is *not required* to have microcode first,
- but it is *required* to have real initrd first

So, fix it by ordering the real initrd before early initrd(s).

After:

    # touch /boot/xen /boot/microcode.cpio
    # grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
            multiboot      /boot/xen ...
            module  /boot/vmlinuz-5.4.0-122-generic ...
            module  --nounzip   /boot/initrd.img-5.4.0-122-generic
            module  --nounzip   /boot/microcode.cpio

...

Corner case specific to Xen implementation details:

It is actually _possible_ to have a microcode initrd first,
but that requires a non-default option (so can't rely on it),
and it turns out to be inconsistent with its counterpart
(really shouldn't rely on it, as it may get confusing; below).

'ucode=1' does manually specify the first module is microcode
_AND_ clears its bit in the module bitmap. The next module is
now the 'new first', and gets passed to Linux as initrd. Good.

'ucode=scan' checks all modules for microcode, but does _NOT_
clear a bit if it finds one (reasonable, as it can find that
prepended in a "real" initrd anyway, which needs to be used).
The first module still gets passed to Linux as initrd. Bad.

Fixes: e86f6aafb8de (grub-mkconfig/20_linux_xen: Support multiple early initrd images)
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
Acked-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotemplates/linux_xen: Properly load multiple initrd files
Mauricio Faria de Oliveira [Thu, 11 Aug 2022 19:10:13 +0000 (16:10 -0300)] 
templates/linux_xen: Properly load multiple initrd files

The linux_xen template can put multiple initrd files in the
same multiboot[2] module[2] command, which is against specs.

This causes ONLY the _first_ initrd file to be loaded; other
files just have filenames in a "cmdline" string of the first
initrd file and are NOT loaded.

Fix this by inserting a module[2] command per initrd file.

Before:

    # touch /boot/xen /boot/microcode.cpio
    # grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
            multiboot       /boot/xen ...
            module  /boot/vmlinuz-5.4.0-122-generic ...
            module  --nounzip   /boot/microcode.cpio /boot/initrd.img-5.4.0-122-generic

After:

    # touch /boot/xen /boot/microcode.cpio
    # grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
            multiboot      /boot/xen ...
            module  /boot/vmlinuz-5.4.0-122-generic ...
            module  --nounzip   /boot/microcode.cpio
            module  --nounzip   /boot/initrd.img-5.4.0-122-generic

Cause:

The code was copied from the linux template, which is *apparently*
equivalent.. but its initrd command grub_cmd_initrd() *supports*
multiple files (see grub_initrd_init()), while module/module2 in
grub_cmd_module() *does not* (see grub_multiboot[2]_add_module()).

See commit e86f6aafb8de (grub-mkconfig/20_linux_xen: Support multiple early initrd images):
    'This is basically a copy of a698240d "grub-mkconfig/10_linux:
     Support multiple early initrd images" ...'

Specs:

Both multiboot and multiboot2 specifications mention support for
'multiple boot modules' (struct/tag used for kernel/initrd files):

    "Boot loaders don’t have to support multiple boot modules,
     but they are strongly encouraged to" [1,2]

However, there is a 1:1 relationship between boot modules and files,
more or less clearly; note the usage of singular/plural "module(s)".
(Multiboot2, clearly: "One tag appears per module".)

  Multiboot [1]:

    "the â€˜mods’ fields indicate ... what boot modules
     were loaded ..., and where they can be found.
     â€˜mods_count’ contains the number of modules loaded"

    "The first two fields contain the start and end addresses
     of the boot module itself."

  Multiboot2 [2]:

    "This tag indicates ... what boot module was loaded ...,
     and where it can be found."

    "The â€˜mod_start’ and â€˜mod_end’ contain the start and end
     physical addresses of the boot module itself."

    "One tag appears per module.
     This tag type may appear multiple times."

And both clearly mention the 'string' field of a boot module,
which is to be used by the operating system, not boot loader:

     "The â€˜string’ field provides an arbitrary string to be
      associated with that particular boot module ...
      its exact use is specific to the operating system."

Links:

[1] https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
    3.3 Boot information format

[2] https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html
    3.6.6 Modules

Fixes: e86f6aafb8de (grub-mkconfig/20_linux_xen: Support multiple early initrd images)
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
Acked-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agomisc: Add cast in grub_strncasecmp() to drop sign when calling grub_tolower()
Glenn Washburn [Thu, 11 Aug 2022 17:48:42 +0000 (12:48 -0500)] 
misc: Add cast in grub_strncasecmp() to drop sign when calling grub_tolower()

Note this cast was fixed in grub_strcasecmp() in commit ce41ab7aab
(* grub-core/kern/misc.c (grub_strcmp): Use unsigned comparison as per
common usage and preffered in several parts of code.), but this commit
omitted fixing it in grub_strncasecmp().

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotests/util/grub-shell: Only show grub-mkrescue output if it returns an error
Glenn Washburn [Thu, 11 Aug 2022 17:06:43 +0000 (12:06 -0500)] 
tests/util/grub-shell: Only show grub-mkrescue output if it returns an error

The previous behavior ignored an error and the output from grub-mkrescue.
This made it difficult to discover that grub-mkrescue was the reason that
tests which rely on grub-shell were failing. Even after discovering
grub-mkrescue was the culprit, there was no output to indicate why it was
failing. It turns out that grub-mkrescue is a thin wrapper around xorriso.
So if you do not have xorriso installed it will fail with an error message
about not being able to find xorriso.

This change will allow grub-mkrescue output to be written to stderr, only
if grub-mkrescue fails. If grub-mkrescue succeeds, there will be no output
from grub-mkrescue so as not to interfere with the functioning of tests.
This change should have no effect on the running of tests or other uses of
grub-shell as it only modifies the error path.

Also, if grub-mkrescue fails, the script exits early. Since grub-shell
needs the ISO image created by grub-mkresue to boot the QEMU instance,
a failure here should be considered fatal.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoloader/arm64/linux: Remove magic number header field check
Ard Biesheuvel [Thu, 11 Aug 2022 14:51:57 +0000 (16:51 +0200)] 
loader/arm64/linux: Remove magic number header field check

The "ARM\x64" magic number in the file header identifies an image as one
that implements the bare metal boot protocol, allowing the loader to
simply move the file to a suitably aligned address in memory, with
sufficient headroom for the trailing .bss segment (the required memory
size is described in the header as well).

Note of this matters for GRUB, as it only supports EFI boot. EFI does
not care about this magic number, and nor should GRUB: this prevents us
from booting other PE linux images, such as the generic EFI zboot
decompressor, which is a pure PE/COFF image, and does not implement the
bare metal boot protocol.

So drop the magic number check.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil/grub-install-common: Confirm directory creation in grub_install_mkdir_p()
Darren Kenny [Tue, 9 Aug 2022 13:29:05 +0000 (13:29 +0000)] 
util/grub-install-common: Confirm directory creation in grub_install_mkdir_p()

Because grub_util_mkdir() is implemented to not return a value on any
platform, grub_instal_mkdir_p() can test for success by confirming that
the directory requested exists after attempting to create it, otherwise
it should fail with an error and exit.

While fixing this, a flaw in the logic was shown, where the first match
of the path separator, which almost always was the first character in
the path (e.g. /boot/grub2) would result in creating a directory with an
empty name (i.e. ""). To avoid that, it should skip the handling of the
path separator where p is pointing to the first character.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil: Ignore return value for grub_util_mkdir() on all platforms
Darren Kenny [Tue, 9 Aug 2022 13:29:04 +0000 (13:29 +0000)] 
util: Ignore return value for grub_util_mkdir() on all platforms

Coverity signaled 2 issues where the return value of grub_util_mkdir()
was not being tested.

The Windows variant of this code defines the function as having no
return value (void), but the UNIX variants all are mapped using a macro
to the libc mkdir() function, which returns an int value.

To be consistent, the mapping should cast to void to for these too.

Fixes: CID 73583
Fixes: CID 73617
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/cryptodisk: Support encrypted volumes using detached headers on a partition
Glenn Washburn [Sun, 7 Aug 2022 05:18:52 +0000 (00:18 -0500)] 
disk/cryptodisk: Support encrypted volumes using detached headers on a partition

Update the read hook to take into account encrypted volumes on a partition.
GRUB disk read hooks supply an absolute sector number at which the read is
started from. If the encrypted volume is in a partition, the sector number
given to the read hook will be offset by the number of the sector at the
start of the partition. The read hook then needs to subtract the partition
start from the supplied sector to get the correct start sector for the read
into the detached header file.

Reported-by: brutser <brutser@perso.be>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: brutser <brutser@perso.be>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agotests/util/grub-shell: Use shell variable instead of autoconf
Glenn Washburn [Sat, 6 Aug 2022 06:26:31 +0000 (01:26 -0500)] 
tests/util/grub-shell: Use shell variable instead of autoconf

By using shell variable that are set once by the expansion of an autoconf
variable, the resulting shell script is more easily moved and modified
from the build/install directory it was generated for. The resulting
script is more readable as well.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoMakefile: Make grub_fstest.pp depend on config-util.h
Stefan Agner [Thu, 4 Aug 2022 09:57:49 +0000 (11:57 +0200)] 
Makefile: Make grub_fstest.pp depend on config-util.h

If you build with "make -j25", sometimes you see:

  /build/output_generic_x86_64/host/bin/x86_64-buildroot-linux-gnu-gcc -E -DHAVE_CONFIG_H -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE=\"util/grub-fstest.c\" -I. -I.. -I. -I.. -I../include -I./include -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64 \
    -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
  config.status: creating config-util.h
  ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such file or directory
     20 | #include <config-util.h>
        |          ^~~~~~~~~~~~~~~
  compilation terminated.

Signed-off-by: Stefan Agner <stefan@agner.ch>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoutil/grub-mkfont: Fix resource leaks
Qiumiao Zhang [Mon, 1 Aug 2022 08:31:02 +0000 (16:31 +0800)] 
util/grub-mkfont: Fix resource leaks

Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agokern/i386/tsc_pmtimer: Make pmtimer tsc calibration not take 51 seconds to fail
Peter Jones [Thu, 28 Jul 2022 18:34:16 +0000 (14:34 -0400)] 
kern/i386/tsc_pmtimer: Make pmtimer tsc calibration not take 51 seconds to fail

On my laptop running at 2.4GHz, if I run a VM where tsc calibration
using pmtimer will fail presuming a broken pmtimer, it takes ~51 seconds
to do so (as measured with the stopwatch on my phone), with a tsc delta
of 0x1cd1c85300, or around 125 billion cycles.

If instead of trying to wait for 5-200ms to show up on the pmtimer, we
try to wait for 5-200us, it decides it's broken in ~0x2626aa0 TSCs, aka
~2.4 million cycles, or more or less instantly.

Additionally, this reading the pmtimer was returning 0xffffffff anyway,
and that's obviously an invalid return. I've added a check for that and
0 so we don't bother waiting for the test if what we're seeing is dead
pins with no response at all.

If "debug" includes "pmtimer", you will see one of the following three
outcomes. If pmtimer gives all 0 or all 1 bits, you will see:

  pmtimer: 0xffffff bad_reads: 1
  pmtimer: 0xffffff bad_reads: 2
  pmtimer: 0xffffff bad_reads: 3
  pmtimer: 0xffffff bad_reads: 4
  pmtimer: 0xffffff bad_reads: 5
  pmtimer: 0xffffff bad_reads: 6
  pmtimer: 0xffffff bad_reads: 7
  pmtimer: 0xffffff bad_reads: 8
  pmtimer: 0xffffff bad_reads: 9
  pmtimer: 0xffffff bad_reads: 10
  timer is broken; giving up.

This outcome was tested using qemu+kvm with UEFI (OVMF) firmware and
these options: -machine pc-q35-2.10 -cpu Broadwell-noTSX

If pmtimer gives any other bit patterns but is not actually marching
forward fast enough to use for clock calibration, you will see:

  pmtimer delta is 0x0 (1904 iterations)
  tsc delta is implausible: 0x2626aa0

This outcome was tested using GRUB patched to not ignore bad reads using
qemu+kvm with UEFI (OVMF) firmware, and these options:
-machine pc-q35-2.10 -cpu Broadwell-noTSX

If pmtimer actually works, you'll see something like:

  pmtimer delta is 0xdff
  tsc delta is 0x278756

This outcome was tested using qemu+kvm with UEFI (OVMF) firmware, and
these options: -machine pc-i440fx-2.4 -cpu Broadwell-noTSX

I've also tested this outcome on a real Intel Xeon E3-1275v3 on an Intel
Server Board S1200V3RPS using the SDV.RP.B8 "Release" build here:
https://www.intel.com/content/www/us/en/download/674448/firmware-update-for-the-intel-server-board-s1200rp-uefi-development-kit-release-vb8.html

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agodisk/luks2: Continue trying all keyslots even if there are some failures
Glenn Washburn [Fri, 22 Jul 2022 08:04:50 +0000 (03:04 -0500)] 
disk/luks2: Continue trying all keyslots even if there are some failures

luks2_get_keyslot() can fail for a variety of reasons that do not necessarily
mean the next keyslot should not be tried (e.g. a new kdf type). So always
try the next slot. This will make GRUB more resilient to non-spec json data
that 3rd party systems may add. We do not care if some of the keyslots are
unusable, only if there is at least one that is.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2 years agoefi: Add efitextmode command for getting/setting the text mode resolution
Glenn Washburn [Fri, 22 Jul 2022 07:16:33 +0000 (02:16 -0500)] 
efi: Add efitextmode command for getting/setting the text mode resolution

This command is meant to behave similarly to the "mode" command of the EFI
Shell application. In addition to allowing mode selection by giving the
number of columns and rows as arguments, the command allows specifying the
mode number to select the mode. Also supported are the arguments "min" and
"max", which set the mode to the minimum and maximum mode respectively as
calculated by the columns * rows of that mode.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agofs/fat: Don't error when mtime is 0
Robbie Harwood [Fri, 15 Jul 2022 20:13:02 +0000 (16:13 -0400)] 
fs/fat: Don't error when mtime is 0

In the wild, we occasionally see valid ESPs where some file modification
times are 0. For instance:

    â”œâ”€â”€ [Dec 31  1979]  EFI
    â”‚   â”œâ”€â”€ [Dec 31  1979]  BOOT
    â”‚   â”‚   â”œâ”€â”€ [Dec 31  1979]  BOOTX64.EFI
    â”‚   â”‚   â””── [Dec 31  1979]  fbx64.efi
    â”‚   â””── [Jun 27 02:41]  fedora
    â”‚       â”œâ”€â”€ [Dec 31  1979]  BOOTX64.CSV
    â”‚       â”œâ”€â”€ [Dec 31  1979]  fonts
    â”‚       â”œâ”€â”€ [Mar 14 03:35]  fw
    â”‚       â”‚   â”œâ”€â”€ [Mar 14 03:35]  fwupd-359c1169-abd6-4a0d-8bce-e4d4713335c1.cap
    â”‚       â”‚   â”œâ”€â”€ [Mar 14 03:34]  fwupd-9d255c4b-2d88-4861-860d-7ee52ade9463.cap
    â”‚       â”‚   â””── [Mar 14 03:34]  fwupd-b36438d8-9128-49d2-b280-487be02d948b.cap
    â”‚       â”œâ”€â”€ [Dec 31  1979]  fwupdx64.efi
    â”‚       â”œâ”€â”€ [May 10 10:47]  grub.cfg
    â”‚       â”œâ”€â”€ [Jun  3 12:38]  grub.cfg.new.new
    â”‚       â”œâ”€â”€ [May 10 10:41]  grub.cfg.old
    â”‚       â”œâ”€â”€ [Jun 27 02:41]  grubenv
    â”‚       â”œâ”€â”€ [Dec 31  1979]  grubx64.efi
    â”‚       â”œâ”€â”€ [Dec 31  1979]  mmx64.efi
    â”‚       â”œâ”€â”€ [Dec 31  1979]  shim.efi
    â”‚       â”œâ”€â”€ [Dec 31  1979]  shimx64.efi
    â”‚       â””── [Dec 31  1979]  shimx64-fedora.efi
    â””── [Dec 31  1979]  FSCK0000.REC

    5 directories, 17 files

This causes grub-probe failure, which in turn causes grub-mkconfig
failure. They are valid filesystems that appear intact, and the Linux
FAT stack is able to mount and manipulate them without complaint.

The check for mtime of 0 has been present since
20def1a3c3952982395cd7c3ea7e78638527962b (fat: support file
modification times).

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agokern/fs: The grub_fs_probe() should dprint errors from filesystems
Robbie Harwood [Fri, 15 Jul 2022 20:13:01 +0000 (16:13 -0400)] 
kern/fs: The grub_fs_probe() should dprint errors from filesystems

When filesystem detection fails, all that's currently debug-logged is
a series of messages like:

    grub-core/kern/fs.c:56:fs: Detecting ntfs...
    grub-core/kern/fs.c:76:fs: ntfs detection failed.

repeated for each filesystem. Any messages provided to grub_error() by
the filesystem are lost, and one has to break out gdb to figure out what
went wrong.

With this change, one instead sees:

    grub-core/kern/fs.c:56:fs: Detecting fat...
    grub-core/osdep/hostdisk.c:357:hostdisk: reusing open device
    `/path/to/device'
    grub-core/kern/fs.c:77:fs: error: invalid modification timestamp for /.
    grub-core/kern/fs.c:79:fs: fat detection failed.

in the debug prints.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agoutil/grub-probe: Document the behavior of multiple -v
Robbie Harwood [Fri, 15 Jul 2022 20:13:00 +0000 (16:13 -0400)] 
util/grub-probe: Document the behavior of multiple -v

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agolib/relocator: Initialize local relocator subchunk struct to all zeros
Ross Philipson [Thu, 14 Jul 2022 13:41:28 +0000 (09:41 -0400)] 
lib/relocator: Initialize local relocator subchunk struct to all zeros

The way the code is written the tofree variable would never be passed to
the free_subchunk() function uninitialized. Coverity cannot determine
this and flags the situation as "Using uninitialized value...". The fix
is just to initialize the local struct.

Fixes: CID 314016
Signed-off-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Tested-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agoefi/tpm: Add EFI_CC_MEASUREMENT_PROTOCOL support
Lu Ken [Wed, 13 Jul 2022 02:06:12 +0000 (10:06 +0800)] 
efi/tpm: Add EFI_CC_MEASUREMENT_PROTOCOL support

The EFI_CC_MEASUREMENT_PROTOCOL abstracts the measurement for virtual firmware
in confidential computing environment. It is similar to the EFI_TCG2_PROTOCOL.
It was proposed by Intel and ARM and approved by UEFI organization.

It is defined in Intel GHCI specification: https://cdrdv2.intel.com/v1/dl/getContent/726790 .
The EDKII header file is available at https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/CcMeasurement.h .

Signed-off-by: Lu Ken <ken.lu@intel.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agocommands/efi/tpm: Use grub_strcpy() instead of grub_memcpy()
Lu Ken [Wed, 13 Jul 2022 02:06:11 +0000 (10:06 +0800)] 
commands/efi/tpm: Use grub_strcpy() instead of grub_memcpy()

The event description is a string, so using grub_strcpy() is cleaner than
using grub_memcpy().

Signed-off-by: Lu Ken <ken.lu@intel.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agocommands/efi/tpm: Refine the status of log event
Lu Ken [Wed, 13 Jul 2022 02:06:10 +0000 (10:06 +0800)] 
commands/efi/tpm: Refine the status of log event

1. Use macro GRUB_ERR_NONE instead of hard code 0.
2. Keep lowercase of the first char for the status string of log event.

Signed-off-by: Lu Ken <ken.lu@intel.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agoconfigure: Warn if stack protector is not allowed
Nicholas Vinson [Thu, 7 Jul 2022 23:52:47 +0000 (19:52 -0400)] 
configure: Warn if stack protector is not allowed

Introduce ERROR_PLATFORM_NOT_SUPPORT_SSP environment variable to treat
the "--enable-stack-protector is only supported on EFI platforms" message
as a warning instead of an error. If ERROR_PLATFORM_NOT_SUPPORT_SSP is
set to "no" (case-insensitive), then the message will be printed as
a warning. Otherwise, it prints as an error. The default behavior is to
print the message as an error.

For any wrapper build script that has some variation of:

    for p in SELECTED_GRUB_PLATFORMS; do    \
        configure --enable-stack-protector  \
            --with-platform${P} ... || die; \
    done
    make

The GRUB will fail to build if SELECTED_GRUB_PLATFORMS contains a platform
that does not support SSP.

Such wrapper scripts need to work-around this issue by modifying the
above for-loop, so it conditionally passes --enable-stack-protector to
configure for the proper GRUB platform(s).

However, if the above example is modified to have to conditionally pass
in --enable-stack-protector, its behavior is effectively the same as the
proposed change. Additionally, The list of SSP supported platforms is
now in 2 places. One in the configure script and one in the build wrapper
script. If the second list is not properly maintained it could mistakenly
disable SSP for a platform that later gained support for it.

Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agoutil/grub-mkfont: Fix tainted loop boundary issues with substitutions
Darren Kenny [Thu, 7 Jul 2022 15:34:38 +0000 (15:34 +0000)] 
util/grub-mkfont: Fix tainted loop boundary issues with substitutions

With gsub substitutions the offsets should be validated against the
number of glyphs in a font face and the memory allocated for the gsub
substitution data.

Both the number of glyphs and the last address in the allocated data are
passed in to process_cursive(), where the number of glyphs validates the end
of the range.

Enabling memory allocation validation uses two macros, one to simply check the
address against the allocated space, and the other to check that the number of
items of a given size doesn't extend outside of the allocated space.

Fixes: CID 73770
Fixes: CID 314040
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agoefi: Add missing header from include/grub/efi/console_control.h
Glenn Washburn [Thu, 12 May 2022 03:02:27 +0000 (22:02 -0500)] 
efi: Add missing header from include/grub/efi/console_control.h

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agodisk: Replace code that calculates the log of sector size with grub_log2ull()
Glenn Washburn [Thu, 9 Jun 2022 16:48:29 +0000 (11:48 -0500)] 
disk: Replace code that calculates the log of sector size with grub_log2ull()

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agotemplates: Remove unused version comparison functions
Mathieu Desnoyers [Mon, 13 Jun 2022 14:08:26 +0000 (10:08 -0400)] 
templates: Remove unused version comparison functions

There are no users left of version_find_latest(), version_test_gt(), and
version_test_numeric(). Remove those unused helper functions. Using
those helper functions is what caused the quadratic sorting performance
issues in the first place, so removing them is a net win.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agotemplates/kfreebsd: Fix quadratic algorithm for sorting menu items
Mathieu Desnoyers [Mon, 13 Jun 2022 14:08:25 +0000 (10:08 -0400)] 
templates/kfreebsd: Fix quadratic algorithm for sorting menu items

The current implementation of the 10_kfreebsd script implements its menu
items sorting in bash with a quadratic algorithm, calling "sed", "sort",
"head", and "grep" to compare versions between individual lines, which
is annoyingly slow for kernel developers who can easily end up with
50-100 kernels in their boot partition.

This fix is ported from the 10_linux script, which has a similar
quadratic code pattern.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: debian-bsd@lists.debian.org
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agotemplates/hurd: Fix quadratic algorithm for sorting menu items
Mathieu Desnoyers [Mon, 13 Jun 2022 14:08:24 +0000 (10:08 -0400)] 
templates/hurd: Fix quadratic algorithm for sorting menu items

The current implementation of the 10_hurd script implements its menu
items sorting in bash with a quadratic algorithm, calling "sed", "sort",
"head", and "grep" to compare versions between individual lines, which
is annoyingly slow for kernel developers who can easily end up with
50-100 kernels in their boot partition.

This fix is ported from the 10_linux script, which has a similar
quadratic code pattern.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
Tested-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agotemplates/linux_xen: Fix quadratic algorithm for sorting menu items
Mathieu Desnoyers [Mon, 13 Jun 2022 14:08:23 +0000 (10:08 -0400)] 
templates/linux_xen: Fix quadratic algorithm for sorting menu items

The current implementation of the 20_linux_xen script implements its
menu items sorting in bash with a quadratic algorithm, calling "sed",
"sort", "head", and "grep" to compare versions between individual lines,
which is annoyingly slow for kernel developers who can easily end up
with 50-100 kernels in their boot partition.

This fix is ported from the 10_linux script, which has a similar
quadratic code pattern.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: xen-devel@lists.xenproject.org
Tested-by: Jason Andryuk <jandryuk@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agotemplates/linux: Fix quadratic algorithm for sorting menu items
Mathieu Desnoyers [Mon, 13 Jun 2022 14:08:22 +0000 (10:08 -0400)] 
templates/linux: Fix quadratic algorithm for sorting menu items

The current implementation of the 10_linux script implements its menu
items sorting in bash with a quadratic algorithm, calling "sed", "sort",
"head", and "grep" to compare versions between individual lines, which
is annoyingly slow for kernel developers who can easily end up with
50-100 kernels in /boot.

As an example, on a Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz, running:

  /usr/sbin/grub-mkconfig > /dev/null

With 44 kernels in /boot, this command takes 10-15 seconds to complete.
After this fix, the same command runs in 5 seconds.

With 116 kernels in /boot, this command takes 40 seconds to complete.
After this fix, the same command runs in 8 seconds.

For reference, the quadratic algorithm here is:

while [ "x$list" != "x" ] ; do      <--- outer loop
  linux=`version_find_latest $list`
    version_find_latest()
      for i in "$@" ; do            <--- inner loop
        version_test_gt()
          fork+exec sed
            version_test_numeric()
              version_sort
                fork+exec sort
              fork+exec head -n 1
              fork+exec grep
  list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
    tr
    fgrep
    tr

So all commands executed under version_test_gt() are executed
O(n^2) times where n is the number of kernel images in /boot.

Here is the improved algorithm proposed:
  - Prepare a list with all the relevant information for ordering by a single
    sort(1) execution. This is done by renaming ".old" suffixes by " 1" and
    by suffixing all other files with " 2", thus making sure the ".old" entries
    will follow the non-old entries in reverse-sorted-order.
  - Call version_reverse_sort on the list (sort -r -V): A single execution of
    sort(1). For instance, GNU coreutils' sort will reverse-sort the list in
    O(n*log(n)) with a merge sort.
  - Replace the " 1" suffixes by ".old", and remove the " 2" suffixes.
  - Iterate on the reverse-sorted list to output each menu entry item.

Therefore, the algorithm proposed has O(n*log(n)) complexity with GNU
coreutils' sort compared to the prior O(n^2) complexity. Moreover, the
constant time required for each list entry is much less because sorting
is done within a single execution of sort(1) rather than requiring
O(n^2) executions of sed(1), sort(1), head(1), and grep(1) in
sub-shells.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agodocs: Add documentation on detached header option to cryptomount
Glenn Washburn [Wed, 8 Jun 2022 15:34:04 +0000 (10:34 -0500)] 
docs: Add documentation on detached header option to cryptomount

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agocryptodisk: Add support for using detached header files
Glenn Washburn [Wed, 8 Jun 2022 15:34:03 +0000 (10:34 -0500)] 
cryptodisk: Add support for using detached header files

Using the disk read hook mechanism, setup a read hook on the source disk
which will read from the given header file during the scan and recovery
cryptodisk backend functions. Disk read hooks are executed after the data
has been read from the disk. This is okay, because the read hook is given
the read buffer before its sent back to the caller. In this case, the hook
can then overwrite the data read from the disk device with data from the
header file sent in as the read hook data. This is transparent to the
read caller. Since the callers of this function have just opened the
source disk, there are no current read hooks, so there's no need to
save/restore them nor consider if they should be called or not.

This hook assumes that the header is at the start of the volume, which
is not the case for some formats (e.g. GELI). So GELI will return an
error if a detached header is specified. It also can only be used
with formats where the detached header file can be written to the
first blocks of the volume and the volume could still be unlocked.
So the header file can not be formatted differently from the on-disk
header. If these assumpts are not met, detached header file processing
must be specially handled in the cryptodisk backend module.

The hook will be called potentially many times by a backend. This is fine
because of the assumptions mentioned and the read hook reads from absolute
offsets and is stateless.

Also add a --header (short -H) option to cryptomount which takes a file
argument.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agodisk: Allow read hook callback to take read buffer to potentially modify it
Glenn Washburn [Wed, 8 Jun 2022 15:34:02 +0000 (10:34 -0500)] 
disk: Allow read hook callback to take read buffer to potentially modify it

It will be desirable in the future to allow having the read hook modify the
data passed back from a read function call on a disk or file. This adds that
infrastructure and has no impact on code flow for existing uses of the read
hook. Also changed is that now when the read hook callback is called it can
also indicate what error code should be sent back to the read caller.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agodocs: Document undocumented variables
Glenn Washburn [Fri, 13 May 2022 21:19:32 +0000 (16:19 -0500)] 
docs: Document undocumented variables

Document the variables net_<interface>_clientid, net_<interface>_clientuuid,
lockdown, and shim_lock in the list of special environment variables.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3 years agokern/efi/mm: Implement runtime addition of pages
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:22 +0000 (15:24 +1000)] 
kern/efi/mm: Implement runtime addition of pages

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
GRUB_MM_ADD_REGION_CONSECUTIVE flag. This allows us to set the function
up as callback for the memory subsystem and have it call out to us in
case there's not enough pages available in the current heap.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agokern/efi/mm: Pass up errors from add_memory_regions()
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:21 +0000 (15:24 +1000)] 
kern/efi/mm: Pass up errors from add_memory_regions()

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 more memory
from the firmware at runtime, where it doesn't make sense anymore to
fail hard.

Refactor the function to return an error to prepare for this. Note that
this does not change the behaviour when initializing the memory system
because grub_efi_mm_init() knows to call grub_fatal() in case
grub_efi_mm_add_regions() returns an error.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agokern/efi/mm: Extract function to add memory regions
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:20 +0000 (15:24 +1000)] 
kern/efi/mm: Extract function to add memory regions

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 <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agokern/efi/mm: Always request a fixed number of pages on init
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:19 +0000 (15:24 +1000)] 
kern/efi/mm: Always request a fixed number of pages on init

When initializing the EFI memory subsystem, 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 scaling of
requested memory based on available memory will not make a lot of sense
anymore.

Remove this logic as a preparatory patch such that we'll instead defer
to the runtime memory allocator. Note that ideally, we'd want to change
this after dynamic requesting of pages has been implemented for the EFI
platform. But because we'll need to split up initialization of the
memory subsystem and the request of pages from the firmware, we'd have
to duplicate quite some logic at first only to remove it afterwards
again. This seems quite pointless, so we instead have patches slightly
out of order.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agomm: Allow dynamically requesting additional memory regions
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:18 +0000 (15:24 +1000)] 
mm: Allow dynamically requesting additional memory regions

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 this many
bytes from the firmware and add it to the heap from the beginning on
some platforms like EFI. As this isn't needed for most configurations,
it is inefficient and may even negatively impact some usecases when,
e.g., chainloading. Nonetheless, allocating big chunks of memory is
required sometimes, where one example is the upcoming support for the
Argon2 key derival function in LUKS2.

In order to avoid pre-allocating big chunks of memory, this commit
implements a runtime mechanism to add more pages to the system. When
a given allocation cannot be currently satisfied, we'll call a given
callback set up by the platform's own memory management subsystem,
asking it to add a memory area with at least "n" bytes. If this
succeeds, we retry searching for a valid memory region, which should
now succeed.

If this fails, we try asking for "n" bytes, possibly spread across
multiple regions, in hopes that region merging means that we end up
with enough memory for things to work out.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agomm: Drop unused unloading of modules on OOM
Patrick Steinhardt [Thu, 21 Apr 2022 05:24:17 +0000 (15:24 +1000)] 
mm: Drop unused unloading of modules on OOM

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_unneeded().

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
3 years agomm: Debug support for region operations
Daniel Axtens [Thu, 21 Apr 2022 05:24:16 +0000 (15:24 +1000)] 
mm: Debug support for region operations

This is handy for debugging. Enable with "set debug=regions".

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>