1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker * Authors: Chad Sellers <[email protected]>
3*2d543d20SAndroid Build Coastguard Worker * Joshua Brindle <[email protected]>
4*2d543d20SAndroid Build Coastguard Worker *
5*2d543d20SAndroid Build Coastguard Worker * Copyright (C) 2006 Tresys Technology, LLC
6*2d543d20SAndroid Build Coastguard Worker *
7*2d543d20SAndroid Build Coastguard Worker * This library is free software; you can redistribute it and/or
8*2d543d20SAndroid Build Coastguard Worker * modify it under the terms of the GNU Lesser General Public
9*2d543d20SAndroid Build Coastguard Worker * License as published by the Free Software Foundation; either
10*2d543d20SAndroid Build Coastguard Worker * version 2.1 of the License, or (at your option) any later version.
11*2d543d20SAndroid Build Coastguard Worker *
12*2d543d20SAndroid Build Coastguard Worker * This library is distributed in the hope that it will be useful,
13*2d543d20SAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*2d543d20SAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*2d543d20SAndroid Build Coastguard Worker * Lesser General Public License for more details.
16*2d543d20SAndroid Build Coastguard Worker *
17*2d543d20SAndroid Build Coastguard Worker * You should have received a copy of the GNU Lesser General Public
18*2d543d20SAndroid Build Coastguard Worker * License along with this library; if not, write to the Free Software
19*2d543d20SAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*2d543d20SAndroid Build Coastguard Worker */
21*2d543d20SAndroid Build Coastguard Worker
22*2d543d20SAndroid Build Coastguard Worker /* This is where the expander tests should go, including:
23*2d543d20SAndroid Build Coastguard Worker * - check role, type, bool, user mapping
24*2d543d20SAndroid Build Coastguard Worker * - add symbols declared in enabled optionals
25*2d543d20SAndroid Build Coastguard Worker * - do not add symbols declared in disabled optionals
26*2d543d20SAndroid Build Coastguard Worker * - add rules from enabled optionals
27*2d543d20SAndroid Build Coastguard Worker * - do not add rules from disabled optionals
28*2d543d20SAndroid Build Coastguard Worker * - verify attribute mapping
29*2d543d20SAndroid Build Coastguard Worker
30*2d543d20SAndroid Build Coastguard Worker * - check conditional expressions for correct mapping
31*2d543d20SAndroid Build Coastguard Worker */
32*2d543d20SAndroid Build Coastguard Worker
33*2d543d20SAndroid Build Coastguard Worker #include "test-expander.h"
34*2d543d20SAndroid Build Coastguard Worker #include "parse_util.h"
35*2d543d20SAndroid Build Coastguard Worker #include "helpers.h"
36*2d543d20SAndroid Build Coastguard Worker #include "test-common.h"
37*2d543d20SAndroid Build Coastguard Worker #include "test-expander-users.h"
38*2d543d20SAndroid Build Coastguard Worker #include "test-expander-roles.h"
39*2d543d20SAndroid Build Coastguard Worker #include "test-expander-attr-map.h"
40*2d543d20SAndroid Build Coastguard Worker
41*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
42*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/expand.h>
43*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/link.h>
44*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/conditional.h>
45*2d543d20SAndroid Build Coastguard Worker #include <limits.h>
46*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
47*2d543d20SAndroid Build Coastguard Worker
48*2d543d20SAndroid Build Coastguard Worker policydb_t role_expanded;
49*2d543d20SAndroid Build Coastguard Worker policydb_t user_expanded;
50*2d543d20SAndroid Build Coastguard Worker policydb_t base_expanded2;
51*2d543d20SAndroid Build Coastguard Worker static policydb_t basemod;
52*2d543d20SAndroid Build Coastguard Worker static policydb_t basemod2;
53*2d543d20SAndroid Build Coastguard Worker static policydb_t mod2;
54*2d543d20SAndroid Build Coastguard Worker static policydb_t base_expanded;
55*2d543d20SAndroid Build Coastguard Worker static policydb_t base_only_mod;
56*2d543d20SAndroid Build Coastguard Worker static policydb_t base_only_expanded;
57*2d543d20SAndroid Build Coastguard Worker static policydb_t role_basemod;
58*2d543d20SAndroid Build Coastguard Worker static policydb_t role_mod;
59*2d543d20SAndroid Build Coastguard Worker static policydb_t user_basemod;
60*2d543d20SAndroid Build Coastguard Worker static policydb_t user_mod;
61*2d543d20SAndroid Build Coastguard Worker static policydb_t alias_basemod;
62*2d543d20SAndroid Build Coastguard Worker static policydb_t alias_mod;
63*2d543d20SAndroid Build Coastguard Worker static policydb_t alias_expanded;
64*2d543d20SAndroid Build Coastguard Worker static uint32_t *typemap;
65*2d543d20SAndroid Build Coastguard Worker extern int mls;
66*2d543d20SAndroid Build Coastguard Worker
67*2d543d20SAndroid Build Coastguard Worker /* Takes base, some number of modules, links them, and expands them
68*2d543d20SAndroid Build Coastguard Worker reads source from myfiles array, which has the base string followed by
69*2d543d20SAndroid Build Coastguard Worker each module string */
expander_policy_init(policydb_t * mybase,int num_modules,policydb_t ** mymodules,policydb_t * myexpanded,const char * const * myfiles)70*2d543d20SAndroid Build Coastguard Worker static int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mymodules, policydb_t * myexpanded, const char *const *myfiles)
71*2d543d20SAndroid Build Coastguard Worker {
72*2d543d20SAndroid Build Coastguard Worker char *filename[num_modules + 1];
73*2d543d20SAndroid Build Coastguard Worker int i;
74*2d543d20SAndroid Build Coastguard Worker
75*2d543d20SAndroid Build Coastguard Worker for (i = 0; i < num_modules + 1; i++) {
76*2d543d20SAndroid Build Coastguard Worker filename[i] = calloc(PATH_MAX, sizeof(char));
77*2d543d20SAndroid Build Coastguard Worker if (snprintf(filename[i], PATH_MAX, "policies/test-expander/%s%s", myfiles[i], mls ? ".mls" : ".std") < 0)
78*2d543d20SAndroid Build Coastguard Worker return -1;
79*2d543d20SAndroid Build Coastguard Worker }
80*2d543d20SAndroid Build Coastguard Worker
81*2d543d20SAndroid Build Coastguard Worker if (policydb_init(mybase)) {
82*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "out of memory!\n");
83*2d543d20SAndroid Build Coastguard Worker return -1;
84*2d543d20SAndroid Build Coastguard Worker }
85*2d543d20SAndroid Build Coastguard Worker
86*2d543d20SAndroid Build Coastguard Worker for (i = 0; i < num_modules; i++) {
87*2d543d20SAndroid Build Coastguard Worker if (policydb_init(mymodules[i])) {
88*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "out of memory!\n");
89*2d543d20SAndroid Build Coastguard Worker return -1;
90*2d543d20SAndroid Build Coastguard Worker }
91*2d543d20SAndroid Build Coastguard Worker }
92*2d543d20SAndroid Build Coastguard Worker
93*2d543d20SAndroid Build Coastguard Worker if (policydb_init(myexpanded)) {
94*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "out of memory!\n");
95*2d543d20SAndroid Build Coastguard Worker return -1;
96*2d543d20SAndroid Build Coastguard Worker }
97*2d543d20SAndroid Build Coastguard Worker
98*2d543d20SAndroid Build Coastguard Worker mybase->policy_type = POLICY_BASE;
99*2d543d20SAndroid Build Coastguard Worker mybase->mls = mls;
100*2d543d20SAndroid Build Coastguard Worker
101*2d543d20SAndroid Build Coastguard Worker if (read_source_policy(mybase, filename[0], myfiles[0])) {
102*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "read source policy failed %s\n", filename[0]);
103*2d543d20SAndroid Build Coastguard Worker return -1;
104*2d543d20SAndroid Build Coastguard Worker }
105*2d543d20SAndroid Build Coastguard Worker
106*2d543d20SAndroid Build Coastguard Worker for (i = 1; i < num_modules + 1; i++) {
107*2d543d20SAndroid Build Coastguard Worker mymodules[i - 1]->policy_type = POLICY_MOD;
108*2d543d20SAndroid Build Coastguard Worker mymodules[i - 1]->mls = mls;
109*2d543d20SAndroid Build Coastguard Worker if (read_source_policy(mymodules[i - 1], filename[i], myfiles[i])) {
110*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "read source policy failed %s\n", filename[i]);
111*2d543d20SAndroid Build Coastguard Worker return -1;
112*2d543d20SAndroid Build Coastguard Worker }
113*2d543d20SAndroid Build Coastguard Worker }
114*2d543d20SAndroid Build Coastguard Worker
115*2d543d20SAndroid Build Coastguard Worker if (link_modules(NULL, mybase, mymodules, num_modules, 0)) {
116*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "link modules failed\n");
117*2d543d20SAndroid Build Coastguard Worker return -1;
118*2d543d20SAndroid Build Coastguard Worker }
119*2d543d20SAndroid Build Coastguard Worker
120*2d543d20SAndroid Build Coastguard Worker if (expand_module(NULL, mybase, myexpanded, 0, 0)) {
121*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "expand modules failed\n");
122*2d543d20SAndroid Build Coastguard Worker return -1;
123*2d543d20SAndroid Build Coastguard Worker }
124*2d543d20SAndroid Build Coastguard Worker
125*2d543d20SAndroid Build Coastguard Worker for (i = 0; i < num_modules + 1; i++) {
126*2d543d20SAndroid Build Coastguard Worker free(filename[i]);
127*2d543d20SAndroid Build Coastguard Worker }
128*2d543d20SAndroid Build Coastguard Worker return 0;
129*2d543d20SAndroid Build Coastguard Worker }
130*2d543d20SAndroid Build Coastguard Worker
expander_test_init(void)131*2d543d20SAndroid Build Coastguard Worker int expander_test_init(void)
132*2d543d20SAndroid Build Coastguard Worker {
133*2d543d20SAndroid Build Coastguard Worker const char *small_base_file = "small-base.conf";
134*2d543d20SAndroid Build Coastguard Worker const char *base_only_file = "base-base-only.conf";
135*2d543d20SAndroid Build Coastguard Worker int rc;
136*2d543d20SAndroid Build Coastguard Worker policydb_t *mymod2;
137*2d543d20SAndroid Build Coastguard Worker const char *files2[] = { "small-base.conf", "module.conf" };
138*2d543d20SAndroid Build Coastguard Worker const char *role_files[] = { "role-base.conf", "role-module.conf" };
139*2d543d20SAndroid Build Coastguard Worker const char *user_files[] = { "user-base.conf", "user-module.conf" };
140*2d543d20SAndroid Build Coastguard Worker const char *alias_files[] = { "alias-base.conf", "alias-module.conf" };
141*2d543d20SAndroid Build Coastguard Worker
142*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&basemod, 0, NULL, &base_expanded, &small_base_file);
143*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
144*2d543d20SAndroid Build Coastguard Worker return rc;
145*2d543d20SAndroid Build Coastguard Worker
146*2d543d20SAndroid Build Coastguard Worker mymod2 = &mod2;
147*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&basemod2, 1, &mymod2, &base_expanded2, files2);
148*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
149*2d543d20SAndroid Build Coastguard Worker return rc;
150*2d543d20SAndroid Build Coastguard Worker
151*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&base_only_mod, 0, NULL, &base_only_expanded, &base_only_file);
152*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
153*2d543d20SAndroid Build Coastguard Worker return rc;
154*2d543d20SAndroid Build Coastguard Worker
155*2d543d20SAndroid Build Coastguard Worker mymod2 = &role_mod;
156*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&role_basemod, 1, &mymod2, &role_expanded, role_files);
157*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
158*2d543d20SAndroid Build Coastguard Worker return rc;
159*2d543d20SAndroid Build Coastguard Worker
160*2d543d20SAndroid Build Coastguard Worker /* Just init the base for now, until we figure out how to separate out
161*2d543d20SAndroid Build Coastguard Worker mls and non-mls tests since users can't be used in mls module */
162*2d543d20SAndroid Build Coastguard Worker mymod2 = &user_mod;
163*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&user_basemod, 0, NULL, &user_expanded, user_files);
164*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
165*2d543d20SAndroid Build Coastguard Worker return rc;
166*2d543d20SAndroid Build Coastguard Worker
167*2d543d20SAndroid Build Coastguard Worker mymod2 = &alias_mod;
168*2d543d20SAndroid Build Coastguard Worker rc = expander_policy_init(&alias_basemod, 1, &mymod2, &alias_expanded, alias_files);
169*2d543d20SAndroid Build Coastguard Worker if (rc != 0)
170*2d543d20SAndroid Build Coastguard Worker return rc;
171*2d543d20SAndroid Build Coastguard Worker
172*2d543d20SAndroid Build Coastguard Worker return 0;
173*2d543d20SAndroid Build Coastguard Worker }
174*2d543d20SAndroid Build Coastguard Worker
expander_test_cleanup(void)175*2d543d20SAndroid Build Coastguard Worker int expander_test_cleanup(void)
176*2d543d20SAndroid Build Coastguard Worker {
177*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&basemod);
178*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_expanded);
179*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&basemod2);
180*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_expanded2);
181*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&mod2);
182*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_only_mod);
183*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_only_expanded);
184*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&role_basemod);
185*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&role_expanded);
186*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&role_mod);
187*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&user_basemod);
188*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&user_expanded);
189*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&user_mod);
190*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&alias_basemod);
191*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&alias_expanded);
192*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&alias_mod);
193*2d543d20SAndroid Build Coastguard Worker free(typemap);
194*2d543d20SAndroid Build Coastguard Worker
195*2d543d20SAndroid Build Coastguard Worker return 0;
196*2d543d20SAndroid Build Coastguard Worker }
197*2d543d20SAndroid Build Coastguard Worker
test_expander_indexes(void)198*2d543d20SAndroid Build Coastguard Worker static void test_expander_indexes(void)
199*2d543d20SAndroid Build Coastguard Worker {
200*2d543d20SAndroid Build Coastguard Worker test_policydb_indexes(&base_expanded);
201*2d543d20SAndroid Build Coastguard Worker }
202*2d543d20SAndroid Build Coastguard Worker
test_expander_alias(void)203*2d543d20SAndroid Build Coastguard Worker static void test_expander_alias(void)
204*2d543d20SAndroid Build Coastguard Worker {
205*2d543d20SAndroid Build Coastguard Worker test_alias_datum(&alias_expanded, "alias_check_1_a", "alias_check_1_t", 1, 0);
206*2d543d20SAndroid Build Coastguard Worker test_alias_datum(&alias_expanded, "alias_check_2_a", "alias_check_2_t", 1, 0);
207*2d543d20SAndroid Build Coastguard Worker test_alias_datum(&alias_expanded, "alias_check_3_a", "alias_check_3_t", 1, 0);
208*2d543d20SAndroid Build Coastguard Worker }
209*2d543d20SAndroid Build Coastguard Worker
expander_add_tests(CU_pSuite suite)210*2d543d20SAndroid Build Coastguard Worker int expander_add_tests(CU_pSuite suite)
211*2d543d20SAndroid Build Coastguard Worker {
212*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "expander_indexes", test_expander_indexes)) {
213*2d543d20SAndroid Build Coastguard Worker CU_cleanup_registry();
214*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
215*2d543d20SAndroid Build Coastguard Worker }
216*2d543d20SAndroid Build Coastguard Worker
217*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "expander_attr_mapping", test_expander_attr_mapping)) {
218*2d543d20SAndroid Build Coastguard Worker CU_cleanup_registry();
219*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
220*2d543d20SAndroid Build Coastguard Worker }
221*2d543d20SAndroid Build Coastguard Worker
222*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "expander_role_mapping", test_expander_role_mapping)) {
223*2d543d20SAndroid Build Coastguard Worker CU_cleanup_registry();
224*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
225*2d543d20SAndroid Build Coastguard Worker }
226*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "expander_user_mapping", test_expander_user_mapping)) {
227*2d543d20SAndroid Build Coastguard Worker CU_cleanup_registry();
228*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
229*2d543d20SAndroid Build Coastguard Worker }
230*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "expander_alias", test_expander_alias)) {
231*2d543d20SAndroid Build Coastguard Worker CU_cleanup_registry();
232*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
233*2d543d20SAndroid Build Coastguard Worker }
234*2d543d20SAndroid Build Coastguard Worker return 0;
235*2d543d20SAndroid Build Coastguard Worker }
236