File: test_get_xattr.c

package info (click to toggle)
linux 6.16.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,724,468 kB
  • sloc: ansic: 26,560,391; asm: 271,356; sh: 143,999; python: 72,469; makefile: 57,129; perl: 36,821; xml: 19,553; cpp: 5,820; yacc: 4,915; lex: 2,955; awk: 1,667; sed: 28; ruby: 25
file content (84 lines) | stat: -rw-r--r-- 1,947 bytes parent folder | download | duplicates (7)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include "vmlinux.h"
#include <errno.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_kfuncs.h"
#include "bpf_misc.h"

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

__u32 monitored_pid;
__u32 found_xattr_from_file;
__u32 found_xattr_from_dentry;

static const char expected_value[] = "hello";
char value1[32];
char value2[32];

/* Matches caller of test_get_xattr() in prog_tests/fs_kfuncs.c */
static const char xattr_names[][64] = {
	/* The following work. */
	"user.kfuncs",
	"security.bpf.xxx",

	/* The following do not work. */
	"security.bpf",
	"security.selinux"
};

SEC("lsm.s/file_open")
int BPF_PROG(test_file_open, struct file *f)
{
	struct bpf_dynptr value_ptr;
	__u32 pid;
	int ret, i;

	pid = bpf_get_current_pid_tgid() >> 32;
	if (pid != monitored_pid)
		return 0;

	bpf_dynptr_from_mem(value1, sizeof(value1), 0, &value_ptr);

	for (i = 0; i < ARRAY_SIZE(xattr_names); i++) {
		ret = bpf_get_file_xattr(f, xattr_names[i], &value_ptr);
		if (ret == sizeof(expected_value))
			break;
	}
	if (ret != sizeof(expected_value))
		return 0;
	if (bpf_strncmp(value1, ret, expected_value))
		return 0;
	found_xattr_from_file = 1;
	return 0;
}

SEC("lsm.s/inode_getxattr")
int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)
{
	struct bpf_dynptr value_ptr;
	__u32 pid;
	int ret, i;

	pid = bpf_get_current_pid_tgid() >> 32;
	if (pid != monitored_pid)
		return 0;

	bpf_dynptr_from_mem(value2, sizeof(value2), 0, &value_ptr);

	for (i = 0; i < ARRAY_SIZE(xattr_names); i++) {
		ret = bpf_get_dentry_xattr(dentry, xattr_names[i], &value_ptr);
		if (ret == sizeof(expected_value))
			break;
	}
	if (ret != sizeof(expected_value))
		return 0;
	if (bpf_strncmp(value2, ret, expected_value))
		return 0;
	found_xattr_from_dentry = 1;

	/* return non-zero to fail getxattr from user space */
	return -EINVAL;
}