LivePatch signing support
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.
Note that this series depends on another patch that has not yet been merged:
xen/lib: Export additional sha256 functions
https://lists.xenproject.org/archives/html/xen-devel/2025-05/msg00222.html
Jennifer Herbert (1):
livepatch: Verify livepatch signatures
Kevin Lampis (1):
livepatch: Embed public key in Xen
Ross Lagerwall (2):
docs: Introduce live patch signing
crypto: Add RSA support
docs/misc/livepatch.pandoc | 104 +- xen/common/Kconfig | 18 + xen/common/Makefile | 1 + xen/common/livepatch.c | 175 ++++ xen/common/livepatch_elf.c | 55 + xen/common/mpi.c | 1724 +++++++++++++++++++++++++++++++ xen/crypto/Makefile | 13 + xen/crypto/rsa.c | 194 ++++ xen/include/xen/livepatch.h | 5 + xen/include/xen/livepatch_elf.h | 18 + xen/include/xen/mpi.h | 63 ++ xen/include/xen/rsa.h | 72 ++ xen/tools/extract-key.py | 37 + 13 files changed, 2427 insertions(+), 52 deletions(-) create mode 100644 xen/common/mpi.c 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 100755 xen/tools/extract-key.py
Signature · 1 line
2.49.0
PATCH 1/4 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.
--- docs/misc/livepatch.pandoc | 104 ++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 52 deletions(-)
docs/misc/livepatch.pandoc +52 -52
@@ -917,6 +917,58 @@ 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` +and type `0`. + +The note data shall contain a header followed by the signature data: + + #define SIGNATURE_SUPPORTED_VERION 1 + #define SIGNATURE_ALGORITHM_RSA 0 + #define SIGNATURE_HASH_SHA256 0 + struct payload_signature { + uint16_t version; + uint8_t algo; /* Public-key crypto algorithm */ + uint8_t hash; /* Digest algorithm */ + uint32_t sig_len; /* Length of signature data */ + }; + +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 +1230,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
Re: [PATCH 1/4] docs: Introduce live patch signing
Quoted 16 lines · Ross Lagerwall
docs/misc/livepatch.pandoc +10
-0@@ -917,6 +917,58 @@ 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
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 939
Quoted 10 lines
docs/misc/livepatch.pandoc +10
-0+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` +and type `0`. + +The note data shall contain a header followed by the signature data: + + #define SIGNATURE_SUPPORTED_VERION 1
I don't think this is a good name (leaving aside the typo); conceptually multiple versions could be supported. Otoh live patches are hypervisor build specific anyway, so it's hard to see why a version would be needed in the first place. Alternatively should version or ...
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 941
Quoted 2 lines
docs/misc/livepatch.pandoc +2
-0+ #define SIGNATURE_ALGORITHM_RSA 0 + #define SIGNATURE_HASH_SHA256 0
... these two be encoded in the note's type, instead of leaving that effectively unused?
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 946
Quoted 5 lines
docs/misc/livepatch.pandoc +5
-0+ struct payload_signature { + uint16_t version; + uint8_t algo; /* Public-key crypto algorithm */ + uint8_t hash; /* Digest algorithm */ + uint32_t sig_len; /* Length of signature data */
Should there be a minimum length specified, to ensure security at least for the moment (before e.g. quantum computers can break things)?
Quoted 10 lines
docs/misc/livepatch.pandoc +10
-0+ }; + +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
docs/misc/livepatch.pandoc The normal sequence of events is to:
Quoted 10 lines
docs/misc/livepatch.pandoc +10
-0+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.
And this is sufficient to satisfy (Microsoft's?) requirements?
Jan
Re: [PATCH 1/4] docs: Introduce live patch signing
Quoted 25 lines · Jan Beulich
Quoted 23 lines · Ross Lagerwall
docs/misc/livepatch.pandoc +17
-0@@ -917,6 +917,58 @@ 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` +and type `0`. +
Quoted 8 lines
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 939
Quoted 3 lines
docs/misc/livepatch.pandoc +3
-0+The note data shall contain a header followed by the signature data: + + #define SIGNATURE_SUPPORTED_VERION 1I don't think this is a good name (leaving aside the typo); conceptually multiple versions could be supported. Otoh live patches are hypervisor build specific anyway, so it's hard to see why a version would be needed in the first place. Alternatively should version or ...
How about LIVEPATCH_SIGNATURE_VERSION (analogous to LIVEPATCH_PAYLOAD_VERSION)?
I think keeping the version is harmless and may cover some future scenario I haven't considered, even if it is not strictly necessary at the moment.
Quoted 6 lines
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 941
Quoted 3 lines
docs/misc/livepatch.pandoc +2
-0+ #define SIGNATURE_ALGORITHM_RSA 0 + #define SIGNATURE_HASH_SHA256 0... these two be encoded in the note's type, instead of leaving that effectively unused?
That's a good idea. The version, algorithm, and hash can be encoded in the type and the signature length can be encoded in the note descriptor length and that would allow removing the signature header completely.
Quoted 9 lines
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 946
Quoted 6 lines
docs/misc/livepatch.pandoc +5
-0+ struct payload_signature { + uint16_t version; + uint8_t algo; /* Public-key crypto algorithm */ + uint8_t hash; /* Digest algorithm */ + uint32_t sig_len; /* Length of signature data */Should there be a minimum length specified, to ensure security at least for the moment (before e.g. quantum computers can break things)?
I'd prefer to leave that policy to the distros who use this functionality rather than trying to keep up with what is currently considered secure.
Quoted 15 lines
Quoted 15 lines
docs/misc/livepatch.pandoc +14
-0+ }; + +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.
Quoted 9 lines
docs/misc/livepatch.pandoc The normal sequence of events is to:
Quoted 6 lines
docs/misc/livepatch.pandoc +6
-0+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.And this is sufficient to satisfy (Microsoft's?) requirements?
To my knowledge Microsoft has not stated explicitly how various components in the OS should be verified / secured. However, their current requirement is that firmware is verified using at least RSA-2048 with SHA-256 so with a sufficiently large key this would cover that, assuming they apply the same requirement to the OS.
Ross
Re: [PATCH 1/4] docs: Introduce live patch signing
Quoted 35 lines · Ross Lagerwall
Quoted 25 lines · Jan Beulich
Quoted 23 lines · Ross Lagerwall
docs/misc/livepatch.pandoc +17
-0@@ -917,6 +917,58 @@ 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` +and type `0`. +Quoted 8 lines
docs/misc/livepatch.pandoc The normal sequence of events is to: · line 939
Quoted 3 lines
docs/misc/livepatch.pandoc +3
-0+The note data shall contain a header followed by the signature data: + + #define SIGNATURE_SUPPORTED_VERION 1I don't think this is a good name (leaving aside the typo); conceptually multiple versions could be supported. Otoh live patches are hypervisor build specific anyway, so it's hard to see why a version would be needed in the first place. Alternatively should version or ...
Quoted 2 lines
How about
LIVEPATCH_SIGNATURE_VERSION(analogous toLIVEPATCH_PAYLOAD_VERSION)?
Fine with me.
Jan
PATCH 2/4 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/*).
--- xen/common/Makefile | 1 + xen/common/mpi.c | 1724 +++++++++++++++++++++++++++++++++++++++++ xen/crypto/Makefile | 1 + xen/crypto/rsa.c | 194 +++++ xen/include/xen/mpi.h | 63 ++ xen/include/xen/rsa.h | 72 ++ 6 files changed, 2055 insertions(+) create mode 100644 xen/common/mpi.c create mode 100644 xen/crypto/rsa.c create mode 100644 xen/include/xen/mpi.h create mode 100644 xen/include/xen/rsa.h
xen/common/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o +obj-y += mpi.o obj-y += multicall.o obj-y += notifier.o obj-$(CONFIG_NUMA) += numa.o
xen/common/mpi.c new file +1724 -0
1,724 lines not on this page · raw message
xen/crypto/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-y += rijndael.o +obj-y += rsa.o obj-y += vmac.o
xen/crypto/rsa.c new file +194 -0
194 lines not on this page · raw message
xen/include/xen/mpi.h new file +63 -0
63 lines not on this page · raw message
xen/include/xen/rsa.h new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH 2/4] crypto: Add RSA support
Quoted 2 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
If this is needed just for live-patch, ...
xen/common/Makefile obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o · line 31
Quoted 7 lines
xen/common/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o +obj-y += mpi.o
... why would this be obj-y? Same for rsa.o further down.
xen/common/mpi.c line 1
Quoted 4 lines
xen/common/mpi.c new file +1
-0@@ -0,0 +1,1724 @@ +/* mpi-pow.c - MPI functions
Nit: This doesn't match the filename.
Quoted 17 lines
xen/common/mpi.c new file +17
-0+* Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc. +* +* This file is part of GnuPG. +* +* GnuPG is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* GnuPG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
xen/common/mpi.c line 28
Quoted 10 lines
xen/common/mpi.c new file +10
-0+* +* Note: This code is heavily based on the GNU MP Library. +* Actually it's the same code with only minor changes in the +* way the data is stored; this is to support the abstraction +* of an optional secure memory allocation which may be used +* to avoid revealing of sensitive data due to paging etc. +* The GNU MP Library itself is published under the LGPL; +* however I decided to publish this code under the plain GPL. +* +* mpi.c code extracted from linux @ eef0df6a5953, lib/mpi
I fear this line would go unnoticed with future changes, and hence go stale rather easily. Menioning the origin in just the commit message ought to be sufficient.
Btw - how heavily did you need to adjust the code to pass our Eclair scan? Depending on that I then might raise the question of converting to proper Xen style (it currently isn't even proper Linux style, afaict).
xen/common/mpi.c line 37
Quoted 9 lines
xen/common/mpi.c new file +9
-0+*/ + +#include <xen/mpi.h> +#include <xen/lib.h> +#include <xen/err.h> +#include <xen/xmalloc.h> +#include <xen/string.h> +#include <xen/bitops.h> +#include <xen/bug.h>
Please sort alphabetically. Same for the other new files.
Quoted 5 lines
xen/common/mpi.c new file +5
-0+#define MAX_EXTERN_MPI_BITS 16384 + +/* Define it to a value which is good on most machines. +* tested 4, 16, 32 and 64, where 16 gave the best performance when +* checking a 768 and a 1024 bit ElGamal signature.
xen/common/mpi.c line 53
Quoted 10 lines
xen/common/mpi.c new file +10
-0+* (wk 22.12.97) */ +#define KARATSUBA_THRESHOLD 16 + +typedef mpi_limb_t *mpi_ptr_t ;/* pointer to a limb */ +typedef int mpi_size_t ;/* (must be a signed type) */ + +/* Copy N limbs from S to D. */ +#define MPN_COPY (d, s, n) \ + do { \ + mpi_size_t _i; \
With this and ...
xen/common/mpi.c line 60
Quoted 7 lines
xen/common/mpi.c new file +7
-0+for (_i =0 ; _i < (n); _i++) \ + (d)[_i] = (s)[_i]; \ + } while (0) + +#define MPN_COPY_DECR (d, s, n) \ + do { \ + mpi_size_t _i; \
... this I wonder why ...
xen/common/mpi.c line 68
Quoted 8 lines
xen/common/mpi.c new file +8
-0+for (_i = (n)-1 ; _i >=0 ; _i--) \ + (d)[_i] = (s)[_i]; \ + }while (0 ) + +/* Zero N limbs at D */ +#define MPN_ZERO (d, n) \ + do { \ + int _i; \
... this is plain int. There are apparently many similar issue.
Quoted 2 lines
[...]
+leave:
Here and elsewhere - labels indented by at least one blank, please.
Quoted 165 lines
xen/crypto/rsa.c new file +162
-0@@ -0,0 +1,194 @@ +/* rsa.c + +The RSA publickey algorithm. + +Copyright (C) 2001 Niels Möller + +This file is part of GNU Nettle. + +GNU Nettle is free software: you can redistribute it and/or +modify it under the terms of either: + +* the GNU Lesser General Public License as published by the Free +Software Foundation; either version 3 of the License, or (at your +option) any later version. + +or + +* the GNU General Public License as published by the Free +Software Foundation; either version 2 of the License, or (at your +option) any later version. + +or both in parallel, as here. + +GNU Nettle is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received copies of the GNU General Public License and +the GNU Lesser General Public License along with this program. If +not, see http://www.gnu.org/licenses/. +*/ + +#include <xen/rsa.h> +#include <xen/lib.h> +#include <xen/err.h> +#include <xen/bug.h> +#include <xen/string.h> + +void rsa_public_key_init (struct rsa_public_key *key) +{ + key->n =NULL ; + key->e =NULL ; + key->size =0 ; +} + +/* +* Computes the size, in octets, of the modulo. Returns 0 if the +* modulo is too small to be useful, or otherwise appears invalid. +*/ +static size_t rsa_check_size (MPI n) +{ +/* Round upwards */ +size_t size; + +/* Even moduli are invalid */ +if (mpi_test_bit (n,0 ) ==0 ) +return 0 ; + + size = (mpi_get_nbits (n) +7 ) /8 ; + +if ( size <RSA_MINIMUM_N_OCTETS ) +return 0 ; + +return size; +} + +int rsa_public_key_prepare (struct rsa_public_key *key) +{ +if ( !key->n || !key->e || key->size ) +return -EINVAL ; + + key->size =rsa_check_size (key->n); + +return key->size >0 ?0 : -EINVAL ; +} + +/* +* Formats the PKCS#1 padding, of the form +* +* 0x00 0x01 0xff ... 0xff 0x00 id ...digest... +* +* where the 0xff ... 0xff part consists of at least 8 octets. The +* total size equals the octet size of n. +*/ +static uint8_t *pkcs1_signature_prefix (unsigned int key_size,uint8_t *buffer, +unsigned int id_size,const uint8_t *id, +unsigned int digest_size) +{ +unsigned int j; + +if ( key_size <11 + id_size + digest_size ) +return NULL ; + + j = key_size - digest_size - id_size; + +memcpy (buffer + j, id, id_size); + buffer[0 ] =0 ; + buffer[1 ] =1 ; + buffer[j -1 ] =0 ; + +ASSERT (j >=11 ); +memset (buffer +2 ,0xff , j -3 ); + +return buffer + j + id_size; +} + +/* +* From RFC 3447, Public-Key Cryptography Standards (PKCS) #1: RSA +* Cryptography Specifications Version 2.1. +* +* id-sha256 OBJECT IDENTIFIER ::= +* {joint-iso-itu-t(2) country(16) us(840) organization(1) +* gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1} +*/ +static const uint8_t +sha256_prefix[] = +{ +/* 19 octets prefix, 32 octets hash, total 51 */ +0x30 ,49 ,/* SEQUENCE */ +0x30 ,13 ,/* SEQUENCE */ +0x06 ,9 ,/* OBJECT IDENTIFIER */ +0x60 ,0x86 ,0x48 ,0x01 ,0x65 ,0x03 ,0x04 ,0x02 ,0x01 , +0x05 ,0 ,/* NULL */ +0x04 ,32 /* OCTET STRING */ +/* Here comes the raw hash value */ +}; + +static int pkcs1_rsa_sha256_encode (MPI *m,size_t key_size, +struct sha2_256_state *hash) +{ +uint8_t *ptr; +uint8_t *buf; + + buf =xmalloc_bytes (key_size); +if ( !buf ) +return -ENOMEM ; + + ptr =pkcs1_signature_prefix (key_size, buf, +sizeof (sha256_prefix), sha256_prefix, +SHA2_256_DIGEST_SIZE ); +if ( !ptr ) + { +xfree (buf); +return -EINVAL ; + } + +sha2_256_final (hash, ptr); + *m =mpi_read_raw_data (buf, key_size); +xfree (buf); +return 0 ; +} + +static int rsa_verify (const struct rsa_public_key *key,MPI m,MPI s) +{ +int ret; +MPI m1; + +/* (1) Validate 0 <= s < n */ +if (mpi_cmp_ui (s,0 ) <0 ||mpi_cmp (s, key->n) >=0 ) +return -EINVAL ; +
xen/crypto/rsa.c line 172
Quoted 10 lines
xen/crypto/rsa.c new file +10
-0+ m1 =mpi_alloc (key->size /BYTES_PER_MPI_LIMB ); +if ( !m1 ) +return -ENOMEM ; + +/* (2) m = s^e mod n */ + ret =mpi_powm (m1, s, key->e, key->n); +if ( ret ) + goto out; + + ret =mpi_cmp (m, m1) ? -EINVAL :0 ;
You look to have striven to convert this file to Xen style; there's a stray blank here, though.
xen/include/xen/mpi.h line 10
Quoted 13 lines
xen/include/xen/mpi.h new file +10
-0@@ -0,0 +1,63 @@ +/* mpi.h - Multi Precision Integers +* Copyright (C) 1994, 1996, 1998, 1999, +* 2000, 2001 Free Software Foundation, Inc. +* +* This file is part of GNUPG. +* +* GNUPG is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version.
While this may want to remain, accompany it with an SPDX header? (Same elsewhere perhaps.)
Quoted 10 lines
xen/include/xen/mpi.h new file +10
-0+* GNUPG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +* +* Note: This code is heavily based on the GNU MP Library.
xen/include/xen/mpi.h line 31
Quoted 10 lines
xen/include/xen/mpi.h new file +10
-0+* Actually it's the same code with only minor changes in the +* way the data is stored; this is to support the abstraction +* of an optional secure memory allocation which may be used +* to avoid revealing of sensitive data due to paging etc. +* The GNU MP Library itself is published under the LGPL; +* however I decided to publish this code under the plain GPL. +*/ + +#ifndef MPI_H +#define MPI_H
This imo wants at least a XEN_ prefix. Same in rsa.h.
xen/include/xen/mpi.h line 44
Quoted 12 lines
xen/include/xen/mpi.h new file +12
-0+#include <xen/types.h> + +#define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8) +#define BITS_PER_MPI_LIMB BITS_PER_LONG + +typedef unsigned long int mpi_limb_t ; +typedef signed long int mpi_limb_signed_t ; + +struct mpi { +int alloced;/* array size (# of allocated limbs) */ +int nlimbs;/* number of valid limbs */ +int nbits;/* the real number of valid bits (info only) */
I understand the goal likely is to not meaningfully change the original, but none of these look like they can hold negative values, and ...
xen/include/xen/mpi.h line 45
Quoted 1 line
xen/include/xen/mpi.h new file +1
-0+int sign;/* indicates a negative number */
... this one looks like it's a boolean.
xen/include/xen/mpi.h line 46
Quoted 1 line
xen/include/xen/mpi.h new file +1
-0+unsigned flags;/* bit 0: array must be allocated in secure memory space */
In Xen we use "unsigned int", not plain "unsigned".
xen/include/xen/mpi.h line 48
Quoted 2 lines
xen/include/xen/mpi.h new file +2
-0+/* bit 1: not used */ +/* bit 2: the limb is a pointer to some m_alloced data */
I think these comments would better be padded to align with the earlier one. The individual bits likely also want corresponding #define-s.
Quoted 31 lines
xen/include/xen/rsa.h new file +28
-0@@ -0,0 +1,72 @@ +/* rsa.h + +The RSA publickey algorithm. + +Copyright (C) 2001, 2002 Niels Möller + +This file is part of GNU Nettle. + +GNU Nettle is free software: you can redistribute it and/or +modify it under the terms of either: + +* the GNU Lesser General Public License as published by the Free +Software Foundation; either version 3 of the License, or (at your +option) any later version. + +or + +* the GNU General Public License as published by the Free +Software Foundation; either version 2 of the License, or (at your +option) any later version. + +or both in parallel, as here. + +GNU Nettle is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. +
xen/include/xen/rsa.h line 38
Quoted 10 lines
xen/include/xen/rsa.h new file +10
-0+You should have received copies of the GNU General Public License and +the GNU Lesser General Public License along with this program. If +not, see http://www.gnu.org/licenses/. +*/ + +#ifndef RSA_H +#define RSA_H + +#include <xen/mpi.h> +#include <xen/sha2.h>
This isn't needed here, is it? All you need is ...
Quoted 21 lines
xen/include/xen/rsa.h new file +21
-0+#include <xen/types.h> + +/* +* This limit is somewhat arbitrary. Technically, the smallest modulo +* which makes sense at all is 15 = 3*5, phi(15) = 8, size 4 bits. But +* for ridiculously small keys, not all odd e are possible (e.g., for +* 5 bits, the only possible modulo is 3*7 = 21, phi(21) = 12, and e = +* 3 don't work). The smallest size that makes sense with pkcs#1, and +* which allows RSA encryption of one byte messages, is 12 octets, 89 +* bits. +*/ +#define RSA_MINIMUM_N_OCTETS 12 +#define RSA_MINIMUM_N_BITS (8 * RSA_MINIMUM_N_OCTETS - 7) + +struct rsa_public_key +{ +/* +* Size of the modulo, in octets. This is also the size of all +* signatures that are created or verified with this key. +*/ +size_t size;
xen/include/xen/rsa.h line 69
Quoted 10 lines
xen/include/xen/rsa.h new file +10
-0+MPI n;/* Modulo */ +MPI e;/* Public exponent */ +}; + +void rsa_public_key_init (struct rsa_public_key *key); + +int rsa_public_key_prepare (struct rsa_public_key *key); + +int rsa_sha256_verify(const struct rsa_public_key *key, + struct sha2_256_state *hash,
... a forward decl of this type.
Jan
Re: [PATCH 2/4] crypto: Add RSA support
Quoted 27 lines · Jan Beulich
Quoted 19 lines
+ * Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * Note: This code is heavily based on the GNU MP Library.Quoted 8 lines
+ * Actually it's the same code with only minor changes in the
+ * way the data is stored; this is to support the abstraction
+ * of an optional secure memory allocation which may be used
+ * to avoid revealing of sensitive data due to paging etc.
+ * The GNU MP Library itself is published under the LGPL;
+ * however I decided to publish this code under the plain GPL.
+ *
+ *mpi.ccode extracted from linux @eef0df6a5953, lib/mpi
Quoted 7 lines
I fear this line would go unnoticed with future changes, and hence go stale rather easily. Menioning the origin in just the commit message ought to be sufficient.
Btw - how heavily did you need to adjust the code to pass our Eclair scan? Depending on that I then might raise the question of converting to proper Xen style (it currently isn't even proper Linux style, afaict).
I've put this through Eclair. Single R16.3 (missing break) violation. Happy otherwise.
~Andrew
Re: [PATCH 2/4] crypto: Add RSA support
In reply to Jan BeulichQuoted 52 lines · Jan Beulich
Quoted 2 lines · Ross Lagerwall
In preparation for adding support for livepatch signing, add support for RSA crypto.
If this is needed just for live-patch, ...
xen/common/Makefile obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o · line 31
Quoted 7 lines
xen/common/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o +obj-y += mpi.o... why would this be obj-y? Same for rsa.o further down.
xen/common/mpi.c line 1
Quoted 4 lines
xen/common/mpi.c new file +1
-0@@ -0,0 +1,1724 @@ +/* mpi-pow.c - MPI functions Nit: This doesn't match the filename.
Quoted 17 lines
xen/common/mpi.c new file +17
-0+* Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc. +* +* This file is part of GnuPG. +* +* GnuPG is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* GnuPG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA xen/common/mpi.c line 28
Quoted 10 lines
xen/common/mpi.c new file +10
-0+* +* Note: This code is heavily based on the GNU MP Library. +* Actually it's the same code with only minor changes in the +* way the data is stored; this is to support the abstraction +* of an optional secure memory allocation which may be used +* to avoid revealing of sensitive data due to paging etc. +* The GNU MP Library itself is published under the LGPL; +* however I decided to publish this code under the plain GPL. +* +* mpi.c code extracted from linux @ eef0df6a5953, lib/mpi
Quoted 7 lines
I fear this line would go unnoticed with future changes, and hence go stale rather easily. Menioning the origin in just the commit message ought to be sufficient.
Btw - how heavily did you need to adjust the code to pass our Eclair scan? Depending on that I then might raise the question of converting to proper Xen style (it currently isn't even proper Linux style, afaict).
Few changes were needed to the code in this file so I preferred to keep it in the original style.
Since a lot more adjustments were made to rsa.c, I opted to change that to the Xen style.
Quoted 236 lines
xen/common/mpi.c line 37
Quoted 10 lines
xen/common/mpi.c new file +9
-0+*/ + +#include <xen/mpi.h> +#include <xen/lib.h> +#include <xen/err.h> +#include <xen/xmalloc.h> +#include <xen/string.h> +#include <xen/bitops.h> +#include <xen/bug.h> Please sort alphabetically. Same for the other new files.
Quoted 5 lines
xen/common/mpi.c new file +5
-0+#define MAX_EXTERN_MPI_BITS 16384 + +/* Define it to a value which is good on most machines. +* tested 4, 16, 32 and 64, where 16 gave the best performance when +* checking a 768 and a 1024 bit ElGamal signature. xen/common/mpi.c line 53
Quoted 10 lines
xen/common/mpi.c new file +10
-0+* (wk 22.12.97) */ +#define KARATSUBA_THRESHOLD 16 + +typedef mpi_limb_t *mpi_ptr_t ;/* pointer to a limb */ +typedef int mpi_size_t ;/* (must be a signed type) */ + +/* Copy N limbs from S to D. */ +#define MPN_COPY (d, s, n) \ + do { \ + mpi_size_t _i; \With this and ...
xen/common/mpi.c line 60
Quoted 7 lines
xen/common/mpi.c new file +7
-0+for (_i =0 ; _i < (n); _i++) \ + (d)[_i] = (s)[_i]; \ + } while (0) + +#define MPN_COPY_DECR (d, s, n) \ + do { \ + mpi_size_t _i; \... this I wonder why ...
xen/common/mpi.c line 68
Quoted 8 lines
xen/common/mpi.c new file +8
-0+for (_i = (n)-1 ; _i >=0 ; _i--) \ + (d)[_i] = (s)[_i]; \ + }while (0 ) + +/* Zero N limbs at D */ +#define MPN_ZERO (d, n) \ + do { \ + int _i; \... this is plain int. There are apparently many similar issue.
Quoted 2 lines
[...]
+leave:Here and elsewhere - labels indented by at least one blank, please.
Quoted 165 lines
xen/crypto/rsa.c new file +162
-0@@ -0,0 +1,194 @@ +/* rsa.c + +The RSA publickey algorithm. + +Copyright (C) 2001 Niels Möller + +This file is part of GNU Nettle. + +GNU Nettle is free software: you can redistribute it and/or +modify it under the terms of either: + +* the GNU Lesser General Public License as published by the Free +Software Foundation; either version 3 of the License, or (at your +option) any later version. + +or + +* the GNU General Public License as published by the Free +Software Foundation; either version 2 of the License, or (at your +option) any later version. + +or both in parallel, as here. + +GNU Nettle is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received copies of the GNU General Public License and +the GNU Lesser General Public License along with this program. If +not, see http://www.gnu.org/licenses/. +*/ + +#include <xen/rsa.h> +#include <xen/lib.h> +#include <xen/err.h> +#include <xen/bug.h> +#include <xen/string.h> + +void rsa_public_key_init (struct rsa_public_key *key) +{ + key->n =NULL ; + key->e =NULL ; + key->size =0 ; +} + +/* +* Computes the size, in octets, of the modulo. Returns 0 if the +* modulo is too small to be useful, or otherwise appears invalid. +*/ +static size_t rsa_check_size (MPI n) +{ +/* Round upwards */ +size_t size; + +/* Even moduli are invalid */ +if (mpi_test_bit (n,0 ) ==0 ) +return 0 ; + + size = (mpi_get_nbits (n) +7 ) /8 ; + +if ( size <RSA_MINIMUM_N_OCTETS ) +return 0 ; + +return size; +} + +int rsa_public_key_prepare (struct rsa_public_key *key) +{ +if ( !key->n || !key->e || key->size ) +return -EINVAL ; + + key->size =rsa_check_size (key->n); + +return key->size >0 ?0 : -EINVAL ; +} + +/* +* Formats the PKCS#1 padding, of the form +* +* 0x00 0x01 0xff ... 0xff 0x00 id ...digest... +* +* where the 0xff ... 0xff part consists of at least 8 octets. The +* total size equals the octet size of n. +*/ +static uint8_t *pkcs1_signature_prefix (unsigned int key_size,uint8_t *buffer, +unsigned int id_size,const uint8_t *id, +unsigned int digest_size) +{ +unsigned int j; + +if ( key_size <11 + id_size + digest_size ) +return NULL ; + + j = key_size - digest_size - id_size; + +memcpy (buffer + j, id, id_size); + buffer[0 ] =0 ; + buffer[1 ] =1 ; + buffer[j -1 ] =0 ; + +ASSERT (j >=11 ); +memset (buffer +2 ,0xff , j -3 ); + +return buffer + j + id_size; +} + +/* +* From RFC 3447, Public-Key Cryptography Standards (PKCS) #1: RSA +* Cryptography Specifications Version 2.1. +* +* id-sha256 OBJECT IDENTIFIER ::= +* {joint-iso-itu-t(2) country(16) us(840) organization(1) +* gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1} +*/ +static const uint8_t +sha256_prefix[] = +{ +/* 19 octets prefix, 32 octets hash, total 51 */ +0x30 ,49 ,/* SEQUENCE */ +0x30 ,13 ,/* SEQUENCE */ +0x06 ,9 ,/* OBJECT IDENTIFIER */ +0x60 ,0x86 ,0x48 ,0x01 ,0x65 ,0x03 ,0x04 ,0x02 ,0x01 , +0x05 ,0 ,/* NULL */ +0x04 ,32 /* OCTET STRING */ +/* Here comes the raw hash value */ +}; + +static int pkcs1_rsa_sha256_encode (MPI *m,size_t key_size, +struct sha2_256_state *hash) +{ +uint8_t *ptr; +uint8_t *buf; + + buf =xmalloc_bytes (key_size); +if ( !buf ) +return -ENOMEM ; + + ptr =pkcs1_signature_prefix (key_size, buf, +sizeof (sha256_prefix), sha256_prefix, +SHA2_256_DIGEST_SIZE ); +if ( !ptr ) + { +xfree (buf); +return -EINVAL ; + } + +sha2_256_final (hash, ptr); + *m =mpi_read_raw_data (buf, key_size); +xfree (buf); +return 0 ; +} + +static int rsa_verify (const struct rsa_public_key *key,MPI m,MPI s) +{ +int ret; +MPI m1; + +/* (1) Validate 0 <= s < n */ +if (mpi_cmp_ui (s,0 ) <0 ||mpi_cmp (s, key->n) >=0 ) +return -EINVAL ; +xen/crypto/rsa.c line 172
Quoted 10 lines
xen/crypto/rsa.c new file +10
-0+ m1 =mpi_alloc (key->size /BYTES_PER_MPI_LIMB ); +if ( !m1 ) +return -ENOMEM ; + +/* (2) m = s^e mod n */ + ret =mpi_powm (m1, s, key->e, key->n); +if ( ret ) + goto out; + + ret =mpi_cmp (m, m1) ? -EINVAL :0 ;You look to have striven to convert this file to Xen style; there's a stray blank here, though.
Quoted 16 lines
xen/include/xen/mpi.h line 10
Quoted 13 lines
xen/include/xen/mpi.h new file +10
-0@@ -0,0 +1,63 @@ +/* mpi.h - Multi Precision Integers +* Copyright (C) 1994, 1996, 1998, 1999, +* 2000, 2001 Free Software Foundation, Inc. +* +* This file is part of GNUPG. +* +* GNUPG is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. While this may want to remain, accompany it with an SPDX header? (Same elsewhere perhaps.)
Sure.
Quoted 40 lines
Quoted 11 lines
xen/include/xen/mpi.h new file +10
-0+* GNUPG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +* +* Note: This code is heavily based on the GNU MP Library. xen/include/xen/mpi.h line 31
Quoted 10 lines
xen/include/xen/mpi.h new file +10
-0+* Actually it's the same code with only minor changes in the +* way the data is stored; this is to support the abstraction +* of an optional secure memory allocation which may be used +* to avoid revealing of sensitive data due to paging etc. +* The GNU MP Library itself is published under the LGPL; +* however I decided to publish this code under the plain GPL. +*/ + +#ifndef MPI_H +#define MPI_H This imo wants at least a
XEN_prefix. Same inrsa.h.xen/include/xen/mpi.h line 44
Quoted 12 lines
xen/include/xen/mpi.h new file +12
-0+#include <xen/types.h> + +#define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8) +#define BITS_PER_MPI_LIMB BITS_PER_LONG + +typedef unsigned long int mpi_limb_t ; +typedef signed long int mpi_limb_signed_t ; + +struct mpi { +int alloced;/* array size (# of allocated limbs) */ +int nlimbs;/* number of valid limbs */ +int nbits;/* the real number of valid bits (info only) */ I understand the goal likely is to not meaningfully change the original, but none of these look like they can hold negative values, and ...
Quoted 7 lines
xen/include/xen/mpi.h line 45
Quoted 1 line
xen/include/xen/mpi.h new file +1
-0+int sign;/* indicates a negative number */ ... this one looks like it's a boolean.
xen/include/xen/mpi.h line 46
Quoted 1 line
xen/include/xen/mpi.h new file +1
-0+unsigned flags;/* bit 0: array must be allocated in secure memory space */ In Xen we use "unsigned int", not plain "unsigned".
I'd prefer not to change the type from int to something else but I'll fix the unsigned style issue.
Ross
Quoted 13 lines · Ross Lagerwall
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.
Note that this series depends on another patch that has not yet been merged:
xen/lib: Export additional sha256 functions
https://lists.xenproject.org/archives/html/xen-devel/2025-05/msg00222.htmlJennifer Herbert (1):
livepatch: Verify livepatch signatures
Quoted 6 lines
Kevin Lampis (1):
livepatch: Embed public key in XenRoss Lagerwall (2):
docs: Introduce live patch signing
crypto: Add RSA support
Patches 1 and 4 seems to be lost...
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab
Quoted 14 lines · Marek Marczykowski-Górecki
Quoted 13 lines · Ross Lagerwall
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.
Note that this series depends on another patch that has not yet been merged:
xen/lib: Export additional sha256 functions
https://lists.xenproject.org/archives/html/xen-devel/2025-05/msg00222.htmlJennifer Herbert (1):
livepatch: Verify livepatch signatures
Quoted 7 lines
Quoted 6 lines
Kevin Lampis (1):
livepatch: Embed public key in XenRoss Lagerwall (2):
docs: Introduce live patch signing
crypto: Add RSA supportPatches 1 and 4 seems to be lost...
Yes, we're working on that. (Corporate email fun)
~Andrew
PATCH 3/4 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 crypto/signing_key.pem
--- xen/common/Kconfig | 18 ++++++++++++++++++ xen/common/Makefile | 2 +- xen/common/livepatch.c | 41 ++++++++++++++++++++++++++++++++++++++++ xen/crypto/Makefile | 14 +++++++++++++- xen/tools/extract-key.py | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100755 xen/tools/extract-key.py
xen/common/Kconfig +18 -0
@@ -481,6 +481,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_SIGNING + 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_SIG_KEY + string "File name of payload signing public key" + default "signing_key.pem" + depends on PAYLOAD_SIGNING + 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/common/Makefile +1 -1
@@ -28,7 +28,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o -obj-y += mpi.o +obj-$(CONFIG_PAYLOAD_SIGNING) += mpi.o obj-y += multicall.o obj-y += notifier.o obj-$(CONFIG_NUMA) += numa.o
xen/common/livepatch.c +41 -0
65 lines not on this page · raw message
xen/crypto/Makefile +13 -1
@@ -1,3 +1,15 @@ obj-y += rijndael.o -obj-y += rsa.o +obj-$(CONFIG_PAYLOAD_SIGNING) += rsa.o obj-y += vmac.o + +obj-$(CONFIG_PAYLOAD_SIGNING) += builtin_payload_key.o + +ifeq ($(CONFIG_PAYLOAD_SIGNING),y) +key_path := $(srctree)/crypto/$(patsubst "%",%,$(CONFIG_PAYLOAD_SIG_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: $(srctree)/tools/binfile $(obj)/builtin_payload_key.bin FORCE + $(call if_changed,binfile,$(obj)/builtin_payload_key.bin xen_livepatch_key_data) +endif
xen/tools/extract-key.py new file +37 -0
@@ -0,0 +1,37 @@ +#!/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) + +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 3/4] livepatch: Embed public key in Xen
Quoted 11 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.
Quoted 6 lines
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
crypto/signing_key.pem
According to this the .pem file isn't really a source one; how does ...
Quoted 9 lines
xen/common/Kconfig +3
-0@@ -481,6 +481,24 @@ config LIVEPATCH If unsure, say Y. +config PAYLOAD_SIGNING + bool "Verify signed LivePatch payloads" + depends on LIVEPATCH
xen/common/Kconfig config LIVEPATCH · line 496
Quoted 10 lines
xen/common/Kconfig +10
-0+ 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_SIG_KEY + string "File name of payload signing public key" + default "signing_key.pem"
... this work in an out-of-tree build?
xen/common/Kconfig config LIVEPATCH · line 497
Quoted 1 line
xen/common/Kconfig +1
-0+ depends on PAYLOAD_SIGNING
As to the name of this: According to its description, it's signature verification, not signing. I think this wants reflecting in the name.
xen/common/Makefile obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o · line 31
Quoted 8 lines
xen/common/Makefile +1
-1@@ -28,7 +28,7 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o -obj-y += mpi.o +obj-$(CONFIG_PAYLOAD_SIGNING) += mpi.o
Looks like the Kconfig symbol then wants introducing in the earlier patch, or as a prereq thereto.
Quoted 12 lines
xen/common/livepatch.c +2
-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>
xen/common/livepatch.c static struct livepatch_work livepatch_work; · line 80
Quoted 6 lines
xen/common/livepatch.c +3
-0@@ -73,6 +75,12 @@ 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_SIGNING +/* The public key contained with Xen used to verify payload signatures. */ +extern const uint8_t xen_livepatch_key_data[];
Iirc Misra demands that declarations appear in headers.
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2298
Quoted 11 lines
xen/common/livepatch.c +4
-0+static struct rsa_public_key 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 )@@ -2287,6 +2295,34 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +#ifdef CONFIG_PAYLOAD_SIGNING
Nit: The #ifdef would better appear inside the function body, to reduce redundancy.
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2306
Quoted 8 lines
xen/common/livepatch.c +8
-0+static int __init load_builtin_payload_key (void ) +{ +const uint8_t *ptr; +uint32_t len; + +rsa_public_key_init (&builtin_payload_key); + + ptr = xen_livepatch_key_data;
This being the sole place where the array is used, ...
xen/common/livepatch.c static struct notifier_block cpu_nfb = { · line 2347
Quoted 8 lines
xen/common/livepatch.c +4
-0@@ -2305,6 +2341,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)
(Nit: style)
Quoted 8 lines
xen/crypto/Makefile +2
-1@@ -1,3 +1,15 @@ obj-y += rijndael.o -obj-y += rsa.o +obj-$(CONFIG_PAYLOAD_SIGNING) += rsa.o obj-y += vmac.o +
xen/crypto/Makefile line 14
Quoted 10 lines
xen/crypto/Makefile +10
-0+obj-$(CONFIG_PAYLOAD_SIGNING) += builtin_payload_key.o + +ifeq ($(CONFIG_PAYLOAD_SIGNING),y) +key_path := $(srctree)/crypto/$(patsubst "%",%,$(CONFIG_PAYLOAD_SIG_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: $(srctree)/tools/binfile $(obj)/builtin_payload_key.bin FORCE + $(call if_changed,binfile,$(obj)/builtin_payload_key.bin xen_livepatch_key_data)
... arrangements want making for the array to live ideally in .init.rodata, but at least somewhere in .init.*. E.g. by passing -i to tools/binfile.
Jan
PATCH 4/4 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).
--- xen/common/livepatch.c | 134 ++++++++++++++++++++++++++++++++ xen/common/livepatch_elf.c | 55 +++++++++++++ xen/include/xen/livepatch.h | 5 ++ xen/include/xen/livepatch_elf.h | 18 +++++ 4 files changed, 212 insertions(+)
xen/common/livepatch.c +134 -0
@@ -81,6 +81,9 @@ extern const uint8_t xen_livepatch_key_data[];static struct rsa_public_key builtin_payload_key;#endif +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size); +static int get_name (const struct xen_livepatch_name *name,char *n) {if ( !name->size || name->size >XEN_LIVEPATCH_NAME_SIZE )
@@ -1164,6 +1167,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;
@@ -1204,6 +1209,135 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)return rc; } +#ifdef CONFIG_PAYLOAD_SIGNING + +#define MAX_SIG_NOTE_SIZE 1024 + +struct payload_signature { +uint16_t version; +uint8_t algo;/* Public-key crypto algorithm */ +uint8_t hash;/* Digest algorithm */ +uint32_t sig_len;/* Length of signature data */ +}; + +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; +} + +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size) +{ +static const char notename[] ="Xen" ; +struct payload_signature *siginfo; +livepatch_elf_note note; +size_t nsize; +int rc; + + rc =livepatch_elf_note_by_names (elf,ELF_XEN_SIGNATURE , notename,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,0 , ¬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 ; + } + } + + nsize = note.size; +if ( nsize <=sizeof (*siginfo) || nsize >=MAX_SIG_NOTE_SIZE ) + { + printk(XENLOG_ERR LIVEPATCH "Invalid signature note size: %zu\n", + nsize); +return -EINVAL ; + } + + siginfo =xmalloc_bytes (nsize); +if ( !siginfo ) +return -ENOMEM ; + +memcpy (siginfo, note.data, nsize); + +if ( siginfo->version !=SIGNATURE_SUPPORTED_VERION ) + { + printk(XENLOG_ERR LIVEPATCH "Bad signature version %u\n", siginfo->version); + rc = -EINVAL ; + goto out; + } + +if ( siginfo->sig_len ==0 || + nsize -sizeof (*siginfo) < siginfo->sig_len ) + { + printk(XENLOG_ERR LIVEPATCH +"Payload signature size bad. ns=%zu si=%u\n" , + nsize, siginfo->sig_len); + rc = -EINVAL ; + goto out; + } + +if ( siginfo->algo !=SIGNATURE_ALGORITHM_RSA || + siginfo->hash !=SIGNATURE_HASH_SHA256 ) + { + printk(XENLOG_ERR LIVEPATCH "Bad payload signature: v:%u, a:%u, h:%u\n", + siginfo->version, siginfo->algo, siginfo->hash); + rc = -EINVAL ; + goto out; + } + +/* Remove signature from data, as can't be verified with it. */ +memset ((void *)note.data +sizeof (*siginfo),0 , siginfo->sig_len); + rc =check_rsa_sha256_signature (raw, size, siginfo +1 , siginfo->sig_len); + +out: +xfree (siginfo); +return rc; +} +#else +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size) +{ +return -EINVAL ; +} +#endif +static int livepatch_upload (struct xen_sysctl_livepatch_upload *upload) {struct payload *data, *found;
xen/common/livepatch_elf.c +55 -0
61 lines not on this page · raw message
xen/include/xen/livepatch.h +5 -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,10 @@ struct xen_sysctl_livepatch_op;/* Arbitrary limit for payload size and .bss section size. */ #define LIVEPATCH_MAX_SIZE MB(2) +#define SIGNATURE_SUPPORTED_VERION 1 +#define SIGNATURE_ALGORITHM_RSA 0 +#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