-
Notifications
You must be signed in to change notification settings - Fork 345
/
cil_verify.c
2022 lines (1756 loc) · 49.3 KB
/
cil_verify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011 Tresys Technology, LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of Tresys Technology, LLC.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <ctype.h>
#include <sepol/policydb/polcaps.h>
#include <sepol/errcodes.h>
#include "cil_internal.h"
#include "cil_flavor.h"
#include "cil_log.h"
#include "cil_mem.h"
#include "cil_tree.h"
#include "cil_list.h"
#include "cil_find.h"
#include "cil_stack.h"
#include "cil_verify.h"
static int __cil_is_reserved_name(const char *name, enum cil_flavor flavor)
{
switch (flavor) {
case CIL_BOOL:
case CIL_TUNABLE:
if ((name == CIL_KEY_EQ) || (name == CIL_KEY_NEQ))
return CIL_TRUE;
break;
case CIL_PERM:
case CIL_MAP_PERM:
case CIL_USER:
case CIL_USERATTRIBUTE:
case CIL_ROLE:
case CIL_ROLEATTRIBUTE:
if (name == CIL_KEY_ALL)
return CIL_TRUE;
break;
case CIL_TYPE:
case CIL_TYPEATTRIBUTE:
case CIL_TYPEALIAS:
if ((name == CIL_KEY_ALL) || (name == CIL_KEY_SELF) || (name == CIL_KEY_NOTSELF)
|| (name == CIL_KEY_OTHER))
return CIL_TRUE;
break;
case CIL_CAT:
case CIL_CATSET:
case CIL_CATALIAS:
case CIL_PERMISSIONX:
if ((name == CIL_KEY_ALL) || (name == CIL_KEY_RANGE))
return CIL_TRUE;
break;
default:
/* All of these are not used in expressions */
return CIL_FALSE;
break;
}
/* Everything not under the default case is also checked for these */
if ((name == CIL_KEY_AND) || (name == CIL_KEY_OR) || (name == CIL_KEY_NOT) || (name == CIL_KEY_XOR)) {
return CIL_TRUE;
}
return CIL_FALSE;
}
int cil_verify_name(const struct cil_db *db, const char *name, enum cil_flavor flavor)
{
int rc = SEPOL_ERR;
int len;
int i = 0;
if (name == NULL) {
cil_log(CIL_ERR, "Name is NULL\n");
goto exit;
}
len = strlen(name);
if (len >= CIL_MAX_NAME_LENGTH) {
cil_log(CIL_ERR, "Name length greater than max name length of %d",
CIL_MAX_NAME_LENGTH);
rc = SEPOL_ERR;
goto exit;
}
if (!isalpha(name[0])) {
cil_log(CIL_ERR, "First character in %s is not a letter\n", name);
goto exit;
}
if (db->qualified_names == CIL_FALSE) {
for (i = 1; i < len; i++) {
if (!isalnum(name[i]) && name[i] != '_' && name[i] != '-') {
cil_log(CIL_ERR, "Invalid character \"%c\" in %s\n", name[i], name);
goto exit;
}
}
} else {
for (i = 1; i < len; i++) {
if (!isalnum(name[i]) && name[i] != '_' && name[i] != '-' && name[i] != '.') {
cil_log(CIL_ERR, "Invalid character \"%c\" in %s\n", name[i], name);
goto exit;
}
}
}
if (__cil_is_reserved_name(name, flavor)) {
cil_log(CIL_ERR, "Name %s is a reserved word\n", name);
goto exit;
}
return SEPOL_OK;
exit:
cil_log(CIL_ERR, "Invalid name\n");
return rc;
}
int __cil_verify_syntax(struct cil_tree_node *parse_current, enum cil_syntax s[], size_t len)
{
struct cil_tree_node *c = parse_current;
size_t i = 0;
while (i < len && c != NULL) {
if ((s[i] & CIL_SYN_STRING) && c->data != NULL && c->cl_head == NULL) {
c = c->next;
i++;
} else if ((s[i] & CIL_SYN_LIST) && c->data == NULL && c->cl_head != NULL) {
c = c->next;
i++;
} else if ((s[i] & CIL_SYN_EMPTY_LIST) && c->data == NULL && c->cl_head == NULL) {
c = c->next;
i++;
} else if ((s[i] & CIL_SYN_N_LISTS) || (s[i] & CIL_SYN_N_STRINGS)) {
while (c != NULL) {
if ((s[i] & CIL_SYN_N_LISTS) && c->data == NULL && c->cl_head != NULL) {
c = c->next;
} else if ((s[i] & CIL_SYN_N_STRINGS) && c->data != NULL && c->cl_head == NULL) {
c = c->next;
} else {
goto exit;
}
}
i++;
break; /* Only CIL_SYN_END allowed after these */
} else {
goto exit;
}
}
if (i < len && (s[i] & CIL_SYN_END) && c == NULL) {
return SEPOL_OK;
}
exit:
cil_log(CIL_ERR, "Invalid syntax\n");
return SEPOL_ERR;
}
int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, enum cil_flavor expr_flavor)
{
int rc;
enum cil_syntax syntax[] = {
CIL_SYN_STRING,
CIL_SYN_STRING | CIL_SYN_LIST,
CIL_SYN_STRING | CIL_SYN_LIST,
CIL_SYN_END
};
int syntax_len = sizeof(syntax)/sizeof(*syntax);
switch (op) {
case CIL_NOT:
syntax[2] = CIL_SYN_END;
syntax_len = 3;
break;
case CIL_AND:
case CIL_OR:
case CIL_XOR:
break;
case CIL_EQ:
case CIL_NEQ:
if (expr_flavor != CIL_BOOL && expr_flavor != CIL_TUNABLE ) {
cil_log(CIL_ERR,"Invalid operator (%s) for set expression\n", (char*)current->data);
goto exit;
}
break;
case CIL_ALL:
if (expr_flavor == CIL_BOOL || expr_flavor == CIL_TUNABLE) {
cil_log(CIL_ERR,"Invalid operator (%s) for boolean or tunable expression\n", (char*)current->data);
goto exit;
}
syntax[1] = CIL_SYN_END;
syntax_len = 2;
break;
case CIL_RANGE:
if (expr_flavor != CIL_CAT && expr_flavor != CIL_PERMISSIONX) {
cil_log(CIL_ERR,"Operator (%s) only valid for catset and permissionx expression\n", (char*)current->data);
goto exit;
}
syntax[1] = CIL_SYN_STRING;
syntax[2] = CIL_SYN_STRING;
break;
case CIL_NONE: /* String or List */
syntax[0] = CIL_SYN_N_STRINGS | CIL_SYN_N_LISTS;
syntax[1] = CIL_SYN_END;
syntax_len = 2;
break;
default:
cil_log(CIL_ERR,"Unexpected value (%s) for expression operator\n", (char*)current->data);
goto exit;
}
rc = __cil_verify_syntax(current, syntax, syntax_len);
if (rc != SEPOL_OK) {
goto exit;
}
return SEPOL_OK;
exit:
return SEPOL_ERR;
}
int cil_verify_constraint_leaf_expr_syntax(enum cil_flavor l_flavor, enum cil_flavor r_flavor, enum cil_flavor op, enum cil_flavor expr_flavor)
{
if (r_flavor == CIL_STRING || r_flavor == CIL_LIST) {
if (l_flavor == CIL_CONS_L1 || l_flavor == CIL_CONS_L2 || l_flavor == CIL_CONS_H1 || l_flavor == CIL_CONS_H2 ) {
cil_log(CIL_ERR, "l1, l2, h1, and h2 cannot be used on the left side with a string or list on the right side\n");
goto exit;
} else if (l_flavor == CIL_CONS_U3 || l_flavor == CIL_CONS_R3 || l_flavor == CIL_CONS_T3) {
if (expr_flavor != CIL_VALIDATETRANS && expr_flavor != CIL_MLSVALIDATETRANS) {
cil_log(CIL_ERR, "u3, r3, and t3 can only be used with (mls)validatetrans rules\n");
goto exit;
}
}
} else {
if (r_flavor == CIL_CONS_U1 || r_flavor == CIL_CONS_R1 || r_flavor == CIL_CONS_T1) {
cil_log(CIL_ERR, "u1, r1, and t1 are not allowed on the right side\n");
goto exit;
} else if (r_flavor == CIL_CONS_U3 || r_flavor == CIL_CONS_R3 || r_flavor == CIL_CONS_T3) {
cil_log(CIL_ERR, "u3, r3, and t3 are not allowed on the right side\n");
goto exit;
} else if (r_flavor == CIL_CONS_U2) {
if (op != CIL_EQ && op != CIL_NEQ) {
cil_log(CIL_ERR, "u2 on the right side must be used with eq or neq as the operator\n");
goto exit;
} else if (l_flavor != CIL_CONS_U1) {
cil_log(CIL_ERR, "u2 on the right side must be used with u1 on the left\n");
goto exit;
}
} else if (r_flavor == CIL_CONS_R2) {
if (l_flavor != CIL_CONS_R1) {
cil_log(CIL_ERR, "r2 on the right side must be used with r1 on the left\n");
goto exit;
}
} else if (r_flavor == CIL_CONS_T2) {
if (op != CIL_EQ && op != CIL_NEQ) {
cil_log(CIL_ERR, "t2 on the right side must be used with eq or neq as the operator\n");
goto exit;
} else if (l_flavor != CIL_CONS_T1) {
cil_log(CIL_ERR, "t2 on the right side must be used with t1 on the left\n");
goto exit;
}
} else if (r_flavor == CIL_CONS_L2) {
if (l_flavor != CIL_CONS_L1 && l_flavor != CIL_CONS_H1) {
cil_log(CIL_ERR, "l2 on the right side must be used with l1 or h1 on the left\n");
goto exit;
}
} else if (r_flavor == CIL_CONS_H2) {
if (l_flavor != CIL_CONS_L1 && l_flavor != CIL_CONS_L2 && l_flavor != CIL_CONS_H1 ) {
cil_log(CIL_ERR, "h2 on the right side must be used with l1, l2, or h1 on the left\n");
goto exit;
}
} else if (r_flavor == CIL_CONS_H1) {
if (l_flavor != CIL_CONS_L1) {
cil_log(CIL_ERR, "h1 on the right side must be used with l1 on the left\n");
goto exit;
}
}
}
return SEPOL_OK;
exit:
return SEPOL_ERR;
}
int cil_verify_constraint_expr_syntax(struct cil_tree_node *current, enum cil_flavor op)
{
int rc;
enum cil_syntax syntax[] = {
CIL_SYN_STRING,
CIL_SYN_END,
CIL_SYN_END,
CIL_SYN_END
};
int syntax_len = sizeof(syntax)/sizeof(*syntax);
switch (op) {
case CIL_NOT:
syntax[1] = CIL_SYN_LIST;
syntax_len--;
break;
case CIL_AND:
case CIL_OR:
syntax[1] = CIL_SYN_LIST;
syntax[2] = CIL_SYN_LIST;
break;
case CIL_EQ:
case CIL_NEQ:
syntax[1] = CIL_SYN_STRING;
syntax[2] = CIL_SYN_STRING | CIL_SYN_LIST;
break;
case CIL_CONS_DOM:
case CIL_CONS_DOMBY:
case CIL_CONS_INCOMP:
syntax[1] = CIL_SYN_STRING;
syntax[2] = CIL_SYN_STRING;
break;
default:
cil_log(CIL_ERR, "Invalid operator (%s) for constraint expression\n", (char*)current->data);
goto exit;
}
rc = __cil_verify_syntax(current, syntax, syntax_len);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Invalid constraint syntax\n");
goto exit;
}
return SEPOL_OK;
exit:
return SEPOL_ERR;
}
int cil_verify_conditional_blocks(struct cil_tree_node *current)
{
int found_true = CIL_FALSE;
int found_false = CIL_FALSE;
if (current->cl_head->data == CIL_KEY_CONDTRUE) {
found_true = CIL_TRUE;
} else if (current->cl_head->data == CIL_KEY_CONDFALSE) {
found_false = CIL_TRUE;
} else {
cil_tree_log(current, CIL_ERR, "Expected true or false block in conditional");
return SEPOL_ERR;
}
current = current->next;
if (current != NULL) {
if (current->cl_head->data == CIL_KEY_CONDTRUE) {
if (found_true) {
cil_tree_log(current, CIL_ERR, "More than one true block in conditional");
return SEPOL_ERR;
}
} else if (current->cl_head->data == CIL_KEY_CONDFALSE) {
if (found_false) {
cil_tree_log(current, CIL_ERR, "More than one false block in conditional");
return SEPOL_ERR;
}
} else {
cil_tree_log(current, CIL_ERR, "Expected true or false block in conditional");
return SEPOL_ERR;
}
}
return SEPOL_OK;
}
int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, struct cil_tree_node *node, const char *name)
{
struct cil_list_item *item;
struct cil_list *param_list = macro->params;
if (param_list != NULL) {
cil_list_for_each(item, param_list) {
struct cil_param *param = item->data;
if (param->str == name) {
if (param->flavor == node->flavor) {
cil_log(CIL_ERR, "Declaration of %s %s shadows a macro parameter with the same flavor\n", cil_node_to_string(node), name);
return SEPOL_ERR;
} else {
cil_log(CIL_WARN, "Declaration of %s %s has same name as a macro parameter with a different flavor\n", cil_node_to_string(node), name);
}
}
}
}
return SEPOL_OK;
}
static int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_stack *stack);
static int __verify_no_self_reference_in_expr(struct cil_list *expr, struct cil_stack *stack)
{
struct cil_list_item *item;
int rc = SEPOL_OK;
if (!expr) {
return SEPOL_OK;
}
cil_list_for_each(item, expr) {
if (item->flavor == CIL_DATUM) {
struct cil_symtab_datum* datum = item->data;
rc = cil_verify_no_self_reference(FLAVOR(datum), datum, stack);
} else if (item->flavor == CIL_LIST) {
rc = __verify_no_self_reference_in_expr(item->data, stack);
}
if (rc != SEPOL_OK) {
return SEPOL_ERR;
}
}
return SEPOL_OK;
}
static int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_stack *stack)
{
struct cil_stack_item *item;
int i = 0;
int rc = SEPOL_OK;
cil_stack_for_each(stack, i, item) {
struct cil_symtab_datum *prev = item->data;
if (datum == prev) {
cil_tree_log(NODE(datum), CIL_ERR, "Self-reference found for %s", datum->name);
return SEPOL_ERR;
}
}
switch (flavor) {
case CIL_USERATTRIBUTE: {
struct cil_userattribute *attr = (struct cil_userattribute *)datum;
cil_stack_push(stack, CIL_DATUM, datum);
rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
cil_stack_pop(stack);
break;
}
case CIL_ROLEATTRIBUTE: {
struct cil_roleattribute *attr = (struct cil_roleattribute *)datum;
cil_stack_push(stack, CIL_DATUM, datum);
rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
cil_stack_pop(stack);
break;
}
case CIL_TYPEATTRIBUTE: {
struct cil_typeattribute *attr = (struct cil_typeattribute *)datum;
cil_stack_push(stack, CIL_DATUM, datum);
rc = __verify_no_self_reference_in_expr(attr->expr_list, stack);
cil_stack_pop(stack);
break;
}
case CIL_CATSET: {
struct cil_catset *set = (struct cil_catset *)datum;
cil_stack_push(stack, CIL_DATUM, datum);
rc = __verify_no_self_reference_in_expr(set->cats->datum_expr, stack);
cil_stack_pop(stack);
break;
}
default:
break;
}
return rc;
}
int __cil_verify_ranges(struct cil_list *list)
{
int rc = SEPOL_ERR;
struct cil_list_item *curr;
struct cil_list_item *range = NULL;
if (list == NULL || list->head == NULL) {
goto exit;
}
cil_list_for_each(curr, list) {
/* range */
if (curr->flavor == CIL_LIST) {
range = ((struct cil_list*)curr->data)->head;
if (range == NULL || range->next == NULL || range->next->next != NULL) {
goto exit;
}
}
}
return SEPOL_OK;
exit:
cil_log(CIL_ERR,"Invalid Range syntax\n");
return rc;
}
int cil_verify_completed_ordered_list(struct cil_list *complete, struct cil_list *ordered_lists)
{
struct cil_list_item *cprev, *ccurr, *cnext;
int found_prev, found_next;
int rc = SEPOL_OK;
found_prev = CIL_FALSE;
found_next = CIL_FALSE;
cprev = NULL;
ccurr = complete->head;
cnext = ccurr ? ccurr->next : NULL;
while (ccurr) {
struct cil_tree_node *node;
struct cil_ordered *ordered;
struct cil_list_item *curr_list, *oprev, *ocurr, *onext;
int change = CIL_FALSE;
cil_list_for_each(curr_list, ordered_lists) {
node = curr_list->data;
ordered = node->data;
oprev = NULL;
cil_list_for_each(ocurr, ordered->datums) {
onext = ocurr ? ocurr->next : NULL;
if (ccurr->data == ocurr->data) {
if (found_prev == CIL_FALSE && ((!cprev && !oprev) ||
(cprev && oprev && cprev->data == oprev->data))) {
found_prev = CIL_TRUE;
change = CIL_TRUE;
}
if (found_next == CIL_FALSE && ((!cnext && !onext) ||
(cnext && onext && cnext->data == onext->data))) {
found_next = CIL_TRUE;
change = CIL_TRUE;
}
if (found_prev && found_next) {
cprev = ccurr;
ccurr = cnext;
cnext = ccurr ? ccurr->next : NULL;
found_prev = CIL_FALSE;
found_next = CIL_FALSE;
if (!ccurr) {
/* Went through the whole list */
return rc;
}
}
}
oprev = ocurr;
}
}
if (!change) {
rc = SEPOL_ERR;
cil_log(CIL_ERR, "Unable to verify the order of %s\n", DATUM(ccurr->data)->fqn);
cil_log(CIL_ERR, "Found in the following ordering rules:\n");
cil_list_for_each(curr_list, ordered_lists) {
node = curr_list->data;
ordered = node->data;
cil_list_for_each(ocurr, ordered->datums) {
if (ccurr->data == ocurr->data) {
cil_tree_log(node, CIL_ERR, " ");
}
}
}
cprev = ccurr;
ccurr = cnext;
cnext = ccurr ? ccurr->next : NULL;
found_prev = CIL_FALSE;
found_next = CIL_FALSE;
}
}
return rc;
}
struct cil_args_verify_order {
uint32_t *flavor;
};
int __cil_verify_ordered_node_helper(struct cil_tree_node *node, __attribute__((unused)) uint32_t *finished, void *extra_args)
{
struct cil_args_verify_order *args = extra_args;
uint32_t *flavor = args->flavor;
if (node->flavor == *flavor) {
if (node->flavor == CIL_SID) {
struct cil_sid *sid = node->data;
if (sid->ordered == CIL_FALSE) {
cil_tree_log(node, CIL_ERR, "SID %s not in sidorder statement", sid->datum.name);
return SEPOL_ERR;
}
} else if (node->flavor == CIL_CLASS) {
struct cil_class *class = node->data;
if (class->ordered == CIL_FALSE) {
cil_tree_log(node, CIL_ERR, "Class %s not in classorder statement", class->datum.name);
return SEPOL_ERR;
}
} else if (node->flavor == CIL_CAT) {
struct cil_cat *cat = node->data;
if (cat->ordered == CIL_FALSE) {
cil_tree_log(node, CIL_ERR, "Category %s not in categoryorder statement", cat->datum.name);
return SEPOL_ERR;
}
} else if (node->flavor == CIL_SENS) {
struct cil_sens *sens = node->data;
if (sens->ordered == CIL_FALSE) {
cil_tree_log(node, CIL_ERR, "Sensitivity %s not in sensitivityorder statement", sens->datum.name);
return SEPOL_ERR;
}
}
}
return SEPOL_OK;
}
int __cil_verify_ordered(struct cil_tree_node *current, enum cil_flavor flavor)
{
struct cil_args_verify_order extra_args;
int rc = SEPOL_ERR;
extra_args.flavor = &flavor;
rc = cil_tree_walk(current, __cil_verify_ordered_node_helper, NULL, NULL, &extra_args);
return rc;
}
int __cil_verify_initsids(struct cil_list *sids)
{
int rc = SEPOL_OK;
struct cil_list_item *i;
if (sids->head == NULL) {
cil_log(CIL_ERR, "At least one initial sid must be defined in the policy\n");
return SEPOL_ERR;
}
cil_list_for_each(i, sids) {
struct cil_sid *sid = i->data;
if (sid->context == NULL) {
struct cil_tree_node *node = sid->datum.nodes->head->data;
cil_tree_log(node, CIL_INFO, "No context assigned to SID %s, omitting from policy",sid->datum.name);
}
}
return rc;
}
static int __cil_is_cat_in_cats(struct cil_cat *cat, struct cil_cats *cats)
{
struct cil_list_item *i;
cil_list_for_each(i, cats->datum_expr) {
struct cil_cat *c = i->data;
if (c == cat) {
return CIL_TRUE;
}
}
return CIL_FALSE;
}
static int __cil_verify_cat_in_cats(struct cil_cat *cat, struct cil_cats *cats)
{
if (__cil_is_cat_in_cats(cat, cats) != CIL_TRUE) {
cil_log(CIL_ERR, "Failed to find category %s in category list\n", cat->datum.name);
return SEPOL_ERR;
}
return SEPOL_OK;
}
static int __cil_verify_cats_associated_with_sens(struct cil_sens *sens, struct cil_cats *cats)
{
int rc = SEPOL_OK;
struct cil_list_item *i, *j;
if (!cats) {
return SEPOL_OK;
}
if (!sens->cats_list) {
cil_log(CIL_ERR, "No categories can be used with sensitivity %s\n", sens->datum.name);
return SEPOL_ERR;
}
cil_list_for_each(i, cats->datum_expr) {
struct cil_cat *cat = i->data;
int ok = CIL_FALSE;
cil_list_for_each(j, sens->cats_list) {
if (__cil_is_cat_in_cats(cat, j->data) == CIL_TRUE) {
ok = CIL_TRUE;
break;
}
}
if (ok != CIL_TRUE) {
cil_log(CIL_ERR, "Category %s cannot be used with sensitivity %s\n",
cat->datum.name, sens->datum.name);
rc = SEPOL_ERR;
}
}
return rc;
}
static int __cil_verify_levelrange_sensitivity(struct cil_db *db, struct cil_sens *low, struct cil_sens *high)
{
struct cil_list_item *curr;
int found = CIL_FALSE;
int rc = SEPOL_ERR;
cil_list_for_each(curr, db->sensitivityorder) {
if (curr->data == low) {
found = CIL_TRUE;
}
if ((found == CIL_TRUE) && (curr->data == high)) {
break;
}
}
if (found != CIL_TRUE || curr == NULL) {
goto exit;
}
return SEPOL_OK;
exit:
cil_log(CIL_ERR, "Sensitivity %s does not dominate %s\n",
high->datum.name, low->datum.name);
return rc;
}
static int __cil_verify_levelrange_cats(struct cil_cats *low, struct cil_cats *high)
{
int rc = SEPOL_ERR;
struct cil_list_item *item;
if (low == NULL || (low == NULL && high == NULL)) {
return SEPOL_OK;
}
if (high == NULL) {
rc = SEPOL_ERR;
goto exit;
}
cil_list_for_each(item, low->datum_expr) {
rc = __cil_verify_cat_in_cats(item->data, high);
if (rc != SEPOL_OK) {
goto exit;
}
}
return SEPOL_OK;
exit:
cil_log(CIL_ERR, "Low level category set must be a subset of the high level category set\n");
return rc;
}
static int __cil_verify_levelrange(struct cil_db *db, struct cil_levelrange *lr)
{
int rc = SEPOL_ERR;
rc = __cil_verify_levelrange_sensitivity(db, lr->low->sens, lr->high->sens);
if (rc != SEPOL_OK) {
goto exit;
}
rc = __cil_verify_levelrange_cats(lr->low->cats, lr->high->cats);
if (rc != SEPOL_OK) {
goto exit;
}
rc = __cil_verify_cats_associated_with_sens(lr->low->sens, lr->low->cats);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Low level sensitivity and categories are not associated\n");
goto exit;
}
rc = __cil_verify_cats_associated_with_sens(lr->high->sens, lr->high->cats);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "High level sensitivity and categories are not associated\n");
goto exit;
}
return SEPOL_OK;
exit:
return rc;
}
static int __cil_verify_named_levelrange(struct cil_db *db, struct cil_tree_node *node)
{
int rc = SEPOL_ERR;
struct cil_levelrange *lr = node->data;
rc = __cil_verify_levelrange(db, lr);
if (rc != SEPOL_OK) {
goto exit;
}
return SEPOL_OK;
exit:
cil_tree_log(node, CIL_ERR, "Invalid named range");
return rc;
}
static int __cil_verify_user_pre_eval(struct cil_tree_node *node)
{
int rc = SEPOL_ERR;
struct cil_user *user = node->data;
if (user->dftlevel == NULL) {
cil_log(CIL_ERR, "User %s does not have a default level\n", user->datum.name);
goto exit;
} else if (user->range == NULL) {
cil_log(CIL_ERR, "User %s does not have a level range\n", user->datum.name);
goto exit;
} else if (user->bounds != NULL) {
int steps = 0;
int limit = 2;
struct cil_user *u1 = user;
struct cil_user *u2 = user->bounds;
while (u2 != NULL) {
if (u1 == u2) {
cil_log(CIL_ERR, "Circular bounds found for user %s\n", u1->datum.name);
goto exit;
}
if (steps == limit) {
steps = 0;
limit *= 2;
u1 = u2;
}
u2 = u2->bounds;
steps++;
}
}
return SEPOL_OK;
exit:
cil_tree_log(node, CIL_ERR, "Invalid user");
return rc;
}
static int __cil_verify_user_post_eval(struct cil_db *db, struct cil_tree_node *node)
{
int rc = SEPOL_ERR;
struct cil_user *user = node->data;
/* Verify user range only if anonymous */
if (user->range->datum.name == NULL) {
rc = __cil_verify_levelrange(db, user->range);
if (rc != SEPOL_OK) {
goto exit;
}
}
return SEPOL_OK;
exit:
cil_tree_log(node, CIL_ERR, "Invalid user");
return rc;
}
static int __cil_verify_role(struct cil_tree_node *node)
{
int rc = SEPOL_ERR;
struct cil_role *role = node->data;
int steps = 0;
int limit = 2;
struct cil_role *r1 = role;
struct cil_role *r2 = role->bounds;
while (r2 != NULL) {
if (r1 == r2) {
cil_log(CIL_ERR, "Circular bounds found for role %s\n", r1->datum.name);
goto exit;
}
if (steps == limit) {
steps = 0;
limit *= 2;
r1 = r2;
}
r2 = r2->bounds;
steps++;
}
return SEPOL_OK;
exit:
cil_tree_log(node, CIL_ERR, "Invalid role");
return rc;
}
static int __cil_verify_type(struct cil_tree_node *node)
{
int rc = SEPOL_ERR;
struct cil_type *type = node->data;
int steps = 0;
int limit = 2;
struct cil_type *t1 = type;
struct cil_type *t2 = type->bounds;
while (t2 != NULL) {
if (t1 == t2) {
cil_log(CIL_ERR, "Circular bounds found for type %s\n", t1->datum.name);
goto exit;
}
if (steps == limit) {
steps = 0;
limit *= 2;
t1 = t2;
}
t2 = t2->bounds;
steps++;
}
return SEPOL_OK;
exit:
cil_tree_log(node, CIL_ERR, "Invalid type");
return rc;
}
static int __cil_verify_context(struct cil_db *db, struct cil_context *ctx)
{
int rc = SEPOL_ERR;
struct cil_user *user = ctx->user;
struct cil_role *role = ctx->role;
struct cil_type *type = ctx->type;
struct cil_level *user_low = user->range->low;
struct cil_level *user_high = user->range->high;
struct cil_level *ctx_low = ctx->range->low;
struct cil_level *ctx_high = ctx->range->high;
struct cil_list *sensitivityorder = db->sensitivityorder;
struct cil_list_item *curr;
int found = CIL_FALSE;
if (user->roles != NULL) {
if (!ebitmap_get_bit(user->roles, role->value)) {
cil_log(CIL_ERR, "Role %s is invalid for user %s\n", ctx->role_str, ctx->user_str);
rc = SEPOL_ERR;
goto exit;
}
} else {
cil_log(CIL_ERR, "No roles given to the user %s\n", ctx->user_str);
rc = SEPOL_ERR;
goto exit;
}
if (role->types != NULL) {
if (!ebitmap_get_bit(role->types, type->value)) {
cil_log(CIL_ERR, "Type %s is invalid for role %s\n", ctx->type_str, ctx->role_str);
rc = SEPOL_ERR;
goto exit;
}
} else {
cil_log(CIL_ERR, "No types associated with role %s\n", ctx->role_str);
rc = SEPOL_ERR;
goto exit;
}
/* Verify range only when anonymous */
if (ctx->range->datum.name == NULL) {
rc = __cil_verify_levelrange(db, ctx->range);
if (rc != SEPOL_OK) {
goto exit;
}
}
for (curr = sensitivityorder->head; curr != NULL; curr = curr->next) {