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.129.124]) by passt.top (Postfix) with ESMTPS id 1D13C5A0269 for ; Wed, 25 Nov 2026 23:59:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795647596; 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=A2xC2M6g6iZZnlRkuDAXqni9Gapys5LLvZPptwXOTRM=; b=XwVVBrwkn7F428LaEiIPSJVZU4QpQU0jTQacoc30ayCwBmANTCaO8ciY6ERf8k1758tNFi x0Xl4NdQXW+NU9Vhy25GDG4pwTA99FiKj4K8iVB6+wKKKrku1XBhid59NI4KTybEvRwsuU aRyqCWJXYyvAR4RxzRcH7wKslgSJrW0= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-yTN_OKWXOkyjmvPXuouxWA-1; Wed, 25 Nov 2026 22:59:54 +0000 X-MC-Unique: yTN_OKWXOkyjmvPXuouxWA-1 X-Mimecast-MFC-AGG-ID: yTN_OKWXOkyjmvPXuouxWA_1795647593 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-4b9f361d9b8so792219f8f.1 for ; Wed, 25 Nov 2026 14:59:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795647593; x=1796252393; 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=A2xC2M6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V0jf55zbJD7jvD+nsaR2XgLyualQtZc7sMqaeyISDQwGQbP9K OKVv9eoCPuDfPxvzdt6J9PxU71b9vZlsrePMftCzP5sRqYB1SYTz44VD2PrTMANaF16Q= X-Received: by 2002:a05:6000:4812:b0:4a7:b8ab:df86 with SMTP id ffacd0b85a97d-4abb5db73bcmr34833675f8f.38.1795647593289; Wed, 25 Nov 2026 14:59:53 -0800 (PST) X-Received: by 2002:a05:6000:4812:b0:4a7:b8ab:df86 with SMTP id ffacd0b85a97d-4abb5db73bcmr34833634f8f.38.1795647592620; Wed, 25 Nov 2026 14:59:52 -0800 (PST) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [2a10:fc81:a806:d6a9::1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf685asm36956055f8f.20.2026.11.25.14.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 14:59:52 -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: <20261125235950.52c3f700@elisabeth> In-Reply-To: <230202d7-d7b4-4e4a-91d6-28a3b1f75b40@redhat.com> References: <20261124024132.553377-1-sbrivio@redhat.com> <07bf01d7-42fe-4d9c-b03d-221abdb58ee8@redhat.com> <20261125153710.3d1f4d3d@elisabeth> <230202d7-d7b4-4e4a-91d6-28a3b1f75b40@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 23:59:51 +0100 (CET) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 8z3Zd_w6XS7AvHpwIR3x65PzFpf9pr9pw5ccCiN4xJI_1795647593 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: UNPBLN35LRW4YBXT6A2Y6K424BY6YWWX X-Message-ID-Hash: UNPBLN35LRW4YBXT6A2Y6K424BY6YWWX 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 Wed, 25 Nov 2026 16:00:17 -0500 Jon Maloy wrote: > On 2026-11-25 09:37, Stefano Brivio wrote: > > On Tue, 24 Nov 2026 20:44:19 -0500 > > Jon Maloy wrote: > > [...] > > >> With this fix applied, clang-tidy 23.1.5 now reports: > >> > >> tcp_buf.c:363:20: error: Out of bound access to memory after the end > >> of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] > >> 363 | iov->iov_base = > >> &tcp_payload[tcp_payload_used + i].data; > >> [...] > >> tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA > >> 344 | fill_bufs = TCP_FRAMES; > >> [...] > >> tcp_buf.c:355:6: note: Assuming the condition is false > >> 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - > >> 1) { > >> [...] > >> tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing > >> 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. > > > If we limit fill_bufs to TCP_FRAMES_MEM - 1 (see below), the warning > goes away by itself, as the check can then be false without any > wraparound. > > >> 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. > >> > >> But we still have a problem when fill_bufs == 128: > >> > >> - 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. > >> > >> - 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: > > > That flush only sends slots 0 to 126, that is, 127 frames, and it > happens in the iteration for the 127th frame (i == 126) of the loop in > tcp_buf_data_from_sock(). The loop then continues with i == 127, for > the 128th frame, whose data we received into tcp_payload[127]. Oh, I see what you mean now! I just had in mind that it would flush *all* the (used) slots, which is also true, but it doesn't flush the all the tcp_l2_iov[] items, and we certainly don't restart refilling *those* from 0. That is, I didn't really consider the fact that the slots used by tcp_data_to_tap() are counted just like tcp_buf_data_from_sock() does. > >> - 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? > > But we aren't in the next call yet: we still have one frame to send, > with tcp_payload_used == 0 and i == 127. So no_csum is fine, but the > payload of that final frame is now taken from slot 0. > > More specifically: tcp_data_to_tap() builds the 128th frame in slot 0, > and takes its payload from iov[TCP_IOV_PAYLOAD].iov_base, which for > slot 0 is always &tcp_payload[0] (it's only set in tcp_sock_iov_init()). > But the data for that segment is in tcp_payload[127], and nothing > copies it over. So the frame goes out with the header (and sequence > number) of the 128th segment, and the payload of the first one. I see, thanks for the details. By the way, this other segment would be discarded (sequence number collision), and we would have to retransmit it. But anyway, luckily, we didn't release this change yet. > >> I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of > >> tcp_buf_data_from_sock() should fix it. > >> > >> Something like this: > >> > >> #define TCP_FRAMES \ > >> (c->mode == 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. > > Agreed. I was thinking of a single constant for the number of slots we > reserve for flags, used for the TCP_FRAMES limit, the check in > tcp_buf_data_from_sock(), and the flush thresholds in > tcp_data_to_tap() and tcp_buf_send_flag(), so that they can't get out > of sync again. > > With that, data frames never end up in the last slots, so those could > point to smaller, dedicated flag buffers instead of full tcp_payload[] > entries, and we wouldn't waste the space. We'd still keep a single > counter, so that frames and flags are sent in the order we queue them. > > Something like this, to be used in the mentioned functions: > > #define TCP_FRAMES_FLAGS 4 > #define TCP_FRAMES_DATA (TCP_FRAMES_MEM - TCP_FRAMES_FLAGS) > #define TCP_FRAMES (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_DATA) Right, makes sense. Maybe that first define could be called TCP_FRAMES_EXTRA_FLAGS if it doesn't make usage exceedingly awkward, and we could keep 128 for TCP_FRAMES_DATA so that we can send a "round" number of bytes in every batch. Given that this patch introduces the issue you described, perhaps it would be nicer to have a minimal fix on top of it (like the one with TCP_FRAMES_MEM - 1) first, and this macro rework later. -- Stefano