Skip to contentSkip to conversation

selftests/bpf: fix the incorrect verification of port numbers.

PATCH Superseded [email protected] 3 participants 3 messages Aug 3, 2023 – Aug 4, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: fix the incorrect verification of port numbers.

From: Kui-Feng Lee <[email protected]>

Check port numbers before calling htons().

According to Dan Carpenter's report, Smatch identified incorrect port number checks. It is expected that the returned port number is an integer, with negative numbers indicating errors. However, the value was mistakenly verified after being translated by htons().

Fixes 8a8c2231cab2 ("selftests/bpf: fix the incorrect verification of port numbers.")
---
 tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +4 -2
index 95bab61a1e57..0df95bc88a9b 100644
@@ -110,11 +110,13 @@ static int connect_client_server_v6(int client_fd, int listen_fd)
 		.sin6_family = AF_INET6,
 		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
 	};
+	int port;
 	int err;
 
-	addr.sin6_port = htons(get_sock_port_v6(listen_fd));
-	if (addr.sin6_port < 0)
+	port = get_sock_port_v6(listen_fd);
+	if (port < 0)
 		return -1;
+	addr.sin6_port = htons(port);
 
 	err = connect(client_fd, (struct sockaddr *)&addr, sizeof(addr));
 	if (err < 0) {
Signature · 1 line
2.34.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · On 8/3/23 2:53 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Check port numbers before calling htons().

According to Dan Carpenter's report, Smatch identified incorrect port number checks. It is expected that the returned port number is an integer, with negative numbers indicating errors. However, the value was mistakenly verified after being translated by htons().

Quoted 4 lines
Fixes 8a8c2231cab2 ("selftests/bpf: fix the incorrect verification of port numbers.")

Ack with a small nit below.

Quoted 9 lines
---
  tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +0 -0
index 95bab61a1e57..0df95bc88a9b 100644

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c static int connect_client_server_v6(int client_fd, int listen_fd) · line 114

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +1 -0
@@ -110,11 +110,13 @@ static int connect_client_server_v6(int client_fd, int listen_fd)
  		.sin6_family = AF_INET6,
  		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
  	};
+	int port;
  	int err;

No need for a separate line for 'int port'.
Just doing 'int err, port;' sounds better.

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +3 -2
-	addr.sin6_port = htons(get_sock_port_v6(listen_fd));
-	if (addr.sin6_port < 0)
+	port = get_sock_port_v6(listen_fd);
+	if (port < 0)
  		return -1;
+	addr.sin6_port = htons(port);
 
  	err = connect(client_fd, (struct sockaddr *)&addr, sizeof(addr));
  	if (err < 0) {
#2 lore raw
Quoted 25 lines · Yonghong Song
Quoted 9 lines · On 8/3/23 2:53 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Check port numbers before calling htons().

According to Dan Carpenter's report, Smatch identified incorrect port number checks. It is expected that the returned port number is an integer, with negative numbers indicating errors. However, the value was mistakenly verified after being translated by htons().

Quoted 4 lines
Fixes 8a8c2231cab2 ("selftests/bpf: fix the incorrect verification of port numbers.")

Ack with a small nit below.

Quoted 4 lines

---
  tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 14 lines

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c static int connect_client_server_v6(int · line 115

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +1 -0
index 95bab61a1e57..0df95bc88a9b 100644
@@ -110,11 +110,13 @@ static int connect_client_server_v6(int 
 client_fd, int listen_fd)
           .sin6_family = AF_INET6,
           .sin6_addr = IN6ADDR_LOOPBACK_INIT,
       };
+    int port;
       int err;

No need for a separate line for 'int port'.
Just doing 'int err, port;' sounds better.

Got it! Thanks!

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +3 -2
-    addr.sin6_port = htons(get_sock_port_v6(listen_fd));
-    if (addr.sin6_port < 0)
+    port = get_sock_port_v6(listen_fd);
+    if (port < 0)
           return -1;
+    addr.sin6_port = htons(port);
       err = connect(client_fd, (struct sockaddr *)&addr, sizeof(addr));
       if (err < 0) {