From nobody Fri Dec 12 14:05:29 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1765293373; cv=none; d=zohomail.com; s=zohoarc; b=N79+qUTyYFBlI4hSI0zSxy5tMJ+s0bLJGe+KztEt8KrvEnEmqJLUNOhplFAoiQt1b7klCT1IfLBUIc83NMiKJohtsB9fEfKS1VYJZuKvbiB1Sbyc93rbMhfy+zj/hEthZ3th+5z7d+u9TTEfhegwh5lA6Ye7wZ3FCRtT0liBwFY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765293373; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=jsOkJVnfnN8CcwXzIsEbFXNhJLsUG631shyXKgDIWcs=; b=WqHQqZAldZwhhkpWohXsd8OPftvd85GEai6Qr2ZzBqZzg4J8u3PWY0oFyb5lgRa2pVlT+fqqHcFSEpr3HJBDe+qGNRktwYqZXiWPLkoE4r4Mr/ycPcYyLseMJDE3Mt/tJSz7PUMG/P05/hpZiDfSiZgDh9iRvB0AIfV1OwUPb4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1765293373122188.93158930369668; Tue, 9 Dec 2025 07:16:13 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 993) id 3D000418BE; Tue, 9 Dec 2025 10:16:11 -0500 (EST) Received: from [172.19.199.80] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id C139F43DEA; Tue, 9 Dec 2025 10:15:02 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 993) id 5CB3D419A5; Tue, 9 Dec 2025 10:03:43 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 50CED41998 for ; Tue, 9 Dec 2025 10:03:42 -0500 (EST) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-597-X67Ndys6MyeWhK_whLNwoA-1; Tue, 09 Dec 2025 10:03:40 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ECD131955F0E for ; Tue, 9 Dec 2025 15:03:39 +0000 (UTC) Received: from orkuz (unknown [10.44.33.57]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5E04030001A5 for ; Tue, 9 Dec 2025 15:03:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765292622; h=from:from: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; bh=jsOkJVnfnN8CcwXzIsEbFXNhJLsUG631shyXKgDIWcs=; b=E7zmTWRUFIAl8kVvP7SBcFmRrQZYLZgVCoq1XCQ5ckxvbdbnTP9mmgmWpu8Xi7XDcMQWMD YqfTp4rXOA1lNn2D98DtoNlydIWUGZ4gwK2nrx/FEBF4HCwe5kjGZDQDm/a0odq+3XMWwx 5wAYFdbIui5N49DW8DUUViwsjFubeig= X-MC-Unique: X67Ndys6MyeWhK_whLNwoA-1 X-Mimecast-MFC-AGG-ID: X67Ndys6MyeWhK_whLNwoA_1765292620 To: devel@lists.libvirt.org Subject: [PATCH 3/4] util: Fix race condition in virFileIsSharedFSOverride Date: Tue, 9 Dec 2025 16:02:29 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 44b8A4OPrx9hgHm0ICzt_9nAfxmDrIxlZWpyoK68qtI_1765292620 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JUDWUBICRA6MYBUG32TDTL6MFGYP4HDD X-Message-ID-Hash: JUDWUBICRA6MYBUG32TDTL6MFGYP4HDD X-MailFrom: jdenemar@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: <> List-Archive: <> List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Jiri Denemark via Devel Reply-To: Jiri Denemark X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1765293374426158500 Content-Type: text/plain; charset="utf-8" From: Jiri Denemark Switch virFileIsSharedFSOverride to use virFileCheckParents to avoid a race which could result in virFileCanonicalizePath to be called on a path that does not exist anymore. Signed-off-by: Jiri Denemark --- src/util/virfile.c | 59 +++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index b0f4041df4..ea68215655 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3502,33 +3502,6 @@ virFileCheckParents(const char *path, } =20 =20 -static char * -virFileGetExistingParent(const char *path) -{ - g_autofree char *dirpath =3D g_strdup(path); - char *p =3D NULL; - - /* Try less and less of the path until we get to a directory we can ac= cess. - * Even if we don't have 'x' permission on any directory in the path o= n the - * NFS server (assuming it's NFS), we will be able to stat the mount p= oint. - */ - while (!virFileExists(dirpath) && p !=3D dirpath) { - if (!(p =3D strrchr(dirpath, '/'))) { - virReportSystemError(EINVAL, - _("Invalid relative path '%1$s'"), path); - return NULL; - } - - if (p =3D=3D dirpath) - *(p + 1) =3D '\0'; - else - *p =3D '\0'; - } - - return g_steal_pointer(&dirpath); -} - - #ifdef __linux__ =20 # ifndef NFS_SUPER_MAGIC @@ -3875,6 +3848,17 @@ virFileGetDefaultHugepage(virHugeTLBFS *fs, } =20 =20 +static bool +virFileCheckParentsCanonicalize(const char *path, + void *opaque) +{ + char **canonical =3D opaque; + + *canonical =3D virFileCanonicalizePath(path); + return !!*canonical; +} + + /** * virFileIsSharedFSOverride: * @path: Path to check @@ -3888,24 +3872,25 @@ virFileIsSharedFSOverride(const char *path, char *const *overrides) { g_autofree char *dirpath =3D NULL; - g_autofree char *existing =3D NULL; char *p =3D NULL; + int rc; =20 if (!path || path[0] !=3D '/' || !overrides) return false; =20 /* We only care about the longest existing sub-path. Further components - * may will later be created by libvirt will not magically become a sh= ared - * filesystem. */ - if (!(existing =3D virFileGetExistingParent(path))) + * that may later be created by libvirt will not magically become a sh= ared + * filesystem. Overrides have been canonicalized ahead of time, so we = need + * to do the same for the provided path or we'll never be able to find= a + * match if symlinks are involved. + */ + rc =3D virFileCheckParents(path, NULL, + virFileCheckParentsCanonicalize, &dirpath); + if (rc =3D=3D -1) return false; =20 - /* Overrides have been canonicalized ahead of time, so we need to - * do the same for the provided path or we'll never be able to - * find a match if symlinks are involved */ - if (!(dirpath =3D virFileCanonicalizePath(existing))) { - VIR_DEBUG("Cannot canonicalize parent '%s' of path '%s'", - existing, path); + if (rc !=3D 0) { + VIR_DEBUG("Cannot canonicalize path '%s'", path); return false; } =20 --=20 2.52.0