Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Implementation of the policy database. | |
3 | * | |
4 | * Author : Stephen Smalley, <sds@epoch.ncsc.mil> | |
5 | */ | |
6 | ||
7 | /* | |
8 | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> | |
9 | * | |
10 | * Support for enhanced MLS infrastructure. | |
11 | * | |
12 | * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com> | |
13 | * | |
2ced3dfd | 14 | * Added conditional policy language extensions |
1da177e4 | 15 | * |
3bb56b25 PM |
16 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
17 | * | |
18 | * Added support for the policy capability bitmap | |
19 | * | |
20 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. | |
1da177e4 LT |
21 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
22 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC | |
23 | * This program is free software; you can redistribute it and/or modify | |
2ced3dfd | 24 | * it under the terms of the GNU General Public License as published by |
1da177e4 LT |
25 | * the Free Software Foundation, version 2. |
26 | */ | |
27 | ||
28 | #include <linux/kernel.h> | |
9dc99780 | 29 | #include <linux/sched.h> |
1da177e4 LT |
30 | #include <linux/slab.h> |
31 | #include <linux/string.h> | |
32 | #include <linux/errno.h> | |
d9250dea | 33 | #include <linux/audit.h> |
6371dcd3 | 34 | #include <linux/flex_array.h> |
1da177e4 LT |
35 | #include "security.h" |
36 | ||
37 | #include "policydb.h" | |
38 | #include "conditional.h" | |
39 | #include "mls.h" | |
cee74f47 | 40 | #include "services.h" |
1da177e4 LT |
41 | |
42 | #define _DEBUG_HASHES | |
43 | ||
44 | #ifdef DEBUG_HASHES | |
634a539e | 45 | static const char *symtab_name[SYM_NUM] = { |
1da177e4 LT |
46 | "common prefixes", |
47 | "classes", | |
48 | "roles", | |
49 | "types", | |
50 | "users", | |
51 | "bools", | |
52 | "levels", | |
53 | "categories", | |
54 | }; | |
55 | #endif | |
56 | ||
1da177e4 LT |
57 | static unsigned int symtab_sizes[SYM_NUM] = { |
58 | 2, | |
59 | 32, | |
60 | 16, | |
61 | 512, | |
62 | 128, | |
63 | 16, | |
64 | 16, | |
65 | 16, | |
66 | }; | |
67 | ||
68 | struct policydb_compat_info { | |
69 | int version; | |
70 | int sym_num; | |
71 | int ocon_num; | |
72 | }; | |
73 | ||
74 | /* These need to be updated if SYM_NUM or OCON_NUM changes */ | |
75 | static struct policydb_compat_info policydb_compat[] = { | |
76 | { | |
2ced3dfd EP |
77 | .version = POLICYDB_VERSION_BASE, |
78 | .sym_num = SYM_NUM - 3, | |
79 | .ocon_num = OCON_NUM - 1, | |
1da177e4 LT |
80 | }, |
81 | { | |
2ced3dfd EP |
82 | .version = POLICYDB_VERSION_BOOL, |
83 | .sym_num = SYM_NUM - 2, | |
84 | .ocon_num = OCON_NUM - 1, | |
1da177e4 LT |
85 | }, |
86 | { | |
2ced3dfd EP |
87 | .version = POLICYDB_VERSION_IPV6, |
88 | .sym_num = SYM_NUM - 2, | |
89 | .ocon_num = OCON_NUM, | |
1da177e4 LT |
90 | }, |
91 | { | |
2ced3dfd EP |
92 | .version = POLICYDB_VERSION_NLCLASS, |
93 | .sym_num = SYM_NUM - 2, | |
94 | .ocon_num = OCON_NUM, | |
1da177e4 LT |
95 | }, |
96 | { | |
2ced3dfd EP |
97 | .version = POLICYDB_VERSION_MLS, |
98 | .sym_num = SYM_NUM, | |
99 | .ocon_num = OCON_NUM, | |
1da177e4 | 100 | }, |
782ebb99 | 101 | { |
2ced3dfd EP |
102 | .version = POLICYDB_VERSION_AVTAB, |
103 | .sym_num = SYM_NUM, | |
104 | .ocon_num = OCON_NUM, | |
782ebb99 | 105 | }, |
f3f87714 | 106 | { |
2ced3dfd EP |
107 | .version = POLICYDB_VERSION_RANGETRANS, |
108 | .sym_num = SYM_NUM, | |
109 | .ocon_num = OCON_NUM, | |
f3f87714 | 110 | }, |
3bb56b25 PM |
111 | { |
112 | .version = POLICYDB_VERSION_POLCAP, | |
113 | .sym_num = SYM_NUM, | |
114 | .ocon_num = OCON_NUM, | |
64dbf074 EP |
115 | }, |
116 | { | |
117 | .version = POLICYDB_VERSION_PERMISSIVE, | |
118 | .sym_num = SYM_NUM, | |
119 | .ocon_num = OCON_NUM, | |
d9250dea KK |
120 | }, |
121 | { | |
122 | .version = POLICYDB_VERSION_BOUNDARY, | |
123 | .sym_num = SYM_NUM, | |
124 | .ocon_num = OCON_NUM, | |
125 | }, | |
652bb9b0 EP |
126 | { |
127 | .version = POLICYDB_VERSION_FILENAME_TRANS, | |
128 | .sym_num = SYM_NUM, | |
129 | .ocon_num = OCON_NUM, | |
130 | }, | |
8023976c HC |
131 | { |
132 | .version = POLICYDB_VERSION_ROLETRANS, | |
133 | .sym_num = SYM_NUM, | |
134 | .ocon_num = OCON_NUM, | |
135 | }, | |
1da177e4 LT |
136 | }; |
137 | ||
138 | static struct policydb_compat_info *policydb_lookup_compat(int version) | |
139 | { | |
140 | int i; | |
141 | struct policydb_compat_info *info = NULL; | |
142 | ||
32725ad8 | 143 | for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) { |
1da177e4 LT |
144 | if (policydb_compat[i].version == version) { |
145 | info = &policydb_compat[i]; | |
146 | break; | |
147 | } | |
148 | } | |
149 | return info; | |
150 | } | |
151 | ||
152 | /* | |
153 | * Initialize the role table. | |
154 | */ | |
155 | static int roles_init(struct policydb *p) | |
156 | { | |
157 | char *key = NULL; | |
158 | int rc; | |
159 | struct role_datum *role; | |
160 | ||
9398c7f7 | 161 | rc = -ENOMEM; |
89d155ef | 162 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
9398c7f7 | 163 | if (!role) |
1da177e4 | 164 | goto out; |
9398c7f7 EP |
165 | |
166 | rc = -EINVAL; | |
1da177e4 | 167 | role->value = ++p->p_roles.nprim; |
9398c7f7 EP |
168 | if (role->value != OBJECT_R_VAL) |
169 | goto out; | |
170 | ||
171 | rc = -ENOMEM; | |
b3139bbc | 172 | key = kstrdup(OBJECT_R, GFP_KERNEL); |
9398c7f7 EP |
173 | if (!key) |
174 | goto out; | |
175 | ||
1da177e4 LT |
176 | rc = hashtab_insert(p->p_roles.table, key, role); |
177 | if (rc) | |
9398c7f7 | 178 | goto out; |
1da177e4 | 179 | |
9398c7f7 EP |
180 | return 0; |
181 | out: | |
1da177e4 | 182 | kfree(key); |
1da177e4 | 183 | kfree(role); |
9398c7f7 | 184 | return rc; |
1da177e4 LT |
185 | } |
186 | ||
2f3e82d6 SS |
187 | static u32 rangetr_hash(struct hashtab *h, const void *k) |
188 | { | |
189 | const struct range_trans *key = k; | |
190 | return (key->source_type + (key->target_type << 3) + | |
191 | (key->target_class << 5)) & (h->size - 1); | |
192 | } | |
193 | ||
194 | static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2) | |
195 | { | |
196 | const struct range_trans *key1 = k1, *key2 = k2; | |
4419aae1 EP |
197 | int v; |
198 | ||
199 | v = key1->source_type - key2->source_type; | |
200 | if (v) | |
201 | return v; | |
202 | ||
203 | v = key1->target_type - key2->target_type; | |
204 | if (v) | |
205 | return v; | |
206 | ||
207 | v = key1->target_class - key2->target_class; | |
208 | ||
209 | return v; | |
2f3e82d6 SS |
210 | } |
211 | ||
1da177e4 LT |
212 | /* |
213 | * Initialize a policy database structure. | |
214 | */ | |
215 | static int policydb_init(struct policydb *p) | |
216 | { | |
217 | int i, rc; | |
218 | ||
219 | memset(p, 0, sizeof(*p)); | |
220 | ||
221 | for (i = 0; i < SYM_NUM; i++) { | |
222 | rc = symtab_init(&p->symtab[i], symtab_sizes[i]); | |
223 | if (rc) | |
9398c7f7 | 224 | goto out; |
1da177e4 LT |
225 | } |
226 | ||
227 | rc = avtab_init(&p->te_avtab); | |
228 | if (rc) | |
9398c7f7 | 229 | goto out; |
1da177e4 LT |
230 | |
231 | rc = roles_init(p); | |
232 | if (rc) | |
9398c7f7 | 233 | goto out; |
1da177e4 LT |
234 | |
235 | rc = cond_policydb_init(p); | |
236 | if (rc) | |
9398c7f7 | 237 | goto out; |
1da177e4 | 238 | |
2f3e82d6 SS |
239 | p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256); |
240 | if (!p->range_tr) | |
9398c7f7 | 241 | goto out; |
2f3e82d6 | 242 | |
3bb56b25 | 243 | ebitmap_init(&p->policycaps); |
64dbf074 | 244 | ebitmap_init(&p->permissive_map); |
3bb56b25 | 245 | |
9398c7f7 | 246 | return 0; |
1da177e4 | 247 | out: |
1da177e4 LT |
248 | for (i = 0; i < SYM_NUM; i++) |
249 | hashtab_destroy(p->symtab[i].table); | |
9398c7f7 | 250 | return rc; |
1da177e4 LT |
251 | } |
252 | ||
253 | /* | |
254 | * The following *_index functions are used to | |
255 | * define the val_to_name and val_to_struct arrays | |
256 | * in a policy database structure. The val_to_name | |
257 | * arrays are used when converting security context | |
258 | * structures into string representations. The | |
259 | * val_to_struct arrays are used when the attributes | |
260 | * of a class, role, or user are needed. | |
261 | */ | |
262 | ||
263 | static int common_index(void *key, void *datum, void *datap) | |
264 | { | |
265 | struct policydb *p; | |
266 | struct common_datum *comdatum; | |
ac76c05b | 267 | struct flex_array *fa; |
1da177e4 LT |
268 | |
269 | comdatum = datum; | |
270 | p = datap; | |
271 | if (!comdatum->value || comdatum->value > p->p_commons.nprim) | |
272 | return -EINVAL; | |
ac76c05b EP |
273 | |
274 | fa = p->sym_val_to_name[SYM_COMMONS]; | |
275 | if (flex_array_put_ptr(fa, comdatum->value - 1, key, | |
276 | GFP_KERNEL | __GFP_ZERO)) | |
277 | BUG(); | |
1da177e4 LT |
278 | return 0; |
279 | } | |
280 | ||
281 | static int class_index(void *key, void *datum, void *datap) | |
282 | { | |
283 | struct policydb *p; | |
284 | struct class_datum *cladatum; | |
ac76c05b | 285 | struct flex_array *fa; |
1da177e4 LT |
286 | |
287 | cladatum = datum; | |
288 | p = datap; | |
289 | if (!cladatum->value || cladatum->value > p->p_classes.nprim) | |
290 | return -EINVAL; | |
ac76c05b EP |
291 | fa = p->sym_val_to_name[SYM_CLASSES]; |
292 | if (flex_array_put_ptr(fa, cladatum->value - 1, key, | |
293 | GFP_KERNEL | __GFP_ZERO)) | |
294 | BUG(); | |
1da177e4 LT |
295 | p->class_val_to_struct[cladatum->value - 1] = cladatum; |
296 | return 0; | |
297 | } | |
298 | ||
299 | static int role_index(void *key, void *datum, void *datap) | |
300 | { | |
301 | struct policydb *p; | |
302 | struct role_datum *role; | |
ac76c05b | 303 | struct flex_array *fa; |
1da177e4 LT |
304 | |
305 | role = datum; | |
306 | p = datap; | |
d9250dea KK |
307 | if (!role->value |
308 | || role->value > p->p_roles.nprim | |
309 | || role->bounds > p->p_roles.nprim) | |
1da177e4 | 310 | return -EINVAL; |
ac76c05b EP |
311 | |
312 | fa = p->sym_val_to_name[SYM_ROLES]; | |
313 | if (flex_array_put_ptr(fa, role->value - 1, key, | |
314 | GFP_KERNEL | __GFP_ZERO)) | |
315 | BUG(); | |
1da177e4 LT |
316 | p->role_val_to_struct[role->value - 1] = role; |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int type_index(void *key, void *datum, void *datap) | |
321 | { | |
322 | struct policydb *p; | |
323 | struct type_datum *typdatum; | |
ac76c05b | 324 | struct flex_array *fa; |
1da177e4 LT |
325 | |
326 | typdatum = datum; | |
327 | p = datap; | |
328 | ||
329 | if (typdatum->primary) { | |
d9250dea KK |
330 | if (!typdatum->value |
331 | || typdatum->value > p->p_types.nprim | |
332 | || typdatum->bounds > p->p_types.nprim) | |
1da177e4 | 333 | return -EINVAL; |
ac76c05b EP |
334 | fa = p->sym_val_to_name[SYM_TYPES]; |
335 | if (flex_array_put_ptr(fa, typdatum->value - 1, key, | |
336 | GFP_KERNEL | __GFP_ZERO)) | |
337 | BUG(); | |
338 | ||
339 | fa = p->type_val_to_struct_array; | |
340 | if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum, | |
23bdecb0 EP |
341 | GFP_KERNEL | __GFP_ZERO)) |
342 | BUG(); | |
1da177e4 LT |
343 | } |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static int user_index(void *key, void *datum, void *datap) | |
349 | { | |
350 | struct policydb *p; | |
351 | struct user_datum *usrdatum; | |
ac76c05b | 352 | struct flex_array *fa; |
1da177e4 LT |
353 | |
354 | usrdatum = datum; | |
355 | p = datap; | |
d9250dea KK |
356 | if (!usrdatum->value |
357 | || usrdatum->value > p->p_users.nprim | |
358 | || usrdatum->bounds > p->p_users.nprim) | |
1da177e4 | 359 | return -EINVAL; |
ac76c05b EP |
360 | |
361 | fa = p->sym_val_to_name[SYM_USERS]; | |
362 | if (flex_array_put_ptr(fa, usrdatum->value - 1, key, | |
363 | GFP_KERNEL | __GFP_ZERO)) | |
364 | BUG(); | |
1da177e4 LT |
365 | p->user_val_to_struct[usrdatum->value - 1] = usrdatum; |
366 | return 0; | |
367 | } | |
368 | ||
369 | static int sens_index(void *key, void *datum, void *datap) | |
370 | { | |
371 | struct policydb *p; | |
372 | struct level_datum *levdatum; | |
ac76c05b | 373 | struct flex_array *fa; |
1da177e4 LT |
374 | |
375 | levdatum = datum; | |
376 | p = datap; | |
377 | ||
378 | if (!levdatum->isalias) { | |
379 | if (!levdatum->level->sens || | |
380 | levdatum->level->sens > p->p_levels.nprim) | |
381 | return -EINVAL; | |
ac76c05b EP |
382 | fa = p->sym_val_to_name[SYM_LEVELS]; |
383 | if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key, | |
384 | GFP_KERNEL | __GFP_ZERO)) | |
385 | BUG(); | |
1da177e4 LT |
386 | } |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | static int cat_index(void *key, void *datum, void *datap) | |
392 | { | |
393 | struct policydb *p; | |
394 | struct cat_datum *catdatum; | |
ac76c05b | 395 | struct flex_array *fa; |
1da177e4 LT |
396 | |
397 | catdatum = datum; | |
398 | p = datap; | |
399 | ||
400 | if (!catdatum->isalias) { | |
401 | if (!catdatum->value || catdatum->value > p->p_cats.nprim) | |
402 | return -EINVAL; | |
ac76c05b EP |
403 | fa = p->sym_val_to_name[SYM_CATS]; |
404 | if (flex_array_put_ptr(fa, catdatum->value - 1, key, | |
405 | GFP_KERNEL | __GFP_ZERO)) | |
406 | BUG(); | |
1da177e4 LT |
407 | } |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) = | |
413 | { | |
414 | common_index, | |
415 | class_index, | |
416 | role_index, | |
417 | type_index, | |
418 | user_index, | |
419 | cond_index_bool, | |
420 | sens_index, | |
421 | cat_index, | |
422 | }; | |
423 | ||
1da177e4 LT |
424 | #ifdef DEBUG_HASHES |
425 | static void symtab_hash_eval(struct symtab *s) | |
426 | { | |
427 | int i; | |
428 | ||
429 | for (i = 0; i < SYM_NUM; i++) { | |
430 | struct hashtab *h = s[i].table; | |
431 | struct hashtab_info info; | |
432 | ||
433 | hashtab_stat(h, &info); | |
744ba35e | 434 | printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, " |
1da177e4 LT |
435 | "longest chain length %d\n", symtab_name[i], h->nel, |
436 | info.slots_used, h->size, info.max_chain_len); | |
437 | } | |
438 | } | |
2f3e82d6 SS |
439 | |
440 | static void rangetr_hash_eval(struct hashtab *h) | |
441 | { | |
442 | struct hashtab_info info; | |
443 | ||
444 | hashtab_stat(h, &info); | |
445 | printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, " | |
446 | "longest chain length %d\n", h->nel, | |
447 | info.slots_used, h->size, info.max_chain_len); | |
448 | } | |
449 | #else | |
450 | static inline void rangetr_hash_eval(struct hashtab *h) | |
451 | { | |
452 | } | |
1da177e4 LT |
453 | #endif |
454 | ||
455 | /* | |
456 | * Define the other val_to_name and val_to_struct arrays | |
457 | * in a policy database structure. | |
458 | * | |
459 | * Caller must clean up on failure. | |
460 | */ | |
1d9bc6dc | 461 | static int policydb_index(struct policydb *p) |
1da177e4 | 462 | { |
9398c7f7 | 463 | int i, rc; |
1da177e4 | 464 | |
454d972c | 465 | printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools", |
1da177e4 | 466 | p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim); |
0719aaf5 | 467 | if (p->mls_enabled) |
1da177e4 LT |
468 | printk(", %d sens, %d cats", p->p_levels.nprim, |
469 | p->p_cats.nprim); | |
470 | printk("\n"); | |
471 | ||
454d972c | 472 | printk(KERN_DEBUG "SELinux: %d classes, %d rules\n", |
1da177e4 LT |
473 | p->p_classes.nprim, p->te_avtab.nel); |
474 | ||
475 | #ifdef DEBUG_HASHES | |
476 | avtab_hash_eval(&p->te_avtab, "rules"); | |
477 | symtab_hash_eval(p->symtab); | |
478 | #endif | |
479 | ||
1d9bc6dc EP |
480 | rc = -ENOMEM; |
481 | p->class_val_to_struct = | |
482 | kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), | |
483 | GFP_KERNEL); | |
484 | if (!p->class_val_to_struct) | |
485 | goto out; | |
486 | ||
9398c7f7 | 487 | rc = -ENOMEM; |
1da177e4 LT |
488 | p->role_val_to_struct = |
489 | kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)), | |
2ced3dfd | 490 | GFP_KERNEL); |
9398c7f7 | 491 | if (!p->role_val_to_struct) |
1da177e4 | 492 | goto out; |
1da177e4 | 493 | |
9398c7f7 | 494 | rc = -ENOMEM; |
1da177e4 LT |
495 | p->user_val_to_struct = |
496 | kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)), | |
2ced3dfd | 497 | GFP_KERNEL); |
9398c7f7 | 498 | if (!p->user_val_to_struct) |
1da177e4 | 499 | goto out; |
1da177e4 | 500 | |
23bdecb0 | 501 | /* Yes, I want the sizeof the pointer, not the structure */ |
9398c7f7 | 502 | rc = -ENOMEM; |
23bdecb0 EP |
503 | p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *), |
504 | p->p_types.nprim, | |
505 | GFP_KERNEL | __GFP_ZERO); | |
506 | if (!p->type_val_to_struct_array) | |
507 | goto out; | |
508 | ||
509 | rc = flex_array_prealloc(p->type_val_to_struct_array, 0, | |
510 | p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO); | |
511 | if (rc) | |
d9250dea | 512 | goto out; |
d9250dea | 513 | |
3ac285ff DB |
514 | rc = cond_init_bool_indexes(p); |
515 | if (rc) | |
1da177e4 | 516 | goto out; |
1da177e4 | 517 | |
1d9bc6dc | 518 | for (i = 0; i < SYM_NUM; i++) { |
9398c7f7 | 519 | rc = -ENOMEM; |
ac76c05b EP |
520 | p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *), |
521 | p->symtab[i].nprim, | |
522 | GFP_KERNEL | __GFP_ZERO); | |
9398c7f7 | 523 | if (!p->sym_val_to_name[i]) |
1da177e4 | 524 | goto out; |
ac76c05b EP |
525 | |
526 | rc = flex_array_prealloc(p->sym_val_to_name[i], | |
527 | 0, p->symtab[i].nprim - 1, | |
528 | GFP_KERNEL | __GFP_ZERO); | |
529 | if (rc) | |
530 | goto out; | |
531 | ||
1da177e4 LT |
532 | rc = hashtab_map(p->symtab[i].table, index_f[i], p); |
533 | if (rc) | |
534 | goto out; | |
535 | } | |
9398c7f7 | 536 | rc = 0; |
1da177e4 LT |
537 | out: |
538 | return rc; | |
539 | } | |
540 | ||
541 | /* | |
542 | * The following *_destroy functions are used to | |
543 | * free any memory allocated for each kind of | |
544 | * symbol data in the policy database. | |
545 | */ | |
546 | ||
547 | static int perm_destroy(void *key, void *datum, void *p) | |
548 | { | |
549 | kfree(key); | |
550 | kfree(datum); | |
551 | return 0; | |
552 | } | |
553 | ||
554 | static int common_destroy(void *key, void *datum, void *p) | |
555 | { | |
556 | struct common_datum *comdatum; | |
557 | ||
558 | kfree(key); | |
9398c7f7 EP |
559 | if (datum) { |
560 | comdatum = datum; | |
561 | hashtab_map(comdatum->permissions.table, perm_destroy, NULL); | |
562 | hashtab_destroy(comdatum->permissions.table); | |
563 | } | |
1da177e4 LT |
564 | kfree(datum); |
565 | return 0; | |
566 | } | |
567 | ||
6cbda6b6 | 568 | static int cls_destroy(void *key, void *datum, void *p) |
1da177e4 LT |
569 | { |
570 | struct class_datum *cladatum; | |
571 | struct constraint_node *constraint, *ctemp; | |
572 | struct constraint_expr *e, *etmp; | |
573 | ||
574 | kfree(key); | |
9398c7f7 EP |
575 | if (datum) { |
576 | cladatum = datum; | |
577 | hashtab_map(cladatum->permissions.table, perm_destroy, NULL); | |
578 | hashtab_destroy(cladatum->permissions.table); | |
579 | constraint = cladatum->constraints; | |
580 | while (constraint) { | |
581 | e = constraint->expr; | |
582 | while (e) { | |
583 | ebitmap_destroy(&e->names); | |
584 | etmp = e; | |
585 | e = e->next; | |
586 | kfree(etmp); | |
587 | } | |
588 | ctemp = constraint; | |
589 | constraint = constraint->next; | |
590 | kfree(ctemp); | |
1da177e4 | 591 | } |
9398c7f7 EP |
592 | |
593 | constraint = cladatum->validatetrans; | |
594 | while (constraint) { | |
595 | e = constraint->expr; | |
596 | while (e) { | |
597 | ebitmap_destroy(&e->names); | |
598 | etmp = e; | |
599 | e = e->next; | |
600 | kfree(etmp); | |
601 | } | |
602 | ctemp = constraint; | |
603 | constraint = constraint->next; | |
604 | kfree(ctemp); | |
1da177e4 | 605 | } |
1da177e4 | 606 | |
9398c7f7 EP |
607 | kfree(cladatum->comkey); |
608 | } | |
1da177e4 LT |
609 | kfree(datum); |
610 | return 0; | |
611 | } | |
612 | ||
613 | static int role_destroy(void *key, void *datum, void *p) | |
614 | { | |
615 | struct role_datum *role; | |
616 | ||
617 | kfree(key); | |
9398c7f7 EP |
618 | if (datum) { |
619 | role = datum; | |
620 | ebitmap_destroy(&role->dominates); | |
621 | ebitmap_destroy(&role->types); | |
622 | } | |
1da177e4 LT |
623 | kfree(datum); |
624 | return 0; | |
625 | } | |
626 | ||
627 | static int type_destroy(void *key, void *datum, void *p) | |
628 | { | |
629 | kfree(key); | |
630 | kfree(datum); | |
631 | return 0; | |
632 | } | |
633 | ||
634 | static int user_destroy(void *key, void *datum, void *p) | |
635 | { | |
636 | struct user_datum *usrdatum; | |
637 | ||
638 | kfree(key); | |
9398c7f7 EP |
639 | if (datum) { |
640 | usrdatum = datum; | |
641 | ebitmap_destroy(&usrdatum->roles); | |
642 | ebitmap_destroy(&usrdatum->range.level[0].cat); | |
643 | ebitmap_destroy(&usrdatum->range.level[1].cat); | |
644 | ebitmap_destroy(&usrdatum->dfltlevel.cat); | |
645 | } | |
1da177e4 LT |
646 | kfree(datum); |
647 | return 0; | |
648 | } | |
649 | ||
650 | static int sens_destroy(void *key, void *datum, void *p) | |
651 | { | |
652 | struct level_datum *levdatum; | |
653 | ||
654 | kfree(key); | |
9398c7f7 EP |
655 | if (datum) { |
656 | levdatum = datum; | |
657 | ebitmap_destroy(&levdatum->level->cat); | |
658 | kfree(levdatum->level); | |
659 | } | |
1da177e4 LT |
660 | kfree(datum); |
661 | return 0; | |
662 | } | |
663 | ||
664 | static int cat_destroy(void *key, void *datum, void *p) | |
665 | { | |
666 | kfree(key); | |
667 | kfree(datum); | |
668 | return 0; | |
669 | } | |
670 | ||
671 | static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = | |
672 | { | |
673 | common_destroy, | |
6cbda6b6 | 674 | cls_destroy, |
1da177e4 LT |
675 | role_destroy, |
676 | type_destroy, | |
677 | user_destroy, | |
678 | cond_destroy_bool, | |
679 | sens_destroy, | |
680 | cat_destroy, | |
681 | }; | |
682 | ||
2f3e82d6 SS |
683 | static int range_tr_destroy(void *key, void *datum, void *p) |
684 | { | |
685 | struct mls_range *rt = datum; | |
686 | kfree(key); | |
687 | ebitmap_destroy(&rt->level[0].cat); | |
688 | ebitmap_destroy(&rt->level[1].cat); | |
689 | kfree(datum); | |
690 | cond_resched(); | |
691 | return 0; | |
692 | } | |
693 | ||
1da177e4 LT |
694 | static void ocontext_destroy(struct ocontext *c, int i) |
695 | { | |
d1b43547 EP |
696 | if (!c) |
697 | return; | |
698 | ||
1da177e4 LT |
699 | context_destroy(&c->context[0]); |
700 | context_destroy(&c->context[1]); | |
701 | if (i == OCON_ISID || i == OCON_FS || | |
702 | i == OCON_NETIF || i == OCON_FSUSE) | |
703 | kfree(c->u.name); | |
704 | kfree(c); | |
705 | } | |
706 | ||
707 | /* | |
708 | * Free any memory allocated by a policy database structure. | |
709 | */ | |
710 | void policydb_destroy(struct policydb *p) | |
711 | { | |
712 | struct ocontext *c, *ctmp; | |
713 | struct genfs *g, *gtmp; | |
714 | int i; | |
782ebb99 SS |
715 | struct role_allow *ra, *lra = NULL; |
716 | struct role_trans *tr, *ltr = NULL; | |
652bb9b0 | 717 | struct filename_trans *ft, *nft; |
1da177e4 LT |
718 | |
719 | for (i = 0; i < SYM_NUM; i++) { | |
9dc99780 | 720 | cond_resched(); |
1da177e4 LT |
721 | hashtab_map(p->symtab[i].table, destroy_f[i], NULL); |
722 | hashtab_destroy(p->symtab[i].table); | |
723 | } | |
724 | ||
ac76c05b EP |
725 | for (i = 0; i < SYM_NUM; i++) { |
726 | if (p->sym_val_to_name[i]) | |
727 | flex_array_free(p->sym_val_to_name[i]); | |
728 | } | |
1da177e4 | 729 | |
9a5f04bf JJ |
730 | kfree(p->class_val_to_struct); |
731 | kfree(p->role_val_to_struct); | |
732 | kfree(p->user_val_to_struct); | |
23bdecb0 EP |
733 | if (p->type_val_to_struct_array) |
734 | flex_array_free(p->type_val_to_struct_array); | |
1da177e4 LT |
735 | |
736 | avtab_destroy(&p->te_avtab); | |
737 | ||
738 | for (i = 0; i < OCON_NUM; i++) { | |
9dc99780 | 739 | cond_resched(); |
1da177e4 LT |
740 | c = p->ocontexts[i]; |
741 | while (c) { | |
742 | ctmp = c; | |
743 | c = c->next; | |
2ced3dfd | 744 | ocontext_destroy(ctmp, i); |
1da177e4 | 745 | } |
6e8c751e | 746 | p->ocontexts[i] = NULL; |
1da177e4 LT |
747 | } |
748 | ||
749 | g = p->genfs; | |
750 | while (g) { | |
9dc99780 | 751 | cond_resched(); |
1da177e4 LT |
752 | kfree(g->fstype); |
753 | c = g->head; | |
754 | while (c) { | |
755 | ctmp = c; | |
756 | c = c->next; | |
2ced3dfd | 757 | ocontext_destroy(ctmp, OCON_FSUSE); |
1da177e4 LT |
758 | } |
759 | gtmp = g; | |
760 | g = g->next; | |
761 | kfree(gtmp); | |
762 | } | |
6e8c751e | 763 | p->genfs = NULL; |
1da177e4 LT |
764 | |
765 | cond_policydb_destroy(p); | |
766 | ||
782ebb99 | 767 | for (tr = p->role_tr; tr; tr = tr->next) { |
9dc99780 | 768 | cond_resched(); |
a7f988ba | 769 | kfree(ltr); |
782ebb99 SS |
770 | ltr = tr; |
771 | } | |
a7f988ba | 772 | kfree(ltr); |
782ebb99 | 773 | |
2ced3dfd | 774 | for (ra = p->role_allow; ra; ra = ra->next) { |
9dc99780 | 775 | cond_resched(); |
a7f988ba | 776 | kfree(lra); |
782ebb99 SS |
777 | lra = ra; |
778 | } | |
a7f988ba | 779 | kfree(lra); |
782ebb99 | 780 | |
2f3e82d6 SS |
781 | hashtab_map(p->range_tr, range_tr_destroy, NULL); |
782 | hashtab_destroy(p->range_tr); | |
782ebb99 | 783 | |
6371dcd3 EP |
784 | if (p->type_attr_map_array) { |
785 | for (i = 0; i < p->p_types.nprim; i++) { | |
786 | struct ebitmap *e; | |
787 | ||
788 | e = flex_array_get(p->type_attr_map_array, i); | |
789 | if (!e) | |
790 | continue; | |
791 | ebitmap_destroy(e); | |
792 | } | |
793 | flex_array_free(p->type_attr_map_array); | |
282c1f5e | 794 | } |
652bb9b0 EP |
795 | |
796 | ft = p->filename_trans; | |
797 | while (ft) { | |
798 | nft = ft->next; | |
799 | kfree(ft->name); | |
800 | kfree(ft); | |
801 | ft = nft; | |
802 | } | |
803 | ||
3bb56b25 | 804 | ebitmap_destroy(&p->policycaps); |
64dbf074 | 805 | ebitmap_destroy(&p->permissive_map); |
3f12070e | 806 | |
1da177e4 LT |
807 | return; |
808 | } | |
809 | ||
810 | /* | |
811 | * Load the initial SIDs specified in a policy database | |
812 | * structure into a SID table. | |
813 | */ | |
814 | int policydb_load_isids(struct policydb *p, struct sidtab *s) | |
815 | { | |
816 | struct ocontext *head, *c; | |
817 | int rc; | |
818 | ||
819 | rc = sidtab_init(s); | |
820 | if (rc) { | |
454d972c | 821 | printk(KERN_ERR "SELinux: out of memory on SID table init\n"); |
1da177e4 LT |
822 | goto out; |
823 | } | |
824 | ||
825 | head = p->ocontexts[OCON_ISID]; | |
826 | for (c = head; c; c = c->next) { | |
9398c7f7 | 827 | rc = -EINVAL; |
1da177e4 | 828 | if (!c->context[0].user) { |
9398c7f7 EP |
829 | printk(KERN_ERR "SELinux: SID %s was never defined.\n", |
830 | c->u.name); | |
1da177e4 LT |
831 | goto out; |
832 | } | |
9398c7f7 EP |
833 | |
834 | rc = sidtab_insert(s, c->sid[0], &c->context[0]); | |
835 | if (rc) { | |
836 | printk(KERN_ERR "SELinux: unable to load initial SID %s.\n", | |
837 | c->u.name); | |
1da177e4 LT |
838 | goto out; |
839 | } | |
840 | } | |
9398c7f7 | 841 | rc = 0; |
1da177e4 LT |
842 | out: |
843 | return rc; | |
844 | } | |
845 | ||
45e5421e SS |
846 | int policydb_class_isvalid(struct policydb *p, unsigned int class) |
847 | { | |
848 | if (!class || class > p->p_classes.nprim) | |
849 | return 0; | |
850 | return 1; | |
851 | } | |
852 | ||
853 | int policydb_role_isvalid(struct policydb *p, unsigned int role) | |
854 | { | |
855 | if (!role || role > p->p_roles.nprim) | |
856 | return 0; | |
857 | return 1; | |
858 | } | |
859 | ||
860 | int policydb_type_isvalid(struct policydb *p, unsigned int type) | |
861 | { | |
862 | if (!type || type > p->p_types.nprim) | |
863 | return 0; | |
864 | return 1; | |
865 | } | |
866 | ||
1da177e4 LT |
867 | /* |
868 | * Return 1 if the fields in the security context | |
869 | * structure `c' are valid. Return 0 otherwise. | |
870 | */ | |
871 | int policydb_context_isvalid(struct policydb *p, struct context *c) | |
872 | { | |
873 | struct role_datum *role; | |
874 | struct user_datum *usrdatum; | |
875 | ||
876 | if (!c->role || c->role > p->p_roles.nprim) | |
877 | return 0; | |
878 | ||
879 | if (!c->user || c->user > p->p_users.nprim) | |
880 | return 0; | |
881 | ||
882 | if (!c->type || c->type > p->p_types.nprim) | |
883 | return 0; | |
884 | ||
885 | if (c->role != OBJECT_R_VAL) { | |
886 | /* | |
887 | * Role must be authorized for the type. | |
888 | */ | |
889 | role = p->role_val_to_struct[c->role - 1]; | |
9398c7f7 | 890 | if (!ebitmap_get_bit(&role->types, c->type - 1)) |
1da177e4 LT |
891 | /* role may not be associated with type */ |
892 | return 0; | |
893 | ||
894 | /* | |
895 | * User must be authorized for the role. | |
896 | */ | |
897 | usrdatum = p->user_val_to_struct[c->user - 1]; | |
898 | if (!usrdatum) | |
899 | return 0; | |
900 | ||
9398c7f7 | 901 | if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1)) |
1da177e4 LT |
902 | /* user may not be associated with role */ |
903 | return 0; | |
904 | } | |
905 | ||
906 | if (!mls_context_isvalid(p, c)) | |
907 | return 0; | |
908 | ||
909 | return 1; | |
910 | } | |
911 | ||
912 | /* | |
913 | * Read a MLS range structure from a policydb binary | |
914 | * representation file. | |
915 | */ | |
916 | static int mls_read_range_helper(struct mls_range *r, void *fp) | |
917 | { | |
b5bf6c55 AD |
918 | __le32 buf[2]; |
919 | u32 items; | |
1da177e4 LT |
920 | int rc; |
921 | ||
922 | rc = next_entry(buf, fp, sizeof(u32)); | |
9398c7f7 | 923 | if (rc) |
1da177e4 LT |
924 | goto out; |
925 | ||
9398c7f7 | 926 | rc = -EINVAL; |
1da177e4 LT |
927 | items = le32_to_cpu(buf[0]); |
928 | if (items > ARRAY_SIZE(buf)) { | |
454d972c | 929 | printk(KERN_ERR "SELinux: mls: range overflow\n"); |
1da177e4 LT |
930 | goto out; |
931 | } | |
9398c7f7 | 932 | |
1da177e4 | 933 | rc = next_entry(buf, fp, sizeof(u32) * items); |
9398c7f7 | 934 | if (rc) { |
454d972c | 935 | printk(KERN_ERR "SELinux: mls: truncated range\n"); |
1da177e4 LT |
936 | goto out; |
937 | } | |
9398c7f7 | 938 | |
1da177e4 LT |
939 | r->level[0].sens = le32_to_cpu(buf[0]); |
940 | if (items > 1) | |
941 | r->level[1].sens = le32_to_cpu(buf[1]); | |
942 | else | |
943 | r->level[1].sens = r->level[0].sens; | |
944 | ||
945 | rc = ebitmap_read(&r->level[0].cat, fp); | |
946 | if (rc) { | |
9398c7f7 | 947 | printk(KERN_ERR "SELinux: mls: error reading low categories\n"); |
1da177e4 LT |
948 | goto out; |
949 | } | |
950 | if (items > 1) { | |
951 | rc = ebitmap_read(&r->level[1].cat, fp); | |
952 | if (rc) { | |
9398c7f7 | 953 | printk(KERN_ERR "SELinux: mls: error reading high categories\n"); |
1da177e4 LT |
954 | goto bad_high; |
955 | } | |
956 | } else { | |
957 | rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat); | |
958 | if (rc) { | |
454d972c | 959 | printk(KERN_ERR "SELinux: mls: out of memory\n"); |
1da177e4 LT |
960 | goto bad_high; |
961 | } | |
962 | } | |
963 | ||
9398c7f7 | 964 | return 0; |
1da177e4 LT |
965 | bad_high: |
966 | ebitmap_destroy(&r->level[0].cat); | |
9398c7f7 EP |
967 | out: |
968 | return rc; | |
1da177e4 LT |
969 | } |
970 | ||
971 | /* | |
972 | * Read and validate a security context structure | |
973 | * from a policydb binary representation file. | |
974 | */ | |
975 | static int context_read_and_validate(struct context *c, | |
976 | struct policydb *p, | |
977 | void *fp) | |
978 | { | |
b5bf6c55 | 979 | __le32 buf[3]; |
1da177e4 LT |
980 | int rc; |
981 | ||
982 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 983 | if (rc) { |
454d972c | 984 | printk(KERN_ERR "SELinux: context truncated\n"); |
1da177e4 LT |
985 | goto out; |
986 | } | |
987 | c->user = le32_to_cpu(buf[0]); | |
988 | c->role = le32_to_cpu(buf[1]); | |
989 | c->type = le32_to_cpu(buf[2]); | |
990 | if (p->policyvers >= POLICYDB_VERSION_MLS) { | |
9398c7f7 EP |
991 | rc = mls_read_range_helper(&c->range, fp); |
992 | if (rc) { | |
993 | printk(KERN_ERR "SELinux: error reading MLS range of context\n"); | |
1da177e4 LT |
994 | goto out; |
995 | } | |
996 | } | |
997 | ||
9398c7f7 | 998 | rc = -EINVAL; |
1da177e4 | 999 | if (!policydb_context_isvalid(p, c)) { |
454d972c | 1000 | printk(KERN_ERR "SELinux: invalid security context\n"); |
1da177e4 | 1001 | context_destroy(c); |
9398c7f7 | 1002 | goto out; |
1da177e4 | 1003 | } |
9398c7f7 | 1004 | rc = 0; |
1da177e4 LT |
1005 | out: |
1006 | return rc; | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * The following *_read functions are used to | |
1011 | * read the symbol data from a policy database | |
1012 | * binary representation file. | |
1013 | */ | |
1014 | ||
1015 | static int perm_read(struct policydb *p, struct hashtab *h, void *fp) | |
1016 | { | |
1017 | char *key = NULL; | |
1018 | struct perm_datum *perdatum; | |
1019 | int rc; | |
b5bf6c55 AD |
1020 | __le32 buf[2]; |
1021 | u32 len; | |
1da177e4 | 1022 | |
9398c7f7 | 1023 | rc = -ENOMEM; |
89d155ef | 1024 | perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL); |
9398c7f7 EP |
1025 | if (!perdatum) |
1026 | goto bad; | |
1da177e4 LT |
1027 | |
1028 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 1029 | if (rc) |
1da177e4 LT |
1030 | goto bad; |
1031 | ||
1032 | len = le32_to_cpu(buf[0]); | |
1033 | perdatum->value = le32_to_cpu(buf[1]); | |
1034 | ||
9398c7f7 | 1035 | rc = -ENOMEM; |
2ced3dfd | 1036 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1037 | if (!key) |
1da177e4 | 1038 | goto bad; |
9398c7f7 | 1039 | |
1da177e4 | 1040 | rc = next_entry(key, fp, len); |
9398c7f7 | 1041 | if (rc) |
1da177e4 | 1042 | goto bad; |
df4ea865 | 1043 | key[len] = '\0'; |
1da177e4 LT |
1044 | |
1045 | rc = hashtab_insert(h, key, perdatum); | |
1046 | if (rc) | |
1047 | goto bad; | |
9398c7f7 EP |
1048 | |
1049 | return 0; | |
1da177e4 LT |
1050 | bad: |
1051 | perm_destroy(key, perdatum, NULL); | |
9398c7f7 | 1052 | return rc; |
1da177e4 LT |
1053 | } |
1054 | ||
1055 | static int common_read(struct policydb *p, struct hashtab *h, void *fp) | |
1056 | { | |
1057 | char *key = NULL; | |
1058 | struct common_datum *comdatum; | |
b5bf6c55 AD |
1059 | __le32 buf[4]; |
1060 | u32 len, nel; | |
1da177e4 LT |
1061 | int i, rc; |
1062 | ||
9398c7f7 | 1063 | rc = -ENOMEM; |
89d155ef | 1064 | comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL); |
9398c7f7 EP |
1065 | if (!comdatum) |
1066 | goto bad; | |
1da177e4 LT |
1067 | |
1068 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 1069 | if (rc) |
1da177e4 LT |
1070 | goto bad; |
1071 | ||
1072 | len = le32_to_cpu(buf[0]); | |
1073 | comdatum->value = le32_to_cpu(buf[1]); | |
1074 | ||
1075 | rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE); | |
1076 | if (rc) | |
1077 | goto bad; | |
1078 | comdatum->permissions.nprim = le32_to_cpu(buf[2]); | |
1079 | nel = le32_to_cpu(buf[3]); | |
1080 | ||
9398c7f7 | 1081 | rc = -ENOMEM; |
2ced3dfd | 1082 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1083 | if (!key) |
1da177e4 | 1084 | goto bad; |
9398c7f7 | 1085 | |
1da177e4 | 1086 | rc = next_entry(key, fp, len); |
9398c7f7 | 1087 | if (rc) |
1da177e4 | 1088 | goto bad; |
df4ea865 | 1089 | key[len] = '\0'; |
1da177e4 LT |
1090 | |
1091 | for (i = 0; i < nel; i++) { | |
1092 | rc = perm_read(p, comdatum->permissions.table, fp); | |
1093 | if (rc) | |
1094 | goto bad; | |
1095 | } | |
1096 | ||
1097 | rc = hashtab_insert(h, key, comdatum); | |
1098 | if (rc) | |
1099 | goto bad; | |
9398c7f7 | 1100 | return 0; |
1da177e4 LT |
1101 | bad: |
1102 | common_destroy(key, comdatum, NULL); | |
9398c7f7 | 1103 | return rc; |
1da177e4 LT |
1104 | } |
1105 | ||
1106 | static int read_cons_helper(struct constraint_node **nodep, int ncons, | |
2ced3dfd | 1107 | int allowxtarget, void *fp) |
1da177e4 LT |
1108 | { |
1109 | struct constraint_node *c, *lc; | |
1110 | struct constraint_expr *e, *le; | |
b5bf6c55 AD |
1111 | __le32 buf[3]; |
1112 | u32 nexpr; | |
1da177e4 LT |
1113 | int rc, i, j, depth; |
1114 | ||
1115 | lc = NULL; | |
1116 | for (i = 0; i < ncons; i++) { | |
89d155ef | 1117 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
1da177e4 LT |
1118 | if (!c) |
1119 | return -ENOMEM; | |
1da177e4 | 1120 | |
2ced3dfd | 1121 | if (lc) |
1da177e4 | 1122 | lc->next = c; |
2ced3dfd | 1123 | else |
1da177e4 | 1124 | *nodep = c; |
1da177e4 LT |
1125 | |
1126 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); | |
9398c7f7 | 1127 | if (rc) |
1da177e4 LT |
1128 | return rc; |
1129 | c->permissions = le32_to_cpu(buf[0]); | |
1130 | nexpr = le32_to_cpu(buf[1]); | |
1131 | le = NULL; | |
1132 | depth = -1; | |
1133 | for (j = 0; j < nexpr; j++) { | |
89d155ef | 1134 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
1da177e4 LT |
1135 | if (!e) |
1136 | return -ENOMEM; | |
1da177e4 | 1137 | |
2ced3dfd | 1138 | if (le) |
1da177e4 | 1139 | le->next = e; |
2ced3dfd | 1140 | else |
1da177e4 | 1141 | c->expr = e; |
1da177e4 LT |
1142 | |
1143 | rc = next_entry(buf, fp, (sizeof(u32) * 3)); | |
9398c7f7 | 1144 | if (rc) |
1da177e4 LT |
1145 | return rc; |
1146 | e->expr_type = le32_to_cpu(buf[0]); | |
1147 | e->attr = le32_to_cpu(buf[1]); | |
1148 | e->op = le32_to_cpu(buf[2]); | |
1149 | ||
1150 | switch (e->expr_type) { | |
1151 | case CEXPR_NOT: | |
1152 | if (depth < 0) | |
1153 | return -EINVAL; | |
1154 | break; | |
1155 | case CEXPR_AND: | |
1156 | case CEXPR_OR: | |
1157 | if (depth < 1) | |
1158 | return -EINVAL; | |
1159 | depth--; | |
1160 | break; | |
1161 | case CEXPR_ATTR: | |
1162 | if (depth == (CEXPR_MAXDEPTH - 1)) | |
1163 | return -EINVAL; | |
1164 | depth++; | |
1165 | break; | |
1166 | case CEXPR_NAMES: | |
1167 | if (!allowxtarget && (e->attr & CEXPR_XTARGET)) | |
1168 | return -EINVAL; | |
1169 | if (depth == (CEXPR_MAXDEPTH - 1)) | |
1170 | return -EINVAL; | |
1171 | depth++; | |
9398c7f7 EP |
1172 | rc = ebitmap_read(&e->names, fp); |
1173 | if (rc) | |
1174 | return rc; | |
1da177e4 LT |
1175 | break; |
1176 | default: | |
1177 | return -EINVAL; | |
1178 | } | |
1179 | le = e; | |
1180 | } | |
1181 | if (depth != 0) | |
1182 | return -EINVAL; | |
1183 | lc = c; | |
1184 | } | |
1185 | ||
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | static int class_read(struct policydb *p, struct hashtab *h, void *fp) | |
1190 | { | |
1191 | char *key = NULL; | |
1192 | struct class_datum *cladatum; | |
b5bf6c55 AD |
1193 | __le32 buf[6]; |
1194 | u32 len, len2, ncons, nel; | |
1da177e4 LT |
1195 | int i, rc; |
1196 | ||
9398c7f7 | 1197 | rc = -ENOMEM; |
89d155ef | 1198 | cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL); |
9398c7f7 EP |
1199 | if (!cladatum) |
1200 | goto bad; | |
1da177e4 LT |
1201 | |
1202 | rc = next_entry(buf, fp, sizeof(u32)*6); | |
9398c7f7 | 1203 | if (rc) |
1da177e4 LT |
1204 | goto bad; |
1205 | ||
1206 | len = le32_to_cpu(buf[0]); | |
1207 | len2 = le32_to_cpu(buf[1]); | |
1208 | cladatum->value = le32_to_cpu(buf[2]); | |
1209 | ||
1210 | rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE); | |
1211 | if (rc) | |
1212 | goto bad; | |
1213 | cladatum->permissions.nprim = le32_to_cpu(buf[3]); | |
1214 | nel = le32_to_cpu(buf[4]); | |
1215 | ||
1216 | ncons = le32_to_cpu(buf[5]); | |
1217 | ||
9398c7f7 | 1218 | rc = -ENOMEM; |
2ced3dfd | 1219 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1220 | if (!key) |
1da177e4 | 1221 | goto bad; |
9398c7f7 | 1222 | |
1da177e4 | 1223 | rc = next_entry(key, fp, len); |
9398c7f7 | 1224 | if (rc) |
1da177e4 | 1225 | goto bad; |
df4ea865 | 1226 | key[len] = '\0'; |
1da177e4 LT |
1227 | |
1228 | if (len2) { | |
9398c7f7 | 1229 | rc = -ENOMEM; |
2ced3dfd | 1230 | cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL); |
9398c7f7 | 1231 | if (!cladatum->comkey) |
1da177e4 | 1232 | goto bad; |
1da177e4 | 1233 | rc = next_entry(cladatum->comkey, fp, len2); |
9398c7f7 | 1234 | if (rc) |
1da177e4 | 1235 | goto bad; |
df4ea865 | 1236 | cladatum->comkey[len2] = '\0'; |
1da177e4 | 1237 | |
9398c7f7 EP |
1238 | rc = -EINVAL; |
1239 | cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); | |
1da177e4 | 1240 | if (!cladatum->comdatum) { |
9398c7f7 | 1241 | printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey); |
1da177e4 LT |
1242 | goto bad; |
1243 | } | |
1244 | } | |
1245 | for (i = 0; i < nel; i++) { | |
1246 | rc = perm_read(p, cladatum->permissions.table, fp); | |
1247 | if (rc) | |
1248 | goto bad; | |
1249 | } | |
1250 | ||
1251 | rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp); | |
1252 | if (rc) | |
1253 | goto bad; | |
1254 | ||
1255 | if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) { | |
1256 | /* grab the validatetrans rules */ | |
1257 | rc = next_entry(buf, fp, sizeof(u32)); | |
9398c7f7 | 1258 | if (rc) |
1da177e4 LT |
1259 | goto bad; |
1260 | ncons = le32_to_cpu(buf[0]); | |
1261 | rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp); | |
1262 | if (rc) | |
1263 | goto bad; | |
1264 | } | |
1265 | ||
1266 | rc = hashtab_insert(h, key, cladatum); | |
1267 | if (rc) | |
1268 | goto bad; | |
1269 | ||
9398c7f7 | 1270 | return 0; |
1da177e4 | 1271 | bad: |
6cbda6b6 | 1272 | cls_destroy(key, cladatum, NULL); |
9398c7f7 | 1273 | return rc; |
1da177e4 LT |
1274 | } |
1275 | ||
1276 | static int role_read(struct policydb *p, struct hashtab *h, void *fp) | |
1277 | { | |
1278 | char *key = NULL; | |
1279 | struct role_datum *role; | |
d9250dea KK |
1280 | int rc, to_read = 2; |
1281 | __le32 buf[3]; | |
b5bf6c55 | 1282 | u32 len; |
1da177e4 | 1283 | |
9398c7f7 | 1284 | rc = -ENOMEM; |
89d155ef | 1285 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
9398c7f7 EP |
1286 | if (!role) |
1287 | goto bad; | |
1da177e4 | 1288 | |
d9250dea KK |
1289 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1290 | to_read = 3; | |
1291 | ||
1292 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); | |
9398c7f7 | 1293 | if (rc) |
1da177e4 LT |
1294 | goto bad; |
1295 | ||
1296 | len = le32_to_cpu(buf[0]); | |
1297 | role->value = le32_to_cpu(buf[1]); | |
d9250dea KK |
1298 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1299 | role->bounds = le32_to_cpu(buf[2]); | |
1da177e4 | 1300 | |
9398c7f7 | 1301 | rc = -ENOMEM; |
2ced3dfd | 1302 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1303 | if (!key) |
1da177e4 | 1304 | goto bad; |
9398c7f7 | 1305 | |
1da177e4 | 1306 | rc = next_entry(key, fp, len); |
9398c7f7 | 1307 | if (rc) |
1da177e4 | 1308 | goto bad; |
df4ea865 | 1309 | key[len] = '\0'; |
1da177e4 LT |
1310 | |
1311 | rc = ebitmap_read(&role->dominates, fp); | |
1312 | if (rc) | |
1313 | goto bad; | |
1314 | ||
1315 | rc = ebitmap_read(&role->types, fp); | |
1316 | if (rc) | |
1317 | goto bad; | |
1318 | ||
1319 | if (strcmp(key, OBJECT_R) == 0) { | |
9398c7f7 | 1320 | rc = -EINVAL; |
1da177e4 | 1321 | if (role->value != OBJECT_R_VAL) { |
744ba35e | 1322 | printk(KERN_ERR "SELinux: Role %s has wrong value %d\n", |
1da177e4 | 1323 | OBJECT_R, role->value); |
1da177e4 LT |
1324 | goto bad; |
1325 | } | |
1326 | rc = 0; | |
1327 | goto bad; | |
1328 | } | |
1329 | ||
1330 | rc = hashtab_insert(h, key, role); | |
1331 | if (rc) | |
1332 | goto bad; | |
9398c7f7 | 1333 | return 0; |
1da177e4 LT |
1334 | bad: |
1335 | role_destroy(key, role, NULL); | |
9398c7f7 | 1336 | return rc; |
1da177e4 LT |
1337 | } |
1338 | ||
1339 | static int type_read(struct policydb *p, struct hashtab *h, void *fp) | |
1340 | { | |
1341 | char *key = NULL; | |
1342 | struct type_datum *typdatum; | |
d9250dea KK |
1343 | int rc, to_read = 3; |
1344 | __le32 buf[4]; | |
b5bf6c55 | 1345 | u32 len; |
1da177e4 | 1346 | |
9398c7f7 | 1347 | rc = -ENOMEM; |
2ced3dfd | 1348 | typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL); |
9398c7f7 EP |
1349 | if (!typdatum) |
1350 | goto bad; | |
1da177e4 | 1351 | |
d9250dea KK |
1352 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1353 | to_read = 4; | |
1354 | ||
1355 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); | |
9398c7f7 | 1356 | if (rc) |
1da177e4 LT |
1357 | goto bad; |
1358 | ||
1359 | len = le32_to_cpu(buf[0]); | |
1360 | typdatum->value = le32_to_cpu(buf[1]); | |
d9250dea KK |
1361 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { |
1362 | u32 prop = le32_to_cpu(buf[2]); | |
1363 | ||
1364 | if (prop & TYPEDATUM_PROPERTY_PRIMARY) | |
1365 | typdatum->primary = 1; | |
1366 | if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE) | |
1367 | typdatum->attribute = 1; | |
1368 | ||
1369 | typdatum->bounds = le32_to_cpu(buf[3]); | |
1370 | } else { | |
1371 | typdatum->primary = le32_to_cpu(buf[2]); | |
1372 | } | |
1da177e4 | 1373 | |
9398c7f7 | 1374 | rc = -ENOMEM; |
2ced3dfd | 1375 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1376 | if (!key) |
1da177e4 | 1377 | goto bad; |
1da177e4 | 1378 | rc = next_entry(key, fp, len); |
9398c7f7 | 1379 | if (rc) |
1da177e4 | 1380 | goto bad; |
df4ea865 | 1381 | key[len] = '\0'; |
1da177e4 LT |
1382 | |
1383 | rc = hashtab_insert(h, key, typdatum); | |
1384 | if (rc) | |
1385 | goto bad; | |
9398c7f7 | 1386 | return 0; |
1da177e4 LT |
1387 | bad: |
1388 | type_destroy(key, typdatum, NULL); | |
9398c7f7 | 1389 | return rc; |
1da177e4 LT |
1390 | } |
1391 | ||
1392 | ||
1393 | /* | |
1394 | * Read a MLS level structure from a policydb binary | |
1395 | * representation file. | |
1396 | */ | |
1397 | static int mls_read_level(struct mls_level *lp, void *fp) | |
1398 | { | |
b5bf6c55 | 1399 | __le32 buf[1]; |
1da177e4 LT |
1400 | int rc; |
1401 | ||
1402 | memset(lp, 0, sizeof(*lp)); | |
1403 | ||
1404 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 1405 | if (rc) { |
454d972c | 1406 | printk(KERN_ERR "SELinux: mls: truncated level\n"); |
9398c7f7 | 1407 | return rc; |
1da177e4 LT |
1408 | } |
1409 | lp->sens = le32_to_cpu(buf[0]); | |
1410 | ||
9398c7f7 EP |
1411 | rc = ebitmap_read(&lp->cat, fp); |
1412 | if (rc) { | |
1413 | printk(KERN_ERR "SELinux: mls: error reading level categories\n"); | |
1414 | return rc; | |
1da177e4 LT |
1415 | } |
1416 | return 0; | |
1da177e4 LT |
1417 | } |
1418 | ||
1419 | static int user_read(struct policydb *p, struct hashtab *h, void *fp) | |
1420 | { | |
1421 | char *key = NULL; | |
1422 | struct user_datum *usrdatum; | |
d9250dea KK |
1423 | int rc, to_read = 2; |
1424 | __le32 buf[3]; | |
b5bf6c55 | 1425 | u32 len; |
1da177e4 | 1426 | |
9398c7f7 | 1427 | rc = -ENOMEM; |
89d155ef | 1428 | usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL); |
9398c7f7 EP |
1429 | if (!usrdatum) |
1430 | goto bad; | |
1da177e4 | 1431 | |
d9250dea KK |
1432 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1433 | to_read = 3; | |
1434 | ||
1435 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); | |
9398c7f7 | 1436 | if (rc) |
1da177e4 LT |
1437 | goto bad; |
1438 | ||
1439 | len = le32_to_cpu(buf[0]); | |
1440 | usrdatum->value = le32_to_cpu(buf[1]); | |
d9250dea KK |
1441 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1442 | usrdatum->bounds = le32_to_cpu(buf[2]); | |
1da177e4 | 1443 | |
9398c7f7 | 1444 | rc = -ENOMEM; |
2ced3dfd | 1445 | key = kmalloc(len + 1, GFP_KERNEL); |
9398c7f7 | 1446 | if (!key) |
1da177e4 | 1447 | goto bad; |
1da177e4 | 1448 | rc = next_entry(key, fp, len); |
9398c7f7 | 1449 | if (rc) |
1da177e4 | 1450 | goto bad; |
df4ea865 | 1451 | key[len] = '\0'; |
1da177e4 LT |
1452 | |
1453 | rc = ebitmap_read(&usrdatum->roles, fp); | |
1454 | if (rc) | |
1455 | goto bad; | |
1456 | ||
1457 | if (p->policyvers >= POLICYDB_VERSION_MLS) { | |
1458 | rc = mls_read_range_helper(&usrdatum->range, fp); | |
1459 | if (rc) | |
1460 | goto bad; | |
1461 | rc = mls_read_level(&usrdatum->dfltlevel, fp); | |
1462 | if (rc) | |
1463 | goto bad; | |
1464 | } | |
1465 | ||
1466 | rc = hashtab_insert(h, key, usrdatum); | |
1467 | if (rc) | |
1468 | goto bad; | |
9398c7f7 | 1469 | return 0; |
1da177e4 LT |
1470 | bad: |
1471 | user_destroy(key, usrdatum, NULL); | |
9398c7f7 | 1472 | return rc; |
1da177e4 LT |
1473 | } |
1474 | ||
1475 | static int sens_read(struct policydb *p, struct hashtab *h, void *fp) | |
1476 | { | |
1477 | char *key = NULL; | |
1478 | struct level_datum *levdatum; | |
1479 | int rc; | |
b5bf6c55 AD |
1480 | __le32 buf[2]; |
1481 | u32 len; | |
1da177e4 | 1482 | |
9398c7f7 | 1483 | rc = -ENOMEM; |
89d155ef | 1484 | levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC); |
9398c7f7 EP |
1485 | if (!levdatum) |
1486 | goto bad; | |
1da177e4 LT |
1487 | |
1488 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 1489 | if (rc) |
1da177e4 LT |
1490 | goto bad; |
1491 | ||
1492 | len = le32_to_cpu(buf[0]); | |
1493 | levdatum->isalias = le32_to_cpu(buf[1]); | |
1494 | ||
9398c7f7 | 1495 | rc = -ENOMEM; |
2ced3dfd | 1496 | key = kmalloc(len + 1, GFP_ATOMIC); |
9398c7f7 | 1497 | if (!key) |
1da177e4 | 1498 | goto bad; |
1da177e4 | 1499 | rc = next_entry(key, fp, len); |
9398c7f7 | 1500 | if (rc) |
1da177e4 | 1501 | goto bad; |
df4ea865 | 1502 | key[len] = '\0'; |
1da177e4 | 1503 | |
9398c7f7 | 1504 | rc = -ENOMEM; |
1da177e4 | 1505 | levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); |
9398c7f7 | 1506 | if (!levdatum->level) |
1da177e4 | 1507 | goto bad; |
9398c7f7 EP |
1508 | |
1509 | rc = mls_read_level(levdatum->level, fp); | |
1510 | if (rc) | |
1da177e4 | 1511 | goto bad; |
1da177e4 LT |
1512 | |
1513 | rc = hashtab_insert(h, key, levdatum); | |
1514 | if (rc) | |
1515 | goto bad; | |
9398c7f7 | 1516 | return 0; |
1da177e4 LT |
1517 | bad: |
1518 | sens_destroy(key, levdatum, NULL); | |
9398c7f7 | 1519 | return rc; |
1da177e4 LT |
1520 | } |
1521 | ||
1522 | static int cat_read(struct policydb *p, struct hashtab *h, void *fp) | |
1523 | { | |
1524 | char *key = NULL; | |
1525 | struct cat_datum *catdatum; | |
1526 | int rc; | |
b5bf6c55 AD |
1527 | __le32 buf[3]; |
1528 | u32 len; | |
1da177e4 | 1529 | |
9398c7f7 | 1530 | rc = -ENOMEM; |
89d155ef | 1531 | catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC); |
9398c7f7 EP |
1532 | if (!catdatum) |
1533 | goto bad; | |
1da177e4 LT |
1534 | |
1535 | rc = next_entry(buf, fp, sizeof buf); | |
9398c7f7 | 1536 | if (rc) |
1da177e4 LT |
1537 | goto bad; |
1538 | ||
1539 | len = le32_to_cpu(buf[0]); | |
1540 | catdatum->value = le32_to_cpu(buf[1]); | |
1541 | catdatum->isalias = le32_to_cpu(buf[2]); | |
1542 | ||
9398c7f7 | 1543 | rc = -ENOMEM; |
2ced3dfd | 1544 | key = kmalloc(len + 1, GFP_ATOMIC); |
9398c7f7 | 1545 | if (!key) |
1da177e4 | 1546 | goto bad; |
1da177e4 | 1547 | rc = next_entry(key, fp, len); |
9398c7f7 | 1548 | if (rc) |
1da177e4 | 1549 | goto bad; |
df4ea865 | 1550 | key[len] = '\0'; |
1da177e4 LT |
1551 | |
1552 | rc = hashtab_insert(h, key, catdatum); | |
1553 | if (rc) | |
1554 | goto bad; | |
9398c7f7 | 1555 | return 0; |
1da177e4 LT |
1556 | bad: |
1557 | cat_destroy(key, catdatum, NULL); | |
9398c7f7 | 1558 | return rc; |
1da177e4 LT |
1559 | } |
1560 | ||
1561 | static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) = | |
1562 | { | |
1563 | common_read, | |
1564 | class_read, | |
1565 | role_read, | |
1566 | type_read, | |
1567 | user_read, | |
1568 | cond_read_bool, | |
1569 | sens_read, | |
1570 | cat_read, | |
1571 | }; | |
1572 | ||
d9250dea KK |
1573 | static int user_bounds_sanity_check(void *key, void *datum, void *datap) |
1574 | { | |
1575 | struct user_datum *upper, *user; | |
1576 | struct policydb *p = datap; | |
1577 | int depth = 0; | |
1578 | ||
1579 | upper = user = datum; | |
1580 | while (upper->bounds) { | |
1581 | struct ebitmap_node *node; | |
1582 | unsigned long bit; | |
1583 | ||
1584 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { | |
1585 | printk(KERN_ERR "SELinux: user %s: " | |
1586 | "too deep or looped boundary", | |
1587 | (char *) key); | |
1588 | return -EINVAL; | |
1589 | } | |
1590 | ||
1591 | upper = p->user_val_to_struct[upper->bounds - 1]; | |
1592 | ebitmap_for_each_positive_bit(&user->roles, node, bit) { | |
1593 | if (ebitmap_get_bit(&upper->roles, bit)) | |
1594 | continue; | |
1595 | ||
1596 | printk(KERN_ERR | |
1597 | "SELinux: boundary violated policy: " | |
1598 | "user=%s role=%s bounds=%s\n", | |
ac76c05b EP |
1599 | sym_name(p, SYM_USERS, user->value - 1), |
1600 | sym_name(p, SYM_ROLES, bit), | |
1601 | sym_name(p, SYM_USERS, upper->value - 1)); | |
d9250dea KK |
1602 | |
1603 | return -EINVAL; | |
1604 | } | |
1605 | } | |
1606 | ||
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | static int role_bounds_sanity_check(void *key, void *datum, void *datap) | |
1611 | { | |
1612 | struct role_datum *upper, *role; | |
1613 | struct policydb *p = datap; | |
1614 | int depth = 0; | |
1615 | ||
1616 | upper = role = datum; | |
1617 | while (upper->bounds) { | |
1618 | struct ebitmap_node *node; | |
1619 | unsigned long bit; | |
1620 | ||
1621 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { | |
1622 | printk(KERN_ERR "SELinux: role %s: " | |
1623 | "too deep or looped bounds\n", | |
1624 | (char *) key); | |
1625 | return -EINVAL; | |
1626 | } | |
1627 | ||
1628 | upper = p->role_val_to_struct[upper->bounds - 1]; | |
1629 | ebitmap_for_each_positive_bit(&role->types, node, bit) { | |
1630 | if (ebitmap_get_bit(&upper->types, bit)) | |
1631 | continue; | |
1632 | ||
1633 | printk(KERN_ERR | |
1634 | "SELinux: boundary violated policy: " | |
1635 | "role=%s type=%s bounds=%s\n", | |
ac76c05b EP |
1636 | sym_name(p, SYM_ROLES, role->value - 1), |
1637 | sym_name(p, SYM_TYPES, bit), | |
1638 | sym_name(p, SYM_ROLES, upper->value - 1)); | |
d9250dea KK |
1639 | |
1640 | return -EINVAL; | |
1641 | } | |
1642 | } | |
1643 | ||
1644 | return 0; | |
1645 | } | |
1646 | ||
1647 | static int type_bounds_sanity_check(void *key, void *datum, void *datap) | |
1648 | { | |
daa6d83a | 1649 | struct type_datum *upper; |
d9250dea KK |
1650 | struct policydb *p = datap; |
1651 | int depth = 0; | |
1652 | ||
daa6d83a | 1653 | upper = datum; |
d9250dea KK |
1654 | while (upper->bounds) { |
1655 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { | |
1656 | printk(KERN_ERR "SELinux: type %s: " | |
1657 | "too deep or looped boundary\n", | |
1658 | (char *) key); | |
1659 | return -EINVAL; | |
1660 | } | |
1661 | ||
23bdecb0 EP |
1662 | upper = flex_array_get_ptr(p->type_val_to_struct_array, |
1663 | upper->bounds - 1); | |
1664 | BUG_ON(!upper); | |
1665 | ||
d9250dea KK |
1666 | if (upper->attribute) { |
1667 | printk(KERN_ERR "SELinux: type %s: " | |
1668 | "bounded by attribute %s", | |
1669 | (char *) key, | |
ac76c05b | 1670 | sym_name(p, SYM_TYPES, upper->value - 1)); |
d9250dea KK |
1671 | return -EINVAL; |
1672 | } | |
1673 | } | |
1674 | ||
1675 | return 0; | |
1676 | } | |
1677 | ||
1678 | static int policydb_bounds_sanity_check(struct policydb *p) | |
1679 | { | |
1680 | int rc; | |
1681 | ||
1682 | if (p->policyvers < POLICYDB_VERSION_BOUNDARY) | |
1683 | return 0; | |
1684 | ||
1685 | rc = hashtab_map(p->p_users.table, | |
1686 | user_bounds_sanity_check, p); | |
1687 | if (rc) | |
1688 | return rc; | |
1689 | ||
1690 | rc = hashtab_map(p->p_roles.table, | |
1691 | role_bounds_sanity_check, p); | |
1692 | if (rc) | |
1693 | return rc; | |
1694 | ||
1695 | rc = hashtab_map(p->p_types.table, | |
1696 | type_bounds_sanity_check, p); | |
1697 | if (rc) | |
1698 | return rc; | |
1699 | ||
1700 | return 0; | |
1701 | } | |
1702 | ||
1da177e4 LT |
1703 | extern int ss_initialized; |
1704 | ||
c6d3aaa4 SS |
1705 | u16 string_to_security_class(struct policydb *p, const char *name) |
1706 | { | |
1707 | struct class_datum *cladatum; | |
1708 | ||
1709 | cladatum = hashtab_search(p->p_classes.table, name); | |
1710 | if (!cladatum) | |
1711 | return 0; | |
1712 | ||
1713 | return cladatum->value; | |
1714 | } | |
1715 | ||
1716 | u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name) | |
1717 | { | |
1718 | struct class_datum *cladatum; | |
1719 | struct perm_datum *perdatum = NULL; | |
1720 | struct common_datum *comdatum; | |
1721 | ||
1722 | if (!tclass || tclass > p->p_classes.nprim) | |
1723 | return 0; | |
1724 | ||
1725 | cladatum = p->class_val_to_struct[tclass-1]; | |
1726 | comdatum = cladatum->comdatum; | |
1727 | if (comdatum) | |
1728 | perdatum = hashtab_search(comdatum->permissions.table, | |
1729 | name); | |
1730 | if (!perdatum) | |
1731 | perdatum = hashtab_search(cladatum->permissions.table, | |
1732 | name); | |
1733 | if (!perdatum) | |
1734 | return 0; | |
1735 | ||
1736 | return 1U << (perdatum->value-1); | |
1737 | } | |
1738 | ||
9ee0c823 EP |
1739 | static int range_read(struct policydb *p, void *fp) |
1740 | { | |
1741 | struct range_trans *rt = NULL; | |
1742 | struct mls_range *r = NULL; | |
1743 | int i, rc; | |
1744 | __le32 buf[2]; | |
1745 | u32 nel; | |
1746 | ||
1747 | if (p->policyvers < POLICYDB_VERSION_MLS) | |
1748 | return 0; | |
1749 | ||
1750 | rc = next_entry(buf, fp, sizeof(u32)); | |
1751 | if (rc) | |
1752 | goto out; | |
1753 | ||
1754 | nel = le32_to_cpu(buf[0]); | |
1755 | for (i = 0; i < nel; i++) { | |
1756 | rc = -ENOMEM; | |
1757 | rt = kzalloc(sizeof(*rt), GFP_KERNEL); | |
1758 | if (!rt) | |
1759 | goto out; | |
1760 | ||
1761 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); | |
1762 | if (rc) | |
1763 | goto out; | |
1764 | ||
1765 | rt->source_type = le32_to_cpu(buf[0]); | |
1766 | rt->target_type = le32_to_cpu(buf[1]); | |
1767 | if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { | |
1768 | rc = next_entry(buf, fp, sizeof(u32)); | |
1769 | if (rc) | |
1770 | goto out; | |
1771 | rt->target_class = le32_to_cpu(buf[0]); | |
1772 | } else | |
1773 | rt->target_class = p->process_class; | |
1774 | ||
1775 | rc = -EINVAL; | |
1776 | if (!policydb_type_isvalid(p, rt->source_type) || | |
1777 | !policydb_type_isvalid(p, rt->target_type) || | |
1778 | !policydb_class_isvalid(p, rt->target_class)) | |
1779 | goto out; | |
1780 | ||
1781 | rc = -ENOMEM; | |
1782 | r = kzalloc(sizeof(*r), GFP_KERNEL); | |
1783 | if (!r) | |
1784 | goto out; | |
1785 | ||
1786 | rc = mls_read_range_helper(r, fp); | |
1787 | if (rc) | |
1788 | goto out; | |
1789 | ||
1790 | rc = -EINVAL; | |
1791 | if (!mls_range_isvalid(p, r)) { | |
1792 | printk(KERN_WARNING "SELinux: rangetrans: invalid range\n"); | |
1793 | goto out; | |
1794 | } | |
1795 | ||
1796 | rc = hashtab_insert(p->range_tr, rt, r); | |
1797 | if (rc) | |
1798 | goto out; | |
1799 | ||
1800 | rt = NULL; | |
1801 | r = NULL; | |
1802 | } | |
1803 | rangetr_hash_eval(p->range_tr); | |
1804 | rc = 0; | |
1805 | out: | |
1806 | kfree(rt); | |
1807 | kfree(r); | |
1808 | return rc; | |
1809 | } | |
1810 | ||
652bb9b0 EP |
1811 | static int filename_trans_read(struct policydb *p, void *fp) |
1812 | { | |
1813 | struct filename_trans *ft, *last; | |
1814 | u32 nel, len; | |
1815 | char *name; | |
1816 | __le32 buf[4]; | |
1817 | int rc, i; | |
1818 | ||
1819 | if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS) | |
1820 | return 0; | |
1821 | ||
1822 | rc = next_entry(buf, fp, sizeof(u32)); | |
1823 | if (rc) | |
1824 | goto out; | |
1825 | nel = le32_to_cpu(buf[0]); | |
1826 | ||
652bb9b0 EP |
1827 | last = p->filename_trans; |
1828 | while (last && last->next) | |
1829 | last = last->next; | |
1830 | ||
1831 | for (i = 0; i < nel; i++) { | |
1832 | rc = -ENOMEM; | |
1833 | ft = kzalloc(sizeof(*ft), GFP_KERNEL); | |
1834 | if (!ft) | |
1835 | goto out; | |
1836 | ||
1837 | /* add it to the tail of the list */ | |
1838 | if (!last) | |
1839 | p->filename_trans = ft; | |
1840 | else | |
1841 | last->next = ft; | |
1842 | last = ft; | |
1843 | ||
1844 | /* length of the path component string */ | |
1845 | rc = next_entry(buf, fp, sizeof(u32)); | |
1846 | if (rc) | |
1847 | goto out; | |
1848 | len = le32_to_cpu(buf[0]); | |
1849 | ||
1850 | rc = -ENOMEM; | |
1851 | name = kmalloc(len + 1, GFP_KERNEL); | |
1852 | if (!name) | |
1853 | goto out; | |
1854 | ||
1855 | ft->name = name; | |
1856 | ||
1857 | /* path component string */ | |
1858 | rc = next_entry(name, fp, len); | |
1859 | if (rc) | |
1860 | goto out; | |
1861 | name[len] = 0; | |
1862 | ||
652bb9b0 EP |
1863 | rc = next_entry(buf, fp, sizeof(u32) * 4); |
1864 | if (rc) | |
1865 | goto out; | |
1866 | ||
1867 | ft->stype = le32_to_cpu(buf[0]); | |
1868 | ft->ttype = le32_to_cpu(buf[1]); | |
1869 | ft->tclass = le32_to_cpu(buf[2]); | |
1870 | ft->otype = le32_to_cpu(buf[3]); | |
1871 | } | |
1872 | rc = 0; | |
1873 | out: | |
1874 | return rc; | |
1875 | } | |
1876 | ||
d1b43547 EP |
1877 | static int genfs_read(struct policydb *p, void *fp) |
1878 | { | |
1879 | int i, j, rc; | |
1880 | u32 nel, nel2, len, len2; | |
1881 | __le32 buf[1]; | |
1882 | struct ocontext *l, *c; | |
1883 | struct ocontext *newc = NULL; | |
1884 | struct genfs *genfs_p, *genfs; | |
1885 | struct genfs *newgenfs = NULL; | |
1886 | ||
1887 | rc = next_entry(buf, fp, sizeof(u32)); | |
1888 | if (rc) | |
1889 | goto out; | |
1890 | nel = le32_to_cpu(buf[0]); | |
1891 | ||
1892 | for (i = 0; i < nel; i++) { | |
1893 | rc = next_entry(buf, fp, sizeof(u32)); | |
1894 | if (rc) | |
1895 | goto out; | |
1896 | len = le32_to_cpu(buf[0]); | |
1897 | ||
1898 | rc = -ENOMEM; | |
1899 | newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL); | |
1900 | if (!newgenfs) | |
1901 | goto out; | |
1902 | ||
1903 | rc = -ENOMEM; | |
1904 | newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL); | |
1905 | if (!newgenfs->fstype) | |
1906 | goto out; | |
1907 | ||
1908 | rc = next_entry(newgenfs->fstype, fp, len); | |
1909 | if (rc) | |
1910 | goto out; | |
1911 | ||
1912 | newgenfs->fstype[len] = 0; | |
1913 | ||
1914 | for (genfs_p = NULL, genfs = p->genfs; genfs; | |
1915 | genfs_p = genfs, genfs = genfs->next) { | |
1916 | rc = -EINVAL; | |
1917 | if (strcmp(newgenfs->fstype, genfs->fstype) == 0) { | |
1918 | printk(KERN_ERR "SELinux: dup genfs fstype %s\n", | |
1919 | newgenfs->fstype); | |
1920 | goto out; | |
1921 | } | |
1922 | if (strcmp(newgenfs->fstype, genfs->fstype) < 0) | |
1923 | break; | |
1924 | } | |
1925 | newgenfs->next = genfs; | |
1926 | if (genfs_p) | |
1927 | genfs_p->next = newgenfs; | |
1928 | else | |
1929 | p->genfs = newgenfs; | |
1930 | genfs = newgenfs; | |
1931 | newgenfs = NULL; | |
1932 | ||
1933 | rc = next_entry(buf, fp, sizeof(u32)); | |
1934 | if (rc) | |
1935 | goto out; | |
1936 | ||
1937 | nel2 = le32_to_cpu(buf[0]); | |
1938 | for (j = 0; j < nel2; j++) { | |
1939 | rc = next_entry(buf, fp, sizeof(u32)); | |
1940 | if (rc) | |
1941 | goto out; | |
1942 | len = le32_to_cpu(buf[0]); | |
1943 | ||
1944 | rc = -ENOMEM; | |
1945 | newc = kzalloc(sizeof(*newc), GFP_KERNEL); | |
1946 | if (!newc) | |
1947 | goto out; | |
1948 | ||
1949 | rc = -ENOMEM; | |
1950 | newc->u.name = kmalloc(len + 1, GFP_KERNEL); | |
1951 | if (!newc->u.name) | |
1952 | goto out; | |
1953 | ||
1954 | rc = next_entry(newc->u.name, fp, len); | |
1955 | if (rc) | |
1956 | goto out; | |
1957 | newc->u.name[len] = 0; | |
1958 | ||
1959 | rc = next_entry(buf, fp, sizeof(u32)); | |
1960 | if (rc) | |
1961 | goto out; | |
1962 | ||
1963 | newc->v.sclass = le32_to_cpu(buf[0]); | |
1964 | rc = context_read_and_validate(&newc->context[0], p, fp); | |
1965 | if (rc) | |
1966 | goto out; | |
1967 | ||
1968 | for (l = NULL, c = genfs->head; c; | |
1969 | l = c, c = c->next) { | |
1970 | rc = -EINVAL; | |
1971 | if (!strcmp(newc->u.name, c->u.name) && | |
1972 | (!c->v.sclass || !newc->v.sclass || | |
1973 | newc->v.sclass == c->v.sclass)) { | |
1974 | printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n", | |
1975 | genfs->fstype, c->u.name); | |
1976 | goto out; | |
1977 | } | |
1978 | len = strlen(newc->u.name); | |
1979 | len2 = strlen(c->u.name); | |
1980 | if (len > len2) | |
1981 | break; | |
1982 | } | |
1983 | ||
1984 | newc->next = c; | |
1985 | if (l) | |
1986 | l->next = newc; | |
1987 | else | |
1988 | genfs->head = newc; | |
1989 | newc = NULL; | |
1990 | } | |
1991 | } | |
1992 | rc = 0; | |
1993 | out: | |
1994 | if (newgenfs) | |
1995 | kfree(newgenfs->fstype); | |
1996 | kfree(newgenfs); | |
1997 | ocontext_destroy(newc, OCON_FSUSE); | |
1998 | ||
1999 | return rc; | |
2000 | } | |
2001 | ||
692a8a23 EP |
2002 | static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, |
2003 | void *fp) | |
2004 | { | |
2005 | int i, j, rc; | |
2006 | u32 nel, len; | |
2007 | __le32 buf[3]; | |
2008 | struct ocontext *l, *c; | |
2009 | u32 nodebuf[8]; | |
2010 | ||
2011 | for (i = 0; i < info->ocon_num; i++) { | |
2012 | rc = next_entry(buf, fp, sizeof(u32)); | |
2013 | if (rc) | |
2014 | goto out; | |
2015 | nel = le32_to_cpu(buf[0]); | |
2016 | ||
2017 | l = NULL; | |
2018 | for (j = 0; j < nel; j++) { | |
2019 | rc = -ENOMEM; | |
2020 | c = kzalloc(sizeof(*c), GFP_KERNEL); | |
2021 | if (!c) | |
2022 | goto out; | |
2023 | if (l) | |
2024 | l->next = c; | |
2025 | else | |
2026 | p->ocontexts[i] = c; | |
2027 | l = c; | |
2028 | ||
2029 | switch (i) { | |
2030 | case OCON_ISID: | |
2031 | rc = next_entry(buf, fp, sizeof(u32)); | |
2032 | if (rc) | |
2033 | goto out; | |
2034 | ||
2035 | c->sid[0] = le32_to_cpu(buf[0]); | |
2036 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2037 | if (rc) | |
2038 | goto out; | |
2039 | break; | |
2040 | case OCON_FS: | |
2041 | case OCON_NETIF: | |
2042 | rc = next_entry(buf, fp, sizeof(u32)); | |
2043 | if (rc) | |
2044 | goto out; | |
2045 | len = le32_to_cpu(buf[0]); | |
2046 | ||
2047 | rc = -ENOMEM; | |
2048 | c->u.name = kmalloc(len + 1, GFP_KERNEL); | |
2049 | if (!c->u.name) | |
2050 | goto out; | |
2051 | ||
2052 | rc = next_entry(c->u.name, fp, len); | |
2053 | if (rc) | |
2054 | goto out; | |
2055 | ||
2056 | c->u.name[len] = 0; | |
2057 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2058 | if (rc) | |
2059 | goto out; | |
2060 | rc = context_read_and_validate(&c->context[1], p, fp); | |
2061 | if (rc) | |
2062 | goto out; | |
2063 | break; | |
2064 | case OCON_PORT: | |
2065 | rc = next_entry(buf, fp, sizeof(u32)*3); | |
2066 | if (rc) | |
2067 | goto out; | |
2068 | c->u.port.protocol = le32_to_cpu(buf[0]); | |
2069 | c->u.port.low_port = le32_to_cpu(buf[1]); | |
2070 | c->u.port.high_port = le32_to_cpu(buf[2]); | |
2071 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2072 | if (rc) | |
2073 | goto out; | |
2074 | break; | |
2075 | case OCON_NODE: | |
2076 | rc = next_entry(nodebuf, fp, sizeof(u32) * 2); | |
2077 | if (rc) | |
2078 | goto out; | |
2079 | c->u.node.addr = nodebuf[0]; /* network order */ | |
2080 | c->u.node.mask = nodebuf[1]; /* network order */ | |
2081 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2082 | if (rc) | |
2083 | goto out; | |
2084 | break; | |
2085 | case OCON_FSUSE: | |
2086 | rc = next_entry(buf, fp, sizeof(u32)*2); | |
2087 | if (rc) | |
2088 | goto out; | |
2089 | ||
2090 | rc = -EINVAL; | |
2091 | c->v.behavior = le32_to_cpu(buf[0]); | |
2092 | if (c->v.behavior > SECURITY_FS_USE_NONE) | |
2093 | goto out; | |
2094 | ||
2095 | rc = -ENOMEM; | |
2096 | len = le32_to_cpu(buf[1]); | |
2097 | c->u.name = kmalloc(len + 1, GFP_KERNEL); | |
2098 | if (!c->u.name) | |
2099 | goto out; | |
2100 | ||
2101 | rc = next_entry(c->u.name, fp, len); | |
2102 | if (rc) | |
2103 | goto out; | |
2104 | c->u.name[len] = 0; | |
2105 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2106 | if (rc) | |
2107 | goto out; | |
2108 | break; | |
2109 | case OCON_NODE6: { | |
2110 | int k; | |
2111 | ||
2112 | rc = next_entry(nodebuf, fp, sizeof(u32) * 8); | |
2113 | if (rc) | |
2114 | goto out; | |
2115 | for (k = 0; k < 4; k++) | |
2116 | c->u.node6.addr[k] = nodebuf[k]; | |
2117 | for (k = 0; k < 4; k++) | |
2118 | c->u.node6.mask[k] = nodebuf[k+4]; | |
2119 | rc = context_read_and_validate(&c->context[0], p, fp); | |
2120 | if (rc) | |
2121 | goto out; | |
2122 | break; | |
2123 | } | |
2124 | } | |
2125 | } | |
2126 | } | |
2127 | rc = 0; | |
2128 | out: | |
2129 | return rc; | |
2130 | } | |
2131 | ||
1da177e4 LT |
2132 | /* |
2133 | * Read the configuration data from a policy database binary | |
2134 | * representation file into a policy database structure. | |
2135 | */ | |
2136 | int policydb_read(struct policydb *p, void *fp) | |
2137 | { | |
2138 | struct role_allow *ra, *lra; | |
2139 | struct role_trans *tr, *ltr; | |
1da177e4 | 2140 | int i, j, rc; |
59dbd1ba | 2141 | __le32 buf[4]; |
d1b43547 EP |
2142 | u32 len, nprim, nel; |
2143 | ||
1da177e4 LT |
2144 | char *policydb_str; |
2145 | struct policydb_compat_info *info; | |
1da177e4 | 2146 | |
1da177e4 LT |
2147 | rc = policydb_init(p); |
2148 | if (rc) | |
9398c7f7 | 2149 | return rc; |
1da177e4 LT |
2150 | |
2151 | /* Read the magic number and string length. */ | |
2ced3dfd | 2152 | rc = next_entry(buf, fp, sizeof(u32) * 2); |
9398c7f7 | 2153 | if (rc) |
1da177e4 LT |
2154 | goto bad; |
2155 | ||
9398c7f7 | 2156 | rc = -EINVAL; |
b5bf6c55 | 2157 | if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) { |
454d972c | 2158 | printk(KERN_ERR "SELinux: policydb magic number 0x%x does " |
1da177e4 | 2159 | "not match expected magic number 0x%x\n", |
b5bf6c55 | 2160 | le32_to_cpu(buf[0]), POLICYDB_MAGIC); |
1da177e4 LT |
2161 | goto bad; |
2162 | } | |
2163 | ||
9398c7f7 | 2164 | rc = -EINVAL; |
b5bf6c55 | 2165 | len = le32_to_cpu(buf[1]); |
1da177e4 | 2166 | if (len != strlen(POLICYDB_STRING)) { |
454d972c | 2167 | printk(KERN_ERR "SELinux: policydb string length %d does not " |
1da177e4 LT |
2168 | "match expected length %Zu\n", |
2169 | len, strlen(POLICYDB_STRING)); | |
2170 | goto bad; | |
2171 | } | |
9398c7f7 EP |
2172 | |
2173 | rc = -ENOMEM; | |
2ced3dfd | 2174 | policydb_str = kmalloc(len + 1, GFP_KERNEL); |
1da177e4 | 2175 | if (!policydb_str) { |
454d972c | 2176 | printk(KERN_ERR "SELinux: unable to allocate memory for policydb " |
1da177e4 | 2177 | "string of length %d\n", len); |
1da177e4 LT |
2178 | goto bad; |
2179 | } | |
9398c7f7 | 2180 | |
1da177e4 | 2181 | rc = next_entry(policydb_str, fp, len); |
9398c7f7 | 2182 | if (rc) { |
454d972c | 2183 | printk(KERN_ERR "SELinux: truncated policydb string identifier\n"); |
1da177e4 LT |
2184 | kfree(policydb_str); |
2185 | goto bad; | |
2186 | } | |
9398c7f7 EP |
2187 | |
2188 | rc = -EINVAL; | |
df4ea865 | 2189 | policydb_str[len] = '\0'; |
1da177e4 | 2190 | if (strcmp(policydb_str, POLICYDB_STRING)) { |
454d972c | 2191 | printk(KERN_ERR "SELinux: policydb string %s does not match " |
1da177e4 LT |
2192 | "my string %s\n", policydb_str, POLICYDB_STRING); |
2193 | kfree(policydb_str); | |
2194 | goto bad; | |
2195 | } | |
2196 | /* Done with policydb_str. */ | |
2197 | kfree(policydb_str); | |
2198 | policydb_str = NULL; | |
2199 | ||
0719aaf5 | 2200 | /* Read the version and table sizes. */ |
1da177e4 | 2201 | rc = next_entry(buf, fp, sizeof(u32)*4); |
9398c7f7 | 2202 | if (rc) |
1da177e4 | 2203 | goto bad; |
1da177e4 | 2204 | |
9398c7f7 | 2205 | rc = -EINVAL; |
b5bf6c55 | 2206 | p->policyvers = le32_to_cpu(buf[0]); |
1da177e4 LT |
2207 | if (p->policyvers < POLICYDB_VERSION_MIN || |
2208 | p->policyvers > POLICYDB_VERSION_MAX) { | |
454d972c | 2209 | printk(KERN_ERR "SELinux: policydb version %d does not match " |
2ced3dfd EP |
2210 | "my version range %d-%d\n", |
2211 | le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX); | |
2212 | goto bad; | |
1da177e4 LT |
2213 | } |
2214 | ||
b5bf6c55 | 2215 | if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) { |
0719aaf5 | 2216 | p->mls_enabled = 1; |
1da177e4 | 2217 | |
9398c7f7 | 2218 | rc = -EINVAL; |
1da177e4 | 2219 | if (p->policyvers < POLICYDB_VERSION_MLS) { |
744ba35e EP |
2220 | printk(KERN_ERR "SELinux: security policydb version %d " |
2221 | "(MLS) not backwards compatible\n", | |
2222 | p->policyvers); | |
1da177e4 LT |
2223 | goto bad; |
2224 | } | |
1da177e4 | 2225 | } |
3f12070e EP |
2226 | p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN); |
2227 | p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN); | |
1da177e4 | 2228 | |
9398c7f7 EP |
2229 | if (p->policyvers >= POLICYDB_VERSION_POLCAP) { |
2230 | rc = ebitmap_read(&p->policycaps, fp); | |
2231 | if (rc) | |
2232 | goto bad; | |
2233 | } | |
3bb56b25 | 2234 | |
9398c7f7 EP |
2235 | if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { |
2236 | rc = ebitmap_read(&p->permissive_map, fp); | |
2237 | if (rc) | |
2238 | goto bad; | |
2239 | } | |
64dbf074 | 2240 | |
9398c7f7 | 2241 | rc = -EINVAL; |
1da177e4 LT |
2242 | info = policydb_lookup_compat(p->policyvers); |
2243 | if (!info) { | |
454d972c | 2244 | printk(KERN_ERR "SELinux: unable to find policy compat info " |
1da177e4 LT |
2245 | "for version %d\n", p->policyvers); |
2246 | goto bad; | |
2247 | } | |
2248 | ||
9398c7f7 | 2249 | rc = -EINVAL; |
b5bf6c55 AD |
2250 | if (le32_to_cpu(buf[2]) != info->sym_num || |
2251 | le32_to_cpu(buf[3]) != info->ocon_num) { | |
454d972c | 2252 | printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do " |
b5bf6c55 AD |
2253 | "not match mine (%d,%d)\n", le32_to_cpu(buf[2]), |
2254 | le32_to_cpu(buf[3]), | |
1da177e4 LT |
2255 | info->sym_num, info->ocon_num); |
2256 | goto bad; | |
2257 | } | |
2258 | ||
2259 | for (i = 0; i < info->sym_num; i++) { | |
2260 | rc = next_entry(buf, fp, sizeof(u32)*2); | |
9398c7f7 | 2261 | if (rc) |
1da177e4 LT |
2262 | goto bad; |
2263 | nprim = le32_to_cpu(buf[0]); | |
2264 | nel = le32_to_cpu(buf[1]); | |
2265 | for (j = 0; j < nel; j++) { | |
2266 | rc = read_f[i](p, p->symtab[i].table, fp); | |
2267 | if (rc) | |
2268 | goto bad; | |
2269 | } | |
2270 | ||
2271 | p->symtab[i].nprim = nprim; | |
2272 | } | |
2273 | ||
1214eac7 HC |
2274 | rc = -EINVAL; |
2275 | p->process_class = string_to_security_class(p, "process"); | |
2276 | if (!p->process_class) | |
2277 | goto bad; | |
2278 | ||
45e5421e | 2279 | rc = avtab_read(&p->te_avtab, fp, p); |
1da177e4 LT |
2280 | if (rc) |
2281 | goto bad; | |
2282 | ||
2283 | if (p->policyvers >= POLICYDB_VERSION_BOOL) { | |
2284 | rc = cond_read_list(p, fp); | |
2285 | if (rc) | |
2286 | goto bad; | |
2287 | } | |
2288 | ||
2289 | rc = next_entry(buf, fp, sizeof(u32)); | |
9398c7f7 | 2290 | if (rc) |
1da177e4 LT |
2291 | goto bad; |
2292 | nel = le32_to_cpu(buf[0]); | |
2293 | ltr = NULL; | |
2294 | for (i = 0; i < nel; i++) { | |
9398c7f7 | 2295 | rc = -ENOMEM; |
89d155ef | 2296 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
9398c7f7 | 2297 | if (!tr) |
1da177e4 | 2298 | goto bad; |
2ced3dfd | 2299 | if (ltr) |
1da177e4 | 2300 | ltr->next = tr; |
2ced3dfd | 2301 | else |
1da177e4 | 2302 | p->role_tr = tr; |
1da177e4 | 2303 | rc = next_entry(buf, fp, sizeof(u32)*3); |
9398c7f7 | 2304 | if (rc) |
1da177e4 | 2305 | goto bad; |
9398c7f7 EP |
2306 | |
2307 | rc = -EINVAL; | |
1da177e4 LT |
2308 | tr->role = le32_to_cpu(buf[0]); |
2309 | tr->type = le32_to_cpu(buf[1]); | |
2310 | tr->new_role = le32_to_cpu(buf[2]); | |
8023976c HC |
2311 | if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) { |
2312 | rc = next_entry(buf, fp, sizeof(u32)); | |
2313 | if (rc) | |
2314 | goto bad; | |
2315 | tr->tclass = le32_to_cpu(buf[0]); | |
2316 | } else | |
2317 | tr->tclass = p->process_class; | |
2318 | ||
45e5421e SS |
2319 | if (!policydb_role_isvalid(p, tr->role) || |
2320 | !policydb_type_isvalid(p, tr->type) || | |
8023976c | 2321 | !policydb_class_isvalid(p, tr->tclass) || |
9398c7f7 | 2322 | !policydb_role_isvalid(p, tr->new_role)) |
45e5421e | 2323 | goto bad; |
1da177e4 LT |
2324 | ltr = tr; |
2325 | } | |
2326 | ||
2327 | rc = next_entry(buf, fp, sizeof(u32)); | |
9398c7f7 | 2328 | if (rc) |
1da177e4 LT |
2329 | goto bad; |
2330 | nel = le32_to_cpu(buf[0]); | |
2331 | lra = NULL; | |
2332 | for (i = 0; i < nel; i++) { | |
9398c7f7 | 2333 | rc = -ENOMEM; |
89d155ef | 2334 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
9398c7f7 | 2335 | if (!ra) |
1da177e4 | 2336 | goto bad; |
2ced3dfd | 2337 | if (lra) |
1da177e4 | 2338 | lra->next = ra; |
2ced3dfd | 2339 | else |
1da177e4 | 2340 | p->role_allow = ra; |
1da177e4 | 2341 | rc = next_entry(buf, fp, sizeof(u32)*2); |
9398c7f7 | 2342 | if (rc) |
1da177e4 | 2343 | goto bad; |
9398c7f7 EP |
2344 | |
2345 | rc = -EINVAL; | |
1da177e4 LT |
2346 | ra->role = le32_to_cpu(buf[0]); |
2347 | ra->new_role = le32_to_cpu(buf[1]); | |
45e5421e | 2348 | if (!policydb_role_isvalid(p, ra->role) || |
9398c7f7 | 2349 | !policydb_role_isvalid(p, ra->new_role)) |
45e5421e | 2350 | goto bad; |
1da177e4 LT |
2351 | lra = ra; |
2352 | } | |
2353 | ||
652bb9b0 EP |
2354 | rc = filename_trans_read(p, fp); |
2355 | if (rc) | |
2356 | goto bad; | |
2357 | ||
1d9bc6dc | 2358 | rc = policydb_index(p); |
1da177e4 LT |
2359 | if (rc) |
2360 | goto bad; | |
2361 | ||
9398c7f7 EP |
2362 | rc = -EINVAL; |
2363 | p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition"); | |
2364 | p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition"); | |
c6d3aaa4 SS |
2365 | if (!p->process_trans_perms) |
2366 | goto bad; | |
2367 | ||
692a8a23 EP |
2368 | rc = ocontext_read(p, info, fp); |
2369 | if (rc) | |
2370 | goto bad; | |
1da177e4 | 2371 | |
d1b43547 EP |
2372 | rc = genfs_read(p, fp); |
2373 | if (rc) | |
1da177e4 | 2374 | goto bad; |
1da177e4 | 2375 | |
9ee0c823 EP |
2376 | rc = range_read(p, fp); |
2377 | if (rc) | |
2378 | goto bad; | |
1da177e4 | 2379 | |
6371dcd3 EP |
2380 | rc = -ENOMEM; |
2381 | p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap), | |
2382 | p->p_types.nprim, | |
2383 | GFP_KERNEL | __GFP_ZERO); | |
2384 | if (!p->type_attr_map_array) | |
2385 | goto bad; | |
2386 | ||
2387 | /* preallocate so we don't have to worry about the put ever failing */ | |
2388 | rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1, | |
2389 | GFP_KERNEL | __GFP_ZERO); | |
2390 | if (rc) | |
782ebb99 SS |
2391 | goto bad; |
2392 | ||
2393 | for (i = 0; i < p->p_types.nprim; i++) { | |
6371dcd3 EP |
2394 | struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); |
2395 | ||
2396 | BUG_ON(!e); | |
2397 | ebitmap_init(e); | |
782ebb99 | 2398 | if (p->policyvers >= POLICYDB_VERSION_AVTAB) { |
6371dcd3 EP |
2399 | rc = ebitmap_read(e, fp); |
2400 | if (rc) | |
782ebb99 SS |
2401 | goto bad; |
2402 | } | |
2403 | /* add the type itself as the degenerate case */ | |
6371dcd3 EP |
2404 | rc = ebitmap_set_bit(e, i, 1); |
2405 | if (rc) | |
2406 | goto bad; | |
782ebb99 SS |
2407 | } |
2408 | ||
d9250dea KK |
2409 | rc = policydb_bounds_sanity_check(p); |
2410 | if (rc) | |
2411 | goto bad; | |
2412 | ||
1da177e4 LT |
2413 | rc = 0; |
2414 | out: | |
2415 | return rc; | |
1da177e4 | 2416 | bad: |
1da177e4 LT |
2417 | policydb_destroy(p); |
2418 | goto out; | |
2419 | } | |
cee74f47 EP |
2420 | |
2421 | /* | |
2422 | * Write a MLS level structure to a policydb binary | |
2423 | * representation file. | |
2424 | */ | |
2425 | static int mls_write_level(struct mls_level *l, void *fp) | |
2426 | { | |
2427 | __le32 buf[1]; | |
2428 | int rc; | |
2429 | ||
2430 | buf[0] = cpu_to_le32(l->sens); | |
2431 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2432 | if (rc) | |
2433 | return rc; | |
2434 | ||
2435 | rc = ebitmap_write(&l->cat, fp); | |
2436 | if (rc) | |
2437 | return rc; | |
2438 | ||
2439 | return 0; | |
2440 | } | |
2441 | ||
2442 | /* | |
2443 | * Write a MLS range structure to a policydb binary | |
2444 | * representation file. | |
2445 | */ | |
2446 | static int mls_write_range_helper(struct mls_range *r, void *fp) | |
2447 | { | |
2448 | __le32 buf[3]; | |
2449 | size_t items; | |
2450 | int rc, eq; | |
2451 | ||
2452 | eq = mls_level_eq(&r->level[1], &r->level[0]); | |
2453 | ||
2454 | if (eq) | |
2455 | items = 2; | |
2456 | else | |
2457 | items = 3; | |
2458 | buf[0] = cpu_to_le32(items-1); | |
2459 | buf[1] = cpu_to_le32(r->level[0].sens); | |
2460 | if (!eq) | |
2461 | buf[2] = cpu_to_le32(r->level[1].sens); | |
2462 | ||
2463 | BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); | |
2464 | ||
2465 | rc = put_entry(buf, sizeof(u32), items, fp); | |
2466 | if (rc) | |
2467 | return rc; | |
2468 | ||
2469 | rc = ebitmap_write(&r->level[0].cat, fp); | |
2470 | if (rc) | |
2471 | return rc; | |
2472 | if (!eq) { | |
2473 | rc = ebitmap_write(&r->level[1].cat, fp); | |
2474 | if (rc) | |
2475 | return rc; | |
2476 | } | |
2477 | ||
2478 | return 0; | |
2479 | } | |
2480 | ||
2481 | static int sens_write(void *vkey, void *datum, void *ptr) | |
2482 | { | |
2483 | char *key = vkey; | |
2484 | struct level_datum *levdatum = datum; | |
2485 | struct policy_data *pd = ptr; | |
2486 | void *fp = pd->fp; | |
2487 | __le32 buf[2]; | |
2488 | size_t len; | |
2489 | int rc; | |
2490 | ||
2491 | len = strlen(key); | |
2492 | buf[0] = cpu_to_le32(len); | |
2493 | buf[1] = cpu_to_le32(levdatum->isalias); | |
2494 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
2495 | if (rc) | |
2496 | return rc; | |
2497 | ||
2498 | rc = put_entry(key, 1, len, fp); | |
2499 | if (rc) | |
2500 | return rc; | |
2501 | ||
2502 | rc = mls_write_level(levdatum->level, fp); | |
2503 | if (rc) | |
2504 | return rc; | |
2505 | ||
2506 | return 0; | |
2507 | } | |
2508 | ||
2509 | static int cat_write(void *vkey, void *datum, void *ptr) | |
2510 | { | |
2511 | char *key = vkey; | |
2512 | struct cat_datum *catdatum = datum; | |
2513 | struct policy_data *pd = ptr; | |
2514 | void *fp = pd->fp; | |
2515 | __le32 buf[3]; | |
2516 | size_t len; | |
2517 | int rc; | |
2518 | ||
2519 | len = strlen(key); | |
2520 | buf[0] = cpu_to_le32(len); | |
2521 | buf[1] = cpu_to_le32(catdatum->value); | |
2522 | buf[2] = cpu_to_le32(catdatum->isalias); | |
2523 | rc = put_entry(buf, sizeof(u32), 3, fp); | |
2524 | if (rc) | |
2525 | return rc; | |
2526 | ||
2527 | rc = put_entry(key, 1, len, fp); | |
2528 | if (rc) | |
2529 | return rc; | |
2530 | ||
2531 | return 0; | |
2532 | } | |
2533 | ||
c900ff32 | 2534 | static int role_trans_write(struct policydb *p, void *fp) |
cee74f47 | 2535 | { |
c900ff32 | 2536 | struct role_trans *r = p->role_tr; |
cee74f47 EP |
2537 | struct role_trans *tr; |
2538 | u32 buf[3]; | |
2539 | size_t nel; | |
2540 | int rc; | |
2541 | ||
2542 | nel = 0; | |
2543 | for (tr = r; tr; tr = tr->next) | |
2544 | nel++; | |
2545 | buf[0] = cpu_to_le32(nel); | |
2546 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2547 | if (rc) | |
2548 | return rc; | |
2549 | for (tr = r; tr; tr = tr->next) { | |
2550 | buf[0] = cpu_to_le32(tr->role); | |
2551 | buf[1] = cpu_to_le32(tr->type); | |
2552 | buf[2] = cpu_to_le32(tr->new_role); | |
2553 | rc = put_entry(buf, sizeof(u32), 3, fp); | |
2554 | if (rc) | |
2555 | return rc; | |
c900ff32 HC |
2556 | if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) { |
2557 | buf[0] = cpu_to_le32(tr->tclass); | |
2558 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2559 | if (rc) | |
2560 | return rc; | |
2561 | } | |
cee74f47 EP |
2562 | } |
2563 | ||
2564 | return 0; | |
2565 | } | |
2566 | ||
2567 | static int role_allow_write(struct role_allow *r, void *fp) | |
2568 | { | |
2569 | struct role_allow *ra; | |
2570 | u32 buf[2]; | |
2571 | size_t nel; | |
2572 | int rc; | |
2573 | ||
2574 | nel = 0; | |
2575 | for (ra = r; ra; ra = ra->next) | |
2576 | nel++; | |
2577 | buf[0] = cpu_to_le32(nel); | |
2578 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2579 | if (rc) | |
2580 | return rc; | |
2581 | for (ra = r; ra; ra = ra->next) { | |
2582 | buf[0] = cpu_to_le32(ra->role); | |
2583 | buf[1] = cpu_to_le32(ra->new_role); | |
2584 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
2585 | if (rc) | |
2586 | return rc; | |
2587 | } | |
2588 | return 0; | |
2589 | } | |
2590 | ||
2591 | /* | |
2592 | * Write a security context structure | |
2593 | * to a policydb binary representation file. | |
2594 | */ | |
2595 | static int context_write(struct policydb *p, struct context *c, | |
2596 | void *fp) | |
2597 | { | |
2598 | int rc; | |
2599 | __le32 buf[3]; | |
2600 | ||
2601 | buf[0] = cpu_to_le32(c->user); | |
2602 | buf[1] = cpu_to_le32(c->role); | |
2603 | buf[2] = cpu_to_le32(c->type); | |
2604 | ||
2605 | rc = put_entry(buf, sizeof(u32), 3, fp); | |
2606 | if (rc) | |
2607 | return rc; | |
2608 | ||
2609 | rc = mls_write_range_helper(&c->range, fp); | |
2610 | if (rc) | |
2611 | return rc; | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | /* | |
2617 | * The following *_write functions are used to | |
2618 | * write the symbol data to a policy database | |
2619 | * binary representation file. | |
2620 | */ | |
2621 | ||
2622 | static int perm_write(void *vkey, void *datum, void *fp) | |
2623 | { | |
2624 | char *key = vkey; | |
2625 | struct perm_datum *perdatum = datum; | |
2626 | __le32 buf[2]; | |
2627 | size_t len; | |
2628 | int rc; | |
2629 | ||
2630 | len = strlen(key); | |
2631 | buf[0] = cpu_to_le32(len); | |
2632 | buf[1] = cpu_to_le32(perdatum->value); | |
2633 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
2634 | if (rc) | |
2635 | return rc; | |
2636 | ||
2637 | rc = put_entry(key, 1, len, fp); | |
2638 | if (rc) | |
2639 | return rc; | |
2640 | ||
2641 | return 0; | |
2642 | } | |
2643 | ||
2644 | static int common_write(void *vkey, void *datum, void *ptr) | |
2645 | { | |
2646 | char *key = vkey; | |
2647 | struct common_datum *comdatum = datum; | |
2648 | struct policy_data *pd = ptr; | |
2649 | void *fp = pd->fp; | |
2650 | __le32 buf[4]; | |
2651 | size_t len; | |
2652 | int rc; | |
2653 | ||
2654 | len = strlen(key); | |
2655 | buf[0] = cpu_to_le32(len); | |
2656 | buf[1] = cpu_to_le32(comdatum->value); | |
2657 | buf[2] = cpu_to_le32(comdatum->permissions.nprim); | |
2658 | buf[3] = cpu_to_le32(comdatum->permissions.table->nel); | |
2659 | rc = put_entry(buf, sizeof(u32), 4, fp); | |
2660 | if (rc) | |
2661 | return rc; | |
2662 | ||
2663 | rc = put_entry(key, 1, len, fp); | |
2664 | if (rc) | |
2665 | return rc; | |
2666 | ||
2667 | rc = hashtab_map(comdatum->permissions.table, perm_write, fp); | |
2668 | if (rc) | |
2669 | return rc; | |
2670 | ||
2671 | return 0; | |
2672 | } | |
2673 | ||
2674 | static int write_cons_helper(struct policydb *p, struct constraint_node *node, | |
2675 | void *fp) | |
2676 | { | |
2677 | struct constraint_node *c; | |
2678 | struct constraint_expr *e; | |
2679 | __le32 buf[3]; | |
2680 | u32 nel; | |
2681 | int rc; | |
2682 | ||
2683 | for (c = node; c; c = c->next) { | |
2684 | nel = 0; | |
2685 | for (e = c->expr; e; e = e->next) | |
2686 | nel++; | |
2687 | buf[0] = cpu_to_le32(c->permissions); | |
2688 | buf[1] = cpu_to_le32(nel); | |
2689 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
2690 | if (rc) | |
2691 | return rc; | |
2692 | for (e = c->expr; e; e = e->next) { | |
2693 | buf[0] = cpu_to_le32(e->expr_type); | |
2694 | buf[1] = cpu_to_le32(e->attr); | |
2695 | buf[2] = cpu_to_le32(e->op); | |
2696 | rc = put_entry(buf, sizeof(u32), 3, fp); | |
2697 | if (rc) | |
2698 | return rc; | |
2699 | ||
2700 | switch (e->expr_type) { | |
2701 | case CEXPR_NAMES: | |
2702 | rc = ebitmap_write(&e->names, fp); | |
2703 | if (rc) | |
2704 | return rc; | |
2705 | break; | |
2706 | default: | |
2707 | break; | |
2708 | } | |
2709 | } | |
2710 | } | |
2711 | ||
2712 | return 0; | |
2713 | } | |
2714 | ||
2715 | static int class_write(void *vkey, void *datum, void *ptr) | |
2716 | { | |
2717 | char *key = vkey; | |
2718 | struct class_datum *cladatum = datum; | |
2719 | struct policy_data *pd = ptr; | |
2720 | void *fp = pd->fp; | |
2721 | struct policydb *p = pd->p; | |
2722 | struct constraint_node *c; | |
2723 | __le32 buf[6]; | |
2724 | u32 ncons; | |
2725 | size_t len, len2; | |
2726 | int rc; | |
2727 | ||
2728 | len = strlen(key); | |
2729 | if (cladatum->comkey) | |
2730 | len2 = strlen(cladatum->comkey); | |
2731 | else | |
2732 | len2 = 0; | |
2733 | ||
2734 | ncons = 0; | |
2735 | for (c = cladatum->constraints; c; c = c->next) | |
2736 | ncons++; | |
2737 | ||
2738 | buf[0] = cpu_to_le32(len); | |
2739 | buf[1] = cpu_to_le32(len2); | |
2740 | buf[2] = cpu_to_le32(cladatum->value); | |
2741 | buf[3] = cpu_to_le32(cladatum->permissions.nprim); | |
2742 | if (cladatum->permissions.table) | |
2743 | buf[4] = cpu_to_le32(cladatum->permissions.table->nel); | |
2744 | else | |
2745 | buf[4] = 0; | |
2746 | buf[5] = cpu_to_le32(ncons); | |
2747 | rc = put_entry(buf, sizeof(u32), 6, fp); | |
2748 | if (rc) | |
2749 | return rc; | |
2750 | ||
2751 | rc = put_entry(key, 1, len, fp); | |
2752 | if (rc) | |
2753 | return rc; | |
2754 | ||
2755 | if (cladatum->comkey) { | |
2756 | rc = put_entry(cladatum->comkey, 1, len2, fp); | |
2757 | if (rc) | |
2758 | return rc; | |
2759 | } | |
2760 | ||
2761 | rc = hashtab_map(cladatum->permissions.table, perm_write, fp); | |
2762 | if (rc) | |
2763 | return rc; | |
2764 | ||
2765 | rc = write_cons_helper(p, cladatum->constraints, fp); | |
2766 | if (rc) | |
2767 | return rc; | |
2768 | ||
2769 | /* write out the validatetrans rule */ | |
2770 | ncons = 0; | |
2771 | for (c = cladatum->validatetrans; c; c = c->next) | |
2772 | ncons++; | |
2773 | ||
2774 | buf[0] = cpu_to_le32(ncons); | |
2775 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2776 | if (rc) | |
2777 | return rc; | |
2778 | ||
2779 | rc = write_cons_helper(p, cladatum->validatetrans, fp); | |
2780 | if (rc) | |
2781 | return rc; | |
2782 | ||
2783 | return 0; | |
2784 | } | |
2785 | ||
2786 | static int role_write(void *vkey, void *datum, void *ptr) | |
2787 | { | |
2788 | char *key = vkey; | |
2789 | struct role_datum *role = datum; | |
2790 | struct policy_data *pd = ptr; | |
2791 | void *fp = pd->fp; | |
2792 | struct policydb *p = pd->p; | |
2793 | __le32 buf[3]; | |
2794 | size_t items, len; | |
2795 | int rc; | |
2796 | ||
2797 | len = strlen(key); | |
2798 | items = 0; | |
2799 | buf[items++] = cpu_to_le32(len); | |
2800 | buf[items++] = cpu_to_le32(role->value); | |
2801 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) | |
2802 | buf[items++] = cpu_to_le32(role->bounds); | |
2803 | ||
2804 | BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); | |
2805 | ||
2806 | rc = put_entry(buf, sizeof(u32), items, fp); | |
2807 | if (rc) | |
2808 | return rc; | |
2809 | ||
2810 | rc = put_entry(key, 1, len, fp); | |
2811 | if (rc) | |
2812 | return rc; | |
2813 | ||
2814 | rc = ebitmap_write(&role->dominates, fp); | |
2815 | if (rc) | |
2816 | return rc; | |
2817 | ||
2818 | rc = ebitmap_write(&role->types, fp); | |
2819 | if (rc) | |
2820 | return rc; | |
2821 | ||
2822 | return 0; | |
2823 | } | |
2824 | ||
2825 | static int type_write(void *vkey, void *datum, void *ptr) | |
2826 | { | |
2827 | char *key = vkey; | |
2828 | struct type_datum *typdatum = datum; | |
2829 | struct policy_data *pd = ptr; | |
2830 | struct policydb *p = pd->p; | |
2831 | void *fp = pd->fp; | |
2832 | __le32 buf[4]; | |
2833 | int rc; | |
2834 | size_t items, len; | |
2835 | ||
2836 | len = strlen(key); | |
2837 | items = 0; | |
2838 | buf[items++] = cpu_to_le32(len); | |
2839 | buf[items++] = cpu_to_le32(typdatum->value); | |
2840 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { | |
2841 | u32 properties = 0; | |
2842 | ||
2843 | if (typdatum->primary) | |
2844 | properties |= TYPEDATUM_PROPERTY_PRIMARY; | |
2845 | ||
2846 | if (typdatum->attribute) | |
2847 | properties |= TYPEDATUM_PROPERTY_ATTRIBUTE; | |
2848 | ||
2849 | buf[items++] = cpu_to_le32(properties); | |
2850 | buf[items++] = cpu_to_le32(typdatum->bounds); | |
2851 | } else { | |
2852 | buf[items++] = cpu_to_le32(typdatum->primary); | |
2853 | } | |
2854 | BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); | |
2855 | rc = put_entry(buf, sizeof(u32), items, fp); | |
2856 | if (rc) | |
2857 | return rc; | |
2858 | ||
2859 | rc = put_entry(key, 1, len, fp); | |
2860 | if (rc) | |
2861 | return rc; | |
2862 | ||
2863 | return 0; | |
2864 | } | |
2865 | ||
2866 | static int user_write(void *vkey, void *datum, void *ptr) | |
2867 | { | |
2868 | char *key = vkey; | |
2869 | struct user_datum *usrdatum = datum; | |
2870 | struct policy_data *pd = ptr; | |
2871 | struct policydb *p = pd->p; | |
2872 | void *fp = pd->fp; | |
2873 | __le32 buf[3]; | |
2874 | size_t items, len; | |
2875 | int rc; | |
2876 | ||
2877 | len = strlen(key); | |
2878 | items = 0; | |
2879 | buf[items++] = cpu_to_le32(len); | |
2880 | buf[items++] = cpu_to_le32(usrdatum->value); | |
2881 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) | |
2882 | buf[items++] = cpu_to_le32(usrdatum->bounds); | |
2883 | BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); | |
2884 | rc = put_entry(buf, sizeof(u32), items, fp); | |
2885 | if (rc) | |
2886 | return rc; | |
2887 | ||
2888 | rc = put_entry(key, 1, len, fp); | |
2889 | if (rc) | |
2890 | return rc; | |
2891 | ||
2892 | rc = ebitmap_write(&usrdatum->roles, fp); | |
2893 | if (rc) | |
2894 | return rc; | |
2895 | ||
2896 | rc = mls_write_range_helper(&usrdatum->range, fp); | |
2897 | if (rc) | |
2898 | return rc; | |
2899 | ||
2900 | rc = mls_write_level(&usrdatum->dfltlevel, fp); | |
2901 | if (rc) | |
2902 | return rc; | |
2903 | ||
2904 | return 0; | |
2905 | } | |
2906 | ||
2907 | static int (*write_f[SYM_NUM]) (void *key, void *datum, | |
2908 | void *datap) = | |
2909 | { | |
2910 | common_write, | |
2911 | class_write, | |
2912 | role_write, | |
2913 | type_write, | |
2914 | user_write, | |
2915 | cond_write_bool, | |
2916 | sens_write, | |
2917 | cat_write, | |
2918 | }; | |
2919 | ||
2920 | static int ocontext_write(struct policydb *p, struct policydb_compat_info *info, | |
2921 | void *fp) | |
2922 | { | |
2923 | unsigned int i, j, rc; | |
2924 | size_t nel, len; | |
2925 | __le32 buf[3]; | |
2926 | u32 nodebuf[8]; | |
2927 | struct ocontext *c; | |
2928 | for (i = 0; i < info->ocon_num; i++) { | |
2929 | nel = 0; | |
2930 | for (c = p->ocontexts[i]; c; c = c->next) | |
2931 | nel++; | |
2932 | buf[0] = cpu_to_le32(nel); | |
2933 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2934 | if (rc) | |
2935 | return rc; | |
2936 | for (c = p->ocontexts[i]; c; c = c->next) { | |
2937 | switch (i) { | |
2938 | case OCON_ISID: | |
2939 | buf[0] = cpu_to_le32(c->sid[0]); | |
2940 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2941 | if (rc) | |
2942 | return rc; | |
2943 | rc = context_write(p, &c->context[0], fp); | |
2944 | if (rc) | |
2945 | return rc; | |
2946 | break; | |
2947 | case OCON_FS: | |
2948 | case OCON_NETIF: | |
2949 | len = strlen(c->u.name); | |
2950 | buf[0] = cpu_to_le32(len); | |
2951 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
2952 | if (rc) | |
2953 | return rc; | |
2954 | rc = put_entry(c->u.name, 1, len, fp); | |
2955 | if (rc) | |
2956 | return rc; | |
2957 | rc = context_write(p, &c->context[0], fp); | |
2958 | if (rc) | |
2959 | return rc; | |
2960 | rc = context_write(p, &c->context[1], fp); | |
2961 | if (rc) | |
2962 | return rc; | |
2963 | break; | |
2964 | case OCON_PORT: | |
2965 | buf[0] = cpu_to_le32(c->u.port.protocol); | |
2966 | buf[1] = cpu_to_le32(c->u.port.low_port); | |
2967 | buf[2] = cpu_to_le32(c->u.port.high_port); | |
2968 | rc = put_entry(buf, sizeof(u32), 3, fp); | |
2969 | if (rc) | |
2970 | return rc; | |
2971 | rc = context_write(p, &c->context[0], fp); | |
2972 | if (rc) | |
2973 | return rc; | |
2974 | break; | |
2975 | case OCON_NODE: | |
2976 | nodebuf[0] = c->u.node.addr; /* network order */ | |
2977 | nodebuf[1] = c->u.node.mask; /* network order */ | |
2978 | rc = put_entry(nodebuf, sizeof(u32), 2, fp); | |
2979 | if (rc) | |
2980 | return rc; | |
2981 | rc = context_write(p, &c->context[0], fp); | |
2982 | if (rc) | |
2983 | return rc; | |
2984 | break; | |
2985 | case OCON_FSUSE: | |
2986 | buf[0] = cpu_to_le32(c->v.behavior); | |
2987 | len = strlen(c->u.name); | |
2988 | buf[1] = cpu_to_le32(len); | |
2989 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
2990 | if (rc) | |
2991 | return rc; | |
2992 | rc = put_entry(c->u.name, 1, len, fp); | |
2993 | if (rc) | |
2994 | return rc; | |
2995 | rc = context_write(p, &c->context[0], fp); | |
2996 | if (rc) | |
2997 | return rc; | |
2998 | break; | |
2999 | case OCON_NODE6: | |
3000 | for (j = 0; j < 4; j++) | |
3001 | nodebuf[j] = c->u.node6.addr[j]; /* network order */ | |
3002 | for (j = 0; j < 4; j++) | |
3003 | nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */ | |
3004 | rc = put_entry(nodebuf, sizeof(u32), 8, fp); | |
3005 | if (rc) | |
3006 | return rc; | |
3007 | rc = context_write(p, &c->context[0], fp); | |
3008 | if (rc) | |
3009 | return rc; | |
3010 | break; | |
3011 | } | |
3012 | } | |
3013 | } | |
3014 | return 0; | |
3015 | } | |
3016 | ||
3017 | static int genfs_write(struct policydb *p, void *fp) | |
3018 | { | |
3019 | struct genfs *genfs; | |
3020 | struct ocontext *c; | |
3021 | size_t len; | |
3022 | __le32 buf[1]; | |
3023 | int rc; | |
3024 | ||
3025 | len = 0; | |
3026 | for (genfs = p->genfs; genfs; genfs = genfs->next) | |
3027 | len++; | |
3028 | buf[0] = cpu_to_le32(len); | |
3029 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3030 | if (rc) | |
3031 | return rc; | |
3032 | for (genfs = p->genfs; genfs; genfs = genfs->next) { | |
3033 | len = strlen(genfs->fstype); | |
3034 | buf[0] = cpu_to_le32(len); | |
3035 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3036 | if (rc) | |
3037 | return rc; | |
3038 | rc = put_entry(genfs->fstype, 1, len, fp); | |
3039 | if (rc) | |
3040 | return rc; | |
3041 | len = 0; | |
3042 | for (c = genfs->head; c; c = c->next) | |
3043 | len++; | |
3044 | buf[0] = cpu_to_le32(len); | |
3045 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3046 | if (rc) | |
3047 | return rc; | |
3048 | for (c = genfs->head; c; c = c->next) { | |
3049 | len = strlen(c->u.name); | |
3050 | buf[0] = cpu_to_le32(len); | |
3051 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3052 | if (rc) | |
3053 | return rc; | |
3054 | rc = put_entry(c->u.name, 1, len, fp); | |
3055 | if (rc) | |
3056 | return rc; | |
3057 | buf[0] = cpu_to_le32(c->v.sclass); | |
3058 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3059 | if (rc) | |
3060 | return rc; | |
3061 | rc = context_write(p, &c->context[0], fp); | |
3062 | if (rc) | |
3063 | return rc; | |
3064 | } | |
3065 | } | |
3066 | return 0; | |
3067 | } | |
3068 | ||
3069 | static int range_count(void *key, void *data, void *ptr) | |
3070 | { | |
3071 | int *cnt = ptr; | |
3072 | *cnt = *cnt + 1; | |
3073 | ||
3074 | return 0; | |
3075 | } | |
3076 | ||
3077 | static int range_write_helper(void *key, void *data, void *ptr) | |
3078 | { | |
3079 | __le32 buf[2]; | |
3080 | struct range_trans *rt = key; | |
3081 | struct mls_range *r = data; | |
3082 | struct policy_data *pd = ptr; | |
3083 | void *fp = pd->fp; | |
3084 | struct policydb *p = pd->p; | |
3085 | int rc; | |
3086 | ||
3087 | buf[0] = cpu_to_le32(rt->source_type); | |
3088 | buf[1] = cpu_to_le32(rt->target_type); | |
3089 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
3090 | if (rc) | |
3091 | return rc; | |
3092 | if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { | |
3093 | buf[0] = cpu_to_le32(rt->target_class); | |
3094 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3095 | if (rc) | |
3096 | return rc; | |
3097 | } | |
3098 | rc = mls_write_range_helper(r, fp); | |
3099 | if (rc) | |
3100 | return rc; | |
3101 | ||
3102 | return 0; | |
3103 | } | |
3104 | ||
3105 | static int range_write(struct policydb *p, void *fp) | |
3106 | { | |
3107 | size_t nel; | |
3108 | __le32 buf[1]; | |
3109 | int rc; | |
3110 | struct policy_data pd; | |
3111 | ||
3112 | pd.p = p; | |
3113 | pd.fp = fp; | |
3114 | ||
3115 | /* count the number of entries in the hashtab */ | |
3116 | nel = 0; | |
3117 | rc = hashtab_map(p->range_tr, range_count, &nel); | |
3118 | if (rc) | |
3119 | return rc; | |
3120 | ||
3121 | buf[0] = cpu_to_le32(nel); | |
3122 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3123 | if (rc) | |
3124 | return rc; | |
3125 | ||
3126 | /* actually write all of the entries */ | |
3127 | rc = hashtab_map(p->range_tr, range_write_helper, &pd); | |
3128 | if (rc) | |
3129 | return rc; | |
3130 | ||
3131 | return 0; | |
3132 | } | |
3133 | ||
652bb9b0 EP |
3134 | static int filename_trans_write(struct policydb *p, void *fp) |
3135 | { | |
3136 | struct filename_trans *ft; | |
3137 | u32 len, nel = 0; | |
3138 | __le32 buf[4]; | |
3139 | int rc; | |
3140 | ||
3141 | for (ft = p->filename_trans; ft; ft = ft->next) | |
3142 | nel++; | |
3143 | ||
3144 | buf[0] = cpu_to_le32(nel); | |
3145 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3146 | if (rc) | |
3147 | return rc; | |
3148 | ||
3149 | for (ft = p->filename_trans; ft; ft = ft->next) { | |
3150 | len = strlen(ft->name); | |
3151 | buf[0] = cpu_to_le32(len); | |
3152 | rc = put_entry(buf, sizeof(u32), 1, fp); | |
3153 | if (rc) | |
3154 | return rc; | |
3155 | ||
3156 | rc = put_entry(ft->name, sizeof(char), len, fp); | |
3157 | if (rc) | |
3158 | return rc; | |
3159 | ||
3160 | buf[0] = ft->stype; | |
3161 | buf[1] = ft->ttype; | |
3162 | buf[2] = ft->tclass; | |
3163 | buf[3] = ft->otype; | |
3164 | ||
3165 | rc = put_entry(buf, sizeof(u32), 4, fp); | |
3166 | if (rc) | |
3167 | return rc; | |
3168 | } | |
3169 | return 0; | |
3170 | } | |
cee74f47 EP |
3171 | /* |
3172 | * Write the configuration data in a policy database | |
3173 | * structure to a policy database binary representation | |
3174 | * file. | |
3175 | */ | |
3176 | int policydb_write(struct policydb *p, void *fp) | |
3177 | { | |
3178 | unsigned int i, num_syms; | |
3179 | int rc; | |
3180 | __le32 buf[4]; | |
3181 | u32 config; | |
3182 | size_t len; | |
3183 | struct policydb_compat_info *info; | |
3184 | ||
3185 | /* | |
3186 | * refuse to write policy older than compressed avtab | |
3187 | * to simplify the writer. There are other tests dropped | |
3188 | * since we assume this throughout the writer code. Be | |
3189 | * careful if you ever try to remove this restriction | |
3190 | */ | |
3191 | if (p->policyvers < POLICYDB_VERSION_AVTAB) { | |
3192 | printk(KERN_ERR "SELinux: refusing to write policy version %d." | |
3193 | " Because it is less than version %d\n", p->policyvers, | |
3194 | POLICYDB_VERSION_AVTAB); | |
3195 | return -EINVAL; | |
3196 | } | |
3197 | ||
3198 | config = 0; | |
3199 | if (p->mls_enabled) | |
3200 | config |= POLICYDB_CONFIG_MLS; | |
3201 | ||
3202 | if (p->reject_unknown) | |
3203 | config |= REJECT_UNKNOWN; | |
3204 | if (p->allow_unknown) | |
3205 | config |= ALLOW_UNKNOWN; | |
3206 | ||
3207 | /* Write the magic number and string identifiers. */ | |
3208 | buf[0] = cpu_to_le32(POLICYDB_MAGIC); | |
3209 | len = strlen(POLICYDB_STRING); | |
3210 | buf[1] = cpu_to_le32(len); | |
3211 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
3212 | if (rc) | |
3213 | return rc; | |
3214 | rc = put_entry(POLICYDB_STRING, 1, len, fp); | |
3215 | if (rc) | |
3216 | return rc; | |
3217 | ||
3218 | /* Write the version, config, and table sizes. */ | |
3219 | info = policydb_lookup_compat(p->policyvers); | |
3220 | if (!info) { | |
3221 | printk(KERN_ERR "SELinux: compatibility lookup failed for policy " | |
3222 | "version %d", p->policyvers); | |
9398c7f7 | 3223 | return -EINVAL; |
cee74f47 EP |
3224 | } |
3225 | ||
3226 | buf[0] = cpu_to_le32(p->policyvers); | |
3227 | buf[1] = cpu_to_le32(config); | |
3228 | buf[2] = cpu_to_le32(info->sym_num); | |
3229 | buf[3] = cpu_to_le32(info->ocon_num); | |
3230 | ||
3231 | rc = put_entry(buf, sizeof(u32), 4, fp); | |
3232 | if (rc) | |
3233 | return rc; | |
3234 | ||
3235 | if (p->policyvers >= POLICYDB_VERSION_POLCAP) { | |
3236 | rc = ebitmap_write(&p->policycaps, fp); | |
3237 | if (rc) | |
3238 | return rc; | |
3239 | } | |
3240 | ||
3241 | if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { | |
3242 | rc = ebitmap_write(&p->permissive_map, fp); | |
3243 | if (rc) | |
3244 | return rc; | |
3245 | } | |
3246 | ||
3247 | num_syms = info->sym_num; | |
3248 | for (i = 0; i < num_syms; i++) { | |
3249 | struct policy_data pd; | |
3250 | ||
3251 | pd.fp = fp; | |
3252 | pd.p = p; | |
3253 | ||
3254 | buf[0] = cpu_to_le32(p->symtab[i].nprim); | |
3255 | buf[1] = cpu_to_le32(p->symtab[i].table->nel); | |
3256 | ||
3257 | rc = put_entry(buf, sizeof(u32), 2, fp); | |
3258 | if (rc) | |
3259 | return rc; | |
3260 | rc = hashtab_map(p->symtab[i].table, write_f[i], &pd); | |
3261 | if (rc) | |
3262 | return rc; | |
3263 | } | |
3264 | ||
3265 | rc = avtab_write(p, &p->te_avtab, fp); | |
3266 | if (rc) | |
3267 | return rc; | |
3268 | ||
3269 | rc = cond_write_list(p, p->cond_list, fp); | |
3270 | if (rc) | |
3271 | return rc; | |
3272 | ||
c900ff32 | 3273 | rc = role_trans_write(p, fp); |
cee74f47 EP |
3274 | if (rc) |
3275 | return rc; | |
3276 | ||
3277 | rc = role_allow_write(p->role_allow, fp); | |
3278 | if (rc) | |
3279 | return rc; | |
3280 | ||
652bb9b0 EP |
3281 | rc = filename_trans_write(p, fp); |
3282 | if (rc) | |
3283 | return rc; | |
3284 | ||
cee74f47 EP |
3285 | rc = ocontext_write(p, info, fp); |
3286 | if (rc) | |
3287 | return rc; | |
3288 | ||
3289 | rc = genfs_write(p, fp); | |
3290 | if (rc) | |
3291 | return rc; | |
3292 | ||
3293 | rc = range_write(p, fp); | |
3294 | if (rc) | |
3295 | return rc; | |
3296 | ||
3297 | for (i = 0; i < p->p_types.nprim; i++) { | |
3298 | struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); | |
3299 | ||
3300 | BUG_ON(!e); | |
3301 | rc = ebitmap_write(e, fp); | |
3302 | if (rc) | |
3303 | return rc; | |
3304 | } | |
3305 | ||
3306 | return 0; | |
3307 | } |