LivePatch signing support
v2 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 v2.
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/Makefile | 1 + xen/common/livepatch.c | 145 +++ xen/common/livepatch_elf.c | 55 + xen/common/mpi.c | 1729 +++++++++++++++++++++++++++++++ xen/crypto/Makefile | 14 + xen/crypto/rsa.c | 196 ++++ xen/include/xen/livepatch.h | 16 + xen/include/xen/livepatch_elf.h | 18 + xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ xen/tools/extract-key.py | 37 + 13 files changed, 2425 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 v2 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.
---
In v2:
* Use ELF note type and descriptor length rather than a custom header.
* Rename SIGNATURE_SUPPORTED_VERION
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 v2 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 v2:
* Split the loading part into a separate patch.
* Expect public key in object tree rather than source tree.
* Rename Kconfig symbols and others to reflect that it is used for verification in Xen rather than signing.
* Move extern declaration to a header.
* Move raw key data to init.rodata.
xen/common/Kconfig | 18 ++++++++++++++++++ xen/crypto/Makefile | 13 +++++++++++++ xen/include/xen/livepatch.h | 6 ++++++ xen/tools/extract-key.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100755 xen/tools/extract-key.py
xen/common/Kconfig +18 -0
@@ -461,6 +461,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 +13 -0
@@ -1,2 +1,15 @@ obj-y += rijndael.o obj-y += vmac.o + +obj-$(CONFIG_PAYLOAD_VERIFY) += builtin_payload_key.o + +ifeq ($(CONFIG_PAYLOAD_VERIFY),y) +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) +endif
xen/include/xen/livepatch.h +6 -0
@@ -18,6 +18,7 @@ struct xen_sysctl_livepatch_op;#ifdef CONFIG_LIVEPATCH +#include <xen/init.h> #include <xen/lib.h> /*
@@ -143,6 +144,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 __initconst xen_livepatch_key_data[]; +#endif +#else /*
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 v2 2/5] livepatch: Embed public key in Xen
xen/crypto/Makefile line 4
Quoted 7 lines · Ross Lagerwall
xen/crypto/Makefile +2
-0@@ -1,2 +1,15 @@ obj-y += rijndael.o obj-y += vmac.o + +obj-$(CONFIG_PAYLOAD_VERIFY) += builtin_payload_key.o
For new files please prefer dashes over underscores in their names.
xen/crypto/Makefile line 6
Quoted 1 line
xen/crypto/Makefile +1
-0+ifeq ($(CONFIG_PAYLOAD_VERIFY),y)
This isn't needed, is it?
xen/crypto/Makefile line 7
Quoted 1 line
xen/crypto/Makefile +1
-0+key_path := $(objtree)/$(patsubst "%",%,$(CONFIG_PAYLOAD_VERIFY_KEY))
Since they can be used there, dashes imo also want preferring for new make variables (unless they need exporting to the shell).
xen/crypto/Makefile struct payload; · line 149
Quoted 7 lines
xen/crypto/Makefile +3
-0@@ -143,6 +144,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 __initconst xen_livepatch_key_data[];
Nit: Section placement annotations are generally meaningless on declarations, and hence want omitting from there.
Jan
PATCH v2 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 v2:
* Fix MISRA complaint.
* Address some style and other minor issues.
xen/common/Makefile | 1 + xen/common/mpi.c | 1729 +++++++++++++++++++++++++++++++++++++++++ xen/crypto/Makefile | 1 + xen/crypto/rsa.c | 196 +++++ xen/include/xen/mpi.h | 68 ++ xen/include/xen/rsa.h | 74 ++ 6 files changed, 2069 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-$(CONFIG_PAYLOAD_VERIFY) += mpi.o obj-y += multicall.o obj-y += notifier.o obj-$(CONFIG_NUMA) += numa.o
xen/common/mpi.c new file +1729 -0
1,729 lines not on this page · raw message
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
Signature · 1 line
2.49.0
Re: [PATCH v2 3/5] crypto: Add RSA support
xen/common/Makefile obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o · line 31
Quoted 7 lines · Ross Lagerwall
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-$(CONFIG_PAYLOAD_VERIFY) += mpi.o
This being odd and non-scalable, I now think the file would better move to lib/. It _is_ library code, after all. Then it can be included in lib-y (thus always being built, i.e. reducing the risk of accidental build breakages), but will be included in the final image only when needed.
Quoted 25 lines
xen/include/xen/mpi.h new file +22
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* 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. +* +* 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 32
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 XEN__MPI_H +#define XEN__MPI_H
With the recent change to header guard naming, the double underscore here (and in rsa.h) wants to shrink back to a single one.
Jan
PATCH v2 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.
---
In v2:
* Split out from "livepatch: Embed public key in Xen"
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 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 +2293,31 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +static int __init 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, len); + ptr += len; + +memcpy (&len, ptr,sizeof (len)); + ptr +=sizeof (len); + builtin_payload_key.e =mpi_read_raw_data (ptr, 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) {
@@ -2305,6 +2336,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 v2 4/5] livepatch: Load built-in key during boot
xen/common/livepatch.c static struct livepatch_work livepatch_work; · line 79
Quoted 6 lines · Ross Lagerwall
xen/common/livepatch.c +2
-0@@ -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 builtin_payload_key;
__read_mostly or even __ro_after_init?
Quoted 5 lines
xen/common/livepatch.c +1
-0@@ -2287,6 +2293,31 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +static int __init load_builtin_payload_key (void )
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2306
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));
Doesn't this (and the similar one further down) need to be an endian- ness conversion? In fact, seeing how the data is being built, it's not really clear to me what endian-ness the field would be written in, when build host and target host endianness differ.
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2308
Quoted 2 lines
xen/common/livepatch.c +2
-0+ ptr +=sizeof (len); + builtin_payload_key.n =mpi_read_raw_data (ptr, len);
Whether there are endianness concerns here I also don't know.
xen/common/livepatch.c static struct notifier_block cpu_nfb = { · line 2342
Quoted 8 lines
xen/common/livepatch.c +4
-0@@ -2305,6 +2336,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): Missing blanks.
Jan
Re: [PATCH v2 4/5] livepatch: Load built-in key during boot
Quoted 15 lines · Jan Beulich
xen/common/livepatch.c static struct livepatch_work livepatch_work; · line 79
Quoted 6 lines · Ross Lagerwall
xen/common/livepatch.c +2
-0@@ -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 builtin_payload_key;
__read_mostlyor even__ro_after_init?Quoted 4 lines
xen/common/livepatch.c +0
-0@@ -2287,6 +2293,31 @@ static void cf_check livepatch_printall(unsigned char key)spin_unlock (&payload_lock); }
Quoted 16 lines
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2306
Quoted 11 lines
xen/common/livepatch.c +11
-0+static int __init 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));Doesn't this (and the similar one further down) need to be an endian- ness conversion? In fact, seeing how the data is being built, it's not really clear to me what endian-ness the field would be written in, when build host and target host endianness differ.
Good point. In v3, I've made this and the build-time tool use little endian which in most cases should be a no-op.
Quoted 5 lines
xen/common/livepatch.c static void cf_check livepatch_printall(unsigned char key) · line 2308
Quoted 3 lines
xen/common/livepatch.c +2
-0+ ptr +=sizeof (len); + builtin_payload_key.n =mpi_read_raw_data (ptr, len);Whether there are endianness concerns here I also don't know.
As a byte sequence, this always uses big endian (much like OpenSSL). I've added a comment in v3 to clarify this.
Thanks,
Ross
PATCH v2 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 v2:
* Adjust according to design tweaks.
xen/common/livepatch.c | 109 ++++++++++++++++++++++++++++++++ xen/common/livepatch_elf.c | 55 ++++++++++++++++ xen/include/xen/livepatch.h | 10 +++ xen/include/xen/livepatch_elf.h | 18 ++++++ 4 files changed, 192 insertions(+)
xen/common/livepatch.c +109 -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>
@@ -79,6 +80,9 @@ static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet);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 )
@@ -1162,6 +1166,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;
@@ -1202,6 +1208,109 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)return rc; } +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 + +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" ; +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 +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 +10 -0
@@ -39,6 +39,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"
@@ -50,6 +51,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 v2 5/5] livepatch: Verify livepatch signatures
xen/common/livepatch.c static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet); · line 84
Quoted 6 lines · Ross Lagerwall
xen/common/livepatch.c +2
-0@@ -79,6 +80,9 @@ static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet);static struct rsa_public_key builtin_payload_key;#endif +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size);
I think it would be nice if this forward decl was avoided. Which looks to be feasible if you moved the definition further up.
Quoted 92 lines
xen/common/livepatch.c +88
-0@@ -1202,6 +1208,109 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)return rc; } +#ifdef CONFIG_PAYLOAD_VERIFY +#define MAX_SIG_NOTE_SIZE 1024 + +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" ; +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); +
xen/common/livepatch.c static int load_payload_data(struct payload *payload, void *raw, size_t len) · line 1308
Quoted 10 lines
xen/common/livepatch.c +10
-0+/* 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 +static int check_signature (const struct livepatch_elf *elf,void *raw, +size_t size)
As indicated before, I also think it would be nice if this redundant function header was eliminated, but changing the #if / #else / #endif placement.
Jan