From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 41F4A5A0268; Fri, 18 Nov 2022 01:26:13 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH] tcp: Explicit bound check on options length field Date: Fri, 18 Nov 2022 01:26:13 +0100 Message-Id: <20221118002613.2656578-1-sbrivio@redhat.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: COE3XZK23MFRWXEG6RERUD4CIXXQ2BWV X-Message-ID-Hash: COE3XZK23MFRWXEG6RERUD4CIXXQ2BWV X-MailFrom: sbrivio@passt.top 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: David Gibson X-Mailman-Version: 3.3.3 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: For some reason, Coverity only reports this (harmless) warning after David's series unifying IPv4 and IPv6 sockets for TCP: an untrusted loop bound (CWE-606) in tcp_opt_get(), coming from the fact that we use indeed the value of a TCP header field as loop bound. Note, though, that the loop already checks we're not exceeding the length of the option field, and this field is used as 8-bit unsigned value, so we can't really look for options past the end of the header. In any case, make Coverity happy with an explicit check. Signed-off-by: Stefano Brivio --- tcp.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tcp.c b/tcp.c index 8044617..8874789 100644 --- a/tcp.c +++ b/tcp.c @@ -1146,7 +1146,11 @@ static int tcp_opt_get(const char *opts, size_t len, uint8_t type_find, break; default: type = *(opts++); - optlen = *(opts++) - 2; + + if (*opts < 2 || *(uint8_t *)opts > len) + return -1; + + optlen = *((uint8_t *)opts++) - 2; len -= 2; if (type != type_find) -- 2.35.1