File Index
Files and directories
Sign in
FILE

read_cgroupfs_xattr.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/progs/read_cgroupfs_xattr.c C
 1// SPDX-License-Identifier: GPL-2.0
 2/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
 3
 4#include <vmlinux.h>
 5#include <bpf/bpf_tracing.h>
 6#include <bpf/bpf_helpers.h>
 7#include <bpf/bpf_core_read.h>
 8#include "bpf_experimental.h"
 9
10char _license[] SEC("license") = "GPL";
11
12pid_t target_pid = 0;
13
14char xattr_value[64];
15static const char expected_value_a[] = "bpf_selftest_value_a";
16static const char expected_value_b[] = "bpf_selftest_value_b";
17bool found_value_a;
18bool found_value_b;
19
20SEC("lsm.s/file_open")
21int BPF_PROG(test_file_open)
22{
23	u64 cgrp_id = bpf_get_current_cgroup_id();
24	struct cgroup_subsys_state *css, *tmp;
25	struct bpf_dynptr value_ptr;
26	struct cgroup *cgrp;
27
28	if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
29		return 0;
30
31	bpf_rcu_read_lock();
32	cgrp = bpf_cgroup_from_id(cgrp_id);
33	if (!cgrp) {
34		bpf_rcu_read_unlock();
35		return 0;
36	}
37
38	css = &cgrp->self;
39	bpf_dynptr_from_mem(xattr_value, sizeof(xattr_value), 0, &value_ptr);
40	bpf_for_each(css, tmp, css, BPF_CGROUP_ITER_ANCESTORS_UP) {
41		int ret;
42
43		ret = bpf_cgroup_read_xattr(tmp->cgroup, "user.bpf_test",
44					    &value_ptr);
45		if (ret < 0)
46			continue;
47
48		if (ret == sizeof(expected_value_a) &&
49		    !bpf_strncmp(xattr_value, sizeof(expected_value_a), expected_value_a))
50			found_value_a = true;
51		if (ret == sizeof(expected_value_b) &&
52		    !bpf_strncmp(xattr_value, sizeof(expected_value_b), expected_value_b))
53			found_value_b = true;
54	}
55
56	bpf_rcu_read_unlock();
57	bpf_cgroup_release(cgrp);
58
59	return 0;
60}
~/ Read-only file preview.