]> git.ipfire.org Git - thirdparty/grub.git/log
thirdparty/grub.git
4 years agoloader/xnu: Fix memory leak
Darren Kenny [Thu, 26 Nov 2020 12:53:10 +0000 (12:53 +0000)] 
loader/xnu: Fix memory leak

The code here is finished with the memory stored in name, but it only
frees it if there curvalue is valid, while it could actually free it
regardless.

The fix is a simple relocation of the grub_free() to before the test
of curvalue.

Fixes: CID 96646
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoloader/bsd: Check for NULL arg up-front
Darren Kenny [Tue, 8 Dec 2020 21:47:13 +0000 (21:47 +0000)] 
loader/bsd: Check for NULL arg up-front

The code in the next block suggests that it is possible for .set to be
true but .arg may still be NULL.

This code assumes that it is never NULL, yet later is testing if it is
NULL - that is inconsistent.

So we should check first if .arg is not NULL, and remove this check that
is being flagged by Coverity since it is no longer required.

Fixes: CID 292471
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agogfxmenu/gui_list: Remove code that coverity is flagging as dead
Darren Kenny [Mon, 7 Dec 2020 14:44:47 +0000 (14:44 +0000)] 
gfxmenu/gui_list: Remove code that coverity is flagging as dead

The test of value for NULL before calling grub_strdup() is not required,
since the if condition prior to this has already tested for value being
NULL and cannot reach this code if it is.

Fixes: CID 73659
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agovideo/readers/jpeg: Test for an invalid next marker reference from a jpeg file
Darren Kenny [Fri, 4 Dec 2020 15:39:00 +0000 (15:39 +0000)] 
video/readers/jpeg: Test for an invalid next marker reference from a jpeg file

While it may never happen, and potentially could be caught at the end of
the function, it is worth checking up front for a bad reference to the
next marker just in case of a maliciously crafted file being provided.

Fixes: CID 73694
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agovideo/fb/video_fb: Fix possible integer overflow
Darren Kenny [Fri, 4 Dec 2020 14:51:30 +0000 (14:51 +0000)] 
video/fb/video_fb: Fix possible integer overflow

It is minimal possibility that the values being used here will overflow.
So, change the code to use the safemath function grub_mul() to ensure
that doesn't happen.

Fixes: CID 73761
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agovideo/fb/video_fb: Fix multiple integer overflows
Darren Kenny [Wed, 4 Nov 2020 14:43:44 +0000 (14:43 +0000)] 
video/fb/video_fb: Fix multiple integer overflows

The calculation of the unsigned 64-bit value is being generated by
multiplying 2, signed or unsigned, 32-bit integers which may overflow
before promotion to unsigned 64-bit. Fix all of them.

Fixes: CID 73703, CID 73767, CID 73833
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agovideo/fb/fbfill: Fix potential integer overflow
Darren Kenny [Wed, 4 Nov 2020 15:10:51 +0000 (15:10 +0000)] 
video/fb/fbfill: Fix potential integer overflow

The multiplication of 2 unsigned 32-bit integers may overflow before
promotion to unsigned 64-bit. We should ensure that the multiplication
is done with overflow detection. Additionally, use grub_sub() for
subtraction.

Fixes: CID 73640, CID 73697, CID 73702, CID 73823
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agovideo/efi_gop: Remove unnecessary return value of grub_video_gop_fill_mode_info()
Darren Kenny [Tue, 8 Dec 2020 21:14:31 +0000 (21:14 +0000)] 
video/efi_gop: Remove unnecessary return value of grub_video_gop_fill_mode_info()

The return value of grub_video_gop_fill_mode_info() is never able to be
anything other than GRUB_ERR_NONE. So, rather than continue to return
a value and checking it each time, it is more correct to redefine the
function to not return anything and remove checks of its return value
altogether.

Fixes: CID 96701
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocommands/probe: Fix a resource leak when probing disks
Darren Kenny [Fri, 27 Nov 2020 15:41:44 +0000 (15:41 +0000)] 
commands/probe: Fix a resource leak when probing disks

Every other return statement in this code is calling grub_device_close()
to clean up dev before returning. This one should do that too.

Fixes: CID 292443
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocommands/hashsum: Fix a memory leak
Chris Coulson [Tue, 1 Dec 2020 23:41:24 +0000 (23:41 +0000)] 
commands/hashsum: Fix a memory leak

check_list() uses grub_file_getline(), which allocates a buffer.
If the hash list file contains invalid lines, the function leaks
this buffer when it returns an error.

Fixes: CID 176635
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agonormal/completion: Fix leaking of memory when processing a completion
Darren Kenny [Fri, 4 Dec 2020 18:56:48 +0000 (18:56 +0000)] 
normal/completion: Fix leaking of memory when processing a completion

It is possible for the code to reach the end of the function without
freeing the memory allocated to argv and argc still to be 0.

We should always call grub_free(argv). The grub_free() will handle
a NULL argument correctly if it reaches that code without the memory
being allocated.

Fixes: CID 96672
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agosyslinux: Fix memory leak while parsing
Darren Kenny [Thu, 26 Nov 2020 15:31:53 +0000 (15:31 +0000)] 
syslinux: Fix memory leak while parsing

In syslinux_parse_real() the 2 points where return is being called
didn't release the memory stored in buf which is no longer required.

Fixes: CID 176634
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agolibgcrypt/mpi: Fix possible NULL dereference
Darren Kenny [Thu, 26 Nov 2020 10:41:54 +0000 (10:41 +0000)] 
libgcrypt/mpi: Fix possible NULL dereference

The code in gcry_mpi_scan() assumes that buffer is not NULL, but there
is no explicit check for that, so we add one.

Fixes: CID 73757
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agolibgcrypt/mpi: Fix possible unintended sign extension
Darren Kenny [Tue, 3 Nov 2020 16:43:37 +0000 (16:43 +0000)] 
libgcrypt/mpi: Fix possible unintended sign extension

The array of unsigned char gets promoted to a signed 32-bit int before
it is finally promoted to a size_t. There is the possibility that this
may result in the signed-bit being set for the intermediate signed
32-bit int. We should ensure that the promotion is to the correct type
before we bitwise-OR the values.

Fixes: CID 96697
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoaffs: Fix memory leaks
Darren Kenny [Thu, 26 Nov 2020 12:48:07 +0000 (12:48 +0000)] 
affs: Fix memory leaks

The node structure reference is being allocated but not freed if it
reaches the end of the function. If any of the hooks had returned
a non-zero value, then node would have been copied in to the context
reference, but otherwise node is not stored and should be freed.

Similarly, the call to grub_affs_create_node() replaces the allocated
memory in node with a newly allocated structure, leaking the existing
memory pointed by node.

Finally, when dir->parent is set, then we again replace node with newly
allocated memory, which seems unnecessary when we copy in the values
from dir->parent immediately after.

Fixes: CID 73759
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agozfsinfo: Correct a check for error allocating memory
Darren Kenny [Thu, 26 Nov 2020 10:56:45 +0000 (10:56 +0000)] 
zfsinfo: Correct a check for error allocating memory

While arguably the check for grub_errno is correct, we should really be
checking the return value from the function since it is always possible
that grub_errno was set elsewhere, making this code behave incorrectly.

Fixes: CID 73668
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agozfs: Fix possible integer overflows
Darren Kenny [Tue, 8 Dec 2020 22:17:04 +0000 (22:17 +0000)] 
zfs: Fix possible integer overflows

In all cases the problem is that the value being acted upon by
a left-shift is a 32-bit number which is then being used in the
context of a 64-bit number.

To avoid overflow we ensure that the number being shifted is 64-bit
before the shift is done.

Fixes: CID 73684, CID 73695, CID 73764
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agozfs: Fix resource leaks while constructing path
Paulo Flabiano Smorigo [Mon, 14 Dec 2020 21:54:49 +0000 (18:54 -0300)] 
zfs: Fix resource leaks while constructing path

There are several exit points in dnode_get_path() that are causing possible
memory leaks.

In the while(1) the correct exit mechanism should not be to do a direct return,
but to instead break out of the loop, setting err first if it is not already set.

The reason behind this is that the dnode_path is a linked list, and while doing
through this loop, it is being allocated and built up - the only way to
correctly unravel it is to traverse it, which is what is being done at the end
of the function outside of the loop.

Several of the existing exit points correctly did a break, but not all so this
change makes that more consistent and should resolve the leaking of memory as
found by Coverity.

Fixes: CID 73741
Signed-off-by: Paulo Flabiano Smorigo <pfsmorigo@canonical.com>
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agozfs: Fix possible negative shift operation
Darren Kenny [Tue, 24 Nov 2020 16:41:49 +0000 (16:41 +0000)] 
zfs: Fix possible negative shift operation

While it is possible for the return value from zfs_log2() to be zero
(0), it is quite unlikely, given that the previous assignment to blksz
is shifted up by SPA_MINBLOCKSHIFT (9) before 9 is subtracted at the
assignment to epbs.

But, while unlikely during a normal operation, it may be that a carefully
crafted ZFS filesystem could result in a zero (0) value to the
dn_datalbkszsec field, which means that the shift left does nothing
and assigns zero (0) to blksz, resulting in a negative epbs value.

Fixes: CID 73608
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agohfsplus: Check that the volume name length is valid
Darren Kenny [Fri, 23 Oct 2020 17:09:31 +0000 (17:09 +0000)] 
hfsplus: Check that the volume name length is valid

HFS+ documentation suggests that the maximum filename and volume name is
255 Unicode characters in length.

So, when converting from big-endian to little-endian, we should ensure
that the name of the volume has a length that is between 0 and 255,
inclusive.

Fixes: CID 73641
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk/cryptodisk: Fix potential integer overflow
Darren Kenny [Thu, 21 Jan 2021 11:38:31 +0000 (11:38 +0000)] 
disk/cryptodisk: Fix potential integer overflow

The encrypt and decrypt functions expect a grub_size_t. So, we need to
ensure that the constant bit shift is using grub_size_t rather than
unsigned int when it is performing the shift.

Fixes: CID 307788
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk/ldm: Fix memory leak on uninserted lv references
Darren Kenny [Tue, 8 Dec 2020 10:00:51 +0000 (10:00 +0000)] 
disk/ldm: Fix memory leak on uninserted lv references

The problem here is that the memory allocated to the variable lv is not
yet inserted into the list that is being processed at the label fail2.

As we can already see at line 342, which correctly frees lv before going
to fail2, we should also be doing that at these earlier jumps to fail2.

Fixes: CID 73824
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk/ldm: If failed then free vg variable too
Paulo Flabiano Smorigo [Mon, 7 Dec 2020 13:07:47 +0000 (10:07 -0300)] 
disk/ldm: If failed then free vg variable too

Fixes: CID 73809
Signed-off-by: Paulo Flabiano Smorigo <pfsmorigo@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk/ldm: Make sure comp data is freed before exiting from make_vg()
Marco A Benatto [Mon, 7 Dec 2020 14:53:03 +0000 (11:53 -0300)] 
disk/ldm: Make sure comp data is freed before exiting from make_vg()

Several error handling paths in make_vg() do not free comp data before
jumping to fail2 label and returning from the function. This will leak
memory. So, let's fix all issues of that kind.

Fixes: CID 73804
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern/partition: Check for NULL before dereferencing input string
Darren Kenny [Fri, 23 Oct 2020 09:49:59 +0000 (09:49 +0000)] 
kern/partition: Check for NULL before dereferencing input string

There is the possibility that the value of str comes from an external
source and continuing to use it before ever checking its validity is
wrong. So, needs fixing.

Additionally, drop unneeded part initialization.

Fixes: CID 292444
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agozstd: Initialize seq_t structure fully
Darren Kenny [Thu, 5 Nov 2020 10:29:59 +0000 (10:29 +0000)] 
zstd: Initialize seq_t structure fully

While many compilers will initialize this to zero, not all will, so it
is better to be sure that fields not being explicitly set are at known
values, and there is code that checks this fields value elsewhere in the
code.

Fixes: CID 292440
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoio/lzopio: Resolve unnecessary self-assignment errors
Darren Kenny [Wed, 21 Oct 2020 14:44:10 +0000 (14:44 +0000)] 
io/lzopio: Resolve unnecessary self-assignment errors

These 2 assignments are unnecessary since they are just assigning
to themselves.

Fixes: CID 73643
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agognulib/regcomp: Fix uninitialized re_token
Darren Kenny [Tue, 24 Nov 2020 18:04:22 +0000 (18:04 +0000)] 
gnulib/regcomp: Fix uninitialized re_token

This issue has been fixed in the latest version of gnulib, so to
maintain consistency, I've backported that change rather than doing
something different.

Fixes: CID 73828
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agognulib/regexec: Fix possible null-dereference
Darren Kenny [Thu, 5 Nov 2020 10:57:14 +0000 (10:57 +0000)] 
gnulib/regexec: Fix possible null-dereference

It appears to be possible that the mctx->state_log field may be NULL,
and the name of this function, clean_state_log_if_needed(), suggests
that it should be checking that it is valid to be cleaned before
assuming that it does.

Fixes: CID 86720
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agognulib/argp-help: Fix dereference of a possibly NULL state
Darren Kenny [Wed, 28 Oct 2020 14:43:01 +0000 (14:43 +0000)] 
gnulib/argp-help: Fix dereference of a possibly NULL state

All other instances of call to __argp_failure() where there is
a dgettext() call is first checking whether state is NULL before
attempting to dereference it to get the root_argp->argp_domain.

Fixes: CID 292436
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agognulib/regcomp: Fix uninitialized token structure
Darren Kenny [Thu, 22 Oct 2020 13:54:06 +0000 (13:54 +0000)] 
gnulib/regcomp: Fix uninitialized token structure

The code is assuming that the value of br_token.constraint was
initialized to zero when it wasn't.

While some compilers will ensure that, not all do, so it is better to
fix this explicitly than leave it to chance.

Fixes: CID 73749
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agognulib/regexec: Resolve unused variable
Darren Kenny [Wed, 21 Oct 2020 14:41:27 +0000 (14:41 +0000)] 
gnulib/regexec: Resolve unused variable

This is a really minor issue where a variable is being assigned to but
not checked before it is overwritten again.

The reason for this issue is that we are not building with DEBUG set and
this in turn means that the assert() that reads the value of the
variable match_last is being processed out.

The solution, move the assignment to match_last in to an ifdef DEBUG too.

Fixes: CID 292459
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern/efi/mm: Fix possible NULL pointer dereference
Darren Kenny [Fri, 11 Dec 2020 15:03:13 +0000 (15:03 +0000)] 
kern/efi/mm: Fix possible NULL pointer dereference

The model of grub_efi_get_memory_map() is that if memory_map is NULL,
then the purpose is to discover how much memory should be allocated to
it for the subsequent call.

The problem here is that with grub_efi_is_finished set to 1, there is no
check at all that the function is being called with a non-NULL memory_map.

While this MAY be true, we shouldn't assume it.

The solution to this is to behave as expected, and if memory_map is NULL,
then don't try to use it and allow memory_map_size to be filled in, and
return 0 as is done later in the code if the buffer is too small (or NULL).

Additionally, drop unneeded ret = 1.

Fixes: CID 96632
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern/efi: Fix memory leak on failure
Darren Kenny [Thu, 5 Nov 2020 10:15:25 +0000 (10:15 +0000)] 
kern/efi: Fix memory leak on failure

Free the memory allocated to name before returning on failure.

Fixes: CID 296222
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern/parser: Fix resource leak if argc == 0
Darren Kenny [Fri, 22 Jan 2021 12:32:41 +0000 (12:32 +0000)] 
kern/parser: Fix resource leak if argc == 0

After processing the command-line yet arriving at the point where we are
setting argv, we are allocating memory, even if argc == 0, which makes
no sense since we never put anything into the allocated argv.

The solution is to simply return that we've successfully processed the
arguments but that argc == 0, and also ensure that argv is NULL when
we're not allocating anything in it.

There are only 2 callers of this function, and both are handling a zero
value in argc assuming nothing is allocated in argv.

Fixes: CID 96680
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agonet/tftp: Fix dangling memory pointer
Darren Kenny [Fri, 19 Feb 2021 17:12:23 +0000 (17:12 +0000)] 
net/tftp: Fix dangling memory pointer

The static code analysis tool, Parfait, reported that the valid of
file->data was left referencing memory that was freed by the call to
grub_free(data) where data was initialized from file->data.

To ensure that there is no unintentional access to this memory
referenced by file->data we should set the pointer to NULL.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agonet/net: Fix possible dereference to of a NULL pointer
Darren Kenny [Fri, 27 Nov 2020 15:10:26 +0000 (15:10 +0000)] 
net/net: Fix possible dereference to of a NULL pointer

It is always possible that grub_zalloc() could fail, so we should check for
a NULL return. Otherwise we run the risk of dereferencing a NULL pointer.

Fixes: CID 296221
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agommap: Fix memory leak when iterating over mapped memory
Darren Kenny [Thu, 3 Dec 2020 14:39:45 +0000 (14:39 +0000)] 
mmap: Fix memory leak when iterating over mapped memory

When returning from grub_mmap_iterate() the memory allocated to present
is not being released causing it to leak.

Fixes: CID 96655
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agousb: Avoid possible out-of-bound accesses caused by malicious devices
Javier Martinez Canillas [Fri, 11 Dec 2020 18:19:21 +0000 (19:19 +0100)] 
usb: Avoid possible out-of-bound accesses caused by malicious devices

The maximum number of configurations and interfaces are fixed but there is
no out-of-bound checking to prevent a malicious USB device to report large
values for these and cause accesses outside the arrays' memory.

Fixes: CVE-2020-25647
Reported-by: Joseph Tartaro <joseph.tartaro@ioactive.com>
Reported-by: Ilja Van Sprundel <ivansprundel@ioactive.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodl: Only allow unloading modules that are not dependencies
Javier Martinez Canillas [Tue, 29 Sep 2020 12:08:55 +0000 (14:08 +0200)] 
dl: Only allow unloading modules that are not dependencies

When a module is attempted to be removed its reference counter is always
decremented. This means that repeated rmmod invocations will cause the
module to be unloaded even if another module depends on it.

This may lead to a use-after-free scenario allowing an attacker to execute
arbitrary code and by-pass the UEFI Secure Boot protection.

While being there, add the extern keyword to some function declarations in
that header file.

Fixes: CVE-2020-25632
Reported-by: Chris Coulson <chris.coulson@canonical.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodocs: Document the cutmem command
Javier Martinez Canillas [Sat, 7 Nov 2020 00:03:18 +0000 (01:03 +0100)] 
docs: Document the cutmem command

The command is not present in the docs/grub.texi user documentation.

Reported-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
4 years agoloader/xnu: Don't allow loading extension and packages when locked down
Javier Martinez Canillas [Wed, 24 Feb 2021 13:44:38 +0000 (14:44 +0100)] 
loader/xnu: Don't allow loading extension and packages when locked down

The shim_lock verifier validates the XNU kernels but no its extensions
and packages. Prevent these to be loaded when the GRUB is locked down.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agogdb: Restrict GDB access when locked down
Javier Martinez Canillas [Wed, 24 Feb 2021 14:03:26 +0000 (15:03 +0100)] 
gdb: Restrict GDB access when locked down

The gdbstub* commands allow to start and control a GDB stub running on
local host that can be used to connect from a remote debugger. Restrict
this functionality when the GRUB is locked down.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocommands/hdparm: Restrict hdparm command when locked down
Javier Martinez Canillas [Wed, 24 Feb 2021 11:59:29 +0000 (12:59 +0100)] 
commands/hdparm: Restrict hdparm command when locked down

The command can be used to get/set ATA disk parameters. Some of these can
be dangerous since change the disk behavior. Restrict it when locked down.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocommands/setpci: Restrict setpci command when locked down
Javier Martinez Canillas [Wed, 24 Feb 2021 21:59:59 +0000 (22:59 +0100)] 
commands/setpci: Restrict setpci command when locked down

This command can set PCI devices register values, which makes it dangerous
in a locked down configuration. Restrict it so can't be used on this setup.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocommands: Restrict commands that can load BIOS or DT blobs when locked down
Javier Martinez Canillas [Wed, 24 Feb 2021 08:00:05 +0000 (09:00 +0100)] 
commands: Restrict commands that can load BIOS or DT blobs when locked down

There are some more commands that should be restricted when the GRUB is
locked down. Following is the list of commands and reasons to restrict:

  * fakebios:   creates BIOS-like structures for backward compatibility with
                existing OSes. This should not be allowed when locked down.

  * loadbios:   reads a BIOS dump from storage and loads it. This action
                should not be allowed when locked down.

  * devicetree: loads a Device Tree blob and passes it to the OS. It replaces
                any Device Tree provided by the firmware. This also should
                not be allowed when locked down.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agommap: Don't register cutmem and badram commands when lockdown is enforced
Javier Martinez Canillas [Wed, 14 Oct 2020 14:33:42 +0000 (16:33 +0200)] 
mmap: Don't register cutmem and badram commands when lockdown is enforced

The cutmem and badram commands can be used to remove EFI memory regions
and potentially disable the UEFI Secure Boot. Prevent the commands to be
registered if the GRUB is locked down.

Fixes: CVE-2020-27779
Reported-by: Teddy Reed <teddy.reed@gmail.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoacpi: Don't register the acpi command when locked down
Javier Martinez Canillas [Mon, 28 Sep 2020 18:08:41 +0000 (20:08 +0200)] 
acpi: Don't register the acpi command when locked down

The command is not allowed when lockdown is enforced. Otherwise an
attacker can instruct the GRUB to load an SSDT table to overwrite
the kernel lockdown configuration and later load and execute
unsigned code.

Fixes: CVE-2020-14372
Reported-by: Máté Kukri <km@mkukri.xyz>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Use grub_is_lockdown() instead of hardcoding a disabled modules list
Javier Martinez Canillas [Mon, 28 Sep 2020 18:08:33 +0000 (20:08 +0200)] 
efi: Use grub_is_lockdown() instead of hardcoding a disabled modules list

Now the GRUB can check if it has been locked down and this can be used to
prevent executing commands that can be utilized to circumvent the UEFI
Secure Boot mechanisms. So, instead of hardcoding a list of modules that
have to be disabled, prevent the usage of commands that can be dangerous.

This not only allows the commands to be disabled on other platforms, but
also properly separate the concerns. Since the shim_lock verifier logic
should be only about preventing to run untrusted binaries and not about
defining these kind of policies.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Lockdown the GRUB when the UEFI Secure Boot is enabled
Javier Martinez Canillas [Mon, 28 Sep 2020 18:08:29 +0000 (20:08 +0200)] 
efi: Lockdown the GRUB when the UEFI Secure Boot is enabled

If the UEFI Secure Boot is enabled then the GRUB must be locked down
to prevent executing code that can potentially be used to subvert its
verification mechanisms.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern/lockdown: Set a variable if the GRUB is locked down
Javier Martinez Canillas [Tue, 2 Feb 2021 18:59:48 +0000 (19:59 +0100)] 
kern/lockdown: Set a variable if the GRUB is locked down

It may be useful for scripts to determine whether the GRUB is locked
down or not. Add the lockdown variable which is set to "y" when the GRUB
is locked down.

Suggested-by: Dimitri John Ledkov <xnox@ubuntu.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agokern: Add lockdown support
Javier Martinez Canillas [Mon, 28 Sep 2020 18:08:02 +0000 (20:08 +0200)] 
kern: Add lockdown support

When the GRUB starts on a secure boot platform, some commands can be
used to subvert the protections provided by the verification mechanism and
could lead to booting untrusted system.

To prevent that situation, allow GRUB to be locked down. That way the code
may check if GRUB has been locked down and further restrict the commands
that are registered or what subset of their functionality could be used.

The lockdown support adds the following components:

* The grub_lockdown() function which can be used to lockdown GRUB if,
  e.g., UEFI Secure Boot is enabled.

* The grub_is_lockdown() function which can be used to check if the GRUB
  was locked down.

* A verifier that flags OS kernels, the GRUB modules, Device Trees and ACPI
  tables as GRUB_VERIFY_FLAGS_DEFER_AUTH to defer verification to other
  verifiers. These files are only successfully verified if another registered
  verifier returns success. Otherwise, the whole verification process fails.

  For example, PE/COFF binaries verification can be done by the shim_lock
  verifier which validates the signatures using the shim_lock protocol.
  However, the verification is not deferred directly to the shim_lock verifier.
  The shim_lock verifier is hooked into the verification process instead.

* A set of grub_{command,extcmd}_lockdown functions that can be used by
  code registering command handlers, to only register unsafe commands if
  the GRUB has not been locked down.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Move the shim_lock verifier to the GRUB core
Marco A Benatto [Wed, 23 Sep 2020 18:21:14 +0000 (14:21 -0400)] 
efi: Move the shim_lock verifier to the GRUB core

Move the shim_lock verifier from its own module into the core image. The
Secure Boot lockdown mechanism has the intent to prevent the load of any
unsigned code or binary when Secure Boot is enabled.

The reason is that GRUB must be able to prevent executing untrusted code
if UEFI Secure Boot is enabled, without depending on external modules.

Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoverifiers: Move verifiers API to kernel image
Marco A Benatto [Wed, 23 Sep 2020 15:33:33 +0000 (11:33 -0400)] 
verifiers: Move verifiers API to kernel image

Move verifiers API from a module to the kernel image, so it can be
used there as well. There are no functional changes in this patch.

Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodocs: Add documentation of disk size limitations
Glenn Washburn [Wed, 16 Dec 2020 00:47:34 +0000 (18:47 -0600)] 
docs: Add documentation of disk size limitations

Document the artificially imposed 1 EiB disk size limit and size limitations
with LUKS volumes.

Fix a few punctuation issues.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Use grub_log2ull() to calculate log_sector_size and improve readability
Glenn Washburn [Tue, 15 Dec 2020 23:31:11 +0000 (17:31 -0600)] 
luks2: Use grub_log2ull() to calculate log_sector_size and improve readability

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agomisc: Add grub_log2ull() macro for calculating log base 2 of 64-bit integers
Glenn Washburn [Tue, 15 Dec 2020 23:31:10 +0000 (17:31 -0600)] 
misc: Add grub_log2ull() macro for calculating log base 2 of 64-bit integers

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agomips: Enable __clzdi2()
Glenn Washburn [Tue, 15 Dec 2020 23:31:09 +0000 (17:31 -0600)] 
mips: Enable __clzdi2()

This patch is similar to commit 9dab2f51e (sparc: Enable __clzsi2() and
__clzdi2()) but for MIPS target and __clzdi2() only, __clzsi2() was
already enabled.

Suggested-by: Daniel Kiper <dkiper@net-space.pl>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Better error handling when setting up the cryptodisk
Glenn Washburn [Tue, 15 Dec 2020 23:31:08 +0000 (17:31 -0600)] 
luks2: Better error handling when setting up the cryptodisk

Do some sanity checking on data coming from the LUKS2 header. If segment.size
is "dynamic", verify that the offset is not past the end of disk. Otherwise,
check for errors from grub_strtoull() when converting segment size from
string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was
not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was
returned, then there was an overflow in converting to a 64-bit unsigned
integer. So this could be a very large disk (perhaps large RAID array).
In this case skip the key too. Additionally, enforce some other limits
and fail if needed.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Do not handle disks of size GRUB_DISK_SIZE_UNKNOWN for now
Glenn Washburn [Tue, 15 Dec 2020 23:31:07 +0000 (17:31 -0600)] 
luks2: Do not handle disks of size GRUB_DISK_SIZE_UNKNOWN for now

Check to make sure that source disk has a known size. If not, print
a message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN
is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases
processing continues. So this is probably a bit conservative. However,
3 of the cases seem pathological, and the other, biosdisk, happens when
booting from a CD-ROM. Since I doubt booting from a LUKS2 volume on
a CD-ROM is a big use case, we'll error until someone complains.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Convert to crypt sectors from GRUB native sectors
Glenn Washburn [Tue, 15 Dec 2020 23:31:06 +0000 (17:31 -0600)] 
luks2: Convert to crypt sectors from GRUB native sectors

The function grub_disk_native_sectors(source) returns the number of sectors
of source in GRUB native (512-byte) sectors, not source sized sectors. So
the conversion needs to use GRUB_DISK_SECTOR_BITS, the GRUB native sector
size.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Error check segment.sector_size
Glenn Washburn [Tue, 8 Dec 2020 22:45:46 +0000 (16:45 -0600)] 
luks2: Error check segment.sector_size

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocryptodisk: Properly handle non-512 byte sized sectors
Glenn Washburn [Tue, 8 Dec 2020 22:45:44 +0000 (16:45 -0600)] 
cryptodisk: Properly handle non-512 byte sized sectors

By default, dm-crypt internally uses an IV that corresponds to 512-byte
sectors, even when a larger sector size is specified. What this means is
that when using a larger sector size, the IV is incremented every sector.
However, the amount the IV is incremented is the number of 512 byte blocks
in a sector (i.e. 8 for 4K sectors). Confusingly the IV does not correspond
to the number of, for example, 4K sectors. So each 512 byte cipher block in
a sector will be encrypted with the same IV and the IV will be incremented
afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have
the IV equal to the sector number regardless of sector size (ie. the fifth
sector would have an IV of 4 for each cipher block). And this is supported
by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3
with the --iv-large-sectors, though not with LUKS headers (only with --type
plain). However, support for this has not been included as grub does not
support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard-
coded 512-byte sector size. So even if your data is encrypted with 4K sector
sizes, the split key encrypted area must be decrypted with a block size of
512 (ie the IV increments every 512 bytes). This made these changes less
aesthetically pleasing than desired.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors
Glenn Washburn [Tue, 8 Dec 2020 22:45:43 +0000 (16:45 -0600)] 
luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

We need to convert the sectors from the size of the underlying device to the
cryptodisk sector size; segment.size is in bytes which need to be converted
to cryptodisk sectors as well.

Also, removed an empty statement.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
Glenn Washburn [Tue, 8 Dec 2020 22:45:42 +0000 (16:45 -0600)] 
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

Add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an
unsigned number with size of type.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
Glenn Washburn [Tue, 8 Dec 2020 22:45:41 +0000 (16:45 -0600)] 
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals

The new macro GRUB_TYPE_BITS(type) returns the number of bits
allocated for type.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Add string "index" to user strings using a json index
Glenn Washburn [Tue, 8 Dec 2020 22:45:40 +0000 (16:45 -0600)] 
luks2: Add string "index" to user strings using a json index

This allows error messages to be more easily distinguishable between indexes
and slot keys. The former include the string "index" in the error/debug
string, and the later are surrounded in quotes.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Rename json index variables to names that they are obviously json indexes
Glenn Washburn [Tue, 8 Dec 2020 22:45:39 +0000 (16:45 -0600)] 
luks2: Rename json index variables to names that they are obviously json indexes

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Use more intuitive object name instead of json index in user messages
Glenn Washburn [Tue, 8 Dec 2020 22:45:38 +0000 (16:45 -0600)] 
luks2: Use more intuitive object name instead of json index in user messages

Use the object name in the json array rather than the 0 based index in the
json array for keyslots, segments, and digests. This is less confusing for
the end user. For example, say you have a LUKS2 device with a key in slot 1
and slot 4. When using the password for slot 4 to unlock the device, the
messages using the index of the keyslot will mention keyslot 1 (its a
zero-based index). Furthermore, with this change the keyslot number will
align with the number used to reference the keyslot when using the
--key-slot argument to cryptsetup.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Add idx member to struct grub_luks2_keyslot/segment/digest
Glenn Washburn [Tue, 8 Dec 2020 22:45:37 +0000 (16:45 -0600)] 
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest

This allows code using these structs to know the named key associated with
these json data structures. In the future we can use these to provide better
error messages to the user.

Get rid of idx local variable in luks2_get_keyslot() which was overloaded to
be used for both keyslot and segment slot keys.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Make sure all fields of output argument in luks2_parse_digest() are written to
Glenn Washburn [Tue, 8 Dec 2020 22:45:36 +0000 (16:45 -0600)] 
luks2: Make sure all fields of output argument in luks2_parse_digest() are written to

We should assume that the output argument "out" is uninitialized and could
have random data. So, make sure to initialize the segments and keyslots bit
fields because potentially not all bits of those fields are written to.
Otherwise, the digest could say it belongs to keyslots and segments that it
does not.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Remove unused argument in grub_error() call
Glenn Washburn [Tue, 8 Dec 2020 22:45:35 +0000 (16:45 -0600)] 
luks2: Remove unused argument in grub_error() call

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Convert 8 spaces to tabs
Glenn Washburn [Tue, 8 Dec 2020 22:45:34 +0000 (16:45 -0600)] 
luks2: Convert 8 spaces to tabs

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agomisc: Add parentheses around ALIGN_UP() and ALIGN_DOWN() arguments
Glenn Washburn [Tue, 8 Dec 2020 22:45:33 +0000 (16:45 -0600)] 
misc: Add parentheses around ALIGN_UP() and ALIGN_DOWN() arguments

This ensures that expected order of operations is preserved when arguments
are expressions.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk: Rename grub_disk_get_size() to grub_disk_native_sectors()
Glenn Washburn [Tue, 8 Dec 2020 22:45:32 +0000 (16:45 -0600)] 
disk: Rename grub_disk_get_size() to grub_disk_native_sectors()

The function grub_disk_get_size() is confusingly named because it actually
returns a sector count where the sectors are sized in the GRUB native sector
size. Rename to something more appropriate.

Suggested-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoloopback: Do not automaticaly replace existing loopback dev, error instead
Glenn Washburn [Fri, 4 Dec 2020 01:57:11 +0000 (19:57 -0600)] 
loopback: Do not automaticaly replace existing loopback dev, error instead

If there is a loopback device with the same name as the one to be created,
instead of closing the old one and replacing it with the new one, return an
error instead. If the loopback device was created, its probably being used
by something and just replacing it may cause GRUB to crash unexpectedly.
This fixes obvious problems like "loopback d (d)/somefile". Its not too
onerous to force the user to delete the loopback first with the "-d" switch.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agodisk: Move hardcoded max disk size literal to a GRUB_DISK_MAX_SECTORS in disk.h
Glenn Washburn [Tue, 1 Dec 2020 05:16:19 +0000 (23:16 -0600)] 
disk: Move hardcoded max disk size literal to a GRUB_DISK_MAX_SECTORS in disk.h

There is a hardcoded maximum disk size that can be read or written from,
currently set at 1 EiB in grub_disk_adjust_range(). Move the literal into a
macro in disk.h, so our assumptions are more visible. This hard coded limit
does not prevent using larger disks, just GRUB won't read/write past the
limit. The comment accompanying this restriction didn't quite make sense to
me, so its been modified too.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agofs: Fix block lists not being able to address to end of disk sometimes
Glenn Washburn [Mon, 23 Nov 2020 09:27:42 +0000 (03:27 -0600)] 
fs: Fix block lists not being able to address to end of disk sometimes

When checking if a block list goes past the end of the disk, make sure
the total size of the disk is in GRUB native sector sizes, otherwise there
will be blocks at the end of the disk inaccessible by block lists.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agombr: Document new limitations on MBR gap support
Vladimir Serbinenko [Tue, 10 Nov 2020 19:23:56 +0000 (20:23 +0100)] 
mbr: Document new limitations on MBR gap support

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agombr: Warn if MBR gap is small and user uses advanced modules
Vladimir Serbinenko [Mon, 27 Apr 2020 15:50:04 +0000 (17:50 +0200)] 
mbr: Warn if MBR gap is small and user uses advanced modules

We don't want to support small MBR gap in pair with anything but the
simplest config of biosdisk + part_msdos + simple filesystem. In this
path "simple filesystems" are all current filesystems except ZFS and
Btrfs.

Signed-off-by: Vladimir Serbinenko <phcoder@google.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi/tpm: Extract duplicate code into independent functions
Tianjia Zhang [Thu, 29 Oct 2020 13:49:49 +0000 (21:49 +0800)] 
efi/tpm: Extract duplicate code into independent functions

Part of the code logic for processing the return value of efi
log_extend_event is repetitive and complicated. Extract the
repetitive code into an independent function.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi/tpm: Add debug information for device protocol and eventlog
Tianjia Zhang [Thu, 29 Oct 2020 13:49:29 +0000 (21:49 +0800)] 
efi/tpm: Add debug information for device protocol and eventlog

Add a number of debug logs to the tpm module. The condition tag
for opening debugging is "tpm". On TPM machines, this will bring
great convenience to diagnosis and debugging.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoloader/linux: Report the UEFI Secure Boot status to the Linux kernel
Daniel Kiper [Thu, 3 Dec 2020 15:01:50 +0000 (16:01 +0100)] 
loader/linux: Report the UEFI Secure Boot status to the Linux kernel

Now that the GRUB has a grub_efi_get_secureboot() function to check the
UEFI Secure Boot status, use it to report that to the Linux kernel.

Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Only register shim_lock verifier if shim_lock protocol is found and SB enabled
Javier Martinez Canillas [Thu, 3 Dec 2020 15:01:49 +0000 (16:01 +0100)] 
efi: Only register shim_lock verifier if shim_lock protocol is found and SB enabled

The shim_lock module registers a verifier to call shim's verify, but the
handler is registered even when the shim_lock protocol was not installed.

This doesn't cause a NULL pointer dereference in shim_lock_write() because
the shim_lock_init() function just returns GRUB_ERR_NONE if sl isn't set.

But in that case there's no point to even register the shim_lock verifier
since won't do anything. Additionally, it is only useful when Secure Boot
is enabled.

Finally, don't assume that the shim_lock protocol will always be present
when the shim_lock_write() function is called, and check for it on every
call to this function.

Reported-by: Michael Chang <mchang@suse.com>
Reported-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Add secure boot detection
Daniel Kiper [Thu, 3 Dec 2020 15:01:48 +0000 (16:01 +0100)] 
efi: Add secure boot detection

Introduce grub_efi_get_secureboot() function which returns whether
UEFI Secure Boot is enabled or not on UEFI systems.

Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Add a function to read EFI variables with attributes
Daniel Kiper [Thu, 3 Dec 2020 15:01:47 +0000 (16:01 +0100)] 
efi: Add a function to read EFI variables with attributes

It will be used to properly detect and report UEFI Secure Boot status to
the x86 Linux kernel. The functionality will be added by subsequent patches.

Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Return grub_efi_status_t from grub_efi_get_variable()
Daniel Kiper [Thu, 3 Dec 2020 15:01:46 +0000 (16:01 +0100)] 
efi: Return grub_efi_status_t from grub_efi_get_variable()

This is needed to properly detect and report UEFI Secure Boot status
to the x86 Linux kernel. The functionality will be added by subsequent
patches.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoefi: Make shim_lock GUID and protocol type public
Daniel Kiper [Thu, 3 Dec 2020 15:01:45 +0000 (16:01 +0100)] 
efi: Make shim_lock GUID and protocol type public

The GUID will be used to properly detect and report UEFI Secure Boot
status to the x86 Linux kernel. The functionality will be added by
subsequent patches. The shim_lock protocol type is made public for
completeness.

Additionally, fix formatting of four preceding GUIDs.

Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoarm/term: Fix linking error due multiple ps2_state definitions
Javier Martinez Canillas [Thu, 3 Dec 2020 15:01:44 +0000 (16:01 +0100)] 
arm/term: Fix linking error due multiple ps2_state definitions

When building with --target=arm-linux-gnu --with-platform=coreboot
a linking error occurs caused by multiple definitions of the
ps2_state variable.

Mark them as static since they aren't used outside their compilation unit.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoinclude/grub/i386/linux.h: Include missing <grub/types.h> header
Javier Martinez Canillas [Thu, 3 Dec 2020 15:01:43 +0000 (16:01 +0100)] 
include/grub/i386/linux.h: Include missing <grub/types.h> header

This header uses types defined in <grub/types.h> but does not include it,
which leads to compile errors like the following:

In file included from ../include/grub/cpu/linux.h:19,
                 from kern/efi/sb.c:21:
../include/grub/i386/linux.h:80:3: error: unknown type name ‘grub_uint64_t’
   80 |   grub_uint64_t addr;

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoi386: Don't include <grub/cpu/linux.h> in coreboot and ieee1275 startup.S
Javier Martinez Canillas [Thu, 3 Dec 2020 15:01:42 +0000 (16:01 +0100)] 
i386: Don't include <grub/cpu/linux.h> in coreboot and ieee1275 startup.S

Nothing defined in the header file is used in the assembly code but it
may lead to build errors if some headers are included through this and
contains definitions that are not recognized by the assembler, e.g.:

../include/grub/types.h: Assembler messages:
../include/grub/types.h:76: Error: no such instruction: `typedef signed char grub_int8_t'
../include/grub/types.h:77: Error: no such instruction: `typedef short grub_int16_t'
../include/grub/types.h:78: Error: no such instruction: `typedef int grub_int32_t'

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Rename index variable "j" to "i" in luks2_get_keyslot()
Glenn Washburn [Sat, 7 Nov 2020 04:44:27 +0000 (22:44 -0600)] 
luks2: Rename index variable "j" to "i" in luks2_get_keyslot()

Looping variable "j" was named such because the variable name "i" was taken.
Since "i" has been renamed in the previous patch, we can rename "j" to "i".

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Rename variable "i" to "keyslot_idx" in luks2_get_keyslot()
Glenn Washburn [Sat, 7 Nov 2020 04:44:26 +0000 (22:44 -0600)] 
luks2: Rename variable "i" to "keyslot_idx" in luks2_get_keyslot()

Variables named "i" are usually looping variables. So, rename it to
"keyslot_idx" to ease luks2_get_keyslot() reading.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Use correct index variable when looping in luks2_get_keyslot()
Glenn Washburn [Sat, 7 Nov 2020 04:44:25 +0000 (22:44 -0600)] 
luks2: Use correct index variable when looping in luks2_get_keyslot()

The loop variable "j" should be used to index the digests and segments json
array, instead of the variable "i", which is the keyslot index.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoluks2: Rename source disk variable named "disk" to "source" as in luks.c
Glenn Washburn [Sat, 7 Nov 2020 04:44:23 +0000 (22:44 -0600)] 
luks2: Rename source disk variable named "disk" to "source" as in luks.c

This makes it more obvious to the reader that the disk referred to is the
source disk, as opposed to say the disk holding the cryptodisk.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocryptodisk: Rename "offset" in grub_cryptodisk_t to "offset_sectors"
Glenn Washburn [Sat, 7 Nov 2020 04:44:22 +0000 (22:44 -0600)] 
cryptodisk: Rename "offset" in grub_cryptodisk_t to "offset_sectors"

This makes it clear that the offset represents sectors, not bytes, in
order to improve readability.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agocryptodisk: Rename "total_length" field in grub_cryptodisk_t to "total_sectors"
Glenn Washburn [Sat, 7 Nov 2020 04:44:21 +0000 (22:44 -0600)] 
cryptodisk: Rename "total_length" field in grub_cryptodisk_t to "total_sectors"

This creates an alignment with grub_disk_t naming of the same field and is
more intuitive as to how it should be used.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agotypes: Define GRUB_CHAR_BIT based on compiler macro instead of using literal
Glenn Washburn [Sat, 7 Nov 2020 04:44:24 +0000 (22:44 -0600)] 
types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoinclude/grub/arm64/linux.h: Include missing <grub/types.h> header
Javier Martinez Canillas [Mon, 9 Nov 2020 10:40:14 +0000 (11:40 +0100)] 
include/grub/arm64/linux.h: Include missing <grub/types.h> header

This header uses types defined in <grub/types.h> but does not include it,
which leads to compile errors like the following:

../include/grub/cpu/linux.h:27:3: error: unknown type name ‘grub_uint32_t’
   27 |   grub_uint32_t code0;  /* Executable code */
      |   ^~~~~~~~~~~~~

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
4 years agoinclude/grub/arm/system.h: Include missing <grub/symbol.h> header
Javier Martinez Canillas [Thu, 5 Nov 2020 14:58:57 +0000 (15:58 +0100)] 
include/grub/arm/system.h: Include missing <grub/symbol.h> header

The header uses the EXPORT_FUNC() macro defined in <grub/types.h> but
doesn't include it, which leads to the following compile error on arm:

../include/grub/cpu/system.h:12:13: error: ‘EXPORT_FUNC’ declared as function returning a function
   12 | extern void EXPORT_FUNC(grub_arm_disable_caches_mmu) (void);
      |             ^~~~~~~~~~~
../include/grub/cpu/system.h:12:1: warning: parameter names (without types) in function declaration
   12 | extern void EXPORT_FUNC(grub_arm_disable_caches_mmu) (void);
      | ^~~~~~
make[3]: *** [Makefile:36581: kern/efi/kernel_exec-sb.o] Error 1

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>