linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Prasanna Kalever <pkalever@redhat.com>
Cc: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>,
	linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
	nbd@other.debian.org, Josef Bacik <josef@toxicpanda.com>,
	axboe@kernel.dk, Ilya Dryomov <idryomov@redhat.com>,
	Xiubo Li <xiubli@redhat.com>,
	Matteo Croce <mcroce@linux.microsoft.com>
Subject: Re: [PATCH] nbd: provide a way for userspace processes to identify device backends
Date: Wed, 19 May 2021 15:54:18 +0800	[thread overview]
Message-ID: <YKTEKmDdKZKTMKYS@T590> (raw)
In-Reply-To: <CANwsLLE6FK3eCmDwQ+7ghwFx0Hi1KDr6TaiKX1VW2Yt+5xe+WA@mail.gmail.com>

On Tue, May 18, 2021 at 03:19:37PM +0530, Prasanna Kalever wrote:
> On Tue, May 18, 2021 at 2:54 PM Ming Lei <ming.lei@redhat.com> wrote:
> >
> > On Tue, May 18, 2021 at 01:22:19PM +0530, Prasanna Kalever wrote:
> > > On Tue, May 18, 2021 at 6:00 AM Ming Lei <ming.lei@redhat.com> wrote:
> > > >
> > > > Hello Prasanna,
> > > >
> > > > On Thu, Apr 29, 2021 at 03:58:28PM +0530, Prasanna Kumar Kalever wrote:
> > > > > Problem:
> > > > > On reconfigure of device, there is no way to defend if the backend
> > > > > storage is matching with the initial backend storage.
> > > > >
> > > > > Say, if an initial connect request for backend "pool1/image1" got
> > > > > mapped to /dev/nbd0 and the userspace process is terminated. A next
> > > > > reconfigure request within NBD_ATTR_DEAD_CONN_TIMEOUT is allowed to
> > > > > use /dev/nbd0 for a different backend "pool1/image2"
> > > > >
> > > > > For example, an operation like below could be dangerous:
> > > >
> > >
> > > Hello Ming,
> > >
> > > > Can you explain a bit why it is dangerous?
> > >
> > > Yes, sure. Please check the below comments inline,
> > >
> > > >
> > > > >
> > > > > $ sudo rbd-nbd map --try-netlink rbd-pool/ext4-image
> > > > > /dev/nbd0
> > > > > $ sudo blkid /dev/nbd0
> > > > > /dev/nbd0: UUID="bfc444b4-64b1-418f-8b36-6e0d170cfc04" TYPE="ext4"
> > >
> > > On Map the rbd-nbd attempting to send NBD_CMD_CONNECT, for backend
> > > 'rbd-pool/ext4-image'. Post which kernel will allocate a new device
> > > say /dev/nbd0 for backend file rbd-pool/ext4-image (format:
> > > <pool>/<backendfile>)
> > >
> > > > > $ sudo pkill -9 rbd-nbd
> > >
> > > Assume normally or abnormally the userspace process (rbd-nbd here) is
> > > terminated, but then as per the settings the device /dev/nbd0 is not
> > > returned immediately, the kernel will wait for the
> > > NBD_ATTR_DEAD_CONN_TIMEOUT to expire.
> > >
> > > At this point two things could be possible:
> > > 1. if there is a reconfigure request from userspace within the timeout
> > > then the kernel might reassign the same device /dev/nbd0.
> > > 2. if the timeout has expired, then the device will be relieved.
> > >
> > > > > $ sudo rbd-nbd attach --try-netlink --device /dev/nbd0 rbd-pool/xfs-image
> > > > > /dev/nbd0
> > > > > $ sudo blkid /dev/nbd0
> > > > > /dev/nbd0: UUID="d29bf343-6570-4069-a9ea-2fa156ced908" TYPE="xfs"
> > >
> > > On attach the rbd-nbd attempt to send NBD_CMD_RECONFIGURE, after which
> > > the kernel will assign '--device /dev/nbd0' to specified backend.
> > >
> > > But there is a chance that userspace processes might accidentally send
> > > NBD_CMD_RECONFIGURE claiming for /dev/nbd0 for a different backend
> > > (rbd-pool/xfs-image instead of original rbd-pool/ext4-image).
> > > Currently, there is no mechanism to verify if the backend provided
> > > later with attach(NBD_CMD_RECONFIGURE) is matching with the one
> > > provided originally with map(NBD_CMD_CONNECT) before granting for a
> > > attach or reconfigure.
> > >
> > > For example in the above-explained scenario:
> > > Assume EXT4 on rbd-pool/ext4-image was mounted, after attach (Note:
> > > device /dev/nbd0 is reconfigured to a different backend file) XFS on
> > > rbd-pool/xfs-image would get corrupted. If there was an application
> > > using /dev/nbd0 directly (as a raw block volume), it wouldn't be happy
> > > either.
> >
> > OK, got it. If I understand correctly, what you need is to not allow
> > reconfigure if the nbd disk is mounted, right?
> 
> Excuse me, not exactly. Mount was one example scenario to showcase why
> allowing attaching without any validation could be dangerous.

If nbd has exclusive owner, it shouldn't be reconfigured, which
can be respected in kernel side only, see loop_configure().

Not all application can provide NBD_ATTR_BACKEND_IDENTIFIER, so
claiming in nbd_genl_reconfigure() is still needed, IMO.

> Basically, we want a way to check and verify if the backend specified
> at map time and backend specified at attach(reconfigure) time are
> matching for a given device, only if they are matching proceed to
> attach else fail.
> 
> >
> > >
> > > > >
> > > > > Solution:
> > > > > Provide a way for userspace processes to keep some metadata to identify
> > > > > between the device and the backend, so that when a reconfigure request is
> > > > > made, we can compare and avoid such dangerous operations.
> > > > >
> > > > > With this solution, as part of the initial connect request, backend
> > > > > path can be stored in the sysfs per device config, so that on a reconfigure
> > > > > request it's easy to check if the backend path matches with the initial
> > > > > connect backend path.
> > > > >
> > > > > Please note, ioctl interface to nbd will not have these changes, as there
> > > > > won't be any reconfigure.
> > > >
> > > > BTW, loop has similar issue, and patch of 'block: add a sequence number to disks'
> > > > is added for addressing this issue, what do you think of that generic
> > > > approach wrt. this nbd's issue? such as used the exposed sysfs sequence number
> > > > for addressing this issue?
> > > >
> > > > https://lore.kernel.org/linux-block/YH81n34d2G3C4Re+@gardel-login/#r
> > >
> > > If I understand the changes and the background of the fix correctly, I
> > > think with that fix author is trying to monotonically increase the seq
> > > number and add it to the disk on every single device map/attach and
> > > expose it through the sysfs, which will help the userspace processes
> > > further to correlate events for particular and specific devices that
> > > reuse the same loop device.
> > >
> > > Coming back to my changes:
> > > I think here with this fix, we are trying to solve a different
> > > problem. The fix with this patch accepts a cookie or a backend string
> > > (could be file-path or whatever id userspace choose to provide) from
> > > userspace at the time of map and stores it in the sysfs
> > > /sys/block/nbdX/backend path and persists it until unmap is issued on
> > > the device (meaning that identity stays throughout the life cycle of
> > > that device, no matter how many detach and attaches happen).
> >
> > Your solution needs change from userspace side, so it isn't flexible.
> >
> > > If there
> > > is a detach request in between (not unmap) then on the next attach
> > > (reconfigure request to reuse the same device) the stored
> > > cookie/UUID/backend-string will stand as a reference to verify if the
> > > newly passed backend is matching with the actual backend passed at map
> > > time to avoid any misconfigurations by accident and to safely proceed
> > > with attach.
> >
> > We can avoid reconfigure if the nbd disk is opened exclusively, such as
> > mount, please see if the following patch can solve your problem:
> 
> IMHO, we should almost never allow reconfigure/reattaching a given
> device with a different backend (except in cases like live migration,
> which the application should take care of), and not just when nbd disk
> is opened exclusively.
> 
> When an attach (reconfigure) is issued, Its application's logic to
> provide the same matching cookie (or device-string or a uuid) so that
> kernel can validate it with /sys/block/nbdX/backend and continue
> safely to attach and reassign the device back.

Your patch looks fine, but only new application can benefit from it, and
we still need to avoid reconfigure if the disk is opened exclusively for
old applications. Anyway:

Reviewed-by: Ming Lei <ming.lei@redhat.com>



Thanks,
Ming


  reply	other threads:[~2021-05-19  7:54 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-29 10:28 Prasanna Kumar Kalever
2021-04-30  2:14 ` Xiubo Li
2021-05-17 14:42   ` Prasanna Kalever
2021-05-18  0:29 ` Ming Lei
2021-05-18  7:52   ` Prasanna Kalever
2021-05-18  9:24     ` Ming Lei
2021-05-18  9:49       ` Prasanna Kalever
2021-05-19  7:54         ` Ming Lei [this message]
2021-05-19 14:17           ` Prasanna Kalever
2021-05-19 14:33     ` Matteo Croce
2021-05-26 14:13       ` Matteo Croce
2021-05-27  5:22         ` Prasanna Kalever
2021-06-16  8:42 ` Prasanna Kalever
2021-06-16 12:59 ` Jens Axboe

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=YKTEKmDdKZKTMKYS@T590 \
    --to=ming.lei@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=idryomov@redhat.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcroce@linux.microsoft.com \
    --cc=nbd@other.debian.org \
    --cc=pkalever@redhat.com \
    --cc=prasanna.kalever@redhat.com \
    --cc=xiubli@redhat.com \
    --subject='Re: [PATCH] nbd: provide a way for userspace processes to identify device backends' \
    /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

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).