From nobody Thu Apr 25 15:15:31 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1637848668393278.36811531243416; Thu, 25 Nov 2021 05:57:48 -0800 (PST) Received: from localhost ([::1]:40016 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mqFFz-0000ek-8G for importer@patchew.org; Thu, 25 Nov 2021 08:57:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46104) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mqFBs-00031U-L3 for qemu-devel@nongnu.org; Thu, 25 Nov 2021 08:53:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:29773) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mqFBr-0000OI-2g for qemu-devel@nongnu.org; Thu, 25 Nov 2021 08:53:32 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-175-QstCObGKNbOXc7obWK1DLg-1; Thu, 25 Nov 2021 08:53:27 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CA8810144E1; Thu, 25 Nov 2021 13:53:26 +0000 (UTC) Received: from localhost (unknown [10.39.193.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 39EBC5D6B1; Thu, 25 Nov 2021 13:53:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637848410; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DW6mnFoKl4Tr6p5LoW3lzBoW8GmYZ/8HM6fQnbgibzE=; b=f67n5snU4TeAwuScwwkoS8j8g05dc/HUn9/bEsLz51zwoSk8CpQr42J5z2b3ZOFiM01p6A X/pmcn+xekR9tS1z8QcuLbF0snxVCPohdEFGOFSrksP2JpsqOwl0CECMIjk+YI5Q42cfJ2 ZnlfEJD3850lS0z8hpnMt83iMpdA30M= X-MC-Unique: QstCObGKNbOXc7obWK1DLg-1 From: Hanna Reitz To: qemu-block@nongnu.org Subject: [PATCH 1/2] block-backend: Retain permissions after migration Date: Thu, 25 Nov 2021 14:53:16 +0100 Message-Id: <20211125135317.186576-2-hreitz@redhat.com> In-Reply-To: <20211125135317.186576-1-hreitz@redhat.com> References: <20211125135317.186576-1-hreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.7, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peng Liang , Hanna Reitz , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1637848668934100001 Content-Type: text/plain; charset="utf-8" After migration, the permissions the guest device wants to impose on its BlockBackend are stored in blk->perm and blk->shared_perm. In blk_root_activate(), we take our permissions, but keep all shared permissions open by calling `blk_set_perm(blk->perm, BLK_PERM_ALL)`. Only afterwards (immediately or later, depending on the runstate) do we restrict the shared permissions by calling `blk_set_perm(blk->perm, blk->shared_perm)`. Unfortunately, our first call with shared_perm=3DBLK_PERM_ALL has overwritten blk->shared_perm to be BLK_PERM_ALL, so this is a no-op and the set of shared permissions is not restricted. Fix this bug by saving the set of shared permissions before invoking blk_set_perm() with BLK_PERM_ALL and restoring it afterwards. Fixes: 5f7772c4d0cf32f4e779fcd5a69ae4dae24aeebf ("block-backend: Defer shared_perm tightening migration completion") Reported-by: Peng Liang Signed-off-by: Hanna Reitz Reviewed-by: Kevin Wolf Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Peng Liang --- block/block-backend.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/block/block-backend.c b/block/block-backend.c index 12ef80ea17..41e388fe1f 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -190,6 +190,7 @@ static void blk_root_activate(BdrvChild *child, Error *= *errp) { BlockBackend *blk =3D child->opaque; Error *local_err =3D NULL; + uint64_t saved_shared_perm; =20 if (!blk->disable_perm) { return; @@ -197,12 +198,22 @@ static void blk_root_activate(BdrvChild *child, Error= **errp) =20 blk->disable_perm =3D false; =20 + /* + * blk->shared_perm contains the permissions we want to share once + * migration is really completely done. For now, we need to share + * all; but we also need to retain blk->shared_perm, which is + * overwritten by a successful blk_set_perm() call. Save it and + * restore it below. + */ + saved_shared_perm =3D blk->shared_perm; + blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err); if (local_err) { error_propagate(errp, local_err); blk->disable_perm =3D true; return; } + blk->shared_perm =3D saved_shared_perm; =20 if (runstate_check(RUN_STATE_INMIGRATE)) { /* Activation can happen when migration process is still active, f= or --=20 2.33.1 From nobody Thu Apr 25 15:15:31 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1637848602161891.7719051062226; Thu, 25 Nov 2021 05:56:42 -0800 (PST) Received: from localhost ([::1]:35152 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mqFEv-0006LD-28 for importer@patchew.org; Thu, 25 Nov 2021 08:56:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46144) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mqFBv-00031i-GZ for qemu-devel@nongnu.org; Thu, 25 Nov 2021 08:53:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:37987) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mqFBt-0000Oo-Vf for qemu-devel@nongnu.org; Thu, 25 Nov 2021 08:53:35 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-375-kHPyFd7tMfmJNYi9hPx8Sw-1; Thu, 25 Nov 2021 08:53:30 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7631C1006AA0; Thu, 25 Nov 2021 13:53:29 +0000 (UTC) Received: from localhost (unknown [10.39.193.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 438471010426; Thu, 25 Nov 2021 13:53:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637848413; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZEmoIYN42QnLxwi/N8ApPh0JHcSLpSlR9FLtCl8euTw=; b=Iq5Twcm2jXy0Cn/sHlvpBiLxcYwAKnJt5sxRhRZ7s2IPTUHGMtxqY7C4Gn/CDodqPP6L/h C8ZpJszTy6bwk9oUGkHifCP4nlVlvWyOdzsQnX9SlsaWY63skmiLKc99oGLnofLuMoFbXF wcMEMhpfsGwv2FHm1K/ufE3t+ZfwuHY= X-MC-Unique: kHPyFd7tMfmJNYi9hPx8Sw-1 From: Hanna Reitz To: qemu-block@nongnu.org Subject: [PATCH 2/2] iotests/migration-permissions: New test Date: Thu, 25 Nov 2021 14:53:17 +0100 Message-Id: <20211125135317.186576-3-hreitz@redhat.com> In-Reply-To: <20211125135317.186576-1-hreitz@redhat.com> References: <20211125135317.186576-1-hreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.7, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peng Liang , Hanna Reitz , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1637848602555100001 Content-Type: text/plain; charset="utf-8" This test checks that a raw image in use by a virtio-blk device does not share the WRITE permission both before and after migration. Signed-off-by: Hanna Reitz Reviewed-by: Kevin Wolf --- .../qemu-iotests/tests/migration-permissions | 101 ++++++++++++++++++ .../tests/migration-permissions.out | 5 + 2 files changed, 106 insertions(+) create mode 100755 tests/qemu-iotests/tests/migration-permissions create mode 100644 tests/qemu-iotests/tests/migration-permissions.out diff --git a/tests/qemu-iotests/tests/migration-permissions b/tests/qemu-io= tests/tests/migration-permissions new file mode 100755 index 0000000000..6be02581c7 --- /dev/null +++ b/tests/qemu-iotests/tests/migration-permissions @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# group: migration +# +# Copyright (C) 2021 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import os +import iotests +from iotests import imgfmt, qemu_img_create, qemu_io + + +test_img =3D os.path.join(iotests.test_dir, 'test.img') +mig_sock =3D os.path.join(iotests.sock_dir, 'mig.sock') + + +class TestMigrationPermissions(iotests.QMPTestCase): + def setUp(self): + qemu_img_create('-f', imgfmt, test_img, '1M') + + # Set up two VMs (source and destination) accessing the same raw + # image file with a virtio-blk device; prepare the destination for + # migration with .add_incoming() and enable migration events + vms =3D [None, None] + for i in range(2): + vms[i] =3D iotests.VM(path_suffix=3Df'{i}') + vms[i].add_blockdev(f'file,node-name=3Dprot,filename=3D{test_i= mg}') + vms[i].add_blockdev(f'{imgfmt},node-name=3Dfmt,file=3Dprot') + vms[i].add_device('virtio-blk,drive=3Dfmt') + + if i =3D=3D 1: + vms[i].add_incoming(f'unix:{mig_sock}') + + vms[i].launch() + + result =3D vms[i].qmp('migrate-set-capabilities', + capabilities=3D[ + {'capability': 'events', 'state': True} + ]) + self.assert_qmp(result, 'return', {}) + + self.vm_s =3D vms[0] + self.vm_d =3D vms[1] + + def tearDown(self): + self.vm_s.shutdown() + self.vm_d.shutdown() + try: + os.remove(mig_sock) + except FileNotFoundError: + pass + os.remove(test_img) + + # Migrate an image in use by a virtio-blk device to another VM and + # verify that the WRITE permission is unshared both before and after + # migration + def test_post_migration_permissions(self): + # Try to access the image R/W, which should fail because virtio-blk + # has not been configured with share-rw=3Don + log =3D qemu_io('-f', imgfmt, '-c', 'quit', test_img) + if not log.strip(): + print('ERROR (pre-migration): qemu-io should not be able to ' + 'access this image, but it reported no error') + else: + # This is the expected output + assert 'Is another process using the image' in log + + # Now migrate the VM + self.vm_s.qmp('migrate', uri=3Df'unix:{mig_sock}') + assert self.vm_s.wait_migration(None) + assert self.vm_d.wait_migration(None) + + # Try the same qemu-io access again, verifying that the WRITE + # permission remains unshared + log =3D qemu_io('-f', imgfmt, '-c', 'quit', test_img) + if not log.strip(): + print('ERROR (post-migration): qemu-io should not be able to ' + 'access this image, but it reported no error') + else: + # This is the expected output + assert 'Is another process using the image' in log + + +if __name__ =3D=3D '__main__': + # Only works with raw images because we are testing the + # BlockBackend permissions; image format drivers may additionally + # unshare permissions and thus tamper with the result + iotests.main(supported_fmts=3D['raw'], + supported_protocols=3D['file']) diff --git a/tests/qemu-iotests/tests/migration-permissions.out b/tests/qem= u-iotests/tests/migration-permissions.out new file mode 100644 index 0000000000..ae1213e6f8 --- /dev/null +++ b/tests/qemu-iotests/tests/migration-permissions.out @@ -0,0 +1,5 @@ +. +---------------------------------------------------------------------- +Ran 1 tests + +OK --=20 2.33.1