From: Kui-Feng Lee Date: Fri, 4 Aug 2023 16:58:31 +0000 (-0700) Subject: selftests/bpf: fix the incorrect verification of port numbers. X-Git-Tag: v6.6.7~2079^2~157^2~13 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9eab71bd887ae28882c29a82cb0cdb00e1654c54;p=platform%2Fkernel%2Flinux-starfive.git selftests/bpf: fix the incorrect verification of port numbers. 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 Closes: https://lore.kernel.org/bpf/cafd6585-d5a2-4096-b94f-7556f5aa7737@moroto.mountain/ Acked-by: Yonghong Song Signed-off-by: Kui-Feng Lee Link: https://lore.kernel.org/r/20230804165831.173627-1-thinker.li@gmail.com Signed-off-by: Martin KaFai Lau --- diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c index 95bab61..d686ef1 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c @@ -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) {