Commit Graph

143 Commits

Author SHA1 Message Date
Gary Lin
4e111bf1af console: Move the countdown function to console.c
Move the countdown function from MokManager to console.c to make the
function public

Also make console_save_and_set_mode() and console_restore_mode() public

Signed-off-by: Gary Lin <glin@suse.com>
2021-02-16 09:12:48 +01:00
Peter Jones
aedb8470bd Fix up a bunch of our license statements and add SPDX most places
The license statements in our source files were getting to be a giant
mess, and mostly they all just say the same thing.  I've switched most
of it to SPDX labels, but left copyright statements in place (where they
were not obviously incorrect copy-paste jobs that I did...).

If there's some change here you don't think is valid, let me know and
we can fix it up together.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-16 09:12:48 +01:00
Peter Jones
890563ee7e Fix some mokmanager deletion paths
This fixes several codepaths where MokList and MokListX are supposed to
be deleted, but are not.  It also adds debug logging to much of the
deletion codepath.
2020-10-15 19:17:35 -04:00
Peter Jones
6df96cdb20 MokManager: fix a wrong allocation failure check.
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#212
2020-07-23 20:53:24 -04:00
Peter Jones
7b77bee796 MokManager: fix uninitialized value
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#212
2020-07-23 20:53:24 -04:00
Jonas Witschel
d57e53f3bd MokManager: avoid -Werror=address-of-packed-member
When compiling with GCC 9, there are a couple of errors of the form

MokManager.c: In function ‘write_back_mok_list’:
MokManager.c:1056:19: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]
 1056 |   if (CompareGuid(&(list[i].Type), &X509_GUID) == 0)
      |                   ^~~~~~~~~~~~~~~

Copying the member of the packed struct to a temporary variable and
pointing to that variable solves the problem.

Upstream-commit-id: 58532e12e9a
2020-07-23 20:53:24 -04:00
Ivan Hu
55163bc82c MokManager: console mode modification for hi-dpi screen devices
There are lots of hi-dpi laptops nowadays, as doing mok enrollment, the font
is too small to see.
https://bugs.launchpad.net/ubuntu/+source/shim/+bug/1822043

This patch checks if the resolution is larger than Full HD (1920x1080) and
current console output columns and rows is in a good mode. Then swith the
console output to a better mode.

Signed-off-by: Ivan Hu <ivan.hu@canonical.com>
Upstream-commit-id: cf05af6d899
2020-07-23 20:53:24 -04:00
Gary Lin
5d30a31fef MokManager: Use CompareMem on MokListNode.Type instead of CompareGuid
Fix the errors from gcc9 '-Werror=address-of-packed-member'

https://github.com/rhboot/shim/issues/161

Signed-off-by: Gary Lin <glin@suse.com>
Upstream-commit-id: aaa09b35e73
2020-07-23 20:53:24 -04:00
Peter Jones
2cbf56b82a Work around stuff -Waddress-of-packed-member finds.
In MokManager we get a lot of these:

../src/MokManager.c:1063:19: error: taking address of packed member of ‘struct <anonymous>’ may result in an unaligned pointer value [-Werror=address-of-packed-member]
 1063 |   if (CompareGuid(&(list[i].Type), &X509_GUID) == 0)
      |                   ^~~~~~~~~~~~~~~

The reason for this is that gnu-efi takes EFI_GUID * as its argument
instead of VOID *, and there's nothing telling the compiler that it
doesn't have alignment constraints on the input, so the compiler wants
it to have 16-byte alignment.

Just use CompareMem() for these, as that's all CompareGuid is calling
anyway.

Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream-commit-id: 08c14376b59
2020-07-23 20:52:12 -04:00
Gary Lin
85c837d67f MokManager: Stop using EFI_VARIABLE_APPEND_WRITE
When writing MokList with EFI_VARIABLE_APPEND_WRITE, some HP laptops
may just return EFI_SUCCESS without writing the content into the flash,
so we have no way to detect if MokList is updated or not. Now we always
read MokList first and write it back with the new content.

https://github.com/rhboot/shim/issues/105

Signed-off-by: Gary Lin <glin@suse.com>
Upstream-commit-id: f442c8424b4
2020-07-23 20:51:18 -04:00
Mathieu Trudel-Lapierre
7471867794 Let MokManager follow a MokTimeout var for timeout length for the prompt
This timeout can have the values [-1,0..0x7fff]; where -1 means "no timeout",
with MokManager going directly to the menu, and is capped to 0x7fff to avoid
unecessary long timeouts. The default remains 10, which will be used whenever
the MokTimeout variable isn't set.

Signed-off-by: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
Upstream-commit-id: 93708c11083
2020-07-23 20:51:18 -04:00
Peter Jones
77ebb3d676 Audit get_variable() calls for correct FreePool() use.
Signed-off-by: Peter Jones <pjones@redhat.com>
2018-04-05 14:49:17 -04:00
Peter Jones
f391e44516 Revert "MokManager: stop using StrnCat"
This reverts commit 6aa5a62515.

Everything Hans said was correct.  But StrnCat() is in gnu-efi 3.0.8,
and using just StrCpy() here confuses coverity.  I'd rather have a CI
page that's not completely full of chaff, but a little bit of redundancy
in the code.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-20 16:28:57 -04:00
Hans de Goede
6aa5a62515 MokManager: stop using StrnCat
StrnCat is not available in gnu-efi-3.0.5 (I did not check if it does
actually exists in 3.0.6). Moreover using strcat on a buffer where we've
just done: "buf[0] = '\0'" is a bit silly, we might as well drop the 0
termination and just use strcpy.

It seems there also is no StrnCpy in gnu-efi-3.0.5, but we are passing in
a pointer to the end of file_name minus 4, so strcpy will consume only
4 bytes anyways and there is no need for the "n".

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2018-03-13 15:40:44 -04:00
Hans de Goede
1ff4a36a23 console: Do not set EFI console to textmode until something is printed
Remove the setup_console(1) calls from shim and instead make lib/console.c
make that call when necessary. This avoids shim forcing the EFI console to
switch to text-mode if nothing is printed.

This commit also modifies MokManager to work the same way for consistency,
even though MokManager will always print something.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2018-03-12 18:00:41 -04:00
Hans de Goede
1fe31ee1b4 console: Add console_print and console_print_at helpers
This is a preparation commit for removing the setup_console(1) calls from
MokManager and shim so that we don't force the EFI console to switch to
text-mode.

This commit replaces all direct calls to Print / PrintAt with calls to
the new helpers (no functional changes) so that we can delay calling
setup_console(1) till the first Print call in a follow-up patch.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2018-03-12 18:00:41 -04:00
Peter Jones
9fdca5bbe1 Don't use uefi_call_wrapper(), ever.
I'm pretty done with typing uefi_call_wrapper() and counting arguments
every time.  Instead, just make the compiler error if we don't have
ms_abi.  Also, make it so nothing can use uefi_call_wrapper() directly.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
933bb4f776 MokManager: get rid of struct menu_item, it is unused.
Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
34e5043496 MokManager: Fix a conditional on an uninitialized value in one error path.
clang-analyzer says:

MokManager.c:1431:6: warning: Branch condition evaluates to a garbage value
        if (mok)
            ^~~
MokManager.c:1433:6: warning: Branch condition evaluates to a garbage value
        if (del_key)
            ^~~~~~~

And it's right; if we take the first error exit in the function, those
never get initialized.  This patch sets them to NULL to begin with.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
a326513605 MokManager: handle mok parameter allocations better.
Covscan daftly claims:

288. var_compare_op: Comparing MokSB to null implies that MokSB might be null.
2330                if (MokSB) {
2331                        menu_strings[i] = L"Change Secure Boot state";
2332                        menu_item[i] = MOK_CHANGE_SB;
2333                        i++;
2334                }
2335
...
2358                choice = console_select(perform_mok_mgmt, menu_strings, 0);
2359                if (choice < 0)
2360                        goto out;
...
2362                switch (menu_item[choice]) {
...
2395                case MOK_CHANGE_SB:
    CID 182841 (#1 of 1): Dereference after null check
    (FORWARD_NULL)293. var_deref_model: Passing null pointer MokSB to
    mok_sb_prompt, which dereferences it. [show details]
2396                        efi_status = mok_sb_prompt(MokSB, MokSBSize);

Which is, of course, entirely false, beause for menu_item[choice] to be
MOK_CHANGE_SB, MokSB must be !NULL.  And then:

    252. Condition efi_status == 0, taking true branch.
2397                        if (efi_status == EFI_SUCCESS)
2398                                MokSB = NULL;

This guarantees it won't be in the list the next time through the loop.

This adds tests for NULLness before mok_sb_prompt(), just to make it
more clear to covscan what's going on.

Also do the same thing for all of:
	MOK_CHANGE_SB
	MOK_SET_PW
	MOK_CHANGE_DB
	MOK_ENROLL_MOKX
	MOK_DELETE_MOKX

I also Lindent-ed everything I had to touch.

Three other minor errors are also fixed:
1) the loop in enter_mok_menu() leaked the menu allocations each time
   through the loop
2) mok_sb_prompt(), mok_pw_prompt(), and mok_db_prompt() all call
   FreePool() on their respective variables (MokSB, etc), and
   check_mok_request() also calls FreePool() on these.  This sounds
   horrible, but it turns out it's not an issue, because they only free
   them in their EFI_SUCCESS paths, and enter_mok_menu() resets the
   system if any of the mok_XX_prompt() calls actually returned
   EFI_SUCCESS, so we never get back to check_mok_request() for it to do
   its FreePool() calls.
3) the loop in enter_mok_menu() winds up introducing a double free in
   the call to free_menu(), but we also can't hit this bug, because all
   the exit paths from the loop are "goto out" (or return error) rather
   than actually exiting on the loop conditional.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
bfeaae2386 MokManager: use EFI_ERROR() instead of comparing to EFI_SUCCESS.
Also consistently name our status variable "efi_status" unless there's a
good reason not to, such as already having another one of those.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
0518453845 MokManager: check_der_suffix(): use StrnCat() on our suffix checker.
We know it's legit already because we computed the pointer from the end,
but covscan gets confused, and we have StrnCat, so we should just use it
anyway.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
01b5d9c6b2 MokManager: Lindent (and other reformats) the whole file.
I'm just tired of all the little quirks.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
54f8f1f9ed Get rid of all the places we cast to (CHAR16 *[])
Lindent gets confused by these, and they're hard to read anyway.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
9facc22ebe Fix some "if (x < 0)" tests where x is UINTN.
Obviously, these are not correct.  Most of them are just useless; one
can be changed to a more useful test.

Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
b953468e91 Don't have tons of local guid definitions for no reason at all.
Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
dc62a3c4dc Move includes around to clean the source tree up a bit.
Signed-off-by: Peter Jones <pjones@redhat.com>
2018-03-12 16:21:43 -04:00
Peter Jones
1d39ada8cb Revert lots of Cryptlib updates.
OpenSSL changes quite a bit of the key validation, and most of the keys
I can find in the wild aren't marked as trusted by the new checker.

Intel noticed this too: https://github.com/vathpela/edk2/commit/f536d7c3ed
but instead of fixing the compatibility error, they switched their test
data to match the bug.

So that's pretty broken.

For now, I'm reverting OpenSSL 1.1.0e, because we need those certs in
the wild to work.

This reverts commit 513cbe2aea.
This reverts commit e9cc33d6f2.
This reverts commit 80d49f758e.
This reverts commit 9bc647e2b2.
This reverts commit ae75df6232.
This reverts commit e883479f35.
This reverts commit 97469449fd.
This reverts commit e39692647f.
This reverts commit 0f3dfc01e2.
This reverts commit 4da6ac8195.
This reverts commit d064bd7eef.
This reverts commit 9bc86cfd6f.
This reverts commit ab9a05a10f.

Signed-off-by: Peter Jones <pjones@redhat.com>
2017-08-31 15:13:58 -04:00
Mathieu Trudel-Lapierre
5202f80c32 Rework looping in enter_mok_menu(), to allow multiple MOK changes
Rather than looping once through the possible actions (MokNew, MokDel, etc.),
revise the logic so that instead of rebooting immediately we get back to the
main menu setting a flag to replace "Continue booting" with a proper reboot.

Getting back to the menu means we can go make other changes before rebooting.
For instance, you might want to enable validation, but beforehand you also
need to enroll a MOK. You can already do so from userland; except the requests
were cleared as soon as one of them was processed.

This involves some extra cleanup of the states to avoid running the same
request more than once, removing the option from the menu once it's done, and
changing prompting functions to return an EFI_STATUS so we can better track
whether the process has succeeded.

Signed-off-by: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
2017-08-18 12:45:02 -04:00
Gary Lin
ae75df6232 MokManager: Update to new openssl API
X509_get_notBefore -> X509_getm_notBefore
X509_get_notAfter  -> X509_getm_notAfter

Signed-off-by: Gary Lin <glin@suse.com>
2017-04-11 10:42:19 -04:00
Mathieu Trudel-Lapierre
8af6e22814 MokManager: list Extended Key Usage OIDs
Signed-off-by: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
2016-09-21 13:32:53 -04:00
Gary Lin
903674a2c4 MokManager: free new_data after use
new_data in write_db() wasn't freed after SetVariable.

Signed-off-by: Gary Lin <glin@suse.com>
2016-09-09 12:07:26 -04:00
Gary Lin
e21068b499 MokManager: Try APPEND_WRITE first
Try to append the MOK/MOKX list first and then fallback to the normal
SetVariable if the firmware doesn't support EFI_VARIABLE_APPEND_WRITE.

Signed-off-by: Gary Lin <glin@suse.com>
2016-09-09 12:07:26 -04:00
Gary Lin
5597a493e2 MokManager: Remove the usage of APPEND_WRITE
We got the bug report about the usage of APPEND_WRITE that may cause the
failure when writing a variable in Lenovo machines. Although
EFI_VARIABLE_APPEND_WRITE already exists in the UEFI spec for years,
unfortunately, some vendors just ignore it and never implement the
attribute. This commit removes the usage of EFI_VARIABLE_APPEND_WRITE to
make MokManager work on those machines.

https://github.com/rhinstaller/shim/issues/55

Signed-off-by: Gary Lin <glin@suse.com>
2016-09-09 12:07:26 -04:00
Gary Lin
e571428e21 Update to openssl to 1.0.2e
Also update Cryptlib to edk2 r19218
- Undefine NO_BUILTIN_VA_FUNCS in Cryptlib/OpenSSL/ for x86_64 to use
  the gcc builtins and remove all EFIAPI from the functions
- Move the most of defines into the headers instead of Makefile
- Remove the global variable 'timeval'
- Remove the unused code: crypto/pqueue/* and crypto/ts/*
- Include bn.h in MokManager.c due to the changes in openssl

Signed-off-by: Gary Lin <glin@suse.com>
2016-09-06 15:05:34 -04:00
Peter Jones
1c0b567fd9 MokManager: Fix a -Wsign-compare bug on i?86
My favorite part of -Wsign-compare is how it shows different results on
different arches for no obvious reason.

Signed-off-by: Peter Jones <pjones@redhat.com>
2015-11-17 11:39:21 -05:00
Peter Jones
6d4803a1c0 MokManager: Nerf SHA-1 again for actual hashes and signatures.
Nobody should be deploying SHA-1.  No hardware deploys it, and the rate
of change on https://en.wikipedia.org/wiki/SHA-1#Attacks is wildly
uninspiring.

Signed-off-by: Peter Jones <pjones@redhat.com>
2015-06-16 11:46:14 -04:00
Gary Ching-Pang Lin
0807ec7cec MokManager: fix comparison between signed and unsigned integer
Patch from Johannes Segitz <jsegitz@suse.com>
2015-06-16 11:46:14 -04:00
Gary Ching-Pang Lin
efa9c47690 MokManager: Discard the list contains an invalid signature
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:46:14 -04:00
Gary Ching-Pang Lin
439f031711 MokManager: Support SHA224, SHA384, and SHA512
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:46:14 -04:00
Gary Ching-Pang Lin
7db9f7c71c MokManager: Add more key list safe checks
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:46:14 -04:00
Gary Ching-Pang Lin
ab9d755484 MokManager: fix the return value and type
There are some functions that the return value and the type
didn't match.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
8bd6b9cd5a MokManager: Support SHA1 hash in MOK
Add SHA1 hash support and amend the code to make it easier to support
other SHA digests.
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
cd15cc0efc MokManager: fix the hash list counting in delete
match_hash() requests the number of keys in a list and it was
mistakenly replaced with the size of the Mok node. This would
made MokManager to remove the whole Mok node instead of one
hash.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
8449925662 MokManager: calculate the variable size correctly
MokSize of the hash signature list includes the owner GUID,
so we should not add the 16bytes compensation.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
6068f51099 MokManager: Write the hash list properly
also return to the previous entry in the list

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
de2acfb43e MokManager: Match all hashes in the list
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
c0fd34674b MokManager: delete the hash properly
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
a462c735fa MokManager: show the hash list properly
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00
Gary Ching-Pang Lin
64c5066ce0 Support MOK blacklist
The new blacklist, MokListX, stores the keys and hashes that are
banned.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
2015-06-16 11:41:32 -04:00