Blame SOURCES/0004-tcp-tcp_splice-Get-rid-of-false-positive-CWE-394-Cov.patch

f07426
From 850bb9c15d39dcbefb0849955f4f09382f587c20 Mon Sep 17 00:00:00 2001
f07426
From: Stefano Brivio <sbrivio@redhat.com>
f07426
Date: Mon, 27 Feb 2023 02:45:42 +0100
f07426
Subject: [PATCH 04/20] tcp, tcp_splice: Get rid of false positive CWE-394
f07426
 Coverity warning from fls()
f07426
f07426
We use the return value of fls() as array index for debug strings.
f07426
f07426
While fls() can return -1 (if no bit is set), Coverity Scan doesn't
f07426
see that we're first checking the return value of another fls() call
f07426
with the same bitmask, before using it.
f07426
f07426
Call fls() once, store its return value, check it, and use the stored
f07426
value as array index.
f07426
f07426
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
f07426
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
f07426
(cherry picked from commit 5474bc5485d814acae19961f9a9cd4b541722a5e)
f07426
---
f07426
 tcp.c        | 12 ++++++++----
f07426
 tcp_splice.c | 24 ++++++++++++++++--------
f07426
 2 files changed, 24 insertions(+), 12 deletions(-)
f07426
f07426
diff --git a/tcp.c b/tcp.c
f07426
index 803c2c4..c62fe44 100644
f07426
--- a/tcp.c
f07426
+++ b/tcp.c
f07426
@@ -743,15 +743,19 @@ static void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
f07426
 			 unsigned long flag)
f07426
 {
f07426
 	if (flag & (flag - 1)) {
f07426
+		int flag_index = fls(~flag);
f07426
+
f07426
 		if (!(conn->flags & ~flag))
f07426
 			return;
f07426
 
f07426
 		conn->flags &= flag;
f07426
-		if (fls(~flag) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP: index %li: %s dropped", CONN_IDX(conn),
f07426
-			      tcp_flag_str[fls(~flag)]);
f07426
+			      tcp_flag_str[flag_index]);
f07426
 		}
f07426
 	} else {
f07426
+		int flag_index = fls(~flag);
f07426
+
f07426
 		if (conn->flags & flag) {
f07426
 			/* Special case: setting ACK_FROM_TAP_DUE on a
f07426
 			 * connection where it's already set is used to
f07426
@@ -766,9 +770,9 @@ static void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
f07426
 		}
f07426
 
f07426
 		conn->flags |= flag;
f07426
-		if (fls(flag) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP: index %li: %s", CONN_IDX(conn),
f07426
-			      tcp_flag_str[fls(flag)]);
f07426
+			      tcp_flag_str[flag_index]);
f07426
 		}
f07426
 	}
f07426
 
f07426
diff --git a/tcp_splice.c b/tcp_splice.c
f07426
index 84f855e..67af46b 100644
f07426
--- a/tcp_splice.c
f07426
+++ b/tcp_splice.c
f07426
@@ -127,22 +127,26 @@ static void conn_flag_do(const struct ctx *c, struct tcp_splice_conn *conn,
f07426
 			 unsigned long flag)
f07426
 {
f07426
 	if (flag & (flag - 1)) {
f07426
+		int flag_index = fls(~flag);
f07426
+
f07426
 		if (!(conn->flags & ~flag))
f07426
 			return;
f07426
 
f07426
 		conn->flags &= flag;
f07426
-		if (fls(~flag) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP (spliced): index %li: %s dropped", CONN_IDX(conn),
f07426
-			      tcp_splice_flag_str[fls(~flag)]);
f07426
+			      tcp_splice_flag_str[flag_index]);
f07426
 		}
f07426
 	} else {
f07426
+		int flag_index = fls(flag);
f07426
+
f07426
 		if (conn->flags & flag)
f07426
 			return;
f07426
 
f07426
 		conn->flags |= flag;
f07426
-		if (fls(flag) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP (spliced): index %li: %s", CONN_IDX(conn),
f07426
-			      tcp_splice_flag_str[fls(flag)]);
f07426
+			      tcp_splice_flag_str[flag_index]);
f07426
 		}
f07426
 	}
f07426
 
f07426
@@ -207,22 +211,26 @@ static void conn_event_do(const struct ctx *c, struct tcp_splice_conn *conn,
f07426
 			  unsigned long event)
f07426
 {
f07426
 	if (event & (event - 1)) {
f07426
+		int flag_index = fls(~event);
f07426
+
f07426
 		if (!(conn->events & ~event))
f07426
 			return;
f07426
 
f07426
 		conn->events &= event;
f07426
-		if (fls(~event) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP (spliced): index %li, ~%s", CONN_IDX(conn),
f07426
-			      tcp_splice_event_str[fls(~event)]);
f07426
+			      tcp_splice_event_str[flag_index]);
f07426
 		}
f07426
 	} else {
f07426
+		int flag_index = fls(event);
f07426
+
f07426
 		if (conn->events & event)
f07426
 			return;
f07426
 
f07426
 		conn->events |= event;
f07426
-		if (fls(event) >= 0) {
f07426
+		if (flag_index >= 0) {
f07426
 			debug("TCP (spliced): index %li, %s", CONN_IDX(conn),
f07426
-			      tcp_splice_event_str[fls(event)]);
f07426
+			      tcp_splice_event_str[flag_index]);
f07426
 		}
f07426
 	}
f07426
 
f07426
-- 
f07426
2.39.2
f07426