|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <sys/types.h> |
| 4 | +#include <sys/socket.h> |
| 5 | +#include <test_progs.h> |
| 6 | + |
| 7 | +#include "lsm_cgroup.skel.h" |
| 8 | +#include "cgroup_helpers.h" |
| 9 | +#include "network_helpers.h" |
| 10 | + |
| 11 | +void test_lsm_cgroup(void) |
| 12 | +{ |
| 13 | + DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, attach_opts); |
| 14 | + DECLARE_LIBBPF_OPTS(bpf_link_update_opts, update_opts); |
| 15 | + int cgroup_fd, cgroup_fd2, err, fd, prio; |
| 16 | + int listen_fd, client_fd, accepted_fd; |
| 17 | + struct lsm_cgroup *skel = NULL; |
| 18 | + int post_create_prog_fd2 = -1; |
| 19 | + int post_create_prog_fd = -1; |
| 20 | + int bind_link_fd2 = -1; |
| 21 | + int bind_prog_fd2 = -1; |
| 22 | + int alloc_prog_fd = -1; |
| 23 | + int bind_prog_fd = -1; |
| 24 | + int bind_link_fd = -1; |
| 25 | + int clone_prog_fd = -1; |
| 26 | + socklen_t socklen; |
| 27 | + |
| 28 | + cgroup_fd = test__join_cgroup("/sock_policy"); |
| 29 | + if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup")) |
| 30 | + goto close_skel; |
| 31 | + |
| 32 | + cgroup_fd2 = create_and_get_cgroup("/sock_policy2"); |
| 33 | + if (!ASSERT_GE(cgroup_fd2, 0, "create second cgroup")) |
| 34 | + goto close_skel; |
| 35 | + |
| 36 | + skel = lsm_cgroup__open_and_load(); |
| 37 | + if (!ASSERT_OK_PTR(skel, "open_and_load")) |
| 38 | + goto close_cgroup; |
| 39 | + |
| 40 | + post_create_prog_fd = bpf_program__fd(skel->progs.socket_post_create); |
| 41 | + post_create_prog_fd2 = bpf_program__fd(skel->progs.socket_post_create2); |
| 42 | + bind_prog_fd = bpf_program__fd(skel->progs.socket_bind); |
| 43 | + bind_prog_fd2 = bpf_program__fd(skel->progs.socket_bind2); |
| 44 | + alloc_prog_fd = bpf_program__fd(skel->progs.socket_alloc); |
| 45 | + clone_prog_fd = bpf_program__fd(skel->progs.socket_clone); |
| 46 | + |
| 47 | + err = bpf_prog_attach(alloc_prog_fd, cgroup_fd, BPF_LSM_CGROUP, 0); |
| 48 | + if (!ASSERT_OK(err, "attach alloc_prog_fd")) |
| 49 | + goto detach_cgroup; |
| 50 | + |
| 51 | + err = bpf_prog_attach(clone_prog_fd, cgroup_fd, BPF_LSM_CGROUP, 0); |
| 52 | + if (!ASSERT_OK(err, "attach clone_prog_fd")) |
| 53 | + goto detach_cgroup; |
| 54 | + |
| 55 | + /* Make sure replacing works. |
| 56 | + */ |
| 57 | + |
| 58 | + err = bpf_prog_attach(post_create_prog_fd, cgroup_fd, |
| 59 | + BPF_LSM_CGROUP, 0); |
| 60 | + if (!ASSERT_OK(err, "attach post_create_prog_fd")) |
| 61 | + goto close_cgroup; |
| 62 | + |
| 63 | + attach_opts.replace_prog_fd = post_create_prog_fd; |
| 64 | + err = bpf_prog_attach_opts(post_create_prog_fd2, cgroup_fd, |
| 65 | + BPF_LSM_CGROUP, &attach_opts); |
| 66 | + if (!ASSERT_OK(err, "prog replace post_create_prog_fd")) |
| 67 | + goto detach_cgroup; |
| 68 | + |
| 69 | + /* Try the same attach/replace via link API. |
| 70 | + */ |
| 71 | + |
| 72 | + bind_link_fd = bpf_link_create(bind_prog_fd, cgroup_fd, |
| 73 | + BPF_LSM_CGROUP, NULL); |
| 74 | + if (!ASSERT_GE(bind_link_fd, 0, "link create bind_prog_fd")) |
| 75 | + goto detach_cgroup; |
| 76 | + |
| 77 | + update_opts.old_prog_fd = bind_prog_fd; |
| 78 | + update_opts.flags = BPF_F_REPLACE; |
| 79 | + |
| 80 | + err = bpf_link_update(bind_link_fd, bind_prog_fd2, &update_opts); |
| 81 | + if (!ASSERT_OK(err, "link update bind_prog_fd")) |
| 82 | + goto detach_cgroup; |
| 83 | + |
| 84 | + /* Attach another instance of bind program to another cgroup. |
| 85 | + * This should trigger the reuse of the trampoline shim (two |
| 86 | + * programs attaching to the same btf_id). |
| 87 | + */ |
| 88 | + |
| 89 | + bind_link_fd2 = bpf_link_create(bind_prog_fd2, cgroup_fd2, |
| 90 | + BPF_LSM_CGROUP, NULL); |
| 91 | + if (!ASSERT_GE(bind_link_fd2, 0, "link create bind_prog_fd2")) |
| 92 | + goto detach_cgroup; |
| 93 | + |
| 94 | + /* AF_UNIX is prohibited. |
| 95 | + */ |
| 96 | + |
| 97 | + fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 98 | + ASSERT_LT(fd, 0, "socket(AF_UNIX)"); |
| 99 | + |
| 100 | + /* AF_INET6 gets default policy (sk_priority). |
| 101 | + */ |
| 102 | + |
| 103 | + fd = socket(AF_INET6, SOCK_STREAM, 0); |
| 104 | + if (!ASSERT_GE(fd, 0, "socket(SOCK_STREAM)")) |
| 105 | + goto detach_cgroup; |
| 106 | + |
| 107 | + prio = 0; |
| 108 | + socklen = sizeof(prio); |
| 109 | + ASSERT_GE(getsockopt(fd, SOL_SOCKET, SO_PRIORITY, &prio, &socklen), 0, |
| 110 | + "getsockopt"); |
| 111 | + ASSERT_EQ(prio, 123, "sk_priority"); |
| 112 | + |
| 113 | + close(fd); |
| 114 | + |
| 115 | + /* TX-only AF_PACKET is allowed. |
| 116 | + */ |
| 117 | + |
| 118 | + ASSERT_LT(socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)), 0, |
| 119 | + "socket(AF_PACKET, ..., ETH_P_ALL)"); |
| 120 | + |
| 121 | + fd = socket(AF_PACKET, SOCK_RAW, 0); |
| 122 | + ASSERT_GE(fd, 0, "socket(AF_PACKET, ..., 0)"); |
| 123 | + |
| 124 | + /* TX-only AF_PACKET can not be rebound. |
| 125 | + */ |
| 126 | + |
| 127 | + struct sockaddr_ll sa = { |
| 128 | + .sll_family = AF_PACKET, |
| 129 | + .sll_protocol = htons(ETH_P_ALL), |
| 130 | + }; |
| 131 | + ASSERT_LT(bind(fd, (struct sockaddr *)&sa, sizeof(sa)), 0, |
| 132 | + "bind(ETH_P_ALL)"); |
| 133 | + |
| 134 | + close(fd); |
| 135 | + |
| 136 | + /* Trigger passive open. |
| 137 | + */ |
| 138 | + |
| 139 | + listen_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0); |
| 140 | + ASSERT_GE(listen_fd, 0, "start_server"); |
| 141 | + client_fd = connect_to_fd(listen_fd, 0); |
| 142 | + ASSERT_GE(client_fd, 0, "connect_to_fd"); |
| 143 | + accepted_fd = accept(listen_fd, NULL, NULL); |
| 144 | + ASSERT_GE(accepted_fd, 0, "accept"); |
| 145 | + |
| 146 | + prio = 0; |
| 147 | + socklen = sizeof(prio); |
| 148 | + ASSERT_GE(getsockopt(accepted_fd, SOL_SOCKET, SO_PRIORITY, &prio, &socklen), 0, |
| 149 | + "getsockopt"); |
| 150 | + ASSERT_EQ(prio, 234, "sk_priority"); |
| 151 | + |
| 152 | + /* These are replaced and never called. */ |
| 153 | + ASSERT_EQ(skel->bss->called_socket_post_create, 0, "called_create"); |
| 154 | + ASSERT_EQ(skel->bss->called_socket_bind, 0, "called_bind"); |
| 155 | + |
| 156 | + /* AF_INET6+SOCK_STREAM |
| 157 | + * AF_PACKET+SOCK_RAW |
| 158 | + * listen_fd |
| 159 | + * client_fd |
| 160 | + * accepted_fd |
| 161 | + */ |
| 162 | + ASSERT_EQ(skel->bss->called_socket_post_create2, 5, "called_create2"); |
| 163 | + |
| 164 | + /* start_server |
| 165 | + * bind(ETH_P_ALL) |
| 166 | + */ |
| 167 | + ASSERT_EQ(skel->bss->called_socket_bind2, 2, "called_bind2"); |
| 168 | + /* Single accept(). */ |
| 169 | + ASSERT_EQ(skel->bss->called_socket_clone, 1, "called_clone"); |
| 170 | + |
| 171 | + /* AF_UNIX+SOCK_STREAM (failed) |
| 172 | + * AF_INET6+SOCK_STREAM |
| 173 | + * AF_PACKET+SOCK_RAW (failed) |
| 174 | + * AF_PACKET+SOCK_RAW |
| 175 | + * listen_fd |
| 176 | + * client_fd |
| 177 | + * accepted_fd |
| 178 | + */ |
| 179 | + ASSERT_EQ(skel->bss->called_socket_alloc, 7, "called_alloc"); |
| 180 | + |
| 181 | + /* Make sure other cgroup doesn't trigger the programs. |
| 182 | + */ |
| 183 | + |
| 184 | + if (!ASSERT_OK(join_cgroup(""), "join root cgroup")) |
| 185 | + goto detach_cgroup; |
| 186 | + |
| 187 | + fd = socket(AF_INET6, SOCK_STREAM, 0); |
| 188 | + if (!ASSERT_GE(fd, 0, "socket(SOCK_STREAM)")) |
| 189 | + goto detach_cgroup; |
| 190 | + |
| 191 | + prio = 0; |
| 192 | + socklen = sizeof(prio); |
| 193 | + ASSERT_GE(getsockopt(fd, SOL_SOCKET, SO_PRIORITY, &prio, &socklen), 0, |
| 194 | + "getsockopt"); |
| 195 | + ASSERT_EQ(prio, 0, "sk_priority"); |
| 196 | + |
| 197 | + close(fd); |
| 198 | + |
| 199 | +detach_cgroup: |
| 200 | + ASSERT_GE(bpf_prog_detach2(post_create_prog_fd2, cgroup_fd, |
| 201 | + BPF_LSM_CGROUP), 0, "detach_create"); |
| 202 | + close(bind_link_fd); |
| 203 | + /* Don't close bind_link_fd2, exercise cgroup release cleanup. */ |
| 204 | + ASSERT_GE(bpf_prog_detach2(alloc_prog_fd, cgroup_fd, |
| 205 | + BPF_LSM_CGROUP), 0, "detach_alloc"); |
| 206 | + ASSERT_GE(bpf_prog_detach2(clone_prog_fd, cgroup_fd, |
| 207 | + BPF_LSM_CGROUP), 0, "detach_clone"); |
| 208 | + |
| 209 | +close_cgroup: |
| 210 | + close(cgroup_fd); |
| 211 | +close_skel: |
| 212 | + lsm_cgroup__destroy(skel); |
| 213 | +} |
0 commit comments