selftests/bpf: fix the incorrect verification of port numbers.
Subsystems (3)
PATCH v2 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().
Major changes from v1:
- Move the variable 'port' to the same line of 'err'.
--- tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +4 -3
@@ -110,11 +110,12 @@ static int connect_client_server_v6(int client_fd, int listen_fd) .sin6_family = AF_INET6, .sin6_addr = IN6ADDR_LOOPBACK_INIT, }; -int err; +int err, port; - 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
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 10 lines · On Fri, 4 Aug 2023 09:58:31 -0700 you 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().[...]
Here is the summary with links:
- [bpf-next,v2] selftests/bpf: fix the incorrect verification of port numbers.
https://git.kernel.org/bpf/bpf-next/c/9eab71bd887a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 27 lines · On 8/4/23 9:58 AM, [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().Major changes from v1:
- Move the variable 'port' to the same line of 'err'.
Fixes 539c7e67aa4a ("selftests/bpf: Verify that the cgroup_skb filters receive expected packets.")Reported-by Dan Carpenter <[email protected]>Acked-by Yonghong Song <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>--- tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +0
-0
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c static int connect_client_server_v6(int client_fd, int listen_fd) · line 116
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +3
-3@@ -110,11 +110,12 @@ static int connect_client_server_v6(int client_fd, int listen_fd) .sin6_family = AF_INET6, .sin6_addr = IN6ADDR_LOOPBACK_INIT, }; -int err; +int err, port; - 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 )
Applied. Some follow up questions:
Does other get_sock_port_v6() usage need to check -1 also?
It is a good idea to see if similar helpers exist in network_helpers.c e.g. There is get_socket_local_port() that supports both v4 and v6 in network_helpers.c which is equivalent to the get_sock_port_v6() here.
To a larger extent, I believe many codes in this new test can be saved by using the helpers in network_helpers.c. For example, the connect_client_server_v6() here can be replaced with connect_to_fd() from network_helpers.c to avoid the mistake this patch fixing. It also has some timeout limit on the socket such that it won't block the test_progs for a long time.
Quoted 41 lines · Martin KaFai Lau
Quoted 27 lines · On 8/4/23 9:58 AM, [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().Major changes from v1:
- Move the variable 'port' to the same line of 'err'.
Fixes 539c7e67aa4a ("selftests/bpf: Verify that the cgroup_skb filters receive expected packets.")Reported-by Dan Carpenter <[email protected]>Acked-by Yonghong Song <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>---
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +0
-0tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c static int connect_client_server_v6(int · line 116
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +3
-3@@ -110,11 +110,12 @@ static int connect_client_server_v6(int client_fd, int listen_fd) .sin6_family = AF_INET6, .sin6_addr = IN6ADDR_LOOPBACK_INIT, }; - int err; + int err, port; - 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)Applied. Some follow up questions:
Quoted 4 lines
Does other
get_sock_port_v6()usage need to check -1 also?It is a good idea to see if similar helpers exist in
network_helpers.ce.g. There isget_socket_local_port()that supports both v4 and v6 innetwork_helpers.cwhich is equivalent to theget_sock_port_v6()here.
To a larger extent, I believe many codes in this new test can be saved by using the helpers innetwork_helpers.c. For example, theconnect_client_server_v6()here can be replaced withconnect_to_fd()fromnetwork_helpers.cto avoid the mistake this patch fixing. It also has some timeout limit on the socket such that it won't block thetest_progsfor a long time.
Got it! I will check the code to send another patch if necessary.