qemusecuritymock: Allow some paths to be not restored

Some paths will not be restored. Because we can't possibly know
if they are still in use or not. Reflect this in the test so that
we can test more domains. Also see next commit for more detailed
explanation.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Michal Privoznik 2019-04-18 11:05:18 +02:00
parent d87f363a3f
commit 760fa05436
3 changed files with 37 additions and 11 deletions

View File

@ -353,20 +353,28 @@ int virFileUnlock(int fd ATTRIBUTE_UNUSED,
} }
typedef struct _checkOwnerData checkOwnerData;
struct _checkOwnerData {
const char **paths;
bool chown_fail;
};
static int static int
checkOwner(void *payload, checkOwner(void *payload,
const void *name, const void *name,
void *data) void *opaque)
{ {
bool *chown_fail = data; checkOwnerData *data = opaque;
uint32_t owner = *((uint32_t*) payload); uint32_t owner = *((uint32_t*) payload);
if (owner % 16 != DEFAULT_UID || if ((owner % 16 != DEFAULT_UID ||
owner >> 16 != DEFAULT_GID) { owner >> 16 != DEFAULT_GID) &&
!virStringListHasString(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",
(const char *) name); (const char *) name);
*chown_fail = true; data->chown_fail = true;
} }
return 0; return 0;
@ -391,22 +399,40 @@ printXATTR(void *payload,
} }
int checkPaths(void) /**
* 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.
*/
int checkPaths(const char **paths)
{ {
int ret = -1; int ret = -1;
bool chown_fail = false; checkOwnerData data = { .paths = paths, .chown_fail = false };
bool xattr_fail = false; bool xattr_fail = false;
size_t i;
virMutexLock(&m); virMutexLock(&m);
init_hash(); init_hash();
if ((virHashForEach(chown_paths, checkOwner, &chown_fail)) < 0) for (i = 0; paths && paths[i]; i++) {
if (!virHashLookup(chown_paths, paths[i])) {
fprintf(stderr, "Unexpected path restored: %s\n", paths[i]);
goto cleanup;
}
}
if ((virHashForEach(chown_paths, checkOwner, &data)) < 0)
goto cleanup; goto cleanup;
if ((virHashForEach(xattr_paths, printXATTR, &xattr_fail)) < 0) if ((virHashForEach(xattr_paths, printXATTR, &xattr_fail)) < 0)
goto cleanup; goto cleanup;
if (chown_fail || xattr_fail) if (data.chown_fail || xattr_fail)
goto cleanup; goto cleanup;
ret = 0; ret = 0;

View File

@ -100,7 +100,7 @@ testDomain(const void *opaque)
qemuSecurityRestoreAllLabel(data->driver, vm, false); qemuSecurityRestoreAllLabel(data->driver, vm, false);
if (checkPaths() < 0) if (checkPaths(NULL) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;

View File

@ -20,6 +20,6 @@
#define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST" #define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST"
extern int checkPaths(void); extern int checkPaths(const char **paths);
extern void freePaths(void); extern void freePaths(void);