1 /*
2 * File contexts backend for labeling system
3 *
4 * Author : Eamon Walsh <ewalsh@tycho.nsa.gov>
5 * Author : Stephen Smalley <sds@tycho.nsa.gov>
6 */
7
8 #include <assert.h>
9 #include <fcntl.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <limits.h>
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <sys/mman.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include "callbacks.h"
23 #include "label_internal.h"
24 #include "label_file.h"
25
26 /*
27 * Internals, mostly moved over from matchpathcon.c
28 */
29
30 /* return the length of the text that is the stem of a file name */
get_stem_from_file_name(const char * const buf)31 static int get_stem_from_file_name(const char *const buf)
32 {
33 const char *tmp = strchr(buf + 1, '/');
34
35 if (!tmp)
36 return 0;
37 return tmp - buf;
38 }
39
40 /* find the stem of a file name, returns the index into stem_arr (or -1 if
41 * there is no match - IE for a file in the root directory or a regex that is
42 * too complex for us). */
find_stem_from_file(struct saved_data * data,const char * key)43 static int find_stem_from_file(struct saved_data *data, const char *key)
44 {
45 int i;
46 int stem_len = get_stem_from_file_name(key);
47
48 if (!stem_len)
49 return -1;
50 for (i = 0; i < data->num_stems; i++) {
51 if (stem_len == data->stem_arr[i].len
52 && !strncmp(key, data->stem_arr[i].buf, stem_len)) {
53 return i;
54 }
55 }
56 return -1;
57 }
58
59 /*
60 * Warn about duplicate specifications.
61 */
nodups_specs(struct saved_data * data,const char * path)62 static int nodups_specs(struct saved_data *data, const char *path)
63 {
64 int rc = 0;
65 unsigned int ii, jj;
66 struct spec *curr_spec, *spec_arr = data->spec_arr;
67
68 for (ii = 0; ii < data->nspec; ii++) {
69 curr_spec = &spec_arr[ii];
70 for (jj = ii + 1; jj < data->nspec; jj++) {
71 if ((!strcmp(spec_arr[jj].regex_str,
72 curr_spec->regex_str))
73 && (!spec_arr[jj].mode || !curr_spec->mode
74 || spec_arr[jj].mode == curr_spec->mode)) {
75 rc = -1;
76 errno = EINVAL;
77 if (strcmp(spec_arr[jj].lr.ctx_raw,
78 curr_spec->lr.ctx_raw)) {
79 COMPAT_LOG
80 (SELINUX_ERROR,
81 "%s: Multiple different specifications for %s (%s and %s).\n",
82 path, curr_spec->regex_str,
83 spec_arr[jj].lr.ctx_raw,
84 curr_spec->lr.ctx_raw);
85 } else {
86 COMPAT_LOG
87 (SELINUX_ERROR,
88 "%s: Multiple same specifications for %s.\n",
89 path, curr_spec->regex_str);
90 }
91 }
92 }
93 }
94 return rc;
95 }
96
process_text_file(FILE * fp,const char * prefix,struct selabel_handle * rec,const char * path)97 static int process_text_file(FILE *fp, const char *prefix,
98 struct selabel_handle *rec, const char *path)
99 {
100 int rc;
101 size_t line_len;
102 unsigned int lineno = 0;
103 char *line_buf = NULL;
104
105 while (getline(&line_buf, &line_len, fp) > 0) {
106 rc = process_line(rec, path, prefix, line_buf, ++lineno);
107 if (rc)
108 goto out;
109 }
110 rc = 0;
111 out:
112 free(line_buf);
113 return rc;
114 }
115
load_mmap(FILE * fp,size_t len,struct selabel_handle * rec,const char * path)116 static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
117 const char *path)
118 {
119 struct saved_data *data = (struct saved_data *)rec->data;
120 int rc;
121 char *addr, *str_buf;
122 int *stem_map;
123 struct mmap_area *mmap_area;
124 uint32_t i, magic, version;
125 uint32_t entry_len, stem_map_len, regex_array_len;
126 const char *reg_version;
127 const char *reg_arch;
128 char reg_arch_matches = 0;
129
130 mmap_area = malloc(sizeof(*mmap_area));
131 if (!mmap_area) {
132 return -1;
133 }
134
135 addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
136 if (addr == MAP_FAILED) {
137 free(mmap_area);
138 perror("mmap");
139 return -1;
140 }
141
142 /* save where we mmap'd the file to cleanup on close() */
143 mmap_area->addr = mmap_area->next_addr = addr;
144 mmap_area->len = mmap_area->next_len = len;
145 mmap_area->next = data->mmap_areas;
146 data->mmap_areas = mmap_area;
147
148 /* check if this looks like an fcontext file */
149 rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
150 if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
151 return -1;
152
153 /* check if this version is higher than we understand */
154 rc = next_entry(&version, mmap_area, sizeof(uint32_t));
155 if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
156 return -1;
157
158 reg_version = regex_version();
159 if (!reg_version)
160 return -1;
161
162 reg_arch = regex_arch_string();
163 if (!reg_arch)
164 return -1;
165
166 if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
167
168 len = strlen(reg_version);
169
170 rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t));
171 if (rc < 0)
172 return -1;
173
174 /* Check version lengths */
175 if (len != entry_len)
176 return -1;
177
178 /* Check if regex version mismatch */
179 str_buf = malloc(entry_len + 1);
180 if (!str_buf)
181 return -1;
182
183 rc = next_entry(str_buf, mmap_area, entry_len);
184 if (rc < 0) {
185 free(str_buf);
186 return -1;
187 }
188
189 str_buf[entry_len] = '\0';
190 if ((strcmp(str_buf, reg_version) != 0)) {
191 COMPAT_LOG(SELINUX_ERROR,
192 "Regex version mismatch, expected: %s actual: %s\n",
193 reg_version, str_buf);
194 free(str_buf);
195 return -1;
196 }
197 free(str_buf);
198
199 if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) {
200 len = strlen(reg_arch);
201
202 rc = next_entry(&entry_len, mmap_area,
203 sizeof(uint32_t));
204 if (rc < 0)
205 return -1;
206
207 /* Check arch string lengths */
208 if (len != entry_len) {
209 /*
210 * Skip the entry and conclude that we have
211 * a mismatch, which is not fatal.
212 */
213 next_entry(NULL, mmap_area, entry_len);
214 goto end_arch_check;
215 }
216
217 /* Check if arch string mismatch */
218 str_buf = malloc(entry_len + 1);
219 if (!str_buf)
220 return -1;
221
222 rc = next_entry(str_buf, mmap_area, entry_len);
223 if (rc < 0) {
224 free(str_buf);
225 return -1;
226 }
227
228 str_buf[entry_len] = '\0';
229 reg_arch_matches = strcmp(str_buf, reg_arch) == 0;
230 free(str_buf);
231 }
232 }
233 end_arch_check:
234
235 /* allocate the stems_data array */
236 rc = next_entry(&stem_map_len, mmap_area, sizeof(uint32_t));
237 if (rc < 0)
238 return -1;
239
240 /*
241 * map indexed by the stem # in the mmap file and contains the stem
242 * number in the data stem_arr
243 */
244 stem_map = calloc(stem_map_len, sizeof(*stem_map));
245 if (!stem_map)
246 return -1;
247
248 for (i = 0; i < stem_map_len; i++) {
249 char *buf;
250 uint32_t stem_len;
251 int newid;
252
253 /* the length does not include the nul */
254 rc = next_entry(&stem_len, mmap_area, sizeof(uint32_t));
255 if (rc < 0 || !stem_len) {
256 rc = -1;
257 goto out;
258 }
259
260 /* Check for stem_len wrap around. */
261 if (stem_len < UINT32_MAX) {
262 buf = (char *)mmap_area->next_addr;
263 /* Check if over-run before null check. */
264 rc = next_entry(NULL, mmap_area, (stem_len + 1));
265 if (rc < 0)
266 goto out;
267
268 if (buf[stem_len] != '\0') {
269 rc = -1;
270 goto out;
271 }
272 } else {
273 rc = -1;
274 goto out;
275 }
276
277 /* store the mapping between old and new */
278 newid = find_stem(data, buf, stem_len);
279 if (newid < 0) {
280 newid = store_stem(data, buf, stem_len);
281 if (newid < 0) {
282 rc = newid;
283 goto out;
284 }
285 data->stem_arr[newid].from_mmap = 1;
286 }
287 stem_map[i] = newid;
288 }
289
290 /* allocate the regex array */
291 rc = next_entry(®ex_array_len, mmap_area, sizeof(uint32_t));
292 if (rc < 0 || !regex_array_len) {
293 rc = -1;
294 goto out;
295 }
296
297 for (i = 0; i < regex_array_len; i++) {
298 struct spec *spec;
299 int32_t stem_id, meta_chars;
300 uint32_t mode = 0, prefix_len = 0;
301
302 rc = grow_specs(data);
303 if (rc < 0)
304 goto out;
305
306 spec = &data->spec_arr[data->nspec];
307 spec->from_mmap = 1;
308
309 /* Process context */
310 rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t));
311 if (rc < 0 || !entry_len) {
312 rc = -1;
313 goto out;
314 }
315
316 str_buf = malloc(entry_len);
317 if (!str_buf) {
318 rc = -1;
319 goto out;
320 }
321 rc = next_entry(str_buf, mmap_area, entry_len);
322 if (rc < 0) {
323 free(str_buf);
324 goto out;
325 }
326
327 if (str_buf[entry_len - 1] != '\0') {
328 free(str_buf);
329 rc = -1;
330 goto out;
331 }
332 spec->lr.ctx_raw = str_buf;
333
334 if (strcmp(spec->lr.ctx_raw, "<<none>>") && rec->validating) {
335 if (selabel_validate(rec, &spec->lr) < 0) {
336 selinux_log(SELINUX_ERROR,
337 "%s: context %s is invalid\n",
338 path, spec->lr.ctx_raw);
339 goto out;
340 }
341 }
342
343 /* Process regex string */
344 rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t));
345 if (rc < 0 || !entry_len) {
346 rc = -1;
347 goto out;
348 }
349
350 spec->regex_str = (char *)mmap_area->next_addr;
351 rc = next_entry(NULL, mmap_area, entry_len);
352 if (rc < 0)
353 goto out;
354
355 if (spec->regex_str[entry_len - 1] != '\0') {
356 rc = -1;
357 goto out;
358 }
359
360 /* Process mode */
361 if (version >= SELINUX_COMPILED_FCONTEXT_MODE)
362 rc = next_entry(&mode, mmap_area, sizeof(uint32_t));
363 else
364 rc = next_entry(&mode, mmap_area, sizeof(mode_t));
365 if (rc < 0)
366 goto out;
367
368 spec->mode = mode;
369
370 /* map the stem id from the mmap file to the data->stem_arr */
371 rc = next_entry(&stem_id, mmap_area, sizeof(int32_t));
372 if (rc < 0)
373 goto out;
374
375 if (stem_id < 0 || stem_id >= (int32_t)stem_map_len)
376 spec->stem_id = -1;
377 else
378 spec->stem_id = stem_map[stem_id];
379
380 /* retrieve the hasMetaChars bit */
381 rc = next_entry(&meta_chars, mmap_area, sizeof(uint32_t));
382 if (rc < 0)
383 goto out;
384
385 spec->hasMetaChars = meta_chars;
386 /* and prefix length for use by selabel_lookup_best_match */
387 if (version >= SELINUX_COMPILED_FCONTEXT_PREFIX_LEN) {
388 rc = next_entry(&prefix_len, mmap_area,
389 sizeof(uint32_t));
390 if (rc < 0)
391 goto out;
392
393 spec->prefix_len = prefix_len;
394 }
395
396 rc = regex_load_mmap(mmap_area, &spec->regex, reg_arch_matches,
397 &spec->regex_compiled);
398 if (rc < 0)
399 goto out;
400
401 __pthread_mutex_init(&spec->regex_lock, NULL);
402 data->nspec++;
403 }
404
405 rc = 0;
406 out:
407 free(stem_map);
408
409 return rc;
410 }
411
412 struct file_details {
413 const char *suffix;
414 struct stat sb;
415 };
416
rolling_append(char * current,const char * suffix,size_t max)417 static char *rolling_append(char *current, const char *suffix, size_t max)
418 {
419 size_t size;
420 size_t suffix_size;
421 size_t current_size;
422
423 if (!suffix)
424 return current;
425
426 current_size = strlen(current);
427 suffix_size = strlen(suffix);
428
429 size = current_size + suffix_size;
430 if (size < current_size || size < suffix_size)
431 return NULL;
432
433 /* ensure space for the '.' and the '\0' characters. */
434 if (size >= (SIZE_MAX - 2))
435 return NULL;
436
437 size += 2;
438
439 if (size > max)
440 return NULL;
441
442 /* Append any given suffix */
443 char *to = current + current_size;
444 *to++ = '.';
445 strcpy(to, suffix);
446
447 return current;
448 }
449
fcontext_is_binary(FILE * fp)450 static bool fcontext_is_binary(FILE *fp)
451 {
452 uint32_t magic;
453
454 size_t len = fread(&magic, sizeof(magic), 1, fp);
455 rewind(fp);
456
457 return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
458 }
459
460 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
461
open_file(const char * path,const char * suffix,char * save_path,size_t len,struct stat * sb,bool open_oldest)462 static FILE *open_file(const char *path, const char *suffix,
463 char *save_path, size_t len, struct stat *sb, bool open_oldest)
464 {
465 unsigned int i;
466 int rc;
467 char stack_path[len];
468 struct file_details *found = NULL;
469
470 /*
471 * Rolling append of suffix. Try to open with path.suffix then the
472 * next as path.suffix.suffix and so forth.
473 */
474 struct file_details fdetails[2] = {
475 { .suffix = suffix },
476 { .suffix = "bin" }
477 };
478
479 rc = snprintf(stack_path, sizeof(stack_path), "%s", path);
480 if (rc >= (int) sizeof(stack_path)) {
481 errno = ENAMETOOLONG;
482 return NULL;
483 }
484
485 for (i = 0; i < ARRAY_SIZE(fdetails); i++) {
486
487 /* This handles the case if suffix is null */
488 path = rolling_append(stack_path, fdetails[i].suffix,
489 sizeof(stack_path));
490 if (!path)
491 return NULL;
492
493 rc = stat(path, &fdetails[i].sb);
494 if (rc)
495 continue;
496
497 /* first file thing found, just take it */
498 if (!found) {
499 strcpy(save_path, path);
500 found = &fdetails[i];
501 continue;
502 }
503
504 /*
505 * Keep picking the newest file found. Where "newest"
506 * includes equality. This provides a precedence on
507 * secondary suffixes even when the timestamp is the
508 * same. Ie choose file_contexts.bin over file_contexts
509 * even if the time stamp is the same. Invert this logic
510 * on open_oldest set to true. The idea is that if the
511 * newest file failed to process, we can attempt to
512 * process the oldest. The logic here is subtle and depends
513 * on the array ordering in fdetails for the case when time
514 * stamps are the same.
515 */
516 if (open_oldest ^
517 (fdetails[i].sb.st_mtime >= found->sb.st_mtime)) {
518 found = &fdetails[i];
519 strcpy(save_path, path);
520 }
521 }
522
523 if (!found) {
524 errno = ENOENT;
525 return NULL;
526 }
527
528 memcpy(sb, &found->sb, sizeof(*sb));
529 return fopen(save_path, "re");
530 }
531
process_file(const char * path,const char * suffix,struct selabel_handle * rec,const char * prefix,struct selabel_digest * digest)532 static int process_file(const char *path, const char *suffix,
533 struct selabel_handle *rec,
534 const char *prefix, struct selabel_digest *digest)
535 {
536 int rc;
537 unsigned int i;
538 struct stat sb;
539 FILE *fp = NULL;
540 char found_path[PATH_MAX];
541
542 /*
543 * On the first pass open the newest modified file. If it fails to
544 * process, then the second pass shall open the oldest file. If both
545 * passes fail, then it's a fatal error.
546 */
547 for (i = 0; i < 2; i++) {
548 fp = open_file(path, suffix, found_path, sizeof(found_path),
549 &sb, i > 0);
550 if (fp == NULL)
551 return -1;
552
553 rc = fcontext_is_binary(fp) ?
554 load_mmap(fp, sb.st_size, rec, found_path) :
555 process_text_file(fp, prefix, rec, found_path);
556 if (!rc)
557 rc = digest_add_specfile(digest, fp, NULL, sb.st_size,
558 found_path);
559
560 fclose(fp);
561
562 if (!rc)
563 return 0;
564 }
565 return -1;
566 }
567
selabel_subs_fini(struct selabel_sub * ptr)568 static void selabel_subs_fini(struct selabel_sub *ptr)
569 {
570 struct selabel_sub *next;
571
572 while (ptr) {
573 next = ptr->next;
574 free(ptr->src);
575 free(ptr->dst);
576 free(ptr);
577 ptr = next;
578 }
579 }
580
selabel_sub(struct selabel_sub * ptr,const char * src)581 static char *selabel_sub(struct selabel_sub *ptr, const char *src)
582 {
583 char *dst = NULL;
584 int len;
585
586 while (ptr) {
587 if (strncmp(src, ptr->src, ptr->slen) == 0 ) {
588 if (src[ptr->slen] == '/' ||
589 src[ptr->slen] == 0) {
590 if ((src[ptr->slen] == '/') &&
591 (strcmp(ptr->dst, "/") == 0))
592 len = ptr->slen + 1;
593 else
594 len = ptr->slen;
595 if (asprintf(&dst, "%s%s", ptr->dst, &src[len]) < 0)
596 return NULL;
597 return dst;
598 }
599 }
600 ptr = ptr->next;
601 }
602 return NULL;
603 }
604
selabel_subs_init(const char * path,struct selabel_digest * digest,struct selabel_sub ** out_subs)605 static int selabel_subs_init(const char *path, struct selabel_digest *digest,
606 struct selabel_sub **out_subs)
607 {
608 char buf[1024];
609 FILE *cfg = fopen(path, "re");
610 struct selabel_sub *list = NULL, *sub = NULL;
611 struct stat sb;
612 int status = -1;
613
614 *out_subs = NULL;
615 if (!cfg) {
616 /* If the file does not exist, it is not fatal */
617 return (errno == ENOENT) ? 0 : -1;
618 }
619
620 if (fstat(fileno(cfg), &sb) < 0)
621 goto out;
622
623 while (fgets_unlocked(buf, sizeof(buf) - 1, cfg)) {
624 char *ptr = NULL;
625 char *src = buf;
626 char *dst = NULL;
627
628 while (*src && isspace(*src))
629 src++;
630 if (src[0] == '#') continue;
631 ptr = src;
632 while (*ptr && ! isspace(*ptr))
633 ptr++;
634 *ptr++ = '\0';
635 if (! *src) continue;
636
637 dst = ptr;
638 while (*dst && isspace(*dst))
639 dst++;
640 ptr = dst;
641 while (*ptr && ! isspace(*ptr))
642 ptr++;
643 *ptr = '\0';
644 if (! *dst)
645 continue;
646
647 sub = malloc(sizeof(*sub));
648 if (! sub)
649 goto err;
650 memset(sub, 0, sizeof(*sub));
651
652 sub->src = strdup(src);
653 if (! sub->src)
654 goto err;
655
656 sub->dst = strdup(dst);
657 if (! sub->dst)
658 goto err;
659
660 sub->slen = strlen(src);
661 sub->next = list;
662 list = sub;
663 sub = NULL;
664 }
665
666 if (digest_add_specfile(digest, cfg, NULL, sb.st_size, path) < 0)
667 goto err;
668
669 *out_subs = list;
670 status = 0;
671
672 out:
673 fclose(cfg);
674 return status;
675 err:
676 if (sub)
677 free(sub->src);
678 free(sub);
679 while (list) {
680 sub = list->next;
681 free(list->src);
682 free(list->dst);
683 free(list);
684 list = sub;
685 }
686 goto out;
687 }
688
selabel_sub_key(struct saved_data * data,const char * key)689 static char *selabel_sub_key(struct saved_data *data, const char *key)
690 {
691 char *ptr = NULL;
692 char *dptr = NULL;
693
694 ptr = selabel_sub(data->subs, key);
695 if (ptr) {
696 dptr = selabel_sub(data->dist_subs, ptr);
697 if (dptr) {
698 free(ptr);
699 ptr = dptr;
700 }
701 } else {
702 ptr = selabel_sub(data->dist_subs, key);
703 }
704 if (ptr)
705 return ptr;
706
707 return NULL;
708 }
709
710 static void closef(struct selabel_handle *rec);
711
init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned n)712 static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
713 unsigned n)
714 {
715 struct saved_data *data = (struct saved_data *)rec->data;
716 const char *path = NULL;
717 const char *prefix = NULL;
718 int status = -1, baseonly = 0;
719
720 /* Process arguments */
721 while (n--)
722 switch(opts[n].type) {
723 case SELABEL_OPT_PATH:
724 path = opts[n].value;
725 break;
726 case SELABEL_OPT_SUBSET:
727 prefix = opts[n].value;
728 break;
729 case SELABEL_OPT_BASEONLY:
730 baseonly = !!opts[n].value;
731 break;
732 }
733
734 #if !defined(BUILD_HOST) && !defined(ANDROID)
735 char subs_file[PATH_MAX + 1];
736 /* Process local and distribution substitution files */
737 if (!path) {
738 status = selabel_subs_init(
739 selinux_file_context_subs_dist_path(),
740 rec->digest, &data->dist_subs);
741 if (status)
742 goto finish;
743 status = selabel_subs_init(selinux_file_context_subs_path(),
744 rec->digest, &data->subs);
745 if (status)
746 goto finish;
747 path = selinux_file_context_path();
748 } else {
749 snprintf(subs_file, sizeof(subs_file), "%s.subs_dist", path);
750 status = selabel_subs_init(subs_file, rec->digest,
751 &data->dist_subs);
752 if (status)
753 goto finish;
754 snprintf(subs_file, sizeof(subs_file), "%s.subs", path);
755 status = selabel_subs_init(subs_file, rec->digest,
756 &data->subs);
757 if (status)
758 goto finish;
759 }
760
761 #endif
762
763 if (!path)
764 goto finish;
765
766 rec->spec_file = strdup(path);
767
768 /*
769 * The do detailed validation of the input and fill the spec array
770 */
771 status = process_file(path, NULL, rec, prefix, rec->digest);
772 if (status)
773 goto finish;
774
775 if (rec->validating) {
776 status = nodups_specs(data, path);
777 if (status)
778 goto finish;
779 }
780
781 if (!baseonly) {
782 status = process_file(path, "homedirs", rec, prefix,
783 rec->digest);
784 if (status && errno != ENOENT)
785 goto finish;
786
787 status = process_file(path, "local", rec, prefix,
788 rec->digest);
789 if (status && errno != ENOENT)
790 goto finish;
791 }
792
793 digest_gen_hash(rec->digest);
794
795 status = sort_specs(data);
796
797 finish:
798 if (status)
799 closef(rec);
800
801 return status;
802 }
803
804 /*
805 * Backend interface routines
806 */
closef(struct selabel_handle * rec)807 static void closef(struct selabel_handle *rec)
808 {
809 struct saved_data *data = (struct saved_data *)rec->data;
810 struct mmap_area *area, *last_area;
811 struct spec *spec;
812 struct stem *stem;
813 unsigned int i;
814
815 selabel_subs_fini(data->subs);
816 selabel_subs_fini(data->dist_subs);
817
818 for (i = 0; i < data->nspec; i++) {
819 spec = &data->spec_arr[i];
820 free(spec->lr.ctx_trans);
821 free(spec->lr.ctx_raw);
822 regex_data_free(spec->regex);
823 __pthread_mutex_destroy(&spec->regex_lock);
824 if (spec->from_mmap)
825 continue;
826 free(spec->regex_str);
827 free(spec->type_str);
828 }
829
830 for (i = 0; i < (unsigned int)data->num_stems; i++) {
831 stem = &data->stem_arr[i];
832 if (stem->from_mmap)
833 continue;
834 free(stem->buf);
835 }
836
837 if (data->spec_arr)
838 free(data->spec_arr);
839 if (data->stem_arr)
840 free(data->stem_arr);
841
842 area = data->mmap_areas;
843 while (area) {
844 munmap(area->addr, area->len);
845 last_area = area;
846 area = area->next;
847 free(last_area);
848 }
849 free(data);
850 }
851
852 // Finds all the matches of |key| in the given context. Returns the result in
853 // the allocated array and updates the match count. If match_count is NULL,
854 // stops early once the 1st match is found.
lookup_all(struct selabel_handle * rec,const char * key,int type,bool partial,size_t * match_count)855 static struct spec **lookup_all(struct selabel_handle *rec,
856 const char *key,
857 int type,
858 bool partial,
859 size_t *match_count)
860 {
861 struct saved_data *data = (struct saved_data *)rec->data;
862 struct spec *spec_arr = data->spec_arr;
863 int i, rc, file_stem;
864 size_t len;
865 mode_t mode = (mode_t)type;
866 char *clean_key = NULL;
867 const char *prev_slash, *next_slash;
868 unsigned int sofar = 0;
869 char *sub = NULL;
870
871 struct spec **result = NULL;
872 if (match_count) {
873 *match_count = 0;
874 result = calloc(data->nspec, sizeof(struct spec*));
875 } else {
876 result = calloc(1, sizeof(struct spec*));
877 }
878 if (!result) {
879 selinux_log(SELINUX_ERROR, "Failed to allocate %zu bytes of data\n",
880 data->nspec * sizeof(struct spec*));
881 goto finish;
882 }
883
884 if (!data->nspec) {
885 errno = ENOENT;
886 goto finish;
887 }
888
889 /* Remove duplicate slashes */
890 if ((next_slash = strstr(key, "//"))) {
891 clean_key = (char *) malloc(strlen(key) + 1);
892 if (!clean_key)
893 goto finish;
894 prev_slash = key;
895 while (next_slash) {
896 memcpy(clean_key + sofar, prev_slash, next_slash - prev_slash);
897 sofar += next_slash - prev_slash;
898 prev_slash = next_slash + 1;
899 next_slash = strstr(prev_slash, "//");
900 }
901 strcpy(clean_key + sofar, prev_slash);
902 key = clean_key;
903 }
904
905 /* remove trailing slash */
906 len = strlen(key);
907 if (len == 0) {
908 errno = EINVAL;
909 goto finish;
910 }
911
912 if (len > 1 && key[len - 1] == '/') {
913 /* reuse clean_key from above if available */
914 if (!clean_key) {
915 clean_key = (char *) malloc(len);
916 if (!clean_key)
917 goto finish;
918
919 memcpy(clean_key, key, len - 1);
920 }
921
922 clean_key[len - 1] = '\0';
923 key = clean_key;
924 }
925
926 sub = selabel_sub_key(data, key);
927 if (sub)
928 key = sub;
929
930 file_stem = find_stem_from_file(data, key);
931 mode &= S_IFMT;
932
933 /*
934 * Check for matching specifications in reverse order, so that
935 * the last matching specification is used.
936 */
937 for (i = data->nspec - 1; i >= 0; i--) {
938 struct spec *spec = &spec_arr[i];
939 /* if the spec in question matches no stem or has the same
940 * stem as the file AND if the spec in question has no mode
941 * specified or if the mode matches the file mode then we do
942 * a regex check */
943 bool stem_matches = spec->stem_id == -1 || spec->stem_id == file_stem;
944 // Don't check the stem if we want to find partial matches.
945 // Otherwise the case "/abc/efg/(/.*)?" will be considered
946 //a miss for "/abc".
947 if ((partial || stem_matches) &&
948 (!mode || !spec->mode || mode == spec->mode)) {
949 if (compile_regex(spec, NULL) < 0)
950 goto finish;
951 rc = regex_match(spec->regex, key, partial);
952 if (rc == REGEX_MATCH || (partial && rc == REGEX_MATCH_PARTIAL)) {
953 if (rc == REGEX_MATCH) {
954 spec->matches++;
955 }
956
957 if (strcmp(spec_arr[i].lr.ctx_raw, "<<none>>") == 0) {
958 errno = ENOENT;
959 goto finish;
960 }
961
962 if (match_count) {
963 result[*match_count] = spec;
964 *match_count += 1;
965 // Continue to find all the matches.
966 continue;
967 }
968 result[0] = spec;
969 break;
970 }
971
972 if (rc == REGEX_NO_MATCH)
973 continue;
974
975 errno = ENOENT;
976 /* else it's an error */
977 goto finish;
978 }
979 }
980 if (!result[0])
981 errno = ENOENT;
982
983 finish:
984 free(clean_key);
985 free(sub);
986 if (result && !result[0]) {
987 free(result);
988 result = NULL;
989 }
990 return result;
991 }
992
lookup_common(struct selabel_handle * rec,const char * key,int type,bool partial)993 static struct spec *lookup_common(struct selabel_handle *rec,
994 const char *key,
995 int type,
996 bool partial) {
997 struct spec **matches = lookup_all(rec, key, type, partial, NULL);
998 if (!matches) {
999 return NULL;
1000 }
1001 struct spec *result = matches[0];
1002 free(matches);
1003 return result;
1004 }
1005
1006 /*
1007 * Returns true if the digest of all partial matched contexts is the same as
1008 * the one saved by setxattr, otherwise returns false. The length of the SHA1
1009 * digest will always be returned. The caller must free any returned digests.
1010 */
get_digests_all_partial_matches(struct selabel_handle * rec,const char * pathname,uint8_t ** calculated_digest,uint8_t ** xattr_digest,size_t * digest_len)1011 static bool get_digests_all_partial_matches(struct selabel_handle *rec,
1012 const char *pathname,
1013 uint8_t **calculated_digest,
1014 uint8_t **xattr_digest,
1015 size_t *digest_len)
1016 {
1017 uint8_t read_digest[SHA1_HASH_SIZE];
1018 ssize_t read_size = getxattr(pathname, RESTORECON_PARTIAL_MATCH_DIGEST,
1019 read_digest, SHA1_HASH_SIZE
1020 #ifdef __APPLE__
1021 , 0, 0
1022 #endif /* __APPLE __ */
1023 );
1024 uint8_t hash_digest[SHA1_HASH_SIZE];
1025 bool status = selabel_hash_all_partial_matches(rec, pathname,
1026 hash_digest);
1027
1028 *xattr_digest = NULL;
1029 *calculated_digest = NULL;
1030 *digest_len = SHA1_HASH_SIZE;
1031
1032 if (read_size == SHA1_HASH_SIZE) {
1033 *xattr_digest = calloc(1, SHA1_HASH_SIZE + 1);
1034 if (!*xattr_digest)
1035 goto oom;
1036
1037 memcpy(*xattr_digest, read_digest, SHA1_HASH_SIZE);
1038 }
1039
1040 if (status) {
1041 *calculated_digest = calloc(1, SHA1_HASH_SIZE + 1);
1042 if (!*calculated_digest)
1043 goto oom;
1044
1045 memcpy(*calculated_digest, hash_digest, SHA1_HASH_SIZE);
1046 }
1047
1048 if (status && read_size == SHA1_HASH_SIZE &&
1049 memcmp(read_digest, hash_digest, SHA1_HASH_SIZE) == 0)
1050 return true;
1051
1052 return false;
1053
1054 oom:
1055 selinux_log(SELINUX_ERROR, "SELinux: %s: Out of memory\n", __func__);
1056 return false;
1057 }
1058
hash_all_partial_matches(struct selabel_handle * rec,const char * key,uint8_t * digest)1059 static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key, uint8_t *digest)
1060 {
1061 assert(digest);
1062
1063 size_t total_matches;
1064 struct spec **matches = lookup_all(rec, key, 0, true, &total_matches);
1065 if (!matches) {
1066 return false;
1067 }
1068
1069 Sha1Context context;
1070 Sha1Initialise(&context);
1071 size_t i;
1072 for (i = 0; i < total_matches; i++) {
1073 char* regex_str = matches[i]->regex_str;
1074 mode_t mode = matches[i]->mode;
1075 char* ctx_raw = matches[i]->lr.ctx_raw;
1076
1077 Sha1Update(&context, regex_str, strlen(regex_str) + 1);
1078 Sha1Update(&context, &mode, sizeof(mode_t));
1079 Sha1Update(&context, ctx_raw, strlen(ctx_raw) + 1);
1080 }
1081
1082 SHA1_HASH sha1_hash;
1083 Sha1Finalise(&context, &sha1_hash);
1084 memcpy(digest, sha1_hash.bytes, SHA1_HASH_SIZE);
1085
1086 free(matches);
1087 return true;
1088 }
1089
lookup(struct selabel_handle * rec,const char * key,int type)1090 static struct selabel_lookup_rec *lookup(struct selabel_handle *rec,
1091 const char *key, int type)
1092 {
1093 struct spec *spec;
1094
1095 spec = lookup_common(rec, key, type, false);
1096 if (spec)
1097 return &spec->lr;
1098 return NULL;
1099 }
1100
partial_match(struct selabel_handle * rec,const char * key)1101 static bool partial_match(struct selabel_handle *rec, const char *key)
1102 {
1103 return lookup_common(rec, key, 0, true) ? true : false;
1104 }
1105
lookup_best_match(struct selabel_handle * rec,const char * key,const char ** aliases,int type)1106 static struct selabel_lookup_rec *lookup_best_match(struct selabel_handle *rec,
1107 const char *key,
1108 const char **aliases,
1109 int type)
1110 {
1111 size_t n, i;
1112 int best = -1;
1113 struct spec **specs;
1114 size_t prefix_len = 0;
1115 struct selabel_lookup_rec *lr = NULL;
1116
1117 if (!aliases || !aliases[0])
1118 return lookup(rec, key, type);
1119
1120 for (n = 0; aliases[n]; n++)
1121 ;
1122
1123 specs = calloc(n+1, sizeof(struct spec *));
1124 if (!specs)
1125 return NULL;
1126 specs[0] = lookup_common(rec, key, type, false);
1127 if (specs[0]) {
1128 if (!specs[0]->hasMetaChars) {
1129 /* exact match on key */
1130 lr = &specs[0]->lr;
1131 goto out;
1132 }
1133 best = 0;
1134 prefix_len = specs[0]->prefix_len;
1135 }
1136 for (i = 1; i <= n; i++) {
1137 specs[i] = lookup_common(rec, aliases[i-1], type, false);
1138 if (specs[i]) {
1139 if (!specs[i]->hasMetaChars) {
1140 /* exact match on alias */
1141 lr = &specs[i]->lr;
1142 goto out;
1143 }
1144 if (specs[i]->prefix_len > prefix_len) {
1145 best = i;
1146 prefix_len = specs[i]->prefix_len;
1147 }
1148 }
1149 }
1150
1151 if (best >= 0) {
1152 /* longest fixed prefix match on key or alias */
1153 lr = &specs[best]->lr;
1154 } else {
1155 errno = ENOENT;
1156 }
1157
1158 out:
1159 free(specs);
1160 return lr;
1161 }
1162
incomp(struct spec * spec1,struct spec * spec2,const char * reason,int i,int j)1163 static enum selabel_cmp_result incomp(struct spec *spec1, struct spec *spec2, const char *reason, int i, int j)
1164 {
1165 selinux_log(SELINUX_INFO,
1166 "selabel_cmp: mismatched %s on entry %d: (%s, %x, %s) vs entry %d: (%s, %x, %s)\n",
1167 reason,
1168 i, spec1->regex_str, spec1->mode, spec1->lr.ctx_raw,
1169 j, spec2->regex_str, spec2->mode, spec2->lr.ctx_raw);
1170 return SELABEL_INCOMPARABLE;
1171 }
1172
cmp(struct selabel_handle * h1,struct selabel_handle * h2)1173 static enum selabel_cmp_result cmp(struct selabel_handle *h1,
1174 struct selabel_handle *h2)
1175 {
1176 struct saved_data *data1 = (struct saved_data *)h1->data;
1177 struct saved_data *data2 = (struct saved_data *)h2->data;
1178 unsigned int i, nspec1 = data1->nspec, j, nspec2 = data2->nspec;
1179 struct spec *spec_arr1 = data1->spec_arr, *spec_arr2 = data2->spec_arr;
1180 struct stem *stem_arr1 = data1->stem_arr, *stem_arr2 = data2->stem_arr;
1181 bool skipped1 = false, skipped2 = false;
1182
1183 i = 0;
1184 j = 0;
1185 while (i < nspec1 && j < nspec2) {
1186 struct spec *spec1 = &spec_arr1[i];
1187 struct spec *spec2 = &spec_arr2[j];
1188
1189 /*
1190 * Because sort_specs() moves exact pathnames to the
1191 * end, we might need to skip over additional regex
1192 * entries that only exist in one of the configurations.
1193 */
1194 if (!spec1->hasMetaChars && spec2->hasMetaChars) {
1195 j++;
1196 skipped2 = true;
1197 continue;
1198 }
1199
1200 if (spec1->hasMetaChars && !spec2->hasMetaChars) {
1201 i++;
1202 skipped1 = true;
1203 continue;
1204 }
1205
1206 if (spec1->regex && spec2->regex) {
1207 if (regex_cmp(spec1->regex, spec2->regex) == SELABEL_INCOMPARABLE){
1208 return incomp(spec1, spec2, "regex", i, j);
1209 }
1210 } else {
1211 if (strcmp(spec1->regex_str, spec2->regex_str))
1212 return incomp(spec1, spec2, "regex_str", i, j);
1213 }
1214
1215 if (spec1->mode != spec2->mode)
1216 return incomp(spec1, spec2, "mode", i, j);
1217
1218 if (spec1->stem_id == -1 && spec2->stem_id != -1)
1219 return incomp(spec1, spec2, "stem_id", i, j);
1220 if (spec2->stem_id == -1 && spec1->stem_id != -1)
1221 return incomp(spec1, spec2, "stem_id", i, j);
1222 if (spec1->stem_id != -1 && spec2->stem_id != -1) {
1223 struct stem *stem1 = &stem_arr1[spec1->stem_id];
1224 struct stem *stem2 = &stem_arr2[spec2->stem_id];
1225 if (stem1->len != stem2->len ||
1226 strncmp(stem1->buf, stem2->buf, stem1->len))
1227 return incomp(spec1, spec2, "stem", i, j);
1228 }
1229
1230 if (strcmp(spec1->lr.ctx_raw, spec2->lr.ctx_raw))
1231 return incomp(spec1, spec2, "ctx_raw", i, j);
1232
1233 i++;
1234 j++;
1235 }
1236
1237 if ((skipped1 || i < nspec1) && !skipped2)
1238 return SELABEL_SUPERSET;
1239 if ((skipped2 || j < nspec2) && !skipped1)
1240 return SELABEL_SUBSET;
1241 if (skipped1 && skipped2)
1242 return SELABEL_INCOMPARABLE;
1243 return SELABEL_EQUAL;
1244 }
1245
1246
stats(struct selabel_handle * rec)1247 static void stats(struct selabel_handle *rec)
1248 {
1249 struct saved_data *data = (struct saved_data *)rec->data;
1250 unsigned int i, nspec = data->nspec;
1251 struct spec *spec_arr = data->spec_arr;
1252
1253 for (i = 0; i < nspec; i++) {
1254 if (spec_arr[i].matches == 0) {
1255 if (spec_arr[i].type_str) {
1256 COMPAT_LOG(SELINUX_WARNING,
1257 "Warning! No matches for (%s, %s, %s)\n",
1258 spec_arr[i].regex_str,
1259 spec_arr[i].type_str,
1260 spec_arr[i].lr.ctx_raw);
1261 } else {
1262 COMPAT_LOG(SELINUX_WARNING,
1263 "Warning! No matches for (%s, %s)\n",
1264 spec_arr[i].regex_str,
1265 spec_arr[i].lr.ctx_raw);
1266 }
1267 }
1268 }
1269 }
1270
selabel_file_init(struct selabel_handle * rec,const struct selinux_opt * opts,unsigned nopts)1271 int selabel_file_init(struct selabel_handle *rec,
1272 const struct selinux_opt *opts,
1273 unsigned nopts)
1274 {
1275 struct saved_data *data;
1276
1277 data = (struct saved_data *)malloc(sizeof(*data));
1278 if (!data)
1279 return -1;
1280 memset(data, 0, sizeof(*data));
1281
1282 rec->data = data;
1283 rec->func_close = &closef;
1284 rec->func_stats = &stats;
1285 rec->func_lookup = &lookup;
1286 rec->func_partial_match = &partial_match;
1287 rec->func_get_digests_all_partial_matches =
1288 &get_digests_all_partial_matches;
1289 rec->func_hash_all_partial_matches = &hash_all_partial_matches;
1290 rec->func_lookup_best_match = &lookup_best_match;
1291 rec->func_cmp = &cmp;
1292
1293 return init(rec, opts, nopts);
1294 }
1295