Skip to contentSkip to conversation

MODSIGN: Use filechk to generate .x509.list

PATCH In review Michal Marek 4 participants 10 messages May 20, 2015 – May 28, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/3 X.509: Fix certificate gathering again

Commit d7ec435f (X.509: Fix certificate gathering) fixed the issue of changing .x509.list, but in the CONFIG_MODULE_SIG=y case, it assumed that $(objtree) was an absolute path. Commit 7e1c0477 (kbuild: Use relative path for $(objtree)) broke this assumption and we get uncecessary rebuilds again, due to ./signing_key.x509 changing to signing_key.x509. Instead of prepending the absolute path and stripping it again, just check if $(srctree) and $(objtree) are different directories and add $(srctree)/*.x509 only in such case.

However, such fix causes a different issue, namely that a potential $(srctree)/signing_key.x509 appears before the to-be-generated signing_key.x509 in the sorted list, and make remembers this file for the purpose of VPATH search. It worked previously, because ./signing_key.x509 sorts before any absolute path. Solve this by sorting the $(objtree) and $(srctree) files separately, to ensure that signing_key.x509 appears before anything from $(srctree).

---
 kernel/Makefile | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
kernel/Makefile +9 -8
index 60c302c..dc78819 100644
@@ -119,17 +119,18 @@ $(obj)/config_data.h: $(obj)/config_data.gz FORCE
 # boot.
 #
 # We look in the source root and the build root for all files whose name ends
-# in ".x509".  Unfortunately, this will generate duplicate filenames, so we
-# have make canonicalise the pathnames and then sort them to discard the
-# duplicates.
+# in ".x509". We put the $(objtree) certificates before those from $(srctree),
+# so that a potential signing_key.x509 file from $(srctree) does not take
+# precedence.
 #
 ###############################################################################
 ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
-X509_CERTIFICATES-y := $(wildcard *.x509) $(wildcard $(srctree)/*.x509)
-X509_CERTIFICATES-$(CONFIG_MODULE_SIG) += $(objtree)/signing_key.x509
-X509_CERTIFICATES-raw := $(sort $(foreach CERT,$(X509_CERTIFICATES-y), \
-				$(or $(realpath $(CERT)),$(CERT))))
-X509_CERTIFICATES := $(subst $(realpath $(objtree))/,,$(X509_CERTIFICATES-raw))
+X509_CERTIFICATES-y := $(wildcard *.x509)
+X509_CERTIFICATES-$(CONFIG_MODULE_SIG) += signing_key.x509
+X509_CERTIFICATES := $(sort $(X509_CERTIFICATES-y))
+ifneq ($(objtree),$(srctree))
+X509_CERTIFICATES += $(sort $(wildcard $(srctree)/*.x509))
+endif
 
 ifeq ($(X509_CERTIFICATES),)
 $(warning *** No X.509 certificates found ***)
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/3 MODSIGN: Use filechk to generate .x509.list

From: Linus Torvalds <[email protected]>

Use the filechk Kbuild function instead of manually comparing the content of the generated file.

---
 kernel/Makefile | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/Makefile +5 -9
index dc78819..3177160 100644
@@ -136,13 +136,6 @@ ifeq ($(X509_CERTIFICATES),)
 $(warning *** No X.509 certificates found ***)
 endif
 
-ifneq ($(wildcard $(obj)/.x509.list),)
-ifneq ($(shell cat $(obj)/.x509.list),$(X509_CERTIFICATES))
-$(info X.509 certificate list changed)
-$(shell rm $(obj)/.x509.list)
-endif
-endif
-
 kernel/system_certificates.o: $(obj)/x509_certificate_list
 
 quiet_cmd_x509certs  = CERTS   $@
@@ -152,9 +145,12 @@ targets += $(obj)/x509_certificate_list
 $(obj)/x509_certificate_list: $(X509_CERTIFICATES) $(obj)/.x509.list
 	$(call if_changed,x509certs)
 
+define filechk_x509_list
+	echo $(X509_CERTIFICATES)
+endef
 targets += $(obj)/.x509.list
-$(obj)/.x509.list:
-	@echo $(X509_CERTIFICATES) >$@
+$(obj)/.x509.list: Makefile FORCE
+	$(call filechk,x509_list)
 endif
 
 clean-files := x509_certificate_list .x509.list
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/3 MODSIGN: Split user-supplied and autogenerated signing key

In reply to Michal Marek

Allow the users to place signing_key.{x509,priv} and x509.genkey in the source tree. If any of these files is missing, generate the file in the build tree with an .auto suffix. This avoids problems with overwriting user-supplied files.

---
 .gitignore                       |  2 ++
 Documentation/module-signing.txt | 10 ++++++---
 Makefile                         |  5 +++--
 kernel/Makefile                  | 46 ++++++++++++++++++++--------------------
 4 files changed, 35 insertions(+), 28 deletions(-)
.gitignore +2 -0
index 4ad4a98..92b9bc8 100644
@@ -100,6 +100,8 @@ extra_certificates
 signing_key.priv
 signing_key.x509
 x509.genkey
+signing_key.*.auto
+x509.genkey.auto
 
 # Kconfig presets
 all.config
Documentation/module-signing.txt +7 -3
index c72702e..74121fb 100644
@@ -100,18 +100,22 @@ it can be deleted or stored securely.  The public key gets built into the
 kernel so that it can be used to check the signatures as the modules are
 loaded.
 
-Under normal conditions, the kernel build will automatically generate a new
-keypair using openssl if one does not exist in the files:
+The kernel expects the keypair to be stored in the files:
 
 	signing_key.priv
 	signing_key.x509
 
+If the keypair is not supplied, the kernel will generate it as:
+
+	signing_key.priv.auto
+	signing_key.x509.auto
+
 during the building of vmlinux (the public part of the key needs to be built
 into vmlinux) using parameters in the:
 
 	x509.genkey
 
-file (which is also generated if it does not already exist).
+file (which, if not supplied, is also generated as x509.genkey.auto).
 
 It is strongly recommended that you provide your own x509.genkey file.
 
Makefile +3 -2
index 2da553f..f46e76a 100644
@@ -872,8 +872,8 @@ INITRD_COMPRESS-$(CONFIG_RD_LZ4)   := lz4
 # export INITRD_COMPRESS := $(INITRD_COMPRESS-y)
 
 ifdef CONFIG_MODULE_SIG_ALL
-MODSECKEY = ./signing_key.priv
-MODPUBKEY = ./signing_key.x509
+MODSECKEY = $(firstword $(wildcard $(srctree)/signing_key.priv) ./signing_key.priv.auto)
+MODPUBKEY = $(firstword $(wildcard $(srctree)/signing_key.x509) ./signing_key.x509.auto)
 export MODPUBKEY
 mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)
 else
@@ -1176,6 +1176,7 @@ MRPROPER_DIRS  += include/config usr/include include/generated          \
 MRPROPER_FILES += .config .config.old .version .old_version \
 		  Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \
 		  signing_key.priv signing_key.x509 x509.genkey		\
+		  signing_key.*.auto x509.genkey.auto			\
 		  extra_certificates signing_key.x509.keyid		\
 		  signing_key.x509.signer vmlinux-gdb.py
 
kernel/Makefile +23 -23

67 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

In reply to Michal Marek

Hi Michal,

Could you have a look at the patch at the end of my branch:

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

It changes things from picking up arbitrary *.x509 files dropped in the kernel source and/or build directory to taking a single named PEM file with all the additional certs as a string config option. The PEM file can contain multiple certs simply cat'd together.

If you're okay with that, it obsoletes these patches of yours.

I've attached it here for convenience also.

David
---
commit 9c71c950793b1b8c23c6d945b31f6545f82adced

Author: David Woodhouse <[email protected]>
Date:   Thu May 21 12:23:55 2015 +0100

modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS option

Let the user explicitly provide a file containing trusted keys, instead of just automatically finding files matching *.x509 in the build tree and trusting whatever we find. This really ought to be an *explicit* configuration, and the build rules for dealing with the files were fairly painful too.

Documentation/module-signing.txt +11 -4
index 5d5e4e32dc26..4e62bc29666e 100644
@@ -88,6 +88,7 @@ This has a number of options available:
      than being a module) so that modules signed with that algorithm can have
      their signatures checked without causing a dependency loop.
 
+
  (4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY)
 
      Setting this option to something other than its default of
@@ -104,6 +105,13 @@ This has a number of options available:
      means of the KBUILD_SIGN_PIN variable.
 
 
+ (5) "Additional X.509 keys for default system keyring" (CONFIG_SYSTEM_TRUSTED_KEYS)
+
+     This option can be set to the filename of a PEM-encoded file containing
+     additional certificates which will be included in the system keyring by
+     default.
+
+
 =======================
 GENERATING SIGNING KEYS
 =======================
@@ -171,10 +179,9 @@ in a keyring called ".system_keyring" that can be seen by:
 	302d2d52 I------     1 perm 1f010000     0     0 asymmetri Fedora kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA a7118079 []
 	...
 
-Beyond the public key generated specifically for module signing, any file
-placed in the kernel source root directory or the kernel build root directory
-whose name is suffixed with ".x509" will be assumed to be an X.509 public key
-and will be added to the keyring.
+Beyond the public key generated specifically for module signing, additional
+trusted certificates can be provided in a PEM-encoded file referenced by the
+CONFIG_SYSTEM_TRUSTED_KEYS configuration option.
 
 Further, the architecture code may take public keys from a hardware store and
 add those in also (e.g. from the UEFI key database).
init/Kconfig +13 -0
index a52935338419..b46c19525074 100644
@@ -1758,6 +1758,19 @@ config SYSTEM_TRUSTED_KEYRING
 
 	  Keys in this keyring are used by module signature checking.
 
+config SYSTEM_TRUSTED_KEYS
+	string "Additional X.509 keys for default system keyring"
+	depends on SYSTEM_TRUSTED_KEYRING
+	help
+	  If set, this option should be the filename of a PEM-formatted file
+	  containing trusted X.509 certificates to be included in the default
+	  system keyring. Any certificate used for module signing is implicitly
+	  also trusted.
+
+	  NOTE: If you previously provided keys for the system keyring in the
+	  form of DER-encoded *.x509 files in the top-level build directory,
+	  those are no longer used. You will need to set this option instead.
+
 config SYSTEM_DATA_VERIFICATION
 	def_bool n
 	select SYSTEM_TRUSTED_KEYRING
kernel/Makefile +65 -60

151 lines not on this page · raw message

#5 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

Quoted 81 lines · David Howells

Hi Michal,

Could you have a look at the patch at the end of my branch:

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

It changes things from picking up arbitrary *.x509 files dropped in the kernel
source and/or build directory to taking a single named PEM file with all the
additional certs as a string config option. The PEM file can contain multiple
certs simply cat'd together.

If you're okay with that, it obsoletes these patches of yours.

I've attached it here for convenience also.

David
---
commit 9c71c950793b1b8c23c6d945b31f6545f82adced

Author: David Woodhouse <[email protected]>
Date:   Thu May 21 12:23:55 2015 +0100

modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS option

Let the user explicitly provide a file containing trusted keys, instead of
just automatically finding files matching *.x509 in the build tree and
trusting whatever we find. This really ought to be an *explicit* configuration, and the build rules for dealing with the files were fairly painful too.

Documentation/module-signing.txt +0 -0
index 5d5e4e32dc26..4e62bc29666e 100644
@@ -88,6 +88,7 @@ This has a number of options available:

than being a module) so that modules signed with that algorithm can have
their signatures checked without causing a dependency loop.

+
(4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY)

Setting this option to something other than its default of

Documentation/module-signing.txt +3 -0
@@ -104,6 +105,13 @@ This has a number of options available:
     means of the KBUILD_SIGN_PIN variable.
 
 
+ (5) "Additional X.509 keys for default system keyring"
 (CONFIG_SYSTEM_TRUSTED_KEYS)
+
+     This option can be set to the filename of a PEM-encoded file

containing
+ additional certificates which will be included in the system
keyring by

Documentation/module-signing.txt +3 -0
+     default.
+
+

=======================
GENERATING SIGNING KEYS
=======================

Documentation/module-signing.txt +0 -0
@@ -171,10 +179,9 @@ in a keyring called ".system_keyring" that can be

seen by:

	302d2d52 I------     1 perm 1f010000     0     0 asymmetri Fedora
kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA
a7118079 []
	...

-Beyond the public key generated specifically for module signing, any file
-placed in the kernel source root directory or the kernel build root directory
-whose name is suffixed with ".x509" will be assumed to be an X.509 public key

Documentation/module-signing.txt

Quoted 2 lines
Documentation/module-signing.txt +1 -1
-and will be added to the keyring.
+Beyond the public key generated specifically for module signing,

I think this should be "private", not "public" key. The modules are signed with the private key...

Petko

#6 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

Quoted 4 lines · Petko Manolov
Quoted 1 line

+Beyond the public key generated specifically for module signing,

I think this should be "private", not "public" key. The modules are signed with the private key...

No. Private keys aren't added to the keyring. The title of the section is "Public keys in the kernel".

David

#7 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

In reply to David Howells

Dne 27.5.2015 v 00:15 David Howells napsal(a):

Quoted 4 lines

Hi Michal,

Could you have a look at the patch at the end of my branch:

Quoted 8 lines

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

It changes things from picking up arbitrary *.x509 files dropped in the kernel source and/or build directory to taking a single named PEM file with all the additional certs as a string config option. The PEM file can contain multiple certs simply cat'd together.

If you're okay with that, it obsoletes these patches of yours.

I'm fine with the concept as it indeed solves the problem with the wildcard matching for good. So please drop the patches that I posted.

The only issue is that the makefile expressions are a bit hairy. For starters, we already have definitions for $(quote) and $(space) in kbuild. I'll have a closer look at the config_filename macro and try to simplify it somehow. But it's just cosmetics, the patch can be merged as is for now.

Michal

#8 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

Quoted 6 lines · Michal Marek

The only issue is that the makefile expressions are a bit hairy. For starters, we already have definitions for $(quote) and $(space) in kbuild. I'll have a closer look at the config_filename macro and try to simplify it somehow. But it's just cosmetics, the patch can be merged as is for now.

I've actually changed my mind about the 'awk | base64 -d' bit. The error handling is too poor. I'd like to do that in C with a variant of the existing extract_cert tool, and make sure we have proper X.509 certificates and error handling/reporting.

I'll fix that, but probably not this week while I'm ostensibly on honeymoon and not answering emails like this one...

(If anyone else wants to beat me to it, you probably just need to make extract_cert.c loop on extracting the certs from the file/BIO, but do make sure it tolerates only *one* in the existing 'find signer cert' mode.)

Signature · 2 lines
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
#9 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

Quoted 10 lines · David Woodhouse
Quoted 5 lines

The only issue is that the makefile expressions are a bit hairy. For starters, we already have definitions for $(quote) and $(space) in kbuild. I'll have a closer look at the config_filename macro and try to simplify it somehow. But it's just cosmetics, the patch can be merged as is for now.

I've actually changed my mind about the 'awk | base64 -d' bit. The error handling is too poor. I'd like to do that in C with a variant of the existing extract_cert tool, and make sure we have proper X.509 certificates and error handling/reporting.

You could also do it in perl pretty easily.

David

#10 lore raw

Re: [PATCH 1/3] X.509: Fix certificate gathering again

Quoted 8 lines · David Howells
Quoted 6 lines · David Woodhouse
Quoted 5 lines

The only issue is that the makefile expressions are a bit hairy. For starters, we already have definitions for $(quote) and $(space) in kbuild. I'll have a closer look at the config_filename macro and try to simplify it somehow. But it's just cosmetics, the patch can be merged as is for now.

Quoted 6 lines
Quoted 4 lines

I've actually changed my mind about the 'awk | base64 -d' bit. The error handling is too poor. I'd like to do that in C with a variant of the existing extract_cert tool, and make sure we have proper X.509 certificates and error handling/reporting.

You could also do it in perl pretty easily.

It's a fairly trivial change to scripts/extract-cert.c though, which gives you validation of the cert ASN.1 instead of just base64-decoding and importing arbitrary crap as long as it's found between
-----BEGIN CERTIFICATE-----
and
-----END CERTIFICATE-----

Yeah, we could do the X.509 parsing/validation in perl too. But let's not.

Signature · 2 lines
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation