blob: 5adca670e5af6b35d205d997d87219652a751d0b [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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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 Parisac76c052010-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
414/*
415 * Define the common val_to_name array and the class
416 * val_to_name and val_to_struct arrays in a policy
417 * database structure.
418 *
419 * Caller must clean up upon failure.
420 */
421static int policydb_index_classes(struct policydb *p)
422{
423 int rc;
424
Eric Paris9398c7f2010-11-23 11:40:08 -0500425 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500426 p->sym_val_to_name[SYM_COMMONS] = flex_array_alloc(sizeof(char *),
427 p->p_commons.nprim,
428 GFP_KERNEL | __GFP_ZERO);
429 if (!p->sym_val_to_name[SYM_COMMONS])
430 goto out;
431
432 rc = flex_array_prealloc(p->sym_val_to_name[SYM_COMMONS],
433 0, p->p_commons.nprim - 1,
434 GFP_KERNEL | __GFP_ZERO);
435 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 rc = hashtab_map(p->p_commons.table, common_index, p);
439 if (rc)
440 goto out;
441
Eric Paris9398c7f2010-11-23 11:40:08 -0500442 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 p->class_val_to_struct =
444 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500445 if (!p->class_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Eric Paris9398c7f2010-11-23 11:40:08 -0500448 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500449 p->sym_val_to_name[SYM_CLASSES] = flex_array_alloc(sizeof(char *),
450 p->p_classes.nprim,
451 GFP_KERNEL | __GFP_ZERO);
452 if (!p->sym_val_to_name[SYM_CLASSES])
453 goto out;
454
455 rc = flex_array_prealloc(p->sym_val_to_name[SYM_CLASSES],
456 0, p->p_classes.nprim - 1,
457 GFP_KERNEL | __GFP_ZERO);
458 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 rc = hashtab_map(p->p_classes.table, class_index, p);
462out:
463 return rc;
464}
465
466#ifdef DEBUG_HASHES
467static void symtab_hash_eval(struct symtab *s)
468{
469 int i;
470
471 for (i = 0; i < SYM_NUM; i++) {
472 struct hashtab *h = s[i].table;
473 struct hashtab_info info;
474
475 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400476 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 "longest chain length %d\n", symtab_name[i], h->nel,
478 info.slots_used, h->size, info.max_chain_len);
479 }
480}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500481
482static void rangetr_hash_eval(struct hashtab *h)
483{
484 struct hashtab_info info;
485
486 hashtab_stat(h, &info);
487 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
488 "longest chain length %d\n", h->nel,
489 info.slots_used, h->size, info.max_chain_len);
490}
491#else
492static inline void rangetr_hash_eval(struct hashtab *h)
493{
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#endif
496
497/*
498 * Define the other val_to_name and val_to_struct arrays
499 * in a policy database structure.
500 *
501 * Caller must clean up on failure.
502 */
503static int policydb_index_others(struct policydb *p)
504{
Eric Paris9398c7f2010-11-23 11:40:08 -0500505 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
James Morris454d9722008-02-26 20:42:02 +1100507 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100509 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 printk(", %d sens, %d cats", p->p_levels.nprim,
511 p->p_cats.nprim);
512 printk("\n");
513
James Morris454d9722008-02-26 20:42:02 +1100514 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 p->p_classes.nprim, p->te_avtab.nel);
516
517#ifdef DEBUG_HASHES
518 avtab_hash_eval(&p->te_avtab, "rules");
519 symtab_hash_eval(p->symtab);
520#endif
521
Eric Paris9398c7f2010-11-23 11:40:08 -0500522 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 p->role_val_to_struct =
524 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400525 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500526 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Eric Paris9398c7f2010-11-23 11:40:08 -0500529 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 p->user_val_to_struct =
531 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400532 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500533 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Eric Paris23bdecb2010-11-29 15:47:09 -0500536 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500537 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500538 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
539 p->p_types.nprim,
540 GFP_KERNEL | __GFP_ZERO);
541 if (!p->type_val_to_struct_array)
542 goto out;
543
544 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
545 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
546 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900547 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900548
Eric Paris9398c7f2010-11-23 11:40:08 -0500549 rc = -ENOMEM;
550 if (cond_init_bool_indexes(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 for (i = SYM_ROLES; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500554 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500555 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
556 p->symtab[i].nprim,
557 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500558 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500560
561 rc = flex_array_prealloc(p->sym_val_to_name[i],
562 0, p->symtab[i].nprim - 1,
563 GFP_KERNEL | __GFP_ZERO);
564 if (rc)
565 goto out;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
568 if (rc)
569 goto out;
570 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500571 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572out:
573 return rc;
574}
575
576/*
577 * The following *_destroy functions are used to
578 * free any memory allocated for each kind of
579 * symbol data in the policy database.
580 */
581
582static int perm_destroy(void *key, void *datum, void *p)
583{
584 kfree(key);
585 kfree(datum);
586 return 0;
587}
588
589static int common_destroy(void *key, void *datum, void *p)
590{
591 struct common_datum *comdatum;
592
593 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500594 if (datum) {
595 comdatum = datum;
596 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
597 hashtab_destroy(comdatum->permissions.table);
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree(datum);
600 return 0;
601}
602
James Morris6cbda6b2006-11-29 16:50:27 -0500603static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct class_datum *cladatum;
606 struct constraint_node *constraint, *ctemp;
607 struct constraint_expr *e, *etmp;
608
609 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500610 if (datum) {
611 cladatum = datum;
612 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
613 hashtab_destroy(cladatum->permissions.table);
614 constraint = cladatum->constraints;
615 while (constraint) {
616 e = constraint->expr;
617 while (e) {
618 ebitmap_destroy(&e->names);
619 etmp = e;
620 e = e->next;
621 kfree(etmp);
622 }
623 ctemp = constraint;
624 constraint = constraint->next;
625 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Eric Paris9398c7f2010-11-23 11:40:08 -0500628 constraint = cladatum->validatetrans;
629 while (constraint) {
630 e = constraint->expr;
631 while (e) {
632 ebitmap_destroy(&e->names);
633 etmp = e;
634 e = e->next;
635 kfree(etmp);
636 }
637 ctemp = constraint;
638 constraint = constraint->next;
639 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Paris9398c7f2010-11-23 11:40:08 -0500642 kfree(cladatum->comkey);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 kfree(datum);
645 return 0;
646}
647
648static int role_destroy(void *key, void *datum, void *p)
649{
650 struct role_datum *role;
651
652 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500653 if (datum) {
654 role = datum;
655 ebitmap_destroy(&role->dominates);
656 ebitmap_destroy(&role->types);
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 kfree(datum);
659 return 0;
660}
661
662static int type_destroy(void *key, void *datum, void *p)
663{
664 kfree(key);
665 kfree(datum);
666 return 0;
667}
668
669static int user_destroy(void *key, void *datum, void *p)
670{
671 struct user_datum *usrdatum;
672
673 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500674 if (datum) {
675 usrdatum = datum;
676 ebitmap_destroy(&usrdatum->roles);
677 ebitmap_destroy(&usrdatum->range.level[0].cat);
678 ebitmap_destroy(&usrdatum->range.level[1].cat);
679 ebitmap_destroy(&usrdatum->dfltlevel.cat);
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 kfree(datum);
682 return 0;
683}
684
685static int sens_destroy(void *key, void *datum, void *p)
686{
687 struct level_datum *levdatum;
688
689 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500690 if (datum) {
691 levdatum = datum;
692 ebitmap_destroy(&levdatum->level->cat);
693 kfree(levdatum->level);
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kfree(datum);
696 return 0;
697}
698
699static int cat_destroy(void *key, void *datum, void *p)
700{
701 kfree(key);
702 kfree(datum);
703 return 0;
704}
705
706static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
707{
708 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500709 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 role_destroy,
711 type_destroy,
712 user_destroy,
713 cond_destroy_bool,
714 sens_destroy,
715 cat_destroy,
716};
717
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500718static int range_tr_destroy(void *key, void *datum, void *p)
719{
720 struct mls_range *rt = datum;
721 kfree(key);
722 ebitmap_destroy(&rt->level[0].cat);
723 ebitmap_destroy(&rt->level[1].cat);
724 kfree(datum);
725 cond_resched();
726 return 0;
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void ocontext_destroy(struct ocontext *c, int i)
730{
Eric Parisd1b43542010-07-21 12:50:57 -0400731 if (!c)
732 return;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 context_destroy(&c->context[0]);
735 context_destroy(&c->context[1]);
736 if (i == OCON_ISID || i == OCON_FS ||
737 i == OCON_NETIF || i == OCON_FSUSE)
738 kfree(c->u.name);
739 kfree(c);
740}
741
742/*
743 * Free any memory allocated by a policy database structure.
744 */
745void policydb_destroy(struct policydb *p)
746{
747 struct ocontext *c, *ctmp;
748 struct genfs *g, *gtmp;
749 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700750 struct role_allow *ra, *lra = NULL;
751 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400754 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
756 hashtab_destroy(p->symtab[i].table);
757 }
758
Eric Parisac76c052010-11-29 15:47:09 -0500759 for (i = 0; i < SYM_NUM; i++) {
760 if (p->sym_val_to_name[i])
761 flex_array_free(p->sym_val_to_name[i]);
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700764 kfree(p->class_val_to_struct);
765 kfree(p->role_val_to_struct);
766 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500767 if (p->type_val_to_struct_array)
768 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 avtab_destroy(&p->te_avtab);
771
772 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400773 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 c = p->ocontexts[i];
775 while (c) {
776 ctmp = c;
777 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400778 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400780 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
783 g = p->genfs;
784 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400785 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 kfree(g->fstype);
787 c = g->head;
788 while (c) {
789 ctmp = c;
790 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400791 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 gtmp = g;
794 g = g->next;
795 kfree(gtmp);
796 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400797 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 cond_policydb_destroy(p);
800
Stephen Smalley782ebb92005-09-03 15:55:16 -0700801 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400802 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800803 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700804 ltr = tr;
805 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800806 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700807
Eric Paris2ced3df2008-04-17 13:37:12 -0400808 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400809 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800810 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700811 lra = ra;
812 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800813 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700814
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500815 hashtab_map(p->range_tr, range_tr_destroy, NULL);
816 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700817
Eric Paris6371dcd2010-07-29 23:02:34 -0400818 if (p->type_attr_map_array) {
819 for (i = 0; i < p->p_types.nprim; i++) {
820 struct ebitmap *e;
821
822 e = flex_array_get(p->type_attr_map_array, i);
823 if (!e)
824 continue;
825 ebitmap_destroy(e);
826 }
827 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700828 }
Paul Moore3bb56b22008-01-29 08:38:19 -0500829 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100830 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833}
834
835/*
836 * Load the initial SIDs specified in a policy database
837 * structure into a SID table.
838 */
839int policydb_load_isids(struct policydb *p, struct sidtab *s)
840{
841 struct ocontext *head, *c;
842 int rc;
843
844 rc = sidtab_init(s);
845 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100846 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
848 }
849
850 head = p->ocontexts[OCON_ISID];
851 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500852 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500854 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
855 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto out;
857 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500858
859 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
860 if (rc) {
861 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
862 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto out;
864 }
865 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500866 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867out:
868 return rc;
869}
870
Stephen Smalley45e54212007-11-07 10:08:00 -0500871int policydb_class_isvalid(struct policydb *p, unsigned int class)
872{
873 if (!class || class > p->p_classes.nprim)
874 return 0;
875 return 1;
876}
877
878int policydb_role_isvalid(struct policydb *p, unsigned int role)
879{
880 if (!role || role > p->p_roles.nprim)
881 return 0;
882 return 1;
883}
884
885int policydb_type_isvalid(struct policydb *p, unsigned int type)
886{
887 if (!type || type > p->p_types.nprim)
888 return 0;
889 return 1;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Return 1 if the fields in the security context
894 * structure `c' are valid. Return 0 otherwise.
895 */
896int policydb_context_isvalid(struct policydb *p, struct context *c)
897{
898 struct role_datum *role;
899 struct user_datum *usrdatum;
900
901 if (!c->role || c->role > p->p_roles.nprim)
902 return 0;
903
904 if (!c->user || c->user > p->p_users.nprim)
905 return 0;
906
907 if (!c->type || c->type > p->p_types.nprim)
908 return 0;
909
910 if (c->role != OBJECT_R_VAL) {
911 /*
912 * Role must be authorized for the type.
913 */
914 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500915 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* role may not be associated with type */
917 return 0;
918
919 /*
920 * User must be authorized for the role.
921 */
922 usrdatum = p->user_val_to_struct[c->user - 1];
923 if (!usrdatum)
924 return 0;
925
Eric Paris9398c7f2010-11-23 11:40:08 -0500926 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* user may not be associated with role */
928 return 0;
929 }
930
931 if (!mls_context_isvalid(p, c))
932 return 0;
933
934 return 1;
935}
936
937/*
938 * Read a MLS range structure from a policydb binary
939 * representation file.
940 */
941static int mls_read_range_helper(struct mls_range *r, void *fp)
942{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700943 __le32 buf[2];
944 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int rc;
946
947 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500948 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto out;
950
Eric Paris9398c7f2010-11-23 11:40:08 -0500951 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 items = le32_to_cpu(buf[0]);
953 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100954 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
956 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500959 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100960 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out;
962 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 r->level[0].sens = le32_to_cpu(buf[0]);
965 if (items > 1)
966 r->level[1].sens = le32_to_cpu(buf[1]);
967 else
968 r->level[1].sens = r->level[0].sens;
969
970 rc = ebitmap_read(&r->level[0].cat, fp);
971 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500972 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 goto out;
974 }
975 if (items > 1) {
976 rc = ebitmap_read(&r->level[1].cat, fp);
977 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500978 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 goto bad_high;
980 }
981 } else {
982 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
983 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100984 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto bad_high;
986 }
987 }
988
Eric Paris9398c7f2010-11-23 11:40:08 -0500989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990bad_high:
991 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500992out:
993 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996/*
997 * Read and validate a security context structure
998 * from a policydb binary representation file.
999 */
1000static int context_read_and_validate(struct context *c,
1001 struct policydb *p,
1002 void *fp)
1003{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001004 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int rc;
1006
1007 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001008 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001009 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto out;
1011 }
1012 c->user = le32_to_cpu(buf[0]);
1013 c->role = le32_to_cpu(buf[1]);
1014 c->type = le32_to_cpu(buf[2]);
1015 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001016 rc = mls_read_range_helper(&c->range, fp);
1017 if (rc) {
1018 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 goto out;
1020 }
1021 }
1022
Eric Paris9398c7f2010-11-23 11:40:08 -05001023 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001025 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001029 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030out:
1031 return rc;
1032}
1033
1034/*
1035 * The following *_read functions are used to
1036 * read the symbol data from a policy database
1037 * binary representation file.
1038 */
1039
1040static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1041{
1042 char *key = NULL;
1043 struct perm_datum *perdatum;
1044 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001045 __le32 buf[2];
1046 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Eric Paris9398c7f2010-11-23 11:40:08 -05001048 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001049 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001050 if (!perdatum)
1051 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001054 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto bad;
1056
1057 len = le32_to_cpu(buf[0]);
1058 perdatum->value = le32_to_cpu(buf[1]);
1059
Eric Paris9398c7f2010-11-23 11:40:08 -05001060 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001061 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001062 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001066 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001068 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 rc = hashtab_insert(h, key, perdatum);
1071 if (rc)
1072 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001073
1074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075bad:
1076 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001077 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1081{
1082 char *key = NULL;
1083 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001084 __le32 buf[4];
1085 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int i, rc;
1087
Eric Paris9398c7f2010-11-23 11:40:08 -05001088 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001089 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001090 if (!comdatum)
1091 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001094 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto bad;
1096
1097 len = le32_to_cpu(buf[0]);
1098 comdatum->value = le32_to_cpu(buf[1]);
1099
1100 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1101 if (rc)
1102 goto bad;
1103 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1104 nel = le32_to_cpu(buf[3]);
1105
Eric Paris9398c7f2010-11-23 11:40:08 -05001106 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001107 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001108 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001112 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001114 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 for (i = 0; i < nel; i++) {
1117 rc = perm_read(p, comdatum->permissions.table, fp);
1118 if (rc)
1119 goto bad;
1120 }
1121
1122 rc = hashtab_insert(h, key, comdatum);
1123 if (rc)
1124 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126bad:
1127 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001128 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001132 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct constraint_node *c, *lc;
1135 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001136 __le32 buf[3];
1137 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int rc, i, j, depth;
1139
1140 lc = NULL;
1141 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001142 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!c)
1144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Eric Paris2ced3df2008-04-17 13:37:12 -04001146 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001148 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001152 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return rc;
1154 c->permissions = le32_to_cpu(buf[0]);
1155 nexpr = le32_to_cpu(buf[1]);
1156 le = NULL;
1157 depth = -1;
1158 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001159 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!e)
1161 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Eric Paris2ced3df2008-04-17 13:37:12 -04001163 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001165 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001169 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return rc;
1171 e->expr_type = le32_to_cpu(buf[0]);
1172 e->attr = le32_to_cpu(buf[1]);
1173 e->op = le32_to_cpu(buf[2]);
1174
1175 switch (e->expr_type) {
1176 case CEXPR_NOT:
1177 if (depth < 0)
1178 return -EINVAL;
1179 break;
1180 case CEXPR_AND:
1181 case CEXPR_OR:
1182 if (depth < 1)
1183 return -EINVAL;
1184 depth--;
1185 break;
1186 case CEXPR_ATTR:
1187 if (depth == (CEXPR_MAXDEPTH - 1))
1188 return -EINVAL;
1189 depth++;
1190 break;
1191 case CEXPR_NAMES:
1192 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1193 return -EINVAL;
1194 if (depth == (CEXPR_MAXDEPTH - 1))
1195 return -EINVAL;
1196 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001197 rc = ebitmap_read(&e->names, fp);
1198 if (rc)
1199 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 default:
1202 return -EINVAL;
1203 }
1204 le = e;
1205 }
1206 if (depth != 0)
1207 return -EINVAL;
1208 lc = c;
1209 }
1210
1211 return 0;
1212}
1213
1214static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1215{
1216 char *key = NULL;
1217 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001218 __le32 buf[6];
1219 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int i, rc;
1221
Eric Paris9398c7f2010-11-23 11:40:08 -05001222 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001223 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001224 if (!cladatum)
1225 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001228 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto bad;
1230
1231 len = le32_to_cpu(buf[0]);
1232 len2 = le32_to_cpu(buf[1]);
1233 cladatum->value = le32_to_cpu(buf[2]);
1234
1235 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1236 if (rc)
1237 goto bad;
1238 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1239 nel = le32_to_cpu(buf[4]);
1240
1241 ncons = le32_to_cpu(buf[5]);
1242
Eric Paris9398c7f2010-11-23 11:40:08 -05001243 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001244 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001245 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001249 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001251 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001254 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001255 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001256 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001259 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001261 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Eric Paris9398c7f2010-11-23 11:40:08 -05001263 rc = -EINVAL;
1264 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001266 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto bad;
1268 }
1269 }
1270 for (i = 0; i < nel; i++) {
1271 rc = perm_read(p, cladatum->permissions.table, fp);
1272 if (rc)
1273 goto bad;
1274 }
1275
1276 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1277 if (rc)
1278 goto bad;
1279
1280 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1281 /* grab the validatetrans rules */
1282 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001283 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto bad;
1285 ncons = le32_to_cpu(buf[0]);
1286 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1287 if (rc)
1288 goto bad;
1289 }
1290
1291 rc = hashtab_insert(h, key, cladatum);
1292 if (rc)
1293 goto bad;
1294
Eric Paris9398c7f2010-11-23 11:40:08 -05001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001297 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001298 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1302{
1303 char *key = NULL;
1304 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001305 int rc, to_read = 2;
1306 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001307 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Eric Paris9398c7f2010-11-23 11:40:08 -05001309 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001310 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001311 if (!role)
1312 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
KaiGai Koheid9250de2008-08-28 16:35:57 +09001314 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1315 to_read = 3;
1316
1317 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001318 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto bad;
1320
1321 len = le32_to_cpu(buf[0]);
1322 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001323 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1324 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Eric Paris9398c7f2010-11-23 11:40:08 -05001326 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001327 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001328 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001332 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001334 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 rc = ebitmap_read(&role->dominates, fp);
1337 if (rc)
1338 goto bad;
1339
1340 rc = ebitmap_read(&role->types, fp);
1341 if (rc)
1342 goto bad;
1343
1344 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001345 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001347 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 goto bad;
1350 }
1351 rc = 0;
1352 goto bad;
1353 }
1354
1355 rc = hashtab_insert(h, key, role);
1356 if (rc)
1357 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359bad:
1360 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001361 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1365{
1366 char *key = NULL;
1367 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001368 int rc, to_read = 3;
1369 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001370 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Eric Paris9398c7f2010-11-23 11:40:08 -05001372 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001373 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001374 if (!typdatum)
1375 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
KaiGai Koheid9250de2008-08-28 16:35:57 +09001377 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1378 to_read = 4;
1379
1380 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001381 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto bad;
1383
1384 len = le32_to_cpu(buf[0]);
1385 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001386 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1387 u32 prop = le32_to_cpu(buf[2]);
1388
1389 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1390 typdatum->primary = 1;
1391 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1392 typdatum->attribute = 1;
1393
1394 typdatum->bounds = le32_to_cpu(buf[3]);
1395 } else {
1396 typdatum->primary = le32_to_cpu(buf[2]);
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Eric Paris9398c7f2010-11-23 11:40:08 -05001399 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001400 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001401 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001404 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001406 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 rc = hashtab_insert(h, key, typdatum);
1409 if (rc)
1410 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412bad:
1413 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001414 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417
1418/*
1419 * Read a MLS level structure from a policydb binary
1420 * representation file.
1421 */
1422static int mls_read_level(struct mls_level *lp, void *fp)
1423{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001424 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int rc;
1426
1427 memset(lp, 0, sizeof(*lp));
1428
1429 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001430 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001431 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001432 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 lp->sens = le32_to_cpu(buf[0]);
1435
Eric Paris9398c7f2010-11-23 11:40:08 -05001436 rc = ebitmap_read(&lp->cat, fp);
1437 if (rc) {
1438 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1439 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1445{
1446 char *key = NULL;
1447 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001448 int rc, to_read = 2;
1449 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001450 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Eric Paris9398c7f2010-11-23 11:40:08 -05001452 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001453 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001454 if (!usrdatum)
1455 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
KaiGai Koheid9250de2008-08-28 16:35:57 +09001457 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1458 to_read = 3;
1459
1460 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001461 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto bad;
1463
1464 len = le32_to_cpu(buf[0]);
1465 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001466 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1467 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Eric Paris9398c7f2010-11-23 11:40:08 -05001469 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001470 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001471 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001474 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001476 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 rc = ebitmap_read(&usrdatum->roles, fp);
1479 if (rc)
1480 goto bad;
1481
1482 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1483 rc = mls_read_range_helper(&usrdatum->range, fp);
1484 if (rc)
1485 goto bad;
1486 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1487 if (rc)
1488 goto bad;
1489 }
1490
1491 rc = hashtab_insert(h, key, usrdatum);
1492 if (rc)
1493 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495bad:
1496 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001497 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1501{
1502 char *key = NULL;
1503 struct level_datum *levdatum;
1504 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001505 __le32 buf[2];
1506 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Eric Paris9398c7f2010-11-23 11:40:08 -05001508 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001509 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001510 if (!levdatum)
1511 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001514 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 goto bad;
1516
1517 len = le32_to_cpu(buf[0]);
1518 levdatum->isalias = le32_to_cpu(buf[1]);
1519
Eric Paris9398c7f2010-11-23 11:40:08 -05001520 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001521 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001522 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001525 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001527 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Eric Paris9398c7f2010-11-23 11:40:08 -05001529 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001531 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001533
1534 rc = mls_read_level(levdatum->level, fp);
1535 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 rc = hashtab_insert(h, key, levdatum);
1539 if (rc)
1540 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542bad:
1543 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001544 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1548{
1549 char *key = NULL;
1550 struct cat_datum *catdatum;
1551 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001552 __le32 buf[3];
1553 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Eric Paris9398c7f2010-11-23 11:40:08 -05001555 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001556 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001557 if (!catdatum)
1558 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001561 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 goto bad;
1563
1564 len = le32_to_cpu(buf[0]);
1565 catdatum->value = le32_to_cpu(buf[1]);
1566 catdatum->isalias = le32_to_cpu(buf[2]);
1567
Eric Paris9398c7f2010-11-23 11:40:08 -05001568 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001569 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001570 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001573 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001575 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 rc = hashtab_insert(h, key, catdatum);
1578 if (rc)
1579 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581bad:
1582 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001583 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1587{
1588 common_read,
1589 class_read,
1590 role_read,
1591 type_read,
1592 user_read,
1593 cond_read_bool,
1594 sens_read,
1595 cat_read,
1596};
1597
KaiGai Koheid9250de2008-08-28 16:35:57 +09001598static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1599{
1600 struct user_datum *upper, *user;
1601 struct policydb *p = datap;
1602 int depth = 0;
1603
1604 upper = user = datum;
1605 while (upper->bounds) {
1606 struct ebitmap_node *node;
1607 unsigned long bit;
1608
1609 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1610 printk(KERN_ERR "SELinux: user %s: "
1611 "too deep or looped boundary",
1612 (char *) key);
1613 return -EINVAL;
1614 }
1615
1616 upper = p->user_val_to_struct[upper->bounds - 1];
1617 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1618 if (ebitmap_get_bit(&upper->roles, bit))
1619 continue;
1620
1621 printk(KERN_ERR
1622 "SELinux: boundary violated policy: "
1623 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001624 sym_name(p, SYM_USERS, user->value - 1),
1625 sym_name(p, SYM_ROLES, bit),
1626 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001627
1628 return -EINVAL;
1629 }
1630 }
1631
1632 return 0;
1633}
1634
1635static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1636{
1637 struct role_datum *upper, *role;
1638 struct policydb *p = datap;
1639 int depth = 0;
1640
1641 upper = role = datum;
1642 while (upper->bounds) {
1643 struct ebitmap_node *node;
1644 unsigned long bit;
1645
1646 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1647 printk(KERN_ERR "SELinux: role %s: "
1648 "too deep or looped bounds\n",
1649 (char *) key);
1650 return -EINVAL;
1651 }
1652
1653 upper = p->role_val_to_struct[upper->bounds - 1];
1654 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1655 if (ebitmap_get_bit(&upper->types, bit))
1656 continue;
1657
1658 printk(KERN_ERR
1659 "SELinux: boundary violated policy: "
1660 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001661 sym_name(p, SYM_ROLES, role->value - 1),
1662 sym_name(p, SYM_TYPES, bit),
1663 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001664
1665 return -EINVAL;
1666 }
1667 }
1668
1669 return 0;
1670}
1671
1672static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1673{
Eric Parisdaa6d832010-08-03 15:26:05 -04001674 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001675 struct policydb *p = datap;
1676 int depth = 0;
1677
Eric Parisdaa6d832010-08-03 15:26:05 -04001678 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001679 while (upper->bounds) {
1680 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1681 printk(KERN_ERR "SELinux: type %s: "
1682 "too deep or looped boundary\n",
1683 (char *) key);
1684 return -EINVAL;
1685 }
1686
Eric Paris23bdecb2010-11-29 15:47:09 -05001687 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1688 upper->bounds - 1);
1689 BUG_ON(!upper);
1690
KaiGai Koheid9250de2008-08-28 16:35:57 +09001691 if (upper->attribute) {
1692 printk(KERN_ERR "SELinux: type %s: "
1693 "bounded by attribute %s",
1694 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001695 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001696 return -EINVAL;
1697 }
1698 }
1699
1700 return 0;
1701}
1702
1703static int policydb_bounds_sanity_check(struct policydb *p)
1704{
1705 int rc;
1706
1707 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1708 return 0;
1709
1710 rc = hashtab_map(p->p_users.table,
1711 user_bounds_sanity_check, p);
1712 if (rc)
1713 return rc;
1714
1715 rc = hashtab_map(p->p_roles.table,
1716 role_bounds_sanity_check, p);
1717 if (rc)
1718 return rc;
1719
1720 rc = hashtab_map(p->p_types.table,
1721 type_bounds_sanity_check, p);
1722 if (rc)
1723 return rc;
1724
1725 return 0;
1726}
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728extern int ss_initialized;
1729
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001730u16 string_to_security_class(struct policydb *p, const char *name)
1731{
1732 struct class_datum *cladatum;
1733
1734 cladatum = hashtab_search(p->p_classes.table, name);
1735 if (!cladatum)
1736 return 0;
1737
1738 return cladatum->value;
1739}
1740
1741u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1742{
1743 struct class_datum *cladatum;
1744 struct perm_datum *perdatum = NULL;
1745 struct common_datum *comdatum;
1746
1747 if (!tclass || tclass > p->p_classes.nprim)
1748 return 0;
1749
1750 cladatum = p->class_val_to_struct[tclass-1];
1751 comdatum = cladatum->comdatum;
1752 if (comdatum)
1753 perdatum = hashtab_search(comdatum->permissions.table,
1754 name);
1755 if (!perdatum)
1756 perdatum = hashtab_search(cladatum->permissions.table,
1757 name);
1758 if (!perdatum)
1759 return 0;
1760
1761 return 1U << (perdatum->value-1);
1762}
1763
Eric Paris9ee0c822010-06-11 12:37:05 -04001764static int range_read(struct policydb *p, void *fp)
1765{
1766 struct range_trans *rt = NULL;
1767 struct mls_range *r = NULL;
1768 int i, rc;
1769 __le32 buf[2];
1770 u32 nel;
1771
1772 if (p->policyvers < POLICYDB_VERSION_MLS)
1773 return 0;
1774
1775 rc = next_entry(buf, fp, sizeof(u32));
1776 if (rc)
1777 goto out;
1778
1779 nel = le32_to_cpu(buf[0]);
1780 for (i = 0; i < nel; i++) {
1781 rc = -ENOMEM;
1782 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1783 if (!rt)
1784 goto out;
1785
1786 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1787 if (rc)
1788 goto out;
1789
1790 rt->source_type = le32_to_cpu(buf[0]);
1791 rt->target_type = le32_to_cpu(buf[1]);
1792 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1793 rc = next_entry(buf, fp, sizeof(u32));
1794 if (rc)
1795 goto out;
1796 rt->target_class = le32_to_cpu(buf[0]);
1797 } else
1798 rt->target_class = p->process_class;
1799
1800 rc = -EINVAL;
1801 if (!policydb_type_isvalid(p, rt->source_type) ||
1802 !policydb_type_isvalid(p, rt->target_type) ||
1803 !policydb_class_isvalid(p, rt->target_class))
1804 goto out;
1805
1806 rc = -ENOMEM;
1807 r = kzalloc(sizeof(*r), GFP_KERNEL);
1808 if (!r)
1809 goto out;
1810
1811 rc = mls_read_range_helper(r, fp);
1812 if (rc)
1813 goto out;
1814
1815 rc = -EINVAL;
1816 if (!mls_range_isvalid(p, r)) {
1817 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1818 goto out;
1819 }
1820
1821 rc = hashtab_insert(p->range_tr, rt, r);
1822 if (rc)
1823 goto out;
1824
1825 rt = NULL;
1826 r = NULL;
1827 }
1828 rangetr_hash_eval(p->range_tr);
1829 rc = 0;
1830out:
1831 kfree(rt);
1832 kfree(r);
1833 return rc;
1834}
1835
Eric Parisd1b43542010-07-21 12:50:57 -04001836static int genfs_read(struct policydb *p, void *fp)
1837{
1838 int i, j, rc;
1839 u32 nel, nel2, len, len2;
1840 __le32 buf[1];
1841 struct ocontext *l, *c;
1842 struct ocontext *newc = NULL;
1843 struct genfs *genfs_p, *genfs;
1844 struct genfs *newgenfs = NULL;
1845
1846 rc = next_entry(buf, fp, sizeof(u32));
1847 if (rc)
1848 goto out;
1849 nel = le32_to_cpu(buf[0]);
1850
1851 for (i = 0; i < nel; i++) {
1852 rc = next_entry(buf, fp, sizeof(u32));
1853 if (rc)
1854 goto out;
1855 len = le32_to_cpu(buf[0]);
1856
1857 rc = -ENOMEM;
1858 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1859 if (!newgenfs)
1860 goto out;
1861
1862 rc = -ENOMEM;
1863 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1864 if (!newgenfs->fstype)
1865 goto out;
1866
1867 rc = next_entry(newgenfs->fstype, fp, len);
1868 if (rc)
1869 goto out;
1870
1871 newgenfs->fstype[len] = 0;
1872
1873 for (genfs_p = NULL, genfs = p->genfs; genfs;
1874 genfs_p = genfs, genfs = genfs->next) {
1875 rc = -EINVAL;
1876 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1877 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1878 newgenfs->fstype);
1879 goto out;
1880 }
1881 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1882 break;
1883 }
1884 newgenfs->next = genfs;
1885 if (genfs_p)
1886 genfs_p->next = newgenfs;
1887 else
1888 p->genfs = newgenfs;
1889 genfs = newgenfs;
1890 newgenfs = NULL;
1891
1892 rc = next_entry(buf, fp, sizeof(u32));
1893 if (rc)
1894 goto out;
1895
1896 nel2 = le32_to_cpu(buf[0]);
1897 for (j = 0; j < nel2; j++) {
1898 rc = next_entry(buf, fp, sizeof(u32));
1899 if (rc)
1900 goto out;
1901 len = le32_to_cpu(buf[0]);
1902
1903 rc = -ENOMEM;
1904 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1905 if (!newc)
1906 goto out;
1907
1908 rc = -ENOMEM;
1909 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1910 if (!newc->u.name)
1911 goto out;
1912
1913 rc = next_entry(newc->u.name, fp, len);
1914 if (rc)
1915 goto out;
1916 newc->u.name[len] = 0;
1917
1918 rc = next_entry(buf, fp, sizeof(u32));
1919 if (rc)
1920 goto out;
1921
1922 newc->v.sclass = le32_to_cpu(buf[0]);
1923 rc = context_read_and_validate(&newc->context[0], p, fp);
1924 if (rc)
1925 goto out;
1926
1927 for (l = NULL, c = genfs->head; c;
1928 l = c, c = c->next) {
1929 rc = -EINVAL;
1930 if (!strcmp(newc->u.name, c->u.name) &&
1931 (!c->v.sclass || !newc->v.sclass ||
1932 newc->v.sclass == c->v.sclass)) {
1933 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1934 genfs->fstype, c->u.name);
1935 goto out;
1936 }
1937 len = strlen(newc->u.name);
1938 len2 = strlen(c->u.name);
1939 if (len > len2)
1940 break;
1941 }
1942
1943 newc->next = c;
1944 if (l)
1945 l->next = newc;
1946 else
1947 genfs->head = newc;
1948 newc = NULL;
1949 }
1950 }
1951 rc = 0;
1952out:
1953 if (newgenfs)
1954 kfree(newgenfs->fstype);
1955 kfree(newgenfs);
1956 ocontext_destroy(newc, OCON_FSUSE);
1957
1958 return rc;
1959}
1960
Eric Paris692a8a22010-07-21 12:51:03 -04001961static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1962 void *fp)
1963{
1964 int i, j, rc;
1965 u32 nel, len;
1966 __le32 buf[3];
1967 struct ocontext *l, *c;
1968 u32 nodebuf[8];
1969
1970 for (i = 0; i < info->ocon_num; i++) {
1971 rc = next_entry(buf, fp, sizeof(u32));
1972 if (rc)
1973 goto out;
1974 nel = le32_to_cpu(buf[0]);
1975
1976 l = NULL;
1977 for (j = 0; j < nel; j++) {
1978 rc = -ENOMEM;
1979 c = kzalloc(sizeof(*c), GFP_KERNEL);
1980 if (!c)
1981 goto out;
1982 if (l)
1983 l->next = c;
1984 else
1985 p->ocontexts[i] = c;
1986 l = c;
1987
1988 switch (i) {
1989 case OCON_ISID:
1990 rc = next_entry(buf, fp, sizeof(u32));
1991 if (rc)
1992 goto out;
1993
1994 c->sid[0] = le32_to_cpu(buf[0]);
1995 rc = context_read_and_validate(&c->context[0], p, fp);
1996 if (rc)
1997 goto out;
1998 break;
1999 case OCON_FS:
2000 case OCON_NETIF:
2001 rc = next_entry(buf, fp, sizeof(u32));
2002 if (rc)
2003 goto out;
2004 len = le32_to_cpu(buf[0]);
2005
2006 rc = -ENOMEM;
2007 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2008 if (!c->u.name)
2009 goto out;
2010
2011 rc = next_entry(c->u.name, fp, len);
2012 if (rc)
2013 goto out;
2014
2015 c->u.name[len] = 0;
2016 rc = context_read_and_validate(&c->context[0], p, fp);
2017 if (rc)
2018 goto out;
2019 rc = context_read_and_validate(&c->context[1], p, fp);
2020 if (rc)
2021 goto out;
2022 break;
2023 case OCON_PORT:
2024 rc = next_entry(buf, fp, sizeof(u32)*3);
2025 if (rc)
2026 goto out;
2027 c->u.port.protocol = le32_to_cpu(buf[0]);
2028 c->u.port.low_port = le32_to_cpu(buf[1]);
2029 c->u.port.high_port = le32_to_cpu(buf[2]);
2030 rc = context_read_and_validate(&c->context[0], p, fp);
2031 if (rc)
2032 goto out;
2033 break;
2034 case OCON_NODE:
2035 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2036 if (rc)
2037 goto out;
2038 c->u.node.addr = nodebuf[0]; /* network order */
2039 c->u.node.mask = nodebuf[1]; /* network order */
2040 rc = context_read_and_validate(&c->context[0], p, fp);
2041 if (rc)
2042 goto out;
2043 break;
2044 case OCON_FSUSE:
2045 rc = next_entry(buf, fp, sizeof(u32)*2);
2046 if (rc)
2047 goto out;
2048
2049 rc = -EINVAL;
2050 c->v.behavior = le32_to_cpu(buf[0]);
2051 if (c->v.behavior > SECURITY_FS_USE_NONE)
2052 goto out;
2053
2054 rc = -ENOMEM;
2055 len = le32_to_cpu(buf[1]);
2056 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2057 if (!c->u.name)
2058 goto out;
2059
2060 rc = next_entry(c->u.name, fp, len);
2061 if (rc)
2062 goto out;
2063 c->u.name[len] = 0;
2064 rc = context_read_and_validate(&c->context[0], p, fp);
2065 if (rc)
2066 goto out;
2067 break;
2068 case OCON_NODE6: {
2069 int k;
2070
2071 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2072 if (rc)
2073 goto out;
2074 for (k = 0; k < 4; k++)
2075 c->u.node6.addr[k] = nodebuf[k];
2076 for (k = 0; k < 4; k++)
2077 c->u.node6.mask[k] = nodebuf[k+4];
2078 rc = context_read_and_validate(&c->context[0], p, fp);
2079 if (rc)
2080 goto out;
2081 break;
2082 }
2083 }
2084 }
2085 }
2086 rc = 0;
2087out:
2088 return rc;
2089}
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * Read the configuration data from a policy database binary
2093 * representation file into a policy database structure.
2094 */
2095int policydb_read(struct policydb *p, void *fp)
2096{
2097 struct role_allow *ra, *lra;
2098 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002100 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002101 u32 len, nprim, nel;
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 char *policydb_str;
2104 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 rc = policydb_init(p);
2107 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002108 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002111 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002112 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto bad;
2114
Eric Paris9398c7f2010-11-23 11:40:08 -05002115 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002116 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002117 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002119 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto bad;
2121 }
2122
Eric Paris9398c7f2010-11-23 11:40:08 -05002123 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002124 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002126 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 "match expected length %Zu\n",
2128 len, strlen(POLICYDB_STRING));
2129 goto bad;
2130 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002131
2132 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002133 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002135 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto bad;
2138 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002141 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002142 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 kfree(policydb_str);
2144 goto bad;
2145 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002146
2147 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002148 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002150 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 "my string %s\n", policydb_str, POLICYDB_STRING);
2152 kfree(policydb_str);
2153 goto bad;
2154 }
2155 /* Done with policydb_str. */
2156 kfree(policydb_str);
2157 policydb_str = NULL;
2158
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002159 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002161 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Eric Paris9398c7f2010-11-23 11:40:08 -05002164 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002165 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (p->policyvers < POLICYDB_VERSION_MIN ||
2167 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002168 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002169 "my version range %d-%d\n",
2170 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2171 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002174 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002175 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Eric Paris9398c7f2010-11-23 11:40:08 -05002177 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002179 printk(KERN_ERR "SELinux: security policydb version %d "
2180 "(MLS) not backwards compatible\n",
2181 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto bad;
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Eric Paris3f120702007-09-21 14:37:10 -04002185 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2186 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Eric Paris9398c7f2010-11-23 11:40:08 -05002188 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2189 rc = ebitmap_read(&p->policycaps, fp);
2190 if (rc)
2191 goto bad;
2192 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002193
Eric Paris9398c7f2010-11-23 11:40:08 -05002194 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2195 rc = ebitmap_read(&p->permissive_map, fp);
2196 if (rc)
2197 goto bad;
2198 }
Eric Paris64dbf072008-03-31 12:17:33 +11002199
Eric Paris9398c7f2010-11-23 11:40:08 -05002200 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 info = policydb_lookup_compat(p->policyvers);
2202 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002203 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 "for version %d\n", p->policyvers);
2205 goto bad;
2206 }
2207
Eric Paris9398c7f2010-11-23 11:40:08 -05002208 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002209 if (le32_to_cpu(buf[2]) != info->sym_num ||
2210 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002211 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002212 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2213 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 info->sym_num, info->ocon_num);
2215 goto bad;
2216 }
2217
2218 for (i = 0; i < info->sym_num; i++) {
2219 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002220 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto bad;
2222 nprim = le32_to_cpu(buf[0]);
2223 nel = le32_to_cpu(buf[1]);
2224 for (j = 0; j < nel; j++) {
2225 rc = read_f[i](p, p->symtab[i].table, fp);
2226 if (rc)
2227 goto bad;
2228 }
2229
2230 p->symtab[i].nprim = nprim;
2231 }
2232
Stephen Smalley45e54212007-11-07 10:08:00 -05002233 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (rc)
2235 goto bad;
2236
2237 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2238 rc = cond_read_list(p, fp);
2239 if (rc)
2240 goto bad;
2241 }
2242
2243 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002244 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 goto bad;
2246 nel = le32_to_cpu(buf[0]);
2247 ltr = NULL;
2248 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002249 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002250 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002251 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002253 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002258 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002260
2261 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 tr->role = le32_to_cpu(buf[0]);
2263 tr->type = le32_to_cpu(buf[1]);
2264 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002265 if (!policydb_role_isvalid(p, tr->role) ||
2266 !policydb_type_isvalid(p, tr->type) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002267 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002268 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 ltr = tr;
2270 }
2271
2272 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002273 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto bad;
2275 nel = le32_to_cpu(buf[0]);
2276 lra = NULL;
2277 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002278 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002279 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002280 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002282 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002284 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002287 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002289
2290 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 ra->role = le32_to_cpu(buf[0]);
2292 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002293 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002294 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002295 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 lra = ra;
2297 }
2298
2299 rc = policydb_index_classes(p);
2300 if (rc)
2301 goto bad;
2302
2303 rc = policydb_index_others(p);
2304 if (rc)
2305 goto bad;
2306
Eric Paris9398c7f2010-11-23 11:40:08 -05002307 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002308 p->process_class = string_to_security_class(p, "process");
2309 if (!p->process_class)
2310 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002311
2312 rc = -EINVAL;
2313 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2314 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002315 if (!p->process_trans_perms)
2316 goto bad;
2317
Eric Paris692a8a22010-07-21 12:51:03 -04002318 rc = ocontext_read(p, info, fp);
2319 if (rc)
2320 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Eric Parisd1b43542010-07-21 12:50:57 -04002322 rc = genfs_read(p, fp);
2323 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Eric Paris9ee0c822010-06-11 12:37:05 -04002326 rc = range_read(p, fp);
2327 if (rc)
2328 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Eric Paris6371dcd2010-07-29 23:02:34 -04002330 rc = -ENOMEM;
2331 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2332 p->p_types.nprim,
2333 GFP_KERNEL | __GFP_ZERO);
2334 if (!p->type_attr_map_array)
2335 goto bad;
2336
2337 /* preallocate so we don't have to worry about the put ever failing */
2338 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2339 GFP_KERNEL | __GFP_ZERO);
2340 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002341 goto bad;
2342
2343 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002344 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2345
2346 BUG_ON(!e);
2347 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002348 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002349 rc = ebitmap_read(e, fp);
2350 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002351 goto bad;
2352 }
2353 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002354 rc = ebitmap_set_bit(e, i, 1);
2355 if (rc)
2356 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002357 }
2358
KaiGai Koheid9250de2008-08-28 16:35:57 +09002359 rc = policydb_bounds_sanity_check(p);
2360 if (rc)
2361 goto bad;
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 rc = 0;
2364out:
2365 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 policydb_destroy(p);
2368 goto out;
2369}
Eric Pariscee74f42010-10-13 17:50:25 -04002370
2371/*
2372 * Write a MLS level structure to a policydb binary
2373 * representation file.
2374 */
2375static int mls_write_level(struct mls_level *l, void *fp)
2376{
2377 __le32 buf[1];
2378 int rc;
2379
2380 buf[0] = cpu_to_le32(l->sens);
2381 rc = put_entry(buf, sizeof(u32), 1, fp);
2382 if (rc)
2383 return rc;
2384
2385 rc = ebitmap_write(&l->cat, fp);
2386 if (rc)
2387 return rc;
2388
2389 return 0;
2390}
2391
2392/*
2393 * Write a MLS range structure to a policydb binary
2394 * representation file.
2395 */
2396static int mls_write_range_helper(struct mls_range *r, void *fp)
2397{
2398 __le32 buf[3];
2399 size_t items;
2400 int rc, eq;
2401
2402 eq = mls_level_eq(&r->level[1], &r->level[0]);
2403
2404 if (eq)
2405 items = 2;
2406 else
2407 items = 3;
2408 buf[0] = cpu_to_le32(items-1);
2409 buf[1] = cpu_to_le32(r->level[0].sens);
2410 if (!eq)
2411 buf[2] = cpu_to_le32(r->level[1].sens);
2412
2413 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2414
2415 rc = put_entry(buf, sizeof(u32), items, fp);
2416 if (rc)
2417 return rc;
2418
2419 rc = ebitmap_write(&r->level[0].cat, fp);
2420 if (rc)
2421 return rc;
2422 if (!eq) {
2423 rc = ebitmap_write(&r->level[1].cat, fp);
2424 if (rc)
2425 return rc;
2426 }
2427
2428 return 0;
2429}
2430
2431static int sens_write(void *vkey, void *datum, void *ptr)
2432{
2433 char *key = vkey;
2434 struct level_datum *levdatum = datum;
2435 struct policy_data *pd = ptr;
2436 void *fp = pd->fp;
2437 __le32 buf[2];
2438 size_t len;
2439 int rc;
2440
2441 len = strlen(key);
2442 buf[0] = cpu_to_le32(len);
2443 buf[1] = cpu_to_le32(levdatum->isalias);
2444 rc = put_entry(buf, sizeof(u32), 2, fp);
2445 if (rc)
2446 return rc;
2447
2448 rc = put_entry(key, 1, len, fp);
2449 if (rc)
2450 return rc;
2451
2452 rc = mls_write_level(levdatum->level, fp);
2453 if (rc)
2454 return rc;
2455
2456 return 0;
2457}
2458
2459static int cat_write(void *vkey, void *datum, void *ptr)
2460{
2461 char *key = vkey;
2462 struct cat_datum *catdatum = datum;
2463 struct policy_data *pd = ptr;
2464 void *fp = pd->fp;
2465 __le32 buf[3];
2466 size_t len;
2467 int rc;
2468
2469 len = strlen(key);
2470 buf[0] = cpu_to_le32(len);
2471 buf[1] = cpu_to_le32(catdatum->value);
2472 buf[2] = cpu_to_le32(catdatum->isalias);
2473 rc = put_entry(buf, sizeof(u32), 3, fp);
2474 if (rc)
2475 return rc;
2476
2477 rc = put_entry(key, 1, len, fp);
2478 if (rc)
2479 return rc;
2480
2481 return 0;
2482}
2483
2484static int role_trans_write(struct role_trans *r, void *fp)
2485{
2486 struct role_trans *tr;
2487 u32 buf[3];
2488 size_t nel;
2489 int rc;
2490
2491 nel = 0;
2492 for (tr = r; tr; tr = tr->next)
2493 nel++;
2494 buf[0] = cpu_to_le32(nel);
2495 rc = put_entry(buf, sizeof(u32), 1, fp);
2496 if (rc)
2497 return rc;
2498 for (tr = r; tr; tr = tr->next) {
2499 buf[0] = cpu_to_le32(tr->role);
2500 buf[1] = cpu_to_le32(tr->type);
2501 buf[2] = cpu_to_le32(tr->new_role);
2502 rc = put_entry(buf, sizeof(u32), 3, fp);
2503 if (rc)
2504 return rc;
2505 }
2506
2507 return 0;
2508}
2509
2510static int role_allow_write(struct role_allow *r, void *fp)
2511{
2512 struct role_allow *ra;
2513 u32 buf[2];
2514 size_t nel;
2515 int rc;
2516
2517 nel = 0;
2518 for (ra = r; ra; ra = ra->next)
2519 nel++;
2520 buf[0] = cpu_to_le32(nel);
2521 rc = put_entry(buf, sizeof(u32), 1, fp);
2522 if (rc)
2523 return rc;
2524 for (ra = r; ra; ra = ra->next) {
2525 buf[0] = cpu_to_le32(ra->role);
2526 buf[1] = cpu_to_le32(ra->new_role);
2527 rc = put_entry(buf, sizeof(u32), 2, fp);
2528 if (rc)
2529 return rc;
2530 }
2531 return 0;
2532}
2533
2534/*
2535 * Write a security context structure
2536 * to a policydb binary representation file.
2537 */
2538static int context_write(struct policydb *p, struct context *c,
2539 void *fp)
2540{
2541 int rc;
2542 __le32 buf[3];
2543
2544 buf[0] = cpu_to_le32(c->user);
2545 buf[1] = cpu_to_le32(c->role);
2546 buf[2] = cpu_to_le32(c->type);
2547
2548 rc = put_entry(buf, sizeof(u32), 3, fp);
2549 if (rc)
2550 return rc;
2551
2552 rc = mls_write_range_helper(&c->range, fp);
2553 if (rc)
2554 return rc;
2555
2556 return 0;
2557}
2558
2559/*
2560 * The following *_write functions are used to
2561 * write the symbol data to a policy database
2562 * binary representation file.
2563 */
2564
2565static int perm_write(void *vkey, void *datum, void *fp)
2566{
2567 char *key = vkey;
2568 struct perm_datum *perdatum = datum;
2569 __le32 buf[2];
2570 size_t len;
2571 int rc;
2572
2573 len = strlen(key);
2574 buf[0] = cpu_to_le32(len);
2575 buf[1] = cpu_to_le32(perdatum->value);
2576 rc = put_entry(buf, sizeof(u32), 2, fp);
2577 if (rc)
2578 return rc;
2579
2580 rc = put_entry(key, 1, len, fp);
2581 if (rc)
2582 return rc;
2583
2584 return 0;
2585}
2586
2587static int common_write(void *vkey, void *datum, void *ptr)
2588{
2589 char *key = vkey;
2590 struct common_datum *comdatum = datum;
2591 struct policy_data *pd = ptr;
2592 void *fp = pd->fp;
2593 __le32 buf[4];
2594 size_t len;
2595 int rc;
2596
2597 len = strlen(key);
2598 buf[0] = cpu_to_le32(len);
2599 buf[1] = cpu_to_le32(comdatum->value);
2600 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2601 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2602 rc = put_entry(buf, sizeof(u32), 4, fp);
2603 if (rc)
2604 return rc;
2605
2606 rc = put_entry(key, 1, len, fp);
2607 if (rc)
2608 return rc;
2609
2610 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2611 if (rc)
2612 return rc;
2613
2614 return 0;
2615}
2616
2617static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2618 void *fp)
2619{
2620 struct constraint_node *c;
2621 struct constraint_expr *e;
2622 __le32 buf[3];
2623 u32 nel;
2624 int rc;
2625
2626 for (c = node; c; c = c->next) {
2627 nel = 0;
2628 for (e = c->expr; e; e = e->next)
2629 nel++;
2630 buf[0] = cpu_to_le32(c->permissions);
2631 buf[1] = cpu_to_le32(nel);
2632 rc = put_entry(buf, sizeof(u32), 2, fp);
2633 if (rc)
2634 return rc;
2635 for (e = c->expr; e; e = e->next) {
2636 buf[0] = cpu_to_le32(e->expr_type);
2637 buf[1] = cpu_to_le32(e->attr);
2638 buf[2] = cpu_to_le32(e->op);
2639 rc = put_entry(buf, sizeof(u32), 3, fp);
2640 if (rc)
2641 return rc;
2642
2643 switch (e->expr_type) {
2644 case CEXPR_NAMES:
2645 rc = ebitmap_write(&e->names, fp);
2646 if (rc)
2647 return rc;
2648 break;
2649 default:
2650 break;
2651 }
2652 }
2653 }
2654
2655 return 0;
2656}
2657
2658static int class_write(void *vkey, void *datum, void *ptr)
2659{
2660 char *key = vkey;
2661 struct class_datum *cladatum = datum;
2662 struct policy_data *pd = ptr;
2663 void *fp = pd->fp;
2664 struct policydb *p = pd->p;
2665 struct constraint_node *c;
2666 __le32 buf[6];
2667 u32 ncons;
2668 size_t len, len2;
2669 int rc;
2670
2671 len = strlen(key);
2672 if (cladatum->comkey)
2673 len2 = strlen(cladatum->comkey);
2674 else
2675 len2 = 0;
2676
2677 ncons = 0;
2678 for (c = cladatum->constraints; c; c = c->next)
2679 ncons++;
2680
2681 buf[0] = cpu_to_le32(len);
2682 buf[1] = cpu_to_le32(len2);
2683 buf[2] = cpu_to_le32(cladatum->value);
2684 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2685 if (cladatum->permissions.table)
2686 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2687 else
2688 buf[4] = 0;
2689 buf[5] = cpu_to_le32(ncons);
2690 rc = put_entry(buf, sizeof(u32), 6, fp);
2691 if (rc)
2692 return rc;
2693
2694 rc = put_entry(key, 1, len, fp);
2695 if (rc)
2696 return rc;
2697
2698 if (cladatum->comkey) {
2699 rc = put_entry(cladatum->comkey, 1, len2, fp);
2700 if (rc)
2701 return rc;
2702 }
2703
2704 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2705 if (rc)
2706 return rc;
2707
2708 rc = write_cons_helper(p, cladatum->constraints, fp);
2709 if (rc)
2710 return rc;
2711
2712 /* write out the validatetrans rule */
2713 ncons = 0;
2714 for (c = cladatum->validatetrans; c; c = c->next)
2715 ncons++;
2716
2717 buf[0] = cpu_to_le32(ncons);
2718 rc = put_entry(buf, sizeof(u32), 1, fp);
2719 if (rc)
2720 return rc;
2721
2722 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2723 if (rc)
2724 return rc;
2725
2726 return 0;
2727}
2728
2729static int role_write(void *vkey, void *datum, void *ptr)
2730{
2731 char *key = vkey;
2732 struct role_datum *role = datum;
2733 struct policy_data *pd = ptr;
2734 void *fp = pd->fp;
2735 struct policydb *p = pd->p;
2736 __le32 buf[3];
2737 size_t items, len;
2738 int rc;
2739
2740 len = strlen(key);
2741 items = 0;
2742 buf[items++] = cpu_to_le32(len);
2743 buf[items++] = cpu_to_le32(role->value);
2744 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2745 buf[items++] = cpu_to_le32(role->bounds);
2746
2747 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2748
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 rc = ebitmap_write(&role->dominates, fp);
2758 if (rc)
2759 return rc;
2760
2761 rc = ebitmap_write(&role->types, fp);
2762 if (rc)
2763 return rc;
2764
2765 return 0;
2766}
2767
2768static int type_write(void *vkey, void *datum, void *ptr)
2769{
2770 char *key = vkey;
2771 struct type_datum *typdatum = datum;
2772 struct policy_data *pd = ptr;
2773 struct policydb *p = pd->p;
2774 void *fp = pd->fp;
2775 __le32 buf[4];
2776 int rc;
2777 size_t items, len;
2778
2779 len = strlen(key);
2780 items = 0;
2781 buf[items++] = cpu_to_le32(len);
2782 buf[items++] = cpu_to_le32(typdatum->value);
2783 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2784 u32 properties = 0;
2785
2786 if (typdatum->primary)
2787 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2788
2789 if (typdatum->attribute)
2790 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2791
2792 buf[items++] = cpu_to_le32(properties);
2793 buf[items++] = cpu_to_le32(typdatum->bounds);
2794 } else {
2795 buf[items++] = cpu_to_le32(typdatum->primary);
2796 }
2797 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2798 rc = put_entry(buf, sizeof(u32), items, fp);
2799 if (rc)
2800 return rc;
2801
2802 rc = put_entry(key, 1, len, fp);
2803 if (rc)
2804 return rc;
2805
2806 return 0;
2807}
2808
2809static int user_write(void *vkey, void *datum, void *ptr)
2810{
2811 char *key = vkey;
2812 struct user_datum *usrdatum = datum;
2813 struct policy_data *pd = ptr;
2814 struct policydb *p = pd->p;
2815 void *fp = pd->fp;
2816 __le32 buf[3];
2817 size_t items, len;
2818 int rc;
2819
2820 len = strlen(key);
2821 items = 0;
2822 buf[items++] = cpu_to_le32(len);
2823 buf[items++] = cpu_to_le32(usrdatum->value);
2824 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2825 buf[items++] = cpu_to_le32(usrdatum->bounds);
2826 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2827 rc = put_entry(buf, sizeof(u32), items, fp);
2828 if (rc)
2829 return rc;
2830
2831 rc = put_entry(key, 1, len, fp);
2832 if (rc)
2833 return rc;
2834
2835 rc = ebitmap_write(&usrdatum->roles, fp);
2836 if (rc)
2837 return rc;
2838
2839 rc = mls_write_range_helper(&usrdatum->range, fp);
2840 if (rc)
2841 return rc;
2842
2843 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2844 if (rc)
2845 return rc;
2846
2847 return 0;
2848}
2849
2850static int (*write_f[SYM_NUM]) (void *key, void *datum,
2851 void *datap) =
2852{
2853 common_write,
2854 class_write,
2855 role_write,
2856 type_write,
2857 user_write,
2858 cond_write_bool,
2859 sens_write,
2860 cat_write,
2861};
2862
2863static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2864 void *fp)
2865{
2866 unsigned int i, j, rc;
2867 size_t nel, len;
2868 __le32 buf[3];
2869 u32 nodebuf[8];
2870 struct ocontext *c;
2871 for (i = 0; i < info->ocon_num; i++) {
2872 nel = 0;
2873 for (c = p->ocontexts[i]; c; c = c->next)
2874 nel++;
2875 buf[0] = cpu_to_le32(nel);
2876 rc = put_entry(buf, sizeof(u32), 1, fp);
2877 if (rc)
2878 return rc;
2879 for (c = p->ocontexts[i]; c; c = c->next) {
2880 switch (i) {
2881 case OCON_ISID:
2882 buf[0] = cpu_to_le32(c->sid[0]);
2883 rc = put_entry(buf, sizeof(u32), 1, fp);
2884 if (rc)
2885 return rc;
2886 rc = context_write(p, &c->context[0], fp);
2887 if (rc)
2888 return rc;
2889 break;
2890 case OCON_FS:
2891 case OCON_NETIF:
2892 len = strlen(c->u.name);
2893 buf[0] = cpu_to_le32(len);
2894 rc = put_entry(buf, sizeof(u32), 1, fp);
2895 if (rc)
2896 return rc;
2897 rc = put_entry(c->u.name, 1, len, fp);
2898 if (rc)
2899 return rc;
2900 rc = context_write(p, &c->context[0], fp);
2901 if (rc)
2902 return rc;
2903 rc = context_write(p, &c->context[1], fp);
2904 if (rc)
2905 return rc;
2906 break;
2907 case OCON_PORT:
2908 buf[0] = cpu_to_le32(c->u.port.protocol);
2909 buf[1] = cpu_to_le32(c->u.port.low_port);
2910 buf[2] = cpu_to_le32(c->u.port.high_port);
2911 rc = put_entry(buf, sizeof(u32), 3, fp);
2912 if (rc)
2913 return rc;
2914 rc = context_write(p, &c->context[0], fp);
2915 if (rc)
2916 return rc;
2917 break;
2918 case OCON_NODE:
2919 nodebuf[0] = c->u.node.addr; /* network order */
2920 nodebuf[1] = c->u.node.mask; /* network order */
2921 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2922 if (rc)
2923 return rc;
2924 rc = context_write(p, &c->context[0], fp);
2925 if (rc)
2926 return rc;
2927 break;
2928 case OCON_FSUSE:
2929 buf[0] = cpu_to_le32(c->v.behavior);
2930 len = strlen(c->u.name);
2931 buf[1] = cpu_to_le32(len);
2932 rc = put_entry(buf, sizeof(u32), 2, fp);
2933 if (rc)
2934 return rc;
2935 rc = put_entry(c->u.name, 1, len, fp);
2936 if (rc)
2937 return rc;
2938 rc = context_write(p, &c->context[0], fp);
2939 if (rc)
2940 return rc;
2941 break;
2942 case OCON_NODE6:
2943 for (j = 0; j < 4; j++)
2944 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2945 for (j = 0; j < 4; j++)
2946 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2947 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2948 if (rc)
2949 return rc;
2950 rc = context_write(p, &c->context[0], fp);
2951 if (rc)
2952 return rc;
2953 break;
2954 }
2955 }
2956 }
2957 return 0;
2958}
2959
2960static int genfs_write(struct policydb *p, void *fp)
2961{
2962 struct genfs *genfs;
2963 struct ocontext *c;
2964 size_t len;
2965 __le32 buf[1];
2966 int rc;
2967
2968 len = 0;
2969 for (genfs = p->genfs; genfs; genfs = genfs->next)
2970 len++;
2971 buf[0] = cpu_to_le32(len);
2972 rc = put_entry(buf, sizeof(u32), 1, fp);
2973 if (rc)
2974 return rc;
2975 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2976 len = strlen(genfs->fstype);
2977 buf[0] = cpu_to_le32(len);
2978 rc = put_entry(buf, sizeof(u32), 1, fp);
2979 if (rc)
2980 return rc;
2981 rc = put_entry(genfs->fstype, 1, len, fp);
2982 if (rc)
2983 return rc;
2984 len = 0;
2985 for (c = genfs->head; c; c = c->next)
2986 len++;
2987 buf[0] = cpu_to_le32(len);
2988 rc = put_entry(buf, sizeof(u32), 1, fp);
2989 if (rc)
2990 return rc;
2991 for (c = genfs->head; c; c = c->next) {
2992 len = strlen(c->u.name);
2993 buf[0] = cpu_to_le32(len);
2994 rc = put_entry(buf, sizeof(u32), 1, fp);
2995 if (rc)
2996 return rc;
2997 rc = put_entry(c->u.name, 1, len, fp);
2998 if (rc)
2999 return rc;
3000 buf[0] = cpu_to_le32(c->v.sclass);
3001 rc = put_entry(buf, sizeof(u32), 1, fp);
3002 if (rc)
3003 return rc;
3004 rc = context_write(p, &c->context[0], fp);
3005 if (rc)
3006 return rc;
3007 }
3008 }
3009 return 0;
3010}
3011
3012static int range_count(void *key, void *data, void *ptr)
3013{
3014 int *cnt = ptr;
3015 *cnt = *cnt + 1;
3016
3017 return 0;
3018}
3019
3020static int range_write_helper(void *key, void *data, void *ptr)
3021{
3022 __le32 buf[2];
3023 struct range_trans *rt = key;
3024 struct mls_range *r = data;
3025 struct policy_data *pd = ptr;
3026 void *fp = pd->fp;
3027 struct policydb *p = pd->p;
3028 int rc;
3029
3030 buf[0] = cpu_to_le32(rt->source_type);
3031 buf[1] = cpu_to_le32(rt->target_type);
3032 rc = put_entry(buf, sizeof(u32), 2, fp);
3033 if (rc)
3034 return rc;
3035 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3036 buf[0] = cpu_to_le32(rt->target_class);
3037 rc = put_entry(buf, sizeof(u32), 1, fp);
3038 if (rc)
3039 return rc;
3040 }
3041 rc = mls_write_range_helper(r, fp);
3042 if (rc)
3043 return rc;
3044
3045 return 0;
3046}
3047
3048static int range_write(struct policydb *p, void *fp)
3049{
3050 size_t nel;
3051 __le32 buf[1];
3052 int rc;
3053 struct policy_data pd;
3054
3055 pd.p = p;
3056 pd.fp = fp;
3057
3058 /* count the number of entries in the hashtab */
3059 nel = 0;
3060 rc = hashtab_map(p->range_tr, range_count, &nel);
3061 if (rc)
3062 return rc;
3063
3064 buf[0] = cpu_to_le32(nel);
3065 rc = put_entry(buf, sizeof(u32), 1, fp);
3066 if (rc)
3067 return rc;
3068
3069 /* actually write all of the entries */
3070 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3071 if (rc)
3072 return rc;
3073
3074 return 0;
3075}
3076
3077/*
3078 * Write the configuration data in a policy database
3079 * structure to a policy database binary representation
3080 * file.
3081 */
3082int policydb_write(struct policydb *p, void *fp)
3083{
3084 unsigned int i, num_syms;
3085 int rc;
3086 __le32 buf[4];
3087 u32 config;
3088 size_t len;
3089 struct policydb_compat_info *info;
3090
3091 /*
3092 * refuse to write policy older than compressed avtab
3093 * to simplify the writer. There are other tests dropped
3094 * since we assume this throughout the writer code. Be
3095 * careful if you ever try to remove this restriction
3096 */
3097 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3098 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3099 " Because it is less than version %d\n", p->policyvers,
3100 POLICYDB_VERSION_AVTAB);
3101 return -EINVAL;
3102 }
3103
3104 config = 0;
3105 if (p->mls_enabled)
3106 config |= POLICYDB_CONFIG_MLS;
3107
3108 if (p->reject_unknown)
3109 config |= REJECT_UNKNOWN;
3110 if (p->allow_unknown)
3111 config |= ALLOW_UNKNOWN;
3112
3113 /* Write the magic number and string identifiers. */
3114 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3115 len = strlen(POLICYDB_STRING);
3116 buf[1] = cpu_to_le32(len);
3117 rc = put_entry(buf, sizeof(u32), 2, fp);
3118 if (rc)
3119 return rc;
3120 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3121 if (rc)
3122 return rc;
3123
3124 /* Write the version, config, and table sizes. */
3125 info = policydb_lookup_compat(p->policyvers);
3126 if (!info) {
3127 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3128 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003129 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003130 }
3131
3132 buf[0] = cpu_to_le32(p->policyvers);
3133 buf[1] = cpu_to_le32(config);
3134 buf[2] = cpu_to_le32(info->sym_num);
3135 buf[3] = cpu_to_le32(info->ocon_num);
3136
3137 rc = put_entry(buf, sizeof(u32), 4, fp);
3138 if (rc)
3139 return rc;
3140
3141 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3142 rc = ebitmap_write(&p->policycaps, fp);
3143 if (rc)
3144 return rc;
3145 }
3146
3147 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3148 rc = ebitmap_write(&p->permissive_map, fp);
3149 if (rc)
3150 return rc;
3151 }
3152
3153 num_syms = info->sym_num;
3154 for (i = 0; i < num_syms; i++) {
3155 struct policy_data pd;
3156
3157 pd.fp = fp;
3158 pd.p = p;
3159
3160 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3161 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3162
3163 rc = put_entry(buf, sizeof(u32), 2, fp);
3164 if (rc)
3165 return rc;
3166 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3167 if (rc)
3168 return rc;
3169 }
3170
3171 rc = avtab_write(p, &p->te_avtab, fp);
3172 if (rc)
3173 return rc;
3174
3175 rc = cond_write_list(p, p->cond_list, fp);
3176 if (rc)
3177 return rc;
3178
3179 rc = role_trans_write(p->role_tr, fp);
3180 if (rc)
3181 return rc;
3182
3183 rc = role_allow_write(p->role_allow, fp);
3184 if (rc)
3185 return rc;
3186
3187 rc = ocontext_write(p, info, fp);
3188 if (rc)
3189 return rc;
3190
3191 rc = genfs_write(p, fp);
3192 if (rc)
3193 return rc;
3194
3195 rc = range_write(p, fp);
3196 if (rc)
3197 return rc;
3198
3199 for (i = 0; i < p->p_types.nprim; i++) {
3200 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3201
3202 BUG_ON(!e);
3203 rc = ebitmap_write(e, fp);
3204 if (rc)
3205 return rc;
3206 }
3207
3208 return 0;
3209}