blob: ca7a7231b5a23f249dc9a74322ce9f38865a5a56 [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
Eric Paris03a4c012011-04-28 15:11:21 -0400243 ebitmap_init(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500244 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100245 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500246
Eric Paris9398c7f2010-11-23 11:40:08 -0500247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 for (i = 0; i < SYM_NUM; i++)
250 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500251 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*
255 * The following *_index functions are used to
256 * define the val_to_name and val_to_struct arrays
257 * in a policy database structure. The val_to_name
258 * arrays are used when converting security context
259 * structures into string representations. The
260 * val_to_struct arrays are used when the attributes
261 * of a class, role, or user are needed.
262 */
263
264static int common_index(void *key, void *datum, void *datap)
265{
266 struct policydb *p;
267 struct common_datum *comdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500268 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 comdatum = datum;
271 p = datap;
272 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
273 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500274
275 fa = p->sym_val_to_name[SYM_COMMONS];
276 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
277 GFP_KERNEL | __GFP_ZERO))
278 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
281
282static int class_index(void *key, void *datum, void *datap)
283{
284 struct policydb *p;
285 struct class_datum *cladatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500286 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 cladatum = datum;
289 p = datap;
290 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
291 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500292 fa = p->sym_val_to_name[SYM_CLASSES];
293 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
294 GFP_KERNEL | __GFP_ZERO))
295 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 p->class_val_to_struct[cladatum->value - 1] = cladatum;
297 return 0;
298}
299
300static int role_index(void *key, void *datum, void *datap)
301{
302 struct policydb *p;
303 struct role_datum *role;
Eric Parisac76c05b2010-11-29 15:47:09 -0500304 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 role = datum;
307 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900308 if (!role->value
309 || role->value > p->p_roles.nprim
310 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500312
313 fa = p->sym_val_to_name[SYM_ROLES];
314 if (flex_array_put_ptr(fa, role->value - 1, key,
315 GFP_KERNEL | __GFP_ZERO))
316 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 p->role_val_to_struct[role->value - 1] = role;
318 return 0;
319}
320
321static int type_index(void *key, void *datum, void *datap)
322{
323 struct policydb *p;
324 struct type_datum *typdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500325 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 typdatum = datum;
328 p = datap;
329
330 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900331 if (!typdatum->value
332 || typdatum->value > p->p_types.nprim
333 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500335 fa = p->sym_val_to_name[SYM_TYPES];
336 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
337 GFP_KERNEL | __GFP_ZERO))
338 BUG();
339
340 fa = p->type_val_to_struct_array;
341 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500342 GFP_KERNEL | __GFP_ZERO))
343 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 return 0;
347}
348
349static int user_index(void *key, void *datum, void *datap)
350{
351 struct policydb *p;
352 struct user_datum *usrdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500353 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 usrdatum = datum;
356 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900357 if (!usrdatum->value
358 || usrdatum->value > p->p_users.nprim
359 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500361
362 fa = p->sym_val_to_name[SYM_USERS];
363 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
364 GFP_KERNEL | __GFP_ZERO))
365 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
367 return 0;
368}
369
370static int sens_index(void *key, void *datum, void *datap)
371{
372 struct policydb *p;
373 struct level_datum *levdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500374 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 levdatum = datum;
377 p = datap;
378
379 if (!levdatum->isalias) {
380 if (!levdatum->level->sens ||
381 levdatum->level->sens > p->p_levels.nprim)
382 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500383 fa = p->sym_val_to_name[SYM_LEVELS];
384 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
385 GFP_KERNEL | __GFP_ZERO))
386 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 return 0;
390}
391
392static int cat_index(void *key, void *datum, void *datap)
393{
394 struct policydb *p;
395 struct cat_datum *catdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500396 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 catdatum = datum;
399 p = datap;
400
401 if (!catdatum->isalias) {
402 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
403 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500404 fa = p->sym_val_to_name[SYM_CATS];
405 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
406 GFP_KERNEL | __GFP_ZERO))
407 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 return 0;
411}
412
413static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
414{
415 common_index,
416 class_index,
417 role_index,
418 type_index,
419 user_index,
420 cond_index_bool,
421 sens_index,
422 cat_index,
423};
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#ifdef DEBUG_HASHES
Eric Parisbe30b162011-04-28 15:11:21 -0400426static void hash_eval(struct hashtab *h, const char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500427{
428 struct hashtab_info info;
429
430 hashtab_stat(h, &info);
Eric Parisbe30b162011-04-28 15:11:21 -0400431 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
432 "longest chain length %d\n", hash_name, h->nel,
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500433 info.slots_used, h->size, info.max_chain_len);
434}
Eric Parisbe30b162011-04-28 15:11:21 -0400435
436static void symtab_hash_eval(struct symtab *s)
437{
438 int i;
439
440 for (i = 0; i < SYM_NUM; i++)
441 hash_eval(s[i].table, symtab_name[i]);
442}
443
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500444#else
Eric Parisbe30b162011-04-28 15:11:21 -0400445static inline void hash_eval(struct hashtab *h, char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500446{
447}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#endif
449
450/*
451 * Define the other val_to_name and val_to_struct arrays
452 * in a policy database structure.
453 *
454 * Caller must clean up on failure.
455 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500456static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Eric Paris9398c7f2010-11-23 11:40:08 -0500458 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
James Morris454d9722008-02-26 20:42:02 +1100460 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100462 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 printk(", %d sens, %d cats", p->p_levels.nprim,
464 p->p_cats.nprim);
465 printk("\n");
466
James Morris454d9722008-02-26 20:42:02 +1100467 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 p->p_classes.nprim, p->te_avtab.nel);
469
470#ifdef DEBUG_HASHES
471 avtab_hash_eval(&p->te_avtab, "rules");
472 symtab_hash_eval(p->symtab);
473#endif
474
Eric Paris9398c7f2010-11-23 11:40:08 -0500475 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500476 p->class_val_to_struct =
477 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
478 GFP_KERNEL);
479 if (!p->class_val_to_struct)
480 goto out;
481
482 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 p->role_val_to_struct =
484 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400485 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500486 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Eric Paris9398c7f2010-11-23 11:40:08 -0500489 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 p->user_val_to_struct =
491 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400492 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500493 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Paris23bdecb2010-11-29 15:47:09 -0500496 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500497 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500498 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
499 p->p_types.nprim,
500 GFP_KERNEL | __GFP_ZERO);
501 if (!p->type_val_to_struct_array)
502 goto out;
503
504 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
505 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
506 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900507 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900508
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300509 rc = cond_init_bool_indexes(p);
510 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500513 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500514 rc = -ENOMEM;
Eric Parisac76c05b2010-11-29 15:47:09 -0500515 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
516 p->symtab[i].nprim,
517 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500518 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
Eric Parisac76c05b2010-11-29 15:47:09 -0500520
521 rc = flex_array_prealloc(p->sym_val_to_name[i],
522 0, p->symtab[i].nprim - 1,
523 GFP_KERNEL | __GFP_ZERO);
524 if (rc)
525 goto out;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
528 if (rc)
529 goto out;
530 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500531 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532out:
533 return rc;
534}
535
536/*
537 * The following *_destroy functions are used to
538 * free any memory allocated for each kind of
539 * symbol data in the policy database.
540 */
541
542static int perm_destroy(void *key, void *datum, void *p)
543{
544 kfree(key);
545 kfree(datum);
546 return 0;
547}
548
549static int common_destroy(void *key, void *datum, void *p)
550{
551 struct common_datum *comdatum;
552
553 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500554 if (datum) {
555 comdatum = datum;
556 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
557 hashtab_destroy(comdatum->permissions.table);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 kfree(datum);
560 return 0;
561}
562
James Morris6cbda6b2006-11-29 16:50:27 -0500563static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct class_datum *cladatum;
566 struct constraint_node *constraint, *ctemp;
567 struct constraint_expr *e, *etmp;
568
569 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500570 if (datum) {
571 cladatum = datum;
572 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
573 hashtab_destroy(cladatum->permissions.table);
574 constraint = cladatum->constraints;
575 while (constraint) {
576 e = constraint->expr;
577 while (e) {
578 ebitmap_destroy(&e->names);
579 etmp = e;
580 e = e->next;
581 kfree(etmp);
582 }
583 ctemp = constraint;
584 constraint = constraint->next;
585 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Eric Paris9398c7f2010-11-23 11:40:08 -0500588 constraint = cladatum->validatetrans;
589 while (constraint) {
590 e = constraint->expr;
591 while (e) {
592 ebitmap_destroy(&e->names);
593 etmp = e;
594 e = e->next;
595 kfree(etmp);
596 }
597 ctemp = constraint;
598 constraint = constraint->next;
599 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric Paris9398c7f2010-11-23 11:40:08 -0500602 kfree(cladatum->comkey);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 kfree(datum);
605 return 0;
606}
607
608static int role_destroy(void *key, void *datum, void *p)
609{
610 struct role_datum *role;
611
612 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500613 if (datum) {
614 role = datum;
615 ebitmap_destroy(&role->dominates);
616 ebitmap_destroy(&role->types);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree(datum);
619 return 0;
620}
621
622static int type_destroy(void *key, void *datum, void *p)
623{
624 kfree(key);
625 kfree(datum);
626 return 0;
627}
628
629static int user_destroy(void *key, void *datum, void *p)
630{
631 struct user_datum *usrdatum;
632
633 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500634 if (datum) {
635 usrdatum = datum;
636 ebitmap_destroy(&usrdatum->roles);
637 ebitmap_destroy(&usrdatum->range.level[0].cat);
638 ebitmap_destroy(&usrdatum->range.level[1].cat);
639 ebitmap_destroy(&usrdatum->dfltlevel.cat);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree(datum);
642 return 0;
643}
644
645static int sens_destroy(void *key, void *datum, void *p)
646{
647 struct level_datum *levdatum;
648
649 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500650 if (datum) {
651 levdatum = datum;
652 ebitmap_destroy(&levdatum->level->cat);
653 kfree(levdatum->level);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 kfree(datum);
656 return 0;
657}
658
659static int cat_destroy(void *key, void *datum, void *p)
660{
661 kfree(key);
662 kfree(datum);
663 return 0;
664}
665
666static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
667{
668 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500669 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 role_destroy,
671 type_destroy,
672 user_destroy,
673 cond_destroy_bool,
674 sens_destroy,
675 cat_destroy,
676};
677
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500678static int range_tr_destroy(void *key, void *datum, void *p)
679{
680 struct mls_range *rt = datum;
681 kfree(key);
682 ebitmap_destroy(&rt->level[0].cat);
683 ebitmap_destroy(&rt->level[1].cat);
684 kfree(datum);
685 cond_resched();
686 return 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void ocontext_destroy(struct ocontext *c, int i)
690{
Eric Parisd1b43542010-07-21 12:50:57 -0400691 if (!c)
692 return;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 context_destroy(&c->context[0]);
695 context_destroy(&c->context[1]);
696 if (i == OCON_ISID || i == OCON_FS ||
697 i == OCON_NETIF || i == OCON_FSUSE)
698 kfree(c->u.name);
699 kfree(c);
700}
701
702/*
703 * Free any memory allocated by a policy database structure.
704 */
705void policydb_destroy(struct policydb *p)
706{
707 struct ocontext *c, *ctmp;
708 struct genfs *g, *gtmp;
709 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700710 struct role_allow *ra, *lra = NULL;
711 struct role_trans *tr, *ltr = NULL;
Eric Paris652bb9b2011-02-01 11:05:40 -0500712 struct filename_trans *ft, *nft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400715 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
717 hashtab_destroy(p->symtab[i].table);
718 }
719
Eric Parisac76c05b2010-11-29 15:47:09 -0500720 for (i = 0; i < SYM_NUM; i++) {
721 if (p->sym_val_to_name[i])
722 flex_array_free(p->sym_val_to_name[i]);
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700725 kfree(p->class_val_to_struct);
726 kfree(p->role_val_to_struct);
727 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500728 if (p->type_val_to_struct_array)
729 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 avtab_destroy(&p->te_avtab);
732
733 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400734 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 c = p->ocontexts[i];
736 while (c) {
737 ctmp = c;
738 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400739 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400741 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 g = p->genfs;
745 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400746 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 kfree(g->fstype);
748 c = g->head;
749 while (c) {
750 ctmp = c;
751 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400752 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 gtmp = g;
755 g = g->next;
756 kfree(gtmp);
757 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400758 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 cond_policydb_destroy(p);
761
Stephen Smalley782ebb92005-09-03 15:55:16 -0700762 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400763 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800764 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700765 ltr = tr;
766 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800767 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700768
Eric Paris2ced3df2008-04-17 13:37:12 -0400769 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400770 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800771 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700772 lra = ra;
773 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800774 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700775
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500776 hashtab_map(p->range_tr, range_tr_destroy, NULL);
777 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700778
Eric Paris6371dcd2010-07-29 23:02:34 -0400779 if (p->type_attr_map_array) {
780 for (i = 0; i < p->p_types.nprim; i++) {
781 struct ebitmap *e;
782
783 e = flex_array_get(p->type_attr_map_array, i);
784 if (!e)
785 continue;
786 ebitmap_destroy(e);
787 }
788 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700789 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500790
791 ft = p->filename_trans;
792 while (ft) {
793 nft = ft->next;
794 kfree(ft->name);
795 kfree(ft);
796 ft = nft;
797 }
798
Eric Paris03a4c012011-04-28 15:11:21 -0400799 ebitmap_destroy(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500800 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100801 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804}
805
806/*
807 * Load the initial SIDs specified in a policy database
808 * structure into a SID table.
809 */
810int policydb_load_isids(struct policydb *p, struct sidtab *s)
811{
812 struct ocontext *head, *c;
813 int rc;
814
815 rc = sidtab_init(s);
816 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100817 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto out;
819 }
820
821 head = p->ocontexts[OCON_ISID];
822 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500823 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500825 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
826 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto out;
828 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500829
830 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
831 if (rc) {
832 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
833 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto out;
835 }
836 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500837 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838out:
839 return rc;
840}
841
Stephen Smalley45e54212007-11-07 10:08:00 -0500842int policydb_class_isvalid(struct policydb *p, unsigned int class)
843{
844 if (!class || class > p->p_classes.nprim)
845 return 0;
846 return 1;
847}
848
849int policydb_role_isvalid(struct policydb *p, unsigned int role)
850{
851 if (!role || role > p->p_roles.nprim)
852 return 0;
853 return 1;
854}
855
856int policydb_type_isvalid(struct policydb *p, unsigned int type)
857{
858 if (!type || type > p->p_types.nprim)
859 return 0;
860 return 1;
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Return 1 if the fields in the security context
865 * structure `c' are valid. Return 0 otherwise.
866 */
867int policydb_context_isvalid(struct policydb *p, struct context *c)
868{
869 struct role_datum *role;
870 struct user_datum *usrdatum;
871
872 if (!c->role || c->role > p->p_roles.nprim)
873 return 0;
874
875 if (!c->user || c->user > p->p_users.nprim)
876 return 0;
877
878 if (!c->type || c->type > p->p_types.nprim)
879 return 0;
880
881 if (c->role != OBJECT_R_VAL) {
882 /*
883 * Role must be authorized for the type.
884 */
885 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500886 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* role may not be associated with type */
888 return 0;
889
890 /*
891 * User must be authorized for the role.
892 */
893 usrdatum = p->user_val_to_struct[c->user - 1];
894 if (!usrdatum)
895 return 0;
896
Eric Paris9398c7f2010-11-23 11:40:08 -0500897 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* user may not be associated with role */
899 return 0;
900 }
901
902 if (!mls_context_isvalid(p, c))
903 return 0;
904
905 return 1;
906}
907
908/*
909 * Read a MLS range structure from a policydb binary
910 * representation file.
911 */
912static int mls_read_range_helper(struct mls_range *r, void *fp)
913{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700914 __le32 buf[2];
915 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int rc;
917
918 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500919 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto out;
921
Eric Paris9398c7f2010-11-23 11:40:08 -0500922 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 items = le32_to_cpu(buf[0]);
924 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100925 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
927 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500930 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100931 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto out;
933 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 r->level[0].sens = le32_to_cpu(buf[0]);
936 if (items > 1)
937 r->level[1].sens = le32_to_cpu(buf[1]);
938 else
939 r->level[1].sens = r->level[0].sens;
940
941 rc = ebitmap_read(&r->level[0].cat, fp);
942 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500943 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto out;
945 }
946 if (items > 1) {
947 rc = ebitmap_read(&r->level[1].cat, fp);
948 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500949 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto bad_high;
951 }
952 } else {
953 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
954 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100955 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto bad_high;
957 }
958 }
959
Eric Paris9398c7f2010-11-23 11:40:08 -0500960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961bad_high:
962 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500963out:
964 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967/*
968 * Read and validate a security context structure
969 * from a policydb binary representation file.
970 */
971static int context_read_and_validate(struct context *c,
972 struct policydb *p,
973 void *fp)
974{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700975 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int rc;
977
978 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500979 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100980 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out;
982 }
983 c->user = le32_to_cpu(buf[0]);
984 c->role = le32_to_cpu(buf[1]);
985 c->type = le32_to_cpu(buf[2]);
986 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500987 rc = mls_read_range_helper(&c->range, fp);
988 if (rc) {
989 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto out;
991 }
992 }
993
Eric Paris9398c7f2010-11-23 11:40:08 -0500994 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100996 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -0500998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001000 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001out:
1002 return rc;
1003}
1004
1005/*
1006 * The following *_read functions are used to
1007 * read the symbol data from a policy database
1008 * binary representation file.
1009 */
1010
1011static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1012{
1013 char *key = NULL;
1014 struct perm_datum *perdatum;
1015 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001016 __le32 buf[2];
1017 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Eric Paris9398c7f2010-11-23 11:40:08 -05001019 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001020 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001021 if (!perdatum)
1022 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001025 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto bad;
1027
1028 len = le32_to_cpu(buf[0]);
1029 perdatum->value = le32_to_cpu(buf[1]);
1030
Eric Paris9398c7f2010-11-23 11:40:08 -05001031 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001032 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001033 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001037 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001039 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 rc = hashtab_insert(h, key, perdatum);
1042 if (rc)
1043 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001044
1045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046bad:
1047 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001048 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1052{
1053 char *key = NULL;
1054 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001055 __le32 buf[4];
1056 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int i, rc;
1058
Eric Paris9398c7f2010-11-23 11:40:08 -05001059 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001060 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001061 if (!comdatum)
1062 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001065 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto bad;
1067
1068 len = le32_to_cpu(buf[0]);
1069 comdatum->value = le32_to_cpu(buf[1]);
1070
1071 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1072 if (rc)
1073 goto bad;
1074 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1075 nel = le32_to_cpu(buf[3]);
1076
Eric Paris9398c7f2010-11-23 11:40:08 -05001077 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001078 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001079 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001083 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001085 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 for (i = 0; i < nel; i++) {
1088 rc = perm_read(p, comdatum->permissions.table, fp);
1089 if (rc)
1090 goto bad;
1091 }
1092
1093 rc = hashtab_insert(h, key, comdatum);
1094 if (rc)
1095 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097bad:
1098 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001099 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001103 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct constraint_node *c, *lc;
1106 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001107 __le32 buf[3];
1108 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 int rc, i, j, depth;
1110
1111 lc = NULL;
1112 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001113 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!c)
1115 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Paris2ced3df2008-04-17 13:37:12 -04001117 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001119 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001123 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return rc;
1125 c->permissions = le32_to_cpu(buf[0]);
1126 nexpr = le32_to_cpu(buf[1]);
1127 le = NULL;
1128 depth = -1;
1129 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001130 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!e)
1132 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric Paris2ced3df2008-04-17 13:37:12 -04001134 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001136 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001140 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return rc;
1142 e->expr_type = le32_to_cpu(buf[0]);
1143 e->attr = le32_to_cpu(buf[1]);
1144 e->op = le32_to_cpu(buf[2]);
1145
1146 switch (e->expr_type) {
1147 case CEXPR_NOT:
1148 if (depth < 0)
1149 return -EINVAL;
1150 break;
1151 case CEXPR_AND:
1152 case CEXPR_OR:
1153 if (depth < 1)
1154 return -EINVAL;
1155 depth--;
1156 break;
1157 case CEXPR_ATTR:
1158 if (depth == (CEXPR_MAXDEPTH - 1))
1159 return -EINVAL;
1160 depth++;
1161 break;
1162 case CEXPR_NAMES:
1163 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1164 return -EINVAL;
1165 if (depth == (CEXPR_MAXDEPTH - 1))
1166 return -EINVAL;
1167 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001168 rc = ebitmap_read(&e->names, fp);
1169 if (rc)
1170 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 default:
1173 return -EINVAL;
1174 }
1175 le = e;
1176 }
1177 if (depth != 0)
1178 return -EINVAL;
1179 lc = c;
1180 }
1181
1182 return 0;
1183}
1184
1185static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1186{
1187 char *key = NULL;
1188 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001189 __le32 buf[6];
1190 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int i, rc;
1192
Eric Paris9398c7f2010-11-23 11:40:08 -05001193 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001194 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001195 if (!cladatum)
1196 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001199 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto bad;
1201
1202 len = le32_to_cpu(buf[0]);
1203 len2 = le32_to_cpu(buf[1]);
1204 cladatum->value = le32_to_cpu(buf[2]);
1205
1206 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1207 if (rc)
1208 goto bad;
1209 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1210 nel = le32_to_cpu(buf[4]);
1211
1212 ncons = le32_to_cpu(buf[5]);
1213
Eric Paris9398c7f2010-11-23 11:40:08 -05001214 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001215 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001216 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001220 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001222 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001225 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001226 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001227 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001230 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001232 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Eric Paris9398c7f2010-11-23 11:40:08 -05001234 rc = -EINVAL;
1235 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001237 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto bad;
1239 }
1240 }
1241 for (i = 0; i < nel; i++) {
1242 rc = perm_read(p, cladatum->permissions.table, fp);
1243 if (rc)
1244 goto bad;
1245 }
1246
1247 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1248 if (rc)
1249 goto bad;
1250
1251 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1252 /* grab the validatetrans rules */
1253 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001254 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto bad;
1256 ncons = le32_to_cpu(buf[0]);
1257 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1258 if (rc)
1259 goto bad;
1260 }
1261
1262 rc = hashtab_insert(h, key, cladatum);
1263 if (rc)
1264 goto bad;
1265
Eric Paris9398c7f2010-11-23 11:40:08 -05001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001268 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001269 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1273{
1274 char *key = NULL;
1275 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001276 int rc, to_read = 2;
1277 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001278 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Eric Paris9398c7f2010-11-23 11:40:08 -05001280 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001281 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001282 if (!role)
1283 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
KaiGai Koheid9250de2008-08-28 16:35:57 +09001285 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1286 to_read = 3;
1287
1288 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001289 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto bad;
1291
1292 len = le32_to_cpu(buf[0]);
1293 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001294 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1295 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Eric Paris9398c7f2010-11-23 11:40:08 -05001297 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001298 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001299 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001303 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001305 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 rc = ebitmap_read(&role->dominates, fp);
1308 if (rc)
1309 goto bad;
1310
1311 rc = ebitmap_read(&role->types, fp);
1312 if (rc)
1313 goto bad;
1314
1315 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001316 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001318 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto bad;
1321 }
1322 rc = 0;
1323 goto bad;
1324 }
1325
1326 rc = hashtab_insert(h, key, role);
1327 if (rc)
1328 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330bad:
1331 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001332 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1336{
1337 char *key = NULL;
1338 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001339 int rc, to_read = 3;
1340 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001341 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Eric Paris9398c7f2010-11-23 11:40:08 -05001343 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001344 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001345 if (!typdatum)
1346 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
KaiGai Koheid9250de2008-08-28 16:35:57 +09001348 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1349 to_read = 4;
1350
1351 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001352 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 goto bad;
1354
1355 len = le32_to_cpu(buf[0]);
1356 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001357 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1358 u32 prop = le32_to_cpu(buf[2]);
1359
1360 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1361 typdatum->primary = 1;
1362 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1363 typdatum->attribute = 1;
1364
1365 typdatum->bounds = le32_to_cpu(buf[3]);
1366 } else {
1367 typdatum->primary = le32_to_cpu(buf[2]);
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Paris9398c7f2010-11-23 11:40:08 -05001370 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001371 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001372 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001375 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001377 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 rc = hashtab_insert(h, key, typdatum);
1380 if (rc)
1381 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383bad:
1384 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001385 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388
1389/*
1390 * Read a MLS level structure from a policydb binary
1391 * representation file.
1392 */
1393static int mls_read_level(struct mls_level *lp, void *fp)
1394{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001395 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int rc;
1397
1398 memset(lp, 0, sizeof(*lp));
1399
1400 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001401 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001402 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001403 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 lp->sens = le32_to_cpu(buf[0]);
1406
Eric Paris9398c7f2010-11-23 11:40:08 -05001407 rc = ebitmap_read(&lp->cat, fp);
1408 if (rc) {
1409 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1410 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1416{
1417 char *key = NULL;
1418 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001419 int rc, to_read = 2;
1420 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001421 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Eric Paris9398c7f2010-11-23 11:40:08 -05001423 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001424 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001425 if (!usrdatum)
1426 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
KaiGai Koheid9250de2008-08-28 16:35:57 +09001428 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1429 to_read = 3;
1430
1431 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001432 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto bad;
1434
1435 len = le32_to_cpu(buf[0]);
1436 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001437 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1438 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Eric Paris9398c7f2010-11-23 11:40:08 -05001440 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001441 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001442 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001445 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001447 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 rc = ebitmap_read(&usrdatum->roles, fp);
1450 if (rc)
1451 goto bad;
1452
1453 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1454 rc = mls_read_range_helper(&usrdatum->range, fp);
1455 if (rc)
1456 goto bad;
1457 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1458 if (rc)
1459 goto bad;
1460 }
1461
1462 rc = hashtab_insert(h, key, usrdatum);
1463 if (rc)
1464 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466bad:
1467 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001468 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1472{
1473 char *key = NULL;
1474 struct level_datum *levdatum;
1475 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001476 __le32 buf[2];
1477 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric Paris9398c7f2010-11-23 11:40:08 -05001479 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001480 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001481 if (!levdatum)
1482 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001485 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto bad;
1487
1488 len = le32_to_cpu(buf[0]);
1489 levdatum->isalias = le32_to_cpu(buf[1]);
1490
Eric Paris9398c7f2010-11-23 11:40:08 -05001491 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001492 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001493 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001496 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001498 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Eric Paris9398c7f2010-11-23 11:40:08 -05001500 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001502 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001504
1505 rc = mls_read_level(levdatum->level, fp);
1506 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 rc = hashtab_insert(h, key, levdatum);
1510 if (rc)
1511 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513bad:
1514 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001515 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1519{
1520 char *key = NULL;
1521 struct cat_datum *catdatum;
1522 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001523 __le32 buf[3];
1524 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Eric Paris9398c7f2010-11-23 11:40:08 -05001526 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001527 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001528 if (!catdatum)
1529 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001532 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 goto bad;
1534
1535 len = le32_to_cpu(buf[0]);
1536 catdatum->value = le32_to_cpu(buf[1]);
1537 catdatum->isalias = le32_to_cpu(buf[2]);
1538
Eric Paris9398c7f2010-11-23 11:40:08 -05001539 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001540 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001541 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001544 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001546 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 rc = hashtab_insert(h, key, catdatum);
1549 if (rc)
1550 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552bad:
1553 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001554 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1558{
1559 common_read,
1560 class_read,
1561 role_read,
1562 type_read,
1563 user_read,
1564 cond_read_bool,
1565 sens_read,
1566 cat_read,
1567};
1568
KaiGai Koheid9250de2008-08-28 16:35:57 +09001569static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1570{
1571 struct user_datum *upper, *user;
1572 struct policydb *p = datap;
1573 int depth = 0;
1574
1575 upper = user = datum;
1576 while (upper->bounds) {
1577 struct ebitmap_node *node;
1578 unsigned long bit;
1579
1580 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1581 printk(KERN_ERR "SELinux: user %s: "
1582 "too deep or looped boundary",
1583 (char *) key);
1584 return -EINVAL;
1585 }
1586
1587 upper = p->user_val_to_struct[upper->bounds - 1];
1588 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1589 if (ebitmap_get_bit(&upper->roles, bit))
1590 continue;
1591
1592 printk(KERN_ERR
1593 "SELinux: boundary violated policy: "
1594 "user=%s role=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001595 sym_name(p, SYM_USERS, user->value - 1),
1596 sym_name(p, SYM_ROLES, bit),
1597 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001598
1599 return -EINVAL;
1600 }
1601 }
1602
1603 return 0;
1604}
1605
1606static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1607{
1608 struct role_datum *upper, *role;
1609 struct policydb *p = datap;
1610 int depth = 0;
1611
1612 upper = role = datum;
1613 while (upper->bounds) {
1614 struct ebitmap_node *node;
1615 unsigned long bit;
1616
1617 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1618 printk(KERN_ERR "SELinux: role %s: "
1619 "too deep or looped bounds\n",
1620 (char *) key);
1621 return -EINVAL;
1622 }
1623
1624 upper = p->role_val_to_struct[upper->bounds - 1];
1625 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1626 if (ebitmap_get_bit(&upper->types, bit))
1627 continue;
1628
1629 printk(KERN_ERR
1630 "SELinux: boundary violated policy: "
1631 "role=%s type=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001632 sym_name(p, SYM_ROLES, role->value - 1),
1633 sym_name(p, SYM_TYPES, bit),
1634 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001635
1636 return -EINVAL;
1637 }
1638 }
1639
1640 return 0;
1641}
1642
1643static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1644{
Eric Parisdaa6d832010-08-03 15:26:05 -04001645 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001646 struct policydb *p = datap;
1647 int depth = 0;
1648
Eric Parisdaa6d832010-08-03 15:26:05 -04001649 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001650 while (upper->bounds) {
1651 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1652 printk(KERN_ERR "SELinux: type %s: "
1653 "too deep or looped boundary\n",
1654 (char *) key);
1655 return -EINVAL;
1656 }
1657
Eric Paris23bdecb2010-11-29 15:47:09 -05001658 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1659 upper->bounds - 1);
1660 BUG_ON(!upper);
1661
KaiGai Koheid9250de2008-08-28 16:35:57 +09001662 if (upper->attribute) {
1663 printk(KERN_ERR "SELinux: type %s: "
1664 "bounded by attribute %s",
1665 (char *) key,
Eric Parisac76c05b2010-11-29 15:47:09 -05001666 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001667 return -EINVAL;
1668 }
1669 }
1670
1671 return 0;
1672}
1673
1674static int policydb_bounds_sanity_check(struct policydb *p)
1675{
1676 int rc;
1677
1678 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1679 return 0;
1680
1681 rc = hashtab_map(p->p_users.table,
1682 user_bounds_sanity_check, p);
1683 if (rc)
1684 return rc;
1685
1686 rc = hashtab_map(p->p_roles.table,
1687 role_bounds_sanity_check, p);
1688 if (rc)
1689 return rc;
1690
1691 rc = hashtab_map(p->p_types.table,
1692 type_bounds_sanity_check, p);
1693 if (rc)
1694 return rc;
1695
1696 return 0;
1697}
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699extern int ss_initialized;
1700
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001701u16 string_to_security_class(struct policydb *p, const char *name)
1702{
1703 struct class_datum *cladatum;
1704
1705 cladatum = hashtab_search(p->p_classes.table, name);
1706 if (!cladatum)
1707 return 0;
1708
1709 return cladatum->value;
1710}
1711
1712u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1713{
1714 struct class_datum *cladatum;
1715 struct perm_datum *perdatum = NULL;
1716 struct common_datum *comdatum;
1717
1718 if (!tclass || tclass > p->p_classes.nprim)
1719 return 0;
1720
1721 cladatum = p->class_val_to_struct[tclass-1];
1722 comdatum = cladatum->comdatum;
1723 if (comdatum)
1724 perdatum = hashtab_search(comdatum->permissions.table,
1725 name);
1726 if (!perdatum)
1727 perdatum = hashtab_search(cladatum->permissions.table,
1728 name);
1729 if (!perdatum)
1730 return 0;
1731
1732 return 1U << (perdatum->value-1);
1733}
1734
Eric Paris9ee0c822010-06-11 12:37:05 -04001735static int range_read(struct policydb *p, void *fp)
1736{
1737 struct range_trans *rt = NULL;
1738 struct mls_range *r = NULL;
1739 int i, rc;
1740 __le32 buf[2];
1741 u32 nel;
1742
1743 if (p->policyvers < POLICYDB_VERSION_MLS)
1744 return 0;
1745
1746 rc = next_entry(buf, fp, sizeof(u32));
1747 if (rc)
1748 goto out;
1749
1750 nel = le32_to_cpu(buf[0]);
1751 for (i = 0; i < nel; i++) {
1752 rc = -ENOMEM;
1753 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1754 if (!rt)
1755 goto out;
1756
1757 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1758 if (rc)
1759 goto out;
1760
1761 rt->source_type = le32_to_cpu(buf[0]);
1762 rt->target_type = le32_to_cpu(buf[1]);
1763 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1764 rc = next_entry(buf, fp, sizeof(u32));
1765 if (rc)
1766 goto out;
1767 rt->target_class = le32_to_cpu(buf[0]);
1768 } else
1769 rt->target_class = p->process_class;
1770
1771 rc = -EINVAL;
1772 if (!policydb_type_isvalid(p, rt->source_type) ||
1773 !policydb_type_isvalid(p, rt->target_type) ||
1774 !policydb_class_isvalid(p, rt->target_class))
1775 goto out;
1776
1777 rc = -ENOMEM;
1778 r = kzalloc(sizeof(*r), GFP_KERNEL);
1779 if (!r)
1780 goto out;
1781
1782 rc = mls_read_range_helper(r, fp);
1783 if (rc)
1784 goto out;
1785
1786 rc = -EINVAL;
1787 if (!mls_range_isvalid(p, r)) {
1788 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1789 goto out;
1790 }
1791
1792 rc = hashtab_insert(p->range_tr, rt, r);
1793 if (rc)
1794 goto out;
1795
1796 rt = NULL;
1797 r = NULL;
1798 }
Eric Parisbe30b162011-04-28 15:11:21 -04001799 hash_eval(p->range_tr, "rangetr");
Eric Paris9ee0c822010-06-11 12:37:05 -04001800 rc = 0;
1801out:
1802 kfree(rt);
1803 kfree(r);
1804 return rc;
1805}
1806
Eric Paris652bb9b2011-02-01 11:05:40 -05001807static int filename_trans_read(struct policydb *p, void *fp)
1808{
1809 struct filename_trans *ft, *last;
1810 u32 nel, len;
1811 char *name;
1812 __le32 buf[4];
1813 int rc, i;
1814
1815 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1816 return 0;
1817
1818 rc = next_entry(buf, fp, sizeof(u32));
1819 if (rc)
1820 goto out;
1821 nel = le32_to_cpu(buf[0]);
1822
Eric Paris652bb9b2011-02-01 11:05:40 -05001823 last = p->filename_trans;
1824 while (last && last->next)
1825 last = last->next;
1826
1827 for (i = 0; i < nel; i++) {
1828 rc = -ENOMEM;
1829 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1830 if (!ft)
1831 goto out;
1832
1833 /* add it to the tail of the list */
1834 if (!last)
1835 p->filename_trans = ft;
1836 else
1837 last->next = ft;
1838 last = ft;
1839
1840 /* length of the path component string */
1841 rc = next_entry(buf, fp, sizeof(u32));
1842 if (rc)
1843 goto out;
1844 len = le32_to_cpu(buf[0]);
1845
1846 rc = -ENOMEM;
1847 name = kmalloc(len + 1, GFP_KERNEL);
1848 if (!name)
1849 goto out;
1850
1851 ft->name = name;
1852
1853 /* path component string */
1854 rc = next_entry(name, fp, len);
1855 if (rc)
1856 goto out;
1857 name[len] = 0;
1858
Eric Paris652bb9b2011-02-01 11:05:40 -05001859 rc = next_entry(buf, fp, sizeof(u32) * 4);
1860 if (rc)
1861 goto out;
1862
1863 ft->stype = le32_to_cpu(buf[0]);
1864 ft->ttype = le32_to_cpu(buf[1]);
1865 ft->tclass = le32_to_cpu(buf[2]);
1866 ft->otype = le32_to_cpu(buf[3]);
Eric Paris03a4c012011-04-28 15:11:21 -04001867
1868 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1869 if (rc)
1870 goto out;
Eric Paris652bb9b2011-02-01 11:05:40 -05001871 }
1872 rc = 0;
1873out:
1874 return rc;
1875}
1876
Eric Parisd1b43542010-07-21 12:50:57 -04001877static int genfs_read(struct policydb *p, void *fp)
1878{
1879 int i, j, rc;
1880 u32 nel, nel2, len, len2;
1881 __le32 buf[1];
1882 struct ocontext *l, *c;
1883 struct ocontext *newc = NULL;
1884 struct genfs *genfs_p, *genfs;
1885 struct genfs *newgenfs = NULL;
1886
1887 rc = next_entry(buf, fp, sizeof(u32));
1888 if (rc)
1889 goto out;
1890 nel = le32_to_cpu(buf[0]);
1891
1892 for (i = 0; i < nel; i++) {
1893 rc = next_entry(buf, fp, sizeof(u32));
1894 if (rc)
1895 goto out;
1896 len = le32_to_cpu(buf[0]);
1897
1898 rc = -ENOMEM;
1899 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1900 if (!newgenfs)
1901 goto out;
1902
1903 rc = -ENOMEM;
1904 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1905 if (!newgenfs->fstype)
1906 goto out;
1907
1908 rc = next_entry(newgenfs->fstype, fp, len);
1909 if (rc)
1910 goto out;
1911
1912 newgenfs->fstype[len] = 0;
1913
1914 for (genfs_p = NULL, genfs = p->genfs; genfs;
1915 genfs_p = genfs, genfs = genfs->next) {
1916 rc = -EINVAL;
1917 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1918 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1919 newgenfs->fstype);
1920 goto out;
1921 }
1922 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1923 break;
1924 }
1925 newgenfs->next = genfs;
1926 if (genfs_p)
1927 genfs_p->next = newgenfs;
1928 else
1929 p->genfs = newgenfs;
1930 genfs = newgenfs;
1931 newgenfs = NULL;
1932
1933 rc = next_entry(buf, fp, sizeof(u32));
1934 if (rc)
1935 goto out;
1936
1937 nel2 = le32_to_cpu(buf[0]);
1938 for (j = 0; j < nel2; j++) {
1939 rc = next_entry(buf, fp, sizeof(u32));
1940 if (rc)
1941 goto out;
1942 len = le32_to_cpu(buf[0]);
1943
1944 rc = -ENOMEM;
1945 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1946 if (!newc)
1947 goto out;
1948
1949 rc = -ENOMEM;
1950 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1951 if (!newc->u.name)
1952 goto out;
1953
1954 rc = next_entry(newc->u.name, fp, len);
1955 if (rc)
1956 goto out;
1957 newc->u.name[len] = 0;
1958
1959 rc = next_entry(buf, fp, sizeof(u32));
1960 if (rc)
1961 goto out;
1962
1963 newc->v.sclass = le32_to_cpu(buf[0]);
1964 rc = context_read_and_validate(&newc->context[0], p, fp);
1965 if (rc)
1966 goto out;
1967
1968 for (l = NULL, c = genfs->head; c;
1969 l = c, c = c->next) {
1970 rc = -EINVAL;
1971 if (!strcmp(newc->u.name, c->u.name) &&
1972 (!c->v.sclass || !newc->v.sclass ||
1973 newc->v.sclass == c->v.sclass)) {
1974 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1975 genfs->fstype, c->u.name);
1976 goto out;
1977 }
1978 len = strlen(newc->u.name);
1979 len2 = strlen(c->u.name);
1980 if (len > len2)
1981 break;
1982 }
1983
1984 newc->next = c;
1985 if (l)
1986 l->next = newc;
1987 else
1988 genfs->head = newc;
1989 newc = NULL;
1990 }
1991 }
1992 rc = 0;
1993out:
1994 if (newgenfs)
1995 kfree(newgenfs->fstype);
1996 kfree(newgenfs);
1997 ocontext_destroy(newc, OCON_FSUSE);
1998
1999 return rc;
2000}
2001
Eric Paris692a8a22010-07-21 12:51:03 -04002002static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2003 void *fp)
2004{
2005 int i, j, rc;
2006 u32 nel, len;
2007 __le32 buf[3];
2008 struct ocontext *l, *c;
2009 u32 nodebuf[8];
2010
2011 for (i = 0; i < info->ocon_num; i++) {
2012 rc = next_entry(buf, fp, sizeof(u32));
2013 if (rc)
2014 goto out;
2015 nel = le32_to_cpu(buf[0]);
2016
2017 l = NULL;
2018 for (j = 0; j < nel; j++) {
2019 rc = -ENOMEM;
2020 c = kzalloc(sizeof(*c), GFP_KERNEL);
2021 if (!c)
2022 goto out;
2023 if (l)
2024 l->next = c;
2025 else
2026 p->ocontexts[i] = c;
2027 l = c;
2028
2029 switch (i) {
2030 case OCON_ISID:
2031 rc = next_entry(buf, fp, sizeof(u32));
2032 if (rc)
2033 goto out;
2034
2035 c->sid[0] = le32_to_cpu(buf[0]);
2036 rc = context_read_and_validate(&c->context[0], p, fp);
2037 if (rc)
2038 goto out;
2039 break;
2040 case OCON_FS:
2041 case OCON_NETIF:
2042 rc = next_entry(buf, fp, sizeof(u32));
2043 if (rc)
2044 goto out;
2045 len = le32_to_cpu(buf[0]);
2046
2047 rc = -ENOMEM;
2048 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2049 if (!c->u.name)
2050 goto out;
2051
2052 rc = next_entry(c->u.name, fp, len);
2053 if (rc)
2054 goto out;
2055
2056 c->u.name[len] = 0;
2057 rc = context_read_and_validate(&c->context[0], p, fp);
2058 if (rc)
2059 goto out;
2060 rc = context_read_and_validate(&c->context[1], p, fp);
2061 if (rc)
2062 goto out;
2063 break;
2064 case OCON_PORT:
2065 rc = next_entry(buf, fp, sizeof(u32)*3);
2066 if (rc)
2067 goto out;
2068 c->u.port.protocol = le32_to_cpu(buf[0]);
2069 c->u.port.low_port = le32_to_cpu(buf[1]);
2070 c->u.port.high_port = le32_to_cpu(buf[2]);
2071 rc = context_read_and_validate(&c->context[0], p, fp);
2072 if (rc)
2073 goto out;
2074 break;
2075 case OCON_NODE:
2076 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2077 if (rc)
2078 goto out;
2079 c->u.node.addr = nodebuf[0]; /* network order */
2080 c->u.node.mask = nodebuf[1]; /* network order */
2081 rc = context_read_and_validate(&c->context[0], p, fp);
2082 if (rc)
2083 goto out;
2084 break;
2085 case OCON_FSUSE:
2086 rc = next_entry(buf, fp, sizeof(u32)*2);
2087 if (rc)
2088 goto out;
2089
2090 rc = -EINVAL;
2091 c->v.behavior = le32_to_cpu(buf[0]);
2092 if (c->v.behavior > SECURITY_FS_USE_NONE)
2093 goto out;
2094
2095 rc = -ENOMEM;
2096 len = le32_to_cpu(buf[1]);
2097 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2098 if (!c->u.name)
2099 goto out;
2100
2101 rc = next_entry(c->u.name, fp, len);
2102 if (rc)
2103 goto out;
2104 c->u.name[len] = 0;
2105 rc = context_read_and_validate(&c->context[0], p, fp);
2106 if (rc)
2107 goto out;
2108 break;
2109 case OCON_NODE6: {
2110 int k;
2111
2112 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2113 if (rc)
2114 goto out;
2115 for (k = 0; k < 4; k++)
2116 c->u.node6.addr[k] = nodebuf[k];
2117 for (k = 0; k < 4; k++)
2118 c->u.node6.mask[k] = nodebuf[k+4];
2119 rc = context_read_and_validate(&c->context[0], p, fp);
2120 if (rc)
2121 goto out;
2122 break;
2123 }
2124 }
2125 }
2126 }
2127 rc = 0;
2128out:
2129 return rc;
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/*
2133 * Read the configuration data from a policy database binary
2134 * representation file into a policy database structure.
2135 */
2136int policydb_read(struct policydb *p, void *fp)
2137{
2138 struct role_allow *ra, *lra;
2139 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002141 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002142 u32 len, nprim, nel;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 char *policydb_str;
2145 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 rc = policydb_init(p);
2148 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002149 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002152 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002153 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 goto bad;
2155
Eric Paris9398c7f2010-11-23 11:40:08 -05002156 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002157 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002158 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002160 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto bad;
2162 }
2163
Eric Paris9398c7f2010-11-23 11:40:08 -05002164 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002165 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002167 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 "match expected length %Zu\n",
2169 len, strlen(POLICYDB_STRING));
2170 goto bad;
2171 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002172
2173 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002174 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002176 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 goto bad;
2179 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002182 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002183 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 kfree(policydb_str);
2185 goto bad;
2186 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002187
2188 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002189 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002191 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 "my string %s\n", policydb_str, POLICYDB_STRING);
2193 kfree(policydb_str);
2194 goto bad;
2195 }
2196 /* Done with policydb_str. */
2197 kfree(policydb_str);
2198 policydb_str = NULL;
2199
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002200 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002202 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Eric Paris9398c7f2010-11-23 11:40:08 -05002205 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002206 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (p->policyvers < POLICYDB_VERSION_MIN ||
2208 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002209 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002210 "my version range %d-%d\n",
2211 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2212 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002215 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002216 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Eric Paris9398c7f2010-11-23 11:40:08 -05002218 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002220 printk(KERN_ERR "SELinux: security policydb version %d "
2221 "(MLS) not backwards compatible\n",
2222 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 goto bad;
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Eric Paris3f120702007-09-21 14:37:10 -04002226 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2227 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Eric Paris9398c7f2010-11-23 11:40:08 -05002229 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2230 rc = ebitmap_read(&p->policycaps, fp);
2231 if (rc)
2232 goto bad;
2233 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002234
Eric Paris9398c7f2010-11-23 11:40:08 -05002235 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2236 rc = ebitmap_read(&p->permissive_map, fp);
2237 if (rc)
2238 goto bad;
2239 }
Eric Paris64dbf072008-03-31 12:17:33 +11002240
Eric Paris9398c7f2010-11-23 11:40:08 -05002241 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 info = policydb_lookup_compat(p->policyvers);
2243 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002244 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 "for version %d\n", p->policyvers);
2246 goto bad;
2247 }
2248
Eric Paris9398c7f2010-11-23 11:40:08 -05002249 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002250 if (le32_to_cpu(buf[2]) != info->sym_num ||
2251 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002252 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002253 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2254 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 info->sym_num, info->ocon_num);
2256 goto bad;
2257 }
2258
2259 for (i = 0; i < info->sym_num; i++) {
2260 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002261 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto bad;
2263 nprim = le32_to_cpu(buf[0]);
2264 nel = le32_to_cpu(buf[1]);
2265 for (j = 0; j < nel; j++) {
2266 rc = read_f[i](p, p->symtab[i].table, fp);
2267 if (rc)
2268 goto bad;
2269 }
2270
2271 p->symtab[i].nprim = nprim;
2272 }
2273
Harry Ciao1214eac2011-04-07 14:12:57 +08002274 rc = -EINVAL;
2275 p->process_class = string_to_security_class(p, "process");
2276 if (!p->process_class)
2277 goto bad;
2278
Stephen Smalley45e54212007-11-07 10:08:00 -05002279 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (rc)
2281 goto bad;
2282
2283 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2284 rc = cond_read_list(p, fp);
2285 if (rc)
2286 goto bad;
2287 }
2288
2289 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002290 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto bad;
2292 nel = le32_to_cpu(buf[0]);
2293 ltr = NULL;
2294 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002295 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002296 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002297 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002299 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002301 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002304 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002306
2307 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 tr->role = le32_to_cpu(buf[0]);
2309 tr->type = le32_to_cpu(buf[1]);
2310 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002311 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2312 rc = next_entry(buf, fp, sizeof(u32));
2313 if (rc)
2314 goto bad;
2315 tr->tclass = le32_to_cpu(buf[0]);
2316 } else
2317 tr->tclass = p->process_class;
2318
Stephen Smalley45e54212007-11-07 10:08:00 -05002319 if (!policydb_role_isvalid(p, tr->role) ||
2320 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002321 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002322 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002323 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 ltr = tr;
2325 }
2326
2327 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002328 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto bad;
2330 nel = le32_to_cpu(buf[0]);
2331 lra = NULL;
2332 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002333 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002334 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002335 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002337 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002339 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002342 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002344
2345 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 ra->role = le32_to_cpu(buf[0]);
2347 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002348 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002349 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002350 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 lra = ra;
2352 }
2353
Eric Paris652bb9b2011-02-01 11:05:40 -05002354 rc = filename_trans_read(p, fp);
2355 if (rc)
2356 goto bad;
2357
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002358 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (rc)
2360 goto bad;
2361
Eric Paris9398c7f2010-11-23 11:40:08 -05002362 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002363 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2364 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002365 if (!p->process_trans_perms)
2366 goto bad;
2367
Eric Paris692a8a22010-07-21 12:51:03 -04002368 rc = ocontext_read(p, info, fp);
2369 if (rc)
2370 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Eric Parisd1b43542010-07-21 12:50:57 -04002372 rc = genfs_read(p, fp);
2373 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Eric Paris9ee0c822010-06-11 12:37:05 -04002376 rc = range_read(p, fp);
2377 if (rc)
2378 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Eric Paris6371dcd2010-07-29 23:02:34 -04002380 rc = -ENOMEM;
2381 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2382 p->p_types.nprim,
2383 GFP_KERNEL | __GFP_ZERO);
2384 if (!p->type_attr_map_array)
2385 goto bad;
2386
2387 /* preallocate so we don't have to worry about the put ever failing */
2388 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2389 GFP_KERNEL | __GFP_ZERO);
2390 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002391 goto bad;
2392
2393 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002394 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2395
2396 BUG_ON(!e);
2397 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002398 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002399 rc = ebitmap_read(e, fp);
2400 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002401 goto bad;
2402 }
2403 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002404 rc = ebitmap_set_bit(e, i, 1);
2405 if (rc)
2406 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002407 }
2408
KaiGai Koheid9250de2008-08-28 16:35:57 +09002409 rc = policydb_bounds_sanity_check(p);
2410 if (rc)
2411 goto bad;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 rc = 0;
2414out:
2415 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 policydb_destroy(p);
2418 goto out;
2419}
Eric Pariscee74f42010-10-13 17:50:25 -04002420
2421/*
2422 * Write a MLS level structure to a policydb binary
2423 * representation file.
2424 */
2425static int mls_write_level(struct mls_level *l, void *fp)
2426{
2427 __le32 buf[1];
2428 int rc;
2429
2430 buf[0] = cpu_to_le32(l->sens);
2431 rc = put_entry(buf, sizeof(u32), 1, fp);
2432 if (rc)
2433 return rc;
2434
2435 rc = ebitmap_write(&l->cat, fp);
2436 if (rc)
2437 return rc;
2438
2439 return 0;
2440}
2441
2442/*
2443 * Write a MLS range structure to a policydb binary
2444 * representation file.
2445 */
2446static int mls_write_range_helper(struct mls_range *r, void *fp)
2447{
2448 __le32 buf[3];
2449 size_t items;
2450 int rc, eq;
2451
2452 eq = mls_level_eq(&r->level[1], &r->level[0]);
2453
2454 if (eq)
2455 items = 2;
2456 else
2457 items = 3;
2458 buf[0] = cpu_to_le32(items-1);
2459 buf[1] = cpu_to_le32(r->level[0].sens);
2460 if (!eq)
2461 buf[2] = cpu_to_le32(r->level[1].sens);
2462
2463 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2464
2465 rc = put_entry(buf, sizeof(u32), items, fp);
2466 if (rc)
2467 return rc;
2468
2469 rc = ebitmap_write(&r->level[0].cat, fp);
2470 if (rc)
2471 return rc;
2472 if (!eq) {
2473 rc = ebitmap_write(&r->level[1].cat, fp);
2474 if (rc)
2475 return rc;
2476 }
2477
2478 return 0;
2479}
2480
2481static int sens_write(void *vkey, void *datum, void *ptr)
2482{
2483 char *key = vkey;
2484 struct level_datum *levdatum = datum;
2485 struct policy_data *pd = ptr;
2486 void *fp = pd->fp;
2487 __le32 buf[2];
2488 size_t len;
2489 int rc;
2490
2491 len = strlen(key);
2492 buf[0] = cpu_to_le32(len);
2493 buf[1] = cpu_to_le32(levdatum->isalias);
2494 rc = put_entry(buf, sizeof(u32), 2, fp);
2495 if (rc)
2496 return rc;
2497
2498 rc = put_entry(key, 1, len, fp);
2499 if (rc)
2500 return rc;
2501
2502 rc = mls_write_level(levdatum->level, fp);
2503 if (rc)
2504 return rc;
2505
2506 return 0;
2507}
2508
2509static int cat_write(void *vkey, void *datum, void *ptr)
2510{
2511 char *key = vkey;
2512 struct cat_datum *catdatum = datum;
2513 struct policy_data *pd = ptr;
2514 void *fp = pd->fp;
2515 __le32 buf[3];
2516 size_t len;
2517 int rc;
2518
2519 len = strlen(key);
2520 buf[0] = cpu_to_le32(len);
2521 buf[1] = cpu_to_le32(catdatum->value);
2522 buf[2] = cpu_to_le32(catdatum->isalias);
2523 rc = put_entry(buf, sizeof(u32), 3, fp);
2524 if (rc)
2525 return rc;
2526
2527 rc = put_entry(key, 1, len, fp);
2528 if (rc)
2529 return rc;
2530
2531 return 0;
2532}
2533
Harry Ciaoc900ff32011-03-25 13:52:00 +08002534static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002535{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002536 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002537 struct role_trans *tr;
2538 u32 buf[3];
2539 size_t nel;
2540 int rc;
2541
2542 nel = 0;
2543 for (tr = r; tr; tr = tr->next)
2544 nel++;
2545 buf[0] = cpu_to_le32(nel);
2546 rc = put_entry(buf, sizeof(u32), 1, fp);
2547 if (rc)
2548 return rc;
2549 for (tr = r; tr; tr = tr->next) {
2550 buf[0] = cpu_to_le32(tr->role);
2551 buf[1] = cpu_to_le32(tr->type);
2552 buf[2] = cpu_to_le32(tr->new_role);
2553 rc = put_entry(buf, sizeof(u32), 3, fp);
2554 if (rc)
2555 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002556 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2557 buf[0] = cpu_to_le32(tr->tclass);
2558 rc = put_entry(buf, sizeof(u32), 1, fp);
2559 if (rc)
2560 return rc;
2561 }
Eric Pariscee74f42010-10-13 17:50:25 -04002562 }
2563
2564 return 0;
2565}
2566
2567static int role_allow_write(struct role_allow *r, void *fp)
2568{
2569 struct role_allow *ra;
2570 u32 buf[2];
2571 size_t nel;
2572 int rc;
2573
2574 nel = 0;
2575 for (ra = r; ra; ra = ra->next)
2576 nel++;
2577 buf[0] = cpu_to_le32(nel);
2578 rc = put_entry(buf, sizeof(u32), 1, fp);
2579 if (rc)
2580 return rc;
2581 for (ra = r; ra; ra = ra->next) {
2582 buf[0] = cpu_to_le32(ra->role);
2583 buf[1] = cpu_to_le32(ra->new_role);
2584 rc = put_entry(buf, sizeof(u32), 2, fp);
2585 if (rc)
2586 return rc;
2587 }
2588 return 0;
2589}
2590
2591/*
2592 * Write a security context structure
2593 * to a policydb binary representation file.
2594 */
2595static int context_write(struct policydb *p, struct context *c,
2596 void *fp)
2597{
2598 int rc;
2599 __le32 buf[3];
2600
2601 buf[0] = cpu_to_le32(c->user);
2602 buf[1] = cpu_to_le32(c->role);
2603 buf[2] = cpu_to_le32(c->type);
2604
2605 rc = put_entry(buf, sizeof(u32), 3, fp);
2606 if (rc)
2607 return rc;
2608
2609 rc = mls_write_range_helper(&c->range, fp);
2610 if (rc)
2611 return rc;
2612
2613 return 0;
2614}
2615
2616/*
2617 * The following *_write functions are used to
2618 * write the symbol data to a policy database
2619 * binary representation file.
2620 */
2621
2622static int perm_write(void *vkey, void *datum, void *fp)
2623{
2624 char *key = vkey;
2625 struct perm_datum *perdatum = datum;
2626 __le32 buf[2];
2627 size_t len;
2628 int rc;
2629
2630 len = strlen(key);
2631 buf[0] = cpu_to_le32(len);
2632 buf[1] = cpu_to_le32(perdatum->value);
2633 rc = put_entry(buf, sizeof(u32), 2, fp);
2634 if (rc)
2635 return rc;
2636
2637 rc = put_entry(key, 1, len, fp);
2638 if (rc)
2639 return rc;
2640
2641 return 0;
2642}
2643
2644static int common_write(void *vkey, void *datum, void *ptr)
2645{
2646 char *key = vkey;
2647 struct common_datum *comdatum = datum;
2648 struct policy_data *pd = ptr;
2649 void *fp = pd->fp;
2650 __le32 buf[4];
2651 size_t len;
2652 int rc;
2653
2654 len = strlen(key);
2655 buf[0] = cpu_to_le32(len);
2656 buf[1] = cpu_to_le32(comdatum->value);
2657 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2658 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2659 rc = put_entry(buf, sizeof(u32), 4, fp);
2660 if (rc)
2661 return rc;
2662
2663 rc = put_entry(key, 1, len, fp);
2664 if (rc)
2665 return rc;
2666
2667 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2668 if (rc)
2669 return rc;
2670
2671 return 0;
2672}
2673
2674static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2675 void *fp)
2676{
2677 struct constraint_node *c;
2678 struct constraint_expr *e;
2679 __le32 buf[3];
2680 u32 nel;
2681 int rc;
2682
2683 for (c = node; c; c = c->next) {
2684 nel = 0;
2685 for (e = c->expr; e; e = e->next)
2686 nel++;
2687 buf[0] = cpu_to_le32(c->permissions);
2688 buf[1] = cpu_to_le32(nel);
2689 rc = put_entry(buf, sizeof(u32), 2, fp);
2690 if (rc)
2691 return rc;
2692 for (e = c->expr; e; e = e->next) {
2693 buf[0] = cpu_to_le32(e->expr_type);
2694 buf[1] = cpu_to_le32(e->attr);
2695 buf[2] = cpu_to_le32(e->op);
2696 rc = put_entry(buf, sizeof(u32), 3, fp);
2697 if (rc)
2698 return rc;
2699
2700 switch (e->expr_type) {
2701 case CEXPR_NAMES:
2702 rc = ebitmap_write(&e->names, fp);
2703 if (rc)
2704 return rc;
2705 break;
2706 default:
2707 break;
2708 }
2709 }
2710 }
2711
2712 return 0;
2713}
2714
2715static int class_write(void *vkey, void *datum, void *ptr)
2716{
2717 char *key = vkey;
2718 struct class_datum *cladatum = datum;
2719 struct policy_data *pd = ptr;
2720 void *fp = pd->fp;
2721 struct policydb *p = pd->p;
2722 struct constraint_node *c;
2723 __le32 buf[6];
2724 u32 ncons;
2725 size_t len, len2;
2726 int rc;
2727
2728 len = strlen(key);
2729 if (cladatum->comkey)
2730 len2 = strlen(cladatum->comkey);
2731 else
2732 len2 = 0;
2733
2734 ncons = 0;
2735 for (c = cladatum->constraints; c; c = c->next)
2736 ncons++;
2737
2738 buf[0] = cpu_to_le32(len);
2739 buf[1] = cpu_to_le32(len2);
2740 buf[2] = cpu_to_le32(cladatum->value);
2741 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2742 if (cladatum->permissions.table)
2743 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2744 else
2745 buf[4] = 0;
2746 buf[5] = cpu_to_le32(ncons);
2747 rc = put_entry(buf, sizeof(u32), 6, fp);
2748 if (rc)
2749 return rc;
2750
2751 rc = put_entry(key, 1, len, fp);
2752 if (rc)
2753 return rc;
2754
2755 if (cladatum->comkey) {
2756 rc = put_entry(cladatum->comkey, 1, len2, fp);
2757 if (rc)
2758 return rc;
2759 }
2760
2761 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2762 if (rc)
2763 return rc;
2764
2765 rc = write_cons_helper(p, cladatum->constraints, fp);
2766 if (rc)
2767 return rc;
2768
2769 /* write out the validatetrans rule */
2770 ncons = 0;
2771 for (c = cladatum->validatetrans; c; c = c->next)
2772 ncons++;
2773
2774 buf[0] = cpu_to_le32(ncons);
2775 rc = put_entry(buf, sizeof(u32), 1, fp);
2776 if (rc)
2777 return rc;
2778
2779 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2780 if (rc)
2781 return rc;
2782
2783 return 0;
2784}
2785
2786static int role_write(void *vkey, void *datum, void *ptr)
2787{
2788 char *key = vkey;
2789 struct role_datum *role = datum;
2790 struct policy_data *pd = ptr;
2791 void *fp = pd->fp;
2792 struct policydb *p = pd->p;
2793 __le32 buf[3];
2794 size_t items, len;
2795 int rc;
2796
2797 len = strlen(key);
2798 items = 0;
2799 buf[items++] = cpu_to_le32(len);
2800 buf[items++] = cpu_to_le32(role->value);
2801 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2802 buf[items++] = cpu_to_le32(role->bounds);
2803
2804 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2805
2806 rc = put_entry(buf, sizeof(u32), items, fp);
2807 if (rc)
2808 return rc;
2809
2810 rc = put_entry(key, 1, len, fp);
2811 if (rc)
2812 return rc;
2813
2814 rc = ebitmap_write(&role->dominates, fp);
2815 if (rc)
2816 return rc;
2817
2818 rc = ebitmap_write(&role->types, fp);
2819 if (rc)
2820 return rc;
2821
2822 return 0;
2823}
2824
2825static int type_write(void *vkey, void *datum, void *ptr)
2826{
2827 char *key = vkey;
2828 struct type_datum *typdatum = datum;
2829 struct policy_data *pd = ptr;
2830 struct policydb *p = pd->p;
2831 void *fp = pd->fp;
2832 __le32 buf[4];
2833 int rc;
2834 size_t items, len;
2835
2836 len = strlen(key);
2837 items = 0;
2838 buf[items++] = cpu_to_le32(len);
2839 buf[items++] = cpu_to_le32(typdatum->value);
2840 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2841 u32 properties = 0;
2842
2843 if (typdatum->primary)
2844 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2845
2846 if (typdatum->attribute)
2847 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2848
2849 buf[items++] = cpu_to_le32(properties);
2850 buf[items++] = cpu_to_le32(typdatum->bounds);
2851 } else {
2852 buf[items++] = cpu_to_le32(typdatum->primary);
2853 }
2854 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2855 rc = put_entry(buf, sizeof(u32), items, fp);
2856 if (rc)
2857 return rc;
2858
2859 rc = put_entry(key, 1, len, fp);
2860 if (rc)
2861 return rc;
2862
2863 return 0;
2864}
2865
2866static int user_write(void *vkey, void *datum, void *ptr)
2867{
2868 char *key = vkey;
2869 struct user_datum *usrdatum = datum;
2870 struct policy_data *pd = ptr;
2871 struct policydb *p = pd->p;
2872 void *fp = pd->fp;
2873 __le32 buf[3];
2874 size_t items, len;
2875 int rc;
2876
2877 len = strlen(key);
2878 items = 0;
2879 buf[items++] = cpu_to_le32(len);
2880 buf[items++] = cpu_to_le32(usrdatum->value);
2881 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2882 buf[items++] = cpu_to_le32(usrdatum->bounds);
2883 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2884 rc = put_entry(buf, sizeof(u32), items, fp);
2885 if (rc)
2886 return rc;
2887
2888 rc = put_entry(key, 1, len, fp);
2889 if (rc)
2890 return rc;
2891
2892 rc = ebitmap_write(&usrdatum->roles, fp);
2893 if (rc)
2894 return rc;
2895
2896 rc = mls_write_range_helper(&usrdatum->range, fp);
2897 if (rc)
2898 return rc;
2899
2900 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2901 if (rc)
2902 return rc;
2903
2904 return 0;
2905}
2906
2907static int (*write_f[SYM_NUM]) (void *key, void *datum,
2908 void *datap) =
2909{
2910 common_write,
2911 class_write,
2912 role_write,
2913 type_write,
2914 user_write,
2915 cond_write_bool,
2916 sens_write,
2917 cat_write,
2918};
2919
2920static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2921 void *fp)
2922{
2923 unsigned int i, j, rc;
2924 size_t nel, len;
2925 __le32 buf[3];
2926 u32 nodebuf[8];
2927 struct ocontext *c;
2928 for (i = 0; i < info->ocon_num; i++) {
2929 nel = 0;
2930 for (c = p->ocontexts[i]; c; c = c->next)
2931 nel++;
2932 buf[0] = cpu_to_le32(nel);
2933 rc = put_entry(buf, sizeof(u32), 1, fp);
2934 if (rc)
2935 return rc;
2936 for (c = p->ocontexts[i]; c; c = c->next) {
2937 switch (i) {
2938 case OCON_ISID:
2939 buf[0] = cpu_to_le32(c->sid[0]);
2940 rc = put_entry(buf, sizeof(u32), 1, fp);
2941 if (rc)
2942 return rc;
2943 rc = context_write(p, &c->context[0], fp);
2944 if (rc)
2945 return rc;
2946 break;
2947 case OCON_FS:
2948 case OCON_NETIF:
2949 len = strlen(c->u.name);
2950 buf[0] = cpu_to_le32(len);
2951 rc = put_entry(buf, sizeof(u32), 1, fp);
2952 if (rc)
2953 return rc;
2954 rc = put_entry(c->u.name, 1, len, fp);
2955 if (rc)
2956 return rc;
2957 rc = context_write(p, &c->context[0], fp);
2958 if (rc)
2959 return rc;
2960 rc = context_write(p, &c->context[1], fp);
2961 if (rc)
2962 return rc;
2963 break;
2964 case OCON_PORT:
2965 buf[0] = cpu_to_le32(c->u.port.protocol);
2966 buf[1] = cpu_to_le32(c->u.port.low_port);
2967 buf[2] = cpu_to_le32(c->u.port.high_port);
2968 rc = put_entry(buf, sizeof(u32), 3, fp);
2969 if (rc)
2970 return rc;
2971 rc = context_write(p, &c->context[0], fp);
2972 if (rc)
2973 return rc;
2974 break;
2975 case OCON_NODE:
2976 nodebuf[0] = c->u.node.addr; /* network order */
2977 nodebuf[1] = c->u.node.mask; /* network order */
2978 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2979 if (rc)
2980 return rc;
2981 rc = context_write(p, &c->context[0], fp);
2982 if (rc)
2983 return rc;
2984 break;
2985 case OCON_FSUSE:
2986 buf[0] = cpu_to_le32(c->v.behavior);
2987 len = strlen(c->u.name);
2988 buf[1] = cpu_to_le32(len);
2989 rc = put_entry(buf, sizeof(u32), 2, fp);
2990 if (rc)
2991 return rc;
2992 rc = put_entry(c->u.name, 1, len, fp);
2993 if (rc)
2994 return rc;
2995 rc = context_write(p, &c->context[0], fp);
2996 if (rc)
2997 return rc;
2998 break;
2999 case OCON_NODE6:
3000 for (j = 0; j < 4; j++)
3001 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3002 for (j = 0; j < 4; j++)
3003 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3004 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3005 if (rc)
3006 return rc;
3007 rc = context_write(p, &c->context[0], fp);
3008 if (rc)
3009 return rc;
3010 break;
3011 }
3012 }
3013 }
3014 return 0;
3015}
3016
3017static int genfs_write(struct policydb *p, void *fp)
3018{
3019 struct genfs *genfs;
3020 struct ocontext *c;
3021 size_t len;
3022 __le32 buf[1];
3023 int rc;
3024
3025 len = 0;
3026 for (genfs = p->genfs; genfs; genfs = genfs->next)
3027 len++;
3028 buf[0] = cpu_to_le32(len);
3029 rc = put_entry(buf, sizeof(u32), 1, fp);
3030 if (rc)
3031 return rc;
3032 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3033 len = strlen(genfs->fstype);
3034 buf[0] = cpu_to_le32(len);
3035 rc = put_entry(buf, sizeof(u32), 1, fp);
3036 if (rc)
3037 return rc;
3038 rc = put_entry(genfs->fstype, 1, len, fp);
3039 if (rc)
3040 return rc;
3041 len = 0;
3042 for (c = genfs->head; c; c = c->next)
3043 len++;
3044 buf[0] = cpu_to_le32(len);
3045 rc = put_entry(buf, sizeof(u32), 1, fp);
3046 if (rc)
3047 return rc;
3048 for (c = genfs->head; c; c = c->next) {
3049 len = strlen(c->u.name);
3050 buf[0] = cpu_to_le32(len);
3051 rc = put_entry(buf, sizeof(u32), 1, fp);
3052 if (rc)
3053 return rc;
3054 rc = put_entry(c->u.name, 1, len, fp);
3055 if (rc)
3056 return rc;
3057 buf[0] = cpu_to_le32(c->v.sclass);
3058 rc = put_entry(buf, sizeof(u32), 1, fp);
3059 if (rc)
3060 return rc;
3061 rc = context_write(p, &c->context[0], fp);
3062 if (rc)
3063 return rc;
3064 }
3065 }
3066 return 0;
3067}
3068
Eric Paris3f058ef2011-04-28 15:11:21 -04003069static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003070{
3071 int *cnt = ptr;
3072 *cnt = *cnt + 1;
3073
3074 return 0;
3075}
3076
3077static int range_write_helper(void *key, void *data, void *ptr)
3078{
3079 __le32 buf[2];
3080 struct range_trans *rt = key;
3081 struct mls_range *r = data;
3082 struct policy_data *pd = ptr;
3083 void *fp = pd->fp;
3084 struct policydb *p = pd->p;
3085 int rc;
3086
3087 buf[0] = cpu_to_le32(rt->source_type);
3088 buf[1] = cpu_to_le32(rt->target_type);
3089 rc = put_entry(buf, sizeof(u32), 2, fp);
3090 if (rc)
3091 return rc;
3092 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3093 buf[0] = cpu_to_le32(rt->target_class);
3094 rc = put_entry(buf, sizeof(u32), 1, fp);
3095 if (rc)
3096 return rc;
3097 }
3098 rc = mls_write_range_helper(r, fp);
3099 if (rc)
3100 return rc;
3101
3102 return 0;
3103}
3104
3105static int range_write(struct policydb *p, void *fp)
3106{
3107 size_t nel;
3108 __le32 buf[1];
3109 int rc;
3110 struct policy_data pd;
3111
3112 pd.p = p;
3113 pd.fp = fp;
3114
3115 /* count the number of entries in the hashtab */
3116 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003117 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003118 if (rc)
3119 return rc;
3120
3121 buf[0] = cpu_to_le32(nel);
3122 rc = put_entry(buf, sizeof(u32), 1, fp);
3123 if (rc)
3124 return rc;
3125
3126 /* actually write all of the entries */
3127 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3128 if (rc)
3129 return rc;
3130
3131 return 0;
3132}
3133
Eric Paris652bb9b2011-02-01 11:05:40 -05003134static int filename_trans_write(struct policydb *p, void *fp)
3135{
3136 struct filename_trans *ft;
3137 u32 len, nel = 0;
3138 __le32 buf[4];
3139 int rc;
3140
3141 for (ft = p->filename_trans; ft; ft = ft->next)
3142 nel++;
3143
3144 buf[0] = cpu_to_le32(nel);
3145 rc = put_entry(buf, sizeof(u32), 1, fp);
3146 if (rc)
3147 return rc;
3148
3149 for (ft = p->filename_trans; ft; ft = ft->next) {
3150 len = strlen(ft->name);
3151 buf[0] = cpu_to_le32(len);
3152 rc = put_entry(buf, sizeof(u32), 1, fp);
3153 if (rc)
3154 return rc;
3155
3156 rc = put_entry(ft->name, sizeof(char), len, fp);
3157 if (rc)
3158 return rc;
3159
3160 buf[0] = ft->stype;
3161 buf[1] = ft->ttype;
3162 buf[2] = ft->tclass;
3163 buf[3] = ft->otype;
3164
3165 rc = put_entry(buf, sizeof(u32), 4, fp);
3166 if (rc)
3167 return rc;
3168 }
3169 return 0;
3170}
Eric Pariscee74f42010-10-13 17:50:25 -04003171/*
3172 * Write the configuration data in a policy database
3173 * structure to a policy database binary representation
3174 * file.
3175 */
3176int policydb_write(struct policydb *p, void *fp)
3177{
3178 unsigned int i, num_syms;
3179 int rc;
3180 __le32 buf[4];
3181 u32 config;
3182 size_t len;
3183 struct policydb_compat_info *info;
3184
3185 /*
3186 * refuse to write policy older than compressed avtab
3187 * to simplify the writer. There are other tests dropped
3188 * since we assume this throughout the writer code. Be
3189 * careful if you ever try to remove this restriction
3190 */
3191 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3192 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3193 " Because it is less than version %d\n", p->policyvers,
3194 POLICYDB_VERSION_AVTAB);
3195 return -EINVAL;
3196 }
3197
3198 config = 0;
3199 if (p->mls_enabled)
3200 config |= POLICYDB_CONFIG_MLS;
3201
3202 if (p->reject_unknown)
3203 config |= REJECT_UNKNOWN;
3204 if (p->allow_unknown)
3205 config |= ALLOW_UNKNOWN;
3206
3207 /* Write the magic number and string identifiers. */
3208 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3209 len = strlen(POLICYDB_STRING);
3210 buf[1] = cpu_to_le32(len);
3211 rc = put_entry(buf, sizeof(u32), 2, fp);
3212 if (rc)
3213 return rc;
3214 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3215 if (rc)
3216 return rc;
3217
3218 /* Write the version, config, and table sizes. */
3219 info = policydb_lookup_compat(p->policyvers);
3220 if (!info) {
3221 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3222 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003223 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003224 }
3225
3226 buf[0] = cpu_to_le32(p->policyvers);
3227 buf[1] = cpu_to_le32(config);
3228 buf[2] = cpu_to_le32(info->sym_num);
3229 buf[3] = cpu_to_le32(info->ocon_num);
3230
3231 rc = put_entry(buf, sizeof(u32), 4, fp);
3232 if (rc)
3233 return rc;
3234
3235 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3236 rc = ebitmap_write(&p->policycaps, fp);
3237 if (rc)
3238 return rc;
3239 }
3240
3241 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3242 rc = ebitmap_write(&p->permissive_map, fp);
3243 if (rc)
3244 return rc;
3245 }
3246
3247 num_syms = info->sym_num;
3248 for (i = 0; i < num_syms; i++) {
3249 struct policy_data pd;
3250
3251 pd.fp = fp;
3252 pd.p = p;
3253
3254 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3255 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3256
3257 rc = put_entry(buf, sizeof(u32), 2, fp);
3258 if (rc)
3259 return rc;
3260 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3261 if (rc)
3262 return rc;
3263 }
3264
3265 rc = avtab_write(p, &p->te_avtab, fp);
3266 if (rc)
3267 return rc;
3268
3269 rc = cond_write_list(p, p->cond_list, fp);
3270 if (rc)
3271 return rc;
3272
Harry Ciaoc900ff32011-03-25 13:52:00 +08003273 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003274 if (rc)
3275 return rc;
3276
3277 rc = role_allow_write(p->role_allow, fp);
3278 if (rc)
3279 return rc;
3280
Eric Paris652bb9b2011-02-01 11:05:40 -05003281 rc = filename_trans_write(p, fp);
3282 if (rc)
3283 return rc;
3284
Eric Pariscee74f42010-10-13 17:50:25 -04003285 rc = ocontext_write(p, info, fp);
3286 if (rc)
3287 return rc;
3288
3289 rc = genfs_write(p, fp);
3290 if (rc)
3291 return rc;
3292
3293 rc = range_write(p, fp);
3294 if (rc)
3295 return rc;
3296
3297 for (i = 0; i < p->p_types.nprim; i++) {
3298 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3299
3300 BUG_ON(!e);
3301 rc = ebitmap_write(e, fp);
3302 if (rc)
3303 return rc;
3304 }
3305
3306 return 0;
3307}