qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Fam Zheng <famz@redhat.com>
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] file-posix: Skip effectiveless OFD lock operations
Date: Mon, 13 Aug 2018 10:52:22 +0200	[thread overview]
Message-ID: <20180813085222.GB4323@localhost.localdomain> (raw)
In-Reply-To: <20180813014559.GA7549@lemon.usersys.redhat.com>

Am 13.08.2018 um 03:45 hat Fam Zheng geschrieben:
> On Fri, 08/10 14:14, Kevin Wolf wrote:
> > Am 18.07.2018 um 10:43 hat Fam Zheng geschrieben:
> > > If we know we've already locked the bytes, don't do it again; similarly
> > > don't unlock a byte if we haven't locked it. This doesn't change the
> > > behavior, but fixes a corner case explained below.
> > > 
> > > Libvirt had an error handling bug that an image can get its (ownership,
> > > file mode, SELinux) permissions changed (RHBZ 1584982) by mistake behind
> > > QEMU. Specifically, an image in use by Libvirt VM has:
> > > 
> > >     $ ls -lhZ b.img
> > >     -rw-r--r--. qemu qemu system_u:object_r:svirt_image_t:s0:c600,c690 b.img
> > > 
> > > Trying to attach it a second time won't work because of image locking.
> > > And after the error, it becomes:
> > > 
> > >     $ ls -lhZ b.img
> > >     -rw-r--r--. root root system_u:object_r:virt_image_t:s0 b.img
> > > 
> > > Then, we won't be able to do OFD lock operations with the existing fd.
> > > In other words, the code such as in blk_detach_dev:
> > > 
> > >     blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
> > > 
> > > can abort() QEMU, out of environmental changes.
> > > 
> > > This patch is an easy fix to this and the change is regardlessly
> > > reasonable, so do it.
> > > 
> > > Signed-off-by: Fam Zheng <famz@redhat.com>
> > > ---
> > >  block/file-posix.c | 27 +++++++++++++++++----------
> > >  1 file changed, 17 insertions(+), 10 deletions(-)
> > > 
> > > diff --git a/block/file-posix.c b/block/file-posix.c
> > > index 60af4b3d51..45d44c9947 100644
> > > --- a/block/file-posix.c
> > > +++ b/block/file-posix.c
> > > @@ -680,23 +680,28 @@ typedef enum {
> > >   * file; if @unlock == true, also unlock the unneeded bytes.
> > >   * @shared_perm_lock_bits is the mask of all permissions that are NOT shared.
> > >   */
> > > -static int raw_apply_lock_bytes(int fd,
> > > +static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
> > >                                  uint64_t perm_lock_bits,
> > >                                  uint64_t shared_perm_lock_bits,
> > >                                  bool unlock, Error **errp)
> > >  {
> > >      int ret;
> > >      int i;
> > > +    uint64_t locked_perm, locked_shared_perm;
> > > +
> > > +    locked_perm = s ? s->perm : 0;
> > > +    locked_shared_perm = s ? ~s->shared_perm & BLK_PERM_ALL : 0;
> > 
> > For the s == NULL case, using 0 is okay for locking because we will
> > always consider the bit as previously unlocked, so we will lock it.
> > 
> > For unlocking, however, we'll also see it as previously unlocked, so we
> > will never actually unlock anything any more.
> > 
> > Am I missing something?
> 
> You are right. Though s == NULL only happens in raw_co_create and the fd will be
> closed before the function returns, I agree for the correctness of this function
> it's better to do a blanket unlock when unlocking. Will respin.

At least one reason why you can't rely on file descriptors being closed
and releasing the locks is fdsets, where there are still more file
descriptors for the same OFD.

Kevin

      reply	other threads:[~2018-08-13  8:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-18  8:43 [Qemu-devel] [PATCH] file-posix: Skip effectiveless OFD lock operations Fam Zheng
2018-08-10 12:14 ` Kevin Wolf
2018-08-13  1:45   ` Fam Zheng
2018-08-13  8:52     ` Kevin Wolf [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=20180813085222.GB4323@localhost.localdomain \
    --to=kwolf@redhat.com \
    --cc=famz@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /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).