提交 6c4de116 编写于 作者: M Michal Privoznik

security_manager: Don't manipulate domain XML in virDomainDefGetSecurityLabelDef

The virDomainDefGetSecurityLabelDef was modifying the domain XML.
It tried to find a seclabel corresponding to given sec driver. If the
label wasn't found, the function created one which is wrong. In fact
it's security manager which should modify this part of domain XML.
上级 7a0f5021
...@@ -1001,7 +1001,7 @@ virDomainGraphicsListenDefClear(virDomainGraphicsListenDefPtr def) ...@@ -1001,7 +1001,7 @@ virDomainGraphicsListenDefClear(virDomainGraphicsListenDefPtr def)
return; return;
} }
static void void
virSecurityLabelDefFree(virSecurityLabelDefPtr def) virSecurityLabelDefFree(virSecurityLabelDefPtr def)
{ {
if (!def) if (!def)
...@@ -1014,7 +1014,7 @@ virSecurityLabelDefFree(virSecurityLabelDefPtr def) ...@@ -1014,7 +1014,7 @@ virSecurityLabelDefFree(virSecurityLabelDefPtr def)
} }
static void void
virSecurityDeviceLabelDefFree(virSecurityDeviceLabelDefPtr def) virSecurityDeviceLabelDefFree(virSecurityDeviceLabelDefPtr def)
{ {
if (!def) if (!def)
...@@ -16626,10 +16626,6 @@ virDomainDefGetSecurityLabelDef(virDomainDefPtr def, const char *model) ...@@ -16626,10 +16626,6 @@ virDomainDefGetSecurityLabelDef(virDomainDefPtr def, const char *model)
return def->seclabels[i]; return def->seclabels[i];
} }
seclabel = virDomainDefAddSecurityLabelDef(def, model);
if (seclabel)
seclabel->implicit = true;
return seclabel; return seclabel;
} }
...@@ -16664,55 +16660,31 @@ virDomainChrDefGetSecurityLabelDef(virDomainChrDefPtr def, const char *model) ...@@ -16664,55 +16660,31 @@ virDomainChrDefGetSecurityLabelDef(virDomainChrDefPtr def, const char *model)
} }
virSecurityLabelDefPtr virSecurityLabelDefPtr
virDomainDefAddSecurityLabelDef(virDomainDefPtr def, const char *model) virDomainDefGenSecurityLabelDef(const char *model)
{ {
virSecurityLabelDefPtr seclabel = NULL; virSecurityLabelDefPtr seclabel = NULL;
if (VIR_ALLOC(seclabel) < 0) if (VIR_ALLOC(seclabel) < 0 ||
goto no_memory; (model && !(seclabel->model = strdup(model)))) {
virReportOOMError();
if (model) { virSecurityLabelDefFree(seclabel);
seclabel->model = strdup(model); seclabel = NULL;
if (seclabel->model == NULL)
goto no_memory;
} }
if (VIR_EXPAND_N(def->seclabels, def->nseclabels, 1) < 0)
goto no_memory;
def->seclabels[def->nseclabels - 1] = seclabel;
return seclabel; return seclabel;
no_memory:
virReportOOMError();
virSecurityLabelDefFree(seclabel);
return NULL;
} }
virSecurityDeviceLabelDefPtr virSecurityDeviceLabelDefPtr
virDomainDiskDefAddSecurityLabelDef(virDomainDiskDefPtr def, const char *model) virDomainDiskDefGenSecurityLabelDef(const char *model)
{ {
virSecurityDeviceLabelDefPtr seclabel = NULL; virSecurityDeviceLabelDefPtr seclabel = NULL;
if (VIR_ALLOC(seclabel) < 0) if (VIR_ALLOC(seclabel) < 0 ||
goto no_memory; (model && !(seclabel->model = strdup(model)))) {
virReportOOMError();
if (model) { virSecurityDeviceLabelDefFree(seclabel);
seclabel->model = strdup(model); seclabel = NULL;
if (seclabel->model == NULL)
goto no_memory;
} }
if (VIR_EXPAND_N(def->seclabels, def->nseclabels, 1) < 0)
goto no_memory;
def->seclabels[def->nseclabels - 1] = seclabel;
return seclabel; return seclabel;
no_memory:
virReportOOMError();
virSecurityDeviceLabelDefFree(seclabel);
return NULL;
} }
...@@ -2298,10 +2298,13 @@ virSecurityDeviceLabelDefPtr ...@@ -2298,10 +2298,13 @@ virSecurityDeviceLabelDefPtr
virDomainChrDefGetSecurityLabelDef(virDomainChrDefPtr def, const char *model); virDomainChrDefGetSecurityLabelDef(virDomainChrDefPtr def, const char *model);
virSecurityLabelDefPtr virSecurityLabelDefPtr
virDomainDefAddSecurityLabelDef(virDomainDefPtr def, const char *model); virDomainDefGenSecurityLabelDef(const char *model);
virSecurityDeviceLabelDefPtr virSecurityDeviceLabelDefPtr
virDomainDiskDefAddSecurityLabelDef(virDomainDiskDefPtr def, const char *model); virDomainDiskDefGenSecurityLabelDef(const char *model);
void virSecurityLabelDefFree(virSecurityLabelDefPtr def);
void virSecurityDeviceLabelDefFree(virSecurityDeviceLabelDefPtr def);
typedef const char* (*virEventActionToStringFunc)(int type); typedef const char* (*virEventActionToStringFunc)(int type);
typedef int (*virEventActionFromStringFunc)(const char *type); typedef int (*virEventActionFromStringFunc)(const char *type);
......
...@@ -108,7 +108,6 @@ virDomainControllerTypeToString; ...@@ -108,7 +108,6 @@ virDomainControllerTypeToString;
virDomainCpuPlacementModeTypeFromString; virDomainCpuPlacementModeTypeFromString;
virDomainCpuPlacementModeTypeToString; virDomainCpuPlacementModeTypeToString;
virDomainDefAddImplicitControllers; virDomainDefAddImplicitControllers;
virDomainDefAddSecurityLabelDef;
virDomainDefCheckABIStability; virDomainDefCheckABIStability;
virDomainDefClearCCWAddresses; virDomainDefClearCCWAddresses;
virDomainDefClearDeviceAliases; virDomainDefClearDeviceAliases;
......
...@@ -424,24 +424,26 @@ int virSecurityManagerRestoreSavedStateLabel(virSecurityManagerPtr mgr, ...@@ -424,24 +424,26 @@ int virSecurityManagerRestoreSavedStateLabel(virSecurityManagerPtr mgr,
int virSecurityManagerGenLabel(virSecurityManagerPtr mgr, int virSecurityManagerGenLabel(virSecurityManagerPtr mgr,
virDomainDefPtr vm) virDomainDefPtr vm)
{ {
int rc = 0; int ret = -1;
size_t i; size_t i;
virSecurityManagerPtr* sec_managers = NULL; virSecurityManagerPtr* sec_managers = NULL;
virSecurityLabelDefPtr seclabel; virSecurityLabelDefPtr seclabel;
bool generated = false;
if (mgr == NULL || mgr->drv == NULL) if (mgr == NULL || mgr->drv == NULL)
return -1; return ret;
if ((sec_managers = virSecurityManagerGetNested(mgr)) == NULL) if ((sec_managers = virSecurityManagerGetNested(mgr)) == NULL)
return -1; return ret;
virObjectLock(mgr); virObjectLock(mgr);
for (i = 0; sec_managers[i]; i++) { for (i = 0; sec_managers[i]; i++) {
seclabel = virDomainDefGetSecurityLabelDef(vm, generated = false;
sec_managers[i]->drv->name); seclabel = virDomainDefGetSecurityLabelDef(vm, sec_managers[i]->drv->name);
if (seclabel == NULL) { if (!seclabel) {
rc = -1; if (!(seclabel = virDomainDefGenSecurityLabelDef(sec_managers[i]->drv->name)))
goto cleanup; goto cleanup;
generated = seclabel->implicit = true;
} }
if (seclabel->type == VIR_DOMAIN_SECLABEL_DEFAULT) { if (seclabel->type == VIR_DOMAIN_SECLABEL_DEFAULT) {
...@@ -457,23 +459,37 @@ int virSecurityManagerGenLabel(virSecurityManagerPtr mgr, ...@@ -457,23 +459,37 @@ int virSecurityManagerGenLabel(virSecurityManagerPtr mgr,
sec_managers[i]->requireConfined) { sec_managers[i]->requireConfined) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Unconfined guests are not allowed on this host")); _("Unconfined guests are not allowed on this host"));
rc = -1;
goto cleanup; goto cleanup;
} }
if (!sec_managers[i]->drv->domainGenSecurityLabel) { if (!sec_managers[i]->drv->domainGenSecurityLabel) {
virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__); virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
} else { } else {
rc += sec_managers[i]->drv->domainGenSecurityLabel(sec_managers[i], vm); /* The seclabel must be added to @vm prior calling domainGenSecurityLabel
if (rc) * which may require seclabel to be presented already */
if (VIR_APPEND_ELEMENT(vm->seclabels, vm->nseclabels, seclabel) < 0) {
virReportOOMError();
goto cleanup;
}
if (sec_managers[i]->drv->domainGenSecurityLabel(sec_managers[i], vm) < 0) {
if (VIR_DELETE_ELEMENT(vm->seclabels,
vm->nseclabels -1, vm->nseclabels) < 0)
vm->nseclabels--;
goto cleanup; goto cleanup;
}
} }
} }
ret = 0;
cleanup: cleanup:
virObjectUnlock(mgr); virObjectUnlock(mgr);
if (generated)
virSecurityLabelDefFree(seclabel);
VIR_FREE(sec_managers); VIR_FREE(sec_managers);
return rc; return ret;
} }
int virSecurityManagerReserveLabel(virSecurityManagerPtr mgr, int virSecurityManagerReserveLabel(virSecurityManagerPtr mgr,
......
...@@ -1161,11 +1161,15 @@ virSecuritySELinuxSetSecurityFileLabel(virDomainDiskDefPtr disk, ...@@ -1161,11 +1161,15 @@ virSecuritySELinuxSetSecurityFileLabel(virDomainDiskDefPtr disk,
if (ret == 1 && !disk_seclabel) { if (ret == 1 && !disk_seclabel) {
/* If we failed to set a label, but virt_use_nfs let us /* If we failed to set a label, but virt_use_nfs let us
* proceed anyway, then we don't need to relabel later. */ * proceed anyway, then we don't need to relabel later. */
disk_seclabel = disk_seclabel = virDomainDiskDefGenSecurityLabelDef(SECURITY_SELINUX_NAME);
virDomainDiskDefAddSecurityLabelDef(disk, SECURITY_SELINUX_NAME);
if (!disk_seclabel) if (!disk_seclabel)
return -1; return -1;
disk_seclabel->norelabel = true; disk_seclabel->norelabel = true;
if (VIR_APPEND_ELEMENT(disk->seclabels, disk->nseclabels, disk_seclabel) < 0) {
virReportOOMError();
virSecurityDeviceLabelDefFree(disk_seclabel);
return -1;
}
ret = 0; ret = 0;
} }
return ret; return ret;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册