summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/map_excl.c
blob: 3f4422b9ffa699a768407aa36502147343592ad8 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2025 Google LLC. */
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <test_progs.h>
#include <bpf/btf.h>

#include "map_excl.skel.h"
#include "bpf_iter_bpf_array_map.skel.h"

#ifndef SHA256_DIGEST_SIZE
#define SHA256_DIGEST_SIZE	32
#endif

static void test_map_excl_allowed(void)
{
	struct map_excl *skel = map_excl__open();
	int err;

	err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access);
	if (!ASSERT_OK(err, "bpf_map__set_exclusive_program"))
		goto out;

	bpf_program__set_autoload(skel->progs.should_have_access, true);
	bpf_program__set_autoload(skel->progs.should_not_have_access, false);

	err = map_excl__load(skel);
	ASSERT_OK(err, "map_excl__load");
out:
	map_excl__destroy(skel);
}

static void test_map_excl_denied(void)
{
	struct map_excl *skel = map_excl__open();
	int err;

	err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access);
	if (!ASSERT_OK(err, "bpf_map__make_exclusive"))
		goto out;

	bpf_program__set_autoload(skel->progs.should_have_access, false);
	bpf_program__set_autoload(skel->progs.should_not_have_access, true);

	err = map_excl__load(skel);
	ASSERT_EQ(err, -EACCES, "exclusive map access not denied\n");
out:
	map_excl__destroy(skel);

}

static void test_map_excl_no_map_in_map(void)
{
	__u8 hash[SHA256_DIGEST_SIZE] = {};
	LIBBPF_OPTS(bpf_map_create_opts, excl_opts,
		    .excl_prog_hash = hash,
		    .excl_prog_hash_size = sizeof(hash));
	LIBBPF_OPTS(bpf_map_create_opts, outer_opts);
	int excl_fd, tmpl_fd = -1, outer_fd = -1, err;
	__u32 key = 0;

	excl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl_inner", 4, 4, 1, &excl_opts);
	if (!ASSERT_OK_FD(excl_fd, "create exclusive map"))
		return;

	outer_opts.inner_map_fd = excl_fd;
	err = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer_from_excl",
			     4, 4, 1, &outer_opts);
	if (err >= 0)
		close(err);
	ASSERT_EQ(err, -ENOTSUPP, "reject exclusive map as map-in-map template");

	tmpl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "tmpl", 4, 4, 1, NULL);
	if (!ASSERT_OK_FD(tmpl_fd, "create inner template"))
		goto out;

	outer_opts.inner_map_fd = tmpl_fd;
	outer_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer", 4, 4, 1, &outer_opts);
	if (!ASSERT_OK_FD(outer_fd, "create map-of-maps"))
		goto out;

	err = bpf_map_update_elem(outer_fd, &key, &excl_fd, 0);
	ASSERT_EQ(err, -ENOTSUPP, "reject exclusive map as map-in-map element");
out:
	if (outer_fd >= 0)
		close(outer_fd);
	if (tmpl_fd >= 0)
		close(tmpl_fd);
	close(excl_fd);
}

static void test_map_excl_no_map_iter(void)
{
	__u8 hash[SHA256_DIGEST_SIZE] = {};
	LIBBPF_OPTS(bpf_map_create_opts, excl_opts,
		    .excl_prog_hash = hash,
		    .excl_prog_hash_size = sizeof(hash));
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_bpf_array_map *skel = NULL;
	union bpf_iter_link_info linfo;
	struct bpf_link *link;
	int excl_fd;

	excl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl_iter", 4, 8, 3, &excl_opts);
	if (!ASSERT_OK_FD(excl_fd, "create exclusive map"))
		return;

	skel = bpf_iter_bpf_array_map__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_array_map__open_and_load"))
		goto out;

	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = excl_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);

	link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
	if (!ASSERT_ERR_PTR(link, "reject exclusive map as iter target")) {
		bpf_link__destroy(link);
		goto out;
	}
	ASSERT_EQ(libbpf_get_error(link), -EPERM, "iter attach errno");
out:
	bpf_iter_bpf_array_map__destroy(skel);
	close(excl_fd);
}

static void test_map_excl_create_validation(void)
{
	LIBBPF_OPTS(bpf_map_create_opts, o);
	__u8 hash[SHA256_DIGEST_SIZE] = {};
	int fd;

	o.excl_prog_hash = hash;
	o.excl_prog_hash_size = SHA256_DIGEST_SIZE / 2;
	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
	if (fd >= 0)
		close(fd);
	ASSERT_EQ(fd, -EINVAL, "reject short excl_prog_hash_size");

	o.excl_prog_hash = hash;
	o.excl_prog_hash_size = SHA256_DIGEST_SIZE * 2;
	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
	if (fd >= 0)
		close(fd);
	ASSERT_EQ(fd, -EINVAL, "reject long excl_prog_hash_size");

	o.excl_prog_hash = hash;
	o.excl_prog_hash_size = 0;
	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
	if (fd >= 0)
		close(fd);
	ASSERT_EQ(fd, -EINVAL, "reject hash pointer with zero size");

	o.excl_prog_hash = NULL;
	o.excl_prog_hash_size = SHA256_DIGEST_SIZE;
	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
	if (fd >= 0)
		close(fd);
	ASSERT_EQ(fd, -EINVAL, "reject size with NULL hash pointer");
}

void test_map_excl(void)
{
	if (test__start_subtest("map_excl_allowed"))
		test_map_excl_allowed();
	if (test__start_subtest("map_excl_denied"))
		test_map_excl_denied();
	if (test__start_subtest("map_excl_no_map_in_map"))
		test_map_excl_no_map_in_map();
	if (test__start_subtest("map_excl_no_map_iter"))
		test_map_excl_no_map_iter();
	if (test__start_subtest("map_excl_create_validation"))
		test_map_excl_create_validation();
}