Skip to content

Commit c8c7487

Browse files
committed
selftests/bpf: Add tests for exclusive maps
Check if access is denied to another program for an exclusive map Signed-off-by: KP Singh <[email protected]>
1 parent d38fab4 commit c8c7487

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
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 Paccess not denied\n");
43+
out:
44+
map_excl__destroy(skel);
45+
46+
}
47+
48+
void test_map_excl(void)
49+
{
50+
start_libbpf_log_capture();
51+
if (test__start_subtest("map_excl_allowed"))
52+
test_map_excl_allowed();
53+
stop_libbpf_log_capture();
54+
if (test__start_subtest("map_excl_denied"))
55+
test_map_excl_denied();
56+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
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+
}

0 commit comments

Comments
 (0)