From: Alex Williamson <alex.williamson@redhat.com>
To: Laszlo Ersek <lersek@redhat.com>
Cc: weifuqiang@huawei.com, mst@redhat.com, qemu-devel@nongnu.org,
Markus Armbruster <armbru@redhat.com>,
arei.gonglei@huawei.com, huangzhichao@huawei.com,
"Longpeng \(Mike,
Cloud Infrastructure Service Product Dept.\)"
<longpeng2@huawei.com>
Subject: Re: [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read
Date: Tue, 10 Mar 2020 19:36:24 -0600 [thread overview]
Message-ID: <20200310193624.402fdb18@x1.home> (raw)
In-Reply-To: <467b2253-a065-91c3-5b0c-4f03ee236d0c@redhat.com>
On Wed, 11 Mar 2020 00:14:31 +0100
Laszlo Ersek <lersek@redhat.com> wrote:
> On 03/10/20 17:11, Alex Williamson wrote:
>
> > commit 2088fc1e1f426b98e9ca4d7bcdbe53d886a18c37
> > Author: Alex Williamson <alex.williamson@redhat.com>
> > Date: Tue Mar 10 10:04:36 2020 -0600
> >
> > vfio/pci: Use defined memcpy() behavior
> >
> > vfio_rom_read() relies on memcpy() doing the logically correct thing,
> > ie. safely copying zero bytes from a NULL pointer when rom_size is
> > zero, rather than the spec definition, which is undefined when the
> > source or target pointers are NULL. Resolve this by wrapping the
> > call in the condition expressed previously by the ternary.
> >
> > Additionally, we still use @val to fill data based on the provided
> > @size regardless of mempcy(), so we should initialize @val rather
> > than @data.
> >
> > Reported-by: Longpeng <longpeng2@huawei.com>
> > Reported-by: Laszlo Ersek <lersek@redhat.com>
> > Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
> >
> > diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> > index 5e75a95129ac..b0799cdc28ad 100644
> > --- a/hw/vfio/pci.c
> > +++ b/hw/vfio/pci.c
> > @@ -859,16 +859,17 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
> > uint16_t word;
> > uint32_t dword;
> > uint64_t qword;
> > - } val;
> > - uint64_t data = 0;
> > + } val = { 0 };
> > + uint64_t data;
> >
> > /* Load the ROM lazily when the guest tries to read it */
> > if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
> > vfio_pci_load_rom(vdev);
> > }
> >
> > - memcpy(&val, vdev->rom + addr,
> > - (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
> > + if (addr < vdev->rom_size) {
> > + memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
> > + }
> >
> > switch (size) {
> > case 1:
>
> Regarding the pre-patch code:
>
> My understanding is that the memcpy() could be reached with a
> guest-originated "addr" even if "vdev->rom" was NULL. If that's the
> case, then the pre-patch code invokes undefined behavior regardless of
> memcpy(), because it performs pointer arithmetic on a null pointer (not
> to mention that the type of that pointer is (void *)....)
>
> Regarding the proposed change:
>
> (addr < vdev->rom_size) requires that "vdev->rom_size" be positive. In
> that case, I assume that
>
> - "vdev->rom" is not NULL, and
> - MIN(size, vdev->rom_size - addr) bytes are "in range" for the object
> allocated at "vdev->rom".
>
> So from a memcpy() and range perspective, the patch looks OK. But
> there's still a wart I dislike: we should never perform pointer
> arithmetic on a (void*). I suggest casting (vdev->rom) to (uint8_t*) or
> (unsigned char*) first.
>
> Here's an excerpt from the ISO C99 standard:
>
> -v-
> 6.5.6 Additive operators
>
> Constraints
>
> 2 For addition, either both operands shall have arithmetic type, or one
> operand shall be a pointer to an object type and the other shall have
> integer type. [...]
> -^-
>
> A "pointer-to-void" is not a "pointer to an object type", because "void"
> is not an object type -- it is an incomplete type that cannot be completed:
>
> -v-
> 6.2.5 Types
>
> 1 [...] Types are partitioned into object types (types that fully
> describe objects), function types (types that describe functions), and
> incomplete types (types that describe objects but lack information
> needed to determine their sizes).
>
> [...]
>
> 19 The void type comprises an empty set of values; it is an incomplete
> type that cannot be completed.
> -^-
>
> For a different illustration, (vdev->rom + addr) is equivalent to
> &(vdev->rom[addr]) -- and we clearly can't have an "array of void".
>
> This anti-pattern (of doing pointer arithmetic on (void*)) likely comes
> from a guarantee that the standard does make, in the same "6.2.5 Types"
> section:
>
> -v-
> 27 A pointer to void shall have the same representation and alignment
> requirements as a pointer to a character type. 39) [...]
>
> Footnote 39: The same representation and alignment requirements are
> meant to imply interchangeability as arguments to
> functions, return values from functions, and members of
> unions.
> -^-
>
> It does not extend to the "+" operator.
GNU C specifically allows arithmetic on pointers and defines the size
of a void as 1. I'll comply, but this makes me want to stab myself in
the face :-\ Thanks,
Alex
next prev parent reply other threads:[~2020-03-11 1:37 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-24 6:42 [PATCH RESEND 0/3] fix some warnings by static code scan tool Longpeng(Mike)
2020-02-24 6:42 ` [PATCH RESEND 1/3] vfio/pci: fix a null pointer reference in vfio_rom_read Longpeng(Mike)
2020-02-24 16:04 ` Alex Williamson
2020-02-24 23:48 ` Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
2020-03-10 16:11 ` Alex Williamson
2020-03-10 23:14 ` Laszlo Ersek
2020-03-11 1:36 ` Alex Williamson [this message]
2020-03-11 7:08 ` Markus Armbruster
2020-03-11 10:28 ` Laszlo Ersek
2020-03-11 10:26 ` Laszlo Ersek
2020-03-11 11:54 ` Markus Armbruster
2020-03-11 13:00 ` Laszlo Ersek
2020-03-11 7:04 ` Markus Armbruster
[not found] ` <20200311093939.494bfe27@w520.home>
2020-03-12 5:50 ` Markus Armbruster
2020-03-12 14:07 ` Alex Williamson
2020-02-24 6:42 ` [PATCH RESEND 2/3] vhost: fix a null pointer reference of vhost_log Longpeng(Mike)
2020-03-10 2:11 ` Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
2020-03-10 5:57 ` Michael S. Tsirkin
2020-03-10 8:04 ` Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
2020-03-10 8:23 ` Michael S. Tsirkin
2020-03-10 12:02 ` Longpeng (Mike)
2020-03-10 12:18 ` Michael S. Tsirkin
2020-02-24 6:42 ` [PATCH RESEND 3/3] util/pty: fix a null pointer reference in qemu_openpty_raw Longpeng(Mike)
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=20200310193624.402fdb18@x1.home \
--to=alex.williamson@redhat.com \
--cc=arei.gonglei@huawei.com \
--cc=armbru@redhat.com \
--cc=huangzhichao@huawei.com \
--cc=lersek@redhat.com \
--cc=longpeng2@huawei.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=weifuqiang@huawei.com \
/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).