William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdarg.h> |
| 3 | #include <ctype.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <string.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdint.h> |
| 10 | #include <search.h> |
| 11 | #include <sepol/sepol.h> |
| 12 | #include <sepol/policydb/policydb.h> |
| 13 | |
| 14 | #define TABLE_SIZE 1024 |
| 15 | #define KVP_NUM_OF_RULES (sizeof(rules) / sizeof(key_map)) |
| 16 | #define log_set_verbose() do { logging_verbose = 1; log_info("Enabling verbose\n"); } while(0) |
| 17 | #define log_error(fmt, ...) log_msg(stderr, "Error: ", fmt, ##__VA_ARGS__) |
| 18 | #define log_warn(fmt, ...) log_msg(stderr, "Warning: ", fmt, ##__VA_ARGS__) |
| 19 | #define log_info(fmt, ...) if (logging_verbose ) { log_msg(stdout, "Info: ", fmt, ##__VA_ARGS__); } |
| 20 | |
| 21 | typedef struct line_order_list line_order_list; |
| 22 | typedef struct hash_entry hash_entry; |
| 23 | typedef enum key_dir key_dir; |
| 24 | typedef enum data_type data_type; |
| 25 | typedef enum rule_map_switch rule_map_switch; |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 26 | typedef enum map_match map_match; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 27 | typedef struct key_map key_map; |
| 28 | typedef struct kvp kvp; |
| 29 | typedef struct rule_map rule_map; |
| 30 | typedef struct policy_info policy_info; |
| 31 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 32 | enum map_match { |
| 33 | map_no_matches, |
| 34 | map_input_matched, |
| 35 | map_matched |
| 36 | }; |
| 37 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Whether or not the "key" from a key vaue pair is considered an |
| 40 | * input or an output. |
| 41 | */ |
| 42 | enum key_dir { |
| 43 | dir_in, dir_out |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Used as options to rule_map_free() |
| 48 | * |
| 49 | * This is needed to get around the fact that GNU C's hash_map doesn't copy the key, so |
| 50 | * we cannot free a key when overrding rule_map's in the table. |
| 51 | */ |
| 52 | enum rule_map_switch { |
| 53 | rule_map_preserve_key, /** Used to preserve the key in the rule_map, ie don't free it*/ |
| 54 | rule_map_destroy_key /** Used when you need a full free of the rule_map structure*/ |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * The expected "type" of data the value in the key |
| 59 | * value pair should be. |
| 60 | */ |
| 61 | enum data_type { |
| 62 | dt_bool, dt_string |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * This list is used to store a double pointer to each |
| 67 | * hash table / line rule combination. This way a replacement |
| 68 | * in the hash table automatically updates the list. The list |
| 69 | * is also used to keep "first encountered" ordering amongst |
| 70 | * the encountered key value pairs in the rules file. |
| 71 | */ |
| 72 | struct line_order_list { |
| 73 | hash_entry *e; |
| 74 | line_order_list *next; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * The workhorse of the logic. This struct maps key value pairs to |
| 79 | * an associated set of meta data maintained in rule_map_new() |
| 80 | */ |
| 81 | struct key_map { |
| 82 | char *name; |
| 83 | key_dir dir; |
| 84 | data_type type; |
| 85 | char *data; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * Key value pair struct, this represents the raw kvp values coming |
| 90 | * from the rules files. |
| 91 | */ |
| 92 | struct kvp { |
| 93 | char *key; |
| 94 | char *value; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Rules are made up of meta data and an associated set of kvp stored in a |
| 99 | * key_map array. |
| 100 | */ |
| 101 | struct rule_map { |
| 102 | char *key; /** key value before hashing */ |
| 103 | int length; /** length of the key map */ |
| 104 | int lineno; /** Line number rule was encounter on */ |
| 105 | rule_map *next; /** next pointer used in hash table for chaining on collision */ |
| 106 | key_map m[]; /** key value mapping */ |
| 107 | }; |
| 108 | |
| 109 | struct hash_entry { |
| 110 | rule_map *r; /** The rule map to store at that location */ |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Data associated for a policy file |
| 115 | */ |
| 116 | struct policy_info { |
| 117 | |
| 118 | char *policy_file_name; /** policy file path name */ |
| 119 | FILE *policy_file; /** file handle to the policy file */ |
| 120 | sepol_policydb_t *db; |
| 121 | sepol_policy_file_t *pf; |
| 122 | sepol_handle_t *handle; |
| 123 | sepol_context_t *con; |
| 124 | }; |
| 125 | |
| 126 | /** Set to !0 to enable verbose logging */ |
| 127 | static int logging_verbose = 0; |
| 128 | |
| 129 | /** file handle to the output file */ |
| 130 | static FILE *output_file = NULL; |
| 131 | |
| 132 | /** file handle to the input file */ |
| 133 | static FILE *input_file = NULL; |
| 134 | |
| 135 | /** output file path name */ |
| 136 | static char *out_file_name = NULL; |
| 137 | |
| 138 | /** input file path name */ |
| 139 | static char *in_file_name = NULL; |
| 140 | |
| 141 | static policy_info pol = { |
| 142 | .policy_file_name = NULL, |
| 143 | .policy_file = NULL, |
| 144 | .db = NULL, |
| 145 | .pf = NULL, |
| 146 | .handle = NULL, |
| 147 | .con = NULL |
| 148 | }; |
| 149 | |
| 150 | /** |
| 151 | * The heart of the mapping process, this must be updated if a new key value pair is added |
| 152 | * to a rule. |
| 153 | */ |
| 154 | key_map rules[] = { |
| 155 | /*Inputs*/ |
| 156 | { .name = "isSystemServer", .type = dt_bool, .dir = dir_in, .data = NULL }, |
| 157 | { .name = "user", .type = dt_string, .dir = dir_in, .data = NULL }, |
| 158 | { .name = "seinfo", .type = dt_string, .dir = dir_in, .data = NULL }, |
| 159 | { .name = "name", .type = dt_string, .dir = dir_in, .data = NULL }, |
| 160 | { .name = "sebool", .type = dt_string, .dir = dir_in, .data = NULL }, |
| 161 | /*Outputs*/ |
| 162 | { .name = "domain", .type = dt_string, .dir = dir_out, .data = NULL }, |
| 163 | { .name = "type", .type = dt_string, .dir = dir_out, .data = NULL }, |
| 164 | { .name = "levelFromUid", .type = dt_bool, .dir = dir_out, .data = NULL }, |
| 165 | { .name = "level", .type = dt_string, .dir = dir_out, .data = NULL }, |
| 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * Head pointer to a linked list of |
| 170 | * rule map table entries, used for |
| 171 | * preserving the order of entries |
| 172 | * based on "first encounter" |
| 173 | */ |
| 174 | static line_order_list *list_head = NULL; |
| 175 | |
| 176 | /** |
| 177 | * Pointer to the tail of the list for |
| 178 | * quick appends to the end of the list |
| 179 | */ |
| 180 | static line_order_list *list_tail = NULL; |
| 181 | |
| 182 | /** |
| 183 | * Send a logging message to a file |
| 184 | * @param out |
| 185 | * Output file to send message too |
| 186 | * @param prefix |
| 187 | * A special prefix to write to the file, such as "Error:" |
| 188 | * @param fmt |
| 189 | * The printf style formatter to use, such as "%d" |
| 190 | */ |
| 191 | static void log_msg(FILE *out, const char *prefix, const char *fmt, ...) { |
| 192 | fprintf(out, "%s", prefix); |
| 193 | va_list args; |
| 194 | va_start(args, fmt); |
| 195 | vfprintf(out, fmt, args); |
| 196 | va_end(args); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Checks for a type in the policy. |
| 201 | * @param db |
| 202 | * The policy db to search |
| 203 | * @param type |
| 204 | * The type to search for |
| 205 | * @return |
| 206 | * 1 if the type is found, 0 otherwise. |
| 207 | * @warning |
| 208 | * This function always returns 1 if libsepol is not linked |
| 209 | * statically to this executable and LINK_SEPOL_STATIC is not |
| 210 | * defined. |
| 211 | */ |
| 212 | int check_type(sepol_policydb_t *db, char *type) { |
| 213 | |
| 214 | int rc = 1; |
| 215 | #if defined(LINK_SEPOL_STATIC) |
| 216 | policydb_t *d = (policydb_t *)db; |
| 217 | hashtab_datum_t dat; |
| 218 | dat = hashtab_search(d->p_types.table, type); |
| 219 | rc = (dat == NULL) ? 0 : 1; |
| 220 | #endif |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Validates a key_map against a set of enforcement rules, this |
| 226 | * function exits the application on a type that cannot be properly |
| 227 | * checked |
| 228 | * |
| 229 | * @param m |
| 230 | * The key map to check |
| 231 | * @param lineno |
| 232 | * The line number in the source file for the corresponding key map |
| 233 | */ |
| 234 | static int key_map_validate(key_map *m, int lineno) { |
| 235 | |
| 236 | int rc = 1; |
| 237 | int ret = 1; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 238 | int resp; |
| 239 | char *key = m->name; |
| 240 | char *value = m->data; |
| 241 | data_type type = m->type; |
| 242 | sepol_bool_key_t *se_key; |
| 243 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 244 | log_info("Validating %s=%s\n", key, value); |
| 245 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 246 | /* Booleans can always be checked for sanity */ |
| 247 | if (type == dt_bool && (!strcmp("true", value) || !strcmp("false", value))) { |
| 248 | goto out; |
| 249 | } |
| 250 | else if (type == dt_bool) { |
| 251 | log_error("Line number: %d, Expected boolean value got: %s=%s\n", |
| 252 | lineno, key, value); |
| 253 | rc = 0; |
| 254 | goto out; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * If their is no policy file present, |
| 259 | * then it is not in strict mode so just return. |
| 260 | * User and name cannot really be checked. |
| 261 | */ |
| 262 | if (!pol.policy_file) { |
| 263 | goto out; |
| 264 | } |
| 265 | else if (!strcasecmp(key, "sebool")) { |
| 266 | |
| 267 | ret = sepol_bool_key_create(pol.handle, value, &se_key); |
| 268 | if (ret < 0) { |
| 269 | log_error("Could not create selinux boolean key, error: %s\n", |
| 270 | strerror(errno)); |
| 271 | rc = 0; |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | ret = sepol_bool_exists(pol.handle, pol.db, se_key, &resp); |
| 276 | if (ret < 0) { |
| 277 | log_error("Could not check selinux boolean, error: %s\n", |
| 278 | strerror(errno)); |
| 279 | rc = 0; |
| 280 | goto bool_err; |
| 281 | } |
| 282 | |
| 283 | if(!resp) { |
| 284 | log_error("Could not find selinux boolean \"%s\" on line: %d in file: %s\n", |
| 285 | value, lineno, out_file_name); |
| 286 | rc = 0; |
| 287 | goto bool_err; |
| 288 | } |
| 289 | } |
| 290 | else if (!strcasecmp(key, "type") || !strcasecmp(key, "domain")) { |
| 291 | |
| 292 | if(!check_type(pol.db, value)) { |
| 293 | log_error("Could not find selinux type \"%s\" on line: %d in file: %s\n", value, |
| 294 | lineno, out_file_name); |
| 295 | rc = 0; |
| 296 | } |
| 297 | goto out; |
| 298 | } |
| 299 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 300 | else if (!strcasecmp(key, "level")) { |
| 301 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 302 | ret = sepol_mls_check(pol.handle, pol.db, value); |
| 303 | if (ret < 0) { |
| 304 | log_error("Could not check selinux mls \"%s\", error: %s\n", |
| 305 | value, strerror(errno)); |
| 306 | rc = 0; |
| 307 | goto out; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 311 | bool_err: |
| 312 | sepol_bool_key_free(se_key); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 313 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 314 | out: |
| 315 | log_info("Key map validate returning: %d\n", rc); |
| 316 | return rc; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Prints a rule map back to a file |
| 321 | * @param fp |
| 322 | * The file handle to print too |
| 323 | * @param r |
| 324 | * The rule map to print |
| 325 | */ |
| 326 | static void rule_map_print(FILE *fp, rule_map *r) { |
| 327 | |
| 328 | int i; |
| 329 | key_map *m; |
| 330 | |
| 331 | for (i = 0; i < r->length; i++) { |
| 332 | m = &(r->m[i]); |
| 333 | if (i < r->length - 1) |
| 334 | fprintf(fp, "%s=%s ", m->name, m->data); |
| 335 | else |
| 336 | fprintf(fp, "%s=%s", m->name, m->data); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Compare two rule maps for equality |
| 342 | * @param rmA |
| 343 | * a rule map to check |
| 344 | * @param rmB |
| 345 | * a rule map to check |
| 346 | * @return |
| 347 | * 0 - If the rules input selectors are different, ie not a match |
| 348 | * 1 - If the input selectors match, ie needs an override |
| 349 | * -1 - If the input and output selectors match, ie duplicate line |
| 350 | */ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 351 | static map_match rule_map_cmp(rule_map *rmA, rule_map *rmB) { |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 352 | |
| 353 | int i; |
| 354 | int j; |
| 355 | int inputs_found = 0; |
| 356 | int num_of_matched_inputs = 0; |
| 357 | int input_mode = 0; |
| 358 | int matches = 0; |
| 359 | key_map *mA; |
| 360 | key_map *mB; |
| 361 | |
| 362 | if (rmA->length != rmB->length) |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 363 | return map_no_matches; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 364 | |
| 365 | for (i = 0; i < rmA->length; i++) { |
| 366 | mA = &(rmA->m[i]); |
| 367 | |
| 368 | for (j = 0; j < rmB->length; j++) { |
| 369 | mB = &(rmB->m[j]); |
| 370 | input_mode = 0; |
| 371 | |
| 372 | if (mA->type != mB->type) |
| 373 | continue; |
| 374 | |
| 375 | if (strcmp(mA->name, mB->name)) |
| 376 | continue; |
| 377 | |
| 378 | if (strcmp(mA->data, mB->data)) |
| 379 | continue; |
| 380 | |
| 381 | if (mB->dir != mA->dir) |
| 382 | continue; |
| 383 | else if (mB->dir == dir_in) { |
| 384 | input_mode = 1; |
| 385 | inputs_found++; |
| 386 | } |
| 387 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 388 | if (input_mode) { |
| 389 | log_info("Matched input lines: type=%s name=%s data=%s dir=%d\n", mA->type, mA->name, mA->data, mA->dir); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 390 | num_of_matched_inputs++; |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 391 | } |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 392 | |
| 393 | /* Match found, move on */ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 394 | log_info("Matched lines: type=%s name=%s data=%s dir=%d\n", mA->type, mA->name, mA->data, mA->dir); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 395 | matches++; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* If they all matched*/ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 401 | if (matches == rmA->length) { |
| 402 | log_info("Rule map cmp MATCH\n"); |
| 403 | return map_matched; |
| 404 | } |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 405 | |
| 406 | /* They didn't all match but the input's did */ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 407 | else if (num_of_matched_inputs == inputs_found) { |
| 408 | log_info("Rule map cmp INPUT MATCH\n"); |
| 409 | return map_input_matched; |
| 410 | } |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 411 | |
| 412 | /* They didn't all match, and the inputs didn't match, ie it didn't |
| 413 | * match */ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 414 | else { |
| 415 | log_info("Rule map cmp NO MATCH\n"); |
| 416 | return map_no_matches; |
| 417 | } |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Frees a rule map |
| 422 | * @param rm |
| 423 | * rule map to be freed. |
| 424 | */ |
| 425 | static void rule_map_free(rule_map *rm, rule_map_switch s) { |
| 426 | |
| 427 | int i; |
| 428 | int len = rm->length; |
| 429 | for (i = 0; i < len; i++) { |
| 430 | key_map *m = &(rm->m[i]); |
| 431 | free(m->data); |
| 432 | } |
| 433 | |
| 434 | if(s == rule_map_destroy_key && rm->key) |
| 435 | free(rm->key); |
| 436 | |
| 437 | free(rm); |
| 438 | } |
| 439 | |
| 440 | static void free_kvp(kvp *k) { |
| 441 | free(k->key); |
| 442 | free(k->value); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Given a set of key value pairs, this will construct a new rule map. |
| 447 | * On error this function calls exit. |
| 448 | * @param keys |
| 449 | * Keys from a rule line to map |
| 450 | * @param num_of_keys |
| 451 | * The length of the keys array |
| 452 | * @param lineno |
| 453 | * The line number the keys were extracted from |
| 454 | * @return |
| 455 | * A rule map pointer. |
| 456 | */ |
| 457 | static rule_map *rule_map_new(kvp keys[], unsigned int num_of_keys, int lineno) { |
| 458 | |
| 459 | unsigned int i = 0, j = 0; |
| 460 | rule_map *new_map = NULL; |
| 461 | kvp *k = NULL; |
| 462 | key_map *r = NULL, *x = NULL; |
| 463 | |
| 464 | new_map = calloc(1, (num_of_keys * sizeof(key_map)) + sizeof(rule_map)); |
| 465 | if (!new_map) |
| 466 | goto oom; |
| 467 | |
| 468 | new_map->length = num_of_keys; |
| 469 | new_map->lineno = lineno; |
| 470 | |
| 471 | /* For all the keys in a rule line*/ |
| 472 | for (i = 0; i < num_of_keys; i++) { |
| 473 | k = &(keys[i]); |
| 474 | r = &(new_map->m[i]); |
| 475 | |
| 476 | for (j = 0; j < KVP_NUM_OF_RULES; j++) { |
| 477 | x = &(rules[j]); |
| 478 | |
| 479 | /* Only assign key name to map name */ |
| 480 | if (strcasecmp(k->key, x->name)) { |
| 481 | if (i == KVP_NUM_OF_RULES) { |
| 482 | log_error("No match for key: %s\n", k->key); |
| 483 | goto err; |
| 484 | } |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | memcpy(r, x, sizeof(key_map)); |
| 489 | |
| 490 | /* Assign rule map value to one from file */ |
| 491 | r->data = strdup(k->value); |
| 492 | if (!r->data) |
| 493 | goto oom; |
| 494 | |
| 495 | /* Enforce type check*/ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 496 | log_info("Validating keys!\n"); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 497 | if (!key_map_validate(r, lineno)) { |
| 498 | log_error("Could not validate\n"); |
| 499 | goto err; |
| 500 | } |
| 501 | |
| 502 | /* Only build key off of inputs*/ |
| 503 | if (r->dir == dir_in) { |
| 504 | char *tmp; |
| 505 | int l = strlen(k->key); |
| 506 | l += strlen(k->value); |
| 507 | l += (new_map->key) ? strlen(new_map->key) : 0; |
| 508 | l += 1; |
| 509 | |
| 510 | tmp = realloc(new_map->key, l); |
| 511 | if (!tmp) |
| 512 | goto oom; |
| 513 | |
| 514 | new_map->key = tmp; |
| 515 | |
| 516 | strcat(new_map->key, k->key); |
| 517 | strcat(new_map->key, k->value); |
| 518 | } |
| 519 | break; |
| 520 | } |
| 521 | free_kvp(k); |
| 522 | } |
| 523 | |
| 524 | if (new_map->key == NULL) { |
| 525 | log_error("Strange, no keys found, input file corrupt perhaps?\n"); |
| 526 | goto err; |
| 527 | } |
| 528 | |
| 529 | return new_map; |
| 530 | |
| 531 | oom: |
| 532 | log_error("Out of memory!\n"); |
| 533 | err: |
| 534 | if(new_map) { |
| 535 | rule_map_free(new_map, rule_map_destroy_key); |
| 536 | for (; i < num_of_keys; i++) { |
| 537 | k = &(keys[i]); |
| 538 | free_kvp(k); |
| 539 | } |
| 540 | } |
| 541 | exit(EXIT_FAILURE); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Print the usage of the program |
| 546 | */ |
| 547 | static void usage() { |
| 548 | printf( |
| 549 | "checkseapp [options] <input file>\n" |
| 550 | "Processes an seapp_contexts file specified by argument <input file> (default stdin) " |
| 551 | "and allows later decelerations to override previous ones on a match.\n" |
| 552 | "Options:\n" |
| 553 | "-h - print this help message\n" |
| 554 | "-v - enable verbose debugging informations\n" |
| 555 | "-p policy file - specify policy file for strict checking of output selectors\n" |
| 556 | "-o output file - specify output file, default is stdout\n"); |
| 557 | } |
| 558 | |
| 559 | static void init() { |
| 560 | |
| 561 | /* If not set on stdin already */ |
| 562 | if(!input_file) { |
| 563 | log_info("Opening input file: %s\n", in_file_name); |
| 564 | input_file = fopen(in_file_name, "r"); |
| 565 | if (!input_file) { |
| 566 | log_error("Could not open file: %s error: %s\n", in_file_name, strerror(errno)); |
| 567 | exit(EXIT_FAILURE); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* If not set on std out already */ |
| 572 | if(!output_file) { |
| 573 | output_file = fopen(out_file_name, "w+"); |
| 574 | if (!output_file) { |
| 575 | log_error("Could not open file: %s error: %s\n", out_file_name, strerror(errno)); |
| 576 | exit(EXIT_FAILURE); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (pol.policy_file_name) { |
| 581 | |
| 582 | log_info("Opening policy file: %s\n", pol.policy_file_name); |
| 583 | pol.policy_file = fopen(pol.policy_file_name, "rb"); |
| 584 | if (!pol.policy_file) { |
| 585 | log_error("Could not open file: %s error: %s\n", |
| 586 | pol.policy_file_name, strerror(errno)); |
| 587 | exit(EXIT_FAILURE); |
| 588 | } |
| 589 | |
| 590 | pol.handle = sepol_handle_create(); |
| 591 | if (!pol.handle) { |
| 592 | log_error("Could not create sepolicy handle: %s\n", |
| 593 | strerror(errno)); |
| 594 | exit(EXIT_FAILURE); |
| 595 | } |
| 596 | |
| 597 | if (sepol_policy_file_create(&pol.pf) < 0) { |
| 598 | log_error("Could not create sepolicy file: %s!\n", |
| 599 | strerror(errno)); |
| 600 | exit(EXIT_FAILURE); |
| 601 | } |
| 602 | |
| 603 | sepol_policy_file_set_fp(pol.pf, pol.policy_file); |
| 604 | sepol_policy_file_set_handle(pol.pf, pol.handle); |
| 605 | |
| 606 | if (sepol_policydb_create(&pol.db) < 0) { |
| 607 | log_error("Could not create sepolicy db: %s!\n", |
| 608 | strerror(errno)); |
| 609 | exit(EXIT_FAILURE); |
| 610 | } |
| 611 | |
| 612 | if (sepol_policydb_read(pol.db, pol.pf) < 0) { |
| 613 | log_error("Could not lod policy file to db: %s!\n", |
| 614 | strerror(errno)); |
| 615 | exit(EXIT_FAILURE); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | log_info("Policy file set to: %s\n", (pol.policy_file_name == NULL) ? "None" : pol.policy_file_name); |
| 620 | log_info("Input file set to: %s\n", (in_file_name == NULL) ? "stdin" : in_file_name); |
| 621 | log_info("Output file set to: %s\n", (out_file_name == NULL) ? "stdout" : out_file_name); |
| 622 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 623 | #if !defined(LINK_SEPOL_STATIC) |
| 624 | log_warning("LINK_SEPOL_STATIC is not defined\n""Not checking types!"); |
| 625 | #endif |
| 626 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Handle parsing and setting the global flags for the command line |
| 631 | * options. This function calls exit on failure. |
| 632 | * @param argc |
| 633 | * argument count |
| 634 | * @param argv |
| 635 | * argument list |
| 636 | */ |
| 637 | static void handle_options(int argc, char *argv[]) { |
| 638 | |
| 639 | int c; |
| 640 | int num_of_args; |
| 641 | |
| 642 | while ((c = getopt(argc, argv, "ho:p:v")) != -1) { |
| 643 | switch (c) { |
| 644 | case 'h': |
| 645 | usage(); |
| 646 | exit(EXIT_SUCCESS); |
| 647 | case 'o': |
| 648 | out_file_name = optarg; |
| 649 | break; |
| 650 | case 'p': |
| 651 | pol.policy_file_name = optarg; |
| 652 | break; |
| 653 | case 'v': |
| 654 | log_set_verbose(); |
| 655 | break; |
| 656 | case '?': |
| 657 | if (optopt == 'o' || optopt == 'p') |
| 658 | log_error("Option -%c requires an argument.\n", optopt); |
| 659 | else if (isprint (optopt)) |
| 660 | log_error("Unknown option `-%c'.\n", optopt); |
| 661 | else { |
| 662 | log_error( |
| 663 | "Unknown option character `\\x%x'.\n", |
| 664 | optopt); |
| 665 | exit(EXIT_FAILURE); |
| 666 | } |
| 667 | break; |
| 668 | default: |
| 669 | exit(EXIT_FAILURE); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | num_of_args = argc - optind; |
| 674 | |
| 675 | if (num_of_args > 1) { |
| 676 | log_error("Too many arguments, expected 0 or 1, argument, got %d\n", num_of_args); |
| 677 | usage(); |
| 678 | exit(EXIT_FAILURE); |
| 679 | } else if (num_of_args == 1) { |
| 680 | in_file_name = argv[argc - 1]; |
| 681 | } else { |
| 682 | input_file = stdin; |
| 683 | in_file_name = "stdin"; |
| 684 | } |
| 685 | |
| 686 | if (!out_file_name) { |
| 687 | output_file = stdout; |
| 688 | out_file_name = "stdout"; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Adds a rule_map double pointer, ie the hash table pointer to the list. |
| 694 | * By using a double pointer, the hash table can have a line be overridden |
| 695 | * and the value is updated in the list. This function calls exit on failure. |
| 696 | * @param rm |
| 697 | * the rule_map to add. |
| 698 | */ |
| 699 | static void list_add(hash_entry *e) { |
| 700 | |
| 701 | line_order_list *node = malloc(sizeof(line_order_list)); |
| 702 | if (node == NULL) |
| 703 | goto oom; |
| 704 | |
| 705 | node->next = NULL; |
| 706 | node->e = e; |
| 707 | |
| 708 | if (list_head == NULL) |
| 709 | list_head = list_tail = node; |
| 710 | else { |
| 711 | list_tail->next = node; |
| 712 | list_tail = list_tail->next; |
| 713 | } |
| 714 | return; |
| 715 | |
| 716 | oom: |
| 717 | log_error("Out of memory!\n"); |
| 718 | exit(EXIT_FAILURE); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Free's the rule map list, which ultimatley contains |
| 723 | * all the malloc'd rule_maps. |
| 724 | */ |
| 725 | static void list_free() { |
| 726 | line_order_list *cursor, *tmp; |
| 727 | hash_entry *e; |
| 728 | |
| 729 | cursor = list_head; |
| 730 | while (cursor) { |
| 731 | e = cursor->e; |
| 732 | rule_map_free(e->r, rule_map_destroy_key); |
| 733 | tmp = cursor; |
| 734 | cursor = cursor->next; |
| 735 | free(e); |
| 736 | free(tmp); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Adds a rule to the hash table and to the ordered list if needed. |
| 742 | * @param rm |
| 743 | * The rule map to add. |
| 744 | */ |
| 745 | static void rule_add(rule_map *rm) { |
| 746 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 747 | map_match cmp; |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 748 | ENTRY e; |
| 749 | ENTRY *f; |
| 750 | hash_entry *entry; |
| 751 | hash_entry *tmp; |
| 752 | char *preserved_key; |
| 753 | |
| 754 | e.key = rm->key; |
| 755 | |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 756 | log_info("Searching for key: %s\n", e.key); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 757 | /* Check to see if it has already been added*/ |
| 758 | f = hsearch(e, FIND); |
| 759 | |
| 760 | /* |
| 761 | * Since your only hashing on a partial key, the inputs we need to handle |
| 762 | * when you want to override the outputs for a given input set, as well as |
| 763 | * checking for duplicate entries. |
| 764 | */ |
| 765 | if(f) { |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 766 | log_info("Existing entry found!\n"); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 767 | tmp = (hash_entry *)f->data; |
| 768 | cmp = rule_map_cmp(rm, tmp->r); |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 769 | log_info("Comparing on rule map ret: %d\n", cmp); |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 770 | /* Override be freeing the old rule map and updating |
| 771 | the pointer */ |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 772 | if(cmp != map_matched) { |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * DO NOT free key pointers given to the hash map, instead |
| 776 | * free the new key. The ordering here is critical! |
| 777 | */ |
| 778 | preserved_key = tmp->r->key; |
| 779 | rule_map_free(tmp->r, rule_map_preserve_key); |
| 780 | free(rm->key); |
| 781 | rm->key = preserved_key; |
| 782 | tmp->r = rm; |
| 783 | } |
| 784 | /* Duplicate */ |
| 785 | else { |
| 786 | log_error("Duplicate line detected in file: %s\n" |
| 787 | "Lines %d and %d match!\n", |
| 788 | out_file_name, tmp->r->lineno, rm->lineno); |
| 789 | rule_map_free(rm, rule_map_destroy_key); |
| 790 | goto err; |
| 791 | } |
| 792 | } |
| 793 | /* It wasn't found, just add the rule map to the table */ |
| 794 | else { |
| 795 | |
| 796 | entry = malloc(sizeof(hash_entry)); |
| 797 | if (!entry) |
| 798 | goto oom; |
| 799 | |
| 800 | entry->r = rm; |
| 801 | e.data = entry; |
| 802 | |
| 803 | f = hsearch(e, ENTER); |
| 804 | if(f == NULL) { |
| 805 | goto oom; |
| 806 | } |
| 807 | |
| 808 | /* new entries must be added to the ordered list */ |
| 809 | entry->r = rm; |
| 810 | list_add(entry); |
| 811 | } |
| 812 | |
| 813 | return; |
| 814 | oom: |
| 815 | if (e.key) |
| 816 | free(e.key); |
| 817 | if (entry) |
| 818 | free(entry); |
| 819 | if (rm) |
| 820 | free(rm); |
| 821 | log_error("Out of memory in function: %s\n", __FUNCTION__); |
| 822 | err: |
| 823 | exit(EXIT_FAILURE); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Parses the seapp_contexts file and adds them to the |
| 828 | * hash table and ordered list entries when it encounters them. |
| 829 | * Calls exit on failure. |
| 830 | */ |
| 831 | static void parse() { |
| 832 | |
| 833 | char line_buf[BUFSIZ]; |
| 834 | char *token; |
| 835 | unsigned lineno = 0; |
| 836 | char *p, *name = NULL, *value = NULL, *saveptr; |
| 837 | size_t len; |
| 838 | kvp keys[KVP_NUM_OF_RULES]; |
| 839 | int token_cnt = 0; |
| 840 | |
| 841 | while (fgets(line_buf, sizeof line_buf - 1, input_file)) { |
| 842 | |
| 843 | lineno++; |
| 844 | log_info("Got line %d\n", lineno); |
| 845 | len = strlen(line_buf); |
| 846 | if (line_buf[len - 1] == '\n') |
| 847 | line_buf[len - 1] = 0; |
| 848 | p = line_buf; |
| 849 | while (isspace(*p)) |
| 850 | p++; |
| 851 | if (*p == '#' || *p == 0) |
| 852 | continue; |
| 853 | |
| 854 | token = strtok_r(p, " \t", &saveptr); |
| 855 | if (!token) |
| 856 | goto err; |
| 857 | |
| 858 | token_cnt = 0; |
| 859 | memset(keys, 0, sizeof(kvp) * KVP_NUM_OF_RULES); |
| 860 | while (1) { |
William Roberts | 0ae3a8a | 2012-09-04 11:51:04 -0700 | [diff] [blame^] | 861 | |
William Roberts | f0e0a94 | 2012-08-27 15:41:15 -0700 | [diff] [blame] | 862 | name = token; |
| 863 | value = strchr(name, '='); |
| 864 | if (!value) |
| 865 | goto err; |
| 866 | *value++ = 0; |
| 867 | |
| 868 | keys[token_cnt].key = strdup(name); |
| 869 | if (!keys[token_cnt].key) |
| 870 | goto oom; |
| 871 | |
| 872 | keys[token_cnt].value = strdup(value); |
| 873 | if (!keys[token_cnt].value) |
| 874 | goto oom; |
| 875 | |
| 876 | token_cnt++; |
| 877 | |
| 878 | token = strtok_r(NULL, " \t", &saveptr); |
| 879 | if (!token) |
| 880 | break; |
| 881 | |
| 882 | } /*End token parsing */ |
| 883 | |
| 884 | rule_map *r = rule_map_new(keys, token_cnt, lineno); |
| 885 | rule_add(r); |
| 886 | |
| 887 | } /* End file parsing */ |
| 888 | return; |
| 889 | |
| 890 | err: |
| 891 | log_error("reading %s, line %u, name %s, value %s\n", |
| 892 | in_file_name, lineno, name, value); |
| 893 | exit(EXIT_FAILURE); |
| 894 | oom: |
| 895 | log_error("In function %s: Out of memory\n", __FUNCTION__); |
| 896 | exit(EXIT_FAILURE); |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Should be called after parsing to cause the printing of the rule_maps |
| 901 | * stored in the ordered list, head first, which preserves the "first encountered" |
| 902 | * ordering. |
| 903 | */ |
| 904 | static void output() { |
| 905 | |
| 906 | rule_map *r; |
| 907 | line_order_list *cursor; |
| 908 | cursor = list_head; |
| 909 | |
| 910 | while (cursor) { |
| 911 | r = cursor->e->r; |
| 912 | rule_map_print(output_file, r); |
| 913 | cursor = cursor->next; |
| 914 | if (cursor) |
| 915 | fprintf(output_file, "\n"); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * This function is registered to the at exit handler and should clean up |
| 921 | * the programs dynamic resources, such as memory and fd's. |
| 922 | */ |
| 923 | static void cleanup() { |
| 924 | |
| 925 | /* Only close this when it was opened by me and not the crt */ |
| 926 | if (out_file_name && output_file) { |
| 927 | log_info("Closing file: %s\n", out_file_name); |
| 928 | fclose(output_file); |
| 929 | } |
| 930 | |
| 931 | /* Only close this when it was opened by me and not the crt */ |
| 932 | if (in_file_name && input_file) { |
| 933 | log_info("Closing file: %s\n", in_file_name); |
| 934 | fclose(input_file); |
| 935 | } |
| 936 | |
| 937 | if (pol.policy_file) { |
| 938 | |
| 939 | log_info("Closing file: %s\n", pol.policy_file_name); |
| 940 | fclose(pol.policy_file); |
| 941 | |
| 942 | if (pol.db) |
| 943 | sepol_policydb_free(pol.db); |
| 944 | |
| 945 | if (pol.pf) |
| 946 | sepol_policy_file_free(pol.pf); |
| 947 | |
| 948 | if (pol.handle) |
| 949 | sepol_handle_destroy(pol.handle); |
| 950 | } |
| 951 | |
| 952 | log_info("Freeing list\n"); |
| 953 | list_free(); |
| 954 | hdestroy(); |
| 955 | } |
| 956 | |
| 957 | int main(int argc, char *argv[]) { |
| 958 | if (!hcreate(TABLE_SIZE)) { |
| 959 | log_error("Could not create hash table: %s\n", strerror(errno)); |
| 960 | exit(EXIT_FAILURE); |
| 961 | } |
| 962 | atexit(cleanup); |
| 963 | handle_options(argc, argv); |
| 964 | init(); |
| 965 | log_info("Starting to parse\n"); |
| 966 | parse(); |
| 967 | log_info("Parsing completed, generating output\n"); |
| 968 | output(); |
| 969 | log_info("Success, generated output\n"); |
| 970 | exit(EXIT_SUCCESS); |
| 971 | } |