From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 968EA74D03
 for <pve-devel@lists.proxmox.com>; Tue, 22 Jun 2021 16:29:19 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 8437928E21
 for <pve-devel@lists.proxmox.com>; Tue, 22 Jun 2021 16:28:49 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id CE29F28E16
 for <pve-devel@lists.proxmox.com>; Tue, 22 Jun 2021 16:28:47 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9BD23457EA
 for <pve-devel@lists.proxmox.com>; Tue, 22 Jun 2021 16:28:47 +0200 (CEST)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 22 Jun 2021 16:28:24 +0200
Message-Id: <20210622142824.18773-1-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.20.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.670 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [tools.pm, proxmox.com]
Subject: [pve-devel] [PATCH common] run_command: untaint end of buffer
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 22 Jun 2021 14:29:19 -0000

The performance improvements added in
cb9db10c1a9855cf40ff13e81f9dd97d6a9b2698 changed the output handling
to not remove the taintedness (see perlsec (1)) of the complete output
anymore.

This results in a few bugs which show up every now and then, and are
usually quite tedious to hunt down - usually they only occur when
run via GUI (since pveproxy/pvedaemon run in taint-mode, whereas our
CLI utilities usually do not) - see for example pve-storage commit
609f117ff24d2cff6b155e1d4b1175ceebe5bd7b

or more recently the report in our community-forum:
https://forum.proxmox.com/threads/cannot-clone-vm-or-move-disk-with-more-than-13-snapshots.89628/
(the magic number of 13 snapshots is the point where the output of
`qemu-img info --output=json` grows to more than 4k)

Given the doubtful security benefit of having to untaint output, only
if it does not end in new-line or if it is longer than 4k (we read
the output in 4k chunks) simply untaint the output unconditionally.

Tested with the reproducer from the forum-thread

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
Huge thanks to Dominik who pointed me in the right direction, after I
spend far more time than I'm proud to admit in the JSON::XS source,
perlsec(1), perlguts(1) and the surprisingly unhelpful output of
debugperl -Du

 src/PVE/Tools.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 8946e93..eb140ae 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -516,7 +516,7 @@ sub run_command {
 				&$outfunc($line) if $outfunc;
 				&$logfunc($line) if $logfunc;
 			    }
-			    $outlog .= $buf;
+			    ($outlog) .= ($buf =~ /(.*)/); #untaint
 			};
 			my $err = $@;
 			if ($err) {
@@ -537,7 +537,7 @@ sub run_command {
 				&$errfunc($line) if $errfunc;
 				&$logfunc($line) if $logfunc;
 			    }
-			    $errlog .= $buf;
+			    ($errlog) .= ($buf =~ /(.*)/); #untaint
 			};
 			my $err = $@;
 			if ($err) {
-- 
2.20.1