Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Add tests for exclusive maps

Check if access is denied to another program for an exclusive map

Signed-off-by: KP Singh <kpsingh@kernel.org>
Link: https://lore.kernel.org/r/20250914215141.15144-6-kpsingh@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

KP Singh and committed by
Alexei Starovoitov
6c850cbc 567010a5

+88
+54
tools/testing/selftests/bpf/prog_tests/map_excl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (C) 2025 Google LLC. */ 3 + #define _GNU_SOURCE 4 + #include <unistd.h> 5 + #include <sys/syscall.h> 6 + #include <test_progs.h> 7 + #include <bpf/btf.h> 8 + 9 + #include "map_excl.skel.h" 10 + 11 + static void test_map_excl_allowed(void) 12 + { 13 + struct map_excl *skel = map_excl__open(); 14 + int err; 15 + 16 + err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access); 17 + if (!ASSERT_OK(err, "bpf_map__set_exclusive_program")) 18 + goto out; 19 + 20 + bpf_program__set_autoload(skel->progs.should_have_access, true); 21 + bpf_program__set_autoload(skel->progs.should_not_have_access, false); 22 + 23 + err = map_excl__load(skel); 24 + ASSERT_OK(err, "map_excl__load"); 25 + out: 26 + map_excl__destroy(skel); 27 + } 28 + 29 + static void test_map_excl_denied(void) 30 + { 31 + struct map_excl *skel = map_excl__open(); 32 + int err; 33 + 34 + err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access); 35 + if (!ASSERT_OK(err, "bpf_map__make_exclusive")) 36 + goto out; 37 + 38 + bpf_program__set_autoload(skel->progs.should_have_access, false); 39 + bpf_program__set_autoload(skel->progs.should_not_have_access, true); 40 + 41 + err = map_excl__load(skel); 42 + ASSERT_EQ(err, -EACCES, "exclusive map access not denied\n"); 43 + out: 44 + map_excl__destroy(skel); 45 + 46 + } 47 + 48 + void test_map_excl(void) 49 + { 50 + if (test__start_subtest("map_excl_allowed")) 51 + test_map_excl_allowed(); 52 + if (test__start_subtest("map_excl_denied")) 53 + test_map_excl_denied(); 54 + }
+34
tools/testing/selftests/bpf/progs/map_excl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (C) 2025 Google LLC. */ 3 + #include <linux/bpf.h> 4 + #include <time.h> 5 + #include <bpf/bpf_helpers.h> 6 + 7 + #include "bpf_misc.h" 8 + 9 + struct { 10 + __uint(type, BPF_MAP_TYPE_ARRAY); 11 + __type(key, __u32); 12 + __type(value, __u32); 13 + __uint(max_entries, 1); 14 + } excl_map SEC(".maps"); 15 + 16 + char _license[] SEC("license") = "GPL"; 17 + 18 + SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 19 + int should_have_access(void *ctx) 20 + { 21 + int key = 0, value = 0xdeadbeef; 22 + 23 + bpf_map_update_elem(&excl_map, &key, &value, 0); 24 + return 0; 25 + } 26 + 27 + SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 28 + int should_not_have_access(void *ctx) 29 + { 30 + int key = 0, value = 0xdeadbeef; 31 + 32 + bpf_map_update_elem(&excl_map, &key, &value, 0); 33 + return 0; 34 + }