Skip to content

Commit

Permalink
selftests/bpf: ensure proper root namespace cleanup when test fail
Browse files Browse the repository at this point in the history
serial_test_flow_dissector_namespace manipulates both the root net
namespace and a dedicated non-root net namespace. If for some reason a
program attach on root namespace succeeds while it was expected to
fail, the unexpected program will remain attached to the root namespace,
possibly affecting other runs or even other tests in the same run.

Fix undesired test failure side effect by explicitly detaching programs
on failing tests expecting attach to fail. As a side effect of this
change, do not test errno value if the tested operation do not fail.

Fixes: 284ed00 ("selftests/bpf: migrate flow_dissector namespace exclusivity test")
Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]>
  • Loading branch information
Tropicao authored and Kernel Patches Daemon committed Nov 29, 2024
1 parent b87df96 commit 6cdbc9b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions tools/testing/selftests/bpf/prog_tests/flow_dissector.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,14 @@ void serial_test_flow_dissector_namespace(void)
ns = open_netns(TEST_NS);
if (!ASSERT_OK_PTR(ns, "enter non-root net namespace"))
goto out_clean_ns;

err = bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0);
if (!ASSERT_ERR(err,
"refuse new flow dissector in non-root net namespace"))
bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);
else
ASSERT_EQ(errno, EEXIST,
"refused because of already attached prog");
close_netns(ns);
ASSERT_ERR(err, "refuse new flow dissector in non-root net namespace");
ASSERT_EQ(errno, EEXIST, "refused because of already attached prog");

/* If no flow dissector is attached to the root namespace, we must
* be able to attach one to a non-root net namespace
Expand All @@ -545,8 +548,11 @@ void serial_test_flow_dissector_namespace(void)
* a flow dissector to root namespace must fail
*/
err = bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0);
ASSERT_ERR(err, "refuse new flow dissector on root namespace");
ASSERT_EQ(errno, EEXIST, "refused because of already attached prog");
if (!ASSERT_ERR(err, "refuse new flow dissector on root namespace"))
bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);
else
ASSERT_EQ(errno, EEXIST,
"refused because of already attached prog");

ns = open_netns(TEST_NS);
bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);
Expand Down

0 comments on commit 6cdbc9b

Please sign in to comment.