Skip to content

Commit bacdf5a

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
selftests/bpf: Fix cgroup_xattr/read_cgroupfs_xattr
cgroup_xattr/read_cgroupfs_xattr has two issues: 1. 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. 2. cgroup_xattr/read_cgroupfs_xattr sets up cgroups without proper mount namespaces. Fix this by using the existing cgroup helpers. A new helper set_cgroup_xattr() is added to set xattr on cgroup files. Fixes: f4fba2d ("selftests/bpf: Add tests for bpf_cgroup_read_xattr") Reported-by: Alexei Starovoitov <[email protected]> Closes: https://lore.kernel.org/bpf/CAADnVQ+iqMi2HEj_iH7hsx+XJAsqaMWqSDe4tzcGAnehFWA9Sw@mail.gmail.com/ Signed-off-by: Song Liu <[email protected]> Tested-by: Eduard Zingerman <[email protected]> Tested-by: Ihor Solodrai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a5a7b25 commit bacdf5a

File tree

4 files changed

+49
-97
lines changed

4 files changed

+49
-97
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/mount.h>
55
#include <sys/stat.h>
66
#include <sys/types.h>
7+
#include <sys/xattr.h>
78
#include <linux/limits.h>
89
#include <stdio.h>
910
#include <stdlib.h>
@@ -318,6 +319,26 @@ int join_parent_cgroup(const char *relative_path)
318319
return join_cgroup_from_top(cgroup_path);
319320
}
320321

322+
/**
323+
* set_cgroup_xattr() - Set xattr on a cgroup dir
324+
* @relative_path: The cgroup path, relative to the workdir, to set xattr
325+
* @name: xattr name
326+
* @value: xattr value
327+
*
328+
* This function set xattr on cgroup dir.
329+
*
330+
* On success, it returns 0, otherwise on failure it returns -1.
331+
*/
332+
int set_cgroup_xattr(const char *relative_path,
333+
const char *name,
334+
const char *value)
335+
{
336+
char cgroup_path[PATH_MAX + 1];
337+
338+
format_cgroup_path(cgroup_path, relative_path);
339+
return setxattr(cgroup_path, name, value, strlen(value) + 1, 0);
340+
}
341+
321342
/**
322343
* __cleanup_cgroup_environment() - Delete temporary cgroups
323344
*

tools/testing/selftests/bpf/cgroup_helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ int join_cgroup(const char *relative_path);
2626
int join_root_cgroup(void);
2727
int join_parent_cgroup(const char *relative_path);
2828

29+
int set_cgroup_xattr(const char *relative_path,
30+
const char *name,
31+
const char *value);
32+
2933
int setup_cgroup_environment(void);
3034
void cleanup_cgroup_environment(void);
3135

tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c

Lines changed: 22 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,133 +7,60 @@
77
#include <string.h>
88
#include <unistd.h>
99
#include <sys/socket.h>
10-
#include <sys/xattr.h>
11-
1210
#include <test_progs.h>
11+
#include "cgroup_helpers.h"
1312

1413
#include "read_cgroupfs_xattr.skel.h"
1514
#include "cgroup_read_xattr.skel.h"
1615

17-
#define CGROUP_FS_ROOT "/sys/fs/cgroup/"
18-
#define CGROUP_FS_PARENT CGROUP_FS_ROOT "foo/"
16+
#define CGROUP_FS_PARENT "foo/"
1917
#define CGROUP_FS_CHILD CGROUP_FS_PARENT "bar/"
20-
21-
static int move_pid_to_cgroup(const char *cgroup_folder, pid_t pid)
22-
{
23-
char filename[128];
24-
char pid_str[64];
25-
int procs_fd;
26-
int ret;
27-
28-
snprintf(filename, sizeof(filename), "%scgroup.procs", cgroup_folder);
29-
snprintf(pid_str, sizeof(pid_str), "%d", pid);
30-
31-
procs_fd = open(filename, O_WRONLY | O_APPEND);
32-
if (!ASSERT_OK_FD(procs_fd, "open"))
33-
return -1;
34-
35-
ret = write(procs_fd, pid_str, strlen(pid_str));
36-
close(procs_fd);
37-
if (!ASSERT_GT(ret, 0, "write cgroup.procs"))
38-
return -1;
39-
return 0;
40-
}
41-
42-
static void reset_cgroups_and_lo(void)
43-
{
44-
rmdir(CGROUP_FS_CHILD);
45-
rmdir(CGROUP_FS_PARENT);
46-
system("ip addr del 1.1.1.1/32 dev lo");
47-
system("ip link set dev lo down");
48-
}
18+
#define TMP_FILE "/tmp/selftests_cgroup_xattr"
4919

5020
static const char xattr_value_a[] = "bpf_selftest_value_a";
5121
static const char xattr_value_b[] = "bpf_selftest_value_b";
5222
static const char xattr_name[] = "user.bpf_test";
5323

54-
static int setup_cgroups_and_lo(void)
55-
{
56-
int err;
57-
58-
err = mkdir(CGROUP_FS_PARENT, 0755);
59-
if (!ASSERT_OK(err, "mkdir 1"))
60-
goto error;
61-
err = mkdir(CGROUP_FS_CHILD, 0755);
62-
if (!ASSERT_OK(err, "mkdir 2"))
63-
goto error;
64-
65-
err = setxattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a,
66-
strlen(xattr_value_a) + 1, 0);
67-
if (!ASSERT_OK(err, "setxattr 1"))
68-
goto error;
69-
70-
err = setxattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b,
71-
strlen(xattr_value_b) + 1, 0);
72-
if (!ASSERT_OK(err, "setxattr 2"))
73-
goto error;
74-
75-
err = system("ip link set dev lo up");
76-
if (!ASSERT_OK(err, "lo up"))
77-
goto error;
78-
79-
err = system("ip addr add 1.1.1.1 dev lo");
80-
if (!ASSERT_OK(err, "lo addr v4"))
81-
goto error;
82-
83-
err = write_sysctl("/proc/sys/net/ipv4/ping_group_range", "0 0");
84-
if (!ASSERT_OK(err, "write_sysctl"))
85-
goto error;
86-
87-
return 0;
88-
error:
89-
reset_cgroups_and_lo();
90-
return err;
91-
}
92-
9324
static void test_read_cgroup_xattr(void)
9425
{
95-
struct sockaddr_in sa4 = {
96-
.sin_family = AF_INET,
97-
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
98-
};
26+
int tmp_fd, parent_cgroup_fd = -1, child_cgroup_fd = -1;
9927
struct read_cgroupfs_xattr *skel = NULL;
100-
pid_t pid = gettid();
101-
int sock_fd = -1;
102-
int connect_fd = -1;
10328

104-
if (!ASSERT_OK(setup_cgroups_and_lo(), "setup_cgroups_and_lo"))
29+
parent_cgroup_fd = test__join_cgroup(CGROUP_FS_PARENT);
30+
if (!ASSERT_OK_FD(parent_cgroup_fd, "create parent cgroup"))
10531
return;
106-
if (!ASSERT_OK(move_pid_to_cgroup(CGROUP_FS_CHILD, pid),
107-
"move_pid_to_cgroup"))
32+
if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a),
33+
"set parent xattr"))
34+
goto out;
35+
36+
child_cgroup_fd = test__join_cgroup(CGROUP_FS_CHILD);
37+
if (!ASSERT_OK_FD(child_cgroup_fd, "create child cgroup"))
38+
goto out;
39+
if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b),
40+
"set child xattr"))
10841
goto out;
10942

11043
skel = read_cgroupfs_xattr__open_and_load();
11144
if (!ASSERT_OK_PTR(skel, "read_cgroupfs_xattr__open_and_load"))
11245
goto out;
11346

114-
skel->bss->target_pid = pid;
47+
skel->bss->target_pid = gettid();
11548

11649
if (!ASSERT_OK(read_cgroupfs_xattr__attach(skel), "read_cgroupfs_xattr__attach"))
11750
goto out;
11851

119-
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
120-
if (!ASSERT_OK_FD(sock_fd, "sock create"))
121-
goto out;
122-
123-
connect_fd = connect(sock_fd, &sa4, sizeof(sa4));
124-
if (!ASSERT_OK_FD(connect_fd, "connect 1"))
125-
goto out;
126-
close(connect_fd);
52+
tmp_fd = open(TMP_FILE, O_RDONLY | O_CREAT);
53+
ASSERT_OK_FD(tmp_fd, "open tmp file");
54+
close(tmp_fd);
12755

12856
ASSERT_TRUE(skel->bss->found_value_a, "found_value_a");
12957
ASSERT_TRUE(skel->bss->found_value_b, "found_value_b");
13058

13159
out:
132-
close(connect_fd);
133-
close(sock_fd);
60+
close(child_cgroup_fd);
61+
close(parent_cgroup_fd);
13462
read_cgroupfs_xattr__destroy(skel);
135-
move_pid_to_cgroup(CGROUP_FS_ROOT, pid);
136-
reset_cgroups_and_lo();
63+
unlink(TMP_FILE);
13764
}
13865

13966
void test_cgroup_xattr(void)

tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ static const char expected_value_b[] = "bpf_selftest_value_b";
1717
bool found_value_a;
1818
bool found_value_b;
1919

20-
SEC("lsm.s/socket_connect")
21-
int BPF_PROG(test_socket_connect)
20+
SEC("lsm.s/file_open")
21+
int BPF_PROG(test_file_open)
2222
{
2323
u64 cgrp_id = bpf_get_current_cgroup_id();
2424
struct cgroup_subsys_state *css, *tmp;

0 commit comments

Comments
 (0)