Appended Signature Secure Boot Support for PowerPC
v5 cover Appended Signature Secure Boot Support for PowerPC
This patch set contains v5 the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.
The v3 series is at
https://lists.gnu.org/archive/html/grub-devel/2025-06/msg00049.html
Changes since v3:
- Daniel Kiper review comments addressed:
- v5 patch 1: corrected the unreliable check conditions.
- v5 patch 3: added the GRUB signing process for single and multi signature.
- v5 patch 5: expanded the commit message .
- v5 patch 9 and 10: dropping some more N_() from various cryptic error
messages.
- v5 patch 11: corrected the coding style, used grub_crypto_pk_rsa and added the explicit module dependencies using depends.
- v5 patch 13: rename appended signature GRUB commands with short names.
- v5 patch 15: merged from v3 patch 17 and 18
- v5 patch 16: corrected the coding style and GRUB_PKS_* constant are moved to "include/grub/efi/pks.h"
Also, fixed the warrnings and errors
Changes since v4:
- v5 patch 1: fixed the build error for Windows builds.
- v5 rsa_pad was removed from libgcrypt-1.11. So, removed patch-4 (factor out rsa_pad).
- rebased with libgcrypt-1.11
Linux on Power LPAR secure boot ensures the integrity of the Linux boot stack. The hypervisor and partition firmware are part of the core root of trust. The partition firmware verifies the signature on the GRUB image before handing control to GRUB. Similarly, GRUB verifies the signature on the kernel image before booting the OS. This ensures that every image running at the boot time is verified and trusted. UEFI platforms relies on PECOFF based signature scheme. Since Power is not a UEFI platform, an alternative mechanism is needed. Power already uses appended signatures on the Linux Kernel, and is now extended to sign the grub as well.
Linux on Power also allows multiple signers, and if any one of the signature passes, then the image passes the validation. Appended signature scheme uses CMS structure to contain signatures. On Power, the multiple signature support relies on the multiple signers features already supported by CMS standards. It does require that all the signers should sign at the same time and are not allowed to add or remove the signatures randomly.
By default, Linux LPAR secure boot uses static key management[1]. This means that each image embeds the keys it needs to verify the image it loads. For example, the keys used to verify the GRUB image are built into the firmware image. Similarly, the keys used for verifying the kernel image are built into the GRUB image. These are pre-defined keys and they cannot be modified at runtime. The drawback of this approach is that key rotations results in both firmware and OS updates. This is where dynamic key management is useful.
An admin can switch from static keys to dynamic keys by coordinating with Hardware Management Console(HMC) admin and enabling the required flags for the given LPAR.
The dynamic key management relies on the Platform KeyStore(PKS)[2] storage allocation for each LPAR with individually managed access controls to store sensitive information securely. Once switched to dynamic keys, HMC advertises this flag to the PowerVM, which then initializes the PKS with the default secvars. It also creates a variable SB_VERSION that represents the secure boot key management mode. The default secvars are used by Partition firmware, grub and the linux kernel to reads keys for verification. These secvars can be managed by user interface exposed via linux kernel. The linux kernel already supports this interface and it is available in the upstream kernel.
This patchset adds the appended signature support both for signing and verification and the key management to the grub component. The whole patchset can be split into following four main parts:
The series has following four main parts:
1.) Sign grub.elf with an appended signature. (Patches 1 - 3)
These patches provide some infrastructure and documentation for signing grub's core.elf with a Linux-kernel-module style appended signature.
An appended signature is a 'dumb' signature over the contents of a file. (It is distinct from schemes like Authenticode that are aware of the structure of the file and only sign certain parts.) The signature is wrapped in a PKCS#7 message, and is appended to the signed file along with some metadata and a magic string. The signatures are validated against a public key which is usually provided as an x509 certificate.
Because some platforms, such as powerpc-ieee1275, may load grub from a raw disk partition rather than a filesystem, we extend grub-install to add an ELF note that allows us to specify the size and location of the signature.
2.) Enable appended signature verification using builtin keys (Patches 4 - 13).
Part of a secure boot chain is allowing grub to verify the boot kernel. For UEFI platforms, this is usually delegated to the shim. However, for platforms that do not implement UEFI, an alternative scheme is required.
This part teaches grub how to verify Linux kernel-style appended signatures. Kernels on powerpc are already signed with this scheme and can be verified by IMA for kexec.
As PKCS#7 messages and x509 certificates are both based on ASN.1, we import libtasn1 to parse them. Because ASN.1 isn't self-documenting, we import from GNUTLS the information we need to navigate their structure.
This section is composed of the following patches:
- patches 4 and 5 are small refactorings.
- patch 6 allows x509 certificates to be built in to the grub core in much the same way as PGP keys.
- patch 7 brings in the code from GNUTLS that allows us to parse PKCS#7 and x509 with libtasn1.
- patch 8, 9 and 10 is our ASN1 node, PKCS#7 and x509 parser. They're minimal and fairly strict parsers that extract only the bits we need to verify the signatures.
- patch 11 is the guts of the appended signature verifier. It uses the verifier infrastructure like pgp, and adds a number of user-friendly commands that mirror the pgp module.
- patch 12 adds tests, and patch 13 adds documentation.
3.) Enable lockdown if secure boot is enabled (Patch 14)
If the 'ibm,secure-boot' property of the root node is 2 or greater, enter lockdown.The main appended signature module now tests for lockdown to enter 'forced' mode.
4.) Enable accessing keys dynamically from Platform KeyStore (Patch 15 - 21)
This part teaches grub how to read db and dbx variables from platform keystore using client interface call then load keys from those two variable, and use it to verify Linux kernel.
This section is composed of the following patches:
- patch 15 is an exposes an interface in ieee1275 for reading secure boot variable db and dbx from Platform Keystore. Read secure boot variables such as db and dbx from PKS and extract certificates from ESL.
- patch 16 is create the db and dbx lists from PKS.
- patch 17 is verify the kernel using db and dbx lists
- patch 18 sets the use_static_keys flag if DB not available in PKS, and patch 19 is reads the DB default keys from ELF Note and store it in trusted lists if use_static_keys flag is set.
- patch 20 adds db and dbx commands, and patch 21 adds documentation.
Thanks to Daniel Kiper for providing review comments on v3.
I've pushed this all to
https://github.com/SudhakarKuppusamy1/grub/tree/appendedsig-2.13
[1]https://www.ibm.com/docs/en/linux-on-systems?topic=servers-guest-secure-boot-static-keys [2]https://community.ibm.com/community/user/power/blogs/chris-engel1/2020/11/20/powervm-introduces-the-platform-keystore
Daniel Axtens (5):
docs/grub: Document signing GRUB under UEFI
crypto: Move storage for grub_crypto_pk_* to crypto.c appended signatures: Verification tests appended signatures: Documentation
ieee1275: Enter lockdown based on /ibm,secure-boot
Sudhakar Kuppusamy (16):
powerpc-ieee1275: Add support for signing GRUB with an appended
signature
docs/grub: Document signing GRUB with an appended signature
pgp: Rename OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY
grub-install: Support embedding x509 certificates appended signatures: Import GNUTLS's ASN.1 description files appended signatures: Parse ASN1 node
appended signatures: Parse PKCS#7 signedData
appended signatures: Parse X.509 certificates appended signatures: Support verifying appended signatures
ieee1275: Read the DB and DBX secure boot variables
appendedsig: Create db and dbx lists
appendedsig: Verify the kernel using db and dbx lists
powerpc_ieee1275: Introduce use_static_keys flag
appendedsig: Read default DB keys from the ELF Note
appendedsig: Introduce GRUB commands to access db and dbx
appendedsig: Documentation
docs/grub.texi | 334 +++- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 24 + grub-core/commands/appendedsig/appendedsig.c | 1412 +++++++++++++++++ grub-core/commands/appendedsig/appendedsig.h | 108 ++ grub-core/commands/appendedsig/asn1util.c | 96 ++ .../commands/appendedsig/gnutls_asn1_tab.c | 148 ++ grub-core/commands/appendedsig/pkcs7.c | 454 ++++++ .../commands/appendedsig/pkix_asn1_tab.c | 485 ++++++ grub-core/commands/appendedsig/x509.c | 953 +++++++++++ grub-core/commands/pgp.c | 6 +- grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 54 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 ++ .../kern/powerpc/ieee1275/platform_keystore.c | 353 +++++ grub-core/lib/crypto.c | 4 + grub-core/tests/appended_signature_test.c | 250 +++ grub-core/tests/appended_signatures.h | 975 ++++++++++++ grub-core/tests/lib/functional_test.c | 5 + include/grub/crypto.h | 1 + include/grub/efi/pks.h | 112 ++ include/grub/file.h | 2 + include/grub/ieee1275/ieee1275.h | 3 + include/grub/kernel.h | 3 +- include/grub/lockdown.h | 3 +- include/grub/powerpc/ieee1275/ieee1275.h | 21 + .../grub/powerpc/ieee1275/platform_keystore.h | 127 ++ include/grub/util/install.h | 13 +- include/grub/util/mkimage.h | 4 +- util/grub-install-common.c | 43 +- util/grub-mkimage.c | 33 +- util/grub-mkimagexx.c | 40 +- util/mkimage.c | 50 +- 33 files changed, 6212 insertions(+), 46 deletions(-) create mode 100644 grub-core/commands/appendedsig/appendedsig.c create mode 100644 grub-core/commands/appendedsig/appendedsig.h create mode 100644 grub-core/commands/appendedsig/asn1util.c create mode 100644 grub-core/commands/appendedsig/gnutls_asn1_tab.c create mode 100644 grub-core/commands/appendedsig/pkcs7.c create mode 100644 grub-core/commands/appendedsig/pkix_asn1_tab.c create mode 100644 grub-core/commands/appendedsig/x509.c create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c create mode 100644 grub-core/tests/appended_signature_test.c create mode 100644 grub-core/tests/appended_signatures.h create mode 100644 include/grub/efi/pks.h create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 1/21 powerpc-ieee1275: Add support for signing GRUB with an appended signature
Add infrastructure to allow firmware to verify the integrity of GRUB by use of a Linux-kernel-module-style appended signature. We initially target powerpc-ieee1275, but the code should be extensible to other platforms.
Usually these signatures are appended to a file without modifying the ELF file itself. (This is what the 'sign-file' tool does, for example.) The verifier loads the signed file from the file system and looks at the end of the file for the appended signature. However, on powerpc-ieee1275 platforms, the bootloader is often stored directly in the PReP partition as raw bytes without a file-system. This makes determining the location of an appended signature more difficult.
To address this, we add a new ELF note.
The name field of shall be the string "Appended-Signature", zero-padded to 4 byte alignment. The type field shall be 0x41536967 (the ASCII values for the string "ASig"). It must be the final section in the ELF binary.
The description shall contain the appended signature structure as defined by the Linux kernel. The description will also be padded to be a multiple of 4 bytes. The padding shall be added before the appended signature structure (not at the end) so that the final bytes of a signed ELF file are the appended signature magic.
A subsequent patch documents how to create a GRUB core.img validly signed under this scheme.
--- include/grub/util/install.h | 10 ++++++++-- include/grub/util/mkimage.h | 4 ++-- util/grub-install-common.c | 24 +++++++++++++++++++--- util/grub-mkimage.c | 19 ++++++++++++++++++ util/grub-mkimagexx.c | 40 ++++++++++++++++++++++++++++++++++++- util/mkimage.c | 9 ++++++--- 6 files changed, 95 insertions(+), 11 deletions(-)
include/grub/util/install.h +8 -2
@@ -69,9 +69,14 @@ N_("disable shim_lock verifier"), 0 }, \ {"disable-cli" ,GRUB_INSTALL_OPTIONS_DISABLE_CLI ,0 ,0 , \ N_("disabled command line interface access"), 0 }, \ + { "appended-signature-size", GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE, \ + "SIZE", 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 1}, \ { "verbose", 'v', 0, 0, \ N_("print verbose messages."), 1 } +/* The max_appended_signature_size = 4096 bit key signature size x number of signature. */ +#define GRUB_APPSIG_SIZE_MAX ((unsigned int) (824 * 200)) + int grub_install_parse (int key, char *arg);
@@ -132,7 +137,8 @@ enum grub_install_options {GRUB_INSTALL_OPTIONS_DTB ,GRUB_INSTALL_OPTIONS_SBAT ,GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK , - GRUB_INSTALL_OPTIONS_DISABLE_CLI +GRUB_INSTALL_OPTIONS_DISABLE_CLI , + GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE };extern char *grub_install_source_directory;
@@ -192,7 +198,7 @@ grub_install_generate_image (const char *dir, const char *prefix,size_t npubkeys, char *config_path, const struct grub_install_image_target_desc *image_target, - int note, + int note, size_t appsig_size, grub_compression_t comp, const char *dtb_file, const char *sbat_path, const int disable_shim_lock, const int disable_cli);
include/grub/util/mkimage.h +2 -2
@@ -51,12 +51,12 @@ grub_mkimage_load_image64 (const char *kernel_path, const struct grub_install_image_target_desc *image_target);void grub_mkimage_generate_elf32 (const struct grub_install_image_target_desc *image_target, -int note,char *sbat,char **core_img,size_t *core_size, +int note,char *sbat,size_t appsig_size,char **core_img,size_t *core_size,Elf32_Addr target_addr,struct grub_mkimage_layout *layout);void grub_mkimage_generate_elf64 (const struct grub_install_image_target_desc *image_target, -int note,char *sbat,char **core_img,size_t *core_size, +int note,char *sbat,size_t appsig_size,char **core_img,size_t *core_size,Elf64_Addr target_addr,struct grub_mkimage_layout *layout);
util/grub-install-common.c +21 -3
@@ -467,10 +467,13 @@ static char *sbat;static int disable_shim_lock;static grub_compression_t compression;static int disable_cli; +static size_t appsig_size;int grub_install_parse (int key,char *arg) { +const char *end; +switch (key) {case GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS :
@@ -571,6 +574,20 @@ grub_install_parse (int key, char *arg)grub_util_error (_ ("Unrecognized compression `%s'" ), arg);case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE :return 1 ; +case GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE : + appsig_size =grub_strtoul (arg, &end,10 ); +if (*arg =='\0' || *end !='\0' ) + { +grub_util_error (_ ("non-numeric or invalid appended signature size `%s'" ), arg); +return 0 ; + } +else if (appsig_size ==0 || appsig_size >GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %u byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 0 ; + } +return 1 ;default :return 0 ; }
@@ -683,9 +700,10 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix, *p ='\0' ;grub_util_info ("grub-mkimage --directory '%s' --prefix '%s' --output '%s'" -" --format '%s' --compression '%s'%s%s%s%s\n" , +" --format '%s' --compression '%s'" +" --appended-signature-size %zu %s %s %s %s\n" , dir, prefix, outname, - mkimage_target, compnames[compression], + mkimage_target, compnames[compression], appsig_size, note ?" --note" :"" , disable_shim_lock ?" --disable-shim-lock" :"" , disable_cli ?" --disable-cli" :"" , s);
@@ -698,7 +716,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,grub_install_generate_image (dir, prefix, fp, outname, modules.entries, memdisk_path, pubkeys, npubkeys, config_path, tgt, - note, compression, dtb, sbat, + note, appsig_size, compression, dtb, sbat, disable_shim_lock, disable_cli);while (dc--)grub_install_pop_module ();
util/grub-mkimage.c +19 -0
@@ -84,6 +84,7 @@ static struct argp_option options[] = { {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0}, {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0}, {"disable-cli", GRUB_INSTALL_OPTIONS_DISABLE_CLI, 0, 0, N_("disable command line interface access"), 0}, + {"appended-signature-size", 'S', N_("SIZE"), 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 0}, {"verbose", 'v', 0, 0, N_("print verbose messages."), 0}, { 0, 0, 0, 0, 0, 0 } };
@@ -130,6 +131,7 @@ struct argumentsint note;int disable_shim_lock;int disable_cli; +size_t appsig_size;const struct grub_install_image_target_desc *image_target;grub_compression_t comp; };
@@ -140,6 +142,7 @@ argp_parser (int key, char *arg, struct argp_state *state)/* Get the input argument from argp_parse, which we know is a pointer to our arguments structure. */ struct arguments *arguments = state->input; +const char *end;switch (key) {
@@ -172,6 +175,21 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->note =1 ;break ; +case 'S' : + arguments->appsig_size =grub_strtoul (arg, &end,10 ); +if (*arg =='\0' || *end !='\0' ) + { +grub_util_error (_ ("non-numeric or invalid appended signature size `%s'" ), arg); +return 1 ; + } +else if (arguments->appsig_size ==0 || arguments->appsig_size >GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %u byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 1 ; + } +break ; +case 'm' :if (arguments->memdisk)free (arguments->memdisk);
@@ -330,6 +348,7 @@ main (int argc, char *argv[]) arguments.memdisk, arguments.pubkeys, arguments.npubkeys, arguments.config, arguments.image_target, arguments.note, + arguments.appsig_size, arguments.comp, arguments.dtb, arguments.sbat, arguments.disable_shim_lock, arguments.disable_cli);
util/grub-mkimagexx.c +39 -1
@@ -115,6 +115,14 @@ struct grub_sbat_note {char name[ALIGN_UP (sizeof (GRUB_SBAT_NOTE_NAME ),4 )]; }; +#define GRUB_APPENDED_SIGNATURE_NOTE_NAME "Appended-Signature" +#define GRUB_APPENDED_SIGNATURE_NOTE_TYPE 0x41536967/* "ASig" */ +struct grub_appended_signature_note +{ +Elf32_Nhdr header; +char name[ALIGN_UP (sizeof (GRUB_APPENDED_SIGNATURE_NOTE_NAME ),4 )]; +}; +static int is_relocatable (const struct grub_install_image_target_desc *image_target) {
@@ -216,7 +224,7 @@ grub_arm_reloc_jump24 (grub_uint32_t *target, Elf32_Addr sym_addr)void SUFFIX (grub_mkimage_generate_elf ) (const struct grub_install_image_target_desc *image_target, -int note,char *sbat,char **core_img,size_t *core_size, +int note,char *sbat,size_t appsig_size,char **core_img,size_t *core_size,Elf_Addr target_addr,struct grub_mkimage_layout *layout) {
@@ -237,6 +245,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc footer_size +=ALIGN_UP (sizeof (struct grub_sbat_note ) + layout->sbat_size,4 ); } +if (appsig_size) + { + phnum++; + footer_size +=ALIGN_UP (sizeof (struct grub_appended_signature_note ),4 ); + } +if (image_target->id !=IMAGE_LOONGSON_ELF ) phnum +=2 ;
@@ -518,6 +532,30 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_descmemcpy (note_ptr->name,GRUB_SBAT_NOTE_NAME ,sizeof (GRUB_SBAT_NOTE_NAME ));memcpy ((char *)(note_ptr +1 ), sbat, layout->sbat_size); + phdr++; + phdr->p_type =grub_host_to_target32 (PT_NOTE ); + phdr->p_flags =grub_host_to_target32 (PF_R ); + phdr->p_align =grub_host_to_target32 (image_target->voidp_sizeof); + phdr->p_vaddr =0 ; + phdr->p_paddr =0 ; + phdr->p_filesz =grub_host_to_target32 (note_size); + phdr->p_memsz =0 ; + phdr->p_offset =grub_host_to_target32 (header_size + program_size + footer_offset); + footer += note_size; + footer_offset += note_size; + } + +if (appsig_size) + { +int note_size =ALIGN_UP (sizeof (struct grub_appended_signature_note ) + appsig_size,4 ); +struct grub_appended_signature_note *note_ptr = (struct grub_appended_signature_note *) footer; + + note_ptr->header.n_namesz =grub_host_to_target32 (sizeof (GRUB_APPENDED_SIGNATURE_NOTE_NAME )); +/* Needs to sit at the end, so we round this up and sign some zero padding. */ + note_ptr->header.n_descsz =grub_host_to_target32 (ALIGN_UP (appsig_size,4 )); + note_ptr->header.n_type =grub_host_to_target32 (GRUB_APPENDED_SIGNATURE_NOTE_TYPE ); +strcpy (note_ptr->name,GRUB_APPENDED_SIGNATURE_NOTE_NAME ); + phdr++; phdr->p_type =grub_host_to_target32 (PT_NOTE ); phdr->p_flags =grub_host_to_target32 (PF_R );
util/mkimage.c +6 -3
@@ -885,7 +885,7 @@ grub_install_generate_image (const char *dir, const char *prefix, char *memdisk_path, char **pubkey_paths, size_t npubkeys, char *config_path, const struct grub_install_image_target_desc *image_target, - int note, grub_compression_t comp, const char *dtb_path, + int note, size_t appsig_size, grub_compression_t comp, const char *dtb_path, const char *sbat_path, int disable_shim_lock, int disable_cli) {
@@ -946,6 +946,9 @@ grub_install_generate_image (const char *dir, const char *prefix,if (sbat_path !=NULL && (image_target->id !=IMAGE_EFI && image_target->id !=IMAGE_PPC ))grub_util_error (_ ("SBAT data can be added only to EFI or powerpc-ieee1275 images" )); +if (appsig_size !=0 && image_target->id !=IMAGE_PPC ) +grub_util_error (_ ("appended signature can be support only to powerpc-ieee1275 images" )); +if (disable_shim_lock) total_module_size +=sizeof (struct grub_module_header );
@@ -1833,10 +1836,10 @@ grub_install_generate_image (const char *dir, const char *prefix,else target_addr = image_target->link_addr;if (image_target->voidp_sizeof ==4 ) -grub_mkimage_generate_elf32 (image_target, note, sbat, &core_img, &core_size, +grub_mkimage_generate_elf32 (image_target, note, sbat, appsig_size, &core_img, &core_size, target_addr, &layout);else -grub_mkimage_generate_elf64 (image_target, note, sbat, &core_img, &core_size, +grub_mkimage_generate_elf64 (image_target, note, sbat, appsig_size, &core_img, &core_size, target_addr, &layout); }break ;
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 01/21] powerpc-ieee1275: Add support for signing GRUB with an appended signature
Quoted 25 lines · Sudhakar Kuppusamy
Add infrastructure to allow firmware to verify the integrity of GRUB by use of a Linux-kernel-module-style appended signature. We initially target powerpc-ieee1275, but the code should be extensible to other platforms.
Usually these signatures are appended to a file without modifying the ELF file itself. (This is what the 'sign-file' tool does, for example.) The verifier loads the signed file from the file system and looks at the end of the file for the appended signature. However, on powerpc-ieee1275 platforms, the bootloader is often stored directly in the PReP partition as raw bytes without a file-system. This makes determining the location of an appended signature more difficult.
To address this, we add a new ELF note.
The name field of shall be the string "Appended-Signature", zero-padded to 4 byte alignment. The type field shall be
0x41536967(the ASCII values for the string "ASig"). It must be the final section in the ELF binary.The description shall contain the appended signature structure as defined by the Linux kernel. The description will also be padded to be a multiple of 4 bytes. The padding shall be added before the appended signature structure (not at the end) so that the final bytes of a signed ELF file are the appended signature magic.
Quoted 8 lines
A subsequent patch documents how to create a GRUB core.img validly signed under this scheme.
Signed-off-by Rashmica Gupta <[email protected]>Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 2/21 docs/grub: Document signing GRUB under UEFI
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
Before adding information about how grub is signed with an appended signature scheme, it's worth adding some information about how it can currently be signed for UEFI.
--- docs/grub.texi | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
docs/grub.texi +17 -1
@@ -8675,6 +8675,7 @@ environment variables and commands are listed in the same order. * Measured Boot:: Measuring boot components * Lockdown:: Lockdown when booting on a secure setup * TPM2 key protector:: Managing disk key with TPM2 key protector +* Signing GRUB itself:: Ensuring the integrity of the GRUB core image @end menu @node Authentication and authorisation
@@ -8755,7 +8756,7 @@ commands. GRUB's @file{core.img} can optionally provide enforcement that all files subsequently read from disk are covered by a valid digital signature. -This document does @strong{not} cover how to ensure that your +This section does @strong{not} cover how to ensure that your platform's firmware (e.g., Coreboot) validates @file{core.img}. If environment variable @code{check_signatures}
@@ -9363,6 +9364,21 @@ which increases the risk of password leakage during the process. Moreover, the superuser list must be well maintained, and the password used cannot be synchronized with LUKS key rotation. +@node Signing GRUB itself +@section Signing GRUB itself +To ensure a complete secure-boot chain, there must be a way for the code that +loads GRUB to verify the integrity of the core image. +This is ultimately platform-specific and individual platforms can define their +own mechanisms. However, there are general-purpose mechanisms that can be used +with GRUB. +@section Signing GRUB for UEFI secure boot +On UEFI platforms, @file{core.img} is a PE binary. Therefore, it can be signed +with a tool such as @command{pesign} or @command{sbsign}. Refer to the +suggestions in @pxref{UEFI secure boot and shim} to ensure that the final +image works under UEFI secure boot and can maintain the secure-boot chain. It +will also be necessary to enroll the public key used into a relevant firmware +key database. + @node Platform limitations @chapter Platform limitations
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 3/21 docs/grub: Document signing GRUB with an appended signature
In reply to Sudhakar KuppusamySigning GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.
--- docs/grub.texi | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
docs/grub.texi +86 -0
@@ -9379,6 +9379,92 @@ image works under UEFI secure boot and can maintain the secure-boot chain. It will also be necessary to enroll the public key used into a relevant firmware key database. +@section Signing GRUB with an appended signature +The @file{core.img} itself can be signed with a Linux kernel module-style +appended signature. +To support IEEE1275 platforms where the boot image is often loaded directly +from a disk partition rather than from a file system, the @file{core.img} +can specify the size and location of the appended signature with an ELF +note added by @command{grub-install}. +An image can be signed this way using the @command{sign-file} command from +the Linux kernel: +@example +@group +# Signing a GRUB image with only one signer key in the appended signature: +# The grub.key is your private key and certificate.der is your GRUB signing public key +# kernel.der is your kernel signing public key. +# +# Determine the size of the appended signature. It depends on the signing certificate +# and the hash algorithm. +# +# Signing the /dev/null with an appended signature. + +sign-file SHA256 grub.key certificate.der /dev/null /dev/empty.sig + +# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.sig` + +# Remove the empty files. + +rm -rf /dev/empty.sig + +# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature. + +grub-install --appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 \ +--appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + +# Signing a GRUB image with an appended signature. + +sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed + +# +# Signing a GRUB image with more than one signer key in the appended signature: +# The grub1.key and grub2.key are your private keys, certificate1.der and certificate2.der +# are your GRUB signing public keys. kernel.der and Kernel2.der are your kernel signing public key. + +# Generate a raw signature for /dev/null signing using OpenSSL. + +openssl cms -sign -binary -nocerts -in /dev/null -signer certificate1.pem -inkey grub1.key \ +-signer certificate2.pem -inkey grub2.key -out /dev/empty.p7s -outform DER -noattr -md sha256 + +# Signing the /dev/null with an appended signature. + +sign-file -s /dev/empty.p7s sha256 /dev/null /dev/null /dev/empty.signed + +# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.signed` + +# Remove the empty files. + +rm -rf /dev/empty.signed /dev/empty.p7s + +# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature. + +grub-install --appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 \ +--appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + +# Generate a raw signature for GRUB image signing using OpenSSL. + +openssl cms -sign -binary -nocerts -in core.elf.unsigned -signer certificate.pem \ +-inkey grub.key -signer certificate1.pem -inkey grub1.key -out core.p7s -outform DER -noattr -md sha256 + +# Signing a GRUB image with an appended signature. + +sign-file -s core.p7s sha256 /dev/null core.elf.unsigned core.elf.signed + +# Don't forget to install the signed image as required (e.g. on powerpc-ieee1275, to the PReP partition) + +@end group +@end example +As with UEFI secure boot, it is necessary to build-in the required modules, +or sign them separately. + @node Platform limitations @chapter Platform limitations
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 03/21] docs/grub: Document signing GRUB with an appended signature
Quoted 31 lines · Sudhakar Kuppusamy
Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- docs/grub.texi | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)docs/grub.texi +11
-0@@ -9379,6 +9379,92 @@ image works under UEFI secure boot and can maintain the secure-boot chain. It will also be necessary to enroll the public key used into a relevant firmware key database. +@section Signing GRUB with an appended signature +The @file{core.img} itself can be signed with a Linux kernel module-style +appended signature. +To support IEEE1275 platforms where the boot image is often loaded directly +from a disk partition rather than from a file system, the @file{core.img} +can specify the size and location of the appended signature with an ELF +note added by @command{grub-install}. +An image can be signed this way using the @command{sign-file} command from +the Linux kernel: +@example +@group
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9402
Quoted 10 lines
docs/grub.texi +10
-0+# Signing a GRUB image with only one signer key in the appended signature: +# The grub.key is your private key and certificate.der is your GRUB signing public key +# kernel.der is your kernel signing public key. +# +# Determine the size of the appended signature. It depends on the signing certificate +# and the hash algorithm. +# +# Signing the /dev/null with an appended signature. + +sign-file SHA256 grub.key certificate.der /dev/null /dev/empty.sig
Please do not pollute /dev directory with randomly generated files. This applies to all command examples below too...
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9408
Quoted 5 lines
docs/grub.texi +5
-0+# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.sig` + +# Remove the empty files.
s/empty files/empty file signature/
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9410
Quoted 1 line
docs/grub.texi +1
-0+rm -rf /dev/empty.sig
The rm without "-rf" should be enough here. Same comment for rm below...
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9417
Quoted 6 lines
docs/grub.texi +6
-0+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature. + +grub-install --appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 \ +--appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ...
I suggest to add at least two spaces before "--appended-signature-size" to make it more visible that it is line continuation.
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9431
Quoted 13 lines
docs/grub.texi +13
-0+# Signing a GRUB image with an appended signature. + +sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed + +# +# Signing a GRUB image with more than one signer key in the appended signature: +# The grub1.key and grub2.key are your private keys, certificate1.der and certificate2.der +# are your GRUB signing public keys. kernel.der and Kernel2.der are your kernel signing public key. + +# Generate a raw signature for /dev/null signing using OpenSSL. + +openssl cms -sign -binary -nocerts -in /dev/null -signer certificate1.pem -inkey grub1.key \ +-signer certificate2.pem -inkey grub2.key -out /dev/empty.p7s -outform DER -noattr -md sha256
Ditto.
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9443
Quoted 11 lines
docs/grub.texi +11
-0+# Signing the /dev/null with an appended signature. + +sign-file -s /dev/empty.p7s sha256 /dev/null /dev/null /dev/empty.signed + +# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.signed` + +# Remove the empty files. + +rm -rf /dev/empty.signed /dev/empty.p7s
s/empty files/empty files signatures/
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9417
Quoted 6 lines
docs/grub.texi +6
-0+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature. + +grub-install --appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 \ +--appended-signature-size $EMPTY_SIG_SIZE --modules="appendedsig ..." ...
Please add at least two spaces at the beginning here...
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9455
Quoted 4 lines
docs/grub.texi +4
-0+# Generate a raw signature for GRUB image signing using OpenSSL. + +openssl cms -sign -binary -nocerts -in core.elf.unsigned -signer certificate.pem \ +-inkey grub.key -signer certificate1.pem -inkey grub1.key -out core.p7s -outform DER -noattr -md sha256
Ditto.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 4/21 crypto: Move storage for grub_crypto_pk_* to crypto.c
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
The way gcry_rsa and friends (the asymmetric ciphers) are loaded for the pgp module is a bit quirky.
include/grub/crypto.h contains:
extern struct gcry_pk_spec *grub_crypto_pk_rsa;
commands/pgp.c contains the actual storage:
struct gcry_pk_spec *grub_crypto_pk_rsa;
And the module itself saves to the storage in pgp.c:
GRUB_MOD_INIT(gcry_rsa)
{
grub_crypto_pk_rsa = &_gcry_pubkey_spec_rsa;}
This is annoying: gcry_rsa now has a dependency on pgp!
We want to be able to bring in gcry_rsa without bringing in PGP, so move the storage to crypto.c.
Previously, gcry_rsa depended on pgp and mpi. Now it depends on crypto and mpi. As pgp depends on crypto, this doesn't add any new module dependencies using the PGP verfier.
[FWIW, the story is different for the symmetric ciphers. cryptodisk and friends (zfs encryption etc) use grub_crypto_lookup_cipher_by_name() to get a cipher handle. That depends on grub_ciphers being populated by people calling grub_cipher_register. import_gcry.py ensures that the symmetric ciphers call it.]
--- grub-core/commands/pgp.c | 4 ---- grub-core/lib/crypto.c | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/commands/pgp.c +0 -4
@@ -136,10 +136,6 @@ struct signature_v4_headergrub_uint16_t hashed_sub; }GRUB_PACKED ; -struct gcry_pk_spec *grub_crypto_pk_dsa; -struct gcry_pk_spec *grub_crypto_pk_ecdsa; -struct gcry_pk_spec *grub_crypto_pk_rsa; -struct {const char *name;
grub-core/lib/crypto.c +4 -0
@@ -170,6 +170,10 @@ grub_md_unregister (gcry_md_spec_t *cipher) } } +struct gcry_pk_spec *grub_crypto_pk_dsa; +struct gcry_pk_spec *grub_crypto_pk_ecdsa; +struct gcry_pk_spec *grub_crypto_pk_rsa; +void grub_crypto_hash (const gcry_md_spec_t *hash,void *out,const void *in,grub_size_t inlen)
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 5/21 pgp: Rename OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY
In reply to Sudhakar KuppusamyPrior to the addition of the X.509 public key support for appended signature, current PGP signature relied on the GPG public key. Changing the enum name from "OBJ_TYPE_PUBKEY" to "OBJ_TYPE_GPG_PUBKEY" to differentiate between x509 certificate based appended signature and GPG certificate based PGP signature.
--- grub-core/commands/pgp.c | 2 +- include/grub/kernel.h | 2 +- util/grub-mkimage.c | 2 +- util/mkimage.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
grub-core/commands/pgp.c +1 -1
@@ -920,7 +920,7 @@ GRUB_MOD_INIT(pgp)grub_memset (&pseudo_file,0 ,sizeof (pseudo_file));/* Not an ELF module, skip. */ -if (header->type !=OBJ_TYPE_PUBKEY ) +if (header->type !=OBJ_TYPE_GPG_PUBKEY )continue ; pseudo_file.fs = &pseudo_fs;
include/grub/kernel.h +1 -1
@@ -28,7 +28,7 @@ enumOBJ_TYPE_MEMDISK ,OBJ_TYPE_CONFIG ,OBJ_TYPE_PREFIX , -OBJ_TYPE_PUBKEY , +OBJ_TYPE_GPG_PUBKEY ,OBJ_TYPE_DTB ,OBJ_TYPE_DISABLE_SHIM_LOCK ,OBJ_TYPE_DISABLE_CLI
util/grub-mkimage.c +1 -1
@@ -75,7 +75,7 @@ static struct argp_option options[] = {/* TRANSLATORS: "embed" is a verb (command description). "*/ {"config", 'c', N_("FILE"), 0, N_("embed FILE as an early config"), 0},/* TRANSLATORS: "embed" is a verb (command description). "*/ - {"pubkey", 'k', N_("FILE"), 0, N_("embed FILE as public key for signature checking"), 0}, + {"pubkey", 'k', N_("FILE"), 0, N_("embed FILE as public key for PGP signature checking"), 0},/* TRANSLATORS: NOTE is a name of segment. */ {"note", 'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0}, {"output", 'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
util/mkimage.c +1 -1
@@ -1056,7 +1056,7 @@ grub_install_generate_image (const char *dir, const char *prefix, curs =grub_util_get_image_size (pubkey_paths[i]); header = (struct grub_module_header *) (kernel_img + offset); - header->type =grub_host_to_target32 (OBJ_TYPE_PUBKEY ); + header->type =grub_host_to_target32 (OBJ_TYPE_GPG_PUBKEY ); header->size =grub_host_to_target32 (curs +sizeof (*header)); offset +=sizeof (*header);
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 6/21 grub-install: Support embedding x509 certificates
In reply to Sudhakar KuppusamyTo support verification of appended signatures, we need a way to embed the necessary public keys. Existing appended signature schemes in the Linux kernel use X.509 certificates, so allow certificates to be embedded in the GRUB core image in the same way as PGP keys.
--- include/grub/kernel.h | 1 + include/grub/util/install.h | 3 +++ util/grub-install-common.c | 19 +++++++++++++++++- util/grub-mkimage.c | 12 +++++++++++- util/mkimage.c | 39 ++++++++++++++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 3 deletions(-)
include/grub/kernel.h +1 -0
@@ -29,6 +29,7 @@ enumOBJ_TYPE_CONFIG ,OBJ_TYPE_PREFIX ,OBJ_TYPE_GPG_PUBKEY , +OBJ_TYPE_X509_PUBKEY ,OBJ_TYPE_DTB ,OBJ_TYPE_DISABLE_SHIM_LOCK ,OBJ_TYPE_DISABLE_CLI
include/grub/util/install.h +3 -0
@@ -69,6 +69,8 @@ N_("disable shim_lock verifier"), 0 }, \ {"disable-cli" ,GRUB_INSTALL_OPTIONS_DISABLE_CLI ,0 ,0 , \ N_("disabled command line interface access"), 0 }, \ + { "x509key", 'x', N_("FILE"), 0, \ + N_("embed FILE as an x509 certificate for appended signature checking"), 0}, \ { "appended-signature-size", GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE, \ "SIZE", 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 1}, \ { "verbose", 'v', 0, 0, \
@@ -196,6 +198,7 @@ grub_install_generate_image (const char *dir, const char *prefix, const char *outname, char *mods[], char *memdisk_path, char **pubkey_paths, size_t npubkeys, + char **x509key_paths, size_t nx509keys, char *config_path, const struct grub_install_image_target_desc *image_target, int note, size_t appsig_size,
util/grub-install-common.c +18 -1
@@ -463,6 +463,8 @@ handle_install_list (struct install_list *il, const char *val,static char **pubkeys;static size_t npubkeys; +static char **x509keys; +static size_t nx509keys;static char *sbat;static int disable_shim_lock;static grub_compression_t compression;
@@ -511,6 +513,10 @@ grub_install_parse (int key, char *arg)case GRUB_INSTALL_OPTIONS_DISABLE_CLI : disable_cli =1 ;return 1 ; +case 'x' : + x509keys =xrealloc (x509keys,sizeof (x509keys[0 ]) * (nx509keys +1 )); + x509keys[nx509keys++] =xstrdup (arg); +return 1 ;case GRUB_INSTALL_OPTIONS_VERBOSITY : verbosity++;
@@ -649,6 +655,9 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,for (pk = pubkeys; pk < pubkeys + npubkeys; pk++) slen +=sizeof (" --pubkey ''" ) +grub_strlen (*pk); +for (pk = x509keys; pk < x509keys + nx509keys; pk++) + slen +=sizeof (" --x509key ''" ) +grub_strlen (*pk); +for (md = modules.entries; *md; md++) slen +=sizeof (" ''" ) +grub_strlen (*md);
@@ -689,6 +698,14 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix, *p++ ='\'' ; } +for (pk = x509keys; pk < x509keys + nx509keys; pk++) + { + p =grub_stpcpy (p,"--x509key '" ); + p =grub_stpcpy (p, *pk); + *p++ ='\'' ; + *p++ =' ' ; + } +for (md = modules.entries; *md; md++) { *p++ =' ' ;
@@ -715,7 +732,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,grub_install_generate_image (dir, prefix, fp, outname, modules.entries, memdisk_path, - pubkeys, npubkeys, config_path, tgt, + pubkeys, npubkeys, x509keys, nx509keys, config_path, tgt, note, appsig_size, compression, dtb, sbat, disable_shim_lock, disable_cli);while (dc--)
util/grub-mkimage.c +11 -1
@@ -76,6 +76,7 @@ static struct argp_option options[] = { {"config", 'c', N_("FILE"), 0, N_("embed FILE as an early config"), 0},/* TRANSLATORS: "embed" is a verb (command description). "*/ {"pubkey", 'k', N_("FILE"), 0, N_("embed FILE as public key for PGP signature checking"), 0}, + {"x509key", 'x', N_("FILE"), 0, N_("embed FILE as an x509 certificate for appended signature checking"), 0},/* TRANSLATORS: NOTE is a name of segment. */ {"note", 'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0}, {"output", 'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
@@ -125,6 +126,8 @@ struct argumentschar *dtb;char **pubkeys;size_t npubkeys; +char **x509keys; +size_t nx509keys;char *font;char *config;char *sbat;
@@ -216,6 +219,12 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->pubkeys[arguments->npubkeys++] =xstrdup (arg);break ; +case 'x' : + arguments->x509keys =xrealloc (arguments->x509keys, +sizeof (arguments->x509keys[0 ]) * (arguments->nx509keys +1 )); + arguments->x509keys[arguments->nx509keys++] =xstrdup (arg); +break ; +case 'c' :if (arguments->config)free (arguments->config);
@@ -346,7 +355,8 @@ main (int argc, char *argv[]) grub_install_generate_image (arguments.dir, arguments.prefix, fp, arguments.output, arguments.modules, arguments.memdisk, arguments.pubkeys, - arguments.npubkeys, arguments.config, + arguments.npubkeys, arguments.x509keys, + arguments.nx509keys, arguments.config, arguments.image_target, arguments.note, arguments.appsig_size, arguments.comp, arguments.dtb,
util/mkimage.c +38 -1
@@ -883,7 +883,7 @@ void grub_install_generate_image (const char *dir, const char *prefix, FILE *out, const char *outname, char *mods[], char *memdisk_path, char **pubkey_paths, - size_t npubkeys, char *config_path, + size_t npubkeys, char **x509key_paths, size_t nx509keys, char *config_path, const struct grub_install_image_target_desc *image_target, int note, size_t appsig_size, grub_compression_t comp, const char *dtb_path, const char *sbat_path, int disable_shim_lock,
@@ -929,6 +929,24 @@ grub_install_generate_image (const char *dir, const char *prefix, } } +if (nx509keys !=0 && image_target->id !=IMAGE_PPC ) +grub_util_error (_ ("x509 public key can be support only to appended signature" +" with powerpc-ieee1275 images" )); + + { +size_t i; + +for (i =0 ; i < nx509keys; i++) + { +size_t curs; + + curs =ALIGN_ADDR (grub_util_get_image_size (x509key_paths[i])); +grub_util_info ("the size of x509 public key %u is 0x%" GRUB_HOST_PRIxLONG_LONG, + (unsigned ) i, (unsigned long long ) curs); + total_module_size += curs +sizeof (struct grub_module_header ); + } + } +if (memdisk_path) { memdisk_size =ALIGN_UP (grub_util_get_image_size (memdisk_path),512 );
@@ -1065,6 +1083,25 @@ grub_install_generate_image (const char *dir, const char *prefix, } } + { +size_t i; + +for (i =0 ; i < nx509keys; i++) + { +size_t curs; +struct grub_module_header *header; + + curs =grub_util_get_image_size (x509key_paths[i]); + header = (struct grub_module_header *) (kernel_img + offset); + header->type =grub_host_to_target32 (OBJ_TYPE_X509_PUBKEY ); + header->size =grub_host_to_target32 (curs +sizeof (*header)); + + offset +=sizeof (*header); +grub_util_load_image (x509key_paths[i], kernel_img + offset); + offset +=ALIGN_ADDR (curs); + } + } +if (memdisk_path) {struct grub_module_header *header;
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 7/21 appended signatures: Import GNUTLS's ASN.1 description files
In reply to Sudhakar KuppusamyIn order to parse PKCS#7 messages and X.509 certificates with libtasn1, we need some information about how they are encoded.
We get these from GNUTLS, which has the benefit that they support the features we need and are well tested.
The GNUTLS files are from:
- https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn
- https://github.com/gnutls/gnutls/blob/master/lib/pkix.asn
The GNUTLS license is LGPLv2.1+, which is GPLv3 compatible, allowing us to import it without issue.
--- .../commands/appendedsig/gnutls_asn1_tab.c | 148 ++++++ .../commands/appendedsig/pkix_asn1_tab.c | 485 ++++++++++++++++++ 2 files changed, 633 insertions(+) create mode 100644 grub-core/commands/appendedsig/gnutls_asn1_tab.c create mode 100644 grub-core/commands/appendedsig/pkix_asn1_tab.c
grub-core/commands/appendedsig/gnutls_asn1_tab.c new file +148 -0
@@ -0,0 +1,148 @@ +#include <grub/mm.h> +#include <libtasn1.h> + +/* +* Imported from gnutls.asn. +* https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn +*/ +const asn1_static_node grub_gnutls_asn1_tab[] = { + {"GNUTLS" ,536872976 ,NULL }, + {NULL ,1073741836 ,NULL }, + {"RSAPublicKey" ,1610612741 ,NULL }, + {"modulus" ,1073741827 ,NULL }, + {"publicExponent" ,3 ,NULL }, + {"RSAPrivateKey" ,1610612741 ,NULL }, + {"version" ,1073741827 ,NULL }, + {"modulus" ,1073741827 ,NULL }, + {"publicExponent" ,1073741827 ,NULL }, + {"privateExponent" ,1073741827 ,NULL }, + {"prime1" ,1073741827 ,NULL }, + {"prime2" ,1073741827 ,NULL }, + {"exponent1" ,1073741827 ,NULL }, + {"exponent2" ,1073741827 ,NULL }, + {"coefficient" ,1073741827 ,NULL }, + {"otherPrimeInfos" ,16386 ,"OtherPrimeInfos" }, + {"ProvableSeed" ,1610612741 ,NULL }, + {"algorithm" ,1073741836 ,NULL }, + {"seed" ,7 ,NULL }, + {"OtherPrimeInfos" ,1612709899 ,NULL }, + {"MAX" ,1074266122 ,"1" }, + {NULL ,2 ,"OtherPrimeInfo" }, + {"OtherPrimeInfo" ,1610612741 ,NULL }, + {"prime" ,1073741827 ,NULL }, + {"exponent" ,1073741827 ,NULL }, + {"coefficient" ,3 ,NULL }, + {"AlgorithmIdentifier" ,1610612741 ,NULL }, + {"algorithm" ,1073741836 ,NULL }, + {"parameters" ,541081613 ,NULL }, + {"algorithm" ,1 ,NULL }, + {"DigestInfo" ,1610612741 ,NULL }, + {"digestAlgorithm" ,1073741826 ,"DigestAlgorithmIdentifier" }, + {"digest" ,7 ,NULL }, + {"DigestAlgorithmIdentifier" ,1073741826 ,"AlgorithmIdentifier" }, + {"DSAPublicKey" ,1073741827 ,NULL }, + {"DSAParameters" ,1610612741 ,NULL }, + {"p" ,1073741827 ,NULL }, + {"q" ,1073741827 ,NULL }, + {"g" ,3 ,NULL }, + {"DSASignatureValue" ,1610612741 ,NULL }, + {"r" ,1073741827 ,NULL }, + {"s" ,3 ,NULL }, + {"DSAPrivateKey" ,1610612741 ,NULL }, + {"version" ,1073741827 ,NULL }, + {"p" ,1073741827 ,NULL }, + {"q" ,1073741827 ,NULL }, + {"g" ,1073741827 ,NULL }, + {"Y" ,1073741827 ,NULL }, + {"priv" ,3 ,NULL }, + {"DHParameter" ,1610612741 ,NULL }, + {"prime" ,1073741827 ,NULL }, + {"base" ,1073741827 ,NULL }, + {"privateValueLength" ,16387 ,NULL }, + {"pkcs-11-ec-Parameters" ,1610612754 ,NULL }, + {"oId" ,1073741836 ,NULL }, + {"curveName" ,31 ,NULL }, + {"ECParameters" ,1610612754 ,NULL }, + {"namedCurve" ,12 ,NULL }, + {"ECPrivateKey" ,1610612741 ,NULL }, + {"Version" ,1073741827 ,NULL }, + {"privateKey" ,1073741831 ,NULL }, + {"parameters" ,1610637314 ,"ECParameters" }, + {NULL ,2056 ,"0" }, + {"publicKey" ,536895494 ,NULL }, + {NULL ,2056 ,"1" }, + {"PrincipalName" ,1610612741 ,NULL }, + {"name-type" ,1610620931 ,NULL }, + {NULL ,2056 ,"0" }, + {"name-string" ,536879115 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,27 ,NULL }, + {"KRB5PrincipalName" ,1610612741 ,NULL }, + {"realm" ,1610620955 ,NULL }, + {NULL ,2056 ,"0" }, + {"principalName" ,536879106 ,"PrincipalName" }, + {NULL ,2056 ,"1" }, + {"RSAPSSParameters" ,1610612741 ,NULL }, + {"hashAlgorithm" ,1610637314 ,"AlgorithmIdentifier" }, + {NULL ,2056 ,"0" }, + {"maskGenAlgorithm" ,1610637314 ,"AlgorithmIdentifier" }, + {NULL ,2056 ,"1" }, + {"saltLength" ,1610653699 ,NULL }, + {NULL ,1073741833 ,"20" }, + {NULL ,2056 ,"2" }, + {"trailerField" ,536911875 ,NULL }, + {NULL ,1073741833 ,"1" }, + {NULL ,2056 ,"3" }, + {"RSAOAEPParameters" ,1610612741 ,NULL }, + {"hashAlgorithm" ,1610637314 ,"AlgorithmIdentifier" }, + {NULL ,2056 ,"0" }, + {"maskGenAlgorithm" ,1610637314 ,"AlgorithmIdentifier" }, + {NULL ,2056 ,"1" }, + {"pSourceFunc" ,536895490 ,"AlgorithmIdentifier" }, + {NULL ,2056 ,"2" }, + {"GOSTParameters" ,1610612741 ,NULL }, + {"publicKeyParamSet" ,1073741836 ,NULL }, + {"digestParamSet" ,16396 ,NULL }, + {"GOSTParametersOld" ,1610612741 ,NULL }, + {"publicKeyParamSet" ,1073741836 ,NULL }, + {"digestParamSet" ,1073741836 ,NULL }, + {"encryptionParamSet" ,16396 ,NULL }, + {"GOSTPrivateKey" ,1073741831 ,NULL }, + {"GOSTPrivateKeyOld" ,1073741827 ,NULL }, + {"IssuerSignTool" ,1610612741 ,NULL }, + {"signTool" ,1073741858 ,NULL }, + {"cATool" ,1073741858 ,NULL }, + {"signToolCert" ,1073741858 ,NULL }, + {"cAToolCert" ,34 ,NULL }, + {"Gost28147-89-EncryptedKey" ,1610612741 ,NULL }, + {"encryptedKey" ,1073741831 ,NULL }, + {"maskKey" ,1610637319 ,NULL }, + {NULL ,4104 ,"0" }, + {"macKey" ,7 ,NULL }, + {"SubjectPublicKeyInfo" ,1610612741 ,NULL }, + {"algorithm" ,1073741826 ,"AlgorithmIdentifier" }, + {"subjectPublicKey" ,6 ,NULL }, + {"GostR3410-TransportParameters" ,1610612741 ,NULL }, + {"encryptionParamSet" ,1073741836 ,NULL }, + {"ephemeralPublicKey" ,1610637314 ,"SubjectPublicKeyInfo" }, + {NULL ,4104 ,"0" }, + {"ukm" ,7 ,NULL }, + {"GostR3410-KeyTransport" ,1610612741 ,NULL }, + {"sessionEncryptedKey" ,1073741826 ,"Gost28147-89-EncryptedKey" }, + {"transportParameters" ,536895490 ,"GostR3410-TransportParameters" }, + {NULL ,4104 ,"0" }, + {"TPMKey" ,1610612741 ,NULL }, + {"type" ,1073741836 ,NULL }, + {"emptyAuth" ,1610637316 ,NULL }, + {NULL ,2056 ,"0" }, + {"parent" ,1073741827 ,NULL }, + {"pubkey" ,1073741831 ,NULL }, + {"privkey" ,7 ,NULL }, + {"MLDSAPrivateKey" ,536870917 ,NULL }, + {"version" ,1073741827 ,NULL }, + {"privateKeyAlgorithm" ,1073741826 ,"AlgorithmIdentifier" }, + {"privateKey" ,1073741831 ,NULL }, + {"publicKey" ,536895495 ,NULL }, + {NULL ,2056 ,"1" }, + {NULL ,0 ,NULL } +};
grub-core/commands/appendedsig/pkix_asn1_tab.c new file +485 -0
485 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 8/21 appended signatures: Parse ASN1 node
In reply to Sudhakar KuppusamyThis code allows us to parse ASN1 node and allocating memory to store it. It will work for anything where the size libtasn1 returns is right:
- Integers
- Octet strings
- DER encoding of other structures
It will _not_ work for things where libtasn1 size requires adjustment:
- Strings that require an extra NULL byte at the end
- Bit strings because libtasn1 returns the length in bits, not bytes.
If the function returns a non-NULL value, the caller must free it.
--- grub-core/commands/appendedsig/appendedsig.h | 42 +++++++++ grub-core/commands/appendedsig/asn1util.c | 96 ++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.h create mode 100644 grub-core/commands/appendedsig/asn1util.c
grub-core/commands/appendedsig/appendedsig.h new file +42 -0
@@ -0,0 +1,42 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020, 2022 Free Software Foundation, Inc. +* Copyright (C) 2020, 2022, 2025 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <libtasn1.h> + +extern asn1_node grub_gnutls_gnutls_asn; +extern asn1_node grub_gnutls_pkix_asn; + +/* Do libtasn1 init. */ +extern int +asn1_init (void ); + +/* +* Read a value from an ASN1 node, allocating memory to store it. +* It will work for anything where the size libtasn1 returns is right: +* - Integers +* - Octet strings +* - DER encoding of other structures +* It will _not_ work for things where libtasn1 size requires adjustment: +* - Strings that require an extra null byte at the end +* - Bit strings because libtasn1 returns the length in bits, not bytes. +* +* If the function returns a non-NULL value, the caller must free it. +*/ +extern void * +grub_asn1_allocate_and_read (asn1_node node,const char *name,const char *friendly_name,int *content_size);
grub-core/commands/appendedsig/asn1util.c new file +96 -0
96 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 9/21 appended signatures: Parse PKCS#7 signedData
In reply to Sudhakar KuppusamyThis code allows us to parse:
- PKCS#7 signedData messages. Only a single signerInfo is supported, which is all that the Linux sign-file utility supports creating out-of-the-box. Only RSA, SHA-256 and SHA-512 are supported. Any certificate embedded in the PKCS#7 message will be ignored.
--- grub-core/commands/appendedsig/appendedsig.h | 36 ++ grub-core/commands/appendedsig/pkcs7.c | 454 +++++++++++++++++++ 2 files changed, 490 insertions(+) create mode 100644 grub-core/commands/appendedsig/pkcs7.c
grub-core/commands/appendedsig/appendedsig.h +36 -0
@@ -17,11 +17,47 @@* along with GRUB. If not, see <http://www.gnu.org/licenses/>. */ +#include <grub/crypto.h> #include <libtasn1.h> extern asn1_node grub_gnutls_gnutls_asn;extern asn1_node grub_gnutls_pkix_asn; +#define MAX_OID_LEN 32 + +/* A PKCS#7 signedData signerInfo. */ +struct pkcs7_signerInfo +{ +const gcry_md_spec_t *hash; +gcry_mpi_t sig_mpi; +}; + +/* +* A PKCS#7 signedData message. +* We make no attempt to match intelligently, so we don't save any info about +* the signer. +*/ +struct pkcs7_signedData +{ +int signerInfo_count; +struct pkcs7_signerInfo *signerInfos; +}; + +/* +* Parse a PKCS#7 message, which must be a signedData message. +* The message must be in 'sigbuf' and of size 'data_size'. The result is +* placed in 'msg', which must already be allocated. +*/ +extern grub_err_t +parse_pkcs7_signedData (const void *sigbuf,grub_size_t data_size,struct pkcs7_signedData *msg); + +/* +* Release all the storage associated with the PKCS#7 message. +* If the caller dynamically allocated the message, it must free it. +*/ +extern void +pkcs7_signedData_release (struct pkcs7_signedData *msg); +/* Do libtasn1 init. */ extern int asn1_init (void );
grub-core/commands/appendedsig/pkcs7.c new file +454 -0
454 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 10/21 appended signatures: Parse X.509 certificates
In reply to Sudhakar KuppusamyThis code allows us to parse:
- X.509 certificates: at least enough to verify the signatures on the PKCS#7 messages. We expect that the certificates embedded in GRUB will be leaf certificates, not CA certificates. The parser enforces this.
- X.509 certificates support the Extended Key Usage extension and handle it by verifying that the certificate has a Code Signing usage.
--- grub-core/commands/appendedsig/appendedsig.h | 30 + grub-core/commands/appendedsig/x509.c | 953 +++++++++++++++++++ 2 files changed, 983 insertions(+) create mode 100644 grub-core/commands/appendedsig/x509.c
grub-core/commands/appendedsig/appendedsig.h +30 -0
@@ -25,6 +25,21 @@ extern asn1_node grub_gnutls_pkix_asn;#define MAX_OID_LEN 32 +/* +* One or more x509 certificates. +* We do limited parsing: extracting only the serial, CN and RSA public key. +*/ +struct x509_certificate +{ +struct x509_certificate *next; +grub_uint8_t *serial; +grub_size_t serial_len; +char *subject; +grub_size_t subject_len; +/* We only support RSA public keys. This encodes [modulus, publicExponent]. */ +gcry_mpi_t mpis[2 ]; +}; +/* A PKCS#7 signedData signerInfo. */ struct pkcs7_signerInfo {
@@ -43,6 +58,21 @@ struct pkcs7_signedDatastruct pkcs7_signerInfo *signerInfos; }; +/* +* Import a DER-encoded certificate at 'data', of size 'size'. +* Place the results into 'results', which must be already allocated. +*/ +extern grub_err_t +parse_x509_certificate (const void *data,grub_size_t size,struct x509_certificate *results); + +/* +* Release all the storage associated with the x509 certificate. +* If the caller dynamically allocated the certificate, it must free it. +* The caller is also responsible for maintenance of the linked list. +*/ +extern void +certificate_release (struct x509_certificate *cert); +/* * Parse a PKCS#7 message, which must be a signedData message. * The message must be in 'sigbuf' and of size 'data_size'. The result is
grub-core/commands/appendedsig/x509.c new file +953 -0
953 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 10/21] appended signatures: Parse X.509 certificates
Quoted 16 lines · Sudhakar Kuppusamy
This code allows us to parse:
- X.509 certificates: at least enough to verify the signatures on the PKCS#7 messages. We expect that the certificates embedded in GRUB will be leaf certificates, not CA certificates. The parser enforces this.
- X.509 certificates support the Extended Key Usage extension and handle it by verifying that the certificate has a Code Signing usage.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>Reviewed-by Daniel Kiper <[email protected]>
[...]
Quoted 30 lines
grub-core/commands/appendedsig/x509.c new file +30
-0+static grub_err_t +grub_x509_read_subject_public_key (asn1_node asn,struct x509_certificate *results) +{ +int result; +grub_err_t err; +const char *algo_name ="tbsCertificate.subjectPublicKeyInfo.algorithm.algorithm" ; +const char *params_name ="tbsCertificate.subjectPublicKeyInfo.algorithm.parameters" ; +const char *pk_name ="tbsCertificate.subjectPublicKeyInfo.subjectPublicKey" ; +char algo_oid[MAX_OID_LEN ]; +int algo_size =sizeof (algo_oid); +char params_value[2 ]; +int params_size =sizeof (params_value); +grub_uint8_t *key_data =NULL ; +int key_size =0 ; +unsigned int key_type; + +/* Algorithm: see notes for rsaEncryption_oid. */ + result =asn1_read_value (asn, algo_name, algo_oid, &algo_size); +if (result !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"error reading x509 public key algorithm: %s" , +asn1_strerror (result)); + +if (grub_strncmp (algo_oid, rsaEncryption_oid,sizeof (rsaEncryption_oid)) !=0 ) +return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET , +"unsupported x509 public key algorithm: %s" , algo_oid); + +/* +* RFC 3279 2.3.1 +* The rsaEncryption OID is intended to be used in the algorithm field +* of a value of type AlgorithmIdentifier. The parameters field MUST
grub-core/commands/appendedsig/x509.c line 188
Quoted 10 lines
grub-core/commands/appendedsig/x509.c new file +10
-0+* have ASN.1 type NULL for this algorithm identifier. +*/ + result =asn1_read_value (asn, params_name, params_value, ¶ms_size); +if (result !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"error reading x509 public key parameters: %s" , +asn1_strerror (result)); + +if (params_value[0 ] !=ASN1_TAG_NULL ) +return grub_error (GRUB_ERR_BAD_FILE_TYPE , +"invalid x509 public key parameters: expected NULL." );
Error messages does not end with full stop. I saw it in a few places here. Please fix it in all patches if needed. If you do that you can retain my RB...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 11/21 appended signatures: Support verifying appended signatures
In reply to Sudhakar KuppusamyBuilding on the parsers and the ability to embed x509 certificates, as well as the existing gcrypt functionality, add a module for verifying appended signatures.
This includes a verifier that requires that Linux kernels and grub modules have appended signatures, and commands to manage the list of trusted certificates for verification.
Verification must be enabled by setting check_appended_signatures. If GRUB is locked down when the module is loaded, verification will be enabled and locked automatically.
As with the PGP verifier, it is not a complete secure-boot solution:
other mechanisms, such as a password or lockdown, must be used to ensure that a user cannot drop to the GRUB shell and disable verification.
--- grub-core/Makefile.core.def | 14 + grub-core/commands/appendedsig/appendedsig.c | 608 +++++++++++++++++++ include/grub/file.h | 2 + 3 files changed, 624 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.c
grub-core/Makefile.core.def +14 -0
@@ -979,6 +979,20 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = appendedsig; + common = commands/appendedsig/appendedsig.c; + common = commands/appendedsig/x509.c; + common = commands/appendedsig/pkcs7.c; + common = commands/appendedsig/asn1util.c; + common = commands/appendedsig/gnutls_asn1_tab.c; + common = commands/appendedsig/pkix_asn1_tab.c; + enable = powerpc_ieee1275; + cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls'; + cppflags = '$(CPPFLAGS_GCRY) -I$(srcdir)/lib/libtasn1-grub'; + depends = crypto, gcry_rsa, gcry_sha256, gcry_sha512, mpi, asn1; +}; + module = { name = hdparm; common = commands/hdparm.c;
grub-core/commands/appendedsig/appendedsig.c new file +608 -0
608 lines not on this page · raw message
include/grub/file.h +2 -0
@@ -80,6 +80,8 @@ enum grub_file_typeGRUB_FILE_TYPE_PUBLIC_KEY ,/* File holding public key to add to trused keys. */ GRUB_FILE_TYPE_PUBLIC_KEY_TRUST , +/* File holding x509 certificiate to add to trusted keys. */ +GRUB_FILE_TYPE_CERTIFICATE_TRUST ,/* File of which we intend to print a blocklist to the user. */ GRUB_FILE_TYPE_PRINT_BLOCKLIST,/* File we intend to use for test loading or testing speed. */
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 11/21] appended signatures: Support verifying appended signatures
Quoted 5 lines · Sudhakar Kuppusamy
Building on the parsers and the ability to embed x509 certificates, as well as the existing gcrypt functionality, add a module for verifying appended signatures.
This includes a verifier that requires that Linux kernels and grub modules
s/grub/GRUB/ this is not fixed in many places in many patches. Please fix that everywhere...
Quoted 120 lines
have appended signatures, and commands to manage the list of trusted certificates for verification.
Verification must be enabled by setting
check_appended_signatures. If GRUB is locked down when the module is loaded, verification will be enabled and locked automatically.As with the PGP verifier, it is not a complete secure-boot solution:
other mechanisms, such as a password or lockdown, must be used to ensure that a user cannot drop to the GRUB shell and disable verification.Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/Makefile.core.def | 14 + grub-core/commands/appendedsig/appendedsig.c | 608 +++++++++++++++++++ include/grub/file.h | 2 + 3 files changed, 624 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.cgrub-core/Makefile.core.def +14
-0@@ -979,6 +979,20 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = appendedsig; + common = commands/appendedsig/appendedsig.c; + common = commands/appendedsig/x509.c; + common = commands/appendedsig/pkcs7.c; + common = commands/appendedsig/asn1util.c; + common = commands/appendedsig/gnutls_asn1_tab.c; + common = commands/appendedsig/pkix_asn1_tab.c; + enable = powerpc_ieee1275; + cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls'; + cppflags = '$(CPPFLAGS_GCRY) -I$(srcdir)/lib/libtasn1-grub'; + depends = crypto, gcry_rsa, gcry_sha256, gcry_sha512, mpi, asn1; +}; + module = { name = hdparm; common = commands/hdparm.c;grub-core/commands/appendedsig/appendedsig.c new file +67
-0@@ -0,0 +1,608 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020, 2021, 2022 Free Software Foundation, Inc. +* Copyright (C) 2020, 2021, 2022, 2025 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/types.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/err.h> +#include <grub/dl.h> +#include <grub/file.h> +#include <grub/command.h> +#include <grub/crypto.h> +#include <grub/i18n.h> +#include <grub/gcrypt/gcrypt.h> +#include <grub/kernel.h> +#include <grub/extcmd.h> +#include <grub/verify.h> +#include <libtasn1.h> +#include <grub/env.h> +#include <grub/lockdown.h> + +#include "appendedsig.h" + +GRUB_MOD_LICENSE ("GPLv3+" ); + +/* Public key type. */ +#define GRUB_PKEY_ID_PKCS7 2 + +/* Appended signature magic string. */ +static const char magic[] ="~Module signature appended~\n" ; + +/* +* This structure is extracted from scripts/sign-file.c in the linux kernel +* source. It was licensed as LGPLv2.1+, which is GPLv3+ compatible. +*/ +struct module_signature +{ +grub_uint8_t algo;/* Public-key crypto algorithm [0]. */ +grub_uint8_t hash;/* Digest algorithm [0]. */ +grub_uint8_t id_type;/* Key identifier type [GRUB_PKEY_ID_PKCS7]. */ +grub_uint8_t signer_len;/* Length of signer's name [0]. */ +grub_uint8_t key_id_len;/* Length of key identifier [0]. */ +grub_uint8_t __pad[3 ]; +grub_uint32_t sig_len;/* Length of signature data. */ +}GRUB_PACKED ; + +/* This represents an entire, parsed, appended signature. */ +struct grub_appended_signature +{ +grub_size_t signature_len;/* Length of PKCS#7 data + metadata + magic. */ +struct module_signature sig_metadata;/* Module signature metadata. */ +struct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data. */
grub-core/commands/appendedsig/appendedsig.c line 77
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+}; + +/* Trusted certificates for verifying appended signatures. */ +struct x509_certificate *db; + +static enum +{ + CHECK_SIGS_NO = 0, + CHECK_SIGS_ENFORCE = 1, + CHECK_SIGS_FORCED = 2
Is there any particular reason to have values assigned to constants? If not please drop all of them.
And I think difference between CHECK_SIGS_ENFORCE and CHECK_SIGS_FORCED should be explained. If they are really different...
grub-core/commands/appendedsig/appendedsig.c line 86
Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c new file +9
-0+} check_sigs =CHECK_SIGS_NO ; + +static const char * +grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)), +const char *val __attribute__ ((unused))) +{ +if (check_sigs ==CHECK_SIGS_FORCED ) +return "forced" ; +else if (check_sigs ==CHECK_SIGS_ENFORCE )
You can drop "else" here...
grub-core/commands/appendedsig/appendedsig.c line 89
Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3
-0+return "enforce" ; + +return "no" ;
I have an impression that these values are not fully documented. And they should be...
grub-core/commands/appendedsig/appendedsig.c line 102
Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c new file +13
-0+} + +static char * +grub_env_write_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val) +{ +char *ret; + +/* Do not allow the value to be changed if set to forced. */ +if (check_sigs ==CHECK_SIGS_FORCED ) + { + ret =grub_strdup ("forced" ); +if (ret ==NULL ) +grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not duplicate a string forced." );
s/."/"/ here and everywhere...
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+return ret; + } + +if ((*val =='2' ) || (*val =='f' )) + check_sigs =CHECK_SIGS_FORCED ; +else if ((*val =='1' ) || (*val =='e' )) + check_sigs =CHECK_SIGS_ENFORCE ; +else if ((*val =='0' ) || (*val =='n' )) + check_sigs =CHECK_SIGS_NO ; +
grub-core/commands/appendedsig/appendedsig.c line 123
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+ ret =grub_strdup (grub_env_read_sec (NULL ,NULL )); +if (ret ==NULL ) +grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not duplicate a string %s" , +grub_env_read_sec (NULL ,NULL )); + +return ret; +} + +static grub_err_t +file_read_all (grub_file_t file,grub_uint8_t **buf,grub_size_t *len)
s/file_read_all/file_read_whole/
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 12/21 appended signatures: Verification tests
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
These tests are run through all_functional_test and test a range of commands and behaviours.
--- grub-core/Makefile.core.def | 7 + grub-core/tests/appended_signature_test.c | 250 ++++++ grub-core/tests/appended_signatures.h | 975 ++++++++++++++++++++++ grub-core/tests/lib/functional_test.c | 5 + 4 files changed, 1237 insertions(+) create mode 100644 grub-core/tests/appended_signature_test.c create mode 100644 grub-core/tests/appended_signatures.h
grub-core/Makefile.core.def +7 -0
@@ -2210,6 +2210,13 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = appended_signature_test; + common = tests/appended_signature_test.c; + common = tests/appended_signatures.h; + enable = powerpc_ieee1275; +}; + module = { name = signature_test; common = tests/signature_test.c;
grub-core/tests/appended_signature_test.c new file +250 -0
250 lines not on this page · raw message
grub-core/tests/appended_signatures.h new file +975 -0
975 lines not on this page · raw message
grub-core/tests/lib/functional_test.c +5 -0
@@ -73,6 +73,11 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),grub_dl_load ("xnu_uuid_test" );grub_dl_load ("pbkdf2_test" );grub_dl_load ("signature_test" ); + +#if defined(__powerpc__) +grub_dl_load ("appended_signature_test" ); +#endif +grub_dl_load ("sleep_test" );grub_dl_load ("bswap_test" );grub_dl_load ("ctz_test" );
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 13/21 appended signatures: Documentation
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
--- docs/grub.texi | 189 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 168 insertions(+), 21 deletions(-)
docs/grub.texi +168 -21
@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB. @menu * biosnum:: +* check_appended_signatures:: * check_signatures:: * chosen:: * cmdpath::
@@ -3343,12 +3344,16 @@ this. For an alternative approach which also changes BIOS drive mappings for the chain-loaded system, @pxref{drivemap}. +@node check_appended_signatures +@subsection check_appended_signatures +This variable controls whether GRUB enforces appended signature validation on +loaded vmlinux file. @xref{Using appended signatures}. @node check_signatures @subsection check_signatures -This variable controls whether GRUB enforces digital signature -validation on loaded files. @xref{Using digital signatures}. +This variable controls whether GRUB enforces GPG-style digital signature +validation on loaded files. @xref{Using GPG-style digital signatures}. @node chosen @subsection chosen
@@ -6414,6 +6419,10 @@ you forget a command, you can run the command @command{help} @menu * [:: Check file types and compare values * acpi:: Load ACPI tables +* append_add_db_cert:: Add an X.509 certificate to the db list +* append_list_db:: List trusted certificates from the db list +* append_rm_dbx_cert:: Remove a certificate from the db list +* append_verify:: Verify appended digital signature using db list * authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminal
@@ -6535,6 +6544,68 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). unsigned code. @end deffn +@node append_add_db_cert +@subsection append_add_db_cert + +@deffn Command append_add_db_cert X509_certificate +Read a DER-formatted X.509 certificate from the file @var{X509_certificate} +and add it to GRUB's internal db list of trusted X.509 certificates. These +certificates are used to validate appended signatures when the environment +variable @code{check_appended_signatures} is set to @code{enforce}. + +Note that if @code{check_appended_signatures} is set to @code{enforce} +when @command{append_add_db_cert} is executed, then @var{X509_certificate} +must itself bear an appended signature. (It is not sufficient that +@var{X509_certificate} be signed by a trusted certificate according to the +X.509 rules: GRUB does not include support for validating signatures within X.509 +certificates themselves.) + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_db +@subsection append_list_db + +@deffn Command append_list_db +List all X.509 certificates trusted by GRUB for validating appended signatures. +The output is a numbered list of certificates, showing the certificate's serial +number and Common Name. + +The certificate number can be used as an argument to +@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_rm_dbx_cert +@subsection append_rm_dbx_cert + +@deffn Command append_rm_dbx_cert cert_number +Remove the X.509 certificate numbered @var{cert_number} from GRUB's keyring of +db for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{append_list_db} (@pxref{append_list_db}). + +These certificates are used to validate appended signatures when environment +variable @code{check_appended_signatures} is set to @code{enforce} +(@pxref{check_appended_signatures}), and by @command{append_verify} +(@pxref{append_verify}). See @xref{Using appended signatures} for more +information. +@end deffn + +@node append_verify +@subsection append_verify + +@deffn Command append_verify file +Verifies an appended signature on @var{file} against the trusted X.509 certificates +known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and +@pxref{append_rm_dbx_cert}). +Exit code @code{$?} is set to 0 if the signature validates +successfully. If validation fails, it is set to a non-zero value. + +See @xref{Using appended signatures}, for more information. +@end deffn @node authenticate @subsection authenticate
@@ -6854,7 +6925,7 @@ These keys are used to validate signatures when environment variable @code{check_signatures} is set to @code{enforce} (@pxref{check_signatures}), and by some invocations of @command{verify_detached} (@pxref{verify_detached}). @xref{Using -digital signatures}, for more information. +GPG-style digital signatures}, for more information. @end deffn @node drivemap
@@ -7250,7 +7321,6 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn - @node list_env @subsection list_env
@@ -7270,7 +7340,7 @@ The output is in GPG's v4 key fingerprint format (i.e., the output of @code{gpg --fingerprint}). The least significant four bytes (last eight hexadecimal digits) can be used as an argument to @command{distrust} (@pxref{distrust}). -@xref{Using digital signatures}, for more information about uses for +@xref{Using GPG-style digital signatures}, for more information about uses for these keys. @end deffn
@@ -7305,8 +7375,11 @@ When used with care, @option{--skip-sig} and the whitelist enable an administrator to configure a system to boot only signed configurations, but to allow the user to select from among multiple configurations, and to enable ``one-shot'' boot attempts and -``savedefault'' behavior. @xref{Using digital signatures}, for more +``savedefault'' behavior. @xref{Using GPG-style digital signatures}, for more information. +Extra care should be taken when combining this command with appended signatures +(@pxref{Using appended signatures}), as this file is not validated by an +appended signature and could set @code{check_appended_signatures=no}. @end deffn
@@ -7677,7 +7750,7 @@ read. It is possible to modify a digitally signed environment block file from within GRUB using this command, such that its signature will no longer be valid on subsequent boots. Care should be taken in such advanced configurations to avoid rendering the system -unbootable. @xref{Using digital signatures}, for more information. +unbootable. @xref{Using GPG-style digital signatures}, for more information. @end deffn
@@ -8167,11 +8240,10 @@ signatures when environment variable @code{check_signatures} is set to must itself be properly signed. The @option{--skip-sig} option can be used to disable signature-checking when reading @var{pubkey_file} itself. It is expected that @option{--skip-sig} is useful for testing -and manual booting. @xref{Using digital signatures}, for more +and manual booting. @xref{Using GPG-style digital signatures}, for more information. @end deffn - @node unset @subsection unset
@@ -8190,7 +8262,6 @@ only on PC BIOS platforms. @end deffn @end ignore - @node verify_detached @subsection verify_detached
@@ -8208,7 +8279,7 @@ tried. Exit code @code{$?} is set to 0 if the signature validates successfully. If validation fails, it is set to a non-zero value. -@xref{Using digital signatures}, for more information. +@xref{Using GPG-style digital signatures}, for more information. @end deffn @node videoinfo
@@ -8668,14 +8739,15 @@ environment variables and commands are listed in the same order. @chapter Security @menu -* Authentication and authorisation:: Users and access control -* Using digital signatures:: Booting digitally signed code -* UEFI secure boot and shim:: Booting digitally signed PE files -* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation -* Measured Boot:: Measuring boot components -* Lockdown:: Lockdown when booting on a secure setup -* TPM2 key protector:: Managing disk key with TPM2 key protector -* Signing GRUB itself:: Ensuring the integrity of the GRUB core image +* Authentication and authorisation:: Users and access control +* Using GPG-style digital signatures:: Booting digitally signed code +* Using appended signatures:: An alternative approach to booting digitally signed code +* UEFI secure boot and shim:: Booting digitally signed PE files +* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation +* Measured Boot:: Measuring boot components +* Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector +* Signing GRUB itself:: Ensuring the integrity of the GRUB core image @end menu @node Authentication and authorisation
@@ -8751,8 +8823,8 @@ generating configuration files with authentication. You can use adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2} commands. -@node Using digital signatures -@section Using digital signatures in GRUB +@node Using GPG-style digital signatures +@section Using GPG-style digital signatures in GRUB GRUB's @file{core.img} can optionally provide enforcement that all files subsequently read from disk are covered by a valid digital signature.
@@ -8835,6 +8907,81 @@ or BIOS) configuration to cause the machine to boot from a different (attacker-controlled) device. GRUB is at best only one link in a secure boot chain. +@node Using appended signatures +@section Using appended signatures in GRUB + +GRUB supports verifying Linux-style 'appended signatures' for secure boot. +Appended signatures are PKCS#7 messages containing a signature over the +contents of a file, plus some metadata, appended to the end of a file. A file +with an appended signature ends with the magic string: + +@example +~Module signature appended~\n +@end example + +where @code{\n} represents the line feed character, @code{0x0a}. + +To enable appended signature verification, load the appendedsig module and an +x509 certificate for verification. Building the appendedsig module into the +core grub image is recommended. + +Certificates can be managed at boot time using the @pxref{append_add_db_cert}, +@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. +Certificates can also be built in to the core image using the @code{--x509} +parameter to @command{grub-install} or @command{grub-mkimage}. +A file can be explicitly verified using the @pxref{append_verify} command. + +Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported, +and only RSA signatures are supported. + +A file can be signed with the @command{sign-file} utility supplied with the +Linux kernel source. For example, if you have @code{signing.key} as the private +key and @code{certificate.der} as the x509 certificate containing the public key: + +@example +sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed +@end example + +Enforcement of signature verification is controlled by the +@code{check_appended_signatures} variable. Verification will only take place +when files are loaded if the variable is set to @code{enforce}. If a +certificate is built into the grub core image with the @code{--x509} parameter, +the variable will be automatically set to @code{enforce} when the appendedsig +module is loaded. + +Unlike GPG-style signatures, not all files loaded by GRUB are required to be +signed. Once verification is turned on, the following file types must carry +appended signatures: + +@enumerate +@item Linux, Multiboot, BSD, XNU and Plan9 kernels +@item Grub modules, except those built in to the core image +@item Any new certificate files to be trusted +@end enumerate + +ACPI tables and Device Tree images will not be checked for appended signatures +but must be verified by another mechanism such as GPG-style signatures before +they will be loaded. + +No attempt is made to validate any other file type. In particular, +chain-loaded binaries are not verified - if your platform supports +chain-loading and this cannot be disabled, consider an alternative secure +boot mechanism. + +As with GPG-style appended signatures, signature checking does @strong{not} +stop an attacker with console access from dropping manually to the GRUB +console and executing: + +@example +set check_appended_signatures=no +@end example + +Refer to the section on password-protecting GRUB (@pxref{Authentication +and authorisation}) for more information on preventing this. + +Additionally, special care must be taken around the @command{loadenv} command, +which can be used to turn off @code{check_appended_signature}. + @node UEFI secure boot and shim @section UEFI secure boot and shim support
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 13/21] appended signatures: Documentation
Quoted 104 lines · Sudhakar Kuppusamy
From: Daniel Axtens <[email protected]>
This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- docs/grub.texi | 189 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 168 insertions(+), 21 deletions(-)docs/grub.texi +55
-2@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB. @menu * biosnum:: +* check_appended_signatures:: * check_signatures:: * chosen:: * cmdpath::@@ -3343,12 +3344,16 @@ this. For an alternative approach which also changes BIOS drive mappings for the chain-loaded system, @pxref{drivemap}. +@node check_appended_signatures +@subsection check_appended_signatures +This variable controls whether GRUB enforces appended signature validation on +loaded vmlinux file. @xref{Using appended signatures}. @node check_signatures @subsection check_signatures -This variable controls whether GRUB enforces digital signature -validation on loaded files. @xref{Using digital signatures}. +This variable controls whether GRUB enforces GPG-style digital signature +validation on loaded files. @xref{Using GPG-style digital signatures}. @node chosen @subsection chosen@@ -6414,6 +6419,10 @@ you forget a command, you can run the command @command{help} @menu * [:: Check file types and compare values * acpi:: Load ACPI tables +* append_add_db_cert:: Add an X.509 certificate to the db list +* append_list_db:: List trusted certificates from the db list +* append_rm_dbx_cert:: Remove a certificate from the db list +* append_verify:: Verify appended digital signature using db list * authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminal@@ -6535,6 +6544,68 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). unsigned code. @end deffn +@node append_add_db_cert +@subsection append_add_db_cert + +@deffn Command append_add_db_cert X509_certificate +Read a DER-formatted X.509 certificate from the file @var{X509_certificate} +and add it to GRUB's internal db list of trusted X.509 certificates. These +certificates are used to validate appended signatures when the environment +variable @code{check_appended_signatures} is set to @code{enforce}. + +Note that if @code{check_appended_signatures} is set to @code{enforce} +when @command{append_add_db_cert} is executed, then @var{X509_certificate} +must itself bear an appended signature. (It is not sufficient that +@var{X509_certificate} be signed by a trusted certificate according to the +X.509 rules: GRUB does not include support for validating signatures within X.509 +certificates themselves.) + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_db +@subsection append_list_db + +@deffn Command append_list_db +List all X.509 certificates trusted by GRUB for validating appended signatures. +The output is a numbered list of certificates, showing the certificate's serial +number and Common Name. + +The certificate number can be used as an argument to +@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_rm_dbx_cert +@subsection append_rm_dbx_cert + +@deffn Command append_rm_dbx_cert cert_number +Remove the X.509 certificate numbered @var{cert_number} from GRUB's keyring of +db for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{append_list_db} (@pxref{append_list_db}). + +These certificates are used to validate appended signatures when environment
docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6600
Quoted 10 lines
docs/grub.texi +10
-0+variable @code{check_appended_signatures} is set to @code{enforce} +(@pxref{check_appended_signatures}), and by @command{append_verify} +(@pxref{append_verify}). See @xref{Using appended signatures} for more +information. +@end deffn + +@node append_verify +@subsection append_verify + +@deffn Command append_verify file
I suggest to use "<>" to mark arguments which gets a value, e.g.: append_verify <file> Please fix this in whole documentation...
docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6601
Quoted 1 line
docs/grub.texi +1
-0+Verifies an appended signature on @var{file} against the trusted X.509 certificates
s/file/<file>
Quoted 50 lines
docs/grub.texi +13
-4+known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and +@pxref{append_rm_dbx_cert}). +Exit code @code{$?} is set to 0 if the signature validates +successfully. If validation fails, it is set to a non-zero value. + +See @xref{Using appended signatures}, for more information. +@end deffn @node authenticate @subsection authenticate@@ -6854,7 +6925,7 @@ These keys are used to validate signatures when environment variable @code{check_signatures} is set to @code{enforce} (@pxref{check_signatures}), and by some invocations of @command{verify_detached} (@pxref{verify_detached}). @xref{Using -digital signatures}, for more information. +GPG-style digital signatures}, for more information. @end deffn @node drivemap@@ -7250,7 +7321,6 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn - @node list_env @subsection list_env@@ -7270,7 +7340,7 @@ The output is in GPG's v4 key fingerprint format (i.e., the output of @code{gpg --fingerprint}). The least significant four bytes (last eight hexadecimal digits) can be used as an argument to @command{distrust} (@pxref{distrust}). -@xref{Using digital signatures}, for more information about uses for +@xref{Using GPG-style digital signatures}, for more information about uses for these keys. @end deffn@@ -7305,8 +7375,11 @@ When used with care, @option{--skip-sig} and the whitelist enable an administrator to configure a system to boot only signed configurations, but to allow the user to select from among multiple configurations, and to enable ``one-shot'' boot attempts and -``savedefault'' behavior. @xref{Using digital signatures}, for more +``savedefault'' behavior. @xref{Using GPG-style digital signatures}, for more information. +Extra care should be taken when combining this command with appended signatures +(@pxref{Using appended signatures}), as this file is not validated by an +appended signature and could set @code{check_appended_signatures=no}. @end deffn
docs/grub.texi read. It is possible to modify a digitally signed environment block · line 7753
Quoted 5 lines
docs/grub.texi +1
-1@@ -7677,7 +7750,7 @@ read. It is possible to modify a digitally signed environment block file from within GRUB using this command, such that its signature will no longer be valid on subsequent boots. Care should be taken in such advanced configurations to avoid rendering the system -unbootable. @xref{Using digital signatures}, for more information. +unbootable. @xref{Using GPG-style digital signatures}, for more information.
I suggest to move all GPG documentation changes to separate patch.
docs/grub.texi signatures when environment variable @code{check_signatures} is set to · line 8243
Quoted 9 lines
docs/grub.texi +1
-1@end deffn@@ -8167,11 +8240,10 @@ signatures when environment variable @code{check_signatures} is set to must itself be properly signed. The @option{--skip-sig} option can be used to disable signature-checking when reading @var{pubkey_file} itself. It is expected that @option{--skip-sig} is useful for testing -and manual booting. @xref{Using digital signatures}, for more +and manual booting. @xref{Using GPG-style digital signatures}, for more
Ditto and below...
docs/grub.texi signatures when environment variable @code{check_signatures} is set to · line 8174
Quoted 4 lines
docs/grub.texi +0
-1information. @end deffn -
This and similar cleanups can be a part of GPG change set but they have to be mentioned in the commit message.
docs/grub.texi only on PC BIOS platforms. · line 8193
Quoted 8 lines
docs/grub.texi +0
-1@node unset @subsection unset@@ -8190,7 +8262,6 @@ only on PC BIOS platforms. @end deffn @end ignore -
Ditto...
Quoted 58 lines
docs/grub.texi +19
-11@node verify_detached @subsection verify_detached@@ -8208,7 +8279,7 @@ tried. Exit code @code{$?} is set to 0 if the signature validates successfully. If validation fails, it is set to a non-zero value. -@xref{Using digital signatures}, for more information. +@xref{Using GPG-style digital signatures}, for more information. @end deffn @node videoinfo@@ -8668,14 +8739,15 @@ environment variables and commands are listed in the same order. @chapter Security @menu -* Authentication and authorisation:: Users and access control -* Using digital signatures:: Booting digitally signed code -* UEFI secure boot and shim:: Booting digitally signed PE files -* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation -* Measured Boot:: Measuring boot components -* Lockdown:: Lockdown when booting on a secure setup -* TPM2 key protector:: Managing disk key with TPM2 key protector -* Signing GRUB itself:: Ensuring the integrity of the GRUB core image +* Authentication and authorisation:: Users and access control +* Using GPG-style digital signatures:: Booting digitally signed code +* Using appended signatures:: An alternative approach to booting digitally signed code +* UEFI secure boot and shim:: Booting digitally signed PE files +* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation +* Measured Boot:: Measuring boot components +* Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector +* Signing GRUB itself:: Ensuring the integrity of the GRUB core image @end menu @node Authentication and authorisation@@ -8751,8 +8823,8 @@ generating configuration files with authentication. You can use adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2} commands. -@node Using digital signatures -@section Using digital signatures in GRUB +@node Using GPG-style digital signatures +@section Using GPG-style digital signatures in GRUB GRUB's @file{core.img} can optionally provide enforcement that all files subsequently read from disk are covered by a valid digital signature.@@ -8835,6 +8907,81 @@ or BIOS) configuration to cause the machine to boot from a different (attacker-controlled) device. GRUB is at best only one link in a secure boot chain. +@node Using appended signatures +@section Using appended signatures in GRUB + +GRUB supports verifying Linux-style 'appended signatures' for secure boot. +Appended signatures are PKCS#7 messages containing a signature over the +contents of a file, plus some metadata, appended to the end of a file. A file +with an appended signature ends with the magic string:
docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8926
Quoted 10 lines
docs/grub.texi +10
-0+ +@example +~Module signature appended~\n +@end example + +where @code{\n} represents the line feed character, @code{0x0a}. + +To enable appended signature verification, load the appendedsig module and an +x509 certificate for verification. Building the appendedsig module into the +core grub image is recommended.
s/grub/GRUB/...
Quoted 21 lines
docs/grub.texi +21
-0+Certificates can be managed at boot time using the @pxref{append_add_db_cert}, +@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. +Certificates can also be built in to the core image using the @code{--x509} +parameter to @command{grub-install} or @command{grub-mkimage}. +A file can be explicitly verified using the @pxref{append_verify} command. + +Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported, +and only RSA signatures are supported. + +A file can be signed with the @command{sign-file} utility supplied with the +Linux kernel source. For example, if you have @code{signing.key} as the private +key and @code{certificate.der} as the x509 certificate containing the public key: + +@example +sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed +@end example + +Enforcement of signature verification is controlled by the +@code{check_appended_signatures} variable. Verification will only take place +when files are loaded if the variable is set to @code{enforce}. If a +certificate is built into the grub core image with the @code{--x509} parameter,
docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8958
Quoted 10 lines
docs/grub.texi +10
-0+the variable will be automatically set to @code{enforce} when the appendedsig +module is loaded. + +Unlike GPG-style signatures, not all files loaded by GRUB are required to be +signed. Once verification is turned on, the following file types must carry +appended signatures: + +@enumerate +@item Linux, Multiboot, BSD, XNU and Plan9 kernels +@item Grub modules, except those built in to the core image
s/Grub/GRUB/...
Quoted 18 lines
docs/grub.texi +18
-0+@item Any new certificate files to be trusted +@end enumerate + +ACPI tables and Device Tree images will not be checked for appended signatures +but must be verified by another mechanism such as GPG-style signatures before +they will be loaded. + +No attempt is made to validate any other file type. In particular, +chain-loaded binaries are not verified - if your platform supports +chain-loading and this cannot be disabled, consider an alternative secure +boot mechanism. + +As with GPG-style appended signatures, signature checking does @strong{not} +stop an attacker with console access from dropping manually to the GRUB +console and executing: + +@example +set check_appended_signatures=no
docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8986
Quoted 10 lines
docs/grub.texi +8
-0+@end example + +Refer to the section on password-protecting GRUB (@pxref{Authentication +and authorisation}) for more information on preventing this. + +Additionally, special care must be taken around the @command{loadenv} command, +which can be used to turn off @code{check_appended_signature}. + @node UEFI secure boot and shim @section UEFI secure boot and shim support
May I ask you to put all documentation patches in one group at the end of the patch set? Same applies to tests patches. I think they should land before documentation patches...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 13/21] appended signatures: Documentation
Quoted 114 lines · Daniel Kiper
Quoted 109 lines · Sudhakar Kuppusamy
From: Daniel Axtens <[email protected]>
This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>---
docs/grub.texi | 189 +++++++++++++++++++++++++++++++++++++++++++------1 file changed, 168 insertions(+), 21 deletions(-)docs/grub.texi +0
-0@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB.@menu
* biosnum::
+*check_appended_signatures::
*check_signatures::
* chosen::
* cmdpath::docs/grub.texi +0
-0@@ -3343,12 +3344,16 @@ this.For an alternative approach which also changes BIOS drive mappings for the chain-loaded system, @pxref{drivemap}.
+@node
check_appended_signatures
+@subsectioncheck_appended_signaturesdocs/grub.texi +2
-0+This variable controls whether GRUB enforces appended signature validation on +loaded vmlinux file. @xref{Using appended signatures}.@node
check_signatures
@subsectioncheck_signaturesdocs/grub.texi +2
-2-This variable controls whether GRUB enforces digital signature -validation on loaded files. @xref{Using digital signatures}. +This variable controls whether GRUB enforces GPG-style digital signature +validation on loaded files. @xref{Using GPG-style digital signatures}.@node chosen
@subsection chosendocs/grub.texi +0
-0@@ -6414,6 +6419,10 @@ you forget a command, you can run the command @command{help}@menu
* [:: Check file types and compare values
* acpi:: Load ACPI tablesdocs/grub.texi +4
-0+* append_add_db_cert:: Add an X.509 certificate to the db list +* append_list_db:: List trusted certificates from the db list +* append_rm_dbx_cert:: Remove a certificate from the db list +* append_verify:: Verify appended digital signature using db list* authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminaldocs/grub.texi +0
-0@@ -6535,6 +6544,68 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). unsigned code.@end deffn
+@node
append_add_db_cert
+@subsectionappend_add_db_certdocs/grub.texi +47
-0+ +@deffn Command append_add_db_cert X509_certificate +Read a DER-formatted X.509 certificate from the file @var{X509_certificate} +and add it to GRUB's internal db list of trusted X.509 certificates. These +certificates are used to validate appended signatures when the environment +variable @code{check_appended_signatures} is set to @code{enforce}. + +Note that if @code{check_appended_signatures} is set to @code{enforce} +when @command{append_add_db_cert} is executed, then @var{X509_certificate} +must itself bear an appended signature. (It is not sufficient that +@var{X509_certificate} be signed by a trusted certificate according to the +X.509 rules: GRUB does not include support for validating signatures within X.509 +certificates themselves.) + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_db +@subsection append_list_db + +@deffn Command append_list_db +List all X.509 certificates trusted by GRUB for validating appended signatures. +The output is a numbered list of certificates, showing the certificate's serial +number and Common Name. + +The certificate number can be used as an argument to +@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_rm_dbx_cert +@subsection append_rm_dbx_cert + +@deffn Command append_rm_dbx_cert cert_number +Remove the X.509 certificate numbered @var{cert_number} from GRUB's keyring of +db for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{append_list_db} (@pxref{append_list_db}). + +These certificates are used to validate appended signatures when environment +variable @code{check_appended_signatures} is set to @code{enforce} +(@pxref{check_appended_signatures}), and by @command{append_verify} +(@pxref{append_verify}). See @xref{Using appended signatures} for more +information. +@end deffn
Quoted 8 lines
docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6600
Quoted 5 lines
docs/grub.texi +5
-0+ +@node append_verify +@subsection append_verify + +@deffn Command append_verify fileI suggest to use "<>" to mark arguments which gets a value, e.g.:
append_verify<file> Please fix this in whole documentation…
Will do it.
Quoted 4 lines
Quoted 2 lines
+Verifies an appended signature on @var{file} against the trusted X.509 certificates
s/file/<file>
Will do it.
Quoted 50 lines
Quoted 50 lines
docs/grub.texi +7
-0+known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and +@pxref{append_rm_dbx_cert}). +Exit code @code{$?} is set to 0 if the signature validates +successfully. If validation fails, it is set to a non-zero value. + +See @xref{Using appended signatures}, for more information. +@end deffn@node authenticate
@subsection authenticatedocs/grub.texi +0
-0@@ -6854,7 +6925,7 @@ These keys are used to validate signatures when environment variable @code{check_signatures} is set to @code{enforce}(@pxref{check_signatures}), and by some invocations of @command{verify_detached} (@pxref{verify_detached}). @xref{Using
docs/grub.texi +1
-1-digital signatures}, for more information. +GPG-style digital signatures}, for more information.@end deffn
@node drivemap
docs/grub.texi +0
-0@@ -7250,7 +7321,6 @@ without any options, the @command{keystatus} command returns true if andonly if checking key modifier status is supported. @end deffn
-
@nodelist_env
@subsectionlist_envdocs/grub.texi +0
-0@@ -7270,7 +7340,7 @@ The output is in GPG's v4 key fingerprint format (i.e., the output of@code{gpg --fingerprint}). The least significant four bytes (last eight hexadecimal digits) can be used as an argument to @command{distrust} (@pxref{distrust}).
-@xref{Using digital signatures}, for more information about uses for +@xref{Using GPG-style digital signatures}, for more information about uses for these keys.
@end deffndocs/grub.texi +0
-0@@ -7305,8 +7375,11 @@ When used with care, @option{--skip-sig} and the whitelist enable anadministrator to configure a system to boot only signed configurations, but to allow the user to select from among multiple configurations, and to enable ``one-shot'' boot attempts and -``savedefault'' behavior. @xref{Using digital signatures}, for more +``savedefault'' behavior. @xref{Using GPG-style digital signatures}, for more information.
docs/grub.texi +3
-0+Extra care should be taken when combining this command with appended signatures +(@pxref{Using appended signatures}), as this file is not validated by an +appended signature and could set @code{check_appended_signatures=no}.@end deffn
Quoted 8 lines
docs/grub.texi read. It is possible to modify a digitally signed environment block · line 7753
Quoted 6 lines
docs/grub.texi +0
-0@@ -7677,7 +7750,7 @@ read. It is possible to modify a digitally signed environment blockfile from within GRUB using this command, such that its signature will no longer be valid on subsequent boots. Care should be taken in such advanced configurations to avoid rendering the system
docs/grub.texi +1
-1-unbootable. @xref{Using digital signatures}, for more information. +unbootable. @xref{Using GPG-style digital signatures}, for more information.I suggest to move all GPG documentation changes to separate patch.
Will do it.
Quoted 13 lines
docs/grub.texi signatures when environment variable @code{check_signatures} is set to · line 8243
Quoted 10 lines
@end deffn
docs/grub.texi +0
-0@@ -8167,11 +8240,10 @@ signatures when environment variable @code{check_signatures} is set tomust itself be properly signed. The @option{--skip-sig} option can be used to disable signature-checking when reading @var{pubkey_file} itself. It is expected that @option{--skip-sig} is useful for testing
docs/grub.texi +1
-1-and manual booting. @xref{Using digital signatures}, for more +and manual booting. @xref{Using GPG-style digital signatures}, for moreDitto and below...
Quoted 7 lines
Quoted 4 lines
information.
@end deffn-
This and similar cleanups can be a part of GPG change set but they have to be mentioned in the commit message.
Will do it.
Quoted 74 lines
Quoted 9 lines
@node unset
@subsection unsetdocs/grub.texi +0
-0@@ -8190,7 +8262,6 @@ only on PC BIOS platforms.@end deffn
@end ignore-
Ditto...
Quoted 62 lines
@node
verify_detached
@subsectionverify_detacheddocs/grub.texi +0
-0@@ -8208,7 +8279,7 @@ tried.Exit code @code{$?} is set to 0 if the signature validates successfully. If validation fails, it is set to a non-zero value. -@xref{Using digital signatures}, for more information. +@xref{Using GPG-style digital signatures}, for more information. @end deffn
@node videoinfo
docs/grub.texi +0
-0@@ -8668,14 +8739,15 @@ environment variables and commands are listed in the same order.@chapter Security
@menu
docs/grub.texi +9
-8-* Authentication and authorisation:: Users and access control -* Using digital signatures:: Booting digitally signed code -* UEFI secure boot and shim:: Booting digitally signed PE files -* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation -* Measured Boot:: Measuring boot components -* Lockdown:: Lockdown when booting on a secure setup -* TPM2 key protector:: Managing disk key with TPM2 key protector -* Signing GRUB itself:: Ensuring the integrity of the GRUB core image +* Authentication and authorisation:: Users and access control +* Using GPG-style digital signatures:: Booting digitally signed code +* Using appended signatures:: An alternative approach to booting digitally signed code +* UEFI secure boot and shim:: Booting digitally signed PE files +* Secure Boot Advanced Targeting:: Embedded information for generation number based revocation +* Measured Boot:: Measuring boot components +* Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector +* Signing GRUB itself:: Ensuring the integrity of the GRUB core image@end menu
@node Authentication and authorisation
docs/grub.texi +0
-0@@ -8751,8 +8823,8 @@ generating configuration files with authentication. You can use adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2}commands.
-@node Using digital signatures
-@section Using digital signatures in GRUB +@node Using GPG-style digital signatures +@section Using GPG-style digital signatures in GRUBGRUB's @file{core.img} can optionally provide enforcement that all files subsequently read from disk are covered by a valid digital signature.
docs/grub.texi +0
-0@@ -8835,6 +8907,81 @@ or BIOS) configuration to cause the machine to boot from a different(attacker-controlled) device. GRUB is at best only one link in a secure boot chain.
+@node Using appended signatures
+@section Using appended signatures in GRUBdocs/grub.texi +9
-0+ +GRUB supports verifying Linux-style 'appended signatures' for secure boot. +Appended signatures are PKCS#7 messages containing a signature over the +contents of a file, plus some metadata, appended to the end of a file. A file +with an appended signature ends with the magic string: + +@example +~Module signature appended~\n +@end example
Quoted 8 lines
docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8926
Quoted 6 lines
docs/grub.texi +6
-0+ +where @code{\n} represents the line feed character, @code{0x0a}. + +To enable appended signature verification, load the appendedsig module and an +x509 certificate for verification. Building the appendedsig module into the +core grub image is recommended.s/grub/GRUB/…
Will do it.
Quoted 26 lines
Quoted 26 lines
docs/grub.texi +25
-0+Certificates can be managed at boot time using the @pxref{append_add_db_cert}, +@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. +Certificates can also be built in to the core image using the @code{--x509} +parameter to @command{grub-install} or @command{grub-mkimage}. +A file can be explicitly verified using the @pxref{append_verify} command. + +Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported, +and only RSA signatures are supported. + +A file can be signed with the @command{sign-file} utility supplied with the +Linux kernel source. For example, if you have @code{signing.key} as the private +key and @code{certificate.der} as the x509 certificate containing the public key: + +@example +sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed +@end example + +Enforcement of signature verification is controlled by the +@code{check_appended_signatures} variable. Verification will only take place +when files are loaded if the variable is set to @code{enforce}. If a +certificate is built into the grub core image with the @code{--x509} parameter, +the variable will be automatically set to @code{enforce} when the appendedsig +module is loaded. + +Unlike GPG-style signatures, not all files loaded by GRUB are required to be
Quoted 8 lines
docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8958
Quoted 6 lines
docs/grub.texi +6
-0+signed. Once verification is turned on, the following file types must carry +appended signatures: + +@enumerate +@item Linux, Multiboot, BSD, XNU and Plan9 kernels +@item Grub modules, except those built in to the core images/Grub/GRUB/…
Will do it.
Quoted 25 lines
Quoted 25 lines
+@item Any new certificate files to be trusted
+@end enumeratedocs/grub.texi +22
-0+ +ACPI tables and Device Tree images will not be checked for appended signatures +but must be verified by another mechanism such as GPG-style signatures before +they will be loaded. + +No attempt is made to validate any other file type. In particular, +chain-loaded binaries are not verified - if your platform supports +chain-loading and this cannot be disabled, consider an alternative secure +boot mechanism. + +As with GPG-style appended signatures, signature checking does @strong{not} +stop an attacker with console access from dropping manually to the GRUB +console and executing: + +@example +set check_appended_signatures=no +@end example + +Refer to the section on password-protecting GRUB (@pxref{Authentication +and authorisation}) for more information on preventing this. + +Additionally, special care must be taken around the @command{loadenv} command,
Quoted 8 lines
Quoted 4 lines
docs/grub.texi +2
-0+which can be used to turn off @code{check_appended_signature}. +@node UEFI secure boot and shim
@section UEFI secure boot and shim supportMay I ask you to put all documentation patches in one group at the end of the patch set? Same applies to tests patches. I think they should land before documentation patches…
Will do it.
Thank you Daniel.
Thanks,
Sudhakar
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 14/21 ieee1275: Enter lockdown based on /ibm,secure-boot
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
If the 'ibm,secure-boot' property of the root node is 2 or greater, enter lockdown.
--- docs/grub.texi | 2 +- grub-core/Makefile.core.def | 1 + grub-core/kern/ieee1275/init.c | 42 ++++++++++++++++++++++++++++++++++ include/grub/lockdown.h | 3 ++- 4 files changed, 46 insertions(+), 2 deletions(-)
docs/grub.texi +1 -1
@@ -9060,7 +9060,7 @@ platforms. @section Lockdown when booting on a secure setup The GRUB can be locked down when booted on a secure boot environment, for example -if the UEFI secure boot is enabled. On a locked down configuration, the GRUB will +if the UEFI or Power secure boot is enabled. On a locked down configuration, the GRUB will be restricted and some operations/commands cannot be executed. This also includes limiting which filesystems are supported to those thought to be more robust and widely used within GRUB.
grub-core/Makefile.core.def +1 -0
@@ -331,6 +331,7 @@ kernel = { powerpc_ieee1275 = kern/powerpc/cache.S; powerpc_ieee1275 = kern/powerpc/dl.c; powerpc_ieee1275 = kern/powerpc/compiler-rt.S; + powerpc_ieee1275 = kern/lockdown.c; sparc64_ieee1275 = kern/sparc64/cache.S; sparc64_ieee1275 = kern/sparc64/dl.c;
grub-core/kern/ieee1275/init.c +42 -0
60 lines not on this page · raw message
include/grub/lockdown.h +2 -1
@@ -24,7 +24,8 @@#define GRUB_LOCKDOWN_DISABLED 0#define GRUB_LOCKDOWN_ENABLED 1 -#ifdef GRUB_MACHINE_EFI +#if defined(GRUB_MACHINE_EFI ) || \ + (defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275 ))extern void EXPORT_FUNC (grub_lockdown ) (void ); extern int
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 15/21 ieee1275: Read the DB and DBX secure boot variables
In reply to Sudhakar KuppusamyEnhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
If secure boot is enabled with PKS, it will read secure boot variables such as db and dbx from PKS and extract ESL's from it.
The ESL's would be saved in the platform keystore buffer, and the appendedsig (module) would read it later to extract
the certificate's details from ESL.
In the following scenarios, static key mode will be activated:
1. When Secure Boot is enabled with static keys
2. When SB Version is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled
Note:-
SB Version - Secure Boot mode
1 - PKS
0 - static key (embeded key)
--- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 2 + grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 18 +- grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 +++++++ .../kern/powerpc/ieee1275/platform_keystore.c | 343 ++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + include/grub/powerpc/ieee1275/ieee1275.h | 21 ++ .../grub/powerpc/ieee1275/platform_keystore.h | 126 +++++++ 9 files changed, 651 insertions(+), 4 deletions(-) create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
grub-core/Makefile.am +2 -0
@@ -241,11 +241,13 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h endif if COND_powerpc_ieee1275 +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/ieee1275.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/platform_keystore.h endif if COND_sparc64_ieee1275
grub-core/Makefile.core.def +2 -0
@@ -328,10 +328,12 @@ kernel = { extra_dist = video/sis315_init.c; mips_loongson = commands/keylayouts.c; + powerpc_ieee1275 = kern/powerpc/ieee1275/ieee1275.c; powerpc_ieee1275 = kern/powerpc/cache.S; powerpc_ieee1275 = kern/powerpc/dl.c; powerpc_ieee1275 = kern/powerpc/compiler-rt.S; powerpc_ieee1275 = kern/lockdown.c; + powerpc_ieee1275 = kern/powerpc/ieee1275/platform_keystore.c; sparc64_ieee1275 = kern/sparc64/cache.S; sparc64_ieee1275 = kern/sparc64/dl.c;
grub-core/kern/ieee1275/ieee1275.c +0 -1
@@ -23,7 +23,6 @@#define IEEE1275_PHANDLE_INVALID ((grub_ieee1275_cell_t) -1)#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_cell_t) 0) -#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1)
grub-core/kern/ieee1275/init.c +15 -3
@@ -51,6 +51,8 @@#endif #if defined(__powerpc__)#include <grub/lockdown.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> #endif /* The maximum heap size we're going to claim at boot. Not used by sparc. */
@@ -1004,7 +1006,8 @@ grub_get_ieee1275_secure_boot (void) {grub_ieee1275_phandle_t root;int rc; -grub_uint32_t is_sb; +grub_err_t err; +grub_uint32_t is_sb =SB_DISABLED ; rc =grub_ieee1275_finddevice ("/" , &root);if (rc !=0 )
@@ -1028,8 +1031,17 @@ grub_get_ieee1275_secure_boot (void)* * We only support enforce. */ -if (is_sb >=2 ) - grub_lockdown (); +if (is_sb ==SB_ENFORCED ) + { +grub_dprintf ("ieee1275" ,"Secure Boot Enabled\n" ); + err =grub_pks_keystore_init (); +if (err !=GRUB_ERR_NONE ) +grub_error (err,"initialization of the Platform Keystore failed.\n" ); + +grub_lockdown (); + } +else + grub_dprintf ("ieee1275", "Secure Boot Disabled\n"); }#endif
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +139 -0
139 lines not on this page · raw message
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +343 -0
343 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +3 -0
@@ -24,6 +24,9 @@#include <grub/types.h> #include <grub/machine/ieee1275.h> +#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1) +#define IEEE1275_CELL_NOT_FOUND ((grub_ieee1275_cell_t) -7) +#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0)#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1)
include/grub/powerpc/ieee1275/ieee1275.h +21 -0
@@ -28,4 +28,25 @@ typedef grub_uint32_t grub_ieee1275_cell_t;#define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T#define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T +#ifdef __powerpc__ + +extern int +EXPORT_FUNC (grub_ieee1275_test ) (const char *name, +grub_ieee1275_cell_t *missing); + +extern int +grub_ieee1275_pks_max_object_size (grub_size_t *result); + +extern int +grub_ieee1275_pks_read_object (grub_uint8_t consumer,grub_uint8_t *label, +grub_size_t label_len,grub_uint8_t *buffer, +grub_size_t buffer_len,grub_size_t *data_len, +grub_uint32_t *policies); + +extern int +grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags,grub_uint8_t sbvartype, +grub_uint8_t *buffer,grub_size_t buffer_len, +grub_size_t *data_len); +#endif +#endif /* ! GRUB_IEEE1275_MACHINE_HEADER */
include/grub/powerpc/ieee1275/platform_keystore.h new file +126 -0
126 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 15/21] ieee1275: Read the DB and DBX secure boot variables
Quoted 15 lines · Sudhakar Kuppusamy
Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
If secure boot is enabled with PKS, it will read secure boot variables such as db and dbx from PKS and extract ESL's from it.
The ESL's would be saved in the platform keystore buffer, and the appendedsig (module) would read it later to extract
the certificate's details from ESL.In the following scenarios, static key mode will be activated:
1. When Secure Boot is enabled with static keys
2. When SB Version is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled
Quoted 5 lines
Note:-
SB Version - Secure Boot mode
1 - PKS
0 - static key (embeded key)
I think "static key" mode should be explained in the commit message too. Additionally, I think it is worth explaining key differences between two modes. I do not mention these should be put into GRUB documentation...
Quoted 15 lines
Signed-off-by Sudhakar Kuppusamy <[email protected]>--- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 2 + grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 18 +- grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 +++++++ .../kern/powerpc/ieee1275/platform_keystore.c | 343 ++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + include/grub/powerpc/ieee1275/ieee1275.h | 21 ++ .../grub/powerpc/ieee1275/platform_keystore.h | 126 +++++++ 9 files changed, 651 insertions(+), 4 deletions(-) create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
[...]
Quoted 43 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +37
-0@@ -0,0 +1,343 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2024 Free Software Foundation, Inc. +* Copyright (C) 2022, 2023, 2024, 2025 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/mm.h> +#include <grub/types.h> +#include <grub/misc.h> +#include <grub/lockdown.h> +#include <grub/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> + +/* PKS consumer type for firmware. */ +#define PKS_CONSUMER_FW ((grub_uint8_t) 1) + +/* PKS read object lable for secure boot version. */ +#define SB_VERSION_KEY_NAME ((grub_uint8_t *) "SB_VERSION") +#define SB_VERSION_KEY_LEN ((grub_size_t) 10) + +/* PKS read secure boot variable request type for db and dbx. */ +#define DB ((grub_uint8_t) 1) +#define DBX ((grub_uint8_t) 2)
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 47
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ +static grub_size_t pks_max_object_size; +grub_uint8_t grub_pks_use_keystore =0 ; + +/* Platform Keystore. */ +grub_pks_t grub_pks_keystore = { .db =NULL , .dbx =NULL , .db_entries =0 , .dbx_entries =0 }; + +/* Convert the esl data into the ESL. */ +static grub_esl_t * +convert_to_esl (const grub_uint8_t *esl_data,const grub_size_t esl_data_size)
This does not convert anything. Just validate sizes and change data types. And this function is called once. So, please put its code where it belongs....
Quoted 84 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +84
-0+{ +grub_esl_t *esl =NULL ; + +if (esl_data_size <sizeof (grub_esl_t) || esl_data ==NULL ) +return esl; + + esl = (grub_esl_t *) esl_data; + +return esl; +} + +/* +* Import the GUID, esd, and its size into the pks sd buffer and +* pks sd entries from the EFI signature list. +*/ +static grub_err_t +esd_from_esl (const grub_uint8_t *esl_data,grub_size_t esl_size, +const grub_size_t signature_size,const grub_packed_guid_t *guid, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_esd_t *esd; +grub_pks_sd_t *signature = *pks_sd; +grub_size_t entries = *pks_sd_entries; +grub_size_t data_size, offset =0 ; + +/* Reads the esd from esl. */ +while (esl_size >0 ) + { + esd = (grub_esd_t *) (esl_data + offset); + data_size = signature_size -sizeof (grub_esd_t); + + signature =grub_realloc (signature, (entries +1 ) *sizeof (grub_pks_sd_t)); +if (signature ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + signature[entries].data =grub_malloc (data_size *sizeof (grub_uint8_t)); +if (signature[entries].data ==NULL ) + { +/* +* Allocated memory will be freed by +* grub_free_platform_keystore. +*/ + *pks_sd = signature; + *pks_sd_entries = entries +1 ; +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + } + +grub_memcpy (signature[entries].data, esd->signature_data, data_size); + signature[entries].data_size = data_size; + signature[entries].guid = *guid; + entries++; + esl_size -= signature_size; + offset += signature_size; + } + + *pks_sd = signature; + *pks_sd_entries = entries; + +return GRUB_ERR_NONE ; +} + +/* Extract the esd after removing the esl header from esl. */ +static grub_err_t +esl_to_esd (const grub_uint8_t *esl_data,grub_size_t *next_esl, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_packed_guid_t guid; +grub_esl_t *esl; +grub_size_t offset, esl_size, + signature_size, signature_header_size; + + esl =convert_to_esl (esl_data, *next_esl); +if (esl ==NULL ) +return grub_error (GRUB_ERR_BUG ,"invalid ESL." ); + + esl_size =grub_le_to_cpu32 (esl->signature_list_size); + signature_header_size =grub_le_to_cpu32 (esl->signature_header_size); + signature_size =grub_le_to_cpu32 (esl->signature_size); +grub_memcpy (&guid, &esl->signature_type,sizeof (grub_packed_guid_t)); + +if (esl_size <sizeof (grub_esl_t) || esl_size > *next_esl) +return grub_error (GRUB_ERR_BUG ,"invalid ESL size (%u)\n" , esl_size); + + *next_esl = esl_size;
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 141
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ offset =sizeof (grub_esl_t) + signature_header_size; + esl_size = esl_size - offset; + +return esd_from_esl (esl_data + offset, esl_size, signature_size, &guid, + pks_sd, pks_sd_entries); +} + +/* +* Import the EFI signature data and the number of esd from the esl +* into the pks sd buffer and pks sd entries.
Could you be more consistent with using abbreviations, i.e. their case? Most of them should be all upper case. And what are esd/ESD" and "sd"?
Quoted 38 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +38
-0+*/ +static grub_err_t +pks_sd_from_esl (const grub_uint8_t *esl_data,grub_size_t esl_size, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_err_t rc; +grub_size_t next_esl = esl_size; + +do + { + rc =esl_to_esd (esl_data, &next_esl, pks_sd, pks_sd_entries); +if (rc !=GRUB_ERR_NONE ) +break ; + + esl_data += next_esl; + esl_size -= next_esl; + next_esl = esl_size; + } +while (esl_size >0 ); + +return rc; +} + +/* +* Read the secure boot version from PKS as an object. +* caller must free result. +*/ +static grub_err_t +read_sbversion_from_pks (grub_uint8_t **out,grub_size_t *outlen,grub_size_t *policy) +{ +int rc; + + *out =grub_malloc (pks_max_object_size); +if (*out ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc = grub_ieee1275_pks_read_object (PKS_CONSUMER_FW, SB_VERSION_KEY_NAME, + SB_VERSION_KEY_LEN, *out, pks_max_object_size,
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 189
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ outlen, policy); +if (rc !=0 ) +return grub_error (GRUB_ERR_READ_ERROR ,"SB version read failed (%d)\n" , rc); + +return GRUB_ERR_NONE ; +} + +/* +* Reads the secure boot variable from PKS. +* caller must free result.
s/caller/Caller/
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 202
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +13
-0+*/ +static grub_err_t +read_sbvar_from_pks (const grub_uint8_t sbvarflags,const grub_uint8_t sbvartype, +grub_uint8_t **out,grub_size_t *outlen) +{ +int rc; + + *out =grub_malloc (pks_max_object_size); +if (*out ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =grub_ieee1275_pks_read_sbvar (sbvarflags, sbvartype, *out, pks_max_object_size, outlen); +if (rc == (int )IEEE1275_CELL_NOT_FOUND )
This cast suggests the rc should be defined as different type...
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 205
Quoted 3 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+return grub_error (GRUB_ERR_UNKNOWN_COMMAND ,"secure boot variable %s not found (%d)" , + (sbvartype ==DB ?"db" :"dbx" ), rc); + else if (rc != 0)
If you look at the code of the grub_ieee1275_pks_read_sbvar() then you will see you return "-1" in case of an error. So, I think you should do "rc < 0" check here...
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+return grub_error (GRUB_ERR_READ_ERROR ,"secure boot variable %s reading (%d)" , + (sbvartype ==DB ?"db" :"dbx" ), rc); + +return GRUB_ERR_NONE ; +}
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 220
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ +/* Test the availability of PKS support. */ +static grub_err_t +is_support_pks (void ) +{ +int rc; +grub_ieee1275_cell_t missing =0 ; + + rc =grub_ieee1275_test ("pks-max-object-size" , &missing); +if (rc !=0 || missing ==IEEE1275_CELL_INVALID )
Ditto...
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 225
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+grub_error (GRUB_ERR_BAD_FIRMWARE ,"firmware doesn't have PKS support.\n" ); + else + { + rc =grub_ieee1275_pks_max_object_size (&pks_max_object_size); +if (rc !=0 )
Ditto... Please fix this problem everywhere...
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 234
Quoted 9 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +9
-0+grub_error (GRUB_ERR_BAD_NUMBER ,"PKS support is there but it has zero objects.\n" ); + } + +return GRUB_ERR_NONE ; +} + +/* +* Retrieve the secure boot variable from PKS, unpacks it, read the esd +* from ESL, and store the information in the pks sd buffer.
Please be consistent with case...
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +13
-0+*/ +static grub_err_t +read_secure_boot_variables (const grub_uint8_t sbvarflags,const grub_uint8_t sbvartype, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_err_t rc; +grub_uint8_t *esl_data =NULL ; +grub_size_t esl_data_size =0 ; + + rc =read_sbvar_from_pks (sbvarflags, sbvartype, &esl_data, &esl_data_size); +if (rc ==GRUB_ERR_NONE && esl_data_size !=0 ) + rc =pks_sd_from_esl ((const grub_uint8_t *) esl_data, esl_data_size, + pks_sd, pks_sd_entries);
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 257
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+grub_free (esl_data); + +return rc; +} + +/* +* Reads secure boot version (SB_VERSION) and it supports following. +* SB_VERSION +* 1 - PKS +* 0 - static key (embeded key)
Same comment as for commit message. We need more info here...
Quoted 7 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +7
-0+*/ +static grub_err_t +get_secure_boot_version (void ) +{ +grub_err_t rc; +grub_uint8_t *data =NULL ; +grub_size_t len =0 , policy =0 ;
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 274
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ + rc =read_sbversion_from_pks (&data, &len, &policy); +if (rc ==GRUB_ERR_NONE && (len !=1 || (*data >=2 ))) + rc =grub_error (GRUB_ERR_BAD_NUMBER ,"found unexpected SB version (%d)\n" , *data); + +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key.\n" ); +if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED ) +grub_fatal ("secure boot locked down." );
s/secure boot locked down./Secure boot locked down/
However, first of all I think it should be explained here why the lockdown cannot be enabled here...
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 277
Quoted 3 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+ } +else + grub_pks_use_keystore = *data;
Why do you assign version number to grub_pks_use_keystore which looks like a flag? Probably the name of variable is wrong or its type.
Quoted 24 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +24
-0+grub_free (data); + +return rc; +} + +/* Free allocated memory. */ +void +grub_pks_free_keystore (void ) +{ +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) +grub_free (grub_pks_keystore.db[i].data); + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) +grub_free (grub_pks_keystore.dbx[i].data); + +grub_free (grub_pks_keystore.db); +grub_free (grub_pks_keystore.dbx); +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +} + +/* Initialization of the Platform Keystore. */ +grub_err_t
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 312
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+grub_pks_keystore_init (void) +{ +grub_err_t rc; + +grub_dprintf ("ieee1275" ,"trying to load Platform Keystore.\n" ); + + rc =is_support_pks (); +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key.\n" );
s/grub_printf/grub_dprintf/?
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 321
Quoted 9 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +9
-0+return rc; + } + +/* Read SB_VERSION from PKS. */ + rc =get_secure_boot_version (); +if (rc !=GRUB_ERR_NONE ) +return rc; + +if (grub_pks_use_keystore)
This looks like a flag. Should not it be defined as bool?
Quoted 53 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +22
-0+ { +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +/* Read DB from PKS. */ + rc =read_secure_boot_variables (0 ,DB , &grub_pks_keystore.db, &grub_pks_keystore.db_entries); +if (rc ==GRUB_ERR_NONE ) + { +/* Read DBX from PKS. */ + rc =read_secure_boot_variables (0 ,DBX , &grub_pks_keystore.dbx, &grub_pks_keystore.dbx_entries); +if (rc ==GRUB_ERR_UNKNOWN_COMMAND ) + { +grub_dprintf ("ieee1275" ,"dbx is not found in PKS.\n" ); + rc =GRUB_ERR_NONE ; + } + } + + } + +if (rc !=GRUB_ERR_NONE ) +grub_pks_free_keystore (); + +return rc; +}include/grub/ieee1275/ieee1275.h +3
-0@@ -24,6 +24,9 @@#include <grub/types.h> #include <grub/machine/ieee1275.h> +#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1) +#define IEEE1275_CELL_NOT_FOUND ((grub_ieee1275_cell_t) -7) +#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0)#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1)include/grub/powerpc/ieee1275/ieee1275.h +9
-0@@ -28,4 +28,25 @@ typedef grub_uint32_t grub_ieee1275_cell_t;#define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T#define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T +#ifdef __powerpc__ + +extern int +EXPORT_FUNC (grub_ieee1275_test ) (const char *name, +grub_ieee1275_cell_t *missing); + +extern int +grub_ieee1275_pks_max_object_size (grub_size_t *result); +
include/grub/powerpc/ieee1275/ieee1275.h typedef grub_uint32_t grub_ieee1275_cell_t; · line 49
Quoted 10 lines
include/grub/powerpc/ieee1275/ieee1275.h +10
-0+extern int +grub_ieee1275_pks_read_object (grub_uint8_t consumer,grub_uint8_t *label, +grub_size_t label_len,grub_uint8_t *buffer, +grub_size_t buffer_len,grub_size_t *data_len, +grub_uint32_t *policies); + +extern int +grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags,grub_uint8_t sbvartype, +grub_uint8_t *buffer,grub_size_t buffer_len, +grub_size_t *data_len);
It seems to me at least some of these functions should not be exported. If they are used in following patches then they should be exported when a given patch adds calls to these functions.
Quoted 45 lines
include/grub/powerpc/ieee1275/ieee1275.h +2
-0+#endif +#endif /* ! GRUB_IEEE1275_MACHINE_HEADER */ include/grub/powerpc/ieee1275/platform_keystore.h new file +36
-0@@ -0,0 +1,126 @@ +/* +* Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved. This +* program and the accompanying materials are licensed and made available +* under the terms and conditions of the 2-Clause BSD License which +* accompanies this distribution. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +* https://github.com/tianocore/edk2-staging (edk2-staging repo of tianocore), +* the ImageAuthentication.h file under it, and here's the copyright and license. +* +* MdePkg/Include/Guid/ImageAuthentication.h +* +* Copyright 2022, 2023, 2024, 2025 IBM Corp. +*/ +
include/grub/powerpc/ieee1275/platform_keystore.h line 46
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+#ifndef __PLATFORM_KEYSTORE_H__ +#define __PLATFORM_KEYSTORE_H__ + +#include <grub/symbol.h> +#include <grub/mm.h> +#include <grub/types.h> + +#if __GNUC__ >=9 +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +#endif
This probably should not be in the header file...
include/grub/powerpc/ieee1275/platform_keystore.h line 58
Quoted 11 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+#define GRUB_MAX_HASH_SIZE 64 + +/* Secure Boot Mode. */ +#define SB_DISABLED 0 +#define SB_ENFORCED 2 + +/* +* It is derived from EFI_SIGNATURE_DATA +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature database (ESD).*/
Aha... Here it is... So, you should use correct ESD abbreviation everywhere...
And missing space after "." here and below...
include/grub/powerpc/ieee1275/platform_keystore.h line 69
Quoted 11 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+struct grub_esd +{ +/* +* An identifier which identifies the agent which added +* the signature to the list. +*/ +grub_packed_guid_t signature_owner; +/* The format of the signature is defined by the SignatureType.*/ +grub_uint8_t signature_data[]; +}GRUB_PACKED ; +
Please drop this empty line...
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +12
-0+typedef struct grub_esd grub_esd_t ; + +/* +* It is derived from EFI_SIGNATURE_LIST +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature list (ESL).*/ +struct grub_esl +{ +/* Type of the signature. GUID signature types are defined in below.*/ +grub_packed_guid_t signature_type; +/* Total size of the signature list, including this header.*/
include/grub/powerpc/ieee1275/platform_keystore.h line 91
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+grub_uint32_t signature_list_size; +/* +* Size of the signature header which precedes +* the array of signatures. +*/ +grub_uint32_t signature_header_size; +/* Size of each signature.*/ +grub_uint32_t signature_size; +}GRUB_PACKED ; +
Ditto...
include/grub/powerpc/ieee1275/platform_keystore.h line 94
Quoted 3 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +3
-0+typedef struct grub_esl grub_esl_t ; + +/* The structure of a PKS signature data.*/
Ha! sd == signature data. So, if you really use sd abbreviation in the comments please define it here, e.g. Signature Data (SD), and use it consistently.
include/grub/powerpc/ieee1275/platform_keystore.h line 101
Quoted 7 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +7
-0+struct grub_pks_sd +{ +grub_packed_guid_t guid;/* Signature type. */ +grub_uint8_t *data;/* Signature data. */ +grub_size_t data_size;/* Size of signature data. */ +}GRUB_PACKED ; +
Redundant empty line...
include/grub/powerpc/ieee1275/platform_keystore.h line 112
Quoted 11 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+typedef struct grub_pks_sd grub_pks_sd_t ; + +/* The structure of a PKS.*/ +struct grub_pks +{ +grub_pks_sd_t *db;/* Signature database. */ +grub_pks_sd_t *dbx;/* Forbidden signature database. */ +grub_size_t db_entries;/* Size of signature database. */ +grub_size_t dbx_entries;/* Size of forbidden signature database. */ +}GRUB_PACKED ; +
Ditto...
include/grub/powerpc/ieee1275/platform_keystore.h line 124
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +12
-0+typedef struct grub_pks grub_pks_t ; + +/* Initialization of the Platform Keystore. */ +extern grub_err_t +grub_pks_keystore_init (void ); + +/* Free allocated memory. */ +extern void +EXPORT_FUNC (grub_pks_free_keystore ) (void ); + +extern grub_uint8_t EXPORT_VAR (grub_pks_use_keystore ); +extern grub_pks_t EXPORT_VAR (grub_pks_keystore );
Really, do these stuff need to be exported?
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 15/21] ieee1275: Read the DB and DBX secure boot variables
Quoted 22 lines · Daniel Kiper
Quoted 17 lines · Sudhakar Kuppusamy
Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
If secure boot is enabled with PKS, it will read secure boot variables such as db and dbx from PKS and extract ESL's from it.
The ESL's would be saved in the platform keystore buffer, and the appendedsig (module) would read it later to extract
the certificate's details from ESL.In the following scenarios, static key mode will be activated:
1. When Secure Boot is enabled with static keys
2. When SB Version is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabledNote:-
Quoted 7 lines
Quoted 3 lines
SB Version - Secure Boot mode
1 - PKS
0 - static key (embeded key)I think "static key" mode should be explained in the commit message too. Additionally, I think it is worth explaining key differences between two modes. I do not mention these should be put into GRUB documentation…
Will do it.
Quoted 68 lines
Quoted 16 lines
Signed-off-by Sudhakar Kuppusamy <[email protected]>---
grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 2 + grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 18 +- grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 +++++++ .../kern/powerpc/ieee1275/platform_keystore.c | 343 ++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + include/grub/powerpc/ieee1275/ieee1275.h | 21 ++ .../grub/powerpc/ieee1275/platform_keystore.h | 126 +++++++ 9 files changed, 651 insertions(+), 4 deletions(-)create mode 100644
grub-core/kern/powerpc/ieee1275/ieee1275.ccreate mode 100644grub-core/kern/powerpc/ieee1275/platform_keystore.ccreate mode 100644include/grub/powerpc/ieee1275/platform_keystore.h[...]
Quoted 49 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +43
-0@@ -0,0 +1,343 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2024 Free Software Foundation, Inc. +* Copyright (C) 2022, 2023, 2024, 2025 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/mm.h> +#include <grub/types.h> +#include <grub/misc.h> +#include <grub/lockdown.h> +#include <grub/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> + +/* PKS consumer type for firmware. */ +#define PKS_CONSUMER_FW ((grub_uint8_t) 1) + +/* PKS read object lable for secure boot version. */ +#define SB_VERSION_KEY_NAME ((grub_uint8_t *) "SB_VERSION") +#define SB_VERSION_KEY_LEN ((grub_size_t) 10) + +/* PKS read secure boot variable request type for db and dbx. */ +#define DB ((grub_uint8_t) 1) +#define DBX ((grub_uint8_t) 2) + +static grub_size_t pks_max_object_size; +grub_uint8_t grub_pks_use_keystore =0 ; + +/* Platform Keystore. */ +grub_pks_t grub_pks_keystore = { .db =NULL , .dbx =NULL , .db_entries =0 , .dbx_entries =0 };
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 47
Quoted 4 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +4
-0+ +/* Convert the esl data into the ESL. */ +static grub_esl_t * +convert_to_esl (const grub_uint8_t *esl_data,const grub_size_t esl_data_size)This does not convert anything. Just validate sizes and change data types. And this function is called once. So, please put its code where it belongs....
Will do it.
Quoted 90 lines
Quoted 90 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +89
-0+{ +grub_esl_t *esl =NULL ; + +if (esl_data_size <sizeof (grub_esl_t) || esl_data ==NULL ) +return esl; + + esl = (grub_esl_t *) esl_data; + +return esl; +} + +/* +* Import the GUID, esd, and its size into the pks sd buffer and +* pks sd entries from the EFI signature list. +*/ +static grub_err_t +esd_from_esl (const grub_uint8_t *esl_data,grub_size_t esl_size, +const grub_size_t signature_size,const grub_packed_guid_t *guid, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_esd_t *esd; +grub_pks_sd_t *signature = *pks_sd; +grub_size_t entries = *pks_sd_entries; +grub_size_t data_size, offset =0 ; + +/* Reads the esd from esl. */ +while (esl_size >0 ) + { + esd = (grub_esd_t *) (esl_data + offset); + data_size = signature_size -sizeof (grub_esd_t); + + signature =grub_realloc (signature, (entries +1 ) *sizeof (grub_pks_sd_t)); +if (signature ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + signature[entries].data =grub_malloc (data_size *sizeof (grub_uint8_t)); +if (signature[entries].data ==NULL ) + { +/* +* Allocated memory will be freed by +* grub_free_platform_keystore. +*/ + *pks_sd = signature; + *pks_sd_entries = entries +1 ; +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + } + +grub_memcpy (signature[entries].data, esd->signature_data, data_size); + signature[entries].data_size = data_size; + signature[entries].guid = *guid; + entries++; + esl_size -= signature_size; + offset += signature_size; + } + + *pks_sd = signature; + *pks_sd_entries = entries; + +return GRUB_ERR_NONE ; +} + +/* Extract the esd after removing the esl header from esl. */ +static grub_err_t +esl_to_esd (const grub_uint8_t *esl_data,grub_size_t *next_esl, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_packed_guid_t guid; +grub_esl_t *esl; +grub_size_t offset, esl_size, + signature_size, signature_header_size; + + esl =convert_to_esl (esl_data, *next_esl); +if (esl ==NULL ) +return grub_error (GRUB_ERR_BUG ,"invalid ESL." ); + + esl_size =grub_le_to_cpu32 (esl->signature_list_size); + signature_header_size =grub_le_to_cpu32 (esl->signature_header_size); + signature_size =grub_le_to_cpu32 (esl->signature_size); +grub_memcpy (&guid, &esl->signature_type,sizeof (grub_packed_guid_t)); + +if (esl_size <sizeof (grub_esl_t) || esl_size > *next_esl) +return grub_error (GRUB_ERR_BUG ,"invalid ESL size (%u)\n" , esl_size); + + *next_esl = esl_size; + offset =sizeof (grub_esl_t) + signature_header_size; + esl_size = esl_size - offset; + +return esd_from_esl (esl_data + offset, esl_size, signature_size, &guid, + pks_sd, pks_sd_entries);
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 141
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+} + +/* +* Import the EFI signature data and the number of esd from the esl +* into the pks sd buffer and pks sd entries. Could you be more consistent with using abbreviations, i.e. their case? Most of them should be all upper case. And what are esd/ESD" and "sd"?
Will do it.
EFI signature data (ESD) and Signature Data (SD).
Quoted 44 lines
Quoted 44 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +42
-0+*/ +static grub_err_t +pks_sd_from_esl (const grub_uint8_t *esl_data,grub_size_t esl_size, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_err_t rc; +grub_size_t next_esl = esl_size; + +do + { + rc =esl_to_esd (esl_data, &next_esl, pks_sd, pks_sd_entries); +if (rc !=GRUB_ERR_NONE ) +break ; + + esl_data += next_esl; + esl_size -= next_esl; + next_esl = esl_size; + } +while (esl_size >0 ); + +return rc; +} + +/* +* Read the secure boot version from PKS as an object. +* caller must free result. +*/ +static grub_err_t +read_sbversion_from_pks (grub_uint8_t **out,grub_size_t *outlen,grub_size_t *policy) +{ +int rc; + + *out =grub_malloc (pks_max_object_size); +if (*out ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =grub_ieee1275_pks_read_object (PKS_CONSUMER_FW ,SB_VERSION_KEY_NAME , +SB_VERSION_KEY_LEN , *out, pks_max_object_size, + outlen, policy); +if (rc !=0 ) +return grub_error (GRUB_ERR_READ_ERROR ,"SB version read failed (%d)\n" , rc); +
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 189
Quoted 6 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +6
-0+return GRUB_ERR_NONE ; +} + +/* +* Reads the secure boot variable from PKS. +* caller must free result. s/caller/Caller/
Will do it.
Quoted 16 lines
Quoted 4 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+*/ +static grub_err_t +read_sbvar_from_pks (const grub_uint8_t sbvarflags, const grub_uint8_t sbvartype,grub-core/kern/powerpc/ieee1275/platform_keystore.c line 202
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ grub_uint8_t **out, grub_size_t *outlen) +{ +int rc; + + *out =grub_malloc (pks_max_object_size); +if (*out ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =grub_ieee1275_pks_read_sbvar (sbvarflags, sbvartype, *out, pks_max_object_size, outlen); +if (rc == (int )IEEE1275_CELL_NOT_FOUND )This cast suggests the rc should be defined as different type…
Will do it.
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 205
Quoted 4 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+return grub_error (GRUB_ERR_UNKNOWN_COMMAND ,"secure boot variable %s not found (%d)" , + (sbvartype ==DB ?"db" :"dbx" ), rc); + else if (rc != 0)If you look at the code of the
grub_ieee1275_pks_read_sbvar()then you will see you return "-1" in case of an error. So, I think you should do "rc < 0" check here…
Will do it.
Quoted 19 lines
Quoted 6 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+return grub_error (GRUB_ERR_READ_ERROR ,"secure boot variable %s reading (%d)" , + (sbvartype ==DB ?"db" :"dbx" ), rc); + +return GRUB_ERR_NONE ; +}grub-core/kern/powerpc/ieee1275/platform_keystore.c line 220
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ +/* Test the availability of PKS support. */ +static grub_err_t +is_support_pks (void ) +{ +int rc; +grub_ieee1275_cell_t missing =0 ; + + rc =grub_ieee1275_test ("pks-max-object-size" , &missing); +if (rc !=0 || missing ==IEEE1275_CELL_INVALID )Ditto...
Quoted 7 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 225
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+grub_error (GRUB_ERR_BAD_FIRMWARE ,"firmware doesn't have PKS support.\n" ); + else + { + rc =grub_ieee1275_pks_max_object_size (&pks_max_object_size); +if (rc !=0 )Ditto... Please fix this problem everywhere…
Will do it.
Quoted 30 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 234
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +9
-0+grub_error (GRUB_ERR_BAD_NUMBER ,"PKS support is there but it has zero objects.\n" ); + } + +return GRUB_ERR_NONE ; +} + +/* +* Retrieve the secure boot variable from PKS, unpacks it, read the esd +* from ESL, and store the information in the pks sd buffer. Please be consistent with case...
Quoted 17 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +17
-0+*/ +static grub_err_t +read_secure_boot_variables (const grub_uint8_t sbvarflags,const grub_uint8_t sbvartype, +grub_pks_sd_t **pks_sd,grub_size_t *pks_sd_entries) +{ +grub_err_t rc; +grub_uint8_t *esl_data =NULL ; +grub_size_t esl_data_size =0 ; + + rc =read_sbvar_from_pks (sbvarflags, sbvartype, &esl_data, &esl_data_size); +if (rc ==GRUB_ERR_NONE && esl_data_size !=0 ) + rc =pks_sd_from_esl ((const grub_uint8_t *) esl_data, esl_data_size, + pks_sd, pks_sd_entries); +grub_free (esl_data); + +return rc; +}
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 257
Quoted 6 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +6
-0+ +/* +* Reads secure boot version (SB_VERSION) and it supports following. +* SB_VERSION +* 1 - PKS +* 0 - static key (embeded key) Same comment as for commit message. We need more info here…
Will do it
Quoted 21 lines
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +7
-0+*/ +static grub_err_t +get_secure_boot_version (void ) +{ +grub_err_t rc; +grub_uint8_t *data =NULL ; +grub_size_t len =0 , policy =0 ;grub-core/kern/powerpc/ieee1275/platform_keystore.c line 274
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ + rc =read_sbversion_from_pks (&data, &len, &policy); +if (rc ==GRUB_ERR_NONE && (len !=1 || (*data >=2 ))) + rc =grub_error (GRUB_ERR_BAD_NUMBER ,"found unexpected SB version (%d)\n" , *data); + +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key.\n" ); +if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED ) +grub_fatal ("secure boot locked down." );s/secure boot locked down./Secure boot locked down/
Quoted 2 lines
However, first of all I think it should be explained here why the lockdown cannot be enabled here…
Will do it.
Quoted 7 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 277
Quoted 4 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+ } +else + grub_pks_use_keystore = *data;Why do you assign version number to
grub_pks_use_keystorewhich looks like a flag? Probably the name of variable is wrong or its type.
Will change to boolean type.
Quoted 29 lines
Quoted 29 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +28
-0+grub_free (data); + +return rc; +} + +/* Free allocated memory. */ +void +grub_pks_free_keystore (void ) +{ +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) +grub_free (grub_pks_keystore.db[i].data); + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) +grub_free (grub_pks_keystore.dbx[i].data); + +grub_free (grub_pks_keystore.db); +grub_free (grub_pks_keystore.dbx); +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +} + +/* Initialization of the Platform Keystore. */ +grub_err_t +grub_pks_keystore_init (void ) +{ +grub_err_t rc; +
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 312
Quoted 6 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +6
-0+grub_dprintf ("ieee1275" ,"trying to load Platform Keystore.\n" ); + + rc =is_support_pks (); +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key.\n" );s/grub_printf/grub_dprintf/?
Will do it.
Quoted 72 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 321
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +9
-0+return rc; + } + +/* Read SB_VERSION from PKS. */ + rc =get_secure_boot_version (); +if (rc !=GRUB_ERR_NONE ) +return rc; + +if (grub_pks_use_keystore)This looks like a flag. Should not it be defined as bool?
Quoted 59 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +22
-0+ { +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +/* Read DB from PKS. */ + rc =read_secure_boot_variables (0 ,DB , &grub_pks_keystore.db, &grub_pks_keystore.db_entries); +if (rc ==GRUB_ERR_NONE ) + { +/* Read DBX from PKS. */ + rc =read_secure_boot_variables (0 ,DBX , &grub_pks_keystore.dbx, &grub_pks_keystore.dbx_entries); +if (rc ==GRUB_ERR_UNKNOWN_COMMAND ) + { +grub_dprintf ("ieee1275" ,"dbx is not found in PKS.\n" ); + rc =GRUB_ERR_NONE ; + } + } + + } + +if (rc !=GRUB_ERR_NONE ) +grub_pks_free_keystore (); + +return rc; +}include/grub/ieee1275/ieee1275.h +3
-0@@ -24,6 +24,9 @@#include <grub/types.h> #include <grub/machine/ieee1275.h> +#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1) +#define IEEE1275_CELL_NOT_FOUND ((grub_ieee1275_cell_t) -7) +#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0)#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1)include/grub/powerpc/ieee1275/ieee1275.h +15
-0@@ -28,4 +28,25 @@ typedef grub_uint32_t grub_ieee1275_cell_t;#define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T#define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T +#ifdef __powerpc__ + +extern int +EXPORT_FUNC (grub_ieee1275_test ) (const char *name, +grub_ieee1275_cell_t *missing); + +extern int +grub_ieee1275_pks_max_object_size (grub_size_t *result); + +extern int +grub_ieee1275_pks_read_object (grub_uint8_t consumer,grub_uint8_t *label, +grub_size_t label_len,grub_uint8_t *buffer, +grub_size_t buffer_len,grub_size_t *data_len, +grub_uint32_t *policies); +
Quoted 8 lines
include/grub/powerpc/ieee1275/ieee1275.h typedef grub_uint32_t grub_ieee1275_cell_t; · line 49
Quoted 4 lines
include/grub/powerpc/ieee1275/ieee1275.h +4
-0+extern int +grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags,grub_uint8_t sbvartype, +grub_uint8_t *buffer,grub_size_t buffer_len, +grub_size_t *data_len);It seems to me at least some of these functions should not be exported. If they are used in following patches then they should be exported when a given patch adds calls to these functions.
Will do it.
Quoted 50 lines
Quoted 50 lines
include/grub/powerpc/ieee1275/ieee1275.h +2
-0+#endif +#endif /* ! GRUB_IEEE1275_MACHINE_HEADER */ include/grub/powerpc/ieee1275/platform_keystore.h new file +40
-0@@ -0,0 +1,126 @@ +/* +* Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved. This +* program and the accompanying materials are licensed and made available +* under the terms and conditions of the 2-Clause BSD License which +* accompanies this distribution. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +* https://github.com/tianocore/edk2-staging (edk2-staging repo of tianocore), +* the ImageAuthentication.h file under it, and here's the copyright and license. +* +* MdePkg/Include/Guid/ImageAuthentication.h +* +* Copyright 2022, 2023, 2024, 2025 IBM Corp. +*/ + +#ifndef __PLATFORM_KEYSTORE_H__ +#define __PLATFORM_KEYSTORE_H__ + +#include <grub/symbol.h>
Quoted 8 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 46
Quoted 6 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +6
-0+#include <grub/mm.h> +#include <grub/types.h> + +#if __GNUC__ >=9 +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +#endif This probably should not be in the header file…
Will take care.
Quoted 16 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 58
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+#define GRUB_MAX_HASH_SIZE 64 + +/* Secure Boot Mode. */ +#define SB_DISABLED 0 +#define SB_ENFORCED 2 + +/* +* It is derived from EFI_SIGNATURE_DATA +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature database (ESD).*/ Aha... Here it is... So, you should use correct ESD abbreviation everywhere...
Quoted 1 line
And missing space after "." here and below...
Will do it.
Quoted 40 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 69
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+struct grub_esd +{ +/* +* An identifier which identifies the agent which added +* the signature to the list. +*/ +grub_packed_guid_t signature_owner; +/* The format of the signature is defined by the SignatureType.*/ +grub_uint8_t signature_data[]; +}GRUB_PACKED ; +Please drop this empty line...
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +12
-0+typedef struct grub_esd grub_esd_t ; + +/* +* It is derived from EFI_SIGNATURE_LIST +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature list (ESL).*/ +struct grub_esl +{ +/* Type of the signature. GUID signature types are defined in below.*/ +grub_packed_guid_t signature_type; +/* Total size of the signature list, including this header.*/ include/grub/powerpc/ieee1275/platform_keystore.h line 91
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+grub_uint32_t signature_list_size; +/* +* Size of the signature header which precedes +* the array of signatures. +*/ +grub_uint32_t signature_header_size; +/* Size of each signature.*/ +grub_uint32_t signature_size; +}GRUB_PACKED ; +Ditto...
Quoted 7 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 94
Quoted 3 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +3
-0+typedef struct grub_esl grub_esl_t ; + +/* The structure of a PKS signature data.*/ Ha! sd == signature data. So, if you really use sd abbreviation in the comments please define it here, e.g. Signature Data (SD), and use it consistently.
Sure will do it.
Quoted 25 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 101
Quoted 8 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +7
-0+struct grub_pks_sd +{ +grub_packed_guid_t guid;/* Signature type. */ +grub_uint8_t *data;/* Signature data. */ +grub_size_t data_size;/* Size of signature data. */ +}GRUB_PACKED ; +Redundant empty line...
include/grub/powerpc/ieee1275/platform_keystore.h line 112
Quoted 11 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+typedef struct grub_pks_sd grub_pks_sd_t ; + +/* The structure of a PKS.*/ +struct grub_pks +{ +grub_pks_sd_t *db;/* Signature database. */ +grub_pks_sd_t *dbx;/* Forbidden signature database. */ +grub_size_t db_entries;/* Size of signature database. */ +grub_size_t dbx_entries;/* Size of forbidden signature database. */ +}GRUB_PACKED ; +Ditto...
Quoted 14 lines
include/grub/powerpc/ieee1275/platform_keystore.h line 124
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +12
-0+typedef struct grub_pks grub_pks_t ; + +/* Initialization of the Platform Keystore. */ +extern grub_err_t +grub_pks_keystore_init (void ); + +/* Free allocated memory. */ +extern void +EXPORT_FUNC (grub_pks_free_keystore ) (void ); + +extern grub_uint8_t EXPORT_VAR (grub_pks_use_keystore ); +extern grub_pks_t EXPORT_VAR (grub_pks_keystore );Really, do these stuff need to be exported?
These are used in following patches (appended signature module). So, I exported it.
Thanks,
Sudhakar
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 16/21 appendedsig: Create db and dbx lists
In reply to Sudhakar KuppusamyIf secure boot is enabled with a static key, the trusted certificates will be extracted from the GRUB ELF NOTE and adding in the db list. The db list is used by GRUB for kernel verification.
If secure boot is enabled with a dynamic key, the trusted certificates and certificate/binary hash will be extracted from the PKS and adding in the db list. The distrusted certificates, certificate/binary hash from the PKS and adding in the dbx list. Both dbx and db list are used by GRUB for kernel verification.
Note:-
If the certificate or the certificate hash exists in the dbx list, then reject the certificate/certificate hash before adding it in the db list.
--- grub-core/commands/appendedsig/appendedsig.c | 558 +++++++++++++++++-- include/grub/crypto.h | 1 + include/grub/efi/pks.h | 112 ++++ 3 files changed, 629 insertions(+), 42 deletions(-) create mode 100644 include/grub/efi/pks.h
grub-core/commands/appendedsig/appendedsig.c +516 -42
@@ -33,13 +33,16 @@#include <libtasn1.h> #include <grub/env.h> #include <grub/lockdown.h> - +#include <grub/efi/pks.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> #include "appendedsig.h" GRUB_MOD_LICENSE ("GPLv3+" );/* Public key type. */ -#define GRUB_PKEY_ID_PKCS7 2 +#define GRUB_PKEY_ID_PKCS7 2 +/* GUID size. */ +#define GRUB_GUID_SIZE (sizeof (grub_packed_guid_t))/* Appended signature magic string. */ static const char magic[] ="~Module signature appended~\n" ;
@@ -67,8 +70,30 @@ struct grub_appended_signaturestruct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data. */ }; -/* Trusted certificates for verifying appended signatures. */ -struct x509_certificate *db; +/* This represents a db/dbx list. */ +struct grub_database +{ +struct x509_certificate *certs;/* Certificates. */ +grub_size_t cert_entries;/* Number of certificates. */ +grub_uint8_t **signatures;/* Certificate/binary hashes. */ +grub_size_t *signature_size;/* Size of certificate/binary hashes. */ +grub_size_t signature_entries;/* Number of certificate/binary hashes. */ +}; + +/* The db list */ +struct grub_database db = {.certs =NULL , .cert_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* The dbx list */ +struct grub_database dbx = {.certs =NULL , .cert_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* Free db list memory */ +static void +free_db_list (void ); +/* Free dbx list memory */ +static void +free_dbx_list (void ); static enum {
@@ -77,6 +102,201 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +/* +* GUID can be used to determine the hashing function and +* generate the hash using determined hashing function. +*/ +static grub_err_t +get_hash (const grub_packed_guid_t *guid,const grub_uint8_t *data,const grub_size_t data_size, +grub_uint8_t *hash,grub_size_t *hash_size) +{ +gcry_md_spec_t *hash_func =NULL ; + +if (guid ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"GUID is empty." ); + +if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha256; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha384; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha512; +else +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"unsupported GUID for hash." ); + +grub_memset (hash,0 ,GRUB_MAX_HASH_SIZE ); +grub_crypto_hash (hash_func, hash, data, data_size); + *hash_size = hash_func->mdlen; + +return GRUB_ERR_NONE ; +} + +/* Add the certificate/binary hash into the db/dbx list. */ +static grub_err_t +add_hash (const grub_uint8_t **data,const grub_size_t data_size, +grub_uint8_t ***signature_list,grub_size_t **signature_size_list, +grub_size_t *signature_list_entries) +{ +grub_uint8_t **signatures = *signature_list; +grub_size_t *signature_size = *signature_size_list; +grub_size_t signature_entries = *signature_list_entries; + +if (*data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate/binary-hash data or size is empty." ); + + signatures =grub_realloc (signatures,sizeof (grub_uint8_t *) * (signature_entries +1 )); + signature_size =grub_realloc (signature_size, +sizeof (grub_size_t) * (signature_entries +1 )); + +if (signatures ==NULL || signature_size ==NULL ) + { +/* +* Allocated memory will be freed by +* free_db_list/free_dbx_list. +*/ +if (signatures !=NULL ) + { + *signature_list = signatures; + *signature_list_entries = signature_entries +1 ; + } + +if (signature_size !=NULL ) + *signature_size_list = signature_size; + +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + } + + signatures[signature_entries] = (grub_uint8_t *) *data; + signature_size[signature_entries] = data_size; + signature_entries++; + *data =NULL ; + + *signature_list = signatures; + *signature_size_list = signature_size; + *signature_list_entries = signature_entries; + +return GRUB_ERR_NONE ; +} + +static bool +is_x509 (const grub_packed_guid_t *guid) +{ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +return false; +} + +static bool +is_cert_match (const struct x509_certificate *distrusted_cert, +const struct x509_certificate *db_cert) +{ +if (grub_memcmp (distrusted_cert->subject, db_cert->subject, db_cert->subject_len) ==0 + &&grub_memcmp (distrusted_cert->serial, db_cert->serial, db_cert->serial_len) ==0 + &&grub_memcmp (distrusted_cert->mpis[0 ], db_cert->mpis[0 ],sizeof (db_cert->mpis[0 ])) ==0 + &&grub_memcmp (distrusted_cert->mpis[1 ], db_cert->mpis[1 ],sizeof (db_cert->mpis[1 ])) ==0 ) +return true; + +return false; +} + + +/* Check the certificate presence in the platform keystore dbx list. */ +static grub_err_t +is_dbx_cert (const struct x509_certificate *db_cert) +{ +grub_err_t rc; +grub_size_t i; +struct x509_certificate *distrusted_cert; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL ) +continue ; + +if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true) + { + distrusted_cert =grub_zalloc (sizeof (struct x509_certificate )); +if (distrusted_cert ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =parse_x509_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, distrusted_cert); +if (rc !=GRUB_ERR_NONE ) + { +grub_free (distrusted_cert); +continue ; + } + +if (is_cert_match (distrusted_cert, db_cert) == true) + { +grub_printf ("Warning: a trusted certificate CN='%s' is ignored " +"because it is on the dbx list.\n" , db_cert->subject); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_sd_t)); +certificate_release (distrusted_cert); +grub_free (distrusted_cert); +return GRUB_ERR_ACCESS_DENIED ; + } + +certificate_release (distrusted_cert); +grub_free (distrusted_cert); + } + } + +return GRUB_ERR_NONE ; +} + +/* Add the certificate into the db/dbx list */ +static grub_err_t +add_certificate (const grub_uint8_t *data,const grub_size_t data_size, +struct grub_database *database,const bool is_db) +{ +struct x509_certificate *cert; +grub_err_t rc; +grub_size_t cert_entries = database->cert_entries; + +if (data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate data or size is empty." ); + + cert =grub_zalloc (sizeof (struct x509_certificate )); +if (cert ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =parse_x509_certificate (data, data_size, cert); +if (rc !=GRUB_ERR_NONE ) + { +grub_dprintf ("appendedsig" ,"skipped %s certificate (%d)\n" , + (is_db == true ?"trusted" :"distrusted" ), (int ) rc); +grub_free (cert); +return rc; + } + +if (is_db == true) + { + rc =is_dbx_cert (cert); +if (rc !=GRUB_ERR_NONE ) + { +certificate_release (cert); +grub_free (cert); +return rc; + } + } + +grub_dprintf ("appendedsig" ,"add a %s certificate CN='%s'\n" , + (is_db == true ?"trusted" :"distrusted" ), cert->subject); + + cert_entries++; + cert->next = database->certs; + database->certs = cert; + database->cert_entries = cert_entries; + +return rc; +} +static const char *grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val __attribute__ ((unused)))
@@ -270,7 +490,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)struct pkcs7_signerInfo *si;int i; -if (db ==NULL ) +if (!db.cert_entries)return grub_error (GRUB_ERR_BAD_SIGNATURE ,"no trusted keys to verify against." ); err =extract_appended_signature (buf, bufsize, &sig);
@@ -300,7 +520,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)grub_dprintf ("appendedsig" ,"data size %" PRIxGRUB_SIZE", signer %d hash %02x%02x%02x%02x...\n" , datasize, i, hash[0 ], hash[1 ], hash[2 ], hash[3 ]); -for (pk = db; pk !=NULL ; pk = pk->next) +for (pk = db.certs; pk !=NULL ; pk = pk->next) { err =verify_signature (pk->mpis, si->sig_mpi, si->hash, hash);if (err ==GRUB_ERR_NONE )
@@ -371,11 +591,11 @@ grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)), int argc, char **argselse if (cert_num ==0 )return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate numbers start at 1." ); -for (cert = prev_cert = db; cert !=NULL ; cert = cert->next) +for (cert = prev_cert = db.certs; cert !=NULL ; cert = cert->next) {if (cert_num ==1 )/* Match with first certificate in the db. */ { - db = cert->next; + db.certs = cert->next;break ; }else if (cert_num == i)
@@ -390,6 +610,7 @@ grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)), int argc, char **argsif (cert !=NULL ) { + db.cert_entries--;certificate_release (cert);grub_free (cert);return GRUB_ERR_NONE ;
@@ -427,8 +648,9 @@ grub_cmd_db (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)grub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); - cert->next = db; - db = cert; + cert->next = db.certs; + db.certs = cert; + db.cert_entries++;return GRUB_ERR_NONE ; }
@@ -441,7 +663,7 @@ grub_cmd_list (grub_command_t cmd __attribute__ ((unused)), int argc __attributeint cert_num =1 ;grub_size_t i; -for (cert = db; cert !=NULL ; cert = cert->next) +for (cert = db.certs; cert !=NULL ; cert = cert->next) {grub_printf ("Certificate %d:\n" , cert_num);grub_printf ("\tSerial: " );
@@ -532,6 +754,247 @@ static struct grub_fs pseudo_fs = {static grub_command_t cmd_verify, cmd_list, cmd_dbx, cmd_db; +/* Check the certificate hash presence in the dbx list. */ +static bool +is_dbx_cert_hash (const grub_uint8_t *data,const grub_size_t data_size) +{ +grub_err_t rc; +grub_size_t i, cert_hash_size =0 ; +grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE ] = {0 }; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL || + grub_pks_keystore.dbx[i].data_size ==0 ) +continue ; + + rc =get_hash (&grub_pks_keystore.dbx[i].guid, data, data_size, + cert_hash, &cert_hash_size); +if (rc !=GRUB_ERR_NONE ) +continue ; + +if (cert_hash_size == grub_pks_keystore.dbx[i].data_size && +grub_memcmp (grub_pks_keystore.dbx[i].data, cert_hash, cert_hash_size) ==0 ) + { +grub_printf ("Warning: a trusted certificate (%02x%02x%02x%02x) is ignored " +"because this certificate hash is on the dbx list.\n" , + cert_hash[0 ], cert_hash[1 ], cert_hash[2 ], cert_hash[3 ]); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_keystore.dbx[i])); +return true; + } + } + +return false; +} + +/* Check the binary hash presence in the dbx list. */ +static bool +is_dbx_binary_hash (const grub_uint8_t *binary_hash,const grub_size_t binary_hash_size) +{ +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL || + grub_pks_keystore.dbx[i].data_size ==0 ) +continue ; + +if (binary_hash_size == grub_pks_keystore.dbx[i].data_size && +grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) ==0 ) + { +grub_printf ("Warning: a trusted binary hash (%02x%02x%02x%02x) is ignored" +" because it is on the dbx list.\n" , binary_hash[0 ], binary_hash[1 ], + binary_hash[2 ], binary_hash[3 ]); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_keystore.dbx[i])); +return true; + } + } + +return false; +} + +/* Add the binary hash to the db list if it does not exist in the dbx list. */ +static grub_err_t +add_db_binary_hash (const grub_uint8_t **data,const grub_size_t data_size) +{ +if (*data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"trusted binary hash data or size is empty." ); + +if (is_dbx_binary_hash (*data, data_size) == false) +return add_hash (data, data_size, &db.signatures, &db.signature_size, + &db.signature_entries); + +return GRUB_ERR_BAD_SIGNATURE ; +} + +static bool +is_hash (const grub_packed_guid_t *guid) +{ +/* GUID type of the binary hash. */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +/* GUID type of the certificate hash. */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +return false; +} + +/* Add the X.509 certificates/binary hash to the db list. */ +static grub_err_t +create_db_list (void ) +{ +grub_err_t rc; +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) + { +if (is_hash (&grub_pks_keystore.db[i].guid) == true) + { + rc =add_db_binary_hash ((const grub_uint8_t **) &grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_x509 (&grub_pks_keystore.db[i].guid) == true) + { +if (is_dbx_cert_hash (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size) == true) +continue ; + + rc =add_certificate (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size, &db, true); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipped (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + +return GRUB_ERR_NONE ; +} + +/* Add the certificates and certificate/binary hash to the dbx list. */ +static grub_err_t +create_dbx_list (void ) +{ +grub_err_t rc; +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data !=NULL || + grub_pks_keystore.dbx[i].data_size >0 ) + { +if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true) + { + rc =add_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, &dbx, false); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_hash (&grub_pks_keystore.dbx[i].guid) == true) + { + rc =add_hash ((const grub_uint8_t **) &grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, + &dbx.signatures, &dbx.signature_size, + &dbx.signature_entries); +if (rc !=GRUB_ERR_NONE ) +return rc; + } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipped (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + } + +return GRUB_ERR_NONE ; +} + +/* +* Extract the x509 certificates from the ELF note header, +* parse it, and add it to the db list. +*/ +static grub_err_t +build_static_db_list (const struct grub_module_header *header) +{ +grub_err_t err; +struct grub_file pseudo_file; +grub_uint8_t *cert_data =NULL ; +grub_size_t cert_data_size =0 ; + +grub_memset (&pseudo_file,0 ,sizeof (pseudo_file)); + pseudo_file.fs = &pseudo_fs; + pseudo_file.size = header->size -sizeof (struct grub_module_header ); + pseudo_file.data = (char *) header +sizeof (struct grub_module_header ); + +grub_dprintf ("appendedsig" ,"found an x509 key, size=%" PRIuGRUB_UINT64_T"\n" , + pseudo_file.size); + + err =file_read_all (&pseudo_file, &cert_data, &cert_data_size); +if (err !=GRUB_ERR_NONE ) +return err; + + err =add_certificate (cert_data, cert_data_size, &db, true); +grub_free (cert_data); + +return err; +} + +/* Free db list memory */ +static void +free_db_list (void ) +{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (db.certs !=NULL ) + { + cert = db.certs; + db.certs = db.certs->next; +certificate_release (cert); +grub_free (cert); + } + +for (i =0 ; i < db.signature_entries; i++) +grub_free (db.signatures[i]); + +grub_free (db.signatures); +grub_free (db.signature_size); +grub_memset (&db,0 ,sizeof (db)); +} + +/* Free dbx list memory */ +static void +free_dbx_list (void ) +{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (dbx.certs !=NULL ) + { + cert = dbx.certs; + dbx.certs = dbx.certs->next; +certificate_release (cert); +grub_free (cert); + } + +for (i =0 ; i < dbx.signature_entries; i++) +grub_free (dbx.signatures[i]); + +grub_free (dbx.signatures); +grub_free (dbx.signature_size); +grub_memset (&dbx,0 ,sizeof (dbx)); +} +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -541,7 +1004,6 @@ GRUB_MOD_INIT (appendedsig)if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED ) check_sigs =CHECK_SIGS_FORCED ; - db =NULL ;grub_register_variable_hook ("check_appended_signatures" , grub_env_read_sec, grub_env_write_sec);grub_env_export ("check_appended_signatures" );
@@ -549,37 +1011,49 @@ GRUB_MOD_INIT (appendedsig)if (rc)grub_fatal ("error initing ASN.1 data structures: %d: %s\n" , rc,asn1_strerror (rc)); -FOR_MODULES (header) - { -struct grub_file pseudo_file; -struct x509_certificate *pk =NULL ; -grub_err_t err; - -/* Not an X.509 certificate, skip. */ -if (header->type !=OBJ_TYPE_X509_PUBKEY ) -continue ; - -grub_memset (&pseudo_file,0 ,sizeof (pseudo_file)); - pseudo_file.fs = &pseudo_fs; - pseudo_file.size = header->size -sizeof (struct grub_module_header ); - pseudo_file.data = (char *) header +sizeof (struct grub_module_header ); - -grub_dprintf ("appendedsig" ,"found an x509 key, size=%" PRIuGRUB_UINT64_T"\n" , - pseudo_file.size); - - pk =grub_zalloc (sizeof (struct x509_certificate )); -if (pk ==NULL ) -grub_fatal ("out of memory loading initial certificates." ); - - err =read_cert_from_file (&pseudo_file, pk); -if (err !=GRUB_ERR_NONE ) -grub_fatal ("error loading initial key: %s" , grub_errmsg); - -grub_dprintf ("appendedsig" ,"loaded certificate CN='%s'\n" , pk->subject); +if (!grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) + { +FOR_MODULES (header) + { +/* Not an ELF module, skip. */ +if (header->type !=OBJ_TYPE_X509_PUBKEY ) +continue ; - pk->next = db; - db = pk; - } + rc =build_static_db_list (header); +if (rc !=GRUB_ERR_NONE ) + { +free_db_list (); +grub_error (rc,"static db list creation failed." ); + } +else +grub_dprintf ("appendedsig" ,"the db list now has %" PRIuGRUB_SIZE" static keys.\n" , + db.cert_entries); + } + } +else if (grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) + { + rc =create_db_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_db_list (); +grub_error (rc,"db list creation failed." ); + } +else + { + rc =create_dbx_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_db_list (); +free_dbx_list (); +grub_error (rc,"dbx list creation failed." ); + } +else +grub_dprintf ("appendedsig" ,"the db list now has %" PRIuGRUB_SIZE" keys.\n" +"the dbx list now has %" PRIuGRUB_SIZE" keys.\n" , + db.signature_entries + db.cert_entries, dbx.signature_entries); + } +grub_pks_free_keystore (); + } cmd_db =grub_register_command ("append_add_db_cert" , grub_cmd_db,N_ ("X509_CERTIFICATE" ),N_ ("Add trusted X509_CERTIFICATE to the db" ));
include/grub/crypto.h +1 -0
@@ -510,6 +510,7 @@ grub_crypto_hmac_buffer (const struct gcry_md_spec *md,extern gcry_md_spec_t _gcry_digest_spec_md5;extern gcry_md_spec_t _gcry_digest_spec_sha1;extern gcry_md_spec_t _gcry_digest_spec_sha256; +extern gcry_md_spec_t _gcry_digest_spec_sha384;extern gcry_md_spec_t _gcry_digest_spec_sha512;extern gcry_md_spec_t _gcry_digest_spec_crc32;extern gcry_cipher_spec_t _gcry_cipher_spec_aes;
include/grub/efi/pks.h new file +112 -0
112 lines not on this page · raw message
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 16/21] appendedsig: Create db and dbx lists
Quoted 2 lines · Sudhakar Kuppusamy
If secure boot is enabled with a static key, the trusted certificates will be extracted from the GRUB ELF NOTE and adding in the db list. The db list is used by GRUB
s/and adding in the/and added to db/
Quoted 1 line
for kernel verification.
AIUI this is introduced by subsequent patch. So, it should be mentioned here...
Quoted 2 lines
If secure boot is enabled with a dynamic key, the trusted certificates and certificate/binary hash will be extracted from the PKS and adding in the db list.
s/and adding in the/and added to db/
Quoted 2 lines
The distrusted certificates, certificate/binary hash from the PKS and adding in the dbx list. Both dbx and db list are used by GRUB for kernel verification.
s/and adding in the/and added to dbx/
Quoted 5 lines
Note:-
If the certificate or the certificate hash exists in the dbx list, then reject the certificate/certificate hash before adding it in the db list.
... then do not add certificate to the db list.
Quoted 20 lines
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/commands/appendedsig/appendedsig.c | 558 +++++++++++++++++-- include/grub/crypto.h | 1 + include/grub/efi/pks.h | 112 ++++ 3 files changed, 629 insertions(+), 42 deletions(-) create mode 100644 include/grub/efi/pks.hgrub-core/commands/appendedsig/appendedsig.c +1
-1@@ -33,13 +33,16 @@#include <libtasn1.h> #include <grub/env.h> #include <grub/lockdown.h> - +#include <grub/efi/pks.h>
grub-core/commands/appendedsig/appendedsig.c line 45
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +4
-1+#include <grub/powerpc/ieee1275/platform_keystore.h> #include "appendedsig.h" GRUB_MOD_LICENSE ("GPLv3+" );/* Public key type. */ -#define GRUB_PKEY_ID_PKCS7 2 +#define GRUB_PKEY_ID_PKCS7 2 +/* GUID size. */ +#define GRUB_GUID_SIZE (sizeof (grub_packed_guid_t))
If you really need that then add it to include/grub/types.h after grub_packed_guid_t definition. And it should be renamed to GRUB_PACKED_GUID_SIZE. And for completeness GRUB_GUID_SIZE should be defined after grub_guid_t definition.
Quoted 41 lines
grub-core/commands/appendedsig/appendedsig.c +26
-2/* Appended signature magic string. */ static const char magic[] ="~Module signature appended~\n" ;@@ -67,8 +70,30 @@ struct grub_appended_signaturestruct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data. */ }; -/* Trusted certificates for verifying appended signatures. */ -struct x509_certificate *db; +/* This represents a db/dbx list. */ +struct grub_database +{ +struct x509_certificate *certs;/* Certificates. */ +grub_size_t cert_entries;/* Number of certificates. */ +grub_uint8_t **signatures;/* Certificate/binary hashes. */ +grub_size_t *signature_size;/* Size of certificate/binary hashes. */ +grub_size_t signature_entries;/* Number of certificate/binary hashes. */ +}; + +/* The db list */ +struct grub_database db = {.certs =NULL , .cert_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* The dbx list */ +struct grub_database dbx = {.certs =NULL , .cert_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* Free db list memory */ +static void +free_db_list (void ); +/* Free dbx list memory */ +static void +free_dbx_list (void ); static enum {@@ -77,6 +102,201 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +/* +* GUID can be used to determine the hashing function and
grub-core/commands/appendedsig/appendedsig.c static enum · line 116
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+* generate the hash using determined hashing function. +*/ +static grub_err_t +get_hash (const grub_packed_guid_t *guid,const grub_uint8_t *data,const grub_size_t data_size, +grub_uint8_t *hash,grub_size_t *hash_size) +{ +gcry_md_spec_t *hash_func =NULL ; + +if (guid ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"GUID is empty." );
s/GUID is empty./GUID is not available/
grub-core/commands/appendedsig/appendedsig.c static enum · line 128
Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c +11
-0+if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha256; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha384; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha512; +else +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"unsupported GUID for hash." );
s/ for//
Quoted 151 lines
grub-core/commands/appendedsig/appendedsig.c +151
-0+grub_memset (hash,0 ,GRUB_MAX_HASH_SIZE ); +grub_crypto_hash (hash_func, hash, data, data_size); + *hash_size = hash_func->mdlen; + +return GRUB_ERR_NONE ; +} + +/* Add the certificate/binary hash into the db/dbx list. */ +static grub_err_t +add_hash (const grub_uint8_t **data,const grub_size_t data_size, +grub_uint8_t ***signature_list,grub_size_t **signature_size_list, +grub_size_t *signature_list_entries) +{ +grub_uint8_t **signatures = *signature_list; +grub_size_t *signature_size = *signature_size_list; +grub_size_t signature_entries = *signature_list_entries; + +if (*data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate/binary-hash data or size is empty." ); + + signatures =grub_realloc (signatures,sizeof (grub_uint8_t *) * (signature_entries +1 )); + signature_size =grub_realloc (signature_size, +sizeof (grub_size_t) * (signature_entries +1 )); + +if (signatures ==NULL || signature_size ==NULL ) + { +/* +* Allocated memory will be freed by +* free_db_list/free_dbx_list. +*/ +if (signatures !=NULL ) + { + *signature_list = signatures; + *signature_list_entries = signature_entries +1 ; + } + +if (signature_size !=NULL ) + *signature_size_list = signature_size; + +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + } + + signatures[signature_entries] = (grub_uint8_t *) *data; + signature_size[signature_entries] = data_size; + signature_entries++; + *data =NULL ; + + *signature_list = signatures; + *signature_size_list = signature_size; + *signature_list_entries = signature_entries; + +return GRUB_ERR_NONE ; +} + +static bool +is_x509 (const grub_packed_guid_t *guid) +{ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +return false; +} + +static bool +is_cert_match (const struct x509_certificate *distrusted_cert, +const struct x509_certificate *db_cert) +{ +if (grub_memcmp (distrusted_cert->subject, db_cert->subject, db_cert->subject_len) ==0 + &&grub_memcmp (distrusted_cert->serial, db_cert->serial, db_cert->serial_len) ==0 + &&grub_memcmp (distrusted_cert->mpis[0 ], db_cert->mpis[0 ],sizeof (db_cert->mpis[0 ])) ==0 + &&grub_memcmp (distrusted_cert->mpis[1 ], db_cert->mpis[1 ],sizeof (db_cert->mpis[1 ])) ==0 ) +return true; + +return false; +} + + +/* Check the certificate presence in the platform keystore dbx list. */ +static grub_err_t +is_dbx_cert (const struct x509_certificate *db_cert) +{ +grub_err_t rc; +grub_size_t i; +struct x509_certificate *distrusted_cert; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL ) +continue ; + +if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true) + { + distrusted_cert =grub_zalloc (sizeof (struct x509_certificate )); +if (distrusted_cert ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =parse_x509_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, distrusted_cert); +if (rc !=GRUB_ERR_NONE ) + { +grub_free (distrusted_cert); +continue ; + } + +if (is_cert_match (distrusted_cert, db_cert) == true) + { +grub_printf ("Warning: a trusted certificate CN='%s' is ignored " +"because it is on the dbx list.\n" , db_cert->subject); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_sd_t)); +certificate_release (distrusted_cert); +grub_free (distrusted_cert); +return GRUB_ERR_ACCESS_DENIED ; + } + +certificate_release (distrusted_cert); +grub_free (distrusted_cert); + } + } + +return GRUB_ERR_NONE ; +} + +/* Add the certificate into the db/dbx list */ +static grub_err_t +add_certificate (const grub_uint8_t *data,const grub_size_t data_size, +struct grub_database *database,const bool is_db) +{ +struct x509_certificate *cert; +grub_err_t rc; +grub_size_t cert_entries = database->cert_entries; + +if (data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate data or size is empty." ); + + cert =grub_zalloc (sizeof (struct x509_certificate )); +if (cert ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory." ); + + rc =parse_x509_certificate (data, data_size, cert); +if (rc !=GRUB_ERR_NONE ) + { +grub_dprintf ("appendedsig" ,"skipped %s certificate (%d)\n" , + (is_db == true ?"trusted" :"distrusted" ), (int ) rc); +grub_free (cert); +return rc; + } + +if (is_db == true) + { + rc =is_dbx_cert (cert);
grub-core/commands/appendedsig/appendedsig.c static enum · line 290
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+if (rc !=GRUB_ERR_NONE ) + { +certificate_release (cert); +grub_free (cert); +return rc; + } + } + +grub_dprintf ("appendedsig" ,"add a %s certificate CN='%s'\n" , + (is_db == true ?"trusted" :"distrusted" ), cert->subject);
Missing spaces around ":"... Or better
(is_db == true) ? "trusted" : "distrusted"
Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +8
-0+ + cert_entries++; + cert->next = database->certs; + database->certs = cert; + database->cert_entries = cert_entries; + +return rc; +}
[...]
Quoted 1 line
+/* Add the X.509 certificates/binary hash to the db list. */
From where? PKS? Please be precise?
Quoted 25 lines
grub-core/commands/appendedsig/appendedsig.c +25
-0+static grub_err_t +create_db_list (void ) +{ +grub_err_t rc; +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) + { +if (is_hash (&grub_pks_keystore.db[i].guid) == true) + { + rc =add_db_binary_hash ((const grub_uint8_t **) &grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_x509 (&grub_pks_keystore.db[i].guid) == true) + { +if (is_dbx_cert_hash (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size) == true) +continue ; + + rc =add_certificate (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size, &db, true); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc;
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 885
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipped (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + +return GRUB_ERR_NONE ; +} + +/* Add the certificates and certificate/binary hash to the dbx list. */
Ditto...
Quoted 25 lines
grub-core/commands/appendedsig/appendedsig.c +25
-0+static grub_err_t +create_dbx_list (void ) +{ +grub_err_t rc; +grub_size_t i; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data !=NULL || + grub_pks_keystore.dbx[i].data_size >0 ) + { +if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true) + { + rc =add_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, &dbx, false); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_hash (&grub_pks_keystore.dbx[i].guid) == true) + { + rc =add_hash ((const grub_uint8_t **) &grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, + &dbx.signatures, &dbx.signature_size, + &dbx.signature_entries); +if (rc !=GRUB_ERR_NONE )
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 920
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+return rc; + } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipped (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + } + +return GRUB_ERR_NONE ; +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 17/21 appendedsig: Verify the kernel using db and dbx lists
In reply to Sudhakar KuppusamyTo verify the kernel's signature: verify the kernel binary against lists of binary hashes that are either in dbx or db list. If it is not list in either db or dbx list then the trusted keys from the db list are used to verify the signature.
--- grub-core/commands/appendedsig/appendedsig.c | 148 ++++++++++++++----- 1 file changed, 115 insertions(+), 33 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +115 -33
@@ -478,6 +478,77 @@ verify_signature (const gcry_mpi_t *pkmpi, const gcry_mpi_t hmpi,return GRUB_ERR_NONE ; } +static grub_err_t +get_binary_hash (const grub_size_t binary_hash_size,const grub_uint8_t *data, +const grub_size_t data_size,grub_uint8_t *hash,grub_size_t *hash_size) +{ +grub_packed_guid_t guid = {0 }; + +/* support SHA256, SHA384 and SHA512 for binary hash */ +if (binary_hash_size ==32 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_GUID_SIZE ); +else if (binary_hash_size ==48 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ); +else if (binary_hash_size ==64 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ); +else + { +grub_dprintf ("appendedsig" ,"unsupported hash type (%" PRIuGRUB_SIZE") and " +"skipped the binary hash\n" , binary_hash_size); +return GRUB_ERR_UNKNOWN_COMMAND ; + } + +return get_hash (&guid, data, data_size, hash, hash_size); +} + +/* +* Verify binary hash against the db and dbx list. +* The following errors can occur: +* - GRUB_ERR_BAD_SIGNATURE: indicates that the hash is in dbx list. +* - GRUB_ERR_EOF: the hash could not be found in the db and dbx list. +* - GRUB_ERR_NONE: the hash is found in db list. +*/ +static grub_err_t +verify_binary_hash (const grub_uint8_t *data,const grub_size_t data_size) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 , hash_size =0 ; +grub_uint8_t hash[GRUB_MAX_HASH_SIZE ] = {0 }; + +for (i =0 ; i < dbx.signature_entries; i++) + { + rc =get_binary_hash (dbx.signature_size[i], data, data_size, hash, &hash_size); +if (rc !=GRUB_ERR_NONE ) +continue ; + +if (hash_size == dbx.signature_size[i] && +grub_memcmp (dbx.signatures[i], hash, hash_size) ==0 ) + { +grub_dprintf ("appendedsig" ,"the binary hash (%02x%02x%02x%02x) is present in the dbx list\n" , + hash[0 ], hash[1 ], hash[2 ], hash[3 ]); +return GRUB_ERR_BAD_SIGNATURE ; + } + } + +for (i =0 ; i < db.signature_entries; i++) + { + rc =get_binary_hash (db.signature_size[i], data, data_size, hash, &hash_size); +if (rc !=GRUB_ERR_NONE ) +continue ; + +if (hash_size == db.signature_size[i] && +grub_memcmp (db.signatures[i], hash, hash_size) ==0 ) + { +grub_dprintf ("appendedsig" ,"verified with a trusted binary hash (%02x%02x%02x%02x)\n" , + hash[0 ], hash[1 ], hash[2 ], hash[3 ]); +return GRUB_ERR_NONE ; + } + } + +return GRUB_ERR_EOF ; +} + +/* Verify the kernel's integrity using db and dbx list */ static grub_err_t grub_verify_appended_signature (const grub_uint8_t *buf,grub_size_t bufsize) {
@@ -490,7 +561,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)struct pkcs7_signerInfo *si;int i; -if (!db.cert_entries) +if (!db.cert_entries && !db.signature_entries)return grub_error (GRUB_ERR_BAD_SIGNATURE ,"no trusted keys to verify against." ); err =extract_appended_signature (buf, bufsize, &sig);
@@ -498,45 +569,56 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)return err; datasize = bufsize - sig.signature_len; - -for (i =0 ; i < sig.pkcs7.signerInfo_count; i++) +/* Verify binary hash against the db and dbx list. */ + err =verify_binary_hash (buf, datasize); +if (err ==GRUB_ERR_BAD_SIGNATURE ) { -/* -* This could be optimised in a couple of ways: -* - we could only compute hashes once per hash type. -* - we could track signer information and only verify where IDs match. -* For now we do the naive O(trusted keys * pkcs7 signers) approach. -*/ - si = &sig.pkcs7.signerInfos[i]; - context =grub_zalloc (si->hash->contextsize); -if (context ==NULL ) -return grub_errno; - - si->hash->init (context,0 ); - si->hash->write (context, buf, datasize); - si->hash->final (context); - hash = si->hash->read (context); - -grub_dprintf ("appendedsig" ,"data size %" PRIxGRUB_SIZE", signer %d hash %02x%02x%02x%02x...\n" , - datasize, i, hash[0 ], hash[1 ], hash[2 ], hash[3 ]); - -for (pk = db.certs; pk !=NULL ; pk = pk->next) +pkcs7_signedData_release (&sig.pkcs7); +return grub_error (err,"failed to verify the signature because this binary is in the dbx list.\n" ); + } +else + { +/* Verify kernel binary signature using trusted keys from db list */ +for (i =0 ; i < sig.pkcs7.signerInfo_count; i++) { - err =verify_signature (pk->mpis, si->sig_mpi, si->hash, hash); -if (err ==GRUB_ERR_NONE ) +/* +* This could be optimised in a couple of ways: +* - we could only compute hashes once per hash type. +* - we could track signer information and only verify where IDs match. +* For now we do the naive O(trusted keys * pkcs7 signers) approach. +*/ + si = &sig.pkcs7.signerInfos[i]; + context =grub_zalloc (si->hash->contextsize); +if (context ==NULL ) +return grub_errno; + + si->hash->init (context,0 ); + si->hash->write (context, buf, datasize); + si->hash->final (context); + hash = si->hash->read (context); + +grub_dprintf ("appendedsig" , +"data size %" PRIxGRUB_SIZE", signer %d hash %02x%02x%02x%02x...\n" , + datasize, i, hash[0 ], hash[1 ], hash[2 ], hash[3 ]); + +for (pk = db.certs; pk !=NULL ; pk = pk->next) { -grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' succeeded.\n" , + err =verify_signature (pk->mpis, si->sig_mpi, si->hash, hash); +if (err ==GRUB_ERR_NONE ) + { +grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' succeeded.\n" , + i, pk->subject); +break ; + } + +grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' failed\n" , i, pk->subject); -break ; } -grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' failed\n" , - i, pk->subject); +grub_free (context); +if (err ==GRUB_ERR_NONE ) +break ; } - -grub_free (context); -if (err ==GRUB_ERR_NONE ) -break ; }pkcs7_signedData_release (&sig.pkcs7);
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 17/21] appendedsig: Verify the kernel using db and dbx lists
Quoted 2 lines · Sudhakar Kuppusamy
To verify the kernel's signature: verify the kernel binary against lists of binary hashes that are either in dbx or db list. If it is not list in either db or dbx list
s/not list/not in the list/
Quoted 5 lines
then the trusted keys from the db list are used to verify the signature.
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>
Otherwise Reviewed-by: Daniel Kiper <[email protected]>...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 18/21 powerpc_ieee1275: Introduce use_static_keys flag
In reply to Sudhakar KuppusamyIntroduce the use_static_keys flag to indicate that static keys are to be used rather than keys from the PKS storage's DB variable. This flag is set when Secure Boot is enabled with PKS but the DB variable is not present in the PKS storage. The appendedsig module would use this flag to extract the static keys from the GRUB ELF Note and stored in the db list.
--- grub-core/kern/powerpc/ieee1275/platform_keystore.c | 12 +++++++++++- include/grub/powerpc/ieee1275/platform_keystore.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-)
grub-core/kern/powerpc/ieee1275/platform_keystore.c +11 -1
@@ -40,7 +40,8 @@ static grub_size_t pks_max_object_size;grub_uint8_t grub_pks_use_keystore =0 ;/* Platform Keystore. */ -grub_pks_t grub_pks_keystore = { .db =NULL , .dbx =NULL , .db_entries =0 , .dbx_entries =0 }; +grub_pks_t grub_pks_keystore = { .db =NULL , .dbx =NULL , .db_entries =0 , .dbx_entries =0 , + .use_static_keys = false };/* Convert the esl data into the ESL. */ static grub_esl_t *
@@ -323,6 +324,15 @@ grub_pks_keystore_init (void)grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t));/* Read DB from PKS. */ rc =read_secure_boot_variables (0 ,DB , &grub_pks_keystore.db, &grub_pks_keystore.db_entries); +if (rc ==GRUB_ERR_UNKNOWN_COMMAND ) + { + rc =GRUB_ERR_NONE ; +/* +* DB variable won't be available by default in PKS. +* So, it will use the static key as a default key from the GRUB ELF Note. +*/ + grub_pks_keystore.use_static_keys = true; + }if (rc ==GRUB_ERR_NONE ) {/* Read DBX from PKS. */
include/grub/powerpc/ieee1275/platform_keystore.h +1 -0
@@ -108,6 +108,7 @@ struct grub_pksgrub_pks_sd_t *dbx;/* Forbidden signature database. */ grub_size_t db_entries;/* Size of signature database. */ grub_size_t dbx_entries;/* Size of forbidden signature database. */ +bool use_static_keys;/* Flag to indicate use of static keys. */ }GRUB_PACKED ;typedef struct grub_pks grub_pks_t ;
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 18/21] powerpc_ieee1275: Introduce use_static_keys flag
Quoted 9 lines · Sudhakar Kuppusamy
Introduce the
use_static_keysflag to indicate that static keys are to be used rather than keys from the PKS storage's DB variable. This flag is set when Secure Boot is enabled with PKS but the DB variable is not present in the PKS storage. The appendedsig module would use this flag to extract the static keys from the GRUB ELF Note and stored in the db list.Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>
By the way, I hope this behavior is described in the GRUB documentation somewhere...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 19/21 appendedsig: Read default DB keys from the ELF Note
In reply to Sudhakar KuppusamyIf Secure Boot is enabled with PKS and the use_static_keys flag is set, then read the static keys as a DB default keys from the ELF note and add stored in the db list.
--- grub-core/commands/appendedsig/appendedsig.c | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +43 -16
@@ -1006,7 +1006,7 @@ create_dbx_list (void)* parse it, and add it to the db list. */ static grub_err_t -build_static_db_list (const struct grub_module_header *header) +build_static_db_list (const struct grub_module_header *header,const bool is_pks) {grub_err_t err;struct grub_file pseudo_file;
@@ -1025,6 +1025,12 @@ build_static_db_list (const struct grub_module_header *header)if (err !=GRUB_ERR_NONE )return err; +if (is_pks == true) + { +if (is_dbx_cert_hash (cert_data, cert_data_size) == true) +return GRUB_ERR_ACCESS_DENIED ; + } + err =add_certificate (cert_data, cert_data_size, &db, true);grub_free (cert_data);
@@ -1077,6 +1083,25 @@ free_dbx_list (void)grub_memset (&dbx,0 ,sizeof (dbx)); } +static grub_err_t +load_static_keys (const struct grub_module_header *header,const bool is_pks) +{ +int rc =GRUB_ERR_NONE ; + +FOR_MODULES (header) + { +/* Not an ELF module, skip. */ +if (header->type !=OBJ_TYPE_X509_PUBKEY ) +continue ; + + rc =build_static_db_list (header, is_pks); +if (rc !=GRUB_ERR_NONE ) +return rc; + } + +return rc; +} +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -1095,26 +1120,28 @@ GRUB_MOD_INIT (appendedsig)if (!grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) { -FOR_MODULES (header) + rc =load_static_keys (header, false); +if (rc !=GRUB_ERR_NONE ) { -/* Not an ELF module, skip. */ -if (header->type !=OBJ_TYPE_X509_PUBKEY ) -continue ; - - rc =build_static_db_list (header); -if (rc !=GRUB_ERR_NONE ) - { -free_db_list (); -grub_error (rc,"static db list creation failed." ); - } -else -grub_dprintf ("appendedsig" ,"the db list now has %" PRIuGRUB_SIZE" static keys.\n" , - db.cert_entries); +free_db_list (); +grub_error (rc,"static db list creation failed." ); } +else +grub_dprintf ("appendedsig" ,"the db list now has %" PRIuGRUB_SIZE" static keys.\n" , + db.cert_entries); }else if (grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) { - rc =create_db_list (); + +if (grub_pks_keystore.use_static_keys == true) + { +grub_printf ("Warning: db variable is not available at PKS and using a static keys " +"as a default key in db list\n" ); + rc =load_static_keys (header, grub_pks_keystore.use_static_keys); + } +else + rc =create_db_list (); +if (rc !=GRUB_ERR_NONE ) {free_db_list ();
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 19/21] appendedsig: Read default DB keys from the ELF Note
Quoted 2 lines · Sudhakar Kuppusamy
If Secure Boot is enabled with PKS and the
use_static_keysflag is set, then read the static keys as a DB default keys from the ELF note and
By convention UEFI uses lowercase "db". However, as I can see you mix cases or even prefer DB. I suggest to stick to UEFI convention and use db, dbx, ... Please fix this in all patches.
Quoted 62 lines
add stored in the db list.
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/commands/appendedsig/appendedsig.c | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-)grub-core/commands/appendedsig/appendedsig.c +26
-1@@ -1006,7 +1006,7 @@ create_dbx_list (void)* parse it, and add it to the db list. */ static grub_err_t -build_static_db_list (const struct grub_module_header *header) +build_static_db_list (const struct grub_module_header *header,const bool is_pks) {grub_err_t err;struct grub_file pseudo_file;@@ -1025,6 +1025,12 @@ build_static_db_list (const struct grub_module_header *header)if (err !=GRUB_ERR_NONE )return err; +if (is_pks == true) + { +if (is_dbx_cert_hash (cert_data, cert_data_size) == true) +return GRUB_ERR_ACCESS_DENIED ; + } + err =add_certificate (cert_data, cert_data_size, &db, true);grub_free (cert_data);@@ -1077,6 +1083,25 @@ free_dbx_list (void)grub_memset (&dbx,0 ,sizeof (dbx)); } +static grub_err_t +load_static_keys (const struct grub_module_header *header,const bool is_pks) +{ +int rc =GRUB_ERR_NONE ; + +FOR_MODULES (header) + { +/* Not an ELF module, skip. */ +if (header->type !=OBJ_TYPE_X509_PUBKEY ) +continue ; + + rc =build_static_db_list (header, is_pks); +if (rc !=GRUB_ERR_NONE ) +return rc; + } + +return rc; +} +GRUB_MOD_INIT (appendedsig) {int rc;
grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1121
Quoted 2 lines
grub-core/commands/appendedsig/appendedsig.c +0
-0@@ -1095,26 +1120,28 @@ GRUB_MOD_INIT (appendedsig)if (!grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED )
Again, this shows that almost certainly grub_pks_use_keystore is a flag. So, it should be defined as bool...
Additionally, after closer looking at the code it seems to me check_sigs could be defined as bool too. Only CHECK_SIGS_NO and CHECK_SIGS_FORCED seems to have meaningful usage and CHECK_SIGS_ENFORCE is used only in definition and one barely meaningful place.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 20/21 appendedsig: Introduce GRUB commands to access db and dbx
In reply to Sudhakar KuppusamyIntroducing the following db and dbx commands
1. append_list_db:
It will show the list of trusted certificates and binary hashes
2. append_list_dbx:
It will show the list of distrusted certificates and binary/certificate hashes
3. append_add_db_cert:
It will add the trusted certificate to the db list
4. append_add_db_sig:
It will add the certificate/binary hash to the db list
5. append_rm_dbx_cert:
It will remove the trusted certificate from the db list
6. append_add_dbx_sig:
It will add the certificate/binary hash to the dbx list
Note:-
The addition/deletion of trusted certificates and binary hashes
are not allowed in GRUB command prompt while secure boot is enabled.
--- grub-core/commands/appendedsig/appendedsig.c | 351 +++++++++++++++---- 1 file changed, 286 insertions(+), 65 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +286 -65
@@ -102,6 +102,38 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +enum +{ +OPTION_BINARY_HASH =0 , +OPTION_CERT_HASH =1 +}; + +static const struct grub_arg_option options[] = +{ + {"binary-hash" ,'b' ,0 ,N_ ("hash file of the binary." ),0 ,ARG_TYPE_NONE }, + {"cert-hash" ,'c' ,1 ,N_ ("hash file of the certificate." ),0 ,ARG_TYPE_NONE }, + {0 ,0 ,0 ,0 ,0 ,0 } +}; + +static void +print_hex (const grub_uint8_t *data,const grub_size_t length) +{ +grub_size_t i, count =0 ; + +for (i =0 ; i < length-1 ; i++) + { +grub_printf ("%02x:" , data[i]); + count++; +if (count ==16 ) + { +grub_printf ("\n\t " ); + count =0 ; + } + } + +grub_printf ("%02x\n" , data[i]); +} +/* * GUID can be used to determine the hashing function and * generate the hash using determined hashing function.
@@ -634,37 +666,203 @@ grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc {grub_file_t signed_file;grub_err_t err; -grub_uint8_t *data; -grub_size_t file_size; +grub_uint8_t *signed_data; +grub_size_t signed_size;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a signed file is expected.\nExample:\n\tappend_verify <SIGNED FILE>\n" );grub_dprintf ("appendedsig" ,"verifying %s\n" , args[0 ]); signed_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_VERIFY_SIGNATURE );if (signed_file ==NULL ) -return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"could not open %s file." , args[0 ]); +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"could not open %s file." , args[0 ]); - err =file_read_all (signed_file, &data, &file_size); + err =file_read_all (signed_file, &signed_data, &signed_size);if (err ==GRUB_ERR_NONE ) - err =grub_verify_appended_signature (data, file_size); + err =grub_verify_appended_signature (signed_data, signed_size);grub_file_close (signed_file); -grub_free (data); +grub_free (signed_data); + +return err; +} + +static grub_err_t +grub_cmd_list_db (grub_command_t cmd __attribute__((unused)), +int argc __attribute__((unused)),char **args __attribute__((unused))) +{ +struct x509_certificate *cert; +grub_size_t i, cert_num =1 ; + +for (cert = db.certs; cert; cert = cert->next) + { +grub_printf ("trusted X.509 certificate %" PRIuGRUB_SIZE":\n" , cert_num); +grub_printf ("\tserial: " ); + +for (i =0 ; i < cert->serial_len -1 ; i++) +grub_printf ("%02x:" , cert->serial[i]); + +grub_printf ("%02x\n" , cert->serial[cert->serial_len -1 ]); +grub_printf ("\tCN: %s\n\n" , cert->subject); + cert_num++; + } + +for (i =0 ; i < db.signature_entries; i++) + { +grub_printf ("trusted binary hash %" PRIuGRUB_SIZE":\n" , i +1 ); +grub_printf ("\thash: " ); +print_hex (db.signatures[i], db.signature_size[i]); + } + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_cmd_list_dbx (grub_command_t cmd __attribute__((unused)), +int argc __attribute__((unused)),char **args __attribute__((unused))) +{ +struct x509_certificate *cert; +grub_size_t i, cert_num =1 ; + +for (cert = dbx.certs; cert; cert = cert->next) + { +grub_printf ("distrusted X.509 certificate %" PRIuGRUB_SIZE":\n" , cert_num); +grub_printf ("\tserial: " ); + +for (i =0 ; i < cert->serial_len -1 ; i++) +grub_printf ("%02x:" , cert->serial[i]); + +grub_printf ("%02x\n" , cert->serial[cert->serial_len -1 ]); +grub_printf ("\tCN: %s\n\n" , cert->subject); + cert_num++; + } + +for (i =0 ; i < dbx.signature_entries; i++) + { +grub_printf ("distrusted certificate/binary hash %" PRIuGRUB_SIZE":\n" , i +1 ); +grub_printf ("\thash: " ); +print_hex (dbx.signatures[i], dbx.signature_size[i]); + } + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_cmd_db_cert (grub_command_t cmd __attribute__((unused)),int argc,char **args) +{ +grub_err_t err; +grub_file_t cert_file; +struct x509_certificate *cert; + +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a trusted X.509 certificate file is expected in DER format.\n" +"Example:\n\tappend_add_db_cert <CERT FILE>\n" ); + +if (check_sigs ==CHECK_SIGS_FORCED ) +return grub_error (GRUB_ERR_ACCESS_DENIED , +"since secure boot is enabled, " +"adding of trusted X.509 certificate is not permitted!\n" ); + +if (grub_strlen (args[0 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing trusted X.509 certificate file." ); + + cert_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_CERTIFICATE_TRUST | +GRUB_FILE_TYPE_NO_DECOMPRESS ); +if (cert_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND , +"unable to open the trusted X.509 certificate file." ); + + cert =grub_zalloc (sizeof (struct x509_certificate )); +if (cert ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not allocate memory for certificate." ); + + err =read_cert_from_file (cert_file, cert); +grub_file_close (cert_file); +if (err !=GRUB_ERR_NONE ) + { +grub_free (cert); +return err; + } + +grub_dprintf ("appendedsig" ,"added the certificate with CN: %s\n" , cert->subject); + + cert->next = db.certs; + db.certs = cert; + db.cert_entries++;return err; }static grub_err_t -grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_db_sig (grub_command_t cmd __attribute__((unused)),int argc,char **args) +{ +grub_err_t rc; +grub_file_t hash_file; +grub_uint8_t *hash_data =NULL ; +grub_size_t hash_data_size =0 ; + +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a trusted binary hash file is expected.\n" +"Example:\n\tappend_add_db_sig <BINARY HASH FILE>\n" ); + +if (check_sigs ==CHECK_SIGS_FORCED ) +return grub_error (GRUB_ERR_ACCESS_DENIED , +"since secure boot is enabled, " +"adding of trusted binary hash is not permitted.\n" ); + +if (grub_strlen (args[0 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing trusted binary hash file." ); + + hash_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_TO_HASH |GRUB_FILE_TYPE_NO_DECOMPRESS ); +if (hash_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"unable to open the trusted binary hash file." ); + + rc =file_read_all (hash_file, &hash_data, &hash_data_size); +grub_file_close (hash_file); +if (rc ==GRUB_ERR_NONE ) + { +grub_dprintf ("appendedsig" , +"adding a trusted binary hash %s\n with size of %" PRIuGRUB_SIZE"\n" , + hash_data, hash_data_size); + +/* Only accept SHA256, SHA384 and SHA512 binary hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable trusted binary hash type." ); + + rc =add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &db.signatures, + &db.signature_size, &db.signature_entries); +grub_free (hash_data); +if (rc !=GRUB_ERR_NONE ) + { +free_db_list (); +free_dbx_list (); +return grub_error (rc,"adding of trusted binary hash failed." ); + } + } + +return rc; +} + +static grub_err_t +grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) {unsigned long cert_num, i =0 ;const char *end;struct x509_certificate *cert, *prev_cert;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a distrusted certificate number is expected.\n" +"Example:\n\tappend_rm_dbx_cert <CERT NUMBER>\n" ); + +if (check_sigs ==CHECK_SIGS_FORCED ) +return grub_error (GRUB_ERR_ACCESS_DENIED , +"since secure boot is enabled, " +"removing of distrusted X.509 certificate is not permitted.\n" ); cert_num =grub_strtoul (args[0 ], &end,10 );if (*(args[0 ]) =='\0' || *end !='\0' )
@@ -703,62 +901,69 @@ grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)), int argc, char **args }static grub_err_t -grub_cmd_db (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_dbx_sig (grub_extcmd_context_t ctxt,int argc,char **args) { -grub_file_t certf; -struct x509_certificate *cert =NULL ; -grub_err_t err; - -if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); - - certf =grub_file_open (args[0 ],GRUB_FILE_TYPE_CERTIFICATE_TRUST |GRUB_FILE_TYPE_NO_DECOMPRESS ); -if (certf ==NULL ) -return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"could not open %s file." , args[0 ]); +grub_err_t rc; +grub_file_t hash_file =NULL ; +grub_uint8_t *hash_data =NULL ; +grub_size_t hash_data_size =0 ; - cert =grub_zalloc (sizeof (struct x509_certificate )); -if (cert ==NULL ) -return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not allocate memory for certificate." ); +if (argc !=2 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a distrusted certificate/binary hash file is expected\n" +"Example:\n\tappend_rm_dbx_sig [option] <FILE>\n" +"option:\n[-b|--binary-hash] FILE [BINARY HASH FILE]\n" +"[-c|--cert-hash] FILE [CERTFICATE HASH FILE]\n" ); - err =read_cert_from_file (certf, cert); -grub_file_close (certf); -if (err !=GRUB_ERR_NONE ) - { -grub_free (cert); -return err; - } +if (check_sigs ==CHECK_SIGS_FORCED ) +return grub_error (GRUB_ERR_ACCESS_DENIED , +"since secure boot is enabled, " +"adding of distrusted certificate/binary hash is not permitted!\n" ); -grub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); +if (!ctxt->state[OPTION_BINARY_HASH ].set && !ctxt->state[OPTION_CERT_HASH ].set) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"missing options and use --help to konw." ); - cert->next = db.certs; - db.certs = cert; - db.cert_entries++; +if (grub_strlen (args[1 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing distrusted certificate/binary hash file." ); -return GRUB_ERR_NONE ; -} + hash_file =grub_file_open (args[1 ],GRUB_FILE_TYPE_TO_HASH |GRUB_FILE_TYPE_NO_DECOMPRESS ); +if (hash_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND , +"unable to open the distrusted certificate/binary hash file." ); -static grub_err_t -grub_cmd_list (grub_command_t cmd __attribute__ ((unused)),int argc __attribute__ ((unused)), -char **args __attribute__ ((unused))) -{ -struct x509_certificate *cert; -int cert_num =1 ; -grub_size_t i; - -for (cert = db.certs; cert !=NULL ; cert = cert->next) + rc =file_read_all (hash_file, &hash_data, &hash_data_size); +grub_file_close (hash_file); +if (rc ==GRUB_ERR_NONE ) { -grub_printf ("Certificate %d:\n" , cert_num); -grub_printf ("\tSerial: " ); +grub_dprintf ("appendedsig" , +"adding a distrusted certificate/binary hash %s\n" +" with size of %" PRIuGRUB_SIZE"\n" , hash_data, hash_data_size); -for (i =0 ; i < cert->serial_len -1 ; i++) -grub_printf ("%02x:" , cert->serial[i]); +if (ctxt->state[OPTION_BINARY_HASH ].set) + { +/* Only accept SHA256, SHA384 and SHA512 binary hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable distrusted binary hash type." ); + } +else if (ctxt->state[OPTION_CERT_HASH ].set) + { +/* Only accept SHA256, SHA384 and SHA512 certificate hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable distrusted certificate hash type." ); + } -grub_printf ("%02x\n" , cert->serial[cert->serial_len -1 ]); -grub_printf ("\tCN: %s\n\n" , cert->subject); - cert_num++; + rc =add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &dbx.signatures, + &dbx.signature_size, &dbx.signature_entries); +grub_free (hash_data); +if (rc !=GRUB_ERR_NONE ) + { +free_db_list (); +free_dbx_list (); +return grub_error (rc,"adding of distrusted binary/certificate hash failed." ); + } } -return GRUB_ERR_NONE ; +return rc; } static grub_err_t
@@ -834,8 +1039,6 @@ static struct grub_fs pseudo_fs = { .fs_read = pseudo_read }; -static grub_command_t cmd_verify, cmd_list, cmd_dbx, cmd_db; -/* Check the certificate hash presence in the dbx list. */ static bool is_dbx_cert_hash (const grub_uint8_t *data,const grub_size_t data_size)
@@ -1102,6 +1305,10 @@ load_static_keys (const struct grub_module_header *header, const bool is_pks)return rc; } +static grub_extcmd_t cmd_dbx_sig; +static grub_command_t cmd_verify, cmd_list_db, cmd_db_cert, cmd_db_sig, + cmd_list_dbx, cmd_dbx_cert; +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -1164,14 +1371,25 @@ GRUB_MOD_INIT (appendedsig)grub_pks_free_keystore (); } - cmd_db =grub_register_command ("append_add_db_cert" , grub_cmd_db,N_ ("X509_CERTIFICATE" ), -N_ ("Add trusted X509_CERTIFICATE to the db" )); - cmd_list =grub_register_command ("append_list_db" , grub_cmd_list,0 , -N_ ("Show the list of trusted X.509 certificates from the db" )); + cmd_db_cert =grub_register_command ("append_add_db_cert" , grub_cmd_db_cert,N_ ("X509_CERTIFICATE" ), +N_ ("Add X509_CERTIFICATE to the db list." )); + cmd_db_sig =grub_register_command ("append_add_db_sig" , grub_cmd_db_sig,N_ ("BINARY HASH FILE" ), +N_ ("Add trusted BINARY HASH to the db list." )); + cmd_dbx_cert =grub_register_command ("append_rm_dbx_cert" , grub_cmd_dbx_cert,N_ ("CERT_NUMBER" ), +N_ ("Remove CERT_NUMBER (as listed by append_list_db) from the db" )); + cmd_dbx_sig =grub_register_extcmd ("append_add_dbx_sig" , grub_cmd_dbx_sig,0 , +N_ ("[-b|--binary-hash] FILE [BINARY HASH FILE]\n" +"[-c|--cert-hash] FILE [CERTFICATE HASH FILE]" ), +N_ ("Add distrusted CERTFICATE/BINARY HASH to the db list." ), options); + cmd_list_db =grub_register_command ("append_list_db" , grub_cmd_list_db,0 , +N_ ("Show the list of trusted x509 certificates and" +" trusted binary hashes from the db list." )); + cmd_list_dbx =grub_register_command ("append_list_dbx" , grub_cmd_list_dbx,0 , +N_ ("Show the list of distrusted certificates and" +" certificate/binary hashes from the dbx list" )); cmd_verify =grub_register_command ("append_verify" , grub_cmd_verify_signature,N_ ("FILE" ), -N_ ("Verify FILE against the trusted X.509 certificates in the db" )); - cmd_dbx =grub_register_command ("append_rm_dbx_cert" , grub_cmd_dbx,N_ ("CERT_NUMBER" ), -N_ ("Remove CERT_NUMBER (as listed by append_list_db) from the db" )); +N_ ("Verify FILE against the trusted x509 certificates/" +"trusted binary hashes." ));grub_verifier_register (&grub_appendedsig_verifier);grub_dl_set_persistent (mod);
@@ -1185,7 +1403,10 @@ GRUB_MOD_FINI (appendedsig)*/ grub_verifier_unregister (&grub_appendedsig_verifier);grub_unregister_command (cmd_verify); -grub_unregister_command (cmd_list); -grub_unregister_command (cmd_db); -grub_unregister_command (cmd_dbx); +grub_unregister_command (cmd_list_db); +grub_unregister_command (cmd_list_dbx); +grub_unregister_command (cmd_db_cert); +grub_unregister_command (cmd_db_sig); +grub_unregister_command (cmd_dbx_cert); +grub_unregister_extcmd (cmd_dbx_sig); }
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 20/21] appendedsig: Introduce GRUB commands to access db and dbx
Quoted 4 lines · Sudhakar Kuppusamy
Introducing the following db and dbx commands
1.
append_list_db:
It will show the list of trusted certificates and binary hashes
s/It will show/Show/
Quoted 2 lines
2.
append_list_dbx:
It will show the list of distrusted certificates and binary/certificate hashes
s/It will show/Show/ and below please...
Quoted 6 lines
3.
append_add_db_cert:
It will add the trusted certificate to the db list
4.append_add_db_sig:
It will add the certificate/binary hash to the db list
5.append_rm_dbx_cert:
It will remove the trusted certificate from the db list
s/db/dbx/
Quoted 18 lines
6.
append_add_dbx_sig:
It will add the certificate/binary hash to the dbx listNote:-
The addition/deletion of trusted certificates and binary hashes
are not allowed in GRUB command prompt while secure boot is enabled.Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/commands/appendedsig/appendedsig.c | 351 +++++++++++++++---- 1 file changed, 286 insertions(+), 65 deletions(-)grub-core/commands/appendedsig/appendedsig.c +0
-0
grub-core/commands/appendedsig/appendedsig.c static enum · line 109
Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +5
-0@@ -102,6 +102,38 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +enum +{ +OPTION_BINARY_HASH =0 , +OPTION_CERT_HASH =1 +};
Please define enum type properly/fully and use it or use plain constants.
grub-core/commands/appendedsig/appendedsig.c static enum · line 119
Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c +9
-0+static const struct grub_arg_option options[] = +{ + {"binary-hash" ,'b' ,0 ,N_ ("hash file of the binary." ),0 ,ARG_TYPE_NONE }, + {"cert-hash" ,'c' ,1 ,N_ ("hash file of the certificate." ),0 ,ARG_TYPE_NONE }, + {0 ,0 ,0 ,0 ,0 ,0 } +}; + +static void +print_hex (const grub_uint8_t *data,const grub_size_t length)
Could you use hexdump function instead?
Quoted 21 lines
grub-core/commands/appendedsig/appendedsig.c +17
-0+{ +grub_size_t i, count =0 ; + +for (i =0 ; i < length-1 ; i++) + { +grub_printf ("%02x:" , data[i]); + count++; +if (count ==16 ) + { +grub_printf ("\n\t " ); + count =0 ; + } + } + +grub_printf ("%02x\n" , data[i]); +} +/* * GUID can be used to determine the hashing function and * generate the hash using determined hashing function.
grub-core/commands/appendedsig/appendedsig.c grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc · line 670
Quoted 7 lines
grub-core/commands/appendedsig/appendedsig.c +2
-2@@ -634,37 +666,203 @@ grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc {grub_file_t signed_file;grub_err_t err; -grub_uint8_t *data; -grub_size_t file_size; +grub_uint8_t *signed_data; +grub_size_t signed_size;
If want better naming please change that in original patch.
Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c +3
-3if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"a signed file is expected.\nExample:\n\tappend_verify <SIGNED FILE>\n" );grub_dprintf ("appendedsig" ,"verifying %s\n" , args[0 ]); signed_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_VERIFY_SIGNATURE );if (signed_file ==NULL ) -return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"could not open %s file." , args[0 ]); +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"could not open %s file." , args[0 ]); - err =file_read_all (signed_file, &data, &file_size);
grub-core/commands/appendedsig/appendedsig.c grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc · line 689
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +5
-2+ err =file_read_all (signed_file, &signed_data, &signed_size);if (err ==GRUB_ERR_NONE ) - err =grub_verify_appended_signature (data, file_size); + err =grub_verify_appended_signature (signed_data, signed_size);grub_file_close (signed_file); -grub_free (data); +grub_free (signed_data); + +return err;
All theses changes belong to original patch...
grub-core/commands/appendedsig/appendedsig.c grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc · line 689
Quoted 1 line
grub-core/commands/appendedsig/appendedsig.c +1
-0+}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 21/21 appendedsig: Documentation
In reply to Sudhakar KuppusamyThis explains how static and dynamic key appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
--- docs/grub.texi | 75 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 17 deletions(-)
docs/grub.texi +58 -17
@@ -6420,9 +6420,12 @@ you forget a command, you can run the command @command{help} * [:: Check file types and compare values * acpi:: Load ACPI tables * append_add_db_cert:: Add an X.509 certificate to the db list -* append_list_db:: List trusted certificates from the db list +* append_add_db_sig:: Add an X.509 certificate/binary hash to the db list +* append_add_dbx_sig:: Add an X.509 certificate/binary hash to the dbx list +* append_list_db:: List trusted certificates/binary hashes from the db list +* append_list_dbx:: List certificates and binary/certificate hashes from the dbx list * append_rm_dbx_cert:: Remove a certificate from the db list -* append_verify:: Verify appended digital signature using db list +* append_verify:: Verify appended digital signature using db and dbx list * authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminal
@@ -6563,16 +6566,48 @@ certificates themselves.) See @xref{Using appended signatures} for more information. @end deffn +@node append_add_db_sig +@subsection append_add_db_sig + +@deffn Command append_add_db_sig hash_file +Read a binary/certificate hash from the file @var{hash_file} +and add it to GRUB's internal db list. These hash are used to validate linux image +integrity if appended signatures validation failed when the environment variable +@code{check_appended_signatures} is set to @code{enforce}. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_add_dbx_sig +@subsection append_add_dbx_sig + +@deffn Command append_add_dbx_sig hash_file +Read a binary/certificate hash from the file @var{hash_file} +and add it to GRUB's internal dbx list. These hash are used to restrict validation +of linux image integrity using db list if appended signatures validation failed +when the environment variable @code{check_appended_signatures} is set to @code{enforce}. + +See @xref{Using appended signatures} for more information. +@end deffn + @node append_list_db @subsection append_list_db @deffn Command append_list_db -List all X.509 certificates trusted by GRUB for validating appended signatures. -The output is a numbered list of certificates, showing the certificate's serial -number and Common Name. +List all X.509 certificates and binary hashes trusted by GRUB for validating +appended signatures. The output is a numbered list of certificates and binary hashes, +showing the certificate's serial number and Common Name. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_dbx +@subsection append_list_dbx -The certificate number can be used as an argument to -@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). +@deffn Command append_list_dbx +List all the distrusted x509 certificates and binary/certificate hashes. +The output is a numbered list of certificates and binary/certificate hashes, +showing the certificate's serial number and Common Name. See @xref{Using appended signatures} for more information. @end deffn
@@ -6597,12 +6632,12 @@ information. @node append_verify @subsection append_verify -@deffn Command append_verify file -Verifies an appended signature on @var{file} against the trusted X.509 certificates -known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and -@pxref{append_rm_dbx_cert}). -Exit code @code{$?} is set to 0 if the signature validates -successfully. If validation fails, it is set to a non-zero value. +@deffn Command append_verify signed_file +Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates +known to GRUB (See @pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert}, +@pxref{append_add_db_sig}, @pxref{append_add_dbx_sig}, and @pxref{append_rm_dbx_cert}). +Exit code @code{$?} is set to 0 if the signature validates successfully. +If validation fails, it is set to a non-zero value. See @xref{Using appended signatures}, for more information. @end deffn
@@ -8925,10 +8960,16 @@ To enable appended signature verification, load the appendedsig module and an x509 certificate for verification. Building the appendedsig module into the core grub image is recommended. -Certificates can be managed at boot time using the @pxref{append_add_db_cert}, -@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. -Certificates can also be built in to the core image using the @code{--x509} -parameter to @command{grub-install} or @command{grub-mkimage}. +For static key secure boot, certificates will be built in to the core image using +the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}. +It allows listing the trusted certificates and binary hashes at boot time using +@pxref{append_list_db} command. + +For dynamic key secure boot, it loads the db and dbx from Platform KeyStore (PKS). +It allows listing the trusted certificates and binary hashes at boot time using +@pxref{append_list_db} and listing distrusted certificates and binary/certificate +hashes at boot time using @pxref{append_list_dbx} commands. + A file can be explicitly verified using the @pxref{append_verify} command. Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported,
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 21/21] appendedsig: Documentation
Quoted 29 lines · Sudhakar Kuppusamy
This explains how static and dynamic key appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- docs/grub.texi | 75 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 17 deletions(-)docs/grub.texi +5
-2@@ -6420,9 +6420,12 @@ you forget a command, you can run the command @command{help} * [:: Check file types and compare values * acpi:: Load ACPI tables * append_add_db_cert:: Add an X.509 certificate to the db list -* append_list_db:: List trusted certificates from the db list +* append_add_db_sig:: Add an X.509 certificate/binary hash to the db list +* append_add_dbx_sig:: Add an X.509 certificate/binary hash to the dbx list +* append_list_db:: List trusted certificates/binary hashes from the db list +* append_list_dbx:: List certificates and binary/certificate hashes from the dbx list * append_rm_dbx_cert:: Remove a certificate from the db list -* append_verify:: Verify appended digital signature using db list +* append_verify:: Verify appended digital signature using db and dbx list * authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminal
docs/grub.texi certificates themselves.) · line 6572
Quoted 7 lines
docs/grub.texi +4
-0@@ -6563,16 +6566,48 @@ certificates themselves.) See @xref{Using appended signatures} for more information. @end deffn +@node append_add_db_sig +@subsection append_add_db_sig + +@deffn Command append_add_db_sig hash_file
s/hash_file/<hash_file>/
docs/grub.texi certificates themselves.) · line 6573
Quoted 1 line
docs/grub.texi +1
-0+Read a binary/certificate hash from the file @var{hash_file}
What is <hash_file> format?
docs/grub.texi certificates themselves.) · line 6584
Quoted 11 lines
docs/grub.texi +11
-0+and add it to GRUB's internal db list. These hash are used to validate linux image +integrity if appended signatures validation failed when the environment variable +@code{check_appended_signatures} is set to @code{enforce}. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_add_dbx_sig +@subsection append_add_dbx_sig + +@deffn Command append_add_dbx_sig hash_file
s/hash_file/<hash_file>/
docs/grub.texi certificates themselves.) · line 6585
Quoted 1 line
docs/grub.texi +1
-0+Read a binary/certificate hash from the file @var{hash_file}
What is <hash_file> format?
Quoted 34 lines
docs/grub.texi +20
-5+and add it to GRUB's internal dbx list. These hash are used to restrict validation +of linux image integrity using db list if appended signatures validation failed +when the environment variable @code{check_appended_signatures} is set to @code{enforce}. + +See @xref{Using appended signatures} for more information. +@end deffn + @node append_list_db @subsection append_list_db @deffn Command append_list_db -List all X.509 certificates trusted by GRUB for validating appended signatures. -The output is a numbered list of certificates, showing the certificate's serial -number and Common Name. +List all X.509 certificates and binary hashes trusted by GRUB for validating +appended signatures. The output is a numbered list of certificates and binary hashes, +showing the certificate's serial number and Common Name. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_dbx +@subsection append_list_dbx -The certificate number can be used as an argument to -@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). +@deffn Command append_list_dbx +List all the distrusted x509 certificates and binary/certificate hashes. +The output is a numbered list of certificates and binary/certificate hashes, +showing the certificate's serial number and Common Name. See @xref{Using appended signatures} for more information. @end deffn
docs/grub.texi information. · line 6635
Quoted 10 lines
docs/grub.texi +1
-6@@ -6597,12 +6632,12 @@ information. @node append_verify @subsection append_verify -@deffn Command append_verify file -Verifies an appended signature on @var{file} against the trusted X.509 certificates -known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and -@pxref{append_rm_dbx_cert}). -Exit code @code{$?} is set to 0 if the signature validates -successfully. If validation fails, it is set to a non-zero value. +@deffn Command append_verify signed_file
s/hash_file/<signed_file>/
docs/grub.texi information. · line 6640
Quoted 5 lines
docs/grub.texi +5
-0+Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates +known to GRUB (See @pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert}, +@pxref{append_add_db_sig}, @pxref{append_add_dbx_sig}, and @pxref{append_rm_dbx_cert}). +Exit code @code{$?} is set to 0 if the signature validates successfully. +If validation fails, it is set to a non-zero value.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel