Skip to content

Commit

Permalink
selftests/bpf: Add write tests for sk local storage map iterator
Browse files Browse the repository at this point in the history
Add test to validate the overwrite of sock local storage map value in
map iterator and another one to ensure out-of-bound value writing is
rejected.

Signed-off-by: Hou Tao <[email protected]>
Acked-by: Yonghong Song <[email protected]>
Acked-by: Martin KaFai Lau <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
Hou Tao authored and Alexei Starovoitov committed Aug 10, 2022
1 parent 5836d81 commit 939a1a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 18 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/bpf_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ static void test_bpf_sk_stoarge_map_iter_fd(void)
if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_sk_storage_map__open_and_load"))
return;

do_read_map_iter_fd(&skel->skeleton, skel->progs.dump_bpf_sk_storage_map,
do_read_map_iter_fd(&skel->skeleton, skel->progs.rw_bpf_sk_storage_map,
skel->maps.sk_stg_map);

bpf_iter_bpf_sk_storage_map__destroy(skel);
Expand Down Expand Up @@ -1117,14 +1117,23 @@ static void test_bpf_sk_storage_map(void)
linfo.map.map_fd = map_fd;
opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_sk_storage_map, &opts);
link = bpf_program__attach_iter(skel->progs.oob_write_bpf_sk_storage_map, &opts);
err = libbpf_get_error(link);
if (!ASSERT_EQ(err, -EACCES, "attach_oob_write_iter")) {
if (!err)
bpf_link__destroy(link);
goto out;
}

link = bpf_program__attach_iter(skel->progs.rw_bpf_sk_storage_map, &opts);
if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out;

iter_fd = bpf_iter_create(bpf_link__fd(link));
if (!ASSERT_GE(iter_fd, 0, "create_iter"))
goto free_link;

skel->bss->to_add_val = time(NULL);
/* do some tests */
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
;
Expand All @@ -1138,6 +1147,13 @@ static void test_bpf_sk_storage_map(void)
if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
goto close_iter;

for (i = 0; i < num_sockets; i++) {
err = bpf_map_lookup_elem(map_fd, &sock_fd[i], &val);
if (!ASSERT_OK(err, "map_lookup") ||
!ASSERT_EQ(val, i + 1 + skel->bss->to_add_val, "check_map_value"))
break;
}

close_iter:
close(iter_fd);
free_link:
Expand Down
22 changes: 20 additions & 2 deletions tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@ struct {

__u32 val_sum = 0;
__u32 ipv6_sk_count = 0;
__u32 to_add_val = 0;

SEC("iter/bpf_sk_storage_map")
int dump_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
int rw_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
{
struct sock *sk = ctx->sk;
__u32 *val = ctx->value;

if (sk == (void *)0 || val == (void *)0)
if (sk == NULL || val == NULL)
return 0;

if (sk->sk_family == AF_INET6)
ipv6_sk_count++;

val_sum += *val;

*val += to_add_val;

return 0;
}

SEC("iter/bpf_sk_storage_map")
int oob_write_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
{
struct sock *sk = ctx->sk;
__u32 *val = ctx->value;

if (sk == NULL || val == NULL)
return 0;

*(val + 1) = 0xdeadbeef;

return 0;
}

0 comments on commit 939a1a9

Please sign in to comment.