From nobody Tue May 7 11:36:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 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=1667231599; cv=none; d=zohomail.com; s=zohoarc; b=IyR1oMq2XaqZXhuw88DbwCmrZK80E/L4yQVI91w4m2FrzUgNd91Qx7Nur90klc/pzZnrZoG+bzUVn2ipOZx3m4F1y416zC34w8GWQIYiLeat43hXmvuZRlY92CuxJFyYTSEoJqP4iLxSV2qWtPczdDpiQTkC/6d5UloPR9/9XV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1667231599; 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=rxZAzNviVJHI52Q2EhBA2pRNcaz03D5SK7/Sa/RS/pI=; b=I1AGqqXl69f/7ELbunkuCV/0RNxG+t5AuL6qmxQAX6sQq+Eur2RQUuiQ5bhnzJSOpC5JK0kneB4FgWxSGDzQmstI3eZdY0jZXVKPHh58ddqhN0siI8ATCWAbsnShPgSkL8UhXbvccF6eVUmeJf6wUBiwHXCMrhNgkQBILfjOt9U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1667231599228221.3760032424351; Mon, 31 Oct 2022 08:53:19 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-544-zTn0zPchNAK_cV1iwCs4bg-1; Mon, 31 Oct 2022 11:53:14 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2DCC486C058; Mon, 31 Oct 2022 15:53:12 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9632F1401C23; Mon, 31 Oct 2022 15:53:11 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 70ABA1946A44; Mon, 31 Oct 2022 15:53:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id A7BA01946594 for ; Mon, 31 Oct 2022 15:53:03 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 39D9940C6FA1; Mon, 31 Oct 2022 15:53:03 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id D385B40C6EC6 for ; Mon, 31 Oct 2022 15:53:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667231598; 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=rxZAzNviVJHI52Q2EhBA2pRNcaz03D5SK7/Sa/RS/pI=; b=IO5A25oLtU7xl+W1VqoPU7Aivskd+NI/5gHxTq+K0uPe33TiWCbriAK3q4xNH6SltLYUV+ qVXdR+EBU5NcVsHMvmGSGK2IVVP/Ox9RX6thG4+kxfrCtoULefC/ku8t2NtSaaodTqi2rX 1kivlYC4d7oNWaDj8YKWObEcbmwSvW4= X-MC-Unique: zTn0zPchNAK_cV1iwCs4bg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/2] qemu_namespace: Don't leak memory in qemuDomainGetPreservedMounts() Date: Mon, 31 Oct 2022 16:52:59 +0100 Message-Id: <17283b3497fb4fa497562ba21f1e1139cf8ed978.1667231427.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1667231600224100001 Content-Type: text/plain; charset="utf-8"; x-default="true" The aim of qemuDomainGetPreservedMounts() is to get a list of filesystems mounted under /dev and optionally generate a path for each one where they are moved temporarily when building the namespace. And the function tries to be a bit clever about it. For instance, if /dev/shm mount point exists, there's no need to consider /dev/shm/a nor /dev/shm/b as preserving just 'top level' /dev/shm gives the same result. To achieve this, the function iterates over the list of filesystem as returned by virFileGetMountSubtree() and removes the nested ones. However, it does so in a bit clumsy way: plain VIR_DELETE_ELEMENT() is used without freeing the string itself. Therefore, if all three aforementioned example paths appeared on the list, /dev/shm/a and /dev/shm/b strings would be leaked. And when I think about it more, there's no real need to shrink the array down (realloc()). It's going to be free()-d when returning from the function. Switch to VIR_DELETE_ELEMENT_INPLACE() then. Fixes: cdd9205dfffa3aaed935446a41f0d2dd1357c268 Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/qemu/qemu_namespace.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c index 311c66d46e..9fed6871e8 100644 --- a/src/qemu/qemu_namespace.c +++ b/src/qemu/qemu_namespace.c @@ -159,7 +159,8 @@ qemuDomainGetPreservedMounts(virQEMUDriverConfig *cfg, =20 if (c && (*c =3D=3D '/' || *c =3D=3D '\0')) { VIR_DEBUG("Dropping path %s because of %s", mounts[j], mou= nts[i]); - VIR_DELETE_ELEMENT(mounts, j, nmounts); + VIR_FREE(mounts[j]); + VIR_DELETE_ELEMENT_INPLACE(mounts, j, nmounts); } else { j++; } --=20 2.37.4 From nobody Tue May 7 11:36:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 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=1667231644; cv=none; d=zohomail.com; s=zohoarc; b=bEp7rrT2/KqG170jgT3rAabrUfL7P951/COIJ0AuKuF5HQk/qB/XeyA2x0QLhpgJe7+hDOVNcewSG7GC3zBsTtFdmMSIlbcMQB89zv1cZSuTnXyx6tgn+V8ygrTjIybG0kjNvvzgcyjK3SvqxZgqu6Drmu0sqwsomoUoiPO2qUM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1667231644; 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=WhatyQ55+iDEdD+Sr3CAQ/8BF7QeKe7crZLNnonWTIE=; b=dCpHTKo0NhE+QeQnj6OlvDUez0k9zHD9Z9ikptRhaVhwWja5VXVsgVvqplyYuq/xqfAKASbiZCA3nssNUC2Nb4cNvP1+fRSAd+cy4a18haNenD8HRM3iKsTWHUuVz2MGTnqLGprjlk2wrXyM0lr4GqxCxpQmQfAFXC0YNznbHOI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1667231644981684.6936207133725; Mon, 31 Oct 2022 08:54:04 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-478-UPCp8ApVP46bM8m6vCoMCQ-1; Mon, 31 Oct 2022 11:53:14 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 35E0C101E9B7; Mon, 31 Oct 2022 15:53:12 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B0C740C845E; Mon, 31 Oct 2022 15:53:12 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id D229A1946A4F; Mon, 31 Oct 2022 15:53:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id A270C194658C for ; Mon, 31 Oct 2022 15:53:03 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id CC15440C94CF; Mon, 31 Oct 2022 15:53:03 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7134740C6EC6 for ; Mon, 31 Oct 2022 15:53:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667231643; 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=WhatyQ55+iDEdD+Sr3CAQ/8BF7QeKe7crZLNnonWTIE=; b=MsyHSLd61teDf4t24dzzH9YC7d52rUxfvhkRnuCfxIQRXNvd3mZ0SpDYTVdwFoqllF0bt+ dvet+fqOvVrfFKVHsE0RzIkVlveDT1PjsL2HW6KC0pJX8mqorfTkzhaxeh3cG2sn7o/sAv ayjsZKE875QeJnRMd6rObtNjzchHHac= X-MC-Unique: UPCp8ApVP46bM8m6vCoMCQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/2] qemu_namespace: Make qemuDomainGetPreservedMounts() more robust wrt running VMs Date: Mon, 31 Oct 2022 16:53:00 +0100 Message-Id: <229163002645cf7b30f3ebeb7378031aa7d3a02e.1667231427.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1667231646627100001 Content-Type: text/plain; charset="utf-8"; x-default="true" The aim of qemuDomainGetPreservedMounts() is to get a list of filesystems mounted under /dev and optionally generate a path for each one where they are moved temporarily when building the namespace. And if given domain is also running it looks into its mount table rather than at the host one. But if it did look at the domain's private mount table, it find /dev mounted twice: the first time by udev, the second time the tmpfs mounted by us. Now, later in the function there's a "sorting" algorithm that tries to reduce number of mount points needing preservation, by identifying nested mount points. And if we keep the second occurrence of /dev on the list, well, after the "sorting" we are left with nothing but "/dev" because all other mount points are nested. Fixes: 46b03819ae8d833b11c2aaccb2c2a0361727f51b Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/qemu/qemu_namespace.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c index 9fed6871e8..8189cc37ba 100644 --- a/src/qemu/qemu_namespace.c +++ b/src/qemu/qemu_namespace.c @@ -154,6 +154,17 @@ qemuDomainGetPreservedMounts(virQEMUDriverConfig *cfg, for (i =3D 1; i < nmounts; i++) { size_t j =3D i + 1; =20 + /* If we looked into mount table of already running VM, + * we might have found /dev twice. Remove the other + * occurrence as it would jeopardize the rest of the prune + * algorithm. + */ + if (STREQ(mounts[i], "/dev")) { + VIR_FREE(mounts[i]); + VIR_DELETE_ELEMENT_INPLACE(mounts, i, nmounts); + continue; + } + while (j < nmounts) { char *c =3D STRSKIP(mounts[j], mounts[i]); =20 --=20 2.37.4