* [PATCH qemu-server] fix #8355: qga response: properly handle left-overs from other clients
@ 2026-11-19 14:02 Fiona Ebner
0 siblings, 0 replies; only message in thread
From: Fiona Ebner @ 2026-11-19 14:02 UTC (permalink / raw)
To: pve-devel
The QGA response buffer may contain left-overs from other clients if
they didn't read the response. The check that was in place only
compared the expected command ID and the one from the response
numerically, which does not work if the left-over originated from a
client with a different PID.
To be able to parse and compare the PID from a sequence number, the
format has to be adapted first, because the mapping ($pid,$seq) ->
int("${pid}0${seq}") is not injective. For example, 50501 can arise
from ($pid=5,$seq=501) or ($pid=505,$seq=1). Convert the sequence
number to octal first and use '8' as a, now uniquely identifiable,
separator to get a bijective mapping.
Warn about command IDs that cannot be parsed and from clients with
different PIDs, but skip them, to avoid interference, while still
aborting when encountering a higher sequence number from the same PID.
If there is a stale response from another PID, the orignal client is
already disconnected, so the response would not be handled either way.
Should the issue from bug #8355 happen during an upgrade, there is a
small chance that the left-over command ID is interpreted as belonging
to the current ID, but this is not worse than without the fix and
telling from the lack of reports until now, the issue itself is
extremely rare.
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/QMPClient.pm | 55 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 48 insertions(+), 7 deletions(-)
diff --git a/src/PVE/QMPClient.pm b/src/PVE/QMPClient.pm
index a7a64711..7c2c345e 100644
--- a/src/PVE/QMPClient.pm
+++ b/src/PVE/QMPClient.pm
@@ -196,18 +196,64 @@ sub cmd {
my $cmdid_seq = 0;
my $cmdid_seq_qga = 0;
+# For QGA, the sequence number is converted to octal and '8' is used as separator between the PID
+# and the sequence number. For QMP, ':' is used as the separator.
my $next_cmdid = sub {
my ($qga) = @_;
if ($qga) {
$cmdid_seq_qga++;
- return "$$" . "0" . $cmdid_seq_qga;
+ return "$$" . "8" . sprintf("%o", $cmdid_seq_qga);
} else {
$cmdid_seq++;
return "$$:$cmdid_seq";
}
};
+# Returns the PID and the sequence number (in decimal) of a command ID used for QGA. Returns undef
+# if not able to parse.
+my sub parse_qga_command_id {
+ my ($cmdid) = @_;
+
+ if ($cmdid =~ m/^([0-9]+)8([0-7]+)\z/) {
+ return ($1, oct($2));
+ }
+
+ return;
+}
+
+# Warns and returns if $cmd_id cannot be parsed or belongs to a different PID. Dies if
+# $expected_cmd_id cannot be parsed or if $cmd_id belongs to the same PID, but has a higher sequence
+# number. Returns 1 if the command IDs match.
+my sub should_handle_qga_response {
+ my ($expected_cmd_id, $cmd_id) = @_;
+
+ my ($expected_cmd_pid, $expected_cmd_seq) = parse_qga_command_id($expected_cmd_id);
+ if (!defined($expected_cmd_pid)) {
+ die "internal error - unable to parse QGA command ID '$expected_cmd_id'\n";
+ }
+
+ my ($cmd_pid, $cmd_seq) = parse_qga_command_id($cmd_id);
+ if (!defined($cmd_pid)) {
+ warn "got unexpected QGA command ID '$cmd_id'\n";
+ return;
+ }
+
+ if ($expected_cmd_pid != $cmd_pid) {
+ warn "skipping result from other PID '$cmd_pid' left in QGA buffer\n";
+ return;
+ }
+
+ if ($cmd_seq < $expected_cmd_seq) {
+ # skip results from previous commands
+ return;
+ } elsif ($cmd_seq > $expected_cmd_seq) {
+ die "got wrong command id '$cmd_id' (expected $expected_cmd_id)\n";
+ }
+
+ return 1;
+}
+
my $lookup_queue_info = sub {
my ($self, $fh, $noerr) = @_;
@@ -460,12 +506,7 @@ sub mux_input {
my $cmdid = $obj->{'return'};
die "received response without command id\n" if !$cmdid;
- # skip results from previous commands
- return if $cmdid < $curcmd->{id};
-
- if ($curcmd->{id} ne $cmdid) {
- die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
- }
+ return if !should_handle_qga_response($curcmd->{id}, $cmdid);
delete $queue_info->{current};
--
2.47.3
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 14:04 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 14:02 [PATCH qemu-server] fix #8355: qga response: properly handle left-overs from other clients Fiona Ebner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox