2012-09-19 14:00:34 +01:00
|
|
|
/*
|
2014-03-19 11:11:16 -06:00
|
|
|
* Copyright (C) 2011-2014 Red Hat, Inc.
|
2012-09-19 14:00:34 +01:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* License along with this library; If not, see
|
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
|
#include <selinux/context.h>
|
2018-08-03 16:35:53 +02:00
|
|
|
#include <sys/xattr.h>
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
#include "testutils.h"
|
|
|
|
|
#include "testutilsqemu.h"
|
|
|
|
|
#include "qemu/qemu_domain.h"
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
|
#include "virerror.h"
|
|
|
|
|
#include "virfile.h"
|
|
|
|
|
#include "virlog.h"
|
|
|
|
|
#include "security/security_manager.h"
|
2013-04-03 12:36:23 +02:00
|
|
|
#include "virstring.h"
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("tests.securityselinuxlabeltest");
|
|
|
|
|
|
2015-09-22 16:27:57 +02:00
|
|
|
static virQEMUDriver driver;
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
static virSecurityManagerPtr mgr;
|
|
|
|
|
|
|
|
|
|
typedef struct testSELinuxFile testSELinuxFile;
|
|
|
|
|
|
|
|
|
|
struct testSELinuxFile {
|
|
|
|
|
char *file;
|
|
|
|
|
char *context;
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-09 19:36:07 +08:00
|
|
|
static int
|
|
|
|
|
testUserXattrEnabled(void)
|
|
|
|
|
{
|
|
|
|
|
int ret = -1;
|
|
|
|
|
ssize_t len;
|
|
|
|
|
const char *con_value = "system_u:object_r:svirt_image_t:s0:c41,c264";
|
|
|
|
|
char *path = NULL;
|
2019-10-22 15:26:14 +02:00
|
|
|
path = g_strdup_printf("%s/securityselinuxlabeldata/testxattr", abs_builddir);
|
2014-06-09 19:36:07 +08:00
|
|
|
|
2014-07-01 14:52:28 +02:00
|
|
|
if (virFileMakePath(abs_builddir "/securityselinuxlabeldata") < 0 ||
|
|
|
|
|
virFileTouch(path, 0600) < 0)
|
2014-06-09 19:36:07 +08:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
len = setxattr(path, "user.libvirt.selinux", con_value,
|
|
|
|
|
strlen(con_value), 0);
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
if (errno == EOPNOTSUPP)
|
|
|
|
|
ret = 0;
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
unlink(path);
|
2014-07-01 14:52:28 +02:00
|
|
|
rmdir(abs_builddir "/securityselinuxlabeldata");
|
2014-06-09 19:36:07 +08:00
|
|
|
VIR_FREE(path);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxMungePath(char **path)
|
|
|
|
|
{
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
2019-10-22 15:26:14 +02:00
|
|
|
tmp = g_strdup_printf("%s/securityselinuxlabeldata%s", abs_builddir, *path);
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
VIR_FREE(*path);
|
|
|
|
|
*path = tmp;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxLoadFileList(const char *testname,
|
|
|
|
|
testSELinuxFile **files,
|
|
|
|
|
size_t *nfiles)
|
|
|
|
|
{
|
|
|
|
|
int ret = -1;
|
|
|
|
|
char *path = NULL;
|
|
|
|
|
FILE *fp = NULL;
|
2013-07-03 09:14:33 -04:00
|
|
|
char *line = NULL;
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
*files = NULL;
|
|
|
|
|
*nfiles = 0;
|
|
|
|
|
|
2019-10-22 15:26:14 +02:00
|
|
|
path = g_strdup_printf("%s/securityselinuxlabeldata/%s.txt", abs_srcdir,
|
|
|
|
|
testname);
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2014-11-13 15:20:43 +01:00
|
|
|
if (!(fp = fopen(path, "r")))
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
|
2020-09-23 01:04:17 +02:00
|
|
|
line = g_new0(char, 1024);
|
2013-07-03 09:14:33 -04:00
|
|
|
|
2012-09-19 14:00:34 +01:00
|
|
|
while (!feof(fp)) {
|
2014-01-13 16:48:00 +01:00
|
|
|
char *file = NULL, *context = NULL, *tmp;
|
2012-09-19 14:00:34 +01:00
|
|
|
if (!fgets(line, 1024, fp)) {
|
|
|
|
|
if (!feof(fp))
|
|
|
|
|
goto cleanup;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 09:14:33 -04:00
|
|
|
tmp = strchr(line, ';');
|
|
|
|
|
if (!tmp) {
|
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
|
"unexpected format for line '%s'",
|
|
|
|
|
line);
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
2012-09-19 14:00:34 +01:00
|
|
|
*tmp = '\0';
|
|
|
|
|
tmp++;
|
|
|
|
|
|
2019-10-22 15:26:14 +02:00
|
|
|
file = g_strdup_printf("%s/securityselinuxlabeldata%s", abs_builddir,
|
|
|
|
|
line);
|
2012-09-19 14:00:34 +01:00
|
|
|
if (*tmp != '\0' && *tmp != '\n') {
|
2019-10-20 13:49:46 +02:00
|
|
|
context = g_strdup(tmp);
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
tmp = strchr(context, '\n');
|
2013-07-03 09:14:33 -04:00
|
|
|
if (tmp)
|
|
|
|
|
*tmp = '\0';
|
2012-09-19 14:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:48:00 +01:00
|
|
|
if (VIR_EXPAND_N(*files, *nfiles, 1) < 0) {
|
|
|
|
|
VIR_FREE(file);
|
|
|
|
|
VIR_FREE(context);
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
2014-01-13 16:48:00 +01:00
|
|
|
}
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
(*files)[(*nfiles)-1].file = file;
|
|
|
|
|
(*files)[(*nfiles)-1].context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
2014-03-25 07:53:44 +01:00
|
|
|
cleanup:
|
2012-09-19 14:00:34 +01:00
|
|
|
VIR_FORCE_FCLOSE(fp);
|
|
|
|
|
VIR_FREE(path);
|
2013-07-03 09:14:33 -04:00
|
|
|
VIR_FREE(line);
|
2012-09-19 14:00:34 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static virDomainDefPtr
|
|
|
|
|
testSELinuxLoadDef(const char *testname)
|
|
|
|
|
{
|
|
|
|
|
char *xmlfile = NULL;
|
|
|
|
|
virDomainDefPtr def = NULL;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
2019-10-22 15:26:14 +02:00
|
|
|
xmlfile = g_strdup_printf("%s/securityselinuxlabeldata/%s.xml", abs_srcdir,
|
|
|
|
|
testname);
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2019-11-27 12:29:21 +00:00
|
|
|
if (!(def = virDomainDefParseFile(xmlfile, driver.xmlopt,
|
2016-12-19 23:35:02 +01:00
|
|
|
NULL, 0)))
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < def->ndisks; i++) {
|
2014-05-21 17:13:12 -06:00
|
|
|
if (def->disks[i]->src->type != VIR_STORAGE_TYPE_FILE &&
|
|
|
|
|
def->disks[i]->src->type != VIR_STORAGE_TYPE_BLOCK)
|
2012-09-19 14:00:34 +01:00
|
|
|
continue;
|
|
|
|
|
|
2014-05-21 17:13:12 -06:00
|
|
|
if (testSELinuxMungePath(&def->disks[i]->src->path) < 0)
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < def->nserials; i++) {
|
2016-10-21 07:45:54 -04:00
|
|
|
if (def->serials[i]->source->type != VIR_DOMAIN_CHR_TYPE_FILE &&
|
|
|
|
|
def->serials[i]->source->type != VIR_DOMAIN_CHR_TYPE_PIPE &&
|
|
|
|
|
def->serials[i]->source->type != VIR_DOMAIN_CHR_TYPE_DEV &&
|
|
|
|
|
def->serials[i]->source->type != VIR_DOMAIN_CHR_TYPE_UNIX)
|
2012-09-19 14:00:34 +01:00
|
|
|
continue;
|
|
|
|
|
|
2016-10-21 07:45:54 -04:00
|
|
|
if (def->serials[i]->source->type == VIR_DOMAIN_CHR_TYPE_UNIX) {
|
|
|
|
|
if (testSELinuxMungePath(&def->serials[i]->source->data.nix.path) < 0)
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
} else {
|
2016-10-21 07:45:54 -04:00
|
|
|
if (testSELinuxMungePath(&def->serials[i]->source->data.file.path) < 0)
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (def->os.kernel &&
|
|
|
|
|
testSELinuxMungePath(&def->os.kernel) < 0)
|
|
|
|
|
goto cleanup;
|
|
|
|
|
if (def->os.initrd &&
|
|
|
|
|
testSELinuxMungePath(&def->os.initrd) < 0)
|
|
|
|
|
goto cleanup;
|
|
|
|
|
|
2014-03-25 07:53:44 +01:00
|
|
|
cleanup:
|
2012-09-19 14:00:34 +01:00
|
|
|
VIR_FREE(xmlfile);
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxCreateDisks(testSELinuxFile *files, size_t nfiles)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
2013-08-13 14:19:14 -06:00
|
|
|
if (virFileMakePath(abs_builddir "/securityselinuxlabeldata/nfs") < 0)
|
2012-09-19 14:00:34 +01:00
|
|
|
return -1;
|
|
|
|
|
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < nfiles; i++) {
|
2012-09-19 14:00:34 +01:00
|
|
|
if (virFileTouch(files[i].file, 0600) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxDeleteDisks(testSELinuxFile *files, size_t nfiles)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < nfiles; i++) {
|
2012-09-19 14:00:34 +01:00
|
|
|
if (unlink(files[i].file) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2013-08-13 14:19:14 -06:00
|
|
|
if (rmdir(abs_builddir "/securityselinuxlabeldata/nfs") < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
/* Ignore failure to remove non-empty directory with in-tree build */
|
|
|
|
|
rmdir(abs_builddir "/securityselinuxlabeldata");
|
2012-09-19 14:00:34 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxCheckLabels(testSELinuxFile *files, size_t nfiles)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
2020-07-15 12:32:48 +02:00
|
|
|
char *ctx;
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < nfiles; i++) {
|
2013-08-13 14:19:14 -06:00
|
|
|
ctx = NULL;
|
2012-09-19 14:00:34 +01:00
|
|
|
if (getfilecon(files[i].file, &ctx) < 0) {
|
|
|
|
|
if (errno == ENODATA) {
|
2013-08-13 14:19:14 -06:00
|
|
|
/* nothing to do */
|
|
|
|
|
} else if (errno == EOPNOTSUPP) {
|
2019-10-20 13:49:46 +02:00
|
|
|
ctx = g_strdup("EOPNOTSUPP");
|
2012-09-19 14:00:34 +01:00
|
|
|
} else {
|
|
|
|
|
virReportSystemError(errno,
|
|
|
|
|
"Cannot read label on %s",
|
|
|
|
|
files[i].file);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-20 21:45:12 +05:30
|
|
|
if (STRNEQ_NULLABLE(files[i].context, ctx)) {
|
2012-09-19 14:00:34 +01:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2018-12-04 19:08:14 +02:00
|
|
|
"File %s context '%s' did not match expected '%s'",
|
2012-09-19 14:00:34 +01:00
|
|
|
files[i].file, ctx, files[i].context);
|
2013-08-13 14:19:14 -06:00
|
|
|
VIR_FREE(ctx);
|
2012-09-19 14:00:34 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-08-13 14:19:14 -06:00
|
|
|
VIR_FREE(ctx);
|
2012-09-19 14:00:34 +01:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
testSELinuxLabeling(const void *opaque)
|
|
|
|
|
{
|
|
|
|
|
const char *testname = opaque;
|
|
|
|
|
int ret = -1;
|
|
|
|
|
testSELinuxFile *files = NULL;
|
|
|
|
|
size_t nfiles = 0;
|
|
|
|
|
size_t i;
|
|
|
|
|
virDomainDefPtr def = NULL;
|
|
|
|
|
|
|
|
|
|
if (testSELinuxLoadFileList(testname, &files, &nfiles) < 0)
|
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
if (testSELinuxCreateDisks(files, nfiles) < 0)
|
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
if (!(def = testSELinuxLoadDef(testname)))
|
|
|
|
|
goto cleanup;
|
|
|
|
|
|
2019-09-11 07:53:09 +02:00
|
|
|
if (virSecurityManagerSetAllLabel(mgr, def, NULL, false, false) < 0)
|
2012-09-19 14:00:34 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
if (testSELinuxCheckLabels(files, nfiles) < 0)
|
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
2014-03-25 07:53:44 +01:00
|
|
|
cleanup:
|
2012-09-19 14:00:34 +01:00
|
|
|
if (testSELinuxDeleteDisks(files, nfiles) < 0)
|
2013-08-13 14:19:14 -06:00
|
|
|
VIR_WARN("unable to fully clean up");
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
virDomainDefFree(def);
|
2013-05-21 15:53:48 +08:00
|
|
|
for (i = 0; i < nfiles; i++) {
|
2012-09-19 14:00:34 +01:00
|
|
|
VIR_FREE(files[i].file);
|
|
|
|
|
VIR_FREE(files[i].context);
|
|
|
|
|
}
|
|
|
|
|
VIR_FREE(files);
|
2016-10-10 06:30:28 -04:00
|
|
|
if (ret < 0)
|
2019-05-03 10:45:58 +02:00
|
|
|
VIR_TEST_VERBOSE("%s", virGetLastErrorMessage());
|
2012-09-19 14:00:34 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
mymain(void)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2014-06-09 19:36:07 +08:00
|
|
|
int rc = testUserXattrEnabled();
|
2019-10-18 10:30:11 -05:00
|
|
|
g_autoptr(virQEMUCaps) qemuCaps = NULL;
|
2014-06-09 19:36:07 +08:00
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
if (!rc)
|
|
|
|
|
return EXIT_AM_SKIP;
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2018-10-02 15:08:28 +02:00
|
|
|
if (!(mgr = virSecurityManagerNew("selinux", "QEMU",
|
2015-10-06 17:01:48 +02:00
|
|
|
VIR_SECURITY_MANAGER_DEFAULT_CONFINED |
|
|
|
|
|
VIR_SECURITY_MANAGER_PRIVILEGED))) {
|
2019-05-03 10:45:58 +02:00
|
|
|
VIR_TEST_VERBOSE("Unable to initialize security driver: %s",
|
2016-08-04 09:20:31 +02:00
|
|
|
virGetLastErrorMessage());
|
2013-02-22 15:42:39 -07:00
|
|
|
return EXIT_FAILURE;
|
2012-09-19 14:00:34 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-22 16:27:57 +02:00
|
|
|
if (qemuTestDriverInit(&driver) < 0)
|
2013-03-05 16:17:24 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
2019-10-18 10:30:11 -05:00
|
|
|
if (!(qemuCaps = virQEMUCapsNew()))
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
|
|
virQEMUCapsSet(qemuCaps, QEMU_CAPS_DEVICE_CIRRUS_VGA);
|
2019-12-17 10:02:10 +01:00
|
|
|
virQEMUCapsSet(qemuCaps, QEMU_CAPS_VNC);
|
2019-10-18 10:30:11 -05:00
|
|
|
|
|
|
|
|
if (qemuTestCapsCacheInsert(driver.qemuCapsCache, qemuCaps) < 0)
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
2017-11-03 13:09:47 +01:00
|
|
|
#define DO_TEST_LABELING(name) \
|
|
|
|
|
if (virTestRun("Labelling " # name, testSELinuxLabeling, name) < 0) \
|
2013-09-20 19:13:35 +01:00
|
|
|
ret = -1;
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2020-07-15 12:32:48 +02:00
|
|
|
setcon("system_r:system_u:libvirtd_t:s0:c0.c1023");
|
2012-09-19 14:00:34 +01:00
|
|
|
|
|
|
|
|
DO_TEST_LABELING("disks");
|
|
|
|
|
DO_TEST_LABELING("kernel");
|
|
|
|
|
DO_TEST_LABELING("chardev");
|
2013-08-13 14:19:14 -06:00
|
|
|
DO_TEST_LABELING("nfs");
|
2012-09-19 14:00:34 +01:00
|
|
|
|
2015-09-22 16:27:57 +02:00
|
|
|
qemuTestDriverFree(&driver);
|
|
|
|
|
|
2012-09-19 14:00:34 +01:00
|
|
|
return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 11:56:46 +01:00
|
|
|
VIR_TEST_MAIN_PRELOAD(mymain,
|
|
|
|
|
VIR_TEST_MOCK("domaincaps"),
|
2020-05-28 02:40:50 +02:00
|
|
|
abs_builddir "/libsecurityselinuxhelper.so")
|