[PATCH bpf-next] selftests/bpf: Fix cgroup_xattr/read_cgroupfs_xattr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



cgroup_xattr/read_cgroupfs_xattr messes up lo without creating a netns
first. This causes issue with other tests. Fix this by using a different
hook (lsm.s/file_open) and not messing with lo.

Fixes: f4fba2d6d282 ("selftests/bpf: Add tests for bpf_cgroup_read_xattr")
Reported-by: Alexei Starovoitov <ast@xxxxxxxxxx>
Closes: https://lore.kernel.org/bpf/CAADnVQ+iqMi2HEj_iH7hsx+XJAsqaMWqSDe4tzcGAnehFWA9Sw@xxxxxxxxxxxxxx/
Signed-off-by: Song Liu <song@xxxxxxxxxx>
---
 .../selftests/bpf/prog_tests/cgroup_xattr.c   | 46 +++++--------------
 .../selftests/bpf/progs/read_cgroupfs_xattr.c |  4 +-
 2 files changed, 13 insertions(+), 37 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c b/tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c
index 87978a0f7eb7..b32fae02b5ce 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c
@@ -17,6 +17,7 @@
 #define CGROUP_FS_ROOT "/sys/fs/cgroup/"
 #define CGROUP_FS_PARENT CGROUP_FS_ROOT "foo/"
 #define CGROUP_FS_CHILD CGROUP_FS_PARENT "bar/"
+#define TMP_FILE "/tmp/selftests_cgroup_xattr"
 
 static int move_pid_to_cgroup(const char *cgroup_folder, pid_t pid)
 {
@@ -39,19 +40,18 @@ static int move_pid_to_cgroup(const char *cgroup_folder, pid_t pid)
 	return 0;
 }
 
-static void reset_cgroups_and_lo(void)
+static void cleanup(void)
 {
 	rmdir(CGROUP_FS_CHILD);
 	rmdir(CGROUP_FS_PARENT);
-	system("ip addr del 1.1.1.1/32 dev lo");
-	system("ip link set dev lo down");
+	unlink(TMP_FILE);
 }
 
 static const char xattr_value_a[] = "bpf_selftest_value_a";
 static const char xattr_value_b[] = "bpf_selftest_value_b";
 static const char xattr_name[] = "user.bpf_test";
 
-static int setup_cgroups_and_lo(void)
+static int setup(void)
 {
 	int err;
 
@@ -72,36 +72,19 @@ static int setup_cgroups_and_lo(void)
 	if (!ASSERT_OK(err, "setxattr 2"))
 		goto error;
 
-	err = system("ip link set dev lo up");
-	if (!ASSERT_OK(err, "lo up"))
-		goto error;
-
-	err = system("ip addr add 1.1.1.1 dev lo");
-	if (!ASSERT_OK(err, "lo addr v4"))
-		goto error;
-
-	err = write_sysctl("/proc/sys/net/ipv4/ping_group_range", "0 0");
-	if (!ASSERT_OK(err, "write_sysctl"))
-		goto error;
-
 	return 0;
 error:
-	reset_cgroups_and_lo();
+	cleanup();
 	return err;
 }
 
 static void test_read_cgroup_xattr(void)
 {
-	struct sockaddr_in sa4 = {
-		.sin_family = AF_INET,
-		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
-	};
 	struct read_cgroupfs_xattr *skel = NULL;
 	pid_t pid = gettid();
-	int sock_fd = -1;
-	int connect_fd = -1;
+	int tmp_fd;
 
-	if (!ASSERT_OK(setup_cgroups_and_lo(), "setup_cgroups_and_lo"))
+	if (!ASSERT_OK(setup(), "setup"))
 		return;
 	if (!ASSERT_OK(move_pid_to_cgroup(CGROUP_FS_CHILD, pid),
 		       "move_pid_to_cgroup"))
@@ -116,24 +99,17 @@ static void test_read_cgroup_xattr(void)
 	if (!ASSERT_OK(read_cgroupfs_xattr__attach(skel), "read_cgroupfs_xattr__attach"))
 		goto out;
 
-	sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
-	if (!ASSERT_OK_FD(sock_fd, "sock create"))
-		goto out;
-
-	connect_fd = connect(sock_fd, &sa4, sizeof(sa4));
-	if (!ASSERT_OK_FD(connect_fd, "connect 1"))
-		goto out;
-	close(connect_fd);
+	tmp_fd = open(TMP_FILE, O_RDONLY | O_CREAT);
+	ASSERT_OK_FD(tmp_fd, "open tmp file");
+	close(tmp_fd);
 
 	ASSERT_TRUE(skel->bss->found_value_a, "found_value_a");
 	ASSERT_TRUE(skel->bss->found_value_b, "found_value_b");
 
 out:
-	close(connect_fd);
-	close(sock_fd);
 	read_cgroupfs_xattr__destroy(skel);
 	move_pid_to_cgroup(CGROUP_FS_ROOT, pid);
-	reset_cgroups_and_lo();
+	cleanup();
 }
 
 void test_cgroup_xattr(void)
diff --git a/tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c b/tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c
index 855f85fc5522..405adbe5e8b0 100644
--- a/tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c
+++ b/tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c
@@ -17,8 +17,8 @@ static const char expected_value_b[] = "bpf_selftest_value_b";
 bool found_value_a;
 bool found_value_b;
 
-SEC("lsm.s/socket_connect")
-int BPF_PROG(test_socket_connect)
+SEC("lsm.s/file_open")
+int BPF_PROG(test_file_open)
 {
 	u64 cgrp_id = bpf_get_current_cgroup_id();
 	struct cgroup_subsys_state *css, *tmp;
-- 
2.47.1





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux