From: Jonathan Cameron via <qemu-devel@nongnu.org>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: "Michael S. Tsirkin" <mst@redhat.com>, <qemu-devel@nongnu.org>,
"Huai-Cheng Kuo" <hchkuo@avery-design.com.tw>,
Chris Browy <cbrowy@avery-design.com>,
Ben Widawsky <ben.widawsky@intel.com>
Subject: Re: [PULL v4 23/83] hw/cxl/cdat: CXL CDAT Data Object Exchange implementation
Date: Wed, 12 Apr 2023 15:39:57 +0100 [thread overview]
Message-ID: <20230412153957.000056fc@Huawei.com> (raw)
In-Reply-To: <CAFEAcA-g0w4K6KfEP0MmHGRhL_8LmfxPBYiLSMG4KMakKHe=UA@mail.gmail.com>
On Tue, 11 Apr 2023 16:52:58 +0100
Peter Maydell <peter.maydell@linaro.org> wrote:
> On Mon, 7 Nov 2022 at 22:49, Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > From: Huai-Cheng Kuo <hchkuo@avery-design.com.tw>
> >
> > The Data Object Exchange implementation of CXL Coherent Device Attribute
> > Table (CDAT). This implementation is referring to "Coherent Device
> > Attribute Table Specification, Rev. 1.03, July. 2022" and "Compute
> > Express Link Specification, Rev. 3.0, July. 2022"
> >
> > This patch adds core support that will be shared by both
> > end-points and switch port emulation.
>
> > +static void ct3_load_cdat(CDATObject *cdat, Error **errp)
> > +{
> > + g_autofree CDATEntry *cdat_st = NULL;
> > + uint8_t sum = 0;
> > + int num_ent;
> > + int i = 0, ent = 1, file_size = 0;
> > + CDATSubHeader *hdr;
> > + FILE *fp = NULL;
> > +
> > + /* Read CDAT file and create its cache */
> > + fp = fopen(cdat->filename, "r");
> > + if (!fp) {
> > + error_setg(errp, "CDAT: Unable to open file");
> > + return;
> > + }
> > +
> > + fseek(fp, 0, SEEK_END);
> > + file_size = ftell(fp);
>
> Coverity points out that ftell() can fail and return -1...
>
> > + fseek(fp, 0, SEEK_SET);
> > + cdat->buf = g_malloc0(file_size);
>
> ...which would cause an attempt to allocate a very large
> amount of memory, since you aren't checking for errors.
> CID 1508185.
>
> Below, some other issues I saw in a quick scan through the code.
>
> > +
> > + if (fread(cdat->buf, file_size, 1, fp) == 0) {
> > + error_setg(errp, "CDAT: File read failed");
> > + return;
> > + }
>
> (The issues in this bit of code I've mentioned in a
> different thread.)
I'll carry a patch locally using g_file_get_contents()
that gets rid of this mess. My assumption being that similar
will emerge from other thread and I can drop my patch.
>
> > +
> > + fclose(fp);
> > +
> > + if (file_size < sizeof(CDATTableHeader)) {
> > + error_setg(errp, "CDAT: File too short");
> > + return;
> > + }
>
> > + i = sizeof(CDATTableHeader);
> > + num_ent = 1;
> > + while (i < file_size) {
> > + hdr = (CDATSubHeader *)(cdat->buf + i);
>
> If the file is not a complete number of records in
> size, then this can index off the end of the buffer.
> You should check for that.
>
> > + cdat_len_check(hdr, errp);
> > + i += hdr->length;
> > + num_ent++;
> > + }
> > + if (i != file_size) {
> > + error_setg(errp, "CDAT: File length missmatch");
>
> Typo: "mismatch"
>
That's fixed in the tree.
> > + return;
> > + }
> > +
> > + cdat_st = g_malloc0(sizeof(*cdat_st) * num_ent);
>
> To allocate an array of N lots of a structure, use
> g_new0(), which will take care to avoid possible
> overflow in the multiply.
>
> > + if (!cdat_st) {
> > + error_setg(errp, "CDAT: Failed to allocate entry array");
>
> g_malloc0() and g_new0() can never fail, so you don't need
> to check for a NULL pointer return.
dropped. I'll remember this one day ;)
>
> > + return;
> > + }
> > +
> > + /* Set CDAT header, Entry = 0 */
> > + cdat_st[0].base = cdat->buf;
> > + cdat_st[0].length = sizeof(CDATTableHeader);
> > + i = 0;
> > +
> > + while (i < cdat_st[0].length) {
> > + sum += cdat->buf[i++];
> > + }
> > +
> > + /* Read CDAT structures */
> > + while (i < file_size) {
> > + hdr = (CDATSubHeader *)(cdat->buf + i);
> > + cdat_len_check(hdr, errp);
>
> We already did this check the first time through the data...
dropped
>
> > +
> > + cdat_st[ent].base = hdr;
> > + cdat_st[ent].length = hdr->length;
> > +
> > + while (cdat->buf + i <
> > + (uint8_t *)cdat_st[ent].base + cdat_st[ent].length) {
> > + assert(i < file_size);
> > + sum += cdat->buf[i++];
> > + }
> > +
> > + ent++;
> > + }
> > +
> > + if (sum != 0) {
> > + warn_report("CDAT: Found checksum mismatch in %s", cdat->filename);
> > + }
> > + cdat->entry_len = num_ent;
> > + cdat->entry = g_steal_pointer(&cdat_st);
> > +}
> > +
> > +void cxl_doe_cdat_init(CXLComponentState *cxl_cstate, Error **errp)
> > +{
> > + CDATObject *cdat = &cxl_cstate->cdat;
> > +
> > + if (cdat->filename) {
> > + ct3_load_cdat(cdat, errp);
> > + } else {
> > + ct3_build_cdat(cdat, errp);
> > + }
> > +}
>
> None of the callsites to this function check for it
> failing. In particular they do not assume "if I call
> this and it fails then I need to call cxl_doe_cdata_release()
> to have it clean up". It would probably be less confusing
> if the init function cleans up after itself, i.e. does not
> leave allocated memory pointed to by cdat->buf and so on.
Agreed. Will make it cleanup and add the error checks at the two
callers.
Thanks,
Jonathan
>
> thanks
> -- PMM
next prev parent reply other threads:[~2023-04-12 14:40 UTC|newest]
Thread overview: 150+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-07 22:47 [PULL v4 00/83] pci,pc,virtio: features, tests, fixes, cleanups Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 01/83] hw/i386/e820: remove legacy reserved entries for e820 Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 02/83] tests/acpi: allow SSDT changes Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 03/83] acpi/ssdt: Fix aml_or() and aml_and() in if clause Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 04/83] acpi/nvdimm: define macro for NVDIMM Device _DSM Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 05/83] acpi/nvdimm: Implement ACPI NVDIMM Label Methods Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 06/83] test/acpi/bios-tables-test: SSDT: update golden master binaries Michael S. Tsirkin
2022-11-07 22:47 ` [PULL v4 07/83] virtio-crypto: Support asynchronous mode Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 08/83] crypto: Support DER encodings Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 09/83] crypto: Support export akcipher to pkcs8 Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 10/83] cryptodev: Add a lkcf-backend for cryptodev Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 11/83] acpi/tests/avocado/bits: initial commit of test scripts that are run by biosbits Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 12/83] acpi/tests/avocado/bits: disable acpi PSS tests that are failing in biosbits Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 13/83] acpi/tests/avocado/bits: add biosbits config file for running bios tests Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 14/83] acpi/tests/avocado/bits: add acpi and smbios avocado tests that uses biosbits Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 15/83] acpi/tests/avocado/bits/doc: add a doc file to describe the acpi bits test Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 16/83] MAINTAINERS: add myself as the maintainer for acpi biosbits avocado tests Michael S. Tsirkin
2022-11-07 22:48 ` [PULL v4 17/83] tests/acpi: virt: allow acpi MADT and FADT changes Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 18/83] acpi: fadt: support revision 6.0 of the ACPI specification Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 19/83] acpi: arm/virt: madt: bump to revision 4 accordingly to ACPI 6.0 Errata A Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 20/83] tests/acpi: virt: update ACPI MADT and FADT binaries Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 21/83] hw/pci: PCIe Data Object Exchange emulation Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 22/83] hw/mem/cxl-type3: Add MSIX support Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 23/83] hw/cxl/cdat: CXL CDAT Data Object Exchange implementation Michael S. Tsirkin
2023-04-11 15:52 ` Peter Maydell
2023-04-12 13:08 ` Jonathan Cameron via
2023-04-12 14:39 ` Jonathan Cameron via [this message]
2022-11-07 22:49 ` [PULL v4 24/83] hw/mem/cxl-type3: Add CXL CDAT Data Object Exchange Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 25/83] hw/pci-bridge/cxl-upstream: Add a CDAT table access DOE Michael S. Tsirkin
2023-06-23 13:23 ` Peter Maydell
2022-11-07 22:49 ` [PULL v4 26/83] hw/virtio/virtio-iommu-pci: Enforce the device is plugged on the root bus Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 27/83] virtio: introduce __virtio_queue_reset() Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 28/83] virtio: introduce virtio_queue_reset() Michael S. Tsirkin
2022-11-07 22:49 ` [PULL v4 29/83] virtio: introduce virtio_queue_enable() Michael S. Tsirkin
2022-11-08 19:43 ` Stefan Hajnoczi
2022-11-09 3:31 ` Jason Wang
2022-11-09 6:39 ` Michael S. Tsirkin
2022-11-09 6:48 ` Xuan Zhuo
2022-11-09 7:01 ` Michael S. Tsirkin
2022-11-09 7:11 ` Xuan Zhuo
2022-11-09 6:51 ` Michael S. Tsirkin
2022-11-09 6:55 ` Jason Wang
2022-11-09 6:56 ` Xuan Zhuo
2022-11-09 7:04 ` Michael S. Tsirkin
2022-11-09 7:12 ` Xuan Zhuo
2022-11-09 6:59 ` Michael S. Tsirkin
2022-11-09 8:56 ` Laszlo Ersek
2022-11-09 11:00 ` Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 30/83] virtio: core: vq reset feature negotation support Michael S. Tsirkin
2022-11-18 14:32 ` Stefano Garzarella
2022-11-18 14:39 ` Stefano Garzarella
2022-11-19 17:19 ` Michael S. Tsirkin
2022-11-21 6:17 ` Jason Wang
2022-11-21 7:07 ` Michael S. Tsirkin
2022-11-21 8:20 ` Stefano Garzarella
2022-11-07 22:50 ` [PULL v4 31/83] virtio-pci: support queue reset Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 32/83] virtio-pci: support queue enable Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 33/83] vhost: expose vhost_virtqueue_start() Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 34/83] vhost: expose vhost_virtqueue_stop() Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 35/83] vhost-net: vhost-kernel: introduce vhost_net_virtqueue_reset() Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 36/83] vhost-net: vhost-kernel: introduce vhost_net_virtqueue_restart() Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 37/83] virtio-net: introduce flush_or_purge_queued_packets() Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 38/83] virtio-net: support queue reset Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 39/83] virtio-net: support queue_enable Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 40/83] vhost: vhost-kernel: enable vq reset feature Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 41/83] virtio-net: " Michael S. Tsirkin
2022-11-07 22:50 ` [PULL v4 42/83] virtio-rng-pci: Allow setting nvectors, so we can use MSI-X Michael S. Tsirkin
2023-01-09 10:20 ` Dr. David Alan Gilbert
2023-01-09 10:38 ` Michael S. Tsirkin
2023-01-09 10:43 ` Dr. David Alan Gilbert
2022-11-07 22:51 ` [PULL v4 43/83] vhost-user: Fix out of order vring host notification handling Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 44/83] acpi: pc: vga: use AcpiDevAmlIf interface to build VGA device descriptors Michael S. Tsirkin
2022-11-09 17:39 ` Laurent Vivier
2022-11-09 21:42 ` Michael S. Tsirkin
2022-11-10 9:28 ` Peter Maydell
2022-11-11 9:43 ` Igor Mammedov
2022-11-11 10:25 ` Igor Mammedov
2022-11-10 2:55 ` Ani Sinha
2022-11-07 22:51 ` [PULL v4 45/83] tests: acpi: whitelist DSDT before generating PCI-ISA bridge AML automatically Michael S. Tsirkin
2022-11-08 13:36 ` Igor Mammedov
2022-11-08 13:39 ` Ani Sinha
2022-11-08 14:06 ` Ani Sinha
2022-11-08 13:49 ` Michael S. Tsirkin
2022-11-08 14:31 ` Igor Mammedov
2022-11-07 22:51 ` [PULL v4 46/83] acpi: pc/q35: drop ad-hoc PCI-ISA bridge AML routines and let bus ennumeration generate AML Michael S. Tsirkin
2022-11-17 21:51 ` Volker Rümelin
2022-11-18 13:08 ` Igor Mammedov
2022-11-18 14:55 ` Igor Mammedov
2022-11-19 8:49 ` Volker Rümelin
2022-11-21 7:27 ` Igor Mammedov
2022-11-19 17:22 ` Michael S. Tsirkin
2022-11-21 7:23 ` Igor Mammedov
2022-11-21 7:50 ` Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 47/83] tests: acpi: update expected DSDT after ISA bridge is moved directly under PCI host bridge Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 48/83] tests: acpi: whitelist DSDT before generating ICH9_SMB AML automatically Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 49/83] acpi: add get_dev_aml_func() helper Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 50/83] acpi: enumerate SMB bridge automatically along with other PCI devices Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 51/83] tests: acpi: update expected blobs Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 52/83] tests: acpi: pc/q35 whitelist DSDT before \_GPE cleanup Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 53/83] acpi: pc/35: sanitize _GPE declaration order Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 54/83] tests: acpi: update expected blobs Michael S. Tsirkin
2022-11-07 22:51 ` [PULL v4 55/83] hw/acpi/erst.c: Fix memory handling issues Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 56/83] MAINTAINERS: Add qapi/virtio.json to section "virtio" Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 57/83] msix: Assert that specified vector is in range Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 58/83] hw/i386/pc.c: CXL Fixed Memory Window should not reserve e820 in bios Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 59/83] hw/i386/acpi-build: Remove unused struct Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 60/83] hw/i386/acpi-build: Resolve redundant attribute Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 61/83] hw/i386/acpi-build: Resolve north rather than south bridges Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 62/83] hmat acpi: Don't require initiator value in -numa Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 63/83] tests: acpi: add and whitelist *.hmat-noinitiator expected blobs Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 64/83] tests: acpi: q35: add test for hmat nodes without initiators Michael S. Tsirkin
2022-11-07 22:52 ` [PULL v4 65/83] tests: acpi: q35: update expected blobs *.hmat-noinitiators expected HMAT: Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 66/83] tests: Add HMAT AArch64/virt empty table files Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 67/83] hw/arm/virt: Enable HMAT on arm virt machine Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 68/83] tests: acpi: aarch64/virt: add a test for hmat nodes with no initiators Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 69/83] tests: virt: Update expected *.acpihmatvirt tables Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 70/83] vfio: move implement of vfio_get_xlat_addr() to memory.c Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 71/83] intel-iommu: don't warn guest errors when getting rid2pasid entry Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 72/83] intel-iommu: drop VTDBus Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 73/83] intel-iommu: convert VTD_PE_GET_FPD_ERR() to be a function Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 74/83] intel-iommu: PASID support Michael S. Tsirkin
2023-04-06 16:22 ` Peter Maydell
2023-04-10 2:55 ` Jason Wang
2022-11-07 22:53 ` [PULL v4 75/83] vhost: Change the sequence of device start Michael S. Tsirkin
2022-11-07 22:53 ` [PULL v4 76/83] vhost-user: Support vhost_dev_start Michael S. Tsirkin
2023-01-06 14:21 ` Laurent Vivier
2023-01-09 10:55 ` Michael S. Tsirkin
2023-01-11 9:50 ` Laurent Vivier
2023-01-12 5:46 ` Yajun Wu
2023-01-12 9:25 ` Maxime Coquelin
2023-01-16 7:14 ` Yajun Wu
2023-01-17 9:49 ` Maxime Coquelin
2023-01-17 12:12 ` Greg Kurz
2023-01-17 12:36 ` Greg Kurz
2023-01-17 15:07 ` Maxime Coquelin
2023-01-17 17:55 ` Greg Kurz
2023-01-17 18:21 ` Greg Kurz
2023-01-18 11:01 ` Michael S. Tsirkin
2023-01-19 19:48 ` Dr. David Alan Gilbert
2022-11-07 22:53 ` [PULL v4 77/83] hw/smbios: add core_count2 to smbios table type 4 Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 78/83] bios-tables-test: teach test to use smbios 3.0 tables Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 79/83] tests/acpi: allow changes for core_count2 test Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 80/83] bios-tables-test: add test for number of cores > 255 Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 81/83] tests/acpi: update tables for new core count test Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 82/83] hw/virtio: introduce virtio_device_should_start Michael S. Tsirkin
2022-11-07 22:54 ` [PULL v4 83/83] checkpatch: better pattern for inline comments Michael S. Tsirkin
2022-11-08 6:23 ` [PULL v4 00/83] pci,pc,virtio: features, tests, fixes, cleanups Michael S. Tsirkin
2022-11-08 13:47 ` Stefan Hajnoczi
2022-11-15 14:01 ` Philippe Mathieu-Daudé
2022-11-16 10:42 ` Igor Mammedov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230412153957.000056fc@Huawei.com \
--to=qemu-devel@nongnu.org \
--cc=Jonathan.Cameron@Huawei.com \
--cc=ben.widawsky@intel.com \
--cc=cbrowy@avery-design.com \
--cc=hchkuo@avery-design.com.tw \
--cc=mst@redhat.com \
--cc=peter.maydell@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).