From nobody Tue Apr 30 17:34:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1513619225192624.2452582201437; Mon, 18 Dec 2017 09:47:05 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 04A9549015; Mon, 18 Dec 2017 17:47:03 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E898469290; Mon, 18 Dec 2017 17:47:01 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4457E4BB79; Mon, 18 Dec 2017 17:46:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBIHkvoX018047 for ; Mon, 18 Dec 2017 12:46:57 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6AC1669286; Mon, 18 Dec 2017 17:46:57 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84EB96928E; Mon, 18 Dec 2017 17:46:53 +0000 (UTC) From: "Daniel P. Berrange" To: libvir-list@redhat.com Date: Mon, 18 Dec 2017 17:46:51 +0000 Message-Id: <20171218174651.21367-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] rpc: fix race sending and encoding sasl data X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 18 Dec 2017 17:47:03 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The virNetSocketWriteSASL method has to encode the buffer it is given and t= hen write it to the underlying socket. This write is not guaranteed to send the full amount of data that was encoded by SASL. We cache the SASL encoded dat= a so that on the next invokation of virNetSocketWriteSASL we carry on sending it. The subtle problem is that the 'len' value passed into virNetSocketWriteSAS= L on the 2nd call may be larger than the original value. So when we've completed sending the SASL encoded data we previously cached, we must return the orig= inal length we encoded, not the new length. This flaw means we could potentially have been discarded queued data without sending it. This would have exhibited itself as a libvirt client never rece= iving the reply to a method it invokes, async events silently going missing, or w= orse stream data silently getting dropped. For this to be a problem libvirtd would have to be queued data to send to t= he client, while at the same time the TCP socket send buffer is full (due to a= very slow client). This is quite unlikely so if this bug was ever triggered by a= real world user it would be almost impossible to reproduce or diagnose, if indee= d it was ever noticed at all. Signed-off-by: Daniel P. Berrange Reviewed-by: John Ferlan --- src/rpc/virnetsocket.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 23089afef4..2d41a716ba 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -107,6 +107,7 @@ struct _virNetSocket { =20 const char *saslEncoded; size_t saslEncodedLength; + size_t saslEncodedRawLength; size_t saslEncodedOffset; #endif #if WITH_SSH2 @@ -1927,6 +1928,7 @@ static ssize_t virNetSocketWriteSASL(virNetSocketPtr = sock, const char *buf, size &sock->saslEncodedLength) < 0) return -1; =20 + sock->saslEncodedRawLength =3D tosend; sock->saslEncodedOffset =3D 0; } =20 @@ -1943,11 +1945,20 @@ static ssize_t virNetSocketWriteSASL(virNetSocketPt= r sock, const char *buf, size =20 /* Sent all encoded, so update raw buffer to indicate completion */ if (sock->saslEncodedOffset =3D=3D sock->saslEncodedLength) { + ssize_t done =3D sock->saslEncodedRawLength; sock->saslEncoded =3D NULL; - sock->saslEncodedOffset =3D sock->saslEncodedLength =3D 0; - - /* Mark as complete, so caller detects completion */ - return tosend; + sock->saslEncodedOffset =3D sock->saslEncodedLength =3D sock->sasl= EncodedRawLength =3D 0; + + /* Mark as complete, so caller detects completion. + * + * Note that 'done' is possibly less than our current + * 'tosend' value, since if virNetSocketWriteWire + * only partially sent the data, we might have been + * called a 2nd time to write remaining cached + * encoded data. This means that the caller might + * also have further raw data pending that's included + * in 'tosend' */ + return done; } else { /* Still have stuff pending in saslEncoded buffer. * Pretend to caller that we didn't send any yet. --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list