blob: be9de387283722607dc025fd52095c3fdf6f6883 [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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static struct policydb_compat_info *policydb_lookup_compat(int version)
129{
130 int i;
131 struct policydb_compat_info *info = NULL;
132
Tobias Klauser32725ad2006-01-06 00:11:23 -0800133 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (policydb_compat[i].version == version) {
135 info = &policydb_compat[i];
136 break;
137 }
138 }
139 return info;
140}
141
142/*
143 * Initialize the role table.
144 */
145static int roles_init(struct policydb *p)
146{
147 char *key = NULL;
148 int rc;
149 struct role_datum *role;
150
Eric Paris9398c7f2010-11-23 11:40:08 -0500151 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800152 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500153 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500155
156 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500158 if (role->value != OBJECT_R_VAL)
159 goto out;
160
161 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200162 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500163 if (!key)
164 goto out;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 rc = hashtab_insert(p->p_roles.table, key, role);
167 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Eric Paris9398c7f2010-11-23 11:40:08 -0500170 return 0;
171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500174 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500177static u32 rangetr_hash(struct hashtab *h, const void *k)
178{
179 const struct range_trans *key = k;
180 return (key->source_type + (key->target_type << 3) +
181 (key->target_class << 5)) & (h->size - 1);
182}
183
184static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
185{
186 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400187 int v;
188
189 v = key1->source_type - key2->source_type;
190 if (v)
191 return v;
192
193 v = key1->target_type - key2->target_type;
194 if (v)
195 return v;
196
197 v = key1->target_class - key2->target_class;
198
199 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * Initialize a policy database structure.
204 */
205static int policydb_init(struct policydb *p)
206{
207 int i, rc;
208
209 memset(p, 0, sizeof(*p));
210
211 for (i = 0; i < SYM_NUM; i++) {
212 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
213 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 rc = avtab_init(&p->te_avtab);
218 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 rc = roles_init(p);
222 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 rc = cond_policydb_init(p);
226 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500229 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
230 if (!p->range_tr)
Eric Paris9398c7f2010-11-23 11:40:08 -0500231 goto out;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500232
Paul Moore3bb56b22008-01-29 08:38:19 -0500233 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100234 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500235
Eric Paris9398c7f2010-11-23 11:40:08 -0500236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 for (i = 0; i < SYM_NUM; i++)
239 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500240 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/*
244 * The following *_index functions are used to
245 * define the val_to_name and val_to_struct arrays
246 * in a policy database structure. The val_to_name
247 * arrays are used when converting security context
248 * structures into string representations. The
249 * val_to_struct arrays are used when the attributes
250 * of a class, role, or user are needed.
251 */
252
253static int common_index(void *key, void *datum, void *datap)
254{
255 struct policydb *p;
256 struct common_datum *comdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500257 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 comdatum = datum;
260 p = datap;
261 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
262 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500263
264 fa = p->sym_val_to_name[SYM_COMMONS];
265 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
266 GFP_KERNEL | __GFP_ZERO))
267 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
271static int class_index(void *key, void *datum, void *datap)
272{
273 struct policydb *p;
274 struct class_datum *cladatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500275 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 cladatum = datum;
278 p = datap;
279 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
280 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500281 fa = p->sym_val_to_name[SYM_CLASSES];
282 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
283 GFP_KERNEL | __GFP_ZERO))
284 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 p->class_val_to_struct[cladatum->value - 1] = cladatum;
286 return 0;
287}
288
289static int role_index(void *key, void *datum, void *datap)
290{
291 struct policydb *p;
292 struct role_datum *role;
Eric Parisac76c05b2010-11-29 15:47:09 -0500293 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 role = datum;
296 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900297 if (!role->value
298 || role->value > p->p_roles.nprim
299 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500301
302 fa = p->sym_val_to_name[SYM_ROLES];
303 if (flex_array_put_ptr(fa, role->value - 1, key,
304 GFP_KERNEL | __GFP_ZERO))
305 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 p->role_val_to_struct[role->value - 1] = role;
307 return 0;
308}
309
310static int type_index(void *key, void *datum, void *datap)
311{
312 struct policydb *p;
313 struct type_datum *typdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500314 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 typdatum = datum;
317 p = datap;
318
319 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900320 if (!typdatum->value
321 || typdatum->value > p->p_types.nprim
322 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500324 fa = p->sym_val_to_name[SYM_TYPES];
325 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
326 GFP_KERNEL | __GFP_ZERO))
327 BUG();
328
329 fa = p->type_val_to_struct_array;
330 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500331 GFP_KERNEL | __GFP_ZERO))
332 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
335 return 0;
336}
337
338static int user_index(void *key, void *datum, void *datap)
339{
340 struct policydb *p;
341 struct user_datum *usrdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500342 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 usrdatum = datum;
345 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900346 if (!usrdatum->value
347 || usrdatum->value > p->p_users.nprim
348 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500350
351 fa = p->sym_val_to_name[SYM_USERS];
352 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
353 GFP_KERNEL | __GFP_ZERO))
354 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
356 return 0;
357}
358
359static int sens_index(void *key, void *datum, void *datap)
360{
361 struct policydb *p;
362 struct level_datum *levdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500363 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 levdatum = datum;
366 p = datap;
367
368 if (!levdatum->isalias) {
369 if (!levdatum->level->sens ||
370 levdatum->level->sens > p->p_levels.nprim)
371 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500372 fa = p->sym_val_to_name[SYM_LEVELS];
373 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
374 GFP_KERNEL | __GFP_ZERO))
375 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 return 0;
379}
380
381static int cat_index(void *key, void *datum, void *datap)
382{
383 struct policydb *p;
384 struct cat_datum *catdatum;
Eric Parisac76c05b2010-11-29 15:47:09 -0500385 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 catdatum = datum;
388 p = datap;
389
390 if (!catdatum->isalias) {
391 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
392 return -EINVAL;
Eric Parisac76c05b2010-11-29 15:47:09 -0500393 fa = p->sym_val_to_name[SYM_CATS];
394 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
396 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 return 0;
400}
401
402static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
403{
404 common_index,
405 class_index,
406 role_index,
407 type_index,
408 user_index,
409 cond_index_bool,
410 sens_index,
411 cat_index,
412};
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef DEBUG_HASHES
415static void symtab_hash_eval(struct symtab *s)
416{
417 int i;
418
419 for (i = 0; i < SYM_NUM; i++) {
420 struct hashtab *h = s[i].table;
421 struct hashtab_info info;
422
423 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400424 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 "longest chain length %d\n", symtab_name[i], h->nel,
426 info.slots_used, h->size, info.max_chain_len);
427 }
428}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500429
430static void rangetr_hash_eval(struct hashtab *h)
431{
432 struct hashtab_info info;
433
434 hashtab_stat(h, &info);
435 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
436 "longest chain length %d\n", h->nel,
437 info.slots_used, h->size, info.max_chain_len);
438}
439#else
440static inline void rangetr_hash_eval(struct hashtab *h)
441{
442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif
444
445/*
446 * Define the other val_to_name and val_to_struct arrays
447 * in a policy database structure.
448 *
449 * Caller must clean up on failure.
450 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500451static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Eric Paris9398c7f2010-11-23 11:40:08 -0500453 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
James Morris454d9722008-02-26 20:42:02 +1100455 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100457 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk(", %d sens, %d cats", p->p_levels.nprim,
459 p->p_cats.nprim);
460 printk("\n");
461
James Morris454d9722008-02-26 20:42:02 +1100462 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 p->p_classes.nprim, p->te_avtab.nel);
464
465#ifdef DEBUG_HASHES
466 avtab_hash_eval(&p->te_avtab, "rules");
467 symtab_hash_eval(p->symtab);
468#endif
469
Eric Paris9398c7f2010-11-23 11:40:08 -0500470 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500471 p->class_val_to_struct =
472 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
473 GFP_KERNEL);
474 if (!p->class_val_to_struct)
475 goto out;
476
477 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 p->role_val_to_struct =
479 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400480 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500481 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Eric Paris9398c7f2010-11-23 11:40:08 -0500484 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 p->user_val_to_struct =
486 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400487 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500488 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Eric Paris23bdecb2010-11-29 15:47:09 -0500491 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500492 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500493 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
494 p->p_types.nprim,
495 GFP_KERNEL | __GFP_ZERO);
496 if (!p->type_val_to_struct_array)
497 goto out;
498
499 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
500 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
501 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900502 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900503
Eric Paris9398c7f2010-11-23 11:40:08 -0500504 rc = -ENOMEM;
505 if (cond_init_bool_indexes(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500508 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500509 rc = -ENOMEM;
Eric Parisac76c05b2010-11-29 15:47:09 -0500510 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
511 p->symtab[i].nprim,
512 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500513 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out;
Eric Parisac76c05b2010-11-29 15:47:09 -0500515
516 rc = flex_array_prealloc(p->sym_val_to_name[i],
517 0, p->symtab[i].nprim - 1,
518 GFP_KERNEL | __GFP_ZERO);
519 if (rc)
520 goto out;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
523 if (rc)
524 goto out;
525 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500526 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527out:
528 return rc;
529}
530
531/*
532 * The following *_destroy functions are used to
533 * free any memory allocated for each kind of
534 * symbol data in the policy database.
535 */
536
537static int perm_destroy(void *key, void *datum, void *p)
538{
539 kfree(key);
540 kfree(datum);
541 return 0;
542}
543
544static int common_destroy(void *key, void *datum, void *p)
545{
546 struct common_datum *comdatum;
547
548 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500549 if (datum) {
550 comdatum = datum;
551 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
552 hashtab_destroy(comdatum->permissions.table);
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 kfree(datum);
555 return 0;
556}
557
James Morris6cbda6b2006-11-29 16:50:27 -0500558static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct class_datum *cladatum;
561 struct constraint_node *constraint, *ctemp;
562 struct constraint_expr *e, *etmp;
563
564 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500565 if (datum) {
566 cladatum = datum;
567 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
568 hashtab_destroy(cladatum->permissions.table);
569 constraint = cladatum->constraints;
570 while (constraint) {
571 e = constraint->expr;
572 while (e) {
573 ebitmap_destroy(&e->names);
574 etmp = e;
575 e = e->next;
576 kfree(etmp);
577 }
578 ctemp = constraint;
579 constraint = constraint->next;
580 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric Paris9398c7f2010-11-23 11:40:08 -0500583 constraint = cladatum->validatetrans;
584 while (constraint) {
585 e = constraint->expr;
586 while (e) {
587 ebitmap_destroy(&e->names);
588 etmp = e;
589 e = e->next;
590 kfree(etmp);
591 }
592 ctemp = constraint;
593 constraint = constraint->next;
594 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric Paris9398c7f2010-11-23 11:40:08 -0500597 kfree(cladatum->comkey);
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree(datum);
600 return 0;
601}
602
603static int role_destroy(void *key, void *datum, void *p)
604{
605 struct role_datum *role;
606
607 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500608 if (datum) {
609 role = datum;
610 ebitmap_destroy(&role->dominates);
611 ebitmap_destroy(&role->types);
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree(datum);
614 return 0;
615}
616
617static int type_destroy(void *key, void *datum, void *p)
618{
619 kfree(key);
620 kfree(datum);
621 return 0;
622}
623
624static int user_destroy(void *key, void *datum, void *p)
625{
626 struct user_datum *usrdatum;
627
628 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500629 if (datum) {
630 usrdatum = datum;
631 ebitmap_destroy(&usrdatum->roles);
632 ebitmap_destroy(&usrdatum->range.level[0].cat);
633 ebitmap_destroy(&usrdatum->range.level[1].cat);
634 ebitmap_destroy(&usrdatum->dfltlevel.cat);
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 kfree(datum);
637 return 0;
638}
639
640static int sens_destroy(void *key, void *datum, void *p)
641{
642 struct level_datum *levdatum;
643
644 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500645 if (datum) {
646 levdatum = datum;
647 ebitmap_destroy(&levdatum->level->cat);
648 kfree(levdatum->level);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 kfree(datum);
651 return 0;
652}
653
654static int cat_destroy(void *key, void *datum, void *p)
655{
656 kfree(key);
657 kfree(datum);
658 return 0;
659}
660
661static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
662{
663 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500664 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 role_destroy,
666 type_destroy,
667 user_destroy,
668 cond_destroy_bool,
669 sens_destroy,
670 cat_destroy,
671};
672
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500673static int range_tr_destroy(void *key, void *datum, void *p)
674{
675 struct mls_range *rt = datum;
676 kfree(key);
677 ebitmap_destroy(&rt->level[0].cat);
678 ebitmap_destroy(&rt->level[1].cat);
679 kfree(datum);
680 cond_resched();
681 return 0;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static void ocontext_destroy(struct ocontext *c, int i)
685{
Eric Parisd1b43542010-07-21 12:50:57 -0400686 if (!c)
687 return;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 context_destroy(&c->context[0]);
690 context_destroy(&c->context[1]);
691 if (i == OCON_ISID || i == OCON_FS ||
692 i == OCON_NETIF || i == OCON_FSUSE)
693 kfree(c->u.name);
694 kfree(c);
695}
696
697/*
698 * Free any memory allocated by a policy database structure.
699 */
700void policydb_destroy(struct policydb *p)
701{
702 struct ocontext *c, *ctmp;
703 struct genfs *g, *gtmp;
704 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700705 struct role_allow *ra, *lra = NULL;
706 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400709 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
711 hashtab_destroy(p->symtab[i].table);
712 }
713
Eric Parisac76c05b2010-11-29 15:47:09 -0500714 for (i = 0; i < SYM_NUM; i++) {
715 if (p->sym_val_to_name[i])
716 flex_array_free(p->sym_val_to_name[i]);
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700719 kfree(p->class_val_to_struct);
720 kfree(p->role_val_to_struct);
721 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500722 if (p->type_val_to_struct_array)
723 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 avtab_destroy(&p->te_avtab);
726
727 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400728 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 c = p->ocontexts[i];
730 while (c) {
731 ctmp = c;
732 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400733 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400735 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 g = p->genfs;
739 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400740 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 kfree(g->fstype);
742 c = g->head;
743 while (c) {
744 ctmp = c;
745 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400746 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 gtmp = g;
749 g = g->next;
750 kfree(gtmp);
751 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400752 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 cond_policydb_destroy(p);
755
Stephen Smalley782ebb92005-09-03 15:55:16 -0700756 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400757 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800758 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700759 ltr = tr;
760 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800761 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700762
Eric Paris2ced3df2008-04-17 13:37:12 -0400763 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400764 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800765 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700766 lra = ra;
767 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800768 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700769
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500770 hashtab_map(p->range_tr, range_tr_destroy, NULL);
771 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700772
Eric Paris6371dcd2010-07-29 23:02:34 -0400773 if (p->type_attr_map_array) {
774 for (i = 0; i < p->p_types.nprim; i++) {
775 struct ebitmap *e;
776
777 e = flex_array_get(p->type_attr_map_array, i);
778 if (!e)
779 continue;
780 ebitmap_destroy(e);
781 }
782 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700783 }
Paul Moore3bb56b22008-01-29 08:38:19 -0500784 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100785 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
788}
789
790/*
791 * Load the initial SIDs specified in a policy database
792 * structure into a SID table.
793 */
794int policydb_load_isids(struct policydb *p, struct sidtab *s)
795{
796 struct ocontext *head, *c;
797 int rc;
798
799 rc = sidtab_init(s);
800 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100801 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto out;
803 }
804
805 head = p->ocontexts[OCON_ISID];
806 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500807 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500809 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
810 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out;
812 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500813
814 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
815 if (rc) {
816 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
817 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto out;
819 }
820 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500821 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822out:
823 return rc;
824}
825
Stephen Smalley45e54212007-11-07 10:08:00 -0500826int policydb_class_isvalid(struct policydb *p, unsigned int class)
827{
828 if (!class || class > p->p_classes.nprim)
829 return 0;
830 return 1;
831}
832
833int policydb_role_isvalid(struct policydb *p, unsigned int role)
834{
835 if (!role || role > p->p_roles.nprim)
836 return 0;
837 return 1;
838}
839
840int policydb_type_isvalid(struct policydb *p, unsigned int type)
841{
842 if (!type || type > p->p_types.nprim)
843 return 0;
844 return 1;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
848 * Return 1 if the fields in the security context
849 * structure `c' are valid. Return 0 otherwise.
850 */
851int policydb_context_isvalid(struct policydb *p, struct context *c)
852{
853 struct role_datum *role;
854 struct user_datum *usrdatum;
855
856 if (!c->role || c->role > p->p_roles.nprim)
857 return 0;
858
859 if (!c->user || c->user > p->p_users.nprim)
860 return 0;
861
862 if (!c->type || c->type > p->p_types.nprim)
863 return 0;
864
865 if (c->role != OBJECT_R_VAL) {
866 /*
867 * Role must be authorized for the type.
868 */
869 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500870 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* role may not be associated with type */
872 return 0;
873
874 /*
875 * User must be authorized for the role.
876 */
877 usrdatum = p->user_val_to_struct[c->user - 1];
878 if (!usrdatum)
879 return 0;
880
Eric Paris9398c7f2010-11-23 11:40:08 -0500881 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* user may not be associated with role */
883 return 0;
884 }
885
886 if (!mls_context_isvalid(p, c))
887 return 0;
888
889 return 1;
890}
891
892/*
893 * Read a MLS range structure from a policydb binary
894 * representation file.
895 */
896static int mls_read_range_helper(struct mls_range *r, void *fp)
897{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700898 __le32 buf[2];
899 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int rc;
901
902 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500903 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto out;
905
Eric Paris9398c7f2010-11-23 11:40:08 -0500906 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 items = le32_to_cpu(buf[0]);
908 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100909 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto out;
911 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500914 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100915 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto out;
917 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 r->level[0].sens = le32_to_cpu(buf[0]);
920 if (items > 1)
921 r->level[1].sens = le32_to_cpu(buf[1]);
922 else
923 r->level[1].sens = r->level[0].sens;
924
925 rc = ebitmap_read(&r->level[0].cat, fp);
926 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500927 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto out;
929 }
930 if (items > 1) {
931 rc = ebitmap_read(&r->level[1].cat, fp);
932 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500933 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto bad_high;
935 }
936 } else {
937 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
938 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100939 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto bad_high;
941 }
942 }
943
Eric Paris9398c7f2010-11-23 11:40:08 -0500944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945bad_high:
946 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500947out:
948 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951/*
952 * Read and validate a security context structure
953 * from a policydb binary representation file.
954 */
955static int context_read_and_validate(struct context *c,
956 struct policydb *p,
957 void *fp)
958{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700959 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int rc;
961
962 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500963 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100964 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
966 }
967 c->user = le32_to_cpu(buf[0]);
968 c->role = le32_to_cpu(buf[1]);
969 c->type = le32_to_cpu(buf[2]);
970 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500971 rc = mls_read_range_helper(&c->range, fp);
972 if (rc) {
973 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto out;
975 }
976 }
977
Eric Paris9398c7f2010-11-23 11:40:08 -0500978 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100980 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -0500982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500984 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985out:
986 return rc;
987}
988
989/*
990 * The following *_read functions are used to
991 * read the symbol data from a policy database
992 * binary representation file.
993 */
994
995static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
996{
997 char *key = NULL;
998 struct perm_datum *perdatum;
999 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001000 __le32 buf[2];
1001 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Paris9398c7f2010-11-23 11:40:08 -05001003 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001004 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001005 if (!perdatum)
1006 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001009 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto bad;
1011
1012 len = le32_to_cpu(buf[0]);
1013 perdatum->value = le32_to_cpu(buf[1]);
1014
Eric Paris9398c7f2010-11-23 11:40:08 -05001015 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001016 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001017 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001021 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001023 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 rc = hashtab_insert(h, key, perdatum);
1026 if (rc)
1027 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001028
1029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030bad:
1031 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001032 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1036{
1037 char *key = NULL;
1038 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001039 __le32 buf[4];
1040 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int i, rc;
1042
Eric Paris9398c7f2010-11-23 11:40:08 -05001043 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001044 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001045 if (!comdatum)
1046 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001049 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto bad;
1051
1052 len = le32_to_cpu(buf[0]);
1053 comdatum->value = le32_to_cpu(buf[1]);
1054
1055 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1056 if (rc)
1057 goto bad;
1058 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1059 nel = le32_to_cpu(buf[3]);
1060
Eric Paris9398c7f2010-11-23 11:40:08 -05001061 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001062 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001063 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001067 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001069 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 for (i = 0; i < nel; i++) {
1072 rc = perm_read(p, comdatum->permissions.table, fp);
1073 if (rc)
1074 goto bad;
1075 }
1076
1077 rc = hashtab_insert(h, key, comdatum);
1078 if (rc)
1079 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081bad:
1082 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001083 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001087 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct constraint_node *c, *lc;
1090 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001091 __le32 buf[3];
1092 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int rc, i, j, depth;
1094
1095 lc = NULL;
1096 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001097 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!c)
1099 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Eric Paris2ced3df2008-04-17 13:37:12 -04001101 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001103 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001107 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return rc;
1109 c->permissions = le32_to_cpu(buf[0]);
1110 nexpr = le32_to_cpu(buf[1]);
1111 le = NULL;
1112 depth = -1;
1113 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001114 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!e)
1116 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Eric Paris2ced3df2008-04-17 13:37:12 -04001118 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001120 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001124 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return rc;
1126 e->expr_type = le32_to_cpu(buf[0]);
1127 e->attr = le32_to_cpu(buf[1]);
1128 e->op = le32_to_cpu(buf[2]);
1129
1130 switch (e->expr_type) {
1131 case CEXPR_NOT:
1132 if (depth < 0)
1133 return -EINVAL;
1134 break;
1135 case CEXPR_AND:
1136 case CEXPR_OR:
1137 if (depth < 1)
1138 return -EINVAL;
1139 depth--;
1140 break;
1141 case CEXPR_ATTR:
1142 if (depth == (CEXPR_MAXDEPTH - 1))
1143 return -EINVAL;
1144 depth++;
1145 break;
1146 case CEXPR_NAMES:
1147 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1148 return -EINVAL;
1149 if (depth == (CEXPR_MAXDEPTH - 1))
1150 return -EINVAL;
1151 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001152 rc = ebitmap_read(&e->names, fp);
1153 if (rc)
1154 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 default:
1157 return -EINVAL;
1158 }
1159 le = e;
1160 }
1161 if (depth != 0)
1162 return -EINVAL;
1163 lc = c;
1164 }
1165
1166 return 0;
1167}
1168
1169static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1170{
1171 char *key = NULL;
1172 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001173 __le32 buf[6];
1174 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int i, rc;
1176
Eric Paris9398c7f2010-11-23 11:40:08 -05001177 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001178 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001179 if (!cladatum)
1180 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001183 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto bad;
1185
1186 len = le32_to_cpu(buf[0]);
1187 len2 = le32_to_cpu(buf[1]);
1188 cladatum->value = le32_to_cpu(buf[2]);
1189
1190 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1191 if (rc)
1192 goto bad;
1193 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1194 nel = le32_to_cpu(buf[4]);
1195
1196 ncons = le32_to_cpu(buf[5]);
1197
Eric Paris9398c7f2010-11-23 11:40:08 -05001198 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001199 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001200 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001204 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001206 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001209 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001210 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001211 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001214 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001216 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric Paris9398c7f2010-11-23 11:40:08 -05001218 rc = -EINVAL;
1219 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001221 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto bad;
1223 }
1224 }
1225 for (i = 0; i < nel; i++) {
1226 rc = perm_read(p, cladatum->permissions.table, fp);
1227 if (rc)
1228 goto bad;
1229 }
1230
1231 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1232 if (rc)
1233 goto bad;
1234
1235 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1236 /* grab the validatetrans rules */
1237 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001238 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto bad;
1240 ncons = le32_to_cpu(buf[0]);
1241 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1242 if (rc)
1243 goto bad;
1244 }
1245
1246 rc = hashtab_insert(h, key, cladatum);
1247 if (rc)
1248 goto bad;
1249
Eric Paris9398c7f2010-11-23 11:40:08 -05001250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001252 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001253 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1257{
1258 char *key = NULL;
1259 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001260 int rc, to_read = 2;
1261 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001262 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Eric Paris9398c7f2010-11-23 11:40:08 -05001264 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001265 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001266 if (!role)
1267 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
KaiGai Koheid9250de2008-08-28 16:35:57 +09001269 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1270 to_read = 3;
1271
1272 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001273 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto bad;
1275
1276 len = le32_to_cpu(buf[0]);
1277 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001278 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1279 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Paris9398c7f2010-11-23 11:40:08 -05001281 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001282 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001283 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001287 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001289 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 rc = ebitmap_read(&role->dominates, fp);
1292 if (rc)
1293 goto bad;
1294
1295 rc = ebitmap_read(&role->types, fp);
1296 if (rc)
1297 goto bad;
1298
1299 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001300 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001302 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto bad;
1305 }
1306 rc = 0;
1307 goto bad;
1308 }
1309
1310 rc = hashtab_insert(h, key, role);
1311 if (rc)
1312 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314bad:
1315 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001316 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1320{
1321 char *key = NULL;
1322 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001323 int rc, to_read = 3;
1324 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001325 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Eric Paris9398c7f2010-11-23 11:40:08 -05001327 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001328 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001329 if (!typdatum)
1330 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
KaiGai Koheid9250de2008-08-28 16:35:57 +09001332 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1333 to_read = 4;
1334
1335 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001336 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto bad;
1338
1339 len = le32_to_cpu(buf[0]);
1340 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001341 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1342 u32 prop = le32_to_cpu(buf[2]);
1343
1344 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1345 typdatum->primary = 1;
1346 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1347 typdatum->attribute = 1;
1348
1349 typdatum->bounds = le32_to_cpu(buf[3]);
1350 } else {
1351 typdatum->primary = le32_to_cpu(buf[2]);
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Eric Paris9398c7f2010-11-23 11:40:08 -05001354 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001355 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001356 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001359 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001361 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 rc = hashtab_insert(h, key, typdatum);
1364 if (rc)
1365 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367bad:
1368 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001369 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372
1373/*
1374 * Read a MLS level structure from a policydb binary
1375 * representation file.
1376 */
1377static int mls_read_level(struct mls_level *lp, void *fp)
1378{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001379 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int rc;
1381
1382 memset(lp, 0, sizeof(*lp));
1383
1384 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001385 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001386 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001387 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 lp->sens = le32_to_cpu(buf[0]);
1390
Eric Paris9398c7f2010-11-23 11:40:08 -05001391 rc = ebitmap_read(&lp->cat, fp);
1392 if (rc) {
1393 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1394 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1400{
1401 char *key = NULL;
1402 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001403 int rc, to_read = 2;
1404 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001405 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric Paris9398c7f2010-11-23 11:40:08 -05001407 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001408 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001409 if (!usrdatum)
1410 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
KaiGai Koheid9250de2008-08-28 16:35:57 +09001412 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1413 to_read = 3;
1414
1415 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001416 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto bad;
1418
1419 len = le32_to_cpu(buf[0]);
1420 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001421 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1422 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric Paris9398c7f2010-11-23 11:40:08 -05001424 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001425 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001426 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001429 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001431 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 rc = ebitmap_read(&usrdatum->roles, fp);
1434 if (rc)
1435 goto bad;
1436
1437 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1438 rc = mls_read_range_helper(&usrdatum->range, fp);
1439 if (rc)
1440 goto bad;
1441 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1442 if (rc)
1443 goto bad;
1444 }
1445
1446 rc = hashtab_insert(h, key, usrdatum);
1447 if (rc)
1448 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450bad:
1451 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001452 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1456{
1457 char *key = NULL;
1458 struct level_datum *levdatum;
1459 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001460 __le32 buf[2];
1461 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Eric Paris9398c7f2010-11-23 11:40:08 -05001463 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001464 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001465 if (!levdatum)
1466 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001469 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto bad;
1471
1472 len = le32_to_cpu(buf[0]);
1473 levdatum->isalias = le32_to_cpu(buf[1]);
1474
Eric Paris9398c7f2010-11-23 11:40:08 -05001475 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001476 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001477 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001480 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001482 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Eric Paris9398c7f2010-11-23 11:40:08 -05001484 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001486 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001488
1489 rc = mls_read_level(levdatum->level, fp);
1490 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 rc = hashtab_insert(h, key, levdatum);
1494 if (rc)
1495 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497bad:
1498 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001499 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1503{
1504 char *key = NULL;
1505 struct cat_datum *catdatum;
1506 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001507 __le32 buf[3];
1508 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Eric Paris9398c7f2010-11-23 11:40:08 -05001510 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001511 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001512 if (!catdatum)
1513 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001516 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto bad;
1518
1519 len = le32_to_cpu(buf[0]);
1520 catdatum->value = le32_to_cpu(buf[1]);
1521 catdatum->isalias = le32_to_cpu(buf[2]);
1522
Eric Paris9398c7f2010-11-23 11:40:08 -05001523 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001524 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001525 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001528 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 goto bad;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03001530 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 rc = hashtab_insert(h, key, catdatum);
1533 if (rc)
1534 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536bad:
1537 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001538 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1542{
1543 common_read,
1544 class_read,
1545 role_read,
1546 type_read,
1547 user_read,
1548 cond_read_bool,
1549 sens_read,
1550 cat_read,
1551};
1552
KaiGai Koheid9250de2008-08-28 16:35:57 +09001553static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1554{
1555 struct user_datum *upper, *user;
1556 struct policydb *p = datap;
1557 int depth = 0;
1558
1559 upper = user = datum;
1560 while (upper->bounds) {
1561 struct ebitmap_node *node;
1562 unsigned long bit;
1563
1564 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1565 printk(KERN_ERR "SELinux: user %s: "
1566 "too deep or looped boundary",
1567 (char *) key);
1568 return -EINVAL;
1569 }
1570
1571 upper = p->user_val_to_struct[upper->bounds - 1];
1572 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1573 if (ebitmap_get_bit(&upper->roles, bit))
1574 continue;
1575
1576 printk(KERN_ERR
1577 "SELinux: boundary violated policy: "
1578 "user=%s role=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001579 sym_name(p, SYM_USERS, user->value - 1),
1580 sym_name(p, SYM_ROLES, bit),
1581 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001582
1583 return -EINVAL;
1584 }
1585 }
1586
1587 return 0;
1588}
1589
1590static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1591{
1592 struct role_datum *upper, *role;
1593 struct policydb *p = datap;
1594 int depth = 0;
1595
1596 upper = role = datum;
1597 while (upper->bounds) {
1598 struct ebitmap_node *node;
1599 unsigned long bit;
1600
1601 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1602 printk(KERN_ERR "SELinux: role %s: "
1603 "too deep or looped bounds\n",
1604 (char *) key);
1605 return -EINVAL;
1606 }
1607
1608 upper = p->role_val_to_struct[upper->bounds - 1];
1609 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1610 if (ebitmap_get_bit(&upper->types, bit))
1611 continue;
1612
1613 printk(KERN_ERR
1614 "SELinux: boundary violated policy: "
1615 "role=%s type=%s bounds=%s\n",
Eric Parisac76c05b2010-11-29 15:47:09 -05001616 sym_name(p, SYM_ROLES, role->value - 1),
1617 sym_name(p, SYM_TYPES, bit),
1618 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001619
1620 return -EINVAL;
1621 }
1622 }
1623
1624 return 0;
1625}
1626
1627static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1628{
Eric Parisdaa6d832010-08-03 15:26:05 -04001629 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001630 struct policydb *p = datap;
1631 int depth = 0;
1632
Eric Parisdaa6d832010-08-03 15:26:05 -04001633 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001634 while (upper->bounds) {
1635 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1636 printk(KERN_ERR "SELinux: type %s: "
1637 "too deep or looped boundary\n",
1638 (char *) key);
1639 return -EINVAL;
1640 }
1641
Eric Paris23bdecb2010-11-29 15:47:09 -05001642 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1643 upper->bounds - 1);
1644 BUG_ON(!upper);
1645
KaiGai Koheid9250de2008-08-28 16:35:57 +09001646 if (upper->attribute) {
1647 printk(KERN_ERR "SELinux: type %s: "
1648 "bounded by attribute %s",
1649 (char *) key,
Eric Parisac76c05b2010-11-29 15:47:09 -05001650 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001651 return -EINVAL;
1652 }
1653 }
1654
1655 return 0;
1656}
1657
1658static int policydb_bounds_sanity_check(struct policydb *p)
1659{
1660 int rc;
1661
1662 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1663 return 0;
1664
1665 rc = hashtab_map(p->p_users.table,
1666 user_bounds_sanity_check, p);
1667 if (rc)
1668 return rc;
1669
1670 rc = hashtab_map(p->p_roles.table,
1671 role_bounds_sanity_check, p);
1672 if (rc)
1673 return rc;
1674
1675 rc = hashtab_map(p->p_types.table,
1676 type_bounds_sanity_check, p);
1677 if (rc)
1678 return rc;
1679
1680 return 0;
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683extern int ss_initialized;
1684
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001685u16 string_to_security_class(struct policydb *p, const char *name)
1686{
1687 struct class_datum *cladatum;
1688
1689 cladatum = hashtab_search(p->p_classes.table, name);
1690 if (!cladatum)
1691 return 0;
1692
1693 return cladatum->value;
1694}
1695
1696u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1697{
1698 struct class_datum *cladatum;
1699 struct perm_datum *perdatum = NULL;
1700 struct common_datum *comdatum;
1701
1702 if (!tclass || tclass > p->p_classes.nprim)
1703 return 0;
1704
1705 cladatum = p->class_val_to_struct[tclass-1];
1706 comdatum = cladatum->comdatum;
1707 if (comdatum)
1708 perdatum = hashtab_search(comdatum->permissions.table,
1709 name);
1710 if (!perdatum)
1711 perdatum = hashtab_search(cladatum->permissions.table,
1712 name);
1713 if (!perdatum)
1714 return 0;
1715
1716 return 1U << (perdatum->value-1);
1717}
1718
Eric Paris9ee0c822010-06-11 12:37:05 -04001719static int range_read(struct policydb *p, void *fp)
1720{
1721 struct range_trans *rt = NULL;
1722 struct mls_range *r = NULL;
1723 int i, rc;
1724 __le32 buf[2];
1725 u32 nel;
1726
1727 if (p->policyvers < POLICYDB_VERSION_MLS)
1728 return 0;
1729
1730 rc = next_entry(buf, fp, sizeof(u32));
1731 if (rc)
1732 goto out;
1733
1734 nel = le32_to_cpu(buf[0]);
1735 for (i = 0; i < nel; i++) {
1736 rc = -ENOMEM;
1737 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1738 if (!rt)
1739 goto out;
1740
1741 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1742 if (rc)
1743 goto out;
1744
1745 rt->source_type = le32_to_cpu(buf[0]);
1746 rt->target_type = le32_to_cpu(buf[1]);
1747 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1748 rc = next_entry(buf, fp, sizeof(u32));
1749 if (rc)
1750 goto out;
1751 rt->target_class = le32_to_cpu(buf[0]);
1752 } else
1753 rt->target_class = p->process_class;
1754
1755 rc = -EINVAL;
1756 if (!policydb_type_isvalid(p, rt->source_type) ||
1757 !policydb_type_isvalid(p, rt->target_type) ||
1758 !policydb_class_isvalid(p, rt->target_class))
1759 goto out;
1760
1761 rc = -ENOMEM;
1762 r = kzalloc(sizeof(*r), GFP_KERNEL);
1763 if (!r)
1764 goto out;
1765
1766 rc = mls_read_range_helper(r, fp);
1767 if (rc)
1768 goto out;
1769
1770 rc = -EINVAL;
1771 if (!mls_range_isvalid(p, r)) {
1772 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1773 goto out;
1774 }
1775
1776 rc = hashtab_insert(p->range_tr, rt, r);
1777 if (rc)
1778 goto out;
1779
1780 rt = NULL;
1781 r = NULL;
1782 }
1783 rangetr_hash_eval(p->range_tr);
1784 rc = 0;
1785out:
1786 kfree(rt);
1787 kfree(r);
1788 return rc;
1789}
1790
Eric Parisd1b43542010-07-21 12:50:57 -04001791static int genfs_read(struct policydb *p, void *fp)
1792{
1793 int i, j, rc;
1794 u32 nel, nel2, len, len2;
1795 __le32 buf[1];
1796 struct ocontext *l, *c;
1797 struct ocontext *newc = NULL;
1798 struct genfs *genfs_p, *genfs;
1799 struct genfs *newgenfs = NULL;
1800
1801 rc = next_entry(buf, fp, sizeof(u32));
1802 if (rc)
1803 goto out;
1804 nel = le32_to_cpu(buf[0]);
1805
1806 for (i = 0; i < nel; i++) {
1807 rc = next_entry(buf, fp, sizeof(u32));
1808 if (rc)
1809 goto out;
1810 len = le32_to_cpu(buf[0]);
1811
1812 rc = -ENOMEM;
1813 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1814 if (!newgenfs)
1815 goto out;
1816
1817 rc = -ENOMEM;
1818 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1819 if (!newgenfs->fstype)
1820 goto out;
1821
1822 rc = next_entry(newgenfs->fstype, fp, len);
1823 if (rc)
1824 goto out;
1825
1826 newgenfs->fstype[len] = 0;
1827
1828 for (genfs_p = NULL, genfs = p->genfs; genfs;
1829 genfs_p = genfs, genfs = genfs->next) {
1830 rc = -EINVAL;
1831 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1832 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1833 newgenfs->fstype);
1834 goto out;
1835 }
1836 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1837 break;
1838 }
1839 newgenfs->next = genfs;
1840 if (genfs_p)
1841 genfs_p->next = newgenfs;
1842 else
1843 p->genfs = newgenfs;
1844 genfs = newgenfs;
1845 newgenfs = NULL;
1846
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc)
1849 goto out;
1850
1851 nel2 = le32_to_cpu(buf[0]);
1852 for (j = 0; j < nel2; j++) {
1853 rc = next_entry(buf, fp, sizeof(u32));
1854 if (rc)
1855 goto out;
1856 len = le32_to_cpu(buf[0]);
1857
1858 rc = -ENOMEM;
1859 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1860 if (!newc)
1861 goto out;
1862
1863 rc = -ENOMEM;
1864 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1865 if (!newc->u.name)
1866 goto out;
1867
1868 rc = next_entry(newc->u.name, fp, len);
1869 if (rc)
1870 goto out;
1871 newc->u.name[len] = 0;
1872
1873 rc = next_entry(buf, fp, sizeof(u32));
1874 if (rc)
1875 goto out;
1876
1877 newc->v.sclass = le32_to_cpu(buf[0]);
1878 rc = context_read_and_validate(&newc->context[0], p, fp);
1879 if (rc)
1880 goto out;
1881
1882 for (l = NULL, c = genfs->head; c;
1883 l = c, c = c->next) {
1884 rc = -EINVAL;
1885 if (!strcmp(newc->u.name, c->u.name) &&
1886 (!c->v.sclass || !newc->v.sclass ||
1887 newc->v.sclass == c->v.sclass)) {
1888 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1889 genfs->fstype, c->u.name);
1890 goto out;
1891 }
1892 len = strlen(newc->u.name);
1893 len2 = strlen(c->u.name);
1894 if (len > len2)
1895 break;
1896 }
1897
1898 newc->next = c;
1899 if (l)
1900 l->next = newc;
1901 else
1902 genfs->head = newc;
1903 newc = NULL;
1904 }
1905 }
1906 rc = 0;
1907out:
1908 if (newgenfs)
1909 kfree(newgenfs->fstype);
1910 kfree(newgenfs);
1911 ocontext_destroy(newc, OCON_FSUSE);
1912
1913 return rc;
1914}
1915
Eric Paris692a8a22010-07-21 12:51:03 -04001916static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1917 void *fp)
1918{
1919 int i, j, rc;
1920 u32 nel, len;
1921 __le32 buf[3];
1922 struct ocontext *l, *c;
1923 u32 nodebuf[8];
1924
1925 for (i = 0; i < info->ocon_num; i++) {
1926 rc = next_entry(buf, fp, sizeof(u32));
1927 if (rc)
1928 goto out;
1929 nel = le32_to_cpu(buf[0]);
1930
1931 l = NULL;
1932 for (j = 0; j < nel; j++) {
1933 rc = -ENOMEM;
1934 c = kzalloc(sizeof(*c), GFP_KERNEL);
1935 if (!c)
1936 goto out;
1937 if (l)
1938 l->next = c;
1939 else
1940 p->ocontexts[i] = c;
1941 l = c;
1942
1943 switch (i) {
1944 case OCON_ISID:
1945 rc = next_entry(buf, fp, sizeof(u32));
1946 if (rc)
1947 goto out;
1948
1949 c->sid[0] = le32_to_cpu(buf[0]);
1950 rc = context_read_and_validate(&c->context[0], p, fp);
1951 if (rc)
1952 goto out;
1953 break;
1954 case OCON_FS:
1955 case OCON_NETIF:
1956 rc = next_entry(buf, fp, sizeof(u32));
1957 if (rc)
1958 goto out;
1959 len = le32_to_cpu(buf[0]);
1960
1961 rc = -ENOMEM;
1962 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1963 if (!c->u.name)
1964 goto out;
1965
1966 rc = next_entry(c->u.name, fp, len);
1967 if (rc)
1968 goto out;
1969
1970 c->u.name[len] = 0;
1971 rc = context_read_and_validate(&c->context[0], p, fp);
1972 if (rc)
1973 goto out;
1974 rc = context_read_and_validate(&c->context[1], p, fp);
1975 if (rc)
1976 goto out;
1977 break;
1978 case OCON_PORT:
1979 rc = next_entry(buf, fp, sizeof(u32)*3);
1980 if (rc)
1981 goto out;
1982 c->u.port.protocol = le32_to_cpu(buf[0]);
1983 c->u.port.low_port = le32_to_cpu(buf[1]);
1984 c->u.port.high_port = le32_to_cpu(buf[2]);
1985 rc = context_read_and_validate(&c->context[0], p, fp);
1986 if (rc)
1987 goto out;
1988 break;
1989 case OCON_NODE:
1990 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1991 if (rc)
1992 goto out;
1993 c->u.node.addr = nodebuf[0]; /* network order */
1994 c->u.node.mask = nodebuf[1]; /* network order */
1995 rc = context_read_and_validate(&c->context[0], p, fp);
1996 if (rc)
1997 goto out;
1998 break;
1999 case OCON_FSUSE:
2000 rc = next_entry(buf, fp, sizeof(u32)*2);
2001 if (rc)
2002 goto out;
2003
2004 rc = -EINVAL;
2005 c->v.behavior = le32_to_cpu(buf[0]);
2006 if (c->v.behavior > SECURITY_FS_USE_NONE)
2007 goto out;
2008
2009 rc = -ENOMEM;
2010 len = le32_to_cpu(buf[1]);
2011 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2012 if (!c->u.name)
2013 goto out;
2014
2015 rc = next_entry(c->u.name, fp, len);
2016 if (rc)
2017 goto out;
2018 c->u.name[len] = 0;
2019 rc = context_read_and_validate(&c->context[0], p, fp);
2020 if (rc)
2021 goto out;
2022 break;
2023 case OCON_NODE6: {
2024 int k;
2025
2026 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2027 if (rc)
2028 goto out;
2029 for (k = 0; k < 4; k++)
2030 c->u.node6.addr[k] = nodebuf[k];
2031 for (k = 0; k < 4; k++)
2032 c->u.node6.mask[k] = nodebuf[k+4];
2033 rc = context_read_and_validate(&c->context[0], p, fp);
2034 if (rc)
2035 goto out;
2036 break;
2037 }
2038 }
2039 }
2040 }
2041 rc = 0;
2042out:
2043 return rc;
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
2047 * Read the configuration data from a policy database binary
2048 * representation file into a policy database structure.
2049 */
2050int policydb_read(struct policydb *p, void *fp)
2051{
2052 struct role_allow *ra, *lra;
2053 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002055 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002056 u32 len, nprim, nel;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 char *policydb_str;
2059 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 rc = policydb_init(p);
2062 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002063 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002066 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002067 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 goto bad;
2069
Eric Paris9398c7f2010-11-23 11:40:08 -05002070 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002071 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002072 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002074 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 goto bad;
2076 }
2077
Eric Paris9398c7f2010-11-23 11:40:08 -05002078 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002079 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002081 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 "match expected length %Zu\n",
2083 len, strlen(POLICYDB_STRING));
2084 goto bad;
2085 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002086
2087 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002088 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002090 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto bad;
2093 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002096 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002097 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 kfree(policydb_str);
2099 goto bad;
2100 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002101
2102 rc = -EINVAL;
Vesa-Matti J Karidf4ea8652008-07-20 23:57:01 +03002103 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002105 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 "my string %s\n", policydb_str, POLICYDB_STRING);
2107 kfree(policydb_str);
2108 goto bad;
2109 }
2110 /* Done with policydb_str. */
2111 kfree(policydb_str);
2112 policydb_str = NULL;
2113
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002114 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002116 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Eric Paris9398c7f2010-11-23 11:40:08 -05002119 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002120 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (p->policyvers < POLICYDB_VERSION_MIN ||
2122 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002123 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002124 "my version range %d-%d\n",
2125 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2126 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002129 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002130 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Eric Paris9398c7f2010-11-23 11:40:08 -05002132 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002134 printk(KERN_ERR "SELinux: security policydb version %d "
2135 "(MLS) not backwards compatible\n",
2136 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto bad;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Eric Paris3f120702007-09-21 14:37:10 -04002140 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2141 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Eric Paris9398c7f2010-11-23 11:40:08 -05002143 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2144 rc = ebitmap_read(&p->policycaps, fp);
2145 if (rc)
2146 goto bad;
2147 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002148
Eric Paris9398c7f2010-11-23 11:40:08 -05002149 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2150 rc = ebitmap_read(&p->permissive_map, fp);
2151 if (rc)
2152 goto bad;
2153 }
Eric Paris64dbf072008-03-31 12:17:33 +11002154
Eric Paris9398c7f2010-11-23 11:40:08 -05002155 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 info = policydb_lookup_compat(p->policyvers);
2157 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002158 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 "for version %d\n", p->policyvers);
2160 goto bad;
2161 }
2162
Eric Paris9398c7f2010-11-23 11:40:08 -05002163 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002164 if (le32_to_cpu(buf[2]) != info->sym_num ||
2165 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002166 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002167 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2168 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 info->sym_num, info->ocon_num);
2170 goto bad;
2171 }
2172
2173 for (i = 0; i < info->sym_num; i++) {
2174 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002175 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto bad;
2177 nprim = le32_to_cpu(buf[0]);
2178 nel = le32_to_cpu(buf[1]);
2179 for (j = 0; j < nel; j++) {
2180 rc = read_f[i](p, p->symtab[i].table, fp);
2181 if (rc)
2182 goto bad;
2183 }
2184
2185 p->symtab[i].nprim = nprim;
2186 }
2187
Stephen Smalley45e54212007-11-07 10:08:00 -05002188 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (rc)
2190 goto bad;
2191
2192 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2193 rc = cond_read_list(p, fp);
2194 if (rc)
2195 goto bad;
2196 }
2197
2198 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002199 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 goto bad;
2201 nel = le32_to_cpu(buf[0]);
2202 ltr = NULL;
2203 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002204 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002205 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002206 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002208 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002210 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002213 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002215
2216 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 tr->role = le32_to_cpu(buf[0]);
2218 tr->type = le32_to_cpu(buf[1]);
2219 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002220 if (!policydb_role_isvalid(p, tr->role) ||
2221 !policydb_type_isvalid(p, tr->type) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002222 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002223 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 ltr = tr;
2225 }
2226
2227 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002228 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto bad;
2230 nel = le32_to_cpu(buf[0]);
2231 lra = NULL;
2232 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002233 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002234 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002235 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002237 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002239 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002242 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002244
2245 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 ra->role = le32_to_cpu(buf[0]);
2247 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002248 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002249 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002250 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 lra = ra;
2252 }
2253
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002254 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (rc)
2256 goto bad;
2257
Eric Paris9398c7f2010-11-23 11:40:08 -05002258 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002259 p->process_class = string_to_security_class(p, "process");
2260 if (!p->process_class)
2261 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002262
2263 rc = -EINVAL;
2264 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2265 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002266 if (!p->process_trans_perms)
2267 goto bad;
2268
Eric Paris692a8a22010-07-21 12:51:03 -04002269 rc = ocontext_read(p, info, fp);
2270 if (rc)
2271 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Eric Parisd1b43542010-07-21 12:50:57 -04002273 rc = genfs_read(p, fp);
2274 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Eric Paris9ee0c822010-06-11 12:37:05 -04002277 rc = range_read(p, fp);
2278 if (rc)
2279 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Eric Paris6371dcd2010-07-29 23:02:34 -04002281 rc = -ENOMEM;
2282 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2283 p->p_types.nprim,
2284 GFP_KERNEL | __GFP_ZERO);
2285 if (!p->type_attr_map_array)
2286 goto bad;
2287
2288 /* preallocate so we don't have to worry about the put ever failing */
2289 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2290 GFP_KERNEL | __GFP_ZERO);
2291 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002292 goto bad;
2293
2294 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002295 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2296
2297 BUG_ON(!e);
2298 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002299 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002300 rc = ebitmap_read(e, fp);
2301 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002302 goto bad;
2303 }
2304 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002305 rc = ebitmap_set_bit(e, i, 1);
2306 if (rc)
2307 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002308 }
2309
KaiGai Koheid9250de2008-08-28 16:35:57 +09002310 rc = policydb_bounds_sanity_check(p);
2311 if (rc)
2312 goto bad;
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 rc = 0;
2315out:
2316 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 policydb_destroy(p);
2319 goto out;
2320}
Eric Pariscee74f42010-10-13 17:50:25 -04002321
2322/*
2323 * Write a MLS level structure to a policydb binary
2324 * representation file.
2325 */
2326static int mls_write_level(struct mls_level *l, void *fp)
2327{
2328 __le32 buf[1];
2329 int rc;
2330
2331 buf[0] = cpu_to_le32(l->sens);
2332 rc = put_entry(buf, sizeof(u32), 1, fp);
2333 if (rc)
2334 return rc;
2335
2336 rc = ebitmap_write(&l->cat, fp);
2337 if (rc)
2338 return rc;
2339
2340 return 0;
2341}
2342
2343/*
2344 * Write a MLS range structure to a policydb binary
2345 * representation file.
2346 */
2347static int mls_write_range_helper(struct mls_range *r, void *fp)
2348{
2349 __le32 buf[3];
2350 size_t items;
2351 int rc, eq;
2352
2353 eq = mls_level_eq(&r->level[1], &r->level[0]);
2354
2355 if (eq)
2356 items = 2;
2357 else
2358 items = 3;
2359 buf[0] = cpu_to_le32(items-1);
2360 buf[1] = cpu_to_le32(r->level[0].sens);
2361 if (!eq)
2362 buf[2] = cpu_to_le32(r->level[1].sens);
2363
2364 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2365
2366 rc = put_entry(buf, sizeof(u32), items, fp);
2367 if (rc)
2368 return rc;
2369
2370 rc = ebitmap_write(&r->level[0].cat, fp);
2371 if (rc)
2372 return rc;
2373 if (!eq) {
2374 rc = ebitmap_write(&r->level[1].cat, fp);
2375 if (rc)
2376 return rc;
2377 }
2378
2379 return 0;
2380}
2381
2382static int sens_write(void *vkey, void *datum, void *ptr)
2383{
2384 char *key = vkey;
2385 struct level_datum *levdatum = datum;
2386 struct policy_data *pd = ptr;
2387 void *fp = pd->fp;
2388 __le32 buf[2];
2389 size_t len;
2390 int rc;
2391
2392 len = strlen(key);
2393 buf[0] = cpu_to_le32(len);
2394 buf[1] = cpu_to_le32(levdatum->isalias);
2395 rc = put_entry(buf, sizeof(u32), 2, fp);
2396 if (rc)
2397 return rc;
2398
2399 rc = put_entry(key, 1, len, fp);
2400 if (rc)
2401 return rc;
2402
2403 rc = mls_write_level(levdatum->level, fp);
2404 if (rc)
2405 return rc;
2406
2407 return 0;
2408}
2409
2410static int cat_write(void *vkey, void *datum, void *ptr)
2411{
2412 char *key = vkey;
2413 struct cat_datum *catdatum = datum;
2414 struct policy_data *pd = ptr;
2415 void *fp = pd->fp;
2416 __le32 buf[3];
2417 size_t len;
2418 int rc;
2419
2420 len = strlen(key);
2421 buf[0] = cpu_to_le32(len);
2422 buf[1] = cpu_to_le32(catdatum->value);
2423 buf[2] = cpu_to_le32(catdatum->isalias);
2424 rc = put_entry(buf, sizeof(u32), 3, fp);
2425 if (rc)
2426 return rc;
2427
2428 rc = put_entry(key, 1, len, fp);
2429 if (rc)
2430 return rc;
2431
2432 return 0;
2433}
2434
2435static int role_trans_write(struct role_trans *r, void *fp)
2436{
2437 struct role_trans *tr;
2438 u32 buf[3];
2439 size_t nel;
2440 int rc;
2441
2442 nel = 0;
2443 for (tr = r; tr; tr = tr->next)
2444 nel++;
2445 buf[0] = cpu_to_le32(nel);
2446 rc = put_entry(buf, sizeof(u32), 1, fp);
2447 if (rc)
2448 return rc;
2449 for (tr = r; tr; tr = tr->next) {
2450 buf[0] = cpu_to_le32(tr->role);
2451 buf[1] = cpu_to_le32(tr->type);
2452 buf[2] = cpu_to_le32(tr->new_role);
2453 rc = put_entry(buf, sizeof(u32), 3, fp);
2454 if (rc)
2455 return rc;
2456 }
2457
2458 return 0;
2459}
2460
2461static int role_allow_write(struct role_allow *r, void *fp)
2462{
2463 struct role_allow *ra;
2464 u32 buf[2];
2465 size_t nel;
2466 int rc;
2467
2468 nel = 0;
2469 for (ra = r; ra; ra = ra->next)
2470 nel++;
2471 buf[0] = cpu_to_le32(nel);
2472 rc = put_entry(buf, sizeof(u32), 1, fp);
2473 if (rc)
2474 return rc;
2475 for (ra = r; ra; ra = ra->next) {
2476 buf[0] = cpu_to_le32(ra->role);
2477 buf[1] = cpu_to_le32(ra->new_role);
2478 rc = put_entry(buf, sizeof(u32), 2, fp);
2479 if (rc)
2480 return rc;
2481 }
2482 return 0;
2483}
2484
2485/*
2486 * Write a security context structure
2487 * to a policydb binary representation file.
2488 */
2489static int context_write(struct policydb *p, struct context *c,
2490 void *fp)
2491{
2492 int rc;
2493 __le32 buf[3];
2494
2495 buf[0] = cpu_to_le32(c->user);
2496 buf[1] = cpu_to_le32(c->role);
2497 buf[2] = cpu_to_le32(c->type);
2498
2499 rc = put_entry(buf, sizeof(u32), 3, fp);
2500 if (rc)
2501 return rc;
2502
2503 rc = mls_write_range_helper(&c->range, fp);
2504 if (rc)
2505 return rc;
2506
2507 return 0;
2508}
2509
2510/*
2511 * The following *_write functions are used to
2512 * write the symbol data to a policy database
2513 * binary representation file.
2514 */
2515
2516static int perm_write(void *vkey, void *datum, void *fp)
2517{
2518 char *key = vkey;
2519 struct perm_datum *perdatum = datum;
2520 __le32 buf[2];
2521 size_t len;
2522 int rc;
2523
2524 len = strlen(key);
2525 buf[0] = cpu_to_le32(len);
2526 buf[1] = cpu_to_le32(perdatum->value);
2527 rc = put_entry(buf, sizeof(u32), 2, fp);
2528 if (rc)
2529 return rc;
2530
2531 rc = put_entry(key, 1, len, fp);
2532 if (rc)
2533 return rc;
2534
2535 return 0;
2536}
2537
2538static int common_write(void *vkey, void *datum, void *ptr)
2539{
2540 char *key = vkey;
2541 struct common_datum *comdatum = datum;
2542 struct policy_data *pd = ptr;
2543 void *fp = pd->fp;
2544 __le32 buf[4];
2545 size_t len;
2546 int rc;
2547
2548 len = strlen(key);
2549 buf[0] = cpu_to_le32(len);
2550 buf[1] = cpu_to_le32(comdatum->value);
2551 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2552 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2553 rc = put_entry(buf, sizeof(u32), 4, fp);
2554 if (rc)
2555 return rc;
2556
2557 rc = put_entry(key, 1, len, fp);
2558 if (rc)
2559 return rc;
2560
2561 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2562 if (rc)
2563 return rc;
2564
2565 return 0;
2566}
2567
2568static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2569 void *fp)
2570{
2571 struct constraint_node *c;
2572 struct constraint_expr *e;
2573 __le32 buf[3];
2574 u32 nel;
2575 int rc;
2576
2577 for (c = node; c; c = c->next) {
2578 nel = 0;
2579 for (e = c->expr; e; e = e->next)
2580 nel++;
2581 buf[0] = cpu_to_le32(c->permissions);
2582 buf[1] = cpu_to_le32(nel);
2583 rc = put_entry(buf, sizeof(u32), 2, fp);
2584 if (rc)
2585 return rc;
2586 for (e = c->expr; e; e = e->next) {
2587 buf[0] = cpu_to_le32(e->expr_type);
2588 buf[1] = cpu_to_le32(e->attr);
2589 buf[2] = cpu_to_le32(e->op);
2590 rc = put_entry(buf, sizeof(u32), 3, fp);
2591 if (rc)
2592 return rc;
2593
2594 switch (e->expr_type) {
2595 case CEXPR_NAMES:
2596 rc = ebitmap_write(&e->names, fp);
2597 if (rc)
2598 return rc;
2599 break;
2600 default:
2601 break;
2602 }
2603 }
2604 }
2605
2606 return 0;
2607}
2608
2609static int class_write(void *vkey, void *datum, void *ptr)
2610{
2611 char *key = vkey;
2612 struct class_datum *cladatum = datum;
2613 struct policy_data *pd = ptr;
2614 void *fp = pd->fp;
2615 struct policydb *p = pd->p;
2616 struct constraint_node *c;
2617 __le32 buf[6];
2618 u32 ncons;
2619 size_t len, len2;
2620 int rc;
2621
2622 len = strlen(key);
2623 if (cladatum->comkey)
2624 len2 = strlen(cladatum->comkey);
2625 else
2626 len2 = 0;
2627
2628 ncons = 0;
2629 for (c = cladatum->constraints; c; c = c->next)
2630 ncons++;
2631
2632 buf[0] = cpu_to_le32(len);
2633 buf[1] = cpu_to_le32(len2);
2634 buf[2] = cpu_to_le32(cladatum->value);
2635 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2636 if (cladatum->permissions.table)
2637 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2638 else
2639 buf[4] = 0;
2640 buf[5] = cpu_to_le32(ncons);
2641 rc = put_entry(buf, sizeof(u32), 6, fp);
2642 if (rc)
2643 return rc;
2644
2645 rc = put_entry(key, 1, len, fp);
2646 if (rc)
2647 return rc;
2648
2649 if (cladatum->comkey) {
2650 rc = put_entry(cladatum->comkey, 1, len2, fp);
2651 if (rc)
2652 return rc;
2653 }
2654
2655 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2656 if (rc)
2657 return rc;
2658
2659 rc = write_cons_helper(p, cladatum->constraints, fp);
2660 if (rc)
2661 return rc;
2662
2663 /* write out the validatetrans rule */
2664 ncons = 0;
2665 for (c = cladatum->validatetrans; c; c = c->next)
2666 ncons++;
2667
2668 buf[0] = cpu_to_le32(ncons);
2669 rc = put_entry(buf, sizeof(u32), 1, fp);
2670 if (rc)
2671 return rc;
2672
2673 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2674 if (rc)
2675 return rc;
2676
2677 return 0;
2678}
2679
2680static int role_write(void *vkey, void *datum, void *ptr)
2681{
2682 char *key = vkey;
2683 struct role_datum *role = datum;
2684 struct policy_data *pd = ptr;
2685 void *fp = pd->fp;
2686 struct policydb *p = pd->p;
2687 __le32 buf[3];
2688 size_t items, len;
2689 int rc;
2690
2691 len = strlen(key);
2692 items = 0;
2693 buf[items++] = cpu_to_le32(len);
2694 buf[items++] = cpu_to_le32(role->value);
2695 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2696 buf[items++] = cpu_to_le32(role->bounds);
2697
2698 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2699
2700 rc = put_entry(buf, sizeof(u32), items, fp);
2701 if (rc)
2702 return rc;
2703
2704 rc = put_entry(key, 1, len, fp);
2705 if (rc)
2706 return rc;
2707
2708 rc = ebitmap_write(&role->dominates, fp);
2709 if (rc)
2710 return rc;
2711
2712 rc = ebitmap_write(&role->types, fp);
2713 if (rc)
2714 return rc;
2715
2716 return 0;
2717}
2718
2719static int type_write(void *vkey, void *datum, void *ptr)
2720{
2721 char *key = vkey;
2722 struct type_datum *typdatum = datum;
2723 struct policy_data *pd = ptr;
2724 struct policydb *p = pd->p;
2725 void *fp = pd->fp;
2726 __le32 buf[4];
2727 int rc;
2728 size_t items, len;
2729
2730 len = strlen(key);
2731 items = 0;
2732 buf[items++] = cpu_to_le32(len);
2733 buf[items++] = cpu_to_le32(typdatum->value);
2734 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2735 u32 properties = 0;
2736
2737 if (typdatum->primary)
2738 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2739
2740 if (typdatum->attribute)
2741 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2742
2743 buf[items++] = cpu_to_le32(properties);
2744 buf[items++] = cpu_to_le32(typdatum->bounds);
2745 } else {
2746 buf[items++] = cpu_to_le32(typdatum->primary);
2747 }
2748 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2749 rc = put_entry(buf, sizeof(u32), items, fp);
2750 if (rc)
2751 return rc;
2752
2753 rc = put_entry(key, 1, len, fp);
2754 if (rc)
2755 return rc;
2756
2757 return 0;
2758}
2759
2760static int user_write(void *vkey, void *datum, void *ptr)
2761{
2762 char *key = vkey;
2763 struct user_datum *usrdatum = datum;
2764 struct policy_data *pd = ptr;
2765 struct policydb *p = pd->p;
2766 void *fp = pd->fp;
2767 __le32 buf[3];
2768 size_t items, len;
2769 int rc;
2770
2771 len = strlen(key);
2772 items = 0;
2773 buf[items++] = cpu_to_le32(len);
2774 buf[items++] = cpu_to_le32(usrdatum->value);
2775 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2776 buf[items++] = cpu_to_le32(usrdatum->bounds);
2777 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2778 rc = put_entry(buf, sizeof(u32), items, fp);
2779 if (rc)
2780 return rc;
2781
2782 rc = put_entry(key, 1, len, fp);
2783 if (rc)
2784 return rc;
2785
2786 rc = ebitmap_write(&usrdatum->roles, fp);
2787 if (rc)
2788 return rc;
2789
2790 rc = mls_write_range_helper(&usrdatum->range, fp);
2791 if (rc)
2792 return rc;
2793
2794 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2795 if (rc)
2796 return rc;
2797
2798 return 0;
2799}
2800
2801static int (*write_f[SYM_NUM]) (void *key, void *datum,
2802 void *datap) =
2803{
2804 common_write,
2805 class_write,
2806 role_write,
2807 type_write,
2808 user_write,
2809 cond_write_bool,
2810 sens_write,
2811 cat_write,
2812};
2813
2814static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2815 void *fp)
2816{
2817 unsigned int i, j, rc;
2818 size_t nel, len;
2819 __le32 buf[3];
2820 u32 nodebuf[8];
2821 struct ocontext *c;
2822 for (i = 0; i < info->ocon_num; i++) {
2823 nel = 0;
2824 for (c = p->ocontexts[i]; c; c = c->next)
2825 nel++;
2826 buf[0] = cpu_to_le32(nel);
2827 rc = put_entry(buf, sizeof(u32), 1, fp);
2828 if (rc)
2829 return rc;
2830 for (c = p->ocontexts[i]; c; c = c->next) {
2831 switch (i) {
2832 case OCON_ISID:
2833 buf[0] = cpu_to_le32(c->sid[0]);
2834 rc = put_entry(buf, sizeof(u32), 1, fp);
2835 if (rc)
2836 return rc;
2837 rc = context_write(p, &c->context[0], fp);
2838 if (rc)
2839 return rc;
2840 break;
2841 case OCON_FS:
2842 case OCON_NETIF:
2843 len = strlen(c->u.name);
2844 buf[0] = cpu_to_le32(len);
2845 rc = put_entry(buf, sizeof(u32), 1, fp);
2846 if (rc)
2847 return rc;
2848 rc = put_entry(c->u.name, 1, len, fp);
2849 if (rc)
2850 return rc;
2851 rc = context_write(p, &c->context[0], fp);
2852 if (rc)
2853 return rc;
2854 rc = context_write(p, &c->context[1], fp);
2855 if (rc)
2856 return rc;
2857 break;
2858 case OCON_PORT:
2859 buf[0] = cpu_to_le32(c->u.port.protocol);
2860 buf[1] = cpu_to_le32(c->u.port.low_port);
2861 buf[2] = cpu_to_le32(c->u.port.high_port);
2862 rc = put_entry(buf, sizeof(u32), 3, fp);
2863 if (rc)
2864 return rc;
2865 rc = context_write(p, &c->context[0], fp);
2866 if (rc)
2867 return rc;
2868 break;
2869 case OCON_NODE:
2870 nodebuf[0] = c->u.node.addr; /* network order */
2871 nodebuf[1] = c->u.node.mask; /* network order */
2872 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2873 if (rc)
2874 return rc;
2875 rc = context_write(p, &c->context[0], fp);
2876 if (rc)
2877 return rc;
2878 break;
2879 case OCON_FSUSE:
2880 buf[0] = cpu_to_le32(c->v.behavior);
2881 len = strlen(c->u.name);
2882 buf[1] = cpu_to_le32(len);
2883 rc = put_entry(buf, sizeof(u32), 2, fp);
2884 if (rc)
2885 return rc;
2886 rc = put_entry(c->u.name, 1, len, fp);
2887 if (rc)
2888 return rc;
2889 rc = context_write(p, &c->context[0], fp);
2890 if (rc)
2891 return rc;
2892 break;
2893 case OCON_NODE6:
2894 for (j = 0; j < 4; j++)
2895 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2896 for (j = 0; j < 4; j++)
2897 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2898 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2899 if (rc)
2900 return rc;
2901 rc = context_write(p, &c->context[0], fp);
2902 if (rc)
2903 return rc;
2904 break;
2905 }
2906 }
2907 }
2908 return 0;
2909}
2910
2911static int genfs_write(struct policydb *p, void *fp)
2912{
2913 struct genfs *genfs;
2914 struct ocontext *c;
2915 size_t len;
2916 __le32 buf[1];
2917 int rc;
2918
2919 len = 0;
2920 for (genfs = p->genfs; genfs; genfs = genfs->next)
2921 len++;
2922 buf[0] = cpu_to_le32(len);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2924 if (rc)
2925 return rc;
2926 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2927 len = strlen(genfs->fstype);
2928 buf[0] = cpu_to_le32(len);
2929 rc = put_entry(buf, sizeof(u32), 1, fp);
2930 if (rc)
2931 return rc;
2932 rc = put_entry(genfs->fstype, 1, len, fp);
2933 if (rc)
2934 return rc;
2935 len = 0;
2936 for (c = genfs->head; c; c = c->next)
2937 len++;
2938 buf[0] = cpu_to_le32(len);
2939 rc = put_entry(buf, sizeof(u32), 1, fp);
2940 if (rc)
2941 return rc;
2942 for (c = genfs->head; c; c = c->next) {
2943 len = strlen(c->u.name);
2944 buf[0] = cpu_to_le32(len);
2945 rc = put_entry(buf, sizeof(u32), 1, fp);
2946 if (rc)
2947 return rc;
2948 rc = put_entry(c->u.name, 1, len, fp);
2949 if (rc)
2950 return rc;
2951 buf[0] = cpu_to_le32(c->v.sclass);
2952 rc = put_entry(buf, sizeof(u32), 1, fp);
2953 if (rc)
2954 return rc;
2955 rc = context_write(p, &c->context[0], fp);
2956 if (rc)
2957 return rc;
2958 }
2959 }
2960 return 0;
2961}
2962
2963static int range_count(void *key, void *data, void *ptr)
2964{
2965 int *cnt = ptr;
2966 *cnt = *cnt + 1;
2967
2968 return 0;
2969}
2970
2971static int range_write_helper(void *key, void *data, void *ptr)
2972{
2973 __le32 buf[2];
2974 struct range_trans *rt = key;
2975 struct mls_range *r = data;
2976 struct policy_data *pd = ptr;
2977 void *fp = pd->fp;
2978 struct policydb *p = pd->p;
2979 int rc;
2980
2981 buf[0] = cpu_to_le32(rt->source_type);
2982 buf[1] = cpu_to_le32(rt->target_type);
2983 rc = put_entry(buf, sizeof(u32), 2, fp);
2984 if (rc)
2985 return rc;
2986 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2987 buf[0] = cpu_to_le32(rt->target_class);
2988 rc = put_entry(buf, sizeof(u32), 1, fp);
2989 if (rc)
2990 return rc;
2991 }
2992 rc = mls_write_range_helper(r, fp);
2993 if (rc)
2994 return rc;
2995
2996 return 0;
2997}
2998
2999static int range_write(struct policydb *p, void *fp)
3000{
3001 size_t nel;
3002 __le32 buf[1];
3003 int rc;
3004 struct policy_data pd;
3005
3006 pd.p = p;
3007 pd.fp = fp;
3008
3009 /* count the number of entries in the hashtab */
3010 nel = 0;
3011 rc = hashtab_map(p->range_tr, range_count, &nel);
3012 if (rc)
3013 return rc;
3014
3015 buf[0] = cpu_to_le32(nel);
3016 rc = put_entry(buf, sizeof(u32), 1, fp);
3017 if (rc)
3018 return rc;
3019
3020 /* actually write all of the entries */
3021 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3022 if (rc)
3023 return rc;
3024
3025 return 0;
3026}
3027
3028/*
3029 * Write the configuration data in a policy database
3030 * structure to a policy database binary representation
3031 * file.
3032 */
3033int policydb_write(struct policydb *p, void *fp)
3034{
3035 unsigned int i, num_syms;
3036 int rc;
3037 __le32 buf[4];
3038 u32 config;
3039 size_t len;
3040 struct policydb_compat_info *info;
3041
3042 /*
3043 * refuse to write policy older than compressed avtab
3044 * to simplify the writer. There are other tests dropped
3045 * since we assume this throughout the writer code. Be
3046 * careful if you ever try to remove this restriction
3047 */
3048 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3049 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3050 " Because it is less than version %d\n", p->policyvers,
3051 POLICYDB_VERSION_AVTAB);
3052 return -EINVAL;
3053 }
3054
3055 config = 0;
3056 if (p->mls_enabled)
3057 config |= POLICYDB_CONFIG_MLS;
3058
3059 if (p->reject_unknown)
3060 config |= REJECT_UNKNOWN;
3061 if (p->allow_unknown)
3062 config |= ALLOW_UNKNOWN;
3063
3064 /* Write the magic number and string identifiers. */
3065 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3066 len = strlen(POLICYDB_STRING);
3067 buf[1] = cpu_to_le32(len);
3068 rc = put_entry(buf, sizeof(u32), 2, fp);
3069 if (rc)
3070 return rc;
3071 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3072 if (rc)
3073 return rc;
3074
3075 /* Write the version, config, and table sizes. */
3076 info = policydb_lookup_compat(p->policyvers);
3077 if (!info) {
3078 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3079 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003080 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003081 }
3082
3083 buf[0] = cpu_to_le32(p->policyvers);
3084 buf[1] = cpu_to_le32(config);
3085 buf[2] = cpu_to_le32(info->sym_num);
3086 buf[3] = cpu_to_le32(info->ocon_num);
3087
3088 rc = put_entry(buf, sizeof(u32), 4, fp);
3089 if (rc)
3090 return rc;
3091
3092 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3093 rc = ebitmap_write(&p->policycaps, fp);
3094 if (rc)
3095 return rc;
3096 }
3097
3098 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3099 rc = ebitmap_write(&p->permissive_map, fp);
3100 if (rc)
3101 return rc;
3102 }
3103
3104 num_syms = info->sym_num;
3105 for (i = 0; i < num_syms; i++) {
3106 struct policy_data pd;
3107
3108 pd.fp = fp;
3109 pd.p = p;
3110
3111 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3112 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3113
3114 rc = put_entry(buf, sizeof(u32), 2, fp);
3115 if (rc)
3116 return rc;
3117 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3118 if (rc)
3119 return rc;
3120 }
3121
3122 rc = avtab_write(p, &p->te_avtab, fp);
3123 if (rc)
3124 return rc;
3125
3126 rc = cond_write_list(p, p->cond_list, fp);
3127 if (rc)
3128 return rc;
3129
3130 rc = role_trans_write(p->role_tr, fp);
3131 if (rc)
3132 return rc;
3133
3134 rc = role_allow_write(p->role_allow, fp);
3135 if (rc)
3136 return rc;
3137
3138 rc = ocontext_write(p, info, fp);
3139 if (rc)
3140 return rc;
3141
3142 rc = genfs_write(p, fp);
3143 if (rc)
3144 return rc;
3145
3146 rc = range_write(p, fp);
3147 if (rc)
3148 return rc;
3149
3150 for (i = 0; i < p->p_types.nprim; i++) {
3151 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3152
3153 BUG_ON(!e);
3154 rc = ebitmap_write(e, fp);
3155 if (rc)
3156 return rc;
3157 }
3158
3159 return 0;
3160}