1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ 3 4 #define _GNU_SOURCE 5 #include <cgroup_helpers.h> 6 #include <test_progs.h> 7 8 #include "cgrp_kfunc_failure.skel.h" 9 #include "cgrp_kfunc_success.skel.h" 10 11 static size_t log_buf_sz = 1 << 20; /* 1 MB */ 12 static char obj_log_buf[1048576]; 13 14 static struct cgrp_kfunc_success *open_load_cgrp_kfunc_skel(void) 15 { 16 struct cgrp_kfunc_success *skel; 17 int err; 18 19 skel = cgrp_kfunc_success__open(); 20 if (!ASSERT_OK_PTR(skel, "skel_open")) 21 return NULL; 22 23 skel->bss->pid = getpid(); 24 25 err = cgrp_kfunc_success__load(skel); 26 if (!ASSERT_OK(err, "skel_load")) 27 goto cleanup; 28 29 return skel; 30 31 cleanup: 32 cgrp_kfunc_success__destroy(skel); 33 return NULL; 34 } 35 36 static int mkdir_rm_test_dir(void) 37 { 38 int fd; 39 const char *cgrp_path = "cgrp_kfunc"; 40 41 fd = create_and_get_cgroup(cgrp_path); 42 if (!ASSERT_GT(fd, 0, "mkdir_cgrp_fd")) 43 return -1; 44 45 close(fd); 46 remove_cgroup(cgrp_path); 47 48 return 0; 49 } 50 51 static void run_success_test(const char *prog_name) 52 { 53 struct cgrp_kfunc_success *skel; 54 struct bpf_program *prog; 55 struct bpf_link *link = NULL; 56 57 skel = open_load_cgrp_kfunc_skel(); 58 if (!ASSERT_OK_PTR(skel, "open_load_skel")) 59 return; 60 61 if (!ASSERT_OK(skel->bss->err, "pre_mkdir_err")) 62 goto cleanup; 63 64 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 65 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 66 goto cleanup; 67 68 link = bpf_program__attach(prog); 69 if (!ASSERT_OK_PTR(link, "attached_link")) 70 goto cleanup; 71 72 ASSERT_EQ(skel->bss->invocations, 0, "pre_rmdir_count"); 73 if (!ASSERT_OK(mkdir_rm_test_dir(), "cgrp_mkdir")) 74 goto cleanup; 75 76 ASSERT_EQ(skel->bss->invocations, 1, "post_rmdir_count"); 77 ASSERT_OK(skel->bss->err, "post_rmdir_err"); 78 79 cleanup: 80 bpf_link__destroy(link); 81 cgrp_kfunc_success__destroy(skel); 82 } 83 84 static const char * const success_tests[] = { 85 "test_cgrp_acquire_release_argument", 86 "test_cgrp_acquire_leave_in_map", 87 "test_cgrp_xchg_release", 88 "test_cgrp_get_release", 89 "test_cgrp_get_ancestors", 90 }; 91 92 static struct { 93 const char *prog_name; 94 const char *expected_err_msg; 95 } failure_tests[] = { 96 {"cgrp_kfunc_acquire_untrusted", "R1 must be referenced or trusted"}, 97 {"cgrp_kfunc_acquire_fp", "arg#0 pointer type STRUCT cgroup must point"}, 98 {"cgrp_kfunc_acquire_unsafe_kretprobe", "reg type unsupported for arg#0 function"}, 99 {"cgrp_kfunc_acquire_trusted_walked", "R1 must be referenced or trusted"}, 100 {"cgrp_kfunc_acquire_null", "arg#0 pointer type STRUCT cgroup must point"}, 101 {"cgrp_kfunc_acquire_unreleased", "Unreleased reference"}, 102 {"cgrp_kfunc_get_non_kptr_param", "arg#0 expected pointer to map value"}, 103 {"cgrp_kfunc_get_non_kptr_acquired", "arg#0 expected pointer to map value"}, 104 {"cgrp_kfunc_get_null", "arg#0 expected pointer to map value"}, 105 {"cgrp_kfunc_xchg_unreleased", "Unreleased reference"}, 106 {"cgrp_kfunc_get_unreleased", "Unreleased reference"}, 107 {"cgrp_kfunc_release_untrusted", "arg#0 is untrusted_ptr_or_null_ expected ptr_ or socket"}, 108 {"cgrp_kfunc_release_fp", "arg#0 pointer type STRUCT cgroup must point"}, 109 {"cgrp_kfunc_release_null", "arg#0 is ptr_or_null_ expected ptr_ or socket"}, 110 {"cgrp_kfunc_release_unacquired", "release kernel function bpf_cgroup_release expects"}, 111 }; 112 113 static void verify_fail(const char *prog_name, const char *expected_err_msg) 114 { 115 LIBBPF_OPTS(bpf_object_open_opts, opts); 116 struct cgrp_kfunc_failure *skel; 117 int err, i; 118 119 opts.kernel_log_buf = obj_log_buf; 120 opts.kernel_log_size = log_buf_sz; 121 opts.kernel_log_level = 1; 122 123 skel = cgrp_kfunc_failure__open_opts(&opts); 124 if (!ASSERT_OK_PTR(skel, "cgrp_kfunc_failure__open_opts")) 125 goto cleanup; 126 127 for (i = 0; i < ARRAY_SIZE(failure_tests); i++) { 128 struct bpf_program *prog; 129 const char *curr_name = failure_tests[i].prog_name; 130 131 prog = bpf_object__find_program_by_name(skel->obj, curr_name); 132 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 133 goto cleanup; 134 135 bpf_program__set_autoload(prog, !strcmp(curr_name, prog_name)); 136 } 137 138 err = cgrp_kfunc_failure__load(skel); 139 if (!ASSERT_ERR(err, "unexpected load success")) 140 goto cleanup; 141 142 if (!ASSERT_OK_PTR(strstr(obj_log_buf, expected_err_msg), "expected_err_msg")) { 143 fprintf(stderr, "Expected err_msg: %s\n", expected_err_msg); 144 fprintf(stderr, "Verifier output: %s\n", obj_log_buf); 145 } 146 147 cleanup: 148 cgrp_kfunc_failure__destroy(skel); 149 } 150 151 void test_cgrp_kfunc(void) 152 { 153 int i, err; 154 155 err = setup_cgroup_environment(); 156 if (!ASSERT_OK(err, "cgrp_env_setup")) 157 goto cleanup; 158 159 for (i = 0; i < ARRAY_SIZE(success_tests); i++) { 160 if (!test__start_subtest(success_tests[i])) 161 continue; 162 163 run_success_test(success_tests[i]); 164 } 165 166 for (i = 0; i < ARRAY_SIZE(failure_tests); i++) { 167 if (!test__start_subtest(failure_tests[i].prog_name)) 168 continue; 169 170 verify_fail(failure_tests[i].prog_name, failure_tests[i].expected_err_msg); 171 } 172 173 cleanup: 174 cleanup_cgroup_environment(); 175 } 176