From nobody Tue Apr 30 12:48:15 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=fail(p=none dis=none) header.from=canonical.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 151991600081295.20190517788171; Thu, 1 Mar 2018 06:53:20 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 198F05D5ED; Thu, 1 Mar 2018 14:53:19 +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 E53796018B; Thu, 1 Mar 2018 14:53:18 +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 996FA1841C44; Thu, 1 Mar 2018 14:53:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w21ErHlB010850 for ; Thu, 1 Mar 2018 09:53:17 -0500 Received: by smtp.corp.redhat.com (Postfix) id 49E592D322; Thu, 1 Mar 2018 14:53:17 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 413CB608F4 for ; Thu, 1 Mar 2018 14:53:15 +0000 (UTC) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A1AC428217 for ; Thu, 1 Mar 2018 14:53:13 +0000 (UTC) Received: from 1.general.paelzer.uk.vpn ([10.172.196.172] helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1erPa4-0006r9-14; Thu, 01 Mar 2018 14:53:12 +0000 From: Christian Ehrhardt To: libvir-list@redhat.com, =?UTF-8?q?Guido=20G=C3=BCnther?= , Jamie Strandboge Date: Thu, 1 Mar 2018 15:53:03 +0100 Message-Id: <1519915983-29120-1-git-send-email-christian.ehrhardt@canonical.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 01 Mar 2018 14:53:13 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 01 Mar 2018 14:53:13 +0000 (UTC) for IP:'91.189.89.112' DOMAIN:'youngberry.canonical.com' HELO:'youngberry.canonical.com' FROM:'christian.ehrhardt@canonical.com' RCPT:'' X-RedHat-Spam-Score: -5.01 (RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD) 91.189.89.112 youngberry.canonical.com 91.189.89.112 youngberry.canonical.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Christian Ehrhardt Subject: [libvirt] [PATCH] virt-aa-helper: resolve yet to be created 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: , MIME-Version: 1.0 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 01 Mar 2018 14:53:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" In certain cases a xml contains paths that do not yet exist, but are valid as qemu will create them later on - for example vhostuser mode=3Dserver sockets. In any such cases so far the check to virFileExists failed and due to that the paths stayed non-resolved in regard to symlinks. But for apparmor those non-resolved rules are non functional as they are evaluated after resolving any symlinks. Therefore for non-existent files and partially non-existent paths resolve as much as possible to get valid rules. Example: Got rendered as: "/var/run/symlinknet" rw, But correct with "/var/run" being a symlink to "/run" is: "/run/symlinknet" rw, Signed-off-by: Christian Ehrhardt --- src/security/virt-aa-helper.c | 45 ++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index ff0068c..91bc339 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -41,6 +41,7 @@ #include "viralloc.h" #include "vircommand.h" #include "virlog.h" +#include "dirname.h" #include "driver.h" =20 #include "security_driver.h" @@ -752,6 +753,9 @@ vah_add_path(virBufferPtr buf, const char *path, const = char *perms, bool recursi bool explicit_deny_rule =3D true; char *sub =3D NULL; char *perms_new =3D NULL; + char *pathdir =3D NULL; + char *pathtmp =3D NULL; + char *pathreal =3D NULL; =20 if (path =3D=3D NULL) return rc; @@ -766,14 +770,38 @@ vah_add_path(virBufferPtr buf, const char *path, cons= t char *perms, bool recursi return 0; } =20 - if (virFileExists(path)) { - if ((tmp =3D realpath(path, NULL)) =3D=3D NULL) { - vah_error(NULL, 0, path); - vah_error(NULL, 0, _("could not find realpath for disk")); - return rc; + /* files might be created by qemu later on and not exist right now. + * But realpath needs a valid path to work on, therefore: + * 1. walk the path to find longest valid path + * 2. get the realpath of that valid path + * 3. re-combine the realpath with the remaining suffix + * Note: A totally non existent path is used as-is + */ + if ((pathdir =3D mdir_name(path)) =3D=3D NULL) + goto cleanup; + while (!virFileExists(pathdir)) { + if (VIR_STRDUP_QUIET(pathtmp, pathdir) < 0) + goto cleanup; + VIR_FREE(pathdir); + if ((pathdir =3D mdir_name(pathtmp)) =3D=3D NULL) + goto cleanup; + VIR_FREE(pathtmp); + } + + if (strlen(pathdir) =3D=3D 1) { + /* nothing of the path does exist yet */ + if (VIR_STRDUP_QUIET(tmp, path) < 0) + goto cleanup; + } else { + if (VIR_STRDUP_QUIET(pathtmp, path+strlen(pathdir)) < 0) + goto cleanup; + if ((pathreal =3D realpath(pathdir, NULL)) =3D=3D NULL) { + vah_error(NULL, 0, pathdir); + vah_error(NULL, 0, _("could not find realpath")); + goto cleanup; } - } else if (VIR_STRDUP_QUIET(tmp, path) < 0) { - return rc; + if (virAsprintfQuiet(&tmp, "%s%s", pathreal, pathtmp) < 0) + goto cleanup; } =20 if (VIR_STRDUP_QUIET(perms_new, perms) < 0) @@ -814,6 +842,9 @@ vah_add_path(virBufferPtr buf, const char *path, const = char *perms, bool recursi } =20 cleanup: + VIR_FREE(pathdir); + VIR_FREE(pathtmp); + VIR_FREE(pathreal); VIR_FREE(perms_new); VIR_FREE(tmp); =20 --=20 2.7.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list