Compare commits

...

436 commits

Author SHA1 Message Date
Lidong Chen
a68a7dece4 loader/i386/pc/linux: Fix resource leak
In grub_cmd_initrd(), memory is allocated for variable initrd_ctx
before calling grub_relocator_alloc_chunk_align_safe(). When the
function call fails, initrd_ctx should be freed before exiting
grub_cmd_initrd().

Fixes: CID 473852

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 20:02:05 +02:00
Adriano Cordova
de80acf368 loader/efi/linux: Unload previous Linux kernel/initrd before updating kernel size
Unload previous Linux kernel/initrd before updating the global variable
kernel_size. Otherwise the previous Linux kernel gets deallocated with
the kernel_size of the Linux kernel that is being currently loaded.

Signed-off-by: Adriano Cordova <adriano.cordova@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
249db11d8f loader/efi/linux: Correctly terminate load_options member
If a simple string for arguments are passed it should be NUL terminated.
This is true for other code but not for "linux" command.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
f3b339af11 loader/efi/linux: Use sizeof() instead of constant
This is more consistent with the above code using sizeof(grub_efi_char16_t).

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
c2b2e0dcf5 loader/efi/linux: Use proper type for len variable
Although the length should not exceed 2^31 grub_size_t is more
suitable for that variable. len is used to compute the size
of buffers which in C is a size_t, not a int. It is used
for GRUB_EFI_BYTES_TO_PAGES which expects unsigned values.
It is assigned to load_options_size which is unsigned, not signed.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
de4e8e2aa6 loader/efi/linux: Do not pass excessive size for source string
The size passed to grub_utf8_to_utf16() for the source string is
used as a limit for the string if NUL character is not encountered.
However, len, which is "strlen(src) * 2 + 2" is surely greater than
strlen(src). Pass the exact correct length.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
8c8f966643 loader/efi/linux: Remove useless assignment
If the following allocation fails this would leave load_options NULL
while load_options_size not valid. If the allocation succeed
load_options_size is overwritten.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Frediano Ziglio
8ebf155af3 include/grub/charset.h: Update documentation
(grub_size_t) -1 is never returned, the function always return
a not negative values. This is important for overflows considerations.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Lidong Chen
2f2ed28d5a Revert "lzma: Make sure we don't dereference past array"
Commit 40e261b89b (lib/LzmaEnc: Validate "len" before subtracting)
ensures that the variable len is at least 2. As a result, GetLenToPosState(len)
never returns a value greater than or equal to kNumLenToPosStates,
making the changes introduced in the commit 16c0dbf4bc (lzma: Make
sure we don't dereference past array) unreachable and no longer necessary.

This reverts commit 16c0dbf4bc (lzma: Make sure we don't dereference past array).

Fixes: CID 481982

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 18:05:08 +02:00
Andrew Hamilton
2539ede82a tests/util/grub-shell: Correct netboot and file_filter test failure
Correct a test failure in netboot_test and file_filter_test caused by an
issue cleaning up the tmp directory created for netboot. Netboot creates
a subdirectory in the tmp folder that causes the rmdir to fail - so
cleanup the subdirectory first.

Fixes: 1d59f39b5f (tests/util/grub-shell: Remove the work directory on successful run and debug is not on)

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Tested-by: Leo Sandoval <lsandova@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 17:24:39 +02:00
Lidong Chen
8c2d4e64ff normal/charset: Fix underflow and overflow in loop init
In bidi_line_wrap(), "kk - 1" in the for loop init, "i = kk - 1",
underflows when "kk" (unsigned int) is 0. Assigning the result of
"kk - 1" to signed int "i" may cause overflow. To address both
issues, cast "kk" to a signed type before subtraction to ensure
safe arithmetic and assignment.

Fixed: CID 473874

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
2025-06-26 17:19:25 +02:00
Daniel Axtens
ba8eadde6b dl: Provide a fake grub_dl_set_persistent() and grub_dl_is_persistent() for the emu target
Trying to start grub-emu with a module that calls grub_dl_set_persistent()
and grub_dl_is_persistent() will crash because grub-emu fakes modules and
passes NULL to the module init function.

Provide an empty function for the emu case.

Fixes: ee7808e219 (dl: Add support for persistent modules)

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Avnish Chouhan <avnish@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 17:19:25 +02:00
Andrew Hamilton
409e72cedf util/grub-protect: Correct uninit "err" variable
In function protect_tpm2_export_tpm2key(), the "err" variable
is uninitialized in the normal (error free) path, so ensure this
defaults to GRUB_ERR_NONE.

This causes the GRUB build to fail with clang (observed with clang-14).

Fixes: 5934bf51c (util/grub-protect: Support NV index mode)

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 17:19:25 +02:00
Lidong Chen
5eca564b19 gnulib: Bring back the fix for resolving unused variable issue
This patch resolved a minor issue spotted by Coverity:
  a983d36bd9 (gnulib/regexec: Resolve unused variable)

But, it was removed by the Gnulib update:
  2b79024598 (Update gnulib version and drop most gnulib patches)

It caused Coverity to continue to flag the issue. Daniel Kiper
suggested to bring back the patch a983d36bd9 (gnulib/regexec: Resolve
unused variable).

Fixes: CID 292459

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 17:19:25 +02:00
Andrew Hamilton
ac1512b872 gnulib: Add patch to allow GRUB w/GCC-15 compile
Pull in Gnulib fix to allow lib/base64.c to compile using GCC 15 or newer.

Pulled from Gnulib commit 25df6dc425 (Silence some
-Wunterminated-string-initialization warnings.)

GCC 15 adds a new compiler warning "-Wunterminated-string-initialization"
that will trigger what is considered a false-positive in lib/base64.c as
this array is not treated as a string but an array of characters so the
lack of NUL string terminator is expected.

GCC team has added ability to flag such instances of arrays that the
compiler may think are strings as "nonstring" arrays to avoid this
warning: __attribute__((nonstring)).

Fixes: https://savannah.gnu.org/bugs/?66470

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-26 17:02:21 +02:00
Alec Brown
db506b3b83 gnulib/regexec: Fix resource leak
In the function merge_state_with_log(), memory is allocated for the variable
next_nodes when creating a union of the variables table_nodes and log_nodes.
However, if next_state->entrance_nodes is NULL, then table_nodes becomes NULL
and we still allocate memory to copy the content of log_nodes. This can cause
a resource leak since we only free the memory for next_nodes if table_nodes
isn't NULL. To prevent this, we need to check that next_state->entrance_nodes
isn't NULL before allocating memory for the union.

This issue has been fixed in the latest version of gnulib and I've backported
this change to maintain consistency.

This issue was found by a Coverity scan of GRUB2 under the CID 473887.

Fixes: CID 473887

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 14:29:29 +02:00
Alec Brown
bba7dd7363 gnulib/regcomp: Fix resource leak
In the functions create_initial_state() and calc_eclosure_iter(), memory
is allocated for the elems member of a re_node_set structure but that
memory isn't freed on error. Before returning an error, a call to
re_node_set_free() should be made to prevent the resource leak.

This issue has been fixed in the latest version of gnulib and I've
backported this change to maintain consistency.

This issue was found by a Coverity scan of GRUB2 under the following
CIDs: 473869, 473888.

Fixes: CID 473869
Fixes: CID 473888

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 14:17:56 +02:00
Gary Lin
91cb7ff6bb tests/tpm2_key_protector_test: Add tests for SHA-384 PCR bank
Add a few more tests to seal and unseal the key with the SHA-384 PCR
bank instead of the default SHA-256 PCR bank.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 14:13:42 +02:00
Gary Lin
451e227e53 tpm2_key_protector: Dump the PCR bank for key unsealing
TPM 2.0 Key File format stores the PCR selection in the parameters
for TPM2_PolicyPCR and it already contains the selected PCR bank.
Currently, tpm2_key_protector dumped the PCR bank specified by the
--bank option, and it may not be the PCR bank for key unsealing.

To dump the real PCR bank for key unsealing, this commit records the PCR
bank used by TPM2_PolicyPCR and dumps PCR values from that bank when
necessary.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 14:12:50 +02:00
Gary Lin
11caacdb22 util/grub-protect: Fix the hash algorithm of PCR digest
For tpm2_key_protector and grub-protect, SHA-256 is chosen as the hash
algorithm for the TPM session. However, grub-protect mistakenly used the
hash algorithm of the PCR bank to calculate PCR digest. If the user
chose a PCR bank other than SHA-256, grub-protect created a non-SHA-256
PCR digest to seal the key. But, tpm2_key_protector expects a SHA-256
PCR digest to the TPM unsealing session, so it would fail due to digest
mismatch.

This commit fixes the hash algorithm of PCR digest in grub-protect to
avoid the potential unsealing failure.

Fixes: https://github.com/lcp/grub2/issues/4

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 14:10:37 +02:00
Andrew Hamilton
ce23919cac build: Add new header files to dist to allow building from tar
Several new header files have been added to GRUB which need
to be manually added to the dist archive. This allows building
from the tar archive created by "make dist".

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 13:51:46 +02:00
Andrew Hamilton
e3b15bafd1 build: Remove extra_deps.lst from EXTRA_DIST
This file is auto-generated based on the selected platform and should
not be included in the source tarball.

Fixes: 6744840b (build: Track explicit module dependencies in Makefile.core.def)

Signed-off-by: Mike Gilbert <floppym@gentoo.org>
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-17 13:47:21 +02:00
Lidong Chen
40e261b89b lib/LzmaEnc: Validate "len" before subtracting
In LzmaEnc_CodeOneBlock(), both GetOptimumFast() and GetOptimum()
returns a value of greater or equal to 1, which is assigned to
"len". But since LZMA_MATCH_LEN_MIN == 2, "len" should be validated
before performing "len - LZMA_MATCH_LEN_MIN" to avoid underflow
when "len" equals to 1.

Fixes: CID 51508

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
2025-06-17 13:37:31 +02:00
Lidong Chen
86e8f2c4b0 osdep/unix/hostdisk: Fix signed integer overflow
The potential overflow issue arises at "size += ret;" because "size"
is of type ssize_t (signed) while "len" is size_t (unsigned). Repeatedly
adding read sizes, "ret", to "size" can potentially exceed the maximum
value of ssize_t, causing it to overflow into a negative or incorrect value.
The fix is to ensure "len" is within the range of SSIZE_MAX.

Fixes: CID 473850
Fixes: CID 473863

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-06-12 18:56:24 +02:00
Egor Ignatov
438f055819 disk/luks2: Add attempting to decrypt message to align with luks and geli modules
Signed-off-by: Egor Ignatov <egori@altlinux.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Renaud Métrich
20e6d0c4a4 osdep/linux/getroot: Detect DDF container similar to IMSM
Similarly to Intel IMSM, there are BIOS and UEFI implementations that
support DDF containers natively.

DDF and IMSM are very similar in handling, especially these should not
be considered as RAID abstraction. This fixes the requirement of having
a device map when probing DDF containers.

Fixes: https://issues.redhat.com/browse/RHEL-44336

Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Andrew Hamilton
b71bc0f8b4 fs/fshelp: Avoid possible NULL pointer deference
Avoid attempting to defererence a NULL pointer to call read_symlink() when
the given filesystem does not provide a read_symlink() function. This could
be triggered if the calling filesystem had a file marked as a symlink.
This appears possible for HFS and was observed during fuzzing of NTFS.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Andrew Hamilton
272ff81cb2 fs/ntfs: Correct possible infinite loops/hangs
Correct several infinite loops/hangs found during fuzzing. The issues
fixed here could occur if certain specific malformed NTFS file systems
were presented to GRUB. Currently, GRUB does not allow NTFS file system
access when lockdown mode is enforced, so these should be of minimal
impact.

The changes made in this commit generally correct issues such as attempting
to iterate through a buffer using a length read from the NTFS file system
without confirming the length is larger than 0.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Andrew Hamilton
8c95307a0b fs/ntfs: Correct possible access violations
Correct several memory access violations found during fuzzing.
The issues fixed here could occur if certain specific malformed NTFS
file systems were presented to GRUB. Currently, GRUB does not allow NTFS
file system access when lockdown mode is enforced, so these should be of
minimal impact.

The changes made in this commit generally correct issues where pointers
into data buffers were being calculated using lengths read from the
NTFS file system without sufficient bounds/sanity checking; or
attempting to access elements of a structure to free them, when the
structure pointer is NULL.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Andrew Hamilton
06914b6141 fs/ntfs: Correct attribute vs attribute list validation
Correct ntfs_test test failures around attempting to validate attribute
list entries as attributes. The NTFS code uses common logic in some
places to parse both attributes and attribute_lists which complicates
validation. Attribute lists contain different headers including a
different size of the length field (2 bytes) at offset 4 instead of the
4 byte length field used in attributes at offset 4. There are other
differences as well, but attempting to validate attribute list types
using attribute header validation was causing failure of the NTFS test
suite. This change restores some of the validation logic which may be
shared between attributes and attribute lists to be closer to the
original logic prior to fixes for previous CVEs. A following commit will
address some of the implications of removing this validation logic by
correcting some fuzzer failures (some which are exposed by removing the
validation in some of the cases).

Fixes: 067b6d225 (fs/ntfs: Implement attribute verification)

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Andrew Hamilton
0e1762c8a0 fs/ntfs: Correct regression with run list calculation
Correct ntfs_test test failures around attempting to validate attribute
run list values. The calculation was incorrect for the "curr" variable.
With previous calculation, some file systems would fail validation
despite being well-formed and valid. This was caused by incrementing
"curr" by min_size which included both the (already accounted for)
min_size as well as the size of the run list. Correct by making a new
variable "run_size" to denote the current run list size to increment
both "curr" and "min_size" separately.

Fixes: 067b6d225 (fs/ntfs: Implement attribute verification)

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:24 +02:00
Shreenidhi Shedi
be303f8c18 lib/envblk: Ignore empty new lines while parsing env files
Environment files may contain empty lines, which should be ignored
during parsing. Currently, these lines are not skipped and resulting in
incorrect behavior. This patch adds a check to skip empty lines along
with those starting with "#".

Signed-off-by: Shreenidhi Shedi <shreenidhi.shedi@broadcom.com>
Reviewed-by: Alexey Makhalov <alexey.makhalov@broadcom.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 15:45:09 +02:00
Glenn Washburn
34bd00ee26 fs/zfs: Fix another memory leak in ZFS code
Commit b66c6f918 (fs/zfs: Fix a number of memory leaks in ZFS code)
fixes many of the same leaks detected in bug #63846 except one, which
is fixed here.

Fixes: https://savannah.gnu.org/bugs/?63846
Fixes: b66c6f918 (fs/zfs: Fix a number of memory leaks in ZFS code)

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 14:53:09 +02:00
Glenn Washburn
ca2a91f43b tests: Disable gfxterm_menu and cmdline_cat tests
Those tests fail depending on the version of unifont. As we don't distribute
our own unifont it fails for most users. Disable them so that they don't mask
real failures. They can be reinstated once we solve unifont problem.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-29 14:48:31 +02:00
Michael Chang
73d1c959ea cryptocheck: Add --quiet option
The option can be used to suppress output if we only want to test the
return value of the command.

Also, mention this option in the documentation.

Signed-off-by: Michael Chang <mchang@suse.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:04 +02:00
Maxim Suhanov
dbc0eb5bd1 disk/cryptodisk: Wipe the passphrase from memory
Switching to another EFI boot application while there are secrets in
RAM is dangerous, because not all firmware is wiping memory on free.

To reduce the attack surface, wipe the passphrase acquired when
unlocking an encrypted volume.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:03 +02:00
Maxim Suhanov
301b4ef25a disk/cryptodisk: Add the "erase secrets" function
This commit adds the grub_cryptodisk_erasesecrets() function to wipe
master keys from all cryptodisks. This function is EFI-only.

Since there is no easy way to "force unmount" a given encrypted disk,
this function renders all mounted cryptodisks unusable. An attempt to
read them will return garbage.

This is why this function must be used in "no way back" conditions.

Currently, it is used when unloading the cryptodisk module and when
performing the "exit" command (it is often used to switch to the next
EFI application). This function is not called when performing the
"chainloader" command, because the callee may return to GRUB. For this
reason, users are encouraged to use "exit" instead of "chainloader" to
execute third-party boot applications.

This function does not guarantee that all secrets are wiped from RAM.
Console output, chunks from disk read requests and other may remain.

This function does not clear the IV prefix and rekey key for geli disks.

Also, this commit adds the relevant documentation improvements.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:03 +02:00
Maxim Suhanov
23ec4535f4 docs: Document available crypto disks checks
Document the --cryptodisk-only argument. Also, document the
"cryptocheck" command invoked when that argument is processed.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:03 +02:00
Maxim Suhanov
10d778c4b4 commands/search: Add the diskfilter support
When the --cryptodisk-only argument is given, also check the target
device using the "cryptocheck" command, if available.

This extends the checks to common layouts like LVM-on-LUKS, so the
--cryptodisk-only argument transparently handles such setups.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:03 +02:00
Maxim Suhanov
7a584fbde0 disk/diskfilter: Introduce the "cryptocheck" command
This command examines a given diskfilter device, e.g., an LVM disk,
and checks if underlying disks, physical volumes, are cryptodisks,
e.g., LUKS disks, this layout is called "LVM-on-LUKS".

The return value is 0 when all underlying disks (of a given device)
are cryptodisks (1 if at least one disk is unencrypted or in an
unknown state).

Users are encouraged to include the relevant check before loading
anything from an LVM disk that is supposed to be encrypted.

This further supports the CLI authentication, blocking bypass
attempts when booting from an encrypted LVM disk.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:03 +02:00
Maxim Suhanov
ed691c0e0e commands/search: Introduce the --cryptodisk-only argument
This allows users to restrict the "search" command's scope to
encrypted disks only.

Typically, this command is used to "rebase" $root and $prefix
before loading additional configuration files via "source" or
"configfile". Unfortunately, this leads to security problems,
like CVE-2023-4001, when an unexpected, attacker-controlled
device is chosen by the "search" command.

The --cryptodisk-only argument allows users to ensure that the
file system picked is encrypted.

This feature supports the CLI authentication, blocking bypass
attempts.

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:14:02 +02:00
Maxim Suhanov
c448f511e7 kern/rescue_reader: Block the rescue mode until the CLI authentication
This further mitigates potential misuse of the CLI after the
root device has been successfully unlocked via TPM.

Fixes: CVE-2025-4382

Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-05-06 17:13:00 +02:00
Eric Sandeen
4abac0ad5a fs/xfs: Fix large extent counters incompat feature support
When large extent counter / NREXT64 support was added to GRUB, it missed
a couple of direct reads of nextents which need to be changed to the new
NREXT64-aware helper as well. Without this, we'll have mis-reads of some
directories with this feature enabled.

The large extent counter fix likely raced on merge with commit 07318ee7e
(fs/xfs: Fix XFS directory extent parsing) which added the new direct
nextents reads just prior, causing this issue.

Fixes: aa7c132267 (fs/xfs: Add large extent counters incompat feature support)

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anthony Iliopoulos <ailiop@suse.com>
Reviewed-by: Jon DeVree <nuxi@vault24.org>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-23 18:10:53 +02:00
Egor Ignatov
a4da71dafe util/grub-install: Include raid5rec module for RAID 4 as well
RAID 4 requires the same recovery module as RAID 5. Extend the condition to
cover both RAID levels.

Signed-off-by: Egor Ignatov <egori@altlinux.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:40:50 +02:00
Vladimir Serbinenko
223fcf8084 loader/ia64/efi/linux: Reset grub_errno on failure to allocate
The code goes on to allocate memory in another region on failure, hence
it should discard the error.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:39:27 +02:00
Vladimir Serbinenko
6504a8d4bd lib/datetime: Specify license in emu module
Other platforms specify license in platform-specific files but corresponding
code for emu is in kernel, so datetime ends up without license section.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:37:16 +02:00
Vladimir Serbinenko
8fef533cf6 configure: Add -mno-relax on riscv*
Without this option compiler sometimes emits R_RISCV_ALIGN relocs.
Unlike other relocs this one requires the linker to do NOP deletions
and we can't ignore them. Just instruct compiler not to emit them.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:34:59 +02:00
Gary Lin
1fe0948558 docs: Document the long options of tpm2_key_protect_init
Add the long options of tpm2_key_protect_init along with the short options.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:32:29 +02:00
Gary Lin
6252eb97ca INSTALL: Document the packages needed for TPM2 key protector tests
The TPM2 key protector tests require two external packages: swtpm-tools
and tpm2-tools. Add those two packages to the INSTALL file to inform
the user to install those packages before starting the TPM2 key protector
tests.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-10 19:32:12 +02:00
Gary Lin
9d4b382aa0 docs: Update NV index mode of TPM2 key protector
This commit updates the NV index mode section and the grub-protect
section to reflect the recent changes in TPM2 key protector and
grub-protect.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:29:03 +02:00
Gary Lin
2043b6899b tests/tpm2_key_protector_test: Add more NV index mode tests
Two more NV index test cases are added to test key sealing and
unsealing with the NV index handle 0x1000000.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:28:02 +02:00
Gary Lin
9f66a4719b tests/tpm2_key_protector_test: Reset "ret" on fail
Reset "ret" to 0 when a test case fails so that the other test cases
could continue.

Also set the exit status to 1 when encountering a failure to reflect the
test result.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 19:25:41 +02:00
Gary Lin
b7d89e6671 tests/tpm2_key_protector_test: Simplify the NV index mode test
Since grub-protect already supports NV index mode, tpm2_seal_nv() is
replaced with one grub-protect command to simplify the test script.

"tpm2_evictcontrol" is also replaced with "grub-protect --tpm2-evict".

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:15:31 +02:00
Gary Lin
5934bf51cb util/grub-protect: Support NV index mode
This commit implements the missing NV index mode support in grub-protect.
NV index mode stores the sealed key in the TPM non-volatile memory (NVRAM)
instead of a file. There are two supported types of TPM handles.

1. Persistent handle (0x81000000~0x81FFFFFF)
   Only the raw format is supported due to the limitation of persistent
   handles. This grub-protect command seals the key into the
   persistent handle 0x81000000.

  # grub-protect \
      --protector=tpm2 \
      --action=add \
      --tpm2-bank=sha256 \
      --tpm2-pcrs=7,11 \
      --tpm2-keyfile=luks-key \
      --tpm2-nvindex=0x81000000

2. NV index handle (0x1000000~0x1FFFFFF)
   Both TPM 2.0 Key File format and the raw format are supported by NV
   index handles. Here is the grub-protect command to seal the key in
   TPM 2.0 Key File format into the NV index handle 0x1000000.

  # grub-protect \
      --protector=tpm2 \
      --action=add \
      --tpm2key \
      --tpm2-bank=sha256 \
      --tpm2-pcrs=7,11 \
      --tpm2-keyfile=luks-key \
      --tpm2-nvindex=0x1000000

Besides the "add" action, the corresponding "remove" action is also
introduced. To remove the data from a persistent or NV index handle,
just use "--tpm2-nvindex=HANDLE" combining with "--tpm2-evict". This
sample command removes the data from the NV index handle 0x1000000.

  # grub-protect \
      --protector=tpm2 \
      --action=remove \
      --tpm2-evict \
      --tpm2-nvindex=0x1000000

Also set and check the boolean variables with true/false instead of 1/0.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:12:57 +02:00
Gary Lin
cd9cb944d9 tpm2_key_protector: Support NV index handles
Previously, NV index mode only supported persistent handles which are
only for TPM objects.

On the other hand, the "NV index" handle allows the user-defined data,
so it can be an alternative to the key file and support TPM 2.0 Key
File format immediately.

The following tpm2-tools commands store the given key file, sealed.tpm,
in either TPM 2.0 Key File format or the raw format into the NV index
handle 0x1000000.

  # tpm2_nvdefine -C o \
      -a "ownerread|ownerwrite" \
      -s $(stat -c %s sealed.tpm) \
      0x1000000
  # tpm2_nvwrite -C o -i sealed.tpm 0x1000000

To unseal the key in GRUB, add the "tpm2_key_protector_init" command to
grub.cfg:

  tpm2_key_protector_init --mode=nv --nvindex=0x1000000
  cryptomount -u <UUID> --protector tpm2

To remove the NV index handle:

  # tpm2_nvundefine -C o 0x1000000

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:10:33 +02:00
Gary Lin
fa69deac56 tpm2_key_protector: Unseal key from a buffer
Extract the logic to handle the file buffer from the SRK recover
function to prepare to load the sealed key from the NV index handle,
so the NV index mode can share the same code path in the later patch.
The SRK recover function now only reads the file and sends the file
buffer to the new function.

Besides this, to avoid introducing more options for the NV index mode,
the file format is detected automatically before unmarshaling the data,
so there is no need to use the command option to specify the file format
anymore. In other words, "-T" and "-k" are the same now.

Also update grub.text to address the change.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:05:58 +02:00
Gary Lin
75c480885a tss2: Add TPM 2.0 NV index commands
The following TPM 2.0 commands are introduced to tss2 to access the
TPM non-volatile memory associated with the NV index handles:
  - TPM2_NV_DefineSpace,
  - TPM2_NV_UndefineSpace,
  - TPM2_NV_ReadPublic,
  - TPM2_NV_Read,
  - TPM2_NV_Write.

The related marshal/unmarshal functions are also introduced.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:04:48 +02:00
Gary Lin
041164d00e tss2: Fix the missing authCommand
grub_tpm2_readpublic() and grub_tpm2_testparms() didn't check
authCommand when marshaling the input data buffer. Currently, there is
no caller using non-NULL authCommand. However, to avoid the potential
issue, the conditional check is added to insert authCommand into the
input buffer if necessary.

Also fix a few pointer checks.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:04:01 +02:00
Gary Lin
46c9f3a8da tpm2_key_protector: Add tpm2_dump_pcr command
The user may need to inspect the TPM 2.0 PCR values with the GRUB shell,
so the new tpm2_dump_pcr command is added to print all PCRs of the
specified bank.

Also update the document for the new command.

Signed-off-by: Gary Lin <glin@suse.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:03:55 +02:00
Gary Lin
617dab9e47 tpm2_key_protector: Dump PCRs on policy fail
PCR mismatch is one common cause of TPM key unsealing fail. Since the
system may be compromised, it is not safe to boot into OS to get the PCR
values and TPM eventlog for the further investigation.

To provide some hints, GRUB now dumps PCRs on policy fail, so the user
can check the current PCR values. PCR 0~15 are chosen to cover the
firmware, bootloader, and OS.

The sample output:

PCR Mismatch! Check firmware and bootloader before typing passphrase!
TPM PCR [sha256]:
  00: 17401f37710984c1d8a03a81fff3ab567ae9291bac61e21715b890ee28879738
  01: 7a114329ba388445a96e8db2a072785937c1b7a8803ed7cc682b87f3ff3dd7a8
  02: 11c2776849e8e24b7d80c926cbc4257871bffa744dadfefd3ed049ce25143e05
  03: 6c33b362073e28e30b47302bbdd3e6f9cee4debca3a304e646f8c68245724350
  04: 62d38838483ecfd2484ee3a2e5450d8ca3b35fc72cda6a8c620f9f43521c37d1
  05: d8a85cb37221ab7d1f2cc5f554dbe0463acb6784b5b8dc3164ccaa66d8fff0e1
  06: 9262e37cbe71ed4daf815b4a4881fb7251c9d371092dde827557d5368121e10e
  07: 219d542233be492d62b079ffe46cf13396a8c27e520e88b08eaf2e6d3b7e70f5
  08: de1f61c973b673e505adebe0d7e8fb65fde6c24dd4ab4fbaff9e28b18df6ecd3
  09: c1de7274fa3e879a16d7e6e7629e3463d95f68adcfd17c477183846dccc41c89
  10: 0000000000000000000000000000000000000000000000000000000000000000
  11: 0000000000000000000000000000000000000000000000000000000000000000
  12: 0000000000000000000000000000000000000000000000000000000000000000
  13: 0000000000000000000000000000000000000000000000000000000000000000
  14: 9ab9ebe4879a7f4dd00c04f37e79cfd69d0dd7a8bcc6b01135525b67676a3e40
  15: 0000000000000000000000000000000000000000000000000000000000000000
  16: 0000000000000000000000000000000000000000000000000000000000000000
  17: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  18: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  19: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  20: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  21: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  22: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  23: 0000000000000000000000000000000000000000000000000000000000000000
error: failed to unseal sealed key (TPM2_Unseal: 0x99d).
error: no key protector provided a usable key for luks (af16e48f-746b-4a12-aae1-c14dcee429e0).

If the user happens to have the PCR values for key sealing, the PCR dump
can be used to identify the changed PCRs and narrow down the scope for
closer inspection.

Please note that the PCR dump is trustworthy only if the GRUB binary is
authentic, so the user has to check the GRUB binary thoroughly before
using the PCR dump.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-10 18:03:38 +02:00
Patrick Colp
204a6ddfb3 loader/i386/linux: Update linux_kernel_params to match upstream
Update linux_kernel_params to match the v6.13.7 upstream version of boot_params.
Refactor most things out into structs, as the Linux kernel does.

edid_info should be a struct with "unsigned char dummy[128]" and efi_info should
be a struct as well, starting at 0x1c0. However, for backwards compatibility,
GRUB can have efi_systab at 0x1b8 and padding at 0x1bc (or padding at both spots).
This cuts into the end of edid_info. Make edid_info inline and only make it go
up to 0x1b8.

Signed-off-by: Patrick Colp <patrick.colp@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 21:57:05 +02:00
Lidong Chen
6b64f297e5 loader/xnu: Fix memory leak
In grub_xnu_load_kext_from_dir(), when the call to grub_device_open()
failed, it simply cleaned up previously allocated memory and returned
GRUB_ERR_NONE. However, it neglected to free ctx->newdirname which is
allocated before the call to grub_device_open().

Fixes: CID 473859

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 19:37:34 +02:00
Lidong Chen
f94d257e8c fs/btrfs: Fix memory leaks
Fix memory leaks in grub_btrfs_extent_read() and grub_btrfs_dir().

Fixes: CID 473842
Fixes: CID 473871

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 19:35:51 +02:00
Lidong Chen
81146fb623 loader/i386/linux: Fix resource leak
In grub_cmd_initrd(), initrd_ctx is allocated before calling
grub_relocator_alloc_chunk_align(). When that function fails,
initrd_ctx should be freed before exiting grub_cmd_initrd().

Fixes: CID 473852

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 19:33:50 +02:00
Lidong Chen
1d00594475 lib/reloacator: Fix memory leaks
Fix memory leaks in grub_relocator_alloc_chunk_align().

Fixes: CID 473844

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 19:31:05 +02:00
Lidong Chen
f3f1fcecdc disk/ldm: Fix memory leaks
Fix memory leaks in make_vg() with new helper functions, free_pv()
and free_lv(). Additionally, correct a check after allocating
comp->segments->nodes that mistakenly checked lv->segments->nodes
instead, likely due to a copy-paste error.

Fixes: CID 473878
Fixes: CID 473884
Fixes: CID 473889
Fixes: CID 473890

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-04-04 19:27:25 +02:00
Andrew Hamilton
aae2ea619e fs/ntfs: Fix NULL pointer dereference and possible infinite loop
A regression was introduced recently as a part of the series of
filesystem related patches to address some CVEs found in GRUB.

This issue may cause either an infinite loop at startup when
accessing certain valid NTFS filesystems, or may cause a crash
due to a NULL pointer dereference on systems where NULL address
is invalid (such as may happen when calling grub-mount from
the operating system level).

Correct this issue by checking that at->attr_cur is within bounds
inside find_attr().

Fixes: https://savannah.gnu.org/bugs/?66855
Fixes: aff263187 (fs/ntfs: Fix out-of-bounds read)

Signed-off-by: B Horn <b@horn.uk>
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
2025-03-26 15:33:02 +01:00
Nicolas Frayer
3b25e494d4 net/drivers/ieee1275/ofnet: Add missing grub_malloc()
The grub_malloc() has been inadvertently removed from the code after it
has been modified to use safe math functions.

Fixes: 4beeff8a (net: Use safe math macros to prevent overflows)

Signed-off-by: Nicolas Frayer <nfrayer@redhat.com>
Tested-by: Marta Lewandowska <mlewando@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 15:15:22 +01:00
Avnish Chouhan
fee6081ec7 kern/ieee1275/init: Increase MIN_RMA size for CAS negotiation on PowerPC machines
Change RMA size from 512 MB to 768 MB which will result in more memory
at boot time for PowerPC. When vTPM, Secure Boot or FADump are enabled
on PowerPC the 512 MB RMA memory is not sufficient for boot. With this
512 MB RMA, GRUB runs out of memory and fails to boot the machine.
Sometimes even usage of CDROM requires more memory for installation and
along with the options mentioned above exhausts the boot memory which
results in boot failures. Increasing the RMA size will resolves multiple
out of memory issues observed on PowerPC machines.

Failure details (GRUB debug console dump):

  kern/ieee1275/init.c:550: mm requested region of size 8513000, flags 1
  kern/ieee1275/init.c:563: Cannot satisfy allocation and retain minimum runtime space
  kern/ieee1275/init.c:550: mm requested region of size 8513000, flags 0
  kern/ieee1275/init.c:563: Cannot satisfy allocation and retain minimum runtime space
  kern/file.c:215: Closing `/ppc/ppc64/initrd.img' ...
  kern/disk.c:297: Closing `ieee1275//vdevice/v-scsi@30000067/disk@8300000000000000'...
  kern/disk.c:311: Closing `ieee1275//vdevice/v-scsi@30000067/disk@8300000000000000' succeeded.
  kern/file.c:225: Closing `/ppc/ppc64/initrd.img' failed with 3.
  kern/file.c:148: Opening `/ppc/ppc64/initrd.img' succeeded.
  error: ../../grub-core/kern/mm.c:552:out of memory.

Signed-off-by: Avnish Chouhan <avnish@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:56:59 +01:00
Stuart Hayes
b66c6f9182 fs/zfs: Fix a number of memory leaks in ZFS code
Without this fix the GRUB failed to boot linux with "out of memory" after
trying to run a "search --fs-uuid..." on a system that has 7 ZFS pools
across about 80 drives.

Signed-off-by: Stuart Hayes <stuart.w.hayes@gmail.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:34:44 +01:00
Glenn Washburn
1d59f39b5f tests/util/grub-shell: Remove the work directory on successful run and debug is not on
This removes a lot of empty grub-shell working directories in the TMPDIR directory.

Signed-off-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:30:56 +01:00
Thomas Schmitt
e0116f3bd5 tests/grub_cmd_cryptomount: Remove temporary directories if successful and debug is not on
grub_cmd_cryptomount creates a directory per subtest. If a subtest is
successful and debugging is not on, the directory should be empty.
So, it can be deleted.

Signed-off-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:29:21 +01:00
Glenn Washburn
e6e2b73db8 tests/grub_cmd_cryptomount: Default TMPDIR to /tmp
This fixes behavior where grub_cmd_cryptomount temporary files, which are
some times not cleaned up, are left in the / directory. Set TMPDIR if your
system does not have /tmp or it can not be used for some reason.

Reported-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:23:42 +01:00
Glenn Washburn
32b02bb92e tests/grub_cmd_cryptomount: Cleanup the cryptsetup script unless debug is enabled
This fixes an issue where the grub_cmd_cryptomount test leaves a file
with an ambiguous name in the / directory when TMPDIR is not set.

Reported-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:22:29 +01:00
Glenn Washburn
c188ca5d5e tests: Cleanup generated files on expected failure in grub_cmd_cryptomount
grub-shell-luks-tester only cleans up generated files when the test it
runs returns success. Sometimes tests are run that should fail. Add
a --xfail argument to grub-shell-luks-tester and pass it from
grub_cmd_cryptomount when invoking a test that is expected to fail.

Reported-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:18:46 +01:00
Glenn Washburn
50320c093a tests/util/grub-shell-luks-tester: Add missing line to create RET variable in cleanup
Set the RET variable to the exit status of the script, as was assumed in
the cleanup() function.

Reported-by: Thomas Schmitt <scdbackup@gmx.net>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Tested-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:16:39 +01:00
Glenn Washburn
bb6d3199b3 tests/util/grub-shell-luks-tester: Find cryptodisk by UUID
GRUB has the capability to search all the disks for a cryptodisk of a
given UUID. Use this instead of hardcoding which disk is the cryptodisk,
which can change when devices are added or removed, or potentially when
QEMU is upgraded. This can not be done for the detached header tests
because the header contains the UUID.

Also, capitalize comment lines for consistency.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:08:49 +01:00
Glenn Washburn
3fd163e453 tests/util/grub-shell: Default qemuopts to envvar $GRUB_QEMU_OPTS
Fix a regression where qemuopts was mistakenly defaulted to the empty
string. This prevents the sending of arbitrary QEMU options to tests,
which may be desirable for overriding the machine type. There was a
concern that allowing the tester to accept arbitrary options would add
headaches for another developer trying to diagnose why a test failed on
the testers machine because he could not be sure if any additional
options were passed to make the test fail. However, all the options are
recorded in the run.sh generated script, so this concern is unwarranted.

Fixes: 6d729ced70 (tests/util/grub-shell: Add $GRUB_QEMU_OPTS to run.sh to easily see unofficial QEMU arguments)

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-26 14:04:49 +01:00
Patrick Plenefisch
ff7f553071 disk/lvm: Add informational messages in error cases of ignored features
Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:28:03 +01:00
Patrick Plenefisch
a16b4304a2 disk/lvm: Add support for cachevol LV
Mark cachevol LV's as ignored features, which is true only if they are
configured as "writethrough". This patch does not let GRUB boot from
"writeback" cache-enabled LV's.

Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:27:54 +01:00
Patrick Plenefisch
9a37d61145 disk/lvm: Add support for integrity LV
The LV matching must be done after processing the ignored feature
indirections, as integrity volumes & caches may have several levels
of indirection that the segments must be shifted through.

Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:27:37 +01:00
Patrick Plenefisch
6c14b87d6f lvm: Match all LVM segments before validation
The PV matching must be completely finished before validating a volume,
otherwise referenced RAID stripes may not have PV data applied yet.

This change is required for integrity & cachevol support.

Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Patrick Plenefisch
d34b9120e7 disk/lvm: Remove unused cache_pool
The cache_pool is never read or used, remove it.

Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Patrick Plenefisch
90848a1f7c disk/lvm: Make cache_lv more generic as ignored_feature_lv
This patch isn't necessary by itself, but when combined with subsequent
patches it enhances readability as ignored_features_lv is then used for
multiple types of extra LV's, not just cache LV's.

Signed-off-by: Patrick Plenefisch <simonpatp@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Glenn Washburn
488ac8bda9 commands/ls: Add directory header for dir args
Like the GNU ls, first print a line with the directory path before printing
files in the directory, which will not have a directory component, but only
if there is more than one argument.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Glenn Washburn
096bf59e4c commands/ls: Print full paths for file args
For arguments that are paths to files, print the full path of the file.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Glenn Washburn
90288fc48d commands/ls: Output path for single file arguments given with path
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Glenn Washburn
6337d84afa commands/ls: Show modification time for file paths
The modification time for paths to files was not being printed because
the grub_dirhook_info, which contains the mtime, was initialized to NULL.
Instead of calling print_file() directly, use fs->fs_dir() to call
print_file() with a properly filled in grub_dirhook_info. This has the
added benefit of reducing code complexity.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:48 +01:00
Glenn Washburn
cbfb031b14 commands/ls: Merge print_files_long() and print_files() into print_file()
Simplify the code by removing logic around which file printer to call.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Glenn Washburn
112d2069cf commands/ls: Return proper GRUB_ERR_* for functions returning type grub_err_t
Also, remove unused code.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Glenn Washburn
da9740cd52 commands/acpi: Use options enum to index command options
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Andrew Hamilton
1acf11fe4c docs: Capture additional commands restricted by lockdown
Update documentation to capture that all memrw commands, the minicmd
dump command, and raw memory dumping via hexdump are restricted when
lockdown is enabled. This aligns to recent GRUB code updates.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Andrew Hamilton
6a168afd32 docs: Document restricted filesystems in lockdown
Document which filesystems are not allowed when lockdown
is enabled to align to recent GRUB changes.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Vladimir Serbinenko
be0ae9583e loader/i386/bsd: Fix type passed for the kernel
FreeBSD loader always passes "elf kernel". We currently pass "elf64 kernel"
when loading 64-bit kernel. The -CURRENT, HEAD, kernel accepts only
"elf kernel". Older kernel accepts either.

Tested with FreeBSD and DragonFlyBSD.

Reference: https://cgit.freebsd.org/src/commit/?id=b72ae900d4348118829fe04abdc11b620930c30f

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Vladimir Serbinenko
ee27f07a65 kern/partition: Unbreak support for nested partitions
When using syntax "hd0,gtp3,dfly1" then ptr points to trailing part, ",dfly1".
So, it's improper to consider it as an invalid partition.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Andrew Hamilton
cb639acea0 lib/tss2/tss2_structs.h: Fix clang build - remove duplicate typedef
grub-core/lib/tss2/tss2_structs.h contains a duplicate typedef as follows:
  typedef TPMS_SCHEME_HASH_t TPMS_SCHEME_KDF2_t;

This causes a build failure when compiling with clang. Remove the
duplicate typedef which allows successfully building GRUB with clang.

Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 21:24:47 +01:00
Yuri Zaporozhets
696e35b7ff include/grub/mm.h: Remove duplicate inclusion of grub/err.h
The header is included twice. Fix that.

Signed-off-by: Yuri Zaporozhets <yuriz@qrv-systems.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 12:16:56 +01:00
James Le Cuirot
187338f1ac script/execute: Don't let trailing blank lines determine the return code
The grub_script_execute_sourcecode() parses and executes code one line
at a time, updating the return code each time because only the last line
determines the final status. However, trailing new lines were also
executed, masking any failure on the previous line. Fix this by only
trying to execute the command when there is actually one present.

This has presumably never been noticed because this code is not used by
regular functions, only in special cases like eval and menu entries. The
latter generally don't return at all, having booted an OS. When failing
to boot, upstream GRUB triggers the fallback mechanism regardless of the
return code.

We noticed the problem while using Red Hat's patches, which change this
behaviour to take account of the return code. In that case, a failure
takes you back to the menu rather than triggering a fallback.

Signed-off-by: James Le Cuirot <jlecuirot@microsoft.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 12:11:09 +01:00
Glenn Washburn
ff173a1c0c gitignore: Ignore generated files from libtasn
The commit 504058e8 (libtasn1: Compile into asn1 module) generates files
into the grub-core/lib/libtasn1-grub directory and commit 99cda678
(asn1_test: Test module for libtasn1) generates files into the
grub-core/tests/asn1/tests directory. Ignore these directories as they
are not under revision control.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 12:05:38 +01:00
Pascal Hambourg
fbcc388917 util/grub.d/30_os-prober.in: Conditionally show or hide chain and efi menu entries
On systems which support multiple boot platforms such as BIOS and
EFI, it makes no sense to show menu entries which are not supported
by the current boot platform. Menu entries generated from os-prober
"chain" boot type use boot sector chainloading which is supported
on PC BIOS platform only.

Show "chain" menu entries only if boot platform is PC BIOS.
Show "efi" menu entries only if boot platform is EFI.

This is aimed to allow os-prober to report both EFI and PC BIOS
boot loaders regardless of the current boot mode on x86 systems
which support both EFI and legacy BIOS boot, in order to generate
a config file which can be used with either BIOS or EFI boot.

Signed-off-by: Pascal Hambourg <pascal@plouf.fr.eu.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-03-05 11:53:33 +01:00
Pascal Hambourg
56ccc5ed56 util/grub.d/30_os-prober.in: Fix GRUB_OS_PROBER_SKIP_LIST for non-EFI
GRUB documentation states:

  GRUB_OS_PROBER_SKIP_LIST
    List of space-separated FS UUIDs of filesystems to be ignored from
    os-prober output. For efi chainloaders it’s <UUID>@<EFI FILE>

But the actual behaviour does not match this description.

  GRUB_OS_PROBER_SKIP_LIST="<UUID>"

does nothing. In order to skip non-EFI bootloaders, you must set

  GRUB_OS_PROBER_SKIP_LIST="<UUID>@<DEVICE>"

which is both absurd, <UUID> and <DEVICE> are redundant, and wrong,
<DEVICE> such as /dev/sd* may not be persistent across boots.

Also, any non-word character is accepted as a separator, including "-"
and "@" which may be present in UUIDs. This can cause false positives
because of partial UUID match.

This patch fixes these flaws while retaining some backward compatibility
with previous behaviour which may be expected by existing setups:
  - also accept <UUID>@/dev/* (with warning) for non-EFI bootloaders,
  - also accept comma and semicolon as separator.

Fixes: 55e706c9 (Add GRUB_OS_PROBER_SKIP_LIST to selectively skipping systems)

Signed-off-by: Pascal Hambourg <pascal@plouf.fr.eu.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Glenn Washburn
01f064064c docs: Do not reference non-existent --dumb option
This appears to be a relic from GRUB legacy that used a --dumb option for
its terminal command. The proper way to do this in GRUB2 is to set the
terminal to "dumb" via the terminfo command.

Fixes: https://savannah.gnu.org/bugs/?66302

Reported-by: Jernej Jakob <jernej.jakob+savgnu@gmail.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Glenn Washburn
3f440b5a53 docs: Replace @lbracechar{} and @rbracechar{} with @{ and @}
Support for @lbracechar{} and @rbracechar{} was added in GNU Texinfo 5.0
but many older systems may have versions lower than this. Use @{ and @}
to support a wider range of GNU Texinfo versions.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Egor Ignatov
f209887381 fs/xfs: Fix grub_xfs_iterate_dir() return value in case of failure
Commit ef7850c757 (fs/xfs: Fix issues found while fuzzing the XFS
filesystem) introduced multiple boundary checks in grub_xfs_iterate_dir()
but handled the error incorrectly returning error code instead of 0.
Fix it. Also change the error message so that it doesn't match the
message in grub_xfs_read_inode().

Fixes: ef7850c757 (fs/xfs: Fix issues found while fuzzing the XFS filesystem)

Signed-off-by: Egor Ignatov <egori@altlinux.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Darrick J. Wong
1ed2628b56 fs/xfs: Add new superblock features added in Linux 6.12/6.13
The Linux port of XFS added a few new features in 2024. The existing
GRUB driver doesn't attempt to read or write any of the new metadata,
so, all three can be added to the incompat allowlist.

On the occasion align XFS_SB_FEAT_INCOMPAT_NREXT64 value.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Michael Chang
348cd416a3 fs/ext2: Rework out-of-bounds read for inline and external extents
Previously, the number of extent entries was not properly capped based
on the actual available space. This could lead to insufficient reads for
external extents since the computation was based solely on the inline
extent layout.

In this patch, when processing the extent header we determine whether
the header is stored inline, i.e. at inode->blocks.dir_blocks, or in an
external extent block. We then clamp the number of entries accordingly
(using max_inline_ext for inline extents and max_external_ext for
external extent blocks).

This change ensures that only the valid number of extent entries is
processed preventing out-of-bound reads and potential filesystem
corruption.

Fixes: 7e2f750f0a (fs/ext2: Fix out-of-bounds read for inline extents)

Signed-off-by: Michael Chang <mchang@suse.com>
Tested-by: Christian Hesse <mail@eworm.de>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:44:36 +01:00
Leo Sandoval
c730eddd2a disk/ahci: Remove conditional operator for endtime
The conditional makes no sense when the two possible expressions have
the same value, so, remove it (perhaps the compiler does it for us but
better to remove it). This change makes spinup argument unused. So, drop
it as well.

Signed-off-by: Leo Sandoval <lsandova@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 20:39:24 +01:00
Benjamin Herrenschmidt
f0a08324d0 term/ns8250-spcr: Return if redirection is disabled
The Microsoft spec for SPCR says "The base address of the Serial Port
register set described using the ACPI Generic Address Structure, or
0 if console redirection is disabled". So, return early if redirection
is disabled (base address = 0). If this check is not done we may get
invalid ports on machines with redirection disabled and boot may hang
when reading the grub.cfg file.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Reviewed-by: Leo Sandoval <lsandova@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 19:34:57 +01:00
Lukas Fink
7161e2437d commands/file: Fix NULL dereference in the knetbsd tests
The pointer returned by grub_elf_file() is not checked to verify it is
not NULL before use. A NULL pointer may be returned when the given file
does not have a valid ELF header.

Fixes: https://savannah.gnu.org/bugs/?61960

Signed-off-by: Glenn Washburn <development@efficientek.com>
Signed-off-by: Lukas Fink <lukas.fink1@gmail.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 19:34:57 +01:00
Heinrich Schuchardt
11b9c2dd0d gdb_helper: Typo hueristic
%s/hueristic/heuristic/

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 19:34:57 +01:00
Ruihan Li
224aefd057 kern/efi/mm: Reset grub_mm_add_region_fn after ExitBootServices() call
The EFI Boot Services can be used after ExitBootServices() call because
the GRUB code still may allocate memory.

An example call stack is:

  grub_multiboot_boot
    grub_multiboot2_make_mbi
      grub_efi_finish_boot_services
        b->exit_boot_services
    normal_boot
      grub_relocator32_boot
        grub_relocator_alloc_chunk_align_safe
          grub_relocator_alloc_chunk_align
            grub_malloc
              grub_memalign
                grub_mm_add_region_fn
                [= grub_efi_mm_add_regions]
                  grub_efi_allocate_any_pages
                    grub_efi_allocate_pages_real
                      b->allocate_pages

This can lead to confusing errors. After ExitBootServices() call
b->allocate_pages may point to the NULL address resulting in something like:

  !!!! X64 Exception Type - 01(#DB - Debug)  CPU Apic ID - 00000000 !!!!
  RIP  - 000000000000201F, CS  - 0000000000000038, RFLAGS - 0000000000200002
  RAX  - 000000007F9EE010, RCX - 0000000000000001, RDX - 0000000000000002
  RBX  - 0000000000000006, RSP - 00000000001CFBEC, RBP - 0000000000000000
  RSI  - 0000000000000000, RDI - 00000000FFFFFFFF
  R8   - 0000000000000006, R9  - 000000007FEDFFB8, R10 - 0000000000000000
  R11  - 0000000000000475, R12 - 0000000000000001, R13 - 0000000000000002
  R14  - 00000000FFFFFFFF, R15 - 000000007E432C08
  DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
  GS   - 0000000000000030, SS  - 0000000000000030
  CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FC01000
  CR4  - 0000000000000668, CR8 - 0000000000000000
  DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
  DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400
  GDTR - 000000007F9DE000 0000000000000047, LDTR - 0000000000000000
  IDTR - 000000007F470018 0000000000000FFF,   TR - 0000000000000000
  FXSAVE_STATE - 00000000001CF840

Ideally we would like to avoid all memory allocations after exiting EFI
Boot Services altogether but that requires significant code changes. This
patch adds a simple workaround that resets grub_mm_add_region_fn to NULL
after ExitBootServices() call, so:

  - Memory allocations have a better chance of succeeding because grub_memalign()
    will try to reclaim the disk cache if it sees a NULL in grub_mm_add_region_fn.

  - At worst it will fail to allocate memory but it will explicitly tell users
    that it's out of memory, which is still much better than the current
    situation where it fails in a fairly random way and triggers a CPU fault.

Signed-off-by: Ruihan Li <lrh2000@pku.edu.cn>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 19:34:57 +01:00
Duan Yayong
531750f7bf i386/tsc: The GRUB menu gets stuck due to unserialized rdtsc
This patch is used to fix GRUB menu gets stuck in server AC
poweron/poweroff stress test of x86_64, which is reproduced with
1/200 ratio. The root cause analysis as below:

Q: What's the code logic?

A: The grub_tsc_init() function will init tsc by setting grub_tsc_rate,
   which call stack is:

     grub_tsc_init() -> grub_tsc_calibrate_from_pmtimer() -> grub_divmod64()

   Among, grub_divmod64() function needs tsc_diff as the second parameter.
   In grub_pmtimer_wait_count_tsc(), we will call grub_get_tsc() function
   to get time stamp counter value to assign to start_tsc variable, and
   get into while (1) loop space to get end_tsc variable value with same
   function, after 3580 ticks, return "end_tsc - start_tsc". Actually,
   rdtsc instruction will be called in grub_get_tsc, but rdtsc instruction
   is not reliable (for the reason see the next question), which will cause
   tsc_diff to be a very big number larger than (1UL << 32) or a negative
   number, so that grub_tsc_rate will be zero. When run_menu() function is
   startup, and calls grub_tsc_get_time_ms() function to get current time
   to check if timeout time reach, at this time, grub_tsc_get_time_ms()
   function will return zero due to zero grub_tsc_rate variable, then GRUB
   menu gets stuck...

Q: What's the difference between rdtsc and rdtscp instructions in x86_64
   architecture? Here is more explanations from Intel® 64 and IA-32
   Architectures Software Developer’s Manual Volume 2B (December 2024):
   https://cdrdv2.intel.com/v1/dl/getContent/671241

A: In page 4-558 -> RDTSC—Read Time-Stamp Counter:
   The RDTSC instruction is not a serializing instruction. It does not
   necessarily wait until all previous instructions have been executed
   before reading the counter. Similarly, subsequent instructions may
   begin execution before the read operation is performed. The following
   items may guide software seeking to order executions of RDTSC:
     - If software requires RDTSC to be executed only after all previous
       instructions have executed and all previous loads are globally
       visible, it can execute LFENCE immediately before RDTSC.
     - If software requires RDTSC to be executed only after all previous
       instructions have executed and all previous loads and stores are
       globally visible, it can execute the sequence MFENCE;LFENCE
       immediately before RDTSC.
     - If software requires RDTSC to be executed prior to execution of any
       subsequent instruction (including any memory accesses), it can execute
       the sequence LFENCE immediately after RDTSC.

A: In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:
   The RDTSCP instruction is not a serializing instruction, but it does wait
   until all previous instructions have executed and all previous loads are
   globally visible. But it does not wait for previous stores to be globally
   visible, and subsequent instructions may begin execution before the read
   operation is performed. The following items may guide software seeking to
   order executions of RDTSCP:
     - If software requires RDTSCP to be executed only after all previous
       stores are globally visible, it can execute MFENCE immediately before
       RDTSCP.
     - If software requires RDTSCP to be executed prior to execution of any
       subsequent instruction (including any memory accesses), it can execute
       LFENCE immediately after RDTSCP.

Q: Why there is a cpuid serializing instruction before rdtsc instruction,
   but "grub_get_tsc" still cannot work as expect?

A: From Intel® 64 and IA-32 Architectures Software Developer's Manual
   Volume 2A: Instruction Set Reference, A-L (December 2024):
   https://cdrdv2.intel.com/v1/dl/getContent/671199

   In page 3-222 -> CPUID—CPU Identification:
   CPUID can be executed at any privilege level to serialize instruction execution.
   Serializing instruction execution guarantees that any modifications to flags,
   registers, and memory for previous instructions are completed before
   the next instruction is fetched and executed.

   So we only kept the instruction rdtsc and its previous instruction in order
   currently. But it is still out-of-order possibility between rdtsc instruction
   and its subsequent instruction.

Q: Why do we do this fix?

A: In the one hand, add cpuid instruction after rdtsc instruction to make sure
   rdtsc instruction to be executed prior to execution of any subsequent instruction,
   about serializing execution that all previous instructions have been executed
   before rdtsc, there is a cpuid usage in original code. In the other hand, using
   cpuid instruction rather than lfence can make sure a forward compatibility for
   previous HW.

   Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce
   this issue again.

Fixes: https://savannah.gnu.org/bugs/?66257

Signed-off-by: Duan Yayong <duanyayong@bytedance.com>
Signed-off-by: Li Yongqiang <liyongqiang@huaqin.com>
Signed-off-by: Sun Ming <simon.sun@huaqin.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 19:25:38 +01:00
Duan Yayong
f2a1f66e72 kern/i386/tsc_pmtimer: The GRUB menu gets stuck due to failed calibration
The grub_divmod64() may return 0 but grub_tsc_calibrate_from_pmtimer()
still returns 1 saying calibration succeeded. Of course it is not true.
So, return 0 when grub_divmod64() returns 0. This way other calibration
functions can be called subsequently.

Signed-off-by: Duan Yayong <duanyayong@bytedance.com>
Signed-off-by: Li Yongqiang <liyongqiang@huaqin.com>
Signed-off-by: Sun Ming <simon.sun@huaqin.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 15:51:04 +01:00
Sergii Dmytruk
13f005ed83 loader/i386/linux: Fix cleanup if kernel doesn't support 64-bit addressing
Simply returning from grub_cmd_linux() doesn't free "file" resource nor
calls grub_dl_ref(my_mod). Jump to "fail" label for proper cleanup like
other error checks do.

Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-26 15:31:59 +01:00
Alec Brown
4dc6166571 loader/i386/bsd: Use safe math to avoid underflow
The operation kern_end - kern_start may underflow when we input it into
grub_relocator_alloc_chunk_addr() call. To avoid this we can use safe
math for this subtraction.

Fixes: CID 73845

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Alec Brown
490a6ab71c loader/i386/linux: Cast left shift to grub_uint32_t
The Coverity complains that we might overflow into a negative value when
setting linux_params.kernel_alignment to (1 << align). We can remedy
this by casting it to grub_uint32_t.

Fixes: CID 473876

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Lidong Chen
a8d6b06331 kern/misc: Add sanity check after grub_strtoul() call
When the format string, fmt0, includes a positional argument
grub_strtoul() or grub_strtoull() is called to extract the argument
position. However, the returned argument position isn't fully validated.
If the format is something like "%0$x" then these functions return
0 which leads to an underflow in the calculation of the args index, curn.
The fix is to add a check to ensure the extracted argument position is
greater than 0 before computing curn. Additionally, replace one
grub_strtoull() with grub_strtoul() and change curn type to make code
more correct.

Fixes: CID 473841

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Lidong Chen
8e6e87e792 kern/partition: Add sanity check after grub_strtoul() call
The current code incorrectly assumes that both the input and the values
returned by grub_strtoul() are always valid which can lead to potential
errors. This fix ensures proper validation to prevent any unintended issues.

Fixes: CID 473843

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Alec Brown
5b36a5210e normal/menu: Use safe math to avoid an integer overflow
The Coverity indicates that the variable current_entry might overflow.
To prevent this use safe math when adding GRUB_MENU_PAGE_SIZE to current_entry.

On the occasion fix limiting condition which was broken.

Fixes: CID 473853

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Alec Brown
9907d9c272 bus/usb/ehci: Define GRUB_EHCI_TOGGLE as grub_uint32_t
The Coverity indicates that GRUB_EHCI_TOGGLE is an int that contains
a negative value and we are using it for the variable token which is
grub_uint32_t. To remedy this we can cast the definition to grub_uint32_t.

Fixes: CID 473851

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Lidong Chen
f8795cde21 misc: Ensure consistent overflow error messages
Update the overflow error messages to make them consistent
across the GRUB code.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:58 +01:00
Lidong Chen
66733f7c7d osdep/unix/getroot: Fix potential underflow
The entry_len is initialized in grub_find_root_devices_from_mountinfo()
to 0 before the while loop iterates through /proc/self/mountinfo. If the
file is empty or contains only invalid entries entry_len remains
0 causing entry_len - 1 in the subsequent for loop initialization
to underflow. To prevent this add a check to ensure entry_len > 0 before
entering the for loop.

Fixes: CID 473877

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
2025-02-13 15:45:58 +01:00
Lidong Chen
d13b6e8ebd script/execute: Fix potential underflow and NULL dereference
The result is initialized to 0 in grub_script_arglist_to_argv().
If the for loop condition is not met both result.args and result.argc
remain 0 causing result.argc - 1 to underflow and/or result.args NULL
dereference. Fix the issues by adding relevant checks.

Fixes: CID 473880

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Alec Brown
e3c578a56f fs/sfs: Check if allocated memory is NULL
When using grub_zalloc(), if we are out of memory, this function can fail.
After allocating memory, we should check if grub_zalloc() returns NULL.
If so, we should handle this error.

Fixes: CID 473856

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Alec Brown
1c06ec9005 net: Check if returned pointer for allocated memory is NULL
When using grub_malloc(), the function can fail if we are out of memory.
After allocating memory we should check if this function returned NULL
and handle this error if it did.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
dee2c14fd6 net: Prevent overflows when allocating memory for arrays
Use grub_calloc() when allocating memory for arrays to ensure proper
overflow checks are in place.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
4beeff8a31 net: Use safe math macros to prevent overflows
Replace direct arithmetic operations with macros from include/grub/safemath.h
to prevent potential overflow issues when calculating the memory sizes.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
dd6a4c8d10 fs/zfs: Add missing NULL check after grub_strdup() call
Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
13065f69da fs/zfs: Check if returned pointer for allocated memory is NULL
When using grub_malloc() or grub_zalloc(), these functions can fail if
we are out of memory. After allocating memory we should check if these
functions returned NULL and handle this error if they did.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
7f38e32c7e fs/zfs: Prevent overflows when allocating memory for arrays
Use grub_calloc() when allocating memory for arrays to ensure proper
overflow checks are in place.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
88e491a0f7 fs/zfs: Use safe math macros to prevent overflows
Replace direct arithmetic operations with macros from include/grub/safemath.h
to prevent potential overflow issues when calculating the memory sizes.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
cde9f7f338 fs: Prevent overflows when assigning returned values from read_number()
The direct assignment of the unsigned long long value returned by
read_number() can potentially lead to an overflow on a 32-bit systems.
The fix replaces the direct assignments with calls to grub_cast()
which detects the overflows and safely assigns the values if no
overflow is detected.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
84bc0a9a68 fs: Prevent overflows when allocating memory for arrays
Use grub_calloc() when allocating memory for arrays to ensure proper
overflow checks are in place.

The HFS+ and squash4 security vulnerabilities were reported by
Jonathan Bar Or <jonathanbaror@gmail.com>.

Fixes: CVE-2025-0678
Fixes: CVE-2025-1125

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Lidong Chen
6608163b08 fs: Use safe math macros to prevent overflows
Replace direct arithmetic operations with macros from include/grub/safemath.h
to prevent potential overflow issues when calculating the memory sizes.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:57 +01:00
Alec Brown
fbaddcca54 disk/ieee1275/ofdisk: Call grub_ieee1275_close() when grub_malloc() fails
In the dev_iterate() function a handle is opened but isn't closed when
grub_malloc() returns NULL. We should fix this by closing it on error.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Alec Brown
33bd6b5ac5 disk: Check if returned pointer for allocated memory is NULL
When using grub_malloc(), grub_zalloc() or grub_calloc(), these functions can
fail if we are out of memory. After allocating memory we should check if these
functions returned NULL and handle this error if they did.

On the occasion make a NULL check in ATA code more obvious.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Alec Brown
d8151f9833 disk: Prevent overflows when allocating memory for arrays
Use grub_calloc() when allocating memory for arrays to ensure proper
overflow checks are in place.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Alec Brown
c407724dad disk: Use safe math macros to prevent overflows
Replace direct arithmetic operations with macros from include/grub/safemath.h
to prevent potential overflow issues when calculating the memory sizes.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Daniel Axtens
c4bc55da28 fs: Disable many filesystems under lockdown
The idea is to permit the following: btrfs, cpio, exfat, ext, f2fs, fat,
hfsplus, iso9660, squash4, tar, xfs and zfs.

The JFS, ReiserFS, romfs, UDF and UFS security vulnerabilities were
reported by Jonathan Bar Or <jonathanbaror@gmail.com>.

Fixes: CVE-2025-0677
Fixes: CVE-2025-0684
Fixes: CVE-2025-0685
Fixes: CVE-2025-0686
Fixes: CVE-2025-0689

Suggested-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Daniel Axtens
26db660503 fs/bfs: Disable under lockdown
The BFS is not fuzz-clean. Don't allow it to be loaded under lockdown.
This will also disable the AFS.

Fixes: CVE-2024-45778
Fixes: CVE-2024-45779

Reported-by: Nils Langius <nils@langius.de>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
B Horn
5f31164aed commands/hexdump: Disable memory reading in lockdown mode
Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
B Horn
340e4d058f commands/memrw: Disable memory reading in lockdown mode
With the rest of module being blocked in lockdown mode it does not make
a lot of sense to leave memory reading enabled. This also goes in par
with disabling the dump command.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
B Horn
34824806ac commands/minicmd: Block the dump command in lockdown mode
The dump enables a user to read memory which should not be possible
in lockdown mode.

Fixes: CVE-2025-1118

Reported-by: B Horn <b@horn.uk>
Reported-by: Jonathan Bar Or <jonathanbaror@gmail.com>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Lidong Chen
c68b7d2362 commands/test: Stack overflow due to unlimited recursion depth
The test_parse() evaluates test expression recursively. Due to lack of
recursion depth check a specially crafted expression may cause a stack
overflow. The recursion is only triggered by the parentheses usage and
it can be unlimited. However, sensible expressions are unlikely to
contain more than a few parentheses. So, this patch limits the recursion
depth to 100, which should be sufficient.

Reported-by: Nils Langius <nils@langius.de>
Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Jonathan Bar Or
dad8f50297 commands/read: Fix an integer overflow when supplying more than 2^31 characters
The grub_getline() function currently has a signed integer variable "i"
that can be overflown when user supplies more than 2^31 characters.
It results in a memory corruption of the allocated line buffer as well
as supplying large negative values to grub_realloc().

Fixes: CVE-2025-0690

Reported-by: Jonathan Bar Or <jonathanbaror@gmail.com>
Signed-off-by: Jonathan Bar Or <jonathanbaror@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:56 +01:00
Lidong Chen
b970a5ed96 gettext: Integer overflow leads to heap OOB write
The size calculation of the translation buffer in
grub_gettext_getstr_from_position() may overflow
to 0 leading to heap OOB write. This patch fixes
the issue by using grub_add() and checking for
an overflow.

Fixes: CVE-2024-45777

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>
2025-02-13 15:45:56 +01:00
Lidong Chen
09bd6eb58b gettext: Integer overflow leads to heap OOB write or read
Calculation of ctx->grub_gettext_msg_list size in grub_mofile_open() may
overflow leading to subsequent OOB write or read. This patch fixes the
issue by replacing grub_zalloc() and explicit multiplication with
grub_calloc() which does the same thing in safe manner.

Fixes: CVE-2024-45776

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>
2025-02-13 15:45:55 +01:00
B Horn
7580addfc8 gettext: Remove variables hooks on module unload
The gettext module does not entirely cleanup after itself in
its GRUB_MOD_FINI() leaving a few variables hooks in place.
It is not possible to unload gettext module because normal
module depends on it. Though fix the issues for completeness.

Fixes: CVE-2025-0622

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
9c16197734 normal: Remove variables hooks on module unload
The normal module does not entirely cleanup after itself in
its GRUB_MOD_FINI() leaving a few variables hooks in place.
It is not possible to unload normal module now but fix the
issues for completeness.

On the occasion replace 0s with NULLs for "pager" variable
hooks unregister.

Fixes: CVE-2025-0622

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
2123c5bca7 commands/pgp: Unregister the "check_signatures" hooks on module unload
If the hooks are not removed they can be called after the module has
been unloaded leading to an use-after-free.

Fixes: CVE-2025-0622

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
0bf56bce47 commands/ls: Fix NULL dereference
The grub_strrchr() may return NULL when the dirname do not contain "/".
This can happen on broken filesystems.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
Lidong Chen
05be856a8c commands/extcmd: Missing check for failed allocation
The grub_extcmd_dispatcher() calls grub_arg_list_alloc() to allocate
a grub_arg_list struct but it does not verify the allocation was successful.
In case of failed allocation the NULL state pointer can be accessed in
parse_option() through grub_arg_parse() which may lead to a security issue.

Fixes: CVE-2024-45775

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>
2025-02-13 15:45:55 +01:00
B Horn
98ad84328d kern/dl: Check for the SHF_INFO_LINK flag in grub_dl_relocate_symbols()
The grub_dl_relocate_symbols() iterates through the sections in
an ELF looking for relocation sections. According to the spec [1]
the SHF_INFO_LINK flag should be set if the sh_info field is meant
to be a section index.

[1] https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
d72208423d kern/dl: Use correct segment in grub_dl_set_mem_attrs()
The previous code would never actually call grub_update_mem_attrs()
as sh_info will always be zero for the sections that exist in memory.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
500e5fdd82 kern/dl: Fix for an integer overflow in grub_dl_ref()
It was possible to overflow the value of mod->ref_count, a signed
integer, by repeatedly invoking insmod on an already loaded module.
This led to a use-after-free. As once ref_count was overflowed it became
possible to unload the module while there was still references to it.

This resolves the issue by using grub_add() to check if the ref_count
will overflow and then stops further increments. Further changes were
also made to grub_dl_unref() to check for the underflow condition and
the reference count was changed to an unsigned 64-bit integer.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
Daniel Axtens
2c34af908e video/readers/jpeg: Do not permit duplicate SOF0 markers in JPEG
Otherwise a subsequent header could change the height and width
allowing future OOB writes.

Fixes: CVE-2024-45774

Reported-by: Nils Langius <nils@langius.de>
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
0707accab1 net/tftp: Fix stack buffer overflow in tftp_open()
An overly long filename can be passed to tftp_open() which would cause
grub_normalize_filename() to write out of bounds.

Fixed by adding an extra argument to grub_normalize_filename() for the
space available, making it act closer to a strlcpy(). As several fixed
strings are strcpy()'d after into the same buffer, their total length is
checked to see if they exceed the remaining space in the buffer. If so,
return an error.

On the occasion simplify code a bit by removing unneeded rrqlen zeroing.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:45:55 +01:00
B Horn
5eef881528 net: Fix OOB write in grub_net_search_config_file()
The function included a call to grub_strcpy() which copied data from an
environment variable to a buffer allocated in grub_cmd_normal(). The
grub_cmd_normal() didn't consider the length of the environment variable.
So, the copy operation could exceed the allocation and lead to an OOB
write. Fix the issue by replacing grub_strcpy() with grub_strlcpy() and
pass the underlying buffers size to the grub_net_search_config_file().

Fixes: CVE-2025-0624

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-02-13 15:44:58 +01:00
B Horn
aa8b4d7fac net: Remove variables hooks when interface is unregisted
The grub_net_network_level_interface_unregister(), previously
implemented in a header, did not remove the variables hooks that
were registered in grub_net_network_level_interface_register().
Fix this by implementing the same logic used to register the
variables and move the function into the grub-core/net/net.c.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:48 +01:00
B Horn
a1dd8e59da net: Unregister net_default_ip and net_default_mac variables hooks on unload
The net module is a dependency of normal. So, it shouldn't be possible
to unload the net. Though unregister variables hooks as a precaution.
It also gets in line with unregistering the other net module hooks.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:48 +01:00
B Horn
d8a937ccae script/execute: Limit the recursion depth
If unbounded recursion is allowed it becomes possible to collide the
stack with the heap. As UEFI firmware often lacks guard pages this
becomes an exploitable issue as it is possible in some cases to do
a controlled overwrite of a section of this heap region with
arbitrary data.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
8a7103fddf kern/partition: Limit recursion in part_iterate()
The part_iterate() is used by grub_partition_iterate() as a callback in
the partition iterate functions. However, part_iterate() may also call
the partition iterate functions which may lead to recursion. Fix potential
issue by limiting the recursion depth.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
18212f0648 kern/disk: Limit recursion depth
The grub_disk_read() may trigger other disk reads, e.g. via loopbacks.
This may lead to very deep recursion which can corrupt the heap. So, fix
the issue by limiting reads depth.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
67f70f70a3 disk/loopback: Reference tracking for the loopback
It was possible to delete a loopback while there were still references
to it. This led to an exploitable use-after-free.

Fixed by implementing a reference counting in the grub_loopback struct.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
Michael Chang
13febd78db disk/cryptodisk: Require authentication after TPM unlock for CLI access
The GRUB may use TPM to verify the integrity of boot components and the
result can determine whether a previously sealed key can be released. If
everything checks out, showing nothing has been tampered with, the key
is released and GRUB unlocks the encrypted root partition for the next
stage of booting.

However, the liberal Command Line Interface (CLI) can be misused by
anyone in this case to access files in the encrypted partition one way
or another. Despite efforts to keep the CLI secure by preventing utility
command output from leaking file content, many techniques in the wild
could still be used to exploit the CLI, enabling attacks or learning
methods to attack. It's nearly impossible to account for all scenarios
where a hack could be applied.

Therefore, to mitigate potential misuse of the CLI after the root device
has been successfully unlocked via TPM, the user should be required to
authenticate using the LUKS password. This added layer of security
ensures that only authorized users can access the CLI reducing the risk
of exploitation or unauthorized access to the encrypted partition.

Fixes: CVE-2024-49504

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
16f196874f kern/file: Implement filesystem reference counting
The grub_file_open() and grub_file_close() should be the only places
that allow a reference to a filesystem to stay open. So, add grub_dl_t
to grub_fs_t and set this in the GRUB_MOD_INIT() for each filesystem to
avoid issues when filesystems forget to do it themselves or do not track
their own references, e.g. squash4.

The fs_label(), fs_uuid(), fs_mtime() and fs_read() should all ref and
unref in the same function but it is essentially redundant in GRUB
single threaded model.

Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
a791068729 kern/file: Ensure file->data is set
This is to avoid a generic issue were some filesystems would not set
data and also not set a grub_errno. This meant it was possible for many
filesystems to grub_dl_unref() themselves multiple times resulting in
it being possible to unload the filesystems while there were still
references to them, e.g., via a loopback.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
B Horn
d1d6b7ea58 fs/xfs: Ensuring failing to mount sets a grub_errno
It was previously possible for grub_xfs_mount() to return NULL without
setting grub_errno if the XFS version was invalid. This resulted in it
being possible for grub_dl_unref() to be called twice allowing the XFS
module to be unloaded while there were still references to it.

Fixing this problem in general by ensuring a grub_errno is set if the
fail label is reached.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2025-01-23 16:22:47 +01:00
Michael Chang
6ccc77b59d 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>
2025-01-23 16:22:47 +01:00
B Horn
067b6d225d 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>
2025-01-23 16:22:47 +01:00
B Horn
048777bc29 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>
2025-01-23 16:22:46 +01:00
B Horn
237a71184a 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>
2025-01-23 16:22:46 +01:00
Michael Chang
aff2631878 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>
2025-01-23 16:22:46 +01:00
Michael Chang
7e2f750f0a 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>
2025-01-23 16:22:46 +01:00
Lidong Chen
edd995a26e 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>
2025-01-23 16:22:46 +01:00
Lidong Chen
bd999310fe 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>
2025-01-23 16:22:46 +01:00
Lidong Chen
ab09fd0531 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>
2025-01-23 16:22:46 +01:00
Lidong Chen
66175696f3 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>
2025-01-23 16:22:46 +01:00
Michael Chang
1443833a95 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>
2025-01-23 16:22:46 +01:00
B Horn
965db59708 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>
2025-01-23 16:22:46 +01:00
B Horn
f7c070a2e2 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>
2025-01-23 16:22:46 +01:00
B Horn
563436258c 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>
2025-01-23 16:22:46 +01:00
Lidong Chen
0087bc6902 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>
2025-01-23 16:15:31 +01:00
B Horn
2c8ac08c99 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>
2025-01-16 15:07:22 +01:00
B Horn
417547c104 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>
2025-01-16 15:05:23 +01:00
B Horn
c1a291b01f 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>
2024-12-02 17:32:51 +01:00
B Horn
ea703528a8 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>
2024-12-02 17:32:51 +01:00
Stefan Berger
6811f6f09d 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>
2024-11-28 23:02:24 +01:00
Stefan Berger
ff14b89bda 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>
2024-11-28 22:57:09 +01:00
Stefan Berger
72092a8641 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>
2024-11-28 22:37:50 +01:00
Stefan Berger
8c0b5f2003 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>
2024-11-28 22:21:41 +01:00
Stefan Berger
7344b3c7ce 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>
2024-11-28 22:15:29 +01:00
Stefan Berger
29d1bd2a96 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>
2024-11-28 22:11:30 +01:00
Stefan Berger
99ee68a014 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>
2024-11-28 22:09:51 +01:00
Gary Lin
3770a69050 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>
2024-11-28 21:50:56 +01:00
Gary Lin
f898440cc1 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>
2024-11-28 21:50:56 +01:00
Gary Lin
76a2bcb997 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>
2024-11-28 21:50:56 +01:00
Gary Lin
135e0bc886 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>
2024-11-28 21:50:56 +01:00
Gary Lin
b35480b48e 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>
2024-11-28 21:50:56 +01:00
Patrick Colp
6abf8af3c5 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>
2024-11-28 21:50:55 +01:00
Patrick Colp
fba3a474e0 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>
2024-11-28 21:50:55 +01:00
Gary Lin
550ada7d67 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>
2024-11-28 21:50:55 +01:00
Hernan Gatta
5f6a2fd513 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>
2024-11-28 21:50:55 +01:00
Hernan Gatta
ad0c52784a 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>
2024-11-28 21:50:55 +01:00
Hernan Gatta
48e230c317 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>
2024-11-28 21:50:55 +01:00
Gary Lin
35c9904df4 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>
2024-11-28 21:50:55 +01:00
Gary Lin
63a78f4b4d 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>
2024-11-28 21:50:55 +01:00
Gary Lin
2ad159d9b3 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>
2024-11-28 21:50:55 +01:00
Hernan Gatta
5d260302da 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>
2024-11-28 21:50:55 +01:00
Gary Lin
3d60732f9f 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>
2024-11-28 21:50:54 +01:00
Daniel Axtens
99cda67889 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>
2024-11-28 21:50:54 +01:00
Daniel Axtens
504058e82a 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>
2024-11-28 21:50:54 +01:00
Gary Lin
8a0fedef21 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>
2024-11-28 21:50:54 +01:00
Gary Lin
66cf4cb144 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>
2024-11-28 21:50:54 +01:00
Gary Lin
0d0913fc61 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>
2024-11-28 21:50:54 +01:00
Gary Lin
2e93a8e4bd 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>
2024-11-28 21:50:54 +01:00
Gary Lin
b7568e3358 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>
2024-11-28 21:50:54 +01:00
Gary Lin
d60a04baef 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>
2024-11-28 21:50:54 +01:00
Gary Lin
54e0e19a29 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>
2024-11-28 21:50:54 +01:00
Gary Lin
0ad1d4ba86 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>
2024-11-28 21:50:54 +01:00
Gary Lin
4160ca9839 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>
2024-11-28 21:50:54 +01:00
Gary Lin
8f56e5e5cf 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>
2024-11-28 21:50:53 +01:00
Gary Lin
d86df91cbe 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>
2024-11-28 21:50:53 +01:00
Gary Lin
32fdfe6008 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>
2024-11-28 21:50:53 +01:00
Gary Lin
fa498af7b9 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>
2024-11-28 21:50:53 +01:00
Daniel Axtens
9a26abbc36 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>
2024-11-28 21:50:53 +01:00
Daniel Axtens
c85c2b9f5f 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>
2024-11-28 21:50:17 +01:00
Rasmus Villemoes
4f6c460917 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>
2024-11-28 20:23:15 +01:00
Glenn Washburn
792132c72a 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>
2024-11-28 19:55:34 +01:00
Andrew Hamilton
1763d83f54 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>
2024-10-31 16:50:01 +01:00
Andrew Hamilton
097fd9d9a1 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>
2024-10-31 16:40:41 +01:00
Andrew Hamilton
f48e6af117 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>
2024-10-31 16:36:25 +01:00
Valentin Gehrke
dd743ba42d 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>
2024-10-31 16:29:01 +01:00
Sudhakar Kuppusamy
9a9082b501 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>
2024-10-31 16:18:36 +01:00
Sudhakar Kuppusamy
f97d4618a5 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>
2024-10-31 16:13:06 +01:00
Leo Sandoval
f26b39860d 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>
2024-10-31 16:08:36 +01:00
Daniel Kiper
337cb24862 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>
2024-10-31 16:07:03 +01:00
Benjamin Herrenschmidt
31de991dee 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>
2024-10-31 16:04:00 +01:00
Mate Kukri
f5bb766e68 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>
2024-10-11 14:45:00 +02:00
Mate Kukri
94649c0267 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>
2024-10-11 14:43:03 +02:00
Mate Kukri
09ca66673a 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>
2024-10-11 14:35:54 +02:00
Mate Kukri
9fb80dd57e 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>
2024-10-11 14:34:28 +02:00
Peter Jones
6e2fe134ef 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>
2024-10-10 13:23:19 +02:00
Peter Jones
2b79d550f3 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>
2024-10-10 13:18:00 +02:00
Peter Jones
246c82cdae 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>
2024-10-10 13:15:17 +02:00
Daniel Kiper
616adeb80b 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>
2024-10-10 13:11:23 +02:00
Daniel Kiper
95a7bfef5d 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>
2024-10-10 13:10:10 +02:00
Daniel Kiper
1b1061409d 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>
2024-10-10 13:09:06 +02:00
Daniel Kiper
929fafdf5e 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>
2024-10-10 13:08:31 +02:00
Daniel Kiper
d96cfd7bf8 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>
2024-10-10 13:06:34 +02:00
Michael Chang
86ec48882b 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>
2024-10-10 13:04:37 +02:00
Michael Chang
3808b1a9bd 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>
2024-10-10 12:38:47 +02:00
Michael Chang
e5f047be05 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>
2024-10-10 12:36:56 +02:00
Heinrich Schuchardt
c5ae124e11 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>
2024-10-10 12:13:37 +02:00
Frediano Ziglio
9c34d56c2d 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>
2024-09-05 19:12:08 +02:00
Frediano Ziglio
92bed41bf8 loader/efi/linux: Reuse len variable
Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-09-05 19:05:32 +02:00
Frediano Ziglio
33cb8aecdd 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>
2024-09-05 17:58:56 +02:00
Frediano Ziglio
77cd623dee 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>
2024-09-05 17:49:19 +02:00
Frediano Ziglio
95145eea5e 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>
2024-09-05 17:46:54 +02:00
Vladimir Serbinenko
d333e8bb37 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>
2024-09-05 17:36:17 +02:00
Vladimir Serbinenko
34b7f37212 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>
2024-09-05 17:29:35 +02:00
Vladimir Serbinenko
ed06516738 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>
2024-09-05 17:25:27 +02:00
Vladimir Serbinenko
648f2d16c0 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>
2024-09-05 17:22:47 +02:00
Vladimir Serbinenko
f0710d2d8f 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>
2024-09-05 17:12:06 +02:00
Vladimir Serbinenko
e61157bbd2 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>
2024-09-05 17:08:38 +02:00
Vladimir Serbinenko
5313fa8394 configure: Look for .otf fonts
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-09-05 17:05:24 +02:00
Mate Kukri
33b94f2a9b 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>
2024-09-05 16:18:02 +02:00
Andrew Hamilton
ab1e6fc044 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>
2024-09-05 16:13:27 +02:00
Michael Chang
9537f4403d commands/bli: Fix crash in get_part_uuid()
The get_part_uuid() function made an assumption that the target GRUB
device is a partition device and accessed device->disk->partition
without checking for NULL. There are four situations where this
assumption is problematic:

1. The device is a net device instead of a disk.
2. The device is an abstraction device, like LVM, RAID, or CRYPTO, which
   is mostly logical "disk" ((lvmid/<UUID>) and so on).
3. Firmware RAID may present the ESP to GRUB as an EFI disk (hd0) device
   if it is contained within a Linux software RAID.
4. When booting from a CD-ROM, the ESP is a VFAT image indexed by the El
   Torito boot catalog. The boot device is set to (cd0), corresponding
   to the CD-ROM image mounted as an ISO 9660 filesystem.

As a result, get_part_uuid() could lead to a NULL pointer dereference
and trigger a synchronous exception during boot if the ESP falls into
one of these categories. This patch fixes the problem by adding the
necessary checks to handle cases where the ESP is not a partition device.

Additionally, to avoid disrupting the boot process, this patch relaxes
the severity of the errors in this context to non-critical. Errors will
be logged, but they will not prevent the boot process from continuing.

Fixes: e0fa7dc84 (bli: Add a module for the Boot Loader Interface)

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-By: Oliver Steffen <osteffen@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-09-05 16:08:17 +02:00
Thomas Schmitt
b53ec06a1d util/grub-mkrescue: Check existence of option arguments
As reported by Victoriia Egorova in bug 65880, grub-mkrescue does not
verify that the expected argument of an option like -d or -k does really
exist in argv. So, check the loop counter before incrementing it inside
the loop which copies argv to argp_argv. Issue an error message similar
to what older versions of grub-mkrescue did with a missing argument,
e.g. 2.02.

Fixes: https://savannah.gnu.org/bugs/index.php?65880

Signed-off-by: Thomas Schmitt <scdbackup@gmx.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 19:31:35 +02:00
Tobias Heider
ab9fe80300 loader/efi/fdt: Add fdtdump command to access device tree
The fdtdump command allows dumping arbitrary device tree properties
and saving them to a variable similar to the smbios command.

This is useful in scripts where further actions such as selecting
a kernel or loading another device tree depend on the compatible
or model values of the device tree provided by the firmware.

For now only the root level properties of the dtb are exposed.

Signed-off-by: Tobias Heider <tobias.heider@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 19:21:02 +02:00
Vladimir Serbinenko
0cfec355d0 osdep/devmapper/getroot: Unmark 2 strings for translation
First they're use macros so they can't be translated as-is.
Second there is no point in translating them as they're too technical.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 19:14:41 +02:00
Vladimir Serbinenko
f171122f03 loader/emu/linux: Fix determination of program name
Current code works only if package matches binary name transformation rules.
It's often true but is not guaranteed.

Fixes: https://savannah.gnu.org/bugs/?64410

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 19:11:48 +02:00
Vladimir Serbinenko
828717833f disk/cryptodisk: Fix translatable message
Fixes: https://savannah.gnu.org/bugs/?64408

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:51:52 +02:00
Vladimir Serbinenko
9a2134a70f tests: Add test for ZFS zstd
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:47:54 +02:00
Vladimir Serbinenko
f96df6fe9f fs/zfs/zfs: Add support for zstd compression
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:43:23 +02:00
Mate Kukri
55d35d6283 kern/efi/mm: Detect calls to grub_efi_drop_alloc() with wrong page counts
Silently keeping entries in the list if the address matches, but the
page count doesn't is a bad idea, and can lead to double frees.

grub_efi_free_pages() have already freed parts of this block by this
point, and thus keeping the whole block in the list and freeing it again
at exit can lead to double frees.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:34:38 +02:00
Mate Kukri
61f1d0a612 kern/efi/mm: Change grub_efi_allocate_pages_real() to call semantically correct free function
If the firmware happens to return 0 as an address of allocated pages,
grub_efi_allocate_pages_real() tries to allocate a new set of pages,
and then free the ones at address 0.

However at that point grub_efi_store_alloc() wasn't yet called, so
freeing the pages at 0 using grub_efi_free_pages() which calls
grub_efi_drop_alloc() isn't necessary, so let's call b->free_pages()
instead.

The call to grub_efi_drop_alloc() doesn't seem particularly harmful,
because it seems to do nothing if the allocation it is asked to drop
isn't on the list, but the call to it is obviously unnecessary here.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:33:16 +02:00
Mate Kukri
dc0a3a27d6 kern/efi/mm: Change grub_efi_mm_add_regions() to keep track of map allocation size
If the map was too big for the initial allocation, it was freed and replaced
with a bigger one, but the free call still used the hard-coded size.

Seems like this wasn't hit for a long time, because most firmware maps
fit into 12K.

This bug was triggered on Project Mu firmware with a big memory map, and
results in the heap getting trashed and the firmware ASSERTING on
corrupted heap guard values when GRUB exits.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:30:32 +02:00
Yifan Zhao
b990df0bef tests/util/grub-fs-tester: Fix EROFS label tests in grub-fs-tester
mkfs.erofs with version < 1.6 does not support the -L option.
Let's detect the version of mkfs.erofs and skip the label tests
if it is not supported.

Suggested-by: Glenn Washburn <development@efficientek.com>
Signed-off-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:23:08 +02:00
Glenn Washburn
d41c64811d tests: Switch to requiring exfatprogs from exfat-utils
The current Debian stable, now 12, has dropped the exfat-utils package
that the exfat filesystem test requires to run. There is an exfatprogs
package that replaces exfat-utils, though it is not a drop-in replacement
because mkfs.exfat has differing command line option names. Note, that
we're not yet switching to using the exfat kernel module because this
allows the testings on kernels that do not have the module.

Update mkfs.exfat usage to adhere to the different exfatprogs usage. Also,
the exfatprogs mkfs.exfat, following the exfat specification more closely,
only allows a maximum of 22 bytes of UTF-16 characters in the volume label
compared to 30 bytes from exfat-utils. So the exfat label test is updated
accordingly.

Update documentation to note that exfatprogs is now needed and also
exfat-fuse, which is needed do the fuse mount.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:19:12 +02:00
Glenn Washburn
c1ee4da6a9 tests/util/grub-shell-luks-tester: Fix detached header test getting wrong header path
When $detached_header was set 1, $luksdiskfile was set to the LUKS header
file path with "${detached_header:-$luksfile}" appended, which evaluates
to "1". Fix this by using two statements to set $luksdiskfile. The first
sets it to the header file if $detached_header is set, otherwise leave it
unset. The second statement sets it to itself if it is already set,
otherwise it is set to $luksfile.

Fixes: a7b540e6e (tests: Add cryptomount functional test)

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:14:20 +02:00
Glenn Washburn
c22e052fe2 tests/util/grub-shell: Add flexibility in QEMU firmware handling
First look for firmware files in the source directory and then, if not
found, look for them in locations where Debian installs them. Prefer to
use the unified firmware file and, if not found, use the pflash firmware
files split in to code and variables. By looking for files in the source
directory first, system firmware files can be overridden and it can be
ensured that the tests can be run regardless of the distro or where the
system firmware files are stored. If no firmware files are found, print
an error message and exit with error.

If a firmware VARS file is found, use it with snapshot mode enabled, which
makes the VARS writable to the virtual machine, but does not write back
the changes to the file. This allows using the readonly system VARS file
without copying it or using it in readonly mode, which causes the ARM
machine to fail. This also gives tests effectively their own ephemeral VARS
file that can be written to without causing side-effects for other tests.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:11:32 +02:00
Glenn Washburn
d2fc9dfcd1 tests/util/grub-shell: Use pflash instead of -bios to load UEFI firmware
According to the OVMF whitepaper [1]:

  IMPORTANT: Never pass OVMF.fd to qemu with the -bios option. That option
  maps the firmware image as ROM into the guest's address space, and forces
  OVMF to emulate non-volatile variables with a fallback driver that is
  bound to have insufficient and confusing semantics.

Use the pflash interface instead. Currently the unified firmware file is
used, which contains both firmware code and variable sections. By enabling
snapshot on the pflash device, the firmware can be loaded in such a way
that variables can be written to without writing to the backing file.

Since pflash does no searching for firmware paths that are not absolute,
unlike the -bios option, also make firmware paths absolute. Additionally,
update the previous firmware paths or file names that did not correspond to
ones installed by Debian.

Use the q35 machine, instead of the default i440fx, for i386-efi because
the default machine type does not emulate a flash device, which is now
needed to load the firmware.

[1] http://www.linux-kvm.org/downloads/lersek/ovmf-whitepaper-c770f8c.txt

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:10:28 +02:00
Glenn Washburn
88a7e64c2c tests/util/grub-shell: Print gdbinfo if on EFI platform
Allow using GDB to debug a failing QEMU test. This output does not cause
issues for tests because it happens before the trim line, and so will be
ignored.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:05:35 +02:00
Glenn Washburn
b8d29f1146 configure: Add Debian/Ubuntu DejaVu font path
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 15:00:51 +02:00
Udo Steinberg
13b315c0a5 term/ns8250-spcr: Add one more 16550 debug type
Type 0x01 was introduced with the ACPI DBGP table and type 0x12 was introduced
with the ACPI DBG2 table. Type 0x12 is used by the ACPI SPCR table on recent
AWS bare-metal instances (c6i/c7i). Also give each debug type a proper name.

Signed-off-by: Udo Steinberg <udo@hypervisor.org>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 14:58:29 +02:00
Vladimir Serbinenko
8abec8e153 loader/i386/multiboot_mbi: Fix handling of errors in broken aout-kludge
Current code in some codepaths neither discards nor reports errors.
Properly surface the error.

While on it split 2 cases of unrelated variables both named err.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 14:46:23 +02:00
Michael Chang
d35ff22516 net/drivers/ieee1275/ofnet: Remove 200 ms timeout in get_card_packet() to reduce input latency
When GRUB image is netbooted on ppc64le, the keyboard input exhibits
significant latency, reports even say that characters are processed
about once per second. This issue makes interactively trying to debug
a ppc64le config very difficult.

It seems that the latency is largely caused by a 200 ms timeout in the
idle event loop, during which the network card interface is consistently
polled for incoming packets. Often, no packets arrive during this
period, so the timeout nearly always expires, which blocks the response
to key inputs.

Furthermore, this 200 ms timeout might not need to be enforced at this
basic layer, considering that GRUB performs synchronous reads and its
timeout management is actually handled by higher layers, not directly in
the card instance. Additionally, the idle polling, which reacts to
unsolicited packets like ICMP and SLAAC, would be fine at a less frequent
polling interval, rather than needing a timeout for receiving a response.

For these reasons, we believe the timeout in get_card_packet() should be
effectively removed. According to test results, the delay has disappeared,
and it is now much easier to use interactively.

Signed-Off-by: Michael Chang <mchang@suse.com>
Tested-by: Tony Jones <tonyj@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-20 14:44:10 +02:00
Hector Cao
86df79275d commands/efi/tpm: Re-enable measurements on confidential computing platforms
The measurements for confidential computing has been introduced in the
commit 4c76565b6 (efi/tpm: Add EFI_CC_MEASUREMENT_PROTOCOL support).
Recently the patch 30708dfe3 (tpm: Disable the tpm verifier if the TPM
device is not present) has been introduced to optimize the memory usage
when a TPM device is not available on platforms. This fix prevents the
tpm module to be loaded on confidential computing platforms, e.g. Intel
machines with TDX enabled, where the TPM device is not available.

In this patch, we propose to load the tpm module for this use case by
generalizing the tpm feature detection in order to cover CC platforms.
Basically, we do it by detecting the availability of the
EFI_CC_MEASUREMENT_PROTOCOL EFI protocol.

Fixes: https://savannah.gnu.org/bugs/?65821
Fixes: 30708dfe3 (tpm: Disable the tpm verifier if the TPM device is not present)

Signed-off-by: Hector Cao <hector.cao@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
2024-06-06 16:55:16 +02:00
Tianjia Zhang
0b4d01794a util/grub-mkpasswd-pbkdf2: Simplify the main function implementation
Allocate memory if needed, while saving the corresponding release
operation, reducing the amount of code and code complexity.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:16 +02:00
Avnish Chouhan
fa36f63760 kern/ieee1275/init: Add IEEE 1275 Radix support for KVM on Power
This patch adds support for Radix, Xive and Radix_gtse in Options
vector5 which is required for KVM LPARs. KVM LPARs ONLY support
Radix and not the Hash. Not enabling Radix on any PowerVM KVM LPARs
will result in boot failure.

Signed-off-by: Avnish Chouhan <avnish@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:16 +02:00
Vladimir Serbinenko
c464f1ec34 fs/zfs/zfs: Mark vdev_zaps_v2 and head_errlog as supported
We don't need any actual adjustments as we don't use the affected structures.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:16 +02:00
Vladimir Serbinenko
2ffc14ba95 types: Add missing casts in compile-time byteswaps
Without them, e.g., 0x80LL on 64-bit target is 32-bit byte-swapped to
0xffffffff80000000 instead of correct 0x80000000.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:16 +02:00
Vladimir Serbinenko
c6ac491204 font: Add Fedora-specific font paths
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:16 +02:00
Vladimir Serbinenko
5e8989e4ed fs/bfs: Fix improper grub_free() on non-existing files
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:15 +02:00
Daniel Axtens
c806e4dc88 io/gzio: Properly init a table
ARRAY_SIZE() is the count of elements, but the element size is 4 bytes, so
this was only initing the first 1/4th of the table. Detected with valgrind.

This should only matter in error paths, and I've not been able to identify
any actual misbehaviour that results from reading in-bounds but uninited data.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:15 +02:00
Daniel Axtens
243682baaa io/gzio: Abort early when get_byte() reads nothing
This isn't intended to be a functional change, but it makes a lot of failures a lot
faster, which is extremely helpful for fuzzing.

Without this change, we keep trying and trying to read more bytes into our buffer,
never being able to (read always returns 0) and so we just return old buffer contents
over and over until the decompression process fails some other way.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:55:15 +02:00
Alec Brown
bb65d81fe3 cli_lock: Add build option to block command line interface
Add functionality to disable command line interface access and editing of GRUB
menu entries if GRUB image is built with --disable-cli.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-06-06 16:54:10 +02:00
Yifan Zhao
56e58828cf fs/erofs: Add tests for EROFS in grub-fs-tester
This patch introduces three EROFS tests which cover compact, extended
and chunk-based inodes respectively.

Signed-off-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-23 15:31:12 +02:00
Yifan Zhao
9d603061aa fs/erofs: Add support for the EROFS
The EROFS [1] is a lightweight read-only filesystem designed for performance
which has already been shipped in most Linux distributions as well as widely
used in several scenarios, such as Android system partitions, container
images and rootfs for embedded devices.

This patch brings in the EROFS uncompressed support. Now, it's possible to
boot directly through GRUB with an EROFS rootfs.

Support for the EROFS compressed files will be added later.

[1] https://erofs.docs.kernel.org

Signed-off-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Tested-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-23 15:30:29 +02:00
Gao Xiang
1ba39de62f safemath: Add ALIGN_UP_OVF() which checks for an overflow
The following EROFS patch will use this helper to handle
ALIGN_UP() overflow.

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-23 15:19:06 +02:00
Jonathan Davies
d291449ba3 docs: Fix spelling mistakes
Signed-off-by: Jonathan Davies <jonathan.davies@nutanix.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-23 15:03:26 +02:00
Pascal Hambourg
6cc2e4481b util/grub.d/00_header.in: Quote background image pathname in output
This is required if the pathname contains spaces or GRUB shell
metacharacters else the generated config file check will fail.

Signed-off-by: Pascal Hambourg <pascal@plouf.fr.eu.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-23 14:57:37 +02:00
Rogier
f456add5f4 disk/lvm: GRUB fails to detect LVM volumes due to an incorrect computation of mda_end
When handling a regular LVM volume, GRUB can fail with the message:

  error: disk `lvmid/******-****-****-****-****-****-****/******-****-****-****-****-****-******' not found.

If the condition which triggers this exists, grub-probe will report the
error mentioned above. Similarly, the GRUB boot code will fail to detect
LVM volumes, resulting in a failure to boot off of LVM disks/partitions.
The condition can be created on any LVM VG by an LVM configuration change,
so any system with /boot on LVM can become unbootable at "any" time (after
any LVM configuration change).

The problem is caused by an incorrect computation of mda_end in disk/lvm.c,
when the metadata area wraps around. Apparently, this can start happening at
around 220 metadata changes to the VG.

Fixes: 879c4a834 (lvm: Fix two more potential data-dependent alloc overflows)
Fixes: https://savannah.gnu.org/bugs/?61620

Signed-off-by: Rogier <rogier777@gmail.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-By: Michael Chang <mchang@suse.com>
2024-05-23 14:42:39 +02:00
Forest
386b59ddb4 disk/cryptodisk: Allow user to retry failed passphrase
Give the user a chance to re-enter their cryptodisk passphrase after a typo,
rather than immediately failing (and likely dumping them into a GRUB shell).

By default, we allow 3 tries before giving up. A value in the
cryptodisk_passphrase_tries environment variable will override this default.

The user can give up early by entering an empty passphrase, just as they
could before this patch.

Signed-off-by: Forest <forestix@nom.one>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-09 15:30:31 +02:00
Lidong Chen
99b4c0c384 disk/mdraid1x_linux: Prevent infinite recursion
The test corpus for version-1 RAID generated an infinite recursion
in grub_partition_iterate() while attempting to read the superblock.
The reason for the issue was that the data region overlapped with
the superblock.

The infinite call loop looks like this:
  grub_partition_iterate() -> partmap->iterate() ->
    -> grub_disk_read() -> grub_disk_read_small() ->
    -> grub_disk_read_small_real() -> grub_diskfilter_read() ->
    -> read_lv() -> read_segment() -> grub_diskfilter_read_node() ->
    -> grub_disk_read() -> grub_disk_read_small() -> ...

The fix adds checks for both the superblock region and the data
region when parsing the superblock metadata in grub_mdraid_detect().

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-09 15:25:46 +02:00
Ard Biesheuvel
b272ed230e efi: Fix stack protector issues
The "ground truth" stack protector cookie value is kept in a global
variable, and loaded in every function prologue and epilogue to store
it into resp. compare it with the stack slot holding the cookie.

If the comparison fails, the program aborts, and this might occur
spuriously when the global variable changes values between the entry and
exit of a function. This implies that assigning the global variable at
boot should not involve any instrumented function calls, unless special
care is taken to ensure that the live call stack is synchronized, which
is non-trivial.

So avoid any function calls, including grub_memcpy(), which is
unnecessary given that the stack cookie is always a suitably aligned
variable of the native word size.

While at it, leave the last byte 0x0 to avoid inadvertent unbounded
strings on the stack.

Note that the use of __attribute__((optimize)) is described as
unsuitable for production use in the GCC documentation, so let's drop
this as well now that it is no longer needed.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-09 15:20:05 +02:00
Oliver Steffen
6744840b17 build: Track explicit module dependencies in Makefile.core.def
Add a new keyword, "depends", to the module definition syntax
used in Makefile.core.def. This allows specifying explicit module
dependencies together with the module definition.

Do not track the "extra_deps.lst" file in the repository anymore,
it is now auto-generated.

Make use of this new keyword in the bli module definition.

Signed-off-by: Oliver Steffen <osteffen@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-05-09 15:04:54 +02:00
Daniel Kiper
8719cc2040 windows: Add _stack_chk_guard/_stack_chk_fail symbols for Windows 64-bit target
Otherwise the GRUB cannot start due to missing symbols when stack
protector is enabled on EFI platforms.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
2024-04-11 15:48:26 +02:00
Gary Lin
0876fdf215 util/bash-completion: Fix for bash-completion 2.12
_split_longopt() was the bash-completion private API and removed since
bash-completion 2.12. This commit initializes the bash-completion
general variables with _init_completion() to avoid the potential
"command not found" error.

Although bash-completion 2.12 introduces _comp_initialize() to deprecate
_init_completion(), _init_completion() is still chosen for the better
backward compatibility.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:48:25 +02:00
Vladimir 'phcoder' Serbinenko
28c4405208 util/grub-fstest: Add a new command zfs-bootfs
It is useful to check zfs-bootfs command.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:48:25 +02:00
Vladimir 'phcoder' Serbinenko
52e039e00b efi: Enable CMOS on x86 EFI platforms
The CMOS actually exists on most EFI platforms and in some cases is used to
store useful data that makes it justifiable for GRUB to read/write it.

As for date and time keep using EFI API and not CMOS one.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:48:25 +02:00
Vladimir 'phcoder' Serbinenko
aa80270154 acpi: Mark MADT entries as packed
No alignment is guaranteed and in fact on my IA-64 SAPIC is aligned
to 4 bytes instead of 8 and causes a trap. It affects only rarely used
lsacpi command and so went unnoticed.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:48:25 +02:00
Michael Chang
39c927df66 gfxmenu/view: Resolve false grub_errno disrupting boot process
When enabling gfxmenu and choosing to boot the Xen hypervisor from its
menu, an error occurred:

  error: ../../grub-core/video/bitmap_scale.c:42:null src bitmap in grub_video_create_scaled.

The error is returned by grub_video_bitmap_create_scaled() when the
source pixmap is not there. The init_background() uses it to scale up
the background image so it can fully fit into the screen resolution.

However not all backgrounds are set by a image, i.e. the "desktop-image"
property of the theme file. Instead a color code may be used, for
example OpenSUSE's green background uses "desktop-color" property:

  desktop-color: "#0D202F"

So it is absolutely fine to call init_background() without a raw pixmap
if color code is used. A missing check has to be added to ensure the
grub_errno will not be erroneously set and gets in the way of ensuing
boot process.

The reason it happens sporadically is due to grub_errno is reset to
GRUB_ERR_NONE in other places if a function's error return can be
ignored. In particular this hunk in grub_gfxmenu_create_box() does the
majority of the reset of grub_errno returned by init_background(), but
the path may not be always chosen.

  grub_video_bitmap_load (&box->raw_pixmaps[i], path);
  grub_free (path);

  /* Ignore missing pixmaps.  */
  grub_errno = GRUB_ERR_NONE;

In any case, we cannot account on such random behavior and should only
return grub_errno if it is justified.

On the occasion move the grub_video_bitmap struct definition to the
beginning of the function.

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:48:25 +02:00
Jon DeVree
68dd65cfda fs/xfs: Handle non-continuous data blocks in directory extents
The directory extent list does not have to be a continuous list of data
blocks. When GRUB tries to read a non-existant member of the list,
grub_xfs_read_file() will return a block of zero'ed memory. Checking for
a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
this was handled as a subtle side effect of reading the (non-existant)
tail data structure. Since the block was zero'ed the computation of the
number of directory entries in the block would return 0 as well.

Fixes: 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2254370

Signed-off-by: Jon DeVree <nuxi@vault24.org>
Reviewed-By: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-04-11 15:46:44 +02:00
Julian Andres Klode
04d2a50f31 Revert "templates: Reinstate unused version comparison functions with warning"
We reinstated these functions before the 2.12 release with a warning
such that users upgrading to 2.12 who had custom scripts using them
would not get broken in the upgrade and agreed to remove them after
the 2.12 release. This removes them accordingly.

This reverts commit e7a831963 (templates: Reinstate unused version
comparison functions with warning).

Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-02-15 16:54:59 +01:00
Gary Lin
4380c2d8ad util/bash-completion: Load scripts on demand
There are two system directories for bash-completion scripts. One is
/usr/share/bash-completion/completions/ and the other is
/etc/bash_completion.d/. The "etc" scripts are loaded in advance and
for backward compatibility while the "usr" scripts are loaded on demand.
To load scripts on demand it requires a corresponding script for every
command. So, the main bash-completion script is split into several
subscripts for different "grub-*" commands. To share the code the real
completion functions are still implemented in "grub" and each
subscript sources "grub" and invokes the corresponding function.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-02-15 16:51:36 +01:00
Samuel Thibault
9e1b18fc17 util/grub.d/10_hurd.in: Find proper ld.so on 64-bit systems
The 64-bit ABI defines ld.so to be /lib/ld-x86-64.so.1.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:24:35 +01:00
Samuel Thibault
a8c0504515 osdep/hurd/getroot: Fix 64-bit build
The file_get_fs_options() takes a mach_msg_type_number_t, 32-bit,
not a size_t, 64-bit on 64-bit platforms.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:17:09 +01:00
Alec Brown
d89a2a6e57 loader/i386/multiboot_mbi: Clean up redundant code
In grub-core/loader/i386/multiboot_mbi.c, Coverity spotted redundant code where
the variable err was being set to GRUB_ERR_NONE and then being overwritten
later without being used. Since this is unnecessary, we can remove the code
that sets err to GRUB_ERR_NONE.

Fixes: CID 428877

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:13:33 +01:00
Alec Brown
db0d19dc5f osdep/unix/getroot: Clean up redundant code
In grub-core/osdep/unix/getroot.c, Coverity spotted redundant code where the
double pointer os_dev was being set to 0 and then being overwritten later
without being used. Since this is unnecessary, we can remove the code that
sets os_dev to 0.

Fixes: CID 428875

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:11:03 +01:00
Alec Brown
c8bf758757 fs/jfs: Clean up redundant code
In grub-core/fs/jfs.c, Coverity spotted redundant code where the pointer diro
was being set to 0 and then being overwritten later without being used. Since
this is unnecessary, we can remove the code that sets diro to 0.

Fixes: CID 428876

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:08:48 +01:00
Gary Lin
5a311d029f tests: Switch password quality check off for luks2 test
When adding/changing the password for the luks2 partition, cryptsetup
may reject the command due to the weak password. Since this is only for
testing, add "--force-password" to switch password quality check off to
avoid the unexpected failure.

Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2024-01-25 18:07:49 +01:00
Oskari Pirhonen
b835601c76 build: Include grub-core/extra_deps.lst in dist
Fixes build failure due to the extra_deps.lst file not existing in the
tarball. Found while trying to package GRUB 2.12 for Gentoo.

  make[3]: *** No rule to make target '/var/tmp/portage/sys-boot/grub-2.12/work/grub-2.12/grub-core/extra_deps.lst', needed by 'syminfo.lst'.  Stop.

Fixes: 89fbe0cac (grub-core/Makefile.am: Make path to extra_deps.lst relative to $(top_srcdir)/grub-core)
Fixes: 154dcb1ae (build: Allow explicit module dependencies)

Signed-off-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-22 16:07:36 +01:00
Daniel Kiper
8961305b4e Bump version to 2.13
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-20 17:25:46 +01:00
Daniel Kiper
5ca9db22e8 Release 2.12
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-20 16:54:46 +01:00
Glenn Washburn
477a0dbd5e efi: Add support for reproducible builds
Having randomly generated bytes in the binary output breaks reproducible
builds. Since build timestamps are usually the source of irreproducibility
there is a standard which defines an environment variable SOURCE_DATE_EPOCH
to be used when set for build timestamps. According to the standard [1], the
value of SOURCE_DATE_EPOCH is a base-10 integer of the number of seconds
since the UNIX epoch. Currently, this is a 10 digit number that fits into
32-bits, but will not shortly after the year 2100. So to be future-proof
only use the least significant 32-bits. On 64-bit architectures, where the
canary is also 64-bits, there is an extra 32-bits that can be filled to
provide more entropy. The first byte is NUL to filter out string buffer
overflow attacks and the remaining 24-bits are set to static random bytes.

[1] https://reproducible-builds.org/specs/source-date-epoch

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-20 14:36:18 +01:00
Glenn Washburn
dcc1af5d68 efi: Generate stack protector canary at build time if urandom is available
Generating the canary at build time allows the canary to be different for
every build which could limit the effectiveness of certain exploits.
Fallback to the statically generated random bytes if /dev/urandom is not
readable, e.g. Windows.

On 32-bit architectures, which use a 32-bit canary, reduce the canary to
4 bytes with one byte being NUL to filter out string buffer overflow attacks.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-20 14:31:50 +01:00
Glenn Washburn
e424e945c9 efi: Initialize canary to non-zero value
The canary, __stack_chk_guard, is in the BSS and so will get initialized to
zero if it is not explicitly initialized. If the UEFI firmware does not
support the RNG protocol, then the canary will not be randomized and will
be zero. This seems like a possibly easier value to write by an attacker.
Initialize canary to static random bytes, so that it is still random when
there is no RNG protocol. Set at least one byte to NUL to protect against
string buffer overflow attacks [1]. Code that writes NUL terminated strings
will terminate when a NUL is encountered in the input byte stream. So the
attacker will not be able to forge the canary by including it in the input
stream without terminating the string operation and thus limiting the
stack corruption.

[1] https://www.sans.org/blog/stack-canaries-gingerly-sidestepping-the-cage/

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-20 14:17:23 +01:00
Alec Brown
7c8ae7dcbd gfxmenu/gui_image: Fix double free of bitmap
In grub-core/gfxmenu/gui_image.c, Coverity detected a double free in the
function load_image(). The function checks if self->bitmap and self->raw_bitmap
aren't NULL and then frees them. In the case self->bitmap and self->raw_bitmap
are the same, only self->raw_bitmap is freed which would also free the memory
used by self->bitmap. However, in this case self->bitmap isn't being set to NULL
which could lead to a double free later in the code. After self->raw_bitmap is
freed, it gets set to the variable bitmap. If this variable is NULL, the code
could have a path that would free self->bitmap a second time in the function
rescale_image().

Fixes: CID 292472

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-14 19:29:34 +01:00
Qiumiao Zhang
63fc253fc9 commands/acpi: Fix calculation of ACPI tables addresses when processing RSDT and XSDT
According to the ACPI specification the XSDT Entry field contains an array
of 64-bit physical addresses which points to other DESCRIPTION_HEADERs. However,
the entry_ptr iterator is defined as a 32-bit pointer. It means each 64-bit
entry in the XSDT table is treated as two separate 32-bit entries then. Fix the
issue by using correct addresses sizes when processing RSDT and XSDT tables.

Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 14:21:21 +01:00
Vladimir Serbinenko
f20123072a libnvpair: Support prefixed nvlist symbol names as found on NetBSD
NetBSD uses slightly different function names for the same functions.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:30:33 +01:00
Vladimir Serbinenko
a13df3d15a bootstrap: Don't check gettext version
NetBSD gettext is older than the check but we don't actually need 0.18.3,
older one works fine. This is needed to make bootstrap work on NetBSD.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:25:34 +01:00
Vladimir Serbinenko
6d2aa7ee01 kern/mm: Use %x and cast for displaying sizeof()
There is some variance in how compiler treats sizeof() especially
on 32-bit platforms where it can be naturally either int or long.
Explicit cast solves the issue.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:18:42 +01:00
Vladimir Serbinenko
b3d49a697b configure: Add RPATH for freetype on NetBSD
Without this build-time mkfont fails dynamic linking. This is not ideal
but improves the situation until a better solution is available.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:07:03 +01:00
Vladimir Serbinenko
52dbf66ea4 configure: Add *BSD font paths
*BSD puts fonts in other places. Add them to the list.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:03:32 +01:00
Vladimir Serbinenko
2d6a899806 autogen: Accept python3.10 as a python alternative
NetBSD doesn't provide python or python3.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-13 13:00:00 +01:00
Vladimir Serbinenko
3d4cb5a432 build: Rename HAVE_LIBZFS to USE_LIBZFS
The HAVE_LIBZFS is defined by libzfs test and hence conflicts with
manual definition. On NetBSD it ends up detecting zfs but not detecting
nvpair and creates confusion. Split them.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 18:01:55 +01:00
Vladimir Serbinenko
e4dbe5cfa4 gnulib: Tolerate always_inline attribute being ignored
It's not critical, -Werror on it is inappropriate. We don't want to
modify gnulib too much. This warning is pretty much irrelevant.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:58:10 +01:00
Vladimir Serbinenko
31e47cfe2c util/editenv: Don't use %m formatter
It's not available on NetBSD outside of syslog. Using strerror() is more
reliable as we retrieve errno immediately rather than down the stack.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:55:14 +01:00
Vladimir Serbinenko
f5905f6566 osdep/bsd/hostdisk: Fix NetBSD compilation
Wrong function and variable name cause a stupid compilation error on
NetBSD and OpenBSD. Only NetBSD and OpenBSD use this file. No other
platform is affected.

Additionally, define RAW_FLOPPY_MAJOR constant if it is missing.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:55:14 +01:00
Vladimir Serbinenko
cb1824a871 osdep/generic/blocklist: Fix compilation
After recent change in blocklist types we have a type mismatch. Fixing it
requires a wrapper or large changes. I feel like wrapper makes more sense.

Without this patch we end up with a compilation problem and without wrapping
callback data is not passed properly anymore.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:53:56 +01:00
Vladimir Serbinenko
2f3faf02c4 disk/diskfilter: Remove unused variable
Variable e is set but never used. We can just remove it now.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:22:20 +01:00
Vladimir Serbinenko
3815acc57b build: Tolerate unused-but-set in generated lexer/bison files
We don't really control the small aspects of generated files and NetBSD
version has an unused variable that is then detected by gcc as warning
that is then promoted to error.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:18:18 +01:00
Vladimir Serbinenko
c129e44e7f loader/i386/bsdXX: Fix loading after unaligned module
Current code implicitly assumes that aligning chunk_size + *kern_end is
the same as aligning on curload which is not the case because
chunk_size starts at zero even if *kern_end is unaligned and ALIGN_PAGE
moved curload to an aligned position but not *kern_end + chunk_size.

This fixes booting of FreeBSD with zfs module.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:14:27 +01:00
Mate Kukri
89fbe0cac9 grub-core/Makefile.am: Make path to extra_deps.lst relative to $(top_srcdir)/grub-core
The commit 154dcb1ae (build: Allow explicit module dependencies) broke
out of tree builds by introducing the extra_deps.lst file into the
source tree but referencing it just by name in grub-core/Makefile.am.
Fix it by adding $(top_srcdir)/grub-core to the path.

Fixes: 154dcb1ae (build: Allow explicit module dependencies)

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 17:06:13 +01:00
Mate Kukri
353beb80c0 util/grub-install: Move platdir path canonicalization after files were copied to grubdir
The commit 3f9eace2d (util/grub-install: Delay copying files to
{grubdir,platdir} after install_device was validated) delaying
copying of files caused a regression when installing without an
existing directory structure.

This patch ensures that the platform directory actually exists by the
time the code tries to canonicalize its filename.

Fixes: 3f9eace2d (util/grub-install: Delay copying files to {grubdir,platdir} after install_device was validated)

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 16:48:44 +01:00
Michael Chang
f18a899ab1 util/grub-mkstandalone: Ensure deterministic tar file creation by sorting contents
The add_tar_files() function currently iterates through a directory's
content using readdir(), which doesn't guarantee a specific order. This
lack of deterministic behavior impacts reproducibility in the build process.

This commit resolves the issue by introducing sorting functionality.
The list retrieved by readdir() is now sorted alphabetically before
incorporation into the tar archive, ensuring consistent and predictable
file ordering within the archive.

On the occasion fix tfp memory leak.

Signed-off-by: Michael Chang <mchang@suse.com>
Signed-off-by: Bernhard Wiedemann <bwiedemann@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 15:47:08 +01:00
Michael Chang
ed74bc3764 util/grub-mkstandalone: Ensure stable timestamps for generated images
This change mirrors a previous fix [1] but is specific to images
generated by grub-mkstandalone.

The former fix, commit 85a7be241 (util/mkimage: Use stable timestamp
when generating binaries.), focused on utilizing a stable timestamp
during binary generation in the util/mkimage context. This commit
extends that approach to the images produced by grub-mkstandalone,
ensuring consistency and stability in timestamps across all generated
binaries.

[1] 85a7be241 util/mkimage: Use stable timestamp when generating binaries.

Signed-off-by: Michael Chang <mchang@suse.com>
Signed-off-by: Bernhard Wiedemann <bwiedemann@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-12 15:12:41 +01:00
Mate Kukri
069cc46c96 net/http: Fix gcc-13 errors relating to type signedness
Replace definition of HTTP_PORT with a pre-processor macro that converts
the constant to the correct grub_uint16_t type.

Change "port" local variable definition in http_establish() to have the
same type.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com
2023-12-05 15:55:10 +01:00
Julian Andres Klode
e7a831963e templates: Reinstate unused version comparison functions with warning
Revert the commit a79c567f6 (templates: Remove unused version comparison
functions) and add a warning to the functions that they are deprecated.

Removing the functions directly caused a lot of upgrade issues
with custom user scripts that called the functions. In Debian and
Ubuntu, grub-mkconfig is invoked as a post-installation script
and would fail, causing upgrades to fail halfway through and
putting the package manager into an inconsistent state.

FWIW, we get one bug per 2 weeks basically, for an interim Ubuntu
release which generally does not receive much usage, that is a high
number.

The proposal is to pick this for 2.12 and directly after the release
remove it again. Then users will have time to fix their scripts without
systems breaking immediately.

This reverts commit a79c567f6 (templates: Remove unused version
comparison functions).

Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 15:47:40 +01:00
Mate Kukri
3f9eace2d3 util/grub-install: Delay copying files to {grubdir,platdir} after install_device was validated
Previously grub-install copied modules to grubdir before doing any
validation on the install_device.

When grub-install was called with an invalid install_device, modules
were already copied to /boot before it found out and was forced to rely
on atexit() rollback.

This patch delays copying the modules after at least some install_device
validation was done, and thus reduces reliance on successful rollback.

Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 15:32:30 +01:00
Julian Andres Klode
e60015f574 efi: Set shim_lock_enabled even if validation is disabled
If validation has been disabled via MokSbState, secure boot on the
firmware is still enabled, and the kernel fails to boot.

This is a bit hacky, because shim_lock is not *fully* enabled, but
it triggers the right code paths.

Ultimately, all this will be resolved by shim gaining it's own image
loading and starting protocol, so this is more a temporary workaround.

Fixes: 6425c12cd (efi: Fallback to legacy mode if shim is loaded on x86 archs)

Cc: Peter Jones <pjones@redhat.com>
Cc: Michael Chang <mchang@suse.com>
Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 15:28:39 +01:00
Oliver Steffen
e35683317e docs: Improve bli module documentation
Improve the documentation of the bli module and explain in more detail what
it does. Make clear that GPT formatted drives are expected and other
partition formats are ignored. Also reorder and reword this section a bit.

Signed-off-by: Oliver Steffen <osteffen@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 15:26:36 +01:00
Oliver Steffen
57059ccb62 bli: Add explicit dependency on the part_gpt module
The bli module has a "hidden" dependency on the part_gpt module, which
is not picked up automatically by the build system. One purpose of the
bli module is to communicate the GPT UUID of the partition GRUB was
launched from to Linux user-space (systemd-gpt-auto-generator).
Without the part_gpt module, bli is not able to obtain the UUID. Since
bli does its work in the module initialization function, the order in
which the modules are loaded is also important: part_gpt needs to be
loaded before the bli module.

To solve this, track this dependency explicitly.

Note that the Boot Loader Interface specification, which bli aims to
implement, requires GPT formatted drives. The bli module ignores all
other partition formats.

Signed-off-by: Oliver Steffen <osteffen@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 15:06:58 +01:00
Oliver Steffen
154dcb1aea build: Allow explicit module dependencies
The build system deduces inter-module dependencies from the symbols
required and exported by the modules. This works well, except for some
rare cases where the dependency is indirect or hidden. A module might
not make use of any function of some other module, but still expect its
functionality to be available to GRUB.

To solve this, introduce a new file, currently empty, called extra_deps.lst
to track these cases manually. This file gets processed in the same way
as the automatically generated syminfo.lst, making it possible to inject
data into the dependency resolver.

Since *.lst files are set to be ignored by git, add an exception for
extra_deps.lst.

Additionally, introduce a new keyword for the syminfo.lst syntax:
"depends" allows specifying a module dependency directly:

  depends <module> <depdendency>...

Signed-off-by: Oliver Steffen <osteffen@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 14:58:36 +01:00
Stefan Berger
17c68472d1 kern/ieee1275/init/ppc64: Display upper_mem_limit when debugging
Display upper_mem_limit and its rounded-down value in MiB.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 14:55:30 +01:00
Stefan Berger
5f8e091b6a kern/ieee1275/init/ppc64: Fix a comment
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-12-05 14:52:49 +01:00
Stefan Berger
dc569b0777 kern/ieee1275/ieee1275: Display successful memory claims when debugging
Display successful memory claims with exact address and rounded-down
MiB location and rounded-up size in MiB.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Eric Snowberg <eric.snowberg@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:43:15 +01:00
Stefan Berger
0ac3d938a3 loader/powerpc/ieee1275: Use new allocation function for kernel and initrd
On PowerVM and KVM on Power use the new memory allocation function that
honors restrictions on which memory GRUB can actually use. In the request
structure indicate the request for a single memory block along with
address alignment restrictions. Request direct usage of the memory block
by setting init_region to false (prevent it from being added to GRUB's
heap). Initialize the found addr to -1, so that -1 will be returned
to the loader in case no memory could be allocated.

Report an out-of-memory error in case the initrd could not be loaded.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:39:33 +01:00
Stefan Berger
2a9a8518e9 kern/ieee1275/cmain/ppc64: Introduce flags to identify KVM and PowerVM
Introduce flags to identify PowerVM and KVM on Power and set them where
each type of host has been detected.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:37:09 +01:00
Stefan Berger
679691a13e kern/ieee1275/init/ppc64: Rename regions_claim() to grub_regions_claim()
Rename regions_claim() to grub_regions_claim() to make it available for
memory allocation. The ieee1275 loader will use this function on PowerVM
and KVM on Power and thus avoid usage of memory that it is not allowed
to use.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:31:38 +01:00
Stefan Berger
d49e86db2c kern/ieee1275/init/ppc64: Add support for alignment requirements
Add support for memory alignment requirements and adjust a candidate
address to it before checking whether the block is large enough. This
must be done in this order since the alignment adjustment can make
a block smaller than what was requested.

None of the current callers has memory alignment requirements but the
ieee1275 loader for kernel and initrd will use it to convey them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:29:55 +01:00
Stefan Berger
fe5d5e8571 kern/ieee1275/init/ppc64: Return allocated address using context
Return the allocated address of the memory block in the request structure
if a memory allocation was actually done. Leave the address untouched
otherwise. This enables a caller who wants to use the allocated memory
directly, rather than adding the memory to the heap, to see where memory
was allocated. None of the current callers need this but the converted
ieee1275 loader will make use of it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:22:54 +01:00
Stefan Berger
ea2c934849 kern/ieee1275/init/ppc64: Decide by request whether to initialize region
Let the regions_claim() request structure's init_region determine whether
to call grub_mm_init_region() on it. This allows for adding memory to
GRUB's memory heap if init_region is set to true, or direct usage of the
memory otherwise. Set all current callers' init_region to true since they
want to add memory regions to GRUB's heap.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:18:44 +01:00
Stefan Berger
0bb59fa9a3 kern/ieee1275/init/ppc64: Introduce a request for regions_claim()
The regions_claim() function limits the allocation of memory regions
by excluding certain memory areas from being used by GRUB. This for
example includes a gap between 640MB and 768MB as well as an upper
limit beyond which no memory may be used when an fadump is present.
However, the ieee1275 loader for kernel and initrd currently does not
use regions_claim() for memory allocation on PowerVM and KVM on Power
and therefore may allocate memory in those areas that it should not use.

To make the regions_claim() function more flexible and ultimately usable
for the ieee1275 loader, introduce a request structure to pass various
parameters to the regions_claim() function that describe the properties
of requested memory chunks. In a first step, move the total and flags
variables into this structure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
2023-12-05 14:12:26 +01:00
Anthony Iliopoulos
aa7c132267 fs/xfs: Add large extent counters incompat feature support
XFS introduced 64-bit extent counters for inodes via a series of
upstream commits and the feature was marked as stable in v6.5 via
commit 61d7e8274cd8 (xfs: drop EXPERIMENTAL tag for large extent
counts).

Further, xfsprogs release v6.5.0 switched this feature on by default
in mkfs.xfs via commit e5b18d7d1d96 (mkfs: enable large extent counts
by default).

Filesystems formatted with large extent count support, nrext64=1, are
thus currently not recognizable by GRUB, since this is an incompat
feature. Add the required support so that those filesystems and inodes
with large extent counters can be read by GRUB.

Signed-off-by: Anthony Iliopoulos <ailiop@suse.com>
Reviewed-by: Andrey Albershteyn <aalbersh@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Marta Lewandowska <mlewando@redhat.com>
Tested-by: Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
2023-11-22 19:13:46 +01:00
Vladimir Serbinenko
64e3cee72a gpt: Add compile time asserts for guid and gpt_partentry sizes
With new alignment specification it's easy to screw up. Fortunately if it
happens the size will be bigger than intended. Compile time assert will catch
this.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-11-08 05:05:06 +01:00
Vladimir Serbinenko
7de6fe9635 types: Split aligned and packed guids
On ia64 alignment requirements are strict. When we pass a pointer to
UUID it needs to be at least 4-byte aligned or EFI will crash.
On the other hand in device path there is no padding for UUID, so we
need 2 types in one formor another. Make 4-byte aligned and unaligned types

The code is structured in a way to accept unaligned inputs
in most cases and supply 4-byte aligned outputs.

Efiemu case is a bit ugly because there inputs and outputs are
reversed and so we need careful casts to account for this
inversion.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-11-08 05:04:24 +01:00
Vladimir Serbinenko
5fc985bfdd gpt_partition: Mark grub_gpt_partentry as having natural alignment
gpt_partition contains grub_guid. We need to decide whether the whole
structure is unaligned and then we need to use packed_guid. But we never
have unaligned part entries as we read them in an aligned buffer from disk.
Hence just make it all aligned.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
2023-11-06 22:48:24 +01:00
Vladimir Serbinenko
7ad30299da efi: Deduplicate configuration table search function
We do table search in many places doing exactly the same algorithm.
The only minor variance in users is which table is used if several entries
are present. As specification mandates uniqueness and even if it ever isn't,
first entry is good enough, unify this code and always use the first entry.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-11-06 22:47:16 +01:00
Vladimir Serbinenko
c6cf807fc0 lsefi: Add missing static qualifier
known_protocols isn't used anywhere else and even misses grub_ prefix, so
let's make it local (static).

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-11-06 22:38:12 +01:00
Vladimir Serbinenko
a964e359bc types: Fix typo
Just a small grammar mistake.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-11-06 22:37:31 +01:00
Qiumiao Zhang
3f79e3b158 util/grub-mount: Check file path sanity
The function argp_parser() in util/grub-mount.c lacks a check on the
sanity of the file path when parsing parameters. This results in
a segmentation fault if a partition is mounted to a non-existent path.

Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-30 18:33:06 +01:00
Richard Marko
85e40b36ed configure: Make the DJVU_FONT_SOURCE configurable with --with-dejavufont=FILE
Font might be located in different location, the default font might
not be available on all systems or other font might be preferred.

Signed-off-by: Richard Marko <srk@48.io>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-30 18:19:07 +01:00
Mads Kiilerich
2d6631d2af configure: Make the Unifont FONT_SOURCE configurable with --with-unifont=FILE
Font might be located in different location, the default font might
not be available on all systems or other font might be preferred.

Signed-off-by: Mads Kiilerich <mads@kiilerich.com>
Signed-off-by: Richard Marko <srk@48.io>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-30 18:08:59 +01:00
Jon DeVree
07318ee7e1 fs/xfs: Fix XFS directory extent parsing
The XFS directory entry parsing code has never been completely correct
for extent based directories. The parser correctly handles the case
where the directory is contained in a single extent, but then mistakenly
assumes the data blocks for the multiple extent case are each identical
to the single extent case. The difference in the format of the data
blocks between the two cases is tiny enough that its gone unnoticed for
a very long time.

A recent change introduced some additional bounds checking into the XFS
parser. Like GRUB's existing parser, it is correct for the single extent
case but incorrect for the multiple extent case. When parsing a directory
with multiple extents, this new bounds checking is sometimes (but not
always) tripped and triggers an "invalid XFS directory entry" error. This
probably would have continued to go unnoticed but the /boot/grub/<arch>
directory is large enough that it often has multiple extents.

The difference between the two cases is that when there are multiple
extents, the data blocks do not contain a trailer nor do they contain
any leaf information. That information is stored in a separate set of
extents dedicated to just the leaf information. These extents come after
the directory entry extents and are not included in the inode size. So
the existing parser already ignores the leaf extents.

The only reason to read the trailer/leaf information at all is so that
the parser can avoid misinterpreting that data as directory entries. So
this updates the parser as follows:

For the single extent case the parser doesn't change much:
1. Read the size of the leaf information from the trailer
2. Set the end pointer for the parser to the start of the leaf
   information. (The previous bounds checking set the end pointer to the
   start of the trailer, so this is actually a small improvement.)
3. Set the entries variable to the expected number of directory entries.

For the multiple extent case:
1. Set the end pointer to the end of the block.
2. Do not set up the entries variable. Figuring out how many entries are
   in each individual block is complex and does not seem worth it when
   it appears to be safe to just iterate over the entire block.

The bounds check itself was also dependent upon the faulty XFS parser
because it accidentally used "filename + length - 1". Presumably this
was able to pass the fuzzer because in the old parser there was always
8 bytes of slack space between the tail pointer and the actual end of
the block. Since this is no longer the case the bounds check needs to be
updated to "filename + length + 1" in order to prevent a regression in
the handling of corrupt fliesystems.

Notes:
* When there is only one extent there will only ever be one block. If
  more than one block is required then XFS will always switch to holding
  leaf information in a separate extent.
* B-tree based directories seems to be parsed properly by the same code
  that handles multiple extents. This is unlikely to ever occur within
  /boot though because its only used when there are an extremely large
  number of directory entries.

Fixes: ef7850c75 (fs/xfs: Fix issues found while fuzzing the XFS filesystem)
Fixes: b2499b29c (Adds support for the XFS filesystem.)
Fixes: https://savannah.gnu.org/bugs/?64376

Signed-off-by: Jon DeVree <nuxi@vault24.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
Tested-by: Marta Lewandowska <mlewando@redhat.com>
2023-10-30 18:01:22 +01:00
Lidong Chen
ad7fb8e2e0 fs/xfs: Incorrect short form directory data boundary check
After parsing of the current entry, the entry pointer is advanced
to the next entry at the end of the "for" loop. In case where the
last entry is at the end of the data boundary, the advanced entry
pointer can point off the data boundary. The subsequent boundary
check for the advanced entry pointer can cause a failure.

The fix is to include the boundary check into the "for" loop
condition.

Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
Tested-by: Marta Lewandowska <mlewando@redhat.com>
2023-10-30 17:16:18 +01:00
Vladimir 'phcoder' Serbinenko
4e10213de6 Revert "zfsinfo: Correct a check for error allocating memory"
Original commit is wrong because grub_file_get_device_name() may return NULL
if we use implicit $root. Additionally, the grub_errno is guaranteed to be
GRUB_ERR_NONE at the beginning of a command. So, everything should work as
expected and Coverity report, CID 73668, WRT to this code should be treated
as false positive.

This reverts commit 7aab03418 (zfsinfo: Correct a check for error allocating memory).

Fixes: 7aab03418 (zfsinfo: Correct a check for error allocating memory)

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 19:28:33 +02:00
ValdikSS
4266fd2bb2 disk/i386/pc/biosdisk: Read up to 63 sectors in LBA mode
Current code imposes limitations on the amount of sectors read in
a single call according to CHS layout of the disk even in LBA
read mode. There's no need to obey CHS layout restrictions for
LBA reads on LBA disks. It only slows down booting process.

See: https://lore.kernel.org/grub-devel/d42a11fa-2a59-b5e7-08b1-d2c60444bb99@valdikss.org.ru/

Signed-off-by: ValdikSS <iam@valdikss.org.ru>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 19:23:17 +02:00
ValdikSS
cab04dcda3 kern/i386/pc/init: Flush cache only on VIA C3 and earlier
The code flushes the cache on VIA processors unconditionally which
is excessive. Check for cpuid family and execute wbinvd only on C3
and earlier.

Fixes: https://savannah.gnu.org/bugs/?45149
Fixes: 25492a0f0 (Add wbinvd around bios call.)

Signed-off-by: ValdikSS <iam@valdikss.org.ru>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 19:18:58 +02:00
Fabian Vogt
3c7e842571 fs/btrfs: Zero file data not backed by extents
Implicit holes in file data need to be zeroed explicitly, instead of
just leaving the data in the buffer uninitialized.

This led to kernels randomly failing to boot in "fun" ways when loaded
from btrfs with the no_holes feature enabled, because large blocks of
zeros in the kernel file contained random data instead.

Signed-off-by: Fabian Vogt <fvogt@suse.de>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
2023-10-12 19:01:45 +02:00
Stefan Berger
4bcf6f747c kern/ieee1275/init: Restrict high memory in presence of fadump on ppc64
When a kernel dump is present then restrict the high memory regions to
avoid allocating memory where the kernel dump resides. Use the
ibm,kernel-dump node under /rtas to determine whether a kernel dump
exists and up to which limit GRUB can use available memory. Set the
upper_mem_limit to the size of the kernel dump section of type
REAL_MODE_REGION and therefore only allow GRUB's memory usage for high
addresses from RMO_ADDR_MAX to upper_mem_limit. This means that GRUB can
use high memory in the range of RMO_ADDR_MAX (768MB) to upper_mem_limit
and the kernel-dump memory regions above upper_mem_limit remain
untouched. This change has no effect on memory allocations below
linux_rmo_save (typically at 640MB).

Also, fall back to allocating below rmo_linux_save in case the chunk of
memory there would be larger than the chunk of memory above RMO_ADDR_MAX.
This can for example occur if a free memory area is found starting at 300MB
extending up to 1GB but a kernel dump is located at 768MB and therefore
does not allow the allocation of the high memory area but requiring to use
the chunk starting at 300MB to avoid an unnecessary out-of-memory condition.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Hari Bathini <hbathini@linux.ibm.com>
Cc: Pavithra Prakash <pavrampu@in.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Carolyn Scherrer <cpscherr@us.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 18:54:58 +02:00
Glenn Washburn
cf58eca2a2 tests/util/grub-shell: Enable RNG device to better test stack smashing
In certain firmwares, e.g. OVMF, the RNG protocol is not enabled unless
there is an RNG device. When not enabled, GRUB fails to initialize the
stack guard with random bytes. For testing, this is not a big issue, but
there have been bugs found in the initialization. So turn this on for EFI
platforms to catch any regressions.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 18:48:28 +02:00
Glenn Washburn
c3bdf263f6 kern/efi/init: Disable stack smashing protection on grub_efi_init()
GCC is electing to instrument grub_efi_init() to give it stack smashing
protection when configuring with --enable-stack-protector on the x86_64-efi
target. In the function prologue, the canary at the top of the stack frame
is set to the value of the stack guard. And in the epilogue, the canary is
checked to verify if it is equal to the guard and if not to call the stack
check fail function. The issue is that grub_efi_init() sets up the guard
by initializing it with random bytes, if the firmware supports the RNG
protocol. So in its prologue the canary will be set with the value of the
uninitialized guard, likely NUL bytes. Then the guard is initialized, and
finally the epilogue checks the canary against the guard, which will almost
certainly be different. This causes the code path for a smashed stack to be
taken, causing the machine to print out a message that stack smashing was
detected, wait 5 seconds, and then reboot. Disable grub_efi_init()
instrumentation so there is no stack smashing false positive generated.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 17:47:28 +02:00
Glenn Washburn
95963d97f8 disk/cryptodisk: Add support for LUKS2 in (proc)/luks_script
The sector size in bytes is added to each line and it is allowed to be
6 decimal digits long, which covers the most common cases of 512 and 4096
byte sectors with space for two additional digits as future-proofing. The
size allocation is updated to reflect this additional field. Also make
clearer the size allocation calculation.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 17:42:20 +02:00
Glenn Washburn
016f142577 disk/cryptodisk: Optimize luks_script_get()
Use the return value of grub_snprintf() to move the string pointer forward,
instead of incrementing the string pointer iteratively until a NULL byte is
reached. Move the space out of the format string argument, a small
optimization, but also makes the spacing clearer. Also, use the new
PRIxGRUB_OFFSET instead of PRIuGRUB_UINT64_T to accurately reflect the
format string for this type.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 17:37:16 +02:00
Glenn Washburn
f7a663c007 term/serial: Ensure proper NULL termination after grub_strncpy()
A large enough argument to the --port option could cause a string buffer
to be not NULL terminated because grub_strncpy() does not guarantee NULL
termination if copied string is longer than max characters to copy.

Fixes: 712309eaae (term/serial: Use grub_strncpy() instead of grub_snprintf() when only copying string)

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 17:27:34 +02:00
Heinrich Schuchardt
a19e47ca41 commands/efi/lsefisystab: Print the UEFI specification revision in human readable form
E.g. 2.10 instead of 00020064 and 2.3.1 instead of 0002001f.

See UEFI 2.10 specification, chapter 4.2.1 EFI_TABLE_HEADER.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-12 17:22:45 +02:00
Maxim Suhanov
e58b870ff9 fs/ntfs: Make code more readable
Move some calls used to access NTFS attribute header fields into
functions with human-readable names.

Suggested-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Maxim Suhanov
1fe82c41e0 fs/ntfs: Fix an OOB read when parsing a volume label
This fix introduces checks to ensure that an NTFS volume label is always
read from the corresponding file record segment.

The current NTFS code allows the volume label string to be read from an
arbitrary, attacker-chosen memory location. However, the bytes read are
always treated as UTF-16LE. So, the final string displayed is mostly
unreadable and it can't be easily converted back to raw bytes.

The lack of this check is a minor issue, likely not causing a significant
data leak.

Reported-by: Maxim Suhanov <dfirblog@gmail.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Maxim Suhanov
7a5a116739 fs/ntfs: Fix an OOB read when parsing bitmaps for index attributes
This fix introduces checks to ensure that bitmaps for directory indices
are never read beyond their actual sizes.

The lack of this check is a minor issue, likely not exploitable in any way.

Reported-by: Maxim Suhanov <dfirblog@gmail.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Maxim Suhanov
7e5f031a6a fs/ntfs: Fix an OOB read when parsing directory entries from resident and non-resident index attributes
This fix introduces checks to ensure that index entries are never read
beyond the corresponding directory index.

The lack of this check is a minor issue, likely not exploitable in any way.

Reported-by: Maxim Suhanov <dfirblog@gmail.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Maxim Suhanov
0ed2458cc4 fs/ntfs: Fix an OOB read when reading data from the resident $DATA attribute
When reading a file containing resident data, i.e., the file data is stored in
the $DATA attribute within the NTFS file record, not in external clusters,
there are no checks that this resident data actually fits the corresponding
file record segment.

When parsing a specially-crafted file system image, the current NTFS code will
read the file data from an arbitrary, attacker-chosen memory offset and of
arbitrary, attacker-chosen length.

This allows an attacker to display arbitrary chunks of memory, which could
contain sensitive information like password hashes or even plain-text,
obfuscated passwords from BS EFI variables.

This fix implements a check to ensure that resident data is read from the
corresponding file record segment only.

Fixes: CVE-2023-4693

Reported-by: Maxim Suhanov <dfirblog@gmail.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Maxim Suhanov
43651027d2 fs/ntfs: Fix an OOB write when parsing the $ATTRIBUTE_LIST attribute for the $MFT file
When parsing an extremely fragmented $MFT file, i.e., the file described
using the $ATTRIBUTE_LIST attribute, current NTFS code will reuse a buffer
containing bytes read from the underlying drive to store sector numbers,
which are consumed later to read data from these sectors into another buffer.

These sectors numbers, two 32-bit integers, are always stored at predefined
offsets, 0x10 and 0x14, relative to first byte of the selected entry within
the $ATTRIBUTE_LIST attribute. Usually, this won't cause any problem.

However, when parsing a specially-crafted file system image, this may cause
the NTFS code to write these integers beyond the buffer boundary, likely
causing the GRUB memory allocator to misbehave or fail. These integers contain
values which are controlled by on-disk structures of the NTFS file system.

Such modification and resulting misbehavior may touch a memory range not
assigned to the GRUB and owned by firmware or another EFI application/driver.

This fix introduces checks to ensure that these sector numbers are never
written beyond the boundary.

Fixes: CVE-2023-4692

Reported-by: Maxim Suhanov <dfirblog@gmail.com>
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Michael Chang
48f569c78a kern/acpi: Skip NULL entries in RSDT and XSDT
During attempts to configure a serial console, a Page Fault Exception
and system reset were encountered, specifically on release 2.12~rc1.
This issue was not present in prior versions and seemed to affect only
a specific machine, potentially pointing to hardware or firmware flaw.

After investigation, it was discovered that the invalid page access
occurred during the discovery of serial MMIO ports as specified by
ACPI's SPCR table [1]. The recent change uncovered an issue in GRUB's
ACPI driver.

In certain cases, the XSDT/RSDT root table might contain a NULL entry as
a terminator, depending on how the tables are assembled. GRUB cannot
blindly trust the address in the root table to be valid and should
perform a sanity check for NULL entries. This patch introduces this
simple check.

This fix is also inspired by a related Linux kernel fix [2].

[1] 7b192ec4c term/ns8250: Use ACPI SPCR table when available to configure serial
[2] 0f929fbf0 ACPICA: Tables: Add new mechanism to skip NULL entries in RSDT and XSDT.

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:38:48 +02:00
Glenn Washburn
28a509dd58 util/grub-install-common: Print usable grub-mkimage command
When grub-install is run with the verbose option, it will print a log
message indicating the grub-mkimage command and arguments used.
GRUB no longer calls the grub-mkimage binary internally, however the
command logged is a command that if run should effectively be what
grub-install used. However, as this has changed some of the newer
options have been incorrectly added so that the printed command fails
when run separately. This change makes the displayed command run as
intended.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:37:13 +02:00
Glenn Washburn
1c2e162306 util/grub-install-common: Minor improvements to printing of grub-mkimage command
This is a preparatory patch to make the following patch less cluttered. The
only visible change made here is to not print extra spaces when either or
both --note or --disable-shim-lock are not given and to not print an extra
space at the end of the command. The latter is done by constructing the
trailing argument string with spaces in front of each argument rather than
trailing. The allocation of the argument string is made precise, which has
the benefit of saving a few bytes, but more importantly self-documenting
what the needed allocated bytes are. Also, unneeded braces are removed from
an if block.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 15:35:24 +02:00
Vladimir 'phcoder' Serbinenko
968928240a lib/i386/relocator64: Fix 64-bit FreeBSD boot on BIOS
The commit 80948f532d (lib/i386/relocator64: Build fixes for i386) has
broken 64-bit FreeBSD boot on BIOS. This patch fixes the issue.

Fixes: 80948f532d (lib/i386/relocator64: Build fixes for i386)

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-10-03 12:46:37 +02:00
Anthony PERARD
db1faedccd templates/linux_xen: Fix XSM entries generation
It turns out that setting $xen_version in linux_entry_xsm() override
$xen_version in the loop over $reverse_sorted_xen_list. This means
that only one entry per Xen version is going to enable XSM, but all
further entries are going to have "(XSM enabled)" in their titles
without enabling XSM.

When a "xenpolicy-$xen_version" file was found for the current
$xen_version, it would overwrite $xen_version to add "(XSM enabled)" to
the menu entry title. Once updated, the next call to linux_entry_xsm()
would also have this modified $xen_version and would look for the file
"xenpolicy-*(XSM enabled)" and fail.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-22 18:38:36 +02:00
Xiaotian Wu
3335591c64 loongarch: Eliminate cmodel compilation warnings
In the configure phase, the "-mcmodel=large" CFLAGS passed the test, but
because it has not been implemented in gcc, the following warning will
appear when compiling:

  gcc: warning: 'large' is not supported, now cmodel is set to 'normal'

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-22 18:38:36 +02:00
Glenn Washburn
084a66e307 configure: Enable -fno-omit-frame-pointer for backtrace module
The backtrace module is written assuming that the frame pointer is in %ebp.
By default, -Os optimization level is used, which enables the gcc option
-fomit-frame-pointer. This breaks the backtrace functionality. Enabling
this may cause an unnoticeable performance cost and virtually no size increase.

The backtrace command on x86_64 and probably i386 is broken due to the
above rationale. I've not verified, but presumably the backtrace that used
to be printed for an unhandled CPU exception is also broken. Do any distros
handle this?

Considering that, to my knowledge, no one has complained about this in the
over 13 years that -Os has been used, has this code actually been useful?
Is it worth disabling -fomit-frame-pointer? Though, I don't see much downside
right now in disabling it. Alternatively, we could disable/remove the
backtrace code. I think it would be nice to keep it and have it working.

Nowadays, presumably QEMU makes the GDB stub rarely used as I imagine most
are developing in a virtual machines. Also, the GDB stub does not work in UEFI.
So, if anyone is using it on real hardware, they are doing so on pretty old
machines. The lack of a GDB stub does not seem to be a pain point because
no one has got it working on UEFI.

This patch gets the backtrace command working on x86_64-efi in QEMU for me.
However, it hangs when run on my laptop. Not sure what's going on there.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-22 18:38:36 +02:00
Ard Biesheuvel
1f5b180742 loader/efi/linux: Implement x86 mixed mode using legacy boot
Recent mixed-mode Linux kernels, i.e., v4.0 or newer, can access EFI
runtime services at OS runtime even when the OS was not entered via the
EFI stub. This is because, instead of reverting back to the firmware's
segment selectors, GDTs and IDTs, the 64-bit kernel simply calls 32-bit
runtime services using compatibility mode, i.e., the same mode used for
32-bit user space, without taking down all interrupt handling, exception
handling, etc.

This means that GRUB's legacy x86 boot mode is sufficient to make use of
this: 32-bit i686 builds of GRUB can already boot 64-bit kernels in EFI
enlightened mode, but without going via the EFI stub, and provide all
the metadata that the OS needs to map the EFI runtime regions and call
EFI runtime services successfully.

It does mean that GRUB should not attempt to invoke the firmware's
LoadImage()/StartImage() methods on kernel builds that it knows cannot
be started natively. So, add a check for this in the native EFI boot
path and fall back to legacy x86 mode in such cases.

Note that in the general case, booting non-native images of the same
native word size, e.g., x64 EFI apps on arm64 firmware, might be
supported by means of emulation. So, let's only disallow images that use
a non-native word size. This will also permit booting i686 kernels on
x86_64 builds, although without access to runtime services, as this is
not supported by Linux.

This change on top of 2.12-rc1 is sufficient to boot ordinary Linux
mixed mode builds and get full access to the EFI runtime services.

Cc: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Steve McIntyre <steve@einval.com>
Cc: Julian Andres Klode <julian.klode@canonical.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-22 18:38:36 +02:00
Ard Biesheuvel
67ae3981dc loader/i386/linux: Prefer entry in long mode when booting via EFI
The x86_64 Linux kernel can be booted in 32-bit mode, in which case the
startup code creates a set of preliminary page tables that map the first
4 GiB of physical memory 1:1 and enables paging. This is a prerequisite
for 64-bit execution and can therefore only be implemented in 32-bit code.

The x86_64 Linux kernel can also be booted in 64-bit mode directly: this
implies that paging is already enabled and it is the responsibility of
the bootloader to ensure that the active page tables cover the entire
loaded image, including its BSS space, the size of which is described in
the image's setup header.

Given that the EFI spec mandates execution in long mode for x86_64 and
stipulates that all system memory is mapped 1:1, the Linux/x86
requirements for 64-bit entry can be met trivially when booting on
x86_64 via EFI. So, enter via the 64-bit entry point in this case.

This involves inspecting the xloadflags field in the setup header to
check whether the 64-bit entry point is supported. This field was
introduced in Linux version v3.8 (early 2013).

This change ensures that all EFI firmware tables and other assets passed
by the firmware or bootloader in memory remain mapped and accessible
throughout the early startup code.

Avoiding the drop out of long mode will also be needed to support
upcoming CPU designs that no longer implement 32-bit mode at all
(as recently announced by Intel [0]).

[0] https://www.intel.com/content/www/us/en/developer/articles/technical/envisioning-future-simplified-architecture.html

Cc: Daniel Kiper <daniel.kiper@oracle.com>
Cc: Julian Andres Klode <julian.klode@canonical.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-22 18:38:36 +02:00
Vladimir Serbinenko
7ce5b49110 ZFS: Check bonustype in addition to dnode type
Some dnodes are shared with properties zap. This is used
e.g. for quotas. Then dnode type is 0xc4 and GRUB stumbles on
this. Check bonus type and if it's ok then ignore dnode type mismatch

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-18 02:44:35 +02:00
Vladimir Serbinenko
444089eec6 ZFS: Don't iterate over null objsets
Reading them is harmless but useless as they are empty by definition

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-18 02:44:02 +02:00
Vladimir Serbinenko
96446ce14e ZFS: Fix invalid memcmp
We ended up comparing over unset values as we had dnode_phys on one side
and dnode on another

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-18 02:43:32 +02:00
Vladimir Serbinenko
42a831d746 ZFS: support inode type embed into its ID
This is a speedup used in some ZFS version. This trips GRUB and makes it
unable to access directories. Just skip it for now and revisit
if we ever need this speedup.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-09-18 02:42:21 +02:00
Heinrich Schuchardt
34a3adff82 video/efi_gop: Require shadow if PixelBltOnly
If the EFI graphics pixel format is PixelBltOnly, we cannot write directly
to the frame buffer. We need the shadow frame buffer which we copy via
the BitBlt operation to the hardware.

If the pixel format is PixelBltOnly and allocation of the shadow frame
buffer fails, we must raise an error to signal that the EFI GOP protocol
is not usable.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 17:21:40 +02:00
Glenn Washburn
ac8ae924ca docs: Add menu to prevent older makeinfo versions from failing
It has been reported that makeinfo version 4.13a complains and returns
error when menus for chapter structuring commands are not present. It
is also known that newer makeinfos, such as version 6.7, will create
default menus when needed. Since the menu will be created regardless,
explicitly create it to support older makeinfo versions. This also
enables building to be successful when an older makeinfo is installed
because in that case info files are attempted to be generated with the
"all" target.

Reported-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Olaf Hering <olaf@aepfle.de>
2023-08-31 17:16:32 +02:00
Glenn Washburn
3c42272105 docs: Use @ref instead of @xref
The @xref command is meant to be used at the beginning of a sentence
because its expansion creates a "See " prefix on all output formats, and
on older makeinfo versions is strict about enforcing a "." or "," after
the command. The @ref command has no such restriction and is just the
link, which allows more control over output. This also fixes an issue
where there was a repeated "see" in the output.

Reported-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Olaf Hering <olaf@aepfle.de>
2023-08-31 17:11:20 +02:00
Glenn Washburn
fff2e9ee83 tests/util/grub-shell-luks-tester: Allow setting timeout
Allow using the envvar GRUB_SHELL_LUKS_TIMEOUT to change the default
timeout. If not specified, use value of GRUB_SHELL_DEFAULT_TIMEOUT. And
if that is not specified, fallback to original 600s timeout.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 17:09:04 +02:00
Glenn Washburn
296d3ec835 disk/cryptodisk: Fix missing change when updating to use grub_uuidcasecmp()
This was causing the cryptomount command to return failure even though
the crypto device was successfully added. Of course, this meant that any
script using the return code would behave unexpectedly.

Fixes: 3cf2e848bc (disk/cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner)

Suggested-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrich Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 17:03:29 +02:00
Glenn Washburn
14c95e57fd kern/misc: Make grub_vsnprintf() C99/POSIX conformant
To comply with C99 and POSIX standards, snprintf() should return the
number of bytes that would be written to the string (excluding the
terminating NUL byte) if the buffer size was big enough. Before this
change, the return value was the minimum of the standard return and the
length of the buffer. Rarely is the return value of grub_snprintf() or
grub_vsnprintf() used with current code, and the few places where it is
used do not need to be changed.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 16:57:22 +02:00
Glenn Washburn
6d6b95720c tests: Add serial_test
This test is meant to test output via various serial devices. Currently,
only the PCI serial device is tested.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 16:55:41 +02:00
Glenn Washburn
846829aca8 tests/util/grub-shell: Allow explicitly using other serial ports for output
While here, move "-qemu=*" case to be next to the "--qemu-opts=*" case.
This causes no change in logic, but is more logically located.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 16:35:57 +02:00
Glenn Washburn
ee2349ef65 tests/util/grub-shell-luks-tester: Do not remove generated files when test fails to allow debugging
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 16:34:35 +02:00
Glenn Washburn
a63ef8c78c tests/util/grub-shell: Convert spaces to TABs
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-31 16:32:59 +02:00
Glenn Washburn
4fdcb339bb commands/ls: Print "????????????" if unable to get file size
In long list mode, if the file can not be opened, the file is not printed.
Instead, print the file but print the size as "????????????".

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 18:12:49 +02:00
Glenn Washburn
6889c67fe7 commands/ls: Send correct dirname to print functions
For each non-directory path argument to the ls command, the full path was
being sent to the print functions, instead of the dirname. The long output
print function expected dirname to be the directory containing the file
and so could not open the file to get the file size because the generated
path was incorrect. This caused the output to be a blank line.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 18:10:36 +02:00
Glenn Washburn
fcbea18c58 fs/archelp: If path given to grub_archelp_dir() is not a directory return error
Specifically, return GRUB_ERR_BAD_FILE_TYPE because this is what is
expected by the ls command when it is given a path to a non-directory.
This fixes a bug where calling ls with a list of non-directory paths
outputs a blank line for each such argument.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 18:07:24 +02:00
Glenn Washburn
1de5832699 commands/videoinfo: Prevent crash when run while video driver already active
The videoinfo command will initialize all non-active video adapters. Video
drivers tend to zero out the global framebuffer object on initialization.
This is not a problem when there is no active video adapter. However, when
there is, then outputting to the video adapter will cause a crash because
methods in the framebuffer object are reinitialized. For example, this
command sequence will cause a crash.

  terminal_output --append gfxterm; videoinfo

When running in a QEMU headless with GRUB built for the x86_64-efi target,
the first command initializes the Bochs video adapter, which, among
other things, sets the set_page() member function. Then when videoinfo is
run, all non-Bochs video adapters will be initialized, each one wiping
the framebuffer and thus setting set_page to NULL. Soon after the videoinfo
command finishes there will be a call to grub_refresh(), which will
ultimately call the framebuffer's set_page which will be NULL and cause
a crash when called.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 18:04:04 +02:00
Glenn Washburn
43bf0b2a10 docs: Improve initrd documentation
A list of improvements:
  * Remove reference to "initial ramdisk" and replace with "initrd". This
    then covers the case of ramdisk and ramfs, which is the usual method
    with kernels 2.6 and newer.
  * Add sentence with URL to initrd documentation Linux kernel.
  * Add a section documenting how to have the initrd command generate
    a new-style initrd via a specially crafted argument and include an example.
  * Update initrd16 to refer to the initrd section and make note that
    initrd16 is only on the pc platform.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 18:00:41 +02:00
Glenn Washburn
b0890d371c term/ns8250-spcr: Continue processing SPCR table even if revision is < 2
According to commit 0231d00082 (ACPI: SPCR: Make SPCR available to x86)
to the Linux kernel, "On x86, many systems have a valid SPCR table but the
table version is not 2 so the table version check must be a warning."

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:56:47 +02:00
Glenn Washburn
711e034235 docs: A note to cat that hexdump should be used for binary data
The cat command should not be used to print binary data because it can
show bytes not in the binary data and not show bytes that are in the data,
which can lead to confusion. This happens because cat does some processing
of the data stream, namely trying to decode substrings as UTF-8.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:55:01 +02:00
Glenn Washburn
7ff4f3c963 docs: Document hexdump command
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:52:41 +02:00
Glenn Washburn
2e1279af2b docs: Group usage of user-space utilities into single chapter
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:49:11 +02:00
Qiumiao Zhang
3077b39bae util/grub-mount: Fix memory leak in fuse_getattr()
Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:48:22 +02:00
Michał Grzelak
eba6bdcd3a configure: Fix SDL2 typo by referencing value
During configuration of SDL2, variable enable_grub_emu_sdl2 is checked
whether to throw an error message. However, error could not happen
because two unequal strings were compared. Fix this by referencing
value of enable_grub_emu_sdl2, not name.

Fixes: 17d6ac1a7 (emu: Add SDL2 support)

Signed-off-by: Michał Grzelak <mchl.grzlk@gmail.com>
Reviewed-by: Julian Andres Klode <julian.klode@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>
2023-08-14 17:19:51 +02:00
Glenn Washburn
459f028cd3 docs: Add missing assumption
Also reword a prior sentence to be more clear.

Fixes: 5a3d2b4742 (docs: Add debugging chapter to development documentation)

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:16:53 +02:00
Oskari Pirhonen
e641b4ab32 util/grub.d/25_bli.in: Fix shebang on unmerged-usr
On an unmerged-usr system, grub-mkconfig errors out with the following
error due to /usr/bin/sh not existing:

  /usr/sbin/grub-mkconfig: /etc/grub.d/25_bli: /usr/bin/sh: bad interpreter: No such file or directory

Use a /bin/sh shebang to fix the error as well as match the other
existing files.

Fixes: 158a6583e (util/grub.d/25_bli.in: Activate bli module on EFI)

Signed-off-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
Reviewed-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Oliver Steffen <osteffen@redhat.com>
2023-08-14 17:11:55 +02:00
Glenn Washburn
7f6149449c tests/util/grub-shell-luks-tester: Allow GRUB_SHELL_LUKS_DEFAULT_DEBUG and GRUB_TEST_DEFAULT_DEBUG to specify the debug level to grub-shell
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:08:09 +02:00
Glenn Washburn
e1b97d7c37 tests/util/grub-shell: Allow setting the value of debug regardless of its previous state
This allows an invocation of grub-shell to set the value of debug regardless
of the global default environment variable GRUB_SHELL_DEFAULT_DEBUG.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:06:28 +02:00
Glenn Washburn
76e8962e85 tests/util/grub-shell: Allow setting default timeout via GRUB_SHELL_DEFAULT_TIMEOUT envvar
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 17:01:13 +02:00
Glenn Washburn
79ccfa90a1 tests/util/grub-shell: Add --verbose to grub-mkrescue when $debug is greater than 2
Since this is fairly verbose output, do not enable first level of debug
is turned on.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2023-08-14 16:58:15 +02:00
334 changed files with 32535 additions and 1987 deletions

5
.gitignore vendored
View file

@ -104,6 +104,7 @@ widthspec.bin
/docs/version-dev.texi /docs/version-dev.texi
/docs/version.texi /docs/version.texi
/ehci_test /ehci_test
/erofs_test
/example_grub_script_test /example_grub_script_test
/example_scripted_test /example_scripted_test
/example_unit_test /example_unit_test
@ -140,10 +141,12 @@ widthspec.bin
/grub-core/kernel.img.bin /grub-core/kernel.img.bin
/grub-core/lib/gnulib /grub-core/lib/gnulib
/grub-core/lib/libgcrypt-grub /grub-core/lib/libgcrypt-grub
/grub-core/lib/libtasn1-grub
/grub-core/modinfo.sh /grub-core/modinfo.sh
/grub-core/rs_decoder.h /grub-core/rs_decoder.h
/grub-core/symlist.c /grub-core/symlist.c
/grub-core/symlist.h /grub-core/symlist.h
/grub-core/tests/asn1/tests
/grub-core/trigtables.c /grub-core/trigtables.c
/grub-core/unidata.c /grub-core/unidata.c
/grub-editenv /grub-editenv
@ -168,6 +171,8 @@ widthspec.bin
/grub-ofpathname.exe /grub-ofpathname.exe
/grub-probe /grub-probe
/grub-probe.exe /grub-probe.exe
/grub-protect
/grub-protect.exe
/grub-reboot /grub-reboot
/grub-render-label /grub-render-label
/grub-render-label.exe /grub-render-label.exe

11
INSTALL
View file

@ -20,7 +20,7 @@ configuring the GRUB.
for i386, x86_64, arm (including thumb), arm64, mips(el), powerpc, sparc64 for i386, x86_64, arm (including thumb), arm64, mips(el), powerpc, sparc64
* GNU Make * GNU Make
* GNU Bison 2.3 or later * GNU Bison 2.3 or later
* GNU gettext 0.17 or later * GNU gettext
* GNU binutils 2.9.1.0.23 or later * GNU binutils 2.9.1.0.23 or later
* Flex 2.5.35 or later * Flex 2.5.35 or later
* pkg-config * pkg-config
@ -74,18 +74,19 @@ Prerequisites for make-check:
* wamerican, for grub-fs-tester * wamerican, for grub-fs-tester
* mtools, FAT tools for EFI platforms * mtools, FAT tools for EFI platforms
* xfonts-unifont, for the functional tests * xfonts-unifont, for the functional tests
* swtpm-tools and tpm2-tools, for TPM2 key protector tests
* If running a Linux kernel the following modules must be loaded: * If running a Linux kernel the following modules must be loaded:
- fuse, loop - fuse, loop
- btrfs, ext4, f2fs, fat, hfs, hfsplus, jfs, mac-roman, minix, nilfs2, - btrfs, erofs, ext4, f2fs, fat, hfs, hfsplus, jfs, mac-roman, minix, nilfs2,
reiserfs, udf, xfs reiserfs, udf, xfs
- On newer kernels, the exfat kernel modules may be used instead of the - On newer kernels, the exfat kernel modules may be used instead of the
exfat FUSE filesystem exfat FUSE filesystem
* The following are Debian named packages required mostly for the full * The following are Debian named packages required mostly for the full
suite of filesystem testing (but some are needed by other tests as well): suite of filesystem testing (but some are needed by other tests as well):
- btrfs-progs, dosfstools, e2fsprogs, exfat-utils, f2fs-tools, genromfs, - btrfs-progs, dosfstools, e2fsprogs, erofs-utils, exfatprogs, exfat-fuse,
hfsprogs, jfsutils, nilfs-tools, ntfs-3g, reiserfsprogs, squashfs-tools, f2fs-tools, genromfs, hfsprogs, jfsutils, nilfs-tools, ntfs-3g,
reiserfsprogs, udftools, xfsprogs, zfs-fuse reiserfsprogs, squashfs-tools, reiserfsprogs, udftools, xfsprogs, zfs-fuse
- exfat-fuse, if not using the exfat kernel module - exfat-fuse, if not using the exfat kernel module
- gzip, lzop, xz-utils - gzip, lzop, xz-utils
- attr, cpio, g++, gawk, parted, recode, tar, util-linux - attr, cpio, g++, gawk, parted, recode, tar, util-linux

View file

@ -40,6 +40,7 @@ library = {
common = grub-core/disk/luks.c; common = grub-core/disk/luks.c;
common = grub-core/disk/luks2.c; common = grub-core/disk/luks2.c;
common = grub-core/disk/geli.c; common = grub-core/disk/geli.c;
common = grub-core/disk/key_protector.c;
common = grub-core/disk/cryptodisk.c; common = grub-core/disk/cryptodisk.c;
common = grub-core/disk/AFSplitter.c; common = grub-core/disk/AFSplitter.c;
common = grub-core/lib/pbkdf2.c; common = grub-core/lib/pbkdf2.c;
@ -55,7 +56,7 @@ library = {
library = { library = {
name = libgrubmods.a; name = libgrubmods.a;
cflags = '-fno-builtin -Wno-undef'; cflags = '-fno-builtin -Wno-undef -Wno-unused-but-set-variable';
cppflags = '-I$(srcdir)/grub-core/lib/minilzo -I$(srcdir)/grub-core/lib/xzembed -I$(srcdir)/grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H'; cppflags = '-I$(srcdir)/grub-core/lib/minilzo -I$(srcdir)/grub-core/lib/xzembed -I$(srcdir)/grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H';
common_nodist = grub_script.tab.c; common_nodist = grub_script.tab.c;
@ -98,6 +99,7 @@ library = {
common = grub-core/fs/cpio_be.c; common = grub-core/fs/cpio_be.c;
common = grub-core/fs/odc.c; common = grub-core/fs/odc.c;
common = grub-core/fs/newc.c; common = grub-core/fs/newc.c;
common = grub-core/fs/erofs.c;
common = grub-core/fs/ext2.c; common = grub-core/fs/ext2.c;
common = grub-core/fs/fat.c; common = grub-core/fs/fat.c;
common = grub-core/fs/exfat.c; common = grub-core/fs/exfat.c;
@ -206,6 +208,32 @@ program = {
ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
}; };
program = {
name = grub-protect;
mansection = 1;
common = grub-core/kern/emu/argp_common.c;
common = grub-core/osdep/init.c;
common = grub-core/lib/tss2/buffer.c;
common = grub-core/lib/tss2/tss2_mu.c;
common = grub-core/lib/tss2/tpm2_cmd.c;
common = grub-core/commands/tpm2_key_protector/args.c;
common = grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.c;
common = util/grub-protect.c;
common = util/probe.c;
cflags = '-I$(srcdir)/grub-core/lib/tss2 -I$(srcdir)/grub-core/commands/tpm2_key_protector';
ldadd = libgrubmods.a;
ldadd = libgrubgcry.a;
ldadd = libgrubkern.a;
ldadd = grub-core/lib/gnulib/libgnu.a;
ldadd = '$(LIBTASN1)';
ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
condition = COND_GRUB_PROTECT;
};
program = { program = {
name = grub-mkrelpath; name = grub-mkrelpath;
mansection = 1; mansection = 1;
@ -763,6 +791,12 @@ script = {
dependencies = 'garbage-gen$(BUILD_EXEEXT)'; dependencies = 'garbage-gen$(BUILD_EXEEXT)';
}; };
script = {
testcase = native;
name = erofs_test;
common = tests/erofs_test.in;
};
script = { script = {
testcase = native; testcase = native;
name = ext234_test; name = ext234_test;
@ -1130,6 +1164,12 @@ script = {
common = tests/netboot_test.in; common = tests/netboot_test.in;
}; };
script = {
testcase = nonnative;
name = serial_test;
common = tests/serial_test.in;
};
script = { script = {
testcase = nonnative; testcase = nonnative;
name = pseries_test; name = pseries_test;
@ -1244,6 +1284,18 @@ script = {
common = tests/luks2_test.in; common = tests/luks2_test.in;
}; };
script = {
testcase = native;
name = asn1_test;
common = tests/asn1_test.in;
};
script = {
testcase = native;
name = tpm2_key_protector_test;
common = tests/tpm2_key_protector_test.in;
};
program = { program = {
testcase = native; testcase = native;
name = example_unit_test; name = example_unit_test;

20
NEWS
View file

@ -1,3 +1,23 @@
New in 2.12:
* GCC 13 support.
* clang 14 support.
* binutils 2.38 support.
* Unification of EFI Linux kernel loader across architectures.
* Transition to EFI Linux kernel stub loader for x86 architecture.
* Initial support for Boot Loader Interface.
* Support for dynamic GRUB runtime memory addition using firmware calls.
* PCI and MMIO UARTs support.
* SDL2 support.
* LoongArch support.
* TPM driver fixes.
* Many filesystems fixes.
* Many CVE and Coverity fixes.
* Debugging support improvements.
* Tests improvements.
* Documentation improvements.
* ...and tons of other fixes and cleanups...
New in 2.06: New in 2.06:
* GCC 10 support. * GCC 10 support.

View file

@ -9,7 +9,7 @@ fi
# Detect python # Detect python
if [ -z "$PYTHON" ]; then if [ -z "$PYTHON" ]; then
for i in python3 python; do for i in python3 python3.10 python; do
if command -v "$i" > /dev/null 2>&1; then if command -v "$i" > /dev/null 2>&1; then
PYTHON="$i" PYTHON="$i"
echo "Using $PYTHON..." echo "Using $PYTHON..."
@ -51,6 +51,39 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul
cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x"
done done
echo "Importing libtasn1..."
if [ -d grub-core/lib/libtasn1-grub ]; then
rm -rf grub-core/lib/libtasn1-grub
fi
mkdir -p grub-core/lib/libtasn1-grub/lib
cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib
cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/
if [ -d grub-core/tests/asn1/tests ]; then
rm -rf grub-core/tests/asn1/tests
fi
mkdir grub-core/tests/asn1/tests
cp grub-core/lib/libtasn1/tests/*.[ch] grub-core/tests/asn1/tests
for patch in \
0001-libtasn1-disable-code-not-needed-in-grub.patch \
0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch \
0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch \
0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch \
0005-libtasn1-Use-grub_divmod64-for-division.patch \
0006-libtasn1-fix-the-potential-buffer-overrun.patch \
0007-asn1_test-include-asn1_test.h-only.patch \
0008-asn1_test-rename-the-main-functions-to-the-test-name.patch \
0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch \
0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch \
0011-asn1_test-print-the-error-messages-with-grub_printf.patch \
0012-asn1_test-use-the-grub-specific-functions-and-types.patch \
0013-asn1_test-enable-the-testcase-only-when-GRUB_LONG_MA.patch ; do
patch -p1 -i grub-core/lib/libtasn1-patches/$patch
done
echo "Generating Automake input..." echo "Generating Automake input..."
# Automake doesn't like including files from a path outside the project. # Automake doesn't like including files from a path outside the project.

View file

@ -68,7 +68,7 @@ SKIP_PO=t
buildreq="\ buildreq="\
autoconf 2.64 autoconf 2.64
automake 1.14 automake 1.14
gettext 0.18.3 gettext -
git 1.5.5 git 1.5.5
patch - patch -
tar - tar -
@ -84,7 +84,14 @@ bootstrap_post_import_hook () {
# Instead of patching our gnulib and therefore maintaining a fork, submit # Instead of patching our gnulib and therefore maintaining a fork, submit
# changes to gnulib and update the hash above when they've merged. Do not # changes to gnulib and update the hash above when they've merged. Do not
# add new patches here. # add new patches here.
patch -d grub-core/lib/gnulib -p2 < grub-core/lib/gnulib-patches/fix-width.patch for patchname in fix-width \
fix-regcomp-resource-leak \
fix-regexec-resource-leak \
fix-gcc-15-compile \
fix-unused-value; do
patch -d grub-core/lib/gnulib -p2 \
< "grub-core/lib/gnulib-patches/$patchname.patch"
done
for patchname in \ for patchname in \
0001-Support-POTFILES-shell \ 0001-Support-POTFILES-shell \

View file

@ -75,7 +75,7 @@ grubconfdir = $(sysconfdir)/grub.d
platformdir = $(pkglibdir)/$(target_cpu)-$(platform) platformdir = $(pkglibdir)/$(target_cpu)-$(platform)
starfielddir = $(pkgdatadir)/themes/starfield starfielddir = $(pkgdatadir)/themes/starfield
CFLAGS_GNULIB = -Wno-undef -Wno-sign-compare -Wno-unused -Wno-unused-parameter -Wno-redundant-decls -Wno-unreachable-code -Wno-conversion CFLAGS_GNULIB = -Wno-undef -Wno-sign-compare -Wno-unused -Wno-unused-parameter -Wno-redundant-decls -Wno-unreachable-code -Wno-conversion -Wno-error=attributes
CPPFLAGS_GNULIB = -I$(top_builddir)/grub-core/lib/gnulib -I$(top_srcdir)/grub-core/lib/gnulib CPPFLAGS_GNULIB = -I$(top_builddir)/grub-core/lib/gnulib -I$(top_srcdir)/grub-core/lib/gnulib
CFLAGS_POSIX = -fno-builtin CFLAGS_POSIX = -fno-builtin
@ -111,6 +111,7 @@ MOD_FILES =
MODULE_FILES = MODULE_FILES =
MARKER_FILES = MARKER_FILES =
KERNEL_HEADER_FILES = KERNEL_HEADER_FILES =
EXTRA_DEPS =
bin_SCRIPTS = bin_SCRIPTS =
bin_PROGRAMS = bin_PROGRAMS =

View file

@ -29,12 +29,19 @@ EXTRA_DIST += grub-core/genemuinit.sh
EXTRA_DIST += grub-core/genemuinitheader.sh EXTRA_DIST += grub-core/genemuinitheader.sh
EXTRA_DIST += grub-core/lib/gnulib-patches/fix-width.patch EXTRA_DIST += grub-core/lib/gnulib-patches/fix-width.patch
EXTRA_DIST += grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch
EXTRA_DIST += grub-core/lib/gnulib-patches/fix-regexec-resource-leak.patch
EXTRA_DIST += grub-core/lib/gnulib-patches/fix-gcc-15-compile.patch
EXTRA_DIST += grub-core/lib/gnulib-patches/fix-unused-value.patch
EXTRA_DIST += grub-core/lib/libgcrypt EXTRA_DIST += grub-core/lib/libgcrypt
EXTRA_DIST += grub-core/lib/libgcrypt-grub/mpi/generic EXTRA_DIST += grub-core/lib/libgcrypt-grub/mpi/generic
EXTRA_DIST += $(shell find $(top_srcdir)/include -name '*.h') EXTRA_DIST += $(shell find $(top_srcdir)/include -name '*.h')
EXTRA_DIST += $(shell find $(top_srcdir)/grub-core/lib -name '*.h') EXTRA_DIST += $(shell find $(top_srcdir)/grub-core/lib -name '*.h')
EXTRA_DIST += grub-core/efiemu/runtime/config.h EXTRA_DIST += grub-core/efiemu/runtime/config.h
EXTRA_DIST += grub-core/tests/asn1/asn1_test.h
EXTRA_DIST += $(shell find $(top_srcdir)/grub-core/tests/asn1/tests -name '*.h')
EXTRA_DIST += $(shell find $(top_srcdir)/grub-core/commands/tpm2_key_protector -name '*.h')
EXTRA_DIST += grub-core/lib/LzmaDec.c EXTRA_DIST += grub-core/lib/LzmaDec.c

View file

@ -64,6 +64,8 @@
# define GRUB_TARGET_CPU "@GRUB_TARGET_CPU@" # define GRUB_TARGET_CPU "@GRUB_TARGET_CPU@"
# define GRUB_PLATFORM "@GRUB_PLATFORM@" # define GRUB_PLATFORM "@GRUB_PLATFORM@"
# define GRUB_STACK_PROTECTOR_INIT @GRUB_STACK_PROTECTOR_INIT@
# define RE_ENABLE_I18N 1 # define RE_ENABLE_I18N 1
# define _GNU_SOURCE 1 # define _GNU_SOURCE 1

View file

@ -34,7 +34,7 @@ dnl "TARGET_" (such as TARGET_CC, TARGET_CFLAGS, etc.) are used for
dnl the target type. See INSTALL for full list of variables and dnl the target type. See INSTALL for full list of variables and
dnl description of the relationships between them. dnl description of the relationships between them.
AC_INIT([GRUB],[2.12~rc1],[bug-grub@gnu.org]) AC_INIT([GRUB],[2.13],[bug-grub@gnu.org])
AS_CASE(["$ERROR_PLATFORM_NOT_SUPPORT_SSP"], AS_CASE(["$ERROR_PLATFORM_NOT_SUPPORT_SSP"],
[n | no | nO | N | No | NO], [ERROR_PLATFORM_NOT_SUPPORT_SSP=no], [n | no | nO | N | No | NO], [ERROR_PLATFORM_NOT_SUPPORT_SSP=no],
@ -76,6 +76,7 @@ grub_TRANSFORM([grub-mkpasswd-pbkdf2])
grub_TRANSFORM([grub-mkrelpath]) grub_TRANSFORM([grub-mkrelpath])
grub_TRANSFORM([grub-mkrescue]) grub_TRANSFORM([grub-mkrescue])
grub_TRANSFORM([grub-probe]) grub_TRANSFORM([grub-probe])
grub_TRANSFORM([grub-protect])
grub_TRANSFORM([grub-reboot]) grub_TRANSFORM([grub-reboot])
grub_TRANSFORM([grub-script-check]) grub_TRANSFORM([grub-script-check])
grub_TRANSFORM([grub-set-default]) grub_TRANSFORM([grub-set-default])
@ -802,6 +803,17 @@ if test "x$target_cpu" = xmips || test "x$target_cpu" = xmipsel ; then
if test "x$grub_cv_cc_mflush_func" = xyes; then if test "x$grub_cv_cc_mflush_func" = xyes; then
TARGET_CFLAGS="$TARGET_CFLAGS -mflush-func=grub_red_herring" TARGET_CFLAGS="$TARGET_CFLAGS -mflush-func=grub_red_herring"
fi fi
AC_CACHE_CHECK([whether -mno-gpopt works], [grub_cv_cc_mno_gpopt], [
CFLAGS="$TARGET_CFLAGS -mno-gpopt -Werror"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
[grub_cv_cc_mno_gpopt=yes],
[grub_cv_cc_mno_gpopt=no])
])
if test "x$grub_cv_cc_mno_gpopt" = xyes; then
TARGET_CFLAGS="$TARGET_CFLAGS -mno-gpopt"
fi
fi fi
@ -901,6 +913,31 @@ if test "x$target_cpu" = xloongarch64; then
TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs" TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
fi fi
if test "x$target_cpu" = xriscv64 || test "x$target_cpu" = xriscv32; then
AC_CACHE_CHECK([for no-relax options], grub_cv_target_cc_mno_relax, [
grub_cv_target_cc_mno_relax=no
for cand in "-mno-relax" "-Wa,-mno-relax"; do
if test x"$grub_cv_target_cc_mno_relax" != xno ; then
break
fi
CFLAGS="$TARGET_CFLAGS $cand -Werror"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
asm (".globl start; start:");
void __main (void);
void __main (void) {}
int main (void);
]], [[]])], [grub_cv_target_cc_mno_relax="$cand"], [])
done
])
CFLAGS="$TARGET_CFLAGS"
if test x"$grub_cv_target_cc_mno_relax" != xno ; then
TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mno_relax"
TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_mno_relax"
fi
fi
# GRUB doesn't use float or doubles at all. Yet some toolchains may decide # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
# that floats are a good fit to run instead of what's written in the code. # that floats are a good fit to run instead of what's written in the code.
# Given that floating point unit is disabled (if present to begin with) # Given that floating point unit is disabled (if present to begin with)
@ -1020,6 +1057,19 @@ if test x"$target_cpu" = xsparc64 ; then
TARGET_LDFLAGS="$TARGET_LDFLAGS $grub_cv_target_cc_mno_relax" TARGET_LDFLAGS="$TARGET_LDFLAGS $grub_cv_target_cc_mno_relax"
fi fi
# The backtrace module relies on frame pointers and the default optimization
# level, -Os, omits them. Make sure they are enabled.
AC_CACHE_CHECK([whether -fno-omit-frame-pointer works], [grub_cv_cc_fno_omit_frame_pointer], [
CFLAGS="$TARGET_CFLAGS -fno-omit-frame-pointer"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
[grub_cv_cc_fno_omit_frame_pointer=yes],
[grub_cv_cc_fno_omit_frame_pointer=no])
])
if test "x$grub_cv_cc_fno_omit_frame_pointer" = xyes; then
TARGET_CFLAGS="$TARGET_CFLAGS -fno-omit-frame-pointer"
fi
# By default, GCC 4.4 generates .eh_frame sections containing unwind # By default, GCC 4.4 generates .eh_frame sections containing unwind
# information in some cases where it previously did not. GRUB doesn't need # information in some cases where it previously did not. GRUB doesn't need
# these and they just use up vital space. Restore the old compiler # these and they just use up vital space. Restore the old compiler
@ -1262,8 +1312,7 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
LDFLAGS="$TARGET_LDFLAGS" LDFLAGS="$TARGET_LDFLAGS"
if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 \ if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
|| test "$target_cpu" = loongarch64 ; then
# Use large model to support 4G memory # Use large model to support 4G memory
AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [ AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
CFLAGS="$TARGET_CFLAGS -mcmodel=large" CFLAGS="$TARGET_CFLAGS -mcmodel=large"
@ -1426,6 +1475,28 @@ else
AC_MSG_ERROR([invalid value $enable_stack_protector for --enable-stack-protector]) AC_MSG_ERROR([invalid value $enable_stack_protector for --enable-stack-protector])
fi fi
TARGET_CPPFLAGS="$TARGET_CPPFLAGS -DGRUB_STACK_PROTECTOR=1" TARGET_CPPFLAGS="$TARGET_CPPFLAGS -DGRUB_STACK_PROTECTOR=1"
if test -n "$SOURCE_DATE_EPOCH"; then
GRUB_STACK_PROTECTOR_INIT="0x00f2b7e2$(printf "%x" "$SOURCE_DATE_EPOCH" | sed 's/.*\(........\)$/\1/')"
elif test -r /dev/urandom; then
# Generate the 8 byte stack protector canary at build time if /dev/urandom
# is able to be read. The first byte should be NUL to filter out string
# buffer overflow attacks.
GRUB_STACK_PROTECTOR_INIT="$($PYTHON -c 'import codecs; rf=open("/dev/urandom", "rb"); print("0x00"+codecs.encode(rf.read(7), "hex").decode("ascii"))')"
else
# Some hosts may not have a urandom, e.g. Windows, so use statically
# generated random bytes
GRUB_STACK_PROTECTOR_INIT="0x00f2b7e2f193b25c"
fi
if test x"$target_m32" = x1 ; then
# Make sure that the canary default value is 24-bits by only using the
# lower 3 bytes on 32 bit systems. This allows the upper byte to be NUL
# to filter out string buffer overflow attacks.
GRUB_STACK_PROTECTOR_INIT="0x00$(echo "$GRUB_STACK_PROTECTOR_INIT" | sed 's/.*\(......\)$/\1/')"
fi
AC_SUBST([GRUB_STACK_PROTECTOR_INIT])
fi fi
CFLAGS="$TARGET_CFLAGS" CFLAGS="$TARGET_CFLAGS"
@ -1609,7 +1680,7 @@ if test "$platform" = emu; then
AC_SUBST(HAVE_SDL2)], AC_SUBST(HAVE_SDL2)],
[grub_emu_sdl2_excuse="libSDL2 libraries are required to build \`grub-emu' with SDL2 support"]) [grub_emu_sdl2_excuse="libSDL2 libraries are required to build \`grub-emu' with SDL2 support"])
[fi] [fi]
if test x"enable_grub_emu_sdl2" = xyes && test x"$grub_emu_sdl2_excuse" != x ; then if test x"$enable_grub_emu_sdl2" = xyes && test x"$grub_emu_sdl2_excuse" != x ; then
AC_MSG_ERROR([SDL2 support for grub-emu was explicitly requested but can't be compiled ($grub_emu_sdl2_excuse)]) AC_MSG_ERROR([SDL2 support for grub-emu was explicitly requested but can't be compiled ($grub_emu_sdl2_excuse)])
fi fi
if test x"$grub_emu_sdl2_excuse" = x ; then if test x"$grub_emu_sdl2_excuse" = x ; then
@ -1704,6 +1775,9 @@ if test x"$grub_mkfont_excuse" = x ; then
CPPFLAGS="$SAVED_CPPFLAGS" CPPFLAGS="$SAVED_CPPFLAGS"
LIBS="$SAVED_LIBS" LIBS="$SAVED_LIBS"
], [grub_mkfont_excuse=["need freetype2 library"]]) ], [grub_mkfont_excuse=["need freetype2 library"]])
if test x"$grub_mkfont_excuse" = x && test x"$host_kernel" = xnetbsd ; then
FREETYPE_LIBS="$FREETYPE_LIBS -Wl,-R,/usr/pkg/lib" ;
fi
fi fi
if test x"$enable_grub_mkfont" = xyes && test x"$grub_mkfont_excuse" != x ; then if test x"$enable_grub_mkfont" = xyes && test x"$grub_mkfont_excuse" != x ; then
@ -1758,6 +1832,11 @@ if test x"$grub_build_mkfont_excuse" = x ; then
LIBS="$SAVED_LIBS" LIBS="$SAVED_LIBS"
CPPFLAGS="$SAVED_CPPFLAGS_2" CPPFLAGS="$SAVED_CPPFLAGS_2"
], [grub_build_mkfont_excuse=["need freetype2 library"]]) ], [grub_build_mkfont_excuse=["need freetype2 library"]])
if test x"$grub_build_mkfont_excuse" = x ; then
case x"$build_os" in
xnetbsd*) BUILD_FREETYPE_LIBS="$BUILD_FREETYPE_LIBS -Wl,-R,/usr/pkg/lib" ;;
esac
fi
PKG_CONFIG="$SAVED_PKG_CONFIG" PKG_CONFIG="$SAVED_PKG_CONFIG"
fi fi
@ -1784,8 +1863,6 @@ CPPFLAGS="$SAVED_CPPFLAGS"
LDFLAGS="$SAVED_LDFLAGS" LDFLAGS="$SAVED_LDFLAGS"
DJVU_FONT_SOURCE=
starfield_excuse= starfield_excuse=
AC_ARG_ENABLE([grub-themes], AC_ARG_ENABLE([grub-themes],
@ -1799,19 +1876,28 @@ if test x"$starfield_excuse" = x && test x"$enable_build_grub_mkfont" = xno ; th
starfield_excuse="No build-time grub-mkfont" starfield_excuse="No build-time grub-mkfont"
fi fi
if test x"$starfield_excuse" = x; then AC_ARG_WITH([dejavufont],
for ext in pcf pcf.gz bdf bdf.gz ttf ttf.gz; do AS_HELP_STRING([--with-dejavufont=FILE],
for dir in . /usr/src /usr/share/fonts/X11/misc /usr/share/fonts/truetype/ttf-dejavu /usr/share/fonts/dejavu /usr/share/fonts/truetype; do [set the DejeVu source [[guessed]]]))
if test -f "$dir/DejaVuSans.$ext"; then
DJVU_FONT_SOURCE="$dir/DejaVuSans.$ext"
break 2
fi
done
done
if test "x$DJVU_FONT_SOURCE" = x; then if test "x$with_dejavufont" = x; then
starfield_excuse="No DejaVu found" # search in well-known directories
fi if test x"$starfield_excuse" = x; then
for ext in pcf pcf.gz bdf bdf.gz ttf ttf.gz; do
for dir in . /usr/src /usr/share/fonts/X11/misc /usr/share/fonts/truetype/ttf-dejavu /usr/share/fonts/dejavu /usr/share/fonts/truetype /usr/pkg/share/fonts/X11/TTF /usr/local/share/fonts/dejavu /usr/X11R6/lib/X11/fonts/TTF /usr/share/fonts/dejavu-sans-fonts /usr/share/fonts/truetype/dejavu; do
if test -f "$dir/DejaVuSans.$ext"; then
DJVU_FONT_SOURCE="$dir/DejaVuSans.$ext"
break 2
fi
done
done
if test "x$DJVU_FONT_SOURCE" = x; then
starfield_excuse="No DejaVu found"
fi
fi
else
DJVU_FONT_SOURCE="$with_dejavufont"
fi fi
if test x"$enable_grub_themes" = xyes && test x"$starfield_excuse" != x; then if test x"$enable_grub_themes" = xyes && test x"$starfield_excuse" != x; then
@ -1820,21 +1906,28 @@ fi
AC_SUBST([DJVU_FONT_SOURCE]) AC_SUBST([DJVU_FONT_SOURCE])
FONT_SOURCE= AC_ARG_WITH([unifont],
AS_HELP_STRING([--with-unifont=FILE],
[set the unifont source [[guessed]]]))
for ext in pcf pcf.gz bdf bdf.gz ttf ttf.gz; do if test "x$with_unifont" = x; then
for dir in . /usr/src /usr/share/fonts/X11/misc /usr/share/fonts/unifont /usr/share/fonts/uni /usr/share/fonts/truetype/unifont /usr/share/fonts/misc; do # search in well-known directories
if test -f "$dir/unifont.$ext"; then for ext in pcf pcf.gz bdf bdf.gz ttf ttf.gz otf otf.gz; do
md5="$(md5sum "$dir/unifont.$ext"|awk '{ print $1; }')" for dir in . /usr/src /usr/share/fonts/X11/misc /usr/share/fonts/unifont /usr/share/fonts/uni /usr/share/fonts/truetype/unifont /usr/share/fonts/misc /usr/pkg/share/fonts/X11/misc /usr/local/share/fonts/gnu-unifont /usr/local/share/fonts/unifont; do
# PCF and BDF from version 6.3 isn't hanled properly by libfreetype. if test -f "$dir/unifont.$ext"; then
if test "$md5" = 0a54834d2788c83886a3e1785a6a1e61 || test "$md5" = 28f2565c7a41d8d407e2551159385edb || test "$md5" = dae5e588461b3b92b87b6ffee734f936 || test "$md5" = 4a3d687aa5bb329ed05f4263a1016791 ; then md5="$(md5sum "$dir/unifont.$ext"|awk '{ print $1; }')"
continue # PCF and BDF from version 6.3 isn't hanled properly by libfreetype.
if test "$md5" = 0a54834d2788c83886a3e1785a6a1e61 || test "$md5" = 28f2565c7a41d8d407e2551159385edb || test "$md5" = dae5e588461b3b92b87b6ffee734f936 || test "$md5" = 4a3d687aa5bb329ed05f4263a1016791 ; then
continue
fi
FONT_SOURCE="$dir/unifont.$ext"
break 2
fi fi
FONT_SOURCE="$dir/unifont.$ext" done
break 2
fi
done done
done else
FONT_SOURCE="$with_unifont"
fi
if test x"$enable_build_grub_mkfont" = xno ; then if test x"$enable_build_grub_mkfont" = xno ; then
FONT_SOURCE= FONT_SOURCE=
@ -1971,8 +2064,19 @@ fi
if test x"$libzfs_excuse" = x ; then if test x"$libzfs_excuse" = x ; then
AC_CHECK_LIB([nvpair], [nvlist_lookup_string], AC_CHECK_LIB([nvpair], [nvlist_lookup_string],
[], [have_normal_nvpair=yes],
[libzfs_excuse="need nvpair library"]) [have_normal_nvpair=no])
if test x"$have_normal_nvpair" = xno ; then
AC_CHECK_LIB([nvpair], [opensolaris_nvlist_lookup_string],
[have_prefixed_nvpair=yes],
[have_prefixed_nvpair=no])
if test x"$have_prefixed_nvpair" = xyes ; then
AC_DEFINE([GRUB_UTIL_NVPAIR_IS_PREFIXED], [1],
[Define to 1 if libnvpair symbols are prefixed with opensolaris_.])
else
libzfs_excuse="need nvpair library"
fi
fi
fi fi
if test x"$enable_libzfs" = xyes && test x"$libzfs_excuse" != x ; then if test x"$enable_libzfs" = xyes && test x"$libzfs_excuse" != x ; then
@ -1982,16 +2086,37 @@ fi
if test x"$libzfs_excuse" = x ; then if test x"$libzfs_excuse" = x ; then
# We need both libzfs and libnvpair for a successful build. # We need both libzfs and libnvpair for a successful build.
LIBZFS="-lzfs" LIBZFS="-lzfs"
AC_DEFINE([HAVE_LIBZFS], [1],
[Define to 1 if you have the ZFS library.])
LIBNVPAIR="-lnvpair" LIBNVPAIR="-lnvpair"
AC_DEFINE([HAVE_LIBNVPAIR], [1], AC_DEFINE([USE_LIBZFS], [1],
[Define to 1 if you have the NVPAIR library.]) [Define to 1 if ZFS library should be used.])
fi fi
AC_SUBST([LIBZFS]) AC_SUBST([LIBZFS])
AC_SUBST([LIBNVPAIR]) AC_SUBST([LIBNVPAIR])
AC_ARG_ENABLE([grub-protect],
[AS_HELP_STRING([--enable-grub-protect],
[build and install the `grub-protect' utility (default=guessed)])])
if test x"$enable_grub_protect" = xno ; then
grub_protect_excuse="explicitly disabled"
fi
LIBTASN1=
if test x"$grub_protect_excuse" = x ; then
AC_CHECK_LIB([tasn1], [asn1_write_value], [LIBTASN1="-ltasn1"], [grub_protect_excuse="need libtasn1 library"])
fi
AC_SUBST([LIBTASN1])
if test x"$enable_grub_protect" = xyes && test x"$grub_protect_excuse" != x ; then
AC_MSG_ERROR([grub-protect was explicitly requested but can't be compiled ($grub_protect_excuse)])
fi
if test x"$grub_protect_excuse" = x ; then
enable_grub_protect=yes
else
enable_grub_protect=no
fi
AC_SUBST([enable_grub_protect])
LIBS="" LIBS=""
AC_SUBST([FONT_SOURCE]) AC_SUBST([FONT_SOURCE])
@ -2108,6 +2233,7 @@ AM_CONDITIONAL([COND_GRUB_EMU_SDL], [test x$enable_grub_emu_sdl = xyes])
AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes]) AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes])
AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes]) AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes])
AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes]) AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes])
AM_CONDITIONAL([COND_GRUB_PROTECT], [test x$enable_grub_protect = xyes])
AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x]) AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x])
if test x$FONT_SOURCE != x ; then if test x$FONT_SOURCE != x ; then
HAVE_FONT_SOURCE=1 HAVE_FONT_SOURCE=1
@ -2235,6 +2361,11 @@ echo grub-mount: Yes
else else
echo grub-mount: No "($grub_mount_excuse)" echo grub-mount: No "($grub_mount_excuse)"
fi fi
if [ x"$grub_protect_excuse" = x ]; then
echo grub-protect: Yes
else
echo grub-protect: No "($grub_protect_excuse)"
fi
if [ x"$starfield_excuse" = x ]; then if [ x"$starfield_excuse" = x ]; then
echo starfield theme: Yes echo starfield theme: Yes
echo With DejaVuSans font from $DJVU_FONT_SOURCE echo With DejaVuSans font from $DJVU_FONT_SOURCE

View file

@ -506,6 +506,7 @@ to update it.
* Gnulib:: * Gnulib::
* jsmn:: * jsmn::
* minilzo:: * minilzo::
* libtasn1::
@end menu @end menu
@node Gnulib @node Gnulib
@ -596,6 +597,40 @@ cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
rm -r minilzo-2.10* rm -r minilzo-2.10*
@end example @end example
@node libtasn1
@section libtasn1
libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as
specified by the X.680 ITU-T recommendation) parsing and structures management,
and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding
functions.
To upgrade to a new version of the libtasn1 library, download the release
tarball and copy the files into the target directory:
@example
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/lib/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*
@end example
After upgrading the library, it may be necessary to apply the patches in
@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with
GRUB. These patches were needed to use the current version of libtasn1. The
existing patches may not apply cleanly, apply at all, or even be needed for a
newer version of the library, and other patches may be needed due to changes in
the newer version. If existing patches need to be refreshed to apply cleanly,
please include updated patches as part of the a patch set sent to the list.
If new patches are needed or existing patches are not needed, also please send
additions or removals as part of any patch set upgrading libtasn1.
@node Debugging @node Debugging
@chapter Debugging @chapter Debugging
@ -660,10 +695,11 @@ GDB is started from the @file{grub-core} directory in the GRUB2 build
directory. GRUB2 developers have made this more simple by including a GDB directory. GRUB2 developers have made this more simple by including a GDB
script which does much of the setup. This file is at @file{grub-core/gdb_grub} script which does much of the setup. This file is at @file{grub-core/gdb_grub}
in the build directory and is also installed via @command{make install}. in the build directory and is also installed via @command{make install}.
If not building GRUB, the distribution may have a package which installs When using a pre-built GRUB, the distribution may have a package which installs
this GDB script along with debug symbol binaries, such as Debian's this GDB script along with debug symbol binaries, such as Debian's
@samp{grub-pc-dbg} package. The GDB script is intended to be used @samp{grub-pc-dbg} package. The GDB script is intended to be used
like so, assuming: like so, assuming that @samp{/path/to/script} is the path to the directory
containing the gdb_grub script and debug symbol files:
@example @example
cd $(dirname /path/to/script/gdb_grub) cd $(dirname /path/to/script/gdb_grub)
@ -787,6 +823,11 @@ out of its way to avoid using hardware breakpoints internally and uses them as
briefly as possible when needed, thus allowing the user to have a maximal briefly as possible when needed, thus allowing the user to have a maximal
number at their disposal. number at their disposal.
@menu
* OVMF debug log::
* Using the gdbinfo command::
@end menu
@node OVMF debug log @node OVMF debug log
@subsection OVMF debug log @subsection OVMF debug log

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,4 @@
[NAME]
grub-protect \- protect a disk key with a key protector
[DESCRIPTION]
grub-protect helps to protect a disk encryption key with a specified key protector.

View file

@ -631,7 +631,10 @@ def platform_values(defn, platform, suffix):
def extra_dist(defn): def extra_dist(defn):
return foreach_value(defn, "extra_dist", lambda value: value + " ") return foreach_value(defn, "extra_dist", lambda value: value + " ")
def platform_sources(defn, p): return platform_values(defn, p, "") def extra_dep(defn):
return foreach_value(defn, "depends", lambda value: value + " ")
def platform_sources(defn, p): return platform_values(defn, p, "_head") + platform_values(defn, p, "")
def platform_nodist_sources(defn, p): return platform_values(defn, p, "_nodist") def platform_nodist_sources(defn, p): return platform_values(defn, p, "_nodist")
def platform_startup(defn, p): return platform_specific_values(defn, p, "_startup", "startup") def platform_startup(defn, p): return platform_specific_values(defn, p, "_startup", "startup")
@ -657,7 +660,7 @@ def first_time(defn, snippet):
def is_platform_independent(defn): def is_platform_independent(defn):
if 'enable' in defn: if 'enable' in defn:
return False return False
for suffix in [ "", "_nodist" ]: for suffix in [ "", "_head", "_nodist" ]:
template = platform_values(defn, GRUB_PLATFORMS[0], suffix) template = platform_values(defn, GRUB_PLATFORMS[0], suffix)
for platform in GRUB_PLATFORMS[1:]: for platform in GRUB_PLATFORMS[1:]:
if template != platform_values(defn, platform, suffix): if template != platform_values(defn, platform, suffix):
@ -699,6 +702,10 @@ def module(defn, platform):
gvar_add("MOD_FILES", name + ".mod") gvar_add("MOD_FILES", name + ".mod")
gvar_add("MARKER_FILES", name + ".marker") gvar_add("MARKER_FILES", name + ".marker")
gvar_add("CLEANFILES", name + ".marker") gvar_add("CLEANFILES", name + ".marker")
for dep in defn.find_all("depends"):
gvar_add("EXTRA_DEPS", "depends " + name + " " + dep + ":")
output(""" output("""
""" + name + """.marker: $(""" + cname(defn) + """_SOURCES) $(nodist_""" + cname(defn) + """_SOURCES) """ + name + """.marker: $(""" + cname(defn) + """_SOURCES) $(nodist_""" + cname(defn) + """_SOURCES)
$(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(""" + cname(defn) + """_CPPFLAGS) $(CPPFLAGS) $^ > $@.new || (rm -f $@; exit 1) $(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(""" + cname(defn) + """_CPPFLAGS) $(CPPFLAGS) $^ > $@.new || (rm -f $@; exit 1)

View file

@ -90,6 +90,7 @@ endif
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/key_protector.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
@ -153,6 +154,7 @@ endif
if COND_i386_ieee1275 if COND_i386_ieee1275
KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
@ -240,6 +242,7 @@ endif
if COND_powerpc_ieee1275 if COND_powerpc_ieee1275
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
@ -452,8 +455,11 @@ crypto.lst: $(srcdir)/lib/libgcrypt-grub/cipher/crypto.lst
platform_DATA += crypto.lst platform_DATA += crypto.lst
CLEANFILES += crypto.lst CLEANFILES += crypto.lst
syminfo.lst: gensyminfo.sh kernel_syms.lst $(MODULE_FILES) extra_deps.lst:
cat kernel_syms.lst > $@.new @echo $(EXTRA_DEPS) | sed "s/\s*:\s*/\n/g" > $@
syminfo.lst: gensyminfo.sh kernel_syms.lst extra_deps.lst $(MODULE_FILES)
cat kernel_syms.lst extra_deps.lst > $@.new
for m in $(MODULE_FILES); do \ for m in $(MODULE_FILES); do \
sh $< $$m >> $@.new || exit 1; \ sh $< $$m >> $@.new || exit 1; \
done done
@ -463,7 +469,7 @@ syminfo.lst: gensyminfo.sh kernel_syms.lst $(MODULE_FILES)
moddep.lst: syminfo.lst genmoddep.awk video.lst moddep.lst: syminfo.lst genmoddep.awk video.lst
cat $< | sort | $(AWK) -f $(srcdir)/genmoddep.awk > $@ || (rm -f $@; exit 1) cat $< | sort | $(AWK) -f $(srcdir)/genmoddep.awk > $@ || (rm -f $@; exit 1)
platform_DATA += moddep.lst platform_DATA += moddep.lst
CLEANFILES += config.log syminfo.lst moddep.lst CLEANFILES += config.log syminfo.lst moddep.lst extra_deps.lst
$(MOD_FILES): %.mod : genmod.sh moddep.lst %.module$(EXEEXT) build-grub-module-verifier$(BUILD_EXEEXT) $(MOD_FILES): %.mod : genmod.sh moddep.lst %.module$(EXEEXT) build-grub-module-verifier$(BUILD_EXEEXT)
TARGET_OBJ2ELF=@TARGET_OBJ2ELF@ sh $^ $@ TARGET_OBJ2ELF=@TARGET_OBJ2ELF@ sh $^ $@

View file

@ -536,7 +536,7 @@ image = {
image = { image = {
name = xz_decompress; name = xz_decompress;
mips = boot/mips/startup_raw.S; mips_head = boot/mips/startup_raw.S;
common = boot/decompressor/minilib.c; common = boot/decompressor/minilib.c;
common = boot/decompressor/xz.c; common = boot/decompressor/xz.c;
common = lib/xzembed/xz_dec_bcj.c; common = lib/xzembed/xz_dec_bcj.c;
@ -554,7 +554,7 @@ image = {
image = { image = {
name = none_decompress; name = none_decompress;
mips = boot/mips/startup_raw.S; mips_head = boot/mips/startup_raw.S;
common = boot/decompressor/none.c; common = boot/decompressor/none.c;
cppflags = '-DGRUB_EMBED_DECOMPRESSOR=1'; cppflags = '-DGRUB_EMBED_DECOMPRESSOR=1';
@ -714,12 +714,16 @@ module = {
name = cmostest; name = cmostest;
common = commands/i386/cmostest.c; common = commands/i386/cmostest.c;
enable = cmos; enable = cmos;
enable = i386_efi;
enable = x86_64_efi;
}; };
module = { module = {
name = cmosdump; name = cmosdump;
common = commands/i386/cmosdump.c; common = commands/i386/cmosdump.c;
enable = cmos; enable = cmos;
enable = i386_efi;
enable = x86_64_efi;
}; };
module = { module = {
@ -1278,6 +1282,11 @@ module = {
common = disk/raid6_recover.c; common = disk/raid6_recover.c;
}; };
module = {
name = key_protector;
common = disk/key_protector.c;
};
module = { module = {
name = scsi; name = scsi;
common = disk/scsi.c; common = disk/scsi.c;
@ -1438,6 +1447,11 @@ module = {
common = fs/odc.c; common = fs/odc.c;
}; };
module = {
name = erofs;
common = fs/erofs.c;
};
module = { module = {
name = ext2; name = ext2;
common = fs/ext2.c; common = fs/ext2.c;
@ -1592,6 +1606,7 @@ module = {
common = fs/zfs/zfs_lz4.c; common = fs/zfs/zfs_lz4.c;
common = fs/zfs/zfs_sha256.c; common = fs/zfs/zfs_sha256.c;
common = fs/zfs/zfs_fletcher.c; common = fs/zfs/zfs_fletcher.c;
cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/zstd';
}; };
module = { module = {
@ -1993,7 +2008,7 @@ module = {
extra_dist = script/yylex.l; extra_dist = script/yylex.l;
extra_dist = script/parser.y; extra_dist = script/parser.y;
cflags = '$(CFLAGS_POSIX) -Wno-redundant-decls'; cflags = '$(CFLAGS_POSIX) -Wno-redundant-decls -Wno-unused-but-set-variable';
cppflags = '$(CPPFLAGS_POSIX)'; cppflags = '$(CPPFLAGS_POSIX)';
}; };
@ -2238,6 +2253,10 @@ module = {
common = tests/videotest_checksum.c; common = tests/videotest_checksum.c;
}; };
/*
* These tests fail depending on the version of unifont. As we don't distribute
* our own unifont it fails for most users. Disable them so that they don't mask
* real failures. They can be reinstated once we solve unifont problem.
module = { module = {
name = gfxterm_menu; name = gfxterm_menu;
common = tests/gfxterm_menu.c; common = tests/gfxterm_menu.c;
@ -2247,6 +2266,7 @@ module = {
name = cmdline_cat_test; name = cmdline_cat_test;
common = tests/cmdline_cat_test.c; common = tests/cmdline_cat_test.c;
}; };
*/
module = { module = {
name = bitmap; name = bitmap;
@ -2552,6 +2572,34 @@ module = {
enable = efi; enable = efi;
}; };
module = {
name = tss2;
common = lib/tss2/buffer.c;
common = lib/tss2/tss2_mu.c;
common = lib/tss2/tpm2_cmd.c;
common = lib/tss2/tss2.c;
efi = lib/efi/tcg2.c;
emu = lib/tss2/tcg2_emu.c;
powerpc_ieee1275 = lib/ieee1275/tcg2.c;
enable = efi;
enable = emu;
enable = powerpc_ieee1275;
cppflags = '-I$(srcdir)/lib/tss2';
};
module = {
name = tpm2_key_protector;
common = commands/tpm2_key_protector/args.c;
common = commands/tpm2_key_protector/module.c;
common = commands/tpm2_key_protector/tpm2key.c;
common = commands/tpm2_key_protector/tpm2key_asn1_tab.c;
/* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */
enable = efi;
enable = emu;
enable = powerpc_ieee1275;
cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub';
};
module = { module = {
name = tr; name = tr;
common = commands/tr.c; common = commands/tr.c;
@ -2594,4 +2642,35 @@ module = {
name = bli; name = bli;
efi = commands/bli.c; efi = commands/bli.c;
enable = efi; enable = efi;
depends = part_gpt;
};
module = {
name = asn1;
common = lib/libtasn1-grub/lib/decoding.c;
common = lib/libtasn1-grub/lib/coding.c;
common = lib/libtasn1-grub/lib/element.c;
common = lib/libtasn1-grub/lib/structure.c;
common = lib/libtasn1-grub/lib/parser_aux.c;
common = lib/libtasn1-grub/lib/gstr.c;
common = lib/libtasn1-grub/lib/errors.c;
common = lib/libtasn1_wrap/wrap.c;
cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)';
/* -Wno-type-limits comes from configure.ac of libtasn1 */
cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits';
};
module = {
name = asn1_test;
common = tests/asn1/tests/CVE-2018-1000654.c;
common = tests/asn1/tests/object-id-decoding.c;
common = tests/asn1/tests/object-id-encoding.c;
common = tests/asn1/tests/octet-string.c;
common = tests/asn1/tests/reproducers.c;
common = tests/asn1/tests/Test_overflow.c;
common = tests/asn1/tests/Test_simple.c;
common = tests/asn1/tests/Test_strings.c;
common = tests/asn1/asn1_test.c;
cflags = '-Wno-uninitialized';
cppflags = '-I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/tests/asn1/';
}; };

View file

@ -218,7 +218,7 @@ enum
#define GRUB_EHCI_TERMINATE (1<<0) #define GRUB_EHCI_TERMINATE (1<<0)
#define GRUB_EHCI_TOGGLE (1<<31) #define GRUB_EHCI_TOGGLE ((grub_uint32_t) 1<<31)
enum enum
{ {

View file

@ -38,6 +38,20 @@
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
enum
{
OPTION_EXCLUDE = 0,
OPTION_LOAD_ONLY,
OPTION_V1,
OPTION_V2,
OPTION_OEMID,
OPTION_OEMTABLE,
OPTION_OEMTABLEREV,
OPTION_OEMTABLECREATOR,
OPTION_OEMTABLECREATORREV,
OPTION_NO_EBDA
};
static const struct grub_arg_option options[] = { static const struct grub_arg_option options[] = {
{"exclude", 'x', 0, {"exclude", 'x', 0,
N_("Don't load host tables specified by comma-separated list."), N_("Don't load host tables specified by comma-separated list."),
@ -490,21 +504,21 @@ grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
if (rsdp) if (rsdp)
{ {
grub_uint32_t *entry_ptr; grub_uint8_t *entry_ptr;
char *exclude = 0; char *exclude = 0;
char *load_only = 0; char *load_only = 0;
char *ptr; char *ptr;
/* RSDT consists of header and an array of 32-bit pointers. */ grub_size_t tbl_addr_size;
struct grub_acpi_table_header *rsdt; struct grub_acpi_table_header *table_head;
exclude = state[0].set ? grub_strdup (state[0].arg) : 0; exclude = state[OPTION_EXCLUDE].set ? grub_strdup (state[OPTION_EXCLUDE].arg) : 0;
if (exclude) if (exclude)
{ {
for (ptr = exclude; *ptr; ptr++) for (ptr = exclude; *ptr; ptr++)
*ptr = grub_tolower (*ptr); *ptr = grub_tolower (*ptr);
} }
load_only = state[1].set ? grub_strdup (state[1].arg) : 0; load_only = state[OPTION_LOAD_ONLY].set ? grub_strdup (state[OPTION_LOAD_ONLY].arg) : 0;
if (load_only) if (load_only)
{ {
for (ptr = load_only; *ptr; ptr++) for (ptr = load_only; *ptr; ptr++)
@ -515,20 +529,31 @@ grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
rev1 = ! rsdp->revision; rev1 = ! rsdp->revision;
rev2 = rsdp->revision; rev2 = rsdp->revision;
if (rev2 && ((struct grub_acpi_table_header *) (grub_addr_t) ((struct grub_acpi_rsdp_v20 *) rsdp)->xsdt_addr) != NULL) if (rev2 && ((struct grub_acpi_table_header *) (grub_addr_t) ((struct grub_acpi_rsdp_v20 *) rsdp)->xsdt_addr) != NULL)
rsdt = (struct grub_acpi_table_header *) (grub_addr_t) ((struct grub_acpi_rsdp_v20 *) rsdp)->xsdt_addr; {
/* XSDT consists of header and an array of 64-bit pointers. */
table_head = (struct grub_acpi_table_header *) (grub_addr_t) ((struct grub_acpi_rsdp_v20 *) rsdp)->xsdt_addr;
tbl_addr_size = sizeof (((struct grub_acpi_rsdp_v20 *) rsdp)->xsdt_addr);
}
else else
rsdt = (struct grub_acpi_table_header *) (grub_addr_t) rsdp->rsdt_addr; {
/* RSDT consists of header and an array of 32-bit pointers. */
table_head = (struct grub_acpi_table_header *) (grub_addr_t) rsdp->rsdt_addr;
tbl_addr_size = sizeof (rsdp->rsdt_addr);
}
/* Load host tables. */ /* Load host tables. */
for (entry_ptr = (grub_uint32_t *) (rsdt + 1); for (entry_ptr = (grub_uint8_t *) (table_head + 1);
entry_ptr < (grub_uint32_t *) (((grub_uint8_t *) rsdt) entry_ptr < (grub_uint8_t *) (((grub_uint8_t *) table_head) + table_head->length);
+ rsdt->length); entry_ptr += tbl_addr_size)
entry_ptr++)
{ {
char signature[5]; char signature[5];
struct efiemu_acpi_table *table; struct efiemu_acpi_table *table;
struct grub_acpi_table_header *curtable struct grub_acpi_table_header *curtable;
= (struct grub_acpi_table_header *) (grub_addr_t) *entry_ptr; if (tbl_addr_size == sizeof (rsdp->rsdt_addr))
curtable = (struct grub_acpi_table_header *) (grub_addr_t) *((grub_uint32_t *) entry_ptr);
else
curtable = (struct grub_acpi_table_header *) (grub_addr_t) *((grub_uint64_t *) entry_ptr);
signature[4] = 0; signature[4] = 0;
for (i = 0; i < 4;i++) for (i = 0; i < 4;i++)
signature[i] = grub_tolower (curtable->signature[i]); signature[i] = grub_tolower (curtable->signature[i]);
@ -612,26 +637,26 @@ grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
} }
/* Does user specify versions to generate? */ /* Does user specify versions to generate? */
if (state[2].set || state[3].set) if (state[OPTION_V1].set || state[OPTION_V2].set)
{ {
rev1 = state[2].set; rev1 = state[OPTION_V1].set;
if (state[3].set) if (state[OPTION_V2].set)
rev2 = rev2 ? : 2; rev2 = rev2 ? : 2;
else else
rev2 = 0; rev2 = 0;
} }
/* Does user override root header information? */ /* Does user override root header information? */
if (state[4].set) if (state[OPTION_OEMID].set)
grub_strncpy (root_oemid, state[4].arg, sizeof (root_oemid)); grub_strncpy (root_oemid, state[OPTION_OEMID].arg, sizeof (root_oemid));
if (state[5].set) if (state[OPTION_OEMTABLE].set)
grub_strncpy (root_oemtable, state[5].arg, sizeof (root_oemtable)); grub_strncpy (root_oemtable, state[OPTION_OEMTABLE].arg, sizeof (root_oemtable));
if (state[6].set) if (state[OPTION_OEMTABLEREV].set)
root_oemrev = grub_strtoul (state[6].arg, 0, 0); root_oemrev = grub_strtoul (state[OPTION_OEMTABLEREV].arg, 0, 0);
if (state[7].set) if (state[OPTION_OEMTABLECREATOR].set)
grub_strncpy (root_creator_id, state[7].arg, sizeof (root_creator_id)); grub_strncpy (root_creator_id, state[OPTION_OEMTABLECREATOR].arg, sizeof (root_creator_id));
if (state[8].set) if (state[OPTION_OEMTABLECREATORREV].set)
root_creator_rev = grub_strtoul (state[8].arg, 0, 0); root_creator_rev = grub_strtoul (state[OPTION_OEMTABLECREATORREV].arg, 0, 0);
/* Load user tables */ /* Load user tables */
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
@ -747,7 +772,7 @@ grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
acpi_tables = 0; acpi_tables = 0;
#if defined (__i386__) || defined (__x86_64__) #if defined (__i386__) || defined (__x86_64__)
if (! state[9].set) if (! state[OPTION_NO_EBDA].set)
{ {
grub_err_t err; grub_err_t err;
err = grub_acpi_create_ebda (); err = grub_acpi_create_ebda ();

View file

@ -48,6 +48,22 @@ get_part_uuid (const char *device_name, char **part_uuid)
if (device == NULL) if (device == NULL)
return grub_error (grub_errno, N_("cannot open device: %s"), device_name); return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
if (device->disk == NULL)
{
grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
*part_uuid = NULL;
grub_device_close (device);
return GRUB_ERR_NONE;
}
if (device->disk->partition == NULL)
{
grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
*part_uuid = NULL;
grub_device_close (device);
return GRUB_ERR_NONE;
}
disk = grub_disk_open (device->disk->name); disk = grub_disk_open (device->disk->name);
if (disk == NULL) if (disk == NULL)
{ {
@ -99,7 +115,7 @@ set_loader_device_part_uuid (void)
status = get_part_uuid (device_name, &part_uuid); status = get_part_uuid (device_name, &part_uuid);
if (status == GRUB_ERR_NONE) if (status == GRUB_ERR_NONE && part_uuid)
status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid, status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
GRUB_EFI_VARIABLE_RUNTIME_ACCESS); GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
@ -117,4 +133,6 @@ GRUB_MOD_INIT (bli)
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
GRUB_EFI_VARIABLE_RUNTIME_ACCESS); GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
set_loader_device_part_uuid (); set_loader_device_part_uuid ();
/* No error here is critical, other than being logged */
grub_print_error ();
} }

View file

@ -92,7 +92,6 @@ lock_rom_area (void)
static void static void
fake_bios_data (int use_rom) fake_bios_data (int use_rom)
{ {
unsigned i;
void *acpi, *smbios; void *acpi, *smbios;
grub_uint16_t *ebda_seg_ptr, *low_mem_ptr; grub_uint16_t *ebda_seg_ptr, *low_mem_ptr;
@ -101,33 +100,15 @@ fake_bios_data (int use_rom)
if ((*ebda_seg_ptr) || (*low_mem_ptr)) if ((*ebda_seg_ptr) || (*low_mem_ptr))
return; return;
acpi = 0; acpi = grub_efi_find_configuration_table (&acpi2_guid);
smbios = 0; grub_dprintf ("efi", "ACPI2: %p\n", acpi);
for (i = 0; i < grub_efi_system_table->num_table_entries; i++) if (!acpi) {
{ acpi = grub_efi_find_configuration_table (&acpi_guid);
grub_guid_t *guid = grub_dprintf ("efi", "ACPI: %p\n", acpi);
&grub_efi_system_table->configuration_table[i].vendor_guid; }
if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_guid_t))) smbios = grub_efi_find_configuration_table (&smbios_guid);
{ grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
acpi = grub_efi_system_table->configuration_table[i].vendor_table;
grub_dprintf ("efi", "ACPI2: %p\n", acpi);
}
else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_guid_t)))
{
void *t;
t = grub_efi_system_table->configuration_table[i].vendor_table;
if (! acpi)
acpi = t;
grub_dprintf ("efi", "ACPI: %p\n", t);
}
else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
{
smbios = grub_efi_system_table->configuration_table[i].vendor_table;
grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
}
}
*ebda_seg_ptr = FAKE_EBDA_SEG; *ebda_seg_ptr = FAKE_EBDA_SEG;
*low_mem_ptr = (FAKE_EBDA_SEG >> 6); *low_mem_ptr = (FAKE_EBDA_SEG >> 6);

View file

@ -29,7 +29,7 @@
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
struct known_protocol static struct known_protocol
{ {
grub_guid_t guid; grub_guid_t guid;
const char *name; const char *name;
@ -96,7 +96,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
grub_efi_handle_t handle = handles[i]; grub_efi_handle_t handle = handles[i];
grub_efi_status_t status; grub_efi_status_t status;
grub_efi_uintn_t num_protocols; grub_efi_uintn_t num_protocols;
grub_guid_t **protocols; grub_packed_guid_t **protocols;
grub_efi_device_path_t *dp; grub_efi_device_path_t *dp;
grub_printf ("Handle %p\n", handle); grub_printf ("Handle %p\n", handle);

View file

@ -64,12 +64,18 @@ grub_cmd_lsefisystab (struct grub_command *cmd __attribute__ ((unused)),
char **args __attribute__ ((unused))) char **args __attribute__ ((unused)))
{ {
const grub_efi_system_table_t *st = grub_efi_system_table; const grub_efi_system_table_t *st = grub_efi_system_table;
const grub_efi_uint32_t major_rev = st->hdr.revision >> 16;
const grub_efi_uint32_t minor_rev_upper = (st->hdr.revision & 0xffff) / 10;
const grub_efi_uint32_t minor_rev_lower = (st->hdr.revision & 0xffff) % 10;
grub_efi_configuration_table_t *t; grub_efi_configuration_table_t *t;
unsigned int i; unsigned int i;
grub_printf ("Address: %p\n", st); grub_printf ("Address: %p\n", st);
grub_printf ("Signature: %016" PRIxGRUB_UINT64_T " revision: %08x\n", grub_printf ("Signature: %016" PRIxGRUB_UINT64_T " revision: %u.%u",
st->hdr.signature, st->hdr.revision); st->hdr.signature, major_rev, minor_rev_upper);
if (minor_rev_lower)
grub_printf (".%u", minor_rev_lower);
grub_printf ("\n");
{ {
char *vendor; char *vendor;
grub_uint16_t *vendor_utf16; grub_uint16_t *vendor_utf16;

View file

@ -136,22 +136,16 @@ grub_cmd_lssal (struct grub_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)), int argc __attribute__ ((unused)),
char **args __attribute__ ((unused))) char **args __attribute__ ((unused)))
{ {
const grub_efi_system_table_t *st = grub_efi_system_table;
grub_efi_configuration_table_t *t = st->configuration_table;
unsigned int i;
static grub_guid_t guid = GRUB_EFI_SAL_TABLE_GUID; static grub_guid_t guid = GRUB_EFI_SAL_TABLE_GUID;
void *table = grub_efi_find_configuration_table (&guid);
for (i = 0; i < st->num_table_entries; i++) if (table == NULL)
{ {
if (grub_memcmp (&guid, &t->vendor_guid, grub_printf ("SAL not found\n");
sizeof (grub_guid_t)) == 0) return GRUB_ERR_NONE;
{
disp_sal (t->vendor_table);
return GRUB_ERR_NONE;
}
t++;
} }
grub_printf ("SAL not found\n");
disp_sal (table);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }

View file

@ -18,44 +18,20 @@
*/ */
#include <grub/smbios.h> #include <grub/smbios.h>
#include <grub/misc.h>
#include <grub/efi/efi.h> #include <grub/efi/efi.h>
#include <grub/efi/api.h>
struct grub_smbios_eps * struct grub_smbios_eps *
grub_machine_smbios_get_eps (void) grub_machine_smbios_get_eps (void)
{ {
unsigned i;
static grub_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID; static grub_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
for (i = 0; i < grub_efi_system_table->num_table_entries; i++) return (struct grub_smbios_eps *) grub_efi_find_configuration_table (&smbios_guid);
{
grub_guid_t *guid =
&grub_efi_system_table->configuration_table[i].vendor_guid;
if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
return (struct grub_smbios_eps *)
grub_efi_system_table->configuration_table[i].vendor_table;
}
return 0;
} }
struct grub_smbios_eps3 * struct grub_smbios_eps3 *
grub_machine_smbios_get_eps3 (void) grub_machine_smbios_get_eps3 (void)
{ {
unsigned i;
static grub_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID; static grub_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID;
for (i = 0; i < grub_efi_system_table->num_table_entries; i++) return (struct grub_smbios_eps3 *) grub_efi_find_configuration_table (&smbios3_guid);
{
grub_guid_t *guid =
&grub_efi_system_table->configuration_table[i].vendor_guid;
if (! grub_memcmp (guid, &smbios3_guid, sizeof (grub_guid_t)))
return (struct grub_smbios_eps3 *)
grub_efi_system_table->configuration_table[i].vendor_table;
}
return 0;
} }

View file

@ -292,6 +292,15 @@ grub_tpm_present (void)
{ {
grub_efi_handle_t tpm_handle; grub_efi_handle_t tpm_handle;
grub_efi_uint8_t protocol_version; grub_efi_uint8_t protocol_version;
grub_efi_cc_protocol_t *cc;
/*
* When confidential computing measurement protocol is enabled
* we assume the TPM is present.
*/
cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
if (cc != NULL)
return 1;
if (!grub_tpm_handle_find (&tpm_handle, &protocol_version)) if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
return 0; return 0;

View file

@ -49,6 +49,9 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
} }
state = grub_arg_list_alloc (ext, argc, args); state = grub_arg_list_alloc (ext, argc, args);
if (state == NULL)
return grub_errno;
if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc)) if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
{ {
context.state = state; context.state = state;

View file

@ -306,6 +306,8 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
elf = grub_elf_file (file, file->name); elf = grub_elf_file (file, file->name);
if (elf == NULL)
break;
if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC) if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
|| elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB) || elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
break; break;

View file

@ -24,6 +24,7 @@
#include <grub/lib/hexdump.h> #include <grub/lib/hexdump.h>
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -51,7 +52,11 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args)
length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256; length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256;
if (!grub_strcmp (args[0], "(mem)")) if (!grub_strcmp (args[0], "(mem)"))
hexdump (skip, (char *) (grub_addr_t) skip, length); {
if (grub_is_lockdown() == GRUB_LOCKDOWN_ENABLED)
return grub_error (GRUB_ERR_ACCESS_DENIED, N_("memory reading is disabled in lockdown mode"));
hexdump (skip, (char *) (grub_addr_t) skip, length);
}
else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')')) else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')'))
{ {
grub_disk_t disk; grub_disk_t disk;

View file

@ -104,13 +104,13 @@ static grub_command_t cmd, cmd_clean, cmd_set;
GRUB_MOD_INIT(cmostest) GRUB_MOD_INIT(cmostest)
{ {
cmd = grub_register_command ("cmostest", grub_cmd_cmostest, cmd = grub_register_command_lockdown ("cmostest", grub_cmd_cmostest,
N_("BYTE:BIT"), N_("BYTE:BIT"),
N_("Test bit at BYTE:BIT in CMOS.")); N_("Test bit at BYTE:BIT in CMOS."));
cmd_clean = grub_register_command ("cmosclean", grub_cmd_cmosclean, cmd_clean = grub_register_command_lockdown ("cmosclean", grub_cmd_cmosclean,
N_("BYTE:BIT"), N_("BYTE:BIT"),
N_("Clear bit at BYTE:BIT in CMOS.")); N_("Clear bit at BYTE:BIT in CMOS."));
cmd_set = grub_register_command ("cmosset", grub_cmd_cmosset, cmd_set = grub_register_command_lockdown ("cmosset", grub_cmd_cmosset,
N_("BYTE:BIT"), N_("BYTE:BIT"),
/* TRANSLATORS: A bit may be either set (1) or clear (0). */ /* TRANSLATORS: A bit may be either set (1) or clear (0). */
N_("Set bit at BYTE:BIT in CMOS.")); N_("Set bit at BYTE:BIT in CMOS."));

View file

@ -26,7 +26,7 @@
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/i386/cpuid.h> #include <grub/i386/cpuid.h>
#include <grub/i386/rdmsr.h> #include <grub/i386/msr.h>
GRUB_MOD_LICENSE("GPLv3+"); GRUB_MOD_LICENSE("GPLv3+");
@ -42,27 +42,16 @@ static const struct grub_arg_option options[] =
static grub_err_t static grub_err_t
grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv) grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
{ {
grub_uint32_t manufacturer[3], max_cpuid, a, b, c, features, addr; grub_err_t err;
grub_uint32_t addr;
grub_uint64_t value; grub_uint64_t value;
const char *ptr; const char *ptr;
char buf[sizeof("1122334455667788")]; char buf[sizeof("1122334455667788")];
/* err = grub_cpu_is_msr_supported ();
* The CPUID instruction should be used to determine whether MSRs
* are supported. (CPUID.01H:EDX[5] = 1)
*/
if (! grub_cpu_is_cpuid_supported ())
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
grub_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]); if (err != GRUB_ERR_NONE)
return grub_error (err, N_("RDMSR is unsupported"));
if (max_cpuid < 1)
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
grub_cpuid (1, a, b, c, features);
if (!(features & (1 << 5)))
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
if (argc != 1) if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected")); return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
@ -76,7 +65,7 @@ grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
if (*ptr != '\0') if (*ptr != '\0')
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument")); return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
value = grub_msr_read (addr); value = grub_rdmsr (addr);
if (ctxt->state[0].set) if (ctxt->state[0].set)
{ {

View file

@ -27,7 +27,7 @@
#include <grub/lockdown.h> #include <grub/lockdown.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/i386/cpuid.h> #include <grub/i386/cpuid.h>
#include <grub/i386/wrmsr.h> #include <grub/i386/msr.h>
GRUB_MOD_LICENSE("GPLv3+"); GRUB_MOD_LICENSE("GPLv3+");
@ -36,26 +36,15 @@ static grub_command_t cmd_write;
static grub_err_t static grub_err_t
grub_cmd_msr_write (grub_command_t cmd __attribute__ ((unused)), int argc, char **argv) grub_cmd_msr_write (grub_command_t cmd __attribute__ ((unused)), int argc, char **argv)
{ {
grub_uint32_t manufacturer[3], max_cpuid, a, b, c, features, addr; grub_err_t err;
grub_uint32_t addr;
grub_uint64_t value; grub_uint64_t value;
const char *ptr; const char *ptr;
/* err = grub_cpu_is_msr_supported ();
* The CPUID instruction should be used to determine whether MSRs
* are supported. (CPUID.01H:EDX[5] = 1)
*/
if (!grub_cpu_is_cpuid_supported ())
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
grub_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]); if (err != GRUB_ERR_NONE)
return grub_error (err, N_("WRMSR is unsupported"));
if (max_cpuid < 1)
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
grub_cpuid (1, a, b, c, features);
if (!(features & (1 << 5)))
return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
if (argc != 2) if (argc != 2)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected")); return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
@ -77,7 +66,7 @@ grub_cmd_msr_write (grub_command_t cmd __attribute__ ((unused)), int argc, char
if (*ptr != '\0') if (*ptr != '\0')
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument")); return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
grub_msr_write (addr, value); grub_wrmsr (addr, value);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }

View file

@ -23,48 +23,10 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/tpm.h> #include <grub/tpm.h>
#include <grub/ieee1275/ieee1275.h> #include <grub/ieee1275/ieee1275.h>
#include <grub/ieee1275/tpm.h>
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/misc.h> #include <grub/misc.h>
static grub_ieee1275_ihandle_t tpm_ihandle;
static grub_uint8_t tpm_version;
#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t) 0)
static void
tpm_get_tpm_version (void)
{
grub_ieee1275_phandle_t vtpm;
char buffer[20];
if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) &&
!grub_ieee1275_get_property (vtpm, "compatible", buffer,
sizeof (buffer), NULL) &&
!grub_strcmp (buffer, "IBM,vtpm20"))
tpm_version = 2;
}
static grub_err_t
tpm_init (void)
{
static int init_success = 0;
if (!init_success)
{
if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0)
{
tpm_ihandle = IEEE1275_IHANDLE_INVALID;
return GRUB_ERR_UNKNOWN_DEVICE;
}
init_success = 1;
tpm_get_tpm_version ();
}
return GRUB_ERR_NONE;
}
static int static int
ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex, ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
grub_uint32_t eventtype, grub_uint32_t eventtype,
@ -90,7 +52,7 @@ ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2); INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
args.method = (grub_ieee1275_cell_t) "2hash-ext-log"; args.method = (grub_ieee1275_cell_t) "2hash-ext-log";
args.ihandle = tpm_ihandle; args.ihandle = grub_ieee1275_tpm_ihandle;
args.pcrindex = pcrindex; args.pcrindex = pcrindex;
args.eventtype = eventtype; args.eventtype = eventtype;
args.description = (grub_ieee1275_cell_t) description; args.description = (grub_ieee1275_cell_t) description;
@ -138,7 +100,7 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n", grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
pcr, size, description); pcr, size, description);
if (tpm_version == 2) if (grub_ieee1275_tpm_ihandle != GRUB_IEEE1275_IHANDLE_INVALID)
return tpm2_log_event (buf, size, pcr, description); return tpm2_log_event (buf, size, pcr, description);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
@ -151,5 +113,5 @@ grub_tpm_present (void)
* Call tpm_init() "late" rather than from GRUB_MOD_INIT() so that device nodes * Call tpm_init() "late" rather than from GRUB_MOD_INIT() so that device nodes
* can be found. * can be found.
*/ */
return tpm_init() == GRUB_ERR_NONE; return grub_ieee1275_tpm_init() == GRUB_ERR_NONE;
} }

View file

@ -198,7 +198,6 @@ legacy_file (const char *filename)
const char **args = grub_malloc (sizeof (args[0])); const char **args = grub_malloc (sizeof (args[0]));
if (!args) if (!args)
{ {
grub_file_close (file);
grub_free (suffix); grub_free (suffix);
grub_free (entrysrc); grub_free (entrysrc);
return grub_errno; return grub_errno;

View file

@ -87,37 +87,44 @@ grub_ls_list_devices (int longlist)
struct grub_ls_list_files_ctx struct grub_ls_list_files_ctx
{ {
char *dirname; char *dirname;
char *filename;
int all; int all;
int human; int human;
int longlist;
int print_dirhdr;
}; };
/* Helper for grub_ls_list_files. */ /* Helper for grub_ls_list_files. */
static int static int
print_files (const char *filename, const struct grub_dirhook_info *info, print_file (const char *filename, const struct grub_dirhook_info *info,
void *data)
{
struct grub_ls_list_files_ctx *ctx = data;
if (ctx->all || filename[0] != '.')
grub_printf ("%s%s ", filename, info->dir ? "/" : "");
return 0;
}
/* Helper for grub_ls_list_files. */
static int
print_files_long (const char *filename, const struct grub_dirhook_info *info,
void *data) void *data)
{ {
char *pathname = NULL;
struct grub_ls_list_files_ctx *ctx = data; struct grub_ls_list_files_ctx *ctx = data;
if ((! ctx->all) && (filename[0] == '.')) if ((! ctx->all) && (filename[0] == '.'))
return 0; return 0;
if ((ctx->filename != NULL) && (grub_strcmp (filename, ctx->filename) != 0))
return 0;
if (ctx->print_dirhdr)
{
grub_printf ("%s:\n", ctx->dirname);
ctx->print_dirhdr = 0;
}
if (! ctx->longlist)
{
if (ctx->filename != NULL)
grub_xputs (ctx->dirname);
grub_printf ("%s%s ", filename, info->dir ? "/" : "");
return 0;
}
if (! info->dir) if (! info->dir)
{ {
grub_file_t file; grub_file_t file;
char *pathname;
if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/') if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
pathname = grub_xasprintf ("%s%s", ctx->dirname, filename); pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
@ -131,20 +138,19 @@ print_files_long (const char *filename, const struct grub_dirhook_info *info,
should be reported as directories. */ should be reported as directories. */
file = grub_file_open (pathname, GRUB_FILE_TYPE_GET_SIZE file = grub_file_open (pathname, GRUB_FILE_TYPE_GET_SIZE
| GRUB_FILE_TYPE_NO_DECOMPRESS); | GRUB_FILE_TYPE_NO_DECOMPRESS);
if (! file) if (file)
{ {
grub_errno = 0; if (! ctx->human)
grub_free (pathname); grub_printf ("%-12llu", (unsigned long long) file->size);
return 0; else
} grub_printf ("%-12s", grub_get_human_size (file->size,
if (! ctx->human)
grub_printf ("%-12llu", (unsigned long long) file->size);
else
grub_printf ("%-12s", grub_get_human_size (file->size,
GRUB_HUMAN_SIZE_SHORT)); GRUB_HUMAN_SIZE_SHORT));
grub_file_close (file); grub_file_close (file);
grub_free (pathname); }
else
grub_xputs ("????????????");
grub_errno = GRUB_ERR_NONE;
} }
else else
grub_printf ("%-12s", _("DIR")); grub_printf ("%-12s", _("DIR"));
@ -165,13 +171,22 @@ print_files_long (const char *filename, const struct grub_dirhook_info *info,
datetime.day, datetime.hour, datetime.day, datetime.hour,
datetime.minute, datetime.second); datetime.minute, datetime.second);
} }
grub_printf ("%s%s\n", filename, info->dir ? "/" : ""); /*
* Only print the full path when listing a file path given as an argument
* to ls, i.e. when ctx->filename != NULL. File listings that are printed
* due to showing the contents of a directory do not need a full path because
* the full path to the directory will have already been printed.
*/
grub_printf ("%s%s\n", (ctx->filename != NULL) ? pathname : filename,
info->dir ? "/" : "");
grub_free (pathname);
return 0; return 0;
} }
static grub_err_t static grub_err_t
grub_ls_list_files (char *dirname, int longlist, int all, int human) grub_ls_list_files (char *dirname, int longlist, int all, int human, int dirhdr)
{ {
char *device_name; char *device_name;
grub_fs_t fs; grub_fs_t fs;
@ -216,44 +231,38 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
{ {
struct grub_ls_list_files_ctx ctx = { struct grub_ls_list_files_ctx ctx = {
.dirname = dirname, .dirname = dirname,
.filename = NULL,
.all = all, .all = all,
.human = human .human = human,
.longlist = longlist,
.print_dirhdr = dirhdr
}; };
if (longlist) (fs->fs_dir) (dev, path, print_file, &ctx);
(fs->fs_dir) (dev, path, print_files_long, &ctx);
else
(fs->fs_dir) (dev, path, print_files, &ctx);
if (grub_errno == GRUB_ERR_BAD_FILE_TYPE if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
&& path[grub_strlen (path) - 1] != '/') && path[grub_strlen (path) - 1] != '/')
{ {
/*
* Reset errno as it is currently set, but will cause subsequent code
* to think there is an error.
*/
grub_errno = GRUB_ERR_NONE;
/* PATH might be a regular file. */ /* PATH might be a regular file. */
char *p; ctx.print_dirhdr = 0;
grub_file_t file; ctx.filename = grub_strrchr (dirname, '/');
struct grub_dirhook_info info; if (ctx.filename == NULL)
grub_errno = 0; goto fail;
++(ctx.filename);
file = grub_file_open (dirname, GRUB_FILE_TYPE_GET_SIZE ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
| GRUB_FILE_TYPE_NO_DECOMPRESS); if (ctx.dirname == NULL)
if (! file)
goto fail; goto fail;
grub_file_close (file); (fs->fs_dir) (dev, ctx.dirname + (path - dirname), print_file, &ctx);
p = grub_strrchr (dirname, '/') + 1; grub_free (ctx.dirname);
dirname = grub_strndup (dirname, p - dirname);
if (! dirname)
goto fail;
all = 1;
grub_memset (&info, 0, sizeof (info));
if (longlist)
print_files_long (p, &info, &ctx);
else
print_files (p, &info, &ctx);
grub_free (dirname);
} }
if (grub_errno == GRUB_ERR_NONE) if (grub_errno == GRUB_ERR_NONE)
@ -268,7 +277,7 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
grub_free (device_name); grub_free (device_name);
return 0; return GRUB_ERR_NONE;
} }
static grub_err_t static grub_err_t
@ -281,10 +290,10 @@ grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
grub_ls_list_devices (state[0].set); grub_ls_list_devices (state[0].set);
else else
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
grub_ls_list_files (args[i], state[0].set, state[2].set, grub_ls_list_files (args[i], state[0].set, state[2].set, state[1].set,
state[1].set); argc > 1);
return 0; return GRUB_ERR_NONE;
} }
static grub_extcmd_t cmd; static grub_extcmd_t cmd;

View file

@ -122,17 +122,20 @@ grub_cmd_write (grub_command_t cmd, int argc, char **argv)
GRUB_MOD_INIT(memrw) GRUB_MOD_INIT(memrw)
{ {
cmd_read_byte = cmd_read_byte =
grub_register_extcmd ("read_byte", grub_cmd_read, 0, grub_register_extcmd_lockdown ("read_byte", grub_cmd_read, 0,
N_("ADDR"), N_("Read 8-bit value from ADDR."), N_("ADDR"),
options); N_("Read 8-bit value from ADDR."),
options);
cmd_read_word = cmd_read_word =
grub_register_extcmd ("read_word", grub_cmd_read, 0, grub_register_extcmd_lockdown ("read_word", grub_cmd_read, 0,
N_("ADDR"), N_("Read 16-bit value from ADDR."), N_("ADDR"),
options); N_("Read 16-bit value from ADDR."),
options);
cmd_read_dword = cmd_read_dword =
grub_register_extcmd ("read_dword", grub_cmd_read, 0, grub_register_extcmd_lockdown ("read_dword", grub_cmd_read, 0,
N_("ADDR"), N_("Read 32-bit value from ADDR."), N_("ADDR"),
options); N_("Read 32-bit value from ADDR."),
options);
cmd_write_byte = cmd_write_byte =
grub_register_command_lockdown ("write_byte", grub_cmd_write, grub_register_command_lockdown ("write_byte", grub_cmd_write,
N_("ADDR VALUE [MASK]"), N_("ADDR VALUE [MASK]"),

View file

@ -29,6 +29,10 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#ifdef GRUB_MACHINE_EFI
#include <grub/cryptodisk.h>
#endif
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
/* cat FILE */ /* cat FILE */
@ -167,7 +171,7 @@ grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
{ {
grub_dl_dep_t dep; grub_dl_dep_t dep;
grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count); grub_printf ("%s\t%" PRIuGRUB_UINT64_T "\t\t", mod->name, mod->ref_count);
for (dep = mod->dep; dep; dep = dep->next) for (dep = mod->dep; dep; dep = dep->next)
{ {
if (dep != mod->dep) if (dep != mod->dep)
@ -187,6 +191,13 @@ grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)), int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused))) char *argv[] __attribute__ ((unused)))
{ {
#ifdef GRUB_MACHINE_EFI
/*
* The "exit" command is often used to launch the next boot application.
* So, erase the secrets.
*/
grub_cryptodisk_erasesecrets ();
#endif
grub_exit (); grub_exit ();
/* Not reached. */ /* Not reached. */
} }
@ -203,8 +214,8 @@ GRUB_MOD_INIT(minicmd)
grub_register_command ("help", grub_mini_cmd_help, grub_register_command ("help", grub_mini_cmd_help,
0, N_("Show this message.")); 0, N_("Show this message."));
cmd_dump = cmd_dump =
grub_register_command ("dump", grub_mini_cmd_dump, grub_register_command_lockdown ("dump", grub_mini_cmd_dump,
N_("ADDR [SIZE]"), N_("Show memory contents.")); N_("ADDR [SIZE]"), N_("Show memory contents."));
cmd_rmmod = cmd_rmmod =
grub_register_command ("rmmod", grub_mini_cmd_rmmod, grub_register_command ("rmmod", grub_mini_cmd_rmmod,
N_("MODULE"), N_("Remove a module.")); N_("MODULE"), N_("Remove a module."));

View file

@ -1010,6 +1010,8 @@ GRUB_MOD_INIT(pgp)
GRUB_MOD_FINI(pgp) GRUB_MOD_FINI(pgp)
{ {
grub_register_variable_hook ("check_signatures", NULL, NULL);
grub_env_unset ("check_signatures");
grub_verifier_unregister (&grub_pubkey_verifier); grub_verifier_unregister (&grub_pubkey_verifier);
grub_unregister_extcmd (cmd); grub_unregister_extcmd (cmd);
grub_unregister_extcmd (cmd_trust); grub_unregister_extcmd (cmd_trust);

View file

@ -25,6 +25,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -37,13 +38,14 @@ static const struct grub_arg_option options[] =
static char * static char *
grub_getline (int silent) grub_getline (int silent)
{ {
int i; grub_size_t i;
char *line; char *line;
char *tmp; char *tmp;
int c; int c;
grub_size_t alloc_size;
i = 0; i = 0;
line = grub_malloc (1 + i + sizeof('\0')); line = grub_malloc (1 + sizeof('\0'));
if (! line) if (! line)
return NULL; return NULL;
@ -59,8 +61,17 @@ grub_getline (int silent)
line[i] = (char) c; line[i] = (char) c;
if (!silent) if (!silent)
grub_printf ("%c", c); grub_printf ("%c", c);
i++; if (grub_add (i, 1, &i))
tmp = grub_realloc (line, 1 + i + sizeof('\0')); {
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
return NULL;
}
if (grub_add (i, 1 + sizeof('\0'), &alloc_size))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
return NULL;
}
tmp = grub_realloc (line, alloc_size);
if (! tmp) if (! tmp)
{ {
grub_free (line); grub_free (line);

View file

@ -54,6 +54,41 @@ struct search_ctx
int is_cache; int is_cache;
}; };
static bool
is_unencrypted_disk (grub_disk_t disk)
{
grub_command_t cmd;
char *disk_str;
int disk_str_len;
int res;
if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
return false; /* This is (crypto) disk. */
if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID)
{
char opt[] = "--quiet";
char *args[2];
cmd = grub_command_find ("cryptocheck");
if (cmd == NULL) /* No diskfilter module loaded for some reason. */
return true;
disk_str_len = grub_strlen (disk->name) + 2 + 1;
disk_str = grub_malloc (disk_str_len);
if (disk_str == NULL) /* Something is wrong, better report as unencrypted. */
return true;
grub_snprintf (disk_str, disk_str_len, "(%s)", disk->name);
args[0] = opt;
args[1] = disk_str;
res = cmd->func (cmd, 2, args);
grub_free (disk_str);
return (res != GRUB_ERR_NONE) ? true : false; /* GRUB_ERR_NONE for encrypted. */
}
return true;
}
/* Helper for FUNC_NAME. */ /* Helper for FUNC_NAME. */
static int static int
iterate_device (const char *name, void *data) iterate_device (const char *name, void *data)
@ -86,6 +121,26 @@ iterate_device (const char *name, void *data)
grub_device_close (dev); grub_device_close (dev);
} }
/* Limit to encrypted disks when requested. */
if (ctx->flags & SEARCH_FLAGS_CRYPTODISK_ONLY)
{
grub_device_t dev;
dev = grub_device_open (name);
if (dev == NULL)
{
grub_errno = GRUB_ERR_NONE;
return 0;
}
if (dev->disk == NULL || is_unencrypted_disk (dev->disk) == true)
{
grub_device_close (dev);
grub_errno = GRUB_ERR_NONE;
return 0;
}
grub_device_close (dev);
}
#ifdef DO_SEARCH_FS_UUID #ifdef DO_SEARCH_FS_UUID
#define compare_fn grub_strcasecmp #define compare_fn grub_strcasecmp
#else #else

View file

@ -41,6 +41,7 @@ static const struct grub_arg_option options[] =
ARG_TYPE_STRING}, ARG_TYPE_STRING},
{"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0}, {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0},
{"efidisk-only", 0, 0, N_("Only probe EFI disks."), 0, 0}, {"efidisk-only", 0, 0, N_("Only probe EFI disks."), 0, 0},
{"cryptodisk-only", 0, 0, N_("Only probe encrypted disks."), 0, 0},
{"hint", 'h', GRUB_ARG_OPTION_REPEATABLE, {"hint", 'h', GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT. If HINT ends in comma, " N_("First try the device HINT. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING}, "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
@ -75,6 +76,7 @@ enum options
SEARCH_SET, SEARCH_SET,
SEARCH_NO_FLOPPY, SEARCH_NO_FLOPPY,
SEARCH_EFIDISK_ONLY, SEARCH_EFIDISK_ONLY,
SEARCH_CRYPTODISK_ONLY,
SEARCH_HINT, SEARCH_HINT,
SEARCH_HINT_IEEE1275, SEARCH_HINT_IEEE1275,
SEARCH_HINT_BIOS, SEARCH_HINT_BIOS,
@ -189,6 +191,9 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
if (state[SEARCH_EFIDISK_ONLY].set) if (state[SEARCH_EFIDISK_ONLY].set)
flags |= SEARCH_FLAGS_EFIDISK_ONLY; flags |= SEARCH_FLAGS_EFIDISK_ONLY;
if (state[SEARCH_CRYPTODISK_ONLY].set)
flags |= SEARCH_FLAGS_CRYPTODISK_ONLY;
if (state[SEARCH_LABEL].set) if (state[SEARCH_LABEL].set)
grub_search_label (id, var, flags, hints, nhints); grub_search_label (id, var, flags, hints, nhints);
else if (state[SEARCH_FS_UUID].set) else if (state[SEARCH_FS_UUID].set)
@ -210,7 +215,7 @@ GRUB_MOD_INIT(search)
cmd = cmd =
grub_register_extcmd ("search", grub_cmd_search, grub_register_extcmd ("search", grub_cmd_search,
GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH, GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]" N_("[-f|-l|-u|-s|-n] [--cryptodisk-only] [--hint HINT [--hint HINT] ...]"
" NAME"), " NAME"),
N_("Search devices by file, filesystem label" N_("Search devices by file, filesystem label"
" or filesystem UUID." " or filesystem UUID."

View file

@ -29,6 +29,9 @@
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
/* Set a limit on recursion to avoid stack overflow. */
#define MAX_TEST_RECURSION_DEPTH 100
/* A simple implementation for signed numbers. */ /* A simple implementation for signed numbers. */
static int static int
grub_strtosl (char *arg, const char ** const end, int base) grub_strtosl (char *arg, const char ** const end, int base)
@ -150,7 +153,7 @@ get_fileinfo (char *path, struct test_parse_ctx *ctx)
/* Parse a test expression starting from *argn. */ /* Parse a test expression starting from *argn. */
static int static int
test_parse (char **args, int *argn, int argc) test_parse (char **args, int *argn, int argc, int *depth)
{ {
struct test_parse_ctx ctx = { struct test_parse_ctx ctx = {
.and = 1, .and = 1,
@ -387,13 +390,24 @@ test_parse (char **args, int *argn, int argc)
if (grub_strcmp (args[*argn], ")") == 0) if (grub_strcmp (args[*argn], ")") == 0)
{ {
(*argn)++; (*argn)++;
if (*depth > 0)
(*depth)--;
return ctx.or || ctx.and; return ctx.or || ctx.and;
} }
/* Recursively invoke if parenthesis. */ /* Recursively invoke if parenthesis. */
if (grub_strcmp (args[*argn], "(") == 0) if (grub_strcmp (args[*argn], "(") == 0)
{ {
(*argn)++; (*argn)++;
update_val (test_parse (args, argn, argc), &ctx);
if (++(*depth) > MAX_TEST_RECURSION_DEPTH)
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("max recursion depth exceeded"));
depth--;
return ctx.or || ctx.and;
}
update_val (test_parse (args, argn, argc, depth), &ctx);
continue; continue;
} }
@ -428,11 +442,12 @@ grub_cmd_test (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args) int argc, char **args)
{ {
int argn = 0; int argn = 0;
int depth = 0;
if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0) if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0)
argc--; argc--;
return test_parse (args, &argn, argc) ? GRUB_ERR_NONE return test_parse (args, &argn, argc, &depth) ? GRUB_ERR_NONE
: grub_error (GRUB_ERR_TEST_FAILURE, N_("false")); : grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
} }

View file

@ -36,6 +36,16 @@ grub_tpm_verify_init (grub_file_t io,
{ {
*context = io->name; *context = io->name;
*flags |= GRUB_VERIFY_FLAGS_SINGLE_CHUNK; *flags |= GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
/*
* The loopback image is mapped as a disk allowing it to function like
* a block device. However, we measure files read from the block device
* not the device itself. For example, we don't measure block devices like
* hd0 disk directly. This process is crucial to prevent out-of-memory
* errors as loopback images are inherently large.
*/
if ((type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_LOOPBACK)
*flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }

View file

@ -0,0 +1,127 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2022 Microsoft Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/err.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include "tpm2_args.h"
grub_err_t
grub_tpm2_protector_parse_pcrs (char *value, grub_uint8_t *pcrs,
grub_uint8_t *pcr_count)
{
char *current_pcr = value;
char *next_pcr;
const char *pcr_end;
grub_uint64_t pcr;
grub_uint8_t i;
if (grub_strlen (value) == 0)
return GRUB_ERR_BAD_ARGUMENT;
*pcr_count = 0;
for (i = 0; i < TPM_MAX_PCRS; i++)
{
next_pcr = grub_strchr (current_pcr, ',');
if (next_pcr == current_pcr)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("empty entry in PCR list"));
if (next_pcr != NULL)
*next_pcr = '\0';
pcr = grub_strtoul (current_pcr, &pcr_end, 10);
if (*current_pcr == '\0' || *pcr_end != '\0')
return grub_error (GRUB_ERR_BAD_NUMBER, N_("entry '%s' in PCR list is not a number"), current_pcr);
if (pcr > TPM_MAX_PCRS - 1)
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("entry %llu in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u"), (unsigned long long)pcr, TPM_MAX_PCRS - 1);
pcrs[i] = (grub_uint8_t) pcr;
++(*pcr_count);
if (next_pcr == NULL)
break;
current_pcr = next_pcr + 1;
if (*current_pcr == '\0')
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("trailing comma at the end of PCR list"));
}
if (i == TPM_MAX_PCRS)
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("too many PCRs in PCR list, the maximum number of PCRs is %u"), TPM_MAX_PCRS);
return GRUB_ERR_NONE;
}
grub_err_t
grub_tpm2_protector_parse_asymmetric (const char *value,
grub_srk_type_t *srk_type)
{
if (grub_strcasecmp (value, "ECC") == 0 ||
grub_strcasecmp (value, "ECC_NIST_P256") == 0)
{
srk_type->type = TPM_ALG_ECC;
srk_type->detail.ecc_curve = TPM_ECC_NIST_P256;
}
else if (grub_strcasecmp (value, "RSA") == 0 ||
grub_strcasecmp (value, "RSA2048") == 0)
{
srk_type->type = TPM_ALG_RSA;
srk_type->detail.rsa_bits = 2048;
}
else
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("value '%s' is not a valid asymmetric key type"), value);
return GRUB_ERR_NONE;
}
grub_err_t
grub_tpm2_protector_parse_bank (const char *value, TPM_ALG_ID_t *bank)
{
if (grub_strcasecmp (value, "SHA1") == 0)
*bank = TPM_ALG_SHA1;
else if (grub_strcasecmp (value, "SHA256") == 0)
*bank = TPM_ALG_SHA256;
else if (grub_strcasecmp (value, "SHA384") == 0)
*bank = TPM_ALG_SHA384;
else if (grub_strcasecmp (value, "SHA512") == 0)
*bank = TPM_ALG_SHA512;
else
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("value '%s' is not a valid PCR bank"), value);
return GRUB_ERR_NONE;
}
grub_err_t
grub_tpm2_protector_parse_tpm_handle (const char *value, TPM_HANDLE_t *handle)
{
grub_uint64_t num;
const char *str_end;
num = grub_strtoul (value, &str_end, 0);
if (*value == '\0' || *str_end != '\0')
return grub_error (GRUB_ERR_BAD_NUMBER, N_("TPM handle value '%s' is not a number"), value);
if (num > GRUB_UINT_MAX)
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("value %llu is too large to be a TPM handle, TPM handles are unsigned 32-bit integers"), (unsigned long long)num);
*handle = (TPM_HANDLE_t) num;
return GRUB_ERR_NONE;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2022 Microsoft Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_TPM2_TPM2_HEADER
#define GRUB_TPM2_TPM2_HEADER 1
#include <tss2_types.h>
#include <tss2_structs.h>
#include <tpm2_cmd.h>
/* Well-Known Windows SRK handle */
#define TPM2_SRK_HANDLE 0x81000001
struct tpm2_sealed_key {
TPM2B_PUBLIC_t public;
TPM2B_PRIVATE_t private;
};
typedef struct tpm2_sealed_key tpm2_sealed_key_t;
#endif /* ! GRUB_TPM2_TPM2_HEADER */

View file

@ -0,0 +1,49 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2022 Microsoft Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_TPM2_INTERNAL_ARGS_HEADER
#define GRUB_TPM2_INTERNAL_ARGS_HEADER 1
#include <grub/err.h>
#include "tpm2.h"
struct grub_srk_type
{
TPMI_ALG_PUBLIC_t type;
union {
TPM_KEY_BITS_t rsa_bits;
TPM_ECC_CURVE_t ecc_curve;
} detail;
};
typedef struct grub_srk_type grub_srk_type_t;
extern grub_err_t
grub_tpm2_protector_parse_pcrs (char *value, grub_uint8_t *pcrs, grub_uint8_t *pcr_count);
extern grub_err_t
grub_tpm2_protector_parse_asymmetric (const char *value, grub_srk_type_t *srk_type);
extern grub_err_t
grub_tpm2_protector_parse_bank (const char *value, TPM_ALG_ID_t *bank);
extern grub_err_t
grub_tpm2_protector_parse_tpm_handle (const char *value, TPM_HANDLE_t *handle);
#endif /* ! GRUB_TPM2_INTERNAL_ARGS_HEADER */

View file

@ -0,0 +1,49 @@
--
-- GRUB: GRand Unified Bootloader
-- Copyright (C) 2024 Free Software Foundation, Inc.
--
-- GRUB is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- GRUB is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with GRUB. If not, see <http://www.gnu.org/licenses/>.
--
-- This file describes TPM 2.0 Key File format for libtasn1.
-- To generate tpm2key_asn1_tab.c: asn1Parser tpm2key.asn
--
TPM2KEY {}
DEFINITIONS IMPLICIT TAGS ::=
BEGIN
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
}
END

View file

@ -0,0 +1,499 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2023 SUSE LLC
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/list.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <tss2_buffer.h>
#include "tpm2key.h"
extern asn1_static_node tpm2key_asn1_tab[];
const char *sealed_key_oid = "2.23.133.10.1.5";
static int
asn1_allocate_and_read (asn1_node node, const char *name, void **content, grub_size_t *content_size)
{
grub_uint8_t *tmpstr = NULL;
int tmpstr_size = 0;
int ret;
if (content == NULL)
return ASN1_MEM_ERROR;
ret = asn1_read_value (node, name, NULL, &tmpstr_size);
if (ret != ASN1_MEM_ERROR)
return ret;
tmpstr = grub_malloc (tmpstr_size);
if (tmpstr == NULL)
return ASN1_MEM_ERROR;
ret = asn1_read_value (node, name, tmpstr, &tmpstr_size);
if (ret != ASN1_SUCCESS)
return ret;
*content = tmpstr;
*content_size = tmpstr_size;
return ASN1_SUCCESS;
}
static int
asn1_read_uint32 (asn1_node node, const char *name, grub_uint32_t *out)
{
grub_uint32_t tmp = 0;
grub_uint8_t *ptr;
void *data = NULL;
grub_size_t data_size;
int ret;
ret = asn1_allocate_and_read (node, name, &data, &data_size);
if (ret != ASN1_SUCCESS)
return ret;
/*
* ASN.1 INTEGER is encoded in the following format:
*
* TAG LENGTH OCTECTS
*
* The integer TAG is 02 and LENGTH is the number of followed OCTECTS in
* big endian. For example:
*
* 0x1: 02 01 01
* 0xabcd: 02 02 ab cd
*
* To decribe 0x1, it only takes 1 octect, so LENGTH is 0x01 and the
* octect is 0x01. On the other hand, 0xabcd requires 2 octects: 'ab" and
* 'cd', so LENGTH is 0x02.
*
* This function only expects a uint32 integer, so it rejects any integer
* containing more than 4 octects.
*/
if (data_size > 4)
{
ret = ASN1_MEM_ERROR;
goto error;
}
/* Copy the octects into 'tmp' to make it a big-endian uint32 */
ptr = (grub_uint8_t *) &tmp + (4 - data_size);
grub_memcpy (ptr, data, data_size);
/* Convert the big-endian integer to host uint32 */
tmp = grub_be_to_cpu32 (tmp);
*out = tmp;
error:
if (data)
grub_free (data);
return ret;
}
grub_err_t
grub_tpm2key_start_parsing (asn1_node *parsed_tpm2key, void *data, grub_size_t size)
{
asn1_node tpm2key;
asn1_node tpm2key_asn1 = NULL;
void *type_oid = NULL;
grub_size_t type_oid_size = 0;
void *empty_auth = NULL;
grub_size_t empty_auth_size = 0;
int tmp_size = 0;
int ret;
grub_err_t err;
/*
* 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
* }
*/
ret = asn1_array2tree (tpm2key_asn1_tab, &tpm2key_asn1, NULL);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to parse TPM2KEY ASN.1 array");
ret = asn1_create_element (tpm2key_asn1, "TPM2KEY.TPMKey", &tpm2key);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to create TPM2KEY.TPMKey");
ret = asn1_der_decoding (&tpm2key, data, size, NULL);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to decode TPM2KEY DER");
/* Check if 'type' is Sealed Key or not */
ret = asn1_allocate_and_read (tpm2key, "type", &type_oid, &type_oid_size);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a valid TPM2KEY file");
if (grub_memcmp (sealed_key_oid, type_oid, type_oid_size) != 0)
{
err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a valid TPM2KEY file");
goto error;
}
/* 'emptyAuth' must be 'TRUE' since we don't support password authorization */
ret = asn1_allocate_and_read (tpm2key, "emptyAuth", &empty_auth, &empty_auth_size);
if (ret != ASN1_SUCCESS || grub_strncmp ("TRUE", empty_auth, empty_auth_size) != 0)
{
err = grub_error (GRUB_ERR_BAD_ARGUMENT, "emptyAuth not TRUE");
goto error;
}
/* 'secret' should not be in a sealed key */
ret = asn1_read_value (tpm2key, "secret", NULL, &tmp_size);
if (ret != ASN1_ELEMENT_NOT_FOUND)
{
err = grub_error (GRUB_ERR_BAD_ARGUMENT, "\"secret\" not allowed for Sealed Key");
goto error;
}
*parsed_tpm2key = tpm2key;
err = GRUB_ERR_NONE;
error:
grub_free (type_oid);
grub_free (empty_auth);
return err;
}
void
grub_tpm2key_end_parsing (asn1_node tpm2key)
{
asn1_delete_structure (&tpm2key);
tpm2key = NULL;
}
grub_err_t
grub_tpm2key_get_rsaparent (asn1_node tpm2key, grub_uint8_t *rsaparent)
{
void *bool_str = NULL;
grub_size_t bool_str_size = 0;
int ret;
if (rsaparent == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "NULL pointer detected");
if (tpm2key == NULL)
return grub_error (GRUB_ERR_READ_ERROR, "invalid parent node");
ret = asn1_allocate_and_read (tpm2key, "rsaParent", &bool_str, &bool_str_size);
if (ret == ASN1_SUCCESS)
{
if (grub_strncmp ("TRUE", bool_str, bool_str_size) == 0)
*rsaparent = 1;
else
*rsaparent = 0;
}
else if (ret == ASN1_ELEMENT_NOT_FOUND)
*rsaparent = 0;
else
return grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve rsaParent");
grub_free (bool_str);
return GRUB_ERR_NONE;
}
grub_err_t
grub_tpm2key_get_parent (asn1_node tpm2key, grub_uint32_t *parent)
{
int ret;
if (parent == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "NULL pointer detected");
if (tpm2key == NULL)
return grub_error (GRUB_ERR_READ_ERROR, "invalid parent node");
ret = asn1_read_uint32 (tpm2key, "parent", parent);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve parent");
return GRUB_ERR_NONE;
}
static grub_err_t
tpm2key_get_octstring (asn1_node tpm2key, const char *name, void **data, grub_size_t *size)
{
int ret;
if (name == NULL || data == NULL || size == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid parameter(s)");
if (tpm2key == NULL)
return grub_error (GRUB_ERR_READ_ERROR, "invalid %s node", name);
ret = asn1_allocate_and_read (tpm2key, name, data, size);
if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve %s", name);
return GRUB_ERR_NONE;
}
grub_err_t
grub_tpm2key_get_pubkey (asn1_node tpm2key, void **data, grub_size_t *size)
{
return tpm2key_get_octstring (tpm2key, "pubkey", data, size);
}
grub_err_t
grub_tpm2key_get_privkey (asn1_node tpm2key, void **data, grub_size_t *size)
{
return tpm2key_get_octstring (tpm2key, "privkey", data, size);
}
/*
* The maximum and minimum number of elements for 'policy' and 'authPolicy' sequences
*
* Although there is no limit for the number of sequences elements, we set the upper
* bound to 99 to make it easier to implement the code.
*
* Any 'policy' or 'authPolicy' contains more than 99 commands/policies would become
* extremely complex to manage so it is impractical to support such use case.
*/
#define TPM2KEY_ELEMENTS_MAX 99
#define TPM2KEY_ELEMENTS_MIN 1
/*
* The string to fetch 'Policy' from 'authPolicy':
* authPolicy.?XX.Policy
*/
#define AUTHPOLICY_POL_MAX_STR "authPolicy.?XX.Policy"
#define AUTHPOLICY_POL_MAX (sizeof (AUTHPOLICY_POL_MAX_STR))
/*
* Expected strings for CommandCode and CommandPolicy:
* policy.?XX.CommandCode
* policy.?XX.CommandPolicy
* authPolicy.?XX.Policy.?YY.CommandCode
* authPolicy.?XX.Policy.?YY.CommandPolicy
*/
#define CMD_CODE_MAX_STR AUTHPOLICY_POL_MAX_STR".?YY.CommandCode"
#define CMD_POL_MAX_STR AUTHPOLICY_POL_MAX_STR".?YY.CommandPolicy"
#define CMD_CODE_MAX (sizeof (CMD_CODE_MAX_STR))
#define CMD_POL_MAX (sizeof (CMD_POL_MAX_STR))
static int
tpm2key_get_policy_seq (asn1_node tpm2key, const char *prefix,
tpm2key_policy_t *policy_seq)
{
tpm2key_policy_t tmp_seq = NULL;
tpm2key_policy_t policy = NULL;
int policy_n;
char cmd_code[CMD_CODE_MAX];
char cmd_pol[CMD_POL_MAX];
grub_size_t cmd_policy_len;
int i;
int ret;
ret = asn1_number_of_elements (tpm2key, prefix, &policy_n);
if (ret != ASN1_SUCCESS)
return ret;
/*
* Limit the number of policy commands to two digits (99)
* Although there is no upper bound for the number of policy commands,
* in practice, it takes one or two policy commands to unseal the key,
* so the 99 commands limit is more than enough.
*/
if (policy_n > TPM2KEY_ELEMENTS_MAX || policy_n < TPM2KEY_ELEMENTS_MIN)
return ASN1_VALUE_NOT_VALID;
/*
* Iterate the policy commands backwards since grub_list_push() prepends
* the item into the list.
*/
for (i = policy_n; i >= 1; i--) {
policy = grub_zalloc (sizeof (struct tpm2key_policy));
if (policy == NULL)
{
ret = ASN1_MEM_ALLOC_ERROR;
goto error;
}
grub_snprintf (cmd_code, CMD_CODE_MAX, "%s.?%d.CommandCode", prefix, i);
grub_snprintf (cmd_pol, CMD_POL_MAX, "%s.?%d.CommandPolicy", prefix, i);
/* CommandCode [0] EXPLICIT INTEGER */
ret = asn1_read_uint32 (tpm2key, cmd_code, &policy->cmd_code);
if (ret != ASN1_SUCCESS)
return ret;
/* CommandPolicy [1] EXPLICIT OCTET STRING */
ret = tpm2key_get_octstring (tpm2key, cmd_pol, &policy->cmd_policy,
&cmd_policy_len);
if (ret != ASN1_SUCCESS)
{
goto error;
}
else if (cmd_policy_len > GRUB_TPM2_BUFFER_CAPACITY)
{
/*
* CommandPolicy is the marshalled parameters for the TPM command so
* it should not be larger than the maximum TPM2 buffer.
*/
ret = ASN1_VALUE_NOT_VALID;
goto error;
}
policy->cmd_policy_len = (grub_uint16_t)cmd_policy_len;
/* Prepend the policy command into the sequence */
grub_list_push (GRUB_AS_LIST_P (&tmp_seq), GRUB_AS_LIST (policy));
}
*policy_seq = tmp_seq;
return ASN1_SUCCESS;
error:
if (policy != NULL)
{
grub_free (policy->cmd_policy);
grub_free (policy);
}
grub_tpm2key_free_policy_seq (tmp_seq);
return ret;
}
grub_err_t
grub_tpm2key_get_policy_seq (asn1_node tpm2key, tpm2key_policy_t *policy_seq)
{
int ret;
ret = tpm2key_get_policy_seq (tpm2key, "policy", policy_seq);
if (ret == ASN1_ELEMENT_NOT_FOUND)
{
/* "policy" is optional, so it may not be available */
*policy_seq = NULL;
return GRUB_ERR_NONE;
}
else if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve policy");
return GRUB_ERR_NONE;
}
void
grub_tpm2key_free_policy_seq (tpm2key_policy_t policy_seq)
{
tpm2key_policy_t policy;
tpm2key_policy_t next;
if (policy_seq == NULL)
return;
FOR_LIST_ELEMENTS_SAFE (policy, next, policy_seq)
{
grub_free (policy->cmd_policy);
grub_free (policy);
}
}
grub_err_t
grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key, tpm2key_authpolicy_t *authpol_seq)
{
tpm2key_authpolicy_t tmp_seq = NULL;
tpm2key_authpolicy_t authpol = NULL;
int authpol_n;
char authpol_pol[AUTHPOLICY_POL_MAX];
int i;
int ret;
grub_err_t err;
ret = asn1_number_of_elements (tpm2key, "authPolicy", &authpol_n);
if (ret == ASN1_ELEMENT_NOT_FOUND)
{
/* "authPolicy" is optional, so it may not be available */
*authpol_seq = NULL;
return GRUB_ERR_NONE;
}
else if (ret != ASN1_SUCCESS)
return grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve authPolicy");
/* Limit the number of authPolicy elements to two digits (99) */
if (authpol_n > TPM2KEY_ELEMENTS_MAX || authpol_n < TPM2KEY_ELEMENTS_MIN)
return grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid number of authPolicy elements");
/*
* Iterate the authPolicy elements backwards since grub_list_push() prepends
* the item into the list.
*/
for (i = authpol_n; i >= 1; i--) {
authpol = grub_zalloc (sizeof (struct tpm2key_authpolicy));
if (authpol == NULL)
{
err = grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate memory for authPolicy");
goto error;
}
grub_snprintf (authpol_pol, AUTHPOLICY_POL_MAX, "authPolicy.?%d.Policy", i);
ret = tpm2key_get_policy_seq (tpm2key, authpol_pol, &authpol->policy_seq);
if (ret != ASN1_SUCCESS)
{
err = grub_error (GRUB_ERR_READ_ERROR, "failed to retrieve policy from authPolicy");
goto error;
}
/* Prepend the authPolicy element into the sequence */
grub_list_push (GRUB_AS_LIST_P (&tmp_seq), GRUB_AS_LIST (authpol));
}
*authpol_seq = tmp_seq;
return GRUB_ERR_NONE;
error:
if (authpol != NULL)
{
grub_tpm2key_free_policy_seq (authpol->policy_seq);
grub_free (authpol);
}
grub_tpm2key_free_authpolicy_seq (tmp_seq);
return err;
}
void
grub_tpm2key_free_authpolicy_seq (tpm2key_authpolicy_t authpol_seq)
{
tpm2key_authpolicy_t authpol;
tpm2key_authpolicy_t next;
if (authpol_seq == NULL)
return;
FOR_LIST_ELEMENTS_SAFE (authpol, next, authpol_seq)
{
grub_tpm2key_free_policy_seq (authpol->policy_seq);
grub_free (authpol);
}
}

View file

@ -0,0 +1,87 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2023 SUSE LLC
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_TPM2_TPM2KEY_HEADER
#define GRUB_TPM2_TPM2KEY_HEADER 1
#include <grub/types.h>
#include <libtasn1.h>
/*
* TPMPolicy ::= SEQUENCE {
* CommandCode [0] EXPLICIT INTEGER,
* CommandPolicy [1] EXPLICIT OCTET STRING
* }
*/
struct tpm2key_policy {
struct tpm2key_policy *next;
struct tpm2key_policy **prev;
grub_uint32_t cmd_code;
void *cmd_policy;
grub_uint16_t cmd_policy_len;
};
typedef struct tpm2key_policy *tpm2key_policy_t;
/*
* TPMAuthPolicy ::= SEQUENCE {
* Name [0] EXPLICIT UTF8String OPTIONAL,
* Policy [1] EXPLICIT SEQUENCE OF TPMPolicy
* }
*
* Name is not a necessary part to unseal the key. Ignore it.
*/
struct tpm2key_authpolicy {
struct tpm2key_authpolicy *next;
struct tpm2key_authpolicy **prev;
/* char *name; */
tpm2key_policy_t policy_seq;
};
typedef struct tpm2key_authpolicy *tpm2key_authpolicy_t;
extern grub_err_t
grub_tpm2key_start_parsing (asn1_node *parsed_tpm2key, void *data, grub_size_t size);
extern void
grub_tpm2key_end_parsing (asn1_node tpm2key);
extern grub_err_t
grub_tpm2key_get_rsaparent (asn1_node tpm2key, grub_uint8_t *rsaparent);
extern grub_err_t
grub_tpm2key_get_parent (asn1_node tpm2key, grub_uint32_t *parent);
extern grub_err_t
grub_tpm2key_get_pubkey (asn1_node tpm2key, void **data, grub_size_t *size);
extern grub_err_t
grub_tpm2key_get_privkey (asn1_node tpm2key, void **data, grub_size_t *size);
extern grub_err_t
grub_tpm2key_get_policy_seq (asn1_node tpm2key, tpm2key_policy_t *policy_seq);
extern void
grub_tpm2key_free_policy_seq (tpm2key_policy_t policy_seq);
extern grub_err_t
grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key, tpm2key_authpolicy_t *authpol_seq);
extern void
grub_tpm2key_free_authpolicy_seq (tpm2key_authpolicy_t authpol_seq);
#endif /* GRUB_TPM2_TPM2KEY_HEADER */

View file

@ -0,0 +1,63 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file is generated by 'asn1Parser tpm2key.asn' and the '#include'
* headers are replaced with the ones in grub2.
* - 'grub/mm.h' for the definition of 'NULL'
* - 'libtasn1.h' for the definition of 'asn1_static_node'
*/
#include <grub/mm.h>
#include <libtasn1.h>
const asn1_static_node tpm2key_asn1_tab[] = {
{ "TPM2KEY", 536875024, NULL },
{ NULL, 1073741836, NULL },
{ "TPMPolicy", 1610612741, NULL },
{ "CommandCode", 1610620931, NULL },
{ NULL, 2056, "0"},
{ "CommandPolicy", 536879111, NULL },
{ NULL, 2056, "1"},
{ "TPMAuthPolicy", 1610612741, NULL },
{ "Name", 1610637346, NULL },
{ NULL, 2056, "0"},
{ "Policy", 536879115, NULL },
{ NULL, 1073743880, "1"},
{ NULL, 2, "TPMPolicy"},
{ "TPMKey", 536870917, NULL },
{ "type", 1073741836, NULL },
{ "emptyAuth", 1610637316, NULL },
{ NULL, 2056, "0"},
{ "policy", 1610637323, NULL },
{ NULL, 1073743880, "1"},
{ NULL, 2, "TPMPolicy"},
{ "secret", 1610637319, NULL },
{ NULL, 2056, "2"},
{ "authPolicy", 1610637323, NULL },
{ NULL, 1073743880, "3"},
{ NULL, 2, "TPMAuthPolicy"},
{ "description", 1610637346, NULL },
{ NULL, 2056, "4"},
{ "rsaParent", 1610637316, NULL },
{ NULL, 2056, "5"},
{ "parent", 1073741827, NULL },
{ "pubkey", 1073741831, NULL },
{ "privkey", 7, NULL },
{ NULL, 0, NULL }
};

View file

@ -191,6 +191,11 @@ grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
/* Don't worry about errors. */ /* Don't worry about errors. */
grub_errno = GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE;
} }
else if (id != GRUB_VIDEO_DRIVER_NONE)
{
grub_puts_ (N_(" A video driver is active, cannot initialize this driver until it is deactivated\n"));
continue;
}
else else
{ {
if (adapter->init ()) if (adapter->init ())

View file

@ -152,8 +152,7 @@ struct grub_ahci_device
static grub_err_t static grub_err_t
grub_ahci_readwrite_real (struct grub_ahci_device *dev, grub_ahci_readwrite_real (struct grub_ahci_device *dev,
struct grub_disk_ata_pass_through_parms *parms, struct grub_disk_ata_pass_through_parms *parms, int reset);
int spinup, int reset);
enum enum
@ -573,7 +572,7 @@ grub_ahci_pciinit (grub_pci_device_t dev,
/* struct grub_disk_ata_pass_through_parms parms2; /* struct grub_disk_ata_pass_through_parms parms2;
grub_memset (&parms2, 0, sizeof (parms2)); grub_memset (&parms2, 0, sizeof (parms2));
parms2.taskfile.cmd = 8; parms2.taskfile.cmd = 8;
grub_ahci_readwrite_real (dev, &parms2, 1, 1);*/ grub_ahci_readwrite_real (dev, &parms2, 1);*/
} }
endtime = grub_get_time_ms () + 32000; endtime = grub_get_time_ms () + 32000;
@ -908,15 +907,14 @@ grub_ahci_reset_port (struct grub_ahci_device *dev, int force)
dev->hba->ports[dev->port].sata_error = dev->hba->ports[dev->port].sata_error; dev->hba->ports[dev->port].sata_error = dev->hba->ports[dev->port].sata_error;
grub_memset (&parms2, 0, sizeof (parms2)); grub_memset (&parms2, 0, sizeof (parms2));
parms2.taskfile.cmd = 8; parms2.taskfile.cmd = 8;
return grub_ahci_readwrite_real (dev, &parms2, 1, 1); return grub_ahci_readwrite_real (dev, &parms2, 1);
} }
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
static grub_err_t static grub_err_t
grub_ahci_readwrite_real (struct grub_ahci_device *dev, grub_ahci_readwrite_real (struct grub_ahci_device *dev,
struct grub_disk_ata_pass_through_parms *parms, struct grub_disk_ata_pass_through_parms *parms, int reset)
int spinup, int reset)
{ {
struct grub_pci_dma_chunk *bufc; struct grub_pci_dma_chunk *bufc;
grub_uint64_t endtime; grub_uint64_t endtime;
@ -1038,7 +1036,7 @@ grub_ahci_readwrite_real (struct grub_ahci_device *dev,
grub_dprintf ("ahci", "AHCI tfd = %x\n", grub_dprintf ("ahci", "AHCI tfd = %x\n",
dev->hba->ports[dev->port].task_file_data); dev->hba->ports[dev->port].task_file_data);
endtime = grub_get_time_ms () + (spinup ? 20000 : 20000); endtime = grub_get_time_ms () + 20000;
while ((dev->hba->ports[dev->port].command_issue & 1)) while ((dev->hba->ports[dev->port].command_issue & 1))
if (grub_get_time_ms () > endtime || if (grub_get_time_ms () > endtime ||
(dev->hba->ports[dev->port].intstatus & GRUB_AHCI_HBA_PORT_IS_FATAL_MASK)) (dev->hba->ports[dev->port].intstatus & GRUB_AHCI_HBA_PORT_IS_FATAL_MASK))
@ -1097,9 +1095,9 @@ grub_ahci_readwrite_real (struct grub_ahci_device *dev,
static grub_err_t static grub_err_t
grub_ahci_readwrite (grub_ata_t disk, grub_ahci_readwrite (grub_ata_t disk,
struct grub_disk_ata_pass_through_parms *parms, struct grub_disk_ata_pass_through_parms *parms,
int spinup) int spinup __attribute__((__unused__)))
{ {
return grub_ahci_readwrite_real (disk->data, parms, spinup, 0); return grub_ahci_readwrite_real (disk->data, parms, 0);
} }
static grub_err_t static grub_err_t

View file

@ -112,10 +112,10 @@ grub_ata_identify (struct grub_ata *dev)
return grub_atapi_identify (dev); return grub_atapi_identify (dev);
info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE); info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE);
if (info64 == NULL)
return grub_errno;
info32 = (grub_uint32_t *) info64; info32 = (grub_uint32_t *) info64;
info16 = (grub_uint16_t *) info64; info16 = (grub_uint16_t *) info64;
if (! info16)
return grub_errno;
grub_memset (&parms, 0, sizeof (parms)); grub_memset (&parms, 0, sizeof (parms));
parms.buffer = info16; parms.buffer = info16;

View file

@ -17,6 +17,7 @@
*/ */
#include <grub/cryptodisk.h> #include <grub/cryptodisk.h>
#include <grub/env.h>
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/misc.h> #include <grub/misc.h>
#include <grub/dl.h> #include <grub/dl.h>
@ -26,6 +27,8 @@
#include <grub/file.h> #include <grub/file.h>
#include <grub/procfs.h> #include <grub/procfs.h>
#include <grub/partition.h> #include <grub/partition.h>
#include <grub/key_protector.h>
#include <grub/safemath.h>
#ifdef GRUB_UTIL #ifdef GRUB_UTIL
#include <grub/emu/hostdisk.h> #include <grub/emu/hostdisk.h>
@ -44,7 +47,8 @@ enum
OPTION_KEYFILE, OPTION_KEYFILE,
OPTION_KEYFILE_OFFSET, OPTION_KEYFILE_OFFSET,
OPTION_KEYFILE_SIZE, OPTION_KEYFILE_SIZE,
OPTION_HEADER OPTION_HEADER,
OPTION_PROTECTOR
}; };
static const struct grub_arg_option options[] = static const struct grub_arg_option options[] =
@ -58,6 +62,8 @@ static const struct grub_arg_option options[] =
{"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT}, {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT},
{"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT}, {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT},
{"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING}, {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
{"protector", 'P', GRUB_ARG_OPTION_REPEATABLE,
N_("Unlock volume(s) using key protector(s)."), 0, ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0} {0, 0, 0, 0, 0, 0}
}; };
@ -1061,6 +1067,7 @@ grub_cryptodisk_scan_device_real (const char *name,
grub_err_t ret = GRUB_ERR_NONE; grub_err_t ret = GRUB_ERR_NONE;
grub_cryptodisk_t dev; grub_cryptodisk_t dev;
grub_cryptodisk_dev_t cr; grub_cryptodisk_dev_t cr;
int i;
struct cryptodisk_read_hook_ctx read_hook_data = {0}; struct cryptodisk_read_hook_ctx read_hook_data = {0};
int askpass = 0; int askpass = 0;
char *part = NULL; char *part = NULL;
@ -1113,41 +1120,175 @@ grub_cryptodisk_scan_device_real (const char *name,
goto error_no_close; goto error_no_close;
if (!dev) if (!dev)
continue; continue;
break;
if (!cargs->key_len)
{
/* Get the passphrase from the user, if no key data. */
askpass = 1;
part = grub_partition_get_name (source->partition);
grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
source->partition != NULL ? "," : "",
part != NULL ? part : N_("UNKNOWN"),
dev->uuid);
grub_free (part);
cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
if (cargs->key_data == NULL)
goto error_no_close;
if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
goto error;
}
cargs->key_len = grub_strlen ((char *) cargs->key_data);
}
ret = cr->recover_key (source, dev, cargs);
if (ret != GRUB_ERR_NONE)
goto error;
ret = grub_cryptodisk_insert (dev, name, source);
if (ret != GRUB_ERR_NONE)
goto error;
goto cleanup;
} }
grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
if (dev == NULL)
{
grub_error (GRUB_ERR_BAD_MODULE,
"no cryptodisk module can handle this device");
goto error_no_close;
}
if (cargs->protectors)
{
for (i = 0; cargs->protectors[i]; i++)
{
if (cargs->key_cache[i].invalid)
continue;
if (cargs->key_cache[i].key == NULL)
{
ret = grub_key_protector_recover_key (cargs->protectors[i],
&cargs->key_cache[i].key,
&cargs->key_cache[i].key_len);
if (ret != GRUB_ERR_NONE)
{
if (grub_errno)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
grub_dprintf ("cryptodisk",
"failed to recover a key from key protector "
"%s, will not try it again for any other "
"disks, if any, during this invocation of "
"cryptomount\n",
cargs->protectors[i]);
cargs->key_cache[i].invalid = 1;
continue;
}
}
cargs->key_data = cargs->key_cache[i].key;
cargs->key_len = cargs->key_cache[i].key_len;
ret = cr->recover_key (source, dev, cargs);
if (ret != GRUB_ERR_NONE)
{
/* Reset key data to trigger the passphrase prompt later */
cargs->key_data = NULL;
cargs->key_len = 0;
part = grub_partition_get_name (source->partition);
grub_dprintf ("cryptodisk",
"recovered a key from key protector %s but it "
"failed to unlock %s%s%s (%s)\n",
cargs->protectors[i], source->name,
source->partition != NULL ? "," : "",
part != NULL ? part : N_("UNKNOWN"), dev->uuid);
grub_free (part);
continue;
}
else
{
ret = grub_cryptodisk_insert (dev, name, source);
if (ret != GRUB_ERR_NONE)
goto error;
#ifndef GRUB_UTIL
grub_cli_set_auth_needed ();
#endif
goto cleanup;
}
}
part = grub_partition_get_name (source->partition);
grub_error (GRUB_ERR_ACCESS_DENIED,
N_("no key protector provided a usable key for %s%s%s (%s)"),
source->name, source->partition != NULL ? "," : "",
part != NULL ? part : N_("UNKNOWN"), dev->uuid);
grub_free (part);
}
if (cargs->key_len)
{
ret = cr->recover_key (source, dev, cargs);
if (ret != GRUB_ERR_NONE)
goto error;
}
else
{
/* Get the passphrase from the user, if no key data. */
unsigned long tries = 3;
const char *tries_env;
/*
* Print the error from key protectors and clear grub_errno.
*
* Since '--protector' cannot coexist with '--password' and
* '--key-file', in case key protectors fail, only
* "cargs->key_len == 0" is expected, so cryptomount falls back
* here to request the passphrase.
*
* To avoid the error from key protectors stops the further code,
* print the error to notify the user why key protectors fail and
* clear grub_errno to have a fresh start.
*/
if (grub_errno != GRUB_ERR_NONE)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
askpass = 1;
cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
if (cargs->key_data == NULL)
goto error_no_close;
tries_env = grub_env_get ("cryptodisk_passphrase_tries");
if (tries_env != NULL && tries_env[0] != '\0')
{
unsigned long tries_env_val;
const char *p;
tries_env_val = grub_strtoul (tries_env, &p, 0);
if (*p == '\0' && tries_env_val != ~0UL)
tries = tries_env_val;
else
grub_printf_ (N_("Invalid cryptodisk_passphrase_tries value `%s'. Defaulting to %lu.\n"),
tries_env,
tries);
}
for (; tries > 0; tries--)
{
part = grub_partition_get_name (source->partition);
grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
source->partition != NULL ? "," : "",
part != NULL ? part : N_("UNKNOWN"),
dev->uuid);
grub_free (part);
if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
goto error;
}
cargs->key_len = grub_strlen ((char *) cargs->key_data);
ret = cr->recover_key (source, dev, cargs);
if (ret == GRUB_ERR_NONE)
break;
if (ret != GRUB_ERR_ACCESS_DENIED || tries == 1)
goto error;
grub_puts_ (N_("Invalid passphrase."));
/*
* Since recover_key() calls a function that returns grub_errno,
* a leftover error value from a previously rejected passphrase
* will trigger a phantom failure. We therefore clear it before
* trying a new passphrase.
*/
grub_errno = GRUB_ERR_NONE;
}
}
ret = grub_cryptodisk_insert (dev, name, source);
if (ret != GRUB_ERR_NONE)
goto error;
goto cleanup; goto cleanup;
error: error:
@ -1161,6 +1302,7 @@ grub_cryptodisk_scan_device_real (const char *name,
if (askpass) if (askpass)
{ {
grub_memset (cargs->key_data, 0, cargs->key_len);
cargs->key_len = 0; cargs->key_len = 0;
grub_free (cargs->key_data); grub_free (cargs->key_data);
} }
@ -1236,7 +1378,8 @@ grub_cryptodisk_scan_device (const char *name,
dev = grub_cryptodisk_scan_device_real (name, source, cargs); dev = grub_cryptodisk_scan_device_real (name, source, cargs);
if (dev) if (dev)
{ {
ret = (cargs->search_uuid != NULL && grub_strcasecmp (cargs->search_uuid, dev->uuid) == 0); ret = (cargs->search_uuid != NULL
&& grub_uuidcasecmp (cargs->search_uuid, dev->uuid, sizeof (dev->uuid)) == 0);
goto cleanup; goto cleanup;
} }
@ -1258,6 +1401,24 @@ grub_cryptodisk_scan_device (const char *name,
return ret; return ret;
} }
static void
grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs)
{
int i;
if (cargs->key_cache == NULL || cargs->protectors == NULL)
return;
for (i = 0; cargs->protectors[i]; i++)
{
if (cargs->key_cache[i].key)
grub_memset (cargs->key_cache[i].key, 0, cargs->key_cache[i].key_len);
grub_free (cargs->key_cache[i].key);
}
grub_free (cargs->key_cache);
}
static grub_err_t static grub_err_t
grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
{ {
@ -1270,6 +1431,14 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
if (grub_cryptodisk_list == NULL) if (grub_cryptodisk_list == NULL)
return grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk modules loaded"); return grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk modules loaded");
if (state[OPTION_PASSWORD].set && state[OPTION_PROTECTOR].set) /* password and key protector */
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"a password and a key protector cannot both be set");
if (state[OPTION_KEYFILE].set && state[OPTION_PROTECTOR].set) /* key file and key protector */
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"a key file and a key protector cannot both be set");
if (state[OPTION_PASSWORD].set) /* password */ if (state[OPTION_PASSWORD].set) /* password */
{ {
cargs.key_data = (grub_uint8_t *) state[OPTION_PASSWORD].arg; cargs.key_data = (grub_uint8_t *) state[OPTION_PASSWORD].arg;
@ -1319,9 +1488,9 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
if (keyfile_offset > keyfile->size) if (keyfile_offset > keyfile->size)
return grub_error (GRUB_ERR_OUT_OF_RANGE, return grub_error (GRUB_ERR_OUT_OF_RANGE,
N_("Keyfile offset, %llu, is greater than" N_("Keyfile offset, %llu, is greater than "
"keyfile size, %" PRIuGRUB_UINT64_T), "keyfile size, %llu"),
keyfile_offset, keyfile->size); keyfile_offset, (unsigned long long) keyfile->size);
if (grub_file_seek (keyfile, (grub_off_t) keyfile_offset) == (grub_off_t) -1) if (grub_file_seek (keyfile, (grub_off_t) keyfile_offset) == (grub_off_t) -1)
return grub_errno; return grub_errno;
@ -1362,6 +1531,15 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
return grub_errno; return grub_errno;
} }
if (state[OPTION_PROTECTOR].set) /* key protector(s) */
{
cargs.key_cache = grub_calloc (state[OPTION_PROTECTOR].set, sizeof (*cargs.key_cache));
if (cargs.key_cache == NULL)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"no memory for key protector key cache");
cargs.protectors = state[OPTION_PROTECTOR].args;
}
if (state[OPTION_UUID].set) /* uuid */ if (state[OPTION_UUID].set) /* uuid */
{ {
int found_uuid; int found_uuid;
@ -1370,6 +1548,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
dev = grub_cryptodisk_get_by_uuid (args[0]); dev = grub_cryptodisk_get_by_uuid (args[0]);
if (dev) if (dev)
{ {
grub_cryptodisk_clear_key_cache (&cargs);
grub_dprintf ("cryptodisk", grub_dprintf ("cryptodisk",
"already mounted as crypto%lu\n", dev->id); "already mounted as crypto%lu\n", dev->id);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
@ -1378,6 +1557,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
cargs.check_boot = state[OPTION_BOOT].set; cargs.check_boot = state[OPTION_BOOT].set;
cargs.search_uuid = args[0]; cargs.search_uuid = args[0];
found_uuid = grub_device_iterate (&grub_cryptodisk_scan_device, &cargs); found_uuid = grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
grub_cryptodisk_clear_key_cache (&cargs);
if (found_uuid) if (found_uuid)
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
@ -1397,6 +1577,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
{ {
cargs.check_boot = state[OPTION_BOOT].set; cargs.check_boot = state[OPTION_BOOT].set;
grub_device_iterate (&grub_cryptodisk_scan_device, &cargs); grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
grub_cryptodisk_clear_key_cache (&cargs);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
else else
@ -1420,6 +1601,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
disk = grub_disk_open (diskname); disk = grub_disk_open (diskname);
if (!disk) if (!disk)
{ {
grub_cryptodisk_clear_key_cache (&cargs);
if (disklast) if (disklast)
*disklast = ')'; *disklast = ')';
return grub_errno; return grub_errno;
@ -1430,12 +1612,14 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
{ {
grub_dprintf ("cryptodisk", "already mounted as crypto%lu\n", dev->id); grub_dprintf ("cryptodisk", "already mounted as crypto%lu\n", dev->id);
grub_disk_close (disk); grub_disk_close (disk);
grub_cryptodisk_clear_key_cache (&cargs);
if (disklast) if (disklast)
*disklast = ')'; *disklast = ')';
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
dev = grub_cryptodisk_scan_device_real (diskname, disk, &cargs); dev = grub_cryptodisk_scan_device_real (diskname, disk, &cargs);
grub_cryptodisk_clear_key_cache (&cargs);
grub_disk_close (disk); grub_disk_close (disk);
if (disklast) if (disklast)
@ -1472,40 +1656,70 @@ static char *
luks_script_get (grub_size_t *sz) luks_script_get (grub_size_t *sz)
{ {
grub_cryptodisk_t i; grub_cryptodisk_t i;
grub_size_t size = 0; grub_size_t size = 0, mul;
char *ptr, *ret; char *ptr, *ret;
*sz = 0; *sz = 0;
for (i = cryptodisk_list; i != NULL; i = i->next) for (i = cryptodisk_list; i != NULL; i = i->next)
if (grub_strcmp (i->modname, "luks") == 0) if (grub_strcmp (i->modname, "luks") == 0 ||
grub_strcmp (i->modname, "luks2") == 0)
{ {
size += sizeof ("luks_mount "); /*
size += grub_strlen (i->uuid); * Add space in the line for (in order) spaces, cipher mode, cipher IV
size += grub_strlen (i->cipher->cipher->name); * mode, sector offset, sector size and the trailing newline. This is
size += 54; * an upper bound on the size of this data. There are 15 extra bytes
* in an earlier version of this code that are unaccounted for. It is
* left in the calculations in case it is needed. At worst, its short-
* lived wasted space.
*
* 60 = 5 + 5 + 8 + 20 + 6 + 1 + 15
*/
if (grub_add (size, grub_strlen (i->modname), &size) ||
grub_add (size, sizeof ("_mount") + 60, &size) ||
grub_add (size, grub_strlen (i->uuid), &size) ||
grub_add (size, grub_strlen (i->cipher->cipher->name), &size) ||
grub_mul (i->keysize, 2, &mul) ||
grub_add (size, mul, &size))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
return 0;
}
if (i->essiv_hash) if (i->essiv_hash)
size += grub_strlen (i->essiv_hash->name); {
size += i->keysize * 2; if (grub_add (size, grub_strlen (i->essiv_hash->name), &size))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
return 0;
}
}
} }
if (grub_add (size, 1, &size))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script");
return 0;
}
ret = grub_malloc (size + 1); ret = grub_malloc (size);
if (!ret) if (!ret)
return 0; return 0;
ptr = ret; ptr = ret;
for (i = cryptodisk_list; i != NULL; i = i->next) for (i = cryptodisk_list; i != NULL; i = i->next)
if (grub_strcmp (i->modname, "luks") == 0) if (grub_strcmp (i->modname, "luks") == 0 ||
grub_strcmp (i->modname, "luks2") == 0)
{ {
unsigned j; unsigned j;
const char *iptr; const char *iptr;
ptr = grub_stpcpy (ptr, "luks_mount "); ptr = grub_stpcpy (ptr, i->modname);
ptr = grub_stpcpy (ptr, "_mount ");
ptr = grub_stpcpy (ptr, i->uuid); ptr = grub_stpcpy (ptr, i->uuid);
*ptr++ = ' '; *ptr++ = ' ';
grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors); ptr += grub_snprintf (ptr, 21, "%" PRIxGRUB_OFFSET, i->offset_sectors);
while (*ptr) *ptr++ = ' ';
ptr++; ptr += grub_snprintf (ptr, 7, "%u", 1 << i->log_sector_size);
*ptr++ = ' ';
for (iptr = i->cipher->cipher->name; *iptr; iptr++) for (iptr = i->cipher->cipher->name; *iptr; iptr++)
*ptr++ = grub_tolower (*iptr); *ptr++ = grub_tolower (*iptr);
switch (i->mode) switch (i->mode)
@ -1562,6 +1776,114 @@ luks_script_get (grub_size_t *sz)
return ret; return ret;
} }
#ifdef GRUB_MACHINE_EFI
grub_err_t
grub_cryptodisk_challenge_password (void)
{
grub_cryptodisk_t cr_dev;
for (cr_dev = cryptodisk_list; cr_dev != NULL; cr_dev = cr_dev->next)
{
grub_cryptodisk_dev_t cr;
grub_disk_t source = NULL;
grub_err_t ret = GRUB_ERR_NONE;
grub_cryptodisk_t dev = NULL;
char *part = NULL;
struct grub_cryptomount_args cargs = {0};
cargs.check_boot = 0;
cargs.search_uuid = cr_dev->uuid;
source = grub_disk_open (cr_dev->source);
if (source == NULL)
{
ret = grub_errno;
goto error_out;
}
FOR_CRYPTODISK_DEVS (cr)
{
dev = cr->scan (source, &cargs);
if (grub_errno)
{
ret = grub_errno;
goto error_out;
}
if (dev == NULL)
continue;
break;
}
if (dev == NULL)
{
ret = grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
goto error_out;
}
part = grub_partition_get_name (source->partition);
grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
source->partition != NULL ? "," : "",
part != NULL ? part : N_("UNKNOWN"), cr_dev->uuid);
grub_free (part);
cargs.key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
if (cargs.key_data == NULL)
{
ret = grub_errno;
goto error_out;
}
if (!grub_password_get ((char *) cargs.key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
{
ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
goto error_out;
}
cargs.key_len = grub_strlen ((char *) cargs.key_data);
ret = cr->recover_key (source, dev, &cargs);
error_out:
grub_disk_close (source);
if (dev != NULL)
cryptodisk_close (dev);
if (cargs.key_data)
{
grub_memset (cargs.key_data, 0, cargs.key_len);
grub_free (cargs.key_data);
}
return ret;
}
return GRUB_ERR_NONE;
}
void
grub_cryptodisk_erasesecrets (void)
{
grub_cryptodisk_t i;
grub_uint8_t *buf;
buf = grub_zalloc (GRUB_CRYPTODISK_MAX_KEYLEN);
if (buf == NULL)
grub_fatal ("grub_cryptodisk_erasesecrets: cannot allocate memory");
for (i = cryptodisk_list; i != NULL; i = i->next)
if (grub_cryptodisk_setkey (i, buf, i->keysize))
grub_fatal ("grub_cryptodisk_erasesecrets: cannot erase secrets for %s", i->source);
else
grub_printf ("Erased crypto secrets for %s\n", i->source);
/*
* Unfortunately, there is no way to "force unmount" a given disk, it may
* have mounted "child" disks as well, e.g., an LVM volume. So, this
* function MUST be called when there is no way back, e.g., when exiting.
* Otherwise, subsequent read calls for a cryptodisk will return garbage.
*/
grub_free (buf);
}
#endif /* GRUB_MACHINE_EFI */
struct grub_procfs_entry luks_script = struct grub_procfs_entry luks_script =
{ {
.name = "luks_script", .name = "luks_script",
@ -1576,6 +1898,7 @@ GRUB_MOD_INIT (cryptodisk)
cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0, cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
N_("[ [-p password] | [-k keyfile" N_("[ [-p password] | [-k keyfile"
" [-O keyoffset] [-S keysize] ] ] [-H file]" " [-O keyoffset] [-S keysize] ] ] [-H file]"
" [-P protector [-P protector ...]]"
" <SOURCE|-u UUID|-a|-b>"), " <SOURCE|-u UUID|-a|-b>"),
N_("Mount a crypto device."), options); N_("Mount a crypto device."), options);
grub_procfs_register ("luks_script", &luks_script); grub_procfs_register ("luks_script", &luks_script);
@ -1583,6 +1906,9 @@ GRUB_MOD_INIT (cryptodisk)
GRUB_MOD_FINI (cryptodisk) GRUB_MOD_FINI (cryptodisk)
{ {
#ifdef GRUB_MACHINE_EFI
grub_cryptodisk_erasesecrets ();
#endif
grub_disk_dev_unregister (&grub_cryptodisk_dev); grub_disk_dev_unregister (&grub_cryptodisk_dev);
cryptodisk_cleanup (); cryptodisk_cleanup ();
grub_unregister_extcmd (cmd); grub_unregister_extcmd (cmd);

View file

@ -20,10 +20,12 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/disk.h> #include <grub/disk.h>
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/command.h>
#include <grub/err.h> #include <grub/err.h>
#include <grub/misc.h> #include <grub/misc.h>
#include <grub/diskfilter.h> #include <grub/diskfilter.h>
#include <grub/partition.h> #include <grub/partition.h>
#include <grub/safemath.h>
#ifdef GRUB_UTIL #ifdef GRUB_UTIL
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/util/misc.h> #include <grub/util/misc.h>
@ -226,15 +228,28 @@ scan_devices (const char *arname)
int need_rescan; int need_rescan;
for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++) for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
for (p = grub_disk_dev_list; p; p = p->next) {
if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID /* look up the crytodisk devices first */
&& p->disk_iterate) for (p = grub_disk_dev_list; p; p = p->next)
{ if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID && p->disk_iterate)
if ((p->disk_iterate) (scan_disk_hook, NULL, pull)) {
return; if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
if (arname && is_lv_readable (find_lv (arname), 1)) return;
return; if (arname && is_lv_readable (find_lv (arname), 1))
} return;
break;
}
/* check the devices other than crytodisk */
for (p = grub_disk_dev_list; p; p = p->next)
if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID && p->disk_iterate)
{
if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
return;
if (arname && is_lv_readable (find_lv (arname), 1))
return;
}
}
scan_depth = 0; scan_depth = 0;
need_rescan = 1; need_rescan = 1;
@ -720,7 +735,7 @@ read_segment (struct grub_diskfilter_segment *seg, grub_disk_addr_t sector,
case GRUB_DISKFILTER_RAID6: case GRUB_DISKFILTER_RAID6:
{ {
grub_disk_addr_t read_sector; grub_disk_addr_t read_sector;
grub_uint64_t b, p, n, disknr, e; grub_uint64_t b, p, n, disknr;
/* n = 1 for level 4 and 5, 2 for level 6. */ /* n = 1 for level 4 and 5, 2 for level 6. */
n = seg->type / 3; n = seg->type / 3;
@ -770,7 +785,6 @@ read_segment (struct grub_diskfilter_segment *seg, grub_disk_addr_t sector,
if (read_size > size) if (read_size > size)
read_size = size; read_size = size;
e = 0;
/* Reset read error. */ /* Reset read error. */
if (grub_errno == GRUB_ERR_READ_ERROR if (grub_errno == GRUB_ERR_READ_ERROR
|| grub_errno == GRUB_ERR_UNKNOWN_DEVICE) || grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
@ -784,7 +798,6 @@ read_segment (struct grub_diskfilter_segment *seg, grub_disk_addr_t sector,
if ((err) && (err != GRUB_ERR_READ_ERROR if ((err) && (err != GRUB_ERR_READ_ERROR
&& err != GRUB_ERR_UNKNOWN_DEVICE)) && err != GRUB_ERR_UNKNOWN_DEVICE))
return err; return err;
e++;
if (err) if (err)
{ {
@ -968,8 +981,6 @@ grub_diskfilter_vg_register (struct grub_diskfilter_vg *vg)
for (lv = vg->lvs; lv; lv = lv->next) for (lv = vg->lvs; lv; lv = lv->next)
{ {
grub_err_t err;
/* RAID 1 and single-disk RAID 0 don't use a chunksize but code /* RAID 1 and single-disk RAID 0 don't use a chunksize but code
assumes one so set one. */ assumes one so set one. */
for (i = 0; i < lv->segment_count; i++) for (i = 0; i < lv->segment_count; i++)
@ -981,6 +992,10 @@ grub_diskfilter_vg_register (struct grub_diskfilter_vg *vg)
&& lv->segments[i].stripe_size == 0) && lv->segments[i].stripe_size == 0)
lv->segments[i].stripe_size = 64; lv->segments[i].stripe_size = 64;
} }
}
for (lv = vg->lvs; lv; lv = lv->next)
{
grub_err_t err;
err = validate_lv(lv); err = validate_lv(lv);
if (err) if (err)
@ -1041,7 +1056,7 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
{ {
struct grub_diskfilter_vg *array; struct grub_diskfilter_vg *array;
int i; int i;
grub_size_t j; grub_size_t j, sz;
grub_uint64_t totsize; grub_uint64_t totsize;
struct grub_diskfilter_pv *pv; struct grub_diskfilter_pv *pv;
grub_err_t err; grub_err_t err;
@ -1142,7 +1157,11 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
} }
array->lvs->vg = array; array->lvs->vg = array;
array->lvs->idname = grub_malloc (sizeof ("mduuid/") + 2 * uuidlen); if (grub_mul (uuidlen, 2, &sz) ||
grub_add (sz, sizeof ("mduuid/"), &sz))
goto fail;
array->lvs->idname = grub_malloc (sz);
if (!array->lvs->idname) if (!array->lvs->idname)
goto fail; goto fail;
@ -1359,6 +1378,86 @@ grub_diskfilter_get_pv_from_disk (grub_disk_t disk,
} }
#endif #endif
static int
grub_diskfilter_check_pvs_encrypted (grub_disk_t disk, int *pvs_cnt)
{
struct grub_diskfilter_lv *lv = disk->data;
struct grub_diskfilter_pv *pv;
*pvs_cnt = 0;
if (lv->vg->pvs)
for (pv = lv->vg->pvs; pv; pv = pv->next)
{
(*pvs_cnt)++;
if (pv->disk == NULL)
{
/* Can be a partially activated VG, bail out. */
return GRUB_ERR_TEST_FAILURE;
}
if (pv->disk->dev->id != GRUB_DISK_DEVICE_CRYPTODISK_ID)
{
/* All backing devices must be cryptodisks, stop. */
return GRUB_ERR_TEST_FAILURE;
}
}
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_cryptocheck (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
grub_disk_t disk;
int check_pvs_res;
int namelen;
int pvs_cnt;
int opt_quiet = 0;
if (argc == 2)
{
if (grub_strcmp (args[0], "--quiet") == 0)
{
opt_quiet = 1;
argc--;
args++;
}
else
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unrecognized option: %s"), args[0]);
}
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("disk name expected"));
namelen = grub_strlen (args[0]);
if (namelen > 2 && (args[0][0] == '(') && (args[0][namelen - 1] == ')'))
args[0][namelen - 1] = 0;
else
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("invalid disk: %s"),
args[0]);
if (!is_valid_diskfilter_name (&args[0][1]))
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("unrecognized disk: %s"),
&args[0][1]);
disk = grub_disk_open (&args[0][1]);
if (disk == NULL)
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("no such disk: %s"),
&args[0][1]);
check_pvs_res = grub_diskfilter_check_pvs_encrypted (disk, &pvs_cnt);
grub_disk_close (disk);
if (!opt_quiet)
grub_printf ("%s is %sencrypted (%d pv%s examined)\n", &args[0][1],
(check_pvs_res == GRUB_ERR_NONE) ? "" : "un",
pvs_cnt,
(pvs_cnt > 1) ? "s" : "");
return check_pvs_res;
}
static struct grub_disk_dev grub_diskfilter_dev = static struct grub_disk_dev grub_diskfilter_dev =
{ {
.name = "diskfilter", .name = "diskfilter",
@ -1375,14 +1474,21 @@ static struct grub_disk_dev grub_diskfilter_dev =
.next = 0 .next = 0
}; };
static grub_command_t cmd;
GRUB_MOD_INIT(diskfilter) GRUB_MOD_INIT(diskfilter)
{ {
grub_disk_dev_register (&grub_diskfilter_dev); grub_disk_dev_register (&grub_diskfilter_dev);
cmd = grub_register_command ("cryptocheck", grub_cmd_cryptocheck,
N_("[--quiet] DEVICE"),
N_("Check if a logical volume resides on encrypted disks."));
} }
GRUB_MOD_FINI(diskfilter) GRUB_MOD_FINI(diskfilter)
{ {
grub_disk_dev_unregister (&grub_diskfilter_dev); grub_disk_dev_unregister (&grub_diskfilter_dev);
if (cmd != NULL)
grub_unregister_command (cmd);
free_array (); free_array ();
} }

View file

@ -471,7 +471,7 @@ grub_biosdisk_rw (int cmd, grub_disk_t disk,
struct grub_biosdisk_dap *dap; struct grub_biosdisk_dap *dap;
dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR
+ (data->sectors + (GRUB_DISK_MAX_LBA_SECTORS
<< disk->log_sector_size)); << disk->log_sector_size));
dap->length = sizeof (*dap); dap->length = sizeof (*dap);
dap->reserved = 0; dap->reserved = 0;
@ -561,6 +561,9 @@ get_safe_sectors (grub_disk_t disk, grub_disk_addr_t sector)
struct grub_biosdisk_data *data = disk->data; struct grub_biosdisk_data *data = disk->data;
grub_uint32_t sectors = data->sectors; grub_uint32_t sectors = data->sectors;
if (data->flags & GRUB_BIOSDISK_FLAG_LBA)
sectors = GRUB_DISK_MAX_LBA_SECTORS;
/* OFFSET = SECTOR % SECTORS */ /* OFFSET = SECTOR % SECTORS */
grub_divmod64 (sector, sectors, &offset); grub_divmod64 (sector, sectors, &offset);

View file

@ -26,6 +26,7 @@
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/scsicmd.h> #include <grub/scsicmd.h>
#include <grub/time.h> #include <grub/time.h>
#include <grub/safemath.h>
#include <grub/ieee1275/ieee1275.h> #include <grub/ieee1275/ieee1275.h>
#include <grub/ieee1275/obdisk.h> #include <grub/ieee1275/obdisk.h>
@ -128,9 +129,17 @@ count_commas (const char *src)
static char * static char *
decode_grub_devname (const char *name) decode_grub_devname (const char *name)
{ {
char *devpath = grub_malloc (grub_strlen (name) + 1); char *devpath;
char *p, c; char *p, c;
grub_size_t sz;
if (grub_add (grub_strlen (name), 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device name"));
return NULL;
}
devpath = grub_malloc (sz);
if (devpath == NULL) if (devpath == NULL)
return NULL; return NULL;
@ -156,12 +165,20 @@ static char *
encode_grub_devname (const char *path) encode_grub_devname (const char *path)
{ {
char *encoding, *optr; char *encoding, *optr;
grub_size_t sz;
if (path == NULL) if (path == NULL)
return NULL; return NULL;
encoding = grub_malloc (sizeof (IEEE1275_DEV) + count_commas (path) + if (grub_add (sizeof (IEEE1275_DEV) + 1, count_commas (path), &sz) ||
grub_strlen (path) + 1); grub_add (sz, grub_strlen (path), &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining encoding size"));
grub_print_error ();
return NULL;
}
encoding = grub_malloc (sz);
if (encoding == NULL) if (encoding == NULL)
{ {
@ -396,8 +413,22 @@ canonicalise_disk (const char *devname)
real_unit_str_len = grub_strlen (op->name) + sizeof (IEEE1275_DISK_ALIAS) real_unit_str_len = grub_strlen (op->name) + sizeof (IEEE1275_DISK_ALIAS)
+ grub_strlen (real_unit_address); + grub_strlen (real_unit_address);
if (grub_add (grub_strlen (op->name), sizeof (IEEE1275_DISK_ALIAS), &real_unit_str_len) ||
grub_add (real_unit_str_len, grub_strlen (real_unit_address), &real_unit_str_len))
{
grub_free (parent);
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of canonical name"));
grub_print_error ();
return NULL;
}
real_canon = grub_malloc (real_unit_str_len); real_canon = grub_malloc (real_unit_str_len);
if (real_canon == NULL)
{
grub_free (parent);
grub_print_error ();
return NULL;
}
grub_snprintf (real_canon, real_unit_str_len, "%s/disk@%s", grub_snprintf (real_canon, real_unit_str_len, "%s/disk@%s",
op->name, real_unit_address); op->name, real_unit_address);
@ -413,6 +444,7 @@ canonicalise_disk (const char *devname)
static struct disk_dev * static struct disk_dev *
add_canon_disk (const char *cname) add_canon_disk (const char *cname)
{ {
grub_size_t sz;
struct disk_dev *dev; struct disk_dev *dev;
dev = grub_zalloc (sizeof (struct disk_dev)); dev = grub_zalloc (sizeof (struct disk_dev));
@ -428,13 +460,18 @@ add_canon_disk (const char *cname)
* arguments and allows a client program to open * arguments and allows a client program to open
* the entire (raw) disk. Any disk label is ignored. * the entire (raw) disk. Any disk label is ignored.
*/ */
dev->raw_name = grub_malloc (grub_strlen (cname) + sizeof (":nolabel")); if (grub_add (grub_strlen (cname), sizeof (":nolabel"), &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while appending :nolabel to end of canonical name");
goto failed;
}
dev->raw_name = grub_malloc (sz);
if (dev->raw_name == NULL) if (dev->raw_name == NULL)
goto failed; goto failed;
grub_snprintf (dev->raw_name, grub_strlen (cname) + sizeof (":nolabel"), grub_snprintf (dev->raw_name, sz, "%s:nolabel", cname);
"%s:nolabel", cname);
} }
/* /*

View file

@ -24,6 +24,7 @@
#include <grub/ieee1275/ofdisk.h> #include <grub/ieee1275/ofdisk.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/time.h> #include <grub/time.h>
#include <grub/safemath.h>
static char *last_devpath; static char *last_devpath;
static grub_ieee1275_ihandle_t last_ihandle; static grub_ieee1275_ihandle_t last_ihandle;
@ -80,6 +81,7 @@ ofdisk_hash_add_real (char *devpath)
struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)]; struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)];
const char *iptr; const char *iptr;
char *optr; char *optr;
grub_size_t sz;
p = grub_zalloc (sizeof (*p)); p = grub_zalloc (sizeof (*p));
if (!p) if (!p)
@ -87,8 +89,14 @@ ofdisk_hash_add_real (char *devpath)
p->devpath = devpath; p->devpath = devpath;
p->grub_devpath = grub_malloc (sizeof ("ieee1275/") if (grub_mul (grub_strlen (p->devpath), 2, &sz) ||
+ 2 * grub_strlen (p->devpath)); grub_add (sz, sizeof ("ieee1275/"), &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path"));
return NULL;
}
p->grub_devpath = grub_malloc (sz);
if (!p->grub_devpath) if (!p->grub_devpath)
{ {
@ -98,7 +106,13 @@ ofdisk_hash_add_real (char *devpath)
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0)) if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0))
{ {
p->open_path = grub_malloc (grub_strlen (p->devpath) + 3); if (grub_add (grub_strlen (p->devpath), 3, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of an open path"));
return NULL;
}
p->open_path = grub_malloc (sz);
if (!p->open_path) if (!p->open_path)
{ {
grub_free (p->grub_devpath); grub_free (p->grub_devpath);
@ -224,6 +238,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
args; args;
char *buf, *bufptr; char *buf, *bufptr;
unsigned i; unsigned i;
grub_size_t sz;
if (grub_ieee1275_open (alias->path, &ihandle)) if (grub_ieee1275_open (alias->path, &ihandle))
return; return;
@ -243,9 +258,19 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
return; return;
} }
buf = grub_malloc (grub_strlen (alias->path) + 32); if (grub_add (grub_strlen (alias->path), 32, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for vscsi");
grub_ieee1275_close (ihandle);
return;
}
buf = grub_malloc (sz);
if (!buf) if (!buf)
return; {
grub_ieee1275_close (ihandle);
return;
}
bufptr = grub_stpcpy (buf, alias->path); bufptr = grub_stpcpy (buf, alias->path);
for (i = 0; i < args.nentries; i++) for (i = 0; i < args.nentries; i++)
@ -287,9 +312,15 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
grub_uint64_t *table; grub_uint64_t *table;
grub_uint16_t table_size; grub_uint16_t table_size;
grub_ieee1275_ihandle_t ihandle; grub_ieee1275_ihandle_t ihandle;
grub_size_t sz;
buf = grub_malloc (grub_strlen (alias->path) + if (grub_add (grub_strlen (alias->path), sizeof ("/disk@7766554433221100"), &sz))
sizeof ("/disk@7766554433221100")); {
grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for sas_ioa");
return;
}
buf = grub_malloc (sz);
if (!buf) if (!buf)
return; return;
bufptr = grub_stpcpy (buf, alias->path); bufptr = grub_stpcpy (buf, alias->path);
@ -427,9 +458,17 @@ grub_ofdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
static char * static char *
compute_dev_path (const char *name) compute_dev_path (const char *name)
{ {
char *devpath = grub_malloc (grub_strlen (name) + 3); char *devpath;
char *p, c; char *p, c;
grub_size_t sz;
if (grub_add (grub_strlen (name), 3, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path"));
return NULL;
}
devpath = grub_malloc (sz);
if (!devpath) if (!devpath)
return NULL; return NULL;
@ -625,6 +664,7 @@ insert_bootpath (void)
char *bootpath; char *bootpath;
grub_ssize_t bootpath_size; grub_ssize_t bootpath_size;
char *type; char *type;
grub_size_t sz;
if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath", if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath",
&bootpath_size) &bootpath_size)
@ -635,7 +675,13 @@ insert_bootpath (void)
return; return;
} }
bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64); if (grub_add (bootpath_size, 64, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining bootpath size"));
return;
}
bootpath = (char *) grub_malloc (sz);
if (! bootpath) if (! bootpath)
{ {
grub_print_error (); grub_print_error ();

View file

@ -0,0 +1,73 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2022 Microsoft Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/list.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/key_protector.h>
GRUB_MOD_LICENSE ("GPLv3+");
struct grub_key_protector *grub_key_protectors = NULL;
grub_err_t
grub_key_protector_register (struct grub_key_protector *protector)
{
if (protector == NULL || protector->name == NULL || protector->name[0] == '\0')
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector for registration");
if (grub_key_protectors != NULL &&
grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), protector->name) != NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Key protector '%s' already registered", protector->name);
grub_list_push (GRUB_AS_LIST_P (&grub_key_protectors), GRUB_AS_LIST (protector));
return GRUB_ERR_NONE;
}
grub_err_t
grub_key_protector_unregister (struct grub_key_protector *protector)
{
if (protector == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector for unregistration");
grub_list_remove (GRUB_AS_LIST (protector));
return GRUB_ERR_NONE;
}
grub_err_t
grub_key_protector_recover_key (const char *protector, grub_uint8_t **key,
grub_size_t *key_size)
{
struct grub_key_protector *kp = NULL;
if (grub_key_protectors == NULL)
return grub_error (GRUB_ERR_OUT_OF_RANGE, "No key protector registered");
if (protector == NULL || protector[0] == '\0')
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid key protector");
kp = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_key_protectors), protector);
if (kp == NULL)
return grub_error (GRUB_ERR_OUT_OF_RANGE, "Key protector '%s' not found", protector);
return kp->recover_key (key, key_size);
}

View file

@ -179,6 +179,36 @@ gpt_ldm_sector (grub_disk_t dsk)
return sector; return sector;
} }
static void
free_pv (struct grub_diskfilter_pv *pv)
{
if (pv == NULL)
return;
grub_free (pv->internal_id);
grub_free (pv->id.uuid);
grub_free (pv);
}
static void
free_lv (struct grub_diskfilter_lv *lv)
{
if (lv == NULL)
return;
grub_free (lv->internal_id);
grub_free (lv->name);
grub_free (lv->fullname);
if (lv->segments)
{
unsigned int i;
for (i = 0; i < lv->segment_count; i++)
grub_free (lv->segments[i].nodes);
grub_free (lv->segments);
}
grub_free (lv);
}
static struct grub_diskfilter_vg * static struct grub_diskfilter_vg *
make_vg (grub_disk_t disk, make_vg (grub_disk_t disk,
const struct grub_ldm_label *label) const struct grub_ldm_label *label)
@ -196,12 +226,8 @@ make_vg (grub_disk_t disk,
vg->name = grub_malloc (LDM_NAME_STRLEN + 1); vg->name = grub_malloc (LDM_NAME_STRLEN + 1);
vg->uuid = grub_malloc (LDM_GUID_STRLEN + 1); vg->uuid = grub_malloc (LDM_GUID_STRLEN + 1);
if (! vg->uuid || !vg->name) if (! vg->uuid || !vg->name)
{ goto fail1;
grub_free (vg->uuid);
grub_free (vg->name);
grub_free (vg);
return NULL;
}
grub_memcpy (vg->uuid, label->group_guid, LDM_GUID_STRLEN); grub_memcpy (vg->uuid, label->group_guid, LDM_GUID_STRLEN);
grub_memcpy (vg->name, label->group_name, LDM_NAME_STRLEN); grub_memcpy (vg->name, label->group_name, LDM_NAME_STRLEN);
vg->name[LDM_NAME_STRLEN] = 0; vg->name[LDM_NAME_STRLEN] = 0;
@ -220,6 +246,7 @@ make_vg (grub_disk_t disk,
struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE
/ sizeof (struct grub_ldm_vblk)]; / sizeof (struct grub_ldm_vblk)];
unsigned i; unsigned i;
grub_size_t sz;
err = grub_disk_read (disk, cursec, 0, err = grub_disk_read (disk, cursec, 0,
sizeof(vblk), &vblk); sizeof(vblk), &vblk);
if (err) if (err)
@ -251,12 +278,18 @@ make_vg (grub_disk_t disk,
grub_free (pv); grub_free (pv);
goto fail2; goto fail2;
} }
pv->internal_id = grub_malloc (ptr[0] + 2); if (grub_add (ptr[0], 2, &sz))
if (!pv->internal_id)
{ {
grub_free (pv); grub_free (pv);
goto fail2; goto fail2;
} }
pv->internal_id = grub_malloc (sz);
if (!pv->internal_id)
{
free_pv (pv);
goto fail2;
}
grub_memcpy (pv->internal_id, ptr, (grub_size_t) ptr[0] + 1); grub_memcpy (pv->internal_id, ptr, (grub_size_t) ptr[0] + 1);
pv->internal_id[(grub_size_t) ptr[0] + 1] = 0; pv->internal_id[(grub_size_t) ptr[0] + 1] = 0;
@ -264,7 +297,7 @@ make_vg (grub_disk_t disk,
if (ptr + *ptr + 1 >= vblk[i].dynamic if (ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (pv); free_pv (pv);
goto fail2; goto fail2;
} }
/* ptr = name. */ /* ptr = name. */
@ -272,11 +305,23 @@ make_vg (grub_disk_t disk,
if (ptr + *ptr + 1 if (ptr + *ptr + 1
>= vblk[i].dynamic + sizeof (vblk[i].dynamic)) >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (pv); free_pv (pv);
goto fail2; goto fail2;
} }
pv->id.uuidlen = *ptr; pv->id.uuidlen = *ptr;
pv->id.uuid = grub_malloc (pv->id.uuidlen + 1);
if (grub_add (pv->id.uuidlen, 1, &sz))
{
free_pv (pv);
goto fail2;
}
pv->id.uuid = grub_malloc (sz);
if (pv->id.uuid == NULL)
{
free_pv (pv);
goto fail2;
}
grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen); grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen);
pv->id.uuid[pv->id.uuidlen] = 0; pv->id.uuid[pv->id.uuidlen] = 0;
@ -322,7 +367,7 @@ make_vg (grub_disk_t disk,
lv->segments = grub_zalloc (sizeof (*lv->segments)); lv->segments = grub_zalloc (sizeof (*lv->segments));
if (!lv->segments) if (!lv->segments)
{ {
grub_free (lv); free_lv (lv);
goto fail2; goto fail2;
} }
lv->segments->start_extent = 0; lv->segments->start_extent = 0;
@ -333,20 +378,25 @@ make_vg (grub_disk_t disk,
sizeof (*lv->segments->nodes)); sizeof (*lv->segments->nodes));
if (!lv->segments->nodes) if (!lv->segments->nodes)
{ {
grub_free (lv); free_lv (lv);
goto fail2; goto fail2;
} }
ptr = vblk[i].dynamic; ptr = vblk[i].dynamic;
if (ptr + *ptr + 1 >= vblk[i].dynamic if (ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (lv); free_lv (lv);
goto fail2; goto fail2;
} }
lv->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2); if (grub_add (ptr[0], 2, &sz))
{
free_lv (lv);
goto fail2;
}
lv->internal_id = grub_malloc (sz);
if (!lv->internal_id) if (!lv->internal_id)
{ {
grub_free (lv); free_lv (lv);
goto fail2; goto fail2;
} }
grub_memcpy (lv->internal_id, ptr, ptr[0] + 1); grub_memcpy (lv->internal_id, ptr, ptr[0] + 1);
@ -356,20 +406,18 @@ make_vg (grub_disk_t disk,
if (ptr + *ptr + 1 >= vblk[i].dynamic if (ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (lv); free_lv (lv);
goto fail2; goto fail2;
} }
if (grub_add (*ptr, 1, &sz)) if (grub_add (*ptr, 1, &sz))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv);
goto fail2; goto fail2;
} }
lv->name = grub_malloc (sz); lv->name = grub_malloc (sz);
if (!lv->name) if (!lv->name)
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv);
goto fail2; goto fail2;
} }
grub_memcpy (lv->name, ptr + 1, *ptr); grub_memcpy (lv->name, ptr + 1, *ptr);
@ -378,36 +426,28 @@ make_vg (grub_disk_t disk,
vg->uuid, lv->name); vg->uuid, lv->name);
if (!lv->fullname) if (!lv->fullname)
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr + *ptr + 1 if (ptr + *ptr + 1
>= vblk[i].dynamic + sizeof (vblk[i].dynamic)) >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
/* ptr = volume type. */ /* ptr = volume type. */
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
/* ptr = flags. */ /* ptr = flags. */
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
@ -416,17 +456,13 @@ make_vg (grub_disk_t disk,
/* ptr = number of children. */ /* ptr = number of children. */
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
@ -436,9 +472,7 @@ make_vg (grub_disk_t disk,
|| ptr + *ptr + 1>= vblk[i].dynamic || ptr + *ptr + 1>= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (lv->internal_id); free_lv (lv);
grub_free (lv->name);
grub_free (lv);
goto fail2; goto fail2;
} }
lv->size = read_int (ptr + 1, *ptr); lv->size = read_int (ptr + 1, *ptr);
@ -455,6 +489,7 @@ make_vg (grub_disk_t disk,
struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE
/ sizeof (struct grub_ldm_vblk)]; / sizeof (struct grub_ldm_vblk)];
unsigned i; unsigned i;
grub_size_t sz;
err = grub_disk_read (disk, cursec, 0, err = grub_disk_read (disk, cursec, 0,
sizeof(vblk), &vblk); sizeof(vblk), &vblk);
if (err) if (err)
@ -487,13 +522,18 @@ make_vg (grub_disk_t disk,
ptr = vblk[i].dynamic; ptr = vblk[i].dynamic;
if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (comp); free_lv (comp);
goto fail2; goto fail2;
} }
comp->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2); if (grub_add (ptr[0], 2, &sz))
{
free_lv (comp);
goto fail2;
}
comp->internal_id = grub_malloc (sz);
if (!comp->internal_id) if (!comp->internal_id)
{ {
grub_free (comp); free_lv (comp);
goto fail2; goto fail2;
} }
grub_memcpy (comp->internal_id, ptr, ptr[0] + 1); grub_memcpy (comp->internal_id, ptr, ptr[0] + 1);
@ -502,16 +542,14 @@ make_vg (grub_disk_t disk,
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
/* ptr = name. */ /* ptr = name. */
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
/* ptr = state. */ /* ptr = state. */
@ -521,8 +559,7 @@ make_vg (grub_disk_t disk,
ptr += 4; ptr += 4;
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
@ -530,16 +567,14 @@ make_vg (grub_disk_t disk,
ptr += *ptr + 1; ptr += *ptr + 1;
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
ptr += 8 + 8; ptr += 8 + 8;
if (ptr + *ptr + 1 >= vblk[i].dynamic if (ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
for (lv = vg->lvs; lv; lv = lv->next) for (lv = vg->lvs; lv; lv = lv->next)
@ -550,8 +585,7 @@ make_vg (grub_disk_t disk,
} }
if (!lv) if (!lv)
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
continue; continue;
} }
comp->size = lv->size; comp->size = lv->size;
@ -563,8 +597,7 @@ make_vg (grub_disk_t disk,
sizeof (*comp->segments)); sizeof (*comp->segments));
if (!comp->segments) if (!comp->segments)
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
} }
@ -575,8 +608,7 @@ make_vg (grub_disk_t disk,
comp->segments = grub_malloc (sizeof (*comp->segments)); comp->segments = grub_malloc (sizeof (*comp->segments));
if (!comp->segments) if (!comp->segments)
{ {
grub_free (comp->internal_id); free_lv (comp);
grub_free (comp);
goto fail2; goto fail2;
} }
comp->segments->start_extent = 0; comp->segments->start_extent = 0;
@ -591,27 +623,21 @@ make_vg (grub_disk_t disk,
} }
else else
{ {
grub_free (comp->segments); free_lv (comp);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
ptr += *ptr + 1; ptr += *ptr + 1;
ptr++; ptr++;
if (!(vblk[i].flags & 0x10)) if (!(vblk[i].flags & 0x10))
{ {
grub_free (comp->segments); free_lv (comp);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic) if (ptr >= vblk[i].dynamic + sizeof (vblk[i].dynamic)
|| ptr + *ptr + 1 >= vblk[i].dynamic || ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->segments); free_lv (comp);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
comp->segments->stripe_size = read_int (ptr + 1, *ptr); comp->segments->stripe_size = read_int (ptr + 1, *ptr);
@ -619,20 +645,16 @@ make_vg (grub_disk_t disk,
if (ptr + *ptr + 1 >= vblk[i].dynamic if (ptr + *ptr + 1 >= vblk[i].dynamic
+ sizeof (vblk[i].dynamic)) + sizeof (vblk[i].dynamic))
{ {
grub_free (comp->segments); free_lv (comp);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
comp->segments->node_count = read_int (ptr + 1, *ptr); comp->segments->node_count = read_int (ptr + 1, *ptr);
comp->segments->node_alloc = comp->segments->node_count; comp->segments->node_alloc = comp->segments->node_count;
comp->segments->nodes = grub_calloc (comp->segments->node_alloc, comp->segments->nodes = grub_calloc (comp->segments->node_alloc,
sizeof (*comp->segments->nodes)); sizeof (*comp->segments->nodes));
if (!lv->segments->nodes) if (comp->segments->nodes == NULL)
{ {
grub_free (comp->segments); free_lv (comp);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
} }
@ -640,25 +662,18 @@ make_vg (grub_disk_t disk,
if (lv->segments->node_alloc == lv->segments->node_count) if (lv->segments->node_alloc == lv->segments->node_count)
{ {
void *t; void *t;
grub_size_t sz;
if (grub_mul (lv->segments->node_alloc, 2, &lv->segments->node_alloc) || if (grub_mul (lv->segments->node_alloc, 2, &lv->segments->node_alloc) ||
grub_mul (lv->segments->node_alloc, sizeof (*lv->segments->nodes), &sz)) grub_mul (lv->segments->node_alloc, sizeof (*lv->segments->nodes), &sz))
{ {
grub_free (comp->segments->nodes); free_lv (comp);
grub_free (comp->segments);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
t = grub_realloc (lv->segments->nodes, sz); t = grub_realloc (lv->segments->nodes, sz);
if (!t) if (!t)
{ {
grub_free (comp->segments->nodes); free_lv (comp);
grub_free (comp->segments);
grub_free (comp->internal_id);
grub_free (comp);
goto fail2; goto fail2;
} }
lv->segments->nodes = t; lv->segments->nodes = t;
@ -798,7 +813,10 @@ make_vg (grub_disk_t disk,
comp->segments[comp->segment_count].nodes comp->segments[comp->segment_count].nodes
= grub_malloc (sizeof (*comp->segments[comp->segment_count].nodes)); = grub_malloc (sizeof (*comp->segments[comp->segment_count].nodes));
if (!comp->segments[comp->segment_count].nodes) if (!comp->segments[comp->segment_count].nodes)
goto fail2; {
grub_free (comp->segments);
goto fail2;
}
comp->segments[comp->segment_count].nodes[0] = part; comp->segments[comp->segment_count].nodes[0] = part;
comp->segment_count++; comp->segment_count++;
} }
@ -813,25 +831,19 @@ make_vg (grub_disk_t disk,
struct grub_diskfilter_pv *pv, *next_pv; struct grub_diskfilter_pv *pv, *next_pv;
for (lv = vg->lvs; lv; lv = next_lv) for (lv = vg->lvs; lv; lv = next_lv)
{ {
unsigned i;
for (i = 0; i < lv->segment_count; i++)
grub_free (lv->segments[i].nodes);
next_lv = lv->next; next_lv = lv->next;
grub_free (lv->segments); free_lv (lv);
grub_free (lv->internal_id);
grub_free (lv->name);
grub_free (lv->fullname);
grub_free (lv);
} }
for (pv = vg->pvs; pv; pv = next_pv) for (pv = vg->pvs; pv; pv = next_pv)
{ {
next_pv = pv->next; next_pv = pv->next;
grub_free (pv->id.uuid); free_pv (pv);
grub_free (pv);
} }
} }
fail1:
grub_free (vg->uuid); grub_free (vg->uuid);
grub_free (vg->name);
grub_free (vg); grub_free (vg);
return NULL; return NULL;
} }

View file

@ -24,6 +24,7 @@
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -33,6 +34,7 @@ struct grub_loopback
grub_file_t file; grub_file_t file;
struct grub_loopback *next; struct grub_loopback *next;
unsigned long id; unsigned long id;
grub_uint64_t refcnt;
}; };
static struct grub_loopback *loopback_list; static struct grub_loopback *loopback_list;
@ -64,6 +66,8 @@ delete_loopback (const char *name)
if (! dev) if (! dev)
return grub_error (GRUB_ERR_BAD_DEVICE, "device not found"); return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
if (dev->refcnt > 0)
return grub_error (GRUB_ERR_STILL_REFERENCED, "device still referenced");
/* Remove the device from the list. */ /* Remove the device from the list. */
*prev = dev->next; *prev = dev->next;
@ -120,6 +124,7 @@ grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
newdev->file = file; newdev->file = file;
newdev->id = last_id++; newdev->id = last_id++;
newdev->refcnt = 0;
/* Add the new entry to the list. */ /* Add the new entry to the list. */
newdev->next = loopback_list; newdev->next = loopback_list;
@ -161,6 +166,9 @@ grub_loopback_open (const char *name, grub_disk_t disk)
if (! dev) if (! dev)
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
if (grub_add (dev->refcnt, 1, &dev->refcnt))
grub_fatal ("Reference count overflow");
/* Use the filesize for the disk size, round up to a complete sector. */ /* Use the filesize for the disk size, round up to a complete sector. */
if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN) if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1) disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
@ -178,6 +186,15 @@ grub_loopback_open (const char *name, grub_disk_t disk)
return 0; return 0;
} }
static void
grub_loopback_close (grub_disk_t disk)
{
struct grub_loopback *dev = disk->data;
if (grub_sub (dev->refcnt, 1, &dev->refcnt))
grub_fatal ("Reference count underflow");
}
static grub_err_t static grub_err_t
grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector, grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
grub_size_t size, char *buf) grub_size_t size, char *buf)
@ -220,6 +237,7 @@ static struct grub_disk_dev grub_loopback_dev =
.id = GRUB_DISK_DEVICE_LOOPBACK_ID, .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
.disk_iterate = grub_loopback_iterate, .disk_iterate = grub_loopback_iterate,
.disk_open = grub_loopback_open, .disk_open = grub_loopback_open,
.disk_close = grub_loopback_close,
.disk_read = grub_loopback_read, .disk_read = grub_loopback_read,
.disk_write = grub_loopback_write, .disk_write = grub_loopback_write,
.next = 0 .next = 0

View file

@ -26,6 +26,7 @@
#include <grub/crypto.h> #include <grub/crypto.h>
#include <grub/partition.h> #include <grub/partition.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/safemath.h>
#include <base64.h> #include <base64.h>
#include <json.h> #include <json.h>
@ -569,6 +570,7 @@ luks2_recover_key (grub_disk_t source,
gcry_err_code_t gcry_ret; gcry_err_code_t gcry_ret;
grub_json_t *json = NULL, keyslots; grub_json_t *json = NULL, keyslots;
grub_err_t ret; grub_err_t ret;
grub_size_t sz;
if (cargs->key_data == NULL || cargs->key_len == 0) if (cargs->key_data == NULL || cargs->key_len == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no key data"); return grub_error (GRUB_ERR_BAD_ARGUMENT, "no key data");
@ -577,7 +579,12 @@ luks2_recover_key (grub_disk_t source,
if (ret) if (ret)
return ret; return ret;
json_header = grub_zalloc (grub_be_to_cpu64 (header.hdr_size) - sizeof (header)); grub_puts_ (N_("Attempting to decrypt master key..."));
if (grub_sub (grub_be_to_cpu64 (header.hdr_size), sizeof (header), &sz))
return grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while calculating json header size");
json_header = grub_zalloc (sz);
if (!json_header) if (!json_header)
return GRUB_ERR_OUT_OF_MEMORY; return GRUB_ERR_OUT_OF_MEMORY;

View file

@ -34,12 +34,11 @@
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
struct cache_lv struct ignored_feature_lv
{ {
struct grub_diskfilter_lv *lv; struct grub_diskfilter_lv *lv;
char *cache_pool;
char *origin; char *origin;
struct cache_lv *next; struct ignored_feature_lv *next;
}; };
@ -105,30 +104,29 @@ grub_lvm_check_flag (const char *p, const char *str, const char *flag)
} }
static void static void
grub_lvm_free_cache_lvs (struct cache_lv *cache_lvs) grub_lvm_free_ignored_feature_lvs (struct ignored_feature_lv *ignored_feature_lvs)
{ {
struct cache_lv *cache; struct ignored_feature_lv *ignored_feature;
while ((cache = cache_lvs)) while ((ignored_feature = ignored_feature_lvs))
{ {
cache_lvs = cache_lvs->next; ignored_feature_lvs = ignored_feature_lvs->next;
if (cache->lv) if (ignored_feature->lv)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < cache->lv->segment_count; ++i) for (i = 0; i < ignored_feature->lv->segment_count; ++i)
if (cache->lv->segments) if (ignored_feature->lv->segments)
grub_free (cache->lv->segments[i].nodes); grub_free (ignored_feature->lv->segments[i].nodes);
grub_free (cache->lv->segments); grub_free (ignored_feature->lv->segments);
grub_free (cache->lv->fullname); grub_free (ignored_feature->lv->fullname);
grub_free (cache->lv->idname); grub_free (ignored_feature->lv->idname);
grub_free (cache->lv->name); grub_free (ignored_feature->lv->name);
} }
grub_free (cache->lv); grub_free (ignored_feature->lv);
grub_free (cache->origin); grub_free (ignored_feature->origin);
grub_free (cache->cache_pool); grub_free (ignored_feature);
grub_free (cache);
} }
} }
@ -290,7 +288,7 @@ grub_lvm_detect (grub_disk_t disk,
p = q = (char *)ptr; p = q = (char *)ptr;
if (grub_add ((grub_size_t)metadatabuf, (grub_size_t)mda_size, &ptr)) if (grub_add (ptr, (grub_size_t) grub_le_to_cpu64 (rlocn->size), &ptr))
goto error_parsing_metadata; goto error_parsing_metadata;
mda_end = (char *)ptr; mda_end = (char *)ptr;
@ -325,7 +323,7 @@ grub_lvm_detect (grub_disk_t disk,
if (! vg) if (! vg)
{ {
struct cache_lv *cache_lvs = NULL; struct ignored_feature_lv *ignored_feature_lvs = NULL;
/* First time we see this volume group. We've to create the /* First time we see this volume group. We've to create the
whole volume group structure. */ whole volume group structure. */
@ -370,6 +368,8 @@ grub_lvm_detect (grub_disk_t disk,
break; break;
pv = grub_zalloc (sizeof (*pv)); pv = grub_zalloc (sizeof (*pv));
if (pv == NULL)
goto fail4;
q = p; q = p;
while (*q != ' ' && q < mda_end) while (*q != ' ' && q < mda_end)
q++; q++;
@ -379,6 +379,8 @@ grub_lvm_detect (grub_disk_t disk,
s = q - p; s = q - p;
pv->name = grub_malloc (s + 1); pv->name = grub_malloc (s + 1);
if (pv->name == NULL)
goto pvs_fail_noname;
grub_memcpy (pv->name, p, s); grub_memcpy (pv->name, p, s);
pv->name[s] = '\0'; pv->name[s] = '\0';
@ -451,6 +453,8 @@ grub_lvm_detect (grub_disk_t disk,
break; break;
lv = grub_zalloc (sizeof (*lv)); lv = grub_zalloc (sizeof (*lv));
if (lv == NULL)
goto fail4;
q = p; q = p;
while (*q != ' ' && q < mda_end) while (*q != ' ' && q < mda_end)
@ -545,6 +549,8 @@ grub_lvm_detect (grub_disk_t disk,
goto lvs_fail; goto lvs_fail;
} }
lv->segments = grub_calloc (lv->segment_count, sizeof (*seg)); lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
if (lv->segments == NULL)
goto lvs_fail;
seg = lv->segments; seg = lv->segments;
for (i = 0; i < lv->segment_count; i++) for (i = 0; i < lv->segment_count; i++)
@ -612,6 +618,8 @@ grub_lvm_detect (grub_disk_t disk,
seg->nodes = grub_calloc (seg->node_count, seg->nodes = grub_calloc (seg->node_count,
sizeof (*stripe)); sizeof (*stripe));
if (seg->nodes == NULL)
goto lvs_segment_fail;
stripe = seg->nodes; stripe = seg->nodes;
p = grub_strstr (p, "stripes = ["); p = grub_strstr (p, "stripes = [");
@ -671,8 +679,9 @@ grub_lvm_detect (grub_disk_t disk,
goto lvs_segment_fail; goto lvs_segment_fail;
} }
seg->nodes = grub_zalloc (sizeof (seg->nodes[0]) seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
* seg->node_count); if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "mirrors = ["); p = grub_strstr (p, "mirrors = [");
if (p == NULL) if (p == NULL)
@ -760,8 +769,9 @@ grub_lvm_detect (grub_disk_t disk,
} }
} }
seg->nodes = grub_zalloc (sizeof (seg->nodes[0]) seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
* seg->node_count); if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "raids = ["); p = grub_strstr (p, "raids = [");
if (p == NULL) if (p == NULL)
@ -807,108 +817,98 @@ grub_lvm_detect (grub_disk_t disk,
seg->nodes[seg->node_count - 1].name = tmp; seg->nodes[seg->node_count - 1].name = tmp;
} }
} }
else if (grub_memcmp (p, "cache\"", /*
sizeof ("cache\"") - 1) == 0) * Cache and integrity LVs have extra parts that
* we can ignore for our read-only access.
*/
else if (grub_strncmp (p, "cache\"", sizeof ("cache\"") - 1) == 0 ||
grub_strncmp (p, "cache+CACHE_USES_CACHEVOL\"", sizeof ("cache+CACHE_USES_CACHEVOL\"") - 1) == 0 ||
grub_strncmp (p, "integrity\"", sizeof ("integrity\"") - 1) == 0)
{ {
struct cache_lv *cache = NULL; struct ignored_feature_lv *ignored_feature = NULL;
char *p2, *p3; char *p2, *p3;
grub_size_t sz; grub_size_t sz;
#ifdef GRUB_UTIL
p2 = grub_strchr (p, '"');
if (p2)
*p2 = '\0';
grub_util_info ("Ignoring extra metadata type '%s' for %s", p, lv->name);
if (p2)
*p2 ='"';
#endif
cache = grub_zalloc (sizeof (*cache)); ignored_feature = grub_zalloc (sizeof (*ignored_feature));
if (!cache) if (!ignored_feature)
goto cache_lv_fail; goto ignored_feature_lv_fail;
cache->lv = grub_zalloc (sizeof (*cache->lv)); ignored_feature->lv = grub_zalloc (sizeof (*ignored_feature->lv));
if (!cache->lv) if (!ignored_feature->lv)
goto cache_lv_fail; goto ignored_feature_lv_fail;
grub_memcpy (cache->lv, lv, sizeof (*cache->lv)); grub_memcpy (ignored_feature->lv, lv, sizeof (*ignored_feature->lv));
if (lv->fullname) if (lv->fullname)
{ {
cache->lv->fullname = grub_strdup (lv->fullname); ignored_feature->lv->fullname = grub_strdup (lv->fullname);
if (!cache->lv->fullname) if (!ignored_feature->lv->fullname)
goto cache_lv_fail; goto ignored_feature_lv_fail;
} }
if (lv->idname) if (lv->idname)
{ {
cache->lv->idname = grub_strdup (lv->idname); ignored_feature->lv->idname = grub_strdup (lv->idname);
if (!cache->lv->idname) if (!ignored_feature->lv->idname)
goto cache_lv_fail; goto ignored_feature_lv_fail;
} }
if (lv->name) if (lv->name)
{ {
cache->lv->name = grub_strdup (lv->name); ignored_feature->lv->name = grub_strdup (lv->name);
if (!cache->lv->name) if (!ignored_feature->lv->name)
goto cache_lv_fail; goto ignored_feature_lv_fail;
} }
skip_lv = 1; skip_lv = 1;
p2 = grub_strstr (p, "cache_pool = \"");
if (!p2)
goto cache_lv_fail;
p2 = grub_strchr (p2, '"');
if (!p2)
goto cache_lv_fail;
p3 = ++p2;
if (p3 == mda_end)
goto cache_lv_fail;
p3 = grub_strchr (p3, '"');
if (!p3)
goto cache_lv_fail;
sz = p3 - p2;
cache->cache_pool = grub_malloc (sz + 1);
if (!cache->cache_pool)
goto cache_lv_fail;
grub_memcpy (cache->cache_pool, p2, sz);
cache->cache_pool[sz] = '\0';
p2 = grub_strstr (p, "origin = \""); p2 = grub_strstr (p, "origin = \"");
if (!p2) if (!p2)
goto cache_lv_fail; goto ignored_feature_lv_fail;
p2 = grub_strchr (p2, '"'); p2 = grub_strchr (p2, '"');
if (!p2) if (!p2)
goto cache_lv_fail; goto ignored_feature_lv_fail;
p3 = ++p2; p3 = ++p2;
if (p3 == mda_end) if (p3 == mda_end)
goto cache_lv_fail; goto ignored_feature_lv_fail;
p3 = grub_strchr (p3, '"'); p3 = grub_strchr (p3, '"');
if (!p3) if (!p3)
goto cache_lv_fail; goto ignored_feature_lv_fail;
sz = p3 - p2; sz = p3 - p2;
cache->origin = grub_malloc (sz + 1); ignored_feature->origin = grub_malloc (sz + 1);
if (!cache->origin) if (!ignored_feature->origin)
goto cache_lv_fail; goto ignored_feature_lv_fail;
grub_memcpy (cache->origin, p2, sz); grub_memcpy (ignored_feature->origin, p2, sz);
cache->origin[sz] = '\0'; ignored_feature->origin[sz] = '\0';
cache->next = cache_lvs; ignored_feature->next = ignored_feature_lvs;
cache_lvs = cache; ignored_feature_lvs = ignored_feature;
break; break;
cache_lv_fail: ignored_feature_lv_fail:
if (cache) if (ignored_feature)
{ {
grub_free (cache->origin); grub_free (ignored_feature->origin);
grub_free (cache->cache_pool); if (ignored_feature->lv)
if (cache->lv)
{ {
grub_free (cache->lv->fullname); grub_free (ignored_feature->lv->fullname);
grub_free (cache->lv->idname); grub_free (ignored_feature->lv->idname);
grub_free (cache->lv->name); grub_free (ignored_feature->lv->name);
} }
grub_free (cache->lv); grub_free (ignored_feature->lv);
grub_free (cache); grub_free (ignored_feature);
} }
grub_lvm_free_cache_lvs (cache_lvs); grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4; goto fail4;
} }
else else
@ -917,7 +917,7 @@ grub_lvm_detect (grub_disk_t disk,
char *p2; char *p2;
p2 = grub_strchr (p, '"'); p2 = grub_strchr (p, '"');
if (p2) if (p2)
*p2 = 0; *p2 = '\0';
grub_util_info ("unknown LVM type %s", p); grub_util_info ("unknown LVM type %s", p);
if (p2) if (p2)
*p2 ='"'; *p2 ='"';
@ -961,10 +961,68 @@ grub_lvm_detect (grub_disk_t disk,
} }
} }
/* Match lvs. */
{
struct ignored_feature_lv *ignored_feature;
for (ignored_feature = ignored_feature_lvs; ignored_feature; ignored_feature = ignored_feature->next)
{
struct grub_diskfilter_lv *lv;
for (lv = vg->lvs; lv; lv = lv->next)
if (grub_strcmp (lv->name, ignored_feature->origin) == 0)
break;
if (lv)
{
ignored_feature->lv->segments = grub_calloc (lv->segment_count, sizeof (*lv->segments));
if (!ignored_feature->lv->segments)
{
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4;
}
grub_memcpy (ignored_feature->lv->segments, lv->segments, lv->segment_count * sizeof (*lv->segments));
for (i = 0; i < lv->segment_count; ++i)
{
struct grub_diskfilter_node *nodes = lv->segments[i].nodes;
grub_size_t node_count = lv->segments[i].node_count;
ignored_feature->lv->segments[i].nodes = grub_calloc (node_count, sizeof (*nodes));
if (!ignored_feature->lv->segments[i].nodes)
{
for (j = 0; j < i; ++j)
grub_free (ignored_feature->lv->segments[j].nodes);
grub_free (ignored_feature->lv->segments);
ignored_feature->lv->segments = NULL;
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4;
}
grub_memcpy (ignored_feature->lv->segments[i].nodes, nodes, node_count * sizeof (*nodes));
}
if (ignored_feature->lv->segments)
{
ignored_feature->lv->segment_count = lv->segment_count;
ignored_feature->lv->vg = vg;
ignored_feature->lv->next = vg->lvs;
vg->lvs = ignored_feature->lv;
ignored_feature->lv = NULL;
}
}
else
{
#ifdef GRUB_UTIL
grub_util_info ("Couldn't find LVM part of ignored feature on %s", ignored_feature->origin);
#endif
}
}
}
/* Match LVs. Must be done after cache and integrity are found. */
{ {
struct grub_diskfilter_lv *lv1; struct grub_diskfilter_lv *lv1;
struct grub_diskfilter_lv *lv2; struct grub_diskfilter_lv *lv2;
for (lv1 = vg->lvs; lv1; lv1 = lv1->next) for (lv1 = vg->lvs; lv1; lv1 = lv1->next)
for (i = 0; i < lv1->segment_count; i++) for (i = 0; i < lv1->segment_count; i++)
for (j = 0; j < lv1->segments[i].node_count; j++) for (j = 0; j < lv1->segments[i].node_count; j++)
@ -989,60 +1047,9 @@ grub_lvm_detect (grub_disk_t disk,
lv1->segments[i].nodes[j].lv = lv2; lv1->segments[i].nodes[j].lv = lv2;
} }
} }
} }
{ grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
struct cache_lv *cache;
for (cache = cache_lvs; cache; cache = cache->next)
{
struct grub_diskfilter_lv *lv;
for (lv = vg->lvs; lv; lv = lv->next)
if (grub_strcmp (lv->name, cache->origin) == 0)
break;
if (lv)
{
cache->lv->segments = grub_calloc (lv->segment_count, sizeof (*lv->segments));
if (!cache->lv->segments)
{
grub_lvm_free_cache_lvs (cache_lvs);
goto fail4;
}
grub_memcpy (cache->lv->segments, lv->segments, lv->segment_count * sizeof (*lv->segments));
for (i = 0; i < lv->segment_count; ++i)
{
struct grub_diskfilter_node *nodes = lv->segments[i].nodes;
grub_size_t node_count = lv->segments[i].node_count;
cache->lv->segments[i].nodes = grub_calloc (node_count, sizeof (*nodes));
if (!cache->lv->segments[i].nodes)
{
for (j = 0; j < i; ++j)
grub_free (cache->lv->segments[j].nodes);
grub_free (cache->lv->segments);
cache->lv->segments = NULL;
grub_lvm_free_cache_lvs (cache_lvs);
goto fail4;
}
grub_memcpy (cache->lv->segments[i].nodes, nodes, node_count * sizeof (*nodes));
}
if (cache->lv->segments)
{
cache->lv->segment_count = lv->segment_count;
cache->lv->vg = vg;
cache->lv->next = vg->lvs;
vg->lvs = cache->lv;
cache->lv = NULL;
}
}
}
}
grub_lvm_free_cache_lvs (cache_lvs);
if (grub_diskfilter_vg_register (vg)) if (grub_diskfilter_vg_register (vg))
goto fail4; goto fail4;
} }

View file

@ -23,6 +23,7 @@
#include <grub/err.h> #include <grub/err.h>
#include <grub/misc.h> #include <grub/misc.h>
#include <grub/diskfilter.h> #include <grub/diskfilter.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -103,6 +104,9 @@ struct grub_raid_super_1x
#define WriteMostly1 1 /* Mask for writemostly flag in above devflags. */ #define WriteMostly1 1 /* Mask for writemostly flag in above devflags. */
#define GRUB_MD_SECTOR_SHIFT 9 /* Follow Linux kernel v6.8. */
#define GRUB_MD_SECTOR_SIZE (1 << GRUB_MD_SECTOR_SHIFT)
static struct grub_diskfilter_vg * static struct grub_diskfilter_vg *
grub_mdraid_detect (grub_disk_t disk, grub_mdraid_detect (grub_disk_t disk,
struct grub_diskfilter_pv_id *id, struct grub_diskfilter_pv_id *id,
@ -129,6 +133,7 @@ grub_mdraid_detect (grub_disk_t disk,
grub_uint32_t level; grub_uint32_t level;
struct grub_diskfilter_vg *array; struct grub_diskfilter_vg *array;
char *uuid; char *uuid;
grub_uint64_t sb_sz, data_end, sb_end;
if (size == GRUB_DISK_SIZE_UNKNOWN && minor_version == 0) if (size == GRUB_DISK_SIZE_UNKNOWN && minor_version == 0)
continue; continue;
@ -154,6 +159,79 @@ grub_mdraid_detect (grub_disk_t disk,
|| grub_le_to_cpu64 (sb.super_offset) != sector) || grub_le_to_cpu64 (sb.super_offset) != sector)
continue; continue;
/*
* The first check follows the Linux kernel's data_size
* validation from v6.8-rc5.
*/
if (grub_le_to_cpu64 (sb.data_size) < 10 ||
grub_le_to_cpu64 (sb.raid_disks) > GRUB_MDRAID_MAX_DISKS)
{
grub_dprintf ("mdraid1x", "Corrupted superblock\n");
return NULL;
}
/*
* Total size of superblock: 256 bytes plus 2 bytes per device
* in the array.
*/
sb_sz = sizeof (struct grub_raid_super_1x) + grub_le_to_cpu64 (sb.raid_disks) * 2;
if (grub_add (grub_le_to_cpu64 (sb.super_offset),
(ALIGN_UP(sb_sz, GRUB_MD_SECTOR_SIZE) >> GRUB_MD_SECTOR_SHIFT), &sb_end))
{
grub_dprintf ("mdraid1x", "Invalid superblock end.\n");
return NULL;
}
if (grub_add (grub_le_to_cpu64 (sb.data_offset),
grub_le_to_cpu64 (sb.data_size), &data_end))
{
grub_dprintf ("mdraid1x", "Invalid data end.\n");
return NULL;
}
/* In minor versions 1 and 2, superblock is positioned before data. */
if (minor_version)
{
if (grub_le_to_cpu64 (sb.data_offset) < sb_end)
{
grub_dprintf ("mdraid1x",
"The superblock either overlaps with the data "
"or is behind it.\n");
return NULL;
}
if (data_end > size)
{
grub_dprintf ("mdraid1x",
"The data region ends at %" PRIuGRUB_UINT64_T ", "
"past the end of the disk (%" PRIuGRUB_UINT64_T ")\n",
data_end, size);
return NULL;
}
}
else
{
/* In minor version 0, superblock is at the end of the device. */
if (grub_le_to_cpu64 (sb.super_offset) < data_end)
{
grub_dprintf ("mdraid1x",
"The data either overlaps with the superblock "
"or is behind it.\n");
return NULL;
}
if (sb_end > size)
{
grub_dprintf ("mdraid1x",
"The superblock region ends at "
"%" PRIuGRUB_UINT64_T ", past the end of "
"the disk (%" PRIuGRUB_UINT64_T ")\n",
sb_end, size);
return NULL;
}
}
if (sb.major_version != grub_cpu_to_le32_compile_time (1)) if (sb.major_version != grub_cpu_to_le32_compile_time (1))
/* Unsupported version. */ /* Unsupported version. */
return NULL; return NULL;

View file

@ -23,6 +23,7 @@
#include <grub/misc.h> #include <grub/misc.h>
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -96,8 +97,14 @@ GRUB_MOD_INIT(memdisk)
grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr); grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
memdisk_size = header->size - sizeof (struct grub_module_header); if (grub_sub (header->size, sizeof (struct grub_module_header), &memdisk_size))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while obtaining memdisk size");
return;
}
memdisk_addr = grub_malloc (memdisk_size); memdisk_addr = grub_malloc (memdisk_size);
if (memdisk_addr == NULL)
return;
grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n"); grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size); grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);

View file

@ -24,6 +24,7 @@
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/partition.h> #include <grub/partition.h>
#include <grub/file.h> #include <grub/file.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -126,7 +127,7 @@ plainmount_configure_password (grub_cryptodisk_t dev, const char *hash,
grub_uint8_t *derived_hash, *dh; grub_uint8_t *derived_hash, *dh;
char *p; char *p;
unsigned int round, i, len, size; unsigned int round, i, len, size;
grub_size_t alloc_size; grub_size_t alloc_size, sz;
grub_err_t err = GRUB_ERR_NONE; grub_err_t err = GRUB_ERR_NONE;
/* Support none (plain) hash */ /* Support none (plain) hash */
@ -145,7 +146,11 @@ plainmount_configure_password (grub_cryptodisk_t dev, const char *hash,
* Allocate buffer for the password and for an added prefix character * Allocate buffer for the password and for an added prefix character
* for each hash round ('alloc_size' may not be a multiple of 'len'). * for each hash round ('alloc_size' may not be a multiple of 'len').
*/ */
p = grub_zalloc (alloc_size + (alloc_size / len) + 1); if (grub_add (alloc_size, (alloc_size / len), &sz) ||
grub_add (sz, 1, &sz))
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while allocating size of password buffer"));
p = grub_zalloc (sz);
derived_hash = grub_zalloc (GRUB_CRYPTODISK_MAX_KEYLEN * 2); derived_hash = grub_zalloc (GRUB_CRYPTODISK_MAX_KEYLEN * 2);
if (p == NULL || derived_hash == NULL) if (p == NULL || derived_hash == NULL)
{ {

View file

@ -66,7 +66,7 @@ efiemu_convert_pointer (grub_efi_uintn_t debug_disposition,
grub_efi_status_t __grub_efi_api grub_efi_status_t __grub_efi_api
efiemu_get_variable (grub_efi_char16_t *variable_name, efiemu_get_variable (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid, const grub_packed_guid_t *vendor_guid,
grub_efi_uint32_t *attributes, grub_efi_uint32_t *attributes,
grub_efi_uintn_t *data_size, grub_efi_uintn_t *data_size,
void *data); void *data);
@ -74,11 +74,11 @@ efiemu_get_variable (grub_efi_char16_t *variable_name,
grub_efi_status_t __grub_efi_api grub_efi_status_t __grub_efi_api
efiemu_get_next_variable_name (grub_efi_uintn_t *variable_name_size, efiemu_get_next_variable_name (grub_efi_uintn_t *variable_name_size,
grub_efi_char16_t *variable_name, grub_efi_char16_t *variable_name,
grub_guid_t *vendor_guid); grub_packed_guid_t *vendor_guid);
grub_efi_status_t __grub_efi_api grub_efi_status_t __grub_efi_api
efiemu_set_variable (grub_efi_char16_t *variable_name, efiemu_set_variable (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid, const grub_packed_guid_t *vendor_guid,
grub_efi_uint32_t attributes, grub_efi_uint32_t attributes,
grub_efi_uintn_t data_size, grub_efi_uintn_t data_size,
void *data); void *data);
@ -416,7 +416,7 @@ EFI_FUNC (efiemu_convert_pointer) (grub_efi_uintn_t debug_disposition,
/* Find variable by name and GUID. */ /* Find variable by name and GUID. */
static struct efi_variable * static struct efi_variable *
find_variable (const grub_guid_t *vendor_guid, find_variable (const grub_packed_guid_t *vendor_guid,
grub_efi_char16_t *variable_name) grub_efi_char16_t *variable_name)
{ {
grub_uint8_t *ptr; grub_uint8_t *ptr;
@ -438,7 +438,7 @@ find_variable (const grub_guid_t *vendor_guid,
grub_efi_status_t __grub_efi_api grub_efi_status_t __grub_efi_api
EFI_FUNC (efiemu_get_variable) (grub_efi_char16_t *variable_name, EFI_FUNC (efiemu_get_variable) (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid, const grub_packed_guid_t *vendor_guid,
grub_efi_uint32_t *attributes, grub_efi_uint32_t *attributes,
grub_efi_uintn_t *data_size, grub_efi_uintn_t *data_size,
void *data) void *data)
@ -464,7 +464,7 @@ EFI_FUNC (efiemu_get_variable) (grub_efi_char16_t *variable_name,
grub_efi_status_t __grub_efi_api EFI_FUNC grub_efi_status_t __grub_efi_api EFI_FUNC
(efiemu_get_next_variable_name) (grub_efi_uintn_t *variable_name_size, (efiemu_get_next_variable_name) (grub_efi_uintn_t *variable_name_size,
grub_efi_char16_t *variable_name, grub_efi_char16_t *variable_name,
grub_guid_t *vendor_guid) grub_packed_guid_t *vendor_guid)
{ {
struct efi_variable *efivar; struct efi_variable *efivar;
LOG ('l'); LOG ('l');
@ -503,7 +503,7 @@ grub_efi_status_t __grub_efi_api EFI_FUNC
grub_efi_status_t __grub_efi_api grub_efi_status_t __grub_efi_api
EFI_FUNC (efiemu_set_variable) (grub_efi_char16_t *variable_name, EFI_FUNC (efiemu_set_variable) (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid, const grub_packed_guid_t *vendor_guid,
grub_efi_uint32_t attributes, grub_efi_uint32_t attributes,
grub_efi_uintn_t data_size, grub_efi_uintn_t data_size,
void *data) void *data)
@ -597,9 +597,30 @@ struct grub_efi_runtime_services efiemu_runtime_services =
.set_virtual_address_map = efiemu_set_virtual_address_map, .set_virtual_address_map = efiemu_set_virtual_address_map,
.convert_pointer = efiemu_convert_pointer, .convert_pointer = efiemu_convert_pointer,
.get_variable = efiemu_get_variable, /*
.get_next_variable_name = efiemu_get_next_variable_name, The code is structured in a way to accept unaligned inputs
.set_variable = efiemu_set_variable, in most cases and supply 4-byte aligned outputs.
Efiemu case is a bit ugly because there inputs and outputs are
reversed and so we need careful casts to account for this
inversion.
*/
.get_variable = (grub_efi_status_t
(__grub_efi_api *) (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid,
grub_efi_uint32_t *attributes,
grub_efi_uintn_t *data_size,
void *data)) efiemu_get_variable,
.get_next_variable_name = (grub_efi_status_t
(__grub_efi_api *) (grub_efi_uintn_t *variable_name_size,
grub_efi_char16_t *variable_name,
grub_guid_t *vendor_guid)) efiemu_get_next_variable_name,
.set_variable = (grub_efi_status_t
(__grub_efi_api *) (grub_efi_char16_t *variable_name,
const grub_guid_t *vendor_guid,
grub_efi_uint32_t attributes,
grub_efi_uintn_t data_size,
void *data)) efiemu_set_variable,
.get_next_high_monotonic_count = efiemu_get_next_high_monotonic_count, .get_next_high_monotonic_count = efiemu_get_next_high_monotonic_count,
.reset_system = efiemu_reset_system .reset_system = efiemu_reset_system

View file

@ -26,6 +26,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -703,11 +704,16 @@ static struct grub_fs grub_affs_fs =
GRUB_MOD_INIT(affs) GRUB_MOD_INIT(affs)
{ {
grub_fs_register (&grub_affs_fs); if (!grub_is_lockdown ())
{
grub_affs_fs.mod = mod;
grub_fs_register (&grub_affs_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI(affs) GRUB_MOD_FINI(affs)
{ {
grub_fs_unregister (&grub_affs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_affs_fs);
} }

View file

@ -21,6 +21,7 @@
#include <grub/fs.h> #include <grub/fs.h>
#include <grub/disk.h> #include <grub/disk.h>
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -68,6 +69,7 @@ handle_symlink (struct grub_archelp_data *data,
char *rest; char *rest;
char *linktarget; char *linktarget;
grub_size_t linktarget_len; grub_size_t linktarget_len;
grub_size_t sz;
*restart = 0; *restart = 0;
@ -98,7 +100,12 @@ handle_symlink (struct grub_archelp_data *data,
if (linktarget[0] == '\0') if (linktarget[0] == '\0')
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
linktarget_len = grub_strlen (linktarget); linktarget_len = grub_strlen (linktarget);
target = grub_malloc (linktarget_len + grub_strlen (*name) + 2);
if (grub_add (linktarget_len, grub_strlen (*name), &sz) ||
grub_add (sz, 2, &sz))
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("link target length overflow"));
target = grub_malloc (sz);
if (!target) if (!target)
return grub_errno; return grub_errno;
@ -180,6 +187,14 @@ grub_archelp_dir (struct grub_archelp_data *data,
if (p) if (p)
*p = 0; *p = 0;
if ((*n == 0) && ((mode & GRUB_ARCHELP_ATTR_TYPE)
!= GRUB_ARCHELP_ATTR_DIR))
{
grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
grub_free (name);
goto fail;
}
if (((!prev) || (grub_strcmp (prev, name) != 0)) && *n != 0) if (((!prev) || (grub_strcmp (prev, name) != 0)) && *n != 0)
{ {
struct grub_dirhook_info info; struct grub_dirhook_info info;

View file

@ -30,6 +30,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -808,7 +809,7 @@ find_file (const char *path, grub_disk_t disk,
.disk = disk, .disk = disk,
.sb = sb, .sb = sb,
}; };
struct grub_fshelp_node *found; struct grub_fshelp_node *found = NULL;
err = read_extent (disk, sb, &sb->root_dir, 0, 0, &root.ino, err = read_extent (disk, sb, &sb->root_dir, 0, 0, &root.ino,
sizeof (root.ino)); sizeof (root.ino));
@ -1106,7 +1107,11 @@ GRUB_MOD_INIT (bfs)
{ {
COMPILE_TIME_ASSERT (1 << LOG_EXTENT_SIZE == COMPILE_TIME_ASSERT (1 << LOG_EXTENT_SIZE ==
sizeof (struct grub_bfs_extent)); sizeof (struct grub_bfs_extent));
grub_fs_register (&grub_bfs_fs); if (!grub_is_lockdown ())
{
grub_bfs_fs.mod = mod;
grub_fs_register (&grub_bfs_fs);
}
} }
#ifdef MODE_AFS #ifdef MODE_AFS
@ -1115,5 +1120,6 @@ GRUB_MOD_FINI (afs)
GRUB_MOD_FINI (bfs) GRUB_MOD_FINI (bfs)
#endif #endif
{ {
grub_fs_unregister (&grub_bfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_bfs_fs);
} }

View file

@ -1276,8 +1276,8 @@ grub_btrfs_mount (grub_device_t dev)
} }
data->n_devices_allocated = 16; data->n_devices_allocated = 16;
data->devices_attached = grub_malloc (sizeof (data->devices_attached[0]) data->devices_attached = grub_calloc (data->n_devices_allocated,
* data->n_devices_allocated); sizeof (data->devices_attached[0]));
if (!data->devices_attached) if (!data->devices_attached)
{ {
grub_free (data); grub_free (data);
@ -1538,7 +1538,10 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
err = lower_bound (data, &key_in, &key_out, tree, err = lower_bound (data, &key_in, &key_out, tree,
&elemaddr, &elemsize, &desc, 0); &elemaddr, &elemsize, &desc, 0);
if (err) if (err)
return -1; {
grub_free (desc.data);
return -1;
}
if (key_out.object_id != ino if (key_out.object_id != ino
|| key_out.type != GRUB_BTRFS_ITEM_TYPE_EXTENT_ITEM) || key_out.type != GRUB_BTRFS_ITEM_TYPE_EXTENT_ITEM)
{ {
@ -1603,6 +1606,8 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
csize = grub_le_to_cpu64 (key_out.offset) - pos; csize = grub_le_to_cpu64 (key_out.offset) - pos;
if (csize > len) if (csize > len)
csize = len; csize = len;
grub_memset (buf, 0, csize);
buf += csize; buf += csize;
pos += csize; pos += csize;
len -= csize; len -= csize;
@ -1799,6 +1804,7 @@ find_path (struct grub_btrfs_data *data,
char *path_alloc = NULL; char *path_alloc = NULL;
char *origpath = NULL; char *origpath = NULL;
unsigned symlinks_max = 32; unsigned symlinks_max = 32;
grub_size_t sz;
err = get_root (data, key, tree, type); err = get_root (data, key, tree, type);
if (err) if (err)
@ -1889,9 +1895,15 @@ find_path (struct grub_btrfs_data *data,
struct grub_btrfs_dir_item *cdirel; struct grub_btrfs_dir_item *cdirel;
if (elemsize > allocated) if (elemsize > allocated)
{ {
allocated = 2 * elemsize; if (grub_mul (2, elemsize, &allocated) ||
grub_add (allocated, 1, &sz))
{
grub_free (path_alloc);
grub_free (origpath);
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory item size overflow"));
}
grub_free (direl); grub_free (direl);
direl = grub_malloc (allocated + 1); direl = grub_malloc (sz);
if (!direl) if (!direl)
{ {
grub_free (path_alloc); grub_free (path_alloc);
@ -1953,8 +1965,16 @@ find_path (struct grub_btrfs_data *data,
grub_free (origpath); grub_free (origpath);
return err; return err;
} }
tmp = grub_malloc (grub_le_to_cpu64 (inode.size)
+ grub_strlen (path) + 1); if (grub_add (grub_le_to_cpu64 (inode.size), grub_strlen (path), &sz) ||
grub_add (sz, 1, &sz))
{
grub_free (direl);
grub_free (path_alloc);
grub_free (origpath);
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("buffer size overflow"));
}
tmp = grub_malloc (sz);
if (!tmp) if (!tmp)
{ {
grub_free (direl); grub_free (direl);
@ -2076,6 +2096,7 @@ grub_btrfs_dir (grub_device_t device, const char *path,
grub_uint64_t tree; grub_uint64_t tree;
grub_uint8_t type; grub_uint8_t type;
grub_size_t est_size = 0; grub_size_t est_size = 0;
grub_size_t sz;
if (!data) if (!data)
return grub_errno; return grub_errno;
@ -2097,6 +2118,7 @@ grub_btrfs_dir (grub_device_t device, const char *path,
if (err) if (err)
{ {
grub_btrfs_unmount (data); grub_btrfs_unmount (data);
grub_free (desc.data);
return err; return err;
} }
if (key_out.type != GRUB_BTRFS_ITEM_TYPE_DIR_ITEM if (key_out.type != GRUB_BTRFS_ITEM_TYPE_DIR_ITEM
@ -2117,9 +2139,15 @@ grub_btrfs_dir (grub_device_t device, const char *path,
} }
if (elemsize > allocated) if (elemsize > allocated)
{ {
allocated = 2 * elemsize; if (grub_mul (2, elemsize, &allocated) ||
grub_add (allocated, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory element size overflow"));
r = -grub_errno;
break;
}
grub_free (direl); grub_free (direl);
direl = grub_malloc (allocated + 1); direl = grub_malloc (sz);
if (!direl) if (!direl)
{ {
r = -grub_errno; r = -grub_errno;
@ -2411,6 +2439,7 @@ static struct grub_fs grub_btrfs_fs = {
GRUB_MOD_INIT (btrfs) GRUB_MOD_INIT (btrfs)
{ {
grub_btrfs_fs.mod = mod;
grub_fs_register (&grub_btrfs_fs); grub_fs_register (&grub_btrfs_fs);
} }

View file

@ -26,6 +26,7 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/cbfs_core.h> #include <grub/cbfs_core.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -390,12 +391,17 @@ GRUB_MOD_INIT (cbfs)
#if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN) #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN)
init_cbfsdisk (); init_cbfsdisk ();
#endif #endif
grub_fs_register (&grub_cbfs_fs); if (!grub_is_lockdown ())
{
grub_cbfs_fs.mod = mod;
grub_fs_register (&grub_cbfs_fs);
}
} }
GRUB_MOD_FINI (cbfs) GRUB_MOD_FINI (cbfs)
{ {
grub_fs_unregister (&grub_cbfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_cbfs_fs);
#if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN) #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN)
fini_cbfsdisk (); fini_cbfsdisk ();
#endif #endif

View file

@ -52,6 +52,7 @@ read_number (const grub_uint16_t *arr, grub_size_t size)
GRUB_MOD_INIT (cpio) GRUB_MOD_INIT (cpio)
{ {
grub_cpio_fs.mod = mod;
grub_fs_register (&grub_cpio_fs); grub_fs_register (&grub_cpio_fs);
} }

View file

@ -52,6 +52,7 @@ read_number (const grub_uint16_t *arr, grub_size_t size)
GRUB_MOD_INIT (cpio_be) GRUB_MOD_INIT (cpio_be)
{ {
grub_cpio_fs.mod = mod;
grub_fs_register (&grub_cpio_fs); grub_fs_register (&grub_cpio_fs);
} }

View file

@ -24,6 +24,7 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/archelp.h> #include <grub/archelp.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -48,6 +49,7 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
struct head hd; struct head hd;
grub_size_t namesize; grub_size_t namesize;
grub_uint32_t modeval; grub_uint32_t modeval;
grub_size_t sz;
data->hofs = data->next_hofs; data->hofs = data->next_hofs;
@ -60,11 +62,21 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
#endif #endif
) )
return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive"); return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive");
data->size = read_number (hd.filesize, ARRAY_SIZE (hd.filesize));
if (grub_cast (read_number (hd.filesize, ARRAY_SIZE (hd.filesize)), &data->size))
return grub_error (GRUB_ERR_BAD_FS, N_("data size overflow"));
if (mtime) if (mtime)
*mtime = read_number (hd.mtime, ARRAY_SIZE (hd.mtime)); {
modeval = read_number (hd.mode, ARRAY_SIZE (hd.mode)); if (grub_cast (read_number (hd.mtime, ARRAY_SIZE (hd.mtime)), mtime))
namesize = read_number (hd.namesize, ARRAY_SIZE (hd.namesize)); return grub_error (GRUB_ERR_BAD_FS, N_("mtime overflow"));
}
if (grub_cast (read_number (hd.mode, ARRAY_SIZE (hd.mode)), &modeval))
return grub_error (GRUB_ERR_BAD_FS, N_("mode overflow"));
if (grub_cast (read_number (hd.namesize, ARRAY_SIZE (hd.namesize)), &namesize))
return grub_error (GRUB_ERR_BAD_FS, N_("namesize overflow"));
/* Don't allow negative numbers. */ /* Don't allow negative numbers. */
if (namesize >= 0x80000000) if (namesize >= 0x80000000)
@ -76,7 +88,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
*mode = modeval; *mode = modeval;
*name = grub_malloc (namesize + 1); if (grub_add (namesize, 1, &sz))
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("file name size overflow"));
*name = grub_malloc (sz);
if (*name == NULL) if (*name == NULL)
return grub_errno; return grub_errno;
@ -110,10 +125,17 @@ grub_cpio_get_link_target (struct grub_archelp_data *data)
{ {
char *ret; char *ret;
grub_err_t err; grub_err_t err;
grub_size_t sz;
if (data->size == 0) if (data->size == 0)
return grub_strdup (""); return grub_strdup ("");
ret = grub_malloc (data->size + 1);
if (grub_add (data->size, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("target data size overflow"));
return NULL;
}
ret = grub_malloc (sz);
if (!ret) if (!ret)
return NULL; return NULL;

1006
grub-core/fs/erofs.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -495,6 +495,11 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
struct grub_ext4_extent *ext; struct grub_ext4_extent *ext;
int i; int i;
grub_disk_addr_t ret; grub_disk_addr_t ret;
grub_uint16_t nent;
/* Maximum number of extent entries in the inode's inline extent area. */
const grub_uint16_t max_inline_ext = sizeof (inode->blocks) / sizeof (*ext) - 1; /* Minus 1 extent header. */
/* Maximum number of extent entries in the external extent block. */
const grub_uint16_t max_external_ext = EXT2_BLOCK_SIZE (data) / sizeof (*ext) - 1; /* Minus 1 extent header. */
if (grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks, if (grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks,
fileblock, &leaf) != GRUB_ERR_NONE) fileblock, &leaf) != GRUB_ERR_NONE)
@ -508,7 +513,23 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
return 0; return 0;
ext = (struct grub_ext4_extent *) (leaf + 1); ext = (struct grub_ext4_extent *) (leaf + 1);
for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++)
nent = grub_le_to_cpu16 (leaf->entries);
/*
* Determine the effective number of extent entries (nent) to process.
* If the extent header (leaf) is stored inline in the inodes block
* area, i.e. at inode->blocks.dir_blocks, then only max_inline_ext
* entries can fit. Otherwise, if the header was read from an external
* extent block use the larger limit, max_external_ext, based on the
* full block size.
*/
if (leaf == (struct grub_ext4_extent_header *) inode->blocks.dir_blocks)
nent = grub_min (nent, max_inline_ext);
else
nent = grub_min (nent, max_external_ext);
for (i = 0; i < nent; i++)
{ {
if (fileblock < grub_le_to_cpu32 (ext[i].block)) if (fileblock < grub_le_to_cpu32 (ext[i].block))
break; break;
@ -1123,6 +1144,7 @@ static struct grub_fs grub_ext2_fs =
GRUB_MOD_INIT(ext2) GRUB_MOD_INIT(ext2)
{ {
grub_ext2_fs.mod = mod;
grub_fs_register (&grub_ext2_fs); grub_fs_register (&grub_ext2_fs);
my_mod = mod; my_mod = mod;
} }

View file

@ -28,6 +28,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -872,6 +873,9 @@ grub_f2fs_mount (grub_disk_t disk)
return data; return data;
fail: fail:
if (grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
grub_free (data); grub_free (data);
return NULL; return NULL;
@ -955,6 +959,7 @@ grub_f2fs_read_symlink (grub_fshelp_node_t node)
char *symlink; char *symlink;
struct grub_fshelp_node *diro = node; struct grub_fshelp_node *diro = node;
grub_uint64_t filesize; grub_uint64_t filesize;
grub_size_t sz;
if (!diro->inode_read) if (!diro->inode_read)
{ {
@ -965,7 +970,12 @@ grub_f2fs_read_symlink (grub_fshelp_node_t node)
filesize = grub_f2fs_file_size(&diro->inode.i); filesize = grub_f2fs_file_size(&diro->inode.i);
symlink = grub_malloc (filesize + 1); if (grub_add (filesize, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink size overflow"));
return 0;
}
symlink = grub_malloc (sz);
if (!symlink) if (!symlink)
return 0; return 0;
@ -994,6 +1004,7 @@ grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx)
enum FILE_TYPE ftype; enum FILE_TYPE ftype;
int name_len; int name_len;
int ret; int ret;
int sz;
if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0) if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
{ {
@ -1007,7 +1018,12 @@ grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx)
if (name_len >= F2FS_NAME_LEN) if (name_len >= F2FS_NAME_LEN)
return 0; return 0;
filename = grub_malloc (name_len + 1); if (grub_add (name_len, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory entry name length overflow"));
return 0;
}
filename = grub_malloc (sz);
if (!filename) if (!filename)
return 0; return 0;
@ -1350,6 +1366,7 @@ static struct grub_fs grub_f2fs_fs = {
GRUB_MOD_INIT (f2fs) GRUB_MOD_INIT (f2fs)
{ {
grub_f2fs_fs.mod = mod;
grub_fs_register (&grub_f2fs_fs); grub_fs_register (&grub_f2fs_fs);
my_mod = mod; my_mod = mod;
} }

View file

@ -1312,6 +1312,7 @@ GRUB_MOD_INIT(fat)
#endif #endif
{ {
COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32); COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32);
grub_fat_fs.mod = mod;
grub_fs_register (&grub_fat_fs); grub_fs_register (&grub_fat_fs);
my_mod = mod; my_mod = mod;
} }

View file

@ -226,7 +226,10 @@ find_file (char *currpath,
return grub_error (GRUB_ERR_SYMLINK_LOOP, return grub_error (GRUB_ERR_SYMLINK_LOOP,
N_("too deep nesting of symlinks")); N_("too deep nesting of symlinks"));
symlink = read_symlink (ctx->currnode->node); if (read_symlink != NULL)
symlink = read_symlink (ctx->currnode->node);
else
return grub_error (GRUB_ERR_BAD_FS, "read_symlink is NULL");
if (!symlink) if (!symlink)
return grub_errno; return grub_errno;

View file

@ -379,7 +379,7 @@ grub_hfs_mount (grub_disk_t disk)
volume name. */ volume name. */
key.parent_dir = grub_cpu_to_be32_compile_time (1); key.parent_dir = grub_cpu_to_be32_compile_time (1);
key.strlen = data->sblock.volname[0]; key.strlen = data->sblock.volname[0];
grub_strcpy ((char *) key.str, (char *) (data->sblock.volname + 1)); grub_strlcpy ((char *) key.str, (char *) (data->sblock.volname + 1), sizeof (key.str));
if (grub_hfs_find_node (data, (char *) &key, data->cat_root, if (grub_hfs_find_node (data, (char *) &key, data->cat_root,
0, (char *) &dir, sizeof (dir)) == 0) 0, (char *) &dir, sizeof (dir)) == 0)
@ -1434,6 +1434,7 @@ static struct grub_fs grub_hfs_fs =
GRUB_MOD_INIT(hfs) GRUB_MOD_INIT(hfs)
{ {
grub_hfs_fs.mod = mod;
if (!grub_is_lockdown ()) if (!grub_is_lockdown ())
grub_fs_register (&grub_hfs_fs); grub_fs_register (&grub_hfs_fs);
my_mod = mod; my_mod = mod;

View file

@ -405,7 +405,7 @@ grub_hfsplus_mount (grub_disk_t disk)
fail: fail:
if (grub_errno == GRUB_ERR_OUT_OF_RANGE) if (grub_errno == GRUB_ERR_OUT_OF_RANGE || grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem"); grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
grub_free (data); grub_free (data);
@ -1176,6 +1176,7 @@ static struct grub_fs grub_hfsplus_fs =
GRUB_MOD_INIT(hfsplus) GRUB_MOD_INIT(hfsplus)
{ {
grub_hfsplus_fs.mod = mod;
grub_fs_register (&grub_hfsplus_fs); grub_fs_register (&grub_hfsplus_fs);
my_mod = mod; my_mod = mod;
} }

View file

@ -244,14 +244,19 @@ hfsplus_open_compressed_real (struct grub_hfsplus_file *node)
return 0; return 0;
} }
node->compress_index_size = grub_le_to_cpu32 (index_size); node->compress_index_size = grub_le_to_cpu32 (index_size);
node->compress_index = grub_malloc (node->compress_index_size node->compress_index = grub_calloc (node->compress_index_size,
* sizeof (node->compress_index[0])); sizeof (node->compress_index[0]));
if (!node->compress_index) if (!node->compress_index)
{ {
node->compressed = 0; node->compressed = 0;
grub_free (attr_node); grub_free (attr_node);
return grub_errno; return grub_errno;
} }
/*
* The node->compress_index_size * sizeof (node->compress_index[0]) is safe here
* due to relevant checks done in grub_calloc() above.
*/
if (grub_hfsplus_read_file (node, 0, 0, if (grub_hfsplus_read_file (node, 0, 0,
0x104 + sizeof (index_size), 0x104 + sizeof (index_size),
node->compress_index_size node->compress_index_size

View file

@ -551,6 +551,9 @@ grub_iso9660_mount (grub_disk_t disk)
return data; return data;
fail: fail:
if (grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
grub_free (data); grub_free (data);
return 0; return 0;
} }
@ -625,9 +628,19 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
filename type is stored. */ filename type is stored. */
/* FIXME: Fix this slightly improper cast. */ /* FIXME: Fix this slightly improper cast. */
if (entry->data[0] & GRUB_ISO9660_RR_DOT) if (entry->data[0] & GRUB_ISO9660_RR_DOT)
ctx->filename = (char *) "."; {
if (ctx->filename_alloc)
grub_free (ctx->filename);
ctx->filename_alloc = 0;
ctx->filename = (char *) ".";
}
else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT) else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
ctx->filename = (char *) ".."; {
if (ctx->filename_alloc)
grub_free (ctx->filename);
ctx->filename_alloc = 0;
ctx->filename = (char *) "..";
}
else if (entry->len >= 5) else if (entry->len >= 5)
{ {
grub_size_t off = 0, csize = 1; grub_size_t off = 0, csize = 1;
@ -1247,6 +1260,7 @@ static struct grub_fs grub_iso9660_fs =
GRUB_MOD_INIT(iso9660) GRUB_MOD_INIT(iso9660)
{ {
grub_iso9660_fs.mod = mod;
grub_fs_register (&grub_iso9660_fs); grub_fs_register (&grub_iso9660_fs);
my_mod = mod; my_mod = mod;
} }

View file

@ -26,6 +26,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -41,6 +42,13 @@ GRUB_MOD_LICENSE ("GPLv3+");
#define GRUB_JFS_TREE_LEAF 2 #define GRUB_JFS_TREE_LEAF 2
/*
* Define max entries stored in-line in an inode.
* https://jfs.sourceforge.net/project/pub/jfslayout.pdf
*/
#define GRUB_JFS_INODE_INLINE_ENTRIES 8
#define GRUB_JFS_DIR_MAX_SLOTS 128
struct grub_jfs_sblock struct grub_jfs_sblock
{ {
/* The magic for JFS. It should contain the string "JFS1". */ /* The magic for JFS. It should contain the string "JFS1". */
@ -203,9 +211,9 @@ struct grub_jfs_inode
grub_uint8_t freecnt; grub_uint8_t freecnt;
grub_uint8_t freelist; grub_uint8_t freelist;
grub_uint32_t idotdot; grub_uint32_t idotdot;
grub_uint8_t sorted[8]; grub_uint8_t sorted[GRUB_JFS_INODE_INLINE_ENTRIES];
} header; } header;
struct grub_jfs_leaf_dirent dirents[8]; struct grub_jfs_leaf_dirent dirents[GRUB_JFS_INODE_INLINE_ENTRIES];
} GRUB_PACKED dir; } GRUB_PACKED dir;
/* Fast symlink. */ /* Fast symlink. */
struct struct
@ -258,7 +266,21 @@ static grub_dl_t my_mod;
static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino); static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino);
static grub_int64_t /*
* An extent's offset, physical and logical, is represented as a 40-bit value.
* This 40-bit value is split into two parts:
* - offset1: the most signficant 8 bits of the offset,
* - offset2: the least significant 32 bits of the offset.
*
* This function calculates and returns the 64-bit offset of an extent.
*/
static grub_uint64_t
get_ext_offset (grub_uint8_t offset1, grub_uint32_t offset2)
{
return (((grub_uint64_t) offset1 << 32) | grub_le_to_cpu32 (offset2));
}
static grub_uint64_t
getblk (struct grub_jfs_treehead *treehead, getblk (struct grub_jfs_treehead *treehead,
struct grub_jfs_tree_extent *extents, struct grub_jfs_tree_extent *extents,
int max_extents, int max_extents,
@ -267,28 +289,33 @@ getblk (struct grub_jfs_treehead *treehead,
{ {
int found = -1; int found = -1;
int i; int i;
grub_uint64_t ext_offset, ext_blk;
grub_errno = GRUB_ERR_NONE;
for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 && for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 &&
i < max_extents; i++) i < max_extents; i++)
{ {
ext_offset = get_ext_offset (extents[i].offset1, extents[i].offset2);
ext_blk = get_ext_offset (extents[i].extent.blk1, extents[i].extent.blk2);
if (treehead->flags & GRUB_JFS_TREE_LEAF) if (treehead->flags & GRUB_JFS_TREE_LEAF)
{ {
/* Read the leafnode. */ /* Read the leafnode. */
if (grub_le_to_cpu32 (extents[i].offset2) <= blk if (ext_offset <= blk
&& ((grub_le_to_cpu16 (extents[i].extent.length)) && ((grub_le_to_cpu16 (extents[i].extent.length))
+ (extents[i].extent.length2 << 16) + (extents[i].extent.length2 << 16)
+ grub_le_to_cpu32 (extents[i].offset2)) > blk) + ext_offset) > blk)
return (blk - grub_le_to_cpu32 (extents[i].offset2) return (blk - ext_offset + ext_blk);
+ grub_le_to_cpu32 (extents[i].extent.blk2));
} }
else else
if (blk >= grub_le_to_cpu32 (extents[i].offset2)) if (blk >= ext_offset)
found = i; found = i;
} }
if (found != -1) if (found != -1)
{ {
grub_int64_t ret = -1; grub_uint64_t ret = 0;
struct struct
{ {
struct grub_jfs_treehead treehead; struct grub_jfs_treehead treehead;
@ -297,13 +324,12 @@ getblk (struct grub_jfs_treehead *treehead,
tree = grub_zalloc (sizeof (*tree)); tree = grub_zalloc (sizeof (*tree));
if (!tree) if (!tree)
return -1; return 0;
if (!grub_disk_read (data->disk, if (!grub_disk_read (data->disk,
((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2)) (grub_disk_addr_t) ext_blk
<< (grub_le_to_cpu16 (data->sblock.log2_blksz) << (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS),
- GRUB_DISK_SECTOR_BITS), 0, 0, sizeof (*tree), (char *) tree))
sizeof (*tree), (char *) tree))
{ {
if (grub_memcmp (&tree->treehead, treehead, sizeof (struct grub_jfs_treehead)) || if (grub_memcmp (&tree->treehead, treehead, sizeof (struct grub_jfs_treehead)) ||
grub_memcmp (&tree->extents, extents, 254 * sizeof (struct grub_jfs_tree_extent))) grub_memcmp (&tree->extents, extents, 254 * sizeof (struct grub_jfs_tree_extent)))
@ -311,19 +337,20 @@ getblk (struct grub_jfs_treehead *treehead,
else else
{ {
grub_error (GRUB_ERR_BAD_FS, "jfs: infinite recursion detected"); grub_error (GRUB_ERR_BAD_FS, "jfs: infinite recursion detected");
ret = -1; ret = 0;
} }
} }
grub_free (tree); grub_free (tree);
return ret; return ret;
} }
return -1; grub_error (GRUB_ERR_READ_ERROR, "jfs: block %" PRIuGRUB_UINT64_T " not found", blk);
return 0;
} }
/* Get the block number for the block BLK in the node INODE in the /* Get the block number for the block BLK in the node INODE in the
mounted filesystem DATA. */ mounted filesystem DATA. */
static grub_int64_t static grub_uint64_t
grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode, grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
grub_uint64_t blk) grub_uint64_t blk)
{ {
@ -354,7 +381,7 @@ grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
sizeof (iag_inodes), &iag_inodes)) sizeof (iag_inodes), &iag_inodes))
return grub_errno; return grub_errno;
inoblk = grub_le_to_cpu32 (iag_inodes[inoext].blk2); inoblk = get_ext_offset (iag_inodes[inoext].blk1, iag_inodes[inoext].blk2);
inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
- GRUB_DISK_SECTOR_BITS); - GRUB_DISK_SECTOR_BITS);
inoblk += inonum; inoblk += inonum;
@ -453,6 +480,13 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
/* Check if the entire tree is contained within the inode. */ /* Check if the entire tree is contained within the inode. */
if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF) if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
{ {
if (inode->dir.header.count > GRUB_JFS_INODE_INLINE_ENTRIES)
{
grub_free (diro);
grub_error (GRUB_ERR_BAD_FS, N_("invalid JFS inode"));
return 0;
}
diro->leaf = inode->dir.dirents; diro->leaf = inode->dir.dirents;
diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de; diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
diro->sorted = inode->dir.header.sorted; diro->sorted = inode->dir.header.sorted;
@ -468,7 +502,16 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
return 0; return 0;
} }
blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2); if (inode->dir.header.sorted[0] >= GRUB_JFS_DIR_MAX_SLOTS)
{
grub_error (GRUB_ERR_BAD_FS, N_("invalid directory slot index"));
grub_free (diro->dirpage);
grub_free (diro);
return 0;
}
blk = get_ext_offset (de[inode->dir.header.sorted[0]].ex.blk1,
de[inode->dir.header.sorted[0]].ex.blk2);
blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS); blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
/* Read in the nodes until we are on the leaf node level. */ /* Read in the nodes until we are on the leaf node level. */
@ -486,7 +529,7 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent; de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32]; index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
blk = (grub_le_to_cpu32 (de[index].ex.blk2) blk = (get_ext_offset (de[index].ex.blk1, de[index].ex.blk2)
<< (grub_le_to_cpu16 (data->sblock.log2_blksz) << (grub_le_to_cpu16 (data->sblock.log2_blksz)
- GRUB_DISK_SECTOR_BITS)); - GRUB_DISK_SECTOR_BITS));
} while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF)); } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
@ -716,7 +759,6 @@ grub_jfs_find_file (struct grub_jfs_data *data, const char *path,
grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode); grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
grub_jfs_closedir (diro); grub_jfs_closedir (diro);
diro = 0;
if (grub_jfs_read_inode (data, ino, &data->currinode)) if (grub_jfs_read_inode (data, ino, &data->currinode))
break; break;
@ -963,11 +1005,16 @@ static struct grub_fs grub_jfs_fs =
GRUB_MOD_INIT(jfs) GRUB_MOD_INIT(jfs)
{ {
grub_fs_register (&grub_jfs_fs); if (!grub_is_lockdown ())
{
grub_jfs_fs.mod = mod;
grub_fs_register (&grub_jfs_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI(jfs) GRUB_MOD_FINI(jfs)
{ {
grub_fs_unregister (&grub_jfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_jfs_fs);
} }

View file

@ -25,6 +25,7 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -734,7 +735,11 @@ GRUB_MOD_INIT(minix)
#endif #endif
#endif #endif
{ {
grub_fs_register (&grub_minix_fs); if (!grub_is_lockdown ())
{
grub_minix_fs.mod = mod;
grub_fs_register (&grub_minix_fs);
}
my_mod = mod; my_mod = mod;
} }
@ -756,5 +761,6 @@ GRUB_MOD_FINI(minix)
#endif #endif
#endif #endif
{ {
grub_fs_unregister (&grub_minix_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_minix_fs);
} }

View file

@ -64,6 +64,7 @@ read_number (const char *str, grub_size_t size)
GRUB_MOD_INIT (newc) GRUB_MOD_INIT (newc)
{ {
grub_cpio_fs.mod = mod;
grub_fs_register (&grub_cpio_fs); grub_fs_register (&grub_cpio_fs);
} }

View file

@ -34,6 +34,7 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -1231,11 +1232,16 @@ GRUB_MOD_INIT (nilfs2)
grub_nilfs2_dat_entry)); grub_nilfs2_dat_entry));
COMPILE_TIME_ASSERT (1 << LOG_INODE_SIZE COMPILE_TIME_ASSERT (1 << LOG_INODE_SIZE
== sizeof (struct grub_nilfs2_inode)); == sizeof (struct grub_nilfs2_inode));
grub_fs_register (&grub_nilfs2_fs); if (!grub_is_lockdown ())
{
grub_nilfs2_fs.mod = mod;
grub_fs_register (&grub_nilfs2_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI (nilfs2) GRUB_MOD_FINI (nilfs2)
{ {
grub_fs_unregister (&grub_nilfs2_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_nilfs2_fs);
} }

View file

@ -27,6 +27,7 @@
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/ntfs.h> #include <grub/ntfs.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -52,6 +53,193 @@ u64at (void *ptr, grub_size_t ofs)
return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs)); return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
} }
static grub_uint16_t
first_attr_off (void *mft_buf_ptr)
{
return u16at (mft_buf_ptr, 0x14);
}
static grub_uint16_t
res_attr_data_off (void *res_attr_ptr)
{
return u16at (res_attr_ptr, 0x14);
}
static grub_uint32_t
res_attr_data_len (void *res_attr_ptr)
{
return u32at (res_attr_ptr, 0x10);
}
/*
* Check if the attribute is valid and doesn't exceed the allocated region.
* This accounts for resident and non-resident data.
*
* This is based off the documentation from the linux-ntfs project:
* https://flatcap.github.io/linux-ntfs/ntfs/concepts/attribute_header.html
*/
static bool
validate_attribute (grub_uint8_t *attr, void *end)
{
grub_size_t attr_size = 0;
grub_size_t min_size = 0;
grub_size_t run_size = 0;
grub_size_t spare = (grub_uint8_t *) end - attr;
/*
* Just used as a temporary variable to try and deal with cases where someone
* tries to overlap fields.
*/
grub_size_t curr = 0;
/* Need verify we can entirely read the attributes header. */
if (attr + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= (grub_uint8_t *) end)
goto fail;
/*
* So, the rest of this code uses a 16bit int for the attribute length but
* from reading the all the documentation I could find it says this field is
* actually 32bit. But let's be consistent with the rest of the code.
*
* https://elixir.bootlin.com/linux/v6.10.7/source/fs/ntfs3/ntfs.h#L370
*/
attr_size = u16at (attr, GRUB_NTFS_ATTRIBUTE_LENGTH);
if (attr_size > spare)
goto fail;
/* Not an error case, just reached the end of the attributes. */
if (attr_size == 0)
return false;
/*
* Extra validation by trying to calculate a minimum possible size for this
* attribute. +8 from the size of the resident data struct which is the
* minimum that can be added.
*/
min_size = GRUB_NTFS_ATTRIBUTE_HEADER_SIZE + 8;
if (min_size > attr_size)
goto fail;
/* Is the data is resident (0) or not (1). */
if (attr[GRUB_NTFS_ATTRIBUTE_RESIDENT] == 0)
{
/* Read the offset and size of the attribute. */
curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_RES_OFFSET);
curr += u32at (attr, GRUB_NTFS_ATTRIBUTE_RES_LENGTH);
if (curr > min_size)
min_size = curr;
}
else
{
/*
* If the data is non-resident, the minimum size is 64 which is where
* the data runs start. We already have a minimum size of 24. So, just
* adding 40 to get to the real value.
*/
min_size += 40;
if (min_size > attr_size)
goto fail;
/* If the compression unit size is > 0, +8 bytes*/
if (u16at (attr, GRUB_NTFS_ATTRIBUTE_COMPRESSION_UNIT_SIZE) > 0)
min_size += 8;
/*
* Need to consider the data runs now. Each member of the run has byte
* that describes the size of the data length and offset. Each being
* 4 bits in the byte.
*/
curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_DATA_RUNS);
if (curr + 1 > min_size)
min_size = curr + 1;
if (min_size > attr_size)
goto fail;
/*
* Each attribute can store multiple data runs which are stored
* continuously in the attribute. They exist as one header byte
* with up to 14 bytes following it depending on the lengths.
* We stop when we hit a header that is just a NUL byte.
*
* https://flatcap.github.io/linux-ntfs/ntfs/concepts/data_runs.html
*/
while (attr[curr] != 0)
{
/*
* We stop when we hit a header that is just a NUL byte. The data
* run header is stored as a single byte where the top 4 bits refer
* to the number of bytes used to store the total length of the
* data run, and the number of bytes used to store the offset.
* These directly follow the header byte, so we use them to update
* the minimum size. Increment by one more than run size to move on
* to the next run size header byte. An example is a run size field
* value of 0x32, 3 + 2 = 5 bytes follow the run size. Increment
* by 5 to get to the end of this data run then one more to get to
* the start of the next run size byte.
*/
run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
curr += (run_size + 1);
min_size += (run_size + 1);
if (min_size > attr_size)
goto fail;
}
}
/* Name offset, doing this after data residence checks. */
if (u16at (attr, GRUB_NTFS_ATTRIBUTE_NAME_OFFSET) != 0)
{
curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_NAME_OFFSET);
/*
* Multiple the name length by 2 as its UTF-16. Can be zero if this in an
* unamed attribute.
*/
curr += attr[GRUB_NTFS_ATTRIBUTE_NAME_LENGTH] * 2;
if (curr > min_size)
min_size = curr;
}
/* Padded to 8 bytes. */
if (min_size % 8 != 0)
min_size += 8 - (min_size % 8);
/*
* At this point min_size should be exactly attr_size but being flexible
* here to avoid any issues.
*/
if (min_size > attr_size)
goto fail;
return true;
fail:
grub_dprintf ("ntfs", "spare=%" PRIuGRUB_SIZE " min_size=%" PRIuGRUB_SIZE " attr_size=%" PRIuGRUB_SIZE "\n",
spare, min_size, attr_size);
return false;
}
/* Return the next attribute if it exists, otherwise return NULL. */
static grub_uint8_t *
next_attribute (grub_uint8_t *curr_attribute, void *end, bool validate)
{
grub_uint8_t *next = curr_attribute;
/*
* Need to verify we aren't exceeding the end of the buffer by reading the
* header for the current attribute
*/
if (curr_attribute + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= (grub_uint8_t *) end)
return NULL;
next += u16at (curr_attribute, 4);
if (validate && validate_attribute (next, end) == false)
return NULL;
return next;
}
grub_ntfscomp_func_t grub_ntfscomp_func; grub_ntfscomp_func_t grub_ntfscomp_func;
static grub_err_t static grub_err_t
@ -60,6 +248,7 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
grub_uint16_t ss; grub_uint16_t ss;
grub_uint8_t *pu; grub_uint8_t *pu;
grub_uint16_t us; grub_uint16_t us;
grub_uint16_t pu_offset;
COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE); COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
@ -69,7 +258,10 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
ss = u16at (buf, 6) - 1; ss = u16at (buf, 6) - 1;
if (ss != len) if (ss != len)
return grub_error (GRUB_ERR_BAD_FS, "size not match"); return grub_error (GRUB_ERR_BAD_FS, "size not match");
pu = buf + u16at (buf, 4); pu_offset = u16at (buf, 4);
if (pu_offset >= (len * GRUB_DISK_SECTOR_SIZE - (2 * ss)))
return grub_error (GRUB_ERR_BAD_FS, "pu offset size incorrect");
pu = buf + pu_offset;
us = u16at (pu, 0); us = u16at (pu, 0);
buf -= 2; buf -= 2;
while (ss > 0) while (ss > 0)
@ -101,13 +293,20 @@ static grub_err_t read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa,
grub_disk_read_hook_t read_hook, grub_disk_read_hook_t read_hook,
void *read_hook_data); void *read_hook_data);
static void static grub_err_t
init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft) init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
{ {
at->mft = mft; at->mft = mft;
at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0; at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
at->attr_nxt = mft->buf + u16at (mft->buf, 0x14); at->attr_nxt = mft->buf + first_attr_off (mft->buf);
at->end = mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
if (at->attr_nxt > at->end)
return grub_error (GRUB_ERR_BAD_FS, "attributes start outside the MFT");
at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL; at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
return GRUB_ERR_NONE;
} }
static void static void
@ -121,16 +320,26 @@ free_attr (struct grub_ntfs_attr *at)
static grub_uint8_t * static grub_uint8_t *
find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
{ {
grub_uint8_t *mft_end;
grub_uint16_t nsize;
grub_uint16_t nxt_offset;
grub_uint32_t edat_offset;
/* GRUB_NTFS_AF_ALST indicates the attribute list type */
if (at->flags & GRUB_NTFS_AF_ALST) if (at->flags & GRUB_NTFS_AF_ALST)
{ {
retry: retry:
while (at->attr_nxt < at->attr_end) while (at->attr_nxt)
{ {
at->attr_cur = at->attr_nxt; at->attr_cur = at->attr_nxt;
at->attr_nxt += u16at (at->attr_cur, 4); /*
* Go to the next attribute in the list but do not validate
* because this is the attribute list type.
*/
at->attr_nxt = next_attribute (at->attr_cur, at->attr_end, false);
if ((*at->attr_cur == attr) || (attr == 0)) if ((*at->attr_cur == attr) || (attr == 0))
{ {
grub_uint8_t *new_pos; grub_uint8_t *new_pos, *end;
if (at->flags & GRUB_NTFS_AF_MMFT) if (at->flags & GRUB_NTFS_AF_MMFT)
{ {
@ -154,15 +363,41 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
return NULL; return NULL;
} }
new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)]; /*
while (*new_pos != 0xFF) * Only time emft_bufs is defined is in this function, with this
* size.
*/
grub_size_t emft_buf_size =
at->mft->data->mft_size << GRUB_NTFS_BLK_SHR;
/*
* Needs to be enough space for the successful case to even
* bother.
*/
if (first_attr_off (at->emft_buf) >= (emft_buf_size - 0x18 - 2))
{
grub_error (GRUB_ERR_BAD_FS,
"can\'t find 0x%X in attribute list",
(unsigned char) *at->attr_cur);
return NULL;
}
new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
end = &at->emft_buf[emft_buf_size];
at->end = end;
while (new_pos && *new_pos != 0xFF)
{ {
if ((*new_pos == *at->attr_cur) if ((*new_pos == *at->attr_cur)
&& (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18))) && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
{ {
return new_pos; return new_pos;
} }
new_pos += u16at (new_pos, 4); /*
* Go to the next attribute in the list but do not validate
* because this is the attribute list type.
*/
new_pos = next_attribute (new_pos, end, false);
} }
grub_error (GRUB_ERR_BAD_FS, grub_error (GRUB_ERR_BAD_FS,
"can\'t find 0x%X in attribute list", "can\'t find 0x%X in attribute list",
@ -173,18 +408,33 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
return NULL; return NULL;
} }
at->attr_cur = at->attr_nxt; at->attr_cur = at->attr_nxt;
while (*at->attr_cur != 0xFF) mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
while (at->attr_cur >= at->mft->buf && at->attr_cur < (mft_end - 4)
&& *at->attr_cur != 0xFF)
{ {
at->attr_nxt += u16at (at->attr_cur, 4); /*
* We can't use validate_attribute here because this logic
* seems to be used for both parsing through attributes
* and attribute lists.
*/
nsize = u16at (at->attr_cur, 4);
if (at->attr_cur + grub_max (GRUB_NTFS_ATTRIBUTE_HEADER_SIZE, nsize) >= at->end)
{
at->attr_nxt = at->attr_cur;
break;
}
else
at->attr_nxt = at->attr_cur + nsize;
if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST) if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
at->attr_end = at->attr_cur; at->attr_end = at->attr_cur;
if ((*at->attr_cur == attr) || (attr == 0)) if ((*at->attr_cur == attr) || (attr == 0) || (nsize == 0))
return at->attr_cur; return at->attr_cur;
at->attr_cur = at->attr_nxt; at->attr_cur = at->attr_nxt;
} }
if (at->attr_end) if (at->attr_end)
{ {
grub_uint8_t *pa; grub_uint8_t *pa, *pa_end;
at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR); at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
if (at->emft_buf == NULL) if (at->emft_buf == NULL)
@ -208,21 +458,54 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
return NULL; return NULL;
} }
at->attr_nxt = at->edat_buf; at->attr_nxt = at->edat_buf;
at->attr_end = at->edat_buf + u32at (pa, 0x30); edat_offset = u32at (pa, 0x30);
if (edat_offset >= n)
{
grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
return NULL;
}
at->attr_end = at->edat_buf + edat_offset;
pa_end = at->edat_buf + n;
} }
else else
{ {
at->attr_nxt = at->attr_end + u16at (pa, 0x14); at->attr_nxt = at->attr_end + res_attr_data_off (pa);
at->attr_end = at->attr_end + u32at (pa, 4); edat_offset = u32at (pa, 4);
if ((at->attr_end + edat_offset) >= (at->end))
{
grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
return NULL;
}
at->attr_end = at->attr_end + edat_offset;
pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
} }
at->flags |= GRUB_NTFS_AF_ALST; at->flags |= GRUB_NTFS_AF_ALST;
while (at->attr_nxt < at->attr_end)
/* From this point on pa_end is the end of the buffer */
at->end = pa_end;
if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
return NULL;
while (at->attr_nxt)
{ {
if ((*at->attr_nxt == attr) || (attr == 0)) if ((*at->attr_nxt == attr) || (attr == 0))
break; break;
at->attr_nxt += u16at (at->attr_nxt, 4);
nxt_offset = u16at (at->attr_nxt, 4);
at->attr_nxt += nxt_offset;
/*
* Stop and set attr_nxt to NULL when either the next offset is zero,
* or when the pointer is within four bytes of the end of the buffer
* since we could attempt to access attr_nxt + 4 bytes offset above to
* get the next 16-bit 'nxt_offset' value.
*/
if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))
at->attr_nxt = NULL;
} }
if (at->attr_nxt >= at->attr_end)
if ((at->attr_nxt + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE) >= at->attr_end || at->attr_nxt == NULL)
return NULL; return NULL;
if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA)) if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
@ -230,22 +513,42 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
at->flags |= GRUB_NTFS_AF_GPOS; at->flags |= GRUB_NTFS_AF_GPOS;
at->attr_cur = at->attr_nxt; at->attr_cur = at->attr_nxt;
pa = at->attr_cur; pa = at->attr_cur;
if ((pa >= pa_end) || (pa_end - pa < 0x18))
{
grub_error (GRUB_ERR_BAD_FS, "can\'t parse attribute list");
return NULL;
}
grub_set_unaligned32 ((char *) pa + 0x10, grub_set_unaligned32 ((char *) pa + 0x10,
grub_cpu_to_le32 (at->mft->data->mft_start)); grub_cpu_to_le32 (at->mft->data->mft_start));
grub_set_unaligned32 ((char *) pa + 0x14, grub_set_unaligned32 ((char *) pa + 0x14,
grub_cpu_to_le32 (at->mft->data->mft_start grub_cpu_to_le32 (at->mft->data->mft_start
+ 1)); + 1));
pa = at->attr_nxt + u16at (pa, 4); pa = at->attr_nxt + u16at (pa, 4);
while (pa < at->attr_end)
if (pa >= pa_end)
pa = NULL;
while (pa)
{ {
if (*pa != attr) if (*pa != attr)
break; break;
if ((pa >= pa_end) || (pa_end - pa < 0x18))
{
grub_error (GRUB_ERR_BAD_FS, "can\'t parse attribute list");
return NULL;
}
if (read_attr if (read_attr
(at, pa + 0x10, (at, pa + 0x10,
u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR), u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0)) at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
return NULL; return NULL;
pa += u16at (pa, 4); pa += u16at (pa, 4);
if (pa >= pa_end)
pa = NULL;
} }
at->attr_nxt = at->attr_cur; at->attr_nxt = at->attr_cur;
at->flags &= ~GRUB_NTFS_AF_GPOS; at->flags &= ~GRUB_NTFS_AF_GPOS;
@ -260,24 +563,31 @@ locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
grub_uint8_t attr) grub_uint8_t attr)
{ {
grub_uint8_t *pa; grub_uint8_t *pa;
grub_uint8_t *last_pa;
if (init_attr (at, mft) != GRUB_ERR_NONE)
return NULL;
init_attr (at, mft);
pa = find_attr (at, attr); pa = find_attr (at, attr);
if (pa == NULL) if (pa == NULL)
return NULL; return NULL;
if ((at->flags & GRUB_NTFS_AF_ALST) == 0) if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
{ {
/* Used to make sure we're not stuck in a loop. */
last_pa = NULL;
while (1) while (1)
{ {
pa = find_attr (at, attr); pa = find_attr (at, attr);
if (pa == NULL) if (pa == NULL || pa == last_pa)
break; break;
if (at->flags & GRUB_NTFS_AF_ALST) if (at->flags & GRUB_NTFS_AF_ALST)
return pa; return pa;
last_pa = pa;
} }
grub_errno = GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE;
free_attr (at); free_attr (at);
init_attr (at, mft); if (init_attr (at, mft) != GRUB_ERR_NONE)
return NULL;
pa = find_attr (at, attr); pa = find_attr (at, attr);
} }
return pa; return pa;
@ -329,7 +639,7 @@ retry:
goto retry; goto retry;
} }
} }
return grub_error (GRUB_ERR_BAD_FS, "run list overflown"); return grub_error (GRUB_ERR_BAD_FS, "run list overflow");
} }
ctx->curr_vcn = ctx->next_vcn; ctx->curr_vcn = ctx->next_vcn;
ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */ ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */
@ -368,6 +678,8 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
grub_disk_read_hook_t read_hook, void *read_hook_data) grub_disk_read_hook_t read_hook, void *read_hook_data)
{ {
struct grub_ntfs_rlst cc, *ctx; struct grub_ntfs_rlst cc, *ctx;
grub_uint8_t *end_ptr = (pa + len);
grub_uint16_t run_offset;
if (len == 0) if (len == 0)
return 0; return 0;
@ -383,13 +695,28 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
if (pa[8] == 0) if (pa[8] == 0)
{ {
if (ofs + len > u32at (pa, 0x10)) if (ofs + len > res_attr_data_len (pa))
return grub_error (GRUB_ERR_BAD_FS, "read out of range"); return grub_error (GRUB_ERR_BAD_FS, "read out of range");
grub_memcpy (dest, pa + u32at (pa, 0x14) + ofs, len);
if (res_attr_data_len (pa) > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
return grub_error (GRUB_ERR_BAD_FS, "resident attribute too large");
if (pa >= at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
if (res_attr_data_off (pa) + res_attr_data_len (pa) >
(grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) pa)
return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
grub_memcpy (dest, pa + res_attr_data_off (pa) + ofs, len);
return 0; return 0;
} }
ctx->cur_run = pa + u16at (pa, 0x20); run_offset = u16at (pa, 0x20);
if ((run_offset + pa) >= end_ptr || ((run_offset + pa) >= (at->end)))
return grub_error (GRUB_ERR_BAD_FS, "run offset out of range");
ctx->cur_run = pa + run_offset;
ctx->next_vcn = u32at (pa, 0x10); ctx->next_vcn = u32at (pa, 0x10);
ctx->curr_lcn = 0; ctx->curr_lcn = 0;
@ -452,6 +779,8 @@ read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
grub_uint8_t *pp; grub_uint8_t *pp;
grub_err_t ret; grub_err_t ret;
if (at == NULL || at->attr_cur == NULL)
return grub_error (GRUB_ERR_BAD_FS, "attribute not found");
save_cur = at->attr_cur; save_cur = at->attr_cur;
at->attr_nxt = at->attr_cur; at->attr_nxt = at->attr_cur;
attr = *at->attr_nxt; attr = *at->attr_nxt;
@ -468,14 +797,17 @@ read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
else else
vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR); vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR);
pa = at->attr_nxt + u16at (at->attr_nxt, 4); pa = at->attr_nxt + u16at (at->attr_nxt, 4);
while (pa < at->attr_end) if (validate_attribute (pa, at->attr_end) == false)
pa = NULL;
while (pa)
{ {
if (*pa != attr) if (*pa != attr)
break; break;
if (u32at (pa, 8) > vcn) if (u32at (pa, 8) > vcn)
break; break;
at->attr_nxt = pa; at->attr_nxt = pa;
pa += u16at (pa, 4); pa = next_attribute (pa, at->attr_end, true);
} }
} }
pp = find_attr (at, attr); pp = find_attr (at, attr);
@ -529,7 +861,7 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
(unsigned long long) mftno); (unsigned long long) mftno);
if (!pa[8]) if (!pa[8])
mft->size = u32at (pa, 0x10); mft->size = res_attr_data_len (pa);
else else
mft->size = u64at (pa, 0x30); mft->size = u64at (pa, 0x30);
@ -537,7 +869,7 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
mft->attr.attr_end = 0; /* Don't jump to attribute list */ mft->attr.attr_end = 0; /* Don't jump to attribute list */
} }
else else
init_attr (&mft->attr, mft); return init_attr (&mft->attr, mft);
return 0; return 0;
} }
@ -545,8 +877,11 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
static void static void
free_file (struct grub_ntfs_file *mft) free_file (struct grub_ntfs_file *mft)
{ {
free_attr (&mft->attr); if (mft)
grub_free (mft->buf); {
free_attr (&mft->attr);
grub_free (mft->buf);
}
} }
static char * static char *
@ -572,17 +907,21 @@ get_utf8 (grub_uint8_t *in, grub_size_t len)
} }
static int static int
list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos, list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos, grub_uint8_t *end_pos,
grub_fshelp_iterate_dir_hook_t hook, void *hook_data) grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
{ {
grub_uint8_t *np; grub_uint8_t *np;
int ns; int ns;
grub_uint16_t pos_incr;
while (1) while (1)
{ {
grub_uint8_t namespace; grub_uint8_t namespace;
char *ustr; char *ustr;
if ((pos >= end_pos) || (end_pos - pos < 0x52))
break;
if (pos[0xC] & 2) /* end signature */ if (pos[0xC] & 2) /* end signature */
break; break;
@ -590,6 +929,9 @@ list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos,
ns = *(np++); ns = *(np++);
namespace = *(np++); namespace = *(np++);
if (2 * ns > end_pos - pos - 0x52)
break;
/* /*
* Ignore files in DOS namespace, as they will reappear as Win32 * Ignore files in DOS namespace, as they will reappear as Win32
* names. * names.
@ -633,7 +975,12 @@ list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos,
grub_free (ustr); grub_free (ustr);
} }
pos += u16at (pos, 8); pos_incr = u16at (pos, 8);
if (pos_incr > 0)
pos += pos_incr;
else
return 0;
} }
return 0; return 0;
} }
@ -744,6 +1091,9 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
int ret = 0; int ret = 0;
grub_size_t bitmap_len; grub_size_t bitmap_len;
struct grub_ntfs_file *mft; struct grub_ntfs_file *mft;
/* Used to make sure we're not stuck in a loop. */
grub_uint8_t *last_pos = NULL;
grub_uint32_t tmp_len;
mft = (struct grub_ntfs_file *) dir; mft = (struct grub_ntfs_file *) dir;
@ -757,40 +1107,57 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
bmp = NULL; bmp = NULL;
at = &attr; at = &attr;
init_attr (at, mft); if (init_attr (at, mft) != GRUB_ERR_NONE)
return 0;
while (1) while (1)
{ {
cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT); cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
if (cur_pos == NULL) if (cur_pos == NULL || cur_pos == last_pos)
{ {
grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT"); grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
goto done; goto done;
} }
last_pos = cur_pos;
/* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */ /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
if ((u32at (cur_pos, 8) != 0x180400) || if ((u32at (cur_pos, 8) != 0x180400) ||
(u32at (cur_pos, 0x18) != 0x490024) || (u32at (cur_pos, 0x18) != 0x490024) ||
(u32at (cur_pos, 0x1C) != 0x300033)) (u32at (cur_pos, 0x1C) != 0x300033))
continue; continue;
cur_pos += u16at (cur_pos, 0x14); cur_pos += res_attr_data_off (cur_pos);
if(cur_pos >= at->end)
continue;
if (*cur_pos != 0x30) /* Not filename index */ if (*cur_pos != 0x30) /* Not filename index */
continue; continue;
break; break;
} }
cur_pos += 0x10; /* Skip index root */ cur_pos += 0x10; /* Skip index root */
ret = list_file (mft, cur_pos + u16at (cur_pos, 0), hook, hook_data); ret = list_file (mft, cur_pos + u16at (cur_pos, 0),
at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
hook, hook_data);
if (ret) if (ret)
goto done; goto done;
bitmap = NULL; bitmap = NULL;
bitmap_len = 0; bitmap_len = 0;
free_attr (at); free_attr (at);
/* No need to check errors here, as it will already be fine */
init_attr (at, mft); init_attr (at, mft);
last_pos = NULL;
while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL) while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL)
{ {
int ofs; int ofs;
if (cur_pos == last_pos)
{
grub_error (GRUB_ERR_BAD_FS, "bitmap attribute loop");
goto done;
}
last_pos = cur_pos;
ofs = cur_pos[0xA]; ofs = cur_pos[0xA];
/* Namelen=4, Name="$I30" */ /* Namelen=4, Name="$I30" */
if ((cur_pos[9] == 4) && if ((cur_pos[9] == 4) &&
@ -799,7 +1166,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
{ {
int is_resident = (cur_pos[8] == 0); int is_resident = (cur_pos[8] == 0);
bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) : bitmap_len = ((is_resident) ? res_attr_data_len (cur_pos) :
u32at (cur_pos, 0x28)); u32at (cur_pos, 0x28));
bmp = grub_malloc (bitmap_len); bmp = grub_malloc (bitmap_len);
@ -808,7 +1175,26 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
if (is_resident) if (is_resident)
{ {
grub_memcpy (bmp, cur_pos + u16at (cur_pos, 0x14), if (bitmap_len > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
{
grub_error (GRUB_ERR_BAD_FS, "resident bitmap too large");
goto done;
}
if (cur_pos >= at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
{
grub_error (GRUB_ERR_BAD_FS, "resident bitmap out of range");
goto done;
}
if (res_attr_data_off (cur_pos) + res_attr_data_len (cur_pos) >
(grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) cur_pos)
{
grub_error (GRUB_ERR_BAD_FS, "resident bitmap out of range");
goto done;
}
grub_memcpy (bmp, cur_pos + res_attr_data_off (cur_pos),
bitmap_len); bitmap_len);
} }
else else
@ -819,7 +1205,15 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
"fails to read non-resident $BITMAP"); "fails to read non-resident $BITMAP");
goto done; goto done;
} }
bitmap_len = u32at (cur_pos, 0x30); tmp_len = u32at (cur_pos, 0x30);
if (tmp_len <= bitmap_len)
bitmap_len = tmp_len;
else
{
grub_error (GRUB_ERR_BAD_FS,
"bitmap len too large for non-resident $BITMAP");
goto done;
}
} }
bitmap = bmp; bitmap = bmp;
@ -828,6 +1222,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
} }
free_attr (at); free_attr (at);
last_pos = NULL;
cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION); cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
while (cur_pos != NULL) while (cur_pos != NULL)
{ {
@ -837,6 +1232,9 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
(u32at (cur_pos, 0x44) == 0x300033)) (u32at (cur_pos, 0x44) == 0x300033))
break; break;
cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION); cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
if (cur_pos == last_pos)
break;
last_pos = cur_pos;
} }
if ((!cur_pos) && (bitmap)) if ((!cur_pos) && (bitmap))
@ -866,6 +1264,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
(const grub_uint8_t *) "INDX"))) (const grub_uint8_t *) "INDX")))
goto done; goto done;
ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)], ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)],
indx + (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
hook, hook_data); hook, hook_data);
if (ret) if (ret)
goto done; goto done;
@ -1131,6 +1530,7 @@ grub_ntfs_label (grub_device_t device, char **label)
struct grub_ntfs_data *data = 0; struct grub_ntfs_data *data = 0;
struct grub_fshelp_node *mft = 0; struct grub_fshelp_node *mft = 0;
grub_uint8_t *pa; grub_uint8_t *pa;
grub_err_t err;
grub_dl_ref (my_mod); grub_dl_ref (my_mod);
@ -1156,15 +1556,35 @@ grub_ntfs_label (grub_device_t device, char **label)
goto fail; goto fail;
} }
init_attr (&mft->attr, mft); err = init_attr (&mft->attr, mft);
if (err != GRUB_ERR_NONE)
return err;
pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME); pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
if (pa == NULL || pa >= mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))
{
grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
goto fail;
}
if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa < 0x16)
{
grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
goto fail;
}
if ((pa) && (pa[8] == 0) && (res_attr_data_len (pa)))
{ {
int len; int len;
len = u32at (pa, 0x10) / 2; len = res_attr_data_len (pa) / 2;
pa += u16at (pa, 0x14); pa += res_attr_data_off (pa);
*label = get_utf8 (pa, len); if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa >= 2 * len &&
pa >= mft->buf && (pa + len < (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))))
*label = get_utf8 (pa, len);
else
grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
} }
fail: fail:
@ -1231,11 +1651,16 @@ static struct grub_fs grub_ntfs_fs =
GRUB_MOD_INIT (ntfs) GRUB_MOD_INIT (ntfs)
{ {
grub_fs_register (&grub_ntfs_fs); if (!grub_is_lockdown ())
{
grub_ntfs_fs.mod = mod;
grub_fs_register (&grub_ntfs_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI (ntfs) GRUB_MOD_FINI (ntfs)
{ {
grub_fs_unregister (&grub_ntfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_ntfs_fs);
} }

View file

@ -22,6 +22,7 @@
#include <grub/disk.h> #include <grub/disk.h>
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/ntfs.h> #include <grub/ntfs.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -29,7 +30,7 @@ static grub_err_t
decomp_nextvcn (struct grub_ntfs_comp *cc) decomp_nextvcn (struct grub_ntfs_comp *cc)
{ {
if (cc->comp_head >= cc->comp_tail) if (cc->comp_head >= cc->comp_tail)
return grub_error (GRUB_ERR_BAD_FS, "compression block overflown"); return grub_error (GRUB_ERR_BAD_FS, "compression block overflow");
if (grub_disk_read if (grub_disk_read
(cc->disk, (cc->disk,
(cc->comp_table[cc->comp_head].next_lcn - (cc->comp_table[cc->comp_head].next_lcn -
@ -310,6 +311,7 @@ ntfscomp (grub_uint8_t *dest, grub_disk_addr_t ofs,
{ {
grub_err_t ret; grub_err_t ret;
grub_disk_addr_t vcn; grub_disk_addr_t vcn;
int log_sz;
if (ctx->attr->sbuf) if (ctx->attr->sbuf)
{ {
@ -349,7 +351,12 @@ ntfscomp (grub_uint8_t *dest, grub_disk_addr_t ofs,
} }
ctx->comp.comp_head = ctx->comp.comp_tail = 0; ctx->comp.comp_head = ctx->comp.comp_tail = 0;
ctx->comp.cbuf = grub_malloc (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR)); if (grub_add (ctx->comp.log_spc, GRUB_NTFS_BLK_SHR, &log_sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("compression buffer size overflow"));
return 0;
}
ctx->comp.cbuf = grub_malloc (1 << log_sz);
if (!ctx->comp.cbuf) if (!ctx->comp.cbuf)
return 0; return 0;

View file

@ -52,6 +52,7 @@ read_number (const char *str, grub_size_t size)
GRUB_MOD_INIT (odc) GRUB_MOD_INIT (odc)
{ {
grub_cpio_fs.mod = mod;
grub_fs_register (&grub_cpio_fs); grub_fs_register (&grub_cpio_fs);
} }

View file

@ -192,6 +192,7 @@ static struct grub_fs grub_procfs_fs =
GRUB_MOD_INIT (procfs) GRUB_MOD_INIT (procfs)
{ {
grub_procfs_fs.mod = mod;
grub_disk_dev_register (&grub_procfs_dev); grub_disk_dev_register (&grub_procfs_dev);
grub_fs_register (&grub_procfs_fs); grub_fs_register (&grub_procfs_fs);
} }

View file

@ -39,6 +39,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -1417,11 +1418,16 @@ static struct grub_fs grub_reiserfs_fs =
GRUB_MOD_INIT(reiserfs) GRUB_MOD_INIT(reiserfs)
{ {
grub_fs_register (&grub_reiserfs_fs); if (!grub_is_lockdown ())
{
grub_reiserfs_fs.mod = mod;
grub_fs_register (&grub_reiserfs_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI(reiserfs) GRUB_MOD_FINI(reiserfs)
{ {
grub_fs_unregister (&grub_reiserfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_reiserfs_fs);
} }

View file

@ -23,6 +23,7 @@
#include <grub/disk.h> #include <grub/disk.h>
#include <grub/fs.h> #include <grub/fs.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -475,10 +476,15 @@ static struct grub_fs grub_romfs_fs =
GRUB_MOD_INIT(romfs) GRUB_MOD_INIT(romfs)
{ {
grub_fs_register (&grub_romfs_fs); if (!grub_is_lockdown ())
{
grub_romfs_fs.mod = mod;
grub_fs_register (&grub_romfs_fs);
}
} }
GRUB_MOD_FINI(romfs) GRUB_MOD_FINI(romfs)
{ {
grub_fs_unregister (&grub_romfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_romfs_fs);
} }

View file

@ -26,6 +26,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/lockdown.h>
#include <grub/safemath.h> #include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -428,6 +429,9 @@ grub_sfs_mount (grub_disk_t disk)
- 24 /* offsetof (struct grub_sfs_objc, objects) */ - 24 /* offsetof (struct grub_sfs_objc, objects) */
- 25); /* offsetof (struct grub_sfs_obj, filename) */ - 25); /* offsetof (struct grub_sfs_obj, filename) */
data->label = grub_zalloc (max_len + 1); data->label = grub_zalloc (max_len + 1);
if (data->label == NULL)
goto fail;
grub_strncpy (data->label, (char *) rootobjc->objects[0].filename, max_len); grub_strncpy (data->label, (char *) rootobjc->objects[0].filename, max_len);
grub_free (rootobjc_data); grub_free (rootobjc_data);
@ -779,11 +783,16 @@ static struct grub_fs grub_sfs_fs =
GRUB_MOD_INIT(sfs) GRUB_MOD_INIT(sfs)
{ {
grub_fs_register (&grub_sfs_fs); if (!grub_is_lockdown ())
{
grub_sfs_fs.mod = mod;
grub_fs_register (&grub_sfs_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI(sfs) GRUB_MOD_FINI(sfs)
{ {
grub_fs_unregister (&grub_sfs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_sfs_fs);
} }

View file

@ -460,11 +460,11 @@ grub_squash_read_symlink (grub_fshelp_node_t node)
{ {
char *ret; char *ret;
grub_err_t err; grub_err_t err;
grub_size_t sz; grub_uint32_t sz;
if (grub_add (grub_le_to_cpu32 (node->ino.symlink.namelen), 1, &sz)) if (grub_add (grub_le_to_cpu32 (node->ino.symlink.namelen), 1, &sz))
{ {
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink name length overflow"));
return NULL; return NULL;
} }
@ -580,6 +580,7 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
struct grub_squash_dirent di; struct grub_squash_dirent di;
struct grub_squash_inode ino; struct grub_squash_inode ino;
grub_size_t sz; grub_size_t sz;
grub_uint16_t nlen;
err = read_chunk (dir->data, &di, sizeof (di), err = read_chunk (dir->data, &di, sizeof (di),
grub_le_to_cpu64 (dir->data->sb.diroffset) grub_le_to_cpu64 (dir->data->sb.diroffset)
@ -595,7 +596,12 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
if (err) if (err)
return 0; return 0;
buf = grub_malloc (grub_le_to_cpu16 (di.namelen) + 2); if (grub_add (grub_le_to_cpu16 (di.namelen), 2, &nlen))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow"));
return 0;
}
buf = grub_malloc (nlen);
if (!buf) if (!buf)
return 0; return 0;
err = read_chunk (dir->data, buf, err = read_chunk (dir->data, buf,
@ -816,10 +822,10 @@ direct_read (struct grub_squash_data *data,
break; break;
} }
total_blocks = ((total_size + data->blksz - 1) >> data->log2_blksz); total_blocks = ((total_size + data->blksz - 1) >> data->log2_blksz);
ino->block_sizes = grub_malloc (total_blocks ino->block_sizes = grub_calloc (total_blocks,
* sizeof (ino->block_sizes[0])); sizeof (ino->block_sizes[0]));
ino->cumulated_block_sizes = grub_malloc (total_blocks ino->cumulated_block_sizes = grub_calloc (total_blocks,
* sizeof (ino->cumulated_block_sizes[0])); sizeof (ino->cumulated_block_sizes[0]));
if (!ino->block_sizes || !ino->cumulated_block_sizes) if (!ino->block_sizes || !ino->cumulated_block_sizes)
{ {
grub_free (ino->block_sizes); grub_free (ino->block_sizes);
@ -1044,6 +1050,7 @@ static struct grub_fs grub_squash_fs =
GRUB_MOD_INIT(squash4) GRUB_MOD_INIT(squash4)
{ {
grub_squash_fs.mod = mod;
grub_fs_register (&grub_squash_fs); grub_fs_register (&grub_squash_fs);
} }

View file

@ -25,6 +25,7 @@
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -76,8 +77,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
{ {
struct head hd; struct head hd;
int reread = 0, have_longname = 0, have_longlink = 0; int reread = 0, have_longname = 0, have_longlink = 0;
grub_size_t sz;
data->hofs = data->next_hofs; data->hofs = data->next_hofs;
*name = NULL;
for (reread = 0; reread < 3; reread++) for (reread = 0; reread < 3; reread++)
{ {
@ -96,8 +99,13 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
if (hd.typeflag == 'L') if (hd.typeflag == 'L')
{ {
grub_err_t err; grub_err_t err;
grub_size_t namesize = read_number (hd.size, sizeof (hd.size)); grub_size_t namesize;
*name = grub_malloc (namesize + 1);
if (grub_cast (read_number (hd.size, sizeof (hd.size)), &namesize) ||
grub_add (namesize, 1, &sz))
return grub_error (GRUB_ERR_BAD_FS, N_("name size overflow"));
*name = grub_malloc (sz);
if (*name == NULL) if (*name == NULL)
return grub_errno; return grub_errno;
err = grub_disk_read (data->disk, 0, err = grub_disk_read (data->disk, 0,
@ -116,16 +124,21 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
if (hd.typeflag == 'K') if (hd.typeflag == 'K')
{ {
grub_err_t err; grub_err_t err;
grub_size_t linksize = read_number (hd.size, sizeof (hd.size)); grub_size_t linksize;
if (data->linkname_alloc < linksize + 1)
if (grub_cast (read_number (hd.size, sizeof (hd.size)), &linksize) ||
grub_add (linksize, 1, &sz))
return grub_error (GRUB_ERR_BAD_FS, N_("link size overflow"));
if (data->linkname_alloc < sz)
{ {
char *n; char *n;
n = grub_calloc (2, linksize + 1); n = grub_calloc (2, sz);
if (!n) if (!n)
return grub_errno; return grub_errno;
grub_free (data->linkname); grub_free (data->linkname);
data->linkname = n; data->linkname = n;
data->linkname_alloc = 2 * (linksize + 1); data->linkname_alloc = 2 * (sz);
} }
err = grub_disk_read (data->disk, 0, err = grub_disk_read (data->disk, 0,
@ -148,7 +161,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
while (extra_size < sizeof (hd.prefix) while (extra_size < sizeof (hd.prefix)
&& hd.prefix[extra_size]) && hd.prefix[extra_size])
extra_size++; extra_size++;
*name = grub_malloc (sizeof (hd.name) + extra_size + 2);
if (grub_add (sizeof (hd.name) + 2, extra_size, &sz))
return grub_error (GRUB_ERR_BAD_FS, N_("long name size overflow"));
*name = grub_malloc (sz);
if (*name == NULL) if (*name == NULL)
return grub_errno; return grub_errno;
if (hd.prefix[0]) if (hd.prefix[0])
@ -160,15 +176,22 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
(*name)[extra_size + sizeof (hd.name)] = 0; (*name)[extra_size + sizeof (hd.name)] = 0;
} }
data->size = read_number (hd.size, sizeof (hd.size)); if (grub_cast (read_number (hd.size, sizeof (hd.size)), &data->size))
return grub_error (GRUB_ERR_BAD_FS, N_("data size overflow"));
data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE; data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE;
data->next_hofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) & data->next_hofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) &
~(GRUB_DISK_SECTOR_SIZE - 1)); ~(GRUB_DISK_SECTOR_SIZE - 1));
if (mtime) if (mtime)
*mtime = read_number (hd.mtime, sizeof (hd.mtime)); {
if (grub_cast (read_number (hd.mtime, sizeof (hd.mtime)), mtime))
return grub_error (GRUB_ERR_BAD_FS, N_("mtime overflow"));
}
if (mode) if (mode)
{ {
*mode = read_number (hd.mode, sizeof (hd.mode)); if (grub_cast (read_number (hd.mode, sizeof (hd.mode)), mode))
return grub_error (GRUB_ERR_BAD_FS, N_("mode overflow"));
switch (hd.typeflag) switch (hd.typeflag)
{ {
/* Hardlink. */ /* Hardlink. */
@ -202,6 +225,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
} }
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
if (*name == NULL)
return grub_error (GRUB_ERR_BAD_FS, "invalid tar archive");
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
@ -336,6 +363,7 @@ static struct grub_fs grub_cpio_fs = {
GRUB_MOD_INIT (tar) GRUB_MOD_INIT (tar)
{ {
grub_cpio_fs.mod = mod;
grub_fs_register (&grub_cpio_fs); grub_fs_register (&grub_cpio_fs);
} }

View file

@ -27,6 +27,7 @@
#include <grub/fshelp.h> #include <grub/fshelp.h>
#include <grub/charset.h> #include <grub/charset.h>
#include <grub/datetime.h> #include <grub/datetime.h>
#include <grub/lockdown.h>
#include <grub/udf.h> #include <grub/udf.h>
#include <grub/safemath.h> #include <grub/safemath.h>
@ -1455,11 +1456,16 @@ static struct grub_fs grub_udf_fs = {
GRUB_MOD_INIT (udf) GRUB_MOD_INIT (udf)
{ {
grub_fs_register (&grub_udf_fs); if (!grub_is_lockdown ())
{
grub_udf_fs.mod = mod;
grub_fs_register (&grub_udf_fs);
}
my_mod = mod; my_mod = mod;
} }
GRUB_MOD_FINI (udf) GRUB_MOD_FINI (udf)
{ {
grub_fs_unregister (&grub_udf_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_udf_fs);
} }

View file

@ -25,6 +25,7 @@
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/lockdown.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@ -463,7 +464,7 @@ grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino)
/* Check against zero is paylindromic, no need to swap. */ /* Check against zero is paylindromic, no need to swap. */
if (data->inode.nblocks == 0 if (data->inode.nblocks == 0
&& INODE_SIZE (data) <= sizeof (data->inode.symlink)) && INODE_SIZE (data) <= sizeof (data->inode.symlink))
grub_strcpy (symlink, (char *) data->inode.symlink); grub_strlcpy (symlink, (char *) data->inode.symlink, sz);
else else
{ {
if (grub_ufs_read_file (data, 0, 0, 0, sz, symlink) < 0) if (grub_ufs_read_file (data, 0, 0, 0, sz, symlink) < 0)
@ -899,7 +900,11 @@ GRUB_MOD_INIT(ufs1)
#endif #endif
#endif #endif
{ {
grub_fs_register (&grub_ufs_fs); if (!grub_is_lockdown ())
{
grub_ufs_fs.mod = mod;
grub_fs_register (&grub_ufs_fs);
}
my_mod = mod; my_mod = mod;
} }
@ -913,6 +918,7 @@ GRUB_MOD_FINI(ufs1)
#endif #endif
#endif #endif
{ {
grub_fs_unregister (&grub_ufs_fs); if (!grub_is_lockdown ())
grub_fs_unregister (&grub_ufs_fs);
} }

View file

@ -79,6 +79,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
/* Inode flags2 flags */ /* Inode flags2 flags */
#define XFS_DIFLAG2_BIGTIME_BIT 3 #define XFS_DIFLAG2_BIGTIME_BIT 3
#define XFS_DIFLAG2_BIGTIME (1 << XFS_DIFLAG2_BIGTIME_BIT) #define XFS_DIFLAG2_BIGTIME (1 << XFS_DIFLAG2_BIGTIME_BIT)
#define XFS_DIFLAG2_NREXT64_BIT 4
#define XFS_DIFLAG2_NREXT64 (1 << XFS_DIFLAG2_NREXT64_BIT)
/* incompat feature flags */ /* incompat feature flags */
#define XFS_SB_FEAT_INCOMPAT_FTYPE (1 << 0) /* filetype in dirent */ #define XFS_SB_FEAT_INCOMPAT_FTYPE (1 << 0) /* filetype in dirent */
@ -86,6 +88,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
#define XFS_SB_FEAT_INCOMPAT_META_UUID (1 << 2) /* metadata UUID */ #define XFS_SB_FEAT_INCOMPAT_META_UUID (1 << 2) /* metadata UUID */
#define XFS_SB_FEAT_INCOMPAT_BIGTIME (1 << 3) /* large timestamps */ #define XFS_SB_FEAT_INCOMPAT_BIGTIME (1 << 3) /* large timestamps */
#define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4) /* needs xfs_repair */ #define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4) /* needs xfs_repair */
#define XFS_SB_FEAT_INCOMPAT_NREXT64 (1 << 5) /* large extent counters */
#define XFS_SB_FEAT_INCOMPAT_EXCHRANGE (1 << 6) /* exchangerange supported */
#define XFS_SB_FEAT_INCOMPAT_PARENT (1 << 7) /* parent pointers */
#define XFS_SB_FEAT_INCOMPAT_METADIR (1 << 8) /* metadata dir tree */
/* /*
* Directory entries with ftype are explicitly handled by GRUB code. * Directory entries with ftype are explicitly handled by GRUB code.
@ -95,13 +101,26 @@ GRUB_MOD_LICENSE ("GPLv3+");
* *
* We do not currently verify metadata UUID, so it is safe to read filesystems * We do not currently verify metadata UUID, so it is safe to read filesystems
* with the XFS_SB_FEAT_INCOMPAT_META_UUID feature. * with the XFS_SB_FEAT_INCOMPAT_META_UUID feature.
*
* We do not currently replay the log, so it is safe to read filesystems
* with the XFS_SB_FEAT_INCOMPAT_EXCHRANGE feature.
*
* We do not currently read directory parent pointers, so it is safe to read
* filesystems with the XFS_SB_FEAT_INCOMPAT_PARENT feature.
*
* We do not currently look at realtime or quota metadata, so it is safe to
* read filesystems with the XFS_SB_FEAT_INCOMPAT_METADIR feature.
*/ */
#define XFS_SB_FEAT_INCOMPAT_SUPPORTED \ #define XFS_SB_FEAT_INCOMPAT_SUPPORTED \
(XFS_SB_FEAT_INCOMPAT_FTYPE | \ (XFS_SB_FEAT_INCOMPAT_FTYPE | \
XFS_SB_FEAT_INCOMPAT_SPINODES | \ XFS_SB_FEAT_INCOMPAT_SPINODES | \
XFS_SB_FEAT_INCOMPAT_META_UUID | \ XFS_SB_FEAT_INCOMPAT_META_UUID | \
XFS_SB_FEAT_INCOMPAT_BIGTIME | \ XFS_SB_FEAT_INCOMPAT_BIGTIME | \
XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR) XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR | \
XFS_SB_FEAT_INCOMPAT_NREXT64 | \
XFS_SB_FEAT_INCOMPAT_EXCHRANGE | \
XFS_SB_FEAT_INCOMPAT_PARENT | \
XFS_SB_FEAT_INCOMPAT_METADIR)
struct grub_xfs_sblock struct grub_xfs_sblock
{ {
@ -203,7 +222,8 @@ struct grub_xfs_inode
grub_uint16_t mode; grub_uint16_t mode;
grub_uint8_t version; grub_uint8_t version;
grub_uint8_t format; grub_uint8_t format;
grub_uint8_t unused2[26]; grub_uint8_t unused2[18];
grub_uint64_t nextents_big;
grub_uint64_t atime; grub_uint64_t atime;
grub_uint64_t mtime; grub_uint64_t mtime;
grub_uint64_t ctime; grub_uint64_t ctime;
@ -223,6 +243,12 @@ struct grub_xfs_inode
/* Size of struct grub_xfs_inode v2, up to unused4 member included. */ /* Size of struct grub_xfs_inode v2, up to unused4 member included. */
#define XFS_V2_INODE_SIZE (XFS_V3_INODE_SIZE - 76) #define XFS_V2_INODE_SIZE (XFS_V3_INODE_SIZE - 76)
struct grub_xfs_dir_leaf_entry
{
grub_uint32_t hashval;
grub_uint32_t address;
} GRUB_PACKED;
struct grub_xfs_dirblock_tail struct grub_xfs_dirblock_tail
{ {
grub_uint32_t leaf_count; grub_uint32_t leaf_count;
@ -316,6 +342,8 @@ static int grub_xfs_sb_valid(struct grub_xfs_data *data)
} }
return 1; return 1;
} }
grub_error (GRUB_ERR_BAD_FS, "unsupported XFS filesystem version");
return 0; return 0;
} }
@ -539,11 +567,26 @@ get_fsb (const void *keys, int idx)
return grub_be_to_cpu64 (grub_get_unaligned64 (p)); return grub_be_to_cpu64 (grub_get_unaligned64 (p));
} }
static int
grub_xfs_inode_has_large_extent_counts (const struct grub_xfs_inode *inode)
{
return inode->version >= 3 &&
(inode->flags2 & grub_cpu_to_be64_compile_time (XFS_DIFLAG2_NREXT64));
}
static grub_uint64_t
grub_xfs_get_inode_nextents (struct grub_xfs_inode *inode)
{
return (grub_xfs_inode_has_large_extent_counts (inode)) ?
grub_be_to_cpu64 (inode->nextents_big) :
grub_be_to_cpu32 (inode->nextents);
}
static grub_disk_addr_t static grub_disk_addr_t
grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
{ {
struct grub_xfs_btree_node *leaf = 0; struct grub_xfs_btree_node *leaf = 0;
int ex, nrec; grub_uint64_t ex, nrec;
struct grub_xfs_extent *exts; struct grub_xfs_extent *exts;
grub_uint64_t ret = 0; grub_uint64_t ret = 0;
@ -568,7 +611,18 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
/ (2 * sizeof (grub_uint64_t)); / (2 * sizeof (grub_uint64_t));
do do
{ {
int i; grub_uint64_t i;
grub_addr_t keys_end, data_end;
if (grub_mul (sizeof (grub_uint64_t), nrec, &keys_end) ||
grub_add ((grub_addr_t) keys, keys_end, &keys_end) ||
grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) ||
keys_end > data_end)
{
grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS root keys");
grub_free (leaf);
return 0;
}
for (i = 0; i < nrec; i++) for (i = 0; i < nrec; i++)
{ {
@ -615,7 +669,7 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
grub_addr_t exts_end = 0; grub_addr_t exts_end = 0;
grub_addr_t data_end = 0; grub_addr_t data_end = 0;
nrec = grub_be_to_cpu32 (node->inode.nextents); nrec = grub_xfs_get_inode_nextents (&node->inode);
exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode); exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
if (grub_mul (sizeof (struct grub_xfs_extent), nrec, &exts_end) || if (grub_mul (sizeof (struct grub_xfs_extent), nrec, &exts_end) ||
@ -679,6 +733,7 @@ static char *
grub_xfs_read_symlink (grub_fshelp_node_t node) grub_xfs_read_symlink (grub_fshelp_node_t node)
{ {
grub_ssize_t size = grub_be_to_cpu64 (node->inode.size); grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
grub_size_t sz;
if (size < 0) if (size < 0)
{ {
@ -700,7 +755,12 @@ grub_xfs_read_symlink (grub_fshelp_node_t node)
if (node->data->hascrc) if (node->data->hascrc)
off = 56; off = 56;
symlink = grub_malloc (size + 1); if (grub_add (size, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink size overflow"));
return 0;
}
symlink = grub_malloc (sz);
if (!symlink) if (!symlink)
return 0; return 0;
@ -750,8 +810,15 @@ static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
{ {
struct grub_fshelp_node *fdiro; struct grub_fshelp_node *fdiro;
grub_err_t err; grub_err_t err;
grub_size_t sz;
fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1); if (grub_add (grub_xfs_fshelp_size(ctx->diro->data), 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory data size overflow"));
grub_print_error ();
return 0;
}
fdiro = grub_malloc (sz);
if (!fdiro) if (!fdiro)
{ {
grub_print_error (); grub_print_error ();
@ -810,14 +877,19 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
if (iterate_dir_call_hook (parent, "..", &ctx)) if (iterate_dir_call_hook (parent, "..", &ctx))
return 1; return 1;
for (i = 0; i < head->count; i++) for (i = 0; i < head->count &&
(grub_uint8_t *) de < ((grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data)); i++)
{ {
grub_uint64_t ino; grub_uint64_t ino;
grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de); grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
grub_uint8_t c; grub_uint8_t c;
if ((inopos + (smallino ? 4 : 8)) > (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data)) if ((inopos + (smallino ? 4 : 8)) > (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data))
return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode"); {
grub_error (GRUB_ERR_BAD_FS, "invalid XFS inode");
return 0;
}
/* inopos might be unaligned. */ /* inopos might be unaligned. */
if (smallino) if (smallino)
@ -845,10 +917,6 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
de->name[de->len] = c; de->name[de->len] = c;
de = grub_xfs_inline_next_de(dir->data, head, de); de = grub_xfs_inline_next_de(dir->data, head, de);
if ((grub_uint8_t *) de >= (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data))
return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry");
} }
break; break;
} }
@ -877,9 +945,9 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
{ {
struct grub_xfs_dir2_entry *direntry = struct grub_xfs_dir2_entry *direntry =
grub_xfs_first_de(dir->data, dirblock); grub_xfs_first_de(dir->data, dirblock);
int entries; int entries = -1;
struct grub_xfs_dirblock_tail *tail = char *end = dirblock + dirblk_size;
grub_xfs_dir_tail(dir->data, dirblock); grub_uint32_t magic;
numread = grub_xfs_read_file (dir, 0, 0, numread = grub_xfs_read_file (dir, 0, 0,
blk << dirblk_log2, blk << dirblk_log2,
@ -890,14 +958,36 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
return 0; return 0;
} }
entries = (grub_be_to_cpu32 (tail->leaf_count) /*
- grub_be_to_cpu32 (tail->leaf_stale)); * If this data block isn't actually part of the extent list then
* grub_xfs_read_file() returns a block of zeros. So, if the magic
if (!entries) * number field is all zeros then this block should be skipped.
*/
magic = *(grub_uint32_t *)(void *) dirblock;
if (!magic)
continue; continue;
/*
* Leaf and tail information are only in the data block if the number
* of extents is 1.
*/
if (grub_xfs_get_inode_nextents (&dir->inode) == 1)
{
struct grub_xfs_dirblock_tail *tail = grub_xfs_dir_tail (dir->data, dirblock);
end = (char *) tail;
/* Subtract the space used by leaf nodes. */
end -= grub_be_to_cpu32 (tail->leaf_count) * sizeof (struct grub_xfs_dir_leaf_entry);
entries = grub_be_to_cpu32 (tail->leaf_count) - grub_be_to_cpu32 (tail->leaf_stale);
if (!entries)
continue;
}
/* Iterate over all entries within this block. */ /* Iterate over all entries within this block. */
while ((char *)direntry < (char *)tail) while ((char *) direntry < (char *) end)
{ {
grub_uint8_t *freetag; grub_uint8_t *freetag;
char *filename; char *filename;
@ -917,8 +1007,11 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
} }
filename = (char *)(direntry + 1); filename = (char *)(direntry + 1);
if (filename + direntry->len - 1 > (char *) tail) if (filename + direntry->len + 1 > (char *) end)
return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry"); {
grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry");
return 0;
}
/* The byte after the filename is for the filetype, padding, or /* The byte after the filename is for the filetype, padding, or
tag, which is not used by GRUB. So it can be overwritten. */ tag, which is not used by GRUB. So it can be overwritten. */
@ -931,11 +1024,17 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
return 1; return 1;
} }
/* Check if last direntry in this block is /*
reached. */ * The expected number of directory entries is only tracked for the
entries--; * single extent case.
if (!entries) */
break; if (grub_xfs_get_inode_nextents (&dir->inode) == 1)
{
/* Check if last direntry in this block is reached. */
entries--;
if (!entries)
break;
}
/* Select the next directory entry. */ /* Select the next directory entry. */
direntry = grub_xfs_next_de(dir->data, direntry); direntry = grub_xfs_next_de(dir->data, direntry);
@ -1006,7 +1105,7 @@ grub_xfs_mount (grub_disk_t disk)
return data; return data;
fail: fail:
if (grub_errno == GRUB_ERR_OUT_OF_RANGE) if (grub_errno == GRUB_ERR_OUT_OF_RANGE || grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem"); grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
grub_free (data); grub_free (data);
@ -1240,6 +1339,7 @@ static struct grub_fs grub_xfs_fs =
GRUB_MOD_INIT(xfs) GRUB_MOD_INIT(xfs)
{ {
grub_xfs_fs.mod = mod;
grub_fs_register (&grub_xfs_fs); grub_fs_register (&grub_xfs_fs);
my_mod = mod; my_mod = mod;
} }

Some files were not shown because too many files have changed in this diff Show more