From nobody Tue May 7 05:35:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1563441306; cv=none; d=zoho.com; s=zohoarc; b=X38vigk/pmWgsmLY/rOpBVB8e2OuyMKBrnohJWM/aSwa1EB9pWyoe0cxV0/VgMq6r/UE9CQozA7iLL76RpAvp/VWg15qb9pSIdvyKDtwB5ZEtN1ZihIgRSXdsBqm+IS7BA12g53FUK8M91jj2Cj9ON2H44iaFq6QD3IopH1/yhM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563441306; h=Content-Type:Content-Transfer-Encoding:Cc: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:ARC-Authentication-Results; bh=k4SjTc6TBqn+3LpQ6eh5u1AdSTrWg3IyAX8j4vtP3zk=; b=MUBDxafHw2NAzBMJXnNNfF+2Y6zb1DYcoi3Z6cbbaQpkTvIiW46J1pn14DmgqZCRjBQak6uAX0XMqMg2ewgNHQHj1t966yl22Dnca9wA1FSazZhA+oxVVFt60XPWupqANjW/pXXT8NuSJEWebz3tvrGrZvcBBwQD+5dmas7QS8Q= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1563441306303998.1896291890617; Thu, 18 Jul 2019 02:15:06 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 51BE730BD1AF; Thu, 18 Jul 2019 09:15:04 +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 7CD365DE68; Thu, 18 Jul 2019 09:15:02 +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 170B61800206; Thu, 18 Jul 2019 09:15:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6I9Ewl9021912 for ; Thu, 18 Jul 2019 05:14:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9853D6013C; Thu, 18 Jul 2019 09:14:58 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id F200B6013B; Thu, 18 Jul 2019 09:14:57 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Jul 2019 11:14:48 +0200 Message-Id: <4c66159b3978460ab82eced2392ec3383e04c7fa.1563441249.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.com Subject: [libvirt] [PATCH 1/2] virSecurityManagerMetadataLock: Expand the comment on deadlocks 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: , Content-Transfer-Encoding: quoted-printable 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-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 18 Jul 2019 09:15:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Document why we need to sort paths while it's still fresh in my memory. Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 Tested-by: Daniel Henrique Barboza --- src/security/security_manager.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/security/security_manager.c b/src/security/security_manage= r.c index c205c3bf17..ade2c96141 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -1289,7 +1289,12 @@ virSecurityManagerMetadataLock(virSecurityManagerPtr= mgr ATTRIBUTE_UNUSED, if (VIR_ALLOC_N(fds, npaths) < 0) return NULL; =20 - /* Sort paths to lock in order to avoid deadlocks. */ + /* Sort paths to lock in order to avoid deadlocks with other + * processes. For instance, if one process wants to lock + * paths A B and there's another that is trying to lock them + * in reversed order a deadlock might occur. But if we sort + * the paths alphabetically then both processes will try lock + * paths in the same order and thus no deadlock can occur. */ qsort(paths, npaths, sizeof(*paths), cmpstringp); =20 for (i =3D 0; i < npaths; i++) { --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 05:35:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1563441306; cv=none; d=zoho.com; s=zohoarc; b=VtJfSL1aUcV5iNIOxgqVIPG8bZ8ZwQBd2NiAT/OiUjqmwLYUeLrwkxoTpzzwLf6BDmffIoIcIWMC+26kTVF2hQDLS1a4qIJwc4w/NhOZAHztL2uLTo2YmsED678yGv7TXN855udGmS26OapSDf3UJFOAgs9NYYio5T9tR3o+a5c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563441306; h=Content-Type:Content-Transfer-Encoding:Cc: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:ARC-Authentication-Results; bh=/GfHBUOGsTnQREgyMO0DC+pfVFaZzB/ayPMzVHS4pQE=; b=IcaRYvYlYBQ2ibAeFUGT5Dc2LS1yhR1hfAtpFXPVdt/prsM5K5oJqH59X781SWI4nCwVuARukoikMOUhocgmGKEzuvnNlxQxFbj0XM3LKniW4Vnby3xhwwp9mXlxpXpzPPsm60FFJ0rKmqYiYLl4T5WkubmMQzPr21XfIoo8Z28= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1563441306582650.8789352438173; Thu, 18 Jul 2019 02:15:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 915368E22B; Thu, 18 Jul 2019 09:15:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 16FF819C65; Thu, 18 Jul 2019 09:15:04 +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 B3A9141F66; Thu, 18 Jul 2019 09:15:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6I9Exss021920 for ; Thu, 18 Jul 2019 05:14:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8B5D06013D; Thu, 18 Jul 2019 09:14:59 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id E475A6013B; Thu, 18 Jul 2019 09:14:58 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Jul 2019 11:14:49 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.com Subject: [libvirt] [PATCH 2/2] virSecurityManagerMetadataLock: Skip over duplicate paths 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 18 Jul 2019 09:15:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" If there are two paths on the list that are the same we need to lock it only once. Because when we try to lock it the second time then open() fails. And if it didn't, locking it the second time would fail for sure. After all, it is sufficient to lock all paths just once satisfy the caller. Reported-by: Daniel Henrique Barboza Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 Tested-by: Daniel Henrique Barboza --- src/security/security_manager.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/security/security_manager.c b/src/security/security_manage= r.c index ade2c96141..7c905f0785 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -1294,16 +1294,35 @@ virSecurityManagerMetadataLock(virSecurityManagerPt= r mgr ATTRIBUTE_UNUSED, * paths A B and there's another that is trying to lock them * in reversed order a deadlock might occur. But if we sort * the paths alphabetically then both processes will try lock - * paths in the same order and thus no deadlock can occur. */ + * paths in the same order and thus no deadlock can occur. + * Lastly, it makes searching for duplicate paths below + * simpler. */ qsort(paths, npaths, sizeof(*paths), cmpstringp); =20 for (i =3D 0; i < npaths; i++) { const char *p =3D paths[i]; struct stat sb; + size_t j; int retries =3D 10 * 1000; int fd; =20 - if (!p || stat(p, &sb) < 0) + if (!p) + continue; + + /* If there's a duplicate path on the list, skip it over. + * Not only we would fail open()-ing it the second time, + * we would deadlock with ourselves trying to lock it the + * second time. After all, we've locked it when iterating + * over it the first time. */ + for (j =3D 0; j < i; j++) { + if (STREQ_NULLABLE(p, paths[j])) + break; + } + + if (i !=3D j) + continue; + + if (stat(p, &sb) < 0) continue; =20 if (S_ISDIR(sb.st_mode)) { --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list