Skip to contentSkip to conversation

MODSIGN: Use PKCS#7 cert to avoid SKIDs

PATCH In review David Howells 2 participants 7 messages Oct 3, 2014 – Oct 20, 2014

Subsystems (4)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

cover MODSIGN: Use PKCS#7 cert to avoid SKIDs

Hi Rusty,

In the current module signing code, we try to use the subject and subjKeyId fields from X.509 certificate representing the key used to sign the modules to locate the X.509 certificate containing the public key required to verify the signature.

Unfortunately, we have situations where we have to deal with signatures generated from keys that don't have a subjKeyId (it is, after all, optional in the X.509 spec for none CA keys).

Now that we have PKCS#7 message handling code in the kernel for kexec(), we can make use of this for module signing. By using a PKCS#7 message with detached data and no embedded X.509 certs as the signature blob, we can forgo specifying all the signature parameters (eg. hash algo, pubkey algo, name, id) elsewhere and rely instead on the PKCS#7 message to supply all of those.

PKCS#7 doesn't use the subjKeyId, but rather matches issuer name and certificate serial number, both of which are mandatory in an X.509 certificate.

We leave out the embedded X.509 certs to make the signature smaller and use detached data so that we don't have to put the module content in there.

The patches are as follows:

(1) Provide a function to pass detached data to the PKCS#7 verifier, rather than always requiring the data to be contained therein.

(2) Provide a utility to sign modules (a drop-in replacement for scripts/sign-file). This does need to be built against -lcrypto from OpenSSL. I couldn't work out how to make a PKCS#7 message with no embedded X.509 certs from the openssl command line.

I also haven't provided a way to externally specify the signature - that's something that will need to be worked out. Quite likely it will involve taking a PKCS#7 message rather than generating one.

(3) Make use of the above and the PKCS#7 handling to sign modules and verify signatures.

Note that this does make signatures generated by previous kernels incompatible with newer kernels, but since the modules being signed may no longer be compatible anyway for other reasons, I'm not sure how much of a problem that will actually be.

I have provided a function, mod_verify_pkcs7(), that takes a buffer containing the actual module data, sans signature, and a buffer containing the PKCS#7 message that does the actual work. This could be called, for instance, if modules are ever loaded with detached signatures.

The patches can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

This is based on James Morris's security/next branch as there are some keyring and PKCS#7 changes in there that are prerequisites for this.

David
---
David Howells (3):
PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/pkcs7_verify.c |   26 ++
 include/crypto/pkcs7.h                |    3 
 include/crypto/public_key.h           |    1 
 init/Kconfig                          |    1 
 kernel/module_signing.c               |  220 +++--------------
 scripts/Makefile                      |    2 
 scripts/sign-file                     |  421 ---------------------------------
 scripts/sign-file.c                   |  189 +++++++++++++++
 8 files changed, 266 insertions(+), 597 deletions(-)
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c
author maintainer
#1 lore raw

PATCH 1/3 PKCS#7: Allow detached data to be supplied for signature checking purposes

It is possible for a PKCS#7 message to have detached data. However, to verify the signatures on a PKCS#7 message, we have to be able to digest the data. Provide a function to supply that data. An error is given if the PKCS#7 message included embedded data.

---

 crypto/asymmetric_keys/pkcs7_verify.c |   26 ++++++++++++++++++++++++++
 include/crypto/pkcs7.h                |    3 +++
 2 files changed, 29 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +26 -0
index cd455450b069..c210f6f2334f 100644
@@ -359,3 +359,29 @@ int pkcs7_verify(struct pkcs7_message *pkcs7)
 	return enopkg;
 }
 EXPORT_SYMBOL_GPL(pkcs7_verify);
+
+/**
+ * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
+ * @pkcs7: The PKCS#7 message
+ * @data: The data to be verified
+ * @datalen: The amount of data
+ *
+ * Supply the detached data needed to verify a PKCS#7 message.  Note that no
+ * attempt to retain/pin the data is made.  That is left to the caller.  The
+ * data will not be modified by pkcs7_verify() and will not be freed when the
+ * PKCS#7 message is freed.
+ *
+ * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
+ */
+int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+			       const void *data, size_t datalen)
+{
+	if (pkcs7->data) {
+		pr_debug("Data already supplied\n");
+		return -EINVAL;
+	}
+	pkcs7->data = data;
+	pkcs7->data_len = datalen;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pkcs7_supply_detached_data);
include/crypto/pkcs7.h +3 -0
index 691c79172a26..e235ab4957ee 100644
@@ -34,3 +34,6 @@ extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
  * pkcs7_verify.c
  */
 extern int pkcs7_verify(struct pkcs7_message *pkcs7);
+
+extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+				      const void *data, size_t datalen);
author maintainer
#2 lore raw

PATCH 2/3 MODSIGN: Provide a utility to append a PKCS#7 signature to a module

In reply to David Howells

Provide a utility that:

(1) Digests a module using the specified hash algorithm (typically sha256).

[The digest can be dumped into a file by passing the '-d' flag]

(2) Generates a PKCS#7 message that:

(a) Has detached data (ie. the module content).

(b) Is signed with the specified private key.

(c) Refers to the specified X.509 certificate.

(d) Has an empty X.509 certificate list.

[The PKCS#7 message can be dumped into a file by passing the '-p' flag]

(3) Generates a signed module by concatenating the old module, the PKCS#7 message, a descriptor and a magic string. The descriptor contains the size of the PKCS#7 message and indicates the id_type as PKEY_ID_PKCS7.

(4) Either writes the signed module to the specified destination or renames it over the source module.

This allows module signing to reuse the PKCS#7 handling code that was added for PE file parsing for signed kexec.

Note that the utility is written in C and must be linked against the OpenSSL crypto library.

Note further that I have temporarily dropped support for handling externally created signatures until we can work out the best way to do those. Hopefully, whoever creates the signature can give me a PKCS#7 certificate.

---

 include/crypto/public_key.h |    1 
 scripts/sign-file.c         |  189 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100755 scripts/sign-file.c
include/crypto/public_key.h +1 -0
index fa73a6fd536c..a2da37710621 100644
@@ -33,6 +33,7 @@ extern const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST];
 enum pkey_id_type {
 	PKEY_ID_PGP,		/* OpenPGP generated key ID */
 	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
+	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
 	PKEY_ID_TYPE__LAST
 };
 
scripts/sign-file.c new file +189 -0

189 lines not on this page · raw message

author maintainer
#3 lore raw

PATCH 3/3 MODSIGN: Use PKCS#7 messages as module signatures

In reply to David Howells

Move to using PKCS#7 messages as module signatures because:

(1) We have to be able to support the use of X.509 certificates that don't have a subjKeyId set. We're currently relying on this to look up the X.509 certificate in the trusted keyring list.

(2) PKCS#7 message signed information blocks have a field that supplies the data required to match with the X.509 certificate that signed it.

(3) The PKCS#7 certificate carries fields that specify the digest algorithm used to generate the signature in a standardised way and the X.509 certificates specify the public key algorithm in a standardised way - so we don't need our own methods of specifying these.

(4) We now have PKCS#7 message support in the kernel for signed kexec purposes and we can make use of this.

To make this work, the old sign-file script has been replaced with a program that needs compiling in a previous patch. The rules to build it are added here.

---

 init/Kconfig            |    1 
 kernel/module_signing.c |  220 +++++--------------------
 scripts/Makefile        |    2 
 scripts/sign-file       |  421 -----------------------------------------------
 4 files changed, 47 insertions(+), 597 deletions(-)
 delete mode 100755 scripts/sign-file
init/Kconfig +1 -0
index 9d76b99af1b9..8bd1dd8c402d 100644
@@ -1794,6 +1794,7 @@ config MODULE_SIG
 	select ASN1
 	select OID_REGISTRY
 	select X509_CERTIFICATE_PARSER
+	select PKCS7_MESSAGE_PARSER
 	help
 	  Check modules for valid signatures upon load: the signature
 	  is simply appended to the module. For more information see
kernel/module_signing.c +44 -176

269 lines not on this page · raw message

scripts/Makefile +2 -0
index 890df5c6adfb..c5479a4535a9 100644
@@ -17,9 +17,11 @@ hostprogs-$(CONFIG_IKCONFIG)     += bin2c
 hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
 hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
 hostprogs-$(CONFIG_ASN1)	 += asn1_compiler
+hostprogs-$(CONFIG_MODULE_SIG)	 += sign-file
 
 HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
 HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
+HOSTCFLAGS_sign-file.o = -lcrypto
 
 always		:= $(hostprogs-y) $(hostprogs-m)
 
scripts/sign-file deleted +0 -421

421 lines not on this page · raw message

#4 lore raw
In reply to David Howells

Hi David,

I just applied these 3 patches, but got build problems.. sign-file.c cannot be build.
Log attached...

Any ideas?

Thanks

- Dmitry

Quoted 77 lines · David Howells

Hi Rusty,

In the current module signing code, we try to use the subject and subjKeyId fields from X.509 certificate representing the key used to sign the modules to locate the X.509 certificate containing the public key required to verify the signature.

Unfortunately, we have situations where we have to deal with signatures generated from keys that don't have a subjKeyId (it is, after all, optional in the X.509 spec for none CA keys).

Now that we have PKCS#7 message handling code in the kernel for kexec(), we can make use of this for module signing. By using a PKCS#7 message with detached data and no embedded X.509 certs as the signature blob, we can forgo specifying all the signature parameters (eg. hash algo, pubkey algo, name, id) elsewhere and rely instead on the PKCS#7 message to supply all of those.

PKCS#7 doesn't use the subjKeyId, but rather matches issuer name and certificate serial number, both of which are mandatory in an X.509 certificate.

We leave out the embedded X.509 certs to make the signature smaller and use detached data so that we don't have to put the module content in there.

The patches are as follows:

(1) Provide a function to pass detached data to the PKCS#7 verifier, rather than always requiring the data to be contained therein.

(2) Provide a utility to sign modules (a drop-in replacement for scripts/sign-file). This does need to be built against -lcrypto from OpenSSL. I couldn't work out how to make a PKCS#7 message with no embedded X.509 certs from the openssl command line.

I also haven't provided a way to externally specify the signature - that's something that will need to be worked out. Quite likely it will involve taking a PKCS#7 message rather than generating one.

(3) Make use of the above and the PKCS#7 handling to sign modules and verify signatures.

Note that this does make signatures generated by previous kernels incompatible with newer kernels, but since the modules being signed may no longer be compatible anyway for other reasons, I'm not sure how much of a problem that will actually be.

I have provided a function, mod_verify_pkcs7(), that takes a buffer containing the actual module data, sans signature, and a buffer containing the PKCS#7 message that does the actual work. This could be called, for instance, if modules are ever loaded with detached signatures.

The patches can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=modsign-pkcs7

This is based on James Morris's security/next branch as there are some keyring and PKCS#7 changes in there that are prerequisites for this.

David
---
David Howells (3):
PKCS#7: Allow detached data to be supplied for signature checking purposes
MODSIGN: Provide a utility to append a PKCS#7 signature to a module
MODSIGN: Use PKCS#7 messages as module signatures

 crypto/asymmetric_keys/pkcs7_verify.c |   26 ++
 include/crypto/pkcs7.h                |    3 
 include/crypto/public_key.h           |    1 
 init/Kconfig                          |    1 
 kernel/module_signing.c               |  220 +++--------------
 scripts/Makefile                      |    2 
 scripts/sign-file                     |  421 ---------------------------------
 scripts/sign-file.c                   |  189 +++++++++++++++
 8 files changed, 266 insertions(+), 597 deletions(-)
 delete mode 100755 scripts/sign-file
 create mode 100755 scripts/sign-file.c
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

EXTRAVERSION=-kds
ARCH: x86_64

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh

<stdin>:1226:2: warning: #warning syscall finit_module not implemented [-Wcpp] <stdin>:1229:2: warning: #warning syscall sched_setattr not implemented [-Wcpp] <stdin>:1232:2: warning: #warning syscall sched_getattr not implemented [-Wcpp] <stdin>:1235:2: warning: #warning syscall renameat2 not implemented [-Wcpp] <stdin>:1238:2: warning: #warning syscall seccomp not implemented [-Wcpp]
HOSTCC scripts/sign-file
scripts/sign-file.c: In function �쁬ain��:
scripts/sign-file.c:136:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(!bd, dest_name);
^
scripts/sign-file.c:162:3: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(!b, pkcs7_name);
^
scripts/sign-file.c:163:3: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(i2d_PKCS7_bio_stream(b, pkcs7, NULL, 0) < 0, pkcs7_name);
^
scripts/sign-file.c:168:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(BIO_reset(bm) < 0, module_name);
^
scripts/sign-file.c:171:3: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(BIO_write(bd, buf, n) < 0, dest_name);
^
scripts/sign-file.c:173:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(n < 0, module_name);
^
scripts/sign-file.c:176:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(i2d_PKCS7_bio_stream(bd, pkcs7, NULL, 0) < 0, dest_name);
^
scripts/sign-file.c:179:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, dest_name);
^
scripts/sign-file.c:180:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, dest_name);
^
scripts/sign-file.c:182:2: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(BIO_free(bd) < 0, dest_name);
^
scripts/sign-file.c:186:3: warning: format not a string literal and no format arguments [-Wformat-security]
ERR(rename(dest_name, module_name) < 0, dest_name);
^
/tmp/ccgSFKJd.o: In function `display_openssl_errors':
sign-file.c:(.text+0x4e): undefined reference to ERR_peek_error' sign-file.c:(.text+0xa1): undefined reference to ERR_error_string' sign-file.c:(.text+0xd7): undefined reference to `ERR_get_error_line'
/tmp/ccgSFKJd.o: In function `main':
sign-file.c:(.text.startup+0xd5): undefined reference to ERR_load_crypto_strings' sign-file.c:(.text.startup+0xda): undefined reference to ERR_clear_error' sign-file.c:(.text.startup+0xe7): undefined reference to BIO_new_file' sign-file.c:(.text.startup+0x10b): undefined reference to PEM_read_bio_PrivateKey' sign-file.c:(.text.startup+0x118): undefined reference to BIO_free' sign-file.c:(.text.startup+0x125): undefined reference to BIO_new_file' sign-file.c:(.text.startup+0x149): undefined reference to PEM_read_bio_X509' sign-file.c:(.text.startup+0x156): undefined reference to BIO_free' sign-file.c:(.text.startup+0x165): undefined reference to BIO_new_file' sign-file.c:(.text.startup+0x180): undefined reference to OpenSSL_add_all_digests' sign-file.c:(.text.startup+0x192): undefined reference to EVP_get_digestbyname' sign-file.c:(.text.startup+0x1b5): undefined reference to BIO_new_file' sign-file.c:(.text.startup+0x1de): undefined reference to PKCS7_sign' sign-file.c:(.text.startup+0x20f): undefined reference to PKCS7_sign_add_signer' sign-file.c:(.text.startup+0x229): undefined reference to PKCS7_final' sign-file.c:(.text.startup+0x286): undefined reference to BIO_new_file' sign-file.c:(.text.startup+0x2ab): undefined reference to i2d_PKCS7_bio_stream' sign-file.c:(.text.startup+0x2c9): undefined reference to BIO_free' sign-file.c:(.text.startup+0x2da): undefined reference to BIO_ctrl' sign-file.c:(.text.startup+0x303): undefined reference to BIO_write' sign-file.c:(.text.startup+0x32b): undefined reference to BIO_read' sign-file.c:(.text.startup+0x351): undefined reference to BIO_number_written' sign-file.c:(.text.startup+0x363): undefined reference to i2d_PKCS7_bio_stream' sign-file.c:(.text.startup+0x381): undefined reference to BIO_number_written' sign-file.c:(.text.startup+0x39e): undefined reference to BIO_write' sign-file.c:(.text.startup+0x3c4): undefined reference to BIO_write' sign-file.c:(.text.startup+0x3dc): undefined reference to `BIO_free'
collect2: error: ld returned 1 exit status
make[1]: *** [scripts/sign-file] Error 1
make: *** [scripts] Error 2

author maintainer
#5 lore raw
Quoted 4 lines · Dmitry Kasatkin

/tmp/ccgSFKJd.o: In function `display_openssl_errors':
sign-file.c:(.text+0x4e): undefined reference to ERR_peek_error' sign-file.c:(.text+0xa1): undefined reference to ERR_error_string'
...

Is this solved for you by making the attached change?

David
---

scripts/Makefile +1 -1
index 1d9c40be606e..719311b7bd46 100644
@@ -20,7 +20,7 @@ hostprogs-$(CONFIG_MODULE_SIG)	 += sign-file
 
 HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
 HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
-HOSTCFLAGS_sign-file.o = -lcrypto
+HOSTLOADLIBES_sign-file = -lcrypto
#6 lore raw
Quoted 16 lines · David Howells
Quoted 4 lines · Dmitry Kasatkin

/tmp/ccgSFKJd.o: In function `display_openssl_errors':
sign-file.c:(.text+0x4e): undefined reference to ERR_peek_error' sign-file.c:(.text+0xa1): undefined reference to ERR_error_string'
...

Is this solved for you by making the attached change?

David
---

scripts/Makefile +0 -0
index 1d9c40be606e..719311b7bd46 100644

scripts/Makefile hostprogs-$(CONFIG_MODULE_SIG) += sign-file · line 24

Quoted 6 lines
scripts/Makefile +1 -1
@@ -20,7 +20,7 @@ hostprogs-$(CONFIG_MODULE_SIG)	 += sign-file
 
 HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
 HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
-HOSTCFLAGS_sign-file.o = -lcrypto
+HOSTLOADLIBES_sign-file = -lcrypto
 

Hi,

Yes. It helps...

- Dmitry