From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 4DC781FF0DE for ; Thu, 19 Nov 2026 15:04:06 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B20F52169B; Thu, 19 Nov 2026 15:04:03 +0100 (CET) From: Fiona Ebner To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server] fix #8355: qga response: properly handle left-overs from other clients Date: Thu, 19 Nov 2026 15:02:49 +0100 Message-ID: <20261119140355.137355-1-f.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795097038371 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.753 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) KAM_MAILER 2 Automated Mailer Tag Left in Email RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: VYWQODZ6VGVDBBHY7KMHMSNEL5LTQSBI X-Message-ID-Hash: VYWQODZ6VGVDBBHY7KMHMSNEL5LTQSBI X-MailFrom: f.ebner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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