qemusecuritytest: Store 'notRestored' files in a hash table

The validation code looks whether certain paths are in the 'notRestored'
list. For the purpose of lookup it's better to use a hash table rather
than a string list.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-05 09:53:36 +01:00
parent 92ca314695
commit 67c345cb97
3 changed files with 20 additions and 17 deletions

View File

@ -398,7 +398,7 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
typedef struct _checkOwnerData checkOwnerData; typedef struct _checkOwnerData checkOwnerData;
struct _checkOwnerData { struct _checkOwnerData {
const char **paths; GHashTable *paths;
bool chown_fail; bool chown_fail;
bool selinux_fail; bool selinux_fail;
}; };
@ -413,7 +413,7 @@ checkSELinux(void *payload,
char *label = payload; char *label = payload;
if (STRNEQ(label, DEFAULT_SELINUX_LABEL) && if (STRNEQ(label, DEFAULT_SELINUX_LABEL) &&
!virStringListHasString(data->paths, name)) { !g_hash_table_contains(data->paths, name)) {
fprintf(stderr, fprintf(stderr,
"Path %s wasn't restored back to its original SELinux label\n", "Path %s wasn't restored back to its original SELinux label\n",
name); name);
@ -434,7 +434,7 @@ checkOwner(void *payload,
if ((owner % 16 != DEFAULT_UID || if ((owner % 16 != DEFAULT_UID ||
owner >> 16 != DEFAULT_GID) && owner >> 16 != DEFAULT_GID) &&
!virStringListHasString(data->paths, name)) { !g_hash_table_contains(data->paths, name)) {
fprintf(stderr, fprintf(stderr,
"Path %s wasn't restored back to its original owner\n", "Path %s wasn't restored back to its original owner\n",
name); name);
@ -473,19 +473,22 @@ printXATTR(void *payload,
* can be passed in @paths argument. If a path is not restored * can be passed in @paths argument. If a path is not restored
* but it's on the list no error is indicated. * but it's on the list no error is indicated.
*/ */
int checkPaths(const char **paths) int checkPaths(GHashTable *paths)
{ {
int ret = -1; int ret = -1;
checkOwnerData data = { .paths = paths, .chown_fail = false, .selinux_fail = false }; checkOwnerData data = { .paths = paths, .chown_fail = false, .selinux_fail = false };
bool xattr_fail = false; bool xattr_fail = false;
size_t i; GHashTableIter htitr;
void *key;
virMutexLock(&m); virMutexLock(&m);
init_hash(); init_hash();
for (i = 0; paths && paths[i]; i++) { g_hash_table_iter_init(&htitr, paths);
if (!virHashLookup(chown_paths, paths[i])) {
fprintf(stderr, "Unexpected path restored: %s\n", paths[i]); while (g_hash_table_iter_next(&htitr, &key, NULL)) {
if (!virHashLookup(chown_paths, key)) {
fprintf(stderr, "Unexpected path restored: %s\n", (const char *) key);
goto cleanup; goto cleanup;
} }
} }

View File

@ -87,7 +87,7 @@ testDomain(const void *opaque)
{ {
const struct testData *data = opaque; const struct testData *data = opaque;
g_autoptr(virDomainObj) vm = NULL; g_autoptr(virDomainObj) vm = NULL;
g_auto(GStrv) notRestored = NULL; g_autoptr(GHashTable) notRestored = virHashNew(NULL);
size_t i; size_t i;
int ret = -1; int ret = -1;
@ -102,14 +102,12 @@ testDomain(const void *opaque)
continue; continue;
if (virStorageSourceIsLocalStorage(src) && src->path && if (virStorageSourceIsLocalStorage(src) && src->path &&
(src->shared || src->readonly) && (src->shared || src->readonly))
virStringListAdd(&notRestored, src->path) < 0) g_hash_table_insert(notRestored, g_strdup(src->path), NULL);
return -1;
for (n = src->backingStore; virStorageSourceIsBacking(n); n = n->backingStore) { for (n = src->backingStore; virStorageSourceIsBacking(n); n = n->backingStore) {
if (virStorageSourceIsLocalStorage(n) && n->path && if (virStorageSourceIsLocalStorage(n) && n->path)
virStringListAdd(&notRestored, n->path) < 0) g_hash_table_insert(notRestored, g_strdup(n->path), NULL);
return -1;
} }
} }
@ -123,7 +121,7 @@ testDomain(const void *opaque)
qemuSecurityRestoreAllLabel(data->driver, vm, false); qemuSecurityRestoreAllLabel(data->driver, vm, false);
if (checkPaths((const char **) notRestored) < 0) if (checkPaths(notRestored) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;

View File

@ -20,6 +20,8 @@
#define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST" #define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST"
extern int checkPaths(const char **paths); #include "internal.h"
extern int checkPaths(GHashTable *paths);
extern void freePaths(void); extern void freePaths(void);