| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "test-deps.h" |
| 22 |
#include "parse_util.h" |
| 23 |
#include "helpers.h" |
| 24 |
|
| 25 |
#include <sepol/policydb/policydb.h> |
| 26 |
#include <sepol/policydb/link.h> |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#include <sepol/debug.h> |
| 67 |
#include <sepol/handle.h> |
| 68 |
|
| 69 |
#define BASE_MODREQ_TYPE_GLOBAL 0 |
| 70 |
#define BASE_MODREQ_ATTR_GLOBAL 1 |
| 71 |
#define BASE_MODREQ_OBJ_GLOBAL 2 |
| 72 |
#define BASE_MODREQ_BOOL_GLOBAL 3 |
| 73 |
#define BASE_MODREQ_ROLE_GLOBAL 4 |
| 74 |
#define BASE_MODREQ_PERM_GLOBAL 5 |
| 75 |
#define BASE_MODREQ_TYPE_OPT 6 |
| 76 |
#define BASE_MODREQ_ATTR_OPT 7 |
| 77 |
#define BASE_MODREQ_OBJ_OPT 8 |
| 78 |
#define BASE_MODREQ_BOOL_OPT 9 |
| 79 |
#define BASE_MODREQ_ROLE_OPT 10 |
| 80 |
#define BASE_MODREQ_PERM_OPT 11 |
| 81 |
#define NUM_BASES 12 |
| 82 |
|
| 83 |
static policydb_t bases_met[NUM_BASES]; |
| 84 |
static policydb_t bases_notmet[NUM_BASES]; |
| 85 |
|
| 86 |
extern int mls; |
| 87 |
|
| 88 |
int deps_test_init(void) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
for (i = 0; i < NUM_BASES; i++) { |
| 98 |
if (test_load_policy(&bases_met[i], POLICY_BASE, mls, "test-deps", "base-metreq.conf")) |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
for (i = 0; i < NUM_BASES; i++) { |
| 103 |
if (test_load_policy(&bases_notmet[i], POLICY_BASE, mls, "test-deps", "base-notmetreq.conf")) |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
int deps_test_cleanup(void) |
| 111 |
{ |
| 112 |
int i; |
| 113 |
|
| 114 |
for (i = 0; i < NUM_BASES; i++) { |
| 115 |
policydb_destroy(&bases_met[i]); |
| 116 |
} |
| 117 |
|
| 118 |
for (i = 0; i < NUM_BASES; i++) { |
| 119 |
policydb_destroy(&bases_notmet[i]); |
| 120 |
} |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static void do_deps_modreq_global(int req_met, int b, const char *policy, const char *decl_type) |
| 139 |
{ |
| 140 |
policydb_t *base; |
| 141 |
policydb_t mod; |
| 142 |
policydb_t *mods[] = { &mod }; |
| 143 |
avrule_decl_t *decl; |
| 144 |
int ret, link_ret; |
| 145 |
sepol_handle_t *h; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
h = sepol_handle_create(); |
| 151 |
CU_ASSERT_FATAL(h != NULL); |
| 152 |
sepol_msg_set_callback(h, NULL, NULL); |
| 153 |
|
| 154 |
if (req_met) { |
| 155 |
base = &bases_met[b]; |
| 156 |
link_ret = 0; |
| 157 |
} else { |
| 158 |
base = &bases_notmet[b]; |
| 159 |
link_ret = -3; |
| 160 |
} |
| 161 |
|
| 162 |
CU_ASSERT_FATAL(test_load_policy(&mod, POLICY_MOD, mls, "test-deps", policy) == 0); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ret = link_modules(h, base, mods, 1, 0); |
| 167 |
CU_ASSERT_FATAL(ret == link_ret); |
| 168 |
policydb_destroy(&mod); |
| 169 |
sepol_handle_destroy(h); |
| 170 |
|
| 171 |
if (!req_met) |
| 172 |
return; |
| 173 |
|
| 174 |
decl = test_find_decl_by_sym(base, SYM_TYPES, decl_type); |
| 175 |
CU_ASSERT_FATAL(decl != NULL); |
| 176 |
|
| 177 |
CU_ASSERT(decl->enabled == 1); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static void deps_modreq_global(void) |
| 188 |
{ |
| 189 |
|
| 190 |
do_deps_modreq_global(1, BASE_MODREQ_OBJ_GLOBAL, "modreq-obj-global.conf", "mod_global_t"); |
| 191 |
do_deps_modreq_global(0, BASE_MODREQ_OBJ_GLOBAL, "modreq-obj-global.conf", "mod_global_t"); |
| 192 |
|
| 193 |
do_deps_modreq_global(1, BASE_MODREQ_TYPE_GLOBAL, "modreq-type-global.conf", "mod_global_t"); |
| 194 |
do_deps_modreq_global(0, BASE_MODREQ_TYPE_GLOBAL, "modreq-type-global.conf", "mod_global_t"); |
| 195 |
|
| 196 |
do_deps_modreq_global(1, BASE_MODREQ_ATTR_GLOBAL, "modreq-attr-global.conf", "mod_global_t"); |
| 197 |
do_deps_modreq_global(0, BASE_MODREQ_ATTR_GLOBAL, "modreq-attr-global.conf", "mod_global_t"); |
| 198 |
|
| 199 |
do_deps_modreq_global(1, BASE_MODREQ_BOOL_GLOBAL, "modreq-bool-global.conf", "mod_global_t"); |
| 200 |
do_deps_modreq_global(0, BASE_MODREQ_BOOL_GLOBAL, "modreq-bool-global.conf", "mod_global_t"); |
| 201 |
|
| 202 |
do_deps_modreq_global(1, BASE_MODREQ_ROLE_GLOBAL, "modreq-role-global.conf", "mod_global_t"); |
| 203 |
do_deps_modreq_global(0, BASE_MODREQ_ROLE_GLOBAL, "modreq-role-global.conf", "mod_global_t"); |
| 204 |
do_deps_modreq_global(1, BASE_MODREQ_PERM_GLOBAL, "modreq-perm-global.conf", "mod_global_t"); |
| 205 |
do_deps_modreq_global(0, BASE_MODREQ_PERM_GLOBAL, "modreq-perm-global.conf", "mod_global_t"); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static void do_deps_modreq_opt(int req_met, int ret_val, int b, const char *policy, const char *decl_type) |
| 222 |
{ |
| 223 |
policydb_t *base; |
| 224 |
policydb_t mod; |
| 225 |
policydb_t *mods[] = { &mod }; |
| 226 |
avrule_decl_t *decl; |
| 227 |
int ret; |
| 228 |
sepol_handle_t *h; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
h = sepol_handle_create(); |
| 234 |
CU_ASSERT_FATAL(h != NULL); |
| 235 |
sepol_msg_set_callback(h, NULL, NULL); |
| 236 |
|
| 237 |
if (req_met) { |
| 238 |
base = &bases_met[b]; |
| 239 |
} else { |
| 240 |
base = &bases_notmet[b]; |
| 241 |
} |
| 242 |
|
| 243 |
CU_ASSERT_FATAL(test_load_policy(&mod, POLICY_MOD, mls, "test-deps", policy) == 0); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
ret = link_modules(h, base, mods, 1, 0); |
| 248 |
CU_ASSERT_FATAL(ret == ret_val); |
| 249 |
policydb_destroy(&mod); |
| 250 |
sepol_handle_destroy(h); |
| 251 |
if (ret_val < 0) |
| 252 |
return; |
| 253 |
|
| 254 |
decl = test_find_decl_by_sym(base, SYM_TYPES, decl_type); |
| 255 |
CU_ASSERT_FATAL(decl != NULL); |
| 256 |
|
| 257 |
if (req_met) { |
| 258 |
CU_ASSERT(decl->enabled == 1); |
| 259 |
} else { |
| 260 |
CU_ASSERT(decl->enabled == 0); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static void deps_modreq_opt(void) |
| 272 |
{ |
| 273 |
|
| 274 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_OBJ_OPT, "modreq-obj-opt.conf", "mod_opt_t"); |
| 275 |
do_deps_modreq_opt(0, 0, BASE_MODREQ_OBJ_OPT, "modreq-obj-opt.conf", "mod_opt_t"); |
| 276 |
|
| 277 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_TYPE_OPT, "modreq-type-opt.conf", "mod_opt_t"); |
| 278 |
do_deps_modreq_opt(0, 0, BASE_MODREQ_TYPE_OPT, "modreq-type-opt.conf", "mod_opt_t"); |
| 279 |
|
| 280 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_ATTR_OPT, "modreq-attr-opt.conf", "mod_opt_t"); |
| 281 |
do_deps_modreq_opt(0, 0, BASE_MODREQ_ATTR_OPT, "modreq-attr-opt.conf", "mod_opt_t"); |
| 282 |
|
| 283 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_BOOL_OPT, "modreq-bool-opt.conf", "mod_opt_t"); |
| 284 |
do_deps_modreq_opt(0, 0, BASE_MODREQ_BOOL_OPT, "modreq-bool-opt.conf", "mod_opt_t"); |
| 285 |
|
| 286 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_ROLE_OPT, "modreq-role-opt.conf", "mod_opt_t"); |
| 287 |
do_deps_modreq_opt(0, 0, BASE_MODREQ_ROLE_OPT, "modreq-role-opt.conf", "mod_opt_t"); |
| 288 |
|
| 289 |
do_deps_modreq_opt(1, 0, BASE_MODREQ_PERM_OPT, "modreq-perm-opt.conf", "mod_opt_t"); |
| 290 |
do_deps_modreq_opt(0, -3, BASE_MODREQ_PERM_OPT, "modreq-perm-opt.conf", "mod_opt_t"); |
| 291 |
} |
| 292 |
|
| 293 |
int deps_add_tests(CU_pSuite suite) |
| 294 |
{ |
| 295 |
if (NULL == CU_add_test(suite, "deps_modreq_global", deps_modreq_global)) { |
| 296 |
return CU_get_error(); |
| 297 |
} |
| 298 |
|
| 299 |
if (NULL == CU_add_test(suite, "deps_modreq_opt", deps_modreq_opt)) { |
| 300 |
return CU_get_error(); |
| 301 |
} |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |