blob: af41fdfe1a71665232d8627dc4ce95392d24e4dc [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;
257
258 comdatum = datum;
259 p = datap;
260 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
261 return -EINVAL;
262 p->p_common_val_to_name[comdatum->value - 1] = key;
263 return 0;
264}
265
266static int class_index(void *key, void *datum, void *datap)
267{
268 struct policydb *p;
269 struct class_datum *cladatum;
270
271 cladatum = datum;
272 p = datap;
273 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
274 return -EINVAL;
275 p->p_class_val_to_name[cladatum->value - 1] = key;
276 p->class_val_to_struct[cladatum->value - 1] = cladatum;
277 return 0;
278}
279
280static int role_index(void *key, void *datum, void *datap)
281{
282 struct policydb *p;
283 struct role_datum *role;
284
285 role = datum;
286 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900287 if (!role->value
288 || role->value > p->p_roles.nprim
289 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -EINVAL;
291 p->p_role_val_to_name[role->value - 1] = key;
292 p->role_val_to_struct[role->value - 1] = role;
293 return 0;
294}
295
296static int type_index(void *key, void *datum, void *datap)
297{
298 struct policydb *p;
299 struct type_datum *typdatum;
300
301 typdatum = datum;
302 p = datap;
303
304 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900305 if (!typdatum->value
306 || typdatum->value > p->p_types.nprim
307 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EINVAL;
309 p->p_type_val_to_name[typdatum->value - 1] = key;
Eric Paris23bdecb2010-11-29 15:47:09 -0500310 /* this flex array was all preallocated, this cannot fail */
311 if (flex_array_put_ptr(p->type_val_to_struct_array,
312 typdatum->value - 1, typdatum,
313 GFP_KERNEL | __GFP_ZERO))
314 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 return 0;
318}
319
320static int user_index(void *key, void *datum, void *datap)
321{
322 struct policydb *p;
323 struct user_datum *usrdatum;
324
325 usrdatum = datum;
326 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900327 if (!usrdatum->value
328 || usrdatum->value > p->p_users.nprim
329 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EINVAL;
331 p->p_user_val_to_name[usrdatum->value - 1] = key;
332 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
333 return 0;
334}
335
336static int sens_index(void *key, void *datum, void *datap)
337{
338 struct policydb *p;
339 struct level_datum *levdatum;
340
341 levdatum = datum;
342 p = datap;
343
344 if (!levdatum->isalias) {
345 if (!levdatum->level->sens ||
346 levdatum->level->sens > p->p_levels.nprim)
347 return -EINVAL;
348 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
349 }
350
351 return 0;
352}
353
354static int cat_index(void *key, void *datum, void *datap)
355{
356 struct policydb *p;
357 struct cat_datum *catdatum;
358
359 catdatum = datum;
360 p = datap;
361
362 if (!catdatum->isalias) {
363 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
364 return -EINVAL;
365 p->p_cat_val_to_name[catdatum->value - 1] = key;
366 }
367
368 return 0;
369}
370
371static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
372{
373 common_index,
374 class_index,
375 role_index,
376 type_index,
377 user_index,
378 cond_index_bool,
379 sens_index,
380 cat_index,
381};
382
383/*
384 * Define the common val_to_name array and the class
385 * val_to_name and val_to_struct arrays in a policy
386 * database structure.
387 *
388 * Caller must clean up upon failure.
389 */
390static int policydb_index_classes(struct policydb *p)
391{
392 int rc;
393
Eric Paris9398c7f2010-11-23 11:40:08 -0500394 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 p->p_common_val_to_name =
396 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500397 if (!p->p_common_val_to_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 rc = hashtab_map(p->p_commons.table, common_index, p);
401 if (rc)
402 goto out;
403
Eric Paris9398c7f2010-11-23 11:40:08 -0500404 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 p->class_val_to_struct =
406 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500407 if (!p->class_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Eric Paris9398c7f2010-11-23 11:40:08 -0500410 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 p->p_class_val_to_name =
412 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500413 if (!p->p_class_val_to_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 rc = hashtab_map(p->p_classes.table, class_index, p);
417out:
418 return rc;
419}
420
421#ifdef DEBUG_HASHES
422static void symtab_hash_eval(struct symtab *s)
423{
424 int i;
425
426 for (i = 0; i < SYM_NUM; i++) {
427 struct hashtab *h = s[i].table;
428 struct hashtab_info info;
429
430 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400431 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 "longest chain length %d\n", symtab_name[i], h->nel,
433 info.slots_used, h->size, info.max_chain_len);
434 }
435}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500436
437static void rangetr_hash_eval(struct hashtab *h)
438{
439 struct hashtab_info info;
440
441 hashtab_stat(h, &info);
442 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
443 "longest chain length %d\n", h->nel,
444 info.slots_used, h->size, info.max_chain_len);
445}
446#else
447static inline void rangetr_hash_eval(struct hashtab *h)
448{
449}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#endif
451
452/*
453 * Define the other val_to_name and val_to_struct arrays
454 * in a policy database structure.
455 *
456 * Caller must clean up on failure.
457 */
458static int policydb_index_others(struct policydb *p)
459{
Eric Paris9398c7f2010-11-23 11:40:08 -0500460 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
James Morris454d9722008-02-26 20:42:02 +1100462 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100464 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 printk(", %d sens, %d cats", p->p_levels.nprim,
466 p->p_cats.nprim);
467 printk("\n");
468
James Morris454d9722008-02-26 20:42:02 +1100469 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 p->p_classes.nprim, p->te_avtab.nel);
471
472#ifdef DEBUG_HASHES
473 avtab_hash_eval(&p->te_avtab, "rules");
474 symtab_hash_eval(p->symtab);
475#endif
476
Eric Paris9398c7f2010-11-23 11:40:08 -0500477 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 p->role_val_to_struct =
479 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400480 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500481 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Eric Paris9398c7f2010-11-23 11:40:08 -0500484 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 p->user_val_to_struct =
486 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400487 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500488 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Eric Paris23bdecb2010-11-29 15:47:09 -0500491 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500492 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500493 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
494 p->p_types.nprim,
495 GFP_KERNEL | __GFP_ZERO);
496 if (!p->type_val_to_struct_array)
497 goto out;
498
499 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
500 p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
501 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900502 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900503
Eric Paris9398c7f2010-11-23 11:40:08 -0500504 rc = -ENOMEM;
505 if (cond_init_bool_indexes(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 for (i = SYM_ROLES; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500509 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 p->sym_val_to_name[i] =
511 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500512 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
515 if (rc)
516 goto out;
517 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500518 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519out:
520 return rc;
521}
522
523/*
524 * The following *_destroy functions are used to
525 * free any memory allocated for each kind of
526 * symbol data in the policy database.
527 */
528
529static int perm_destroy(void *key, void *datum, void *p)
530{
531 kfree(key);
532 kfree(datum);
533 return 0;
534}
535
536static int common_destroy(void *key, void *datum, void *p)
537{
538 struct common_datum *comdatum;
539
540 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500541 if (datum) {
542 comdatum = datum;
543 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
544 hashtab_destroy(comdatum->permissions.table);
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 kfree(datum);
547 return 0;
548}
549
James Morris6cbda6b2006-11-29 16:50:27 -0500550static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct class_datum *cladatum;
553 struct constraint_node *constraint, *ctemp;
554 struct constraint_expr *e, *etmp;
555
556 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500557 if (datum) {
558 cladatum = datum;
559 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
560 hashtab_destroy(cladatum->permissions.table);
561 constraint = cladatum->constraints;
562 while (constraint) {
563 e = constraint->expr;
564 while (e) {
565 ebitmap_destroy(&e->names);
566 etmp = e;
567 e = e->next;
568 kfree(etmp);
569 }
570 ctemp = constraint;
571 constraint = constraint->next;
572 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric Paris9398c7f2010-11-23 11:40:08 -0500575 constraint = cladatum->validatetrans;
576 while (constraint) {
577 e = constraint->expr;
578 while (e) {
579 ebitmap_destroy(&e->names);
580 etmp = e;
581 e = e->next;
582 kfree(etmp);
583 }
584 ctemp = constraint;
585 constraint = constraint->next;
586 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Paris9398c7f2010-11-23 11:40:08 -0500589 kfree(cladatum->comkey);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 kfree(datum);
592 return 0;
593}
594
595static int role_destroy(void *key, void *datum, void *p)
596{
597 struct role_datum *role;
598
599 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500600 if (datum) {
601 role = datum;
602 ebitmap_destroy(&role->dominates);
603 ebitmap_destroy(&role->types);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree(datum);
606 return 0;
607}
608
609static int type_destroy(void *key, void *datum, void *p)
610{
611 kfree(key);
612 kfree(datum);
613 return 0;
614}
615
616static int user_destroy(void *key, void *datum, void *p)
617{
618 struct user_datum *usrdatum;
619
620 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500621 if (datum) {
622 usrdatum = datum;
623 ebitmap_destroy(&usrdatum->roles);
624 ebitmap_destroy(&usrdatum->range.level[0].cat);
625 ebitmap_destroy(&usrdatum->range.level[1].cat);
626 ebitmap_destroy(&usrdatum->dfltlevel.cat);
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 kfree(datum);
629 return 0;
630}
631
632static int sens_destroy(void *key, void *datum, void *p)
633{
634 struct level_datum *levdatum;
635
636 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500637 if (datum) {
638 levdatum = datum;
639 ebitmap_destroy(&levdatum->level->cat);
640 kfree(levdatum->level);
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 kfree(datum);
643 return 0;
644}
645
646static int cat_destroy(void *key, void *datum, void *p)
647{
648 kfree(key);
649 kfree(datum);
650 return 0;
651}
652
653static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
654{
655 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500656 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 role_destroy,
658 type_destroy,
659 user_destroy,
660 cond_destroy_bool,
661 sens_destroy,
662 cat_destroy,
663};
664
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500665static int range_tr_destroy(void *key, void *datum, void *p)
666{
667 struct mls_range *rt = datum;
668 kfree(key);
669 ebitmap_destroy(&rt->level[0].cat);
670 ebitmap_destroy(&rt->level[1].cat);
671 kfree(datum);
672 cond_resched();
673 return 0;
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static void ocontext_destroy(struct ocontext *c, int i)
677{
Eric Parisd1b43542010-07-21 12:50:57 -0400678 if (!c)
679 return;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 context_destroy(&c->context[0]);
682 context_destroy(&c->context[1]);
683 if (i == OCON_ISID || i == OCON_FS ||
684 i == OCON_NETIF || i == OCON_FSUSE)
685 kfree(c->u.name);
686 kfree(c);
687}
688
689/*
690 * Free any memory allocated by a policy database structure.
691 */
692void policydb_destroy(struct policydb *p)
693{
694 struct ocontext *c, *ctmp;
695 struct genfs *g, *gtmp;
696 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700697 struct role_allow *ra, *lra = NULL;
698 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400701 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
703 hashtab_destroy(p->symtab[i].table);
704 }
705
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700706 for (i = 0; i < SYM_NUM; i++)
707 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700709 kfree(p->class_val_to_struct);
710 kfree(p->role_val_to_struct);
711 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500712 if (p->type_val_to_struct_array)
713 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 avtab_destroy(&p->te_avtab);
716
717 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400718 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 c = p->ocontexts[i];
720 while (c) {
721 ctmp = c;
722 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400723 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400725 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 g = p->genfs;
729 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400730 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 kfree(g->fstype);
732 c = g->head;
733 while (c) {
734 ctmp = c;
735 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400736 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 gtmp = g;
739 g = g->next;
740 kfree(gtmp);
741 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400742 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 cond_policydb_destroy(p);
745
Stephen Smalley782ebb92005-09-03 15:55:16 -0700746 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400747 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800748 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700749 ltr = tr;
750 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800751 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700752
Eric Paris2ced3df2008-04-17 13:37:12 -0400753 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400754 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800755 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700756 lra = ra;
757 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800758 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700759
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500760 hashtab_map(p->range_tr, range_tr_destroy, NULL);
761 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700762
Eric Paris6371dcd2010-07-29 23:02:34 -0400763 if (p->type_attr_map_array) {
764 for (i = 0; i < p->p_types.nprim; i++) {
765 struct ebitmap *e;
766
767 e = flex_array_get(p->type_attr_map_array, i);
768 if (!e)
769 continue;
770 ebitmap_destroy(e);
771 }
772 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700773 }
Paul Moore3bb56b22008-01-29 08:38:19 -0500774 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100775 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return;
778}
779
780/*
781 * Load the initial SIDs specified in a policy database
782 * structure into a SID table.
783 */
784int policydb_load_isids(struct policydb *p, struct sidtab *s)
785{
786 struct ocontext *head, *c;
787 int rc;
788
789 rc = sidtab_init(s);
790 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100791 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto out;
793 }
794
795 head = p->ocontexts[OCON_ISID];
796 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500797 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500799 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
800 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto out;
802 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500803
804 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
805 if (rc) {
806 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
807 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto out;
809 }
810 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500811 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812out:
813 return rc;
814}
815
Stephen Smalley45e54212007-11-07 10:08:00 -0500816int policydb_class_isvalid(struct policydb *p, unsigned int class)
817{
818 if (!class || class > p->p_classes.nprim)
819 return 0;
820 return 1;
821}
822
823int policydb_role_isvalid(struct policydb *p, unsigned int role)
824{
825 if (!role || role > p->p_roles.nprim)
826 return 0;
827 return 1;
828}
829
830int policydb_type_isvalid(struct policydb *p, unsigned int type)
831{
832 if (!type || type > p->p_types.nprim)
833 return 0;
834 return 1;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * Return 1 if the fields in the security context
839 * structure `c' are valid. Return 0 otherwise.
840 */
841int policydb_context_isvalid(struct policydb *p, struct context *c)
842{
843 struct role_datum *role;
844 struct user_datum *usrdatum;
845
846 if (!c->role || c->role > p->p_roles.nprim)
847 return 0;
848
849 if (!c->user || c->user > p->p_users.nprim)
850 return 0;
851
852 if (!c->type || c->type > p->p_types.nprim)
853 return 0;
854
855 if (c->role != OBJECT_R_VAL) {
856 /*
857 * Role must be authorized for the type.
858 */
859 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500860 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* role may not be associated with type */
862 return 0;
863
864 /*
865 * User must be authorized for the role.
866 */
867 usrdatum = p->user_val_to_struct[c->user - 1];
868 if (!usrdatum)
869 return 0;
870
Eric Paris9398c7f2010-11-23 11:40:08 -0500871 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* user may not be associated with role */
873 return 0;
874 }
875
876 if (!mls_context_isvalid(p, c))
877 return 0;
878
879 return 1;
880}
881
882/*
883 * Read a MLS range structure from a policydb binary
884 * representation file.
885 */
886static int mls_read_range_helper(struct mls_range *r, void *fp)
887{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700888 __le32 buf[2];
889 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int rc;
891
892 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500893 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto out;
895
Eric Paris9398c7f2010-11-23 11:40:08 -0500896 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 items = le32_to_cpu(buf[0]);
898 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100899 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto out;
901 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500904 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100905 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out;
907 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 r->level[0].sens = le32_to_cpu(buf[0]);
910 if (items > 1)
911 r->level[1].sens = le32_to_cpu(buf[1]);
912 else
913 r->level[1].sens = r->level[0].sens;
914
915 rc = ebitmap_read(&r->level[0].cat, fp);
916 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500917 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto out;
919 }
920 if (items > 1) {
921 rc = ebitmap_read(&r->level[1].cat, fp);
922 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500923 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto bad_high;
925 }
926 } else {
927 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
928 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100929 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto bad_high;
931 }
932 }
933
Eric Paris9398c7f2010-11-23 11:40:08 -0500934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935bad_high:
936 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500937out:
938 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/*
942 * Read and validate a security context structure
943 * from a policydb binary representation file.
944 */
945static int context_read_and_validate(struct context *c,
946 struct policydb *p,
947 void *fp)
948{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700949 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int rc;
951
952 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500953 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100954 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
956 }
957 c->user = le32_to_cpu(buf[0]);
958 c->role = le32_to_cpu(buf[1]);
959 c->type = le32_to_cpu(buf[2]);
960 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500961 rc = mls_read_range_helper(&c->range, fp);
962 if (rc) {
963 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto out;
965 }
966 }
967
Eric Paris9398c7f2010-11-23 11:40:08 -0500968 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100970 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -0500972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500974 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975out:
976 return rc;
977}
978
979/*
980 * The following *_read functions are used to
981 * read the symbol data from a policy database
982 * binary representation file.
983 */
984
985static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
986{
987 char *key = NULL;
988 struct perm_datum *perdatum;
989 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700990 __le32 buf[2];
991 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Eric Paris9398c7f2010-11-23 11:40:08 -0500993 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800994 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500995 if (!perdatum)
996 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500999 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 goto bad;
1001
1002 len = le32_to_cpu(buf[0]);
1003 perdatum->value = le32_to_cpu(buf[1]);
1004
Eric Paris9398c7f2010-11-23 11:40:08 -05001005 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001006 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001007 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001011 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001013 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 rc = hashtab_insert(h, key, perdatum);
1016 if (rc)
1017 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001018
1019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020bad:
1021 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001022 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1026{
1027 char *key = NULL;
1028 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001029 __le32 buf[4];
1030 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int i, rc;
1032
Eric Paris9398c7f2010-11-23 11:40:08 -05001033 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001034 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001035 if (!comdatum)
1036 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001039 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto bad;
1041
1042 len = le32_to_cpu(buf[0]);
1043 comdatum->value = le32_to_cpu(buf[1]);
1044
1045 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1046 if (rc)
1047 goto bad;
1048 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1049 nel = le32_to_cpu(buf[3]);
1050
Eric Paris9398c7f2010-11-23 11:40:08 -05001051 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001052 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001053 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001057 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001059 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 for (i = 0; i < nel; i++) {
1062 rc = perm_read(p, comdatum->permissions.table, fp);
1063 if (rc)
1064 goto bad;
1065 }
1066
1067 rc = hashtab_insert(h, key, comdatum);
1068 if (rc)
1069 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071bad:
1072 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001073 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001077 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct constraint_node *c, *lc;
1080 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001081 __le32 buf[3];
1082 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int rc, i, j, depth;
1084
1085 lc = NULL;
1086 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001087 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!c)
1089 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Eric Paris2ced3df2008-04-17 13:37:12 -04001091 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001093 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001097 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return rc;
1099 c->permissions = le32_to_cpu(buf[0]);
1100 nexpr = le32_to_cpu(buf[1]);
1101 le = NULL;
1102 depth = -1;
1103 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001104 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!e)
1106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Eric Paris2ced3df2008-04-17 13:37:12 -04001108 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001110 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001114 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return rc;
1116 e->expr_type = le32_to_cpu(buf[0]);
1117 e->attr = le32_to_cpu(buf[1]);
1118 e->op = le32_to_cpu(buf[2]);
1119
1120 switch (e->expr_type) {
1121 case CEXPR_NOT:
1122 if (depth < 0)
1123 return -EINVAL;
1124 break;
1125 case CEXPR_AND:
1126 case CEXPR_OR:
1127 if (depth < 1)
1128 return -EINVAL;
1129 depth--;
1130 break;
1131 case CEXPR_ATTR:
1132 if (depth == (CEXPR_MAXDEPTH - 1))
1133 return -EINVAL;
1134 depth++;
1135 break;
1136 case CEXPR_NAMES:
1137 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1138 return -EINVAL;
1139 if (depth == (CEXPR_MAXDEPTH - 1))
1140 return -EINVAL;
1141 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001142 rc = ebitmap_read(&e->names, fp);
1143 if (rc)
1144 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146 default:
1147 return -EINVAL;
1148 }
1149 le = e;
1150 }
1151 if (depth != 0)
1152 return -EINVAL;
1153 lc = c;
1154 }
1155
1156 return 0;
1157}
1158
1159static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1160{
1161 char *key = NULL;
1162 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001163 __le32 buf[6];
1164 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int i, rc;
1166
Eric Paris9398c7f2010-11-23 11:40:08 -05001167 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001168 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001169 if (!cladatum)
1170 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001173 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto bad;
1175
1176 len = le32_to_cpu(buf[0]);
1177 len2 = le32_to_cpu(buf[1]);
1178 cladatum->value = le32_to_cpu(buf[2]);
1179
1180 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1181 if (rc)
1182 goto bad;
1183 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1184 nel = le32_to_cpu(buf[4]);
1185
1186 ncons = le32_to_cpu(buf[5]);
1187
Eric Paris9398c7f2010-11-23 11:40:08 -05001188 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001189 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001190 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001194 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001196 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001199 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001200 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001201 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001204 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001206 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Eric Paris9398c7f2010-11-23 11:40:08 -05001208 rc = -EINVAL;
1209 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001211 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto bad;
1213 }
1214 }
1215 for (i = 0; i < nel; i++) {
1216 rc = perm_read(p, cladatum->permissions.table, fp);
1217 if (rc)
1218 goto bad;
1219 }
1220
1221 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1222 if (rc)
1223 goto bad;
1224
1225 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1226 /* grab the validatetrans rules */
1227 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001228 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto bad;
1230 ncons = le32_to_cpu(buf[0]);
1231 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1232 if (rc)
1233 goto bad;
1234 }
1235
1236 rc = hashtab_insert(h, key, cladatum);
1237 if (rc)
1238 goto bad;
1239
Eric Paris9398c7f2010-11-23 11:40:08 -05001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001242 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001243 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1247{
1248 char *key = NULL;
1249 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001250 int rc, to_read = 2;
1251 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001252 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Eric Paris9398c7f2010-11-23 11:40:08 -05001254 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001255 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001256 if (!role)
1257 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
KaiGai Koheid9250de2008-08-28 16:35:57 +09001259 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1260 to_read = 3;
1261
1262 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001263 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto bad;
1265
1266 len = le32_to_cpu(buf[0]);
1267 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001268 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1269 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Eric Paris9398c7f2010-11-23 11:40:08 -05001271 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001272 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001273 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001277 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001279 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 rc = ebitmap_read(&role->dominates, fp);
1282 if (rc)
1283 goto bad;
1284
1285 rc = ebitmap_read(&role->types, fp);
1286 if (rc)
1287 goto bad;
1288
1289 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001290 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001292 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto bad;
1295 }
1296 rc = 0;
1297 goto bad;
1298 }
1299
1300 rc = hashtab_insert(h, key, role);
1301 if (rc)
1302 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304bad:
1305 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001306 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
1309static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1310{
1311 char *key = NULL;
1312 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001313 int rc, to_read = 3;
1314 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001315 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Eric Paris9398c7f2010-11-23 11:40:08 -05001317 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001318 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001319 if (!typdatum)
1320 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
KaiGai Koheid9250de2008-08-28 16:35:57 +09001322 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1323 to_read = 4;
1324
1325 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001326 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto bad;
1328
1329 len = le32_to_cpu(buf[0]);
1330 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001331 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1332 u32 prop = le32_to_cpu(buf[2]);
1333
1334 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1335 typdatum->primary = 1;
1336 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1337 typdatum->attribute = 1;
1338
1339 typdatum->bounds = le32_to_cpu(buf[3]);
1340 } else {
1341 typdatum->primary = le32_to_cpu(buf[2]);
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Paris9398c7f2010-11-23 11:40:08 -05001344 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001345 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001346 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001349 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001351 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 rc = hashtab_insert(h, key, typdatum);
1354 if (rc)
1355 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357bad:
1358 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001359 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362
1363/*
1364 * Read a MLS level structure from a policydb binary
1365 * representation file.
1366 */
1367static int mls_read_level(struct mls_level *lp, void *fp)
1368{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001369 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 int rc;
1371
1372 memset(lp, 0, sizeof(*lp));
1373
1374 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001375 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001376 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001377 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 lp->sens = le32_to_cpu(buf[0]);
1380
Eric Paris9398c7f2010-11-23 11:40:08 -05001381 rc = ebitmap_read(&lp->cat, fp);
1382 if (rc) {
1383 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1384 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1390{
1391 char *key = NULL;
1392 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001393 int rc, to_read = 2;
1394 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001395 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Eric Paris9398c7f2010-11-23 11:40:08 -05001397 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001398 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001399 if (!usrdatum)
1400 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
KaiGai Koheid9250de2008-08-28 16:35:57 +09001402 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1403 to_read = 3;
1404
1405 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001406 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 goto bad;
1408
1409 len = le32_to_cpu(buf[0]);
1410 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001411 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1412 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Eric Paris9398c7f2010-11-23 11:40:08 -05001414 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001415 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001416 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001419 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001421 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 rc = ebitmap_read(&usrdatum->roles, fp);
1424 if (rc)
1425 goto bad;
1426
1427 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1428 rc = mls_read_range_helper(&usrdatum->range, fp);
1429 if (rc)
1430 goto bad;
1431 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1432 if (rc)
1433 goto bad;
1434 }
1435
1436 rc = hashtab_insert(h, key, usrdatum);
1437 if (rc)
1438 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440bad:
1441 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001442 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1446{
1447 char *key = NULL;
1448 struct level_datum *levdatum;
1449 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001450 __le32 buf[2];
1451 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Paris9398c7f2010-11-23 11:40:08 -05001453 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001454 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001455 if (!levdatum)
1456 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001459 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto bad;
1461
1462 len = le32_to_cpu(buf[0]);
1463 levdatum->isalias = le32_to_cpu(buf[1]);
1464
Eric Paris9398c7f2010-11-23 11:40:08 -05001465 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001466 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001467 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001470 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001472 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Eric Paris9398c7f2010-11-23 11:40:08 -05001474 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001476 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001478
1479 rc = mls_read_level(levdatum->level, fp);
1480 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 rc = hashtab_insert(h, key, levdatum);
1484 if (rc)
1485 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487bad:
1488 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001489 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1493{
1494 char *key = NULL;
1495 struct cat_datum *catdatum;
1496 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001497 __le32 buf[3];
1498 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Eric Paris9398c7f2010-11-23 11:40:08 -05001500 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001501 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001502 if (!catdatum)
1503 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001506 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto bad;
1508
1509 len = le32_to_cpu(buf[0]);
1510 catdatum->value = le32_to_cpu(buf[1]);
1511 catdatum->isalias = le32_to_cpu(buf[2]);
1512
Eric Paris9398c7f2010-11-23 11:40:08 -05001513 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001514 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001515 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001518 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001520 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 rc = hashtab_insert(h, key, catdatum);
1523 if (rc)
1524 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526bad:
1527 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1532{
1533 common_read,
1534 class_read,
1535 role_read,
1536 type_read,
1537 user_read,
1538 cond_read_bool,
1539 sens_read,
1540 cat_read,
1541};
1542
KaiGai Koheid9250de2008-08-28 16:35:57 +09001543static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1544{
1545 struct user_datum *upper, *user;
1546 struct policydb *p = datap;
1547 int depth = 0;
1548
1549 upper = user = datum;
1550 while (upper->bounds) {
1551 struct ebitmap_node *node;
1552 unsigned long bit;
1553
1554 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1555 printk(KERN_ERR "SELinux: user %s: "
1556 "too deep or looped boundary",
1557 (char *) key);
1558 return -EINVAL;
1559 }
1560
1561 upper = p->user_val_to_struct[upper->bounds - 1];
1562 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1563 if (ebitmap_get_bit(&upper->roles, bit))
1564 continue;
1565
1566 printk(KERN_ERR
1567 "SELinux: boundary violated policy: "
1568 "user=%s role=%s bounds=%s\n",
1569 p->p_user_val_to_name[user->value - 1],
1570 p->p_role_val_to_name[bit],
1571 p->p_user_val_to_name[upper->value - 1]);
1572
1573 return -EINVAL;
1574 }
1575 }
1576
1577 return 0;
1578}
1579
1580static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1581{
1582 struct role_datum *upper, *role;
1583 struct policydb *p = datap;
1584 int depth = 0;
1585
1586 upper = role = datum;
1587 while (upper->bounds) {
1588 struct ebitmap_node *node;
1589 unsigned long bit;
1590
1591 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1592 printk(KERN_ERR "SELinux: role %s: "
1593 "too deep or looped bounds\n",
1594 (char *) key);
1595 return -EINVAL;
1596 }
1597
1598 upper = p->role_val_to_struct[upper->bounds - 1];
1599 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1600 if (ebitmap_get_bit(&upper->types, bit))
1601 continue;
1602
1603 printk(KERN_ERR
1604 "SELinux: boundary violated policy: "
1605 "role=%s type=%s bounds=%s\n",
1606 p->p_role_val_to_name[role->value - 1],
1607 p->p_type_val_to_name[bit],
1608 p->p_role_val_to_name[upper->value - 1]);
1609
1610 return -EINVAL;
1611 }
1612 }
1613
1614 return 0;
1615}
1616
1617static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1618{
Eric Parisdaa6d832010-08-03 15:26:05 -04001619 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001620 struct policydb *p = datap;
1621 int depth = 0;
1622
Eric Parisdaa6d832010-08-03 15:26:05 -04001623 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001624 while (upper->bounds) {
1625 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1626 printk(KERN_ERR "SELinux: type %s: "
1627 "too deep or looped boundary\n",
1628 (char *) key);
1629 return -EINVAL;
1630 }
1631
Eric Paris23bdecb2010-11-29 15:47:09 -05001632 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1633 upper->bounds - 1);
1634 BUG_ON(!upper);
1635
KaiGai Koheid9250de2008-08-28 16:35:57 +09001636 if (upper->attribute) {
1637 printk(KERN_ERR "SELinux: type %s: "
1638 "bounded by attribute %s",
1639 (char *) key,
1640 p->p_type_val_to_name[upper->value - 1]);
1641 return -EINVAL;
1642 }
1643 }
1644
1645 return 0;
1646}
1647
1648static int policydb_bounds_sanity_check(struct policydb *p)
1649{
1650 int rc;
1651
1652 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1653 return 0;
1654
1655 rc = hashtab_map(p->p_users.table,
1656 user_bounds_sanity_check, p);
1657 if (rc)
1658 return rc;
1659
1660 rc = hashtab_map(p->p_roles.table,
1661 role_bounds_sanity_check, p);
1662 if (rc)
1663 return rc;
1664
1665 rc = hashtab_map(p->p_types.table,
1666 type_bounds_sanity_check, p);
1667 if (rc)
1668 return rc;
1669
1670 return 0;
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673extern int ss_initialized;
1674
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001675u16 string_to_security_class(struct policydb *p, const char *name)
1676{
1677 struct class_datum *cladatum;
1678
1679 cladatum = hashtab_search(p->p_classes.table, name);
1680 if (!cladatum)
1681 return 0;
1682
1683 return cladatum->value;
1684}
1685
1686u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1687{
1688 struct class_datum *cladatum;
1689 struct perm_datum *perdatum = NULL;
1690 struct common_datum *comdatum;
1691
1692 if (!tclass || tclass > p->p_classes.nprim)
1693 return 0;
1694
1695 cladatum = p->class_val_to_struct[tclass-1];
1696 comdatum = cladatum->comdatum;
1697 if (comdatum)
1698 perdatum = hashtab_search(comdatum->permissions.table,
1699 name);
1700 if (!perdatum)
1701 perdatum = hashtab_search(cladatum->permissions.table,
1702 name);
1703 if (!perdatum)
1704 return 0;
1705
1706 return 1U << (perdatum->value-1);
1707}
1708
Eric Paris9ee0c822010-06-11 12:37:05 -04001709static int range_read(struct policydb *p, void *fp)
1710{
1711 struct range_trans *rt = NULL;
1712 struct mls_range *r = NULL;
1713 int i, rc;
1714 __le32 buf[2];
1715 u32 nel;
1716
1717 if (p->policyvers < POLICYDB_VERSION_MLS)
1718 return 0;
1719
1720 rc = next_entry(buf, fp, sizeof(u32));
1721 if (rc)
1722 goto out;
1723
1724 nel = le32_to_cpu(buf[0]);
1725 for (i = 0; i < nel; i++) {
1726 rc = -ENOMEM;
1727 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1728 if (!rt)
1729 goto out;
1730
1731 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1732 if (rc)
1733 goto out;
1734
1735 rt->source_type = le32_to_cpu(buf[0]);
1736 rt->target_type = le32_to_cpu(buf[1]);
1737 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1738 rc = next_entry(buf, fp, sizeof(u32));
1739 if (rc)
1740 goto out;
1741 rt->target_class = le32_to_cpu(buf[0]);
1742 } else
1743 rt->target_class = p->process_class;
1744
1745 rc = -EINVAL;
1746 if (!policydb_type_isvalid(p, rt->source_type) ||
1747 !policydb_type_isvalid(p, rt->target_type) ||
1748 !policydb_class_isvalid(p, rt->target_class))
1749 goto out;
1750
1751 rc = -ENOMEM;
1752 r = kzalloc(sizeof(*r), GFP_KERNEL);
1753 if (!r)
1754 goto out;
1755
1756 rc = mls_read_range_helper(r, fp);
1757 if (rc)
1758 goto out;
1759
1760 rc = -EINVAL;
1761 if (!mls_range_isvalid(p, r)) {
1762 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1763 goto out;
1764 }
1765
1766 rc = hashtab_insert(p->range_tr, rt, r);
1767 if (rc)
1768 goto out;
1769
1770 rt = NULL;
1771 r = NULL;
1772 }
1773 rangetr_hash_eval(p->range_tr);
1774 rc = 0;
1775out:
1776 kfree(rt);
1777 kfree(r);
1778 return rc;
1779}
1780
Eric Parisd1b43542010-07-21 12:50:57 -04001781static int genfs_read(struct policydb *p, void *fp)
1782{
1783 int i, j, rc;
1784 u32 nel, nel2, len, len2;
1785 __le32 buf[1];
1786 struct ocontext *l, *c;
1787 struct ocontext *newc = NULL;
1788 struct genfs *genfs_p, *genfs;
1789 struct genfs *newgenfs = NULL;
1790
1791 rc = next_entry(buf, fp, sizeof(u32));
1792 if (rc)
1793 goto out;
1794 nel = le32_to_cpu(buf[0]);
1795
1796 for (i = 0; i < nel; i++) {
1797 rc = next_entry(buf, fp, sizeof(u32));
1798 if (rc)
1799 goto out;
1800 len = le32_to_cpu(buf[0]);
1801
1802 rc = -ENOMEM;
1803 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1804 if (!newgenfs)
1805 goto out;
1806
1807 rc = -ENOMEM;
1808 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1809 if (!newgenfs->fstype)
1810 goto out;
1811
1812 rc = next_entry(newgenfs->fstype, fp, len);
1813 if (rc)
1814 goto out;
1815
1816 newgenfs->fstype[len] = 0;
1817
1818 for (genfs_p = NULL, genfs = p->genfs; genfs;
1819 genfs_p = genfs, genfs = genfs->next) {
1820 rc = -EINVAL;
1821 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1822 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1823 newgenfs->fstype);
1824 goto out;
1825 }
1826 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1827 break;
1828 }
1829 newgenfs->next = genfs;
1830 if (genfs_p)
1831 genfs_p->next = newgenfs;
1832 else
1833 p->genfs = newgenfs;
1834 genfs = newgenfs;
1835 newgenfs = NULL;
1836
1837 rc = next_entry(buf, fp, sizeof(u32));
1838 if (rc)
1839 goto out;
1840
1841 nel2 = le32_to_cpu(buf[0]);
1842 for (j = 0; j < nel2; j++) {
1843 rc = next_entry(buf, fp, sizeof(u32));
1844 if (rc)
1845 goto out;
1846 len = le32_to_cpu(buf[0]);
1847
1848 rc = -ENOMEM;
1849 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1850 if (!newc)
1851 goto out;
1852
1853 rc = -ENOMEM;
1854 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1855 if (!newc->u.name)
1856 goto out;
1857
1858 rc = next_entry(newc->u.name, fp, len);
1859 if (rc)
1860 goto out;
1861 newc->u.name[len] = 0;
1862
1863 rc = next_entry(buf, fp, sizeof(u32));
1864 if (rc)
1865 goto out;
1866
1867 newc->v.sclass = le32_to_cpu(buf[0]);
1868 rc = context_read_and_validate(&newc->context[0], p, fp);
1869 if (rc)
1870 goto out;
1871
1872 for (l = NULL, c = genfs->head; c;
1873 l = c, c = c->next) {
1874 rc = -EINVAL;
1875 if (!strcmp(newc->u.name, c->u.name) &&
1876 (!c->v.sclass || !newc->v.sclass ||
1877 newc->v.sclass == c->v.sclass)) {
1878 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1879 genfs->fstype, c->u.name);
1880 goto out;
1881 }
1882 len = strlen(newc->u.name);
1883 len2 = strlen(c->u.name);
1884 if (len > len2)
1885 break;
1886 }
1887
1888 newc->next = c;
1889 if (l)
1890 l->next = newc;
1891 else
1892 genfs->head = newc;
1893 newc = NULL;
1894 }
1895 }
1896 rc = 0;
1897out:
1898 if (newgenfs)
1899 kfree(newgenfs->fstype);
1900 kfree(newgenfs);
1901 ocontext_destroy(newc, OCON_FSUSE);
1902
1903 return rc;
1904}
1905
Eric Paris692a8a22010-07-21 12:51:03 -04001906static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1907 void *fp)
1908{
1909 int i, j, rc;
1910 u32 nel, len;
1911 __le32 buf[3];
1912 struct ocontext *l, *c;
1913 u32 nodebuf[8];
1914
1915 for (i = 0; i < info->ocon_num; i++) {
1916 rc = next_entry(buf, fp, sizeof(u32));
1917 if (rc)
1918 goto out;
1919 nel = le32_to_cpu(buf[0]);
1920
1921 l = NULL;
1922 for (j = 0; j < nel; j++) {
1923 rc = -ENOMEM;
1924 c = kzalloc(sizeof(*c), GFP_KERNEL);
1925 if (!c)
1926 goto out;
1927 if (l)
1928 l->next = c;
1929 else
1930 p->ocontexts[i] = c;
1931 l = c;
1932
1933 switch (i) {
1934 case OCON_ISID:
1935 rc = next_entry(buf, fp, sizeof(u32));
1936 if (rc)
1937 goto out;
1938
1939 c->sid[0] = le32_to_cpu(buf[0]);
1940 rc = context_read_and_validate(&c->context[0], p, fp);
1941 if (rc)
1942 goto out;
1943 break;
1944 case OCON_FS:
1945 case OCON_NETIF:
1946 rc = next_entry(buf, fp, sizeof(u32));
1947 if (rc)
1948 goto out;
1949 len = le32_to_cpu(buf[0]);
1950
1951 rc = -ENOMEM;
1952 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1953 if (!c->u.name)
1954 goto out;
1955
1956 rc = next_entry(c->u.name, fp, len);
1957 if (rc)
1958 goto out;
1959
1960 c->u.name[len] = 0;
1961 rc = context_read_and_validate(&c->context[0], p, fp);
1962 if (rc)
1963 goto out;
1964 rc = context_read_and_validate(&c->context[1], p, fp);
1965 if (rc)
1966 goto out;
1967 break;
1968 case OCON_PORT:
1969 rc = next_entry(buf, fp, sizeof(u32)*3);
1970 if (rc)
1971 goto out;
1972 c->u.port.protocol = le32_to_cpu(buf[0]);
1973 c->u.port.low_port = le32_to_cpu(buf[1]);
1974 c->u.port.high_port = le32_to_cpu(buf[2]);
1975 rc = context_read_and_validate(&c->context[0], p, fp);
1976 if (rc)
1977 goto out;
1978 break;
1979 case OCON_NODE:
1980 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1981 if (rc)
1982 goto out;
1983 c->u.node.addr = nodebuf[0]; /* network order */
1984 c->u.node.mask = nodebuf[1]; /* network order */
1985 rc = context_read_and_validate(&c->context[0], p, fp);
1986 if (rc)
1987 goto out;
1988 break;
1989 case OCON_FSUSE:
1990 rc = next_entry(buf, fp, sizeof(u32)*2);
1991 if (rc)
1992 goto out;
1993
1994 rc = -EINVAL;
1995 c->v.behavior = le32_to_cpu(buf[0]);
1996 if (c->v.behavior > SECURITY_FS_USE_NONE)
1997 goto out;
1998
1999 rc = -ENOMEM;
2000 len = le32_to_cpu(buf[1]);
2001 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2002 if (!c->u.name)
2003 goto out;
2004
2005 rc = next_entry(c->u.name, fp, len);
2006 if (rc)
2007 goto out;
2008 c->u.name[len] = 0;
2009 rc = context_read_and_validate(&c->context[0], p, fp);
2010 if (rc)
2011 goto out;
2012 break;
2013 case OCON_NODE6: {
2014 int k;
2015
2016 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2017 if (rc)
2018 goto out;
2019 for (k = 0; k < 4; k++)
2020 c->u.node6.addr[k] = nodebuf[k];
2021 for (k = 0; k < 4; k++)
2022 c->u.node6.mask[k] = nodebuf[k+4];
2023 rc = context_read_and_validate(&c->context[0], p, fp);
2024 if (rc)
2025 goto out;
2026 break;
2027 }
2028 }
2029 }
2030 }
2031 rc = 0;
2032out:
2033 return rc;
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
2037 * Read the configuration data from a policy database binary
2038 * representation file into a policy database structure.
2039 */
2040int policydb_read(struct policydb *p, void *fp)
2041{
2042 struct role_allow *ra, *lra;
2043 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002045 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002046 u32 len, nprim, nel;
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 char *policydb_str;
2049 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 rc = policydb_init(p);
2052 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002053 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002056 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002057 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 goto bad;
2059
Eric Paris9398c7f2010-11-23 11:40:08 -05002060 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002061 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002062 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002064 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto bad;
2066 }
2067
Eric Paris9398c7f2010-11-23 11:40:08 -05002068 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002069 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002071 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 "match expected length %Zu\n",
2073 len, strlen(POLICYDB_STRING));
2074 goto bad;
2075 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002076
2077 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002078 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002080 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto bad;
2083 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002086 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002087 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 kfree(policydb_str);
2089 goto bad;
2090 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002091
2092 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002093 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002095 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 "my string %s\n", policydb_str, POLICYDB_STRING);
2097 kfree(policydb_str);
2098 goto bad;
2099 }
2100 /* Done with policydb_str. */
2101 kfree(policydb_str);
2102 policydb_str = NULL;
2103
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002104 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002106 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Eric Paris9398c7f2010-11-23 11:40:08 -05002109 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002110 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (p->policyvers < POLICYDB_VERSION_MIN ||
2112 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002113 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002114 "my version range %d-%d\n",
2115 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2116 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002119 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002120 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Eric Paris9398c7f2010-11-23 11:40:08 -05002122 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002124 printk(KERN_ERR "SELinux: security policydb version %d "
2125 "(MLS) not backwards compatible\n",
2126 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto bad;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Eric Paris3f120702007-09-21 14:37:10 -04002130 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2131 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Eric Paris9398c7f2010-11-23 11:40:08 -05002133 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2134 rc = ebitmap_read(&p->policycaps, fp);
2135 if (rc)
2136 goto bad;
2137 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002138
Eric Paris9398c7f2010-11-23 11:40:08 -05002139 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2140 rc = ebitmap_read(&p->permissive_map, fp);
2141 if (rc)
2142 goto bad;
2143 }
Eric Paris64dbf072008-03-31 12:17:33 +11002144
Eric Paris9398c7f2010-11-23 11:40:08 -05002145 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 info = policydb_lookup_compat(p->policyvers);
2147 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002148 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 "for version %d\n", p->policyvers);
2150 goto bad;
2151 }
2152
Eric Paris9398c7f2010-11-23 11:40:08 -05002153 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002154 if (le32_to_cpu(buf[2]) != info->sym_num ||
2155 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002156 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002157 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2158 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 info->sym_num, info->ocon_num);
2160 goto bad;
2161 }
2162
2163 for (i = 0; i < info->sym_num; i++) {
2164 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002165 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto bad;
2167 nprim = le32_to_cpu(buf[0]);
2168 nel = le32_to_cpu(buf[1]);
2169 for (j = 0; j < nel; j++) {
2170 rc = read_f[i](p, p->symtab[i].table, fp);
2171 if (rc)
2172 goto bad;
2173 }
2174
2175 p->symtab[i].nprim = nprim;
2176 }
2177
Stephen Smalley45e54212007-11-07 10:08:00 -05002178 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (rc)
2180 goto bad;
2181
2182 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2183 rc = cond_read_list(p, fp);
2184 if (rc)
2185 goto bad;
2186 }
2187
2188 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002189 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto bad;
2191 nel = le32_to_cpu(buf[0]);
2192 ltr = NULL;
2193 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002194 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002195 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002196 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002198 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002200 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002203 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002205
2206 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 tr->role = le32_to_cpu(buf[0]);
2208 tr->type = le32_to_cpu(buf[1]);
2209 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002210 if (!policydb_role_isvalid(p, tr->role) ||
2211 !policydb_type_isvalid(p, tr->type) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002212 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002213 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 ltr = tr;
2215 }
2216
2217 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002218 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto bad;
2220 nel = le32_to_cpu(buf[0]);
2221 lra = NULL;
2222 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002223 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002224 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002225 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002227 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002229 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002232 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002234
2235 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 ra->role = le32_to_cpu(buf[0]);
2237 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002238 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002239 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002240 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 lra = ra;
2242 }
2243
2244 rc = policydb_index_classes(p);
2245 if (rc)
2246 goto bad;
2247
2248 rc = policydb_index_others(p);
2249 if (rc)
2250 goto bad;
2251
Eric Paris9398c7f2010-11-23 11:40:08 -05002252 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002253 p->process_class = string_to_security_class(p, "process");
2254 if (!p->process_class)
2255 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002256
2257 rc = -EINVAL;
2258 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2259 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002260 if (!p->process_trans_perms)
2261 goto bad;
2262
Eric Paris692a8a22010-07-21 12:51:03 -04002263 rc = ocontext_read(p, info, fp);
2264 if (rc)
2265 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Eric Parisd1b43542010-07-21 12:50:57 -04002267 rc = genfs_read(p, fp);
2268 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Eric Paris9ee0c822010-06-11 12:37:05 -04002271 rc = range_read(p, fp);
2272 if (rc)
2273 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Eric Paris6371dcd2010-07-29 23:02:34 -04002275 rc = -ENOMEM;
2276 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2277 p->p_types.nprim,
2278 GFP_KERNEL | __GFP_ZERO);
2279 if (!p->type_attr_map_array)
2280 goto bad;
2281
2282 /* preallocate so we don't have to worry about the put ever failing */
2283 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2284 GFP_KERNEL | __GFP_ZERO);
2285 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002286 goto bad;
2287
2288 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002289 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2290
2291 BUG_ON(!e);
2292 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002293 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002294 rc = ebitmap_read(e, fp);
2295 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002296 goto bad;
2297 }
2298 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002299 rc = ebitmap_set_bit(e, i, 1);
2300 if (rc)
2301 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002302 }
2303
KaiGai Koheid9250de2008-08-28 16:35:57 +09002304 rc = policydb_bounds_sanity_check(p);
2305 if (rc)
2306 goto bad;
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 rc = 0;
2309out:
2310 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 policydb_destroy(p);
2313 goto out;
2314}
Eric Pariscee74f42010-10-13 17:50:25 -04002315
2316/*
2317 * Write a MLS level structure to a policydb binary
2318 * representation file.
2319 */
2320static int mls_write_level(struct mls_level *l, void *fp)
2321{
2322 __le32 buf[1];
2323 int rc;
2324
2325 buf[0] = cpu_to_le32(l->sens);
2326 rc = put_entry(buf, sizeof(u32), 1, fp);
2327 if (rc)
2328 return rc;
2329
2330 rc = ebitmap_write(&l->cat, fp);
2331 if (rc)
2332 return rc;
2333
2334 return 0;
2335}
2336
2337/*
2338 * Write a MLS range structure to a policydb binary
2339 * representation file.
2340 */
2341static int mls_write_range_helper(struct mls_range *r, void *fp)
2342{
2343 __le32 buf[3];
2344 size_t items;
2345 int rc, eq;
2346
2347 eq = mls_level_eq(&r->level[1], &r->level[0]);
2348
2349 if (eq)
2350 items = 2;
2351 else
2352 items = 3;
2353 buf[0] = cpu_to_le32(items-1);
2354 buf[1] = cpu_to_le32(r->level[0].sens);
2355 if (!eq)
2356 buf[2] = cpu_to_le32(r->level[1].sens);
2357
2358 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2359
2360 rc = put_entry(buf, sizeof(u32), items, fp);
2361 if (rc)
2362 return rc;
2363
2364 rc = ebitmap_write(&r->level[0].cat, fp);
2365 if (rc)
2366 return rc;
2367 if (!eq) {
2368 rc = ebitmap_write(&r->level[1].cat, fp);
2369 if (rc)
2370 return rc;
2371 }
2372
2373 return 0;
2374}
2375
2376static int sens_write(void *vkey, void *datum, void *ptr)
2377{
2378 char *key = vkey;
2379 struct level_datum *levdatum = datum;
2380 struct policy_data *pd = ptr;
2381 void *fp = pd->fp;
2382 __le32 buf[2];
2383 size_t len;
2384 int rc;
2385
2386 len = strlen(key);
2387 buf[0] = cpu_to_le32(len);
2388 buf[1] = cpu_to_le32(levdatum->isalias);
2389 rc = put_entry(buf, sizeof(u32), 2, fp);
2390 if (rc)
2391 return rc;
2392
2393 rc = put_entry(key, 1, len, fp);
2394 if (rc)
2395 return rc;
2396
2397 rc = mls_write_level(levdatum->level, fp);
2398 if (rc)
2399 return rc;
2400
2401 return 0;
2402}
2403
2404static int cat_write(void *vkey, void *datum, void *ptr)
2405{
2406 char *key = vkey;
2407 struct cat_datum *catdatum = datum;
2408 struct policy_data *pd = ptr;
2409 void *fp = pd->fp;
2410 __le32 buf[3];
2411 size_t len;
2412 int rc;
2413
2414 len = strlen(key);
2415 buf[0] = cpu_to_le32(len);
2416 buf[1] = cpu_to_le32(catdatum->value);
2417 buf[2] = cpu_to_le32(catdatum->isalias);
2418 rc = put_entry(buf, sizeof(u32), 3, fp);
2419 if (rc)
2420 return rc;
2421
2422 rc = put_entry(key, 1, len, fp);
2423 if (rc)
2424 return rc;
2425
2426 return 0;
2427}
2428
2429static int role_trans_write(struct role_trans *r, void *fp)
2430{
2431 struct role_trans *tr;
2432 u32 buf[3];
2433 size_t nel;
2434 int rc;
2435
2436 nel = 0;
2437 for (tr = r; tr; tr = tr->next)
2438 nel++;
2439 buf[0] = cpu_to_le32(nel);
2440 rc = put_entry(buf, sizeof(u32), 1, fp);
2441 if (rc)
2442 return rc;
2443 for (tr = r; tr; tr = tr->next) {
2444 buf[0] = cpu_to_le32(tr->role);
2445 buf[1] = cpu_to_le32(tr->type);
2446 buf[2] = cpu_to_le32(tr->new_role);
2447 rc = put_entry(buf, sizeof(u32), 3, fp);
2448 if (rc)
2449 return rc;
2450 }
2451
2452 return 0;
2453}
2454
2455static int role_allow_write(struct role_allow *r, void *fp)
2456{
2457 struct role_allow *ra;
2458 u32 buf[2];
2459 size_t nel;
2460 int rc;
2461
2462 nel = 0;
2463 for (ra = r; ra; ra = ra->next)
2464 nel++;
2465 buf[0] = cpu_to_le32(nel);
2466 rc = put_entry(buf, sizeof(u32), 1, fp);
2467 if (rc)
2468 return rc;
2469 for (ra = r; ra; ra = ra->next) {
2470 buf[0] = cpu_to_le32(ra->role);
2471 buf[1] = cpu_to_le32(ra->new_role);
2472 rc = put_entry(buf, sizeof(u32), 2, fp);
2473 if (rc)
2474 return rc;
2475 }
2476 return 0;
2477}
2478
2479/*
2480 * Write a security context structure
2481 * to a policydb binary representation file.
2482 */
2483static int context_write(struct policydb *p, struct context *c,
2484 void *fp)
2485{
2486 int rc;
2487 __le32 buf[3];
2488
2489 buf[0] = cpu_to_le32(c->user);
2490 buf[1] = cpu_to_le32(c->role);
2491 buf[2] = cpu_to_le32(c->type);
2492
2493 rc = put_entry(buf, sizeof(u32), 3, fp);
2494 if (rc)
2495 return rc;
2496
2497 rc = mls_write_range_helper(&c->range, fp);
2498 if (rc)
2499 return rc;
2500
2501 return 0;
2502}
2503
2504/*
2505 * The following *_write functions are used to
2506 * write the symbol data to a policy database
2507 * binary representation file.
2508 */
2509
2510static int perm_write(void *vkey, void *datum, void *fp)
2511{
2512 char *key = vkey;
2513 struct perm_datum *perdatum = datum;
2514 __le32 buf[2];
2515 size_t len;
2516 int rc;
2517
2518 len = strlen(key);
2519 buf[0] = cpu_to_le32(len);
2520 buf[1] = cpu_to_le32(perdatum->value);
2521 rc = put_entry(buf, sizeof(u32), 2, fp);
2522 if (rc)
2523 return rc;
2524
2525 rc = put_entry(key, 1, len, fp);
2526 if (rc)
2527 return rc;
2528
2529 return 0;
2530}
2531
2532static int common_write(void *vkey, void *datum, void *ptr)
2533{
2534 char *key = vkey;
2535 struct common_datum *comdatum = datum;
2536 struct policy_data *pd = ptr;
2537 void *fp = pd->fp;
2538 __le32 buf[4];
2539 size_t len;
2540 int rc;
2541
2542 len = strlen(key);
2543 buf[0] = cpu_to_le32(len);
2544 buf[1] = cpu_to_le32(comdatum->value);
2545 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2546 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2547 rc = put_entry(buf, sizeof(u32), 4, fp);
2548 if (rc)
2549 return rc;
2550
2551 rc = put_entry(key, 1, len, fp);
2552 if (rc)
2553 return rc;
2554
2555 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2556 if (rc)
2557 return rc;
2558
2559 return 0;
2560}
2561
2562static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2563 void *fp)
2564{
2565 struct constraint_node *c;
2566 struct constraint_expr *e;
2567 __le32 buf[3];
2568 u32 nel;
2569 int rc;
2570
2571 for (c = node; c; c = c->next) {
2572 nel = 0;
2573 for (e = c->expr; e; e = e->next)
2574 nel++;
2575 buf[0] = cpu_to_le32(c->permissions);
2576 buf[1] = cpu_to_le32(nel);
2577 rc = put_entry(buf, sizeof(u32), 2, fp);
2578 if (rc)
2579 return rc;
2580 for (e = c->expr; e; e = e->next) {
2581 buf[0] = cpu_to_le32(e->expr_type);
2582 buf[1] = cpu_to_le32(e->attr);
2583 buf[2] = cpu_to_le32(e->op);
2584 rc = put_entry(buf, sizeof(u32), 3, fp);
2585 if (rc)
2586 return rc;
2587
2588 switch (e->expr_type) {
2589 case CEXPR_NAMES:
2590 rc = ebitmap_write(&e->names, fp);
2591 if (rc)
2592 return rc;
2593 break;
2594 default:
2595 break;
2596 }
2597 }
2598 }
2599
2600 return 0;
2601}
2602
2603static int class_write(void *vkey, void *datum, void *ptr)
2604{
2605 char *key = vkey;
2606 struct class_datum *cladatum = datum;
2607 struct policy_data *pd = ptr;
2608 void *fp = pd->fp;
2609 struct policydb *p = pd->p;
2610 struct constraint_node *c;
2611 __le32 buf[6];
2612 u32 ncons;
2613 size_t len, len2;
2614 int rc;
2615
2616 len = strlen(key);
2617 if (cladatum->comkey)
2618 len2 = strlen(cladatum->comkey);
2619 else
2620 len2 = 0;
2621
2622 ncons = 0;
2623 for (c = cladatum->constraints; c; c = c->next)
2624 ncons++;
2625
2626 buf[0] = cpu_to_le32(len);
2627 buf[1] = cpu_to_le32(len2);
2628 buf[2] = cpu_to_le32(cladatum->value);
2629 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2630 if (cladatum->permissions.table)
2631 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2632 else
2633 buf[4] = 0;
2634 buf[5] = cpu_to_le32(ncons);
2635 rc = put_entry(buf, sizeof(u32), 6, fp);
2636 if (rc)
2637 return rc;
2638
2639 rc = put_entry(key, 1, len, fp);
2640 if (rc)
2641 return rc;
2642
2643 if (cladatum->comkey) {
2644 rc = put_entry(cladatum->comkey, 1, len2, fp);
2645 if (rc)
2646 return rc;
2647 }
2648
2649 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2650 if (rc)
2651 return rc;
2652
2653 rc = write_cons_helper(p, cladatum->constraints, fp);
2654 if (rc)
2655 return rc;
2656
2657 /* write out the validatetrans rule */
2658 ncons = 0;
2659 for (c = cladatum->validatetrans; c; c = c->next)
2660 ncons++;
2661
2662 buf[0] = cpu_to_le32(ncons);
2663 rc = put_entry(buf, sizeof(u32), 1, fp);
2664 if (rc)
2665 return rc;
2666
2667 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2668 if (rc)
2669 return rc;
2670
2671 return 0;
2672}
2673
2674static int role_write(void *vkey, void *datum, void *ptr)
2675{
2676 char *key = vkey;
2677 struct role_datum *role = datum;
2678 struct policy_data *pd = ptr;
2679 void *fp = pd->fp;
2680 struct policydb *p = pd->p;
2681 __le32 buf[3];
2682 size_t items, len;
2683 int rc;
2684
2685 len = strlen(key);
2686 items = 0;
2687 buf[items++] = cpu_to_le32(len);
2688 buf[items++] = cpu_to_le32(role->value);
2689 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2690 buf[items++] = cpu_to_le32(role->bounds);
2691
2692 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2693
2694 rc = put_entry(buf, sizeof(u32), items, fp);
2695 if (rc)
2696 return rc;
2697
2698 rc = put_entry(key, 1, len, fp);
2699 if (rc)
2700 return rc;
2701
2702 rc = ebitmap_write(&role->dominates, fp);
2703 if (rc)
2704 return rc;
2705
2706 rc = ebitmap_write(&role->types, fp);
2707 if (rc)
2708 return rc;
2709
2710 return 0;
2711}
2712
2713static int type_write(void *vkey, void *datum, void *ptr)
2714{
2715 char *key = vkey;
2716 struct type_datum *typdatum = datum;
2717 struct policy_data *pd = ptr;
2718 struct policydb *p = pd->p;
2719 void *fp = pd->fp;
2720 __le32 buf[4];
2721 int rc;
2722 size_t items, len;
2723
2724 len = strlen(key);
2725 items = 0;
2726 buf[items++] = cpu_to_le32(len);
2727 buf[items++] = cpu_to_le32(typdatum->value);
2728 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2729 u32 properties = 0;
2730
2731 if (typdatum->primary)
2732 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2733
2734 if (typdatum->attribute)
2735 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2736
2737 buf[items++] = cpu_to_le32(properties);
2738 buf[items++] = cpu_to_le32(typdatum->bounds);
2739 } else {
2740 buf[items++] = cpu_to_le32(typdatum->primary);
2741 }
2742 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2743 rc = put_entry(buf, sizeof(u32), items, fp);
2744 if (rc)
2745 return rc;
2746
2747 rc = put_entry(key, 1, len, fp);
2748 if (rc)
2749 return rc;
2750
2751 return 0;
2752}
2753
2754static int user_write(void *vkey, void *datum, void *ptr)
2755{
2756 char *key = vkey;
2757 struct user_datum *usrdatum = datum;
2758 struct policy_data *pd = ptr;
2759 struct policydb *p = pd->p;
2760 void *fp = pd->fp;
2761 __le32 buf[3];
2762 size_t items, len;
2763 int rc;
2764
2765 len = strlen(key);
2766 items = 0;
2767 buf[items++] = cpu_to_le32(len);
2768 buf[items++] = cpu_to_le32(usrdatum->value);
2769 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2770 buf[items++] = cpu_to_le32(usrdatum->bounds);
2771 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2772 rc = put_entry(buf, sizeof(u32), items, fp);
2773 if (rc)
2774 return rc;
2775
2776 rc = put_entry(key, 1, len, fp);
2777 if (rc)
2778 return rc;
2779
2780 rc = ebitmap_write(&usrdatum->roles, fp);
2781 if (rc)
2782 return rc;
2783
2784 rc = mls_write_range_helper(&usrdatum->range, fp);
2785 if (rc)
2786 return rc;
2787
2788 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2789 if (rc)
2790 return rc;
2791
2792 return 0;
2793}
2794
2795static int (*write_f[SYM_NUM]) (void *key, void *datum,
2796 void *datap) =
2797{
2798 common_write,
2799 class_write,
2800 role_write,
2801 type_write,
2802 user_write,
2803 cond_write_bool,
2804 sens_write,
2805 cat_write,
2806};
2807
2808static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2809 void *fp)
2810{
2811 unsigned int i, j, rc;
2812 size_t nel, len;
2813 __le32 buf[3];
2814 u32 nodebuf[8];
2815 struct ocontext *c;
2816 for (i = 0; i < info->ocon_num; i++) {
2817 nel = 0;
2818 for (c = p->ocontexts[i]; c; c = c->next)
2819 nel++;
2820 buf[0] = cpu_to_le32(nel);
2821 rc = put_entry(buf, sizeof(u32), 1, fp);
2822 if (rc)
2823 return rc;
2824 for (c = p->ocontexts[i]; c; c = c->next) {
2825 switch (i) {
2826 case OCON_ISID:
2827 buf[0] = cpu_to_le32(c->sid[0]);
2828 rc = put_entry(buf, sizeof(u32), 1, fp);
2829 if (rc)
2830 return rc;
2831 rc = context_write(p, &c->context[0], fp);
2832 if (rc)
2833 return rc;
2834 break;
2835 case OCON_FS:
2836 case OCON_NETIF:
2837 len = strlen(c->u.name);
2838 buf[0] = cpu_to_le32(len);
2839 rc = put_entry(buf, sizeof(u32), 1, fp);
2840 if (rc)
2841 return rc;
2842 rc = put_entry(c->u.name, 1, len, fp);
2843 if (rc)
2844 return rc;
2845 rc = context_write(p, &c->context[0], fp);
2846 if (rc)
2847 return rc;
2848 rc = context_write(p, &c->context[1], fp);
2849 if (rc)
2850 return rc;
2851 break;
2852 case OCON_PORT:
2853 buf[0] = cpu_to_le32(c->u.port.protocol);
2854 buf[1] = cpu_to_le32(c->u.port.low_port);
2855 buf[2] = cpu_to_le32(c->u.port.high_port);
2856 rc = put_entry(buf, sizeof(u32), 3, fp);
2857 if (rc)
2858 return rc;
2859 rc = context_write(p, &c->context[0], fp);
2860 if (rc)
2861 return rc;
2862 break;
2863 case OCON_NODE:
2864 nodebuf[0] = c->u.node.addr; /* network order */
2865 nodebuf[1] = c->u.node.mask; /* network order */
2866 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2867 if (rc)
2868 return rc;
2869 rc = context_write(p, &c->context[0], fp);
2870 if (rc)
2871 return rc;
2872 break;
2873 case OCON_FSUSE:
2874 buf[0] = cpu_to_le32(c->v.behavior);
2875 len = strlen(c->u.name);
2876 buf[1] = cpu_to_le32(len);
2877 rc = put_entry(buf, sizeof(u32), 2, fp);
2878 if (rc)
2879 return rc;
2880 rc = put_entry(c->u.name, 1, len, fp);
2881 if (rc)
2882 return rc;
2883 rc = context_write(p, &c->context[0], fp);
2884 if (rc)
2885 return rc;
2886 break;
2887 case OCON_NODE6:
2888 for (j = 0; j < 4; j++)
2889 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2890 for (j = 0; j < 4; j++)
2891 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2892 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2893 if (rc)
2894 return rc;
2895 rc = context_write(p, &c->context[0], fp);
2896 if (rc)
2897 return rc;
2898 break;
2899 }
2900 }
2901 }
2902 return 0;
2903}
2904
2905static int genfs_write(struct policydb *p, void *fp)
2906{
2907 struct genfs *genfs;
2908 struct ocontext *c;
2909 size_t len;
2910 __le32 buf[1];
2911 int rc;
2912
2913 len = 0;
2914 for (genfs = p->genfs; genfs; genfs = genfs->next)
2915 len++;
2916 buf[0] = cpu_to_le32(len);
2917 rc = put_entry(buf, sizeof(u32), 1, fp);
2918 if (rc)
2919 return rc;
2920 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2921 len = strlen(genfs->fstype);
2922 buf[0] = cpu_to_le32(len);
2923 rc = put_entry(buf, sizeof(u32), 1, fp);
2924 if (rc)
2925 return rc;
2926 rc = put_entry(genfs->fstype, 1, len, fp);
2927 if (rc)
2928 return rc;
2929 len = 0;
2930 for (c = genfs->head; c; c = c->next)
2931 len++;
2932 buf[0] = cpu_to_le32(len);
2933 rc = put_entry(buf, sizeof(u32), 1, fp);
2934 if (rc)
2935 return rc;
2936 for (c = genfs->head; c; c = c->next) {
2937 len = strlen(c->u.name);
2938 buf[0] = cpu_to_le32(len);
2939 rc = put_entry(buf, sizeof(u32), 1, fp);
2940 if (rc)
2941 return rc;
2942 rc = put_entry(c->u.name, 1, len, fp);
2943 if (rc)
2944 return rc;
2945 buf[0] = cpu_to_le32(c->v.sclass);
2946 rc = put_entry(buf, sizeof(u32), 1, fp);
2947 if (rc)
2948 return rc;
2949 rc = context_write(p, &c->context[0], fp);
2950 if (rc)
2951 return rc;
2952 }
2953 }
2954 return 0;
2955}
2956
2957static int range_count(void *key, void *data, void *ptr)
2958{
2959 int *cnt = ptr;
2960 *cnt = *cnt + 1;
2961
2962 return 0;
2963}
2964
2965static int range_write_helper(void *key, void *data, void *ptr)
2966{
2967 __le32 buf[2];
2968 struct range_trans *rt = key;
2969 struct mls_range *r = data;
2970 struct policy_data *pd = ptr;
2971 void *fp = pd->fp;
2972 struct policydb *p = pd->p;
2973 int rc;
2974
2975 buf[0] = cpu_to_le32(rt->source_type);
2976 buf[1] = cpu_to_le32(rt->target_type);
2977 rc = put_entry(buf, sizeof(u32), 2, fp);
2978 if (rc)
2979 return rc;
2980 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2981 buf[0] = cpu_to_le32(rt->target_class);
2982 rc = put_entry(buf, sizeof(u32), 1, fp);
2983 if (rc)
2984 return rc;
2985 }
2986 rc = mls_write_range_helper(r, fp);
2987 if (rc)
2988 return rc;
2989
2990 return 0;
2991}
2992
2993static int range_write(struct policydb *p, void *fp)
2994{
2995 size_t nel;
2996 __le32 buf[1];
2997 int rc;
2998 struct policy_data pd;
2999
3000 pd.p = p;
3001 pd.fp = fp;
3002
3003 /* count the number of entries in the hashtab */
3004 nel = 0;
3005 rc = hashtab_map(p->range_tr, range_count, &nel);
3006 if (rc)
3007 return rc;
3008
3009 buf[0] = cpu_to_le32(nel);
3010 rc = put_entry(buf, sizeof(u32), 1, fp);
3011 if (rc)
3012 return rc;
3013
3014 /* actually write all of the entries */
3015 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3016 if (rc)
3017 return rc;
3018
3019 return 0;
3020}
3021
3022/*
3023 * Write the configuration data in a policy database
3024 * structure to a policy database binary representation
3025 * file.
3026 */
3027int policydb_write(struct policydb *p, void *fp)
3028{
3029 unsigned int i, num_syms;
3030 int rc;
3031 __le32 buf[4];
3032 u32 config;
3033 size_t len;
3034 struct policydb_compat_info *info;
3035
3036 /*
3037 * refuse to write policy older than compressed avtab
3038 * to simplify the writer. There are other tests dropped
3039 * since we assume this throughout the writer code. Be
3040 * careful if you ever try to remove this restriction
3041 */
3042 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3043 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3044 " Because it is less than version %d\n", p->policyvers,
3045 POLICYDB_VERSION_AVTAB);
3046 return -EINVAL;
3047 }
3048
3049 config = 0;
3050 if (p->mls_enabled)
3051 config |= POLICYDB_CONFIG_MLS;
3052
3053 if (p->reject_unknown)
3054 config |= REJECT_UNKNOWN;
3055 if (p->allow_unknown)
3056 config |= ALLOW_UNKNOWN;
3057
3058 /* Write the magic number and string identifiers. */
3059 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3060 len = strlen(POLICYDB_STRING);
3061 buf[1] = cpu_to_le32(len);
3062 rc = put_entry(buf, sizeof(u32), 2, fp);
3063 if (rc)
3064 return rc;
3065 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3066 if (rc)
3067 return rc;
3068
3069 /* Write the version, config, and table sizes. */
3070 info = policydb_lookup_compat(p->policyvers);
3071 if (!info) {
3072 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3073 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003074 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003075 }
3076
3077 buf[0] = cpu_to_le32(p->policyvers);
3078 buf[1] = cpu_to_le32(config);
3079 buf[2] = cpu_to_le32(info->sym_num);
3080 buf[3] = cpu_to_le32(info->ocon_num);
3081
3082 rc = put_entry(buf, sizeof(u32), 4, fp);
3083 if (rc)
3084 return rc;
3085
3086 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3087 rc = ebitmap_write(&p->policycaps, fp);
3088 if (rc)
3089 return rc;
3090 }
3091
3092 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3093 rc = ebitmap_write(&p->permissive_map, fp);
3094 if (rc)
3095 return rc;
3096 }
3097
3098 num_syms = info->sym_num;
3099 for (i = 0; i < num_syms; i++) {
3100 struct policy_data pd;
3101
3102 pd.fp = fp;
3103 pd.p = p;
3104
3105 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3106 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3107
3108 rc = put_entry(buf, sizeof(u32), 2, fp);
3109 if (rc)
3110 return rc;
3111 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3112 if (rc)
3113 return rc;
3114 }
3115
3116 rc = avtab_write(p, &p->te_avtab, fp);
3117 if (rc)
3118 return rc;
3119
3120 rc = cond_write_list(p, p->cond_list, fp);
3121 if (rc)
3122 return rc;
3123
3124 rc = role_trans_write(p->role_tr, fp);
3125 if (rc)
3126 return rc;
3127
3128 rc = role_allow_write(p->role_allow, fp);
3129 if (rc)
3130 return rc;
3131
3132 rc = ocontext_write(p, info, fp);
3133 if (rc)
3134 return rc;
3135
3136 rc = genfs_write(p, fp);
3137 if (rc)
3138 return rc;
3139
3140 rc = range_write(p, fp);
3141 if (rc)
3142 return rc;
3143
3144 for (i = 0; i < p->p_types.nprim; i++) {
3145 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3146
3147 BUG_ON(!e);
3148 rc = ebitmap_write(e, fp);
3149 if (rc)
3150 return rc;
3151 }
3152
3153 return 0;
3154}