tools/testing/selftests/bpf/progs/test_get_xattr.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_get_xattr.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_get_xattr.c- Extension
.c- Size
- 1947 bytes
- Lines
- 85
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.herrno.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf_kfuncs.hbpf_misc.h
Detected Declarations
function BPF_PROGfunction BPF_PROG
Annotated Snippet
// 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;
}
Annotation
- Immediate include surface: `vmlinux.h`, `errno.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf_kfuncs.h`, `bpf_misc.h`.
- Detected declarations: `function BPF_PROG`, `function BPF_PROG`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.