Commit Graph

740 Commits

Author SHA1 Message Date
Jia Zhang
518d9029c2 Ignore *.hash
*.hash should be ignored by git status if ENABLE_SHIM_HASH is
configured.

Signed-off-by: Jia Zhang <zhang.jia@linux.alibaba.com>
2021-02-15 17:20:05 -05:00
Mathieu Trudel-Lapierre
545b4a194f Add mm/fb hashing to TODO, put that and related things under 'Reproducible builds' 2021-02-15 17:20:05 -05:00
Peter Jones
d211ab2435 Add fallback boot loop detection to TODO
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 17:20:05 -05:00
Peter Jones
5cd4ec44b8 Add some *more* TODO tasks.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 17:20:05 -05:00
Peter Jones
0024dc9ebf Add another unfortunate TODO entry.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 17:20:05 -05:00
Peter Jones
c5805d535f Add some more TODOs for shim 16
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 17:20:05 -05:00
Peter Jones
e943efbb5f Try to make scan-build.mk work without scan-build installed. 2021-02-15 17:20:05 -05:00
Peter Jones
f16c7dc632 Try to make coverity.mk work without cov-build installed. 2021-02-15 17:20:05 -05:00
Peter Jones
aa48ac96ba We're not using travis-build.sh any more.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 17:20:05 -05:00
Peter Jones
5fb5537fa9 sbat: make the includes work like everything else.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-15 18:40:07 +01:00
Peter Jones
cf7260d432 add an ascii strndup() implementation.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 13:24:06 -05:00
Peter Jones
b0a2ea0caa get_variable: always allocate a NUL character at the end.
Sometimes we're loading structures that are parsed in string-like ways,
but can't necessarily be trusted to be zero-terminated.  Solve that by
making sure we always have enough aligned, trailing zero bytes to always
have at least one NUL character, no matter which character type is being
parsed.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 13:15:12 -05:00
Peter Jones
94ad063e94 Add some linked list primitives.
This adds basic linked list structures, initializers, and iterators.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 12:58:23 -05:00
Peter Jones
be0612cd77 Add an example SBAT workflow document
Add a file that contains example workflows for SBAT and better illustrate
what type of content is expected to be present in both the .sbat section
and the SBAT authenticated EFI variable.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:29:18 -05:00
Jan Setje-Eilers
4eef10a6b3 Add Secure Boot Advanced Targeting (SBAT) specification document
SBAT is a new Generation Number Based Revocation meant to replace the DBX
Revocation List Files mechanism. It is more flexible and allow to revoke
sets of binaries, instead of having to list all of them as with the DBX.

Metadata that includes the vendor, product family, product, component,
version and generation are added to artifacts in a .sbat section. This
is protected by the digital signature and so it cannot be tampered.

Signed-off-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Gary Lin <glin@suse.com>
2021-02-13 11:29:18 -05:00
Peter Jones
6b8ef61a1a SBAT: parse a copy of the table that's got a NUL at the end
Right now we allocate the PE file's contents in RW memory, but hopefully
that won't always be the case.  Our SBAT parsing, however, very much
expects to be able to edit it.  We also don't actually know that shim's
.sbat section is loaded r/w, so we can't necessarily write there.

This patch copies the SBAT data to its own buffer, plus one NUL byte at
the end, so we can always be sure that will work.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:02:59 -05:00
Javier Martinez Canillas
ee8f7ed332 Add a function to parse the SBAT metadata from the .sbat section
Parse the SBAT [0] Version-Based Revocation Metadata that's contained in a
.sbat data section of the loaded PE binary. This information is used along
with data in a SBAT variable to determine if a EFI binary has been revoked.

[0]: https://github.com/rhboot/shim/blob/sbat/SBAT.md

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
2021-02-13 11:02:59 -05:00
Peter Jones
16732ad128 Add the beginning of .sbat parsing stuff
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:02:59 -05:00
Peter Jones
19f3b31f6a Add some more PE helpers we need for SBAT
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:02:59 -05:00
Peter Jones
dea41d4c27 Refactor some PE handling code
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:02:59 -05:00
Peter Jones
06e98a10f6 Move a bunch of PE-related stuff out of shim.c
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 11:02:59 -05:00
Peter Jones
186595864c
includes: add strchra() and strchrnula() impls
Unfortunately GNU-EFI doesn't currently implement ascii versions of
strchr() or strchrnul(), and we kind of need them, so add an
implementation here for now.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-13 00:04:13 +01:00
Peter Jones
4a4d73f73a
Remove my .syntastic_c_config, it doesn't belong in the repo.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-12 23:53:37 +01:00
Peter Jones
dd70785953 efi bins: add an easy way for vendors to add .sbat data
In cases where we accept vendor shim binaries with additional patches,
it may become necessary to identify those builds with additional SBAT
data.  When we consider such patches, we should be proactive in asking
vendors to include that data in the .sbat sections of their trusted EFI
binaries.

This patch adds any data in data/sbat.*.csv (after a quick sanitizing
pass) after data/sbat.csv in the .sbat section, so that no changes to
the upstream data/sbat.csv are ever required.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-12 19:27:21 +01:00
Javier Martinez Canillas
6d13718c80 Add a .sbat section to EFI binaries
The Secure Boot Advanced Targeting (SBAT) [0] is a Generation Number Based
Revocation mechanism that is meant to replace the DBX revocation file list.

Binaries must contain a .sbat data section that has a set entries, each of
them consisting of UTF-8 strings as comma separated values. Allow to embed
this information into the fwupd EFI binary at build time.

The SBAT metadata must contain at least two entries. One that defines the
SBAT version used and another one that defines the component generation.

This patch adds a sbat.csv that contains these two entries and downstream
users can override if additional entries are needed due changes that make
them diverge from upstream code and potentially add other vulnerabilities.

The same SBAT metadata is added to the fallback and MOK manager binaries
because these are built from the same shim source. These need to have SBAT
metadata as well to be booted if a .sbat section is mandatory.

[0]: https://github.com/rhboot/shim/blob/sbat/SBAT.md

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
2021-02-12 12:51:32 -05:00
Peter Jones
aed06cd1b8 github workflows: add the sbat branch to one PR builds run for
This adds the "sbat" branch to the list of branches where a build is
done if a PR is submitted against that branch.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-05 16:30:57 -05:00
Peter Jones
2b53e3d356 github workflows: Unify the x86 pull request build rules steps
This makes each of the f32/f33/f34 distro builds use the same steps to
do the build, as well as making each of them build both x64 and ia32
targets.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-05 16:30:57 -05:00
Peter Jones
edc08062f1 Fix pe.h -> peimage.h in /both/ places.
Signed-off-by: Peter Jones <pjones@redhat.com>
2021-02-01 16:18:05 -05:00
Peter Jones
1fa0b8d50a Renaming PeImage.h to pe.h wasn't actually a good idea.
I renamed PeImage.h to pe.h when I de-capitalized them, but this turns
out to be a bad idea because we already have a pe.h on the SBAT branch.
Woops.

This moves it to peimage.h

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-01-29 18:45:00 -05:00
Peter Jones
0172d43507 Work around some clang-format oddnesses
In the version of clang-format I've got locally[0],
WhitespaceSensitiveMacros seems to only work sometimes.  That means that
if we ever run it on some particular things, it could seriously mess up
a bunch of our debugging output.  That's not great.

In this patch, I've gone ahead and run clang-format on all the macros
that use __LINE__, which are the obvious places this is dangerous, and
then audited the result and fixed anything that's broken (including a
couple of places where it was already broken.)

[0] random:~/devel/github.com/shim/clang-format$ clang-format --version
    clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-01-29 18:24:57 -05:00
Peter Jones
0789f48d70 Always use lower case for our local include file names.
clang-format doesn't allow you to specify an include sort order, and
just assumes asciibetical is a pretty good order, which doesn't work as
well as you would hope.

This makes them all lower case so they don't need to be re-sorted.

I also went through and checked that we're using quoted local includes
at all the appropriate places.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-01-29 18:24:57 -05:00
Peter Jones
5e3d9cd998 Add a .clang-format file.
There's clearly not enough conformance to a single coding style here.
To some extent that can't really change - I don't intend to adopt edk2's
style for the main codebase, but re-formatting the code we borrow from
edk2 would be insane.

This commit adds a .clang-format file, to be used on new files as such:

clang-format --style=file -i foo.c

It can also be used when new free-standing code is added to existing
files, using the clang-format --lines= option.

The starting style in this is pretty close to the Linux kernel style,
with a couple of minor modifications.

Signed-off-by: Peter Jones <pjones@redhat.com>
2021-01-29 18:24:57 -05:00
Paul Moore
4b0a61dc9a shim: compile time option to bypass the ExitBootServices() check
On systems where a second stage bootloader is not used, and the Linux
Kernel is booted directly from shim, shim's ExitBootServices() hook
can cause problems as the kernel never calls the shim's verification
protocol.  In this case calling the shim verification protocol is
unnecessary and redundant as shim has already verified the kernel
when shim loaded the kernel as the second stage loader.

This functionality is disabled by default and must be enabled via the
DISABLE_EBS_PROTECTION macro/define at build time.

Signed-off-by: Paul Moore <pmoore2@cisco.com>
2021-01-29 15:53:34 -05:00
Peter Jones
1f123ac235 Try to kick the github PR workflow...
Signed-off-by: Peter Jones <pjones@redhat.com>
2020-12-11 19:49:04 -05:00
Peter Jones
636cba95b5 Split up push and PR CI/CD and build all patches in series on PRs
Signed-off-by: Peter Jones <pjones@redhat.com>
2020-12-10 16:02:35 -05:00
Peter Jones
5fd3316bb4 Use github actions for CI builds
This could still use a bit of work, but it's better than Travis
failing...

Signed-off-by: Peter Jones <pjones@redhat.com>
2020-12-07 15:09:09 -05:00
James Bottomley
5ec906ac6c Fix incorrect allocation size for EV_EFI_BOOT_SERVICES_APPLICATION events
sizeof(EFI_IMAGE_LOAD_EVENT) needs to represent the size of the header
so we can add the actual device path size to it to compute the event.

Signed-off-by: Peter Jones <pjones@redhat.com>
2020-10-15 19:38:52 -04:00
Peter Jones
64a18c4ea6 hexdump.h: fix arithmetic error.
When I modified the hexdumper to help debug MokListRT mirroring not
working because of PcdMaxVolatileVariableSize being tiny, I
inadvertently added something that is effectively:

hexdump(..., char *buf, ..., int position)
{
	unsigned long begin = (position % 16);
	unsigned long i;
	...
	for (i = 0; i < begin; i++) {
		...
	}
	...
}

Unfortunately, in c if 0x8 is set in position, that means begin is
0xfffffffffffff8, because signed integer math is horrifying:

include/hexdump.h:99:vhexdumpf() &data[offset]:0x9E77E6BC size-offset:0x14
include/hexdump.h:15:prepare_hex() position:0x9E77E6BC
include/hexdump.h:17:prepare_hex() before:0xFFFFFFFFFFFFFFFC size:0x14
include/hexdump.h:19:prepare_hex() before:0xFFFFFFFFFFFFFFFC after:0x0
include/hexdump.h:21:prepare_hex() buf:0x000000009E77E2BC offset:0 &buf[offset]:0x000000009E77E2BC

Woops.

This could further have been prevented in /some/ cases by simply not
preparing the hexdump buffer when "verbose" is disabled.

This patch makes "pos" be unsigned in all cases, and also checks for
verbose in vhexdumpf() and simply returns if it is 0.

Signed-off-by: Peter Jones <pjones@redhat.com>
2020-10-15 19:17:35 -04: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
Javier Martinez Canillas
74b05de7d1 Fix buffer overrun due DEFAULT_LOADER length miscalculation
The DEFAULT_LOADER is a UCS-2 string and the StrLen() function returns the
number of UCS-2 encoded characters in the string. But the allocated memory
is in bytes, so only half of the needed memory to store it is allocated.

This leads to a buffer overrun when the StrCpy() function attempts to copy
the DEFAULT_LOADER to the allocated buffer.

Fixes: 354bd9b1931 ("Actually check for errors from set_second_stage()")
Reported-by: Stuart Hayes <stuart_hayes@dell.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
2020-09-09 15:56:39 -04:00
Peter Jones
63f7943dbe mirror_one_mok_variable(): round allocation up to a full page
The code currently computes the size of the MoK variable in ram and
rounds up to a full page, but then actually allocates the exact size,
rather than the rounded up version.  This should be completely safe, but
the intent was to round up to at least the page size boundary, and to
always guarantee rounding up /some/, to ensure extra 0-bytes at the end
of the buffer.

Signed-off-by: Peter Jones <pjones@redhat.com>
2020-08-04 12:49:30 -04:00
Peter Jones
65be350308 Implement lennysz's suggestions for MokListRT
Signed-off-by: Peter Jones <pjones@redhat.com>
2020-07-25 22:14:08 -04:00
Peter Jones
fecc2dfb8e Also use a config table to mirror mok variables.
Everything was going just fine until I made a vendor_db with 17kB of
sha256 sums in it.  And then the same source tree that had worked fine
without that threw errors and failed all over the place.  I wrote some
code to diagnose the problem, and of course it was a failure in
mirroring MokList to MokListRT.

As Patrick noted in 741c61abba7, some systems have obnoxiously low
amounts of variable storage available:

mok.c:550:import_mok_state() BS+RT variable info:
		     MaximumVariableStorageSize:0x000000000000DFE4
		     RemainingVariableStorageSize:0x000000000000D21C
		     MaximumVariableSize:0x0000000000001FC4

The most annoying part is that on at least this edk2 build,
SetVariable() /does actually appear to set the variable/, but it returns
EFI_INVALID_PARAMETER.  I'm not planning on relying on that behavior.

So... yeah, the largest *volatile* (i.e. RAM only) variable this edk2
build will let you create is less than two pages.  It's only got 7.9G
free, so I guess it's feeling like space is a little tight.

We're also not quite preserving that return code well enough for his
workaround to work.

New plan.  We try to create variables the normal way, but we don't
consider not having enough space to be fatal.  In that case, we create
an EFI_SECURITY_LIST with one sha256sum in it, with a value of all 0,
and try to add that so we're sure there's /something/ there that's
innocuous.  On systems where the first SetVariable() /
QueryVariableInfo() lied to us, the correct variable should be there,
otherwise the one with the zero-hash will be.

We then also build a config table to hold this info and install that.

The config table is a packed array of this struct:

struct mok_variable_config_entry {
       CHAR8 name[256];
       UINT64 data_size;
       UINT8 data[];
};

There will be N+1 entries, and the last entry is all 0 for name and
data_size.  The total allocation size will always be a multiple of 4096.
In the typical RHEL 7.9 case that means it'll be around 5 pages.

It's installed with this guid:

c451ed2b-9694-45d3-baba-ed9f8988a389

Anything that can go wrong will.

Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: not yet, I don't want people to read this before Wednesday.
Signed-off-by: Peter Jones <pjones@redhat.com>
2020-07-25 22:14:08 -04:00
Peter Jones
fc4368fed5 Improve debug output some
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#213
2020-07-25 22:14:08 -04:00
Peter Jones
705d47ac2c Make openssl accept the right set of KU/EKUs
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#211
2020-07-23 22:22:04 -04:00
Peter Jones
76c0447e20 Handle binaries with multiple signatures.
This adds support for multiple signatures.  It first tries validating
the binary by hash, first against our dbx lists, then against our db
lists.  If it isn't allowed or rejected at that step, it continues to
the normal routine of checking all the signatures.

At this point it does *not* reject a binary just because a signature is
by a cert on a dbx list, though that will override any db list that
certificate is listed on.  If at any point any assertion about the
binary or signature list being well-formed fails, the binary is
immediately rejected, though we do allow skipping over signatures
which have an unsupported sig->Hdr.wCertificateType.

Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#210
2020-07-23 22:22:04 -04:00
Peter Jones
dd3a5d7125 Add support for vendor_db built-in shim authorized list.
Potential new signing strategies ( for example signing grub, fwupdate
and vmlinuz with separate certificates ) require shim to support a
vendor provided bundle of trusted certificates and hashes, which allows
shim to trust EFI binaries matching either certificate by signature or
hash in the vendor_db.  Functionality is similar to vendor_dbx.

This also improves the mirroring quite a bit.
Upstream: pr#206
2020-07-23 22:22:04 -04:00
Peter Jones
7d542805ba Make cert.S not impossible to read.
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#206
2020-07-23 20:53:24 -04:00
Peter Jones
a7f9911b77 Fix a broken tpm type
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#212
2020-07-23 20:53:24 -04:00
Peter Jones
c186bdddaa simple_file: fix uninitialized variable/unchecked return
Signed-off-by: Peter Jones <pjones@redhat.com>
Upstream: pr#212
2020-07-23 20:53:24 -04:00