mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-11-03 11:51:11 +00:00
ff376c6283
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>
418 lines
8.9 KiB
C
418 lines
8.9 KiB
C
/*
|
|
* 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>
|
|
|
|
#include "virmock.h"
|
|
#include "virfile.h"
|
|
#include "virthread.h"
|
|
#include "virhash.h"
|
|
#include "virstring.h"
|
|
#include "qemusecuritytest.h"
|
|
#include "security/security_manager.h"
|
|
|
|
#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
|
|
* in memory. By default, all files are owned by 1:2.
|
|
* 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
|
|
|
|
|
|
static int (*real_chown)(const char *path, uid_t uid, gid_t gid);
|
|
static int (*real_open)(const char *path, int flags, ...);
|
|
static int (*real_close)(int fd);
|
|
|
|
|
|
/* 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. */
|
|
virHashTablePtr xattr_paths = NULL;
|
|
|
|
|
|
/* 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. */
|
|
virHashTablePtr chown_paths = NULL;
|
|
|
|
|
|
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;
|
|
|
|
if (!(xattr_paths = virHashCreate(10, virHashValueFree))) {
|
|
fprintf(stderr, "Unable to create hash table for XATTR paths\n");
|
|
abort();
|
|
}
|
|
|
|
if (!(chown_paths = virHashCreate(10, virHashValueFree))) {
|
|
fprintf(stderr, "Unable to create hash table for chowned paths\n");
|
|
abort();
|
|
}
|
|
}
|
|
|
|
|
|
static void
|
|
init_syms(void)
|
|
{
|
|
if (real_chown)
|
|
return;
|
|
|
|
VIR_MOCK_REAL_INIT(chown);
|
|
VIR_MOCK_REAL_INIT(open);
|
|
VIR_MOCK_REAL_INIT(close);
|
|
|
|
/* Intentionally not calling init_hash() here */
|
|
}
|
|
|
|
|
|
static char *
|
|
get_key(const char *path,
|
|
const char *name)
|
|
{
|
|
char *ret;
|
|
|
|
if (virAsprintf(&ret, "%s:%s", path, name) < 0) {
|
|
fprintf(stderr, "Unable to create hash table key\n");
|
|
abort();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
int
|
|
virFileGetXAttr(const char *path,
|
|
const char *name,
|
|
char **value)
|
|
{
|
|
int ret = -1;
|
|
char *key;
|
|
char *val;
|
|
|
|
key = get_key(path, name);
|
|
|
|
virMutexLock(&m);
|
|
init_syms();
|
|
init_hash();
|
|
|
|
if (!(val = virHashLookup(xattr_paths, key))) {
|
|
errno = ENODATA;
|
|
goto cleanup;
|
|
}
|
|
|
|
if (VIR_STRDUP(*value, val) < 0)
|
|
goto cleanup;
|
|
|
|
ret = 0;
|
|
cleanup:
|
|
virMutexUnlock(&m);
|
|
VIR_FREE(key);
|
|
return ret;
|
|
}
|
|
|
|
|
|
int virFileSetXAttr(const char *path,
|
|
const char *name,
|
|
const char *value)
|
|
{
|
|
int ret = -1;
|
|
char *key;
|
|
char *val;
|
|
|
|
key = get_key(path, name);
|
|
if (VIR_STRDUP(val, value) < 0)
|
|
return -1;
|
|
|
|
virMutexLock(&m);
|
|
init_syms();
|
|
init_hash();
|
|
|
|
if (virHashUpdateEntry(xattr_paths, key, val) < 0)
|
|
goto cleanup;
|
|
val = NULL;
|
|
|
|
ret = 0;
|
|
cleanup:
|
|
virMutexUnlock(&m);
|
|
VIR_FREE(val);
|
|
VIR_FREE(key);
|
|
return ret;
|
|
}
|
|
|
|
|
|
int virFileRemoveXAttr(const char *path,
|
|
const char *name)
|
|
{
|
|
int ret = -1;
|
|
char *key;
|
|
|
|
key = get_key(path, name);
|
|
|
|
virMutexLock(&m);
|
|
init_syms();
|
|
init_hash();
|
|
|
|
if ((ret = virHashRemoveEntry(xattr_paths, key)) < 0)
|
|
errno = ENODATA;
|
|
|
|
virMutexUnlock(&m);
|
|
VIR_FREE(key);
|
|
return ret;
|
|
}
|
|
|
|
|
|
#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 */ \
|
|
sb->st_uid = *val % 16; \
|
|
sb->st_gid = *val >> 16; \
|
|
} \
|
|
\
|
|
virMutexUnlock(&m); \
|
|
\
|
|
return 0; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
mock_chown(const char *path,
|
|
uid_t uid,
|
|
gid_t gid)
|
|
{
|
|
uint32_t *val = NULL;
|
|
int ret = -1;
|
|
|
|
if (gid >> 16 || uid >> 16) {
|
|
fprintf(stderr, "Attempt to set too high UID or GID: %lld %lld",
|
|
(unsigned long long) uid, (unsigned long long) gid);
|
|
abort();
|
|
}
|
|
|
|
if (VIR_ALLOC(val) < 0)
|
|
return -1;
|
|
|
|
*val = (gid << 16) + uid;
|
|
|
|
virMutexLock(&m);
|
|
init_hash();
|
|
|
|
if (virHashUpdateEntry(chown_paths, path, val) < 0)
|
|
goto cleanup;
|
|
val = NULL;
|
|
|
|
ret = 0;
|
|
cleanup:
|
|
virMutexUnlock(&m);
|
|
VIR_FREE(val);
|
|
return ret;
|
|
}
|
|
|
|
|
|
#include "virmockstathelpers.c"
|
|
|
|
static int
|
|
virMockStatRedirect(const char *path ATTRIBUTE_UNUSED, char **newpath ATTRIBUTE_UNUSED)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
int
|
|
close(int fd)
|
|
{
|
|
int ret;
|
|
|
|
init_syms();
|
|
|
|
if (fd == 42 && getenv(ENVVAR))
|
|
ret = 0;
|
|
else
|
|
ret = real_close(fd);
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
int virFileLock(int fd ATTRIBUTE_UNUSED,
|
|
bool shared ATTRIBUTE_UNUSED,
|
|
off_t start ATTRIBUTE_UNUSED,
|
|
off_t len ATTRIBUTE_UNUSED,
|
|
bool waitForLock ATTRIBUTE_UNUSED)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
|
|
int virFileUnlock(int fd ATTRIBUTE_UNUSED,
|
|
off_t start ATTRIBUTE_UNUSED,
|
|
off_t len ATTRIBUTE_UNUSED)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int
|
|
checkOwner(void *payload,
|
|
const void *name,
|
|
void *data)
|
|
{
|
|
bool *chown_fail = data;
|
|
uint32_t owner = *((uint32_t*) payload);
|
|
|
|
if (owner % 16 != DEFAULT_UID ||
|
|
owner >> 16 != DEFAULT_GID) {
|
|
fprintf(stderr,
|
|
"Path %s wasn't restored back to its original owner\n",
|
|
(const char *) name);
|
|
*chown_fail = false;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int
|
|
printXATTR(void *payload,
|
|
const void *name,
|
|
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. */
|
|
*xattr_fail = false;
|
|
|
|
/* Hash table key consists of "$path:$xattr_name", xattr
|
|
* value is then the value stored in the hash table. */
|
|
printf("key=%s val=%s\n", (const char *) name, (const char *) payload);
|
|
return 0;
|
|
}
|
|
|
|
|
|
int checkPaths(void)
|
|
{
|
|
int ret = -1;
|
|
bool chown_fail = false;
|
|
bool xattr_fail = false;
|
|
|
|
virMutexLock(&m);
|
|
init_hash();
|
|
|
|
if ((virHashForEach(chown_paths, checkOwner, &chown_fail)) < 0)
|
|
goto cleanup;
|
|
|
|
if ((virHashForEach(xattr_paths, printXATTR, &xattr_fail)) < 0)
|
|
goto cleanup;
|
|
|
|
if (chown_fail || xattr_fail)
|
|
goto cleanup;
|
|
|
|
ret = 0;
|
|
cleanup:
|
|
virHashRemoveAll(chown_paths);
|
|
virHashRemoveAll(xattr_paths);
|
|
virMutexUnlock(&m);
|
|
return ret;
|
|
}
|