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 1A3A65A0269 for ; Wed, 25 Nov 2026 22:07:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795640839; 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=wowGZP4982bI62XkYFlVEmUOvii5L4qh2bTRxSH63K4=; b=dLLDeApz8ZP/2/mizvHgZ9XM7JMyAlT1quzX5qjauNtDeQ8l/UifuZKYbTnOP8Bjpf/ogF x/obR18szuSy7mB0mZhVLUc8AsYiFg+0SWO9bxenjDzBYaOPnd3vkkxxt/mfXRZBHyi9hM wrnGffbh7m1n+iXln+cdOrrZvq/jNyQ= Received: from mail-oa1-f71.google.com (mail-oa1-f71.google.com [209.85.160.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-235-OISqTkdiNSWN8q9zy6LAPw-1; Wed, 25 Nov 2026 21:07:16 +0000 X-MC-Unique: OISqTkdiNSWN8q9zy6LAPw-1 X-Mimecast-MFC-AGG-ID: OISqTkdiNSWN8q9zy6LAPw_1795640835 Received: by mail-oa1-f71.google.com with SMTP id 586e51a60fabf-4a8eeb3386bso6516298fac.0 for ; Wed, 25 Nov 2026 13:07:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795640835; x=1796245635; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh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V6wG hL2r75PL7mkWH3TfHfA9zzSuUa6TX+uUdmjchZkGBd5zpD20T+ariTC4iYbpInVfBwjmQDfQ10n jJOhU9EnxiJiBmM2w4DJNIWyssPObehNib9ZbG65xs X-Received: by 2002:a05:6870:e95:b0:464:13bb:5b63 with SMTP id 586e51a60fabf-4bb353c7a24mr12422197fac.5.1795640835317; Wed, 25 Nov 2026 13:07:15 -0800 (PST) X-Received: by 2002:a05:622a:a586:b0:55d:37b2:3a0e with SMTP id d75a77b69052e-55d76a6b594mr346542681cf.34.1795640419292; Wed, 25 Nov 2026 13:00:19 -0800 (PST) Received: from [192.168.2.15] (lnsm6-montreal02-142-122-43-105.internet.virginmobile.ca. [142.122.43.105]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-55dbcddc13csm19111551cf.19.2026.11.25.13.00.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Nov 2026 13:00:18 -0800 (PST) Message-ID: <230202d7-d7b4-4e4a-91d6-28a3b1f75b40@redhat.com> Date: Wed, 25 Nov 2026 16:00:17 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch To: Stefano Brivio References: <20261124024132.553377-1-sbrivio@redhat.com> <07bf01d7-42fe-4d9c-b03d-221abdb58ee8@redhat.com> <20261125153710.3d1f4d3d@elisabeth> From: Jon Maloy In-Reply-To: <20261125153710.3d1f4d3d@elisabeth> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3XBnu1M4iXSIyzvV1WBclQChGVhYG0hayEbibjzYm60_1795640835 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID-Hash: 3U3MPCXYGCMDOSQKCWXUVB7RJRLHZ37J X-Message-ID-Hash: 3U3MPCXYGCMDOSQKCWXUVB7RJRLHZ37J X-MailFrom: jmaloy@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 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]. >> - 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 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) ///jon