tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c- Extension
.c- Size
- 9944 bytes
- Lines
- 299
- 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
test_progs.hbpf/btf.htest_unpriv_bpf_disabled.skel.hcap_helpers.hbpf_util.hsysctl_helpers.h
Detected Declarations
function process_ringbuffunction process_perfbuffunction test_unpriv_bpf_disabled_positivefunction test_unpriv_bpf_disabled_negativefunction ASSERT_GTfunction test_unpriv_bpf_disabled
Annotated Snippet
if (prog_array) {
/* need valid prog array value */
vals[0] = prog_fd;
/* prog array lookup returns prog id, not fd */
expected_val = prog_id;
}
ASSERT_OK(bpf_map_update_elem(map_fds[i], &key, vals, 0), "map_update_elem");
ASSERT_OK(bpf_map_lookup_elem(map_fds[i], &key, &lookup_vals), "map_lookup_elem");
ASSERT_EQ(lookup_vals[0], expected_val, "map_lookup_elem_values");
if (!array)
ASSERT_OK(bpf_map_delete_elem(map_fds[i], &key), "map_delete_elem");
}
link_fd = bpf_link_create(bpf_program__fd(skel->progs.handle_perf_event), perf_fd,
BPF_PERF_EVENT, NULL);
ASSERT_GT(link_fd, 0, "link_create");
cleanup:
if (link_fd)
close(link_fd);
if (perfbuf)
perf_buffer__free(perfbuf);
if (ringbuf)
ring_buffer__free(ringbuf);
}
static void test_unpriv_bpf_disabled_negative(struct test_unpriv_bpf_disabled *skel,
__u32 prog_id, int prog_fd, int perf_fd,
char **map_paths, int *map_fds)
{
const struct bpf_insn prog_insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
const size_t prog_insn_cnt = ARRAY_SIZE(prog_insns);
LIBBPF_OPTS(bpf_prog_load_opts, load_opts);
struct bpf_map_info map_info = {};
__u32 map_info_len = sizeof(map_info);
struct bpf_link_info link_info = {};
__u32 link_info_len = sizeof(link_info);
struct btf *btf = NULL;
__u32 attach_flags = 0;
__u32 prog_ids[3] = {};
__u32 prog_cnt = 3;
__u32 next;
int i;
/* Negative tests for unprivileged BPF disabled. Verify we cannot
* - load BPF programs;
* - create BPF maps;
* - get a prog/map/link fd by id;
* - get next prog/map/link id
* - query prog
* - BTF load
*/
ASSERT_EQ(bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "simple_prog", "GPL",
prog_insns, prog_insn_cnt, &load_opts),
-EPERM, "prog_load_fails");
/* some map types require particular correct parameters which could be
* sanity-checked before enforcing -EPERM, so only validate that
* the simple ARRAY and HASH maps are failing with -EPERM
*/
for (i = BPF_MAP_TYPE_HASH; i <= BPF_MAP_TYPE_ARRAY; i++)
ASSERT_EQ(bpf_map_create(i, NULL, sizeof(int), sizeof(int), 1, NULL),
-EPERM, "map_create_fails");
ASSERT_EQ(bpf_prog_get_fd_by_id(prog_id), -EPERM, "prog_get_fd_by_id_fails");
ASSERT_EQ(bpf_prog_get_next_id(prog_id, &next), -EPERM, "prog_get_next_id_fails");
ASSERT_EQ(bpf_prog_get_next_id(0, &next), -EPERM, "prog_get_next_id_fails");
if (ASSERT_OK(bpf_map_get_info_by_fd(map_fds[0], &map_info, &map_info_len),
"obj_get_info_by_fd")) {
ASSERT_EQ(bpf_map_get_fd_by_id(map_info.id), -EPERM, "map_get_fd_by_id_fails");
ASSERT_EQ(bpf_map_get_next_id(map_info.id, &next), -EPERM,
"map_get_next_id_fails");
}
ASSERT_EQ(bpf_map_get_next_id(0, &next), -EPERM, "map_get_next_id_fails");
if (ASSERT_OK(bpf_link_get_info_by_fd(bpf_link__fd(skel->links.sys_nanosleep_enter),
&link_info, &link_info_len),
"obj_get_info_by_fd")) {
ASSERT_EQ(bpf_link_get_fd_by_id(link_info.id), -EPERM, "link_get_fd_by_id_fails");
ASSERT_EQ(bpf_link_get_next_id(link_info.id, &next), -EPERM,
"link_get_next_id_fails");
}
ASSERT_EQ(bpf_link_get_next_id(0, &next), -EPERM, "link_get_next_id_fails");
ASSERT_EQ(bpf_prog_query(prog_fd, BPF_TRACE_FENTRY, 0, &attach_flags, prog_ids,
&prog_cnt), -EPERM, "prog_query_fails");
Annotation
- Immediate include surface: `test_progs.h`, `bpf/btf.h`, `test_unpriv_bpf_disabled.skel.h`, `cap_helpers.h`, `bpf_util.h`, `sysctl_helpers.h`.
- Detected declarations: `function process_ringbuf`, `function process_perfbuf`, `function test_unpriv_bpf_disabled_positive`, `function test_unpriv_bpf_disabled_negative`, `function ASSERT_GT`, `function test_unpriv_bpf_disabled`.
- 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.