* [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read()
@ 2020-07-24 16:46 Andres Beltran
2020-07-24 17:10 ` Stephen Hemminger
0 siblings, 1 reply; 5+ messages in thread
From: Andres Beltran @ 2020-07-24 16:46 UTC (permalink / raw)
To: kys, haiyangz, sthemmin, wei.liu
Cc: linux-hyperv, linux-kernel, mikelley, parri.andrea, skarade,
Andres Beltran
Assignments to buffer_actual_len and requestid happen before packetlen
is checked to be within buflen. If this condition is true,
hv_ringbuffer_read() returns with these variables already set to some
value even though no data is actually read. This might create
inconsistencies in any routine calling hv_ringbuffer_read(). Assign values
to such pointers after the packetlen check.
Signed-off-by: Andres Beltran <lkmlabelt@gmail.com>
---
drivers/hv/ring_buffer.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
index 356e22159e83..e277ce7372a4 100644
--- a/drivers/hv/ring_buffer.c
+++ b/drivers/hv/ring_buffer.c
@@ -350,12 +350,13 @@ int hv_ringbuffer_read(struct vmbus_channel *channel,
offset = raw ? 0 : (desc->offset8 << 3);
packetlen = (desc->len8 << 3) - offset;
- *buffer_actual_len = packetlen;
- *requestid = desc->trans_id;
if (unlikely(packetlen > buflen))
return -ENOBUFS;
+ *buffer_actual_len = packetlen;
+ *requestid = desc->trans_id;
+
/* since ring is double mapped, only one copy is necessary */
memcpy(buffer, (const char *)desc + offset, packetlen);
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read()
2020-07-24 16:46 [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read() Andres Beltran
@ 2020-07-24 17:10 ` Stephen Hemminger
2020-07-24 17:48 ` Haiyang Zhang
2020-07-24 23:04 ` Andres Beltran
0 siblings, 2 replies; 5+ messages in thread
From: Stephen Hemminger @ 2020-07-24 17:10 UTC (permalink / raw)
To: Andres Beltran
Cc: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, wei.liu,
linux-hyperv, linux-kernel, Michael Kelley, parri.andrea,
Saruhan Karademir
On Fri, 24 Jul 2020 09:46:06 -0700
"Andres Beltran" <lkmlabelt@gmail.com> wrote:
> Assignments to buffer_actual_len and requestid happen before packetlen
> is checked to be within buflen. If this condition is true,
> hv_ringbuffer_read() returns with these variables already set to some
> value even though no data is actually read. This might create
> inconsistencies in any routine calling hv_ringbuffer_read(). Assign values
> to such pointers after the packetlen check.
>
> Signed-off-by: Andres Beltran <lkmlabelt@gmail.com>
> ---
> drivers/hv/ring_buffer.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
> index 356e22159e83..e277ce7372a4 100644
> --- a/drivers/hv/ring_buffer.c
> +++ b/drivers/hv/ring_buffer.c
> @@ -350,12 +350,13 @@ int hv_ringbuffer_read(struct vmbus_channel
> *channel,
>
> offset = raw ? 0 : (desc->offset8 << 3);
> packetlen = (desc->len8 << 3) - offset;
> - *buffer_actual_len = packetlen;
> - *requestid = desc->trans_id;
>
> if (unlikely(packetlen > buflen))
> return -ENOBUFS;
>
> + *buffer_actual_len = packetlen;
> + *requestid = desc->trans_id;
> +
> /* since ring is double mapped, only one copy is necessary */
> memcpy(buffer, (const char *)desc + offset, packetlen);
>
What is the rationale for this change, it may break other code.
A common API model in Windows world where this originated
is to have a call where caller first
makes request and then if the requested buffer is not big enough the
caller look at the actual length and allocate a bigger buffer.
Did you audit all the users of this API to make sure they aren't doing that.
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read()
2020-07-24 17:10 ` Stephen Hemminger
@ 2020-07-24 17:48 ` Haiyang Zhang
2020-07-24 23:04 ` Andres Beltran
1 sibling, 0 replies; 5+ messages in thread
From: Haiyang Zhang @ 2020-07-24 17:48 UTC (permalink / raw)
To: Stephen Hemminger, Andres Beltran
Cc: KY Srinivasan, Stephen Hemminger, wei.liu, linux-hyperv,
linux-kernel, Michael Kelley, parri.andrea, Saruhan Karademir
> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Friday, July 24, 2020 1:11 PM
> To: Andres Beltran <lkmlabelt@gmail.com>
> Cc: KY Srinivasan <kys@microsoft.com>; Haiyang Zhang
> <haiyangz@microsoft.com>; Stephen Hemminger <sthemmin@microsoft.com>;
> wei.liu@kernel.org; linux-hyperv@vger.kernel.org; linux-
> kernel@vger.kernel.org; Michael Kelley <mikelley@microsoft.com>;
> parri.andrea@gmail.com; Saruhan Karademir <skarade@microsoft.com>
> Subject: Re: [PATCH] Drivers: hv: vmbus: Fix variable assignments in
> hv_ringbuffer_read()
>
> On Fri, 24 Jul 2020 09:46:06 -0700
> "Andres Beltran" <lkmlabelt@gmail.com> wrote:
>
> > Assignments to buffer_actual_len and requestid happen before packetlen
> > is checked to be within buflen. If this condition is true,
> > hv_ringbuffer_read() returns with these variables already set to some
> > value even though no data is actually read. This might create
> > inconsistencies in any routine calling hv_ringbuffer_read(). Assign values
> > to such pointers after the packetlen check.
> >
> > Signed-off-by: Andres Beltran <lkmlabelt@gmail.com>
> > ---
> > drivers/hv/ring_buffer.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
> > index 356e22159e83..e277ce7372a4 100644
> > --- a/drivers/hv/ring_buffer.c
> > +++ b/drivers/hv/ring_buffer.c
> > @@ -350,12 +350,13 @@ int hv_ringbuffer_read(struct vmbus_channel
> > *channel,
> >
> > offset = raw ? 0 : (desc->offset8 << 3);
> > packetlen = (desc->len8 << 3) - offset;
> > - *buffer_actual_len = packetlen;
> > - *requestid = desc->trans_id;
> >
> > if (unlikely(packetlen > buflen))
> > return -ENOBUFS;
> >
> > + *buffer_actual_len = packetlen;
> > + *requestid = desc->trans_id;
> > +
> > /* since ring is double mapped, only one copy is necessary */
> > memcpy(buffer, (const char *)desc + offset, packetlen);
> >
>
> What is the rationale for this change, it may break other code.
>
> A common API model in Windows world where this originated
> is to have a call where caller first
> makes request and then if the requested buffer is not big enough the
> caller look at the actual length and allocate a bigger buffer.
>
> Did you audit all the users of this API to make sure they aren't doing that.
I took a quick look. I saw a case which treats the ringbuffer_read as
successful if the buffer_actual_len > 0. So if hv_ringbuffer_read() should
not be fixed this way, then all callers like balloon_onchannelcallback()
need a fix.
1476 static void balloon_onchannelcallback(void *context)
1477 {
1478 struct hv_device *dev = context;
1479 u32 recvlen;
1480 u64 requestid;
1481 struct dm_message *dm_msg;
1482 struct dm_header *dm_hdr;
1483 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1484 struct dm_balloon *bal_msg;
1485 struct dm_hot_add *ha_msg;
1486 union dm_mem_page_range *ha_pg_range;
1487 union dm_mem_page_range *ha_region;
1488
1489 memset(recv_buffer, 0, sizeof(recv_buffer));
1490 vmbus_recvpacket(dev->channel, recv_buffer,
1491 HV_HYP_PAGE_SIZE, &recvlen, &requestid);
1492
1493 if (recvlen > 0) {
1494 dm_msg = (struct dm_message *)recv_buffer;
1495 dm_hdr = &dm_msg->hdr;
Thanks,
- Haiyang
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read()
2020-07-24 17:10 ` Stephen Hemminger
2020-07-24 17:48 ` Haiyang Zhang
@ 2020-07-24 23:04 ` Andres Beltran
2020-07-26 23:53 ` Haiyang Zhang
1 sibling, 1 reply; 5+ messages in thread
From: Andres Beltran @ 2020-07-24 23:04 UTC (permalink / raw)
To: Stephen Hemminger
Cc: KY Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
linux-hyperv, linux-kernel, Michael Kelley, Andrea Parri,
Saruhan Karademir
On Fri, Jul 24, 2020 at 1:10 PM Stephen Hemminger
<stephen@networkplumber.org> wrote:
> What is the rationale for this change, it may break other code.
>
> A common API model in Windows world where this originated
> is to have a call where caller first
> makes request and then if the requested buffer is not big enough the
> caller look at the actual length and allocate a bigger buffer.
>
> Did you audit all the users of this API to make sure they aren't doing that.
>
The rationale for the change was to solve instances like the one
@Haiyang Zhang pointed out, especially in hv_utils, which needs
additional hardening. Unfortunately, there is an instance in
hv_pci_onchannelcallback() that does what you just described. Thus,
the fix will have to be made to all the callers of vmbus_recvpacket()
and vmbus_recvpacket_raw() to make sure they check the return value,
which most callers are not doing now. Thanks for pointing out this
behavior. I was not aware that the length can be checked by callers to
allocate a bigger buffer.
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read()
2020-07-24 23:04 ` Andres Beltran
@ 2020-07-26 23:53 ` Haiyang Zhang
0 siblings, 0 replies; 5+ messages in thread
From: Haiyang Zhang @ 2020-07-26 23:53 UTC (permalink / raw)
To: Andres Beltran, Stephen Hemminger
Cc: KY Srinivasan, Stephen Hemminger, Wei Liu, linux-hyperv,
linux-kernel, Michael Kelley, Andrea Parri, Saruhan Karademir
> -----Original Message-----
> From: Andres Beltran <lkmlabelt@gmail.com>
> Sent: Friday, July 24, 2020 7:04 PM
> To: Stephen Hemminger <stephen@networkplumber.org>
> Cc: KY Srinivasan <kys@microsoft.com>; Haiyang Zhang
> <haiyangz@microsoft.com>; Stephen Hemminger <sthemmin@microsoft.com>;
> Wei Liu <wei.liu@kernel.org>; linux-hyperv@vger.kernel.org; linux-
> kernel@vger.kernel.org; Michael Kelley <mikelley@microsoft.com>; Andrea
> Parri <parri.andrea@gmail.com>; Saruhan Karademir
> <skarade@microsoft.com>
> Subject: Re: [PATCH] Drivers: hv: vmbus: Fix variable assignments in
> hv_ringbuffer_read()
>
> On Fri, Jul 24, 2020 at 1:10 PM Stephen Hemminger
> <stephen@networkplumber.org> wrote:
> > What is the rationale for this change, it may break other code.
> >
> > A common API model in Windows world where this originated
> > is to have a call where caller first
> > makes request and then if the requested buffer is not big enough the
> > caller look at the actual length and allocate a bigger buffer.
> >
> > Did you audit all the users of this API to make sure they aren't doing that.
> >
>
> The rationale for the change was to solve instances like the one
> @Haiyang Zhang pointed out, especially in hv_utils, which needs
> additional hardening. Unfortunately, there is an instance in
> hv_pci_onchannelcallback() that does what you just described. Thus,
> the fix will have to be made to all the callers of vmbus_recvpacket()
> and vmbus_recvpacket_raw() to make sure they check the return value,
> which most callers are not doing now. Thanks for pointing out this
> behavior. I was not aware that the length can be checked by callers to
> allocate a bigger buffer.
To prevent future coding error, please add code comments for
hv_ringbuffer_read() to indicate that the buffer_actual_len may be
nonzero when the function fails, and should not be used to
determine if the function succeeds or not.
Thanks,
- Haiyang
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-07-26 23:53 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-24 16:46 [PATCH] Drivers: hv: vmbus: Fix variable assignments in hv_ringbuffer_read() Andres Beltran
2020-07-24 17:10 ` Stephen Hemminger
2020-07-24 17:48 ` Haiyang Zhang
2020-07-24 23:04 ` Andres Beltran
2020-07-26 23:53 ` Haiyang Zhang
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).