blob: 96b9ebf89ced554f0726a3dbe5005dd510a7c378 [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);
Michael Peckf54b3622017-02-14 09:48:57 -0800197static bool validate_uint(char *value, char **errmsg);
William Roberts696a66b2016-01-29 10:41:50 -0800198
William Roberts81e1f902015-06-03 21:57:47 -0700199/**
William Robertsf0e0a942012-08-27 15:41:15 -0700200 * The heart of the mapping process, this must be updated if a new key value pair is added
201 * to a rule.
202 */
203key_map rules[] = {
204 /*Inputs*/
William Roberts29adea52016-01-29 15:12:58 -0800205 { .name = "isSystemServer", .dir = dir_in, .fn_validate = validate_bool },
Chad Brubaker06cf31e2016-10-06 13:15:44 -0700206 { .name = "isEphemeralApp", .dir = dir_in, .fn_validate = validate_bool },
William Roberts29adea52016-01-29 15:12:58 -0800207 { .name = "isOwner", .dir = dir_in, .fn_validate = validate_bool },
208 { .name = "user", .dir = dir_in, },
209 { .name = "seinfo", .dir = dir_in, },
210 { .name = "name", .dir = dir_in, },
211 { .name = "path", .dir = dir_in, },
212 { .name = "isPrivApp", .dir = dir_in, .fn_validate = validate_bool },
Michael Peckf54b3622017-02-14 09:48:57 -0800213 { .name = "minTargetSdkVersion", .dir = dir_in, .fn_validate = validate_uint },
William Robertsf0e0a942012-08-27 15:41:15 -0700214 /*Outputs*/
William Roberts29adea52016-01-29 15:12:58 -0800215 { .name = "domain", .dir = dir_out, .fn_validate = validate_selinux_type },
216 { .name = "type", .dir = dir_out, .fn_validate = validate_selinux_type },
217 { .name = "levelFromUid", .dir = dir_out, .fn_validate = validate_bool },
218 { .name = "levelFrom", .dir = dir_out, .fn_validate = validate_levelFrom },
219 { .name = "level", .dir = dir_out, .fn_validate = validate_selinux_level },
William Robertsfff29802012-11-27 14:20:34 -0800220};
William Robertsf0e0a942012-08-27 15:41:15 -0700221
222/**
William Roberts81e1f902015-06-03 21:57:47 -0700223 * Appends to the end of the list.
224 * @list The list to append to
225 * @e the element to append
William Robertsf0e0a942012-08-27 15:41:15 -0700226 */
William Roberts81e1f902015-06-03 21:57:47 -0700227void list_append(list *list, list_element *e) {
228
229 memset(e, 0, sizeof(*e));
230
231 if (list->head == NULL ) {
232 list->head = list->tail = e;
233 return;
234 }
235
236 list->tail->next = e;
237 list->tail = e;
238 return;
239}
William Robertsf0e0a942012-08-27 15:41:15 -0700240
241/**
William Roberts81e1f902015-06-03 21:57:47 -0700242 * Free's all the elements in the specified list.
243 * @list The list to free
William Robertsf0e0a942012-08-27 15:41:15 -0700244 */
William Roberts81e1f902015-06-03 21:57:47 -0700245static void list_free(list *list) {
246
247 list_element *tmp;
248 list_element *cursor = list->head;
249
250 while (cursor) {
251 tmp = cursor;
252 cursor = cursor->next;
253 if (list->freefn) {
254 list->freefn(tmp);
255 }
256 }
257}
258
259/*
260 * called when the lists are freed
261 */
262static void line_order_list_freefn(list_element *e) {
263 hash_entry *h = list_entry(e, typeof(*h), listify);
264 rule_map_free(h->r, true);
265 free(h);
266}
267
268static void input_file_list_freefn(list_element *e) {
269 file_info *f = list_entry(e, typeof(*f), listify);
270
271 if (f->file) {
272 fclose(f->file);
273 }
274 free(f);
275}
William Robertsf0e0a942012-08-27 15:41:15 -0700276
277/**
278 * Send a logging message to a file
279 * @param out
280 * Output file to send message too
281 * @param prefix
282 * A special prefix to write to the file, such as "Error:"
283 * @param fmt
284 * The printf style formatter to use, such as "%d"
285 */
William Roberts1e8c0612013-04-19 19:06:02 -0700286static void __attribute__ ((format(printf, 3, 4)))
287log_msg(FILE *out, const char *prefix, const char *fmt, ...) {
288
William Robertsf0e0a942012-08-27 15:41:15 -0700289 fprintf(out, "%s", prefix);
290 va_list args;
291 va_start(args, fmt);
292 vfprintf(out, fmt, args);
293 va_end(args);
294}
295
296/**
297 * Checks for a type in the policy.
298 * @param db
299 * The policy db to search
300 * @param type
301 * The type to search for
302 * @return
303 * 1 if the type is found, 0 otherwise.
304 * @warning
305 * This function always returns 1 if libsepol is not linked
306 * statically to this executable and LINK_SEPOL_STATIC is not
307 * defined.
308 */
William Roberts25528cf2016-01-29 10:32:34 -0800309static int check_type(sepol_policydb_t *db, char *type) {
William Robertsf0e0a942012-08-27 15:41:15 -0700310
311 int rc = 1;
312#if defined(LINK_SEPOL_STATIC)
313 policydb_t *d = (policydb_t *)db;
314 hashtab_datum_t dat;
315 dat = hashtab_search(d->p_types.table, type);
316 rc = (dat == NULL) ? 0 : 1;
317#endif
318 return rc;
319}
320
William Roberts81e1f902015-06-03 21:57:47 -0700321static bool match_regex(key_map *assert, const key_map *check) {
322
323 char *tomatch = check->data;
324
Janis Danisevskis750d7972016-04-01 13:31:57 +0100325 int ret = pcre2_match(assert->regex.compiled, (PCRE2_SPTR) tomatch,
326 PCRE2_ZERO_TERMINATED, 0, 0,
327 assert->regex.match_data, NULL);
William Roberts81e1f902015-06-03 21:57:47 -0700328
Janis Danisevskis750d7972016-04-01 13:31:57 +0100329 /* ret > 0 from pcre2_match means matched */
330 return ret > 0;
William Roberts81e1f902015-06-03 21:57:47 -0700331}
332
Janis Danisevskis750d7972016-04-01 13:31:57 +0100333static bool compile_regex(key_map *km, int *errcode, PCRE2_SIZE *erroff) {
William Roberts81e1f902015-06-03 21:57:47 -0700334
335 size_t size;
336 char *anchored;
337
338 /*
339 * Explicitly anchor all regex's
340 * The size is the length of the string to anchor (km->data), the anchor
341 * characters ^ and $ and the null byte. Hence strlen(km->data) + 3
342 */
343 size = strlen(km->data) + 3;
344 anchored = alloca(size);
345 sprintf(anchored, "^%s$", km->data);
346
Janis Danisevskis750d7972016-04-01 13:31:57 +0100347 km->regex.compiled = pcre2_compile((PCRE2_SPTR) anchored,
348 PCRE2_ZERO_TERMINATED,
349 PCRE2_DOTALL,
350 errcode, erroff,
351 NULL);
William Roberts81e1f902015-06-03 21:57:47 -0700352 if (!km->regex.compiled) {
353 return false;
354 }
355
Janis Danisevskis750d7972016-04-01 13:31:57 +0100356 km->regex.match_data = pcre2_match_data_create_from_pattern(
357 km->regex.compiled, NULL);
358 if (!km->regex.match_data) {
359 pcre2_code_free(km->regex.compiled);
360 return false;
361 }
William Roberts81e1f902015-06-03 21:57:47 -0700362 return true;
363}
364
William Roberts696a66b2016-01-29 10:41:50 -0800365static bool validate_bool(char *value, char **errmsg) {
366
367 if (!strcmp("true", value) || !strcmp("false", value)) {
368 return true;
369 }
370
371 *errmsg = "Expecting \"true\" or \"false\"";
372 return false;
373}
374
375static bool validate_levelFrom(char *value, char **errmsg) {
376
377 if(strcasecmp(value, "none") && strcasecmp(value, "all") &&
378 strcasecmp(value, "app") && strcasecmp(value, "user")) {
379 *errmsg = "Expecting one of: \"none\", \"all\", \"app\" or \"user\"";
380 return false;
381 }
382 return true;
383}
384
385static bool validate_selinux_type(char *value, char **errmsg) {
386
387 /*
388 * No policy file present means we cannot check
389 * SE Linux types
390 */
391 if (!pol.policy_file) {
392 return true;
393 }
394
395 if(!check_type(pol.db, value)) {
396 *errmsg = "Expecting a valid SELinux type";
397 return false;
398 }
399
400 return true;
401}
402
403static bool validate_selinux_level(char *value, char **errmsg) {
404
405 /*
406 * No policy file present means we cannot check
407 * SE Linux MLS
408 */
409 if (!pol.policy_file) {
410 return true;
411 }
412
413 int ret = sepol_mls_check(pol.handle, pol.db, value);
414 if (ret < 0) {
415 *errmsg = "Expecting a valid SELinux MLS value";
416 return false;
417 }
418
419 return true;
420}
421
Michael Peckf54b3622017-02-14 09:48:57 -0800422static bool validate_uint(char *value, char **errmsg) {
423
424 char *endptr;
425 long longvalue;
426 longvalue = strtol(value, &endptr, 10);
427 if (('\0' != *endptr) || (longvalue < 0) || (longvalue > INT32_MAX)) {
428 *errmsg = "Expecting a valid unsigned integer";
429 return false;
430 }
431
432 return true;
433}
434
William Robertsf0e0a942012-08-27 15:41:15 -0700435/**
436 * Validates a key_map against a set of enforcement rules, this
437 * function exits the application on a type that cannot be properly
438 * checked
439 *
440 * @param m
441 * The key map to check
442 * @param lineno
443 * The line number in the source file for the corresponding key map
William Robertsfff29802012-11-27 14:20:34 -0800444 * @return
William Roberts81e1f902015-06-03 21:57:47 -0700445 * true if valid, false if invalid
William Robertsf0e0a942012-08-27 15:41:15 -0700446 */
William Roberts81e1f902015-06-03 21:57:47 -0700447static bool key_map_validate(key_map *m, const char *filename, int lineno,
448 bool is_neverallow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700449
Janis Danisevskis750d7972016-04-01 13:31:57 +0100450 PCRE2_SIZE erroff;
451 int errcode;
William Roberts81e1f902015-06-03 21:57:47 -0700452 bool rc = true;
William Robertsf0e0a942012-08-27 15:41:15 -0700453 char *key = m->name;
454 char *value = m->data;
William Roberts696a66b2016-01-29 10:41:50 -0800455 char *errmsg = NULL;
Janis Danisevskis750d7972016-04-01 13:31:57 +0100456 char errstr[256];
William Robertsf0e0a942012-08-27 15:41:15 -0700457
William Roberts0ae3a8a2012-09-04 11:51:04 -0700458 log_info("Validating %s=%s\n", key, value);
459
William Roberts81e1f902015-06-03 21:57:47 -0700460 /*
461 * Neverallows are completely skipped from sanity checking so you can match
462 * un-unspecified inputs.
463 */
464 if (is_neverallow) {
465 if (!m->regex.compiled) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100466 rc = compile_regex(m, &errcode, &erroff);
William Roberts81e1f902015-06-03 21:57:47 -0700467 if (!rc) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100468 pcre2_get_error_message(errcode,
469 (PCRE2_UCHAR*) errstr,
470 sizeof(errstr));
471 log_error("Invalid regex on line %d : %s PCRE error: %s at offset %lu",
472 lineno, value, errstr, erroff);
William Roberts81e1f902015-06-03 21:57:47 -0700473 }
474 }
475 goto out;
476 }
477
William Roberts696a66b2016-01-29 10:41:50 -0800478 /* If the key has a validation routine, call it */
479 if (m->fn_validate) {
480 rc = m->fn_validate(value, &errmsg);
William Robertsf0e0a942012-08-27 15:41:15 -0700481
William Roberts696a66b2016-01-29 10:41:50 -0800482 if (!rc) {
483 log_error("Could not validate key \"%s\" for value \"%s\" on line: %d in file: \"%s\": %s\n", key, value,
484 lineno, filename, errmsg);
William Robertsf0e0a942012-08-27 15:41:15 -0700485 }
486 }
487
William Roberts0ae3a8a2012-09-04 11:51:04 -0700488out:
489 log_info("Key map validate returning: %d\n", rc);
490 return rc;
William Robertsf0e0a942012-08-27 15:41:15 -0700491}
492
493/**
494 * Prints a rule map back to a file
495 * @param fp
496 * The file handle to print too
497 * @param r
498 * The rule map to print
499 */
500static void rule_map_print(FILE *fp, rule_map *r) {
501
William Roberts610a4b12013-10-15 18:26:00 -0700502 size_t i;
William Robertsf0e0a942012-08-27 15:41:15 -0700503 key_map *m;
504
505 for (i = 0; i < r->length; i++) {
506 m = &(r->m[i]);
507 if (i < r->length - 1)
508 fprintf(fp, "%s=%s ", m->name, m->data);
509 else
510 fprintf(fp, "%s=%s", m->name, m->data);
511 }
512}
513
514/**
515 * Compare two rule maps for equality
516 * @param rmA
517 * a rule map to check
518 * @param rmB
519 * a rule map to check
520 * @return
William Robertsae23a1f2012-09-05 12:53:52 -0700521 * a map_match enum indicating the result
William Robertsf0e0a942012-08-27 15:41:15 -0700522 */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700523static map_match rule_map_cmp(rule_map *rmA, rule_map *rmB) {
William Robertsf0e0a942012-08-27 15:41:15 -0700524
William Roberts610a4b12013-10-15 18:26:00 -0700525 size_t i;
526 size_t j;
William Robertsf0e0a942012-08-27 15:41:15 -0700527 int inputs_found = 0;
528 int num_of_matched_inputs = 0;
529 int input_mode = 0;
William Roberts610a4b12013-10-15 18:26:00 -0700530 size_t matches = 0;
William Robertsf0e0a942012-08-27 15:41:15 -0700531 key_map *mA;
532 key_map *mB;
533
William Robertsf0e0a942012-08-27 15:41:15 -0700534 for (i = 0; i < rmA->length; i++) {
535 mA = &(rmA->m[i]);
536
537 for (j = 0; j < rmB->length; j++) {
538 mB = &(rmB->m[j]);
539 input_mode = 0;
540
William Robertsf0e0a942012-08-27 15:41:15 -0700541 if (strcmp(mA->name, mB->name))
542 continue;
543
544 if (strcmp(mA->data, mB->data))
545 continue;
546
547 if (mB->dir != mA->dir)
548 continue;
549 else if (mB->dir == dir_in) {
550 input_mode = 1;
551 inputs_found++;
552 }
553
William Roberts0ae3a8a2012-09-04 11:51:04 -0700554 if (input_mode) {
William Roberts1e8c0612013-04-19 19:06:02 -0700555 log_info("Matched input lines: name=%s data=%s\n", mA->name, mA->data);
William Robertsf0e0a942012-08-27 15:41:15 -0700556 num_of_matched_inputs++;
William Roberts0ae3a8a2012-09-04 11:51:04 -0700557 }
William Robertsf0e0a942012-08-27 15:41:15 -0700558
559 /* Match found, move on */
William Roberts1e8c0612013-04-19 19:06:02 -0700560 log_info("Matched lines: name=%s data=%s", mA->name, mA->data);
William Robertsf0e0a942012-08-27 15:41:15 -0700561 matches++;
562 break;
563 }
564 }
565
566 /* If they all matched*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700567 if (matches == rmA->length) {
568 log_info("Rule map cmp MATCH\n");
569 return map_matched;
570 }
William Robertsf0e0a942012-08-27 15:41:15 -0700571
572 /* They didn't all match but the input's did */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700573 else if (num_of_matched_inputs == inputs_found) {
574 log_info("Rule map cmp INPUT MATCH\n");
575 return map_input_matched;
576 }
William Robertsf0e0a942012-08-27 15:41:15 -0700577
578 /* They didn't all match, and the inputs didn't match, ie it didn't
579 * match */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700580 else {
581 log_info("Rule map cmp NO MATCH\n");
582 return map_no_matches;
583 }
William Robertsf0e0a942012-08-27 15:41:15 -0700584}
585
586/**
587 * Frees a rule map
588 * @param rm
589 * rule map to be freed.
William Roberts7d65b542015-06-19 09:43:28 -0700590 * @is_in_htable
591 * True if the rule map has been added to the hash table, false
592 * otherwise.
593 */
594static void rule_map_free(rule_map *rm, bool is_in_htable) {
William Robertsf0e0a942012-08-27 15:41:15 -0700595
William Roberts610a4b12013-10-15 18:26:00 -0700596 size_t i;
597 size_t len = rm->length;
William Robertsf0e0a942012-08-27 15:41:15 -0700598 for (i = 0; i < len; i++) {
599 key_map *m = &(rm->m[i]);
600 free(m->data);
William Roberts81e1f902015-06-03 21:57:47 -0700601
602 if (m->regex.compiled) {
Janis Danisevskis750d7972016-04-01 13:31:57 +0100603 pcre2_code_free(m->regex.compiled);
William Roberts81e1f902015-06-03 21:57:47 -0700604 }
605
Janis Danisevskis750d7972016-04-01 13:31:57 +0100606 if (m->regex.match_data) {
607 pcre2_match_data_free(m->regex.match_data);
William Roberts81e1f902015-06-03 21:57:47 -0700608 }
William Robertsf0e0a942012-08-27 15:41:15 -0700609 }
610
William Roberts7d65b542015-06-19 09:43:28 -0700611 /*
612 * hdestroy() frees comparsion keys for non glibc
613 * on GLIBC we always free on NON-GLIBC we free if
614 * it is not in the htable.
615 */
616 if (rm->key) {
rpcraig5dbfdc02012-10-23 11:03:47 -0400617#ifdef __GLIBC__
William Roberts7d65b542015-06-19 09:43:28 -0700618 /* silence unused warning */
619 (void)is_in_htable;
William Robertsf0e0a942012-08-27 15:41:15 -0700620 free(rm->key);
William Roberts7d65b542015-06-19 09:43:28 -0700621#else
622 if (!is_in_htable) {
623 free(rm->key);
624 }
rpcraig5dbfdc02012-10-23 11:03:47 -0400625#endif
William Roberts7d65b542015-06-19 09:43:28 -0700626 }
William Robertsf0e0a942012-08-27 15:41:15 -0700627
William Roberts81e1f902015-06-03 21:57:47 -0700628 free(rm->filename);
William Robertsf0e0a942012-08-27 15:41:15 -0700629 free(rm);
630}
631
632static void free_kvp(kvp *k) {
633 free(k->key);
634 free(k->value);
635}
636
637/**
William Roberts61846292013-10-15 09:38:24 -0700638 * Checks a rule_map for any variation of KVP's that shouldn't be allowed.
William Roberts81e1f902015-06-03 21:57:47 -0700639 * It builds an assertion failure list for each rule map.
William Roberts61846292013-10-15 09:38:24 -0700640 * Note that this function logs all errors.
641 *
642 * Current Checks:
643 * 1. That a specified name entry should have a specified seinfo entry as well.
William Roberts81e1f902015-06-03 21:57:47 -0700644 * 2. That no rule violates a neverallow
William Roberts61846292013-10-15 09:38:24 -0700645 * @param rm
646 * The rule map to check for validity.
William Roberts61846292013-10-15 09:38:24 -0700647 */
William Roberts81e1f902015-06-03 21:57:47 -0700648static void rule_map_validate(rule_map *rm) {
William Roberts61846292013-10-15 09:38:24 -0700649
William Roberts81e1f902015-06-03 21:57:47 -0700650 size_t i, j;
651 const key_map *rule;
652 key_map *nrule;
653 hash_entry *e;
654 rule_map *assert;
655 list_element *cursor;
William Roberts61846292013-10-15 09:38:24 -0700656
William Roberts81e1f902015-06-03 21:57:47 -0700657 list_for_each(&nallow_list, cursor) {
658 e = list_entry(cursor, typeof(*e), listify);
659 assert = e->r;
William Roberts61846292013-10-15 09:38:24 -0700660
William Roberts81e1f902015-06-03 21:57:47 -0700661 size_t cnt = 0;
662
663 for (j = 0; j < assert->length; j++) {
664 nrule = &(assert->m[j]);
665
666 // mark that nrule->name is for a null check
667 bool is_null_check = !strcmp(nrule->data, "\"\"");
668
669 for (i = 0; i < rm->length; i++) {
670 rule = &(rm->m[i]);
671
672 if (!strcmp(rule->name, nrule->name)) {
673
674 /* the name was found, (data cannot be false) then it was specified */
675 is_null_check = false;
676
677 if (match_regex(nrule, rule)) {
678 cnt++;
679 }
680 }
681 }
682
683 /*
684 * the nrule was marked in a null check and we never found a match on nrule, thus
685 * it matched and we update the cnt
686 */
687 if (is_null_check) {
688 cnt++;
689 }
William Roberts61846292013-10-15 09:38:24 -0700690 }
William Roberts81e1f902015-06-03 21:57:47 -0700691 if (cnt == assert->length) {
692 list_append(&rm->violations, &assert->listify);
William Roberts61846292013-10-15 09:38:24 -0700693 }
694 }
William Roberts61846292013-10-15 09:38:24 -0700695}
696
697/**
William Robertsf0e0a942012-08-27 15:41:15 -0700698 * Given a set of key value pairs, this will construct a new rule map.
699 * On error this function calls exit.
700 * @param keys
701 * Keys from a rule line to map
702 * @param num_of_keys
703 * The length of the keys array
704 * @param lineno
705 * The line number the keys were extracted from
706 * @return
707 * A rule map pointer.
708 */
William Roberts81e1f902015-06-03 21:57:47 -0700709static rule_map *rule_map_new(kvp keys[], size_t num_of_keys, int lineno,
710 const char *filename, bool is_never_allow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700711
William Roberts610a4b12013-10-15 18:26:00 -0700712 size_t i = 0, j = 0;
William Robertsf0e0a942012-08-27 15:41:15 -0700713 rule_map *new_map = NULL;
714 kvp *k = NULL;
715 key_map *r = NULL, *x = NULL;
Stephen Smalley534fb072015-02-13 14:06:08 -0500716 bool seen[KVP_NUM_OF_RULES];
717
718 for (i = 0; i < KVP_NUM_OF_RULES; i++)
719 seen[i] = false;
William Robertsf0e0a942012-08-27 15:41:15 -0700720
721 new_map = calloc(1, (num_of_keys * sizeof(key_map)) + sizeof(rule_map));
722 if (!new_map)
723 goto oom;
724
William Roberts81e1f902015-06-03 21:57:47 -0700725 new_map->is_never_allow = is_never_allow;
William Robertsf0e0a942012-08-27 15:41:15 -0700726 new_map->length = num_of_keys;
727 new_map->lineno = lineno;
William Roberts81e1f902015-06-03 21:57:47 -0700728 new_map->filename = strdup(filename);
729 if (!new_map->filename) {
730 goto oom;
731 }
William Robertsf0e0a942012-08-27 15:41:15 -0700732
733 /* For all the keys in a rule line*/
734 for (i = 0; i < num_of_keys; i++) {
735 k = &(keys[i]);
736 r = &(new_map->m[i]);
737
738 for (j = 0; j < KVP_NUM_OF_RULES; j++) {
739 x = &(rules[j]);
740
741 /* Only assign key name to map name */
742 if (strcasecmp(k->key, x->name)) {
743 if (i == KVP_NUM_OF_RULES) {
744 log_error("No match for key: %s\n", k->key);
745 goto err;
746 }
747 continue;
748 }
749
Stephen Smalley534fb072015-02-13 14:06:08 -0500750 if (seen[j]) {
751 log_error("Duplicated key: %s\n", k->key);
752 goto err;
753 }
754 seen[j] = true;
755
William Robertsf0e0a942012-08-27 15:41:15 -0700756 memcpy(r, x, sizeof(key_map));
757
758 /* Assign rule map value to one from file */
759 r->data = strdup(k->value);
760 if (!r->data)
761 goto oom;
762
763 /* Enforce type check*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700764 log_info("Validating keys!\n");
William Roberts81e1f902015-06-03 21:57:47 -0700765 if (!key_map_validate(r, filename, lineno, new_map->is_never_allow)) {
William Robertsf0e0a942012-08-27 15:41:15 -0700766 log_error("Could not validate\n");
767 goto err;
768 }
769
William Roberts81e1f902015-06-03 21:57:47 -0700770 /*
771 * Only build key off of inputs with the exception of neverallows.
772 * Neverallows are keyed off of all key value pairs,
773 */
774 if (r->dir == dir_in || new_map->is_never_allow) {
William Robertsf0e0a942012-08-27 15:41:15 -0700775 char *tmp;
William Robertsb3ab56c2012-09-17 14:35:02 -0700776 int key_len = strlen(k->key);
777 int val_len = strlen(k->value);
778 int l = (new_map->key) ? strlen(new_map->key) : 0;
779 l = l + key_len + val_len;
William Robertsf0e0a942012-08-27 15:41:15 -0700780 l += 1;
781
782 tmp = realloc(new_map->key, l);
783 if (!tmp)
784 goto oom;
785
William Robertsb3ab56c2012-09-17 14:35:02 -0700786 if (!new_map->key)
787 memset(tmp, 0, l);
788
William Robertsf0e0a942012-08-27 15:41:15 -0700789 new_map->key = tmp;
790
William Robertsb3ab56c2012-09-17 14:35:02 -0700791 strncat(new_map->key, k->key, key_len);
792 strncat(new_map->key, k->value, val_len);
William Robertsf0e0a942012-08-27 15:41:15 -0700793 }
794 break;
795 }
796 free_kvp(k);
797 }
798
799 if (new_map->key == NULL) {
800 log_error("Strange, no keys found, input file corrupt perhaps?\n");
801 goto err;
802 }
803
804 return new_map;
805
806oom:
807 log_error("Out of memory!\n");
808err:
809 if(new_map) {
William Roberts7d65b542015-06-19 09:43:28 -0700810 rule_map_free(new_map, false);
William Robertsf0e0a942012-08-27 15:41:15 -0700811 for (; i < num_of_keys; i++) {
812 k = &(keys[i]);
813 free_kvp(k);
814 }
815 }
Stephen Smalley534fb072015-02-13 14:06:08 -0500816 return NULL;
William Robertsf0e0a942012-08-27 15:41:15 -0700817}
818
819/**
820 * Print the usage of the program
821 */
822static void usage() {
823 printf(
824 "checkseapp [options] <input file>\n"
825 "Processes an seapp_contexts file specified by argument <input file> (default stdin) "
William Robertsae23a1f2012-09-05 12:53:52 -0700826 "and allows later declarations to override previous ones on a match.\n"
William Robertsf0e0a942012-08-27 15:41:15 -0700827 "Options:\n"
828 "-h - print this help message\n"
829 "-v - enable verbose debugging informations\n"
William Roberts63297212013-04-19 19:06:23 -0700830 "-p policy file - specify policy file for strict checking of output selectors against the policy\n"
William Roberts81e1f902015-06-03 21:57:47 -0700831 "-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 -0700832}
833
834static void init() {
835
William Roberts81e1f902015-06-03 21:57:47 -0700836 bool has_out_file;
837 list_element *cursor;
838 file_info *tmp;
839
840 /* input files if the list is empty, use stdin */
841 if (!input_file_list.head) {
842 log_info("Using stdin for input\n");
843 tmp = malloc(sizeof(*tmp));
844 if (!tmp) {
845 log_error("oom");
William Robertsf0e0a942012-08-27 15:41:15 -0700846 exit(EXIT_FAILURE);
847 }
William Roberts81e1f902015-06-03 21:57:47 -0700848 tmp->name = "stdin";
849 tmp->file = stdin;
850 list_append(&input_file_list, &(tmp->listify));
851 }
852 else {
853 list_for_each(&input_file_list, cursor) {
854 tmp = list_entry(cursor, typeof(*tmp), listify);
855
856 log_info("Opening input file: \"%s\"\n", tmp->name);
857 tmp->file = fopen(tmp->name, "r");
858 if (!tmp->file) {
859 log_error("Could not open file: %s error: %s\n", tmp->name,
860 strerror(errno));
861 exit(EXIT_FAILURE);
862 }
863 }
William Robertsf0e0a942012-08-27 15:41:15 -0700864 }
865
William Roberts81e1f902015-06-03 21:57:47 -0700866 has_out_file = out_file.name != NULL;
867
868 /* If output file is -, then use stdout, else open the path */
869 if (has_out_file && !strcmp(out_file.name, "-")) {
870 out_file.file = stdout;
871 out_file.name = "stdout";
872 }
873 else if (has_out_file) {
874 out_file.file = fopen(out_file.name, "w+");
875 }
876
877 if (has_out_file && !out_file.file) {
878 log_error("Could not open file: \"%s\" error: \"%s\"\n", out_file.name,
879 strerror(errno));
880 exit(EXIT_FAILURE);
William Robertsf0e0a942012-08-27 15:41:15 -0700881 }
882
883 if (pol.policy_file_name) {
William Robertsf0e0a942012-08-27 15:41:15 -0700884 log_info("Opening policy file: %s\n", pol.policy_file_name);
885 pol.policy_file = fopen(pol.policy_file_name, "rb");
886 if (!pol.policy_file) {
887 log_error("Could not open file: %s error: %s\n",
888 pol.policy_file_name, strerror(errno));
889 exit(EXIT_FAILURE);
890 }
891
892 pol.handle = sepol_handle_create();
893 if (!pol.handle) {
894 log_error("Could not create sepolicy handle: %s\n",
895 strerror(errno));
896 exit(EXIT_FAILURE);
897 }
898
899 if (sepol_policy_file_create(&pol.pf) < 0) {
900 log_error("Could not create sepolicy file: %s!\n",
901 strerror(errno));
902 exit(EXIT_FAILURE);
903 }
904
905 sepol_policy_file_set_fp(pol.pf, pol.policy_file);
906 sepol_policy_file_set_handle(pol.pf, pol.handle);
907
908 if (sepol_policydb_create(&pol.db) < 0) {
909 log_error("Could not create sepolicy db: %s!\n",
910 strerror(errno));
911 exit(EXIT_FAILURE);
912 }
913
914 if (sepol_policydb_read(pol.db, pol.pf) < 0) {
William Robertsf7d6bb32016-08-08 10:31:54 -0700915 log_error("Could not load policy file to db: invalid input file!\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700916 exit(EXIT_FAILURE);
917 }
918 }
919
William Roberts81e1f902015-06-03 21:57:47 -0700920 list_for_each(&input_file_list, cursor) {
921 tmp = list_entry(cursor, typeof(*tmp), listify);
922 log_info("Input file set to: \"%s\"\n", tmp->name);
923 }
924
925 log_info("Policy file set to: \"%s\"\n",
926 (pol.policy_file_name == NULL) ? "None" : pol.policy_file_name);
927 log_info("Output file set to: \"%s\"\n", out_file.name);
William Robertsf0e0a942012-08-27 15:41:15 -0700928
William Roberts0ae3a8a2012-09-04 11:51:04 -0700929#if !defined(LINK_SEPOL_STATIC)
William Robertsa53ccf32012-09-17 12:53:44 -0700930 log_warn("LINK_SEPOL_STATIC is not defined\n""Not checking types!");
William Roberts0ae3a8a2012-09-04 11:51:04 -0700931#endif
932
William Robertsf0e0a942012-08-27 15:41:15 -0700933}
934
935/**
936 * Handle parsing and setting the global flags for the command line
937 * options. This function calls exit on failure.
938 * @param argc
939 * argument count
940 * @param argv
941 * argument list
942 */
943static void handle_options(int argc, char *argv[]) {
944
945 int c;
William Roberts81e1f902015-06-03 21:57:47 -0700946 file_info *input_file;
William Robertsf0e0a942012-08-27 15:41:15 -0700947
William Robertsf26b6d42015-06-23 10:22:45 -0700948 while ((c = getopt(argc, argv, "ho:p:v")) != -1) {
William Robertsf0e0a942012-08-27 15:41:15 -0700949 switch (c) {
950 case 'h':
951 usage();
952 exit(EXIT_SUCCESS);
953 case 'o':
William Roberts81e1f902015-06-03 21:57:47 -0700954 out_file.name = optarg;
William Robertsf0e0a942012-08-27 15:41:15 -0700955 break;
956 case 'p':
957 pol.policy_file_name = optarg;
958 break;
959 case 'v':
960 log_set_verbose();
961 break;
962 case '?':
963 if (optopt == 'o' || optopt == 'p')
964 log_error("Option -%c requires an argument.\n", optopt);
965 else if (isprint (optopt))
966 log_error("Unknown option `-%c'.\n", optopt);
967 else {
968 log_error(
969 "Unknown option character `\\x%x'.\n",
970 optopt);
William Robertsf0e0a942012-08-27 15:41:15 -0700971 }
William Robertsf0e0a942012-08-27 15:41:15 -0700972 default:
973 exit(EXIT_FAILURE);
974 }
975 }
976
William Roberts81e1f902015-06-03 21:57:47 -0700977 for (c = optind; c < argc; c++) {
William Robertsf0e0a942012-08-27 15:41:15 -0700978
William Roberts81e1f902015-06-03 21:57:47 -0700979 input_file = calloc(1, sizeof(*input_file));
980 if (!input_file) {
981 log_error("oom");
982 exit(EXIT_FAILURE);
983 }
984 input_file->name = argv[c];
985 list_append(&input_file_list, &input_file->listify);
William Robertsf0e0a942012-08-27 15:41:15 -0700986 }
987}
988
989/**
990 * Adds a rule to the hash table and to the ordered list if needed.
991 * @param rm
992 * The rule map to add.
993 */
994static void rule_add(rule_map *rm) {
995
William Roberts0ae3a8a2012-09-04 11:51:04 -0700996 map_match cmp;
William Robertsf0e0a942012-08-27 15:41:15 -0700997 ENTRY e;
998 ENTRY *f;
999 hash_entry *entry;
1000 hash_entry *tmp;
William Roberts81e1f902015-06-03 21:57:47 -07001001 list *list_to_addto;
William Robertsf0e0a942012-08-27 15:41:15 -07001002
1003 e.key = rm->key;
Rahul Chaudhrye1682c72016-10-12 12:22:39 -07001004 e.data = NULL;
William Robertsf0e0a942012-08-27 15:41:15 -07001005
William Roberts0ae3a8a2012-09-04 11:51:04 -07001006 log_info("Searching for key: %s\n", e.key);
William Robertsf0e0a942012-08-27 15:41:15 -07001007 /* Check to see if it has already been added*/
1008 f = hsearch(e, FIND);
1009
1010 /*
1011 * Since your only hashing on a partial key, the inputs we need to handle
1012 * when you want to override the outputs for a given input set, as well as
1013 * checking for duplicate entries.
1014 */
1015 if(f) {
William Roberts0ae3a8a2012-09-04 11:51:04 -07001016 log_info("Existing entry found!\n");
William Robertsf0e0a942012-08-27 15:41:15 -07001017 tmp = (hash_entry *)f->data;
1018 cmp = rule_map_cmp(rm, tmp->r);
Stephen Smalley0b820042015-02-13 14:58:31 -05001019 log_error("Duplicate line detected in file: %s\n"
1020 "Lines %d and %d %s!\n",
William Roberts81e1f902015-06-03 21:57:47 -07001021 rm->filename, tmp->r->lineno, rm->lineno,
Stephen Smalley0b820042015-02-13 14:58:31 -05001022 map_match_str[cmp]);
William Roberts7d65b542015-06-19 09:43:28 -07001023 rule_map_free(rm, false);
Stephen Smalley0b820042015-02-13 14:58:31 -05001024 goto err;
William Robertsf0e0a942012-08-27 15:41:15 -07001025 }
1026 /* It wasn't found, just add the rule map to the table */
1027 else {
1028
1029 entry = malloc(sizeof(hash_entry));
1030 if (!entry)
1031 goto oom;
1032
1033 entry->r = rm;
1034 e.data = entry;
1035
1036 f = hsearch(e, ENTER);
1037 if(f == NULL) {
1038 goto oom;
1039 }
1040
1041 /* new entries must be added to the ordered list */
1042 entry->r = rm;
William Roberts81e1f902015-06-03 21:57:47 -07001043 list_to_addto = rm->is_never_allow ? &nallow_list : &line_order_list;
1044 list_append(list_to_addto, &entry->listify);
William Robertsf0e0a942012-08-27 15:41:15 -07001045 }
1046
1047 return;
1048oom:
1049 if (e.key)
1050 free(e.key);
1051 if (entry)
1052 free(entry);
1053 if (rm)
1054 free(rm);
1055 log_error("Out of memory in function: %s\n", __FUNCTION__);
1056err:
1057 exit(EXIT_FAILURE);
1058}
1059
William Roberts81e1f902015-06-03 21:57:47 -07001060static void parse_file(file_info *in_file) {
William Robertsf0e0a942012-08-27 15:41:15 -07001061
William Roberts81e1f902015-06-03 21:57:47 -07001062 char *p;
William Robertsf0e0a942012-08-27 15:41:15 -07001063 size_t len;
William Roberts81e1f902015-06-03 21:57:47 -07001064 char *token;
1065 char *saveptr;
1066 bool is_never_allow;
1067 bool found_whitespace;
1068
1069 size_t lineno = 0;
1070 char *name = NULL;
1071 char *value = NULL;
William Roberts610a4b12013-10-15 18:26:00 -07001072 size_t token_cnt = 0;
William Robertsf0e0a942012-08-27 15:41:15 -07001073
William Roberts81e1f902015-06-03 21:57:47 -07001074 char line_buf[BUFSIZ];
1075 kvp keys[KVP_NUM_OF_RULES];
William Robertsf0e0a942012-08-27 15:41:15 -07001076
William Roberts81e1f902015-06-03 21:57:47 -07001077 while (fgets(line_buf, sizeof(line_buf) - 1, in_file->file)) {
William Robertsf0e0a942012-08-27 15:41:15 -07001078 lineno++;
William Roberts81e1f902015-06-03 21:57:47 -07001079 is_never_allow = false;
1080 found_whitespace = false;
1081 log_info("Got line %zu\n", lineno);
William Robertsf0e0a942012-08-27 15:41:15 -07001082 len = strlen(line_buf);
1083 if (line_buf[len - 1] == '\n')
Alice Chuf6647eb2012-10-30 16:27:00 -07001084 line_buf[len - 1] = '\0';
William Robertsf0e0a942012-08-27 15:41:15 -07001085 p = line_buf;
William Roberts81e1f902015-06-03 21:57:47 -07001086
1087 /* neverallow lines must start with neverallow (ie ^neverallow) */
1088 if (!strncasecmp(p, "neverallow", strlen("neverallow"))) {
1089 p += strlen("neverallow");
1090 is_never_allow = true;
1091 }
1092
1093 /* strip trailing whitespace skip comments */
1094 while (isspace(*p)) {
William Robertsf0e0a942012-08-27 15:41:15 -07001095 p++;
William Roberts81e1f902015-06-03 21:57:47 -07001096 found_whitespace = true;
1097 }
Alice Chuf6647eb2012-10-30 16:27:00 -07001098 if (*p == '#' || *p == '\0')
William Robertsf0e0a942012-08-27 15:41:15 -07001099 continue;
1100
1101 token = strtok_r(p, " \t", &saveptr);
1102 if (!token)
1103 goto err;
1104
1105 token_cnt = 0;
1106 memset(keys, 0, sizeof(kvp) * KVP_NUM_OF_RULES);
1107 while (1) {
William Roberts0ae3a8a2012-09-04 11:51:04 -07001108
William Robertsf0e0a942012-08-27 15:41:15 -07001109 name = token;
1110 value = strchr(name, '=');
1111 if (!value)
1112 goto err;
1113 *value++ = 0;
1114
1115 keys[token_cnt].key = strdup(name);
1116 if (!keys[token_cnt].key)
1117 goto oom;
1118
1119 keys[token_cnt].value = strdup(value);
1120 if (!keys[token_cnt].value)
1121 goto oom;
1122
1123 token_cnt++;
1124
1125 token = strtok_r(NULL, " \t", &saveptr);
1126 if (!token)
1127 break;
1128
1129 } /*End token parsing */
1130
William Roberts81e1f902015-06-03 21:57:47 -07001131 rule_map *r = rule_map_new(keys, token_cnt, lineno, in_file->name, is_never_allow);
Stephen Smalley534fb072015-02-13 14:06:08 -05001132 if (!r)
1133 goto err;
William Robertsf0e0a942012-08-27 15:41:15 -07001134 rule_add(r);
1135
1136 } /* End file parsing */
1137 return;
1138
1139err:
William Robertsefebf972016-01-29 10:34:04 -08001140 log_error("Reading file: \"%s\" line: %zu name: \"%s\" value: \"%s\"\n",
William Roberts81e1f902015-06-03 21:57:47 -07001141 in_file->name, lineno, name, value);
1142 if(found_whitespace && name && !strcasecmp(name, "neverallow")) {
1143 log_error("perhaps whitespace before neverallow\n");
1144 }
William Robertsf0e0a942012-08-27 15:41:15 -07001145 exit(EXIT_FAILURE);
1146oom:
1147 log_error("In function %s: Out of memory\n", __FUNCTION__);
1148 exit(EXIT_FAILURE);
1149}
1150
1151/**
William Roberts81e1f902015-06-03 21:57:47 -07001152 * Parses the seapp_contexts file and neverallow file
1153 * and adds them to the hash table and ordered list entries
1154 * when it encounters them.
1155 * Calls exit on failure.
1156 */
1157static void parse() {
1158
1159 file_info *current;
1160 list_element *cursor;
1161 list_for_each(&input_file_list, cursor) {
1162 current = list_entry(cursor, typeof(*current), listify);
1163 parse_file(current);
1164 }
1165}
1166
1167static void validate() {
1168
1169 list_element *cursor, *v;
1170 bool found_issues = false;
1171 hash_entry *e;
1172 rule_map *r;
1173 list_for_each(&line_order_list, cursor) {
1174 e = list_entry(cursor, typeof(*e), listify);
1175 rule_map_validate(e->r);
1176 }
1177
1178 list_for_each(&line_order_list, cursor) {
1179 e = list_entry(cursor, typeof(*e), listify);
1180 r = e->r;
1181 list_for_each(&r->violations, v) {
1182 found_issues = true;
1183 log_error("Rule in File \"%s\" on line %d: \"", e->r->filename, e->r->lineno);
1184 rule_map_print(stderr, e->r);
1185 r = list_entry(v, rule_map, listify);
1186 fprintf(stderr, "\" violates neverallow in File \"%s\" on line %d: \"", r->filename, r->lineno);
1187 rule_map_print(stderr, r);
1188 fprintf(stderr, "\"\n");
1189 }
1190 }
1191
1192 if (found_issues) {
1193 exit(EXIT_FAILURE);
1194 }
1195}
1196
1197/**
William Robertsf0e0a942012-08-27 15:41:15 -07001198 * Should be called after parsing to cause the printing of the rule_maps
1199 * stored in the ordered list, head first, which preserves the "first encountered"
1200 * ordering.
1201 */
1202static void output() {
1203
William Roberts81e1f902015-06-03 21:57:47 -07001204 hash_entry *e;
1205 list_element *cursor;
William Robertsf0e0a942012-08-27 15:41:15 -07001206
William Roberts81e1f902015-06-03 21:57:47 -07001207 if (!out_file.file) {
1208 log_info("No output file, not outputting.\n");
1209 return;
1210 }
1211
1212 list_for_each(&line_order_list, cursor) {
1213 e = list_entry(cursor, hash_entry, listify);
1214 rule_map_print(out_file.file, e->r);
1215 fprintf(out_file.file, "\n");
William Robertsf0e0a942012-08-27 15:41:15 -07001216 }
1217}
1218
1219/**
1220 * This function is registered to the at exit handler and should clean up
1221 * the programs dynamic resources, such as memory and fd's.
1222 */
1223static void cleanup() {
1224
1225 /* Only close this when it was opened by me and not the crt */
William Roberts81e1f902015-06-03 21:57:47 -07001226 if (out_file.name && strcmp(out_file.name, "stdout") && out_file.file) {
1227 log_info("Closing file: %s\n", out_file.name);
1228 fclose(out_file.file);
William Robertsf0e0a942012-08-27 15:41:15 -07001229 }
1230
1231 if (pol.policy_file) {
1232
1233 log_info("Closing file: %s\n", pol.policy_file_name);
1234 fclose(pol.policy_file);
1235
1236 if (pol.db)
1237 sepol_policydb_free(pol.db);
1238
1239 if (pol.pf)
1240 sepol_policy_file_free(pol.pf);
1241
1242 if (pol.handle)
1243 sepol_handle_destroy(pol.handle);
1244 }
1245
William Roberts81e1f902015-06-03 21:57:47 -07001246 log_info("Freeing lists\n");
1247 list_free(&input_file_list);
1248 list_free(&line_order_list);
1249 list_free(&nallow_list);
William Robertsf0e0a942012-08-27 15:41:15 -07001250 hdestroy();
1251}
1252
1253int main(int argc, char *argv[]) {
1254 if (!hcreate(TABLE_SIZE)) {
1255 log_error("Could not create hash table: %s\n", strerror(errno));
1256 exit(EXIT_FAILURE);
1257 }
1258 atexit(cleanup);
1259 handle_options(argc, argv);
1260 init();
1261 log_info("Starting to parse\n");
1262 parse();
1263 log_info("Parsing completed, generating output\n");
William Roberts81e1f902015-06-03 21:57:47 -07001264 validate();
William Robertsf0e0a942012-08-27 15:41:15 -07001265 output();
1266 log_info("Success, generated output\n");
1267 exit(EXIT_SUCCESS);
1268}