2018-03-27 03:48:38

by Jason Wang

[permalink] [raw]
Subject: [PATCH net] vhost: correctly remove wait queue during poll failure

We tried to remove vq poll from wait queue, but do not check whether
or not it was in a list before. This will lead double free. Fixing
this by checking poll->wqh to make sure it was in a list.

Reported-by: [email protected]
Fixes: 2b8b328b61c79 ("vhost_net: handle polling errors when setting backend")
Signed-off-by: Jason Wang <[email protected]>
---
drivers/vhost/vhost.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 1b3e8d2d..5d5a9d9 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -212,8 +212,7 @@ int vhost_poll_start(struct vhost_poll *poll, struct file *file)
if (mask)
vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
if (mask & EPOLLERR) {
- if (poll->wqh)
- remove_wait_queue(poll->wqh, &poll->wait);
+ vhost_poll_stop(poll);
ret = -EINVAL;
}

--
2.7.4



2018-03-27 09:29:36

by Darren Kenny

[permalink] [raw]
Subject: Re: [PATCH net] vhost: correctly remove wait queue during poll failure

Hi Jason,

On Tue, Mar 27, 2018 at 11:47:22AM +0800, Jason Wang wrote:
>We tried to remove vq poll from wait queue, but do not check whether
>or not it was in a list before. This will lead double free. Fixing
>this by checking poll->wqh to make sure it was in a list.

This text seems at odds with the code below, instead of checking
poll-whq, you are removing that check...

Maybe the text needs rewording?

Thanks,

Darren.

>
>Reported-by: [email protected]
>Fixes: 2b8b328b61c79 ("vhost_net: handle polling errors when setting backend")
>Signed-off-by: Jason Wang <[email protected]>
>---
> drivers/vhost/vhost.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
>diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>index 1b3e8d2d..5d5a9d9 100644
>--- a/drivers/vhost/vhost.c
>+++ b/drivers/vhost/vhost.c
>@@ -212,8 +212,7 @@ int vhost_poll_start(struct vhost_poll *poll, struct file *file)
> if (mask)
> vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
> if (mask & EPOLLERR) {
>- if (poll->wqh)
>- remove_wait_queue(poll->wqh, &poll->wait);
>+ vhost_poll_stop(poll);
> ret = -EINVAL;
> }
>
>--
>2.7.4
>

2018-03-27 09:44:56

by Jason Wang

[permalink] [raw]
Subject: Re: [PATCH net] vhost: correctly remove wait queue during poll failure



On 2018年03月27日 17:28, Darren Kenny wrote:
> Hi Jason,
>
> On Tue, Mar 27, 2018 at 11:47:22AM +0800, Jason Wang wrote:
>> We tried to remove vq poll from wait queue, but do not check whether
>> or not it was in a list before. This will lead double free. Fixing
>> this by checking poll->wqh to make sure it was in a list.
>
> This text seems at odds with the code below, instead of checking
> poll-whq, you are removing that check...
>
> Maybe the text needs rewording?

Yes, I admit it's bad, thanks for pointing out.

How about:

"Fixing this by switching to use vhost_poll_stop() which zeros poll->wqh
after removing poll from waitqueue to make sure it won't be freed twice."

Thanks

>
> Thanks,
>
> Darren.
>
>>
>> Reported-by: [email protected]
>> Fixes: 2b8b328b61c79 ("vhost_net: handle polling errors when setting
>> backend")
>> Signed-off-by: Jason Wang <[email protected]>
>> ---
>> drivers/vhost/vhost.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>> index 1b3e8d2d..5d5a9d9 100644
>> --- a/drivers/vhost/vhost.c
>> +++ b/drivers/vhost/vhost.c
>> @@ -212,8 +212,7 @@ int vhost_poll_start(struct vhost_poll *poll,
>> struct file *file)
>>     if (mask)
>>         vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
>>     if (mask & EPOLLERR) {
>> -        if (poll->wqh)
>> -            remove_wait_queue(poll->wqh, &poll->wait);
>> +        vhost_poll_stop(poll);
>>         ret = -EINVAL;
>>     }
>>
>> --
>> 2.7.4
>>


2018-03-27 14:00:24

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH net] vhost: correctly remove wait queue during poll failure

On Tue, Mar 27, 2018 at 05:43:14PM +0800, Jason Wang wrote:
>
>
> On 2018年03月27日 17:28, Darren Kenny wrote:
> > Hi Jason,
> >
> > On Tue, Mar 27, 2018 at 11:47:22AM +0800, Jason Wang wrote:
> > > We tried to remove vq poll from wait queue, but do not check whether
> > > or not it was in a list before. This will lead double free. Fixing
> > > this by checking poll->wqh to make sure it was in a list.
> >
> > This text seems at odds with the code below, instead of checking
> > poll-whq, you are removing that check...
> >
> > Maybe the text needs rewording?
>
> Yes, I admit it's bad, thanks for pointing out.
>
> How about:
>
> "Fixing this by switching to use vhost_poll_stop() which zeros poll->wqh
> after removing poll from waitqueue to make sure it won't be freed twice."
>
> Thanks

Let's be a bit more specific about the problem maybe?

when vhost's attempt to start polling a descriptor fails, we remove the
poll->wqh entry from wait queue but do not clear it, so the following
cleanup (e.g. on release) will attempt to remove it again.

To fix, switch to vhost_poll_stop() which zeros poll->wqh
after removing poll from waitqueue to make sure it won't be freed twice."


the patch itself is fine though:


Acked-by: Michael S. Tsirkin <[email protected]>


> >
> > Thanks,
> >
> > Darren.
> >
> > >
> > > Reported-by: [email protected]
> > > Fixes: 2b8b328b61c79 ("vhost_net: handle polling errors when setting
> > > backend")
> > > Signed-off-by: Jason Wang <[email protected]>
> > > ---
> > > drivers/vhost/vhost.c | 3 +--
> > > 1 file changed, 1 insertion(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> > > index 1b3e8d2d..5d5a9d9 100644
> > > --- a/drivers/vhost/vhost.c
> > > +++ b/drivers/vhost/vhost.c
> > > @@ -212,8 +212,7 @@ int vhost_poll_start(struct vhost_poll *poll,
> > > struct file *file)
> > >     if (mask)
> > >         vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
> > >     if (mask & EPOLLERR) {
> > > -        if (poll->wqh)
> > > -            remove_wait_queue(poll->wqh, &poll->wait);
> > > +        vhost_poll_stop(poll);
> > >         ret = -EINVAL;
> > >     }
> > >
> > > --
> > > 2.7.4
> > >