blob: a493eae24e0ac76ddeaff78c722ab88d31594b83 [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 },
Harry Ciao80239762011-03-25 13:51:56 +0800131 {
132 .version = POLICYDB_VERSION_ROLETRANS,
133 .sym_num = SYM_NUM,
134 .ocon_num = OCON_NUM,
135 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static struct policydb_compat_info *policydb_lookup_compat(int version)
139{
140 int i;
141 struct policydb_compat_info *info = NULL;
142
Tobias Klauser32725ad2006-01-06 00:11:23 -0800143 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (policydb_compat[i].version == version) {
145 info = &policydb_compat[i];
146 break;
147 }
148 }
149 return info;
150}
151
152/*
153 * Initialize the role table.
154 */
155static int roles_init(struct policydb *p)
156{
157 char *key = NULL;
158 int rc;
159 struct role_datum *role;
160
Eric Paris9398c7f2010-11-23 11:40:08 -0500161 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800162 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500163 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500165
166 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500168 if (role->value != OBJECT_R_VAL)
169 goto out;
170
171 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200172 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500173 if (!key)
174 goto out;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 rc = hashtab_insert(p->p_roles.table, key, role);
177 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Eric Paris9398c7f2010-11-23 11:40:08 -0500180 return 0;
181out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500184 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500187static u32 rangetr_hash(struct hashtab *h, const void *k)
188{
189 const struct range_trans *key = k;
190 return (key->source_type + (key->target_type << 3) +
191 (key->target_class << 5)) & (h->size - 1);
192}
193
194static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
195{
196 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400197 int v;
198
199 v = key1->source_type - key2->source_type;
200 if (v)
201 return v;
202
203 v = key1->target_type - key2->target_type;
204 if (v)
205 return v;
206
207 v = key1->target_class - key2->target_class;
208
209 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * Initialize a policy database structure.
214 */
215static int policydb_init(struct policydb *p)
216{
217 int i, rc;
218
219 memset(p, 0, sizeof(*p));
220
221 for (i = 0; i < SYM_NUM; i++) {
222 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
223 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 rc = avtab_init(&p->te_avtab);
228 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 rc = roles_init(p);
232 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 rc = cond_policydb_init(p);
236 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500239 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
240 if (!p->range_tr)
Eric Paris9398c7f2010-11-23 11:40:08 -0500241 goto out;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500242
Paul Moore3bb56b22008-01-29 08:38:19 -0500243 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100244 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500245
Eric Paris9398c7f2010-11-23 11:40:08 -0500246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 for (i = 0; i < SYM_NUM; i++)
249 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500250 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * The following *_index functions are used to
255 * define the val_to_name and val_to_struct arrays
256 * in a policy database structure. The val_to_name
257 * arrays are used when converting security context
258 * structures into string representations. The
259 * val_to_struct arrays are used when the attributes
260 * of a class, role, or user are needed.
261 */
262
263static int common_index(void *key, void *datum, void *datap)
264{
265 struct policydb *p;
266 struct common_datum *comdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500267 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 comdatum = datum;
270 p = datap;
271 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
272 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500273
274 fa = p->sym_val_to_name[SYM_COMMONS];
275 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
276 GFP_KERNEL | __GFP_ZERO))
277 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
281static int class_index(void *key, void *datum, void *datap)
282{
283 struct policydb *p;
284 struct class_datum *cladatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500285 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 cladatum = datum;
288 p = datap;
289 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
290 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500291 fa = p->sym_val_to_name[SYM_CLASSES];
292 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
293 GFP_KERNEL | __GFP_ZERO))
294 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p->class_val_to_struct[cladatum->value - 1] = cladatum;
296 return 0;
297}
298
299static int role_index(void *key, void *datum, void *datap)
300{
301 struct policydb *p;
302 struct role_datum *role;
Eric Parisac76c05b2010-11-29 15:47:09 -0500303 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 role = datum;
306 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900307 if (!role->value
308 || role->value > p->p_roles.nprim
309 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500311
312 fa = p->sym_val_to_name[SYM_ROLES];
313 if (flex_array_put_ptr(fa, role->value - 1, key,
314 GFP_KERNEL | __GFP_ZERO))
315 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 p->role_val_to_struct[role->value - 1] = role;
317 return 0;
318}
319
320static int type_index(void *key, void *datum, void *datap)
321{
322 struct policydb *p;
323 struct type_datum *typdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500324 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 typdatum = datum;
327 p = datap;
328
329 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900330 if (!typdatum->value
331 || typdatum->value > p->p_types.nprim
332 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500334 fa = p->sym_val_to_name[SYM_TYPES];
335 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
336 GFP_KERNEL | __GFP_ZERO))
337 BUG();
338
339 fa = p->type_val_to_struct_array;
340 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500341 GFP_KERNEL | __GFP_ZERO))
342 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 return 0;
346}
347
348static int user_index(void *key, void *datum, void *datap)
349{
350 struct policydb *p;
351 struct user_datum *usrdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500352 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 usrdatum = datum;
355 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900356 if (!usrdatum->value
357 || usrdatum->value > p->p_users.nprim
358 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500360
361 fa = p->sym_val_to_name[SYM_USERS];
362 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
363 GFP_KERNEL | __GFP_ZERO))
364 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
366 return 0;
367}
368
369static int sens_index(void *key, void *datum, void *datap)
370{
371 struct policydb *p;
372 struct level_datum *levdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500373 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 levdatum = datum;
376 p = datap;
377
378 if (!levdatum->isalias) {
379 if (!levdatum->level->sens ||
380 levdatum->level->sens > p->p_levels.nprim)
381 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500382 fa = p->sym_val_to_name[SYM_LEVELS];
383 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
384 GFP_KERNEL | __GFP_ZERO))
385 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
388 return 0;
389}
390
391static int cat_index(void *key, void *datum, void *datap)
392{
393 struct policydb *p;
394 struct cat_datum *catdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500395 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 catdatum = datum;
398 p = datap;
399
400 if (!catdatum->isalias) {
401 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
402 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500403 fa = p->sym_val_to_name[SYM_CATS];
404 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
405 GFP_KERNEL | __GFP_ZERO))
406 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 return 0;
410}
411
412static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
413{
414 common_index,
415 class_index,
416 role_index,
417 type_index,
418 user_index,
419 cond_index_bool,
420 sens_index,
421 cat_index,
422};
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#ifdef DEBUG_HASHES
425static void symtab_hash_eval(struct symtab *s)
426{
427 int i;
428
429 for (i = 0; i < SYM_NUM; i++) {
430 struct hashtab *h = s[i].table;
431 struct hashtab_info info;
432
433 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400434 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 "longest chain length %d\n", symtab_name[i], h->nel,
436 info.slots_used, h->size, info.max_chain_len);
437 }
438}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500439
440static void rangetr_hash_eval(struct hashtab *h)
441{
442 struct hashtab_info info;
443
444 hashtab_stat(h, &info);
445 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
446 "longest chain length %d\n", h->nel,
447 info.slots_used, h->size, info.max_chain_len);
448}
449#else
450static inline void rangetr_hash_eval(struct hashtab *h)
451{
452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#endif
454
455/*
456 * Define the other val_to_name and val_to_struct arrays
457 * in a policy database structure.
458 *
459 * Caller must clean up on failure.
460 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500461static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Eric Paris9398c7f2010-11-23 11:40:08 -0500463 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
James Morris454d9722008-02-26 20:42:02 +1100465 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100467 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 printk(", %d sens, %d cats", p->p_levels.nprim,
469 p->p_cats.nprim);
470 printk("\n");
471
James Morris454d9722008-02-26 20:42:02 +1100472 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 p->p_classes.nprim, p->te_avtab.nel);
474
475#ifdef DEBUG_HASHES
476 avtab_hash_eval(&p->te_avtab, "rules");
477 symtab_hash_eval(p->symtab);
478#endif
479
Eric Paris9398c7f2010-11-23 11:40:08 -0500480 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500481 p->class_val_to_struct =
482 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
483 GFP_KERNEL);
484 if (!p->class_val_to_struct)
485 goto out;
486
487 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 p->role_val_to_struct =
489 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400490 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500491 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Eric Paris9398c7f2010-11-23 11:40:08 -0500494 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 p->user_val_to_struct =
496 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400497 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500498 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Eric Paris23bdecb2010-11-29 15:47:09 -0500501 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500502 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500503 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
504 p->p_types.nprim,
505 GFP_KERNEL | __GFP_ZERO);
506 if (!p->type_val_to_struct_array)
507 goto out;
508
509 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
510 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
511 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900512 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900513
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300514 rc = cond_init_bool_indexes(p);
515 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500518 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500519 rc = -ENOMEM;
Eric Parisac76c05b2010-11-29 15:47:09 -0500520 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
521 p->symtab[i].nprim,
522 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500523 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto out;
Eric Parisac76c05b2010-11-29 15:47:09 -0500525
526 rc = flex_array_prealloc(p->sym_val_to_name[i],
527 0, p->symtab[i].nprim - 1,
528 GFP_KERNEL | __GFP_ZERO);
529 if (rc)
530 goto out;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
533 if (rc)
534 goto out;
535 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500536 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537out:
538 return rc;
539}
540
541/*
542 * The following *_destroy functions are used to
543 * free any memory allocated for each kind of
544 * symbol data in the policy database.
545 */
546
547static int perm_destroy(void *key, void *datum, void *p)
548{
549 kfree(key);
550 kfree(datum);
551 return 0;
552}
553
554static int common_destroy(void *key, void *datum, void *p)
555{
556 struct common_datum *comdatum;
557
558 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500559 if (datum) {
560 comdatum = datum;
561 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
562 hashtab_destroy(comdatum->permissions.table);
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 kfree(datum);
565 return 0;
566}
567
James Morris6cbda6b2006-11-29 16:50:27 -0500568static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct class_datum *cladatum;
571 struct constraint_node *constraint, *ctemp;
572 struct constraint_expr *e, *etmp;
573
574 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500575 if (datum) {
576 cladatum = datum;
577 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
578 hashtab_destroy(cladatum->permissions.table);
579 constraint = cladatum->constraints;
580 while (constraint) {
581 e = constraint->expr;
582 while (e) {
583 ebitmap_destroy(&e->names);
584 etmp = e;
585 e = e->next;
586 kfree(etmp);
587 }
588 ctemp = constraint;
589 constraint = constraint->next;
590 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Eric Paris9398c7f2010-11-23 11:40:08 -0500593 constraint = cladatum->validatetrans;
594 while (constraint) {
595 e = constraint->expr;
596 while (e) {
597 ebitmap_destroy(&e->names);
598 etmp = e;
599 e = e->next;
600 kfree(etmp);
601 }
602 ctemp = constraint;
603 constraint = constraint->next;
604 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Eric Paris9398c7f2010-11-23 11:40:08 -0500607 kfree(cladatum->comkey);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 kfree(datum);
610 return 0;
611}
612
613static int role_destroy(void *key, void *datum, void *p)
614{
615 struct role_datum *role;
616
617 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500618 if (datum) {
619 role = datum;
620 ebitmap_destroy(&role->dominates);
621 ebitmap_destroy(&role->types);
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 kfree(datum);
624 return 0;
625}
626
627static int type_destroy(void *key, void *datum, void *p)
628{
629 kfree(key);
630 kfree(datum);
631 return 0;
632}
633
634static int user_destroy(void *key, void *datum, void *p)
635{
636 struct user_datum *usrdatum;
637
638 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500639 if (datum) {
640 usrdatum = datum;
641 ebitmap_destroy(&usrdatum->roles);
642 ebitmap_destroy(&usrdatum->range.level[0].cat);
643 ebitmap_destroy(&usrdatum->range.level[1].cat);
644 ebitmap_destroy(&usrdatum->dfltlevel.cat);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kfree(datum);
647 return 0;
648}
649
650static int sens_destroy(void *key, void *datum, void *p)
651{
652 struct level_datum *levdatum;
653
654 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500655 if (datum) {
656 levdatum = datum;
657 ebitmap_destroy(&levdatum->level->cat);
658 kfree(levdatum->level);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 kfree(datum);
661 return 0;
662}
663
664static int cat_destroy(void *key, void *datum, void *p)
665{
666 kfree(key);
667 kfree(datum);
668 return 0;
669}
670
671static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
672{
673 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500674 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 role_destroy,
676 type_destroy,
677 user_destroy,
678 cond_destroy_bool,
679 sens_destroy,
680 cat_destroy,
681};
682
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500683static int range_tr_destroy(void *key, void *datum, void *p)
684{
685 struct mls_range *rt = datum;
686 kfree(key);
687 ebitmap_destroy(&rt->level[0].cat);
688 ebitmap_destroy(&rt->level[1].cat);
689 kfree(datum);
690 cond_resched();
691 return 0;
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void ocontext_destroy(struct ocontext *c, int i)
695{
Eric Parisd1b43542010-07-21 12:50:57 -0400696 if (!c)
697 return;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 context_destroy(&c->context[0]);
700 context_destroy(&c->context[1]);
701 if (i == OCON_ISID || i == OCON_FS ||
702 i == OCON_NETIF || i == OCON_FSUSE)
703 kfree(c->u.name);
704 kfree(c);
705}
706
707/*
708 * Free any memory allocated by a policy database structure.
709 */
710void policydb_destroy(struct policydb *p)
711{
712 struct ocontext *c, *ctmp;
713 struct genfs *g, *gtmp;
714 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700715 struct role_allow *ra, *lra = NULL;
716 struct role_trans *tr, *ltr = NULL;
Eric Paris652bb9b2011-02-01 11:05:40 -0500717 struct filename_trans *ft, *nft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400720 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
722 hashtab_destroy(p->symtab[i].table);
723 }
724
Eric Parisac76c05b2010-11-29 15:47:09 -0500725 for (i = 0; i < SYM_NUM; i++) {
726 if (p->sym_val_to_name[i])
727 flex_array_free(p->sym_val_to_name[i]);
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700730 kfree(p->class_val_to_struct);
731 kfree(p->role_val_to_struct);
732 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500733 if (p->type_val_to_struct_array)
734 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 avtab_destroy(&p->te_avtab);
737
738 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400739 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 c = p->ocontexts[i];
741 while (c) {
742 ctmp = c;
743 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400744 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400746 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 g = p->genfs;
750 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400751 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 kfree(g->fstype);
753 c = g->head;
754 while (c) {
755 ctmp = c;
756 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400757 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 gtmp = g;
760 g = g->next;
761 kfree(gtmp);
762 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400763 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 cond_policydb_destroy(p);
766
Stephen Smalley782ebb92005-09-03 15:55:16 -0700767 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400768 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800769 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700770 ltr = tr;
771 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800772 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700773
Eric Paris2ced3df2008-04-17 13:37:12 -0400774 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400775 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800776 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700777 lra = ra;
778 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800779 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700780
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500781 hashtab_map(p->range_tr, range_tr_destroy, NULL);
782 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700783
Eric Paris6371dcd2010-07-29 23:02:34 -0400784 if (p->type_attr_map_array) {
785 for (i = 0; i < p->p_types.nprim; i++) {
786 struct ebitmap *e;
787
788 e = flex_array_get(p->type_attr_map_array, i);
789 if (!e)
790 continue;
791 ebitmap_destroy(e);
792 }
793 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700794 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500795
796 ft = p->filename_trans;
797 while (ft) {
798 nft = ft->next;
799 kfree(ft->name);
800 kfree(ft);
801 ft = nft;
802 }
803
Paul Moore3bb56b22008-01-29 08:38:19 -0500804 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100805 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
808}
809
810/*
811 * Load the initial SIDs specified in a policy database
812 * structure into a SID table.
813 */
814int policydb_load_isids(struct policydb *p, struct sidtab *s)
815{
816 struct ocontext *head, *c;
817 int rc;
818
819 rc = sidtab_init(s);
820 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100821 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto out;
823 }
824
825 head = p->ocontexts[OCON_ISID];
826 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500827 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500829 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
830 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto out;
832 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500833
834 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
835 if (rc) {
836 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
837 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto out;
839 }
840 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500841 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842out:
843 return rc;
844}
845
Stephen Smalley45e54212007-11-07 10:08:00 -0500846int policydb_class_isvalid(struct policydb *p, unsigned int class)
847{
848 if (!class || class > p->p_classes.nprim)
849 return 0;
850 return 1;
851}
852
853int policydb_role_isvalid(struct policydb *p, unsigned int role)
854{
855 if (!role || role > p->p_roles.nprim)
856 return 0;
857 return 1;
858}
859
860int policydb_type_isvalid(struct policydb *p, unsigned int type)
861{
862 if (!type || type > p->p_types.nprim)
863 return 0;
864 return 1;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * Return 1 if the fields in the security context
869 * structure `c' are valid. Return 0 otherwise.
870 */
871int policydb_context_isvalid(struct policydb *p, struct context *c)
872{
873 struct role_datum *role;
874 struct user_datum *usrdatum;
875
876 if (!c->role || c->role > p->p_roles.nprim)
877 return 0;
878
879 if (!c->user || c->user > p->p_users.nprim)
880 return 0;
881
882 if (!c->type || c->type > p->p_types.nprim)
883 return 0;
884
885 if (c->role != OBJECT_R_VAL) {
886 /*
887 * Role must be authorized for the type.
888 */
889 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500890 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* role may not be associated with type */
892 return 0;
893
894 /*
895 * User must be authorized for the role.
896 */
897 usrdatum = p->user_val_to_struct[c->user - 1];
898 if (!usrdatum)
899 return 0;
900
Eric Paris9398c7f2010-11-23 11:40:08 -0500901 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* user may not be associated with role */
903 return 0;
904 }
905
906 if (!mls_context_isvalid(p, c))
907 return 0;
908
909 return 1;
910}
911
912/*
913 * Read a MLS range structure from a policydb binary
914 * representation file.
915 */
916static int mls_read_range_helper(struct mls_range *r, void *fp)
917{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700918 __le32 buf[2];
919 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int rc;
921
922 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500923 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out;
925
Eric Paris9398c7f2010-11-23 11:40:08 -0500926 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 items = le32_to_cpu(buf[0]);
928 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100929 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto out;
931 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500934 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100935 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto out;
937 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 r->level[0].sens = le32_to_cpu(buf[0]);
940 if (items > 1)
941 r->level[1].sens = le32_to_cpu(buf[1]);
942 else
943 r->level[1].sens = r->level[0].sens;
944
945 rc = ebitmap_read(&r->level[0].cat, fp);
946 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500947 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto out;
949 }
950 if (items > 1) {
951 rc = ebitmap_read(&r->level[1].cat, fp);
952 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500953 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto bad_high;
955 }
956 } else {
957 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
958 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100959 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 goto bad_high;
961 }
962 }
963
Eric Paris9398c7f2010-11-23 11:40:08 -0500964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965bad_high:
966 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500967out:
968 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
972 * Read and validate a security context structure
973 * from a policydb binary representation file.
974 */
975static int context_read_and_validate(struct context *c,
976 struct policydb *p,
977 void *fp)
978{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700979 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int rc;
981
982 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500983 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100984 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out;
986 }
987 c->user = le32_to_cpu(buf[0]);
988 c->role = le32_to_cpu(buf[1]);
989 c->type = le32_to_cpu(buf[2]);
990 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500991 rc = mls_read_range_helper(&c->range, fp);
992 if (rc) {
993 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995 }
996 }
997
Eric Paris9398c7f2010-11-23 11:40:08 -0500998 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001000 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001004 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005out:
1006 return rc;
1007}
1008
1009/*
1010 * The following *_read functions are used to
1011 * read the symbol data from a policy database
1012 * binary representation file.
1013 */
1014
1015static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1016{
1017 char *key = NULL;
1018 struct perm_datum *perdatum;
1019 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001020 __le32 buf[2];
1021 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Eric Paris9398c7f2010-11-23 11:40:08 -05001023 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001024 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001025 if (!perdatum)
1026 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001029 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto bad;
1031
1032 len = le32_to_cpu(buf[0]);
1033 perdatum->value = le32_to_cpu(buf[1]);
1034
Eric Paris9398c7f2010-11-23 11:40:08 -05001035 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001036 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001037 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001041 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001043 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 rc = hashtab_insert(h, key, perdatum);
1046 if (rc)
1047 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001048
1049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050bad:
1051 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001052 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1056{
1057 char *key = NULL;
1058 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001059 __le32 buf[4];
1060 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int i, rc;
1062
Eric Paris9398c7f2010-11-23 11:40:08 -05001063 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001064 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001065 if (!comdatum)
1066 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001069 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto bad;
1071
1072 len = le32_to_cpu(buf[0]);
1073 comdatum->value = le32_to_cpu(buf[1]);
1074
1075 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1076 if (rc)
1077 goto bad;
1078 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1079 nel = le32_to_cpu(buf[3]);
1080
Eric Paris9398c7f2010-11-23 11:40:08 -05001081 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001082 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001083 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001087 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001089 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 for (i = 0; i < nel; i++) {
1092 rc = perm_read(p, comdatum->permissions.table, fp);
1093 if (rc)
1094 goto bad;
1095 }
1096
1097 rc = hashtab_insert(h, key, comdatum);
1098 if (rc)
1099 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101bad:
1102 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001103 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001107 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct constraint_node *c, *lc;
1110 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001111 __le32 buf[3];
1112 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int rc, i, j, depth;
1114
1115 lc = NULL;
1116 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001117 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!c)
1119 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Eric Paris2ced3df2008-04-17 13:37:12 -04001121 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001123 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001127 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return rc;
1129 c->permissions = le32_to_cpu(buf[0]);
1130 nexpr = le32_to_cpu(buf[1]);
1131 le = NULL;
1132 depth = -1;
1133 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001134 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!e)
1136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Eric Paris2ced3df2008-04-17 13:37:12 -04001138 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001140 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001144 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return rc;
1146 e->expr_type = le32_to_cpu(buf[0]);
1147 e->attr = le32_to_cpu(buf[1]);
1148 e->op = le32_to_cpu(buf[2]);
1149
1150 switch (e->expr_type) {
1151 case CEXPR_NOT:
1152 if (depth < 0)
1153 return -EINVAL;
1154 break;
1155 case CEXPR_AND:
1156 case CEXPR_OR:
1157 if (depth < 1)
1158 return -EINVAL;
1159 depth--;
1160 break;
1161 case CEXPR_ATTR:
1162 if (depth == (CEXPR_MAXDEPTH - 1))
1163 return -EINVAL;
1164 depth++;
1165 break;
1166 case CEXPR_NAMES:
1167 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1168 return -EINVAL;
1169 if (depth == (CEXPR_MAXDEPTH - 1))
1170 return -EINVAL;
1171 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001172 rc = ebitmap_read(&e->names, fp);
1173 if (rc)
1174 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
1176 default:
1177 return -EINVAL;
1178 }
1179 le = e;
1180 }
1181 if (depth != 0)
1182 return -EINVAL;
1183 lc = c;
1184 }
1185
1186 return 0;
1187}
1188
1189static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1190{
1191 char *key = NULL;
1192 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001193 __le32 buf[6];
1194 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int i, rc;
1196
Eric Paris9398c7f2010-11-23 11:40:08 -05001197 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001198 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001199 if (!cladatum)
1200 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001203 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto bad;
1205
1206 len = le32_to_cpu(buf[0]);
1207 len2 = le32_to_cpu(buf[1]);
1208 cladatum->value = le32_to_cpu(buf[2]);
1209
1210 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1211 if (rc)
1212 goto bad;
1213 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1214 nel = le32_to_cpu(buf[4]);
1215
1216 ncons = le32_to_cpu(buf[5]);
1217
Eric Paris9398c7f2010-11-23 11:40:08 -05001218 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001219 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001220 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001224 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001226 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001229 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001230 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001231 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001234 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001236 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Eric Paris9398c7f2010-11-23 11:40:08 -05001238 rc = -EINVAL;
1239 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001241 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto bad;
1243 }
1244 }
1245 for (i = 0; i < nel; i++) {
1246 rc = perm_read(p, cladatum->permissions.table, fp);
1247 if (rc)
1248 goto bad;
1249 }
1250
1251 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1252 if (rc)
1253 goto bad;
1254
1255 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1256 /* grab the validatetrans rules */
1257 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001258 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto bad;
1260 ncons = le32_to_cpu(buf[0]);
1261 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1262 if (rc)
1263 goto bad;
1264 }
1265
1266 rc = hashtab_insert(h, key, cladatum);
1267 if (rc)
1268 goto bad;
1269
Eric Paris9398c7f2010-11-23 11:40:08 -05001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001272 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001273 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1277{
1278 char *key = NULL;
1279 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001280 int rc, to_read = 2;
1281 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001282 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Eric Paris9398c7f2010-11-23 11:40:08 -05001284 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001285 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001286 if (!role)
1287 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
KaiGai Koheid9250de2008-08-28 16:35:57 +09001289 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1290 to_read = 3;
1291
1292 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001293 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto bad;
1295
1296 len = le32_to_cpu(buf[0]);
1297 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001298 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1299 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Eric Paris9398c7f2010-11-23 11:40:08 -05001301 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001302 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001303 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001307 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001309 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 rc = ebitmap_read(&role->dominates, fp);
1312 if (rc)
1313 goto bad;
1314
1315 rc = ebitmap_read(&role->types, fp);
1316 if (rc)
1317 goto bad;
1318
1319 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001320 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001322 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto bad;
1325 }
1326 rc = 0;
1327 goto bad;
1328 }
1329
1330 rc = hashtab_insert(h, key, role);
1331 if (rc)
1332 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334bad:
1335 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001336 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1340{
1341 char *key = NULL;
1342 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001343 int rc, to_read = 3;
1344 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001345 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Eric Paris9398c7f2010-11-23 11:40:08 -05001347 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001348 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001349 if (!typdatum)
1350 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
KaiGai Koheid9250de2008-08-28 16:35:57 +09001352 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1353 to_read = 4;
1354
1355 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001356 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto bad;
1358
1359 len = le32_to_cpu(buf[0]);
1360 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001361 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1362 u32 prop = le32_to_cpu(buf[2]);
1363
1364 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1365 typdatum->primary = 1;
1366 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1367 typdatum->attribute = 1;
1368
1369 typdatum->bounds = le32_to_cpu(buf[3]);
1370 } else {
1371 typdatum->primary = le32_to_cpu(buf[2]);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric Paris9398c7f2010-11-23 11:40:08 -05001374 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001375 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001376 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001379 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001381 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 rc = hashtab_insert(h, key, typdatum);
1384 if (rc)
1385 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387bad:
1388 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001389 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392
1393/*
1394 * Read a MLS level structure from a policydb binary
1395 * representation file.
1396 */
1397static int mls_read_level(struct mls_level *lp, void *fp)
1398{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001399 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int rc;
1401
1402 memset(lp, 0, sizeof(*lp));
1403
1404 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001405 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001406 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001407 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 lp->sens = le32_to_cpu(buf[0]);
1410
Eric Paris9398c7f2010-11-23 11:40:08 -05001411 rc = ebitmap_read(&lp->cat, fp);
1412 if (rc) {
1413 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1414 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1420{
1421 char *key = NULL;
1422 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001423 int rc, to_read = 2;
1424 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001425 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Eric Paris9398c7f2010-11-23 11:40:08 -05001427 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001428 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001429 if (!usrdatum)
1430 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
KaiGai Koheid9250de2008-08-28 16:35:57 +09001432 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1433 to_read = 3;
1434
1435 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001436 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto bad;
1438
1439 len = le32_to_cpu(buf[0]);
1440 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001441 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1442 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric Paris9398c7f2010-11-23 11:40:08 -05001444 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001445 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001446 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001449 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001451 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 rc = ebitmap_read(&usrdatum->roles, fp);
1454 if (rc)
1455 goto bad;
1456
1457 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1458 rc = mls_read_range_helper(&usrdatum->range, fp);
1459 if (rc)
1460 goto bad;
1461 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1462 if (rc)
1463 goto bad;
1464 }
1465
1466 rc = hashtab_insert(h, key, usrdatum);
1467 if (rc)
1468 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470bad:
1471 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001472 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1476{
1477 char *key = NULL;
1478 struct level_datum *levdatum;
1479 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001480 __le32 buf[2];
1481 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Eric Paris9398c7f2010-11-23 11:40:08 -05001483 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001484 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001485 if (!levdatum)
1486 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001489 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto bad;
1491
1492 len = le32_to_cpu(buf[0]);
1493 levdatum->isalias = le32_to_cpu(buf[1]);
1494
Eric Paris9398c7f2010-11-23 11:40:08 -05001495 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001496 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001497 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001500 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001502 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Eric Paris9398c7f2010-11-23 11:40:08 -05001504 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001506 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001508
1509 rc = mls_read_level(levdatum->level, fp);
1510 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 rc = hashtab_insert(h, key, levdatum);
1514 if (rc)
1515 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517bad:
1518 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001519 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1523{
1524 char *key = NULL;
1525 struct cat_datum *catdatum;
1526 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001527 __le32 buf[3];
1528 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric Paris9398c7f2010-11-23 11:40:08 -05001530 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001531 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001532 if (!catdatum)
1533 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001536 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 goto bad;
1538
1539 len = le32_to_cpu(buf[0]);
1540 catdatum->value = le32_to_cpu(buf[1]);
1541 catdatum->isalias = le32_to_cpu(buf[2]);
1542
Eric Paris9398c7f2010-11-23 11:40:08 -05001543 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001544 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001545 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001548 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001550 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 rc = hashtab_insert(h, key, catdatum);
1553 if (rc)
1554 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556bad:
1557 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001558 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1562{
1563 common_read,
1564 class_read,
1565 role_read,
1566 type_read,
1567 user_read,
1568 cond_read_bool,
1569 sens_read,
1570 cat_read,
1571};
1572
KaiGai Koheid9250de2008-08-28 16:35:57 +09001573static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1574{
1575 struct user_datum *upper, *user;
1576 struct policydb *p = datap;
1577 int depth = 0;
1578
1579 upper = user = datum;
1580 while (upper->bounds) {
1581 struct ebitmap_node *node;
1582 unsigned long bit;
1583
1584 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1585 printk(KERN_ERR "SELinux: user %s: "
1586 "too deep or looped boundary",
1587 (char *) key);
1588 return -EINVAL;
1589 }
1590
1591 upper = p->user_val_to_struct[upper->bounds - 1];
1592 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1593 if (ebitmap_get_bit(&upper->roles, bit))
1594 continue;
1595
1596 printk(KERN_ERR
1597 "SELinux: boundary violated policy: "
1598 "user=%s role=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001599 sym_name(p, SYM_USERS, user->value - 1),
1600 sym_name(p, SYM_ROLES, bit),
1601 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001602
1603 return -EINVAL;
1604 }
1605 }
1606
1607 return 0;
1608}
1609
1610static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1611{
1612 struct role_datum *upper, *role;
1613 struct policydb *p = datap;
1614 int depth = 0;
1615
1616 upper = role = datum;
1617 while (upper->bounds) {
1618 struct ebitmap_node *node;
1619 unsigned long bit;
1620
1621 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1622 printk(KERN_ERR "SELinux: role %s: "
1623 "too deep or looped bounds\n",
1624 (char *) key);
1625 return -EINVAL;
1626 }
1627
1628 upper = p->role_val_to_struct[upper->bounds - 1];
1629 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1630 if (ebitmap_get_bit(&upper->types, bit))
1631 continue;
1632
1633 printk(KERN_ERR
1634 "SELinux: boundary violated policy: "
1635 "role=%s type=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001636 sym_name(p, SYM_ROLES, role->value - 1),
1637 sym_name(p, SYM_TYPES, bit),
1638 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001639
1640 return -EINVAL;
1641 }
1642 }
1643
1644 return 0;
1645}
1646
1647static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1648{
Eric Parisdaa6d832010-08-03 15:26:05 -04001649 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001650 struct policydb *p = datap;
1651 int depth = 0;
1652
Eric Parisdaa6d832010-08-03 15:26:05 -04001653 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001654 while (upper->bounds) {
1655 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1656 printk(KERN_ERR "SELinux: type %s: "
1657 "too deep or looped boundary\n",
1658 (char *) key);
1659 return -EINVAL;
1660 }
1661
Eric Paris23bdecb2010-11-29 15:47:09 -05001662 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1663 upper->bounds - 1);
1664 BUG_ON(!upper);
1665
KaiGai Koheid9250de2008-08-28 16:35:57 +09001666 if (upper->attribute) {
1667 printk(KERN_ERR "SELinux: type %s: "
1668 "bounded by attribute %s",
1669 (char *) key,
Eric Parisac76c05b2010-11-29 15:47:09 -05001670 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001671 return -EINVAL;
1672 }
1673 }
1674
1675 return 0;
1676}
1677
1678static int policydb_bounds_sanity_check(struct policydb *p)
1679{
1680 int rc;
1681
1682 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1683 return 0;
1684
1685 rc = hashtab_map(p->p_users.table,
1686 user_bounds_sanity_check, p);
1687 if (rc)
1688 return rc;
1689
1690 rc = hashtab_map(p->p_roles.table,
1691 role_bounds_sanity_check, p);
1692 if (rc)
1693 return rc;
1694
1695 rc = hashtab_map(p->p_types.table,
1696 type_bounds_sanity_check, p);
1697 if (rc)
1698 return rc;
1699
1700 return 0;
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703extern int ss_initialized;
1704
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001705u16 string_to_security_class(struct policydb *p, const char *name)
1706{
1707 struct class_datum *cladatum;
1708
1709 cladatum = hashtab_search(p->p_classes.table, name);
1710 if (!cladatum)
1711 return 0;
1712
1713 return cladatum->value;
1714}
1715
1716u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1717{
1718 struct class_datum *cladatum;
1719 struct perm_datum *perdatum = NULL;
1720 struct common_datum *comdatum;
1721
1722 if (!tclass || tclass > p->p_classes.nprim)
1723 return 0;
1724
1725 cladatum = p->class_val_to_struct[tclass-1];
1726 comdatum = cladatum->comdatum;
1727 if (comdatum)
1728 perdatum = hashtab_search(comdatum->permissions.table,
1729 name);
1730 if (!perdatum)
1731 perdatum = hashtab_search(cladatum->permissions.table,
1732 name);
1733 if (!perdatum)
1734 return 0;
1735
1736 return 1U << (perdatum->value-1);
1737}
1738
Eric Paris9ee0c822010-06-11 12:37:05 -04001739static int range_read(struct policydb *p, void *fp)
1740{
1741 struct range_trans *rt = NULL;
1742 struct mls_range *r = NULL;
1743 int i, rc;
1744 __le32 buf[2];
1745 u32 nel;
1746
1747 if (p->policyvers < POLICYDB_VERSION_MLS)
1748 return 0;
1749
1750 rc = next_entry(buf, fp, sizeof(u32));
1751 if (rc)
1752 goto out;
1753
1754 nel = le32_to_cpu(buf[0]);
1755 for (i = 0; i < nel; i++) {
1756 rc = -ENOMEM;
1757 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1758 if (!rt)
1759 goto out;
1760
1761 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1762 if (rc)
1763 goto out;
1764
1765 rt->source_type = le32_to_cpu(buf[0]);
1766 rt->target_type = le32_to_cpu(buf[1]);
1767 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1768 rc = next_entry(buf, fp, sizeof(u32));
1769 if (rc)
1770 goto out;
1771 rt->target_class = le32_to_cpu(buf[0]);
1772 } else
1773 rt->target_class = p->process_class;
1774
1775 rc = -EINVAL;
1776 if (!policydb_type_isvalid(p, rt->source_type) ||
1777 !policydb_type_isvalid(p, rt->target_type) ||
1778 !policydb_class_isvalid(p, rt->target_class))
1779 goto out;
1780
1781 rc = -ENOMEM;
1782 r = kzalloc(sizeof(*r), GFP_KERNEL);
1783 if (!r)
1784 goto out;
1785
1786 rc = mls_read_range_helper(r, fp);
1787 if (rc)
1788 goto out;
1789
1790 rc = -EINVAL;
1791 if (!mls_range_isvalid(p, r)) {
1792 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1793 goto out;
1794 }
1795
1796 rc = hashtab_insert(p->range_tr, rt, r);
1797 if (rc)
1798 goto out;
1799
1800 rt = NULL;
1801 r = NULL;
1802 }
1803 rangetr_hash_eval(p->range_tr);
1804 rc = 0;
1805out:
1806 kfree(rt);
1807 kfree(r);
1808 return rc;
1809}
1810
Eric Paris652bb9b2011-02-01 11:05:40 -05001811static int filename_trans_read(struct policydb *p, void *fp)
1812{
1813 struct filename_trans *ft, *last;
1814 u32 nel, len;
1815 char *name;
1816 __le32 buf[4];
1817 int rc, i;
1818
1819 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1820 return 0;
1821
1822 rc = next_entry(buf, fp, sizeof(u32));
1823 if (rc)
1824 goto out;
1825 nel = le32_to_cpu(buf[0]);
1826
1827 printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1828
1829 last = p->filename_trans;
1830 while (last && last->next)
1831 last = last->next;
1832
1833 for (i = 0; i < nel; i++) {
1834 rc = -ENOMEM;
1835 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1836 if (!ft)
1837 goto out;
1838
1839 /* add it to the tail of the list */
1840 if (!last)
1841 p->filename_trans = ft;
1842 else
1843 last->next = ft;
1844 last = ft;
1845
1846 /* length of the path component string */
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc)
1849 goto out;
1850 len = le32_to_cpu(buf[0]);
1851
1852 rc = -ENOMEM;
1853 name = kmalloc(len + 1, GFP_KERNEL);
1854 if (!name)
1855 goto out;
1856
1857 ft->name = name;
1858
1859 /* path component string */
1860 rc = next_entry(name, fp, len);
1861 if (rc)
1862 goto out;
1863 name[len] = 0;
1864
1865 printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1866
1867 rc = next_entry(buf, fp, sizeof(u32) * 4);
1868 if (rc)
1869 goto out;
1870
1871 ft->stype = le32_to_cpu(buf[0]);
1872 ft->ttype = le32_to_cpu(buf[1]);
1873 ft->tclass = le32_to_cpu(buf[2]);
1874 ft->otype = le32_to_cpu(buf[3]);
1875 }
1876 rc = 0;
1877out:
1878 return rc;
1879}
1880
Eric Parisd1b43542010-07-21 12:50:57 -04001881static int genfs_read(struct policydb *p, void *fp)
1882{
1883 int i, j, rc;
1884 u32 nel, nel2, len, len2;
1885 __le32 buf[1];
1886 struct ocontext *l, *c;
1887 struct ocontext *newc = NULL;
1888 struct genfs *genfs_p, *genfs;
1889 struct genfs *newgenfs = NULL;
1890
1891 rc = next_entry(buf, fp, sizeof(u32));
1892 if (rc)
1893 goto out;
1894 nel = le32_to_cpu(buf[0]);
1895
1896 for (i = 0; i < nel; i++) {
1897 rc = next_entry(buf, fp, sizeof(u32));
1898 if (rc)
1899 goto out;
1900 len = le32_to_cpu(buf[0]);
1901
1902 rc = -ENOMEM;
1903 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1904 if (!newgenfs)
1905 goto out;
1906
1907 rc = -ENOMEM;
1908 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1909 if (!newgenfs->fstype)
1910 goto out;
1911
1912 rc = next_entry(newgenfs->fstype, fp, len);
1913 if (rc)
1914 goto out;
1915
1916 newgenfs->fstype[len] = 0;
1917
1918 for (genfs_p = NULL, genfs = p->genfs; genfs;
1919 genfs_p = genfs, genfs = genfs->next) {
1920 rc = -EINVAL;
1921 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1922 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1923 newgenfs->fstype);
1924 goto out;
1925 }
1926 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1927 break;
1928 }
1929 newgenfs->next = genfs;
1930 if (genfs_p)
1931 genfs_p->next = newgenfs;
1932 else
1933 p->genfs = newgenfs;
1934 genfs = newgenfs;
1935 newgenfs = NULL;
1936
1937 rc = next_entry(buf, fp, sizeof(u32));
1938 if (rc)
1939 goto out;
1940
1941 nel2 = le32_to_cpu(buf[0]);
1942 for (j = 0; j < nel2; j++) {
1943 rc = next_entry(buf, fp, sizeof(u32));
1944 if (rc)
1945 goto out;
1946 len = le32_to_cpu(buf[0]);
1947
1948 rc = -ENOMEM;
1949 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1950 if (!newc)
1951 goto out;
1952
1953 rc = -ENOMEM;
1954 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1955 if (!newc->u.name)
1956 goto out;
1957
1958 rc = next_entry(newc->u.name, fp, len);
1959 if (rc)
1960 goto out;
1961 newc->u.name[len] = 0;
1962
1963 rc = next_entry(buf, fp, sizeof(u32));
1964 if (rc)
1965 goto out;
1966
1967 newc->v.sclass = le32_to_cpu(buf[0]);
1968 rc = context_read_and_validate(&newc->context[0], p, fp);
1969 if (rc)
1970 goto out;
1971
1972 for (l = NULL, c = genfs->head; c;
1973 l = c, c = c->next) {
1974 rc = -EINVAL;
1975 if (!strcmp(newc->u.name, c->u.name) &&
1976 (!c->v.sclass || !newc->v.sclass ||
1977 newc->v.sclass == c->v.sclass)) {
1978 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1979 genfs->fstype, c->u.name);
1980 goto out;
1981 }
1982 len = strlen(newc->u.name);
1983 len2 = strlen(c->u.name);
1984 if (len > len2)
1985 break;
1986 }
1987
1988 newc->next = c;
1989 if (l)
1990 l->next = newc;
1991 else
1992 genfs->head = newc;
1993 newc = NULL;
1994 }
1995 }
1996 rc = 0;
1997out:
1998 if (newgenfs)
1999 kfree(newgenfs->fstype);
2000 kfree(newgenfs);
2001 ocontext_destroy(newc, OCON_FSUSE);
2002
2003 return rc;
2004}
2005
Eric Paris692a8a22010-07-21 12:51:03 -04002006static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2007 void *fp)
2008{
2009 int i, j, rc;
2010 u32 nel, len;
2011 __le32 buf[3];
2012 struct ocontext *l, *c;
2013 u32 nodebuf[8];
2014
2015 for (i = 0; i < info->ocon_num; i++) {
2016 rc = next_entry(buf, fp, sizeof(u32));
2017 if (rc)
2018 goto out;
2019 nel = le32_to_cpu(buf[0]);
2020
2021 l = NULL;
2022 for (j = 0; j < nel; j++) {
2023 rc = -ENOMEM;
2024 c = kzalloc(sizeof(*c), GFP_KERNEL);
2025 if (!c)
2026 goto out;
2027 if (l)
2028 l->next = c;
2029 else
2030 p->ocontexts[i] = c;
2031 l = c;
2032
2033 switch (i) {
2034 case OCON_ISID:
2035 rc = next_entry(buf, fp, sizeof(u32));
2036 if (rc)
2037 goto out;
2038
2039 c->sid[0] = le32_to_cpu(buf[0]);
2040 rc = context_read_and_validate(&c->context[0], p, fp);
2041 if (rc)
2042 goto out;
2043 break;
2044 case OCON_FS:
2045 case OCON_NETIF:
2046 rc = next_entry(buf, fp, sizeof(u32));
2047 if (rc)
2048 goto out;
2049 len = le32_to_cpu(buf[0]);
2050
2051 rc = -ENOMEM;
2052 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2053 if (!c->u.name)
2054 goto out;
2055
2056 rc = next_entry(c->u.name, fp, len);
2057 if (rc)
2058 goto out;
2059
2060 c->u.name[len] = 0;
2061 rc = context_read_and_validate(&c->context[0], p, fp);
2062 if (rc)
2063 goto out;
2064 rc = context_read_and_validate(&c->context[1], p, fp);
2065 if (rc)
2066 goto out;
2067 break;
2068 case OCON_PORT:
2069 rc = next_entry(buf, fp, sizeof(u32)*3);
2070 if (rc)
2071 goto out;
2072 c->u.port.protocol = le32_to_cpu(buf[0]);
2073 c->u.port.low_port = le32_to_cpu(buf[1]);
2074 c->u.port.high_port = le32_to_cpu(buf[2]);
2075 rc = context_read_and_validate(&c->context[0], p, fp);
2076 if (rc)
2077 goto out;
2078 break;
2079 case OCON_NODE:
2080 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2081 if (rc)
2082 goto out;
2083 c->u.node.addr = nodebuf[0]; /* network order */
2084 c->u.node.mask = nodebuf[1]; /* network order */
2085 rc = context_read_and_validate(&c->context[0], p, fp);
2086 if (rc)
2087 goto out;
2088 break;
2089 case OCON_FSUSE:
2090 rc = next_entry(buf, fp, sizeof(u32)*2);
2091 if (rc)
2092 goto out;
2093
2094 rc = -EINVAL;
2095 c->v.behavior = le32_to_cpu(buf[0]);
2096 if (c->v.behavior > SECURITY_FS_USE_NONE)
2097 goto out;
2098
2099 rc = -ENOMEM;
2100 len = le32_to_cpu(buf[1]);
2101 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2102 if (!c->u.name)
2103 goto out;
2104
2105 rc = next_entry(c->u.name, fp, len);
2106 if (rc)
2107 goto out;
2108 c->u.name[len] = 0;
2109 rc = context_read_and_validate(&c->context[0], p, fp);
2110 if (rc)
2111 goto out;
2112 break;
2113 case OCON_NODE6: {
2114 int k;
2115
2116 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2117 if (rc)
2118 goto out;
2119 for (k = 0; k < 4; k++)
2120 c->u.node6.addr[k] = nodebuf[k];
2121 for (k = 0; k < 4; k++)
2122 c->u.node6.mask[k] = nodebuf[k+4];
2123 rc = context_read_and_validate(&c->context[0], p, fp);
2124 if (rc)
2125 goto out;
2126 break;
2127 }
2128 }
2129 }
2130 }
2131 rc = 0;
2132out:
2133 return rc;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/*
2137 * Read the configuration data from a policy database binary
2138 * representation file into a policy database structure.
2139 */
2140int policydb_read(struct policydb *p, void *fp)
2141{
2142 struct role_allow *ra, *lra;
2143 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002145 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002146 u32 len, nprim, nel;
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 char *policydb_str;
2149 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 rc = policydb_init(p);
2152 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002153 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002156 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002157 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 goto bad;
2159
Eric Paris9398c7f2010-11-23 11:40:08 -05002160 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002161 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002162 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002164 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 goto bad;
2166 }
2167
Eric Paris9398c7f2010-11-23 11:40:08 -05002168 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002169 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002171 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 "match expected length %Zu\n",
2173 len, strlen(POLICYDB_STRING));
2174 goto bad;
2175 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002176
2177 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002178 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002180 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto bad;
2183 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002186 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002187 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 kfree(policydb_str);
2189 goto bad;
2190 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002191
2192 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002193 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002195 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 "my string %s\n", policydb_str, POLICYDB_STRING);
2197 kfree(policydb_str);
2198 goto bad;
2199 }
2200 /* Done with policydb_str. */
2201 kfree(policydb_str);
2202 policydb_str = NULL;
2203
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002204 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002206 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Eric Paris9398c7f2010-11-23 11:40:08 -05002209 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002210 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (p->policyvers < POLICYDB_VERSION_MIN ||
2212 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002213 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002214 "my version range %d-%d\n",
2215 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2216 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002219 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002220 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Eric Paris9398c7f2010-11-23 11:40:08 -05002222 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002224 printk(KERN_ERR "SELinux: security policydb version %d "
2225 "(MLS) not backwards compatible\n",
2226 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto bad;
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Eric Paris3f120702007-09-21 14:37:10 -04002230 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2231 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Eric Paris9398c7f2010-11-23 11:40:08 -05002233 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2234 rc = ebitmap_read(&p->policycaps, fp);
2235 if (rc)
2236 goto bad;
2237 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002238
Eric Paris9398c7f2010-11-23 11:40:08 -05002239 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2240 rc = ebitmap_read(&p->permissive_map, fp);
2241 if (rc)
2242 goto bad;
2243 }
Eric Paris64dbf072008-03-31 12:17:33 +11002244
Eric Paris9398c7f2010-11-23 11:40:08 -05002245 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 info = policydb_lookup_compat(p->policyvers);
2247 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002248 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 "for version %d\n", p->policyvers);
2250 goto bad;
2251 }
2252
Eric Paris9398c7f2010-11-23 11:40:08 -05002253 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002254 if (le32_to_cpu(buf[2]) != info->sym_num ||
2255 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002256 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002257 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2258 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 info->sym_num, info->ocon_num);
2260 goto bad;
2261 }
2262
2263 for (i = 0; i < info->sym_num; i++) {
2264 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002265 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto bad;
2267 nprim = le32_to_cpu(buf[0]);
2268 nel = le32_to_cpu(buf[1]);
2269 for (j = 0; j < nel; j++) {
2270 rc = read_f[i](p, p->symtab[i].table, fp);
2271 if (rc)
2272 goto bad;
2273 }
2274
2275 p->symtab[i].nprim = nprim;
2276 }
2277
Stephen Smalley45e54212007-11-07 10:08:00 -05002278 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (rc)
2280 goto bad;
2281
2282 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2283 rc = cond_read_list(p, fp);
2284 if (rc)
2285 goto bad;
2286 }
2287
2288 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002289 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto bad;
2291 nel = le32_to_cpu(buf[0]);
2292 ltr = NULL;
2293 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002294 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002295 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002296 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002298 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002300 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002303 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002305
2306 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 tr->role = le32_to_cpu(buf[0]);
2308 tr->type = le32_to_cpu(buf[1]);
2309 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002310 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2311 rc = next_entry(buf, fp, sizeof(u32));
2312 if (rc)
2313 goto bad;
2314 tr->tclass = le32_to_cpu(buf[0]);
2315 } else
2316 tr->tclass = p->process_class;
2317
Stephen Smalley45e54212007-11-07 10:08:00 -05002318 if (!policydb_role_isvalid(p, tr->role) ||
2319 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002320 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002321 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002322 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 ltr = tr;
2324 }
2325
2326 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002327 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto bad;
2329 nel = le32_to_cpu(buf[0]);
2330 lra = NULL;
2331 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002332 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002333 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002334 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002336 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002338 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002341 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002343
2344 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 ra->role = le32_to_cpu(buf[0]);
2346 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002347 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002348 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002349 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 lra = ra;
2351 }
2352
Eric Paris652bb9b2011-02-01 11:05:40 -05002353 rc = filename_trans_read(p, fp);
2354 if (rc)
2355 goto bad;
2356
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002357 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (rc)
2359 goto bad;
2360
Eric Paris9398c7f2010-11-23 11:40:08 -05002361 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002362 p->process_class = string_to_security_class(p, "process");
2363 if (!p->process_class)
2364 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002365
2366 rc = -EINVAL;
2367 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2368 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002369 if (!p->process_trans_perms)
2370 goto bad;
2371
Eric Paris692a8a22010-07-21 12:51:03 -04002372 rc = ocontext_read(p, info, fp);
2373 if (rc)
2374 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Eric Parisd1b43542010-07-21 12:50:57 -04002376 rc = genfs_read(p, fp);
2377 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Eric Paris9ee0c822010-06-11 12:37:05 -04002380 rc = range_read(p, fp);
2381 if (rc)
2382 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Eric Paris6371dcd2010-07-29 23:02:34 -04002384 rc = -ENOMEM;
2385 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2386 p->p_types.nprim,
2387 GFP_KERNEL | __GFP_ZERO);
2388 if (!p->type_attr_map_array)
2389 goto bad;
2390
2391 /* preallocate so we don't have to worry about the put ever failing */
2392 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2393 GFP_KERNEL | __GFP_ZERO);
2394 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002395 goto bad;
2396
2397 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002398 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2399
2400 BUG_ON(!e);
2401 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002402 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002403 rc = ebitmap_read(e, fp);
2404 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002405 goto bad;
2406 }
2407 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002408 rc = ebitmap_set_bit(e, i, 1);
2409 if (rc)
2410 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002411 }
2412
KaiGai Koheid9250de2008-08-28 16:35:57 +09002413 rc = policydb_bounds_sanity_check(p);
2414 if (rc)
2415 goto bad;
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 rc = 0;
2418out:
2419 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 policydb_destroy(p);
2422 goto out;
2423}
Eric Pariscee74f42010-10-13 17:50:25 -04002424
2425/*
2426 * Write a MLS level structure to a policydb binary
2427 * representation file.
2428 */
2429static int mls_write_level(struct mls_level *l, void *fp)
2430{
2431 __le32 buf[1];
2432 int rc;
2433
2434 buf[0] = cpu_to_le32(l->sens);
2435 rc = put_entry(buf, sizeof(u32), 1, fp);
2436 if (rc)
2437 return rc;
2438
2439 rc = ebitmap_write(&l->cat, fp);
2440 if (rc)
2441 return rc;
2442
2443 return 0;
2444}
2445
2446/*
2447 * Write a MLS range structure to a policydb binary
2448 * representation file.
2449 */
2450static int mls_write_range_helper(struct mls_range *r, void *fp)
2451{
2452 __le32 buf[3];
2453 size_t items;
2454 int rc, eq;
2455
2456 eq = mls_level_eq(&r->level[1], &r->level[0]);
2457
2458 if (eq)
2459 items = 2;
2460 else
2461 items = 3;
2462 buf[0] = cpu_to_le32(items-1);
2463 buf[1] = cpu_to_le32(r->level[0].sens);
2464 if (!eq)
2465 buf[2] = cpu_to_le32(r->level[1].sens);
2466
2467 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2468
2469 rc = put_entry(buf, sizeof(u32), items, fp);
2470 if (rc)
2471 return rc;
2472
2473 rc = ebitmap_write(&r->level[0].cat, fp);
2474 if (rc)
2475 return rc;
2476 if (!eq) {
2477 rc = ebitmap_write(&r->level[1].cat, fp);
2478 if (rc)
2479 return rc;
2480 }
2481
2482 return 0;
2483}
2484
2485static int sens_write(void *vkey, void *datum, void *ptr)
2486{
2487 char *key = vkey;
2488 struct level_datum *levdatum = datum;
2489 struct policy_data *pd = ptr;
2490 void *fp = pd->fp;
2491 __le32 buf[2];
2492 size_t len;
2493 int rc;
2494
2495 len = strlen(key);
2496 buf[0] = cpu_to_le32(len);
2497 buf[1] = cpu_to_le32(levdatum->isalias);
2498 rc = put_entry(buf, sizeof(u32), 2, fp);
2499 if (rc)
2500 return rc;
2501
2502 rc = put_entry(key, 1, len, fp);
2503 if (rc)
2504 return rc;
2505
2506 rc = mls_write_level(levdatum->level, fp);
2507 if (rc)
2508 return rc;
2509
2510 return 0;
2511}
2512
2513static int cat_write(void *vkey, void *datum, void *ptr)
2514{
2515 char *key = vkey;
2516 struct cat_datum *catdatum = datum;
2517 struct policy_data *pd = ptr;
2518 void *fp = pd->fp;
2519 __le32 buf[3];
2520 size_t len;
2521 int rc;
2522
2523 len = strlen(key);
2524 buf[0] = cpu_to_le32(len);
2525 buf[1] = cpu_to_le32(catdatum->value);
2526 buf[2] = cpu_to_le32(catdatum->isalias);
2527 rc = put_entry(buf, sizeof(u32), 3, fp);
2528 if (rc)
2529 return rc;
2530
2531 rc = put_entry(key, 1, len, fp);
2532 if (rc)
2533 return rc;
2534
2535 return 0;
2536}
2537
Harry Ciaoc900ff32011-03-25 13:52:00 +08002538static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002539{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002540 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002541 struct role_trans *tr;
2542 u32 buf[3];
2543 size_t nel;
2544 int rc;
2545
2546 nel = 0;
2547 for (tr = r; tr; tr = tr->next)
2548 nel++;
2549 buf[0] = cpu_to_le32(nel);
2550 rc = put_entry(buf, sizeof(u32), 1, fp);
2551 if (rc)
2552 return rc;
2553 for (tr = r; tr; tr = tr->next) {
2554 buf[0] = cpu_to_le32(tr->role);
2555 buf[1] = cpu_to_le32(tr->type);
2556 buf[2] = cpu_to_le32(tr->new_role);
2557 rc = put_entry(buf, sizeof(u32), 3, fp);
2558 if (rc)
2559 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002560 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2561 buf[0] = cpu_to_le32(tr->tclass);
2562 rc = put_entry(buf, sizeof(u32), 1, fp);
2563 if (rc)
2564 return rc;
2565 }
Eric Pariscee74f42010-10-13 17:50:25 -04002566 }
2567
2568 return 0;
2569}
2570
2571static int role_allow_write(struct role_allow *r, void *fp)
2572{
2573 struct role_allow *ra;
2574 u32 buf[2];
2575 size_t nel;
2576 int rc;
2577
2578 nel = 0;
2579 for (ra = r; ra; ra = ra->next)
2580 nel++;
2581 buf[0] = cpu_to_le32(nel);
2582 rc = put_entry(buf, sizeof(u32), 1, fp);
2583 if (rc)
2584 return rc;
2585 for (ra = r; ra; ra = ra->next) {
2586 buf[0] = cpu_to_le32(ra->role);
2587 buf[1] = cpu_to_le32(ra->new_role);
2588 rc = put_entry(buf, sizeof(u32), 2, fp);
2589 if (rc)
2590 return rc;
2591 }
2592 return 0;
2593}
2594
2595/*
2596 * Write a security context structure
2597 * to a policydb binary representation file.
2598 */
2599static int context_write(struct policydb *p, struct context *c,
2600 void *fp)
2601{
2602 int rc;
2603 __le32 buf[3];
2604
2605 buf[0] = cpu_to_le32(c->user);
2606 buf[1] = cpu_to_le32(c->role);
2607 buf[2] = cpu_to_le32(c->type);
2608
2609 rc = put_entry(buf, sizeof(u32), 3, fp);
2610 if (rc)
2611 return rc;
2612
2613 rc = mls_write_range_helper(&c->range, fp);
2614 if (rc)
2615 return rc;
2616
2617 return 0;
2618}
2619
2620/*
2621 * The following *_write functions are used to
2622 * write the symbol data to a policy database
2623 * binary representation file.
2624 */
2625
2626static int perm_write(void *vkey, void *datum, void *fp)
2627{
2628 char *key = vkey;
2629 struct perm_datum *perdatum = datum;
2630 __le32 buf[2];
2631 size_t len;
2632 int rc;
2633
2634 len = strlen(key);
2635 buf[0] = cpu_to_le32(len);
2636 buf[1] = cpu_to_le32(perdatum->value);
2637 rc = put_entry(buf, sizeof(u32), 2, fp);
2638 if (rc)
2639 return rc;
2640
2641 rc = put_entry(key, 1, len, fp);
2642 if (rc)
2643 return rc;
2644
2645 return 0;
2646}
2647
2648static int common_write(void *vkey, void *datum, void *ptr)
2649{
2650 char *key = vkey;
2651 struct common_datum *comdatum = datum;
2652 struct policy_data *pd = ptr;
2653 void *fp = pd->fp;
2654 __le32 buf[4];
2655 size_t len;
2656 int rc;
2657
2658 len = strlen(key);
2659 buf[0] = cpu_to_le32(len);
2660 buf[1] = cpu_to_le32(comdatum->value);
2661 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2662 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2663 rc = put_entry(buf, sizeof(u32), 4, fp);
2664 if (rc)
2665 return rc;
2666
2667 rc = put_entry(key, 1, len, fp);
2668 if (rc)
2669 return rc;
2670
2671 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2672 if (rc)
2673 return rc;
2674
2675 return 0;
2676}
2677
2678static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2679 void *fp)
2680{
2681 struct constraint_node *c;
2682 struct constraint_expr *e;
2683 __le32 buf[3];
2684 u32 nel;
2685 int rc;
2686
2687 for (c = node; c; c = c->next) {
2688 nel = 0;
2689 for (e = c->expr; e; e = e->next)
2690 nel++;
2691 buf[0] = cpu_to_le32(c->permissions);
2692 buf[1] = cpu_to_le32(nel);
2693 rc = put_entry(buf, sizeof(u32), 2, fp);
2694 if (rc)
2695 return rc;
2696 for (e = c->expr; e; e = e->next) {
2697 buf[0] = cpu_to_le32(e->expr_type);
2698 buf[1] = cpu_to_le32(e->attr);
2699 buf[2] = cpu_to_le32(e->op);
2700 rc = put_entry(buf, sizeof(u32), 3, fp);
2701 if (rc)
2702 return rc;
2703
2704 switch (e->expr_type) {
2705 case CEXPR_NAMES:
2706 rc = ebitmap_write(&e->names, fp);
2707 if (rc)
2708 return rc;
2709 break;
2710 default:
2711 break;
2712 }
2713 }
2714 }
2715
2716 return 0;
2717}
2718
2719static int class_write(void *vkey, void *datum, void *ptr)
2720{
2721 char *key = vkey;
2722 struct class_datum *cladatum = datum;
2723 struct policy_data *pd = ptr;
2724 void *fp = pd->fp;
2725 struct policydb *p = pd->p;
2726 struct constraint_node *c;
2727 __le32 buf[6];
2728 u32 ncons;
2729 size_t len, len2;
2730 int rc;
2731
2732 len = strlen(key);
2733 if (cladatum->comkey)
2734 len2 = strlen(cladatum->comkey);
2735 else
2736 len2 = 0;
2737
2738 ncons = 0;
2739 for (c = cladatum->constraints; c; c = c->next)
2740 ncons++;
2741
2742 buf[0] = cpu_to_le32(len);
2743 buf[1] = cpu_to_le32(len2);
2744 buf[2] = cpu_to_le32(cladatum->value);
2745 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2746 if (cladatum->permissions.table)
2747 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2748 else
2749 buf[4] = 0;
2750 buf[5] = cpu_to_le32(ncons);
2751 rc = put_entry(buf, sizeof(u32), 6, fp);
2752 if (rc)
2753 return rc;
2754
2755 rc = put_entry(key, 1, len, fp);
2756 if (rc)
2757 return rc;
2758
2759 if (cladatum->comkey) {
2760 rc = put_entry(cladatum->comkey, 1, len2, fp);
2761 if (rc)
2762 return rc;
2763 }
2764
2765 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2766 if (rc)
2767 return rc;
2768
2769 rc = write_cons_helper(p, cladatum->constraints, fp);
2770 if (rc)
2771 return rc;
2772
2773 /* write out the validatetrans rule */
2774 ncons = 0;
2775 for (c = cladatum->validatetrans; c; c = c->next)
2776 ncons++;
2777
2778 buf[0] = cpu_to_le32(ncons);
2779 rc = put_entry(buf, sizeof(u32), 1, fp);
2780 if (rc)
2781 return rc;
2782
2783 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2784 if (rc)
2785 return rc;
2786
2787 return 0;
2788}
2789
2790static int role_write(void *vkey, void *datum, void *ptr)
2791{
2792 char *key = vkey;
2793 struct role_datum *role = datum;
2794 struct policy_data *pd = ptr;
2795 void *fp = pd->fp;
2796 struct policydb *p = pd->p;
2797 __le32 buf[3];
2798 size_t items, len;
2799 int rc;
2800
2801 len = strlen(key);
2802 items = 0;
2803 buf[items++] = cpu_to_le32(len);
2804 buf[items++] = cpu_to_le32(role->value);
2805 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2806 buf[items++] = cpu_to_le32(role->bounds);
2807
2808 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2809
2810 rc = put_entry(buf, sizeof(u32), items, fp);
2811 if (rc)
2812 return rc;
2813
2814 rc = put_entry(key, 1, len, fp);
2815 if (rc)
2816 return rc;
2817
2818 rc = ebitmap_write(&role->dominates, fp);
2819 if (rc)
2820 return rc;
2821
2822 rc = ebitmap_write(&role->types, fp);
2823 if (rc)
2824 return rc;
2825
2826 return 0;
2827}
2828
2829static int type_write(void *vkey, void *datum, void *ptr)
2830{
2831 char *key = vkey;
2832 struct type_datum *typdatum = datum;
2833 struct policy_data *pd = ptr;
2834 struct policydb *p = pd->p;
2835 void *fp = pd->fp;
2836 __le32 buf[4];
2837 int rc;
2838 size_t items, len;
2839
2840 len = strlen(key);
2841 items = 0;
2842 buf[items++] = cpu_to_le32(len);
2843 buf[items++] = cpu_to_le32(typdatum->value);
2844 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2845 u32 properties = 0;
2846
2847 if (typdatum->primary)
2848 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2849
2850 if (typdatum->attribute)
2851 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2852
2853 buf[items++] = cpu_to_le32(properties);
2854 buf[items++] = cpu_to_le32(typdatum->bounds);
2855 } else {
2856 buf[items++] = cpu_to_le32(typdatum->primary);
2857 }
2858 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2859 rc = put_entry(buf, sizeof(u32), items, fp);
2860 if (rc)
2861 return rc;
2862
2863 rc = put_entry(key, 1, len, fp);
2864 if (rc)
2865 return rc;
2866
2867 return 0;
2868}
2869
2870static int user_write(void *vkey, void *datum, void *ptr)
2871{
2872 char *key = vkey;
2873 struct user_datum *usrdatum = datum;
2874 struct policy_data *pd = ptr;
2875 struct policydb *p = pd->p;
2876 void *fp = pd->fp;
2877 __le32 buf[3];
2878 size_t items, len;
2879 int rc;
2880
2881 len = strlen(key);
2882 items = 0;
2883 buf[items++] = cpu_to_le32(len);
2884 buf[items++] = cpu_to_le32(usrdatum->value);
2885 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2886 buf[items++] = cpu_to_le32(usrdatum->bounds);
2887 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2888 rc = put_entry(buf, sizeof(u32), items, fp);
2889 if (rc)
2890 return rc;
2891
2892 rc = put_entry(key, 1, len, fp);
2893 if (rc)
2894 return rc;
2895
2896 rc = ebitmap_write(&usrdatum->roles, fp);
2897 if (rc)
2898 return rc;
2899
2900 rc = mls_write_range_helper(&usrdatum->range, fp);
2901 if (rc)
2902 return rc;
2903
2904 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2905 if (rc)
2906 return rc;
2907
2908 return 0;
2909}
2910
2911static int (*write_f[SYM_NUM]) (void *key, void *datum,
2912 void *datap) =
2913{
2914 common_write,
2915 class_write,
2916 role_write,
2917 type_write,
2918 user_write,
2919 cond_write_bool,
2920 sens_write,
2921 cat_write,
2922};
2923
2924static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2925 void *fp)
2926{
2927 unsigned int i, j, rc;
2928 size_t nel, len;
2929 __le32 buf[3];
2930 u32 nodebuf[8];
2931 struct ocontext *c;
2932 for (i = 0; i < info->ocon_num; i++) {
2933 nel = 0;
2934 for (c = p->ocontexts[i]; c; c = c->next)
2935 nel++;
2936 buf[0] = cpu_to_le32(nel);
2937 rc = put_entry(buf, sizeof(u32), 1, fp);
2938 if (rc)
2939 return rc;
2940 for (c = p->ocontexts[i]; c; c = c->next) {
2941 switch (i) {
2942 case OCON_ISID:
2943 buf[0] = cpu_to_le32(c->sid[0]);
2944 rc = put_entry(buf, sizeof(u32), 1, fp);
2945 if (rc)
2946 return rc;
2947 rc = context_write(p, &c->context[0], fp);
2948 if (rc)
2949 return rc;
2950 break;
2951 case OCON_FS:
2952 case OCON_NETIF:
2953 len = strlen(c->u.name);
2954 buf[0] = cpu_to_le32(len);
2955 rc = put_entry(buf, sizeof(u32), 1, fp);
2956 if (rc)
2957 return rc;
2958 rc = put_entry(c->u.name, 1, len, fp);
2959 if (rc)
2960 return rc;
2961 rc = context_write(p, &c->context[0], fp);
2962 if (rc)
2963 return rc;
2964 rc = context_write(p, &c->context[1], fp);
2965 if (rc)
2966 return rc;
2967 break;
2968 case OCON_PORT:
2969 buf[0] = cpu_to_le32(c->u.port.protocol);
2970 buf[1] = cpu_to_le32(c->u.port.low_port);
2971 buf[2] = cpu_to_le32(c->u.port.high_port);
2972 rc = put_entry(buf, sizeof(u32), 3, fp);
2973 if (rc)
2974 return rc;
2975 rc = context_write(p, &c->context[0], fp);
2976 if (rc)
2977 return rc;
2978 break;
2979 case OCON_NODE:
2980 nodebuf[0] = c->u.node.addr; /* network order */
2981 nodebuf[1] = c->u.node.mask; /* network order */
2982 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2983 if (rc)
2984 return rc;
2985 rc = context_write(p, &c->context[0], fp);
2986 if (rc)
2987 return rc;
2988 break;
2989 case OCON_FSUSE:
2990 buf[0] = cpu_to_le32(c->v.behavior);
2991 len = strlen(c->u.name);
2992 buf[1] = cpu_to_le32(len);
2993 rc = put_entry(buf, sizeof(u32), 2, fp);
2994 if (rc)
2995 return rc;
2996 rc = put_entry(c->u.name, 1, len, fp);
2997 if (rc)
2998 return rc;
2999 rc = context_write(p, &c->context[0], fp);
3000 if (rc)
3001 return rc;
3002 break;
3003 case OCON_NODE6:
3004 for (j = 0; j < 4; j++)
3005 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3006 for (j = 0; j < 4; j++)
3007 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3008 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3009 if (rc)
3010 return rc;
3011 rc = context_write(p, &c->context[0], fp);
3012 if (rc)
3013 return rc;
3014 break;
3015 }
3016 }
3017 }
3018 return 0;
3019}
3020
3021static int genfs_write(struct policydb *p, void *fp)
3022{
3023 struct genfs *genfs;
3024 struct ocontext *c;
3025 size_t len;
3026 __le32 buf[1];
3027 int rc;
3028
3029 len = 0;
3030 for (genfs = p->genfs; genfs; genfs = genfs->next)
3031 len++;
3032 buf[0] = cpu_to_le32(len);
3033 rc = put_entry(buf, sizeof(u32), 1, fp);
3034 if (rc)
3035 return rc;
3036 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3037 len = strlen(genfs->fstype);
3038 buf[0] = cpu_to_le32(len);
3039 rc = put_entry(buf, sizeof(u32), 1, fp);
3040 if (rc)
3041 return rc;
3042 rc = put_entry(genfs->fstype, 1, len, fp);
3043 if (rc)
3044 return rc;
3045 len = 0;
3046 for (c = genfs->head; c; c = c->next)
3047 len++;
3048 buf[0] = cpu_to_le32(len);
3049 rc = put_entry(buf, sizeof(u32), 1, fp);
3050 if (rc)
3051 return rc;
3052 for (c = genfs->head; c; c = c->next) {
3053 len = strlen(c->u.name);
3054 buf[0] = cpu_to_le32(len);
3055 rc = put_entry(buf, sizeof(u32), 1, fp);
3056 if (rc)
3057 return rc;
3058 rc = put_entry(c->u.name, 1, len, fp);
3059 if (rc)
3060 return rc;
3061 buf[0] = cpu_to_le32(c->v.sclass);
3062 rc = put_entry(buf, sizeof(u32), 1, fp);
3063 if (rc)
3064 return rc;
3065 rc = context_write(p, &c->context[0], fp);
3066 if (rc)
3067 return rc;
3068 }
3069 }
3070 return 0;
3071}
3072
3073static int range_count(void *key, void *data, void *ptr)
3074{
3075 int *cnt = ptr;
3076 *cnt = *cnt + 1;
3077
3078 return 0;
3079}
3080
3081static int range_write_helper(void *key, void *data, void *ptr)
3082{
3083 __le32 buf[2];
3084 struct range_trans *rt = key;
3085 struct mls_range *r = data;
3086 struct policy_data *pd = ptr;
3087 void *fp = pd->fp;
3088 struct policydb *p = pd->p;
3089 int rc;
3090
3091 buf[0] = cpu_to_le32(rt->source_type);
3092 buf[1] = cpu_to_le32(rt->target_type);
3093 rc = put_entry(buf, sizeof(u32), 2, fp);
3094 if (rc)
3095 return rc;
3096 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3097 buf[0] = cpu_to_le32(rt->target_class);
3098 rc = put_entry(buf, sizeof(u32), 1, fp);
3099 if (rc)
3100 return rc;
3101 }
3102 rc = mls_write_range_helper(r, fp);
3103 if (rc)
3104 return rc;
3105
3106 return 0;
3107}
3108
3109static int range_write(struct policydb *p, void *fp)
3110{
3111 size_t nel;
3112 __le32 buf[1];
3113 int rc;
3114 struct policy_data pd;
3115
3116 pd.p = p;
3117 pd.fp = fp;
3118
3119 /* count the number of entries in the hashtab */
3120 nel = 0;
3121 rc = hashtab_map(p->range_tr, range_count, &nel);
3122 if (rc)
3123 return rc;
3124
3125 buf[0] = cpu_to_le32(nel);
3126 rc = put_entry(buf, sizeof(u32), 1, fp);
3127 if (rc)
3128 return rc;
3129
3130 /* actually write all of the entries */
3131 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3132 if (rc)
3133 return rc;
3134
3135 return 0;
3136}
3137
Eric Paris652bb9b2011-02-01 11:05:40 -05003138static int filename_trans_write(struct policydb *p, void *fp)
3139{
3140 struct filename_trans *ft;
3141 u32 len, nel = 0;
3142 __le32 buf[4];
3143 int rc;
3144
3145 for (ft = p->filename_trans; ft; ft = ft->next)
3146 nel++;
3147
3148 buf[0] = cpu_to_le32(nel);
3149 rc = put_entry(buf, sizeof(u32), 1, fp);
3150 if (rc)
3151 return rc;
3152
3153 for (ft = p->filename_trans; ft; ft = ft->next) {
3154 len = strlen(ft->name);
3155 buf[0] = cpu_to_le32(len);
3156 rc = put_entry(buf, sizeof(u32), 1, fp);
3157 if (rc)
3158 return rc;
3159
3160 rc = put_entry(ft->name, sizeof(char), len, fp);
3161 if (rc)
3162 return rc;
3163
3164 buf[0] = ft->stype;
3165 buf[1] = ft->ttype;
3166 buf[2] = ft->tclass;
3167 buf[3] = ft->otype;
3168
3169 rc = put_entry(buf, sizeof(u32), 4, fp);
3170 if (rc)
3171 return rc;
3172 }
3173 return 0;
3174}
Eric Pariscee74f42010-10-13 17:50:25 -04003175/*
3176 * Write the configuration data in a policy database
3177 * structure to a policy database binary representation
3178 * file.
3179 */
3180int policydb_write(struct policydb *p, void *fp)
3181{
3182 unsigned int i, num_syms;
3183 int rc;
3184 __le32 buf[4];
3185 u32 config;
3186 size_t len;
3187 struct policydb_compat_info *info;
3188
3189 /*
3190 * refuse to write policy older than compressed avtab
3191 * to simplify the writer. There are other tests dropped
3192 * since we assume this throughout the writer code. Be
3193 * careful if you ever try to remove this restriction
3194 */
3195 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3196 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3197 " Because it is less than version %d\n", p->policyvers,
3198 POLICYDB_VERSION_AVTAB);
3199 return -EINVAL;
3200 }
3201
3202 config = 0;
3203 if (p->mls_enabled)
3204 config |= POLICYDB_CONFIG_MLS;
3205
3206 if (p->reject_unknown)
3207 config |= REJECT_UNKNOWN;
3208 if (p->allow_unknown)
3209 config |= ALLOW_UNKNOWN;
3210
3211 /* Write the magic number and string identifiers. */
3212 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3213 len = strlen(POLICYDB_STRING);
3214 buf[1] = cpu_to_le32(len);
3215 rc = put_entry(buf, sizeof(u32), 2, fp);
3216 if (rc)
3217 return rc;
3218 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3219 if (rc)
3220 return rc;
3221
3222 /* Write the version, config, and table sizes. */
3223 info = policydb_lookup_compat(p->policyvers);
3224 if (!info) {
3225 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3226 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003227 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003228 }
3229
3230 buf[0] = cpu_to_le32(p->policyvers);
3231 buf[1] = cpu_to_le32(config);
3232 buf[2] = cpu_to_le32(info->sym_num);
3233 buf[3] = cpu_to_le32(info->ocon_num);
3234
3235 rc = put_entry(buf, sizeof(u32), 4, fp);
3236 if (rc)
3237 return rc;
3238
3239 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3240 rc = ebitmap_write(&p->policycaps, fp);
3241 if (rc)
3242 return rc;
3243 }
3244
3245 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3246 rc = ebitmap_write(&p->permissive_map, fp);
3247 if (rc)
3248 return rc;
3249 }
3250
3251 num_syms = info->sym_num;
3252 for (i = 0; i < num_syms; i++) {
3253 struct policy_data pd;
3254
3255 pd.fp = fp;
3256 pd.p = p;
3257
3258 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3259 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3260
3261 rc = put_entry(buf, sizeof(u32), 2, fp);
3262 if (rc)
3263 return rc;
3264 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3265 if (rc)
3266 return rc;
3267 }
3268
3269 rc = avtab_write(p, &p->te_avtab, fp);
3270 if (rc)
3271 return rc;
3272
3273 rc = cond_write_list(p, p->cond_list, fp);
3274 if (rc)
3275 return rc;
3276
Harry Ciaoc900ff32011-03-25 13:52:00 +08003277 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003278 if (rc)
3279 return rc;
3280
3281 rc = role_allow_write(p->role_allow, fp);
3282 if (rc)
3283 return rc;
3284
Eric Paris652bb9b2011-02-01 11:05:40 -05003285 rc = filename_trans_write(p, fp);
3286 if (rc)
3287 return rc;
3288
Eric Pariscee74f42010-10-13 17:50:25 -04003289 rc = ocontext_write(p, info, fp);
3290 if (rc)
3291 return rc;
3292
3293 rc = genfs_write(p, fp);
3294 if (rc)
3295 return rc;
3296
3297 rc = range_write(p, fp);
3298 if (rc)
3299 return rc;
3300
3301 for (i = 0; i < p->p_types.nprim; i++) {
3302 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3303
3304 BUG_ON(!e);
3305 rc = ebitmap_write(e, fp);
3306 if (rc)
3307 return rc;
3308 }
3309
3310 return 0;
3311}