|
|
80913e |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
80913e |
From: Daniel Axtens <dja@axtens.net>
|
|
|
80913e |
Date: Thu, 30 Jul 2020 01:35:43 +1000
|
|
|
80913e |
Subject: [PATCH] appended signatures: support verifying appended signatures
|
|
|
80913e |
|
|
|
80913e |
Building on the parsers and the ability to embed x509 certificates, as
|
|
|
80913e |
well as the existing gcrypt functionality, add a module for verifying
|
|
|
80913e |
appended signatures.
|
|
|
80913e |
|
|
|
80913e |
This includes:
|
|
|
80913e |
|
|
|
80913e |
- a verifier that requires that kernels and grub modules have appended
|
|
|
80913e |
signatures. It shares lots of logic with shim-lock verifier about what
|
|
|
80913e |
files need to be verified and what modules are unsafe to have loaded.
|
|
|
80913e |
|
|
|
80913e |
- commands to manage the list of trusted certificates for verification.
|
|
|
80913e |
|
|
|
80913e |
Similar to the PGP verifier, if a certificate is embedded in the core
|
|
|
80913e |
image, verification will be enforced unless disabled on the the grub
|
|
|
80913e |
command line or by load_env.
|
|
|
80913e |
|
|
|
80913e |
Thus, as with the PGP verifier, it is not a complete secure-boot solution:
|
|
|
80913e |
other mechanisms must be used to ensure that a user cannot drop to the
|
|
|
80913e |
grub shell and disable verification.
|
|
|
80913e |
|
|
|
80913e |
Signed-off-by: Daniel Axtens <dja@axtens.net>
|
|
|
80913e |
---
|
|
|
80913e |
grub-core/Makefile.core.def | 12 +
|
|
|
80913e |
grub-core/commands/appendedsig/appendedsig.c | 644 +++++++++++++++++++++++++++
|
|
|
80913e |
include/grub/file.h | 2 +
|
|
|
80913e |
3 files changed, 658 insertions(+)
|
|
|
80913e |
create mode 100644 grub-core/commands/appendedsig/appendedsig.c
|
|
|
80913e |
|
|
|
80913e |
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
|
|
|
b32e65 |
index fd1229c63..1cf6b60f8 100644
|
|
|
80913e |
--- a/grub-core/Makefile.core.def
|
|
|
80913e |
+++ b/grub-core/Makefile.core.def
|
|
|
80913e |
@@ -921,6 +921,18 @@ module = {
|
|
|
80913e |
cppflags = '-I$(srcdir)/lib/posix_wrap';
|
|
|
80913e |
};
|
|
|
80913e |
|
|
|
80913e |
+module = {
|
|
|
80913e |
+ name = appendedsig;
|
|
|
80913e |
+ common = commands/appendedsig/appendedsig.c;
|
|
|
80913e |
+ common = commands/appendedsig/x509.c;
|
|
|
80913e |
+ common = commands/appendedsig/pkcs7.c;
|
|
|
80913e |
+ common = commands/appendedsig/asn1util.c;
|
|
|
80913e |
+ common = commands/appendedsig/gnutls_asn1_tab.c;
|
|
|
80913e |
+ common = commands/appendedsig/pkix_asn1_tab.c;
|
|
|
80913e |
+ cflags = '$(CFLAGS_POSIX)';
|
|
|
80913e |
+ cppflags = '-I$(srcdir)/lib/posix_wrap';
|
|
|
80913e |
+};
|
|
|
80913e |
+
|
|
|
80913e |
module = {
|
|
|
80913e |
name = verifiers;
|
|
|
80913e |
common = commands/verifiers.c;
|
|
|
80913e |
diff --git a/grub-core/commands/appendedsig/appendedsig.c b/grub-core/commands/appendedsig/appendedsig.c
|
|
|
80913e |
new file mode 100644
|
|
|
b32e65 |
index 000000000..5d8897be5
|
|
|
80913e |
--- /dev/null
|
|
|
80913e |
+++ b/grub-core/commands/appendedsig/appendedsig.c
|
|
|
80913e |
@@ -0,0 +1,644 @@
|
|
|
80913e |
+/*
|
|
|
80913e |
+ * GRUB -- GRand Unified Bootloader
|
|
|
80913e |
+ * Copyright (C) 2020 IBM Corporation.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * GRUB is free software: you can redistribute it and/or modify
|
|
|
80913e |
+ * it under the terms of the GNU General Public License as published by
|
|
|
80913e |
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
80913e |
+ * (at your option) any later version.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * GRUB is distributed in the hope that it will be useful,
|
|
|
80913e |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
80913e |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
80913e |
+ * GNU General Public License for more details.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * You should have received a copy of the GNU General Public License
|
|
|
80913e |
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
80913e |
+ */
|
|
|
80913e |
+
|
|
|
80913e |
+#include <grub/types.h>
|
|
|
80913e |
+#include <grub/misc.h>
|
|
|
80913e |
+#include <grub/mm.h>
|
|
|
80913e |
+#include <grub/err.h>
|
|
|
80913e |
+#include <grub/dl.h>
|
|
|
80913e |
+#include <grub/file.h>
|
|
|
80913e |
+#include <grub/command.h>
|
|
|
80913e |
+#include <grub/crypto.h>
|
|
|
80913e |
+#include <grub/pkcs1_v15.h>
|
|
|
80913e |
+#include <grub/i18n.h>
|
|
|
80913e |
+#include <grub/gcrypt/gcrypt.h>
|
|
|
80913e |
+#include <grub/kernel.h>
|
|
|
80913e |
+#include <grub/extcmd.h>
|
|
|
80913e |
+#include <grub/verify.h>
|
|
|
80913e |
+#include <grub/libtasn1.h>
|
|
|
80913e |
+#include <grub/env.h>
|
|
|
80913e |
+
|
|
|
80913e |
+#include "appendedsig.h"
|
|
|
80913e |
+
|
|
|
80913e |
+GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
80913e |
+
|
|
|
80913e |
+const char magic[] = "~Module signature appended~\n";
|
|
|
80913e |
+
|
|
|
80913e |
+/*
|
|
|
80913e |
+ * This structure is extracted from scripts/sign-file.c in the linux kernel
|
|
|
80913e |
+ * source. It was licensed as LGPLv2.1+, which is GPLv3+ compatible.
|
|
|
80913e |
+ */
|
|
|
80913e |
+struct module_signature
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_uint8_t algo; /* Public-key crypto algorithm [0] */
|
|
|
80913e |
+ grub_uint8_t hash; /* Digest algorithm [0] */
|
|
|
80913e |
+ grub_uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
|
|
|
80913e |
+ grub_uint8_t signer_len; /* Length of signer's name [0] */
|
|
|
80913e |
+ grub_uint8_t key_id_len; /* Length of key identifier [0] */
|
|
|
80913e |
+ grub_uint8_t __pad[3];
|
|
|
80913e |
+ grub_uint32_t sig_len; /* Length of signature data */
|
|
|
80913e |
+} GRUB_PACKED;
|
|
|
80913e |
+
|
|
|
80913e |
+
|
|
|
80913e |
+/* This represents an entire, parsed, appended signature */
|
|
|
80913e |
+struct grub_appended_signature
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_size_t signature_len; /* Length of PKCS#7 data +
|
|
|
80913e |
+ * metadata + magic */
|
|
|
80913e |
+
|
|
|
80913e |
+ struct module_signature sig_metadata; /* Module signature metadata */
|
|
|
80913e |
+ struct pkcs7_signedData pkcs7; /* Parsed PKCS#7 data */
|
|
|
80913e |
+};
|
|
|
80913e |
+
|
|
|
80913e |
+/* Trusted certificates for verifying appended signatures */
|
|
|
80913e |
+struct x509_certificate *grub_trusted_key;
|
|
|
80913e |
+
|
|
|
80913e |
+/*
|
|
|
80913e |
+ * Force gcry_rsa to be a module dependency.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * If we use grub_crypto_pk_rsa, then then the gcry_rsa module won't be built
|
|
|
80913e |
+ * in if you add 'appendedsig' to grub-install --modules. You would need to
|
|
|
80913e |
+ * add 'gcry_rsa' too. That's confusing and seems suboptimal, especially when
|
|
|
80913e |
+ * we only support RSA.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * Dynamic loading also causes some concerns. We can't load gcry_rsa from the
|
|
|
80913e |
+ * the filesystem after we install the verifier - we won't be able to verify
|
|
|
80913e |
+ * it without having it already present. We also shouldn't load it before we
|
|
|
80913e |
+ * install the verifier, because that would mean it wouldn't be verified - an
|
|
|
80913e |
+ * attacker could insert any code they wanted into the module.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * So instead, reference the internal symbol from gcry_rsa. That creates a
|
|
|
80913e |
+ * direct dependency on gcry_rsa, so it will be built in when this module
|
|
|
80913e |
+ * is built in. Being built in (assuming the core image is itself signed!)
|
|
|
80913e |
+ * also resolves our concerns about loading from the filesystem.
|
|
|
80913e |
+ */
|
|
|
80913e |
+extern gcry_pk_spec_t _gcry_pubkey_spec_rsa;
|
|
|
80913e |
+
|
|
|
80913e |
+static int check_sigs = 0;
|
|
|
80913e |
+
|
|
|
80913e |
+static char *
|
|
|
80913e |
+grub_env_write_sec (struct grub_env_var *var __attribute__((unused)),
|
|
|
80913e |
+ const char *val)
|
|
|
80913e |
+{
|
|
|
80913e |
+ check_sigs = (*val == '1') || (*val == 'e');
|
|
|
80913e |
+ return grub_strdup (check_sigs ? "enforce" : "no");
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+read_cert_from_file (grub_file_t f, struct x509_certificate *certificate)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_err_t err;
|
|
|
80913e |
+ grub_uint8_t *buf = NULL;
|
|
|
80913e |
+ grub_ssize_t read_size;
|
|
|
80913e |
+ grub_off_t total_read_size = 0;
|
|
|
80913e |
+ grub_off_t file_size = grub_file_size (f);
|
|
|
80913e |
+
|
|
|
80913e |
+
|
|
|
80913e |
+ if (file_size == GRUB_FILE_SIZE_UNKNOWN)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
80913e |
+ N_("Cannot parse a certificate file of unknown size"));
|
|
|
80913e |
+
|
|
|
80913e |
+ buf = grub_zalloc (file_size);
|
|
|
80913e |
+ if (!buf)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
|
|
80913e |
+ N_("Could not allocate buffer for certificate file contents"));
|
|
|
80913e |
+
|
|
|
80913e |
+ while (total_read_size < file_size)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ read_size =
|
|
|
80913e |
+ grub_file_read (f, &buf[total_read_size],
|
|
|
80913e |
+ file_size - total_read_size);
|
|
|
80913e |
+ if (read_size < 0)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ err = grub_error (GRUB_ERR_READ_ERROR,
|
|
|
80913e |
+ N_("Error reading certificate file"));
|
|
|
80913e |
+ goto cleanup_buf;
|
|
|
80913e |
+ }
|
|
|
80913e |
+ total_read_size += read_size;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ err = certificate_import (buf, total_read_size, certificate);
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ goto cleanup_buf;
|
|
|
80913e |
+
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+
|
|
|
80913e |
+cleanup_buf:
|
|
|
80913e |
+ grub_free (buf);
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+extract_appended_signature (grub_uint8_t * buf, grub_size_t bufsize,
|
|
|
80913e |
+ struct grub_appended_signature *sig)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_err_t err;
|
|
|
80913e |
+ grub_size_t pkcs7_size;
|
|
|
80913e |
+ grub_size_t remaining_len;
|
|
|
80913e |
+ grub_uint8_t *appsigdata = buf + bufsize - grub_strlen (magic);
|
|
|
80913e |
+
|
|
|
80913e |
+ if (bufsize < grub_strlen (magic))
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("File too short for signature magic"));
|
|
|
80913e |
+
|
|
|
80913e |
+ if (grub_memcmp (appsigdata, (grub_uint8_t *) magic, grub_strlen (magic)))
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("Missing or invalid signature magic"));
|
|
|
80913e |
+
|
|
|
80913e |
+ remaining_len = bufsize - grub_strlen (magic);
|
|
|
80913e |
+
|
|
|
80913e |
+ if (remaining_len < sizeof (struct module_signature))
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("File too short for signature metadata"));
|
|
|
80913e |
+
|
|
|
80913e |
+ appsigdata -= sizeof (struct module_signature);
|
|
|
80913e |
+
|
|
|
80913e |
+ /* extract the metadata */
|
|
|
80913e |
+ grub_memcpy (&(sig->sig_metadata), appsigdata,
|
|
|
80913e |
+ sizeof (struct module_signature));
|
|
|
80913e |
+
|
|
|
80913e |
+ remaining_len -= sizeof (struct module_signature);
|
|
|
80913e |
+
|
|
|
80913e |
+ if (sig->sig_metadata.id_type != 2)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("Wrong signature type"));
|
|
|
80913e |
+
|
|
|
80913e |
+#ifdef GRUB_TARGET_WORDS_BIGENDIAN
|
|
|
80913e |
+ pkcs7_size = sig->sig_metadata.sig_len;
|
|
|
80913e |
+#else
|
|
|
80913e |
+ pkcs7_size = __builtin_bswap32 (sig->sig_metadata.sig_len);
|
|
|
80913e |
+#endif
|
|
|
80913e |
+
|
|
|
80913e |
+ if (pkcs7_size > remaining_len)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("File too short for PKCS#7 message"));
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "sig len %" PRIuGRUB_SIZE "\n", pkcs7_size);
|
|
|
80913e |
+
|
|
|
80913e |
+ sig->signature_len =
|
|
|
80913e |
+ grub_strlen (magic) + sizeof (struct module_signature) + pkcs7_size;
|
|
|
80913e |
+
|
|
|
80913e |
+ /* rewind pointer and parse pkcs7 data */
|
|
|
80913e |
+ appsigdata -= pkcs7_size;
|
|
|
80913e |
+
|
|
|
80913e |
+ err = parse_pkcs7_signedData (appsigdata, pkcs7_size, &sig->pkcs7);
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+grub_verify_appended_signature (grub_uint8_t * buf, grub_size_t bufsize)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_err_t err = GRUB_ERR_NONE;
|
|
|
80913e |
+ grub_size_t datasize;
|
|
|
80913e |
+ void *context;
|
|
|
80913e |
+ unsigned char *hash;
|
|
|
80913e |
+ gcry_mpi_t hashmpi;
|
|
|
80913e |
+ gcry_err_code_t rc;
|
|
|
80913e |
+ struct x509_certificate *pk;
|
|
|
80913e |
+ struct grub_appended_signature sig;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (!grub_trusted_key)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("No trusted keys to verify against"));
|
|
|
80913e |
+
|
|
|
80913e |
+ err = extract_appended_signature (buf, bufsize, &sig);
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+
|
|
|
80913e |
+ datasize = bufsize - sig.signature_len;
|
|
|
80913e |
+
|
|
|
80913e |
+ context = grub_zalloc (sig.pkcs7.hash->contextsize);
|
|
|
80913e |
+ if (!context)
|
|
|
80913e |
+ return grub_errno;
|
|
|
80913e |
+
|
|
|
80913e |
+ sig.pkcs7.hash->init (context);
|
|
|
80913e |
+ sig.pkcs7.hash->write (context, buf, datasize);
|
|
|
80913e |
+ sig.pkcs7.hash->final (context);
|
|
|
80913e |
+ hash = sig.pkcs7.hash->read (context);
|
|
|
80913e |
+ grub_dprintf ("appendedsig",
|
|
|
80913e |
+ "data size %" PRIxGRUB_SIZE ", hash %02x%02x%02x%02x...\n",
|
|
|
80913e |
+ datasize, hash[0], hash[1], hash[2], hash[3]);
|
|
|
80913e |
+
|
|
|
80913e |
+ err = GRUB_ERR_BAD_SIGNATURE;
|
|
|
80913e |
+ for (pk = grub_trusted_key; pk; pk = pk->next)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ rc = grub_crypto_rsa_pad (&hashmpi, hash, sig.pkcs7.hash, pk->mpis[0]);
|
|
|
80913e |
+ if (rc)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ err = grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("Error padding hash for RSA verification: %d"),
|
|
|
80913e |
+ rc);
|
|
|
80913e |
+ goto cleanup;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ rc = _gcry_pubkey_spec_rsa.verify (0, hashmpi, &sig.pkcs7.sig_mpi,
|
|
|
80913e |
+ pk->mpis, NULL, NULL);
|
|
|
80913e |
+ gcry_mpi_release (hashmpi);
|
|
|
80913e |
+
|
|
|
80913e |
+ if (rc == 0)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "verify with key '%s' succeeded\n",
|
|
|
80913e |
+ pk->subject);
|
|
|
80913e |
+ err = GRUB_ERR_NONE;
|
|
|
80913e |
+ break;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "verify with key '%s' failed with %d\n",
|
|
|
80913e |
+ pk->subject, rc);
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ /* If we didn't verify, provide a neat message */
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ err = grub_error (GRUB_ERR_BAD_SIGNATURE,
|
|
|
80913e |
+ N_("Failed to verify signature against a trusted key"));
|
|
|
80913e |
+
|
|
|
80913e |
+cleanup:
|
|
|
80913e |
+ grub_free (context);
|
|
|
80913e |
+ pkcs7_signedData_release (&sig.pkcs7);
|
|
|
80913e |
+
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+grub_cmd_verify_signature (grub_command_t cmd __attribute__((unused)),
|
|
|
80913e |
+ int argc, char **args)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_file_t f;
|
|
|
80913e |
+ grub_err_t err = GRUB_ERR_NONE;
|
|
|
80913e |
+ grub_uint8_t *data;
|
|
|
80913e |
+ grub_ssize_t read_size;
|
|
|
80913e |
+ grub_off_t file_size, total_read_size = 0;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (argc < 1)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "verifying %s\n", args[0]);
|
|
|
80913e |
+
|
|
|
80913e |
+ f = grub_file_open (args[0], GRUB_FILE_TYPE_VERIFY_SIGNATURE);
|
|
|
80913e |
+ if (!f)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ err = grub_errno;
|
|
|
80913e |
+ goto cleanup;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ file_size = grub_file_size (f);
|
|
|
80913e |
+ if (file_size == GRUB_FILE_SIZE_UNKNOWN)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
80913e |
+ N_("Cannot verify the signature of a file of unknown size"));
|
|
|
80913e |
+
|
|
|
80913e |
+ data = grub_malloc (file_size);
|
|
|
80913e |
+ if (!data)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
|
|
80913e |
+ N_("Could not allocate data buffer size "
|
|
|
80913e |
+ PRIuGRUB_UINT64_T " for verification"), file_size);
|
|
|
80913e |
+
|
|
|
80913e |
+ while (total_read_size < file_size)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ read_size =
|
|
|
80913e |
+ grub_file_read (f, &data[total_read_size],
|
|
|
80913e |
+ file_size - total_read_size);
|
|
|
80913e |
+ if (read_size < 0)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ err = grub_error (GRUB_ERR_READ_ERROR,
|
|
|
80913e |
+ N_("Error reading file to verify"));
|
|
|
80913e |
+ goto cleanup_data;
|
|
|
80913e |
+ }
|
|
|
80913e |
+ total_read_size += read_size;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ err = grub_verify_appended_signature (data, file_size);
|
|
|
80913e |
+
|
|
|
80913e |
+cleanup_data:
|
|
|
80913e |
+ grub_free (data);
|
|
|
80913e |
+cleanup:
|
|
|
80913e |
+ if (f)
|
|
|
80913e |
+ grub_file_close (f);
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+grub_cmd_distrust (grub_command_t cmd __attribute__((unused)),
|
|
|
80913e |
+ int argc, char **args)
|
|
|
80913e |
+{
|
|
|
80913e |
+ unsigned long cert_num, i;
|
|
|
80913e |
+ struct x509_certificate *cert, *prev;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (argc != 1)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("One argument expected"));
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_errno = GRUB_ERR_NONE;
|
|
|
80913e |
+ cert_num = grub_strtoul (args[0], NULL, 10);
|
|
|
80913e |
+ if (grub_errno != GRUB_ERR_NONE)
|
|
|
80913e |
+ return grub_errno;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (cert_num < 1)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
80913e |
+ N_("Certificate number too small - numbers start at 1"));
|
|
|
80913e |
+
|
|
|
80913e |
+ if (cert_num == 1)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ cert = grub_trusted_key;
|
|
|
80913e |
+ grub_trusted_key = cert->next;
|
|
|
80913e |
+
|
|
|
80913e |
+ certificate_release (cert);
|
|
|
80913e |
+ grub_free (cert);
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+ }
|
|
|
80913e |
+ i = 2;
|
|
|
80913e |
+ prev = grub_trusted_key;
|
|
|
80913e |
+ cert = grub_trusted_key->next;
|
|
|
80913e |
+ while (cert)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ if (i == cert_num)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ prev->next = cert->next;
|
|
|
80913e |
+ certificate_release (cert);
|
|
|
80913e |
+ grub_free (cert);
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+ }
|
|
|
80913e |
+ i++;
|
|
|
80913e |
+ prev = cert;
|
|
|
80913e |
+ cert = cert->next;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
80913e |
+ N_("No certificate number %d found - only %d certificates in the store"),
|
|
|
80913e |
+ cert_num, i - 1);
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+grub_cmd_trust (grub_command_t cmd __attribute__((unused)),
|
|
|
80913e |
+ int argc, char **args)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_file_t certf;
|
|
|
80913e |
+ struct x509_certificate *cert = NULL;
|
|
|
80913e |
+ grub_err_t err;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (argc != 1)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
|
|
|
80913e |
+
|
|
|
80913e |
+ certf = grub_file_open (args[0],
|
|
|
80913e |
+ GRUB_FILE_TYPE_CERTIFICATE_TRUST
|
|
|
80913e |
+ | GRUB_FILE_TYPE_NO_DECOMPRESS);
|
|
|
80913e |
+ if (!certf)
|
|
|
80913e |
+ return grub_errno;
|
|
|
80913e |
+
|
|
|
80913e |
+
|
|
|
80913e |
+ cert = grub_zalloc (sizeof (struct x509_certificate));
|
|
|
80913e |
+ if (!cert)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
|
|
80913e |
+ N_("Could not allocate memory for certificate"));
|
|
|
80913e |
+
|
|
|
80913e |
+ err = read_cert_from_file (certf, cert);
|
|
|
80913e |
+ grub_file_close (certf);
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_free (cert);
|
|
|
80913e |
+ return err;
|
|
|
80913e |
+ }
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "Loaded certificate with CN: %s\n",
|
|
|
80913e |
+ cert->subject);
|
|
|
80913e |
+
|
|
|
80913e |
+ cert->next = grub_trusted_key;
|
|
|
80913e |
+ grub_trusted_key = cert;
|
|
|
80913e |
+
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+grub_cmd_list (grub_command_t cmd __attribute__((unused)),
|
|
|
80913e |
+ int argc __attribute__((unused)),
|
|
|
80913e |
+ char **args __attribute__((unused)))
|
|
|
80913e |
+{
|
|
|
80913e |
+ struct x509_certificate *cert;
|
|
|
80913e |
+ int cert_num = 1;
|
|
|
80913e |
+ grub_size_t i;
|
|
|
80913e |
+
|
|
|
80913e |
+ for (cert = grub_trusted_key; cert; cert = cert->next)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_printf (N_("Certificate %d:\n"), cert_num);
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_printf (N_("\tSerial: "));
|
|
|
80913e |
+ for (i = 0; i < cert->serial_len - 1; i++)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_printf ("%02x:", cert->serial[i]);
|
|
|
80913e |
+ }
|
|
|
80913e |
+ grub_printf ("%02x\n", cert->serial[cert->serial_len - 1]);
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_printf ("\tCN: %s\n\n", cert->subject);
|
|
|
80913e |
+ cert_num++;
|
|
|
80913e |
+
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+appendedsig_init (grub_file_t io, enum grub_file_type type,
|
|
|
80913e |
+ void **context __attribute__((unused)),
|
|
|
80913e |
+ enum grub_verify_flags *flags)
|
|
|
80913e |
+{
|
|
|
80913e |
+ const char *dangerous_mod;
|
|
|
80913e |
+
|
|
|
80913e |
+ if (!check_sigs)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ switch (type & GRUB_FILE_TYPE_MASK)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ case GRUB_FILE_TYPE_GRUB_MODULE:
|
|
|
80913e |
+ if (grub_is_dangerous_module (io))
|
|
|
80913e |
+ return grub_error (GRUB_ERR_ACCESS_DENIED,
|
|
|
80913e |
+ N_("module cannot be loaded in appended signature mode: %s"),
|
|
|
80913e |
+ io->name);
|
|
|
80913e |
+
|
|
|
80913e |
+ *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+
|
|
|
80913e |
+ case GRUB_FILE_TYPE_ACPI_TABLE:
|
|
|
80913e |
+ case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE:
|
|
|
80913e |
+ *flags = GRUB_VERIFY_FLAGS_DEFER_AUTH;
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+
|
|
|
80913e |
+ case GRUB_FILE_TYPE_CERTIFICATE_TRUST:
|
|
|
80913e |
+ /*
|
|
|
80913e |
+ * This is a certificate to add to trusted keychain.
|
|
|
80913e |
+ *
|
|
|
80913e |
+ * This needs to be verified or blocked. Ideally we'd write an x509
|
|
|
80913e |
+ * verifier, but we lack the hubris required to take this on. Instead,
|
|
|
80913e |
+ * require that it have an appended signature.
|
|
|
80913e |
+ */
|
|
|
80913e |
+
|
|
|
80913e |
+ /* Fall through */
|
|
|
80913e |
+
|
|
|
80913e |
+ case GRUB_FILE_TYPE_LINUX_KERNEL:
|
|
|
80913e |
+ case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
|
|
|
80913e |
+ case GRUB_FILE_TYPE_BSD_KERNEL:
|
|
|
80913e |
+ case GRUB_FILE_TYPE_XNU_KERNEL:
|
|
|
80913e |
+ case GRUB_FILE_TYPE_PLAN9_KERNEL:
|
|
|
80913e |
+
|
|
|
80913e |
+ dangerous_mod = grub_dangerous_module_loaded ();
|
|
|
80913e |
+ if (dangerous_mod)
|
|
|
80913e |
+ return grub_error (GRUB_ERR_ACCESS_DENIED,
|
|
|
80913e |
+ N_("cannot proceed due to dangerous module in memory: %s"),
|
|
|
80913e |
+ dangerous_mod);
|
|
|
80913e |
+
|
|
|
80913e |
+ *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+
|
|
|
80913e |
+ default:
|
|
|
80913e |
+ /*
|
|
|
80913e |
+ * powerpc only supports the linux loader. If you support more,
|
|
|
80913e |
+ * (especially chain loaded binaries) make sure they're checked!
|
|
|
80913e |
+ */
|
|
|
80913e |
+ *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
|
|
|
80913e |
+ return GRUB_ERR_NONE;
|
|
|
80913e |
+ }
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_err_t
|
|
|
80913e |
+appendedsig_write (void *ctxt __attribute__((unused)),
|
|
|
80913e |
+ void *buf, grub_size_t size)
|
|
|
80913e |
+{
|
|
|
80913e |
+ return grub_verify_appended_signature (buf, size);
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+struct grub_file_verifier grub_appendedsig_verifier = {
|
|
|
80913e |
+ .name = "appendedsig",
|
|
|
80913e |
+ .init = appendedsig_init,
|
|
|
80913e |
+ .write = appendedsig_write,
|
|
|
80913e |
+};
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_ssize_t
|
|
|
80913e |
+pseudo_read (struct grub_file *file, char *buf, grub_size_t len)
|
|
|
80913e |
+{
|
|
|
80913e |
+ grub_memcpy (buf, (grub_uint8_t *) file->data + file->offset, len);
|
|
|
80913e |
+ return len;
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+/* Filesystem descriptor. */
|
|
|
80913e |
+static struct grub_fs pseudo_fs = {
|
|
|
80913e |
+ .name = "pseudo",
|
|
|
80913e |
+ .read = pseudo_read
|
|
|
80913e |
+};
|
|
|
80913e |
+
|
|
|
80913e |
+static grub_command_t cmd_verify, cmd_list, cmd_distrust, cmd_trust;
|
|
|
80913e |
+
|
|
|
80913e |
+GRUB_MOD_INIT (appendedsig)
|
|
|
80913e |
+{
|
|
|
80913e |
+ int rc;
|
|
|
80913e |
+ struct grub_module_header *header;
|
|
|
80913e |
+ const char *val;
|
|
|
80913e |
+
|
|
|
80913e |
+ val = grub_env_get ("check_appended_signatures");
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "check_appended_signatures='%s'\n", val);
|
|
|
80913e |
+
|
|
|
80913e |
+ if (val && (val[0] == '1' || val[0] == 'e'))
|
|
|
80913e |
+ check_sigs = 1;
|
|
|
80913e |
+ else
|
|
|
80913e |
+ check_sigs = 0;
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_trusted_key = NULL;
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_register_variable_hook ("check_appended_signatures", 0,
|
|
|
80913e |
+ grub_env_write_sec);
|
|
|
80913e |
+ grub_env_export ("check_appended_signatures");
|
|
|
80913e |
+
|
|
|
80913e |
+ rc = asn1_init ();
|
|
|
80913e |
+ if (rc)
|
|
|
80913e |
+ grub_fatal ("Error initing ASN.1 data structures: %d: %s\n", rc,
|
|
|
80913e |
+ asn1_strerror (rc));
|
|
|
80913e |
+
|
|
|
80913e |
+ FOR_MODULES (header)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ struct grub_file pseudo_file;
|
|
|
80913e |
+ struct x509_certificate *pk = NULL;
|
|
|
80913e |
+ grub_err_t err;
|
|
|
80913e |
+
|
|
|
80913e |
+ /* Not an ELF module, skip. */
|
|
|
80913e |
+ if (header->type != OBJ_TYPE_X509_PUBKEY)
|
|
|
80913e |
+ continue;
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
|
|
|
80913e |
+ pseudo_file.fs = &pseudo_fs;
|
|
|
80913e |
+ pseudo_file.size = header->size - sizeof (struct grub_module_header);
|
|
|
80913e |
+ pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_dprintf ("appendedsig",
|
|
|
80913e |
+ "Found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
|
|
|
80913e |
+ pseudo_file.size);
|
|
|
80913e |
+
|
|
|
80913e |
+ pk = grub_zalloc (sizeof (struct x509_certificate));
|
|
|
80913e |
+ if (!pk)
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_fatal ("Out of memory loading initial certificates");
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ err = read_cert_from_file (&pseudo_file, pk);
|
|
|
80913e |
+ if (err != GRUB_ERR_NONE)
|
|
|
80913e |
+ grub_fatal ("Error loading initial key: %s", grub_errmsg);
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_dprintf ("appendedsig", "loaded certificate CN='%s'\n", pk->subject);
|
|
|
80913e |
+
|
|
|
80913e |
+ pk->next = grub_trusted_key;
|
|
|
80913e |
+ grub_trusted_key = pk;
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ if (!val || val[0] == '\0')
|
|
|
80913e |
+ {
|
|
|
80913e |
+ grub_env_set ("check_appended_signatures",
|
|
|
80913e |
+ grub_trusted_key ? "enforce" : "no");
|
|
|
80913e |
+ }
|
|
|
80913e |
+
|
|
|
80913e |
+ cmd_trust =
|
|
|
80913e |
+ grub_register_command ("trust_certificate", grub_cmd_trust,
|
|
|
80913e |
+ N_("X509_CERTIFICATE"),
|
|
|
80913e |
+ N_("Add X509_CERTIFICATE to trusted certificates."));
|
|
|
80913e |
+ cmd_list =
|
|
|
80913e |
+ grub_register_command ("list_certificates", grub_cmd_list, 0,
|
|
|
80913e |
+ N_("Show the list of trusted x509 certificates."));
|
|
|
80913e |
+ cmd_verify =
|
|
|
80913e |
+ grub_register_command ("verify_appended", grub_cmd_verify_signature,
|
|
|
80913e |
+ N_("FILE"),
|
|
|
80913e |
+ N_("Verify FILE against the trusted x509 certificates."));
|
|
|
80913e |
+ cmd_distrust =
|
|
|
80913e |
+ grub_register_command ("distrust_certificate", grub_cmd_distrust,
|
|
|
80913e |
+ N_("CERT_NUMBER"),
|
|
|
80913e |
+ N_("Remove CERT_NUMBER (as listed by list_certificates) from trusted certificates."));
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_verifier_register (&grub_appendedsig_verifier);
|
|
|
80913e |
+ grub_dl_set_persistent (mod);
|
|
|
80913e |
+}
|
|
|
80913e |
+
|
|
|
80913e |
+GRUB_MOD_FINI (appendedsig)
|
|
|
80913e |
+{
|
|
|
80913e |
+ /*
|
|
|
80913e |
+ * grub_dl_set_persistent should prevent this from actually running, but
|
|
|
80913e |
+ * it does still run under emu.
|
|
|
80913e |
+ */
|
|
|
80913e |
+
|
|
|
80913e |
+ grub_verifier_unregister (&grub_appendedsig_verifier);
|
|
|
80913e |
+ grub_unregister_command (cmd_verify);
|
|
|
80913e |
+ grub_unregister_command (cmd_list);
|
|
|
80913e |
+ grub_unregister_command (cmd_trust);
|
|
|
80913e |
+ grub_unregister_command (cmd_distrust);
|
|
|
80913e |
+}
|
|
|
80913e |
diff --git a/include/grub/file.h b/include/grub/file.h
|
|
|
b32e65 |
index cbbd29465..2e337dbd6 100644
|
|
|
80913e |
--- a/include/grub/file.h
|
|
|
80913e |
+++ b/include/grub/file.h
|
|
|
80913e |
@@ -82,6 +82,8 @@ enum grub_file_type
|
|
|
80913e |
GRUB_FILE_TYPE_PUBLIC_KEY,
|
|
|
80913e |
/* File holding public key to add to trused keys. */
|
|
|
80913e |
GRUB_FILE_TYPE_PUBLIC_KEY_TRUST,
|
|
|
80913e |
+ /* File holding x509 certificiate to add to trusted keys. */
|
|
|
80913e |
+ GRUB_FILE_TYPE_CERTIFICATE_TRUST,
|
|
|
80913e |
/* File of which we intend to print a blocklist to the user. */
|
|
|
80913e |
GRUB_FILE_TYPE_PRINT_BLOCKLIST,
|
|
|
80913e |
/* File we intend to use for test loading or testing speed. */
|