From nobody Thu Apr 25 05:55:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1595838783; cv=none; d=zohomail.com; s=zohoarc; b=Tf3YgZLxA76Bw21UUowJ1X+IkOkMyEc9/nmbKGspTvfAJQ2q+4se3zd4rU5JmeIt17SULT/IixFbwUCmlU7MeONoX60Zw6U8rcmeF3v86FKpW9Yco0uIgUaLJho0vUOY365edrqPrB/Tf2DbPMSIEgvfLahoxZ4vYF16GGZzm9o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595838783; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=SxogL/mccdWtBle+d2X01Glf1oQInZp7FqxFwyoea44=; b=ac2hau/Ja7dD3MBDq6liX6VYvZsYAOpLkjJ9LhanjTLVdTQe89dpmZV/MrD5Ol7o0pmwnWknWo08OZsx7aD5930vZwbJ2uwN3Yd8X+L5D1DBO63qEPMVxT8mIBa8i2fyHIdVe7PmGUqBotRYzhOl9od9gJUqjHA9IQX3X3+VvhI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 159583878342292.55012703489024; Mon, 27 Jul 2020 01:33:03 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-153-Pt898V1BO8WgW2w3HioTWA-1; Mon, 27 Jul 2020 04:31:36 -0400 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 2E6301932487; Mon, 27 Jul 2020 08:31:31 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0D91C10013C4; Mon, 27 Jul 2020 08:31:31 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CCA061809554; Mon, 27 Jul 2020 08:31:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06R8VQ2Z013592 for ; Mon, 27 Jul 2020 04:31:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 199CF60F96; Mon, 27 Jul 2020 08:31:26 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.218]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8BFC9610F3 for ; Mon, 27 Jul 2020 08:31:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595838782; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=SxogL/mccdWtBle+d2X01Glf1oQInZp7FqxFwyoea44=; b=gxVdNNHFsIkCbohc6IbjiB6u/9iHXTNNECPPFT35qQ0ZjpZMFjCPqzbtuVOF6n8O4kC8fq 3Xmx91XhcuBsaeDHAyWXOURUP+Lla93q0OWLn2rfgZc4lVeOtojIOuv5liU34pjrMhlENx hwEUNxcvjGFS/W0cyE0CKcgA+dkFeIM= X-MC-Unique: Pt898V1BO8WgW2w3HioTWA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/4] virdevmapper.c: Join two WITH_DEVMAPPER sections together Date: Mon, 27 Jul 2020 10:31:09 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) There are two distinct WITH_DEVMAPPER sections in the file, for different functions each. Rearrange the code to make some of future commits smaller. Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- src/util/virdevmapper.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c index 40a82285f9..118dc82e94 100644 --- a/src/util/virdevmapper.c +++ b/src/util/virdevmapper.c @@ -195,19 +195,7 @@ virDevMapperGetTargets(const char *path, return virDevMapperGetTargetsImpl(path, devPaths, ttl); } =20 -#else /* ! WITH_DEVMAPPER */ =20 -int -virDevMapperGetTargets(const char *path G_GNUC_UNUSED, - char ***devPaths G_GNUC_UNUSED) -{ - errno =3D ENOSYS; - return -1; -} -#endif /* ! WITH_DEVMAPPER */ - - -#if WITH_DEVMAPPER bool virIsDevMapperDevice(const char *dev_name) { @@ -223,6 +211,15 @@ virIsDevMapperDevice(const char *dev_name) =20 #else /* ! WITH_DEVMAPPER */ =20 +int +virDevMapperGetTargets(const char *path G_GNUC_UNUSED, + char ***devPaths G_GNUC_UNUSED) +{ + errno =3D ENOSYS; + return -1; +} + + bool virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED) { --=20 2.26.2 From nobody Thu Apr 25 05:55:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1595838739; cv=none; d=zohomail.com; s=zohoarc; b=PbSgPZAVvkIehy4pdQAoZTCf7zgoE0lfTLtCv4eAfVzVQVvgqr0+zIOGYZjmL5PV8IlP1xaIpeFaMZW9hgG8YeLn69Dfn+W12g/dtR1BAbjCJhaVgjoR8JH/f7FAdhGLU/XbDuoIs3dkggvRx6g+dFyXGrXhQVXr/dd3eON5zVg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595838739; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3H55hGDuEBw5eJjrB5AWybpoi0yEvxI58t+gC9uhS6c=; b=Ok6brdSAh36EeuglKiyJEo76lU45jJ/DDSqrhvwA1fNVOQV/uj6tucnJuL/mBsnmljxPUpPHssKZrs3IDLOh0vdbOOqEKnrFECg9hOiKFz7dM0YEvYmFYZJrtHjhPSQmJYdNcvxfMsYzM8WVzed9Zz6N8VxXfmf4vIRx9vX2O9c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1595838739103357.3134626161145; Mon, 27 Jul 2020 01:32:19 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-307-N7QNeMKwNumUdrXbnYX3cw-1; Mon, 27 Jul 2020 04:31:39 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 03BB88015CE; Mon, 27 Jul 2020 08:31:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D5BF68FA5A; Mon, 27 Jul 2020 08:31:33 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A12431800FDE; Mon, 27 Jul 2020 08:31:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06R8VTNl013606 for ; Mon, 27 Jul 2020 04:31:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 18F8C6842F; Mon, 27 Jul 2020 08:31:29 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.218]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C05D60F96 for ; Mon, 27 Jul 2020 08:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595838737; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=3H55hGDuEBw5eJjrB5AWybpoi0yEvxI58t+gC9uhS6c=; b=dTKYCv8NBw/eJox1NSMtLtKWZPUqyhFUs2RS9L3eppXadMk6+rOWy2nxRR3PfsIf22EzEF kyp/ksMrplxFuhiYy4u18TVdXqpVVA9eQnw8KSMNnTnZFvoa3pyYb/R+odkcRK4PxWphFX Uv+4BlHkho7c5pNDqxZZl3GfxaArMhU= X-MC-Unique: N7QNeMKwNumUdrXbnYX3cw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/4] virDevMapperGetTargetsImpl: Use VIR_AUTOSTRINGLIST Date: Mon, 27 Jul 2020 10:31:10 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Since we have VIR_AUTOSTRINGLIST we can use it to free string lists used in the function automatically. Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- src/util/virdevmapper.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c index 118dc82e94..44c4731fb4 100644 --- a/src/util/virdevmapper.c +++ b/src/util/virdevmapper.c @@ -67,8 +67,7 @@ virDevMapperGetTargetsImpl(const char *path, struct dm_task *dmt =3D NULL; struct dm_deps *deps; struct dm_info info; - char **devPaths =3D NULL; - char **recursiveDevPaths =3D NULL; + VIR_AUTOSTRINGLIST devPaths =3D NULL; size_t i; int ret =3D -1; =20 @@ -133,28 +132,19 @@ virDevMapperGetTargetsImpl(const char *path, minor(deps->device[i])); } =20 - recursiveDevPaths =3D NULL; for (i =3D 0; i < deps->count; i++) { - char **tmpPaths; + VIR_AUTOSTRINGLIST tmpPaths =3D NULL; =20 if (virDevMapperGetTargetsImpl(devPaths[i], &tmpPaths, ttl - 1) < = 0) goto cleanup; =20 - if (tmpPaths && - virStringListMerge(&recursiveDevPaths, &tmpPaths) < 0) { - virStringListFree(tmpPaths); + if (virStringListMerge(&devPaths, &tmpPaths) < 0) goto cleanup; - } } =20 - if (virStringListMerge(&devPaths, &recursiveDevPaths) < 0) - goto cleanup; - *devPaths_ret =3D g_steal_pointer(&devPaths); ret =3D 0; cleanup: - virStringListFree(recursiveDevPaths); - virStringListFree(devPaths); dm_task_destroy(dmt); return ret; } --=20 2.26.2 From nobody Thu Apr 25 05:55:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1595838702; cv=none; d=zohomail.com; s=zohoarc; b=ZpWf1IlSzkJ72R52YwJ6JlhUYZKoxke6XckJpXPOHdrbRzUISzYHTXOfKJr4SgIcZbRmgZOCvkk4LojvKtv243EXOL/etFDtbr20s8Q27NlYS/19dveB/NiWOg9stXixwpyB36QlCsnJG275SH1J3V9G7u1eW2YKW++k8+gVIbA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595838702; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gtDu7Vr/7lqNzHxsHja2bVhhjU4UJ+Ih5lLgd9zYZSQ=; b=cRMMLxhWMyqDUn58hxpqPPiA2TR69+Q3aRVTiBE2jsBW5tJJg/v5wAC0LG1OFQ8TpmCfqHslGLtXoVO7psRYUtbF3sE9qSNb2aIdvyUZwG3EEi4fDpu6ChPCTv9kZ+q2aUOyEvzAr37CXGK1Jw1UkRapT/55R1J4xNuvtWn9kfc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1595838702098179.1297700325598; Mon, 27 Jul 2020 01:31:42 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-271-ARxELZs2N0K__6Zz5f_Dbg-1; Mon, 27 Jul 2020 04:31:38 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C001B100AA2A; Mon, 27 Jul 2020 08:31:32 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9A8C45D9F3; Mon, 27 Jul 2020 08:31:32 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5B6DF1809561; Mon, 27 Jul 2020 08:31:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06R8VTD0013611 for ; Mon, 27 Jul 2020 04:31:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id EE5D037DD; Mon, 27 Jul 2020 08:31:29 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.218]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C70760F96 for ; Mon, 27 Jul 2020 08:31:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595838700; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=gtDu7Vr/7lqNzHxsHja2bVhhjU4UJ+Ih5lLgd9zYZSQ=; b=XD75Nx6dhh4WrJ6BBawmiDzlLJg4b4161UudfB0JjzgX1Ipk0V0In+Vb6bLEBHD+qKaNmw EiFejYgzW7RiaeXxjgTrzmlwJLcB7Y+12waksIn6skzCgpwvFjy/DEWYE5tVSahjz5ibQH fbk3NfMgb7Kyt9bY48Qq5n9alAL60EE= X-MC-Unique: ARxELZs2N0K__6Zz5f_Dbg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/4] virdevmapper: Don't use libdevmapper to obtain dependencies Date: Mon, 27 Jul 2020 10:31:11 +0200 Message-Id: <22494556542c676d1b9e7f1c1f2ea13ac17e1e3e.1595837960.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) CVE-2020-14339 When building domain's private /dev in a namespace, libdevmapper is consulted for getting full dependency tree of domain's disks. The reason is that for a multipath devices all dependent devices must be created in the namespace and allowed in CGroups. However, this approach is very fragile as building of namespace happens in the forked off child process, after mass close of FDs and just before dropping privileges and execing QEMU. And it so happens that when calling libdevmapper APIs, one of them opens /dev/mapper/control and saves the FD into a global variable. The FD is kept open until the lib is unlinked or dm_lib_release() is called explicitly. We are doing neither. However, the virDevMapperGetTargets() function is called also from libvirtd (when setting up CGroups) and thus has to be thread safe. Unfortunately, libdevmapper APIs are not thread safe (nor async signal safe) and thus we can't use them. Reimplement what libdevmapper would do using plain C (ioctl()-s, /proc/devices parsing, /dev/mapper dirwalking, and so on). Fixes: a30078cb832646177defd256e77c632905f1e6d0 Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1858260 Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- po/POTFILES.in | 1 + src/util/virdevmapper.c | 304 ++++++++++++++++++++++++++++------------ 2 files changed, 219 insertions(+), 86 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index b10008ae3d..0498a0e819 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -240,6 +240,7 @@ @SRCDIR@src/util/vircrypto.c @SRCDIR@src/util/virdaemon.c @SRCDIR@src/util/virdbus.c +@SRCDIR@src/util/virdevmapper.c @SRCDIR@src/util/virdnsmasq.c @SRCDIR@src/util/virerror.c @SRCDIR@src/util/virerror.h diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c index 44c4731fb4..a471504176 100644 --- a/src/util/virdevmapper.c +++ b/src/util/virdevmapper.c @@ -20,38 +20,67 @@ =20 #include =20 -#ifdef __linux__ -# include -#endif - -#ifdef WITH_DEVMAPPER -# include -#endif - #include "virdevmapper.h" #include "internal.h" -#include "virthread.h" -#include "viralloc.h" -#include "virstring.h" - -#ifdef WITH_DEVMAPPER -static void -virDevMapperDummyLogger(int level G_GNUC_UNUSED, - const char *file G_GNUC_UNUSED, - int line G_GNUC_UNUSED, - int dm_errno G_GNUC_UNUSED, - const char *fmt G_GNUC_UNUSED, - ...) -{ - return; -} + +#ifdef __linux__ +# include +# include +# include +# include +# include +# include + +# include "virthread.h" +# include "viralloc.h" +# include "virstring.h" +# include "virfile.h" + +# define VIR_FROM_THIS VIR_FROM_STORAGE + +# define PROC_DEVICES "/proc/devices" +# define DM_NAME "device-mapper" +# define DEV_DM_DIR "/dev/" DM_DIR +# define CONTROL_PATH DEV_DM_DIR "/" DM_CONTROL_NODE +# define BUF_SIZE (16 * 1024) + +G_STATIC_ASSERT(BUF_SIZE > sizeof(struct dm_ioctl)); + +static unsigned int virDMMajor; + =20 static int virDevMapperOnceInit(void) { - /* Ideally, we would not need this. But libdevmapper prints - * error messages to stderr by default. Sad but true. */ - dm_log_with_errno_init(virDevMapperDummyLogger); + g_autofree char *buf =3D NULL; + VIR_AUTOSTRINGLIST lines =3D NULL; + size_t i; + + if (virFileReadAll(PROC_DEVICES, BUF_SIZE, &buf) < 0) + return -1; + + lines =3D virStringSplit(buf, "\n", 0); + if (!lines) + return -1; + + for (i =3D 0; lines[i]; i++) { + g_autofree char *dev =3D NULL; + unsigned int maj; + + if (sscanf(lines[i], "%u %ms\n", &maj, &dev) =3D=3D 2 && + STREQ(dev, DM_NAME)) { + virDMMajor =3D maj; + break; + } + } + + if (!lines[i]) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find major for %s"), + DM_NAME); + return -1; + } + return 0; } =20 @@ -59,94 +88,190 @@ virDevMapperOnceInit(void) VIR_ONCE_GLOBAL_INIT(virDevMapper); =20 =20 +static void * +virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf) +{ + size_t bufsize =3D BUF_SIZE; + + reread: + *buf =3D g_new0(char, bufsize); + + dm->version[0] =3D DM_VERSION_MAJOR; + dm->version[1] =3D 0; + dm->version[2] =3D 0; + dm->data_size =3D bufsize; + dm->data_start =3D sizeof(struct dm_ioctl); + + memcpy(*buf, dm, sizeof(struct dm_ioctl)); + + if (ioctl(controlFD, cmd, *buf) < 0) { + VIR_FREE(*buf); + return NULL; + } + + memcpy(dm, *buf, sizeof(struct dm_ioctl)); + + if (dm->flags & DM_BUFFER_FULL_FLAG) { + bufsize +=3D BUF_SIZE; + VIR_FREE(*buf); + goto reread; + } + + return *buf + dm->data_start; +} + + static int -virDevMapperGetTargetsImpl(const char *path, +virDMOpen(void) +{ + VIR_AUTOCLOSE controlFD =3D -1; + struct dm_ioctl dm; + g_autofree char *tmp =3D NULL; + int ret; + + memset(&dm, 0, sizeof(dm)); + + if ((controlFD =3D open(CONTROL_PATH, O_RDWR)) < 0) + return -1; + + if (!virDMIoctl(controlFD, DM_VERSION, &dm, &tmp)) { + virReportSystemError(errno, "%s", + _("Unable to get device-mapper version")); + return -1; + } + + if (dm.version[0] !=3D DM_VERSION_MAJOR) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("Unsupported device-mapper version. Expected %d g= ot %d"), + DM_VERSION_MAJOR, dm.version[0]); + return -1; + } + + ret =3D controlFD; + controlFD =3D -1; + return ret; +} + + +static char * +virDMSanitizepath(const char *path) +{ + g_autofree char *dmDirPath =3D NULL; + struct dirent *ent =3D NULL; + struct stat sb[2]; + DIR *dh =3D NULL; + const char *p; + char *ret =3D NULL; + int rc; + + /* If a path is NOT provided then assume it's DM name */ + p =3D strrchr(path, '/'); + + if (!p) + return g_strdup(path); + else + p++; + + /* It's a path. Check if the last component is DM name */ + if (stat(path, &sb[0]) < 0) { + virReportError(errno, + _("Unable to stat %p"), + path); + return NULL; + } + + dmDirPath =3D g_strdup_printf(DEV_DM_DIR "/%s", p); + + if (stat(dmDirPath, &sb[1]) =3D=3D 0 && + sb[0].st_rdev =3D=3D sb[1].st_rdev) { + return g_strdup(p); + } + + /* The last component of @path wasn't DM name. Let's check if + * there's a device under /dev/mapper/ with the same rdev. */ + if (virDirOpen(&dh, DEV_DM_DIR) < 0) + return NULL; + + while ((rc =3D virDirRead(dh, &ent, DEV_DM_DIR)) > 0) { + g_autofree char *tmp =3D g_strdup_printf(DEV_DM_DIR "/%s", ent->d_= name); + + if (stat(tmp, &sb[1]) =3D=3D 0 && + sb[0].st_rdev =3D=3D sb[0].st_rdev) { + ret =3D g_steal_pointer(&tmp); + break; + } + } + + virDirClose(&dh); + return ret; +} + + +static int +virDevMapperGetTargetsImpl(int controlFD, + const char *path, char ***devPaths_ret, unsigned int ttl) { - struct dm_task *dmt =3D NULL; - struct dm_deps *deps; - struct dm_info info; + g_autofree char *sanitizedPath =3D NULL; + g_autofree char *buf =3D NULL; + struct dm_ioctl dm; + struct dm_target_deps *deps =3D NULL; VIR_AUTOSTRINGLIST devPaths =3D NULL; size_t i; - int ret =3D -1; =20 + memset(&dm, 0, sizeof(dm)); *devPaths_ret =3D NULL; =20 - if (virDevMapperInitialize() < 0) - return ret; - if (ttl =3D=3D 0) { errno =3D ELOOP; - return ret; + return -1; } =20 if (!virIsDevMapperDevice(path)) return 0; =20 - if (!(dmt =3D dm_task_create(DM_DEVICE_DEPS))) { - if (errno =3D=3D ENOENT || errno =3D=3D ENODEV) { - /* It's okay. Kernel is probably built without - * devmapper support. */ - ret =3D 0; - } - return ret; - } + if (!(sanitizedPath =3D virDMSanitizepath(path))) + return 0; =20 - if (!dm_task_set_name(dmt, path)) { - if (errno =3D=3D ENOENT) { - /* It's okay, @path is not managed by devmapper =3D> - * not a devmapper device. */ - ret =3D 0; - } - goto cleanup; + if (virStrncpy(dm.name, sanitizedPath, -1, DM_TABLE_DEPS) < 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("Resolved device mapper name too long")); + return -1; } =20 - dm_task_no_open_count(dmt); + deps =3D virDMIoctl(controlFD, DM_TABLE_DEPS, &dm, &buf); + if (!deps) { + if (errno =3D=3D ENXIO) + return 0; =20 - if (!dm_task_run(dmt)) { - if (errno =3D=3D ENXIO) { - /* If @path =3D "/dev/mapper/control" ENXIO is returned. */ - ret =3D 0; - } - goto cleanup; + virReportSystemError(errno, + _("Unable to query dependencies for %s"), + path); + return -1; } =20 - if (!dm_task_get_info(dmt, &info)) - goto cleanup; - - if (!info.exists) { - ret =3D 0; - goto cleanup; - } - - if (!(deps =3D dm_task_get_deps(dmt))) - goto cleanup; - if (VIR_ALLOC_N_QUIET(devPaths, deps->count + 1) < 0) - goto cleanup; + return -1; =20 for (i =3D 0; i < deps->count; i++) { devPaths[i] =3D g_strdup_printf("/dev/block/%u:%u", - major(deps->device[i]), - minor(deps->device[i])); + major(deps->dev[i]), + minor(deps->dev[i])); } =20 for (i =3D 0; i < deps->count; i++) { VIR_AUTOSTRINGLIST tmpPaths =3D NULL; =20 - if (virDevMapperGetTargetsImpl(devPaths[i], &tmpPaths, ttl - 1) < = 0) - goto cleanup; + if (virDevMapperGetTargetsImpl(controlFD, devPaths[i], &tmpPaths, = ttl - 1) < 0) + return -1; =20 if (virStringListMerge(&devPaths, &tmpPaths) < 0) - goto cleanup; + return -1; } =20 *devPaths_ret =3D g_steal_pointer(&devPaths); - ret =3D 0; - cleanup: - dm_task_destroy(dmt); - return ret; + return 0; } =20 =20 @@ -165,9 +290,6 @@ virDevMapperGetTargetsImpl(const char *path, * If @path consists of yet another devmapper targets these are * consulted recursively. * - * If we don't have permissions to talk to kernel, -1 is returned - * and errno is set to EBADF. - * * Returns 0 on success, * -1 otherwise (with errno set, no libvirt error is * reported) @@ -176,13 +298,20 @@ int virDevMapperGetTargets(const char *path, char ***devPaths) { + VIR_AUTOCLOSE controlFD =3D -1; const unsigned int ttl =3D 32; =20 /* Arbitrary limit on recursion level. A devmapper target can * consist of devices or yet another targets. If that's the * case, we have to stop recursion somewhere. */ =20 - return virDevMapperGetTargetsImpl(path, devPaths, ttl); + if (virDevMapperInitialize() < 0) + return -1; + + if ((controlFD =3D virDMOpen()) < 0) + return -1; + + return virDevMapperGetTargetsImpl(controlFD, path, devPaths, ttl); } =20 =20 @@ -191,15 +320,18 @@ virIsDevMapperDevice(const char *dev_name) { struct stat buf; =20 + if (virDevMapperInitialize() < 0) + return false; + if (!stat(dev_name, &buf) && S_ISBLK(buf.st_mode) && - dm_is_dm_major(major(buf.st_rdev))) - return true; + major(buf.st_rdev) =3D=3D virDMMajor) + return true; =20 return false; } =20 -#else /* ! WITH_DEVMAPPER */ +#else /* !defined(__linux__) */ =20 int virDevMapperGetTargets(const char *path G_GNUC_UNUSED, @@ -215,4 +347,4 @@ virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED) { return false; } -#endif /* ! WITH_DEVMAPPER */ +#endif /* ! defined(__linux__) */ --=20 2.26.2 From nobody Thu Apr 25 05:55:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1595838705; cv=none; d=zohomail.com; s=zohoarc; b=PESFDkeeL7zEQyToSHSUjVf+9J/VMnsOR/TjqsZTyMOLGZrvA60KfcD2zpulnABXuOIw5MnwZqKdSdAvtyZDN1ADirVg+wwN4WYALW/xTvlzzWznidH4fn0SnbKtcr4dO6em2NoqlGngE2dZUCSAd4oNzE5idB6FzsL73vu3+i0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595838705; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FDNHg967hcvKe1SzdXd6pU/C+tC4T1ZpLrzP9TZCJoU=; b=AOp0jLKw6iaDQXMv1xVSkeF/mbG+9fWYnD6VCEtVzjBbvnmFEKqeMMx090KqzvUBtHmzvXTOKSTZz869JeNXoTT7hnHWk+G4mBwyOgYX0NPdopubdPQSyYrUKVDrsyF3UzJG6bklf07gcCWG+IGOUH65Y5kxxdroqFHopHwni6g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1595838705316714.8844944699987; Mon, 27 Jul 2020 01:31:45 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-251-zL2lC1PZN3S_oxKeHpwKsQ-1; Mon, 27 Jul 2020 04:31:41 -0400 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 3E493100AA2F; Mon, 27 Jul 2020 08:31:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1D87572685; Mon, 27 Jul 2020 08:31:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DDE0D1806B0A; Mon, 27 Jul 2020 08:31:34 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06R8VUm6013622 for ; Mon, 27 Jul 2020 04:31:30 -0400 Received: by smtp.corp.redhat.com (Postfix) id CF6B66842F; Mon, 27 Jul 2020 08:31:30 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.218]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E540610F3 for ; Mon, 27 Jul 2020 08:31:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595838704; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=FDNHg967hcvKe1SzdXd6pU/C+tC4T1ZpLrzP9TZCJoU=; b=hi5ei+jLYC5TJ4Va1vSmqZRG5CQg7XbmcH/GBq/GON61mWd9g1/CrUHC+NJRglSKdQz6Kx nTL+OOsF1OVgX7fgXZhP2zFFiJgxMYWPjYDDwBguksCXhcXpvhDQqwoRNVqrR5PjuSD/6z EubLm44nM749AlaCCaRVITW8BxMoveE= X-MC-Unique: zL2lC1PZN3S_oxKeHpwKsQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 4/4] virDevMapperGetTargets: Don't ignore EBADF Date: Mon, 27 Jul 2020 10:31:12 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- src/qemu/qemu_cgroup.c | 2 +- src/qemu/qemu_domain.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 914bf640ca..e88da02341 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -87,7 +87,7 @@ qemuSetupImagePathCgroup(virDomainObjPtr vm, } =20 if (virDevMapperGetTargets(path, &targetPaths) < 0 && - errno !=3D ENOSYS && errno !=3D EBADF) { + errno !=3D ENOSYS) { virReportSystemError(errno, _("Unable to get devmapper targets for %s"), path); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 5b22eb2eaa..2058290870 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -10264,7 +10264,7 @@ qemuDomainSetupDisk(virQEMUDriverConfigPtr cfg G_GN= UC_UNUSED, return -1; =20 if (virDevMapperGetTargets(next->path, &targetPaths) < 0 && - errno !=3D ENOSYS && errno !=3D EBADF) { + errno !=3D ENOSYS) { virReportSystemError(errno, _("Unable to get devmapper targets fo= r %s"), next->path); @@ -11328,7 +11328,7 @@ qemuDomainNamespaceSetupDisk(virDomainObjPtr vm, tmpPath =3D g_strdup(next->path); =20 if (virDevMapperGetTargets(next->path, &targetPaths) < 0 && - errno !=3D ENOSYS && errno !=3D EBADF) { + errno !=3D ENOSYS) { virReportSystemError(errno, _("Unable to get devmapper targets fo= r %s"), next->path); --=20 2.26.2