From: Johan Hovold <johan@kernel.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Johan Hovold <johan@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Oliver Neukum <oneukum@suse.com>,
"Paul E . McKenney" <paulmck@linux.vnet.ibm.com>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
stable <stable@vger.kernel.org>
Subject: Re: [PATCH RFC v2 2/2] USB: ldusb: fix ring-buffer locking
Date: Mon, 21 Oct 2019 20:30:27 +0200 [thread overview]
Message-ID: <20191021183027.GN24768@localhost> (raw)
In-Reply-To: <Pine.LNX.4.44L0.1910211109400.1673-100000@iolanthe.rowland.org>
On Mon, Oct 21, 2019 at 11:17:11AM -0400, Alan Stern wrote:
> On Fri, 18 Oct 2019, Johan Hovold wrote:
>
> > The custom ring-buffer implementation was merged without any locking
> > whatsoever, but a spinlock was later added by commit 9d33efd9a791
> > ("USB: ldusb bugfix").
> >
> > The lock did not cover the loads from the ring-buffer entry after
> > determining the buffer was non-empty, nor the update of the tail index
> > once the entry had been processed. The former could lead to stale data
> > being returned, while the latter could lead to memory corruption on
> > sufficiently weakly ordered architectures.
>
> Let's see if I understand this correctly.
>
> The completion routine stores a buffer-length value at the location
> actual_buffer points to, and it stores the buffer contents themselves
> in the immediately following bytes. All this happens while the
> dev->rbsl spinlock is held.
Right.
> Later on the read routine loads a value from *actual_buffer while
> holding the spinlock, but drops the spinlock before copying the
> immediately following buffer contents to userspace.
It doesn't currently hold the spinlock while reading *actual_buffer,
only when checking if the ring-buffer is non-empty. The patch below
extends the check to cover also the load from *actual_buffer.
> Your question is whether the read routine needs to call smp_rmb() after
> dropping the spinlock and before doing copy_to_user(), right?
Right, or alternatively, if an smp_rmb() after dropping the spinlock and
before loading *actual_buffer is needed.
> The answer is: No, smp_rmb() isn't needed. All the data stored while
> ld_usb_interrupt_in_callback() held the spinlock will be visible to
> ld_usb_read() while it holds the spinlock and afterward (assuming the
> critical section in ld_usb_read() runs after the critical section in
> ld_usb_interrupt_in_callback() -- but you know this is true because of
> the value you read from *actual_buffer).
Did you mean the value "read from dev->ring_head" (which tells us the
ring-buffer has been updated) here?
We currently have something like this in ld_usb_read():
spin_lock_irq(&lock);
while (head == tail) {
spin_unlock(&lock);
wait_event(event);
spin_lock(&lock);
}
spin_unlock_irq(&lock);
entry = &buffer[tail];
len = *entry;
copy_to_user(buf, entry + 1, len);
/* update tail */
And without an smp_rmb() after dropping the spinlock, what prevents the
load from *entry from being done before the load from head? Nothing,
right (the spin_unlock_irq() is only a compiler barrier for later
loads)? But that's fine because all stores done by the completion
handler under the spinlock would of course be visible at that point.
So the current code is fine wrt to copy_to_user(), and only the tail
bits below are actually needed.
Thanks, Alan! Had myself confused there.
Johan
> > Fixes: 2824bd250f0b ("[PATCH] USB: add ldusb driver")
> > Fixes: 9d33efd9a791 ("USB: ldusb bugfix")
> > Cc: stable <stable@vger.kernel.org> # 2.6.13
> > Signed-off-by: Johan Hovold <johan@kernel.org>
> > ---
> > drivers/usb/misc/ldusb.c | 15 ++++++++++++---
> > 1 file changed, 12 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
> > index 15b5f06fb0b3..6b5843b0071e 100644
> > --- a/drivers/usb/misc/ldusb.c
> > +++ b/drivers/usb/misc/ldusb.c
> > @@ -477,11 +477,11 @@ static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
> >
> > spin_lock_irq(&dev->rbsl);
> > }
> > - spin_unlock_irq(&dev->rbsl);
> >
> > /* actual_buffer contains actual_length + interrupt_in_buffer */
> > actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
> > if (*actual_buffer > dev->interrupt_in_endpoint_size) {
> > + spin_unlock_irq(&dev->rbsl);
> > retval = -EIO;
> > goto unlock_exit;
> > }
> > @@ -489,17 +489,26 @@ static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
> > if (bytes_to_read < *actual_buffer)
> > dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
> > *actual_buffer-bytes_to_read);
> > + spin_unlock_irq(&dev->rbsl);
> > +
> > + /*
> > + * Pairs with spin_unlock_irqrestore() in
> > + * ld_usb_interrupt_in_callback() and makes sure the ring-buffer entry
> > + * has been updated before copy_to_user().
> > + */
> > + smp_rmb();
> >
> > /* copy one interrupt_in_buffer from ring_buffer into userspace */
> > if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
> > retval = -EFAULT;
> > goto unlock_exit;
> > }
> > - dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
> > -
> > retval = bytes_to_read;
> >
> > spin_lock_irq(&dev->rbsl);
> > +
> > + dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
> > +
> > if (dev->buffer_overflow) {
> > dev->buffer_overflow = 0;
> > spin_unlock_irq(&dev->rbsl);
> >
>
>
prev parent reply other threads:[~2019-10-21 18:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-18 15:19 [PATCH v2 0/2] USB: ldusb: fix ring-buffer bugs Johan Hovold
2019-10-18 15:19 ` [PATCH v2 1/2] USB: ldusb: fix read info leaks Johan Hovold
2019-10-18 15:19 ` [PATCH RFC v2 2/2] USB: ldusb: fix ring-buffer locking Johan Hovold
2019-10-18 18:54 ` Greg Kroah-Hartman
2019-10-21 8:56 ` Johan Hovold
2019-10-21 13:48 ` Greg Kroah-Hartman
2019-10-21 15:17 ` Alan Stern
2019-10-21 18:30 ` Johan Hovold [this message]
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=20191021183027.GN24768@localhost \
--to=johan@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=oneukum@suse.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=stable@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
/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).