|
| 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 | +} |
0 commit comments