From nobody Tue May 21 09:16:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1597159944; cv=none; d=zohomail.com; s=zohoarc; b=Fh2Ld3P8C3KyB5jRuzMwHFHWc19melxLPBp4d5IgVNdUtPTa+85swn9uc56eQQGmNRahl1kKRB/hjYoCTKydXjitOTeeTtnbyNSylhPwopBPFFWMiEK7v9mcMzSPNdixfAXNXkO9QgGljMFLPxrzuNpS7oOK8gDXu4yKtydaNjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597159944; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4+JvPda2knWDpfbUFQ+FFnspQAgoV58oIJRpuufoob0=; b=Il1NrOL3uDYkA/xdkYAnwNGBFCOS9TwYgU3yhsvtREfFUKN1TZnQprHiSBIA2/T8iZSUznsI75IHTNUgutcou9y6VaCzuSYibQ1u/TICuCHH8a5iwCxV887PcvImiwD/B0HetsY+ro2Hh58A3jKwgw4cjYl8/XHcl4xyrqvIeys= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1597159944297219.95041180999158; Tue, 11 Aug 2020 08:32:24 -0700 (PDT) Received: from localhost ([::1]:54648 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k5WGE-0000ao-OQ for importer@patchew.org; Tue, 11 Aug 2020 11:32:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56248) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k5WEF-0007eT-3e for qemu-devel@nongnu.org; Tue, 11 Aug 2020 11:30:19 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:50405 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1k5WEC-0002Jl-2d for qemu-devel@nongnu.org; Tue, 11 Aug 2020 11:30:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-556-sU1a593qO6Cp_1SBqCErKg-1; Tue, 11 Aug 2020 11:30:10 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E681319067E3; Tue, 11 Aug 2020 15:30:07 +0000 (UTC) Received: from bahia.lan (ovpn-112-38.ams2.redhat.com [10.36.112.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B9A069318; Tue, 11 Aug 2020 15:30:06 +0000 (UTC) X-MC-Unique: sU1a593qO6Cp_1SBqCErKg-1 Subject: [PATCH 1/2] nvram: Add dry_run argument to chrp_nvram_create_system_partition() From: Greg Kurz To: Thomas Huth Date: Tue, 11 Aug 2020 17:30:05 +0200 Message-ID: <159715980541.1635409.14788051616570436860.stgit@bahia.lan> In-Reply-To: <159715979684.1635409.14107632395902701971.stgit@bahia.lan> References: <159715979684.1635409.14107632395902701971.stgit@bahia.lan> User-Agent: StGit/0.21 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Content-Transfer-Encoding: quoted-printable 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: softfail client-ip=205.139.110.61; envelope-from=groug@kaod.org; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/11 06:40:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Mark Cave-Ayland , Laurent Vivier , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Since commit 55d9950aaa8e ("nvram: Introduce helper functions for CHRP "system" and "free space" partitions") it is possible to pre-initialize a "system" partition in the NVRAM with the data passed to all -prom-env parameters on the QEMU command line. Unfortunately, this doesn't take the total size of the data into account and chrp_nvram_create_system_partition() may crash at some point if the caller hasn't allocated enough space. Add a dry_run argument that causes chrp_nvram_create_system_partition() to only return the size of the partition without actually copying data into it. This can be used by callers to allocate enough memory. Signed-off-by: Greg Kurz --- hw/nvram/chrp_nvram.c | 35 +++++++++++++++++++++++------------ hw/nvram/mac_nvram.c | 2 +- hw/nvram/spapr_nvram.c | 3 ++- hw/sparc/sun4m.c | 2 +- hw/sparc64/sun4u.c | 2 +- include/hw/nvram/chrp_nvram.h | 3 ++- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/hw/nvram/chrp_nvram.c b/hw/nvram/chrp_nvram.c index d969f267048e..548cb8b5c97a 100644 --- a/hw/nvram/chrp_nvram.c +++ b/hw/nvram/chrp_nvram.c @@ -24,37 +24,47 @@ #include "hw/nvram/chrp_nvram.h" #include "sysemu/sysemu.h" =20 -static int chrp_nvram_set_var(uint8_t *nvram, int addr, const char *str) +static int chrp_nvram_set_var(uint8_t *nvram, int addr, const char *str, + bool dry_run) { int len; =20 len =3D strlen(str) + 1; - memcpy(&nvram[addr], str, len); - + if (!dry_run) { + memcpy(&nvram[addr], str, len); + } return addr + len; } =20 /** * Create a "system partition", used for the Open Firmware - * environment variables. + * environment variables. If @dry_run is false, only returns + * the size of the partition but don't write the data. */ -int chrp_nvram_create_system_partition(uint8_t *data, int min_len) +int chrp_nvram_create_system_partition(uint8_t *data, int min_len, bool dr= y_run) { ChrpNvramPartHdr *part_header; unsigned int i; int end; =20 - part_header =3D (ChrpNvramPartHdr *)data; - part_header->signature =3D CHRP_NVPART_SYSTEM; - pstrcpy(part_header->name, sizeof(part_header->name), "system"); + assert(data || dry_run); + + if (!dry_run) { + part_header =3D (ChrpNvramPartHdr *)data; + part_header->signature =3D CHRP_NVPART_SYSTEM; + pstrcpy(part_header->name, sizeof(part_header->name), "system"); + } =20 end =3D sizeof(ChrpNvramPartHdr); for (i =3D 0; i < nb_prom_envs; i++) { - end =3D chrp_nvram_set_var(data, end, prom_envs[i]); + end =3D chrp_nvram_set_var(data, end, prom_envs[i], dry_run); } =20 /* End marker */ - data[end++] =3D '\0'; + if (!dry_run) { + data[end] =3D '\0'; + } + end++; =20 end =3D (end + 15) & ~15; /* XXX: OpenBIOS is not able to grow up a partition. Leave some space = for @@ -62,8 +72,9 @@ int chrp_nvram_create_system_partition(uint8_t *data, int= min_len) if (end < min_len) { end =3D min_len; } - chrp_nvram_finish_partition(part_header, end); - + if (!dry_run) { + chrp_nvram_finish_partition(part_header, end); + } return end; } =20 diff --git a/hw/nvram/mac_nvram.c b/hw/nvram/mac_nvram.c index beec1c4e4d11..4396f893f14a 100644 --- a/hw/nvram/mac_nvram.c +++ b/hw/nvram/mac_nvram.c @@ -141,7 +141,7 @@ static void pmac_format_nvram_partition_of(MacIONVRAMSt= ate *nvr, int off, =20 /* OpenBIOS nvram variables partition */ sysp_end =3D chrp_nvram_create_system_partition(&nvr->data[off], - DEF_SYSTEM_SIZE) + off; + DEF_SYSTEM_SIZE, false) = + off; =20 /* Free space partition */ chrp_nvram_create_free_partition(&nvr->data[sysp_end], len - sysp_end); diff --git a/hw/nvram/spapr_nvram.c b/hw/nvram/spapr_nvram.c index 15d08281d411..992b818d34e7 100644 --- a/hw/nvram/spapr_nvram.c +++ b/hw/nvram/spapr_nvram.c @@ -188,7 +188,8 @@ static void spapr_nvram_realize(SpaprVioDevice *dev, Er= ror **errp) } } else if (nb_prom_envs > 0) { /* Create a system partition to pass the -prom-env variables */ - chrp_nvram_create_system_partition(nvram->buf, MIN_NVRAM_SIZE / 4); + chrp_nvram_create_system_partition(nvram->buf, MIN_NVRAM_SIZE / 4, + false); chrp_nvram_create_free_partition(&nvram->buf[MIN_NVRAM_SIZE / 4], nvram->size - MIN_NVRAM_SIZE / 4); } diff --git a/hw/sparc/sun4m.c b/hw/sparc/sun4m.c index 9be930415f8e..61804ccd4286 100644 --- a/hw/sparc/sun4m.c +++ b/hw/sparc/sun4m.c @@ -143,7 +143,7 @@ static void nvram_init(Nvram *nvram, uint8_t *macaddr, memset(image, '\0', sizeof(image)); =20 /* OpenBIOS nvram variables partition */ - sysp_end =3D chrp_nvram_create_system_partition(image, 0); + sysp_end =3D chrp_nvram_create_system_partition(image, 0, false); =20 /* Free space partition */ chrp_nvram_create_free_partition(&image[sysp_end], 0x1fd0 - sysp_end); diff --git a/hw/sparc64/sun4u.c b/hw/sparc64/sun4u.c index 9e30203dcc44..2409e739e81b 100644 --- a/hw/sparc64/sun4u.c +++ b/hw/sparc64/sun4u.c @@ -136,7 +136,7 @@ static int sun4u_NVRAM_set_params(Nvram *nvram, uint16_= t NVRAM_size, memset(image, '\0', sizeof(image)); =20 /* OpenBIOS nvram variables partition */ - sysp_end =3D chrp_nvram_create_system_partition(image, 0); + sysp_end =3D chrp_nvram_create_system_partition(image, 0, false); =20 /* Free space partition */ chrp_nvram_create_free_partition(&image[sysp_end], 0x1fd0 - sysp_end); diff --git a/include/hw/nvram/chrp_nvram.h b/include/hw/nvram/chrp_nvram.h index 09941a9be454..1d32dbf61331 100644 --- a/include/hw/nvram/chrp_nvram.h +++ b/include/hw/nvram/chrp_nvram.h @@ -50,7 +50,8 @@ chrp_nvram_finish_partition(ChrpNvramPartHdr *header, uin= t32_t size) header->checksum =3D sum & 0xff; } =20 -int chrp_nvram_create_system_partition(uint8_t *data, int min_len); +int chrp_nvram_create_system_partition(uint8_t *data, int min_len, + bool dry_run); int chrp_nvram_create_free_partition(uint8_t *data, int len); =20 #endif From nobody Tue May 21 09:16:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1597160060; cv=none; d=zohomail.com; s=zohoarc; b=B9yEiLEAIFhig3Yl2OAM+cccbj2SesEkdRh1h8Dy+ZQulQpB6boxDdJdLIAF1MgRK3NfY2F5+CqWqJ3M6Rn67IXqFHo0B/zZnm8eUtMX25gx74xqoxqbimT0B++Tr1C3kQt9A5PVvBC6gCwjmfVk9CqSYmgcjnqBmmcFPSTw1EA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597160060; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CyQ1i02EOQQAVVNaW7RiqlWBnwG83N+NaVoA1o0UDqQ=; b=F1e1B+7Qu7ZRAEM3IOVh5ddmvZBCCl2xqoO89S/FjrwZ04w2ib+iSvCCwNSxyDfjqScElyeXBdRHBV3fICPbslIGmNVV6otR5U/k207hePYQty36N9+o3hVRn0AoPNh/vojivftRKL0BOLJEnBfszT6+b9ixBJtJKtwsI66V+gI= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1597160060472734.291114780365; Tue, 11 Aug 2020 08:34:20 -0700 (PDT) Received: from localhost ([::1]:60928 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k5WI7-0003A9-55 for importer@patchew.org; Tue, 11 Aug 2020 11:34:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k5WEJ-0007j0-Rg for qemu-devel@nongnu.org; Tue, 11 Aug 2020 11:30:23 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:36645 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1k5WEH-0002P7-Kn for qemu-devel@nongnu.org; Tue, 11 Aug 2020 11:30:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-22-E2JI0fNdN9Gw7T28fXJKgA-1; Tue, 11 Aug 2020 11:30:16 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2960919067E0; Tue, 11 Aug 2020 15:30:15 +0000 (UTC) Received: from bahia.lan (ovpn-112-38.ams2.redhat.com [10.36.112.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id F137D8AD10; Tue, 11 Aug 2020 15:30:13 +0000 (UTC) X-MC-Unique: E2JI0fNdN9Gw7T28fXJKgA-1 Subject: [PATCH 2/2] spapr/nvram: Allocate enough space for all -prom-env options From: Greg Kurz To: Thomas Huth Date: Tue, 11 Aug 2020 17:30:13 +0200 Message-ID: <159715981316.1635409.16117540313443167075.stgit@bahia.lan> In-Reply-To: <159715979684.1635409.14107632395902701971.stgit@bahia.lan> References: <159715979684.1635409.14107632395902701971.stgit@bahia.lan> User-Agent: StGit/0.21 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=groug@kaod.org X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: kaod.org Content-Transfer-Encoding: quoted-printable 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: softfail client-ip=205.139.110.120; envelope-from=groug@kaod.org; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/11 02:18:06 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Mark Cave-Ayland , Laurent Vivier , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Since commit 61f20b9dc5b7 ("spapr_nvram: Pre-initialize the NVRAM to support the -prom-env parameter"), pseries machines can pre-initialize the "system" partition in the NVRAM with the data passed to all -prom-env parameters on the QEMU command line. In this cases it is assumed that all the data fits in 64 KiB, but the user can easily pass more and crash QEMU: $ qemu-system-ppc64 -M pseries $(for ((x=3D0;x<128;x++)); do \ echo -n " -prom-env "$(for ((y=3D0;y<1024;y++)); do echo -n x ; done) ; \ done) # this requires ~128 Kib malloc(): corrupted top size Aborted (core dumped) Call chrp_nvram_create_system_partition() first with its recently added parameter dry_run set to false, to know the required size and allocate the NVRAM buffer accordingly. Fixes: 61f20b9dc5b7 ("spapr_nvram: Pre-initialize the NVRAM to support the = -prom-env parameter") Signed-off-by: Greg Kurz --- hw/nvram/spapr_nvram.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hw/nvram/spapr_nvram.c b/hw/nvram/spapr_nvram.c index 992b818d34e7..1b74bec6200a 100644 --- a/hw/nvram/spapr_nvram.c +++ b/hw/nvram/spapr_nvram.c @@ -165,6 +165,10 @@ static void spapr_nvram_realize(SpaprVioDevice *dev, E= rror **errp) if (ret < 0) { return; } + } else if (nb_prom_envs > 0) { + nvram->size =3D chrp_nvram_create_system_partition(NULL, + MIN_NVRAM_SIZE / = 4, + true); } else { nvram->size =3D DEFAULT_NVRAM_SIZE; }