|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (C) 2023 Yafang Shao <[email protected]> */ |
| 3 | + |
| 4 | +#include <sys/types.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <test_progs.h> |
| 7 | +#include "cgroup_helpers.h" |
| 8 | +#include "test_cgroup1_hierarchy.skel.h" |
| 9 | + |
| 10 | +static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel) |
| 11 | +{ |
| 12 | + struct bpf_link *lsm_link, *fentry_link; |
| 13 | + int err; |
| 14 | + |
| 15 | + /* Attach LSM prog first */ |
| 16 | + lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run); |
| 17 | + if (!ASSERT_OK_PTR(lsm_link, "lsm_attach")) |
| 18 | + return; |
| 19 | + |
| 20 | + /* LSM prog will be triggered when attaching fentry */ |
| 21 | + fentry_link = bpf_program__attach_trace(skel->progs.fentry_run); |
| 22 | + ASSERT_NULL(fentry_link, "fentry_attach_fail"); |
| 23 | + |
| 24 | + err = bpf_link__destroy(lsm_link); |
| 25 | + ASSERT_OK(err, "destroy_lsm"); |
| 26 | +} |
| 27 | + |
| 28 | +static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel) |
| 29 | +{ |
| 30 | + struct bpf_link *lsm_link, *fentry_link; |
| 31 | + int err; |
| 32 | + |
| 33 | + /* Attach LSM prog first */ |
| 34 | + lsm_link = bpf_program__attach_lsm(skel->progs.lsm_s_run); |
| 35 | + if (!ASSERT_OK_PTR(lsm_link, "lsm_attach")) |
| 36 | + return; |
| 37 | + |
| 38 | + /* LSM prog will be triggered when attaching fentry */ |
| 39 | + fentry_link = bpf_program__attach_trace(skel->progs.fentry_run); |
| 40 | + ASSERT_NULL(fentry_link, "fentry_attach_fail"); |
| 41 | + |
| 42 | + err = bpf_link__destroy(lsm_link); |
| 43 | + ASSERT_OK(err, "destroy_lsm"); |
| 44 | +} |
| 45 | + |
| 46 | +static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel) |
| 47 | +{ |
| 48 | + struct bpf_link *lsm_link, *fentry_link; |
| 49 | + int err; |
| 50 | + |
| 51 | + /* Attach LSM prog first */ |
| 52 | + lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run); |
| 53 | + if (!ASSERT_OK_PTR(lsm_link, "lsm_attach")) |
| 54 | + return; |
| 55 | + |
| 56 | + /* LSM prog will be triggered when attaching fentry */ |
| 57 | + fentry_link = bpf_program__attach_trace(skel->progs.fentry_run); |
| 58 | + if (!ASSERT_OK_PTR(fentry_link, "fentry_attach_success")) |
| 59 | + goto cleanup; |
| 60 | + |
| 61 | + err = bpf_link__destroy(fentry_link); |
| 62 | + ASSERT_OK(err, "destroy_lsm"); |
| 63 | + |
| 64 | +cleanup: |
| 65 | + err = bpf_link__destroy(lsm_link); |
| 66 | + ASSERT_OK(err, "destroy_fentry"); |
| 67 | +} |
| 68 | + |
| 69 | +void test_cgroup1_hierarchy(void) |
| 70 | +{ |
| 71 | + struct test_cgroup1_hierarchy *skel; |
| 72 | + __u64 current_cgid; |
| 73 | + int hid, err; |
| 74 | + |
| 75 | + skel = test_cgroup1_hierarchy__open(); |
| 76 | + if (!ASSERT_OK_PTR(skel, "open")) |
| 77 | + return; |
| 78 | + |
| 79 | + skel->bss->target_pid = getpid(); |
| 80 | + |
| 81 | + err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1"); |
| 82 | + if (!ASSERT_OK(err, "fentry_set_target")) |
| 83 | + goto destroy; |
| 84 | + |
| 85 | + err = test_cgroup1_hierarchy__load(skel); |
| 86 | + if (!ASSERT_OK(err, "load")) |
| 87 | + goto destroy; |
| 88 | + |
| 89 | + /* Setup cgroup1 hierarchy */ |
| 90 | + err = setup_classid_environment(); |
| 91 | + if (!ASSERT_OK(err, "setup_classid_environment")) |
| 92 | + goto destroy; |
| 93 | + |
| 94 | + err = join_classid(); |
| 95 | + if (!ASSERT_OK(err, "join_cgroup1")) |
| 96 | + goto cleanup; |
| 97 | + |
| 98 | + current_cgid = get_classid_cgroup_id(); |
| 99 | + if (!ASSERT_GE(current_cgid, 0, "cgroup1 id")) |
| 100 | + goto cleanup; |
| 101 | + |
| 102 | + hid = get_cgroup1_hierarchy_id("net_cls"); |
| 103 | + if (!ASSERT_GE(hid, 0, "cgroup1 id")) |
| 104 | + goto cleanup; |
| 105 | + skel->bss->target_hid = hid; |
| 106 | + |
| 107 | + if (test__start_subtest("test_cgroup1_hierarchy")) { |
| 108 | + skel->bss->target_ancestor_cgid = current_cgid; |
| 109 | + bpf_cgroup1(skel); |
| 110 | + } |
| 111 | + |
| 112 | + if (test__start_subtest("test_root_cgid")) { |
| 113 | + skel->bss->target_ancestor_cgid = 1; |
| 114 | + skel->bss->target_ancestor_level = 0; |
| 115 | + bpf_cgroup1(skel); |
| 116 | + } |
| 117 | + |
| 118 | + if (test__start_subtest("test_invalid_level")) { |
| 119 | + skel->bss->target_ancestor_cgid = 1; |
| 120 | + skel->bss->target_ancestor_level = 1; |
| 121 | + bpf_cgroup1_invalid_id(skel); |
| 122 | + } |
| 123 | + |
| 124 | + if (test__start_subtest("test_invalid_cgid")) { |
| 125 | + skel->bss->target_ancestor_cgid = 0; |
| 126 | + bpf_cgroup1_invalid_id(skel); |
| 127 | + } |
| 128 | + |
| 129 | + if (test__start_subtest("test_invalid_hid")) { |
| 130 | + skel->bss->target_ancestor_cgid = 1; |
| 131 | + skel->bss->target_ancestor_level = 0; |
| 132 | + skel->bss->target_hid = -1; |
| 133 | + bpf_cgroup1_invalid_id(skel); |
| 134 | + } |
| 135 | + |
| 136 | + if (test__start_subtest("test_invalid_cgrp_name")) { |
| 137 | + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl"); |
| 138 | + skel->bss->target_ancestor_cgid = current_cgid; |
| 139 | + bpf_cgroup1_invalid_id(skel); |
| 140 | + } |
| 141 | + |
| 142 | + if (test__start_subtest("test_invalid_cgrp_name2")) { |
| 143 | + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,"); |
| 144 | + skel->bss->target_ancestor_cgid = current_cgid; |
| 145 | + bpf_cgroup1_invalid_id(skel); |
| 146 | + } |
| 147 | + |
| 148 | + if (test__start_subtest("test_sleepable_prog")) { |
| 149 | + skel->bss->target_hid = hid; |
| 150 | + skel->bss->target_ancestor_cgid = current_cgid; |
| 151 | + bpf_cgroup1_sleepable(skel); |
| 152 | + } |
| 153 | + |
| 154 | +cleanup: |
| 155 | + cleanup_classid_environment(); |
| 156 | +destroy: |
| 157 | + test_cgroup1_hierarchy__destroy(skel); |
| 158 | +} |
0 commit comments