Skip to content

Commit e896db9

Browse files
committed
ch: Validate device type for serial/console
When creating the vm configuration options passed to cloud-hypervisor, look at the source->type and ensure it is a PTY before trying to use that device. Similarly when trying to open a console ensure the source->type is also a PTY. This is done as the vm->def for console may be populated with content that isn't valid for cloud-hypervisor usage. The preferred vm definition should contain the following: <console type='pty'> <target type='serial' port='0'/> </console> for serial console and <console type='pty'> <target type='virtio' port='0'/> </console> for virtio console (with only one defined regardless of type).
1 parent ae33d3f commit e896db9

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/ch/ch_driver.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,8 @@ chDomainOpenConsole(virDomainPtr dom,
865865
chr = vm->def->parallels[i];
866866
}
867867
} else {
868-
if (vm->def->nconsoles)
868+
if (vm->def->nconsoles &&
869+
vm->def->consoles[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY)
869870
chr = vm->def->consoles[0];
870871
else if (vm->def->nserials)
871872
chr = vm->def->serials[0];

src/ch/ch_monitor.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,33 @@ virCHMonitorBuildPTYJson(virJSONValuePtr content, virDomainDefPtr vmdef)
114114
virJSONValuePtr ptys = virJSONValueNewObject();
115115

116116
if (vmdef->nconsoles || vmdef->nserials) {
117-
if (vmdef->nconsoles > 1) {
117+
if ((vmdef->nconsoles &&
118+
vmdef->consoles[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY)
119+
&& (vmdef->nserials &&
120+
vmdef->serials[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY)) {
121+
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
122+
_("Only a single console or serial can be configured for this domain"));
123+
return -1;
124+
} else if (vmdef->nconsoles > 1) {
118125
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
119126
_("Only a single console can be configured for this domain"));
120127
return -1;
121-
}
122-
if (vmdef->nserials > 1) {
128+
} else if (vmdef->nserials > 1) {
123129
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
124130
_("Only a single serial can be configured for this domain"));
125131
return -1;
126132
}
127133
}
128134

129-
if (vmdef->nconsoles) {
135+
if (vmdef->nconsoles && vmdef->consoles[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY) {
130136
if (virJSONValueObjectAppendString(ptyc, "mode", "Pty") < 0)
131137
goto cleanup;
132138
} else {
133139
if (virJSONValueObjectAppendString(ptyc, "mode", "Null") < 0)
134140
goto cleanup;
135141
}
136142

137-
if (vmdef->nserials) {
143+
if (vmdef->nserials && vmdef->serials[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY) {
138144
if (virJSONValueObjectAppendString(ptys, "mode", "Pty") < 0)
139145
goto cleanup;
140146
} else {

0 commit comments

Comments
 (0)