2018-12-07 12:21:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
# include <selinux/selinux.h>
|
|
|
|
# include <selinux/label.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
#include "virmock.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virthread.h"
|
|
|
|
#include "virhash.h"
|
|
|
|
#include "qemusecuritytest.h"
|
|
|
|
#include "security/security_manager.h"
|
2019-08-08 08:17:45 +00:00
|
|
|
#include "virhostuptime.h"
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
/* Okay, here's the deal. The qemusecuritytest calls several
|
|
|
|
* virSecurityManager public APIs in order to check if XATTRs
|
|
|
|
* work as expected. Therefore there is a lot we have to mock
|
|
|
|
* (chown, stat, XATTR APIs, etc.). Since the test won't run as
|
|
|
|
* root chown() would fail, therefore we have to keep everything
|
2020-11-02 21:31:03 +00:00
|
|
|
* in memory. By default, all files are owned by 1:2 and have a
|
|
|
|
* SELinux label.
|
2018-12-07 12:21:43 +00:00
|
|
|
* By the way, since there are some cases where real stat needs
|
|
|
|
* to be called, the mocked functions are effective only if
|
|
|
|
* $ENVVAR is set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_UID 1
|
|
|
|
#define DEFAULT_GID 2
|
2020-11-02 21:31:03 +00:00
|
|
|
#define DEFAULT_SELINUX_LABEL "system_u:object_r:default_t:s0"
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int (*real_chown)(const char *path, uid_t uid, gid_t gid);
|
|
|
|
static int (*real_open)(const char *path, int flags, ...);
|
2024-03-13 16:25:35 +00:00
|
|
|
#if WITH___OPEN_2
|
|
|
|
static int (*real___open_2)(const char *path, int flags);
|
|
|
|
#endif
|
2018-12-07 12:21:43 +00:00
|
|
|
static int (*real_close)(int fd);
|
2020-11-02 21:31:03 +00:00
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
static int (*real_setfilecon_raw)(const char *path, const char *context);
|
|
|
|
static int (*real_getfilecon_raw)(const char *path, char **context);
|
|
|
|
#endif
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Global mutex to avoid races */
|
|
|
|
virMutex m = VIR_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
/* Hash table to store XATTRs for paths. For simplicity, key is
|
|
|
|
* "$path:$name" and value is just XATTR "$value". We don't need
|
|
|
|
* to list XATTRs a path has, therefore we don't need something
|
|
|
|
* more clever. */
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *xattr_paths = NULL;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* The UID:GID is stored in a hash table. Again, for simplicity,
|
|
|
|
* the path is the key and the value is an uint32_t , where
|
|
|
|
* the lower half is UID and the higher is GID. */
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *chown_paths = NULL;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
/* The SELinux label is stored in a hash table. For simplicity,
|
|
|
|
* the path is the key and the value is the label. */
|
2020-10-22 17:04:18 +00:00
|
|
|
GHashTable *selinux_paths = NULL;
|
2020-11-02 21:31:03 +00:00
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_hash(void)
|
|
|
|
{
|
|
|
|
/* The reason the init is split is that virHash calls
|
|
|
|
* virRandomBits() which in turn calls a gnutls function.
|
|
|
|
* However, when gnutls is initializing itself it calls
|
|
|
|
* stat() so we would call a gnutls function before it is
|
|
|
|
* initialized which will lead to a crash.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (xattr_paths)
|
|
|
|
return;
|
|
|
|
|
2021-07-05 13:42:46 +00:00
|
|
|
xattr_paths = virHashNew(g_free);
|
|
|
|
chown_paths = virHashNew(g_free);
|
|
|
|
selinux_paths = virHashNew(g_free);
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_syms(void)
|
|
|
|
{
|
|
|
|
if (real_chown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_MOCK_REAL_INIT(chown);
|
|
|
|
VIR_MOCK_REAL_INIT(open);
|
2024-03-13 16:25:35 +00:00
|
|
|
#if WITH___OPEN_2
|
|
|
|
VIR_MOCK_REAL_INIT(__open_2);
|
|
|
|
#endif
|
2018-12-07 12:21:43 +00:00
|
|
|
VIR_MOCK_REAL_INIT(close);
|
2020-11-02 21:31:03 +00:00
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
VIR_MOCK_REAL_INIT(setfilecon_raw);
|
|
|
|
VIR_MOCK_REAL_INIT(getfilecon_raw);
|
|
|
|
#endif
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
/* Intentionally not calling init_hash() here */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_key(const char *path,
|
|
|
|
const char *name)
|
|
|
|
{
|
2021-10-22 08:56:01 +00:00
|
|
|
return g_strdup_printf("%s:%s", path, name);
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-03-22 14:08:36 +00:00
|
|
|
virFileGetXAttrQuiet(const char *path,
|
|
|
|
const char *name,
|
|
|
|
char **value)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
2022-02-08 14:17:30 +00:00
|
|
|
g_autofree char *key = get_key(path, name);
|
2018-12-07 12:21:43 +00:00
|
|
|
char *val;
|
2022-02-08 14:17:30 +00:00
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
if (!(val = virHashLookup(xattr_paths, key))) {
|
|
|
|
errno = ENODATA;
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
*value = g_strdup(val);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2022-02-08 14:17:30 +00:00
|
|
|
return 0;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-05 17:14:52 +00:00
|
|
|
/*
|
|
|
|
* This may look redundant but is needed to work around an
|
|
|
|
* compiler quirk. The call from the real virFileGetXAttr
|
|
|
|
* to the real virFileGetXAttrQuiet has a quirk where the
|
|
|
|
* return value from virFileGetXAttrQuiet gets scrambled
|
|
|
|
* if we mock virFileGetXAttrQuiet, returning -1 instead
|
|
|
|
* of 0 despite succeeding. This happens on FreeBSD 11/12
|
2020-11-12 13:57:55 +00:00
|
|
|
* hosts with Clang, and is suspected to be some kind of
|
2019-07-05 17:14:52 +00:00
|
|
|
* compiler optimization. By mocking this function too we
|
|
|
|
* can workaround it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileGetXAttr(const char *path,
|
|
|
|
const char *name,
|
|
|
|
char **value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = virFileGetXAttrQuiet(path, name, value)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"Unable to get XATTR %s on %s",
|
|
|
|
name, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
int virFileSetXAttr(const char *path,
|
|
|
|
const char *name,
|
|
|
|
const char *value)
|
|
|
|
{
|
2022-02-08 14:17:30 +00:00
|
|
|
g_autofree char *key = get_key(path, name);
|
|
|
|
g_autofree char *val = g_strdup(value);
|
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
if (virHashUpdateEntry(xattr_paths, key, val) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
val = NULL;
|
|
|
|
|
2022-02-08 14:17:30 +00:00
|
|
|
return 0;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virFileRemoveXAttr(const char *path,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2022-02-08 14:17:30 +00:00
|
|
|
g_autofree char *key = get_key(path, name);
|
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
if ((ret = virHashRemoveEntry(xattr_paths, key)) < 0)
|
|
|
|
errno = ENODATA;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
#define VIR_MOCK_STAT_HOOK \
|
|
|
|
do { \
|
|
|
|
if (getenv(ENVVAR)) { \
|
|
|
|
uint32_t *val; \
|
|
|
|
\
|
|
|
|
virMutexLock(&m); \
|
|
|
|
init_hash(); \
|
|
|
|
\
|
|
|
|
memset(sb, 0, sizeof(*sb)); \
|
|
|
|
\
|
|
|
|
sb->st_mode = S_IFREG | 0666; \
|
|
|
|
sb->st_size = 123456; \
|
|
|
|
sb->st_ino = 1; \
|
|
|
|
\
|
|
|
|
if (!(val = virHashLookup(chown_paths, path))) { \
|
|
|
|
/* New path. Set the defaults */ \
|
|
|
|
sb->st_uid = DEFAULT_UID; \
|
|
|
|
sb->st_gid = DEFAULT_GID; \
|
|
|
|
} else { \
|
|
|
|
/* Known path. Set values passed to chown() earlier */ \
|
2019-04-15 15:17:40 +00:00
|
|
|
sb->st_uid = *val & 0xffff; \
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
sb->st_gid = *val >> 16; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
virMutexUnlock(&m); \
|
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
mock_chown(const char *path,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid)
|
|
|
|
{
|
2022-02-08 14:17:30 +00:00
|
|
|
g_autofree uint32_t *val = NULL;
|
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
if (gid >> 16 || uid >> 16) {
|
2020-09-22 20:17:03 +00:00
|
|
|
fprintf(stderr, "Attempt to set too high UID or GID: %llu %llu",
|
2018-12-07 12:21:43 +00:00
|
|
|
(unsigned long long) uid, (unsigned long long) gid);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2020-09-22 22:42:45 +00:00
|
|
|
val = g_new0(uint32_t, 1);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
*val = (gid << 16) + uid;
|
|
|
|
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
if (virHashUpdateEntry(chown_paths, path, val) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2022-02-08 14:17:30 +00:00
|
|
|
val = NULL;
|
|
|
|
return 0;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
#include "virmockstathelpers.c"
|
2018-12-07 12:21:43 +00:00
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
virMockStatRedirect(const char *path G_GNUC_UNUSED, char **newpath G_GNUC_UNUSED)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return 0;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
chown(const char *path, uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (getenv(ENVVAR))
|
|
|
|
ret = mock_chown(path, uid, gid);
|
|
|
|
else
|
|
|
|
ret = real_chown(path, uid, gid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
open(const char *path, int flags, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (getenv(ENVVAR)) {
|
|
|
|
ret = 42; /* Some dummy FD */
|
|
|
|
} else if (flags & O_CREAT) {
|
|
|
|
va_list ap;
|
|
|
|
mode_t mode;
|
|
|
|
va_start(ap, flags);
|
|
|
|
mode = (mode_t) va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
ret = real_open(path, flags, mode);
|
|
|
|
} else {
|
|
|
|
ret = real_open(path, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-13 16:25:35 +00:00
|
|
|
#if WITH___OPEN_2
|
|
|
|
int
|
|
|
|
__open_2(const char *path, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (getenv(ENVVAR)) {
|
|
|
|
ret = 42; /* Some dummy FD */
|
|
|
|
} else {
|
|
|
|
ret = real___open_2(path, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
int
|
|
|
|
close(int fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
init_syms();
|
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
if (fd == 42 && getenv(ENVVAR))
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = real_close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
int virFileLock(int fd G_GNUC_UNUSED,
|
|
|
|
bool shared G_GNUC_UNUSED,
|
|
|
|
off_t start G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED,
|
|
|
|
bool waitForLock G_GNUC_UNUSED)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
int virFileUnlock(int fd G_GNUC_UNUSED,
|
|
|
|
off_t start G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-18 09:05:18 +00:00
|
|
|
typedef struct _checkOwnerData checkOwnerData;
|
|
|
|
struct _checkOwnerData {
|
2021-02-05 08:53:36 +00:00
|
|
|
GHashTable *paths;
|
2019-04-18 09:05:18 +00:00
|
|
|
bool chown_fail;
|
2020-11-02 21:31:03 +00:00
|
|
|
bool selinux_fail;
|
2019-04-18 09:05:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
static int
|
|
|
|
checkSELinux(void *payload,
|
|
|
|
const char *name,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
checkOwnerData *data = opaque;
|
|
|
|
char *label = payload;
|
|
|
|
|
|
|
|
if (STRNEQ(label, DEFAULT_SELINUX_LABEL) &&
|
2021-02-05 08:53:36 +00:00
|
|
|
!g_hash_table_contains(data->paths, name)) {
|
2020-11-02 21:31:03 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Path %s wasn't restored back to its original SELinux label\n",
|
|
|
|
name);
|
|
|
|
data->selinux_fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-07 12:21:43 +00:00
|
|
|
static int
|
|
|
|
checkOwner(void *payload,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name,
|
2019-04-18 09:05:18 +00:00
|
|
|
void *opaque)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
2019-04-18 09:05:18 +00:00
|
|
|
checkOwnerData *data = opaque;
|
2018-12-07 12:21:43 +00:00
|
|
|
uint32_t owner = *((uint32_t*) payload);
|
|
|
|
|
2019-04-18 09:05:18 +00:00
|
|
|
if ((owner % 16 != DEFAULT_UID ||
|
|
|
|
owner >> 16 != DEFAULT_GID) &&
|
2021-02-05 08:53:36 +00:00
|
|
|
!g_hash_table_contains(data->paths, name)) {
|
2018-12-07 12:21:43 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Path %s wasn't restored back to its original owner\n",
|
2020-10-21 11:31:16 +00:00
|
|
|
name);
|
2019-04-18 09:05:18 +00:00
|
|
|
data->chown_fail = true;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
printXATTR(void *payload,
|
2020-10-21 11:31:16 +00:00
|
|
|
const char *name,
|
2018-12-07 12:21:43 +00:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
bool *xattr_fail = data;
|
|
|
|
|
|
|
|
/* The fact that we are in this function means that there are
|
|
|
|
* some XATTRs left behind. This is enough to claim an error. */
|
2019-04-15 11:56:11 +00:00
|
|
|
*xattr_fail = true;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
/* Hash table key consists of "$path:$xattr_name", xattr
|
|
|
|
* value is then the value stored in the hash table. */
|
2020-10-21 11:31:16 +00:00
|
|
|
printf("key=%s val=%s\n", name, (const char *) payload);
|
2018-12-07 12:21:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-18 09:05:18 +00:00
|
|
|
/**
|
|
|
|
* checkPaths:
|
|
|
|
* @paths: a NULL terminated list of paths expected not to be restored
|
|
|
|
*
|
|
|
|
* Check if all paths were restored and if no XATTR was left
|
|
|
|
* behind. Since restore is not done on all domain's paths, some
|
|
|
|
* paths are expected to be not restored. A list of such paths
|
|
|
|
* can be passed in @paths argument. If a path is not restored
|
|
|
|
* but it's on the list no error is indicated.
|
|
|
|
*/
|
2021-02-05 08:53:36 +00:00
|
|
|
int checkPaths(GHashTable *paths)
|
2018-12-07 12:21:43 +00:00
|
|
|
{
|
2020-11-02 21:31:03 +00:00
|
|
|
checkOwnerData data = { .paths = paths, .chown_fail = false, .selinux_fail = false };
|
2018-12-07 12:21:43 +00:00
|
|
|
bool xattr_fail = false;
|
2021-02-05 08:53:36 +00:00
|
|
|
GHashTableIter htitr;
|
|
|
|
void *key;
|
2022-02-08 14:17:30 +00:00
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2018-12-07 12:21:43 +00:00
|
|
|
|
|
|
|
init_hash();
|
|
|
|
|
2021-02-05 08:53:36 +00:00
|
|
|
g_hash_table_iter_init(&htitr, paths);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next(&htitr, &key, NULL)) {
|
|
|
|
if (!virHashLookup(chown_paths, key)) {
|
|
|
|
fprintf(stderr, "Unexpected path restored: %s\n", (const char *) key);
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2019-04-18 09:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
if (virHashForEach(selinux_paths, checkSELinux, &data) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
if (virHashForEach(chown_paths, checkOwner, &data) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2020-11-02 21:31:03 +00:00
|
|
|
if (virHashForEach(xattr_paths, printXATTR, &xattr_fail) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2020-11-02 21:31:03 +00:00
|
|
|
|
|
|
|
if (data.chown_fail || data.selinux_fail || xattr_fail)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2018-12-07 12:21:43 +00:00
|
|
|
|
2022-02-08 14:17:30 +00:00
|
|
|
return 0;
|
2018-12-07 12:21:43 +00:00
|
|
|
}
|
2019-04-15 11:45:38 +00:00
|
|
|
|
|
|
|
|
2019-04-15 14:15:10 +00:00
|
|
|
void freePaths(void)
|
|
|
|
{
|
2022-02-08 14:17:30 +00:00
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
|
|
|
|
2019-04-15 14:15:10 +00:00
|
|
|
init_hash();
|
|
|
|
|
2021-11-30 13:13:28 +00:00
|
|
|
g_clear_pointer(&selinux_paths, g_hash_table_unref);
|
|
|
|
g_clear_pointer(&chown_paths, g_hash_table_unref);
|
|
|
|
g_clear_pointer(&xattr_paths, g_hash_table_unref);
|
2019-04-15 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 11:45:38 +00:00
|
|
|
int
|
|
|
|
virProcessRunInFork(virProcessForkCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
return cb(-1, opaque);
|
|
|
|
}
|
2019-08-08 08:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* We don't really need to mock this function. The qemusecuritytest doesn't
|
|
|
|
* care about the actual value. However, travis runs build and tests in a
|
|
|
|
* container where utmp is missing and thus this function fails. */
|
|
|
|
int
|
|
|
|
virHostGetBootTime(unsigned long long *when)
|
|
|
|
{
|
|
|
|
*when = 1234567890;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-02 21:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
int
|
|
|
|
is_selinux_enabled(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct selabel_handle *
|
|
|
|
selabel_open(unsigned int backend G_GNUC_UNUSED,
|
|
|
|
const struct selinux_opt *opts G_GNUC_UNUSED,
|
|
|
|
unsigned nopts G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
return (void*)((intptr_t) 0x1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
selabel_close(struct selabel_handle *rec G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
/* nada */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
selinux_virtual_domain_context_path(void)
|
|
|
|
{
|
|
|
|
return abs_srcdir "/qemusecuritydata/virtual_domain_context";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
selinux_virtual_image_context_path(void)
|
|
|
|
{
|
|
|
|
return abs_srcdir "/qemusecuritydata/virtual_image_context";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int getcon_raw(char **context)
|
|
|
|
{
|
|
|
|
*context = g_strdup("system_u:system_r:virtd_t:s0-s0:c0.c1023");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mock_setfilecon_raw(const char *path,
|
|
|
|
const char *context)
|
|
|
|
{
|
|
|
|
g_autofree char *val = g_strdup(context);
|
2022-02-08 14:17:30 +00:00
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2020-11-02 21:31:03 +00:00
|
|
|
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
if (virHashUpdateEntry(selinux_paths, path, val) < 0)
|
2022-02-08 14:17:30 +00:00
|
|
|
return -1;
|
2020-11-02 21:31:03 +00:00
|
|
|
val = NULL;
|
|
|
|
|
2022-02-08 14:17:30 +00:00
|
|
|
return 0;
|
2020-11-02 21:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mock_getfilecon_raw(const char *path,
|
|
|
|
char **context)
|
|
|
|
{
|
|
|
|
const char *val;
|
2022-02-08 14:17:30 +00:00
|
|
|
VIR_LOCK_GUARD lock = virLockGuardLock(&m);
|
2020-11-02 21:31:03 +00:00
|
|
|
|
|
|
|
init_hash();
|
|
|
|
|
|
|
|
val = virHashLookup(selinux_paths, path);
|
|
|
|
if (!val)
|
|
|
|
val = DEFAULT_SELINUX_LABEL;
|
|
|
|
|
|
|
|
*context = g_strdup(val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
setfilecon_raw(const char *path,
|
|
|
|
const char *context)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (getenv(ENVVAR))
|
|
|
|
ret = mock_setfilecon_raw(path, context);
|
|
|
|
else
|
|
|
|
ret = real_setfilecon_raw(path, context);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
getfilecon_raw(const char *path,
|
|
|
|
char **context)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (getenv(ENVVAR))
|
|
|
|
ret = mock_getfilecon_raw(path, context);
|
|
|
|
else
|
|
|
|
ret = real_getfilecon_raw(path, context);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
selabel_lookup_raw(struct selabel_handle *hnd G_GNUC_UNUSED,
|
|
|
|
char **context,
|
|
|
|
const char *key G_GNUC_UNUSED,
|
|
|
|
int type G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
/* This function will be called only if we haven't found original label in
|
|
|
|
* XATTRs. Return something else than DEFAULT_SELINUX_LABEL so that it is
|
|
|
|
* considered as error. */
|
|
|
|
*context = g_strdup("system_u:object_r:default_t:s1");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|