public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
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	[thread overview]
Message-ID: <20261119140355.137355-1-f.ebner@proxmox.com> (raw)

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





                 reply	other threads:[~2026-11-19 14:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119140355.137355-1-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal