LivePatch signing support
v3 cover LivePatch signing support
Live patch signing support was mentioned as future work in the design document several years ago. This series finally implements support for it since it is a requirement of Secure Boot to prevent loading unsigned code into Xen.
See the individual patches for what has changed in v3.
Jennifer Herbert (1):
livepatch: Verify livepatch signatures
Kevin Lampis (1):
livepatch: Embed public key in Xen
Ross Lagerwall (3):
docs: Introduce live patch signing
crypto: Add RSA support
livepatch: Load built-in key during boot
docs/misc/livepatch.pandoc | 106 +- xen/common/Kconfig | 18 + xen/common/livepatch.c | 139 +++ xen/common/livepatch_elf.c | 55 + xen/crypto/Makefile | 12 + xen/crypto/rsa.c | 196 ++++ xen/include/xen/livepatch.h | 15 + xen/include/xen/livepatch_elf.h | 18 + xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/lib/Makefile | 1 + xen/lib/mpi.c | 1729 +++++++++++++++++++++++++++++++ xen/tools/extract-key.py | 40 + 13 files changed, 2419 insertions(+), 52 deletions(-) create mode 100644 xen/crypto/rsa.c create mode 100644 xen/include/xen/mpi.h create mode 100644 xen/include/xen/rsa.h create mode 100644 xen/lib/mpi.c create mode 100755 xen/tools/extract-key.py
Signature · 1 line
2.49.0
PATCH v3 1/5 docs: Introduce live patch signing
Remove a never-implemented description of live patch signing from the TODO section and document signing as implemented by the following patches.
---
No change in v3.
docs/misc/livepatch.pandoc | 106 +++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 52 deletions(-)
docs/misc/livepatch.pandoc +54 -52
@@ -917,6 +917,60 @@ The normal sequence of events is to: 3. *XEN_SYSCTL_LIVEPATCH_ACTION* with *LIVEPATCH_ACTION_APPLY* to apply the patch. 4. *XEN_SYSCTL_LIVEPATCH_GET* to check the `->rc`. If in *-XEN_EAGAIN* spin. If zero exit with success. +## Signature Checking + +While loading live patches would generally be restricted to a privileged +process in dom0, in certain cases signature checking in Xen may be required. +For example, when Secure Boot is enabled live patches need to be verified +before being loaded. + +Xen live patches are ELF binaries but there is no standardized mechanism for +signing ELF binaries. One approach used by Linux is to append a signature to +the end of the binary, outside of the ELF container. While this works, it tends +to be fragile since tools that handle ELF binaries do not correctly handle the +signature. Instead, the approach taken here is to use an ELF note for the +signature. + +The ELF note section name shall be `.note.Xen.signature` with note name `Xen`. +The note type shall encode the signature version, algorithm, and hash: + +* version - uint16_t, bits 0-15 +* algorithm - uint8_t, bits 16-23 +* hash - uint8_t, bits 24-31 + +All other bits of the note type shall be zero. + +The known values for the above fields are: + + #define LIVEPATCH_SIGNATURE_VERSION 1 + #define SIGNATURE_ALGORITHM_RSA 0 + #define SIGNATURE_HASH_SHA256 0 + +The note descriptor length defines the length of the signature. + +To sign a live patch: + +1) Add a new note section with a populated payload signature and zeroed out + signature. +2) Generate a detached signature over the entire binary. +3) Fill in the signature in the note section. + +During live patch load, Xen shall verify the signature using the following +steps: + +1) Copy the signature out of the note section. +2) Zero the signature. +3) Generate a detached signature over the entire binary. +4) Compare against the signature from (1). + +Initially, to avoid including DER / X.509 parsing of certificates, handling +chains, etc. Xen shall verify signatures against a compiled in RSA key in +exponent/modulus form. However, it may be extended in future to support other +types of signatures and key types. + +Support of signatures in Xen and in live patches is optional. However, certain +features such as Secure Boot may require live patches to be signed. + ## Addendum
@@ -1178,58 +1232,6 @@ the function itself. Similar considerations are true to a lesser extent for \__FILE__, but it could be argued that file renaming should be done outside of hotpatches. -## Signature checking requirements. - -The signature checking requires that the layout of the data in memory -**MUST** be same for signature to be verified. This means that the payload -data layout in ELF format **MUST** match what the hypervisor would be -expecting such that it can properly do signature verification. - -The signature is based on the all of the payloads continuously laid out -in memory. The signature is to be appended at the end of the ELF payload -prefixed with the string '`~Module signature appended~\n`', followed by -an signature header then followed by the signature, key identifier, and signers -name. - -Specifically the signature header would be: - - #define PKEY_ALGO_DSA 0 - #define PKEY_ALGO_RSA 1 - - #define PKEY_ID_PGP 0 /* OpenPGP generated key ID */ - #define PKEY_ID_X509 1 /* X.509 arbitrary subjectKeyIdentifier */ - - #define HASH_ALGO_MD4 0 - #define HASH_ALGO_MD5 1 - #define HASH_ALGO_SHA1 2 - #define HASH_ALGO_RIPE_MD_160 3 - #define HASH_ALGO_SHA256 4 - #define HASH_ALGO_SHA384 5 - #define HASH_ALGO_SHA512 6 - #define HASH_ALGO_SHA224 7 - #define HASH_ALGO_RIPE_MD_128 8 - #define HASH_ALGO_RIPE_MD_256 9 - #define HASH_ALGO_RIPE_MD_320 10 - #define HASH_ALGO_WP_256 11 - #define HASH_ALGO_WP_384 12 - #define HASH_ALGO_WP_512 13 - #define HASH_ALGO_TGR_128 14 - #define HASH_ALGO_TGR_160 15 - #define HASH_ALGO_TGR_192 16 - - struct elf_payload_signature { - u8 algo; /* Public-key crypto algorithm PKEY_ALGO_*. */ - u8 hash; /* Digest algorithm: HASH_ALGO_*. */ - u8 id_type; /* Key identifier type PKEY_ID*. */ - u8 signer_len; /* Length of signer's name */ - u8 key_id_len; /* Length of key identifier */ - u8 __pad[3]; - __be32 sig_len; /* Length of signature data */ - }; - -(Note that this has been borrowed from Linux module signature code.). - - ### .bss and .data sections. In place patching writable data is not suitable as it is unclear what should be done
Signature · 1 line
2.49.0
PATCH v3 2/5 livepatch: Embed public key in Xen
In reply to Ross LagerwallFrom: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \
-sha256 -days 3650 -nodes \
-subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
---
In v3:
* Drop unnecessary condition in Makefile
* Use dashes instead of underscores
* Drop section placement annotation on declaration
* Clarify endianness of embedded key
xen/common/Kconfig | 18 +++++++++++++++++ xen/crypto/Makefile | 11 ++++++++++ xen/include/xen/livepatch.h | 5 +++++ xen/tools/extract-key.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.py
xen/common/Kconfig +18 -0
@@ -472,6 +472,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_VERIFY + bool "Verify signed LivePatch payloads" + depends on LIVEPATCH + select CRYPTO + help + Verify signed LivePatch payloads using an RSA public key built + into the Xen hypervisor. Selecting this option requires a + public key in PEM format to be available for embedding during + the build. + +config PAYLOAD_VERIFY_KEY + string "File name of public key used to verify payloads" + default "verify_key.pem" + depends on PAYLOAD_VERIFY + help + The file name of an RSA public key in PEM format to be used for + verifying signed LivePatch payloads. + config FAST_SYMBOL_LOOKUP bool "Fast symbol lookup (bigger binary)" default y
xen/crypto/Makefile +11 -0
@@ -1,2 +1,13 @@ obj-y += rijndael.o obj-y += vmac.o + +obj-$(CONFIG_PAYLOAD_VERIFY) += builtin-payload-key.o + +key-path := $(objtree)/$(patsubst "%",%,$(CONFIG_PAYLOAD_VERIFY_KEY)) +$(obj)/builtin-payload-key.bin: $(key-path) $(srctree)/tools/extract-key.py + $(srctree)/tools/extract-key.py < $< > [email protected] + $(call move-if-changed,[email protected],$@) + +$(obj)/builtin-payload-key.S: BINFILE_FLAGS := -i +$(obj)/builtin-payload-key.S: $(srctree)/tools/binfile $(obj)/builtin-payload-key.bin FORCE + $(call if_changed,binfile,$(obj)/builtin-payload-key.bin xen_livepatch_key_data)
xen/include/xen/livepatch.h +5 -0
@@ -143,6 +143,11 @@ struct payload;int revert_payload (struct payload *data);void revert_payload_tail (struct payload *data); +#ifdef CONFIG_PAYLOAD_VERIFY +/* The public key data contained with Xen used to verify payload signatures. */ +extern const uint8_t xen_livepatch_key_data[]; +#endif +#else /*
xen/tools/extract-key.py new file +40 -0
@@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: GPL-2.0 + +import binascii +import struct +import sys +import subprocess +import re + +# Decode a certificate into a format suitable for embedding in Xen. + +out = subprocess.check_output(['openssl', 'rsa', '-pubin', '-inform', 'PEM', + '-noout', '-text'], stdin=sys.stdin, + universal_newlines=True) +combined = '' +for line in out.split('\n'): + line = line.rstrip() + if line.startswith(' '): + combined += line.strip().replace(':', '') + match = re.match(r'Exponent: .* \(0x(.*)\)', line) + if match: + e = match.group(1) + +n = combined.lstrip('0') +if len(n) % 2 == 1: + n = '0' + n +n = binascii.unhexlify(n) +e = e.lstrip('0') +if len(e) % 2 == 1: + e = '0' + e +e = binascii.unhexlify(e) + +# Use little-endian for lengths. +# Use big-endian for byte sequences (as openssl does). + +sys.stdout.buffer.write(struct.pack('<I', len(n))) +sys.stdout.buffer.write(n) +sys.stdout.buffer.write(struct.pack('<I', len(e))) +sys.stdout.buffer.write(e)
Signature · 1 line
2.49.0
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
Quoted 45 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Drop unnecessary condition in
Makefile
* Use dashes instead of underscores
* Drop section placement annotation on declaration
* Clarify endianness of embedded keyxen/common/Kconfig | 18 +++++++++++++++++ xen/crypto/Makefile | 11 ++++++++++ xen/include/xen/livepatch.h | 5 +++++ xen/tools/extract-key.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.pyxen/common/Kconfig +1
-0@@ -472,6 +472,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_VERIFY
xen/common/Kconfig config LIVEPATCH · line 485
Quoted 10 lines
xen/common/Kconfig +10
-0+ bool "Verify signed LivePatch payloads" + depends on LIVEPATCH + select CRYPTO + help + Verify signed LivePatch payloads using an RSA public key built + into the Xen hypervisor. Selecting this option requires a + public key in PEM format to be available for embedding during + the build. + +config PAYLOAD_VERIFY_KEY
This identifier to me mean "verify the key of payloads", when really ...
xen/common/Kconfig config LIVEPATCH · line 486
Quoted 1 line
xen/common/Kconfig +1
-0+ string "File name of public key used to verify payloads"
... you mean the key used for verification. To express this in the name, how about PAYLOAD_VERIFICATION_KEY?
xen/common/Kconfig config LIVEPATCH · line 487
Quoted 1 line
xen/common/Kconfig +1
-0+ default "verify_key.pem"
The revlog says you replaced underscores by dashes.
xen/common/Kconfig config LIVEPATCH · line 491
Quoted 4 lines
xen/common/Kconfig +4
-0+ depends on PAYLOAD_VERIFY + help + The file name of an RSA public key in PEM format to be used for + verifying signed LivePatch payloads.
Perhaps the fact that it needs to be an RSA key may also want expressing in the Kconfig identifier?
Should the help text perhaps further clarify where the file is going to be looked for in case it's a relative path (as in particular the default is)? Seeing that ...
xen/crypto/Makefile line 6
Quoted 9 lines
xen/crypto/Makefile +4
-0@@ -1,2 +1,13 @@ obj-y += rijndael.o obj-y += vmac.o + +obj-$(CONFIG_PAYLOAD_VERIFY) += builtin-payload-key.o + +key-path := $(objtree)/$(patsubst "%",%,$(CONFIG_PAYLOAD_VERIFY_KEY))
... it's in the object tree now (which will break if an absolute path was specified), how do you intend the key to make it there? In the default case, for an out-of-tree build the build tree doesn't exist ahead of the build being invoked.
xen/crypto/Makefile line 13
Quoted 7 lines
xen/crypto/Makefile +7
-0+$(obj)/builtin-payload-key.bin: $(key-path) $(srctree)/tools/extract-key.py + $(srctree)/tools/extract-key.py < $< > [email protected] + $(call move-if-changed,[email protected],$@) + +$(obj)/builtin-payload-key.S: BINFILE_FLAGS := -i +$(obj)/builtin-payload-key.S: $(srctree)/tools/binfile $(obj)/builtin-payload-key.bin FORCE + $(call if_changed,binfile,$(obj)/builtin-payload-key.bin xen_livepatch_key_data)
Does the builtin- prefix have any real significance here? Anything that's part of the hypervisor build is "built in", after all.
xen/include/xen/livepatch.h struct payload; · line 149
Quoted 10 lines
xen/include/xen/livepatch.h +4
-0@@ -143,6 +143,11 @@ struct payload;int revert_payload (struct payload *data);void revert_payload_tail (struct payload *data); +#ifdef CONFIG_PAYLOAD_VERIFY +/* The public key data contained with Xen used to verify payload signatures. */ +extern const uint8_t xen_livepatch_key_data[]; +#endif
What's our position towards such declarations without (C) definition? The 8.6 deviation talks about cases "when the definition is compiled-out or optimized-out by the compiler". Shouldn't we follow the 8.4 model here and annotate them asmlinkage as well?
Jan
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
In reply to Ross LagerwallQuoted 13 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
Quoted 7 lines
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>
While reviewing patch 4 it occurred to me: Why embed the key? Can't this be specified as (another) boot module?
Jan
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Quoted 5 lines
Quoted 2 lines
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>While reviewing patch 4 it occurred to me: Why embed the key? Can't this be specified as (another) boot module?
Then the key itself will need to be signed, and it's extra churn that we would need to verify at boot. I'm not opposed to being able to load the key as a module, but it seems reasonable to also bundle one in Xen. If there's interest in passing one as a module it could always be implemented as a separate feature. IMO: I don't see both approaches as being incompatible with each other.
Thanks, Roger.
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
In reply to Ross LagerwallQuoted 51 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Drop unnecessary condition in
Makefile
* Use dashes instead of underscores
* Drop section placement annotation on declaration
* Clarify endianness of embedded keyxen/common/Kconfig | 18 +++++++++++++++++ xen/crypto/Makefile | 11 ++++++++++ xen/include/xen/livepatch.h | 5 +++++ xen/tools/extract-key.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.pyxen/common/Kconfig +7
-0@@ -472,6 +472,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_VERIFY + bool "Verify signed LivePatch payloads" + depends on LIVEPATCH + select CRYPTO + help + Verify signed LivePatch payloads using an RSA public key built + into the Xen hypervisor. Selecting this option requires a
xen/common/Kconfig config LIVEPATCH · line 491
Quoted 10 lines
xen/common/Kconfig +10
-0+ public key in PEM format to be available for embedding during + the build. + +config PAYLOAD_VERIFY_KEY + string "File name of public key used to verify payloads" + default "verify_key.pem" + depends on PAYLOAD_VERIFY + help + The file name of an RSA public key in PEM format to be used for + verifying signed LivePatch payloads.
I think this is likely to break the randconfig testing that we do in Gitlab CI, as randconfig could select PAYLOAD_VERIFY, but there will be no key included, and hence the build will fail?
Ideally Gitlab CI would need to be adjusted to provide such key so the build doesn't fail. I think it could be provided unconditionally to simplify the logic, if the option is not selected the file will simply be ignored.
Thanks, Roger.
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
Quoted 67 lines · Roger Pau Monné
Quoted 51 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Drop unnecessary condition in
Makefile
* Use dashes instead of underscores
* Drop section placement annotation on declaration
* Clarify endianness of embedded keyxen/common/Kconfig | 18 +++++++++++++++++ xen/crypto/Makefile | 11 ++++++++++ xen/include/xen/livepatch.h | 5 +++++ xen/tools/extract-key.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.pyxen/common/Kconfig +7
-0@@ -472,6 +472,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_VERIFY + bool "Verify signed LivePatch payloads" + depends on LIVEPATCH + select CRYPTO + help + Verify signed LivePatch payloads using an RSA public key built + into the Xen hypervisor. Selecting this option requires axen/common/Kconfig config LIVEPATCH · line 491
Quoted 10 lines
xen/common/Kconfig +10
-0+ public key in PEM format to be available for embedding during + the build. + +config PAYLOAD_VERIFY_KEY + string "File name of public key used to verify payloads" + default "verify_key.pem" + depends on PAYLOAD_VERIFY + help + The file name of an RSA public key in PEM format to be used for + verifying signed LivePatch payloads.I think this is likely to break the randconfig testing that we do in Gitlab CI, as randconfig could select
PAYLOAD_VERIFY, but there will be no key included, and hence the build will fail?
Quoted 4 lines
Ideally Gitlab CI would need to be adjusted to provide such key so the build doesn't fail. I think it could be provided unconditionally to simplify the logic, if the option is not selected the file will simply be ignored.
Alternatively the two options could be folded, the default being the empty string meaning "no payload verification". I don't think randconfig can sensibly make up random strings ...
Jan
Re: [PATCH v3 2/5] livepatch: Embed public key in Xen
Quoted 68 lines · Jan Beulich
Quoted 67 lines · Roger Pau Monné
Quoted 51 lines · Ross Lagerwall
From: Kevin Lampis <[email protected]>
Make it possible to embed a public key in Xen to be used when verifying live patch payloads. Inclusion of the public key is optional.
To avoid needing to include a DER / X.509 parser in the hypervisor, the public key is unpacked at build time and included in a form that is convenient for the hypervisor to consume. This is different approach from that used by Linux which embeds the entire X.509 certificate and builds in a parser for it.
A suitable key can be created using openssl:
openssl req -x509 -newkey rsa:2048 -keyout priv.pem -out pub.pem \ -sha256 -days 3650 -nodes \ -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"openssl x509 -inform PEM -in pub.pem -outform PEM -pubkey -nocert -out verify_key.pem
Signed-off-by Kevin Lampis <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Drop unnecessary condition in
Makefile
* Use dashes instead of underscores
* Drop section placement annotation on declaration
* Clarify endianness of embedded keyxen/common/Kconfig | 18 +++++++++++++++++ xen/crypto/Makefile | 11 ++++++++++ xen/include/xen/livepatch.h | 5 +++++ xen/tools/extract-key.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.pyxen/common/Kconfig +7
-0@@ -472,6 +472,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_VERIFY + bool "Verify signed LivePatch payloads" + depends on LIVEPATCH + select CRYPTO + help + Verify signed LivePatch payloads using an RSA public key built + into the Xen hypervisor. Selecting this option requires axen/common/Kconfig config LIVEPATCH · line 491
Quoted 10 lines
xen/common/Kconfig +10
-0+ public key in PEM format to be available for embedding during + the build. + +config PAYLOAD_VERIFY_KEY + string "File name of public key used to verify payloads" + default "verify_key.pem" + depends on PAYLOAD_VERIFY + help + The file name of an RSA public key in PEM format to be used for + verifying signed LivePatch payloads.I think this is likely to break the randconfig testing that we do in Gitlab CI, as randconfig could select
PAYLOAD_VERIFY, but there will be no key included, and hence the build will fail?
Quoted 8 lines
Quoted 4 lines
Ideally Gitlab CI would need to be adjusted to provide such key so the build doesn't fail. I think it could be provided unconditionally to simplify the logic, if the option is not selected the file will simply be ignored.
Alternatively the two options could be folded, the default being the empty string meaning "no payload verification". I don't think randconfig can sensibly make up random strings ...
Could be an option. Not sure if the menu interface would then look a bit weird. IMO it's a nicer UI to enable the option and then get asked for the cert to use rather than bundling everything in a single string-like option.
Thanks, Roger.
PATCH v3 3/5 crypto: Add RSA support
In reply to Ross LagerwallIn preparation for adding support for livepatch signing, add support for RSA crypto.
The RSA code is extracted from Nettle at tag nettle_3.2_release_20160128
(https://git.lysator.liu.se/nettle/nettle).
The MPI code is extracted from Linux at commit eef0df6a5953 (lib/mpi/*).
---
In v3:
* Move mpi.c to lib
* Fix header guard name
xen/crypto/Makefile | 1 + xen/crypto/rsa.c | 196 +++++ xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/lib/Makefile | 1 + xen/lib/mpi.c | 1729 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 2069 insertions(+) create mode 100644 xen/crypto/rsa.c create mode 100644 xen/include/xen/mpi.h create mode 100644 xen/include/xen/rsa.h create mode 100644 xen/lib/mpi.c
xen/crypto/Makefile +1 -0
@@ -1,4 +1,5 @@ obj-y += rijndael.o +obj-$(CONFIG_PAYLOAD_VERIFY) += rsa.o obj-y += vmac.o obj-$(CONFIG_PAYLOAD_VERIFY) += builtin-payload-key.o
xen/crypto/rsa.c new file +196 -0
196 lines not on this page · raw message
xen/include/xen/mpi.h new file +68 -0
68 lines not on this page · raw message
xen/include/xen/rsa.h new file +74 -0
74 lines not on this page · raw message
xen/lib/Makefile +1 -0
@@ -14,6 +14,7 @@ lib-y += memcmp.o lib-y += memcpy.o lib-y += memmove.o lib-y += memset.o +lib-y += mpi.o lib-y += muldiv64.o lib-y += parse-size.o lib-y += rbtree.o
xen/lib/mpi.c new file +1729 -0
1,729 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v3 3/5] crypto: Add RSA support
Quoted 9 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
The RSA code is extracted from Nettle at tag nettle_3.2_release_20160128
(https://git.lysator.liu.se/nettle/nettle).The MPI code is extracted from Linux at commit
eef0df6a5953(lib/mpi/*).Signed-off-by Ross Lagerwall <[email protected]>
This looks like it's independent of the earlier patches, and hence could go in right away. Then again maybe doing so wouldn't make much sense prior to ...
xen/crypto/Makefile line 2
Quoted 5 lines
xen/crypto/Makefile +1
-0@@ -1,4 +1,5 @@ obj-y += rijndael.o +obj-$(CONFIG_PAYLOAD_VERIFY) += rsa.o
... this new Kconfig setting appearing (eve though the lack thereof wouldn't break anything, afaict).
Jan
Re: [PATCH v3 3/5] crypto: Add RSA support
In reply to Ross LagerwallQuoted 16 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
The RSA code is extracted from Nettle at tag nettle_3.2_release_20160128
(https://git.lysator.liu.se/nettle/nettle).The MPI code is extracted from Linux at commit
eef0df6a5953(lib/mpi/*).Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Move
mpi.cto lib
* Fix header guard name
Quoted 6 lines
xen/crypto/Makefile | 1 + xen/crypto/rsa.c | 196 +++++ xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/lib/Makefile | 1 + xen/lib/mpi.c | 1729 +++++++++++++++++++++++++++++++++++++++++
Just FTAOD: all the functions imported in mpi.c are used I think? So that we don't introduce unreachable code.
Thanks, Roger.
Re: [PATCH v3 3/5] crypto: Add RSA support
Quoted 25 lines · Roger Pau Monné
Quoted 16 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
The RSA code is extracted from Nettle at tag nettle_3.2_release_20160128
(https://git.lysator.liu.se/nettle/nettle).The MPI code is extracted from Linux at commit
eef0df6a5953(lib/mpi/*).Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Move
mpi.cto lib
* Fix header guard nameQuoted 6 lines
xen/crypto/Makefile | 1 + xen/crypto/rsa.c | 196 +++++ xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/lib/Makefile | 1 + xen/lib/mpi.c | 1729 +++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
Just FTAOD: all the functions imported in
mpi.care used I think? So that we don't introduce unreachable code.
Yes, everything included here is used by the end of the patch series.
Ross
Re: [PATCH v3 3/5] crypto: Add RSA support
Quoted 26 lines · Ross Lagerwall
Quoted 25 lines · Roger Pau Monné
Quoted 22 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
The RSA code is extracted from Nettle at tag nettle_3.2_release_20160128
(https://git.lysator.liu.se/nettle/nettle).The MPI code is extracted from Linux at commit
eef0df6a5953(lib/mpi/*).Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Move
mpi.cto lib
* Fix header guard namexen/crypto/Makefile | 1 + xen/crypto/rsa.c | 196 +++++ xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/lib/Makefile | 1 + xen/lib/mpi.c | 1729 +++++++++++++++++++++++++++++++++++++++++
Quoted 4 lines
Quoted 2 lines
Just FTAOD: all the functions imported in
mpi.care used I think? So that we don't introduce unreachable code.Yes, everything included here is used by the end of the patch series.
What about intermediate state? It's conceivable that such a series might go in piecemeal ...
Jan
PATCH v3 4/5 livepatch: Load built-in key during boot
In reply to Ross LagerwallParse the raw data of the embedded RSA key into a form that can be later used for verifying live patch signatures.
---
* Fix endianness of builtin key lengths
* Set builtin key to __ro_after_init
xen/common/livepatch.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
xen/common/livepatch.c +36 -0
@@ -11,6 +11,8 @@#include <xen/lib.h> #include <xen/list.h> #include <xen/mm.h> +#include <xen/mpi.h> +#include <xen/rsa.h> #include <xen/sched.h> #include <xen/smp.h> #include <xen/softirq.h>
@@ -73,6 +75,10 @@ static struct livepatch_work livepatch_work; static DEFINE_PER_CPU(bool, work_to_do); static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet); +#ifdef CONFIG_PAYLOAD_VERIFY +static struct rsa_public_key __ro_after_init builtin_payload_key; +#endif +static int get_name (const struct xen_livepatch_name *name,char *n) {if ( !name->size || name->size >XEN_LIVEPATCH_NAME_SIZE )
@@ -2300,6 +2306,31 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +static int load_builtin_payload_key (void ) +{ +#ifdef CONFIG_PAYLOAD_VERIFY +const uint8_t *ptr; +uint32_t len; + +rsa_public_key_init (&builtin_payload_key); + + ptr = xen_livepatch_key_data; + +memcpy (&len, ptr,sizeof (len)); + ptr +=sizeof (len); + builtin_payload_key.n =mpi_read_raw_data (ptr,le32_to_cpu (len)); + ptr += len; + +memcpy (&len, ptr,sizeof (len)); + ptr +=sizeof (len); + builtin_payload_key.e =mpi_read_raw_data (ptr,le32_to_cpu (len)); + +return rsa_public_key_prepare (&builtin_payload_key); +#else +return 0 ; +#endif +} + static int cf_check cpu_callback(struct notifier_block *nfb,unsigned long action,void *hcpu) {
@@ -2318,6 +2349,11 @@ static struct notifier_block cpu_nfb = { static int __init cf_check livepatch_init(void) {unsigned int cpu; +int err; + + err =load_builtin_payload_key (); +if ( err ) +return err;for_each_online_cpu ( cpu ) {
Signature · 1 line
2.49.0
Re: [PATCH v3 4/5] livepatch: Load built-in key during boot
Quoted 37 lines · Ross Lagerwall
Parse the raw data of the embedded RSA key into a form that can be later used for verifying live patch signatures.
Signed-off-by Ross Lagerwall <[email protected]>---
* Fix endianness of builtin key lengths
* Set builtin key to__ro_after_initxen/common/livepatch.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)xen/common/livepatch.c +6
-0@@ -11,6 +11,8 @@#include <xen/lib.h> #include <xen/list.h> #include <xen/mm.h> +#include <xen/mpi.h> +#include <xen/rsa.h> #include <xen/sched.h> #include <xen/smp.h> #include <xen/softirq.h> @@ -73,6 +75,10 @@ static struct livepatch_work livepatch_work; static DEFINE_PER_CPU(bool, work_to_do); static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet); +#ifdef CONFIG_PAYLOAD_VERIFY +static struct rsa_public_key __ro_after_init builtin_payload_key; +#endif +static int get_name (const struct xen_livepatch_name *name,char *n) {if ( !name->size || name->size >XEN_LIVEPATCH_NAME_SIZE )
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2309
Quoted 4 lines
xen/common/livepatch.c +1
-0@@ -2300,6 +2306,31 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +static int load_builtin_payload_key (void )
__init
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2319
Quoted 10 lines
xen/common/livepatch.c +10
-0+{ +#ifdef CONFIG_PAYLOAD_VERIFY +const uint8_t *ptr; +uint32_t len; + +rsa_public_key_init (&builtin_payload_key); + + ptr = xen_livepatch_key_data; + +memcpy (&len, ptr,sizeof (len));
How do you know enough data is available?
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2321
Quoted 2 lines
xen/common/livepatch.c +2
-0+ ptr +=sizeof (len); + builtin_payload_key.n =mpi_read_raw_data (ptr,le32_to_cpu (len));
Even more so here. And then again below.
Quoted 17 lines
xen/common/livepatch.c +12
-0+ ptr += len; + +memcpy (&len, ptr,sizeof (len)); + ptr +=sizeof (len); + builtin_payload_key.e =mpi_read_raw_data (ptr,le32_to_cpu (len)); + +return rsa_public_key_prepare (&builtin_payload_key); +#else +return 0 ; +#endif +} + static int cf_check cpu_callback(struct notifier_block *nfb,unsigned long action,void *hcpu) {@@ -2318,6 +2349,11 @@ static struct notifier_block cpu_nfb = { static int __init cf_check livepatch_init(void)
xen/common/livepatch.c static struct notifier_block cpu_nfb = { · line 2359
Quoted 10 lines
xen/common/livepatch.c +5
-0{unsigned int cpu; +int err; + + err =load_builtin_payload_key (); +if ( err ) +return err;for_each_online_cpu ( cpu ) {
So far this function can't fail. Therefore it makes sense that I can't find any provisions for this case elsewhere in the file. Such provisions will need adding as a prereq, I expect. Or alternatively it would want clarifying explicitly that (why) none are required.
Jan
PATCH v3 5/5 livepatch: Verify livepatch signatures
In reply to Ross LagerwallFrom: Jennifer Herbert <[email protected]>
Verify livepatch signatures against the embedded public key in Xen. Failing to verify does not prevent the livepatch from being loaded. In future, this will be changed for certain cases (e.g. when Secure Boot is enabled).
---
In v3:
* Minor style fixes
xen/common/livepatch.c | 103 ++++++++++++++++++++++++++++++++ xen/common/livepatch_elf.c | 55 +++++++++++++++++ xen/include/xen/livepatch.h | 10 ++++ xen/include/xen/livepatch_elf.h | 18 ++++++ 4 files changed, 186 insertions(+)
xen/common/livepatch.c +103 -0
@@ -14,6 +14,7 @@#include <xen/mpi.h> #include <xen/rsa.h> #include <xen/sched.h> +#include <xen/sha2.h> #include <xen/smp.h> #include <xen/softirq.h> #include <xen/spinlock.h>
@@ -525,6 +526,106 @@ static int check_xen_buildid(const struct livepatch_elf *elf)return 0 ; } +#ifdef CONFIG_PAYLOAD_VERIFY +static int check_rsa_sha256_signature (void *data,size_t datalen, +void *sig,uint32_t siglen) +{ +struct sha2_256_state hash; +MPI s; +int rc; + + s =mpi_read_raw_data (sig, siglen); +if ( !s ) + { + printk(XENLOG_ERR LIVEPATCH "Failed to mpi_read_raw_data\n"); +return -ENOMEM ; + } + +sha2_256_init (&hash); +sha2_256_update (&hash, data, datalen); + + rc =rsa_sha256_verify (&builtin_payload_key, &hash, s); +if ( rc ) + printk(XENLOG_ERR LIVEPATCH "rsa_sha256_verify failed: %d\n", rc); + +mpi_free (s); + +return rc; +} +#endif + +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size) +{ +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 +static const char notename[] ="Xen" ; +void *sig; +livepatch_elf_note note; +int rc; + + rc =livepatch_elf_note_by_names (elf,ELF_XEN_SIGNATURE , notename,-1 , + ¬e); +if ( rc ) + { +dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: Signature not present\n" , + elf->name); +return rc; + } + +/* We expect only one signature, find a second is an error! */ + rc =livepatch_elf_next_note_by_name (notename,-1 , ¬e); +if ( rc != -ENOENT ) + { +if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH + "Error while checking for notes! err = %d\n", rc); +return rc; + } +else + { + printk(XENLOG_ERR LIVEPATCH + "Error, found second signature note! There can be only one!\n"); +return -EINVAL ; + } + } + +if (SIGNATURE_VERSION (note.type) !=LIVEPATCH_SIGNATURE_VERSION || +SIGNATURE_ALGORITHM (note.type) !=SIGNATURE_ALGORITHM_RSA || +SIGNATURE_HASH (note.type) !=SIGNATURE_HASH_SHA256 ) + { + printk(XENLOG_ERR LIVEPATCH +"Unsupported signature type: v:%u, a:%u, h:%u\n" , +SIGNATURE_VERSION (note.type),SIGNATURE_ALGORITHM (note.type), + SIGNATURE_HASH(note.type)); +return -EINVAL ; + } + +if ( note.size ==0 || note.size >=MAX_SIG_NOTE_SIZE ) + { + printk(XENLOG_ERR LIVEPATCH "Invalid signature note size: %u\n", + note.size); +return -EINVAL ; + } + + sig =xmalloc_bytes (note.size); +if ( !sig ) +return -ENOMEM ; + +memcpy (sig, note.data, note.size); + +/* Remove signature from data, as can't be verified with it. */ +memset ((void *)note.data,0 , note.size); + rc =check_rsa_sha256_signature (raw, size, sig, note.size); + +xfree (sig); +return rc; +#else +return -EINVAL ; +#endif +} +static int check_special_sections (const struct livepatch_elf *elf) {unsigned int i;
@@ -1162,6 +1263,8 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)if ( rc ) goto out; +check_signature (&elf, raw, len); + rc =move_payload (payload, &elf);if ( rc ) goto out;
xen/common/livepatch_elf.c +55 -0
61 lines not on this page · raw message
xen/include/xen/livepatch.h +10 -0
@@ -38,6 +38,7 @@ struct xen_sysctl_livepatch_op;#define ELF_LIVEPATCH_DEPENDS ".livepatch.depends"#define ELF_LIVEPATCH_XEN_DEPENDS ".livepatch.xen_depends"#define ELF_BUILD_ID_NOTE ".note.gnu.build-id" +#define ELF_XEN_SIGNATURE ".note.Xen.signature"#define ELF_LIVEPATCH_LOAD_HOOKS ".livepatch.hooks.load"#define ELF_LIVEPATCH_UNLOAD_HOOKS ".livepatch.hooks.unload"#define ELF_LIVEPATCH_PREAPPLY_HOOK ".livepatch.hooks.preapply"
@@ -49,6 +50,15 @@ struct xen_sysctl_livepatch_op;/* Arbitrary limit for payload size and .bss section size. */ #define LIVEPATCH_MAX_SIZE MB(2) +#define SIGNATURE_VERSION (type) ((type) & 0xffff) +#define LIVEPATCH_SIGNATURE_VERSION 1 + +#define SIGNATURE_ALGORITHM (type) (((type) >> 16) & 0xff) +#define SIGNATURE_ALGORITHM_RSA 0 + +#define SIGNATURE_HASH (type) (((type) >> 24) & 0xff) +#define SIGNATURE_HASH_SHA256 0 +struct livepatch_symbol {const char *name;unsigned long value;
xen/include/xen/livepatch_elf.h +18 -0
@@ -39,6 +39,16 @@ struct livepatch_elf {unsigned int symtab_idx; }; +typedef struct { +uint32_t size;/* Note size */ +uint32_t type;/* Note type */ +const void *data;/* Pointer to note */ + +/* Private */ +const Elf_Note *next; +const void *end; +}livepatch_elf_note ; +const struct livepatch_elf_sec *livepatch_elf_sec_by_name (const struct livepatch_elf *elf,const char *name);
@@ -48,6 +58,14 @@ void livepatch_elf_free(struct livepatch_elf *elf);int livepatch_elf_resolve_symbols (struct livepatch_elf *elf);int livepatch_elf_perform_relocs (struct livepatch_elf *elf); +int livepatch_elf_note_by_names (const struct livepatch_elf *elf, +const char *sec_name,const char *note_name, +const unsigned int type, +livepatch_elf_note *note); +int livepatch_elf_next_note_by_name (const char *note_name, +const unsigned int type, +livepatch_elf_note *note); +static inline bool livepatch_elf_ignore_section (const Elf_Shdr *sec) {return !(sec->sh_flags &SHF_ALLOC );
Signature · 1 line
2.49.0
Re: [PATCH v3 5/5] livepatch: Verify livepatch signatures
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 537
Quoted 13 lines · Ross Lagerwall
xen/common/livepatch.c +9
-0@@ -525,6 +526,106 @@ static int check_xen_buildid(const struct livepatch_elf *elf)return 0 ; } +#ifdef CONFIG_PAYLOAD_VERIFY +static int check_rsa_sha256_signature (void *data,size_t datalen, +void *sig,uint32_t siglen) +{ +struct sha2_256_state hash; +MPI s; +int rc; + + s =mpi_read_raw_data (sig, siglen);
sig apparently being an input, can't the function parameter be pointer-to-const?
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 540
Quoted 3 lines
xen/common/livepatch.c +3
-0+if ( !s ) + { + printk(XENLOG_ERR LIVEPATCH "Failed to mpi_read_raw_data\n");
Both there and ...
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 549
Quoted 9 lines
xen/common/livepatch.c +9
-0+return -ENOMEM ; + } + +sha2_256_init (&hash); +sha2_256_update (&hash, data, datalen); + + rc =rsa_sha256_verify (&builtin_payload_key, &hash, s); +if ( rc ) + printk(XENLOG_ERR LIVEPATCH "rsa_sha256_verify failed: %d\n", rc);
... here: Instead of mentioning function names, perhaps better to say in normal word what failed. E.g. here "RSA/SHA256 verification failed".
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 557
Quoted 7 lines
xen/common/livepatch.c +7
-0+mpi_free (s); + +return rc; +} +#endif + +static int check_signature(const struct livepatch_elf *elf, void *raw,
"raw" is an input only, too, isn't it? As such it ought to be const void *.
Quoted 10 lines
xen/common/livepatch.c +10
-0+ size_t size) +{ +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 +static const char notename[] ="Xen" ; +void *sig; +livepatch_elf_note note; +int rc; + + rc = livepatch_elf_note_by_names(elf, ELF_XEN_SIGNATURE, notename, -1,
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 577
Quoted 10 lines
xen/common/livepatch.c +10
-0+ ¬e); +if ( rc ) + { +dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: Signature not present\n" , + elf->name); +return rc; + } + +/* We expect only one signature, find a second is an error! */ + rc =livepatch_elf_next_note_by_name (notename,-1 , ¬e);
I can see why finding another signature of the same type might be a problem, but one of a different type should be fine (i.e. unambiguous), I suppose. Irrespective I find it slightly odd that you pass in a pointer to the same local variable. The function, after all, is free to alter that variable ahead of encountering the next matching note.
Quoted 25 lines
xen/common/livepatch.c +25
-0+if ( rc != -ENOENT ) + { +if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH + "Error while checking for notes! err = %d\n", rc); +return rc; + } +else + { + printk(XENLOG_ERR LIVEPATCH + "Error, found second signature note! There can be only one!\n"); +return -EINVAL ; + } + } + +if (SIGNATURE_VERSION (note.type) !=LIVEPATCH_SIGNATURE_VERSION || +SIGNATURE_ALGORITHM (note.type) !=SIGNATURE_ALGORITHM_RSA || +SIGNATURE_HASH (note.type) !=SIGNATURE_HASH_SHA256 ) + { + printk(XENLOG_ERR LIVEPATCH +"Unsupported signature type: v:%u, a:%u, h:%u\n" , +SIGNATURE_VERSION (note.type),SIGNATURE_ALGORITHM (note.type), + SIGNATURE_HASH(note.type)); +return -EINVAL ;
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 612
Quoted 10 lines
xen/common/livepatch.c +10
-0+ } + +if ( note.size ==0 || note.size >=MAX_SIG_NOTE_SIZE ) + { + printk(XENLOG_ERR LIVEPATCH "Invalid signature note size: %u\n", + note.size); +return -EINVAL ; + } + + sig =xmalloc_bytes (note.size);
In principle new code is supposed to be using functions from the xvmalloc() family. (That family deliberately does not include xvmalloc_bytes(). I think you mean xmalloc_array() here anyway.)
xen/common/livepatch.c static int load_payload_data(struct payload *payload, void *raw, size_t len) · line 1266
Quoted 5 lines
xen/common/livepatch.c +1
-0@@ -1162,6 +1263,8 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)if ( rc ) goto out; +check_signature (&elf, raw, len);
You entirely ignore the return value? (I was first wondering why the stub variant of the function would return -EINVAL rather than 0.)
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 27
Quoted 8 lines
xen/common/livepatch_elf.c +2
-0@@ -23,6 +23,61 @@ livepatch_elf_sec_by_name(const struct livepatch_elf *elf,return NULL ; } +int livepatch_elf_note_by_names(const struct livepatch_elf *elf, + const char *sec_name, const char *note_name,
So the plural in the function name is because it's both a section and a note name, whereas ...
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 34
Quoted 7 lines
xen/common/livepatch_elf.c +7
-0+const unsigned int type, + livepatch_elf_note *note) +{ + const struct livepatch_elf_sec *sec = livepatch_elf_sec_by_name(elf, + sec_name); +if ( !sec ) +return -ENOENT ;
(Nit: Too deep indentation.)
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 44
Quoted 9 lines
xen/common/livepatch_elf.c +9
-0+ note->end = sec->addr + sec->sec->sh_size; + note->next = sec->addr; + +return livepatch_elf_next_note_by_name (note_name, type, note); +} + +int livepatch_elf_next_note_by_name (const char *note_name, +const unsigned int type, +livepatch_elf_note *note)
... here you only look for further instances in the previously found section. What if there's a 2nd section of the given name?
livepatch_elf_sec_by_name() also doesn't look to be making sure the section is of the correct type.
Why is it necessary o pass in the note name again? A typical "find next" would find the same kind as the "find first" did. Whereas here by passing in a different name one can achieve "interesting" results. (Getting this correct may not be necessary, but a comment would then want putting ahead of the function.)
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 52
Quoted 8 lines
xen/common/livepatch_elf.c +8
-0+{ +const Elf_Note *pkd_note = note->next; +size_t notenamelen =strlen (note_name) +1 ; +size_t note_hd_size; +size_t note_size; +size_t remaining; + +while ( (void *)pkd_note < note->end )
Misra objects to the casting away of type qualifiers (and I do, too). More such further down (and at least one instance also further up).
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 53
Quoted 2 lines
xen/common/livepatch_elf.c +2
-0+ { +
Nit: Stray blank line. Perhaps you really meant to move some of the variable declarations here?
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 60
Quoted 6 lines
xen/common/livepatch_elf.c +6
-0+ remaining = note->end - (void *)pkd_note; +if ( remaining <sizeof (livepatch_elf_note) ) +return -EINVAL ; + + note_hd_size =sizeof (Elf_Note) + ((pkd_note->namesz +3 ) & ~0x3 ); + note_size = note_hd_size + ((pkd_note->descsz +3 ) & ~0x3 );
What use are the 0x prefixes here (and then only on 2 of the three instances)?
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 65
Quoted 5 lines
xen/common/livepatch_elf.c +5
-0+if ( remaining < note_size ) +return -EINVAL ; + +if ( notenamelen == pkd_note->namesz && + !memcmp(note_name, (const void *) pkd_note + sizeof(Elf_Note),
Nit: Stray blank between cast and cast value.
Jan
Re: [PATCH v3 5/5] livepatch: Verify livepatch signatures
In reply to Ross LagerwallQuoted 34 lines · Ross Lagerwall
From: Jennifer Herbert <[email protected]>
Verify livepatch signatures against the embedded public key in Xen. Failing to verify does not prevent the livepatch from being loaded. In future, this will be changed for certain cases (e.g. when Secure Boot is enabled).
Signed-off-by Jennifer Herbert <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Minor style fixes
xen/common/livepatch.c | 103 ++++++++++++++++++++++++++++++++ xen/common/livepatch_elf.c | 55 +++++++++++++++++ xen/include/xen/livepatch.h | 10 ++++ xen/include/xen/livepatch_elf.h | 18 ++++++ 4 files changed, 186 insertions(+)xen/common/livepatch.c +1
-0@@ -14,6 +14,7 @@#include <xen/mpi.h> #include <xen/rsa.h> #include <xen/sched.h> +#include <xen/sha2.h> #include <xen/smp.h> #include <xen/softirq.h> #include <xen/spinlock.h>
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 531
Quoted 6 lines
xen/common/livepatch.c +3
-0@@ -525,6 +526,106 @@ static int check_xen_buildid(const struct livepatch_elf *elf)return 0 ; } +#ifdef CONFIG_PAYLOAD_VERIFY +static int check_rsa_sha256_signature (void *data,size_t datalen, +void *sig,uint32_t siglen)
I think both data and sig could be const here?
Quoted 48 lines
xen/common/livepatch.c +48
-0+{ +struct sha2_256_state hash; +MPI s; +int rc; + + s =mpi_read_raw_data (sig, siglen); +if ( !s ) + { + printk(XENLOG_ERR LIVEPATCH "Failed to mpi_read_raw_data\n"); +return -ENOMEM ; + } + +sha2_256_init (&hash); +sha2_256_update (&hash, data, datalen); + + rc =rsa_sha256_verify (&builtin_payload_key, &hash, s); +if ( rc ) + printk(XENLOG_ERR LIVEPATCH "rsa_sha256_verify failed: %d\n", rc); + +mpi_free (s); + +return rc; +} +#endif + +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size) +{ +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 +static const char notename[] ="Xen" ; +void *sig; +livepatch_elf_note note; +int rc; + + rc =livepatch_elf_note_by_names (elf,ELF_XEN_SIGNATURE , notename,-1 , + ¬e); +if ( rc ) + { +dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: Signature not present\n" , + elf->name); +return rc; + } + +/* We expect only one signature, find a second is an error! */ + rc =livepatch_elf_next_note_by_name (notename,-1 , ¬e); +if ( rc != -ENOENT ) + {
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 589
Quoted 10 lines
xen/common/livepatch.c +10
-0+if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH + "Error while checking for notes! err = %d\n", rc); +return rc; + } +else + { + printk(XENLOG_ERR LIVEPATCH + "Error, found second signature note! There can be only one!\n");
FWIW, it's a nit, but I think there are too many exclamation marks in the error message above:
"Error, multiple signatures found\n"
Would be better.
I'm also wondering, would it make sense to allow multiple signatures to be present? I guess not because livepatches are built for specific Xen binaries, and then we know exactly which key is embedded there. A livepatch would never apply to two different builds with different keys.
Quoted 26 lines
xen/common/livepatch.c +26
-0+return -EINVAL ; + } + } + +if (SIGNATURE_VERSION (note.type) !=LIVEPATCH_SIGNATURE_VERSION || +SIGNATURE_ALGORITHM (note.type) !=SIGNATURE_ALGORITHM_RSA || +SIGNATURE_HASH (note.type) !=SIGNATURE_HASH_SHA256 ) + { + printk(XENLOG_ERR LIVEPATCH +"Unsupported signature type: v:%u, a:%u, h:%u\n" , +SIGNATURE_VERSION (note.type),SIGNATURE_ALGORITHM (note.type), + SIGNATURE_HASH(note.type)); +return -EINVAL ; + } + +if ( note.size ==0 || note.size >=MAX_SIG_NOTE_SIZE ) + { + printk(XENLOG_ERR LIVEPATCH "Invalid signature note size: %u\n", + note.size); +return -EINVAL ; + } + + sig =xmalloc_bytes (note.size); +if ( !sig ) +return -ENOMEM ; +
xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 625
Quoted 10 lines
xen/common/livepatch.c +10
-0+memcpy (sig, note.data, note.size); + +/* Remove signature from data, as can't be verified with it. */ +memset ((void *)note.data,0 , note.size); + rc =check_rsa_sha256_signature (raw, size, sig, note.size); + +xfree (sig); +return rc; +#else +return -EINVAL ;
EOPNOTSUPP would be more accurate here.
xen/common/livepatch.c static int load_payload_data(struct payload *payload, void *raw, size_t len) · line 1266
Quoted 11 lines
xen/common/livepatch.c +4
-0+#endif +} +static int check_special_sections (const struct livepatch_elf *elf) {unsigned int i;@@ -1162,6 +1263,8 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)if ( rc ) goto out; +check_signature (&elf, raw, len);
Wouldn't it make more sense to unconditionally fail here if CONFIG_PAYLOAD_VERIFY is enabled and signature verification fails?
IOW: if you built an hypervisor with PAYLOAD_VERIFY enabled signature verification needs to be enforced.
Quoted 31 lines
xen/common/livepatch.c +1
-0+ rc =move_payload (payload, &elf);if ( rc ) goto out;xen/common/livepatch_elf.c +19
-0@@ -23,6 +23,61 @@ livepatch_elf_sec_by_name(const struct livepatch_elf *elf,return NULL ; } +int livepatch_elf_note_by_names (const struct livepatch_elf *elf, +const char *sec_name,const char *note_name, +const unsigned int type, +livepatch_elf_note *note) +{ +const struct livepatch_elf_sec *sec =livepatch_elf_sec_by_name (elf, + sec_name); +if ( !sec ) +return -ENOENT ; + + note->end = sec->addr + sec->sec->sh_size; + note->next = sec->addr; + +return livepatch_elf_next_note_by_name (note_name, type, note); +} + +int livepatch_elf_next_note_by_name (const char *note_name, +const unsigned int type, +livepatch_elf_note *note)
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 54
Quoted 10 lines
xen/common/livepatch_elf.c +10
-0+{ +const Elf_Note *pkd_note = note->next; +size_t notenamelen =strlen (note_name) +1 ; +size_t note_hd_size; +size_t note_size; +size_t remaining; + +while ( (void *)pkd_note < note->end ) + { +
Stray newline?
remaining and note_hd_size can be defined inside of the loop to reduce the scope.
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 60
Quoted 6 lines
xen/common/livepatch_elf.c +6
-0+ remaining = note->end - (void *)pkd_note; +if ( remaining <sizeof (livepatch_elf_note) ) +return -EINVAL ; + + note_hd_size =sizeof (Elf_Note) + ((pkd_note->namesz +3 ) & ~0x3 ); + note_size = note_hd_size + ((pkd_note->descsz +3 ) & ~0x3 );
Could be have a macro or similar for this ((x + 3) & ~0x3) manipulation?
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 62
Quoted 2 lines
xen/common/livepatch_elf.c +2
-0+if ( remaining < note_size ) +return -EINVAL ;
ENOSPC might be less ambiguous than EINVAL for both the above?
Quoted 4 lines
xen/common/livepatch_elf.c +4
-0+ + if ( notenamelen == pkd_note->namesz && + !memcmp(note_name, (const void *) pkd_note + sizeof(Elf_Note), + notenamelen) &&
xen/common/livepatch_elf.c livepatch_elf_sec_by_name(const struct livepatch_elf *elf, · line 76
Quoted 10 lines
xen/common/livepatch_elf.c +10
-0+ (type == -1 || pkd_note->type == type) ) + { + note->size = pkd_note->descsz; + note->type = pkd_note->type; + note->data = (void *)pkd_note + note_hd_size; + note->next = (void *)pkd_note + note_size; +return 0 ; + } + pkd_note = (void *)pkd_note + note_size; + }
It's a nit, but I would write this as a for loop, as it's clearer then the index advance:
for ( pkd_note = note->next; (void *)pkd_note < note->end;
pkd_note = (void *)pkd_note + note_size )
{
...
Quoted 21 lines
xen/common/livepatch_elf.c +4
-0+ +return -ENOENT ; +} +static int elf_verify_strtab (const struct livepatch_elf_sec *sec) {const Elf_Shdr *s;xen/include/xen/livepatch.h +1
-0@@ -38,6 +38,7 @@ struct xen_sysctl_livepatch_op;#define ELF_LIVEPATCH_DEPENDS ".livepatch.depends"#define ELF_LIVEPATCH_XEN_DEPENDS ".livepatch.xen_depends"#define ELF_BUILD_ID_NOTE ".note.gnu.build-id" +#define ELF_XEN_SIGNATURE ".note.Xen.signature"#define ELF_LIVEPATCH_LOAD_HOOKS ".livepatch.hooks.load"#define ELF_LIVEPATCH_UNLOAD_HOOKS ".livepatch.hooks.unload"#define ELF_LIVEPATCH_PREAPPLY_HOOK ".livepatch.hooks.preapply"@@ -49,6 +50,15 @@ struct xen_sysctl_livepatch_op;/* Arbitrary limit for payload size and .bss section size. */
xen/include/xen/livepatch.h struct xen_sysctl_livepatch_op; · line 60
Quoted 10 lines
xen/include/xen/livepatch.h +8
-0#define LIVEPATCH_MAX_SIZE MB(2) +#define SIGNATURE_VERSION (type) ((type) & 0xffff) +#define LIVEPATCH_SIGNATURE_VERSION 1 + +#define SIGNATURE_ALGORITHM (type) (((type) >> 16) & 0xff) +#define SIGNATURE_ALGORITHM_RSA 0 + +#define SIGNATURE_HASH (type) (((type) >> 24) & 0xff) +#define SIGNATURE_HASH_SHA256 0
You could possibly use MASK_EXTR() for the macros above?
Thanks, Roger.
Re: [PATCH v3 5/5] livepatch: Verify livepatch signatures
Quoted 109 lines · Roger Pau Monné
Quoted 34 lines · Ross Lagerwall
From: Jennifer Herbert <[email protected]>
Verify livepatch signatures against the embedded public key in Xen. Failing to verify does not prevent the livepatch from being loaded. In future, this will be changed for certain cases (e.g. when Secure Boot is enabled).
Signed-off-by Jennifer Herbert <[email protected]>Signed-off-by Ross Lagerwall <[email protected]>---
In v3:
* Minor style fixes
xen/common/livepatch.c | 103 ++++++++++++++++++++++++++++++++ xen/common/livepatch_elf.c | 55 +++++++++++++++++ xen/include/xen/livepatch.h | 10 ++++ xen/include/xen/livepatch_elf.h | 18 ++++++ 4 files changed, 186 insertions(+)xen/common/livepatch.c +1
-0@@ -14,6 +14,7 @@#include <xen/mpi.h> #include <xen/rsa.h> #include <xen/sched.h> +#include <xen/sha2.h> #include <xen/smp.h> #include <xen/softirq.h> #include <xen/spinlock.h> xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 531
Quoted 6 lines
xen/common/livepatch.c +3
-0@@ -525,6 +526,106 @@ static int check_xen_buildid(const struct livepatch_elf *elf)return 0 ; } +#ifdef CONFIG_PAYLOAD_VERIFY +static int check_rsa_sha256_signature (void *data,size_t datalen, +void *sig,uint32_t siglen)I think both data and sig could be const here?
Quoted 48 lines
xen/common/livepatch.c +48
-0+{ +struct sha2_256_state hash; +MPI s; +int rc; + + s =mpi_read_raw_data (sig, siglen); +if ( !s ) + { + printk(XENLOG_ERR LIVEPATCH "Failed to mpi_read_raw_data\n"); +return -ENOMEM ; + } + +sha2_256_init (&hash); +sha2_256_update (&hash, data, datalen); + + rc =rsa_sha256_verify (&builtin_payload_key, &hash, s); +if ( rc ) + printk(XENLOG_ERR LIVEPATCH "rsa_sha256_verify failed: %d\n", rc); + +mpi_free (s); + +return rc; +} +#endif + +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size) +{ +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 +static const char notename[] ="Xen" ; +void *sig; +livepatch_elf_note note; +int rc; + + rc =livepatch_elf_note_by_names (elf,ELF_XEN_SIGNATURE , notename,-1 , + ¬e); +if ( rc ) + { +dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: Signature not present\n" , + elf->name); +return rc; + } + +/* We expect only one signature, find a second is an error! */ + rc =livepatch_elf_next_note_by_name (notename,-1 , ¬e); +if ( rc != -ENOENT ) + {xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 589
Quoted 10 lines
xen/common/livepatch.c +10
-0+if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH + "Error while checking for notes! err = %d\n", rc); +return rc; + } +else + { + printk(XENLOG_ERR LIVEPATCH + "Error, found second signature note! There can be only one!\n");FWIW, it's a nit, but I think there are too many exclamation marks in the error message above:
"Error, multiple signatures found\n"
Quoted 7 lines
Would be better.
I'm also wondering, would it make sense to allow multiple signatures to be present? I guess not because livepatches are built for specific Xen binaries, and then we know exactly which key is embedded there. A livepatch would never apply to two different builds with different keys.
I agree that while it could be possible, I don't really see a valid use for it. This restriction could be relaxed in future if it is deemed useful.
Quoted 50 lines
Quoted 27 lines
xen/common/livepatch.c +26
-0+return -EINVAL ; + } + } + +if (SIGNATURE_VERSION (note.type) !=LIVEPATCH_SIGNATURE_VERSION || +SIGNATURE_ALGORITHM (note.type) !=SIGNATURE_ALGORITHM_RSA || +SIGNATURE_HASH (note.type) !=SIGNATURE_HASH_SHA256 ) + { + printk(XENLOG_ERR LIVEPATCH +"Unsupported signature type: v:%u, a:%u, h:%u\n" , +SIGNATURE_VERSION (note.type),SIGNATURE_ALGORITHM (note.type), + SIGNATURE_HASH(note.type)); +return -EINVAL ; + } + +if ( note.size ==0 || note.size >=MAX_SIG_NOTE_SIZE ) + { + printk(XENLOG_ERR LIVEPATCH "Invalid signature note size: %u\n", + note.size); +return -EINVAL ; + } + + sig =xmalloc_bytes (note.size); +if ( !sig ) +return -ENOMEM ; +xen/common/livepatch.c static int check_xen_buildid(const struct livepatch_elf *elf) · line 625
Quoted 10 lines
xen/common/livepatch.c +10
-0+memcpy (sig, note.data, note.size); + +/* Remove signature from data, as can't be verified with it. */ +memset ((void *)note.data,0 , note.size); + rc =check_rsa_sha256_signature (raw, size, sig, note.size); + +xfree (sig); +return rc; +#else +return -EINVAL ;EOPNOTSUPP would be more accurate here.
Quoted 10 lines
xen/common/livepatch.c +3
-0+#endif +} +static int check_special_sections (const struct livepatch_elf *elf) {unsigned int i;@@ -1162,6 +1263,8 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)if ( rc ) goto out;
Quoted 7 lines
xen/common/livepatch.c static int load_payload_data(struct payload *payload, void *raw, size_t len) · line 1266
Quoted 1 line
xen/common/livepatch.c +1
-0+check_signature (&elf, raw, len);Wouldn't it make more sense to unconditionally fail here if
CONFIG_PAYLOAD_VERIFYis enabled and signature verification fails?IOW: if you built an hypervisor with
PAYLOAD_VERIFYenabled signature verification needs to be enforced.
As per the commit message, this doesn't actually enforce anything yet. The intention is to tie signature enforcement to lockdown mode once those patches have been merged.
Ross