public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Yewon Choi <woni9911@gmail.com>
Cc: Bryan Tan <bryantan@vmware.com>, Vishnu Dasa <vdasa@vmware.com>,
	VMware PV-Drivers Reviewers <pv-drivers@vmware.com>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-kernel@vger.kernel.org, threeearcat@gmail.com
Subject: Re: [PATCH] vmci_host: use smp_load_acquire/smp_store_release when accessing vmci_host_dev->ct_type
Date: Thu, 23 Nov 2023 08:44:46 +0000	[thread overview]
Message-ID: <2023112331-wise-regain-72dc@gregkh> (raw)
In-Reply-To: <20231123074920.GA10480@libra05>

On Thu, Nov 23, 2023 at 04:49:22PM +0900, Yewon Choi wrote:
> On Wed, Nov 22, 2023 at 02:34:55PM +0000, Greg Kroah-Hartman wrote:
> > On Wed, Nov 22, 2023 at 09:20:08PM +0900, Yewon Choi wrote:
> > > In vmci_host.c, missing memory barrier between vmci_host_dev->ct_type
> > > and vmci_host_dev->context may cause uninitialized data access.
> > > 
> > > One of possible execution flows is as follows:
> > > 
> > > CPU 1 (vmci_host_do_init_context)
> > > =====
> > > vmci_host_dev->context = vmci_ctx_create(...) // 1
> > > vmci_host_dev->ct_type = VMCIOBJ_CONTEXT; // 2
> > > 
> > > CPU 2 (vmci_host_poll)
> > > =====
> > > if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) { // 3
> > > 	context = vmci_host_dev->context; // 4
> > > 	poll_wait(..., &context->host_context.wait_queue, ...);
> > > 
> > > While ct_type serves as a flag indicating that context is initialized,
> > > there is no memory barrier which prevents reordering between
> > > 1,2 and 3, 4. So it is possible that 4 reads uninitialized
> > > vmci_host_dev->context.
> > > In this case, the null dereference occurs in poll_wait().
> > > 
> > > In order to prevent this kind of reordering, we change plain accesses
> > > to ct_type into smp_load_acquire() and smp_store_release().
> > > 
> > > Signed-off-by: Yewon Choi <woni9911@gmail.com>
> > > ---
> > >  drivers/misc/vmw_vmci/vmci_host.c | 40 ++++++++++++++++++-------------
> > >  1 file changed, 23 insertions(+), 17 deletions(-)
> > > 
> > > diff --git a/drivers/misc/vmw_vmci/vmci_host.c b/drivers/misc/vmw_vmci/vmci_host.c
> > > index abe79f6fd2a7..e83b6e0fe55b 100644
> > > --- a/drivers/misc/vmw_vmci/vmci_host.c
> > > +++ b/drivers/misc/vmw_vmci/vmci_host.c
> > > @@ -139,7 +139,7 @@ static int vmci_host_close(struct inode *inode, struct file *filp)
> > >  {
> > >  	struct vmci_host_dev *vmci_host_dev = filp->private_data;
> > >  
> > > -	if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
> > > +	if (smp_load_acquire(&vmci_host_dev->ct_type) == VMCIOBJ_CONTEXT) {
> > 
> > This is getting tricky, why not use a normal lock to ensure that all is
> > safe?  close isn't on a "fast path", so this shouldn't be a speed issue,
> > right?
> > 
> 
> I think using locks can be considered orthogonal to correcting memory ordering. 

But they ensure proper memory ordering.

> As you pointed out, vmci_host_close is not a performance-critical function
> while other functions using vmci_host_dev->context are performance-critical.

In what way?  Why is the context being constantly checked in such
situations?  And if so, it can change right after being checked so a
real lock needs to be used.

> If the lock is needed, we will need to add locks in all of them. I cannot be
> sure which is better. Besides that, it seems to be a separate issue.

Nope, I think it's the same issue :)

> On the other hand, the current implementation doesn't guarantee memory ordering 
> which leads to wrong behavior.
> This patch fixes this issue by adding primitives. 

But it's still wrong, again, what keeps the value from changing right
after checking it?

thanks,

greg k-h

  reply	other threads:[~2023-11-23  8:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 12:20 [PATCH] vmci_host: use smp_load_acquire/smp_store_release when accessing vmci_host_dev->ct_type Yewon Choi
2023-11-22 14:34 ` Greg Kroah-Hartman
2023-11-23  7:49   ` Yewon Choi
2023-11-23  8:44     ` Greg Kroah-Hartman [this message]
2023-11-23 10:06       ` Dae R. Jeong
2023-11-23 10:14         ` Greg Kroah-Hartman
2023-11-23 11:08           ` Dae R. Jeong
2023-11-23 12:21             ` Greg Kroah-Hartman

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=2023112331-wise-regain-72dc@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=arnd@arndb.de \
    --cc=bryantan@vmware.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pv-drivers@vmware.com \
    --cc=threeearcat@gmail.com \
    --cc=vdasa@vmware.com \
    --cc=woni9911@gmail.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