• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author: Joshua Brindle <jbrindle@tresys.com>
3  *         Chad Sellers <csellers@tresys.com>
4  *
5  * Copyright (C) 2006 Tresys Technology, LLC
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2.1 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "test-linker-types.h"
23 #include "parse_util.h"
24 #include "helpers.h"
25 #include "test-common.h"
26 
27 #include <sepol/policydb/policydb.h>
28 #include <sepol/policydb/link.h>
29 
30 #include <CUnit/Basic.h>
31 #include <stdlib.h>
32 
33 /* Tests for types:
34  * Test for each of these for
35  * - type in appropriate symtab (global and decl)
36  * - datum in the decl symtab has correct type bitmap (if attr)
37  * - primary is set correctly
38  * - scope datum has correct decl ids
39  * Tests:
40  * - type in base, no modules
41  * - type in base optional, no modules
42  * - type a in base, b in module
43  * - type a in base optional, b in module
44  * - type a in base, b in module optional
45  * - type a in base optional, b in module optional
46  * - attr in base, no modules
47  * - attr in base optional, no modules
48  * - attr a in base, b in module
49  * - attr a in base optional, b in module
50  * - attr a in base, b in module optional
51  * - attr a in base optional, b in module optional
52  * - attr a declared in base, added to in module
53  * - attr a declared in base, added to in module optional
54  * - attr a declared in base, added to in 2 modules
55  * - attr a declared in base, added to in 2 modules (optional and global)
56  * - attr a declared in base optional, added to in module
57  * - attr a declared in base optional, added to in module optional
58  * - attr a added to in base optional, declared in module
59  * - attr a added to in base optional, declared in module optional
60  * - attr a added to in base optional, declared in module, added to in other module
61  * - attr a added to in base optional, declared in module optional, added to in other module
62  * - attr a added to in base optional, declared in module , added to in other module optional
63  * - attr a added to in base optional, declared in module optional, added to in other module optional
64  * - alias in base of primary type in base, no modules
65  * - alias in base optional of primary type in base, no modules
66  * - alias in base optional of primary type in base optional
67  * - alias in module of primary type in base
68  * - alias in module optional of primary type in base
69  * - alias in module optional of primary type in base optional
70  * - alias in module of primary type in module
71  * - alias in module optional of primary type in module
72  * - alias in module optional of primary type in module optional
73  * - alias a in base, b in module, primary type in base
74  * - alias a in base, b in module, primary type in module
75  * - alias a in base optional, b in module, primary type in base
76  * - alias a in base optional, b in module, primary type in module
77  * - alias a in base, b in module optional, primary type in base
78  * - alias a in base, b in module optional, primary type in module
79  * - alias a in base optional, b in module optional, primary type in base
80  * - alias a in base optional, b in module optional, primary type in module
81  * - alias a in base, required in module, primary type in base
82  * - alias a in base, required in base optional, primary type in base
83  * - alias a in base, required in module optional, primary type in base
84  * - alias a in module, required in base optional, primary type in base
85  * - alias a in module, required in module optional, primary type in base
86  * - alias a in base optional, required in module, primary type in base
87  * - alias a in base optional, required in different base optional, primary type in base
88  * - alias a in base optional, required in module optional, primary type in base
89  * - alias a in module optional, required in base optional, primary type in base
90  * - alias a in module optional, required in module optional, primary type in base
91  * - alias a in module, required in base optional, primary type in module
92  * - alias a in module, required in module optional, primary type in module
93  * - alias a in base optional, required in module, primary type in module
94  * - alias a in base optional, required in different base optional, primary type in module
95  * - alias a in base optional, required in module optional, primary type in module
96  * - alias a in module optional, required in base optional, primary type in module
97  * - alias a in module optional, required in module optional, primary type in module
98  */
99 
100 /* Don't pass in decls from global blocks since symbols aren't stored in their symtab */
test_type_datum(policydb_t * p,const char * id,unsigned int * decls,int len,unsigned int primary)101 static void test_type_datum(policydb_t * p, const char *id, unsigned int *decls, int len, unsigned int primary)
102 {
103 	int i;
104 	unsigned int value;
105 	type_datum_t *type;
106 
107 	/* just test the type datums for each decl to see if it is what we expect */
108 	type = hashtab_search(p->p_types.table, id);
109 
110 	CU_ASSERT_FATAL(type != NULL);
111 	CU_ASSERT(type->primary == primary);
112 	CU_ASSERT(type->flavor == TYPE_TYPE);
113 
114 	value = type->s.value;
115 
116 	for (i = 0; i < len; i++) {
117 		type = hashtab_search(p->decl_val_to_struct[decls[i] - 1]->p_types.table, id);
118 		CU_ASSERT_FATAL(type != NULL);
119 		CU_ASSERT(type->primary == primary);
120 		CU_ASSERT(type->flavor == TYPE_TYPE);
121 		CU_ASSERT(type->s.value == value);
122 	}
123 
124 }
125 
base_type_tests(policydb_t * base)126 void base_type_tests(policydb_t * base)
127 {
128 	unsigned int decls[2];
129 	const char *types[2];
130 
131 	/* These tests look at types in the base only, the desire is to ensure that
132 	 * types are not destroyed or otherwise removed during the link process.
133 	 * if this happens these tests won't work anyway since we are using types to
134 	 * mark blocks */
135 
136 	/**** test for g_b_type_1 in base and decl 1 (global) ****/
137 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
138 	test_sym_presence(base, "g_b_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
139 	test_type_datum(base, "g_b_type_1", NULL, 0, 1);
140 	/* this attr is in the same decl as the type */
141 	test_sym_presence(base, "g_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
142 	types[0] = "g_b_type_1";
143 	test_attr_types(base, "g_b_attr_1", NULL, types, 1);
144 
145 	/**** test for o1_b_type_1 in optional (decl 2) ****/
146 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"))->decl_id;
147 	test_sym_presence(base, "o1_b_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
148 	test_type_datum(base, "o1_b_type_1", NULL, 0, 1);
149 	/* this attr is in the same decl as the type */
150 	test_sym_presence(base, "o1_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
151 	types[0] = "o1_b_type_1";
152 	test_attr_types(base, "o1_b_attr_1", base->decl_val_to_struct[decls[0] - 1], types, 1);
153 
154 	/* tests for aliases */
155 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
156 	test_sym_presence(base, "g_b_alias_1", SYM_TYPES, SCOPE_DECL, decls, 1);
157 	test_alias_datum(base, "g_b_alias_1", "g_b_type_3", 1, 0);
158 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o6_b"))->decl_id;
159 	test_sym_presence(base, "g_b_alias_2", SYM_TYPES, SCOPE_DECL, decls, 1);
160 	test_alias_datum(base, "g_b_alias_2", "g_b_type_3", 1, 0);
161 
162 }
163 
module_type_tests(policydb_t * base)164 void module_type_tests(policydb_t * base)
165 {
166 	unsigned int decls[2];
167 	const char *types[2];
168 	avrule_decl_t *d;
169 
170 	/* These tests look at types that were copied from modules or attributes
171 	 * that were modified and declared in modules and base. These apply to
172 	 * declarations and modifications in and out of optionals. These tests
173 	 * should ensure that types and attributes are correctly copied from modules
174 	 * and that attribute type sets are correctly copied and mapped. */
175 
176 	/* note: scope for attributes is currently smashed if the attribute is declared
177 	 * somewhere so the scope test only looks at global, the type bitmap test looks
178 	 * at the appropriate decl symtab */
179 
180 	/* test for type in module 1 (global) */
181 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
182 	test_sym_presence(base, "g_m1_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
183 	test_type_datum(base, "g_m1_type_1", NULL, 0, 1);
184 	/* attr has is in the same decl as the above type */
185 	test_sym_presence(base, "g_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
186 	types[0] = "g_m1_type_1";
187 	types[1] = "g_m1_type_2";
188 	test_attr_types(base, "g_m1_attr_1", NULL, types, 2);
189 
190 	/* test for type in module 1 (optional) */
191 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1"))->decl_id;
192 	test_sym_presence(base, "o1_m1_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
193 	test_type_datum(base, "o1_m1_type_1", NULL, 0, 1);
194 	/* attr has is in the same decl as the above type */
195 	test_sym_presence(base, "o1_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
196 	types[0] = "o1_m1_type_2";
197 	test_attr_types(base, "o1_m1_attr_1", base->decl_val_to_struct[decls[0] - 1], types, 1);
198 
199 	/* test for attr declared in base, added to in module (global).
200 	 * Since these are both global it'll be merged in the main symtab */
201 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
202 	test_sym_presence(base, "g_b_attr_3", SYM_TYPES, SCOPE_DECL, decls, 1);
203 	types[0] = "g_m1_type_3";
204 	test_attr_types(base, "g_b_attr_3", NULL, types, 1);
205 
206 	/* test for attr declared in base, added to in module (optional). */
207 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
208 	test_sym_presence(base, "g_b_attr_4", SYM_TYPES, SCOPE_DECL, decls, 1);
209 
210 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1"))->decl_id;
211 	types[0] = "o1_m1_type_3";
212 	test_attr_types(base, "g_b_attr_4", base->decl_val_to_struct[decls[0] - 1], types, 1);
213 
214 	/* test for attr declared in base, added to in 2 modules (global). (merged in main symtab) */
215 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
216 	test_sym_presence(base, "g_b_attr_5", SYM_TYPES, SCOPE_DECL, decls, 1);
217 	types[0] = "g_m1_type_4";
218 	types[1] = "g_m2_type_4";
219 	test_attr_types(base, "g_b_attr_5", NULL, types, 2);
220 
221 	/* test for attr declared in base, added to in 2 modules (optional/global). */
222 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
223 	test_sym_presence(base, "g_b_attr_6", SYM_TYPES, SCOPE_DECL, decls, 1);
224 	/* module 2 was global to its type is in main symtab */
225 	types[0] = "g_m2_type_5";
226 	test_attr_types(base, "g_b_attr_6", NULL, types, 1);
227 	d = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"));
228 	types[0] = "o3_m1_type_2";
229 	test_attr_types(base, "g_b_attr_6", d, types, 1);
230 
231 	/* test for attr declared in base optional, added to in module (global). */
232 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b"))->decl_id;
233 	test_sym_presence(base, "o4_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
234 	types[0] = "g_m1_type_5";
235 	test_attr_types(base, "o4_b_attr_1", NULL, types, 1);
236 
237 	/* test for attr declared in base optional, added to in module (optional). */
238 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"))->decl_id;
239 	test_sym_presence(base, "o1_b_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
240 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1");
241 	types[0] = "o1_m1_type_5";
242 	test_attr_types(base, "o1_b_attr_2", d, types, 1);
243 
244 	/* test for attr declared in module, added to in base optional */
245 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
246 	test_sym_presence(base, "g_m1_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
247 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b");
248 	types[0] = "o1_b_type_2";
249 	test_attr_types(base, "g_m1_attr_2", d, types, 1);
250 
251 	/* test for attr declared in module optional, added to in base optional */
252 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"))->decl_id;
253 	test_sym_presence(base, "o3_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
254 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b");
255 	types[0] = "o4_b_type_1";
256 	test_attr_types(base, "o3_m1_attr_1", d, types, 1);
257 
258 	/* attr a added to in base optional, declared/added to in module, added to in other module */
259 	/* first the module declare/add and module 2 add (since its global it'll be in the main symtab */
260 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
261 	test_sym_presence(base, "g_m1_attr_3", SYM_TYPES, SCOPE_DECL, decls, 1);
262 	types[0] = "g_m1_type_6";
263 	types[1] = "g_m2_type_3";
264 	test_attr_types(base, "g_m1_attr_3", NULL, types, 2);
265 	/* base add */
266 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b");
267 	types[0] = "o4_b_type_2";
268 	test_attr_types(base, "g_m1_attr_3", d, types, 1);
269 
270 	/* attr a added to in base optional, declared/added in module optional, added to in other module */
271 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1");
272 	decls[0] = d->decl_id;
273 	test_sym_presence(base, "o3_m1_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
274 	types[0] = "o3_m1_type_3";
275 	test_attr_types(base, "o3_m1_attr_2", d, types, 1);
276 	/* module 2's type will be in the main symtab */
277 	types[0] = "g_m2_type_6";
278 	test_attr_types(base, "o3_m1_attr_2", NULL, types, 1);
279 	/* base add */
280 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_b");
281 	types[0] = "o2_b_type_1";
282 	test_attr_types(base, "o3_m1_attr_2", d, types, 1);
283 
284 	/* attr a added to in base optional, declared/added in module , added to in other module optional */
285 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
286 	test_sym_presence(base, "g_m1_attr_4", SYM_TYPES, SCOPE_DECL, decls, 1);
287 	types[0] = "g_m1_type_7";
288 	test_attr_types(base, "g_m1_attr_4", NULL, types, 1);
289 	/* module 2 */
290 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m2");
291 	types[0] = "o2_m2_type_1";
292 	test_attr_types(base, "g_m1_attr_4", d, types, 1);
293 	/* base add */
294 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o5_b");
295 	types[0] = "o5_b_type_1";
296 	test_attr_types(base, "g_m1_attr_4", d, types, 1);
297 
298 	/* attr a added to in base optional, declared/added in module optional, added to in other module optional */
299 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_m1");
300 	decls[0] = d->decl_id;
301 	test_sym_presence(base, "o4_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
302 	types[0] = "o4_m1_type_1";
303 	test_attr_types(base, "o4_m1_attr_1", d, types, 1);
304 	/* module 2 */
305 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m2");
306 	types[0] = "o2_m2_type_2";
307 	test_attr_types(base, "o4_m1_attr_1", d, types, 1);
308 	/* base add */
309 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o5_b");
310 	types[0] = "o5_b_type_2";
311 	test_attr_types(base, "o4_m1_attr_1", d, types, 1);
312 
313 	/* tests for aliases */
314 	decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
315 	test_sym_presence(base, "g_m_alias_1", SYM_TYPES, SCOPE_DECL, decls, 1);
316 	test_alias_datum(base, "g_m_alias_1", "g_b_type_3", 1, 0);
317 
318 }
319