Skip to content

Commit 43dd115

Browse files
aspskAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add tests for write-only stacks/queues
For write-only stacks and queues bpf_map_update_elem should be allowed, but bpf_map_lookup_elem and bpf_map_lookup_and_delete_elem should fail with EPERM. Signed-off-by: Anton Protopopov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1ea0f91 commit 43dd115

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tools/testing/selftests/bpf/test_maps.c

+39-1
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ static void test_map_rdonly(void)
14051405
close(fd);
14061406
}
14071407

1408-
static void test_map_wronly(void)
1408+
static void test_map_wronly_hash(void)
14091409
{
14101410
int fd, key = 0, value = 0;
14111411

@@ -1429,6 +1429,44 @@ static void test_map_wronly(void)
14291429
close(fd);
14301430
}
14311431

1432+
static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type)
1433+
{
1434+
int fd, value = 0;
1435+
1436+
assert(map_type == BPF_MAP_TYPE_QUEUE ||
1437+
map_type == BPF_MAP_TYPE_STACK);
1438+
fd = bpf_create_map(map_type, 0, sizeof(value), MAP_SIZE,
1439+
map_flags | BPF_F_WRONLY);
1440+
/* Stack/Queue maps do not support BPF_F_NO_PREALLOC */
1441+
if (map_flags & BPF_F_NO_PREALLOC) {
1442+
assert(fd < 0 && errno == EINVAL);
1443+
return;
1444+
}
1445+
if (fd < 0) {
1446+
printf("Failed to create map '%s'!\n", strerror(errno));
1447+
exit(1);
1448+
}
1449+
1450+
value = 1234;
1451+
assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0);
1452+
1453+
/* Peek element should fail */
1454+
assert(bpf_map_lookup_elem(fd, NULL, &value) == -1 && errno == EPERM);
1455+
1456+
/* Pop element should fail */
1457+
assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) == -1 &&
1458+
errno == EPERM);
1459+
1460+
close(fd);
1461+
}
1462+
1463+
static void test_map_wronly(void)
1464+
{
1465+
test_map_wronly_hash();
1466+
test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK);
1467+
test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE);
1468+
}
1469+
14321470
static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
14331471
__s64 *fds64, __u64 *sk_cookies,
14341472
unsigned int n)

0 commit comments

Comments
 (0)