blob: d8fa636475f3e0f9052c63ae393f128560a21e92 [file] [log] [blame]
William Robertsf0e0a942012-08-27 15:41:15 -07001#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>
William Roberts61846292013-10-15 09:38:24 -070011#include <stdbool.h>
William Robertsf0e0a942012-08-27 15:41:15 -070012#include <sepol/sepol.h>
13#include <sepol/policydb/policydb.h>
Janis Danisevskis750d7972016-04-01 13:31:57 +010014#include <pcre2.h>
William Robertsf0e0a942012-08-27 15:41:15 -070015
16#define TABLE_SIZE 1024
17#define KVP_NUM_OF_RULES (sizeof(rules) / sizeof(key_map))
18#define log_set_verbose() do { logging_verbose = 1; log_info("Enabling verbose\n"); } while(0)
19#define log_error(fmt, ...) log_msg(stderr, "Error: ", fmt, ##__VA_ARGS__)
20#define log_warn(fmt, ...) log_msg(stderr, "Warning: ", fmt, ##__VA_ARGS__)
21#define log_info(fmt, ...) if (logging_verbose ) { log_msg(stdout, "Info: ", fmt, ##__VA_ARGS__); }
22
William Roberts81e1f902015-06-03 21:57:47 -070023/**
24 * Initializes an empty, static list.
25 */
Chih-Hung Hsieh33500c92016-05-11 14:59:45 -070026#define list_init(free_fn) { .head = NULL, .tail = NULL, .freefn = (free_fn) }
William Roberts81e1f902015-06-03 21:57:47 -070027
28/**
29 * given an item in the list, finds the offset for the container
30 * it was stored in.
31 *
32 * @element The element from the list
33 * @type The container type ie what you allocated that has the list_element structure in it.
34 * @name The name of the field that is the list_element
35 *
36 */
37#define list_entry(element, type, name) \
Chih-Hung Hsieh33500c92016-05-11 14:59:45 -070038 (type *)(((uint8_t *)(element)) - (uint8_t *)&(((type *)NULL)->name))
William Roberts81e1f902015-06-03 21:57:47 -070039
40/**
41 * Iterates over the list, do not free elements from the list when using this.
42 * @list The list head to walk
43 * @var The variable name for the cursor
44 */
45#define list_for_each(list, var) \
Chih-Hung Hsieh33500c92016-05-11 14:59:45 -070046 for(var = (list)->head; var != NULL; var = var->next) /*NOLINT*/
William Roberts81e1f902015-06-03 21:57:47 -070047
48
William Robertsf0e0a942012-08-27 15:41:15 -070049typedef struct hash_entry hash_entry;
50typedef enum key_dir key_dir;
51typedef enum data_type data_type;
52typedef enum rule_map_switch rule_map_switch;
William Roberts0ae3a8a2012-09-04 11:51:04 -070053typedef enum map_match map_match;
William Robertsf0e0a942012-08-27 15:41:15 -070054typedef struct key_map key_map;
55typedef struct kvp kvp;
56typedef struct rule_map rule_map;
57typedef struct policy_info policy_info;
William Roberts81e1f902015-06-03 21:57:47 -070058typedef struct list_element list_element;
59typedef struct list list;
60typedef struct key_map_regex key_map_regex;
61typedef struct file_info file_info;
William Robertsf0e0a942012-08-27 15:41:15 -070062
William Roberts0ae3a8a2012-09-04 11:51:04 -070063enum map_match {
64 map_no_matches,
65 map_input_matched,
66 map_matched
67};
68
Stephen Smalley0b820042015-02-13 14:58:31 -050069const char *map_match_str[] = {
70 "do not match",
71 "match on all inputs",
72 "match on everything"
73};
74
William Robertsf0e0a942012-08-27 15:41:15 -070075/**
76 * Whether or not the "key" from a key vaue pair is considered an
77 * input or an output.
78 */
79enum key_dir {
80 dir_in, dir_out
81};
82
William Roberts81e1f902015-06-03 21:57:47 -070083struct list_element {
84 list_element *next;
85};
86
87struct list {
88 list_element *head;
89 list_element *tail;
90 void (*freefn)(list_element *e);
91};
92
93struct key_map_regex {
Janis Danisevskis750d7972016-04-01 13:31:57 +010094 pcre2_code *compiled;
95 pcre2_match_data *match_data;
William Robertsf0e0a942012-08-27 15:41:15 -070096};
97
98/**
99 * The workhorse of the logic. This struct maps key value pairs to
100 * an associated set of meta data maintained in rule_map_new()
101 */
102struct key_map {
103 char *name;
104 key_dir dir;
William Robertsf0e0a942012-08-27 15:41:15 -0700105 char *data;
William Roberts81e1f902015-06-03 21:57:47 -0700106 key_map_regex regex;
William Roberts696a66b2016-01-29 10:41:50 -0800107 bool (*fn_validate)(char *value, char **errmsg);
William Robertsf0e0a942012-08-27 15:41:15 -0700108};
109
110/**
111 * Key value pair struct, this represents the raw kvp values coming
112 * from the rules files.
113 */
114struct kvp {
115 char *key;
116 char *value;
117};
118
119/**
120 * Rules are made up of meta data and an associated set of kvp stored in a
121 * key_map array.
122 */
123struct rule_map {
William Roberts81e1f902015-06-03 21:57:47 -0700124 bool is_never_allow;
125 list violations;
126 list_element listify;
William Robertsf0e0a942012-08-27 15:41:15 -0700127 char *key; /** key value before hashing */
William Roberts610a4b12013-10-15 18:26:00 -0700128 size_t length; /** length of the key map */
William Robertsf0e0a942012-08-27 15:41:15 -0700129 int lineno; /** Line number rule was encounter on */
William Roberts81e1f902015-06-03 21:57:47 -0700130 char *filename; /** File it was found in */
William Robertsf0e0a942012-08-27 15:41:15 -0700131 key_map m[]; /** key value mapping */
132};
133
134struct hash_entry {
William Roberts81e1f902015-06-03 21:57:47 -0700135 list_element listify;
William Robertsf0e0a942012-08-27 15:41:15 -0700136 rule_map *r; /** The rule map to store at that location */
137};
138
139/**
140 * Data associated for a policy file
141 */
142struct policy_info {
143
144 char *policy_file_name; /** policy file path name */
145 FILE *policy_file; /** file handle to the policy file */
146 sepol_policydb_t *db;
147 sepol_policy_file_t *pf;
148 sepol_handle_t *handle;
149 sepol_context_t *con;
150};
151
William Roberts81e1f902015-06-03 21:57:47 -0700152struct file_info {
153 FILE *file; /** file itself */
154 const char *name; /** name of file. do not free, these are not alloc'd */
155 list_element listify;
156};
157
158static void input_file_list_freefn(list_element *e);
159static void line_order_list_freefn(list_element *e);
160static void rule_map_free(rule_map *rm, bool is_in_htable);
161
William Robertsf0e0a942012-08-27 15:41:15 -0700162/** Set to !0 to enable verbose logging */
163static int logging_verbose = 0;
164
165/** file handle to the output file */
William Roberts81e1f902015-06-03 21:57:47 -0700166static file_info out_file;
William Robertsf0e0a942012-08-27 15:41:15 -0700167
William Roberts81e1f902015-06-03 21:57:47 -0700168static list input_file_list = list_init(input_file_list_freefn);
William Robertsf0e0a942012-08-27 15:41:15 -0700169
170static policy_info pol = {
171 .policy_file_name = NULL,
172 .policy_file = NULL,
173 .db = NULL,
174 .pf = NULL,
175 .handle = NULL,
176 .con = NULL
177};
178
179/**
William Roberts81e1f902015-06-03 21:57:47 -0700180 * Head pointer to a linked list of
181 * rule map table entries (hash_entry), used for
182 * preserving the order of entries
183 * based on "first encounter"
184 */
185static list line_order_list = list_init(line_order_list_freefn);
186
187/*
188 * List of hash_entrys for never allow rules.
189 */
190static list nallow_list = list_init(line_order_list_freefn);
191
William Roberts696a66b2016-01-29 10:41:50 -0800192/* validation call backs */
193static bool validate_bool(char *value, char **errmsg);
194static bool validate_levelFrom(char *value, char **errmsg);
195static bool validate_selinux_type(char *value, char **errmsg);
196static bool validate_selinux_level(char *value, char **errmsg);
197
William Roberts81e1f902015-06-03 21:57:47 -0700198/**
William Robertsf0e0a942012-08-27 15:41:15 -0700199 * The heart of the mapping process, this must be updated if a new key value pair is added
200 * to a rule.
201 */
202key_map rules[] = {
203 /*Inputs*/
William Roberts29adea52016-01-29 15:12:58 -0800204 { .name = "isSystemServer", .dir = dir_in, .fn_validate = validate_bool },
Chad Brubaker06cf31e2016-10-06 13:15:44 -0700205 { .name = "isEphemeralApp", .dir = dir_in, .fn_validate = validate_bool },
William Roberts29adea52016-01-29 15:12:58 -0800206 { .name = "isOwner", .dir = dir_in, .fn_validate = validate_bool },
207 { .name = "user", .dir = dir_in, },
208 { .name = "seinfo", .dir = dir_in, },
209 { .name = "name", .dir = dir_in, },
210 { .name = "path", .dir = dir_in, },
211 { .name = "isPrivApp", .dir = dir_in, .fn_validate = validate_bool },
William Robertsf0e0a942012-08-27 15:41:15 -0700212 /*Outputs*/
William Roberts29adea52016-01-29 15:12:58 -0800213 { .name = "domain", .dir = dir_out, .fn_validate = validate_selinux_type },
214 { .name = "type", .dir = dir_out, .fn_validate = validate_selinux_type },
215 { .name = "levelFromUid", .dir = dir_out, .fn_validate = validate_bool },
216 { .name = "levelFrom", .dir = dir_out, .fn_validate = validate_levelFrom },
217 { .name = "level", .dir = dir_out, .fn_validate = validate_selinux_level },
William Robertsfff29802012-11-27 14:20:34 -0800218};
William Robertsf0e0a942012-08-27 15:41:15 -0700219
220/**
William Roberts81e1f902015-06-03 21:57:47 -0700221 * Appends to the end of the list.
222 * @list The list to append to
223 * @e the element to append
William Robertsf0e0a942012-08-27 15:41:15 -0700224 */
William Roberts81e1f902015-06-03 21:57:47 -0700225void list_append(list *list, list_element *e) {
226
227 memset(e, 0, sizeof(*e));
228
229 if (list->head == NULL ) {
230 list->head = list->tail = e;
231 return;
232 }
233
234 list->tail->next = e;
235 list->tail = e;
236 return;
237}
William Robertsf0e0a942012-08-27 15:41:15 -0700238
239/**
William Roberts81e1f902015-06-03 21:57:47 -0700240 * Free's all the elements in the specified list.
241 * @list The list to free
William Robertsf0e0a942012-08-27 15:41:15 -0700242 */
William Roberts81e1f902015-06-03 21:57:47 -0700243static void list_free(list *list) {
244
245 list_element *tmp;
246 list_element *cursor = list->head;
247
248 while (cursor) {
249 tmp = cursor;
250 cursor = cursor->next;
251 if (list->freefn) {
252 list->freefn(tmp);
253 }
254 }
255}
256
257/*
258 * called when the lists are freed
259 */
260static void line_order_list_freefn(list_element *e) {
261 hash_entry *h = list_entry(e, typeof(*h), listify);
262 rule_map_free(h->r, true);
263 free(h);
264}
265
266static void input_file_list_freefn(list_element *e) {
267 file_info *f = list_entry(e, typeof(*f), listify);
268
269 if (f->file) {
270 fclose(f->file);
271 }
272 free(f);
273}
William Robertsf0e0a942012-08-27 15:41:15 -0700274
275/**
276 * Send a logging message to a file
277 * @param out
278 * Output file to send message too
279 * @param prefix
280 * A special prefix to write to the file, such as "Error:"
281 * @param fmt
282 * The printf style formatter to use, such as "%d"
283 */
William Roberts1e8c0612013-04-19 19:06:02 -0700284static void __attribute__ ((format(printf, 3, 4)))
285log_msg(FILE *out, const char *prefix, const char *fmt, ...) {
286
William Robertsf0e0a942012-08-27 15:41:15 -0700287 fprintf(out, "%s", prefix);
288 va_list args;
289 va_start(args, fmt);
290 vfprintf(out, fmt, args);
291 va_end(args);
292}
293
294/**
295 * Checks for a type in the policy.
296 * @param db
297 * The policy db to search
298 * @param type
299 * The type to search for
300 * @return
301 * 1 if the type is found, 0 otherwise.
302 * @warning
303 * This function always returns 1 if libsepol is not linked
304 * statically to this executable and LINK_SEPOL_STATIC is not
305 * defined.
306 */
William Roberts25528cf2016-01-29 10:32:34 -0800307static int check_type(sepol_policydb_t *db, char *type) {
William Robertsf0e0a942012-08-27 15:41:15 -0700308
309 int rc = 1;
310#if defined(LINK_SEPOL_STATIC)
311 policydb_t *d = (policydb_t *)db;
312 hashtab_datum_t dat;
313 dat = hashtab_search(d->p_types.table, type);
314 rc = (dat == NULL) ? 0 : 1;
315#endif
316 return rc;
317}
318
William Roberts81e1f902015-06-03 21:57:47 -0700319static bool match_regex(key_map *assert, const key_map *check) {
320
321 char *tomatch = check->data;
322
Janis Danisevskis750d7972016-04-01 13:31:57 +0100323 int ret = pcre2_match(assert->regex.compiled, (PCRE2_SPTR) tomatch,
324 PCRE2_ZERO_TERMINATED, 0, 0,
325 assert->regex.match_data, NULL);
William Roberts81e1f902015-06-03 21:57:47 -0700326
Janis Danisevskis750d7972016-04-01 13:31:57 +0100327 /* ret > 0 from pcre2_match means matched */
328 return ret > 0;
William Roberts81e1f902015-06-03 21:57:47 -0700329}
330
Janis Danisevskis750d7972016-04-01 13:31:57 +0100331static bool compile_regex(key_map *km, int *errcode, PCRE2_SIZE *erroff) {
William Roberts81e1f902015-06-03 21:57:47 -0700332
333 size_t size;
334 char *anchored;
335
336 /*
337 * Explicitly anchor all regex's
338 * The size is the length of the string to anchor (km->data), the anchor
339 * characters ^ and $ and the null byte. Hence strlen(km->data) + 3
340 */
341 size = strlen(km->data) + 3;
342 anchored = alloca(size);
343 sprintf(anchored, "^%s$", km->data);
344
Janis Danisevskis750d7972016-04-01 13:31:57 +0100345 km->regex.compiled = pcre2_compile((PCRE2_SPTR) anchored,
346 PCRE2_ZERO_TERMINATED,
347 PCRE2_DOTALL,
348 errcode, erroff,
349 NULL);
William Roberts81e1f902015-06-03 21:57:47 -0700350 if (!km->regex.compiled) {
351 return false;
352 }
353
Janis Danisevskis750d7972016-04-01 13:31:57 +0100354 km->regex.match_data = pcre2_match_data_create_from_pattern(
355 km->regex.compiled, NULL);
356 if (!km->regex.match_data) {
357 pcre2_code_free(km->regex.compiled);
358 return false;
359 }
William Roberts81e1f902015-06-03 21:57:47 -0700360 return true;
361}
362
William Roberts696a66b2016-01-29 10:41:50 -0800363static bool validate_bool(char *value, char **errmsg) {
364
365 if (!strcmp("true", value) || !strcmp("false", value)) {
366 return true;
367 }
368
369 *errmsg = "Expecting \"true\" or \"false\"";
370 return false;
371}
372
373static bool validate_levelFrom(char *value, char **errmsg) {
374
375 if(strcasecmp(value, "none") && strcasecmp(value, "all") &&
376 strcasecmp(value, "app") && strcasecmp(value, "user")) {
377 *errmsg = "Expecting one of: \"none\", \"all\", \"app\" or \"user\"";
378 return false;
379 }
380 return true;
381}
382
383static bool validate_selinux_type(char *value, char **errmsg) {
384
385 /*
386 * No policy file present means we cannot check
387 * SE Linux types
388 */
389 if (!pol.policy_file) {
390 return true;
391 }
392
393 if(!check_type(pol.db, value)) {
394 *errmsg = "Expecting a valid SELinux type";
395 return false;
396 }
397
398 return true;
399}
400
401static bool validate_selinux_level(char *value, char **errmsg) {
402
403 /*
404 * No policy file present means we cannot check
405 * SE Linux MLS
406 */
407 if (!pol.policy_file) {
408 return true;
409 }
410
411 int ret = sepol_mls_check(pol.handle, pol.db, value);
412 if (ret < 0) {
413 *errmsg = "Expecting a valid SELinux MLS value";
414 return false;
415 }
416
417 return true;
418}
419
William Robertsf0e0a942012-08-27 15:41:15 -0700420/**
421 * Validates a key_map against a set of enforcement rules, this
422 * function exits the application on a type that cannot be properly
423 * checked
424 *
425 * @param m
426 * The key map to check
427 * @param lineno
428 * The line number in the source file for the corresponding key map
William Robertsfff29802012-11-27 14:20:34 -0800429 * @return
William Roberts81e1f902015-06-03 21:57:47 -0700430 * true if valid, false if invalid
William Robertsf0e0a942012-08-27 15:41:15 -0700431 */
William Roberts81e1f902015-06-03 21:57:47 -0700432static bool key_map_validate(key_map *m, const char *filename, int lineno,
433 bool is_neverallow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700434
Janis Danisevskis750d7972016-04-01 13:31:57 +0100435 PCRE2_SIZE erroff;
436 int errcode;
William Roberts81e1f902015-06-03 21:57:47 -0700437 bool rc = true;
William Robertsf0e0a942012-08-27 15:41:15 -0700438 char *key = m->name;
439 char *value = m->data;
William Roberts696a66b2016-01-29 10:41:50 -0800440 char *errmsg = NULL;
Janis Danisevskis750d7972016-04-01 13:31:57 +0100441 char errstr[256];
William Robertsf0e0a942012-08-27 15:41:15 -0700442
William Roberts0ae3a8a2012-09-04 11:51:04 -0700443 log_info("Validating %s=%s\n", key, value);
444
William Roberts81e1f902015-06-03 21:57:47 -0700445 /*
446 * Neverallows are completely skipped from sanity checking so you can match
447 * un-unspecified inputs.
448 */
449 if (is_neverallow) {
450 if (!m->regex.compiled) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100451 rc = compile_regex(m, &errcode, &erroff);
William Roberts81e1f902015-06-03 21:57:47 -0700452 if (!rc) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100453 pcre2_get_error_message(errcode,
454 (PCRE2_UCHAR*) errstr,
455 sizeof(errstr));
456 log_error("Invalid regex on line %d : %s PCRE error: %s at offset %lu",
457 lineno, value, errstr, erroff);
William Roberts81e1f902015-06-03 21:57:47 -0700458 }
459 }
460 goto out;
461 }
462
William Roberts696a66b2016-01-29 10:41:50 -0800463 /* If the key has a validation routine, call it */
464 if (m->fn_validate) {
465 rc = m->fn_validate(value, &errmsg);
William Robertsf0e0a942012-08-27 15:41:15 -0700466
William Roberts696a66b2016-01-29 10:41:50 -0800467 if (!rc) {
468 log_error("Could not validate key \"%s\" for value \"%s\" on line: %d in file: \"%s\": %s\n", key, value,
469 lineno, filename, errmsg);
William Robertsf0e0a942012-08-27 15:41:15 -0700470 }
471 }
472
William Roberts0ae3a8a2012-09-04 11:51:04 -0700473out:
474 log_info("Key map validate returning: %d\n", rc);
475 return rc;
William Robertsf0e0a942012-08-27 15:41:15 -0700476}
477
478/**
479 * Prints a rule map back to a file
480 * @param fp
481 * The file handle to print too
482 * @param r
483 * The rule map to print
484 */
485static void rule_map_print(FILE *fp, rule_map *r) {
486
William Roberts610a4b12013-10-15 18:26:00 -0700487 size_t i;
William Robertsf0e0a942012-08-27 15:41:15 -0700488 key_map *m;
489
490 for (i = 0; i < r->length; i++) {
491 m = &(r->m[i]);
492 if (i < r->length - 1)
493 fprintf(fp, "%s=%s ", m->name, m->data);
494 else
495 fprintf(fp, "%s=%s", m->name, m->data);
496 }
497}
498
499/**
500 * Compare two rule maps for equality
501 * @param rmA
502 * a rule map to check
503 * @param rmB
504 * a rule map to check
505 * @return
William Robertsae23a1f2012-09-05 12:53:52 -0700506 * a map_match enum indicating the result
William Robertsf0e0a942012-08-27 15:41:15 -0700507 */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700508static map_match rule_map_cmp(rule_map *rmA, rule_map *rmB) {
William Robertsf0e0a942012-08-27 15:41:15 -0700509
William Roberts610a4b12013-10-15 18:26:00 -0700510 size_t i;
511 size_t j;
William Robertsf0e0a942012-08-27 15:41:15 -0700512 int inputs_found = 0;
513 int num_of_matched_inputs = 0;
514 int input_mode = 0;
William Roberts610a4b12013-10-15 18:26:00 -0700515 size_t matches = 0;
William Robertsf0e0a942012-08-27 15:41:15 -0700516 key_map *mA;
517 key_map *mB;
518
William Robertsf0e0a942012-08-27 15:41:15 -0700519 for (i = 0; i < rmA->length; i++) {
520 mA = &(rmA->m[i]);
521
522 for (j = 0; j < rmB->length; j++) {
523 mB = &(rmB->m[j]);
524 input_mode = 0;
525
William Robertsf0e0a942012-08-27 15:41:15 -0700526 if (strcmp(mA->name, mB->name))
527 continue;
528
529 if (strcmp(mA->data, mB->data))
530 continue;
531
532 if (mB->dir != mA->dir)
533 continue;
534 else if (mB->dir == dir_in) {
535 input_mode = 1;
536 inputs_found++;
537 }
538
William Roberts0ae3a8a2012-09-04 11:51:04 -0700539 if (input_mode) {
William Roberts1e8c0612013-04-19 19:06:02 -0700540 log_info("Matched input lines: name=%s data=%s\n", mA->name, mA->data);
William Robertsf0e0a942012-08-27 15:41:15 -0700541 num_of_matched_inputs++;
William Roberts0ae3a8a2012-09-04 11:51:04 -0700542 }
William Robertsf0e0a942012-08-27 15:41:15 -0700543
544 /* Match found, move on */
William Roberts1e8c0612013-04-19 19:06:02 -0700545 log_info("Matched lines: name=%s data=%s", mA->name, mA->data);
William Robertsf0e0a942012-08-27 15:41:15 -0700546 matches++;
547 break;
548 }
549 }
550
551 /* If they all matched*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700552 if (matches == rmA->length) {
553 log_info("Rule map cmp MATCH\n");
554 return map_matched;
555 }
William Robertsf0e0a942012-08-27 15:41:15 -0700556
557 /* They didn't all match but the input's did */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700558 else if (num_of_matched_inputs == inputs_found) {
559 log_info("Rule map cmp INPUT MATCH\n");
560 return map_input_matched;
561 }
William Robertsf0e0a942012-08-27 15:41:15 -0700562
563 /* They didn't all match, and the inputs didn't match, ie it didn't
564 * match */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700565 else {
566 log_info("Rule map cmp NO MATCH\n");
567 return map_no_matches;
568 }
William Robertsf0e0a942012-08-27 15:41:15 -0700569}
570
571/**
572 * Frees a rule map
573 * @param rm
574 * rule map to be freed.
William Roberts7d65b542015-06-19 09:43:28 -0700575 * @is_in_htable
576 * True if the rule map has been added to the hash table, false
577 * otherwise.
578 */
579static void rule_map_free(rule_map *rm, bool is_in_htable) {
William Robertsf0e0a942012-08-27 15:41:15 -0700580
William Roberts610a4b12013-10-15 18:26:00 -0700581 size_t i;
582 size_t len = rm->length;
William Robertsf0e0a942012-08-27 15:41:15 -0700583 for (i = 0; i < len; i++) {
584 key_map *m = &(rm->m[i]);
585 free(m->data);
William Roberts81e1f902015-06-03 21:57:47 -0700586
587 if (m->regex.compiled) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100588 pcre2_code_free(m->regex.compiled);
William Roberts81e1f902015-06-03 21:57:47 -0700589 }
590
Janis Danisevskis750d7972016-04-01 13:31:57 +0100591 if (m->regex.match_data) {
592 pcre2_match_data_free(m->regex.match_data);
William Roberts81e1f902015-06-03 21:57:47 -0700593 }
William Robertsf0e0a942012-08-27 15:41:15 -0700594 }
595
William Roberts7d65b542015-06-19 09:43:28 -0700596 /*
597 * hdestroy() frees comparsion keys for non glibc
598 * on GLIBC we always free on NON-GLIBC we free if
599 * it is not in the htable.
600 */
601 if (rm->key) {
rpcraig5dbfdc02012-10-23 11:03:47 -0400602#ifdef __GLIBC__
William Roberts7d65b542015-06-19 09:43:28 -0700603 /* silence unused warning */
604 (void)is_in_htable;
William Robertsf0e0a942012-08-27 15:41:15 -0700605 free(rm->key);
William Roberts7d65b542015-06-19 09:43:28 -0700606#else
607 if (!is_in_htable) {
608 free(rm->key);
609 }
rpcraig5dbfdc02012-10-23 11:03:47 -0400610#endif
William Roberts7d65b542015-06-19 09:43:28 -0700611 }
William Robertsf0e0a942012-08-27 15:41:15 -0700612
William Roberts81e1f902015-06-03 21:57:47 -0700613 free(rm->filename);
William Robertsf0e0a942012-08-27 15:41:15 -0700614 free(rm);
615}
616
617static void free_kvp(kvp *k) {
618 free(k->key);
619 free(k->value);
620}
621
622/**
William Roberts61846292013-10-15 09:38:24 -0700623 * Checks a rule_map for any variation of KVP's that shouldn't be allowed.
William Roberts81e1f902015-06-03 21:57:47 -0700624 * It builds an assertion failure list for each rule map.
William Roberts61846292013-10-15 09:38:24 -0700625 * Note that this function logs all errors.
626 *
627 * Current Checks:
628 * 1. That a specified name entry should have a specified seinfo entry as well.
William Roberts81e1f902015-06-03 21:57:47 -0700629 * 2. That no rule violates a neverallow
William Roberts61846292013-10-15 09:38:24 -0700630 * @param rm
631 * The rule map to check for validity.
William Roberts61846292013-10-15 09:38:24 -0700632 */
William Roberts81e1f902015-06-03 21:57:47 -0700633static void rule_map_validate(rule_map *rm) {
William Roberts61846292013-10-15 09:38:24 -0700634
William Roberts81e1f902015-06-03 21:57:47 -0700635 size_t i, j;
636 const key_map *rule;
637 key_map *nrule;
638 hash_entry *e;
639 rule_map *assert;
640 list_element *cursor;
William Roberts61846292013-10-15 09:38:24 -0700641
William Roberts81e1f902015-06-03 21:57:47 -0700642 list_for_each(&nallow_list, cursor) {
643 e = list_entry(cursor, typeof(*e), listify);
644 assert = e->r;
William Roberts61846292013-10-15 09:38:24 -0700645
William Roberts81e1f902015-06-03 21:57:47 -0700646 size_t cnt = 0;
647
648 for (j = 0; j < assert->length; j++) {
649 nrule = &(assert->m[j]);
650
651 // mark that nrule->name is for a null check
652 bool is_null_check = !strcmp(nrule->data, "\"\"");
653
654 for (i = 0; i < rm->length; i++) {
655 rule = &(rm->m[i]);
656
657 if (!strcmp(rule->name, nrule->name)) {
658
659 /* the name was found, (data cannot be false) then it was specified */
660 is_null_check = false;
661
662 if (match_regex(nrule, rule)) {
663 cnt++;
664 }
665 }
666 }
667
668 /*
669 * the nrule was marked in a null check and we never found a match on nrule, thus
670 * it matched and we update the cnt
671 */
672 if (is_null_check) {
673 cnt++;
674 }
William Roberts61846292013-10-15 09:38:24 -0700675 }
William Roberts81e1f902015-06-03 21:57:47 -0700676 if (cnt == assert->length) {
677 list_append(&rm->violations, &assert->listify);
William Roberts61846292013-10-15 09:38:24 -0700678 }
679 }
William Roberts61846292013-10-15 09:38:24 -0700680}
681
682/**
William Robertsf0e0a942012-08-27 15:41:15 -0700683 * Given a set of key value pairs, this will construct a new rule map.
684 * On error this function calls exit.
685 * @param keys
686 * Keys from a rule line to map
687 * @param num_of_keys
688 * The length of the keys array
689 * @param lineno
690 * The line number the keys were extracted from
691 * @return
692 * A rule map pointer.
693 */
William Roberts81e1f902015-06-03 21:57:47 -0700694static rule_map *rule_map_new(kvp keys[], size_t num_of_keys, int lineno,
695 const char *filename, bool is_never_allow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700696
William Roberts610a4b12013-10-15 18:26:00 -0700697 size_t i = 0, j = 0;
William Robertsf0e0a942012-08-27 15:41:15 -0700698 rule_map *new_map = NULL;
699 kvp *k = NULL;
700 key_map *r = NULL, *x = NULL;
Stephen Smalley534fb072015-02-13 14:06:08 -0500701 bool seen[KVP_NUM_OF_RULES];
702
703 for (i = 0; i < KVP_NUM_OF_RULES; i++)
704 seen[i] = false;
William Robertsf0e0a942012-08-27 15:41:15 -0700705
706 new_map = calloc(1, (num_of_keys * sizeof(key_map)) + sizeof(rule_map));
707 if (!new_map)
708 goto oom;
709
William Roberts81e1f902015-06-03 21:57:47 -0700710 new_map->is_never_allow = is_never_allow;
William Robertsf0e0a942012-08-27 15:41:15 -0700711 new_map->length = num_of_keys;
712 new_map->lineno = lineno;
William Roberts81e1f902015-06-03 21:57:47 -0700713 new_map->filename = strdup(filename);
714 if (!new_map->filename) {
715 goto oom;
716 }
William Robertsf0e0a942012-08-27 15:41:15 -0700717
718 /* For all the keys in a rule line*/
719 for (i = 0; i < num_of_keys; i++) {
720 k = &(keys[i]);
721 r = &(new_map->m[i]);
722
723 for (j = 0; j < KVP_NUM_OF_RULES; j++) {
724 x = &(rules[j]);
725
726 /* Only assign key name to map name */
727 if (strcasecmp(k->key, x->name)) {
728 if (i == KVP_NUM_OF_RULES) {
729 log_error("No match for key: %s\n", k->key);
730 goto err;
731 }
732 continue;
733 }
734
Stephen Smalley534fb072015-02-13 14:06:08 -0500735 if (seen[j]) {
736 log_error("Duplicated key: %s\n", k->key);
737 goto err;
738 }
739 seen[j] = true;
740
William Robertsf0e0a942012-08-27 15:41:15 -0700741 memcpy(r, x, sizeof(key_map));
742
743 /* Assign rule map value to one from file */
744 r->data = strdup(k->value);
745 if (!r->data)
746 goto oom;
747
748 /* Enforce type check*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700749 log_info("Validating keys!\n");
William Roberts81e1f902015-06-03 21:57:47 -0700750 if (!key_map_validate(r, filename, lineno, new_map->is_never_allow)) {
William Robertsf0e0a942012-08-27 15:41:15 -0700751 log_error("Could not validate\n");
752 goto err;
753 }
754
William Roberts81e1f902015-06-03 21:57:47 -0700755 /*
756 * Only build key off of inputs with the exception of neverallows.
757 * Neverallows are keyed off of all key value pairs,
758 */
759 if (r->dir == dir_in || new_map->is_never_allow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700760 char *tmp;
William Robertsb3ab56c2012-09-17 14:35:02 -0700761 int key_len = strlen(k->key);
762 int val_len = strlen(k->value);
763 int l = (new_map->key) ? strlen(new_map->key) : 0;
764 l = l + key_len + val_len;
William Robertsf0e0a942012-08-27 15:41:15 -0700765 l += 1;
766
767 tmp = realloc(new_map->key, l);
768 if (!tmp)
769 goto oom;
770
William Robertsb3ab56c2012-09-17 14:35:02 -0700771 if (!new_map->key)
772 memset(tmp, 0, l);
773
William Robertsf0e0a942012-08-27 15:41:15 -0700774 new_map->key = tmp;
775
William Robertsb3ab56c2012-09-17 14:35:02 -0700776 strncat(new_map->key, k->key, key_len);
777 strncat(new_map->key, k->value, val_len);
William Robertsf0e0a942012-08-27 15:41:15 -0700778 }
779 break;
780 }
781 free_kvp(k);
782 }
783
784 if (new_map->key == NULL) {
785 log_error("Strange, no keys found, input file corrupt perhaps?\n");
786 goto err;
787 }
788
789 return new_map;
790
791oom:
792 log_error("Out of memory!\n");
793err:
794 if(new_map) {
William Roberts7d65b542015-06-19 09:43:28 -0700795 rule_map_free(new_map, false);
William Robertsf0e0a942012-08-27 15:41:15 -0700796 for (; i < num_of_keys; i++) {
797 k = &(keys[i]);
798 free_kvp(k);
799 }
800 }
Stephen Smalley534fb072015-02-13 14:06:08 -0500801 return NULL;
William Robertsf0e0a942012-08-27 15:41:15 -0700802}
803
804/**
805 * Print the usage of the program
806 */
807static void usage() {
808 printf(
809 "checkseapp [options] <input file>\n"
810 "Processes an seapp_contexts file specified by argument <input file> (default stdin) "
William Robertsae23a1f2012-09-05 12:53:52 -0700811 "and allows later declarations to override previous ones on a match.\n"
William Robertsf0e0a942012-08-27 15:41:15 -0700812 "Options:\n"
813 "-h - print this help message\n"
814 "-v - enable verbose debugging informations\n"
William Roberts63297212013-04-19 19:06:23 -0700815 "-p policy file - specify policy file for strict checking of output selectors against the policy\n"
William Roberts81e1f902015-06-03 21:57:47 -0700816 "-o output file - specify output file or - for stdout. No argument runs in silent mode and outputs nothing\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700817}
818
819static void init() {
820
William Roberts81e1f902015-06-03 21:57:47 -0700821 bool has_out_file;
822 list_element *cursor;
823 file_info *tmp;
824
825 /* input files if the list is empty, use stdin */
826 if (!input_file_list.head) {
827 log_info("Using stdin for input\n");
828 tmp = malloc(sizeof(*tmp));
829 if (!tmp) {
830 log_error("oom");
William Robertsf0e0a942012-08-27 15:41:15 -0700831 exit(EXIT_FAILURE);
832 }
William Roberts81e1f902015-06-03 21:57:47 -0700833 tmp->name = "stdin";
834 tmp->file = stdin;
835 list_append(&input_file_list, &(tmp->listify));
836 }
837 else {
838 list_for_each(&input_file_list, cursor) {
839 tmp = list_entry(cursor, typeof(*tmp), listify);
840
841 log_info("Opening input file: \"%s\"\n", tmp->name);
842 tmp->file = fopen(tmp->name, "r");
843 if (!tmp->file) {
844 log_error("Could not open file: %s error: %s\n", tmp->name,
845 strerror(errno));
846 exit(EXIT_FAILURE);
847 }
848 }
William Robertsf0e0a942012-08-27 15:41:15 -0700849 }
850
William Roberts81e1f902015-06-03 21:57:47 -0700851 has_out_file = out_file.name != NULL;
852
853 /* If output file is -, then use stdout, else open the path */
854 if (has_out_file && !strcmp(out_file.name, "-")) {
855 out_file.file = stdout;
856 out_file.name = "stdout";
857 }
858 else if (has_out_file) {
859 out_file.file = fopen(out_file.name, "w+");
860 }
861
862 if (has_out_file && !out_file.file) {
863 log_error("Could not open file: \"%s\" error: \"%s\"\n", out_file.name,
864 strerror(errno));
865 exit(EXIT_FAILURE);
William Robertsf0e0a942012-08-27 15:41:15 -0700866 }
867
868 if (pol.policy_file_name) {
William Robertsf0e0a942012-08-27 15:41:15 -0700869 log_info("Opening policy file: %s\n", pol.policy_file_name);
870 pol.policy_file = fopen(pol.policy_file_name, "rb");
871 if (!pol.policy_file) {
872 log_error("Could not open file: %s error: %s\n",
873 pol.policy_file_name, strerror(errno));
874 exit(EXIT_FAILURE);
875 }
876
877 pol.handle = sepol_handle_create();
878 if (!pol.handle) {
879 log_error("Could not create sepolicy handle: %s\n",
880 strerror(errno));
881 exit(EXIT_FAILURE);
882 }
883
884 if (sepol_policy_file_create(&pol.pf) < 0) {
885 log_error("Could not create sepolicy file: %s!\n",
886 strerror(errno));
887 exit(EXIT_FAILURE);
888 }
889
890 sepol_policy_file_set_fp(pol.pf, pol.policy_file);
891 sepol_policy_file_set_handle(pol.pf, pol.handle);
892
893 if (sepol_policydb_create(&pol.db) < 0) {
894 log_error("Could not create sepolicy db: %s!\n",
895 strerror(errno));
896 exit(EXIT_FAILURE);
897 }
898
899 if (sepol_policydb_read(pol.db, pol.pf) < 0) {
William Robertsf7d6bb32016-08-08 10:31:54 -0700900 log_error("Could not load policy file to db: invalid input file!\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700901 exit(EXIT_FAILURE);
902 }
903 }
904
William Roberts81e1f902015-06-03 21:57:47 -0700905 list_for_each(&input_file_list, cursor) {
906 tmp = list_entry(cursor, typeof(*tmp), listify);
907 log_info("Input file set to: \"%s\"\n", tmp->name);
908 }
909
910 log_info("Policy file set to: \"%s\"\n",
911 (pol.policy_file_name == NULL) ? "None" : pol.policy_file_name);
912 log_info("Output file set to: \"%s\"\n", out_file.name);
William Robertsf0e0a942012-08-27 15:41:15 -0700913
William Roberts0ae3a8a2012-09-04 11:51:04 -0700914#if !defined(LINK_SEPOL_STATIC)
William Robertsa53ccf32012-09-17 12:53:44 -0700915 log_warn("LINK_SEPOL_STATIC is not defined\n""Not checking types!");
William Roberts0ae3a8a2012-09-04 11:51:04 -0700916#endif
917
William Robertsf0e0a942012-08-27 15:41:15 -0700918}
919
920/**
921 * Handle parsing and setting the global flags for the command line
922 * options. This function calls exit on failure.
923 * @param argc
924 * argument count
925 * @param argv
926 * argument list
927 */
928static void handle_options(int argc, char *argv[]) {
929
930 int c;
William Roberts81e1f902015-06-03 21:57:47 -0700931 file_info *input_file;
William Robertsf0e0a942012-08-27 15:41:15 -0700932
William Robertsf26b6d42015-06-23 10:22:45 -0700933 while ((c = getopt(argc, argv, "ho:p:v")) != -1) {
William Robertsf0e0a942012-08-27 15:41:15 -0700934 switch (c) {
935 case 'h':
936 usage();
937 exit(EXIT_SUCCESS);
938 case 'o':
William Roberts81e1f902015-06-03 21:57:47 -0700939 out_file.name = optarg;
William Robertsf0e0a942012-08-27 15:41:15 -0700940 break;
941 case 'p':
942 pol.policy_file_name = optarg;
943 break;
944 case 'v':
945 log_set_verbose();
946 break;
947 case '?':
948 if (optopt == 'o' || optopt == 'p')
949 log_error("Option -%c requires an argument.\n", optopt);
950 else if (isprint (optopt))
951 log_error("Unknown option `-%c'.\n", optopt);
952 else {
953 log_error(
954 "Unknown option character `\\x%x'.\n",
955 optopt);
William Robertsf0e0a942012-08-27 15:41:15 -0700956 }
William Robertsf0e0a942012-08-27 15:41:15 -0700957 default:
958 exit(EXIT_FAILURE);
959 }
960 }
961
William Roberts81e1f902015-06-03 21:57:47 -0700962 for (c = optind; c < argc; c++) {
William Robertsf0e0a942012-08-27 15:41:15 -0700963
William Roberts81e1f902015-06-03 21:57:47 -0700964 input_file = calloc(1, sizeof(*input_file));
965 if (!input_file) {
966 log_error("oom");
967 exit(EXIT_FAILURE);
968 }
969 input_file->name = argv[c];
970 list_append(&input_file_list, &input_file->listify);
William Robertsf0e0a942012-08-27 15:41:15 -0700971 }
972}
973
974/**
975 * Adds a rule to the hash table and to the ordered list if needed.
976 * @param rm
977 * The rule map to add.
978 */
979static void rule_add(rule_map *rm) {
980
William Roberts0ae3a8a2012-09-04 11:51:04 -0700981 map_match cmp;
William Robertsf0e0a942012-08-27 15:41:15 -0700982 ENTRY e;
983 ENTRY *f;
984 hash_entry *entry;
985 hash_entry *tmp;
William Roberts81e1f902015-06-03 21:57:47 -0700986 list *list_to_addto;
William Robertsf0e0a942012-08-27 15:41:15 -0700987
988 e.key = rm->key;
Rahul Chaudhrye1682c72016-10-12 12:22:39 -0700989 e.data = NULL;
William Robertsf0e0a942012-08-27 15:41:15 -0700990
William Roberts0ae3a8a2012-09-04 11:51:04 -0700991 log_info("Searching for key: %s\n", e.key);
William Robertsf0e0a942012-08-27 15:41:15 -0700992 /* Check to see if it has already been added*/
993 f = hsearch(e, FIND);
994
995 /*
996 * Since your only hashing on a partial key, the inputs we need to handle
997 * when you want to override the outputs for a given input set, as well as
998 * checking for duplicate entries.
999 */
1000 if(f) {
William Roberts0ae3a8a2012-09-04 11:51:04 -07001001 log_info("Existing entry found!\n");
William Robertsf0e0a942012-08-27 15:41:15 -07001002 tmp = (hash_entry *)f->data;
1003 cmp = rule_map_cmp(rm, tmp->r);
Stephen Smalley0b820042015-02-13 14:58:31 -05001004 log_error("Duplicate line detected in file: %s\n"
1005 "Lines %d and %d %s!\n",
William Roberts81e1f902015-06-03 21:57:47 -07001006 rm->filename, tmp->r->lineno, rm->lineno,
Stephen Smalley0b820042015-02-13 14:58:31 -05001007 map_match_str[cmp]);
William Roberts7d65b542015-06-19 09:43:28 -07001008 rule_map_free(rm, false);
Stephen Smalley0b820042015-02-13 14:58:31 -05001009 goto err;
William Robertsf0e0a942012-08-27 15:41:15 -07001010 }
1011 /* It wasn't found, just add the rule map to the table */
1012 else {
1013
1014 entry = malloc(sizeof(hash_entry));
1015 if (!entry)
1016 goto oom;
1017
1018 entry->r = rm;
1019 e.data = entry;
1020
1021 f = hsearch(e, ENTER);
1022 if(f == NULL) {
1023 goto oom;
1024 }
1025
1026 /* new entries must be added to the ordered list */
1027 entry->r = rm;
William Roberts81e1f902015-06-03 21:57:47 -07001028 list_to_addto = rm->is_never_allow ? &nallow_list : &line_order_list;
1029 list_append(list_to_addto, &entry->listify);
William Robertsf0e0a942012-08-27 15:41:15 -07001030 }
1031
1032 return;
1033oom:
1034 if (e.key)
1035 free(e.key);
1036 if (entry)
1037 free(entry);
1038 if (rm)
1039 free(rm);
1040 log_error("Out of memory in function: %s\n", __FUNCTION__);
1041err:
1042 exit(EXIT_FAILURE);
1043}
1044
William Roberts81e1f902015-06-03 21:57:47 -07001045static void parse_file(file_info *in_file) {
William Robertsf0e0a942012-08-27 15:41:15 -07001046
William Roberts81e1f902015-06-03 21:57:47 -07001047 char *p;
William Robertsf0e0a942012-08-27 15:41:15 -07001048 size_t len;
William Roberts81e1f902015-06-03 21:57:47 -07001049 char *token;
1050 char *saveptr;
1051 bool is_never_allow;
1052 bool found_whitespace;
1053
1054 size_t lineno = 0;
1055 char *name = NULL;
1056 char *value = NULL;
William Roberts610a4b12013-10-15 18:26:00 -07001057 size_t token_cnt = 0;
William Robertsf0e0a942012-08-27 15:41:15 -07001058
William Roberts81e1f902015-06-03 21:57:47 -07001059 char line_buf[BUFSIZ];
1060 kvp keys[KVP_NUM_OF_RULES];
William Robertsf0e0a942012-08-27 15:41:15 -07001061
William Roberts81e1f902015-06-03 21:57:47 -07001062 while (fgets(line_buf, sizeof(line_buf) - 1, in_file->file)) {
William Robertsf0e0a942012-08-27 15:41:15 -07001063 lineno++;
William Roberts81e1f902015-06-03 21:57:47 -07001064 is_never_allow = false;
1065 found_whitespace = false;
1066 log_info("Got line %zu\n", lineno);
William Robertsf0e0a942012-08-27 15:41:15 -07001067 len = strlen(line_buf);
1068 if (line_buf[len - 1] == '\n')
Alice Chuf6647eb2012-10-30 16:27:00 -07001069 line_buf[len - 1] = '\0';
William Robertsf0e0a942012-08-27 15:41:15 -07001070 p = line_buf;
William Roberts81e1f902015-06-03 21:57:47 -07001071
1072 /* neverallow lines must start with neverallow (ie ^neverallow) */
1073 if (!strncasecmp(p, "neverallow", strlen("neverallow"))) {
1074 p += strlen("neverallow");
1075 is_never_allow = true;
1076 }
1077
1078 /* strip trailing whitespace skip comments */
1079 while (isspace(*p)) {
William Robertsf0e0a942012-08-27 15:41:15 -07001080 p++;
William Roberts81e1f902015-06-03 21:57:47 -07001081 found_whitespace = true;
1082 }
Alice Chuf6647eb2012-10-30 16:27:00 -07001083 if (*p == '#' || *p == '\0')
William Robertsf0e0a942012-08-27 15:41:15 -07001084 continue;
1085
1086 token = strtok_r(p, " \t", &saveptr);
1087 if (!token)
1088 goto err;
1089
1090 token_cnt = 0;
1091 memset(keys, 0, sizeof(kvp) * KVP_NUM_OF_RULES);
1092 while (1) {
William Roberts0ae3a8a2012-09-04 11:51:04 -07001093
William Robertsf0e0a942012-08-27 15:41:15 -07001094 name = token;
1095 value = strchr(name, '=');
1096 if (!value)
1097 goto err;
1098 *value++ = 0;
1099
1100 keys[token_cnt].key = strdup(name);
1101 if (!keys[token_cnt].key)
1102 goto oom;
1103
1104 keys[token_cnt].value = strdup(value);
1105 if (!keys[token_cnt].value)
1106 goto oom;
1107
1108 token_cnt++;
1109
1110 token = strtok_r(NULL, " \t", &saveptr);
1111 if (!token)
1112 break;
1113
1114 } /*End token parsing */
1115
William Roberts81e1f902015-06-03 21:57:47 -07001116 rule_map *r = rule_map_new(keys, token_cnt, lineno, in_file->name, is_never_allow);
Stephen Smalley534fb072015-02-13 14:06:08 -05001117 if (!r)
1118 goto err;
William Robertsf0e0a942012-08-27 15:41:15 -07001119 rule_add(r);
1120
1121 } /* End file parsing */
1122 return;
1123
1124err:
William Robertsefebf972016-01-29 10:34:04 -08001125 log_error("Reading file: \"%s\" line: %zu name: \"%s\" value: \"%s\"\n",
William Roberts81e1f902015-06-03 21:57:47 -07001126 in_file->name, lineno, name, value);
1127 if(found_whitespace && name && !strcasecmp(name, "neverallow")) {
1128 log_error("perhaps whitespace before neverallow\n");
1129 }
William Robertsf0e0a942012-08-27 15:41:15 -07001130 exit(EXIT_FAILURE);
1131oom:
1132 log_error("In function %s: Out of memory\n", __FUNCTION__);
1133 exit(EXIT_FAILURE);
1134}
1135
1136/**
William Roberts81e1f902015-06-03 21:57:47 -07001137 * Parses the seapp_contexts file and neverallow file
1138 * and adds them to the hash table and ordered list entries
1139 * when it encounters them.
1140 * Calls exit on failure.
1141 */
1142static void parse() {
1143
1144 file_info *current;
1145 list_element *cursor;
1146 list_for_each(&input_file_list, cursor) {
1147 current = list_entry(cursor, typeof(*current), listify);
1148 parse_file(current);
1149 }
1150}
1151
1152static void validate() {
1153
1154 list_element *cursor, *v;
1155 bool found_issues = false;
1156 hash_entry *e;
1157 rule_map *r;
1158 list_for_each(&line_order_list, cursor) {
1159 e = list_entry(cursor, typeof(*e), listify);
1160 rule_map_validate(e->r);
1161 }
1162
1163 list_for_each(&line_order_list, cursor) {
1164 e = list_entry(cursor, typeof(*e), listify);
1165 r = e->r;
1166 list_for_each(&r->violations, v) {
1167 found_issues = true;
1168 log_error("Rule in File \"%s\" on line %d: \"", e->r->filename, e->r->lineno);
1169 rule_map_print(stderr, e->r);
1170 r = list_entry(v, rule_map, listify);
1171 fprintf(stderr, "\" violates neverallow in File \"%s\" on line %d: \"", r->filename, r->lineno);
1172 rule_map_print(stderr, r);
1173 fprintf(stderr, "\"\n");
1174 }
1175 }
1176
1177 if (found_issues) {
1178 exit(EXIT_FAILURE);
1179 }
1180}
1181
1182/**
William Robertsf0e0a942012-08-27 15:41:15 -07001183 * Should be called after parsing to cause the printing of the rule_maps
1184 * stored in the ordered list, head first, which preserves the "first encountered"
1185 * ordering.
1186 */
1187static void output() {
1188
William Roberts81e1f902015-06-03 21:57:47 -07001189 hash_entry *e;
1190 list_element *cursor;
William Robertsf0e0a942012-08-27 15:41:15 -07001191
William Roberts81e1f902015-06-03 21:57:47 -07001192 if (!out_file.file) {
1193 log_info("No output file, not outputting.\n");
1194 return;
1195 }
1196
1197 list_for_each(&line_order_list, cursor) {
1198 e = list_entry(cursor, hash_entry, listify);
1199 rule_map_print(out_file.file, e->r);
1200 fprintf(out_file.file, "\n");
William Robertsf0e0a942012-08-27 15:41:15 -07001201 }
1202}
1203
1204/**
1205 * This function is registered to the at exit handler and should clean up
1206 * the programs dynamic resources, such as memory and fd's.
1207 */
1208static void cleanup() {
1209
1210 /* Only close this when it was opened by me and not the crt */
William Roberts81e1f902015-06-03 21:57:47 -07001211 if (out_file.name && strcmp(out_file.name, "stdout") && out_file.file) {
1212 log_info("Closing file: %s\n", out_file.name);
1213 fclose(out_file.file);
William Robertsf0e0a942012-08-27 15:41:15 -07001214 }
1215
1216 if (pol.policy_file) {
1217
1218 log_info("Closing file: %s\n", pol.policy_file_name);
1219 fclose(pol.policy_file);
1220
1221 if (pol.db)
1222 sepol_policydb_free(pol.db);
1223
1224 if (pol.pf)
1225 sepol_policy_file_free(pol.pf);
1226
1227 if (pol.handle)
1228 sepol_handle_destroy(pol.handle);
1229 }
1230
William Roberts81e1f902015-06-03 21:57:47 -07001231 log_info("Freeing lists\n");
1232 list_free(&input_file_list);
1233 list_free(&line_order_list);
1234 list_free(&nallow_list);
William Robertsf0e0a942012-08-27 15:41:15 -07001235 hdestroy();
1236}
1237
1238int main(int argc, char *argv[]) {
1239 if (!hcreate(TABLE_SIZE)) {
1240 log_error("Could not create hash table: %s\n", strerror(errno));
1241 exit(EXIT_FAILURE);
1242 }
1243 atexit(cleanup);
1244 handle_options(argc, argv);
1245 init();
1246 log_info("Starting to parse\n");
1247 parse();
1248 log_info("Parsing completed, generating output\n");
William Roberts81e1f902015-06-03 21:57:47 -07001249 validate();
William Robertsf0e0a942012-08-27 15:41:15 -07001250 output();
1251 log_info("Success, generated output\n");
1252 exit(EXIT_SUCCESS);
1253}