Skip to contentSkip to conversation

selftests/bpf: Fix cgroup_mprog_ordering failure due to uninitialized variable

PATCH Applied Yonghong Song 2 participants 2 messages Jun 11, 2025

Subsystems (3)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Fix cgroup_mprog_ordering failure due to uninitialized variable

On arm64, the cgroup_mprog_ordering selftest failed with test_progs run when building with clang compiler. The reason is due to socklen_t optlen not initialized.

In kernel function do_ip_getsockopt(), we have

        if (copy_from_sockptr(&len, optlen, sizeof(int)))
                return -EFAULT;
        if (len < 0)
                return -EINVAL;

The above 'len' variable is a negative value and hence the test failed.

But the test is okay on x86_64. I checked the x86_64 asm code and I didn't see explicit initialization of 'optlen' but its value is 0 so kernel didn't return error. This should be a pure luck.

Fix the bug by initializing 'oplen' var properly.

Fixes e422d5f118e4 ("selftests/bpf: Add two selftests for mprog API based cgroup progs")
---
 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c +1 -1
index 4a4e9710b474..a36d2e968bc5 100644
@@ -12,7 +12,7 @@ static int run_getsockopt_test(int cg_parent, int sock_fd, bool has_relative_fd)
 	struct cgroup_preorder *skel = NULL;
 	struct bpf_program *prog;
 	__u8 *result, buf;
-	socklen_t optlen;
+	socklen_t optlen = 1;
 	int err = 0;
 
 	skel = cgroup_preorder__open_and_load();
Signature · 1 line
2.47.1
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 4 lines · On Wed, 11 Jun 2025 09:21:03 -0700 you wrote:

On arm64, the cgroup_mprog_ordering selftest failed with test_progs run when building with clang compiler. The reason is due to socklen_t optlen not initialized.

Quoted 8 lines

In kernel function do_ip_getsockopt(), we have

        if (copy_from_sockptr(&len, optlen, sizeof(int)))
                return -EFAULT;
        if (len < 0)
                return -EINVAL;

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: Fix cgroup_mprog_ordering failure due to uninitialized variable

    https://git.kernel.org/bpf/bpf-next/c/517b088a846b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html