Fix wrong sizeof().

CHAR16* vs CHAR16**, so the result is the same on all platforms.

Detected by coverity.

Signed-off-by: Peter Jones <pjones@redhat.com>
Initialize entries before we pass it to another function.

Coverity scan noticed that entries is uninitialized when we pass its
location to another function.

Signed-off-by: Peter Jones <pjones@redhat.com>
Rewrite directory traversal allocation path so coverity can grok it.

The things we do for our tools.  In this case, make the AllocatePool()
happen outside of a conditional, even though that conditional will
always bee satisfied.  This way coverity won't think we're setting fi
to NULL and passing it to StrCaseCmp.

Signed-off-by: Peter Jones <pjones@redhat.com>
Error check the right thing in get_variable_attr() when allocating.

Signed-off-by: Peter Jones <pjones@redhat.com>
[fallback] For HD() device paths, use just the media node and later.

UEFI 2.x section 3.1.2 provides for "short-form device path", where the
first element specified is a "hard drive media device path", so that you
can move a disk around on different buses without invalidating your
device path.  Fallback has not been using this option, though in most
cases efibootmgr has.

Note that we still keep the full device path, because LoadImage()
isn't necessarily the layer where HD() works - one some systems BDS is
responsible for resolving the full path and passes that to LoadImage()
instead.  So we have to do LoadImage() with the full path.
[fallback] Attempt to re-use existing entries when possible.

Some firmwares seem to ignore our boot entries and put their fallback
entries back on top.  Right now that results in a lot of boot entries
for our stuff, a la https://bugzilla.redhat.com/show_bug.cgi?id=995834 .

Instead of that happening, if we simply find existing entries that match
the entry we would create and move them to the top of the boot order,
the machine will continue to operate in failure mode (which we can't
avoid), but at least we won't create thousands of extra entries.

Signed-off-by: Peter Jones <pjones@redhat.com>
Add a preliminary test plan.

Because you know you wanted a test plan.  You feel it deeply inside.

Note that none of the /negative/ cases are tested yet.

Signed-off-by: Peter Jones <pjones@redhat.com>
Add a failure case to the test plan and fix an ordering error.

Signed-off-by: Peter Jones <pjones@redhat.com>
Allow fallback to use the system's LoadImage/StartImage .

Track use of the system's LoadImage(), and when the next StartImage()
call is for an image the system verified, allow that to count as
participating, since it has been verified by the system's db.

Signed-off-by: Peter Jones <pjones@redhat.com>
additional bounds-checking on section sizes

This adds additional bounds-checking on the section sizes. Also adds
-Wsign-compare to the Makefile and replaces some signed variables with
unsigned counteparts for robustness.

Signed-off-by: Kees Cook <kees@ubuntu.com>
Kees' patch missed the offset adjustment to PEHdr.

In read_header, we adjust context->PEHdr's address by doshdr->e_lfanew.
If we're going to recompute that address, we have to adjust it here
too.

Signed-off-by: Peter Jones <pjones@redhat.com>
Get rid of SectionCache in generate_hash(), it is unused.

Signed-off-by: Peter Jones <pjones@redhat.com>
[fallback] Avoid duplicate old BootOrder

set_boot_order() already copies the old BootOrder to the variable,
bootorder. Besides, we can adjust BootOrder when adding the newly
generated boot option. So, we don't have to copy the old one again
in update_boot_order(). This avoid the duplicate entries in BootOrder.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
[fallback] Fix the data size for boot option comparison

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
[fallback] Try to boot the first boot option anyway

Some UEFI implementations never care the boot options, so the
restored boot options could be just ignored and this results in
endless reboot. To avoid this situation, this commit makes
fallback.efi to load the first matched boot option even if there
is no boot option to be restored. It may not be perfect, but at
least the bootloader is loaded...

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
Fetch the netboot image from the same device

The previous strategy is to locate the first available PXE_BASE_CODE
protocol and to fetch the second stage image from it, and this may
cause shim to fetch the wrong second stage image, i.e. grub.efi.

Consider the machine with the following boot order:
1. PXE Boot
2. Hard Drive

Assume that the EFI image, e.g. bootx64.efi, in the PXE server is
broken, then "PXE Boot" will fail and fallback to "Hard Drive". While
shim.efi in "Hard Drive" is loaded, it will find the PXE protocol is
available and fetch grub.efi from the PXE server, not grub.efi in the
disk.

This commit checks the DeviceHandle from Loaded Image. If the device
supports PXE, then shim fetches grub.efi with the PXE protocol. Otherwise,
shim loads grub.efi from the disk.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
Check the first 4 bytes of the certificate

A non-DER encoding x509 certificate may be mistakenly enrolled into
db or MokList. This commit checks the first 4 bytes of the certificate
to ensure that it's DER encoding.

This commit also removes the iteration of the x509 signature list.
Per UEFI SPEC, each x509 signature list contains only one x509 certificate.
Besides, the size of certificate is incorrect. The size of the header must
be substracted from the signature size.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
Remove grubpath in generate_path()

The variable is not used anymore.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
MokManager: delete the BS+NV variables the right way

LibDeleteVariable assumes that the variable is RT+NV and it
won't work on a BS+NV variable.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
MokManager: handle the error status from ReadKeyStroke

On some machines, even though the key event was signaled, ReadKeyStroke
still got EFI_NOT_READY. This commit handles the error status to avoid
console_get_keystroke from returning unexpected keys.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>

Conflicts:
	MokManager.c