Re: [PATCH] hyper-v: Check for ring buffer in hv_get_bytes_to_read/write

From: Mohammed Gamal
Date: Thu Mar 07 2019 - 13:32:31 EST


On Thu, 2019-03-07 at 17:33 +0000, Michael Kelley wrote:
> From: Mohammed Gamal <mgamal@xxxxxxxxxx> Sent: Thursday, March 7,
> 2019 8:36 AM
> >
> > This patch adds a check for the presence of the ring buffer in
> > hv_get_bytes_to_read/write() to avoid possible NULL pointer
> > dereferences.
> > If the ring buffer is not yet allocated, return 0 bytes to be
> > read/written.
> >
> > The root cause is that code that accesses the ring buffer including
> > hv_get_bytes_to_read/write() could be vulnerable to the race
> > condition
> > discussed in https://lkml.org/lkml/2018/10/18/779>;
> >
> > This race is being addressed by the patch series by Kimberly Brown
> > in
> > https://lkml.org/lkml/2019/2/21/1236 which is not final yet
> >
> > Signed-off-by: Mohammed Gamal <mgamal@xxxxxxxxxx>
>
> Could you elaborate on the code paths where
> hv_get_bytes_to_read/write() could be called when the ring buffer
> isn't yet allocated?ÂÂMy sense is that Kim Brown's patch will address
> all of the code paths that involved sysfs access from outside the
> driver.ÂÂAnd within a driver, the ring buffer should never be
> accessed
> unless it is already allocated.ÂÂIs there another code path we're not
> aware of?ÂÂI'm wondering if these changes are really needed once
> Kim Brown's patch is finished.
>
> Michael

I've seen one instance of the race in the netvsc driver when running
traffic through it with iperf3 while continuously changing the channel
settings.

The following code path deallocates the ring buffer:
netvsc_set_channels() -> netvsc_detach() ->
rndis_filter_device_remove() -> netvsc_device_remove() -> vmbus_close()
-> vmbus_free_ring() -> hv_ringbuffer_cleanup().

netvsc_send_pkt() -> hv_get_bytes_to_write() might get called
concurrently after vmbus_close() and before vmbus_open() returns and
sets up the new ring buffer.

The race is fairly hard to reproduce on recent upstream kernels, but I
still managed to reproduce it.