blob: 159c818067602ef2bcb123fc1ddb143a0eb85742 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Eric Paris2ced3df2008-04-17 13:37:12 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Paul Moore3bb56b22008-01-29 08:38:19 -050016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for the policy capability bitmap
19 *
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 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
Eric Paris2ced3df2008-04-17 13:37:12 -040024 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the Free Software Foundation, version 2.
26 */
27
28#include <linux/kernel.h>
Eric Paris9dc99782007-06-04 17:41:22 -040029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/string.h>
32#include <linux/errno.h>
KaiGai Koheid9250de2008-08-28 16:35:57 +090033#include <linux/audit.h>
Eric Paris6371dcd2010-07-29 23:02:34 -040034#include <linux/flex_array.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "security.h"
36
37#include "policydb.h"
38#include "conditional.h"
39#include "mls.h"
Eric Pariscee74f42010-10-13 17:50:25 -040040#include "services.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define _DEBUG_HASHES
43
44#ifdef DEBUG_HASHES
Stephen Hemminger634a5392010-03-04 21:59:03 -080045static const char *symtab_name[SYM_NUM] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "common prefixes",
47 "classes",
48 "roles",
49 "types",
50 "users",
51 "bools",
52 "levels",
53 "categories",
54};
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int symtab_sizes[SYM_NUM] = {
58 2,
59 32,
60 16,
61 512,
62 128,
63 16,
64 16,
65 16,
66};
67
68struct 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 */
75static struct policydb_compat_info policydb_compat[] = {
76 {
Eric Paris2ced3df2008-04-17 13:37:12 -040077 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 },
81 {
Eric Paris2ced3df2008-04-17 13:37:12 -040082 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 },
86 {
Eric Paris2ced3df2008-04-17 13:37:12 -040087 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 },
91 {
Eric Paris2ced3df2008-04-17 13:37:12 -040092 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 },
96 {
Eric Paris2ced3df2008-04-17 13:37:12 -040097 .version = POLICYDB_VERSION_MLS,
98 .sym_num = SYM_NUM,
99 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 },
Stephen Smalley782ebb92005-09-03 15:55:16 -0700101 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400102 .version = POLICYDB_VERSION_AVTAB,
103 .sym_num = SYM_NUM,
104 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700105 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700106 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400107 .version = POLICYDB_VERSION_RANGETRANS,
108 .sym_num = SYM_NUM,
109 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700110 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500111 {
112 .version = POLICYDB_VERSION_POLCAP,
113 .sym_num = SYM_NUM,
114 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100115 },
116 {
117 .version = POLICYDB_VERSION_PERMISSIVE,
118 .sym_num = SYM_NUM,
119 .ocon_num = OCON_NUM,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900120 },
121 {
122 .version = POLICYDB_VERSION_BOUNDARY,
123 .sym_num = SYM_NUM,
124 .ocon_num = OCON_NUM,
125 },
Eric Paris652bb9b2011-02-01 11:05:40 -0500126 {
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
128 .sym_num = SYM_NUM,
129 .ocon_num = OCON_NUM,
130 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133static struct policydb_compat_info *policydb_lookup_compat(int version)
134{
135 int i;
136 struct policydb_compat_info *info = NULL;
137
Tobias Klauser32725ad2006-01-06 00:11:23 -0800138 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (policydb_compat[i].version == version) {
140 info = &policydb_compat[i];
141 break;
142 }
143 }
144 return info;
145}
146
147/*
148 * Initialize the role table.
149 */
150static int roles_init(struct policydb *p)
151{
152 char *key = NULL;
153 int rc;
154 struct role_datum *role;
155
Eric Paris9398c7f2010-11-23 11:40:08 -0500156 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800157 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500158 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500160
161 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500163 if (role->value != OBJECT_R_VAL)
164 goto out;
165
166 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200167 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500168 if (!key)
169 goto out;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 rc = hashtab_insert(p->p_roles.table, key, role);
172 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Eric Paris9398c7f2010-11-23 11:40:08 -0500175 return 0;
176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500179 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500182static u32 rangetr_hash(struct hashtab *h, const void *k)
183{
184 const struct range_trans *key = k;
185 return (key->source_type + (key->target_type << 3) +
186 (key->target_class << 5)) & (h->size - 1);
187}
188
189static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
190{
191 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400192 int v;
193
194 v = key1->source_type - key2->source_type;
195 if (v)
196 return v;
197
198 v = key1->target_type - key2->target_type;
199 if (v)
200 return v;
201
202 v = key1->target_class - key2->target_class;
203
204 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Initialize a policy database structure.
209 */
210static int policydb_init(struct policydb *p)
211{
212 int i, rc;
213
214 memset(p, 0, sizeof(*p));
215
216 for (i = 0; i < SYM_NUM; i++) {
217 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
218 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
222 rc = avtab_init(&p->te_avtab);
223 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 rc = roles_init(p);
227 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 rc = cond_policydb_init(p);
231 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500234 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
235 if (!p->range_tr)
Eric Paris9398c7f2010-11-23 11:40:08 -0500236 goto out;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500237
Paul Moore3bb56b22008-01-29 08:38:19 -0500238 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100239 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500240
Eric Paris9398c7f2010-11-23 11:40:08 -0500241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 for (i = 0; i < SYM_NUM; i++)
244 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500245 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/*
249 * The following *_index functions are used to
250 * define the val_to_name and val_to_struct arrays
251 * in a policy database structure. The val_to_name
252 * arrays are used when converting security context
253 * structures into string representations. The
254 * val_to_struct arrays are used when the attributes
255 * of a class, role, or user are needed.
256 */
257
258static int common_index(void *key, void *datum, void *datap)
259{
260 struct policydb *p;
261 struct common_datum *comdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500262 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 comdatum = datum;
265 p = datap;
266 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
267 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500268
269 fa = p->sym_val_to_name[SYM_COMMONS];
270 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
271 GFP_KERNEL | __GFP_ZERO))
272 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274}
275
276static int class_index(void *key, void *datum, void *datap)
277{
278 struct policydb *p;
279 struct class_datum *cladatum;
Eric Parisac76c052010-11-29 15:47:09 -0500280 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 cladatum = datum;
283 p = datap;
284 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
285 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500286 fa = p->sym_val_to_name[SYM_CLASSES];
287 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
288 GFP_KERNEL | __GFP_ZERO))
289 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 p->class_val_to_struct[cladatum->value - 1] = cladatum;
291 return 0;
292}
293
294static int role_index(void *key, void *datum, void *datap)
295{
296 struct policydb *p;
297 struct role_datum *role;
Eric Parisac76c052010-11-29 15:47:09 -0500298 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 role = datum;
301 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900302 if (!role->value
303 || role->value > p->p_roles.nprim
304 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500306
307 fa = p->sym_val_to_name[SYM_ROLES];
308 if (flex_array_put_ptr(fa, role->value - 1, key,
309 GFP_KERNEL | __GFP_ZERO))
310 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 p->role_val_to_struct[role->value - 1] = role;
312 return 0;
313}
314
315static int type_index(void *key, void *datum, void *datap)
316{
317 struct policydb *p;
318 struct type_datum *typdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500319 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 typdatum = datum;
322 p = datap;
323
324 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900325 if (!typdatum->value
326 || typdatum->value > p->p_types.nprim
327 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500329 fa = p->sym_val_to_name[SYM_TYPES];
330 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
331 GFP_KERNEL | __GFP_ZERO))
332 BUG();
333
334 fa = p->type_val_to_struct_array;
335 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500336 GFP_KERNEL | __GFP_ZERO))
337 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 return 0;
341}
342
343static int user_index(void *key, void *datum, void *datap)
344{
345 struct policydb *p;
346 struct user_datum *usrdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500347 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 usrdatum = datum;
350 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900351 if (!usrdatum->value
352 || usrdatum->value > p->p_users.nprim
353 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500355
356 fa = p->sym_val_to_name[SYM_USERS];
357 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
358 GFP_KERNEL | __GFP_ZERO))
359 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
361 return 0;
362}
363
364static int sens_index(void *key, void *datum, void *datap)
365{
366 struct policydb *p;
367 struct level_datum *levdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500368 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 levdatum = datum;
371 p = datap;
372
373 if (!levdatum->isalias) {
374 if (!levdatum->level->sens ||
375 levdatum->level->sens > p->p_levels.nprim)
376 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500377 fa = p->sym_val_to_name[SYM_LEVELS];
378 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
379 GFP_KERNEL | __GFP_ZERO))
380 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 return 0;
384}
385
386static int cat_index(void *key, void *datum, void *datap)
387{
388 struct policydb *p;
389 struct cat_datum *catdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500390 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 catdatum = datum;
393 p = datap;
394
395 if (!catdatum->isalias) {
396 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
397 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500398 fa = p->sym_val_to_name[SYM_CATS];
399 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
400 GFP_KERNEL | __GFP_ZERO))
401 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 return 0;
405}
406
407static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
408{
409 common_index,
410 class_index,
411 role_index,
412 type_index,
413 user_index,
414 cond_index_bool,
415 sens_index,
416 cat_index,
417};
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#ifdef DEBUG_HASHES
420static void symtab_hash_eval(struct symtab *s)
421{
422 int i;
423
424 for (i = 0; i < SYM_NUM; i++) {
425 struct hashtab *h = s[i].table;
426 struct hashtab_info info;
427
428 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400429 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 "longest chain length %d\n", symtab_name[i], h->nel,
431 info.slots_used, h->size, info.max_chain_len);
432 }
433}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500434
435static void rangetr_hash_eval(struct hashtab *h)
436{
437 struct hashtab_info info;
438
439 hashtab_stat(h, &info);
440 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
441 "longest chain length %d\n", h->nel,
442 info.slots_used, h->size, info.max_chain_len);
443}
444#else
445static inline void rangetr_hash_eval(struct hashtab *h)
446{
447}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#endif
449
450/*
451 * Define the other val_to_name and val_to_struct arrays
452 * in a policy database structure.
453 *
454 * Caller must clean up on failure.
455 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500456static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Eric Paris9398c7f2010-11-23 11:40:08 -0500458 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
James Morris454d9722008-02-26 20:42:02 +1100460 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf52010-02-03 16:40:20 +0100462 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 printk(", %d sens, %d cats", p->p_levels.nprim,
464 p->p_cats.nprim);
465 printk("\n");
466
James Morris454d9722008-02-26 20:42:02 +1100467 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 p->p_classes.nprim, p->te_avtab.nel);
469
470#ifdef DEBUG_HASHES
471 avtab_hash_eval(&p->te_avtab, "rules");
472 symtab_hash_eval(p->symtab);
473#endif
474
Eric Paris9398c7f2010-11-23 11:40:08 -0500475 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500476 p->class_val_to_struct =
477 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
478 GFP_KERNEL);
479 if (!p->class_val_to_struct)
480 goto out;
481
482 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 p->role_val_to_struct =
484 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400485 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500486 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Eric Paris9398c7f2010-11-23 11:40:08 -0500489 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 p->user_val_to_struct =
491 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400492 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500493 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Paris23bdecb2010-11-29 15:47:09 -0500496 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500497 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500498 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
499 p->p_types.nprim,
500 GFP_KERNEL | __GFP_ZERO);
501 if (!p->type_val_to_struct_array)
502 goto out;
503
504 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
505 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
506 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900507 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900508
Eric Paris9398c7f2010-11-23 11:40:08 -0500509 rc = -ENOMEM;
510 if (cond_init_bool_indexes(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500513 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500514 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500515 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
516 p->symtab[i].nprim,
517 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500518 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500520
521 rc = flex_array_prealloc(p->sym_val_to_name[i],
522 0, p->symtab[i].nprim - 1,
523 GFP_KERNEL | __GFP_ZERO);
524 if (rc)
525 goto out;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
528 if (rc)
529 goto out;
530 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500531 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532out:
533 return rc;
534}
535
536/*
537 * The following *_destroy functions are used to
538 * free any memory allocated for each kind of
539 * symbol data in the policy database.
540 */
541
542static int perm_destroy(void *key, void *datum, void *p)
543{
544 kfree(key);
545 kfree(datum);
546 return 0;
547}
548
549static int common_destroy(void *key, void *datum, void *p)
550{
551 struct common_datum *comdatum;
552
553 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500554 if (datum) {
555 comdatum = datum;
556 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
557 hashtab_destroy(comdatum->permissions.table);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 kfree(datum);
560 return 0;
561}
562
James Morris6cbda6b2006-11-29 16:50:27 -0500563static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct class_datum *cladatum;
566 struct constraint_node *constraint, *ctemp;
567 struct constraint_expr *e, *etmp;
568
569 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500570 if (datum) {
571 cladatum = datum;
572 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
573 hashtab_destroy(cladatum->permissions.table);
574 constraint = cladatum->constraints;
575 while (constraint) {
576 e = constraint->expr;
577 while (e) {
578 ebitmap_destroy(&e->names);
579 etmp = e;
580 e = e->next;
581 kfree(etmp);
582 }
583 ctemp = constraint;
584 constraint = constraint->next;
585 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Eric Paris9398c7f2010-11-23 11:40:08 -0500588 constraint = cladatum->validatetrans;
589 while (constraint) {
590 e = constraint->expr;
591 while (e) {
592 ebitmap_destroy(&e->names);
593 etmp = e;
594 e = e->next;
595 kfree(etmp);
596 }
597 ctemp = constraint;
598 constraint = constraint->next;
599 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric Paris9398c7f2010-11-23 11:40:08 -0500602 kfree(cladatum->comkey);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 kfree(datum);
605 return 0;
606}
607
608static int role_destroy(void *key, void *datum, void *p)
609{
610 struct role_datum *role;
611
612 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500613 if (datum) {
614 role = datum;
615 ebitmap_destroy(&role->dominates);
616 ebitmap_destroy(&role->types);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree(datum);
619 return 0;
620}
621
622static int type_destroy(void *key, void *datum, void *p)
623{
624 kfree(key);
625 kfree(datum);
626 return 0;
627}
628
629static int user_destroy(void *key, void *datum, void *p)
630{
631 struct user_datum *usrdatum;
632
633 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500634 if (datum) {
635 usrdatum = datum;
636 ebitmap_destroy(&usrdatum->roles);
637 ebitmap_destroy(&usrdatum->range.level[0].cat);
638 ebitmap_destroy(&usrdatum->range.level[1].cat);
639 ebitmap_destroy(&usrdatum->dfltlevel.cat);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree(datum);
642 return 0;
643}
644
645static int sens_destroy(void *key, void *datum, void *p)
646{
647 struct level_datum *levdatum;
648
649 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500650 if (datum) {
651 levdatum = datum;
652 ebitmap_destroy(&levdatum->level->cat);
653 kfree(levdatum->level);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 kfree(datum);
656 return 0;
657}
658
659static int cat_destroy(void *key, void *datum, void *p)
660{
661 kfree(key);
662 kfree(datum);
663 return 0;
664}
665
666static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
667{
668 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500669 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 role_destroy,
671 type_destroy,
672 user_destroy,
673 cond_destroy_bool,
674 sens_destroy,
675 cat_destroy,
676};
677
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500678static int range_tr_destroy(void *key, void *datum, void *p)
679{
680 struct mls_range *rt = datum;
681 kfree(key);
682 ebitmap_destroy(&rt->level[0].cat);
683 ebitmap_destroy(&rt->level[1].cat);
684 kfree(datum);
685 cond_resched();
686 return 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void ocontext_destroy(struct ocontext *c, int i)
690{
Eric Parisd1b43542010-07-21 12:50:57 -0400691 if (!c)
692 return;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 context_destroy(&c->context[0]);
695 context_destroy(&c->context[1]);
696 if (i == OCON_ISID || i == OCON_FS ||
697 i == OCON_NETIF || i == OCON_FSUSE)
698 kfree(c->u.name);
699 kfree(c);
700}
701
702/*
703 * Free any memory allocated by a policy database structure.
704 */
705void policydb_destroy(struct policydb *p)
706{
707 struct ocontext *c, *ctmp;
708 struct genfs *g, *gtmp;
709 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700710 struct role_allow *ra, *lra = NULL;
711 struct role_trans *tr, *ltr = NULL;
Eric Paris652bb9b2011-02-01 11:05:40 -0500712 struct filename_trans *ft, *nft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400715 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
717 hashtab_destroy(p->symtab[i].table);
718 }
719
Eric Parisac76c052010-11-29 15:47:09 -0500720 for (i = 0; i < SYM_NUM; i++) {
721 if (p->sym_val_to_name[i])
722 flex_array_free(p->sym_val_to_name[i]);
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700725 kfree(p->class_val_to_struct);
726 kfree(p->role_val_to_struct);
727 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500728 if (p->type_val_to_struct_array)
729 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 avtab_destroy(&p->te_avtab);
732
733 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400734 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 c = p->ocontexts[i];
736 while (c) {
737 ctmp = c;
738 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400739 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400741 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 g = p->genfs;
745 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400746 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 kfree(g->fstype);
748 c = g->head;
749 while (c) {
750 ctmp = c;
751 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400752 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 gtmp = g;
755 g = g->next;
756 kfree(gtmp);
757 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400758 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 cond_policydb_destroy(p);
761
Stephen Smalley782ebb92005-09-03 15:55:16 -0700762 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400763 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800764 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700765 ltr = tr;
766 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800767 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700768
Eric Paris2ced3df2008-04-17 13:37:12 -0400769 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400770 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800771 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700772 lra = ra;
773 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800774 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700775
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500776 hashtab_map(p->range_tr, range_tr_destroy, NULL);
777 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700778
Eric Paris6371dcd2010-07-29 23:02:34 -0400779 if (p->type_attr_map_array) {
780 for (i = 0; i < p->p_types.nprim; i++) {
781 struct ebitmap *e;
782
783 e = flex_array_get(p->type_attr_map_array, i);
784 if (!e)
785 continue;
786 ebitmap_destroy(e);
787 }
788 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700789 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500790
791 ft = p->filename_trans;
792 while (ft) {
793 nft = ft->next;
794 kfree(ft->name);
795 kfree(ft);
796 ft = nft;
797 }
798
Paul Moore3bb56b22008-01-29 08:38:19 -0500799 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100800 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return;
803}
804
805/*
806 * Load the initial SIDs specified in a policy database
807 * structure into a SID table.
808 */
809int policydb_load_isids(struct policydb *p, struct sidtab *s)
810{
811 struct ocontext *head, *c;
812 int rc;
813
814 rc = sidtab_init(s);
815 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100816 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto out;
818 }
819
820 head = p->ocontexts[OCON_ISID];
821 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500822 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500824 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
825 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto out;
827 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500828
829 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
830 if (rc) {
831 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
832 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto out;
834 }
835 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500836 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837out:
838 return rc;
839}
840
Stephen Smalley45e54212007-11-07 10:08:00 -0500841int policydb_class_isvalid(struct policydb *p, unsigned int class)
842{
843 if (!class || class > p->p_classes.nprim)
844 return 0;
845 return 1;
846}
847
848int policydb_role_isvalid(struct policydb *p, unsigned int role)
849{
850 if (!role || role > p->p_roles.nprim)
851 return 0;
852 return 1;
853}
854
855int policydb_type_isvalid(struct policydb *p, unsigned int type)
856{
857 if (!type || type > p->p_types.nprim)
858 return 0;
859 return 1;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
863 * Return 1 if the fields in the security context
864 * structure `c' are valid. Return 0 otherwise.
865 */
866int policydb_context_isvalid(struct policydb *p, struct context *c)
867{
868 struct role_datum *role;
869 struct user_datum *usrdatum;
870
871 if (!c->role || c->role > p->p_roles.nprim)
872 return 0;
873
874 if (!c->user || c->user > p->p_users.nprim)
875 return 0;
876
877 if (!c->type || c->type > p->p_types.nprim)
878 return 0;
879
880 if (c->role != OBJECT_R_VAL) {
881 /*
882 * Role must be authorized for the type.
883 */
884 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500885 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* role may not be associated with type */
887 return 0;
888
889 /*
890 * User must be authorized for the role.
891 */
892 usrdatum = p->user_val_to_struct[c->user - 1];
893 if (!usrdatum)
894 return 0;
895
Eric Paris9398c7f2010-11-23 11:40:08 -0500896 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* user may not be associated with role */
898 return 0;
899 }
900
901 if (!mls_context_isvalid(p, c))
902 return 0;
903
904 return 1;
905}
906
907/*
908 * Read a MLS range structure from a policydb binary
909 * representation file.
910 */
911static int mls_read_range_helper(struct mls_range *r, void *fp)
912{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700913 __le32 buf[2];
914 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int rc;
916
917 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500918 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto out;
920
Eric Paris9398c7f2010-11-23 11:40:08 -0500921 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 items = le32_to_cpu(buf[0]);
923 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100924 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out;
926 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500929 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100930 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto out;
932 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 r->level[0].sens = le32_to_cpu(buf[0]);
935 if (items > 1)
936 r->level[1].sens = le32_to_cpu(buf[1]);
937 else
938 r->level[1].sens = r->level[0].sens;
939
940 rc = ebitmap_read(&r->level[0].cat, fp);
941 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500942 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto out;
944 }
945 if (items > 1) {
946 rc = ebitmap_read(&r->level[1].cat, fp);
947 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500948 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto bad_high;
950 }
951 } else {
952 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
953 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100954 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto bad_high;
956 }
957 }
958
Eric Paris9398c7f2010-11-23 11:40:08 -0500959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960bad_high:
961 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500962out:
963 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
967 * Read and validate a security context structure
968 * from a policydb binary representation file.
969 */
970static int context_read_and_validate(struct context *c,
971 struct policydb *p,
972 void *fp)
973{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700974 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int rc;
976
977 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500978 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100979 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto out;
981 }
982 c->user = le32_to_cpu(buf[0]);
983 c->role = le32_to_cpu(buf[1]);
984 c->type = le32_to_cpu(buf[2]);
985 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500986 rc = mls_read_range_helper(&c->range, fp);
987 if (rc) {
988 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out;
990 }
991 }
992
Eric Paris9398c7f2010-11-23 11:40:08 -0500993 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100995 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -0500997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500999 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000out:
1001 return rc;
1002}
1003
1004/*
1005 * The following *_read functions are used to
1006 * read the symbol data from a policy database
1007 * binary representation file.
1008 */
1009
1010static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1011{
1012 char *key = NULL;
1013 struct perm_datum *perdatum;
1014 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001015 __le32 buf[2];
1016 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Eric Paris9398c7f2010-11-23 11:40:08 -05001018 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001019 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001020 if (!perdatum)
1021 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001024 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto bad;
1026
1027 len = le32_to_cpu(buf[0]);
1028 perdatum->value = le32_to_cpu(buf[1]);
1029
Eric Paris9398c7f2010-11-23 11:40:08 -05001030 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001031 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001032 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001036 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001038 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 rc = hashtab_insert(h, key, perdatum);
1041 if (rc)
1042 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001043
1044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045bad:
1046 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001047 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1051{
1052 char *key = NULL;
1053 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001054 __le32 buf[4];
1055 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int i, rc;
1057
Eric Paris9398c7f2010-11-23 11:40:08 -05001058 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001059 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001060 if (!comdatum)
1061 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001064 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 goto bad;
1066
1067 len = le32_to_cpu(buf[0]);
1068 comdatum->value = le32_to_cpu(buf[1]);
1069
1070 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1071 if (rc)
1072 goto bad;
1073 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1074 nel = le32_to_cpu(buf[3]);
1075
Eric Paris9398c7f2010-11-23 11:40:08 -05001076 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001077 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001078 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001082 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001084 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 for (i = 0; i < nel; i++) {
1087 rc = perm_read(p, comdatum->permissions.table, fp);
1088 if (rc)
1089 goto bad;
1090 }
1091
1092 rc = hashtab_insert(h, key, comdatum);
1093 if (rc)
1094 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096bad:
1097 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001098 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001102 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct constraint_node *c, *lc;
1105 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001106 __le32 buf[3];
1107 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int rc, i, j, depth;
1109
1110 lc = NULL;
1111 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001112 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!c)
1114 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Paris2ced3df2008-04-17 13:37:12 -04001116 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001118 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001122 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return rc;
1124 c->permissions = le32_to_cpu(buf[0]);
1125 nexpr = le32_to_cpu(buf[1]);
1126 le = NULL;
1127 depth = -1;
1128 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001129 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!e)
1131 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Eric Paris2ced3df2008-04-17 13:37:12 -04001133 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001135 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001139 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return rc;
1141 e->expr_type = le32_to_cpu(buf[0]);
1142 e->attr = le32_to_cpu(buf[1]);
1143 e->op = le32_to_cpu(buf[2]);
1144
1145 switch (e->expr_type) {
1146 case CEXPR_NOT:
1147 if (depth < 0)
1148 return -EINVAL;
1149 break;
1150 case CEXPR_AND:
1151 case CEXPR_OR:
1152 if (depth < 1)
1153 return -EINVAL;
1154 depth--;
1155 break;
1156 case CEXPR_ATTR:
1157 if (depth == (CEXPR_MAXDEPTH - 1))
1158 return -EINVAL;
1159 depth++;
1160 break;
1161 case CEXPR_NAMES:
1162 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1163 return -EINVAL;
1164 if (depth == (CEXPR_MAXDEPTH - 1))
1165 return -EINVAL;
1166 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001167 rc = ebitmap_read(&e->names, fp);
1168 if (rc)
1169 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 default:
1172 return -EINVAL;
1173 }
1174 le = e;
1175 }
1176 if (depth != 0)
1177 return -EINVAL;
1178 lc = c;
1179 }
1180
1181 return 0;
1182}
1183
1184static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1185{
1186 char *key = NULL;
1187 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001188 __le32 buf[6];
1189 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int i, rc;
1191
Eric Paris9398c7f2010-11-23 11:40:08 -05001192 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001193 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001194 if (!cladatum)
1195 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001198 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto bad;
1200
1201 len = le32_to_cpu(buf[0]);
1202 len2 = le32_to_cpu(buf[1]);
1203 cladatum->value = le32_to_cpu(buf[2]);
1204
1205 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1206 if (rc)
1207 goto bad;
1208 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1209 nel = le32_to_cpu(buf[4]);
1210
1211 ncons = le32_to_cpu(buf[5]);
1212
Eric Paris9398c7f2010-11-23 11:40:08 -05001213 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001214 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001215 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001219 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001221 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001224 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001225 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001226 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001229 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001231 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Eric Paris9398c7f2010-11-23 11:40:08 -05001233 rc = -EINVAL;
1234 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001236 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto bad;
1238 }
1239 }
1240 for (i = 0; i < nel; i++) {
1241 rc = perm_read(p, cladatum->permissions.table, fp);
1242 if (rc)
1243 goto bad;
1244 }
1245
1246 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1247 if (rc)
1248 goto bad;
1249
1250 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1251 /* grab the validatetrans rules */
1252 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001253 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto bad;
1255 ncons = le32_to_cpu(buf[0]);
1256 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1257 if (rc)
1258 goto bad;
1259 }
1260
1261 rc = hashtab_insert(h, key, cladatum);
1262 if (rc)
1263 goto bad;
1264
Eric Paris9398c7f2010-11-23 11:40:08 -05001265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001267 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001268 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1272{
1273 char *key = NULL;
1274 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001275 int rc, to_read = 2;
1276 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001277 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Eric Paris9398c7f2010-11-23 11:40:08 -05001279 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001280 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001281 if (!role)
1282 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
KaiGai Koheid9250de2008-08-28 16:35:57 +09001284 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1285 to_read = 3;
1286
1287 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001288 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto bad;
1290
1291 len = le32_to_cpu(buf[0]);
1292 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001293 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1294 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Eric Paris9398c7f2010-11-23 11:40:08 -05001296 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001297 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001298 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001302 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001304 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 rc = ebitmap_read(&role->dominates, fp);
1307 if (rc)
1308 goto bad;
1309
1310 rc = ebitmap_read(&role->types, fp);
1311 if (rc)
1312 goto bad;
1313
1314 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001315 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001317 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto bad;
1320 }
1321 rc = 0;
1322 goto bad;
1323 }
1324
1325 rc = hashtab_insert(h, key, role);
1326 if (rc)
1327 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329bad:
1330 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001331 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1335{
1336 char *key = NULL;
1337 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001338 int rc, to_read = 3;
1339 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001340 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Eric Paris9398c7f2010-11-23 11:40:08 -05001342 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001343 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001344 if (!typdatum)
1345 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
KaiGai Koheid9250de2008-08-28 16:35:57 +09001347 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1348 to_read = 4;
1349
1350 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001351 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto bad;
1353
1354 len = le32_to_cpu(buf[0]);
1355 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001356 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1357 u32 prop = le32_to_cpu(buf[2]);
1358
1359 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1360 typdatum->primary = 1;
1361 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1362 typdatum->attribute = 1;
1363
1364 typdatum->bounds = le32_to_cpu(buf[3]);
1365 } else {
1366 typdatum->primary = le32_to_cpu(buf[2]);
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric Paris9398c7f2010-11-23 11:40:08 -05001369 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001370 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001371 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001374 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001376 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 rc = hashtab_insert(h, key, typdatum);
1379 if (rc)
1380 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382bad:
1383 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001384 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387
1388/*
1389 * Read a MLS level structure from a policydb binary
1390 * representation file.
1391 */
1392static int mls_read_level(struct mls_level *lp, void *fp)
1393{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001394 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int rc;
1396
1397 memset(lp, 0, sizeof(*lp));
1398
1399 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001400 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001401 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001402 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 lp->sens = le32_to_cpu(buf[0]);
1405
Eric Paris9398c7f2010-11-23 11:40:08 -05001406 rc = ebitmap_read(&lp->cat, fp);
1407 if (rc) {
1408 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1409 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1415{
1416 char *key = NULL;
1417 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001418 int rc, to_read = 2;
1419 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001420 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Eric Paris9398c7f2010-11-23 11:40:08 -05001422 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001423 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001424 if (!usrdatum)
1425 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
KaiGai Koheid9250de2008-08-28 16:35:57 +09001427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428 to_read = 3;
1429
1430 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001431 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto bad;
1433
1434 len = le32_to_cpu(buf[0]);
1435 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001436 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1437 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Eric Paris9398c7f2010-11-23 11:40:08 -05001439 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001440 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001441 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001444 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001446 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 rc = ebitmap_read(&usrdatum->roles, fp);
1449 if (rc)
1450 goto bad;
1451
1452 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1453 rc = mls_read_range_helper(&usrdatum->range, fp);
1454 if (rc)
1455 goto bad;
1456 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1457 if (rc)
1458 goto bad;
1459 }
1460
1461 rc = hashtab_insert(h, key, usrdatum);
1462 if (rc)
1463 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465bad:
1466 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001467 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1471{
1472 char *key = NULL;
1473 struct level_datum *levdatum;
1474 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001475 __le32 buf[2];
1476 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Eric Paris9398c7f2010-11-23 11:40:08 -05001478 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001479 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001480 if (!levdatum)
1481 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001484 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto bad;
1486
1487 len = le32_to_cpu(buf[0]);
1488 levdatum->isalias = le32_to_cpu(buf[1]);
1489
Eric Paris9398c7f2010-11-23 11:40:08 -05001490 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001491 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001492 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001495 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001497 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Eric Paris9398c7f2010-11-23 11:40:08 -05001499 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001501 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001503
1504 rc = mls_read_level(levdatum->level, fp);
1505 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 rc = hashtab_insert(h, key, levdatum);
1509 if (rc)
1510 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512bad:
1513 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001514 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1518{
1519 char *key = NULL;
1520 struct cat_datum *catdatum;
1521 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001522 __le32 buf[3];
1523 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Eric Paris9398c7f2010-11-23 11:40:08 -05001525 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001526 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001527 if (!catdatum)
1528 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001531 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto bad;
1533
1534 len = le32_to_cpu(buf[0]);
1535 catdatum->value = le32_to_cpu(buf[1]);
1536 catdatum->isalias = le32_to_cpu(buf[2]);
1537
Eric Paris9398c7f2010-11-23 11:40:08 -05001538 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001539 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001540 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001543 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001545 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 rc = hashtab_insert(h, key, catdatum);
1548 if (rc)
1549 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551bad:
1552 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001553 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1557{
1558 common_read,
1559 class_read,
1560 role_read,
1561 type_read,
1562 user_read,
1563 cond_read_bool,
1564 sens_read,
1565 cat_read,
1566};
1567
KaiGai Koheid9250de2008-08-28 16:35:57 +09001568static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1569{
1570 struct user_datum *upper, *user;
1571 struct policydb *p = datap;
1572 int depth = 0;
1573
1574 upper = user = datum;
1575 while (upper->bounds) {
1576 struct ebitmap_node *node;
1577 unsigned long bit;
1578
1579 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1580 printk(KERN_ERR "SELinux: user %s: "
1581 "too deep or looped boundary",
1582 (char *) key);
1583 return -EINVAL;
1584 }
1585
1586 upper = p->user_val_to_struct[upper->bounds - 1];
1587 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1588 if (ebitmap_get_bit(&upper->roles, bit))
1589 continue;
1590
1591 printk(KERN_ERR
1592 "SELinux: boundary violated policy: "
1593 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001594 sym_name(p, SYM_USERS, user->value - 1),
1595 sym_name(p, SYM_ROLES, bit),
1596 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001597
1598 return -EINVAL;
1599 }
1600 }
1601
1602 return 0;
1603}
1604
1605static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1606{
1607 struct role_datum *upper, *role;
1608 struct policydb *p = datap;
1609 int depth = 0;
1610
1611 upper = role = datum;
1612 while (upper->bounds) {
1613 struct ebitmap_node *node;
1614 unsigned long bit;
1615
1616 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1617 printk(KERN_ERR "SELinux: role %s: "
1618 "too deep or looped bounds\n",
1619 (char *) key);
1620 return -EINVAL;
1621 }
1622
1623 upper = p->role_val_to_struct[upper->bounds - 1];
1624 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1625 if (ebitmap_get_bit(&upper->types, bit))
1626 continue;
1627
1628 printk(KERN_ERR
1629 "SELinux: boundary violated policy: "
1630 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001631 sym_name(p, SYM_ROLES, role->value - 1),
1632 sym_name(p, SYM_TYPES, bit),
1633 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001634
1635 return -EINVAL;
1636 }
1637 }
1638
1639 return 0;
1640}
1641
1642static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1643{
Eric Parisdaa6d832010-08-03 15:26:05 -04001644 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001645 struct policydb *p = datap;
1646 int depth = 0;
1647
Eric Parisdaa6d832010-08-03 15:26:05 -04001648 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001649 while (upper->bounds) {
1650 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1651 printk(KERN_ERR "SELinux: type %s: "
1652 "too deep or looped boundary\n",
1653 (char *) key);
1654 return -EINVAL;
1655 }
1656
Eric Paris23bdecb2010-11-29 15:47:09 -05001657 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1658 upper->bounds - 1);
1659 BUG_ON(!upper);
1660
KaiGai Koheid9250de2008-08-28 16:35:57 +09001661 if (upper->attribute) {
1662 printk(KERN_ERR "SELinux: type %s: "
1663 "bounded by attribute %s",
1664 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001665 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001666 return -EINVAL;
1667 }
1668 }
1669
1670 return 0;
1671}
1672
1673static int policydb_bounds_sanity_check(struct policydb *p)
1674{
1675 int rc;
1676
1677 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1678 return 0;
1679
1680 rc = hashtab_map(p->p_users.table,
1681 user_bounds_sanity_check, p);
1682 if (rc)
1683 return rc;
1684
1685 rc = hashtab_map(p->p_roles.table,
1686 role_bounds_sanity_check, p);
1687 if (rc)
1688 return rc;
1689
1690 rc = hashtab_map(p->p_types.table,
1691 type_bounds_sanity_check, p);
1692 if (rc)
1693 return rc;
1694
1695 return 0;
1696}
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698extern int ss_initialized;
1699
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001700u16 string_to_security_class(struct policydb *p, const char *name)
1701{
1702 struct class_datum *cladatum;
1703
1704 cladatum = hashtab_search(p->p_classes.table, name);
1705 if (!cladatum)
1706 return 0;
1707
1708 return cladatum->value;
1709}
1710
1711u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1712{
1713 struct class_datum *cladatum;
1714 struct perm_datum *perdatum = NULL;
1715 struct common_datum *comdatum;
1716
1717 if (!tclass || tclass > p->p_classes.nprim)
1718 return 0;
1719
1720 cladatum = p->class_val_to_struct[tclass-1];
1721 comdatum = cladatum->comdatum;
1722 if (comdatum)
1723 perdatum = hashtab_search(comdatum->permissions.table,
1724 name);
1725 if (!perdatum)
1726 perdatum = hashtab_search(cladatum->permissions.table,
1727 name);
1728 if (!perdatum)
1729 return 0;
1730
1731 return 1U << (perdatum->value-1);
1732}
1733
Eric Paris9ee0c822010-06-11 12:37:05 -04001734static int range_read(struct policydb *p, void *fp)
1735{
1736 struct range_trans *rt = NULL;
1737 struct mls_range *r = NULL;
1738 int i, rc;
1739 __le32 buf[2];
1740 u32 nel;
1741
1742 if (p->policyvers < POLICYDB_VERSION_MLS)
1743 return 0;
1744
1745 rc = next_entry(buf, fp, sizeof(u32));
1746 if (rc)
1747 goto out;
1748
1749 nel = le32_to_cpu(buf[0]);
1750 for (i = 0; i < nel; i++) {
1751 rc = -ENOMEM;
1752 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1753 if (!rt)
1754 goto out;
1755
1756 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1757 if (rc)
1758 goto out;
1759
1760 rt->source_type = le32_to_cpu(buf[0]);
1761 rt->target_type = le32_to_cpu(buf[1]);
1762 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1763 rc = next_entry(buf, fp, sizeof(u32));
1764 if (rc)
1765 goto out;
1766 rt->target_class = le32_to_cpu(buf[0]);
1767 } else
1768 rt->target_class = p->process_class;
1769
1770 rc = -EINVAL;
1771 if (!policydb_type_isvalid(p, rt->source_type) ||
1772 !policydb_type_isvalid(p, rt->target_type) ||
1773 !policydb_class_isvalid(p, rt->target_class))
1774 goto out;
1775
1776 rc = -ENOMEM;
1777 r = kzalloc(sizeof(*r), GFP_KERNEL);
1778 if (!r)
1779 goto out;
1780
1781 rc = mls_read_range_helper(r, fp);
1782 if (rc)
1783 goto out;
1784
1785 rc = -EINVAL;
1786 if (!mls_range_isvalid(p, r)) {
1787 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1788 goto out;
1789 }
1790
1791 rc = hashtab_insert(p->range_tr, rt, r);
1792 if (rc)
1793 goto out;
1794
1795 rt = NULL;
1796 r = NULL;
1797 }
1798 rangetr_hash_eval(p->range_tr);
1799 rc = 0;
1800out:
1801 kfree(rt);
1802 kfree(r);
1803 return rc;
1804}
1805
Eric Paris652bb9b2011-02-01 11:05:40 -05001806static int filename_trans_read(struct policydb *p, void *fp)
1807{
1808 struct filename_trans *ft, *last;
1809 u32 nel, len;
1810 char *name;
1811 __le32 buf[4];
1812 int rc, i;
1813
1814 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1815 return 0;
1816
1817 rc = next_entry(buf, fp, sizeof(u32));
1818 if (rc)
1819 goto out;
1820 nel = le32_to_cpu(buf[0]);
1821
1822 printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1823
1824 last = p->filename_trans;
1825 while (last && last->next)
1826 last = last->next;
1827
1828 for (i = 0; i < nel; i++) {
1829 rc = -ENOMEM;
1830 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1831 if (!ft)
1832 goto out;
1833
1834 /* add it to the tail of the list */
1835 if (!last)
1836 p->filename_trans = ft;
1837 else
1838 last->next = ft;
1839 last = ft;
1840
1841 /* length of the path component string */
1842 rc = next_entry(buf, fp, sizeof(u32));
1843 if (rc)
1844 goto out;
1845 len = le32_to_cpu(buf[0]);
1846
1847 rc = -ENOMEM;
1848 name = kmalloc(len + 1, GFP_KERNEL);
1849 if (!name)
1850 goto out;
1851
1852 ft->name = name;
1853
1854 /* path component string */
1855 rc = next_entry(name, fp, len);
1856 if (rc)
1857 goto out;
1858 name[len] = 0;
1859
1860 printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1861
1862 rc = next_entry(buf, fp, sizeof(u32) * 4);
1863 if (rc)
1864 goto out;
1865
1866 ft->stype = le32_to_cpu(buf[0]);
1867 ft->ttype = le32_to_cpu(buf[1]);
1868 ft->tclass = le32_to_cpu(buf[2]);
1869 ft->otype = le32_to_cpu(buf[3]);
1870 }
1871 rc = 0;
1872out:
1873 return rc;
1874}
1875
Eric Parisd1b43542010-07-21 12:50:57 -04001876static int genfs_read(struct policydb *p, void *fp)
1877{
1878 int i, j, rc;
1879 u32 nel, nel2, len, len2;
1880 __le32 buf[1];
1881 struct ocontext *l, *c;
1882 struct ocontext *newc = NULL;
1883 struct genfs *genfs_p, *genfs;
1884 struct genfs *newgenfs = NULL;
1885
1886 rc = next_entry(buf, fp, sizeof(u32));
1887 if (rc)
1888 goto out;
1889 nel = le32_to_cpu(buf[0]);
1890
1891 for (i = 0; i < nel; i++) {
1892 rc = next_entry(buf, fp, sizeof(u32));
1893 if (rc)
1894 goto out;
1895 len = le32_to_cpu(buf[0]);
1896
1897 rc = -ENOMEM;
1898 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1899 if (!newgenfs)
1900 goto out;
1901
1902 rc = -ENOMEM;
1903 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1904 if (!newgenfs->fstype)
1905 goto out;
1906
1907 rc = next_entry(newgenfs->fstype, fp, len);
1908 if (rc)
1909 goto out;
1910
1911 newgenfs->fstype[len] = 0;
1912
1913 for (genfs_p = NULL, genfs = p->genfs; genfs;
1914 genfs_p = genfs, genfs = genfs->next) {
1915 rc = -EINVAL;
1916 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1917 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1918 newgenfs->fstype);
1919 goto out;
1920 }
1921 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1922 break;
1923 }
1924 newgenfs->next = genfs;
1925 if (genfs_p)
1926 genfs_p->next = newgenfs;
1927 else
1928 p->genfs = newgenfs;
1929 genfs = newgenfs;
1930 newgenfs = NULL;
1931
1932 rc = next_entry(buf, fp, sizeof(u32));
1933 if (rc)
1934 goto out;
1935
1936 nel2 = le32_to_cpu(buf[0]);
1937 for (j = 0; j < nel2; j++) {
1938 rc = next_entry(buf, fp, sizeof(u32));
1939 if (rc)
1940 goto out;
1941 len = le32_to_cpu(buf[0]);
1942
1943 rc = -ENOMEM;
1944 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1945 if (!newc)
1946 goto out;
1947
1948 rc = -ENOMEM;
1949 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1950 if (!newc->u.name)
1951 goto out;
1952
1953 rc = next_entry(newc->u.name, fp, len);
1954 if (rc)
1955 goto out;
1956 newc->u.name[len] = 0;
1957
1958 rc = next_entry(buf, fp, sizeof(u32));
1959 if (rc)
1960 goto out;
1961
1962 newc->v.sclass = le32_to_cpu(buf[0]);
1963 rc = context_read_and_validate(&newc->context[0], p, fp);
1964 if (rc)
1965 goto out;
1966
1967 for (l = NULL, c = genfs->head; c;
1968 l = c, c = c->next) {
1969 rc = -EINVAL;
1970 if (!strcmp(newc->u.name, c->u.name) &&
1971 (!c->v.sclass || !newc->v.sclass ||
1972 newc->v.sclass == c->v.sclass)) {
1973 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1974 genfs->fstype, c->u.name);
1975 goto out;
1976 }
1977 len = strlen(newc->u.name);
1978 len2 = strlen(c->u.name);
1979 if (len > len2)
1980 break;
1981 }
1982
1983 newc->next = c;
1984 if (l)
1985 l->next = newc;
1986 else
1987 genfs->head = newc;
1988 newc = NULL;
1989 }
1990 }
1991 rc = 0;
1992out:
1993 if (newgenfs)
1994 kfree(newgenfs->fstype);
1995 kfree(newgenfs);
1996 ocontext_destroy(newc, OCON_FSUSE);
1997
1998 return rc;
1999}
2000
Eric Paris692a8a22010-07-21 12:51:03 -04002001static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2002 void *fp)
2003{
2004 int i, j, rc;
2005 u32 nel, len;
2006 __le32 buf[3];
2007 struct ocontext *l, *c;
2008 u32 nodebuf[8];
2009
2010 for (i = 0; i < info->ocon_num; i++) {
2011 rc = next_entry(buf, fp, sizeof(u32));
2012 if (rc)
2013 goto out;
2014 nel = le32_to_cpu(buf[0]);
2015
2016 l = NULL;
2017 for (j = 0; j < nel; j++) {
2018 rc = -ENOMEM;
2019 c = kzalloc(sizeof(*c), GFP_KERNEL);
2020 if (!c)
2021 goto out;
2022 if (l)
2023 l->next = c;
2024 else
2025 p->ocontexts[i] = c;
2026 l = c;
2027
2028 switch (i) {
2029 case OCON_ISID:
2030 rc = next_entry(buf, fp, sizeof(u32));
2031 if (rc)
2032 goto out;
2033
2034 c->sid[0] = le32_to_cpu(buf[0]);
2035 rc = context_read_and_validate(&c->context[0], p, fp);
2036 if (rc)
2037 goto out;
2038 break;
2039 case OCON_FS:
2040 case OCON_NETIF:
2041 rc = next_entry(buf, fp, sizeof(u32));
2042 if (rc)
2043 goto out;
2044 len = le32_to_cpu(buf[0]);
2045
2046 rc = -ENOMEM;
2047 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2048 if (!c->u.name)
2049 goto out;
2050
2051 rc = next_entry(c->u.name, fp, len);
2052 if (rc)
2053 goto out;
2054
2055 c->u.name[len] = 0;
2056 rc = context_read_and_validate(&c->context[0], p, fp);
2057 if (rc)
2058 goto out;
2059 rc = context_read_and_validate(&c->context[1], p, fp);
2060 if (rc)
2061 goto out;
2062 break;
2063 case OCON_PORT:
2064 rc = next_entry(buf, fp, sizeof(u32)*3);
2065 if (rc)
2066 goto out;
2067 c->u.port.protocol = le32_to_cpu(buf[0]);
2068 c->u.port.low_port = le32_to_cpu(buf[1]);
2069 c->u.port.high_port = le32_to_cpu(buf[2]);
2070 rc = context_read_and_validate(&c->context[0], p, fp);
2071 if (rc)
2072 goto out;
2073 break;
2074 case OCON_NODE:
2075 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2076 if (rc)
2077 goto out;
2078 c->u.node.addr = nodebuf[0]; /* network order */
2079 c->u.node.mask = nodebuf[1]; /* network order */
2080 rc = context_read_and_validate(&c->context[0], p, fp);
2081 if (rc)
2082 goto out;
2083 break;
2084 case OCON_FSUSE:
2085 rc = next_entry(buf, fp, sizeof(u32)*2);
2086 if (rc)
2087 goto out;
2088
2089 rc = -EINVAL;
2090 c->v.behavior = le32_to_cpu(buf[0]);
2091 if (c->v.behavior > SECURITY_FS_USE_NONE)
2092 goto out;
2093
2094 rc = -ENOMEM;
2095 len = le32_to_cpu(buf[1]);
2096 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2097 if (!c->u.name)
2098 goto out;
2099
2100 rc = next_entry(c->u.name, fp, len);
2101 if (rc)
2102 goto out;
2103 c->u.name[len] = 0;
2104 rc = context_read_and_validate(&c->context[0], p, fp);
2105 if (rc)
2106 goto out;
2107 break;
2108 case OCON_NODE6: {
2109 int k;
2110
2111 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2112 if (rc)
2113 goto out;
2114 for (k = 0; k < 4; k++)
2115 c->u.node6.addr[k] = nodebuf[k];
2116 for (k = 0; k < 4; k++)
2117 c->u.node6.mask[k] = nodebuf[k+4];
2118 rc = context_read_and_validate(&c->context[0], p, fp);
2119 if (rc)
2120 goto out;
2121 break;
2122 }
2123 }
2124 }
2125 }
2126 rc = 0;
2127out:
2128 return rc;
2129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
2132 * Read the configuration data from a policy database binary
2133 * representation file into a policy database structure.
2134 */
2135int policydb_read(struct policydb *p, void *fp)
2136{
2137 struct role_allow *ra, *lra;
2138 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002140 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002141 u32 len, nprim, nel;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 char *policydb_str;
2144 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 rc = policydb_init(p);
2147 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002148 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002151 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002152 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 goto bad;
2154
Eric Paris9398c7f2010-11-23 11:40:08 -05002155 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002156 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002157 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002159 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto bad;
2161 }
2162
Eric Paris9398c7f2010-11-23 11:40:08 -05002163 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002164 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002166 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 "match expected length %Zu\n",
2168 len, strlen(POLICYDB_STRING));
2169 goto bad;
2170 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002171
2172 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002173 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002175 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto bad;
2178 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002181 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002182 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 kfree(policydb_str);
2184 goto bad;
2185 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002186
2187 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002188 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002190 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 "my string %s\n", policydb_str, POLICYDB_STRING);
2192 kfree(policydb_str);
2193 goto bad;
2194 }
2195 /* Done with policydb_str. */
2196 kfree(policydb_str);
2197 policydb_str = NULL;
2198
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01002199 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002201 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Eric Paris9398c7f2010-11-23 11:40:08 -05002204 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002205 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (p->policyvers < POLICYDB_VERSION_MIN ||
2207 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002208 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002209 "my version range %d-%d\n",
2210 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2211 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002214 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01002215 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Eric Paris9398c7f2010-11-23 11:40:08 -05002217 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002219 printk(KERN_ERR "SELinux: security policydb version %d "
2220 "(MLS) not backwards compatible\n",
2221 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto bad;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Eric Paris3f120702007-09-21 14:37:10 -04002225 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2226 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Eric Paris9398c7f2010-11-23 11:40:08 -05002228 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2229 rc = ebitmap_read(&p->policycaps, fp);
2230 if (rc)
2231 goto bad;
2232 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002233
Eric Paris9398c7f2010-11-23 11:40:08 -05002234 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2235 rc = ebitmap_read(&p->permissive_map, fp);
2236 if (rc)
2237 goto bad;
2238 }
Eric Paris64dbf072008-03-31 12:17:33 +11002239
Eric Paris9398c7f2010-11-23 11:40:08 -05002240 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 info = policydb_lookup_compat(p->policyvers);
2242 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002243 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 "for version %d\n", p->policyvers);
2245 goto bad;
2246 }
2247
Eric Paris9398c7f2010-11-23 11:40:08 -05002248 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002249 if (le32_to_cpu(buf[2]) != info->sym_num ||
2250 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002251 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002252 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2253 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 info->sym_num, info->ocon_num);
2255 goto bad;
2256 }
2257
2258 for (i = 0; i < info->sym_num; i++) {
2259 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002260 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto bad;
2262 nprim = le32_to_cpu(buf[0]);
2263 nel = le32_to_cpu(buf[1]);
2264 for (j = 0; j < nel; j++) {
2265 rc = read_f[i](p, p->symtab[i].table, fp);
2266 if (rc)
2267 goto bad;
2268 }
2269
2270 p->symtab[i].nprim = nprim;
2271 }
2272
Stephen Smalley45e54212007-11-07 10:08:00 -05002273 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (rc)
2275 goto bad;
2276
2277 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2278 rc = cond_read_list(p, fp);
2279 if (rc)
2280 goto bad;
2281 }
2282
2283 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002284 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto bad;
2286 nel = le32_to_cpu(buf[0]);
2287 ltr = NULL;
2288 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002289 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002290 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002291 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002293 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002295 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002298 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002300
2301 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 tr->role = le32_to_cpu(buf[0]);
2303 tr->type = le32_to_cpu(buf[1]);
2304 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002305 if (!policydb_role_isvalid(p, tr->role) ||
2306 !policydb_type_isvalid(p, tr->type) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002307 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002308 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 ltr = tr;
2310 }
2311
2312 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002313 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 goto bad;
2315 nel = le32_to_cpu(buf[0]);
2316 lra = NULL;
2317 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002318 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002319 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002320 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002322 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002324 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002327 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002329
2330 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 ra->role = le32_to_cpu(buf[0]);
2332 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002333 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002334 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002335 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 lra = ra;
2337 }
2338
Eric Paris652bb9b2011-02-01 11:05:40 -05002339 rc = filename_trans_read(p, fp);
2340 if (rc)
2341 goto bad;
2342
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002343 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (rc)
2345 goto bad;
2346
Eric Paris9398c7f2010-11-23 11:40:08 -05002347 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002348 p->process_class = string_to_security_class(p, "process");
2349 if (!p->process_class)
2350 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002351
2352 rc = -EINVAL;
2353 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2354 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002355 if (!p->process_trans_perms)
2356 goto bad;
2357
Eric Paris692a8a22010-07-21 12:51:03 -04002358 rc = ocontext_read(p, info, fp);
2359 if (rc)
2360 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Eric Parisd1b43542010-07-21 12:50:57 -04002362 rc = genfs_read(p, fp);
2363 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Eric Paris9ee0c822010-06-11 12:37:05 -04002366 rc = range_read(p, fp);
2367 if (rc)
2368 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Eric Paris6371dcd2010-07-29 23:02:34 -04002370 rc = -ENOMEM;
2371 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2372 p->p_types.nprim,
2373 GFP_KERNEL | __GFP_ZERO);
2374 if (!p->type_attr_map_array)
2375 goto bad;
2376
2377 /* preallocate so we don't have to worry about the put ever failing */
2378 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2379 GFP_KERNEL | __GFP_ZERO);
2380 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002381 goto bad;
2382
2383 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002384 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2385
2386 BUG_ON(!e);
2387 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002388 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002389 rc = ebitmap_read(e, fp);
2390 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002391 goto bad;
2392 }
2393 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002394 rc = ebitmap_set_bit(e, i, 1);
2395 if (rc)
2396 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002397 }
2398
KaiGai Koheid9250de2008-08-28 16:35:57 +09002399 rc = policydb_bounds_sanity_check(p);
2400 if (rc)
2401 goto bad;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 rc = 0;
2404out:
2405 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 policydb_destroy(p);
2408 goto out;
2409}
Eric Pariscee74f42010-10-13 17:50:25 -04002410
2411/*
2412 * Write a MLS level structure to a policydb binary
2413 * representation file.
2414 */
2415static int mls_write_level(struct mls_level *l, void *fp)
2416{
2417 __le32 buf[1];
2418 int rc;
2419
2420 buf[0] = cpu_to_le32(l->sens);
2421 rc = put_entry(buf, sizeof(u32), 1, fp);
2422 if (rc)
2423 return rc;
2424
2425 rc = ebitmap_write(&l->cat, fp);
2426 if (rc)
2427 return rc;
2428
2429 return 0;
2430}
2431
2432/*
2433 * Write a MLS range structure to a policydb binary
2434 * representation file.
2435 */
2436static int mls_write_range_helper(struct mls_range *r, void *fp)
2437{
2438 __le32 buf[3];
2439 size_t items;
2440 int rc, eq;
2441
2442 eq = mls_level_eq(&r->level[1], &r->level[0]);
2443
2444 if (eq)
2445 items = 2;
2446 else
2447 items = 3;
2448 buf[0] = cpu_to_le32(items-1);
2449 buf[1] = cpu_to_le32(r->level[0].sens);
2450 if (!eq)
2451 buf[2] = cpu_to_le32(r->level[1].sens);
2452
2453 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2454
2455 rc = put_entry(buf, sizeof(u32), items, fp);
2456 if (rc)
2457 return rc;
2458
2459 rc = ebitmap_write(&r->level[0].cat, fp);
2460 if (rc)
2461 return rc;
2462 if (!eq) {
2463 rc = ebitmap_write(&r->level[1].cat, fp);
2464 if (rc)
2465 return rc;
2466 }
2467
2468 return 0;
2469}
2470
2471static int sens_write(void *vkey, void *datum, void *ptr)
2472{
2473 char *key = vkey;
2474 struct level_datum *levdatum = datum;
2475 struct policy_data *pd = ptr;
2476 void *fp = pd->fp;
2477 __le32 buf[2];
2478 size_t len;
2479 int rc;
2480
2481 len = strlen(key);
2482 buf[0] = cpu_to_le32(len);
2483 buf[1] = cpu_to_le32(levdatum->isalias);
2484 rc = put_entry(buf, sizeof(u32), 2, fp);
2485 if (rc)
2486 return rc;
2487
2488 rc = put_entry(key, 1, len, fp);
2489 if (rc)
2490 return rc;
2491
2492 rc = mls_write_level(levdatum->level, fp);
2493 if (rc)
2494 return rc;
2495
2496 return 0;
2497}
2498
2499static int cat_write(void *vkey, void *datum, void *ptr)
2500{
2501 char *key = vkey;
2502 struct cat_datum *catdatum = datum;
2503 struct policy_data *pd = ptr;
2504 void *fp = pd->fp;
2505 __le32 buf[3];
2506 size_t len;
2507 int rc;
2508
2509 len = strlen(key);
2510 buf[0] = cpu_to_le32(len);
2511 buf[1] = cpu_to_le32(catdatum->value);
2512 buf[2] = cpu_to_le32(catdatum->isalias);
2513 rc = put_entry(buf, sizeof(u32), 3, fp);
2514 if (rc)
2515 return rc;
2516
2517 rc = put_entry(key, 1, len, fp);
2518 if (rc)
2519 return rc;
2520
2521 return 0;
2522}
2523
2524static int role_trans_write(struct role_trans *r, void *fp)
2525{
2526 struct role_trans *tr;
2527 u32 buf[3];
2528 size_t nel;
2529 int rc;
2530
2531 nel = 0;
2532 for (tr = r; tr; tr = tr->next)
2533 nel++;
2534 buf[0] = cpu_to_le32(nel);
2535 rc = put_entry(buf, sizeof(u32), 1, fp);
2536 if (rc)
2537 return rc;
2538 for (tr = r; tr; tr = tr->next) {
2539 buf[0] = cpu_to_le32(tr->role);
2540 buf[1] = cpu_to_le32(tr->type);
2541 buf[2] = cpu_to_le32(tr->new_role);
2542 rc = put_entry(buf, sizeof(u32), 3, fp);
2543 if (rc)
2544 return rc;
2545 }
2546
2547 return 0;
2548}
2549
2550static int role_allow_write(struct role_allow *r, void *fp)
2551{
2552 struct role_allow *ra;
2553 u32 buf[2];
2554 size_t nel;
2555 int rc;
2556
2557 nel = 0;
2558 for (ra = r; ra; ra = ra->next)
2559 nel++;
2560 buf[0] = cpu_to_le32(nel);
2561 rc = put_entry(buf, sizeof(u32), 1, fp);
2562 if (rc)
2563 return rc;
2564 for (ra = r; ra; ra = ra->next) {
2565 buf[0] = cpu_to_le32(ra->role);
2566 buf[1] = cpu_to_le32(ra->new_role);
2567 rc = put_entry(buf, sizeof(u32), 2, fp);
2568 if (rc)
2569 return rc;
2570 }
2571 return 0;
2572}
2573
2574/*
2575 * Write a security context structure
2576 * to a policydb binary representation file.
2577 */
2578static int context_write(struct policydb *p, struct context *c,
2579 void *fp)
2580{
2581 int rc;
2582 __le32 buf[3];
2583
2584 buf[0] = cpu_to_le32(c->user);
2585 buf[1] = cpu_to_le32(c->role);
2586 buf[2] = cpu_to_le32(c->type);
2587
2588 rc = put_entry(buf, sizeof(u32), 3, fp);
2589 if (rc)
2590 return rc;
2591
2592 rc = mls_write_range_helper(&c->range, fp);
2593 if (rc)
2594 return rc;
2595
2596 return 0;
2597}
2598
2599/*
2600 * The following *_write functions are used to
2601 * write the symbol data to a policy database
2602 * binary representation file.
2603 */
2604
2605static int perm_write(void *vkey, void *datum, void *fp)
2606{
2607 char *key = vkey;
2608 struct perm_datum *perdatum = datum;
2609 __le32 buf[2];
2610 size_t len;
2611 int rc;
2612
2613 len = strlen(key);
2614 buf[0] = cpu_to_le32(len);
2615 buf[1] = cpu_to_le32(perdatum->value);
2616 rc = put_entry(buf, sizeof(u32), 2, fp);
2617 if (rc)
2618 return rc;
2619
2620 rc = put_entry(key, 1, len, fp);
2621 if (rc)
2622 return rc;
2623
2624 return 0;
2625}
2626
2627static int common_write(void *vkey, void *datum, void *ptr)
2628{
2629 char *key = vkey;
2630 struct common_datum *comdatum = datum;
2631 struct policy_data *pd = ptr;
2632 void *fp = pd->fp;
2633 __le32 buf[4];
2634 size_t len;
2635 int rc;
2636
2637 len = strlen(key);
2638 buf[0] = cpu_to_le32(len);
2639 buf[1] = cpu_to_le32(comdatum->value);
2640 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2641 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2642 rc = put_entry(buf, sizeof(u32), 4, fp);
2643 if (rc)
2644 return rc;
2645
2646 rc = put_entry(key, 1, len, fp);
2647 if (rc)
2648 return rc;
2649
2650 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2651 if (rc)
2652 return rc;
2653
2654 return 0;
2655}
2656
2657static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2658 void *fp)
2659{
2660 struct constraint_node *c;
2661 struct constraint_expr *e;
2662 __le32 buf[3];
2663 u32 nel;
2664 int rc;
2665
2666 for (c = node; c; c = c->next) {
2667 nel = 0;
2668 for (e = c->expr; e; e = e->next)
2669 nel++;
2670 buf[0] = cpu_to_le32(c->permissions);
2671 buf[1] = cpu_to_le32(nel);
2672 rc = put_entry(buf, sizeof(u32), 2, fp);
2673 if (rc)
2674 return rc;
2675 for (e = c->expr; e; e = e->next) {
2676 buf[0] = cpu_to_le32(e->expr_type);
2677 buf[1] = cpu_to_le32(e->attr);
2678 buf[2] = cpu_to_le32(e->op);
2679 rc = put_entry(buf, sizeof(u32), 3, fp);
2680 if (rc)
2681 return rc;
2682
2683 switch (e->expr_type) {
2684 case CEXPR_NAMES:
2685 rc = ebitmap_write(&e->names, fp);
2686 if (rc)
2687 return rc;
2688 break;
2689 default:
2690 break;
2691 }
2692 }
2693 }
2694
2695 return 0;
2696}
2697
2698static int class_write(void *vkey, void *datum, void *ptr)
2699{
2700 char *key = vkey;
2701 struct class_datum *cladatum = datum;
2702 struct policy_data *pd = ptr;
2703 void *fp = pd->fp;
2704 struct policydb *p = pd->p;
2705 struct constraint_node *c;
2706 __le32 buf[6];
2707 u32 ncons;
2708 size_t len, len2;
2709 int rc;
2710
2711 len = strlen(key);
2712 if (cladatum->comkey)
2713 len2 = strlen(cladatum->comkey);
2714 else
2715 len2 = 0;
2716
2717 ncons = 0;
2718 for (c = cladatum->constraints; c; c = c->next)
2719 ncons++;
2720
2721 buf[0] = cpu_to_le32(len);
2722 buf[1] = cpu_to_le32(len2);
2723 buf[2] = cpu_to_le32(cladatum->value);
2724 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2725 if (cladatum->permissions.table)
2726 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2727 else
2728 buf[4] = 0;
2729 buf[5] = cpu_to_le32(ncons);
2730 rc = put_entry(buf, sizeof(u32), 6, fp);
2731 if (rc)
2732 return rc;
2733
2734 rc = put_entry(key, 1, len, fp);
2735 if (rc)
2736 return rc;
2737
2738 if (cladatum->comkey) {
2739 rc = put_entry(cladatum->comkey, 1, len2, fp);
2740 if (rc)
2741 return rc;
2742 }
2743
2744 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2745 if (rc)
2746 return rc;
2747
2748 rc = write_cons_helper(p, cladatum->constraints, fp);
2749 if (rc)
2750 return rc;
2751
2752 /* write out the validatetrans rule */
2753 ncons = 0;
2754 for (c = cladatum->validatetrans; c; c = c->next)
2755 ncons++;
2756
2757 buf[0] = cpu_to_le32(ncons);
2758 rc = put_entry(buf, sizeof(u32), 1, fp);
2759 if (rc)
2760 return rc;
2761
2762 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2763 if (rc)
2764 return rc;
2765
2766 return 0;
2767}
2768
2769static int role_write(void *vkey, void *datum, void *ptr)
2770{
2771 char *key = vkey;
2772 struct role_datum *role = datum;
2773 struct policy_data *pd = ptr;
2774 void *fp = pd->fp;
2775 struct policydb *p = pd->p;
2776 __le32 buf[3];
2777 size_t items, len;
2778 int rc;
2779
2780 len = strlen(key);
2781 items = 0;
2782 buf[items++] = cpu_to_le32(len);
2783 buf[items++] = cpu_to_le32(role->value);
2784 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2785 buf[items++] = cpu_to_le32(role->bounds);
2786
2787 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2788
2789 rc = put_entry(buf, sizeof(u32), items, fp);
2790 if (rc)
2791 return rc;
2792
2793 rc = put_entry(key, 1, len, fp);
2794 if (rc)
2795 return rc;
2796
2797 rc = ebitmap_write(&role->dominates, fp);
2798 if (rc)
2799 return rc;
2800
2801 rc = ebitmap_write(&role->types, fp);
2802 if (rc)
2803 return rc;
2804
2805 return 0;
2806}
2807
2808static int type_write(void *vkey, void *datum, void *ptr)
2809{
2810 char *key = vkey;
2811 struct type_datum *typdatum = datum;
2812 struct policy_data *pd = ptr;
2813 struct policydb *p = pd->p;
2814 void *fp = pd->fp;
2815 __le32 buf[4];
2816 int rc;
2817 size_t items, len;
2818
2819 len = strlen(key);
2820 items = 0;
2821 buf[items++] = cpu_to_le32(len);
2822 buf[items++] = cpu_to_le32(typdatum->value);
2823 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2824 u32 properties = 0;
2825
2826 if (typdatum->primary)
2827 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2828
2829 if (typdatum->attribute)
2830 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2831
2832 buf[items++] = cpu_to_le32(properties);
2833 buf[items++] = cpu_to_le32(typdatum->bounds);
2834 } else {
2835 buf[items++] = cpu_to_le32(typdatum->primary);
2836 }
2837 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2838 rc = put_entry(buf, sizeof(u32), items, fp);
2839 if (rc)
2840 return rc;
2841
2842 rc = put_entry(key, 1, len, fp);
2843 if (rc)
2844 return rc;
2845
2846 return 0;
2847}
2848
2849static int user_write(void *vkey, void *datum, void *ptr)
2850{
2851 char *key = vkey;
2852 struct user_datum *usrdatum = datum;
2853 struct policy_data *pd = ptr;
2854 struct policydb *p = pd->p;
2855 void *fp = pd->fp;
2856 __le32 buf[3];
2857 size_t items, len;
2858 int rc;
2859
2860 len = strlen(key);
2861 items = 0;
2862 buf[items++] = cpu_to_le32(len);
2863 buf[items++] = cpu_to_le32(usrdatum->value);
2864 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2865 buf[items++] = cpu_to_le32(usrdatum->bounds);
2866 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2867 rc = put_entry(buf, sizeof(u32), items, fp);
2868 if (rc)
2869 return rc;
2870
2871 rc = put_entry(key, 1, len, fp);
2872 if (rc)
2873 return rc;
2874
2875 rc = ebitmap_write(&usrdatum->roles, fp);
2876 if (rc)
2877 return rc;
2878
2879 rc = mls_write_range_helper(&usrdatum->range, fp);
2880 if (rc)
2881 return rc;
2882
2883 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2884 if (rc)
2885 return rc;
2886
2887 return 0;
2888}
2889
2890static int (*write_f[SYM_NUM]) (void *key, void *datum,
2891 void *datap) =
2892{
2893 common_write,
2894 class_write,
2895 role_write,
2896 type_write,
2897 user_write,
2898 cond_write_bool,
2899 sens_write,
2900 cat_write,
2901};
2902
2903static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2904 void *fp)
2905{
2906 unsigned int i, j, rc;
2907 size_t nel, len;
2908 __le32 buf[3];
2909 u32 nodebuf[8];
2910 struct ocontext *c;
2911 for (i = 0; i < info->ocon_num; i++) {
2912 nel = 0;
2913 for (c = p->ocontexts[i]; c; c = c->next)
2914 nel++;
2915 buf[0] = cpu_to_le32(nel);
2916 rc = put_entry(buf, sizeof(u32), 1, fp);
2917 if (rc)
2918 return rc;
2919 for (c = p->ocontexts[i]; c; c = c->next) {
2920 switch (i) {
2921 case OCON_ISID:
2922 buf[0] = cpu_to_le32(c->sid[0]);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2924 if (rc)
2925 return rc;
2926 rc = context_write(p, &c->context[0], fp);
2927 if (rc)
2928 return rc;
2929 break;
2930 case OCON_FS:
2931 case OCON_NETIF:
2932 len = strlen(c->u.name);
2933 buf[0] = cpu_to_le32(len);
2934 rc = put_entry(buf, sizeof(u32), 1, fp);
2935 if (rc)
2936 return rc;
2937 rc = put_entry(c->u.name, 1, len, fp);
2938 if (rc)
2939 return rc;
2940 rc = context_write(p, &c->context[0], fp);
2941 if (rc)
2942 return rc;
2943 rc = context_write(p, &c->context[1], fp);
2944 if (rc)
2945 return rc;
2946 break;
2947 case OCON_PORT:
2948 buf[0] = cpu_to_le32(c->u.port.protocol);
2949 buf[1] = cpu_to_le32(c->u.port.low_port);
2950 buf[2] = cpu_to_le32(c->u.port.high_port);
2951 rc = put_entry(buf, sizeof(u32), 3, fp);
2952 if (rc)
2953 return rc;
2954 rc = context_write(p, &c->context[0], fp);
2955 if (rc)
2956 return rc;
2957 break;
2958 case OCON_NODE:
2959 nodebuf[0] = c->u.node.addr; /* network order */
2960 nodebuf[1] = c->u.node.mask; /* network order */
2961 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2962 if (rc)
2963 return rc;
2964 rc = context_write(p, &c->context[0], fp);
2965 if (rc)
2966 return rc;
2967 break;
2968 case OCON_FSUSE:
2969 buf[0] = cpu_to_le32(c->v.behavior);
2970 len = strlen(c->u.name);
2971 buf[1] = cpu_to_le32(len);
2972 rc = put_entry(buf, sizeof(u32), 2, fp);
2973 if (rc)
2974 return rc;
2975 rc = put_entry(c->u.name, 1, len, fp);
2976 if (rc)
2977 return rc;
2978 rc = context_write(p, &c->context[0], fp);
2979 if (rc)
2980 return rc;
2981 break;
2982 case OCON_NODE6:
2983 for (j = 0; j < 4; j++)
2984 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2985 for (j = 0; j < 4; j++)
2986 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2987 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2988 if (rc)
2989 return rc;
2990 rc = context_write(p, &c->context[0], fp);
2991 if (rc)
2992 return rc;
2993 break;
2994 }
2995 }
2996 }
2997 return 0;
2998}
2999
3000static int genfs_write(struct policydb *p, void *fp)
3001{
3002 struct genfs *genfs;
3003 struct ocontext *c;
3004 size_t len;
3005 __le32 buf[1];
3006 int rc;
3007
3008 len = 0;
3009 for (genfs = p->genfs; genfs; genfs = genfs->next)
3010 len++;
3011 buf[0] = cpu_to_le32(len);
3012 rc = put_entry(buf, sizeof(u32), 1, fp);
3013 if (rc)
3014 return rc;
3015 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3016 len = strlen(genfs->fstype);
3017 buf[0] = cpu_to_le32(len);
3018 rc = put_entry(buf, sizeof(u32), 1, fp);
3019 if (rc)
3020 return rc;
3021 rc = put_entry(genfs->fstype, 1, len, fp);
3022 if (rc)
3023 return rc;
3024 len = 0;
3025 for (c = genfs->head; c; c = c->next)
3026 len++;
3027 buf[0] = cpu_to_le32(len);
3028 rc = put_entry(buf, sizeof(u32), 1, fp);
3029 if (rc)
3030 return rc;
3031 for (c = genfs->head; c; c = c->next) {
3032 len = strlen(c->u.name);
3033 buf[0] = cpu_to_le32(len);
3034 rc = put_entry(buf, sizeof(u32), 1, fp);
3035 if (rc)
3036 return rc;
3037 rc = put_entry(c->u.name, 1, len, fp);
3038 if (rc)
3039 return rc;
3040 buf[0] = cpu_to_le32(c->v.sclass);
3041 rc = put_entry(buf, sizeof(u32), 1, fp);
3042 if (rc)
3043 return rc;
3044 rc = context_write(p, &c->context[0], fp);
3045 if (rc)
3046 return rc;
3047 }
3048 }
3049 return 0;
3050}
3051
3052static int range_count(void *key, void *data, void *ptr)
3053{
3054 int *cnt = ptr;
3055 *cnt = *cnt + 1;
3056
3057 return 0;
3058}
3059
3060static int range_write_helper(void *key, void *data, void *ptr)
3061{
3062 __le32 buf[2];
3063 struct range_trans *rt = key;
3064 struct mls_range *r = data;
3065 struct policy_data *pd = ptr;
3066 void *fp = pd->fp;
3067 struct policydb *p = pd->p;
3068 int rc;
3069
3070 buf[0] = cpu_to_le32(rt->source_type);
3071 buf[1] = cpu_to_le32(rt->target_type);
3072 rc = put_entry(buf, sizeof(u32), 2, fp);
3073 if (rc)
3074 return rc;
3075 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3076 buf[0] = cpu_to_le32(rt->target_class);
3077 rc = put_entry(buf, sizeof(u32), 1, fp);
3078 if (rc)
3079 return rc;
3080 }
3081 rc = mls_write_range_helper(r, fp);
3082 if (rc)
3083 return rc;
3084
3085 return 0;
3086}
3087
3088static int range_write(struct policydb *p, void *fp)
3089{
3090 size_t nel;
3091 __le32 buf[1];
3092 int rc;
3093 struct policy_data pd;
3094
3095 pd.p = p;
3096 pd.fp = fp;
3097
3098 /* count the number of entries in the hashtab */
3099 nel = 0;
3100 rc = hashtab_map(p->range_tr, range_count, &nel);
3101 if (rc)
3102 return rc;
3103
3104 buf[0] = cpu_to_le32(nel);
3105 rc = put_entry(buf, sizeof(u32), 1, fp);
3106 if (rc)
3107 return rc;
3108
3109 /* actually write all of the entries */
3110 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3111 if (rc)
3112 return rc;
3113
3114 return 0;
3115}
3116
Eric Paris652bb9b2011-02-01 11:05:40 -05003117static int filename_trans_write(struct policydb *p, void *fp)
3118{
3119 struct filename_trans *ft;
3120 u32 len, nel = 0;
3121 __le32 buf[4];
3122 int rc;
3123
3124 for (ft = p->filename_trans; ft; ft = ft->next)
3125 nel++;
3126
3127 buf[0] = cpu_to_le32(nel);
3128 rc = put_entry(buf, sizeof(u32), 1, fp);
3129 if (rc)
3130 return rc;
3131
3132 for (ft = p->filename_trans; ft; ft = ft->next) {
3133 len = strlen(ft->name);
3134 buf[0] = cpu_to_le32(len);
3135 rc = put_entry(buf, sizeof(u32), 1, fp);
3136 if (rc)
3137 return rc;
3138
3139 rc = put_entry(ft->name, sizeof(char), len, fp);
3140 if (rc)
3141 return rc;
3142
3143 buf[0] = ft->stype;
3144 buf[1] = ft->ttype;
3145 buf[2] = ft->tclass;
3146 buf[3] = ft->otype;
3147
3148 rc = put_entry(buf, sizeof(u32), 4, fp);
3149 if (rc)
3150 return rc;
3151 }
3152 return 0;
3153}
Eric Pariscee74f42010-10-13 17:50:25 -04003154/*
3155 * Write the configuration data in a policy database
3156 * structure to a policy database binary representation
3157 * file.
3158 */
3159int policydb_write(struct policydb *p, void *fp)
3160{
3161 unsigned int i, num_syms;
3162 int rc;
3163 __le32 buf[4];
3164 u32 config;
3165 size_t len;
3166 struct policydb_compat_info *info;
3167
3168 /*
3169 * refuse to write policy older than compressed avtab
3170 * to simplify the writer. There are other tests dropped
3171 * since we assume this throughout the writer code. Be
3172 * careful if you ever try to remove this restriction
3173 */
3174 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3175 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3176 " Because it is less than version %d\n", p->policyvers,
3177 POLICYDB_VERSION_AVTAB);
3178 return -EINVAL;
3179 }
3180
3181 config = 0;
3182 if (p->mls_enabled)
3183 config |= POLICYDB_CONFIG_MLS;
3184
3185 if (p->reject_unknown)
3186 config |= REJECT_UNKNOWN;
3187 if (p->allow_unknown)
3188 config |= ALLOW_UNKNOWN;
3189
3190 /* Write the magic number and string identifiers. */
3191 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3192 len = strlen(POLICYDB_STRING);
3193 buf[1] = cpu_to_le32(len);
3194 rc = put_entry(buf, sizeof(u32), 2, fp);
3195 if (rc)
3196 return rc;
3197 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3198 if (rc)
3199 return rc;
3200
3201 /* Write the version, config, and table sizes. */
3202 info = policydb_lookup_compat(p->policyvers);
3203 if (!info) {
3204 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3205 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003206 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003207 }
3208
3209 buf[0] = cpu_to_le32(p->policyvers);
3210 buf[1] = cpu_to_le32(config);
3211 buf[2] = cpu_to_le32(info->sym_num);
3212 buf[3] = cpu_to_le32(info->ocon_num);
3213
3214 rc = put_entry(buf, sizeof(u32), 4, fp);
3215 if (rc)
3216 return rc;
3217
3218 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3219 rc = ebitmap_write(&p->policycaps, fp);
3220 if (rc)
3221 return rc;
3222 }
3223
3224 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3225 rc = ebitmap_write(&p->permissive_map, fp);
3226 if (rc)
3227 return rc;
3228 }
3229
3230 num_syms = info->sym_num;
3231 for (i = 0; i < num_syms; i++) {
3232 struct policy_data pd;
3233
3234 pd.fp = fp;
3235 pd.p = p;
3236
3237 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3238 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3239
3240 rc = put_entry(buf, sizeof(u32), 2, fp);
3241 if (rc)
3242 return rc;
3243 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3244 if (rc)
3245 return rc;
3246 }
3247
3248 rc = avtab_write(p, &p->te_avtab, fp);
3249 if (rc)
3250 return rc;
3251
3252 rc = cond_write_list(p, p->cond_list, fp);
3253 if (rc)
3254 return rc;
3255
3256 rc = role_trans_write(p->role_tr, fp);
3257 if (rc)
3258 return rc;
3259
3260 rc = role_allow_write(p->role_allow, fp);
3261 if (rc)
3262 return rc;
3263
Eric Paris652bb9b2011-02-01 11:05:40 -05003264 rc = filename_trans_write(p, fp);
3265 if (rc)
3266 return rc;
3267
Eric Pariscee74f42010-10-13 17:50:25 -04003268 rc = ocontext_write(p, info, fp);
3269 if (rc)
3270 return rc;
3271
3272 rc = genfs_write(p, fp);
3273 if (rc)
3274 return rc;
3275
3276 rc = range_write(p, fp);
3277 if (rc)
3278 return rc;
3279
3280 for (i = 0; i < p->p_types.nprim; i++) {
3281 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3282
3283 BUG_ON(!e);
3284 rc = ebitmap_write(e, fp);
3285 if (rc)
3286 return rc;
3287 }
3288
3289 return 0;
3290}