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