util: ensure all TPM global vars access is protected by lock

The virTPMEmulatorInit method updates various global variables
and holds a lock while doing so. Other methods which access
these variables, however, don't reliably hold locks over all
of their accesses.

Since virTPMEmulatorInit is no longer exported, we can push
the locking up into all the callers and achieve proper safety
for concurrent usage.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2021-11-24 10:30:25 +00:00
parent c032786e08
commit 63c42ba1d1

View File

@ -137,15 +137,17 @@ static int virTPMEmulatorInit(void);
static char * static char *
virTPMBinaryGetPath(virTPMBinary binary) virTPMBinaryGetPath(virTPMBinary binary)
{ {
char *s; char *s = NULL;
if (!swtpmBinaries[binary].path && virTPMEmulatorInit() < 0)
return NULL;
virMutexLock(&swtpm_tools_lock); virMutexLock(&swtpm_tools_lock);
s = g_strdup(swtpmBinaries[binary].path);
virMutexUnlock(&swtpm_tools_lock);
if (virTPMEmulatorInit() < 0)
goto cleanup;
s = g_strdup(swtpmBinaries[binary].path);
cleanup:
virMutexUnlock(&swtpm_tools_lock);
return s; return s;
} }
@ -269,11 +271,8 @@ virTPMGetCaps(virTPMBinaryCapsParse capsParse,
static int static int
virTPMEmulatorInit(void) virTPMEmulatorInit(void)
{ {
int ret = -1;
size_t i; size_t i;
virMutexLock(&swtpm_tools_lock);
for (i = 0; i < VIR_TPM_BINARY_LAST; i++) { for (i = 0; i < VIR_TPM_BINARY_LAST; i++) {
g_autofree char *path = NULL; g_autofree char *path = NULL;
bool findit = swtpmBinaries[i].path == NULL; bool findit = swtpmBinaries[i].path == NULL;
@ -297,18 +296,18 @@ virTPMEmulatorInit(void)
virReportSystemError(ENOENT, virReportSystemError(ENOENT,
_("Unable to find '%s' binary in $PATH"), _("Unable to find '%s' binary in $PATH"),
virTPMBinaryTypeToString(i)); virTPMBinaryTypeToString(i));
goto cleanup; return -1;
} }
if (!virFileIsExecutable(path)) { if (!virFileIsExecutable(path)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s is not an executable"), _("%s is not an executable"),
path); path);
goto cleanup; return -1;
} }
if (stat(path, &swtpmBinaries[i].stat) < 0) { if (stat(path, &swtpmBinaries[i].stat) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Could not stat %s"), path); _("Could not stat %s"), path);
goto cleanup; return -1;
} }
swtpmBinaries[i].path = g_steal_pointer(&path); swtpmBinaries[i].path = g_steal_pointer(&path);
@ -317,26 +316,29 @@ virTPMEmulatorInit(void)
swtpmBinaries[i].path, swtpmBinaries[i].path,
swtpmBinaries[i].parm); swtpmBinaries[i].parm);
if (!swtpmBinaries[i].caps) if (!swtpmBinaries[i].caps)
goto cleanup; return -1;
} }
} }
} }
ret = 0; return 0;
cleanup:
virMutexUnlock(&swtpm_tools_lock);
return ret;
} }
static bool static bool
virTPMBinaryGetCaps(virTPMBinary binary, virTPMBinaryGetCaps(virTPMBinary binary,
unsigned int cap) unsigned int cap)
{ {
bool ret = false;
virMutexLock(&swtpm_tools_lock);
if (virTPMEmulatorInit() < 0) if (virTPMEmulatorInit() < 0)
return false; goto cleanup;
return virBitmapIsBitSet(swtpmBinaries[binary].caps, cap); ret = virBitmapIsBitSet(swtpmBinaries[binary].caps, cap);
cleanup:
virMutexUnlock(&swtpm_tools_lock);
return ret;
} }
bool bool