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>,
Fan Ni <fan.ni@samsung.com>,
Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Subject: Re: [PULL 53/60] hw/cxl: Standardize all references on CXL r3.1 and minor updates
Date: Fri, 8 Mar 2024 14:34:20 +0000 [thread overview]
Message-ID: <20240308143420.0000536e@Huawei.com> (raw)
In-Reply-To: <CAFEAcA_W8BxG6rpcao2hCYntfU9aQfAzHQiy6RJQ-v3ZB4sNZg@mail.gmail.com>
On Fri, 8 Mar 2024 13:47:47 +0000
Peter Maydell <peter.maydell@linaro.org> wrote:
> On Wed, 14 Feb 2024 at 11:16, Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >
> > Previously not all references mentioned any spec version at all.
> > Given r3.1 is the current specification available for evaluation at
> > www.computeexpresslink.org update references to refer to that.
> > Hopefully this won't become a never ending job.
> >
> > A few structure definitions have been updated to add new fields.
> > Defaults of 0 and read only are valid choices for these new DVSEC
> > registers so go with that for now.
> >
> > There are additional error codes and some of the 'questions' in
> > the comments are resolved now.
> >
> > Update documentation reference to point to the CXL r3.1 specification
> > with naming closer to what is on the cover.
> >
> > For cases where there are structure version numbers, add defines
> > so they can be found next to the register definitions.
>
> Hi; Coverity points out that this change has introduced a
> buffer overrun (CID 1534905). In hw/mem/cxl_type3.c:build_dvsecs()
> we create a local struct of type CXLDVSecDevice, and then we
> pass it to cxl_component_create_dvsec() as the body parameter,
> passing it a length argument PCIE_CXL_DEVICE_DVSEC_LENGTH.
>
> Before this change, both sizeof(CXLDVSecDevice) and
> PCIE_CXL_DEVICE_DVSEC_LENGTH were 0x38, so this was fine.
> But now...
>
> > diff --git a/include/hw/cxl/cxl_pci.h b/include/hw/cxl/cxl_pci.h
> > index ddf01a543b..265db6c407 100644
> > --- a/include/hw/cxl/cxl_pci.h
> > +++ b/include/hw/cxl/cxl_pci.h
> > @@ -16,9 +16,8 @@
> > #define PCIE_DVSEC_HEADER1_OFFSET 0x4 /* Offset from start of extend cap */
> > #define PCIE_DVSEC_ID_OFFSET 0x8
> >
> > -#define PCIE_CXL_DEVICE_DVSEC_LENGTH 0x38
> > -#define PCIE_CXL1_DEVICE_DVSEC_REVID 0
> > -#define PCIE_CXL2_DEVICE_DVSEC_REVID 1
> > +#define PCIE_CXL_DEVICE_DVSEC_LENGTH 0x3C
> > +#define PCIE_CXL31_DEVICE_DVSEC_REVID 3
> >
> > #define EXTENSIONS_PORT_DVSEC_LENGTH 0x28
> > #define EXTENSIONS_PORT_DVSEC_REVID 0
>
> ...PCIE_CXL_DEVICE_DVSEC_LENGTH is 0x3C...
Gah. Evil spec change - they defined only one extra
u16 worth of data but added padding after it and I missed
that in the structure definition.
>
> > -/* CXL 2.0 - 8.1.3 (ID 0001) */
> > +/*
> > + * CXL r3.1 Section 8.1.3: PCIe DVSEC for Devices
> > + * DVSEC ID: 0, Revision: 3
> > + */
> > typedef struct CXLDVSECDevice {
> > DVSECHeader hdr;
> > uint16_t cap;
> > @@ -82,10 +91,14 @@ typedef struct CXLDVSECDevice {
> > uint32_t range2_size_lo;
> > uint32_t range2_base_hi;
> > uint32_t range2_base_lo;
> > -} CXLDVSECDevice;
> > -QEMU_BUILD_BUG_ON(sizeof(CXLDVSECDevice) != 0x38);
> > + uint16_t cap3;
> > +} QEMU_PACKED CXLDVSECDevice;
> > +QEMU_BUILD_BUG_ON(sizeof(CXLDVSECDevice) != 0x3A);
(this is the assert I mention below)
>
> ...and CXLDVSECDevice is only size 0x3A, so we try to read off the
> end of the struct.
>
> What was supposed to happen here?
needs an extra uint16_t resv; at the end.
>
> > --- a/hw/mem/cxl_type3.c
> > +++ b/hw/mem/cxl_type3.c
> > @@ -319,7 +319,7 @@ static void build_dvsecs(CXLType3Dev *ct3d)
> > cxl_component_create_dvsec(cxl_cstate, CXL2_TYPE3_DEVICE,
> > PCIE_CXL_DEVICE_DVSEC_LENGTH,
> > PCIE_CXL_DEVICE_DVSEC,
> > - PCIE_CXL2_DEVICE_DVSEC_REVID, dvsec);
> > + PCIE_CXL31_DEVICE_DVSEC_REVID, dvsec);
> >
> > dvsec = (uint8_t *)&(CXLDVSECRegisterLocator){
> > .rsvd = 0,
>
> Perhaps this call to cxl_component_create_dvsec() was
> supposed to have the length argument changed, as seems
> to have been done with this other call:
>
> > @@ -346,9 +346,9 @@ static void build_dvsecs(CXLType3Dev *ct3d)
> > .rcvd_mod_ts_data_phase1 = 0xef, /* WTF? */
> > };
> > cxl_component_create_dvsec(cxl_cstate, CXL2_TYPE3_DEVICE,
> > - PCIE_FLEXBUS_PORT_DVSEC_LENGTH_2_0,
> > + PCIE_CXL3_FLEXBUS_PORT_DVSEC_LENGTH,
> > PCIE_FLEXBUS_PORT_DVSEC,
> > - PCIE_FLEXBUS_PORT_DVSEC_REVID_2_0, dvsec);
> > + PCIE_CXL3_FLEXBUS_PORT_DVSEC_REVID, dvsec);
> > }
>
> static void hdm_decoder_commit(CXLType3Dev *ct3d, int which)
>
>
> and with similar other calls in the commit ?
>
> Is there a way we could write this that would catch this error?
> I'm thinking maybe something like
>
> #define CXL_CREATE_DVSEC(CXL, DEVTYPE, TYPE, DATA) do { \
> assert(sizeof(*DATA) == TYPE##_LENGTH); \
> cxl_component_create_dvsec(CXL, DEVTYPE, TYPE##_LENGTH, \
> TYPE, TYPE##_REVID, (uint8_t*)DATA); \
> } while (0)
We should be able to use the length definitions in the original assert.
I'm not sure why that wasn't done before. I think there were some cases
where we supported multiple versions and so the length can be shorter
than the structure defintion but that doesn't matter on this one.
So I think minimal fix is u16 of padding and update the assert.
Can circle back to tidy up the multiple places the value is defined.
Any mismatch in which the wrong length define is used should be easy
enough to spot so not sure we need the macro you suggest.
I'll send a patch shortly.
Thanks,
Jonathan
>
> thanks
> -- PMM
next prev parent reply other threads:[~2024-03-08 14:34 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-14 11:13 [PULL 00/60] virtio,pc,pci: features, cleanups, fixes Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 01/60] virtio: split into vhost-user-base and vhost-user-device Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 02/60] hw/virtio: convert vhost-user-base to async shutdown Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 03/60] hw/virtio: derive vhost-user-rng from vhost-user-base Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 04/60] hw/virtio: derive vhost-user-gpio " Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 05/60] hw/virtio: derive vhost-user-i2c " Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 06/60] hw/virtio: add vhost-user-snd and vhost-user-snd-pci devices Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 07/60] docs/system: add a basic enumeration of vhost-user devices Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 08/60] hw/virtio: Support set_config() callback in vhost-user-base Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 09/60] docs/system: Add vhost-user-input documentation Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 10/60] hw/virtio: Move vhost-user-input into virtio folder Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 11/60] hw/virtio: derive vhost-user-input from vhost-user-base Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 12/60] i386/tcg: implement x2APIC registers MSR access Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 13/60] apic: add support for x2APIC mode Michael S. Tsirkin
2024-02-14 11:13 ` [PULL 14/60] apic, i386/tcg: add x2apic transitions Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 15/60] intel_iommu: allow Extended Interrupt Mode when using userspace APIC Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 16/60] test: bios-tables-test: prepare IVRS change in ACPI table Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 17/60] amd_iommu: report x2APIC support to the operating system Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 18/60] test: bios-tables-test: add IVRS changed binary Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 19/60] hw/i386/x86: Reverse if statement Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 20/60] hw/i386/x86: Fix PIC interrupt handling if APIC is globally disabled Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 21/60] target/i386/cpu: Fix typo in comment Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 22/60] hw/block/fdc-isa: Move portio_list from FDCtrl to FDCtrlISABus Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 23/60] hw/block/fdc-sysbus: Move iomem from FDCtrl to FDCtrlSysBus Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 24/60] hw/char/parallel: Move portio_list from ParallelState to ISAParallelState Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 25/60] exec/ioport: Resolve redundant .base attribute in struct MemoryRegionPortio Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 26/60] exec/ioport: Add portio_list_set_address() Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 27/60] exec/ioport: Add portio_list_set_enabled() Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 28/60] hw/block/fdc-isa: Implement relocation and enabling/disabling for TYPE_ISA_FDC Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 29/60] hw/char/serial-isa: Implement relocation and enabling/disabling for TYPE_ISA_SERIAL Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 30/60] hw/char/parallel-isa: Implement relocation and enabling/disabling for TYPE_ISA_PARALLEL Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 31/60] hw/ppc/pegasos2: Let pegasos2 machine configure SuperI/O functions Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 32/60] hw/isa/vt82c686: Implement relocation and toggling of " Michael S. Tsirkin
2024-02-14 11:14 ` [PULL 33/60] vhost-user.rst: Fix vring address description Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 34/60] MAINTAINERS: Drop myself as VT-d maintainers Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 35/60] virtio_iommu: Clear IOMMUPciBus pointer cache when system reset Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 36/60] smmu: Clear SMMUPciBus " Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 37/60] cxl/cdat: Handle cdat table build errors Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 38/60] hw/mem/cxl_type3: Drop handling of failure of g_malloc0() and g_malloc() Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 39/60] hw/pci-bridge/cxl_upstream: Drop g_malloc() failure handling Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 40/60] cxl/cdat: Fix header sum value in CDAT checksum Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 41/60] hw/cxl/mbox: Remove dead code Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 42/60] hw/cxl/device: read from register values in mdev_reg_read() Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 43/60] hw/cxl: Pass CXLComponentState to cache_mem_ops Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 44/60] hw/cxl: Pass NULL for a NULL MemoryRegionOps Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 45/60] hw/mem/cxl_type3: Fix potential divide by zero reported by coverity Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 46/60] tests/acpi: Allow update of DSDT.cxl Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 47/60] hw/i386: Fix _STA return value for ACPI0017 Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 48/60] tests/acpi: Update DSDT.cxl to reflect change _STA return value Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 49/60] hw/cxl: Update HDM Decoder capability to version 3 Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 50/60] hw/cxl: Update link register definitions Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 51/60] hw/cxl: Update RAS Capability Definitions for version 3 Michael S. Tsirkin
2024-02-14 11:15 ` [PULL 52/60] hw/cxl: Update mailbox status registers Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 53/60] hw/cxl: Standardize all references on CXL r3.1 and minor updates Michael S. Tsirkin
2024-03-08 13:47 ` Peter Maydell
2024-03-08 14:34 ` Jonathan Cameron via [this message]
2024-03-08 14:38 ` Peter Maydell
2024-03-08 15:07 ` Jonathan Cameron via
2024-02-14 11:16 ` [PULL 54/60] virtio-gpu: Correct virgl_renderer_resource_get_info() error check Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 55/60] hw/smbios: Fix OEM strings table option validation Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 56/60] hw/smbios: Fix port connector " Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 57/60] hw/display/virtio-gpu.c: use reset_bh class method Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 58/60] virtio-gpu.c: add resource_destroy " Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 59/60] virtio-gpu-rutabaga.c: override resource_destroy method Michael S. Tsirkin
2024-02-14 11:16 ` [PULL 60/60] MAINTAINERS: Switch to my Enfabrica email Michael S. Tsirkin
2024-02-14 11:19 ` [PULL 00/60] virtio,pc,pci: features, cleanups, fixes Michael S. Tsirkin
2024-02-14 17:32 ` Peter Maydell
2024-02-15 9:20 ` Michael Tokarev
2024-02-15 13:39 ` Michael S. Tsirkin
2024-02-15 13:51 ` Michael Tokarev
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=20240308143420.0000536e@Huawei.com \
--to=qemu-devel@nongnu.org \
--cc=Jonathan.Cameron@Huawei.com \
--cc=fan.ni@samsung.com \
--cc=marcel.apfelbaum@gmail.com \
--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).