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

View File

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

View File

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