diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 2a45172071..37539c262d 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -14,6 +14,7 @@ */ #include #include +#include #include #include #include @@ -362,6 +363,7 @@ SELinuxRestoreSecurityFileLabel(const char *path) { struct stat buf; security_context_t fcon = NULL; + struct selabel_handle *handle = NULL; int rc = -1; char *newpath = NULL; char ebuf[1024]; @@ -380,14 +382,16 @@ SELinuxRestoreSecurityFileLabel(const char *path) goto err; } - if (matchpathcon(newpath, buf.st_mode, &fcon) == 0) { - rc = SELinuxSetFilecon(newpath, fcon); + if ((handle = selabel_open(SELABEL_CTX_FILE, NULL, 0)) == NULL || + selabel_lookup(handle, &fcon, newpath, buf.st_mode) < 0) { + VIR_WARN("cannot lookup default selinux label for %s", newpath); } else { - VIR_WARN("cannot lookup default selinux label for %s", - newpath); + rc = SELinuxSetFilecon(newpath, fcon); } err: + if (handle) + selabel_close(handle); freecon(fcon); VIR_FREE(newpath); return rc;