1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 #include <test_progs.h> 4 #include <network_helpers.h> 5 #include <bpf/btf.h> 6 7 typedef int (*test_cb)(struct bpf_object *obj); 8 9 static int check_data_map(struct bpf_object *obj, int prog_cnt, bool reset) 10 { 11 struct bpf_map *data_map = NULL, *map; 12 __u64 *result = NULL; 13 const int zero = 0; 14 __u32 duration = 0; 15 int ret = -1, i; 16 17 result = malloc((prog_cnt + 32 /* spare */) * sizeof(__u64)); 18 if (CHECK(!result, "alloc_memory", "failed to alloc memory")) 19 return -ENOMEM; 20 21 bpf_object__for_each_map(map, obj) 22 if (bpf_map__is_internal(map)) { 23 data_map = map; 24 break; 25 } 26 if (CHECK(!data_map, "find_data_map", "data map not found\n")) 27 goto out; 28 29 ret = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, result); 30 if (CHECK(ret, "get_result", 31 "failed to get output data: %d\n", ret)) 32 goto out; 33 34 for (i = 0; i < prog_cnt; i++) { 35 if (CHECK(result[i] != 1, "result", 36 "fexit_bpf2bpf result[%d] failed err %llu\n", 37 i, result[i])) 38 goto out; 39 result[i] = 0; 40 } 41 if (reset) { 42 ret = bpf_map_update_elem(bpf_map__fd(data_map), &zero, result, 0); 43 if (CHECK(ret, "reset_result", "failed to reset result\n")) 44 goto out; 45 } 46 47 ret = 0; 48 out: 49 free(result); 50 return ret; 51 } 52 53 static void test_fexit_bpf2bpf_common(const char *obj_file, 54 const char *target_obj_file, 55 int prog_cnt, 56 const char **prog_name, 57 bool run_prog, 58 test_cb cb) 59 { 60 struct bpf_object *obj = NULL, *tgt_obj; 61 __u32 retval, tgt_prog_id, info_len; 62 struct bpf_prog_info prog_info = {}; 63 struct bpf_program **prog = NULL, *p; 64 struct bpf_link **link = NULL; 65 int err, tgt_fd, i; 66 struct btf *btf; 67 68 err = bpf_prog_test_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 69 &tgt_obj, &tgt_fd); 70 if (!ASSERT_OK(err, "tgt_prog_load")) 71 return; 72 73 info_len = sizeof(prog_info); 74 err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len); 75 if (!ASSERT_OK(err, "tgt_fd_get_info")) 76 goto close_prog; 77 78 tgt_prog_id = prog_info.id; 79 btf = bpf_object__btf(tgt_obj); 80 81 link = calloc(sizeof(struct bpf_link *), prog_cnt); 82 if (!ASSERT_OK_PTR(link, "link_ptr")) 83 goto close_prog; 84 85 prog = calloc(sizeof(struct bpf_program *), prog_cnt); 86 if (!ASSERT_OK_PTR(prog, "prog_ptr")) 87 goto close_prog; 88 89 obj = bpf_object__open_file(obj_file, NULL); 90 if (!ASSERT_OK_PTR(obj, "obj_open")) 91 goto close_prog; 92 93 bpf_object__for_each_program(p, obj) { 94 err = bpf_program__set_attach_target(p, tgt_fd, NULL); 95 ASSERT_OK(err, "set_attach_target"); 96 } 97 98 err = bpf_object__load(obj); 99 if (!ASSERT_OK(err, "obj_load")) 100 goto close_prog; 101 102 for (i = 0; i < prog_cnt; i++) { 103 struct bpf_link_info link_info; 104 struct bpf_program *pos; 105 const char *pos_sec_name; 106 char *tgt_name; 107 __s32 btf_id; 108 109 tgt_name = strstr(prog_name[i], "/"); 110 if (!ASSERT_OK_PTR(tgt_name, "tgt_name")) 111 goto close_prog; 112 btf_id = btf__find_by_name_kind(btf, tgt_name + 1, BTF_KIND_FUNC); 113 114 prog[i] = NULL; 115 bpf_object__for_each_program(pos, obj) { 116 pos_sec_name = bpf_program__section_name(pos); 117 if (pos_sec_name && !strcmp(pos_sec_name, prog_name[i])) { 118 prog[i] = pos; 119 break; 120 } 121 } 122 if (!ASSERT_OK_PTR(prog[i], prog_name[i])) 123 goto close_prog; 124 125 link[i] = bpf_program__attach_trace(prog[i]); 126 if (!ASSERT_OK_PTR(link[i], "attach_trace")) 127 goto close_prog; 128 129 info_len = sizeof(link_info); 130 memset(&link_info, 0, sizeof(link_info)); 131 err = bpf_obj_get_info_by_fd(bpf_link__fd(link[i]), 132 &link_info, &info_len); 133 ASSERT_OK(err, "link_fd_get_info"); 134 ASSERT_EQ(link_info.tracing.attach_type, 135 bpf_program__get_expected_attach_type(prog[i]), 136 "link_attach_type"); 137 ASSERT_EQ(link_info.tracing.target_obj_id, tgt_prog_id, "link_tgt_obj_id"); 138 ASSERT_EQ(link_info.tracing.target_btf_id, btf_id, "link_tgt_btf_id"); 139 } 140 141 if (cb) { 142 err = cb(obj); 143 if (err) 144 goto close_prog; 145 } 146 147 if (!run_prog) 148 goto close_prog; 149 150 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 151 NULL, NULL, &retval, NULL); 152 ASSERT_OK(err, "prog_run"); 153 ASSERT_EQ(retval, 0, "prog_run_ret"); 154 155 if (check_data_map(obj, prog_cnt, false)) 156 goto close_prog; 157 158 close_prog: 159 for (i = 0; i < prog_cnt; i++) 160 bpf_link__destroy(link[i]); 161 bpf_object__close(obj); 162 bpf_object__close(tgt_obj); 163 free(link); 164 free(prog); 165 } 166 167 static void test_target_no_callees(void) 168 { 169 const char *prog_name[] = { 170 "fexit/test_pkt_md_access", 171 }; 172 test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o", 173 "./test_pkt_md_access.o", 174 ARRAY_SIZE(prog_name), 175 prog_name, true, NULL); 176 } 177 178 static void test_target_yes_callees(void) 179 { 180 const char *prog_name[] = { 181 "fexit/test_pkt_access", 182 "fexit/test_pkt_access_subprog1", 183 "fexit/test_pkt_access_subprog2", 184 "fexit/test_pkt_access_subprog3", 185 }; 186 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 187 "./test_pkt_access.o", 188 ARRAY_SIZE(prog_name), 189 prog_name, true, NULL); 190 } 191 192 static void test_func_replace(void) 193 { 194 const char *prog_name[] = { 195 "fexit/test_pkt_access", 196 "fexit/test_pkt_access_subprog1", 197 "fexit/test_pkt_access_subprog2", 198 "fexit/test_pkt_access_subprog3", 199 "freplace/get_skb_len", 200 "freplace/get_skb_ifindex", 201 "freplace/get_constant", 202 "freplace/test_pkt_write_access_subprog", 203 }; 204 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 205 "./test_pkt_access.o", 206 ARRAY_SIZE(prog_name), 207 prog_name, true, NULL); 208 } 209 210 static void test_func_replace_verify(void) 211 { 212 const char *prog_name[] = { 213 "freplace/do_bind", 214 }; 215 test_fexit_bpf2bpf_common("./freplace_connect4.o", 216 "./connect4_prog.o", 217 ARRAY_SIZE(prog_name), 218 prog_name, false, NULL); 219 } 220 221 static int test_second_attach(struct bpf_object *obj) 222 { 223 const char *prog_name = "security_new_get_constant"; 224 const char *tgt_name = "get_constant"; 225 const char *tgt_obj_file = "./test_pkt_access.o"; 226 struct bpf_program *prog = NULL; 227 struct bpf_object *tgt_obj; 228 __u32 duration = 0, retval; 229 struct bpf_link *link; 230 int err = 0, tgt_fd; 231 232 prog = bpf_object__find_program_by_name(obj, prog_name); 233 if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name)) 234 return -ENOENT; 235 236 err = bpf_prog_test_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC, 237 &tgt_obj, &tgt_fd); 238 if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n", 239 tgt_obj_file, err, errno)) 240 return err; 241 242 link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name); 243 if (!ASSERT_OK_PTR(link, "second_link")) 244 goto out; 245 246 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 247 NULL, NULL, &retval, &duration); 248 if (CHECK(err || retval, "ipv6", 249 "err %d errno %d retval %d duration %d\n", 250 err, errno, retval, duration)) 251 goto out; 252 253 err = check_data_map(obj, 1, true); 254 if (err) 255 goto out; 256 257 out: 258 bpf_link__destroy(link); 259 bpf_object__close(tgt_obj); 260 return err; 261 } 262 263 static void test_func_replace_multi(void) 264 { 265 const char *prog_name[] = { 266 "freplace/get_constant", 267 }; 268 test_fexit_bpf2bpf_common("./freplace_get_constant.o", 269 "./test_pkt_access.o", 270 ARRAY_SIZE(prog_name), 271 prog_name, true, test_second_attach); 272 } 273 274 static void test_fmod_ret_freplace(void) 275 { 276 struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL; 277 const char *freplace_name = "./freplace_get_constant.o"; 278 const char *fmod_ret_name = "./fmod_ret_freplace.o"; 279 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); 280 const char *tgt_name = "./test_pkt_access.o"; 281 struct bpf_link *freplace_link = NULL; 282 struct bpf_program *prog; 283 __u32 duration = 0; 284 int err, pkt_fd, attach_prog_fd; 285 286 err = bpf_prog_test_load(tgt_name, BPF_PROG_TYPE_UNSPEC, 287 &pkt_obj, &pkt_fd); 288 /* the target prog should load fine */ 289 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 290 tgt_name, err, errno)) 291 return; 292 293 freplace_obj = bpf_object__open_file(freplace_name, NULL); 294 if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open")) 295 goto out; 296 297 prog = bpf_object__next_program(freplace_obj, NULL); 298 err = bpf_program__set_attach_target(prog, pkt_fd, NULL); 299 ASSERT_OK(err, "freplace__set_attach_target"); 300 301 err = bpf_object__load(freplace_obj); 302 if (CHECK(err, "freplace_obj_load", "err %d\n", err)) 303 goto out; 304 305 freplace_link = bpf_program__attach_trace(prog); 306 if (!ASSERT_OK_PTR(freplace_link, "freplace_attach_trace")) 307 goto out; 308 309 fmod_obj = bpf_object__open_file(fmod_ret_name, NULL); 310 if (!ASSERT_OK_PTR(fmod_obj, "fmod_obj_open")) 311 goto out; 312 313 attach_prog_fd = bpf_program__fd(prog); 314 prog = bpf_object__next_program(fmod_obj, NULL); 315 err = bpf_program__set_attach_target(prog, attach_prog_fd, NULL); 316 ASSERT_OK(err, "fmod_ret_set_attach_target"); 317 318 err = bpf_object__load(fmod_obj); 319 if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n")) 320 goto out; 321 322 out: 323 bpf_link__destroy(freplace_link); 324 bpf_object__close(freplace_obj); 325 bpf_object__close(fmod_obj); 326 bpf_object__close(pkt_obj); 327 } 328 329 330 static void test_func_sockmap_update(void) 331 { 332 const char *prog_name[] = { 333 "freplace/cls_redirect", 334 }; 335 test_fexit_bpf2bpf_common("./freplace_cls_redirect.o", 336 "./test_cls_redirect.o", 337 ARRAY_SIZE(prog_name), 338 prog_name, false, NULL); 339 } 340 341 static void test_obj_load_failure_common(const char *obj_file, 342 const char *target_obj_file) 343 { 344 /* 345 * standalone test that asserts failure to load freplace prog 346 * because of invalid return code. 347 */ 348 struct bpf_object *obj = NULL, *pkt_obj; 349 struct bpf_program *prog; 350 int err, pkt_fd; 351 __u32 duration = 0; 352 353 err = bpf_prog_test_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 354 &pkt_obj, &pkt_fd); 355 /* the target prog should load fine */ 356 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 357 target_obj_file, err, errno)) 358 return; 359 360 obj = bpf_object__open_file(obj_file, NULL); 361 if (!ASSERT_OK_PTR(obj, "obj_open")) 362 goto close_prog; 363 364 prog = bpf_object__next_program(obj, NULL); 365 err = bpf_program__set_attach_target(prog, pkt_fd, NULL); 366 ASSERT_OK(err, "set_attach_target"); 367 368 /* It should fail to load the program */ 369 err = bpf_object__load(obj); 370 if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err)) 371 goto close_prog; 372 373 close_prog: 374 bpf_object__close(obj); 375 bpf_object__close(pkt_obj); 376 } 377 378 static void test_func_replace_return_code(void) 379 { 380 /* test invalid return code in the replaced program */ 381 test_obj_load_failure_common("./freplace_connect_v4_prog.o", 382 "./connect4_prog.o"); 383 } 384 385 static void test_func_map_prog_compatibility(void) 386 { 387 /* test with spin lock map value in the replaced program */ 388 test_obj_load_failure_common("./freplace_attach_probe.o", 389 "./test_attach_probe.o"); 390 } 391 392 /* NOTE: affect other tests, must run in serial mode */ 393 void serial_test_fexit_bpf2bpf(void) 394 { 395 if (test__start_subtest("target_no_callees")) 396 test_target_no_callees(); 397 if (test__start_subtest("target_yes_callees")) 398 test_target_yes_callees(); 399 if (test__start_subtest("func_replace")) 400 test_func_replace(); 401 if (test__start_subtest("func_replace_verify")) 402 test_func_replace_verify(); 403 if (test__start_subtest("func_sockmap_update")) 404 test_func_sockmap_update(); 405 if (test__start_subtest("func_replace_return_code")) 406 test_func_replace_return_code(); 407 if (test__start_subtest("func_map_prog_compatibility")) 408 test_func_map_prog_compatibility(); 409 if (test__start_subtest("func_replace_multi")) 410 test_func_replace_multi(); 411 if (test__start_subtest("fmod_ret_freplace")) 412 test_fmod_ret_freplace(); 413 } 414