From nobody Thu Sep 19 01:00:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1720047022; cv=none; d=zohomail.com; s=zohoarc; b=Bf+3f2f8wOEaGkBhUQEL7r672aiA2B9GjJGpbASU/Eg738b0BQwSt0xtBGQyc7X8N2HkNr0dvTtUS7M5BAQiLJEg3gzF7ZfohetXzEO/1apVzuQ4JlZSMaX1smrA/HQcMNbyybmQLIkMIs3OjmSDoMHrigoLVzhBlG5eQ/dM0Rs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720047022; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=H3osRPyR8JjW6MeyHBrRsmkcAIK1uR3HKs135scl6wM=; b=MqYIyqgpCizNiOy0WmtmVtisC+rA/qGvvB3pXBwxLOvjGOQLpZEmgrOT23TnQOIMz6kWQ97usdS093bRA8EoZXGIST/we1yqd/iYoqffkcXlHa8jmfcqITCjyTeAyijHd2LimXYWirn9fvbM0LFtu0LY0a8Pi5aDgp2F53RgIHc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720047022257720.7326277107176; Wed, 3 Jul 2024 15:50:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sP8ms-0004mj-Gm; Wed, 03 Jul 2024 18:49:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sP8mo-0004U9-QB for qemu-devel@nongnu.org; Wed, 03 Jul 2024 18:49:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sP8mY-0005Tu-Cu for qemu-devel@nongnu.org; Wed, 03 Jul 2024 18:49:14 -0400 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-557-p_0d4iUpMsCdtph_woEHiA-1; Wed, 03 Jul 2024 18:48:56 -0400 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-367987cff30so35037f8f.1 for ; Wed, 03 Jul 2024 15:48:56 -0700 (PDT) Received: from redhat.com ([2a0d:6fc7:441:91a8:a47d:5a9:c02f:92f2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4264a1d0b24sm1234945e9.3.2024.07.03.15.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 15:48:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720046937; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=H3osRPyR8JjW6MeyHBrRsmkcAIK1uR3HKs135scl6wM=; b=SY1YGsNQGllu8x5XJjc1PDJESSF/C3fPbPzxuIeWTnO33yfa93oV4ZNo0c2e+hhOnDkOEn MsYCNfA4YVIrfo4+byO1y0ds0G4XM3DSv92R6r1WYlWxJcqgxg3Erc8YTlWi9zwr49KXX7 c50CII3l0xmRJyjHd4/2jodc7laKsWc= X-MC-Unique: p_0d4iUpMsCdtph_woEHiA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720046935; x=1720651735; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=H3osRPyR8JjW6MeyHBrRsmkcAIK1uR3HKs135scl6wM=; b=MkR6YfX043vIwW/EU467l3iaMUWs8Sz9QbxEYMzz9FBaDPMkKqSw/DOmc15/c4Xqx2 aFhB9CK6hzQ7/rSGdI1nuZwt8M0JpI/Dh6vJnRUkfNQ+HQZa6i9xJfvvbi/Ra5QAbej4 U26zfhyA6rMvnNVyobOp08eW7SJ26XJaIXxQZ5zrmGmm0rSqgEC9JfuFEK8NeHvI2bng VnDEiEVcNuxzUlbNyvrbtT9J61jrKLqJ/U8xosHflFqBfBNjR7UAtG6F8XjqVlLTn4Qw 4mo6Tq7nBchTQ52MZRLmSX6RmLdr0fBXBmXcdkEvvDkEWeBca7TZkHIOPvWYep0bBz33 tKRw== X-Gm-Message-State: AOJu0YzY2KMDLn3yEXp/LaTDAs/SwQCALeMrCk8S1UgZOcRGROuETgAd fmkjbDpWJLfoKaJCRdycl6aRSoprJawEVdIwIh2C2Sc6NFe6tMDGLCTJaPZ4y8XKNoakp5VsAcn JOntytBSxO1D7scjOLnGPfj0Ikr5uXqkngS8VlEwN0+y966A4gQnAtbiZ0nJk/kN5TejQkrwDJk G4z06gp9rPmy8P8lun/sRld4DjL0BfXg== X-Received: by 2002:a5d:66ca:0:b0:367:8f29:f7ae with SMTP id ffacd0b85a97d-3679dd66b1bmr5160f8f.49.1720046934776; Wed, 03 Jul 2024 15:48:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEFKhCAyPDpugccROJeD258jAmHezRr/Sn4NGzUPphB9Iay3BGmMuCHosOVZfO24CbmP1GOMg== X-Received: by 2002:a5d:66ca:0:b0:367:8f29:f7ae with SMTP id ffacd0b85a97d-3679dd66b1bmr5139f8f.49.1720046934182; Wed, 03 Jul 2024 15:48:54 -0700 (PDT) Date: Wed, 3 Jul 2024 18:48:49 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , David Woodhouse , David Woodhouse , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Marcel Apfelbaum , Sergio Lopez , Marcelo Tosatti , Paul Durrant , kvm@vger.kernel.org Subject: [PULL v3 58/85] hw/i386/fw_cfg: Add etc/e820 to fw_cfg late Message-ID: <93c76555d842b5d84b95f66abecb6b19545338d9.1720046570.git.mst@redhat.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1720047024016100015 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse In e820_add_entry() the e820_table is reallocated with g_renew() to make space for a new entry. However, fw_cfg_arch_create() just uses the existing e820_table pointer. This leads to a use-after-free if anything adds a new entry after fw_cfg is set up. Shift the addition of the etc/e820 file to the machine done notifier, via a new fw_cfg_add_e820() function. Also make e820_table private and use an e820_get_table() accessor function for it, which sets a flag that will trigger an assert() for any *later* attempts to add to the table. Make e820_add_entry() return void, as most callers don't check for error anyway. Signed-off-by: David Woodhouse Message-Id: Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/i386/e820_memory_layout.h | 8 ++------ hw/i386/fw_cfg.h | 1 + hw/i386/e820_memory_layout.c | 17 ++++++++++++----- hw/i386/fw_cfg.c | 18 +++++++++++++----- hw/i386/microvm.c | 4 ++-- hw/i386/pc.c | 1 + target/i386/kvm/kvm.c | 6 +----- target/i386/kvm/xen-emu.c | 7 +------ 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/hw/i386/e820_memory_layout.h b/hw/i386/e820_memory_layout.h index 7c239aa033..b50acfa201 100644 --- a/hw/i386/e820_memory_layout.h +++ b/hw/i386/e820_memory_layout.h @@ -22,13 +22,9 @@ struct e820_entry { uint32_t type; } QEMU_PACKED __attribute((__aligned__(4))); =20 -extern struct e820_entry *e820_table; - -int e820_add_entry(uint64_t address, uint64_t length, uint32_t type); -int e820_get_num_entries(void); +void e820_add_entry(uint64_t address, uint64_t length, uint32_t type); bool e820_get_entry(int index, uint32_t type, uint64_t *address, uint64_t *length); - - +int e820_get_table(struct e820_entry **table); =20 #endif diff --git a/hw/i386/fw_cfg.h b/hw/i386/fw_cfg.h index 92e310f5fd..e560fd7be8 100644 --- a/hw/i386/fw_cfg.h +++ b/hw/i386/fw_cfg.h @@ -27,5 +27,6 @@ void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState= *fw_cfg, SmbiosEntryPointType ep_type); void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg); void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg); +void fw_cfg_add_e820(FWCfgState *fw_cfg); =20 #endif diff --git a/hw/i386/e820_memory_layout.c b/hw/i386/e820_memory_layout.c index 06970ac44a..3e848fb69c 100644 --- a/hw/i386/e820_memory_layout.c +++ b/hw/i386/e820_memory_layout.c @@ -11,22 +11,29 @@ #include "e820_memory_layout.h" =20 static size_t e820_entries; -struct e820_entry *e820_table; +static struct e820_entry *e820_table; +static gboolean e820_done; =20 -int e820_add_entry(uint64_t address, uint64_t length, uint32_t type) +void e820_add_entry(uint64_t address, uint64_t length, uint32_t type) { + assert(!e820_done); + /* new "etc/e820" file -- include ram and reserved entries */ e820_table =3D g_renew(struct e820_entry, e820_table, e820_entries + 1= ); e820_table[e820_entries].address =3D cpu_to_le64(address); e820_table[e820_entries].length =3D cpu_to_le64(length); e820_table[e820_entries].type =3D cpu_to_le32(type); e820_entries++; - - return e820_entries; } =20 -int e820_get_num_entries(void) +int e820_get_table(struct e820_entry **table) { + e820_done =3D true; + + if (table) { + *table =3D e820_table; + } + return e820_entries; } =20 diff --git a/hw/i386/fw_cfg.c b/hw/i386/fw_cfg.c index 7c43c325ef..0e4494627c 100644 --- a/hw/i386/fw_cfg.c +++ b/hw/i386/fw_cfg.c @@ -48,6 +48,15 @@ const char *fw_cfg_arch_key_name(uint16_t key) return NULL; } =20 +/* Add etc/e820 late, once all regions should be present */ +void fw_cfg_add_e820(FWCfgState *fw_cfg) +{ + struct e820_entry *table; + int nr_e820 =3D e820_get_table(&table); + + fw_cfg_add_file(fw_cfg, "etc/e820", table, nr_e820 * sizeof(*table)); +} + void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState *fw_cfg, SmbiosEntryPointType ep_type) { @@ -60,6 +69,7 @@ void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState= *fw_cfg, PCMachineClass *pcmc =3D PC_MACHINE_GET_CLASS(pcms); MachineClass *mc =3D MACHINE_GET_CLASS(pcms); X86CPU *cpu =3D X86_CPU(ms->possible_cpus->cpus[0].cpu); + int nr_e820; =20 if (pcmc->smbios_defaults) { /* These values are guest ABI, do not change */ @@ -78,8 +88,9 @@ void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState= *fw_cfg, } =20 /* build the array of physical mem area from e820 table */ - mem_array =3D g_malloc0(sizeof(*mem_array) * e820_get_num_entries()); - for (i =3D 0, array_count =3D 0; i < e820_get_num_entries(); i++) { + nr_e820 =3D e820_get_table(NULL); + mem_array =3D g_malloc0(sizeof(*mem_array) * nr_e820); + for (i =3D 0, array_count =3D 0; i < nr_e820; i++) { uint64_t addr, len; =20 if (e820_get_entry(i, E820_RAM, &addr, &len)) { @@ -138,9 +149,6 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms, #endif fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1); =20 - fw_cfg_add_file(fw_cfg, "etc/e820", e820_table, - sizeof(struct e820_entry) * e820_get_num_entries()); - fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg)); /* allocate memory for the NUMA channel: one (64bit) word for the numb= er * of nodes, one word for each VCPU->node and one word for each node to diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c index fec63cacfa..40edcee7af 100644 --- a/hw/i386/microvm.c +++ b/hw/i386/microvm.c @@ -324,8 +324,6 @@ static void microvm_memory_init(MicrovmMachineState *mm= s) fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, machine->smp.max_cpus); fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size); fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1); - fw_cfg_add_file(fw_cfg, "etc/e820", e820_table, - sizeof(struct e820_entry) * e820_get_num_entries()); =20 rom_set_fw(fw_cfg); =20 @@ -586,9 +584,11 @@ static void microvm_machine_done(Notifier *notifier, v= oid *data) { MicrovmMachineState *mms =3D container_of(notifier, MicrovmMachineStat= e, machine_done); + X86MachineState *x86ms =3D X86_MACHINE(mms); =20 acpi_setup_microvm(mms); dt_setup_microvm(mms); + fw_cfg_add_e820(x86ms->fw_cfg); } =20 static void microvm_powerdown_req(Notifier *notifier, void *data) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 77415064c6..d2c29fbfcb 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -625,6 +625,7 @@ void pc_machine_done(Notifier *notifier, void *data) acpi_setup(); if (x86ms->fw_cfg) { fw_cfg_build_smbios(pcms, x86ms->fw_cfg, pcms->smbios_entry_point_= type); + fw_cfg_add_e820(x86ms->fw_cfg); fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg); /* update FW_CFG_NB_CPUS to account for -device added CPUs */ fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index dd8b0f3313..bf182570fe 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2706,11 +2706,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } =20 /* Tell fw_cfg to notify the BIOS to reserve the range. */ - ret =3D e820_add_entry(identity_base, 0x4000, E820_RESERVED); - if (ret < 0) { - fprintf(stderr, "e820_add_entry() table is full\n"); - return ret; - } + e820_add_entry(identity_base, 0x4000, E820_RESERVED); =20 shadow_mem =3D object_property_get_int(OBJECT(s), "kvm-shadow-mem", &e= rror_abort); if (shadow_mem !=3D -1) { diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c index fc2c2321ac..2f89dc628e 100644 --- a/target/i386/kvm/xen-emu.c +++ b/target/i386/kvm/xen-emu.c @@ -176,12 +176,7 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr) s->xen_caps =3D xen_caps; =20 /* Tell fw_cfg to notify the BIOS to reserve the range. */ - ret =3D e820_add_entry(XEN_SPECIAL_AREA_ADDR, XEN_SPECIAL_AREA_SIZE, - E820_RESERVED); - if (ret < 0) { - fprintf(stderr, "e820_add_entry() table is full\n"); - return ret; - } + e820_add_entry(XEN_SPECIAL_AREA_ADDR, XEN_SPECIAL_AREA_SIZE, E820_RESE= RVED); =20 /* The pages couldn't be overlaid until KVM was initialized */ xen_primary_console_reset(); --=20 MST