File: read_cgroupfs_xattr.c

package info (click to toggle)
linux 6.17.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,734,788 kB
  • sloc: ansic: 26,682,992; asm: 271,227; sh: 147,381; python: 75,980; makefile: 57,306; perl: 36,943; xml: 19,562; cpp: 5,899; yacc: 4,909; lex: 2,943; awk: 1,556; sed: 29; ruby: 25
file content (60 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */

#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_experimental.h"

char _license[] SEC("license") = "GPL";

pid_t target_pid = 0;

char xattr_value[64];
static const char expected_value_a[] = "bpf_selftest_value_a";
static const char expected_value_b[] = "bpf_selftest_value_b";
bool found_value_a;
bool found_value_b;

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;
	struct bpf_dynptr value_ptr;
	struct cgroup *cgrp;

	if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
		return 0;

	bpf_rcu_read_lock();
	cgrp = bpf_cgroup_from_id(cgrp_id);
	if (!cgrp) {
		bpf_rcu_read_unlock();
		return 0;
	}

	css = &cgrp->self;
	bpf_dynptr_from_mem(xattr_value, sizeof(xattr_value), 0, &value_ptr);
	bpf_for_each(css, tmp, css, BPF_CGROUP_ITER_ANCESTORS_UP) {
		int ret;

		ret = bpf_cgroup_read_xattr(tmp->cgroup, "user.bpf_test",
					    &value_ptr);
		if (ret < 0)
			continue;

		if (ret == sizeof(expected_value_a) &&
		    !bpf_strncmp(xattr_value, sizeof(expected_value_a), expected_value_a))
			found_value_a = true;
		if (ret == sizeof(expected_value_b) &&
		    !bpf_strncmp(xattr_value, sizeof(expected_value_b), expected_value_b))
			found_value_b = true;
	}

	bpf_rcu_read_unlock();
	bpf_cgroup_release(cgrp);

	return 0;
}