From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=fail (p=quarantine dis=none) header.from=redhat.com Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id E6ECF5A026E for ; Wed, 25 Nov 2026 15:37:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795617437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ba+b8BCaeyiX8O3Apzfnj9HCOC6eEkO179TPlBOOEdY=; b=Gg+LQHDqNcyJvb5SR1WWnh4dxHhfDEG6Ayz5XHp/oIKomlKoYTPd2jsZ0cx4nvyOE+YHYg i2Od5fKG9y5MJmetifGTSSKVdwqcjjECegiC9cah7J0T0lBnsLW9HlHNA6c3M0ylucRn54 y120WXp2uyhcxh/8gjFyxdH/Jj1lsBg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-324-R2CEqT1XODmFMhzsonFuJg-1; Wed, 25 Nov 2026 14:37:14 +0000 X-MC-Unique: R2CEqT1XODmFMhzsonFuJg-1 X-Mimecast-MFC-AGG-ID: R2CEqT1XODmFMhzsonFuJg_1795617433 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-4a82fb7e71eso3280051f8f.0 for ; Wed, 25 Nov 2026 06:37:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795617433; x=1796222233; h=date:content-transfer-encoding:content-type:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=ec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q6J/XTf4lDkPTA7PBeApOk2pRDQekBW/Kk391pKX+uyJX6IxcjsJ8tbaE7GHuodtpKtODvX5 Mb1Yd7tM224kxrdQqv1s6pCQH3rNbOz3l7AiqwqKaaKAgnc1U8V/HuG4k3pXpJAlQWKHKqOWuUj NIeO8RrUDC X-Received: by 2002:a05:6000:25c2:b0:4a6:7781:b064 with SMTP id ffacd0b85a97d-4abb51457e8mr43980194f8f.31.1795617432574; Wed, 25 Nov 2026 06:37:12 -0800 (PST) X-Received: by 2002:a05:6000:25c2:b0:4a6:7781:b064 with SMTP id ffacd0b85a97d-4abb51457e8mr43980105f8f.31.1795617431919; Wed, 25 Nov 2026 06:37:11 -0800 (PST) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6a680483sm35066942f8f.28.2026.11.25.06.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 06:37:11 -0800 (PST) From: Stefano Brivio To: Jon Maloy Subject: Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch Message-ID: <20261125153710.3d1f4d3d@elisabeth> In-Reply-To: <07bf01d7-42fe-4d9c-b03d-221abdb58ee8@redhat.com> References: <20261124024132.553377-1-sbrivio@redhat.com> <07bf01d7-42fe-4d9c-b03d-221abdb58ee8@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 Date: Wed, 25 Nov 2026 15:37:11 +0100 (CET) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 9cAtODL1RSp5DO7-Dzg2on66e4QP7WSoXCOmSCteaWE_1795617433 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: MPTNCETD77VYJVRYNVZURO4FGWAMS6IW X-Message-ID-Hash: MPTNCETD77VYJVRYNVZURO4FGWAMS6IW X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier , passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Tue, 24 Nov 2026 20:44:19 -0500 Jon Maloy wrote: > On 2026-11-24 02:42, Laurent Vivier wrote: > > On 11/24/26 03:41, Stefano Brivio wrote: > >> To avoid the risk of splitting two repeated ACKs, the second one being > >> a duplicate one, onto different batches, as it might significantly > >> worsen recovery time using fast retransmits, we don't check, in > >> tcp_buf_send_flag(), if we need to flush the current batch of frames > >> before we can append a duplicate ACK flag. > >> > >> So we originally kept one reserved slot at the end of it: for example, > >> tcp_buf_send_flag() calls tcp_payload_flush() if we have less than two > >> free slots. > >> > >> However, with the merging of data and flags buffers, not all the > >> conditions under which we call tcp_payload_flush() were updated: > >> tcp_data_to_tap() and tcp_buf_data_from_sock() could return without > >> ensuring that an extra free slot would be available for a duplicate > >> ACK flag.=C2=A0 If this happens, and we coincidentally append a duplic= ate > >> ACK flag to the batch right after a call to those functions, we'll > >> crash (reliably) in tcp_buf_send_flag(). > >> > >> Fix those two paths to also reserve one extra slot for duplicate > >> ACK flags. > >> > >> Note that, recently, https://bugs.passt.top/show_bug.cgi?id=3D224 > >> raised some compliance issues with RFC 5681, so we might need to > >> reserve more slots once we implement full compliance, and probably, > >> at that point, it would make more sense to consider a different > >> approach, instead of wasting all that space. > >> > >> Analysed-by: Laurent Vivier > >> Fixes: 78da088f7bab ("tcp: unify payload and flags l2 frames array") > >> Signed-off-by: Stefano Brivio =20 > >=20 > > Reviewed-by: Laurent Vivier > > =20 > >> --- > >> =C2=A0 tcp_buf.c | 4 ++-- > >> =C2=A0 1 file changed, 2 insertions(+), 2 deletions(-) > >> > >> diff --git a/tcp_buf.c b/tcp_buf.c > >> index ee8c7ce..0780a32 100644 > >> --- a/tcp_buf.c > >> +++ b/tcp_buf.c > >> @@ -309,7 +309,7 @@ static void tcp_data_to_tap(const struct ctx *c,= =20 > >> struct tcp_tap_conn *conn, > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 tcp_l2_buf_pad(iov); > >> -=C2=A0=C2=A0=C2=A0 if (++tcp_payload_used > TCP_FRAMES_MEM - 1) > >> +=C2=A0=C2=A0=C2=A0 if (++tcp_payload_used > TCP_FRAMES_MEM - 2) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 tcp_payload_flu= sh(c, now); > >> =C2=A0 } > >> @@ -352,7 +352,7 @@ int tcp_buf_data_from_sock(const struct ctx *c,=20 > >> struct tcp_tap_conn *conn, > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -1; > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > >> -=C2=A0=C2=A0=C2=A0 if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM)= { > >> +=C2=A0=C2=A0=C2=A0 if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM = - 1) { > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 tcp_payload_flu= sh(c, now); > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* Silence Cove= rity CWE-125 false positive */ =20 > > =20 >=20 > With this fix applied, clang-tidy 23.1.5 now reports: >=20 > tcp_buf.c:363:20: error: Out of bound access to memory after the end= =20 > of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] > 363 | iov->iov_base =3D=20 > &tcp_payload[tcp_payload_used + i].data; > [...] > tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA > 344 | fill_bufs =3D TCP_FRAMES; > [...] > tcp_buf.c:355:6: note: Assuming the condition is false > 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM -= =20 > 1) { > [...] > tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing=20 > index, while it holds only 128 'struct tcp_payload_t' elements Oops, sorry, my version of clang-tidy doesn't report this. We should add a suppression, unless you have something more elegant in mind. > The path itself is a false positive. If fill_bufs is set to 128 on > line 344, the condition on line 355 will always be true > ((tcp_payload_used + 128) > 127) unless the sum wraps around > at 2^32, which is impossible. >=20 > But we still have a problem when fill_bufs =3D=3D 128: >=20 > - The check on line 355 is always true, so we flush, tcp_payload_used > becomes 0, and we are ready to receive into all 128 buffers. >=20 > - If the following recvmsg() fills more than 127 buffers, that is, > returns more than 127 * mss bytes, send_bufs is 128 (it can't be more > than fill_bufs). If so, tcp_data_to_tap() flushes slots [0, 126] after > the 127th frame, as ++tcp_payload_used > TCP_FRAMES_MEM - 2. I don't understand this part (and I don't see an issue here): if send_bufs is 128, we'll eventually enter tcp_data_to_tap() with tcp_payload_used being 126, and flush the whole array. Now, tcp_payload_used becomes 0, and: > - When this happens, the 128th frame is built based on the payload in > slot 0 instead of slot 127, which is where its data was actually > received. So we end up sending the guest a frame with the sequence > number of the 128th segment, but with the data of the first one, > which we already flushed. The guest accepts and acknowledges it, > and the stream is silently corrupted. ...this doesn't actually happen because tcp_payload_used is 0 on the next call, so 'no_csum' will be 0 as well, and we don't use previous frames at all, right? > I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of > tcp_buf_data_from_sock() should fix it. >=20 > Something like this: >=20 > #define TCP_FRAMES \ > (c->mode =3D=3D MODE_PASTA ? 1 : TCP_FRAMES_MEM - 1) Regardless of whether there's any real issue, given that this seems to cause confusion, I start thinking whether this would make things clearer. Perhaps we should leave some tail space specifically for flags, though, because actually we should be able to queue up to 4 flags, once we properly fix compliance with RFC 5681, and at that point I don't think we want to risk wasting 256 KiB for that. > I haven't tried to reproduce this issue, so I am open to > the possibility that I am wrong. >=20 > ///jon >=20 --=20 Stefano