From: Jean-Philippe Brucker <[email protected]> Add a Rom notifier to keep track of binary blobs loaded in Realm memory. That way we can deterministically calculate the Realm Initial Measurement (RIM).
Signed-off-by: Jean-Philippe Brucker <[email protected]> Signed-off-by: Lorenzo Pieralisi <[email protected]> Signed-off-by: Mathieu Poirier <[email protected]> --- hw/core/loader.c | 24 +++++++++++++++++++++++ include/hw/core/loader.h | 17 +++++++++++++++++ target/arm/kvm-rme.c | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/hw/core/loader.c b/hw/core/loader.c index 5cbfba0a86d2..644d55c9e0ed 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -74,6 +74,8 @@ #endif static int roms_loaded; +static NotifierList rom_loader_notifier = + NOTIFIER_LIST_INITIALIZER(rom_loader_notifier); /* return the size or -1 if error */ int64_t get_image_size(const char *filename, Error **errp) @@ -1201,6 +1203,11 @@ MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len, return mr; } +void rom_add_load_notifier(Notifier *notifier) +{ + notifier_list_add(&rom_loader_notifier, notifier); +} + /* This function is specific for elf program because we don't need to allocate * all the rom. We just allocate the first part and the rest is just zeros. This * is why romsize and datasize are different. Also, this function takes its own @@ -1242,6 +1249,7 @@ ssize_t rom_add_option(const char *file, int32_t bootindex) static void rom_reset(void *unused) { Rom *rom; + RomLoaderNotifyData notify; QTAILQ_FOREACH(rom, &roms, next) { if (rom->fw_file) { @@ -1290,6 +1298,22 @@ static void rom_reset(void *unused) address_space_flush_icache_range(rom->as, rom->addr, rom->datasize); trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom); + + if (rom->addr == -1) { + /* + * These blobs (ACPI tables) are not loaded into guest RAM at reset. + * Instead the firmware will load them via fw_cfg and measure them + * itself. + */ + continue; + } + + notify = (RomLoaderNotifyData) { + .addr = rom->addr, + .len = rom->datasize, + .data = rom->data, + }; + notifier_list_notify(&rom_loader_notifier, ¬ify); } } diff --git a/include/hw/core/loader.h b/include/hw/core/loader.h index d9431e8a8d12..fdfddfa7443e 100644 --- a/include/hw/core/loader.h +++ b/include/hw/core/loader.h @@ -342,6 +342,23 @@ void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size); ssize_t rom_add_vga(const char *file); ssize_t rom_add_option(const char *file, int32_t bootindex); +typedef struct RomLoaderNotifyData { + /* Address of the blob in guest memory */ + hwaddr addr; + /* Length of the blob */ + size_t len; + /* Blog data */ + uint8_t *data; +} RomLoaderNotifyData; + +/** + * rom_add_load_notifier - Add a notifier for loaded images + * + * Add a notifier that will be invoked with a RomLoaderNotifyData structure for + * each blob loaded into guest memory, after the blob is loaded. + */ +void rom_add_load_notifier(Notifier *notifier); + /* This is the usual maximum in uboot, so if a uImage overflows this, it would * overflow on real hardware too. */ #define UBOOT_MAX_DECOMPRESSED_BYTES (64 << 20) diff --git a/target/arm/kvm-rme.c b/target/arm/kvm-rme.c index f7c2cc786e2c..b1fb8957f3de 100644 --- a/target/arm/kvm-rme.c +++ b/target/arm/kvm-rme.c @@ -10,6 +10,7 @@ #include "hw/core/boards.h" #include "hw/core/cpu.h" +#include "hw/core/loader.h" #include "kvm_arm.h" #include "migration/blocker.h" #include "qapi/error.h" @@ -22,8 +23,16 @@ #define TYPE_RME_GUEST "rme-guest" OBJECT_DECLARE_SIMPLE_TYPE(RmeGuest, RME_GUEST) +typedef struct { + hwaddr base; + hwaddr size; + uint8_t *data; +} RmeRamRegion; + struct RmeGuest { ConfidentialGuestSupport parent_obj; + Notifier rom_load_notifier; + GSList *ram_regions; }; OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(RmeGuest, rme_guest, RME_GUEST, @@ -41,6 +50,35 @@ static void rme_vm_state_change(void *opaque, bool running, RunState state) kvm_mark_guest_state_protected(); } +static gint rme_compare_ram_regions(gconstpointer a, gconstpointer b) +{ + const RmeRamRegion *ra = a; + const RmeRamRegion *rb = b; + + g_assert(ra->base != rb->base); + return ra->base < rb->base ? -1 : 1; +} + +static void rme_rom_load_notify(Notifier *notifier, void *data) +{ + RmeRamRegion *region; + RomLoaderNotifyData *rom = data; + + region = g_new0(RmeRamRegion, 1); + region->base = rom->addr; + region->size = rom->len; + region->data = rom->data; + + /* + * The Realm Initial Measurement (RIM) depends on the order in which we + * initialize and populate the RAM regions. To help a verifier + * independently calculate the RIM, sort regions by GPA. + */ + rme_guest->ram_regions = g_slist_insert_sorted(rme_guest->ram_regions, + region, + rme_compare_ram_regions); +} + static int kvm_arm_rme_init(ConfidentialGuestSupport *cgs, Error **errp) { KVMState *s = KVM_STATE(current_accel()); @@ -58,6 +96,9 @@ static int kvm_arm_rme_init(ConfidentialGuestSupport *cgs, Error **errp) error_setg(&rme_mig_blocker, "RME: migration is not implemented"); migrate_add_blocker(&rme_mig_blocker, &error_fatal); + rme_guest->rom_load_notifier.notify = rme_rom_load_notify; + rom_add_load_notifier(&rme_guest->rom_load_notifier); + /* * The realm activation is done last, when the VM starts, after all images * have been loaded and all vcpus finalized. -- 2.43.0
