2012-01-20 17:49:32 +00:00
|
|
|
/*
|
2014-03-17 09:38:38 +00:00
|
|
|
* Copyright (C) 2013, 2014 Red Hat, Inc.
|
2012-01-20 17:49:32 +00: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, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
2014-03-06 06:02:49 +00:00
|
|
|
#if WITH_SELINUX
|
|
|
|
# include <selinux/selinux.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 17:49:32 +00:00
|
|
|
#include "testutils.h"
|
|
|
|
|
|
|
|
#include "viridentity.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
|
|
|
|
#include "virlockspace.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("tests.identitytest");
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
static int testIdentityAttrs(const void *data G_GNUC_UNUSED)
|
2012-01-20 17:49:32 +00:00
|
|
|
{
|
2019-09-19 14:38:03 +00:00
|
|
|
g_autoptr(virIdentity) ident = virIdentityNew();
|
2012-01-20 17:49:32 +00:00
|
|
|
const char *val;
|
2019-08-07 15:30:57 +00:00
|
|
|
int rc;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-07-26 11:21:29 +00:00
|
|
|
if (virIdentitySetUserName(ident, "fred") < 0)
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if ((rc = virIdentityGetUserName(ident, &val)) < 0)
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
|
2012-01-20 17:49:32 +00:00
|
|
|
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if ((rc = virIdentityGetGroupName(ident, &val)) < 0)
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (val != NULL || rc != 0) {
|
2012-01-20 17:49:32 +00:00
|
|
|
VIR_DEBUG("Unexpected groupname attribute");
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:21:29 +00:00
|
|
|
if (virIdentitySetUserName(ident, "joe") >= 0) {
|
2012-01-20 17:49:32 +00:00
|
|
|
VIR_DEBUG("Unexpectedly overwrote attribute");
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if ((rc = virIdentityGetUserName(ident, &val)) < 0)
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
|
2012-01-20 17:49:32 +00:00
|
|
|
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 16:38:12 +00:00
|
|
|
return 0;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 06:02:49 +00:00
|
|
|
static int testIdentityGetSystem(const void *data)
|
|
|
|
{
|
|
|
|
const char *context = data;
|
2019-10-01 16:38:12 +00:00
|
|
|
g_autoptr(virIdentity) ident = NULL;
|
2014-03-06 06:02:49 +00:00
|
|
|
const char *val;
|
2019-08-07 15:30:57 +00:00
|
|
|
int rc;
|
2014-03-06 06:02:49 +00:00
|
|
|
|
|
|
|
#if !WITH_SELINUX
|
|
|
|
if (context) {
|
|
|
|
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
|
|
|
|
ret = EXIT_AM_SKIP;
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2014-03-06 06:02:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!(ident = virIdentityGetSystem())) {
|
|
|
|
VIR_DEBUG("Unable to get system identity");
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2014-03-06 06:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if ((rc = virIdentityGetSELinuxContext(ident, &val)) < 0)
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2014-03-06 06:02:49 +00:00
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (context == NULL) {
|
|
|
|
if (val != NULL || rc != 0) {
|
|
|
|
VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val));
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2019-08-07 15:30:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (STRNEQ_NULLABLE(val, context) || rc != 1) {
|
|
|
|
VIR_DEBUG("Want SELinux context '%s' got '%s'",
|
|
|
|
context, val);
|
2019-10-01 16:38:12 +00:00
|
|
|
return -1;
|
2019-08-07 15:30:57 +00:00
|
|
|
}
|
2014-03-06 06:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 16:38:12 +00:00
|
|
|
return 0;
|
2014-03-06 06:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
static int testSetFakeSELinuxContext(const void *data G_GNUC_UNUSED)
|
2014-03-06 06:02:49 +00:00
|
|
|
{
|
|
|
|
#if WITH_SELINUX
|
2020-07-15 10:32:48 +00:00
|
|
|
return setcon_raw(data);
|
2014-03-06 06:02:49 +00:00
|
|
|
#else
|
|
|
|
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
|
|
|
|
return EXIT_AM_SKIP;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
static int testDisableFakeSELinux(const void *data G_GNUC_UNUSED)
|
2014-03-06 06:02:49 +00:00
|
|
|
{
|
|
|
|
#if WITH_SELINUX
|
|
|
|
return security_disable();
|
|
|
|
#else
|
|
|
|
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
|
|
|
|
return EXIT_AM_SKIP;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-20 17:49:32 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
2014-03-06 06:02:49 +00:00
|
|
|
const char *context = "unconfined_u:unconfined_r:unconfined_t:s0";
|
2012-01-20 17:49:32 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Identity attributes ", testIdentityAttrs, NULL) < 0)
|
2012-01-20 17:49:32 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Setting fake SELinux context ", testSetFakeSELinuxContext, context) < 0)
|
2014-03-06 06:02:49 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("System identity (fake SELinux enabled) ", testIdentityGetSystem, context) < 0)
|
2014-03-06 06:02:49 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Disabling fake SELinux ", testDisableFakeSELinux, NULL) < 0)
|
2014-03-06 06:02:49 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("System identity (fake SELinux disabled) ", testIdentityGetSystem, NULL) < 0)
|
2014-03-06 06:02:49 +00:00
|
|
|
ret = -1;
|
2012-01-20 17:49:32 +00:00
|
|
|
|
2014-03-17 09:38:38 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2012-01-20 17:49:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 06:02:49 +00:00
|
|
|
#if WITH_SELINUX
|
2020-05-28 00:40:50 +00:00
|
|
|
VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/libsecurityselinuxhelper.so")
|
2014-03-06 06:02:49 +00:00
|
|
|
#else
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|
2014-03-06 06:02:49 +00:00
|
|
|
#endif
|