From nobody Fri May 17 07:08:14 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=quarantine dis=quarantine) header.from=suse.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1712009782304113.93302026818435; Mon, 1 Apr 2024 15:16:22 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C72941B9A; Mon, 1 Apr 2024 18:16:20 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id D8F8B1B1B; Mon, 1 Apr 2024 18:15:42 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C848B1AB6; Mon, 1 Apr 2024 18:15:39 -0400 (EDT) Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 038501ABA for ; Mon, 1 Apr 2024 18:15:38 -0400 (EDT) Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2d700beb60bso78095451fa.1 for ; Mon, 01 Apr 2024 15:15:38 -0700 (PDT) Received: from localhost (75-169-5-227.slkc.qwest.net. [75.169.5.227]) by smtp.gmail.com with ESMTPSA id s15-20020a92cc0f000000b003684fbf8ba6sm2998116ilp.44.2024.04.01.15.15.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 15:15:36 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712009737; x=1712614537; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kqRIIvuOBE5WZvq0GQRw50VSrjoXNIjpbrPVV96DWGU=; b=Fys343kAw+XLOziW80hqCaVXaKMtsC+CpH5WStJ1pvGT9tg04A0TweCcPK49oerCZj fPLK/8sp4KlpBpBphD4olx9Y89T2BiQ4nr6vSzoqyoJshL/+MNYOWeJRyzzGNCFmdQFa Gtk9niy3GPDwlRFoX4sGhZEZ+Y/Tx7Z7j1rVumRkgZGiPLctqzu9vxzr4++JXD46ZILE 3Ftstuj2mc8MmY2/YmpWXB1rFVbv4DGwnfTzrnEG9lkBmLchKfZx56+QxrDCmLF6sJui A1AMGLZYQHahmGOzTvuXvLqivwdWRuLdUiu3afJw5oXKD9EQ/IgbVy4Cy/1+A/H2BxsX icSA== X-Gm-Message-State: AOJu0YzKbnfDIUAgFWqEUZPh58ztoYy4HdkTa3YesYYzrQOJtseO6hK4 gFY2XsZWxVHZOKg6PvWH08NLxv2fgd+qImSRFWJC9Xuue0AtjKf1NMF8mMtcbBcMeDyBd0OhONQ s X-Google-Smtp-Source: AGHT+IEaU8Z1rMyEP7zIKz8ASUINui4wjHVbTKktUjTwAmyfWkf55oZoXk8jmbWPsV9SWhOUMfU5Fw== X-Received: by 2002:a2e:b0f3:0:b0:2d4:676b:f591 with SMTP id h19-20020a2eb0f3000000b002d4676bf591mr7418071ljl.45.1712009737386; Mon, 01 Apr 2024 15:15:37 -0700 (PDT) From: Jim Fehlig To: devel@lists.libvirt.org Subject: [PATCH V2] security: Ensure file exists before attempting to restore label Date: Mon, 1 Apr 2024 16:14:59 -0600 Message-ID: <20240401221535.5510-1-jfehlig@suse.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Message-ID-Hash: MSB53HQTNUEK5RS5EPQB6TF7AAEQZVKA X-Message-ID-Hash: MSB53HQTNUEK5RS5EPQB6TF7AAEQZVKA X-MailFrom: jfehlig@suse.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1712009783762100001 When performing an install, it's common for tooling such as virt-install to remove the install kernel/initrd once they are successfully booted and the domain has been redefined to boot without them. After the installation is complete and the domain is rebooted/shutdown, the DAC and selinux security drivers attempt to restore labels on the now deleted files. It's harmles wrt functionality, but results in error messages such as Mar 08 12:40:37 virtqemud[5639]: internal error: child reported (status=3D1= 25): unable to stat: /var/lib/libvirt/boot/vir> Mar 08 12:40:37 virtqemud[5639]: unable to stat: /var/lib/libvirt/boot/virt= inst-yvp19moo-linux: No such file or directo> Mar 08 12:40:37 virtqemud[5639]: Unable to run security manager transaction Add a check for file existence to the virSecurity*RestoreFileLabel function= s, and avoid relabeling if the file is no longer available. Skipping the resto= re caused failures in qemusecuritytest, which mocks stat, chown, etc as part of ensuring the security drivers properly restore labels. virFileExists is now mocked in qemusecuritymock.c to return true when passed a file previously seen by the mocked stat, chown, etc functions. Signed-off-by: Jim Fehlig Reviewed-by: Michal Privoznik --- src/security/security_dac.c | 3 +++ src/security/security_selinux.c | 2 ++ tests/qemusecuritymock.c | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/security/security_dac.c b/src/security/security_dac.c index 567be4bd23..4e850e219e 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -825,6 +825,9 @@ virSecurityDACRestoreFileLabelInternal(virSecurityManag= er *mgr, virStorageSourceIsLocalStorage(src)) path =3D src->path; =20 + if (!virFileExists(path)) + return 0; + /* Be aware that this function might run in a separate process. * Therefore, any driver state changes would be thrown away. */ =20 diff --git a/src/security/security_selinux.c b/src/security/security_selinu= x.c index b49af26e49..aaec34ff8b 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -1488,6 +1488,8 @@ virSecuritySELinuxRestoreFileLabel(virSecurityManager= *mgr, */ if (!path) return 0; + if (!virFileExists(path)) + return 0; =20 VIR_INFO("Restoring SELinux context on '%s'", path); =20 diff --git a/tests/qemusecuritymock.c b/tests/qemusecuritymock.c index 80d59536b1..bb0a85544b 100644 --- a/tests/qemusecuritymock.c +++ b/tests/qemusecuritymock.c @@ -382,6 +382,24 @@ int virFileUnlock(int fd G_GNUC_UNUSED, } =20 =20 +bool virFileExists(const char *path G_GNUC_UNUSED) +{ + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); + + if (getenv(ENVVAR) =3D=3D NULL) + return access(path, F_OK) =3D=3D 0; + + init_hash(); + if (virHashHasEntry(chown_paths, path)) + return true; + + if (virHashHasEntry(selinux_paths, path)) + return true; + + return false; +} + + typedef struct _checkOwnerData checkOwnerData; struct _checkOwnerData { GHashTable *paths; --=20 2.44.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org