]> git.ipfire.org Git - thirdparty/grub.git/log
thirdparty/grub.git
6 months agofs/xfs: Fix out-of-bounds read
Michael Chang [Fri, 31 May 2024 07:14:57 +0000 (15:14 +0800)] 
fs/xfs: Fix out-of-bounds read

The number of records in the root key array read from disk was not being
validated against the size of the root node. This could lead to an
out-of-bounds read.

This patch adds a check to ensure that the number of records in the root
key array does not exceed the expected size of a root node read from
disk. If this check detects an out-of-bounds condition the operation is
aborted to prevent random errors due to metadata corruption.

Reported-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/ntfs: Implement attribute verification
B Horn [Tue, 14 May 2024 11:39:56 +0000 (12:39 +0100)] 
fs/ntfs: Implement attribute verification

It was possible to read OOB when an attribute had a size that exceeded
the allocated buffer. This resolves that by making sure all attributes
that get read are fully in the allocated space by implementing
a function to validate them.

Defining the offsets in include/grub/ntfs.h but they are only used in
the validation function and not across the rest of the NTFS code.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/ntfs: Use a helper function to access attributes
B Horn [Tue, 14 May 2024 11:39:56 +0000 (12:39 +0100)] 
fs/ntfs: Use a helper function to access attributes

Right now to access the next attribute the code reads the length of the
current attribute and adds that to the current pointer. This is error
prone as bounds checking needs to be performed all over the place. So,
implement a helper and ensure its used across find_attr() and read_attr().

This commit does *not* implement full bounds checking. It is just the
preparation work for this to be added into the helper.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/ntfs: Track the end of the MFT attribute buffer
B Horn [Tue, 7 Jan 2025 11:38:34 +0000 (11:38 +0000)] 
fs/ntfs: Track the end of the MFT attribute buffer

The end of the attribute buffer should be stored alongside the rest of
the attribute struct as right now it is not possible to implement bounds
checking when accessing attributes sequentially.

This is done via:
  - updating init_attr() to set at->end and check is is not initially out of bounds,
  - implementing checks as init_attr() had its type change in its callers,
  - updating the value of at->end when needed.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/ntfs: Fix out-of-bounds read
Michael Chang [Mon, 3 Jun 2024 04:12:06 +0000 (12:12 +0800)] 
fs/ntfs: Fix out-of-bounds read

When parsing NTFS file records the presence of the 0xFF marker indicates
the end of the attribute list. This value signifies that there are no
more attributes to process.

However, when the end marker is missing due to corrupted metadata the
loop continues to read beyond the attribute list resulting in out-of-bounds
reads and potentially entering an infinite loop.

This patch adds a check to provide a stop condition for the loop ensuring
it stops at the end of the attribute list or at the end of the Master File
Table. This guards against out-of-bounds reads and prevents infinite loops.

Reported-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/ext2: Fix out-of-bounds read for inline extents
Michael Chang [Fri, 31 May 2024 07:14:23 +0000 (15:14 +0800)] 
fs/ext2: Fix out-of-bounds read for inline extents

When inline extents are used, i.e. the extent tree depth equals zero,
a maximum of four entries can fit into the inode's data block. If the
extent header states a number of entries greater than four the current
ext2 implementation causes an out-of-bounds read. Fix this issue by
capping the number of extents to four when reading inline extents.

Reported-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/jfs: Inconsistent signed/unsigned types usage in return values
Lidong Chen [Mon, 16 Dec 2024 20:22:40 +0000 (20:22 +0000)] 
fs/jfs: Inconsistent signed/unsigned types usage in return values

The getblk() returns a value of type grub_int64_t which is assigned to
iagblk and inoblk, both of type grub_uint64_t, in grub_jfs_read_inode()
via grub_jfs_blkno(). This patch fixes the type mismatch in the
functions. Additionally, the getblk() will return 0 instead of -1 on
failure cases. This change is safe because grub_errno is always set in
getblk() to indicate errors and it is later checked in the callers.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/jfs: Use full 40 bits offset and address for a data extent
Lidong Chen [Mon, 16 Dec 2024 20:22:39 +0000 (20:22 +0000)] 
fs/jfs: Use full 40 bits offset and address for a data extent

An extent's logical offset and address are represented as a 40-bit value
split into two parts: the most significant 8 bits and the least
significant 32 bits. Currently the JFS code uses only the least
significant 32 bits value for offsets and addresses assuming the data
size will never exceed the 32-bit range. This approach ignores the most
significant 8 bits potentially leading to incorrect offsets and
addresses for larger values. The patch fixes it by incorporating the
most significant 8 bits into the calculation to get the full 40-bits
value for offsets and addresses.

https://jfs.sourceforge.net/project/pub/jfslayout.pdf

  "off1,off2 is a 40-bit field, containing the logical offset of the first
   block in the extent.
   ...
   addr1,addr2 is a 40-bit field, containing the address of the extent."

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/jfs: Fix OOB read caused by invalid dir slot index
Lidong Chen [Fri, 22 Nov 2024 06:28:00 +0000 (06:28 +0000)] 
fs/jfs: Fix OOB read caused by invalid dir slot index

While fuzz testing JFS with ASAN enabled an OOB read was detected in
grub_jfs_opendir(). The issue occurred due to an invalid directory slot
index in the first entry of the sorted directory slot array in the inode
directory header. The fix ensures the slot index is validated before
accessing it. Given that an internal or a leaf node in a directory B+
tree is a 4 KiB in size and each directory slot is always 32 bytes, the
max number of slots in a node is 128. The validation ensures that the
slot index doesn't exceed this limit.

[1] https://jfs.sourceforge.net/project/pub/jfslayout.pdf

  JFS will allocate 4K of disk space for an internal node of the B+ tree.
  An internal node looks the same as a leaf node.
          - page 10

  Fixed number of Directory Slots depending on the size of the node. These are
  the slots to be used for storing the directory slot array and the directory
  entries or router entries. A directory slot is always 32 bytes.
  ...
  A Directory Slot Array which is a sorted array of indices to the directory
  slots that are currently in use.
  ...
  An internal or a leaf node in the directory B+ tree is a 4K page.
          - page 25

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
6 months agofs/jfs: Fix OOB read in jfs_getent()
Lidong Chen [Fri, 22 Nov 2024 06:27:59 +0000 (06:27 +0000)] 
fs/jfs: Fix OOB read in jfs_getent()

The JFS fuzzing revealed an OOB read in grub_jfs_getent(). The crash
was caused by an invalid leaf nodes count, diro->dirpage->header.count,
which was larger than the maximum number of leaf nodes allowed in an
inode. This fix is to ensure that the leaf nodes count is validated in
grub_jfs_opendir() before calling grub_jfs_getent().

On the occasion replace existing raw numbers with newly defined constant.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
6 months agofs/iso9660: Fix invalid free
Michael Chang [Fri, 31 May 2024 07:14:42 +0000 (15:14 +0800)] 
fs/iso9660: Fix invalid free

The ctx->filename can point to either a string literal or a dynamically
allocated string. The ctx->filename_alloc field is used to indicate the
type of allocation.

An issue has been identified where ctx->filename is reassigned to
a string literal in susp_iterate_dir() but ctx->filename_alloc is not
correctly handled. This oversight causes a memory leak and an invalid
free operation later.

The fix involves checking ctx->filename_alloc, freeing the allocated
string if necessary and clearing ctx->filename_alloc for string literals.

Reported-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/iso9660: Set a grub_errno if mount fails
B Horn [Sun, 12 May 2024 05:37:08 +0000 (06:37 +0100)] 
fs/iso9660: Set a grub_errno if mount fails

It was possible for a grub_errno to not be set if mount of an ISO 9660
filesystem failed when set_rockridge() returned 0.

This isn't known to be exploitable as the other filesystems due to
filesystem helper checking the requested file type. Though fixing
as a precaution.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/hfsplus: Set a grub_errno if mount fails
B Horn [Sun, 12 May 2024 05:22:51 +0000 (06:22 +0100)] 
fs/hfsplus: Set a grub_errno if mount fails

It was possible for mount to fail but not set grub_errno. This led to
a possible double decrement of the module reference count if the NULL
page was mapped.

Fixing in general as a similar bug was fixed in commit 61b13c187
(fs/hfsplus: Set grub_errno to prevent NULL pointer access) and there
are likely more variants around.

Fixes: CVE-2024-45783
Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/f2fs: Set a grub_errno if mount fails
B Horn [Sun, 12 May 2024 05:15:03 +0000 (06:15 +0100)] 
fs/f2fs: Set a grub_errno if mount fails

It was previously possible for grub_errno to not be set when
grub_f2fs_mount() failed if nat_bitmap_ptr() returned NULL.

This issue is solved by ensuring a grub_errno is set in the fail case.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/tar: Integer overflow leads to heap OOB write
Lidong Chen [Fri, 22 Nov 2024 06:27:58 +0000 (06:27 +0000)] 
fs/tar: Integer overflow leads to heap OOB write

Both namesize and linksize are derived from hd.size, a 12-digit octal
number parsed by read_number(). Later direct arithmetic calculation like
"namesize + 1" and "linksize + 1" may exceed the maximum value of
grub_size_t leading to heap OOB write. This patch fixes the issue by
using grub_add() and checking for an overflow.

Fixes: CVE-2024-45780
Reported-by: Nils Langius <nils@langius.de>
Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
6 months agofs/tar: Initialize name in grub_cpio_find_file()
B Horn [Sun, 12 May 2024 01:47:54 +0000 (02:47 +0100)] 
fs/tar: Initialize name in grub_cpio_find_file()

It was possible to iterate through grub_cpio_find_file() without
allocating name and not setting mode to GRUB_ARCHELP_ATTR_END, which
would cause the uninitialized value for name to be used as an argument
for canonicalize() in grub_archelp_dir().

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
6 months agofs/hfs: Fix stack OOB write with grub_strcpy()
B Horn [Sun, 12 May 2024 01:48:33 +0000 (02:48 +0100)] 
fs/hfs: Fix stack OOB write with grub_strcpy()

Replaced with grub_strlcpy().

Fixes: CVE-2024-45782
Fixes: CVE-2024-56737
Fixes: https://savannah.gnu.org/bugs/?66599
Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agofs/ufs: Fix a heap OOB write
B Horn [Sun, 12 May 2024 01:03:33 +0000 (02:03 +0100)] 
fs/ufs: Fix a heap OOB write

grub_strcpy() was used to copy a symlink name from the filesystem
image to a heap allocated buffer. This led to a OOB write to adjacent
heap allocations. Fix by using grub_strlcpy().

Fixes: CVE-2024-45781
Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agomisc: Implement grub_strlcpy()
B Horn [Sat, 15 Jun 2024 01:33:08 +0000 (02:33 +0100)] 
misc: Implement grub_strlcpy()

grub_strlcpy() acts the same way as strlcpy() does on most *NIX,
returning the length of src and ensuring dest is always NUL
terminated except when size is 0.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agotpm2_key_protector: Enable build for powerpc_ieee1275
Stefan Berger [Tue, 26 Nov 2024 20:39:46 +0000 (15:39 -0500)] 
tpm2_key_protector: Enable build for powerpc_ieee1275

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agoieee1275/tcg2: Add TCG2 driver for ieee1275 PowerPC firmware
Stefan Berger [Tue, 26 Nov 2024 20:39:45 +0000 (15:39 -0500)] 
ieee1275/tcg2: Add TCG2 driver for ieee1275 PowerPC firmware

Follow recent extensions of EFI support providing a TCG2 driver with
a public API for getting the maximum TPM command size and passing a TPM
command through to the TPM 2. Implement this functionality using ieee1275
PowerPC firmware API calls. Move tcg2.c into the TCG2 driver.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agoieee1275/tcg2: Refactor grub_ieee1275_tpm_init()
Stefan Berger [Tue, 26 Nov 2024 20:39:44 +0000 (15:39 -0500)] 
ieee1275/tcg2: Refactor grub_ieee1275_tpm_init()

Move tpm_get_tpm_version() into grub_ieee1275_tpm_init() and invalidate
grub_ieee1275_tpm_ihandle in case no TPM 2 could be detected. Try the
initialization only once so that grub_tpm_present() will always return
the same result. Use the grub_ieee1275_tpm_ihandle as indicator for an
available TPM instead of grub_ieee1275_tpm_version, which can now be
removed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agoieee1275/ibmvpm: Move TPM initialization functions to own file
Stefan Berger [Tue, 26 Nov 2024 20:39:43 +0000 (15:39 -0500)] 
ieee1275/ibmvpm: Move TPM initialization functions to own file

Move common initialization functions from the ibmvtpm driver module into
tcg2.c that will be moved into the new TCG2 driver in a subsequent patch.
Make the functions available to the ibmvtpm driver as public functions
and variables.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agoieee1275: Consolidate repeated definitions of IEEE1275_IHANDLE_INVALID
Stefan Berger [Tue, 26 Nov 2024 20:39:42 +0000 (15:39 -0500)] 
ieee1275: Consolidate repeated definitions of IEEE1275_IHANDLE_INVALID

Consolidate repeated definitions of IEEE1275_IHANDLE_INVALID that are cast
to the type grub_ieee1275_ihandle_t. On the occasion add "GRUB_" prefix to
the constant name.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agoterm/ieee1275/serial: Cast 0 to proper type
Stefan Berger [Tue, 26 Nov 2024 20:39:41 +0000 (15:39 -0500)] 
term/ieee1275/serial: Cast 0 to proper type

Cast 0 to proper type grub_ieee1275_ihandle_t. This type is
used for struct grub_serial_port's handle that assigns or
compares with IEEE1275_IHANDLE_INVALID.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agotss2: Adjust bit fields for big endian targets
Stefan Berger [Tue, 26 Nov 2024 20:39:40 +0000 (15:39 -0500)] 
tss2: Adjust bit fields for big endian targets

The TPM bit fields need to be in reverse order for big endian targets,
such as ieee1275 PowerPC platforms that run GRUB in big endian mode.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agodocs: Document TPM2 key protector
Gary Lin [Fri, 15 Nov 2024 07:35:01 +0000 (15:35 +0800)] 
docs: Document TPM2 key protector

Update the user manual to address TPM2 key protector including the two
related commands, tpm2_key_protector_init and tpm2_key_protector_clear,
and the user-space utility: grub-protect.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotests: Add tpm2_key_protector_test
Gary Lin [Fri, 15 Nov 2024 07:35:00 +0000 (15:35 +0800)] 
tests: Add tpm2_key_protector_test

For the tpm2_key_protector module, the TCG2 command submission function
is the only difference between a QEMU instance and grub-emu. To test
TPM2 key unsealing with a QEMU instance, it requires an extra OS image
to invoke grub-protect to seal the LUKS key, rather than a simple
grub-shell rescue CD image. On the other hand, grub-emu can share the
emulated TPM2 device with the host, so that we can seal the LUKS key on
host and test key unsealing with grub-emu.

This test script firstly creates a simple LUKS image to be loaded as a
loopback device in grub-emu. Then an emulated TPM2 device is created by
"swtpm chardev" and PCR 0 and 1 are extended.

There are several test cases in the script to test various settings. Each
test case uses grub-protect or tpm2-tools to seal the LUKS password
with PCR 0 and PCR 1. Then grub-emu is launched to load the LUKS image,
try to mount the image with tpm2_key_protector_init and cryptomount, and
verify the result.

Based on the idea from Michael Chang.

Cc: Michael Chang <mchang@suse.com>
Cc: Stefan Berger <stefanb@linux.ibm.com>
Cc: Glenn Washburn <development@efficientek.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotpm2_key_protector: Add grub-emu support
Gary Lin [Fri, 15 Nov 2024 07:34:59 +0000 (15:34 +0800)] 
tpm2_key_protector: Add grub-emu support

As a preparation to test tpm2_key_protector with grub-emu, the new
option, --tpm-device, is introduced to specify the TPM device for
grub-emu so that grub-emu can access an emulated TPM device from
the host.

Since grub-emu can directly access the device on host, it's easy to
implement the essential TCG2 command submission function with the
read/write functions and enable tpm2_key_protector module for grub-emu,
so that we can further test TPM2 key unsealing with grub-emu.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agodiskfilter: Look up cryptodisk devices first
Gary Lin [Fri, 15 Nov 2024 07:34:58 +0000 (15:34 +0800)] 
diskfilter: Look up cryptodisk devices first

When using disk auto-unlocking with TPM 2.0, the typical grub.cfg may
look like this:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub/sealed.tpm
  cryptomount -u <PART-UUID> -P tpm2
  search --fs-uuid --set=root <FS-UUID>

Since the disk search order is based on the order of module loading, the
attacker could insert a malicious disk with the same FS-UUID root to
trick GRUB to boot into the malicious root and further dump memory to
steal the unsealed key.

Do defend against such an attack, we can specify the hint provided by
"grub-probe" to search the encrypted partition first:

  search --fs-uuid --set=root --hint='cryptouuid/<PART-UUID>' <FS-UUID>

However, for LVM on an encrypted partition, the search hint provided by
"grub-probe" is:

  --hint='lvmid/<VG-UUID>/<LV-UUID>'

It doesn't guarantee to look up the logical volume from the encrypted
partition, so the attacker may have the chance to fool GRUB to boot
into the malicious disk.

To minimize the attack surface, this commit tweaks the disk device search
in diskfilter to look up cryptodisk devices first and then others, so
that the auto-unlocked disk will be found first, not the attacker's disk.

Cc: Fabian Vogt <fvogt@suse.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agocryptodisk: Wipe out the cached keys from protectors
Gary Lin [Fri, 15 Nov 2024 07:34:57 +0000 (15:34 +0800)] 
cryptodisk: Wipe out the cached keys from protectors

An attacker may insert a malicious disk with the same crypto UUID and
trick GRUB to mount the fake root. Even though the key from the key
protector fails to unlock the fake root, it's not wiped out cleanly so
the attacker could dump the memory to retrieve the secret key. To defend
such attack, wipe out the cached key when we don't need it.

Cc: Fabian Vogt <fvogt@suse.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agocryptodisk: Fallback to passphrase
Patrick Colp [Fri, 15 Nov 2024 07:34:56 +0000 (15:34 +0800)] 
cryptodisk: Fallback to passphrase

If a protector is specified, but it fails to unlock the disk, fall back
to asking for the passphrase.

Before requesting the passphrase, the error from the key protector(s)
has to be cleared, or the later code, e.g., LUKS code, may stop as
grub_errno is set. This commit prints error from the key protector(s)
and sets grub_errno to GRUB_ERR_NONE to have a fresh start.

Signed-off-by: Patrick Colp <patrick.colp@oracle.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotpm2_key_protector: Implement NV index
Patrick Colp [Fri, 15 Nov 2024 07:34:55 +0000 (15:34 +0800)] 
tpm2_key_protector: Implement NV index

Currently with the TPM2 protector, only SRK mode is supported and
NV index support is just a stub. Implement the NV index option.

Note: This only extends support on the unseal path. grub-protect
has not been updated. tpm2-tools can be used to insert a key into
the NV index.

An example of inserting a key using tpm2-tools:

  # Get random key.
  tpm2_getrandom 32 > key.dat

  # Create primary object.
  tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx

  # Create policy object. `pcrs.dat` contains the PCR values to seal against.
  tpm2_startauthsession -S session.dat
  tpm2_policypcr -S session.dat -l sha256:7,11 -f pcrs.dat -L policy.dat
  tpm2_flushcontext session.dat

  # Seal key into TPM.
  cat key.dat | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i-
  tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx
  tpm2_evictcontrol -C o -c sealing.ctx 0x81000000

Then to unseal the key in GRUB, add this to grub.cfg:

  tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=7,11
  cryptomount -u <UUID> --protector tpm2

Signed-off-by: Patrick Colp <patrick.colp@oracle.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotpm2_key_protector: Support authorized policy
Gary Lin [Fri, 15 Nov 2024 07:34:54 +0000 (15:34 +0800)] 
tpm2_key_protector: Support authorized policy

This commit handles the TPM2_PolicyAuthorize command from the key file
in TPM 2.0 Key File format.

TPM2_PolicyAuthorize is the essential command to support authorized
policy which allows the users to sign TPM policies with their own keys.
Per TPM 2.0 Key File [1], CommandPolicy for TPM2_PolicyAuthorize
comprises "TPM2B_PUBLIC pubkey", "TPM2B_DIGEST policy_ref", and
"TPMT_SIGNATURE signature". To verify the signature, the current policy
digest is hashed with the hash algorithm written in "signature", and then
"signature" is verified with the hashed policy digest and "pubkey". Once
TPM accepts "signature", TPM2_PolicyAuthorize is invoked to authorize the
signed policy.

To create the key file with authorized policy, here are the pcr-oracle [2]
commands:

  # Generate the RSA key and create the authorized policy file
  $ pcr-oracle \
--rsa-generate-key \
--private-key policy-key.pem \
--auth authorized.policy \
create-authorized-policy 0,2,4,7,9

  # Seal the secret with the authorized policy
  $ pcr-oracle \
--key-format tpm2.0 \
--auth authorized.policy \
--input disk-secret.txt \
--output sealed.key \
seal-secret

  # Sign the predicted PCR policy
  $ pcr-oracle \
--key-format tpm2.0 \
--private-key policy-key.pem \
--from eventlog \
--stop-event "grub-file=grub.cfg" \
--after \
--input sealed.key \
--output /boot/efi/efi/grub/sealed.tpm \
sign 0,2,4,7,9

Then specify the key file and the key protector to grub.cfg in the EFI
system partition:

  tpm2_key_protector_init -a RSA --tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm
  cryptomount -u <PART_UUID> -P tpm2

For any change in the boot components, just run the "sign" command again
to update the signature in sealed.tpm, and TPM can unseal the key file
with the updated PCR policy.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://github.com/okirch/pcr-oracle

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoutil/grub-protect: Add new tool
Hernan Gatta [Fri, 15 Nov 2024 07:34:53 +0000 (15:34 +0800)] 
util/grub-protect: Add new tool

To utilize the key protectors framework, there must be a way to protect
full-disk encryption keys in the first place. The grub-protect tool
includes support for the TPM2 key protector but other protectors that
require setup ahead of time can be supported in the future.

For the TPM2 key protector, the intended flow is for a user to have
a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates
a new LUKS key file, say by reading /dev/urandom into a file, and creates
a new LUKS key slot for this key. Then, the user invokes the grub-protect
tool to seal this key file to a set of PCRs using the system's TPM 2.0.
The resulting sealed key file is stored in an unencrypted partition such
as the EFI System Partition (ESP) so that GRUB may read it. The user also
has to ensure the cryptomount command is included in GRUB's boot script
and that it carries the requisite key protector (-P) parameter.

Sample usage:

  $ dd if=/dev/urandom of=luks-key bs=1 count=32
  $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2 --hash=sha512

To seal the key with TPM 2.0 Key File (recommended):

  $ sudo grub-protect --action=add \
                      --protector=tpm2 \
                      --tpm2-pcrs=0,2,4,7,9 \
                      --tpm2key \
                      --tpm2-keyfile=luks-key \
                      --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm

Or, to seal the key with the raw sealed key:

  $ sudo grub-protect --action=add \
                      --protector=tpm2 \
                      --tpm2-pcrs=0,2,4,7,9 \
                      --tpm2-keyfile=luks-key \
                      --tpm2-outfile=/boot/efi/efi/grub/sealed.key

Then, in the boot script, for TPM 2.0 Key File:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm
  cryptomount -u <SDB1_UUID> -P tpm2

Or, for the raw sealed key:

  tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9
  cryptomount -u <SDB1_UUID> -P tpm2

The benefit of using TPM 2.0 Key File is that the PCR set is already
written in the key file, so there is no need to specify PCRs when
invoking tpm2_key_protector_init.

Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agocryptodisk: Support key protectors
Hernan Gatta [Fri, 15 Nov 2024 07:34:52 +0000 (15:34 +0800)] 
cryptodisk: Support key protectors

Add a new parameter to cryptomount to support the key protectors framework: -P.
The parameter is used to automatically retrieve a key from specified key
protectors. The parameter may be repeated to specify any number of key
protectors. These are tried in order until one provides a usable key for any
given disk.

Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Michael Chang <mchang@suse.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agokey_protector: Add TPM2 Key Protector
Hernan Gatta [Fri, 15 Nov 2024 07:34:51 +0000 (15:34 +0800)] 
key_protector: Add TPM2 Key Protector

The TPM2 key protector is a module that enables the automatic retrieval
of a fully-encrypted disk's unlocking key from a TPM 2.0.

The theory of operation is such that the module accepts various
arguments, most of which are optional and therefore possess reasonable
defaults. One of these arguments is the keyfile/tpm2key parameter, which
is mandatory. There are two supported key formats:

1. Raw Sealed Key (--keyfile)
   When sealing a key with TPM2_Create, the public portion of the sealed
   key is stored in TPM2B_PUBLIC, and the private portion is in
   TPM2B_PRIVATE. The raw sealed key glues the fully marshalled
   TPM2B_PUBLIC and TPM2B_PRIVATE into one file.

2. TPM 2.0 Key (--tpm2key)
   The following is the ASN.1 definition of TPM 2.0 Key File:

   TPMPolicy ::= SEQUENCE {
     CommandCode   [0] EXPLICIT INTEGER
     CommandPolicy [1] EXPLICIT OCTET STRING
   }

   TPMAuthPolicy ::= SEQUENCE {
     Name    [0] EXPLICIT UTF8STRING OPTIONAL
     Policy  [1] EXPLICIT SEQUENCE OF TPMPolicy
   }

   TPMKey ::= SEQUENCE {
     type        OBJECT IDENTIFIER
     emptyAuth   [0] EXPLICIT BOOLEAN OPTIONAL
     policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
     secret      [2] EXPLICIT OCTET STRING OPTIONAL
     authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
     description [4] EXPLICIT UTF8String OPTIONAL,
     rsaParent   [5] EXPLICIT BOOLEAN OPTIONAL,
     parent      INTEGER
     pubkey      OCTET STRING
     privkey     OCTET STRING
   }

  The TPM2 key protector only expects a "sealed" key in DER encoding,
  so "type" is always 2.23.133.10.1.5, "emptyAuth" is "TRUE", and
  "secret" is empty. "policy" and "authPolicy" are the possible policy
  command sequences to construct the policy digest to unseal the key.
  Similar to the raw sealed key, the public portion (TPM2B_PUBLIC) of
  the sealed key is stored in "pubkey", and the private portion
  (TPM2B_PRIVATE) is in "privkey".

  For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

This sealed key file is created via the grub-protect tool. The tool
utilizes the TPM's sealing functionality to seal (i.e., encrypt) an
unlocking key using a Storage Root Key (SRK) to the values of various
Platform Configuration Registers (PCRs). These PCRs reflect the state
of the system as it boots. If the values are as expected, the system
may be considered trustworthy, at which point the TPM allows for a
caller to utilize the private component of the SRK to unseal (i.e.,
decrypt) the sealed key file. The caller, in this case, is this key
protector.

The TPM2 key protector registers two commands:

  - tpm2_key_protector_init: Initializes the state of the TPM2 key
                             protector for later usage, clearing any
                             previous state, too, if any.

  - tpm2_key_protector_clear: Clears any state set by tpm2_key_protector_init.

The way this is expected to be used requires the user to, either
interactively or, normally, via a boot script, initialize/configure
the key protector and then specify that it be used by the "cryptomount"
command (modifications to this command are in a different patch).

For instance, to unseal the raw sealed key file:

  tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed-1.key
  cryptomount -u <PART1_UUID> -P tpm2

  tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed-2.key --pcrs=7,11
  cryptomount -u <PART2_UUID> -P tpm2

Or, to unseal the TPM 2.0 Key file:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/sealed-1.tpm
  cryptomount -u <PART1_UUID> -P tpm2

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/sealed-2.tpm --pcrs=7,11
  cryptomount -u <PART2_UUID> -P tpm2

If a user does not initialize the key protector and attempts to use it
anyway, the protector returns an error.

Before unsealing the key, the TPM2 key protector follows the "TPMPolicy"
sequences to enforce the TPM policy commands to construct a valid policy
digest to unseal the key.

For the TPM 2.0 Key files, "authPolicy" may contain multiple "TPMPolicy"
sequences, the TPM2 key protector iterates "authPolicy" to find a valid
sequence to unseal key. If "authPolicy" is empty or all sequences in
"authPolicy" fail, the protector tries the one from "policy". In case
"policy" is also empty, the protector creates a "TPMPolicy" sequence
based on the given PCR selection.

For the raw sealed key, the TPM2 key protector treats the key file as a
TPM 2.0 Key file without "authPolicy" and "policy", so the "TPMPolicy"
sequence is always based on the PCR selection from the command
parameters.

This commit only supports one policy command: TPM2_PolicyPCR. The
command set will be extended to support advanced features, such as
authorized policy, in the later commits.

Cc: James Bottomley <jejb@linux.ibm.com>
Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotss2: Add TPM2 Software Stack (TSS2) support
Gary Lin [Fri, 15 Nov 2024 07:34:50 +0000 (15:34 +0800)] 
tss2: Add TPM2 Software Stack (TSS2) support

A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to
compose and submit TPM commands and parse responses.

A limited number of TPM commands may be accessed via the EFI TCG2
protocol. This protocol exposes functionality that is primarily geared
toward TPM usage within the context of Secure Boot. For all other TPM
commands, however, such as sealing and unsealing, this protocol does not
provide any help, with the exception of passthrough command submission.

The SubmitCommand method allows a caller to send raw commands to the
system's TPM and to receive the corresponding response. These
command/response pairs are formatted using the TPM wire protocol. To
construct commands in this way, and to parse the TPM's response, it is
necessary to, first, possess knowledge of the various TPM structures, and,
second, of the TPM wire protocol itself.

As such, this patch includes implementations of various grub_tpm2_* functions
(inventoried below), and logic to write and read command and response
buffers, respectively, using the TPM wire protocol.

Functions:
  - grub_tpm2_create(),
  - grub_tpm2_createprimary(),
  - grub_tpm2_evictcontrol(),
  - grub_tpm2_flushcontext(),
  - grub_tpm2_load(),
  - grub_tpm2_pcr_read(),
  - grub_tpm2_policygetdigest(),
  - grub_tpm2_policypcr(),
  - grub_tpm2_readpublic(),
  - grub_tpm2_startauthsession(),
  - grub_tpm2_unseal(),
  - grub_tpm2_loadexternal(),
  - grub_tpm2_hash(),
  - grub_tpm2_verifysignature(),
  - grub_tpm2_policyauthorize(),
  - grub_tpm2_testparms().

Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotss2: Add TPM2 types and Marshal/Unmarshal functions
Gary Lin [Fri, 15 Nov 2024 07:34:49 +0000 (15:34 +0800)] 
tss2: Add TPM2 types and Marshal/Unmarshal functions

This commit adds the necessary TPM2 types and structs as the preparation
for the TPM2 Software Stack (TSS2) support. The Marshal/Unmarshal
functions are also added to handle the data structure to be submitted to
TPM2 commands and to be received from the response.

Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agotss2: Add TPM2 buffer handling functions
Gary Lin [Fri, 15 Nov 2024 07:34:48 +0000 (15:34 +0800)] 
tss2: Add TPM2 buffer handling functions

As the preparation to support TPM2 Software Stack (TSS2), this commit
implements the TPM2 buffer handling functions to pack data for the TPM2
commands and unpack the data from the response.

Cc: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agokey_protector: Add key protectors framework
Hernan Gatta [Fri, 15 Nov 2024 07:34:47 +0000 (15:34 +0800)] 
key_protector: Add key protectors framework

A key protector encapsulates functionality to retrieve an unlocking key
for a fully-encrypted disk from a specific source. A key protector
module registers itself with the key protectors framework when it is
loaded and unregisters when unloaded. Additionally, a key protector may
accept parameters that describe how it should operate.

The key protectors framework, besides offering registration and
unregistration functions, also offers a one-stop routine for finding and
invoking a key protector by name. If a key protector with the specified
name exists and if an unlocking key is successfully retrieved by it, the
function returns to the caller the retrieved key and its length.

Cc: Vladimir Serbinenko <phcoder@gmail.com>
Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Add the documentation
Gary Lin [Fri, 15 Nov 2024 07:34:46 +0000 (15:34 +0800)] 
libtasn1: Add the documentation

Document libtasn1 in docs/grub-dev.texi and add the upgrade steps.
Also add the patches to make libtasn1 compatible with GRUB code.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Test module for libtasn1
Daniel Axtens [Fri, 15 Nov 2024 07:34:45 +0000 (15:34 +0800)] 
asn1_test: Test module for libtasn1

Import tests from libtasn1 that use functionality we import.
This test module is integrated into functional_test so that the
user can run the test in GRUB shell.

This doesn't test the full decoder but that will be exercised in
test suites for coming patch sets.

Add testcase target in accordance with commit 5e10be48e5 (tests: Add
check-native and check-nonnative make targets).

Cc: Vladimir Serbinenko <phcoder@gmail.com>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Compile into asn1 module
Daniel Axtens [Fri, 15 Nov 2024 07:34:44 +0000 (15:34 +0800)] 
libtasn1: Compile into asn1 module

Create a wrapper file that specifies the module license.
Set up the makefile so it is built.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Enable the testcase only when GRUB_LONG_MAX is larger than GRUB_INT_MAX
Gary Lin [Fri, 15 Nov 2024 07:34:43 +0000 (15:34 +0800)] 
asn1_test: Enable the testcase only when GRUB_LONG_MAX is larger than GRUB_INT_MAX

There is a testcase to test the values larger than "int" but smaller
than "long". However, for some architectures, "long" and "int" are the
same and the compiler may issue a warning like this:

grub-core/tests/asn1/tests/Test_overflow.c:48:50: error: left shift of negative value [-Werror=shift-negative-value]
       unsigned long num = ((long) GRUB_UINT_MAX) << 2;
                                                  ^~

To avoid unnecessary error the testcase is enabled only when
GRUB_LONG_MAX is larger than GRUB_INT_MAX.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Use the grub-specific functions and types
Gary Lin [Fri, 15 Nov 2024 07:34:42 +0000 (15:34 +0800)] 
asn1_test: Use the grub-specific functions and types

This commit converts functions and types to the grub-specific ones:
  - LONG_MAX -> GRUB_LONG_MAX,
  - INT_MAX -> GRUB_INT_MAX,
  - UINT_MAX -> GRUB_UINT_MAX,
  - size_t -> grub_size_t,
  - memcmp() -> grub_memcmp(),
  - memcpy() -> grub_memcpy(),
  - free() -> grub_free(),
  - strcmp() -> grub_strcmp().

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Print the error messages with grub_printf()
Gary Lin [Fri, 15 Nov 2024 07:34:41 +0000 (15:34 +0800)] 
asn1_test: Print the error messages with grub_printf()

This commit replaces printf() and fprintf() with grub_printf() to print
the error messages for the testcases. Besides, asn1_strerror() is used
to convert the result code to strings instead of asn1_perror().

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Remove "verbose" and the unnecessary printf()
Gary Lin [Fri, 15 Nov 2024 07:34:40 +0000 (15:34 +0800)] 
asn1_test: Remove "verbose" and the unnecessary printf()

This commit removes the "verbose" variables and the unnecessary printf()
to simplify the output.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Return either 0 or 1 to reflect the results
Gary Lin [Fri, 15 Nov 2024 07:34:39 +0000 (15:34 +0800)] 
asn1_test: Return either 0 or 1 to reflect the results

Some testcases use exit() to end the test. Since all the asn1 testcases
are invoked as functions, this commit replaces exit() with return to
reflect the test results, so that the main test function can check the
results.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Rename the main functions to the test names
Gary Lin [Fri, 15 Nov 2024 07:34:38 +0000 (15:34 +0800)] 
asn1_test: Rename the main functions to the test names

This commit changes the main functions in the testcases to the test
names so that the real "main" test function can invokes them.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoasn1_test: Include asn1_test.h only
Gary Lin [Fri, 15 Nov 2024 07:34:37 +0000 (15:34 +0800)] 
asn1_test: Include asn1_test.h only

This commit removes all the headers and only uses asn1_test.h.
To avoid including int.h from grub-core/lib/libtasn1-grub/lib,
CONST_DOWN is defined in reproducers.c.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Fix the potential buffer overrun
Gary Lin [Fri, 15 Nov 2024 07:34:36 +0000 (15:34 +0800)] 
libtasn1: Fix the potential buffer overrun

In _asn1_tag_der(), the first while loop for the long form may end up
with a "k" value with "ASN1_MAX_TAG_SIZE" and cause the buffer overrun
in the second while loop. This commit tweaks the conditional check to
avoid producing a too large "k".

This is a quick fix and may differ from the official upstream fix.

libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Use grub_divmod64() for division
Gary Lin [Fri, 15 Nov 2024 07:34:35 +0000 (15:34 +0800)] 
libtasn1: Use grub_divmod64() for division

Replace a 64-bit division with a call to grub_divmod64(), preventing
creation of __udivdi3() calls on 32-bit platforms.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Adjust the header paths in libtasn1.h
Gary Lin [Fri, 15 Nov 2024 07:34:34 +0000 (15:34 +0800)] 
libtasn1: Adjust the header paths in libtasn1.h

Since libtasn1.h is the header to be included by users, including the
standard POSIX headers in libtasn1.h would force the user to add the
CFLAGS/CPPFLAGS for the POSIX headers.

This commit adjusts the header paths to use the grub headers instead of
the standard POSIX headers, so that users only need to include
libtasn1.h to use libtasn1 functions.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Replace strcat() with _asn1_str_cat()
Gary Lin [Fri, 15 Nov 2024 07:34:33 +0000 (15:34 +0800)] 
libtasn1: Replace strcat() with _asn1_str_cat()

strcat() is not available in GRUB. This commit replaces strcat() and
_asn1_strcat() with the bounds-checking _asn1_str_cat().

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Replace strcat() with strcpy() in _asn1_str_cat()
Gary Lin [Fri, 15 Nov 2024 07:34:32 +0000 (15:34 +0800)] 
libtasn1: Replace strcat() with strcpy() in _asn1_str_cat()

strcat() is not available in GRUB. This commit replaces strcat() with
strcpy() in _asn1_str_cat() as the preparation to replace other strcat()
with the bounds-checking _asn1_str_cat().

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Disable code not needed in GRUB
Gary Lin [Fri, 15 Nov 2024 07:34:31 +0000 (15:34 +0800)] 
libtasn1: Disable code not needed in GRUB

We don't expect to be able to write ASN.1, only read it,
so we can disable some code.

Do that with #if 0/#endif, rather than deletion. This means
that the difference between upstream and GRUB is smaller,
which should make updating libtasn1 easier in the future.

With these exclusions we also avoid the need for minmax.h,
which is convenient because it means we don't have to
import it from gnulib.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agolibtasn1: Import libtasn1-4.19.0
Daniel Axtens [Fri, 15 Nov 2024 07:34:30 +0000 (15:34 +0800)] 
libtasn1: Import libtasn1-4.19.0

Import a very trimmed-down set of libtasn1 files:

  curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
  tar xvzf libtasn1-4.19.0.tar.gz
  rm -rf grub-core/lib/libtasn1
  mkdir -p grub-core/lib/libtasn1/lib
  mkdir -p grub-core/lib/libtasn1/tests
  cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1
  cp libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} grub-core/libtasn1/lib
  cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1
  cp libtasn1-4.19.0/tests/{CVE-2018-1000654-1_asn1_tab.h,CVE-2018-1000654-2_asn1_tab.h,CVE-2018-1000654.c,object-id-decoding.c,object-id-encoding.c,octet-string.c,reproducers.c,Test_overflow.c,Test_simple.c,Test_strings.c} grub-core/lib/libtasn1/tests
  rm -rf libtasn1-4.19.0*

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agoposix_wrap: Tweaks in preparation for libtasn1
Daniel Axtens [Fri, 15 Nov 2024 07:34:29 +0000 (15:34 +0800)] 
posix_wrap: Tweaks in preparation for libtasn1

Cc: Vladimir Serbinenko <phcoder@gmail.com>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
8 months agokern/fs: Honour file->read_hook() in grub_fs_blocklist_read()
Rasmus Villemoes [Thu, 29 Aug 2024 11:01:02 +0000 (13:01 +0200)] 
kern/fs: Honour file->read_hook() in grub_fs_blocklist_read()

Unlike files accessed via a normal file system, the file->read_hook() is
not honoured when using blocklist notation.

This means that when trying to use a dedicated, 1 KiB, raw partition
for the environment block and hence does something like

  save_env --file=(hd0,gpt9)0+2 X Y Z

this fails with "sparse file not allowed", which is rather unexpected,
as I've explicitly said exactly which blocks should be used. Adding
a little debugging reveals that grub_file_size(file) is 1024 as expected,
but total_length is 0, simply because the callback was never invoked, so
blocklists is an empty list.

Fix that by honouring the ->read_hook() set by the caller, also when
a "file" is specified with blocklist notation.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
8 months agodocs: Fix incorrect and potentially confusing language and minor formatting
Glenn Washburn [Fri, 6 Sep 2024 01:37:11 +0000 (20:37 -0500)] 
docs: Fix incorrect and potentially confusing language and minor formatting

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agodocs: Correct GRUB config file name for network boot
Andrew Hamilton [Thu, 31 Oct 2024 00:24:54 +0000 (19:24 -0500)] 
docs: Correct GRUB config file name for network boot

Correct the documentation for the grub.cfg searching via network that
will be done based on ethernet type, -01, which was missing, and a given
MAC address.

Fixes: https://savannah.gnu.org/bugs/?65152
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agodocs: Correct chainloader UEFI secure boot info
Andrew Hamilton [Thu, 31 Oct 2024 00:24:53 +0000 (19:24 -0500)] 
docs: Correct chainloader UEFI secure boot info

Correct documentation for UEFI secure boot to remove statement that
chainloader does not work with secure boot. This was fixed by the commit
6d05264 (kern/efi/sb: Add chainloaded image as shim's verifiable object).

Fixes: https://savannah.gnu.org/bugs/?62004
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agodocs: Correct PXE environment variables descriptions
Andrew Hamilton [Thu, 31 Oct 2024 00:24:52 +0000 (19:24 -0500)] 
docs: Correct PXE environment variables descriptions

Correct documentation for pxe_default_server, pxe_default_gatway and
pxe_blksize. Only pxe_default_server is actually used (alias for
net_default_server). So, capture this and remove the other two.

Fixes: https://savannah.gnu.org/bugs/?54480
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agoloader/multiboot: Do not add modules before successful download
Valentin Gehrke [Wed, 30 Oct 2024 17:12:56 +0000 (18:12 +0100)] 
loader/multiboot: Do not add modules before successful download

Multiboot modules that could not be read successfully, e.g. via network,
should not be added to the list of modules to forward to the operating
system that is to be booted subsequently.

This patch is necessary because even if a grub.cfg checks whether or not
a module was successfully downloaded, it is futile to retry a failed
download as the corrupted module will be forwarded either way.

Signed-off-by: Valentin Gehrke <valentin.gehrke@kernkonzept.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agogrub-mkimage: Add SBAT metadata into ELF note for PowerPC targets
Sudhakar Kuppusamy [Wed, 23 Oct 2024 12:24:33 +0000 (17:54 +0530)] 
grub-mkimage: Add SBAT metadata into ELF note for PowerPC targets

The SBAT metadata is read from CSV file and transformed into an ELF note
with the -s option.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agogrub-mkimage: Create new ELF note for SBAT
Sudhakar Kuppusamy [Wed, 23 Oct 2024 12:24:32 +0000 (17:54 +0530)] 
grub-mkimage: Create new ELF note for SBAT

In order to store the SBAT data we create a new ELF note. The string
".sbat", zero-padded to 4 byte alignment, shall be entered in the name
field. The string "SBAT"'s ASCII values, 0x53424154, should be entered
in the type field.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agocommands/legacycfg: Avoid closing file twice
Leo Sandoval [Wed, 16 Oct 2024 17:54:38 +0000 (11:54 -0600)] 
commands/legacycfg: Avoid closing file twice

An internal (at Red Hat) static soure code scan detected an
use-after-free scenario:

  Error: USE_AFTER_FREE (CWE-416):
  grub-2.06/grub-core/commands/legacycfg.c:194: freed_arg: "grub_file_close" frees "file".
  grub-2.06/grub-core/commands/legacycfg.c:201: deref_arg: Calling "grub_file_close" dereferences freed pointer "file".
  #  199|         if (!args)
  #  200|    {
  #  201|->    grub_file_close (file);
  #  202|      grub_free (suffix);
  #  203|      grub_free (entrysrc);

So, remove the extra file close call.

Signed-off-by: Leo Sandoval <lsandova@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9 months agonx: Rename GRUB_DL_ALIGN to DL_ALIGN
Daniel Kiper [Wed, 16 Oct 2024 13:04:17 +0000 (15:04 +0200)] 
nx: Rename GRUB_DL_ALIGN to DL_ALIGN

Rename has been skipped by mistake in the original commit.

Fixes: 94649c026 (nx: Set page permissions for loaded modules)
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Sudeep Holla <sudeep.holla@arm.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
9 months agokern/acpi: Fix out of bounds access in grub_acpi_xsdt_find_table()
Benjamin Herrenschmidt [Wed, 16 Oct 2024 05:20:24 +0000 (16:20 +1100)] 
kern/acpi: Fix out of bounds access in grub_acpi_xsdt_find_table()

The calculation of the size of the table was incorrect (copy/pasta from
grub_acpi_rsdt_find_table() I assume...). The entries are 64-bit long.

This causes us to access beyond the end of the table which is causing
crashes during boot on some systems. Typically this is causing a crash
on VMWare when using UEFI and enabling serial autodetection, as

  grub_acpi_find_table (GRUB_ACPI_SPCR_SIGNATURE);

will goes past the end of the table (the SPCR table doesn't exits).

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Tested-by: Renata Ravanelli <rravanel@redhat.com>
10 months agonx: Set the NX compatible flag for the GRUB EFI images
Mate Kukri [Wed, 9 Oct 2024 08:16:42 +0000 (09:16 +0100)] 
nx: Set the NX compatible flag for the GRUB EFI images

For NX the GRUB binary has to announce that it is compatible with the
NX feature. This implies that when loading the executable GRUB image
several attributes are true:
  - the binary doesn't need an executable stack,
  - the binary doesn't need sections to be both executable and writable,
  - the binary knows how to use the EFI Memory Attributes Protocol on code
    it is loading.

This patch:
  - adds a definition for the PE DLL Characteristics flag GRUB_PE32_NX_COMPAT,
  - changes grub-mkimage to set that flag.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agonx: Set page permissions for loaded modules
Mate Kukri [Wed, 9 Oct 2024 08:16:41 +0000 (09:16 +0100)] 
nx: Set page permissions for loaded modules

For NX we need to set write and executable permissions on the sections
of GRUB modules when we load them. All allocatable sections are marked
readable. In addition:
  - SHF_WRITE sections are marked as writable,
  - and SHF_EXECINSTR sections are marked as executable.

Where relevant for the platform the tramp and GOT areas are marked non-writable.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agonx: Add memory attribute get/set API
Mate Kukri [Wed, 9 Oct 2024 08:16:40 +0000 (09:16 +0100)] 
nx: Add memory attribute get/set API

For NX we need to set the page access permission attributes for write
and execute permissions. This patch adds two new primitives, grub_set_mem_attrs()
and grub_clear_mem_attrs(), and associated constants definitions used
for that purpose. For most platforms it adds a dummy implementation.
On EFI platforms it implements the primitives using the EFI Memory
Attribute Protocol, defined in UEFI 2.10 specification.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agomodules: Load module sections at page-aligned addresses
Mate Kukri [Wed, 9 Oct 2024 08:16:39 +0000 (09:16 +0100)] 
modules: Load module sections at page-aligned addresses

Currently we load module sections at whatever alignment gcc+ld happened
to dump into the ELF section header which is often less then the page
size. Since NX protections are page based this alignment must be rounded
up to page size on platforms supporting NX protections. This patch
switches EFI platforms to load module sections at 4 KiB page-aligned
addresses. It then changes the allocation size computation and the
loader code in grub_dl_load_segments() to align the locations and sizes
up to these boundaries and fills any added padding with zeros. All of
this happens before relocations are applied, so the relocations factor
that in with no change.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agomodules: Don't allocate space for non-allocable sections
Peter Jones [Wed, 9 Oct 2024 08:16:38 +0000 (09:16 +0100)] 
modules: Don't allocate space for non-allocable sections

Currently when loading GRUB modules we allocate space for all sections
including those without SHF_ALLOC set. We then copy the sections that
/do/ have SHF_ALLOC set into the allocated memory leaving some of our
allocation untouched forever. Additionally, on platforms with GOT fixups
and trampolines we currently compute alignment round-ups for the
sections and sections with sh_size = 0. This patch removes the extra
space from the allocation computation and makes the allocation
computation loop skip empty sections as the loading loop does.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-By: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agomodules: Strip .llvm_addrsig sections and similar
Peter Jones [Wed, 9 Oct 2024 08:16:37 +0000 (09:16 +0100)] 
modules: Strip .llvm_addrsig sections and similar

Currently GRUB modules built with Clang or GCC have several sections
which we don't actually need or support. We already have a list of
sections to skip in genmod.sh and this patch adds the following
sections to that list (as well as a few newlines):
  - .note.gnu.property
  - .llvm*

Note that the glob there won't work without a new enough linker but the
failure is just reversion to the status quo. So, that's not a big problem.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-By: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agomodules: Make .module_license read-only
Peter Jones [Wed, 9 Oct 2024 08:16:36 +0000 (09:16 +0100)] 
modules: Make .module_license read-only

Currently .module_license is set writable, that is, the section has the
SHF_WRITE flag set, in the module's ELF headers. This probably never
actually matters but it can't possibly be correct. The patch sets that
data as "const" which causes that flag not to be set.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-By: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoi386/memory: Rename PAGE_SIZE to GRUB_PAGE_SIZE and make it global
Daniel Kiper [Sun, 6 Oct 2024 14:14:46 +0000 (17:14 +0300)] 
i386/memory: Rename PAGE_SIZE to GRUB_PAGE_SIZE and make it global

This is an x86-specific thing and should be available globally.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoi386/memory: Rename PAGE_SHIFT to GRUB_PAGE_SHIFT
Daniel Kiper [Sun, 6 Oct 2024 14:14:45 +0000 (17:14 +0300)] 
i386/memory: Rename PAGE_SHIFT to GRUB_PAGE_SHIFT

This fixes naming inconsistency that goes against coding style as well
as helps to avoid potential conflicts and confusion as this constant is
used in multiple places.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoi386/msr: Extract and improve MSR support detection code
Daniel Kiper [Sun, 6 Oct 2024 14:14:44 +0000 (17:14 +0300)] 
i386/msr: Extract and improve MSR support detection code

Currently rdmsr and wrmsr commands have own MSR support detection code.
This code is the same. So, it is duplicated. Additionally, this code
cannot be reused by others. Hence, extract this code to a function and
make it public. By the way, improve a code a bit.

Additionally, use GRUB_ERR_BAD_DEVICE instead of GRUB_ERR_BUG to signal
an error because errors encountered by this new routine are not bugs.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoi386/msr: Rename grub_msr_read() and grub_msr_write()
Daniel Kiper [Sun, 6 Oct 2024 14:14:43 +0000 (17:14 +0300)] 
i386/msr: Rename grub_msr_read() and grub_msr_write()

Use more obvious names which match corresponding instructions:
  * grub_msr_read()  => grub_rdmsr(),
  * grub_msr_write() => grub_wrmsr().

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoi386/msr: Merge rdmsr.h and wrmsr.h into msr.h
Daniel Kiper [Sun, 6 Oct 2024 14:14:42 +0000 (17:14 +0300)] 
i386/msr: Merge rdmsr.h and wrmsr.h into msr.h

It does not make sense to have separate headers for individual static
functions. So, make one common place to store them.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agocommands/tpm: Skip loopback image measurement
Michael Chang [Thu, 3 Oct 2024 07:23:22 +0000 (15:23 +0800)] 
commands/tpm: Skip loopback image measurement

The loopback image is configured to function as a disk by being mapped
as a block device. Instead of measuring the entire block device we
should focus on tracking the individual files accessed from it. For
example, we do not directly measure block devices like hd0 disk but the
files opened from it.

This method is important to avoid running out of memory since loopback
images can be very large. Trying to read and measure the whole image at
once could cause out of memory errors and disrupt the boot process.

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agonet/drivers/efi/efinet: Skip virtual VLAN devices during card enumeration
Michael Chang [Thu, 3 Oct 2024 07:23:15 +0000 (15:23 +0800)] 
net/drivers/efi/efinet: Skip virtual VLAN devices during card enumeration

Similarly to the issue described in commit c52ae4057 (efinet: skip
virtual IPv4 and IPv6 devices during card enumeration) the UEFI PXE
driver creates additional VLAN child devices when a VLAN ID is
configured on a network interface associated with a physical NIC. These
virtual VLAN devices must be skipped during card enumeration to ensure
that the subsequent SNP exclusive open operation targets the correct
physical card instances. Otherwise packet transfer would fail.

A device path example with VLAN nodes:

  /MAC(123456789ABC,0x1)/Vlan(20)/IPv4(0.0.0.0,0x0,DHCP,0.0.0.0,0.0.0.0,0.0.0.0)

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agoefi/console: Properly clear leftover artifacts from the screen
Michael Chang [Thu, 3 Oct 2024 07:23:08 +0000 (15:23 +0800)] 
efi/console: Properly clear leftover artifacts from the screen

A regression in GRUB 2.12 causes the GRUB screen to become cluttered
with artifacts from the previous screen whether it's the UEFI post UI,
UEFI shell or any graphical UI running before GRUB. This issue occurs
in situations like booting GRUB from the UEFI shell and going straight
to the rescue or command shell causing visual discomfort.

The regression was introduced by commit 2d7c3abd8 (efi/console: Do not
set text-mode until it is actually needed). To address the screen
flickering issue this commit suppresses the text-mode setting until the
first output is requested. Before text-mode is set any attempt to clear
the screen has no effect. This inactive period renders the clear screen
ineffective in early boot stages, potentially leaving leftover artifacts
that will clutter the GRUB console display, as there is no guarantee
there will always be a clear screen after the first output.

The issue is fixed by ensuring grub_console_cls() to work through lazy
mode-setting, while also avoiding screen clearing for the hidden menu
which the flicker-free patch aims to improve.

Fixes: 2d7c3abd8 (efi/console: Do not set text-mode until we actually need it)
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
10 months agokern/riscv/efi/init: Use time register in grub_efi_get_time_ms()
Heinrich Schuchardt [Mon, 12 Aug 2024 14:13:18 +0000 (16:13 +0200)] 
kern/riscv/efi/init: Use time register in grub_efi_get_time_ms()

The cycle register is not guaranteed to count at constant frequency.
If it is counting at all depends on the state the performance monitoring
unit. Use the time register to measure time.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoloader/efi/linux: Reset freed pointer
Frediano Ziglio [Tue, 3 Sep 2024 15:15:47 +0000 (16:15 +0100)] 
loader/efi/linux: Reset freed pointer

Avoid dangling pointer. Code should not be reached but better safe than sorry.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoloader/efi/linux: Reuse len variable
Frediano Ziglio [Tue, 3 Sep 2024 15:15:46 +0000 (16:15 +0100)] 
loader/efi/linux: Reuse len variable

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agolib/x86_64/relocator_asm: Use .quad instead of .long
Frediano Ziglio [Tue, 3 Sep 2024 08:00:30 +0000 (09:00 +0100)] 
lib/x86_64/relocator_asm: Use .quad instead of .long

They are single 64-bit values. Used in other assembly files too.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agolib/x86_64/relocator_asm: Fix comment in code
Frediano Ziglio [Tue, 3 Sep 2024 08:00:29 +0000 (09:00 +0100)] 
lib/x86_64/relocator_asm: Fix comment in code

The instruction uses a 64-bit immediate.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoloader/efi/linux: Update comment
Frediano Ziglio [Tue, 3 Sep 2024 08:00:27 +0000 (09:00 +0100)] 
loader/efi/linux: Update comment

The function called is grub_utf8_to_utf16().

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoutil/grub-mkimagexx: Explicitly move modules to __bss_start for MIPS targets
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:52 +0000 (20:58 +0300)] 
util/grub-mkimagexx: Explicitly move modules to __bss_start for MIPS targets

Assembly code looks for modules at __bss_start. Make this position explicit
rather than matching BSS alignment and module alignment.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoinclude/grub/offsets.h: Set mod_align to 4 on MIPS
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:51 +0000 (20:58 +0300)] 
include/grub/offsets.h: Set mod_align to 4 on MIPS

Module structure has natural alignment of 4. Respect it explicitly
rather than relying on the fact that _end is usually aligned.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agogentpl: Put boot/mips/startup_raw.S into beginning of the image
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:50 +0000 (20:58 +0300)] 
gentpl: Put boot/mips/startup_raw.S into beginning of the image

Otherwise it breaks the decompressors for MIPS targets.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoconfigure: Add -mno-gpopt option for mips and mipsel targets
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:49 +0000 (20:58 +0300)] 
configure: Add -mno-gpopt option for mips and mipsel targets

Without it compiler generates GPREL16 references which do not work
with our memory layout.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agolib/xzembed/xz_dec_bcj: Silence warning when no BCJ is available
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:48 +0000 (20:58 +0300)] 
lib/xzembed/xz_dec_bcj: Silence warning when no BCJ is available

BCJ is not available for all platforms hence arguments may end up unused.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agofs/erofs: Replace 64-bit modulo with bitwise operations
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:47 +0000 (20:58 +0300)] 
fs/erofs: Replace 64-bit modulo with bitwise operations

Otherwise depending on compiler we end up with umoddi3 reference and
failed module dependency resolution.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoconfigure: Look for .otf fonts
Vladimir Serbinenko [Tue, 3 Sep 2024 17:58:46 +0000 (20:58 +0300)] 
configure: Look for .otf fonts

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agoloader/efi/chainloader: Do not print device path of chainloaded file
Mate Kukri [Thu, 15 Aug 2024 09:52:56 +0000 (10:52 +0100)] 
loader/efi/chainloader: Do not print device path of chainloaded file

Users have no reason to see this and it can break graphical boot.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
11 months agodocs: Document all GRUB modules
Andrew Hamilton [Sun, 4 Aug 2024 16:32:51 +0000 (11:32 -0500)] 
docs: Document all GRUB modules

Add documentation for all GRUB modules contained in the source code tree.
When possible, cross-references to additional detail on commands was added
from their corresponding module documentation. In addition, documentation
for the file command was added.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>