security: Ensure file exists before attempting to restore label

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=125): unable to stat: /var/lib/libvirt/boot/vir>
Mar 08 12:40:37 virtqemud[5639]: unable to stat: /var/lib/libvirt/boot/virtinst-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 functions,
and avoid relabeling if the file is no longer available. Skipping the restore
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 <jfehlig@suse.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Jim Fehlig 2024-03-25 16:38:19 -06:00
parent fdd440c096
commit 4ed5ade753
3 changed files with 25 additions and 0 deletions

View File

@ -825,6 +825,9 @@ virSecurityDACRestoreFileLabelInternal(virSecurityManager *mgr,
virStorageSourceIsLocalStorage(src))
path = src->path;
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. */

View File

@ -1488,6 +1488,8 @@ virSecuritySELinuxRestoreFileLabel(virSecurityManager *mgr,
*/
if (!path)
return 0;
if (!virFileExists(path))
return 0;
VIR_INFO("Restoring SELinux context on '%s'", path);

View File

@ -66,6 +66,7 @@ static int (*real_close)(int fd);
static int (*real_setfilecon_raw)(const char *path, const char *context);
static int (*real_getfilecon_raw)(const char *path, char **context);
#endif
static bool (*real_virFileExists)(const char *file);
/* Global mutex to avoid races */
@ -123,6 +124,7 @@ init_syms(void)
VIR_MOCK_REAL_INIT(setfilecon_raw);
VIR_MOCK_REAL_INIT(getfilecon_raw);
#endif
VIR_MOCK_REAL_INIT(virFileExists);
/* Intentionally not calling init_hash() here */
}
@ -382,6 +384,24 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
}
bool virFileExists(const char *path)
{
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
if (getenv(ENVVAR) == NULL)
return real_virFileExists(path);
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;