blob: 6ad73e81da5ce294944ef09f7324f79222bf90f8 [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;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900310 p->type_val_to_struct[typdatum->value - 1] = typdatum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 return 0;
314}
315
316static int user_index(void *key, void *datum, void *datap)
317{
318 struct policydb *p;
319 struct user_datum *usrdatum;
320
321 usrdatum = datum;
322 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900323 if (!usrdatum->value
324 || usrdatum->value > p->p_users.nprim
325 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327 p->p_user_val_to_name[usrdatum->value - 1] = key;
328 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
329 return 0;
330}
331
332static int sens_index(void *key, void *datum, void *datap)
333{
334 struct policydb *p;
335 struct level_datum *levdatum;
336
337 levdatum = datum;
338 p = datap;
339
340 if (!levdatum->isalias) {
341 if (!levdatum->level->sens ||
342 levdatum->level->sens > p->p_levels.nprim)
343 return -EINVAL;
344 p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
345 }
346
347 return 0;
348}
349
350static int cat_index(void *key, void *datum, void *datap)
351{
352 struct policydb *p;
353 struct cat_datum *catdatum;
354
355 catdatum = datum;
356 p = datap;
357
358 if (!catdatum->isalias) {
359 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
360 return -EINVAL;
361 p->p_cat_val_to_name[catdatum->value - 1] = key;
362 }
363
364 return 0;
365}
366
367static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
368{
369 common_index,
370 class_index,
371 role_index,
372 type_index,
373 user_index,
374 cond_index_bool,
375 sens_index,
376 cat_index,
377};
378
379/*
380 * Define the common val_to_name array and the class
381 * val_to_name and val_to_struct arrays in a policy
382 * database structure.
383 *
384 * Caller must clean up upon failure.
385 */
386static int policydb_index_classes(struct policydb *p)
387{
388 int rc;
389
Eric Paris9398c7f2010-11-23 11:40:08 -0500390 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 p->p_common_val_to_name =
392 kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500393 if (!p->p_common_val_to_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 rc = hashtab_map(p->p_commons.table, common_index, p);
397 if (rc)
398 goto out;
399
Eric Paris9398c7f2010-11-23 11:40:08 -0500400 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 p->class_val_to_struct =
402 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500403 if (!p->class_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Paris9398c7f2010-11-23 11:40:08 -0500406 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 p->p_class_val_to_name =
408 kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500409 if (!p->p_class_val_to_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 rc = hashtab_map(p->p_classes.table, class_index, p);
413out:
414 return rc;
415}
416
417#ifdef DEBUG_HASHES
418static void symtab_hash_eval(struct symtab *s)
419{
420 int i;
421
422 for (i = 0; i < SYM_NUM; i++) {
423 struct hashtab *h = s[i].table;
424 struct hashtab_info info;
425
426 hashtab_stat(h, &info);
Eric Paris744ba352008-04-17 11:52:44 -0400427 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 "longest chain length %d\n", symtab_name[i], h->nel,
429 info.slots_used, h->size, info.max_chain_len);
430 }
431}
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500432
433static void rangetr_hash_eval(struct hashtab *h)
434{
435 struct hashtab_info info;
436
437 hashtab_stat(h, &info);
438 printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
439 "longest chain length %d\n", h->nel,
440 info.slots_used, h->size, info.max_chain_len);
441}
442#else
443static inline void rangetr_hash_eval(struct hashtab *h)
444{
445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#endif
447
448/*
449 * Define the other val_to_name and val_to_struct arrays
450 * in a policy database structure.
451 *
452 * Caller must clean up on failure.
453 */
454static int policydb_index_others(struct policydb *p)
455{
Eric Paris9398c7f2010-11-23 11:40:08 -0500456 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
James Morris454d9722008-02-26 20:42:02 +1100458 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf52010-02-03 16:40:20 +0100460 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 printk(", %d sens, %d cats", p->p_levels.nprim,
462 p->p_cats.nprim);
463 printk("\n");
464
James Morris454d9722008-02-26 20:42:02 +1100465 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 p->p_classes.nprim, p->te_avtab.nel);
467
468#ifdef DEBUG_HASHES
469 avtab_hash_eval(&p->te_avtab, "rules");
470 symtab_hash_eval(p->symtab);
471#endif
472
Eric Paris9398c7f2010-11-23 11:40:08 -0500473 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 p->role_val_to_struct =
475 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400476 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500477 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Eric Paris9398c7f2010-11-23 11:40:08 -0500480 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 p->user_val_to_struct =
482 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400483 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500484 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Eric Paris9398c7f2010-11-23 11:40:08 -0500487 rc = -ENOMEM;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900488 p->type_val_to_struct =
489 kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
490 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500491 if (!p->type_val_to_struct)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900492 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900493
Eric Paris9398c7f2010-11-23 11:40:08 -0500494 rc = -ENOMEM;
495 if (cond_init_bool_indexes(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 for (i = SYM_ROLES; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500499 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 p->sym_val_to_name[i] =
501 kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500502 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
505 if (rc)
506 goto out;
507 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500508 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509out:
510 return rc;
511}
512
513/*
514 * The following *_destroy functions are used to
515 * free any memory allocated for each kind of
516 * symbol data in the policy database.
517 */
518
519static int perm_destroy(void *key, void *datum, void *p)
520{
521 kfree(key);
522 kfree(datum);
523 return 0;
524}
525
526static int common_destroy(void *key, void *datum, void *p)
527{
528 struct common_datum *comdatum;
529
530 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500531 if (datum) {
532 comdatum = datum;
533 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
534 hashtab_destroy(comdatum->permissions.table);
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 kfree(datum);
537 return 0;
538}
539
James Morris6cbda6b2006-11-29 16:50:27 -0500540static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct class_datum *cladatum;
543 struct constraint_node *constraint, *ctemp;
544 struct constraint_expr *e, *etmp;
545
546 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500547 if (datum) {
548 cladatum = datum;
549 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
550 hashtab_destroy(cladatum->permissions.table);
551 constraint = cladatum->constraints;
552 while (constraint) {
553 e = constraint->expr;
554 while (e) {
555 ebitmap_destroy(&e->names);
556 etmp = e;
557 e = e->next;
558 kfree(etmp);
559 }
560 ctemp = constraint;
561 constraint = constraint->next;
562 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Eric Paris9398c7f2010-11-23 11:40:08 -0500565 constraint = cladatum->validatetrans;
566 while (constraint) {
567 e = constraint->expr;
568 while (e) {
569 ebitmap_destroy(&e->names);
570 etmp = e;
571 e = e->next;
572 kfree(etmp);
573 }
574 ctemp = constraint;
575 constraint = constraint->next;
576 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Paris9398c7f2010-11-23 11:40:08 -0500579 kfree(cladatum->comkey);
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kfree(datum);
582 return 0;
583}
584
585static int role_destroy(void *key, void *datum, void *p)
586{
587 struct role_datum *role;
588
589 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500590 if (datum) {
591 role = datum;
592 ebitmap_destroy(&role->dominates);
593 ebitmap_destroy(&role->types);
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 kfree(datum);
596 return 0;
597}
598
599static int type_destroy(void *key, void *datum, void *p)
600{
601 kfree(key);
602 kfree(datum);
603 return 0;
604}
605
606static int user_destroy(void *key, void *datum, void *p)
607{
608 struct user_datum *usrdatum;
609
610 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500611 if (datum) {
612 usrdatum = datum;
613 ebitmap_destroy(&usrdatum->roles);
614 ebitmap_destroy(&usrdatum->range.level[0].cat);
615 ebitmap_destroy(&usrdatum->range.level[1].cat);
616 ebitmap_destroy(&usrdatum->dfltlevel.cat);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree(datum);
619 return 0;
620}
621
622static int sens_destroy(void *key, void *datum, void *p)
623{
624 struct level_datum *levdatum;
625
626 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500627 if (datum) {
628 levdatum = datum;
629 ebitmap_destroy(&levdatum->level->cat);
630 kfree(levdatum->level);
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 kfree(datum);
633 return 0;
634}
635
636static int cat_destroy(void *key, void *datum, void *p)
637{
638 kfree(key);
639 kfree(datum);
640 return 0;
641}
642
643static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
644{
645 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500646 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 role_destroy,
648 type_destroy,
649 user_destroy,
650 cond_destroy_bool,
651 sens_destroy,
652 cat_destroy,
653};
654
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500655static int range_tr_destroy(void *key, void *datum, void *p)
656{
657 struct mls_range *rt = datum;
658 kfree(key);
659 ebitmap_destroy(&rt->level[0].cat);
660 ebitmap_destroy(&rt->level[1].cat);
661 kfree(datum);
662 cond_resched();
663 return 0;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static void ocontext_destroy(struct ocontext *c, int i)
667{
Eric Parisd1b43542010-07-21 12:50:57 -0400668 if (!c)
669 return;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 context_destroy(&c->context[0]);
672 context_destroy(&c->context[1]);
673 if (i == OCON_ISID || i == OCON_FS ||
674 i == OCON_NETIF || i == OCON_FSUSE)
675 kfree(c->u.name);
676 kfree(c);
677}
678
679/*
680 * Free any memory allocated by a policy database structure.
681 */
682void policydb_destroy(struct policydb *p)
683{
684 struct ocontext *c, *ctmp;
685 struct genfs *g, *gtmp;
686 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700687 struct role_allow *ra, *lra = NULL;
688 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400691 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
693 hashtab_destroy(p->symtab[i].table);
694 }
695
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700696 for (i = 0; i < SYM_NUM; i++)
697 kfree(p->sym_val_to_name[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700699 kfree(p->class_val_to_struct);
700 kfree(p->role_val_to_struct);
701 kfree(p->user_val_to_struct);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900702 kfree(p->type_val_to_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 avtab_destroy(&p->te_avtab);
705
706 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400707 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 c = p->ocontexts[i];
709 while (c) {
710 ctmp = c;
711 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400712 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400714 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
717 g = p->genfs;
718 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400719 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 kfree(g->fstype);
721 c = g->head;
722 while (c) {
723 ctmp = c;
724 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400725 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 gtmp = g;
728 g = g->next;
729 kfree(gtmp);
730 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400731 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 cond_policydb_destroy(p);
734
Stephen Smalley782ebb92005-09-03 15:55:16 -0700735 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400736 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800737 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700738 ltr = tr;
739 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800740 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700741
Eric Paris2ced3df2008-04-17 13:37:12 -0400742 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400743 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800744 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700745 lra = ra;
746 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800747 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700748
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500749 hashtab_map(p->range_tr, range_tr_destroy, NULL);
750 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700751
Eric Paris6371dcd2010-07-29 23:02:34 -0400752 if (p->type_attr_map_array) {
753 for (i = 0; i < p->p_types.nprim; i++) {
754 struct ebitmap *e;
755
756 e = flex_array_get(p->type_attr_map_array, i);
757 if (!e)
758 continue;
759 ebitmap_destroy(e);
760 }
761 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700762 }
Paul Moore3bb56b22008-01-29 08:38:19 -0500763 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100764 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return;
767}
768
769/*
770 * Load the initial SIDs specified in a policy database
771 * structure into a SID table.
772 */
773int policydb_load_isids(struct policydb *p, struct sidtab *s)
774{
775 struct ocontext *head, *c;
776 int rc;
777
778 rc = sidtab_init(s);
779 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100780 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto out;
782 }
783
784 head = p->ocontexts[OCON_ISID];
785 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500786 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500788 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
789 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto out;
791 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500792
793 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
794 if (rc) {
795 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
796 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out;
798 }
799 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500800 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801out:
802 return rc;
803}
804
Stephen Smalley45e54212007-11-07 10:08:00 -0500805int policydb_class_isvalid(struct policydb *p, unsigned int class)
806{
807 if (!class || class > p->p_classes.nprim)
808 return 0;
809 return 1;
810}
811
812int policydb_role_isvalid(struct policydb *p, unsigned int role)
813{
814 if (!role || role > p->p_roles.nprim)
815 return 0;
816 return 1;
817}
818
819int policydb_type_isvalid(struct policydb *p, unsigned int type)
820{
821 if (!type || type > p->p_types.nprim)
822 return 0;
823 return 1;
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/*
827 * Return 1 if the fields in the security context
828 * structure `c' are valid. Return 0 otherwise.
829 */
830int policydb_context_isvalid(struct policydb *p, struct context *c)
831{
832 struct role_datum *role;
833 struct user_datum *usrdatum;
834
835 if (!c->role || c->role > p->p_roles.nprim)
836 return 0;
837
838 if (!c->user || c->user > p->p_users.nprim)
839 return 0;
840
841 if (!c->type || c->type > p->p_types.nprim)
842 return 0;
843
844 if (c->role != OBJECT_R_VAL) {
845 /*
846 * Role must be authorized for the type.
847 */
848 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500849 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* role may not be associated with type */
851 return 0;
852
853 /*
854 * User must be authorized for the role.
855 */
856 usrdatum = p->user_val_to_struct[c->user - 1];
857 if (!usrdatum)
858 return 0;
859
Eric Paris9398c7f2010-11-23 11:40:08 -0500860 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* user may not be associated with role */
862 return 0;
863 }
864
865 if (!mls_context_isvalid(p, c))
866 return 0;
867
868 return 1;
869}
870
871/*
872 * Read a MLS range structure from a policydb binary
873 * representation file.
874 */
875static int mls_read_range_helper(struct mls_range *r, void *fp)
876{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700877 __le32 buf[2];
878 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int rc;
880
881 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500882 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto out;
884
Eric Paris9398c7f2010-11-23 11:40:08 -0500885 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 items = le32_to_cpu(buf[0]);
887 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100888 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto out;
890 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -0500893 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100894 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto out;
896 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 r->level[0].sens = le32_to_cpu(buf[0]);
899 if (items > 1)
900 r->level[1].sens = le32_to_cpu(buf[1]);
901 else
902 r->level[1].sens = r->level[0].sens;
903
904 rc = ebitmap_read(&r->level[0].cat, fp);
905 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500906 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto out;
908 }
909 if (items > 1) {
910 rc = ebitmap_read(&r->level[1].cat, fp);
911 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500912 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto bad_high;
914 }
915 } else {
916 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
917 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100918 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto bad_high;
920 }
921 }
922
Eric Paris9398c7f2010-11-23 11:40:08 -0500923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924bad_high:
925 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500926out:
927 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
931 * Read and validate a security context structure
932 * from a policydb binary representation file.
933 */
934static int context_read_and_validate(struct context *c,
935 struct policydb *p,
936 void *fp)
937{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700938 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int rc;
940
941 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500942 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100943 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto out;
945 }
946 c->user = le32_to_cpu(buf[0]);
947 c->role = le32_to_cpu(buf[1]);
948 c->type = le32_to_cpu(buf[2]);
949 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500950 rc = mls_read_range_helper(&c->range, fp);
951 if (rc) {
952 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto out;
954 }
955 }
956
Eric Paris9398c7f2010-11-23 11:40:08 -0500957 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +1100959 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -0500961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500963 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964out:
965 return rc;
966}
967
968/*
969 * The following *_read functions are used to
970 * read the symbol data from a policy database
971 * binary representation file.
972 */
973
974static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
975{
976 char *key = NULL;
977 struct perm_datum *perdatum;
978 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700979 __le32 buf[2];
980 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Paris9398c7f2010-11-23 11:40:08 -0500982 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800983 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500984 if (!perdatum)
985 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -0500988 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto bad;
990
991 len = le32_to_cpu(buf[0]);
992 perdatum->value = le32_to_cpu(buf[1]);
993
Eric Paris9398c7f2010-11-23 11:40:08 -0500994 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -0400995 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500996 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -0500998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001000 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001002 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 rc = hashtab_insert(h, key, perdatum);
1005 if (rc)
1006 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001007
1008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009bad:
1010 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001011 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1015{
1016 char *key = NULL;
1017 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001018 __le32 buf[4];
1019 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int i, rc;
1021
Eric Paris9398c7f2010-11-23 11:40:08 -05001022 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001023 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001024 if (!comdatum)
1025 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001028 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto bad;
1030
1031 len = le32_to_cpu(buf[0]);
1032 comdatum->value = le32_to_cpu(buf[1]);
1033
1034 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1035 if (rc)
1036 goto bad;
1037 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1038 nel = le32_to_cpu(buf[3]);
1039
Eric Paris9398c7f2010-11-23 11:40:08 -05001040 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001041 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001042 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001046 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001048 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 for (i = 0; i < nel; i++) {
1051 rc = perm_read(p, comdatum->permissions.table, fp);
1052 if (rc)
1053 goto bad;
1054 }
1055
1056 rc = hashtab_insert(h, key, comdatum);
1057 if (rc)
1058 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060bad:
1061 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001062 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static int read_cons_helper(struct constraint_node **nodep, int ncons,
Eric Paris2ced3df2008-04-17 13:37:12 -04001066 int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct constraint_node *c, *lc;
1069 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001070 __le32 buf[3];
1071 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int rc, i, j, depth;
1073
1074 lc = NULL;
1075 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001076 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!c)
1078 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Eric Paris2ced3df2008-04-17 13:37:12 -04001080 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001082 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001086 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return rc;
1088 c->permissions = le32_to_cpu(buf[0]);
1089 nexpr = le32_to_cpu(buf[1]);
1090 le = NULL;
1091 depth = -1;
1092 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001093 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!e)
1095 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Eric Paris2ced3df2008-04-17 13:37:12 -04001097 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001099 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001103 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return rc;
1105 e->expr_type = le32_to_cpu(buf[0]);
1106 e->attr = le32_to_cpu(buf[1]);
1107 e->op = le32_to_cpu(buf[2]);
1108
1109 switch (e->expr_type) {
1110 case CEXPR_NOT:
1111 if (depth < 0)
1112 return -EINVAL;
1113 break;
1114 case CEXPR_AND:
1115 case CEXPR_OR:
1116 if (depth < 1)
1117 return -EINVAL;
1118 depth--;
1119 break;
1120 case CEXPR_ATTR:
1121 if (depth == (CEXPR_MAXDEPTH - 1))
1122 return -EINVAL;
1123 depth++;
1124 break;
1125 case CEXPR_NAMES:
1126 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1127 return -EINVAL;
1128 if (depth == (CEXPR_MAXDEPTH - 1))
1129 return -EINVAL;
1130 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001131 rc = ebitmap_read(&e->names, fp);
1132 if (rc)
1133 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 default:
1136 return -EINVAL;
1137 }
1138 le = e;
1139 }
1140 if (depth != 0)
1141 return -EINVAL;
1142 lc = c;
1143 }
1144
1145 return 0;
1146}
1147
1148static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1149{
1150 char *key = NULL;
1151 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001152 __le32 buf[6];
1153 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int i, rc;
1155
Eric Paris9398c7f2010-11-23 11:40:08 -05001156 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001157 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001158 if (!cladatum)
1159 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001162 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto bad;
1164
1165 len = le32_to_cpu(buf[0]);
1166 len2 = le32_to_cpu(buf[1]);
1167 cladatum->value = le32_to_cpu(buf[2]);
1168
1169 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1170 if (rc)
1171 goto bad;
1172 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1173 nel = le32_to_cpu(buf[4]);
1174
1175 ncons = le32_to_cpu(buf[5]);
1176
Eric Paris9398c7f2010-11-23 11:40:08 -05001177 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001178 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001179 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001183 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001185 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001188 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001189 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001190 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001193 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001195 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Eric Paris9398c7f2010-11-23 11:40:08 -05001197 rc = -EINVAL;
1198 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001200 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto bad;
1202 }
1203 }
1204 for (i = 0; i < nel; i++) {
1205 rc = perm_read(p, cladatum->permissions.table, fp);
1206 if (rc)
1207 goto bad;
1208 }
1209
1210 rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
1211 if (rc)
1212 goto bad;
1213
1214 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1215 /* grab the validatetrans rules */
1216 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001217 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto bad;
1219 ncons = le32_to_cpu(buf[0]);
1220 rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
1221 if (rc)
1222 goto bad;
1223 }
1224
1225 rc = hashtab_insert(h, key, cladatum);
1226 if (rc)
1227 goto bad;
1228
Eric Paris9398c7f2010-11-23 11:40:08 -05001229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001231 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001232 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1236{
1237 char *key = NULL;
1238 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001239 int rc, to_read = 2;
1240 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001241 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Eric Paris9398c7f2010-11-23 11:40:08 -05001243 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001244 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001245 if (!role)
1246 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
KaiGai Koheid9250de2008-08-28 16:35:57 +09001248 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1249 to_read = 3;
1250
1251 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001252 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto bad;
1254
1255 len = le32_to_cpu(buf[0]);
1256 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001257 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1258 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Eric Paris9398c7f2010-11-23 11:40:08 -05001260 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001261 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001262 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001266 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001268 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 rc = ebitmap_read(&role->dominates, fp);
1271 if (rc)
1272 goto bad;
1273
1274 rc = ebitmap_read(&role->types, fp);
1275 if (rc)
1276 goto bad;
1277
1278 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001279 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001281 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto bad;
1284 }
1285 rc = 0;
1286 goto bad;
1287 }
1288
1289 rc = hashtab_insert(h, key, role);
1290 if (rc)
1291 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293bad:
1294 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001295 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1299{
1300 char *key = NULL;
1301 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001302 int rc, to_read = 3;
1303 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001304 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Eric Paris9398c7f2010-11-23 11:40:08 -05001306 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001307 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001308 if (!typdatum)
1309 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
KaiGai Koheid9250de2008-08-28 16:35:57 +09001311 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1312 to_read = 4;
1313
1314 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001315 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto bad;
1317
1318 len = le32_to_cpu(buf[0]);
1319 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001320 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1321 u32 prop = le32_to_cpu(buf[2]);
1322
1323 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1324 typdatum->primary = 1;
1325 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1326 typdatum->attribute = 1;
1327
1328 typdatum->bounds = le32_to_cpu(buf[3]);
1329 } else {
1330 typdatum->primary = le32_to_cpu(buf[2]);
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Eric Paris9398c7f2010-11-23 11:40:08 -05001333 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001334 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001335 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001338 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001340 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 rc = hashtab_insert(h, key, typdatum);
1343 if (rc)
1344 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346bad:
1347 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001348 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351
1352/*
1353 * Read a MLS level structure from a policydb binary
1354 * representation file.
1355 */
1356static int mls_read_level(struct mls_level *lp, void *fp)
1357{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001358 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int rc;
1360
1361 memset(lp, 0, sizeof(*lp));
1362
1363 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001364 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001365 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 lp->sens = le32_to_cpu(buf[0]);
1369
Eric Paris9398c7f2010-11-23 11:40:08 -05001370 rc = ebitmap_read(&lp->cat, fp);
1371 if (rc) {
1372 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1373 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1379{
1380 char *key = NULL;
1381 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001382 int rc, to_read = 2;
1383 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001384 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Eric Paris9398c7f2010-11-23 11:40:08 -05001386 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001387 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001388 if (!usrdatum)
1389 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
KaiGai Koheid9250de2008-08-28 16:35:57 +09001391 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1392 to_read = 3;
1393
1394 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001395 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto bad;
1397
1398 len = le32_to_cpu(buf[0]);
1399 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001400 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1401 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Eric Paris9398c7f2010-11-23 11:40:08 -05001403 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001404 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001405 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001408 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001410 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 rc = ebitmap_read(&usrdatum->roles, fp);
1413 if (rc)
1414 goto bad;
1415
1416 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1417 rc = mls_read_range_helper(&usrdatum->range, fp);
1418 if (rc)
1419 goto bad;
1420 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1421 if (rc)
1422 goto bad;
1423 }
1424
1425 rc = hashtab_insert(h, key, usrdatum);
1426 if (rc)
1427 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429bad:
1430 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001431 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1435{
1436 char *key = NULL;
1437 struct level_datum *levdatum;
1438 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001439 __le32 buf[2];
1440 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Eric Paris9398c7f2010-11-23 11:40:08 -05001442 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001443 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001444 if (!levdatum)
1445 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001448 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 goto bad;
1450
1451 len = le32_to_cpu(buf[0]);
1452 levdatum->isalias = le32_to_cpu(buf[1]);
1453
Eric Paris9398c7f2010-11-23 11:40:08 -05001454 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001455 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001456 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001459 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001461 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Eric Paris9398c7f2010-11-23 11:40:08 -05001463 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001465 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001467
1468 rc = mls_read_level(levdatum->level, fp);
1469 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 rc = hashtab_insert(h, key, levdatum);
1473 if (rc)
1474 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476bad:
1477 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001478 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1482{
1483 char *key = NULL;
1484 struct cat_datum *catdatum;
1485 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001486 __le32 buf[3];
1487 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Eric Paris9398c7f2010-11-23 11:40:08 -05001489 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001490 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001491 if (!catdatum)
1492 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001495 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto bad;
1497
1498 len = le32_to_cpu(buf[0]);
1499 catdatum->value = le32_to_cpu(buf[1]);
1500 catdatum->isalias = le32_to_cpu(buf[2]);
1501
Eric Paris9398c7f2010-11-23 11:40:08 -05001502 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001503 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001504 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001507 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001509 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 rc = hashtab_insert(h, key, catdatum);
1512 if (rc)
1513 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515bad:
1516 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001517 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1521{
1522 common_read,
1523 class_read,
1524 role_read,
1525 type_read,
1526 user_read,
1527 cond_read_bool,
1528 sens_read,
1529 cat_read,
1530};
1531
KaiGai Koheid9250de2008-08-28 16:35:57 +09001532static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1533{
1534 struct user_datum *upper, *user;
1535 struct policydb *p = datap;
1536 int depth = 0;
1537
1538 upper = user = datum;
1539 while (upper->bounds) {
1540 struct ebitmap_node *node;
1541 unsigned long bit;
1542
1543 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1544 printk(KERN_ERR "SELinux: user %s: "
1545 "too deep or looped boundary",
1546 (char *) key);
1547 return -EINVAL;
1548 }
1549
1550 upper = p->user_val_to_struct[upper->bounds - 1];
1551 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1552 if (ebitmap_get_bit(&upper->roles, bit))
1553 continue;
1554
1555 printk(KERN_ERR
1556 "SELinux: boundary violated policy: "
1557 "user=%s role=%s bounds=%s\n",
1558 p->p_user_val_to_name[user->value - 1],
1559 p->p_role_val_to_name[bit],
1560 p->p_user_val_to_name[upper->value - 1]);
1561
1562 return -EINVAL;
1563 }
1564 }
1565
1566 return 0;
1567}
1568
1569static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1570{
1571 struct role_datum *upper, *role;
1572 struct policydb *p = datap;
1573 int depth = 0;
1574
1575 upper = role = datum;
1576 while (upper->bounds) {
1577 struct ebitmap_node *node;
1578 unsigned long bit;
1579
1580 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1581 printk(KERN_ERR "SELinux: role %s: "
1582 "too deep or looped bounds\n",
1583 (char *) key);
1584 return -EINVAL;
1585 }
1586
1587 upper = p->role_val_to_struct[upper->bounds - 1];
1588 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1589 if (ebitmap_get_bit(&upper->types, bit))
1590 continue;
1591
1592 printk(KERN_ERR
1593 "SELinux: boundary violated policy: "
1594 "role=%s type=%s bounds=%s\n",
1595 p->p_role_val_to_name[role->value - 1],
1596 p->p_type_val_to_name[bit],
1597 p->p_role_val_to_name[upper->value - 1]);
1598
1599 return -EINVAL;
1600 }
1601 }
1602
1603 return 0;
1604}
1605
1606static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1607{
Eric Parisdaa6d832010-08-03 15:26:05 -04001608 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001609 struct policydb *p = datap;
1610 int depth = 0;
1611
Eric Parisdaa6d832010-08-03 15:26:05 -04001612 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001613 while (upper->bounds) {
1614 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1615 printk(KERN_ERR "SELinux: type %s: "
1616 "too deep or looped boundary\n",
1617 (char *) key);
1618 return -EINVAL;
1619 }
1620
1621 upper = p->type_val_to_struct[upper->bounds - 1];
1622 if (upper->attribute) {
1623 printk(KERN_ERR "SELinux: type %s: "
1624 "bounded by attribute %s",
1625 (char *) key,
1626 p->p_type_val_to_name[upper->value - 1]);
1627 return -EINVAL;
1628 }
1629 }
1630
1631 return 0;
1632}
1633
1634static int policydb_bounds_sanity_check(struct policydb *p)
1635{
1636 int rc;
1637
1638 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1639 return 0;
1640
1641 rc = hashtab_map(p->p_users.table,
1642 user_bounds_sanity_check, p);
1643 if (rc)
1644 return rc;
1645
1646 rc = hashtab_map(p->p_roles.table,
1647 role_bounds_sanity_check, p);
1648 if (rc)
1649 return rc;
1650
1651 rc = hashtab_map(p->p_types.table,
1652 type_bounds_sanity_check, p);
1653 if (rc)
1654 return rc;
1655
1656 return 0;
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659extern int ss_initialized;
1660
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001661u16 string_to_security_class(struct policydb *p, const char *name)
1662{
1663 struct class_datum *cladatum;
1664
1665 cladatum = hashtab_search(p->p_classes.table, name);
1666 if (!cladatum)
1667 return 0;
1668
1669 return cladatum->value;
1670}
1671
1672u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1673{
1674 struct class_datum *cladatum;
1675 struct perm_datum *perdatum = NULL;
1676 struct common_datum *comdatum;
1677
1678 if (!tclass || tclass > p->p_classes.nprim)
1679 return 0;
1680
1681 cladatum = p->class_val_to_struct[tclass-1];
1682 comdatum = cladatum->comdatum;
1683 if (comdatum)
1684 perdatum = hashtab_search(comdatum->permissions.table,
1685 name);
1686 if (!perdatum)
1687 perdatum = hashtab_search(cladatum->permissions.table,
1688 name);
1689 if (!perdatum)
1690 return 0;
1691
1692 return 1U << (perdatum->value-1);
1693}
1694
Eric Paris9ee0c822010-06-11 12:37:05 -04001695static int range_read(struct policydb *p, void *fp)
1696{
1697 struct range_trans *rt = NULL;
1698 struct mls_range *r = NULL;
1699 int i, rc;
1700 __le32 buf[2];
1701 u32 nel;
1702
1703 if (p->policyvers < POLICYDB_VERSION_MLS)
1704 return 0;
1705
1706 rc = next_entry(buf, fp, sizeof(u32));
1707 if (rc)
1708 goto out;
1709
1710 nel = le32_to_cpu(buf[0]);
1711 for (i = 0; i < nel; i++) {
1712 rc = -ENOMEM;
1713 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1714 if (!rt)
1715 goto out;
1716
1717 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1718 if (rc)
1719 goto out;
1720
1721 rt->source_type = le32_to_cpu(buf[0]);
1722 rt->target_type = le32_to_cpu(buf[1]);
1723 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1724 rc = next_entry(buf, fp, sizeof(u32));
1725 if (rc)
1726 goto out;
1727 rt->target_class = le32_to_cpu(buf[0]);
1728 } else
1729 rt->target_class = p->process_class;
1730
1731 rc = -EINVAL;
1732 if (!policydb_type_isvalid(p, rt->source_type) ||
1733 !policydb_type_isvalid(p, rt->target_type) ||
1734 !policydb_class_isvalid(p, rt->target_class))
1735 goto out;
1736
1737 rc = -ENOMEM;
1738 r = kzalloc(sizeof(*r), GFP_KERNEL);
1739 if (!r)
1740 goto out;
1741
1742 rc = mls_read_range_helper(r, fp);
1743 if (rc)
1744 goto out;
1745
1746 rc = -EINVAL;
1747 if (!mls_range_isvalid(p, r)) {
1748 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1749 goto out;
1750 }
1751
1752 rc = hashtab_insert(p->range_tr, rt, r);
1753 if (rc)
1754 goto out;
1755
1756 rt = NULL;
1757 r = NULL;
1758 }
1759 rangetr_hash_eval(p->range_tr);
1760 rc = 0;
1761out:
1762 kfree(rt);
1763 kfree(r);
1764 return rc;
1765}
1766
Eric Parisd1b43542010-07-21 12:50:57 -04001767static int genfs_read(struct policydb *p, void *fp)
1768{
1769 int i, j, rc;
1770 u32 nel, nel2, len, len2;
1771 __le32 buf[1];
1772 struct ocontext *l, *c;
1773 struct ocontext *newc = NULL;
1774 struct genfs *genfs_p, *genfs;
1775 struct genfs *newgenfs = NULL;
1776
1777 rc = next_entry(buf, fp, sizeof(u32));
1778 if (rc)
1779 goto out;
1780 nel = le32_to_cpu(buf[0]);
1781
1782 for (i = 0; i < nel; i++) {
1783 rc = next_entry(buf, fp, sizeof(u32));
1784 if (rc)
1785 goto out;
1786 len = le32_to_cpu(buf[0]);
1787
1788 rc = -ENOMEM;
1789 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
1790 if (!newgenfs)
1791 goto out;
1792
1793 rc = -ENOMEM;
1794 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
1795 if (!newgenfs->fstype)
1796 goto out;
1797
1798 rc = next_entry(newgenfs->fstype, fp, len);
1799 if (rc)
1800 goto out;
1801
1802 newgenfs->fstype[len] = 0;
1803
1804 for (genfs_p = NULL, genfs = p->genfs; genfs;
1805 genfs_p = genfs, genfs = genfs->next) {
1806 rc = -EINVAL;
1807 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
1808 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
1809 newgenfs->fstype);
1810 goto out;
1811 }
1812 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
1813 break;
1814 }
1815 newgenfs->next = genfs;
1816 if (genfs_p)
1817 genfs_p->next = newgenfs;
1818 else
1819 p->genfs = newgenfs;
1820 genfs = newgenfs;
1821 newgenfs = NULL;
1822
1823 rc = next_entry(buf, fp, sizeof(u32));
1824 if (rc)
1825 goto out;
1826
1827 nel2 = le32_to_cpu(buf[0]);
1828 for (j = 0; j < nel2; j++) {
1829 rc = next_entry(buf, fp, sizeof(u32));
1830 if (rc)
1831 goto out;
1832 len = le32_to_cpu(buf[0]);
1833
1834 rc = -ENOMEM;
1835 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
1836 if (!newc)
1837 goto out;
1838
1839 rc = -ENOMEM;
1840 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
1841 if (!newc->u.name)
1842 goto out;
1843
1844 rc = next_entry(newc->u.name, fp, len);
1845 if (rc)
1846 goto out;
1847 newc->u.name[len] = 0;
1848
1849 rc = next_entry(buf, fp, sizeof(u32));
1850 if (rc)
1851 goto out;
1852
1853 newc->v.sclass = le32_to_cpu(buf[0]);
1854 rc = context_read_and_validate(&newc->context[0], p, fp);
1855 if (rc)
1856 goto out;
1857
1858 for (l = NULL, c = genfs->head; c;
1859 l = c, c = c->next) {
1860 rc = -EINVAL;
1861 if (!strcmp(newc->u.name, c->u.name) &&
1862 (!c->v.sclass || !newc->v.sclass ||
1863 newc->v.sclass == c->v.sclass)) {
1864 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
1865 genfs->fstype, c->u.name);
1866 goto out;
1867 }
1868 len = strlen(newc->u.name);
1869 len2 = strlen(c->u.name);
1870 if (len > len2)
1871 break;
1872 }
1873
1874 newc->next = c;
1875 if (l)
1876 l->next = newc;
1877 else
1878 genfs->head = newc;
1879 newc = NULL;
1880 }
1881 }
1882 rc = 0;
1883out:
1884 if (newgenfs)
1885 kfree(newgenfs->fstype);
1886 kfree(newgenfs);
1887 ocontext_destroy(newc, OCON_FSUSE);
1888
1889 return rc;
1890}
1891
Eric Paris692a8a22010-07-21 12:51:03 -04001892static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
1893 void *fp)
1894{
1895 int i, j, rc;
1896 u32 nel, len;
1897 __le32 buf[3];
1898 struct ocontext *l, *c;
1899 u32 nodebuf[8];
1900
1901 for (i = 0; i < info->ocon_num; i++) {
1902 rc = next_entry(buf, fp, sizeof(u32));
1903 if (rc)
1904 goto out;
1905 nel = le32_to_cpu(buf[0]);
1906
1907 l = NULL;
1908 for (j = 0; j < nel; j++) {
1909 rc = -ENOMEM;
1910 c = kzalloc(sizeof(*c), GFP_KERNEL);
1911 if (!c)
1912 goto out;
1913 if (l)
1914 l->next = c;
1915 else
1916 p->ocontexts[i] = c;
1917 l = c;
1918
1919 switch (i) {
1920 case OCON_ISID:
1921 rc = next_entry(buf, fp, sizeof(u32));
1922 if (rc)
1923 goto out;
1924
1925 c->sid[0] = le32_to_cpu(buf[0]);
1926 rc = context_read_and_validate(&c->context[0], p, fp);
1927 if (rc)
1928 goto out;
1929 break;
1930 case OCON_FS:
1931 case OCON_NETIF:
1932 rc = next_entry(buf, fp, sizeof(u32));
1933 if (rc)
1934 goto out;
1935 len = le32_to_cpu(buf[0]);
1936
1937 rc = -ENOMEM;
1938 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1939 if (!c->u.name)
1940 goto out;
1941
1942 rc = next_entry(c->u.name, fp, len);
1943 if (rc)
1944 goto out;
1945
1946 c->u.name[len] = 0;
1947 rc = context_read_and_validate(&c->context[0], p, fp);
1948 if (rc)
1949 goto out;
1950 rc = context_read_and_validate(&c->context[1], p, fp);
1951 if (rc)
1952 goto out;
1953 break;
1954 case OCON_PORT:
1955 rc = next_entry(buf, fp, sizeof(u32)*3);
1956 if (rc)
1957 goto out;
1958 c->u.port.protocol = le32_to_cpu(buf[0]);
1959 c->u.port.low_port = le32_to_cpu(buf[1]);
1960 c->u.port.high_port = le32_to_cpu(buf[2]);
1961 rc = context_read_and_validate(&c->context[0], p, fp);
1962 if (rc)
1963 goto out;
1964 break;
1965 case OCON_NODE:
1966 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
1967 if (rc)
1968 goto out;
1969 c->u.node.addr = nodebuf[0]; /* network order */
1970 c->u.node.mask = nodebuf[1]; /* network order */
1971 rc = context_read_and_validate(&c->context[0], p, fp);
1972 if (rc)
1973 goto out;
1974 break;
1975 case OCON_FSUSE:
1976 rc = next_entry(buf, fp, sizeof(u32)*2);
1977 if (rc)
1978 goto out;
1979
1980 rc = -EINVAL;
1981 c->v.behavior = le32_to_cpu(buf[0]);
1982 if (c->v.behavior > SECURITY_FS_USE_NONE)
1983 goto out;
1984
1985 rc = -ENOMEM;
1986 len = le32_to_cpu(buf[1]);
1987 c->u.name = kmalloc(len + 1, GFP_KERNEL);
1988 if (!c->u.name)
1989 goto out;
1990
1991 rc = next_entry(c->u.name, fp, len);
1992 if (rc)
1993 goto out;
1994 c->u.name[len] = 0;
1995 rc = context_read_and_validate(&c->context[0], p, fp);
1996 if (rc)
1997 goto out;
1998 break;
1999 case OCON_NODE6: {
2000 int k;
2001
2002 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2003 if (rc)
2004 goto out;
2005 for (k = 0; k < 4; k++)
2006 c->u.node6.addr[k] = nodebuf[k];
2007 for (k = 0; k < 4; k++)
2008 c->u.node6.mask[k] = nodebuf[k+4];
2009 rc = context_read_and_validate(&c->context[0], p, fp);
2010 if (rc)
2011 goto out;
2012 break;
2013 }
2014 }
2015 }
2016 }
2017 rc = 0;
2018out:
2019 return rc;
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/*
2023 * Read the configuration data from a policy database binary
2024 * representation file into a policy database structure.
2025 */
2026int policydb_read(struct policydb *p, void *fp)
2027{
2028 struct role_allow *ra, *lra;
2029 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002031 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002032 u32 len, nprim, nel;
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 char *policydb_str;
2035 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 rc = policydb_init(p);
2038 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002039 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002042 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002043 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto bad;
2045
Eric Paris9398c7f2010-11-23 11:40:08 -05002046 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002047 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002048 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002050 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto bad;
2052 }
2053
Eric Paris9398c7f2010-11-23 11:40:08 -05002054 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002055 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002057 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 "match expected length %Zu\n",
2059 len, strlen(POLICYDB_STRING));
2060 goto bad;
2061 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002062
2063 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002064 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002066 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 goto bad;
2069 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002072 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002073 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 kfree(policydb_str);
2075 goto bad;
2076 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002077
2078 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002079 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002081 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 "my string %s\n", policydb_str, POLICYDB_STRING);
2083 kfree(policydb_str);
2084 goto bad;
2085 }
2086 /* Done with policydb_str. */
2087 kfree(policydb_str);
2088 policydb_str = NULL;
2089
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01002090 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002092 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric Paris9398c7f2010-11-23 11:40:08 -05002095 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002096 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (p->policyvers < POLICYDB_VERSION_MIN ||
2098 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002099 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002100 "my version range %d-%d\n",
2101 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2102 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002105 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf52010-02-03 16:40:20 +01002106 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Eric Paris9398c7f2010-11-23 11:40:08 -05002108 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002110 printk(KERN_ERR "SELinux: security policydb version %d "
2111 "(MLS) not backwards compatible\n",
2112 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto bad;
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Eric Paris3f120702007-09-21 14:37:10 -04002116 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2117 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Eric Paris9398c7f2010-11-23 11:40:08 -05002119 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2120 rc = ebitmap_read(&p->policycaps, fp);
2121 if (rc)
2122 goto bad;
2123 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002124
Eric Paris9398c7f2010-11-23 11:40:08 -05002125 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2126 rc = ebitmap_read(&p->permissive_map, fp);
2127 if (rc)
2128 goto bad;
2129 }
Eric Paris64dbf072008-03-31 12:17:33 +11002130
Eric Paris9398c7f2010-11-23 11:40:08 -05002131 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 info = policydb_lookup_compat(p->policyvers);
2133 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002134 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 "for version %d\n", p->policyvers);
2136 goto bad;
2137 }
2138
Eric Paris9398c7f2010-11-23 11:40:08 -05002139 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002140 if (le32_to_cpu(buf[2]) != info->sym_num ||
2141 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002142 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002143 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2144 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 info->sym_num, info->ocon_num);
2146 goto bad;
2147 }
2148
2149 for (i = 0; i < info->sym_num; i++) {
2150 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002151 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto bad;
2153 nprim = le32_to_cpu(buf[0]);
2154 nel = le32_to_cpu(buf[1]);
2155 for (j = 0; j < nel; j++) {
2156 rc = read_f[i](p, p->symtab[i].table, fp);
2157 if (rc)
2158 goto bad;
2159 }
2160
2161 p->symtab[i].nprim = nprim;
2162 }
2163
Stephen Smalley45e54212007-11-07 10:08:00 -05002164 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (rc)
2166 goto bad;
2167
2168 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2169 rc = cond_read_list(p, fp);
2170 if (rc)
2171 goto bad;
2172 }
2173
2174 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002175 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto bad;
2177 nel = le32_to_cpu(buf[0]);
2178 ltr = NULL;
2179 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002180 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002181 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002182 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002184 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002186 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002189 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002191
2192 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 tr->role = le32_to_cpu(buf[0]);
2194 tr->type = le32_to_cpu(buf[1]);
2195 tr->new_role = le32_to_cpu(buf[2]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002196 if (!policydb_role_isvalid(p, tr->role) ||
2197 !policydb_type_isvalid(p, tr->type) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002198 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002199 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 ltr = tr;
2201 }
2202
2203 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002204 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto bad;
2206 nel = le32_to_cpu(buf[0]);
2207 lra = NULL;
2208 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002209 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002210 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002211 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002213 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002215 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002218 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002220
2221 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ra->role = le32_to_cpu(buf[0]);
2223 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002224 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002225 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002226 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 lra = ra;
2228 }
2229
2230 rc = policydb_index_classes(p);
2231 if (rc)
2232 goto bad;
2233
2234 rc = policydb_index_others(p);
2235 if (rc)
2236 goto bad;
2237
Eric Paris9398c7f2010-11-23 11:40:08 -05002238 rc = -EINVAL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002239 p->process_class = string_to_security_class(p, "process");
2240 if (!p->process_class)
2241 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002242
2243 rc = -EINVAL;
2244 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2245 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002246 if (!p->process_trans_perms)
2247 goto bad;
2248
Eric Paris692a8a22010-07-21 12:51:03 -04002249 rc = ocontext_read(p, info, fp);
2250 if (rc)
2251 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Eric Parisd1b43542010-07-21 12:50:57 -04002253 rc = genfs_read(p, fp);
2254 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Eric Paris9ee0c822010-06-11 12:37:05 -04002257 rc = range_read(p, fp);
2258 if (rc)
2259 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Eric Paris6371dcd2010-07-29 23:02:34 -04002261 rc = -ENOMEM;
2262 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2263 p->p_types.nprim,
2264 GFP_KERNEL | __GFP_ZERO);
2265 if (!p->type_attr_map_array)
2266 goto bad;
2267
2268 /* preallocate so we don't have to worry about the put ever failing */
2269 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
2270 GFP_KERNEL | __GFP_ZERO);
2271 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002272 goto bad;
2273
2274 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002275 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2276
2277 BUG_ON(!e);
2278 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002279 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002280 rc = ebitmap_read(e, fp);
2281 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002282 goto bad;
2283 }
2284 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002285 rc = ebitmap_set_bit(e, i, 1);
2286 if (rc)
2287 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002288 }
2289
KaiGai Koheid9250de2008-08-28 16:35:57 +09002290 rc = policydb_bounds_sanity_check(p);
2291 if (rc)
2292 goto bad;
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 rc = 0;
2295out:
2296 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 policydb_destroy(p);
2299 goto out;
2300}
Eric Pariscee74f42010-10-13 17:50:25 -04002301
2302/*
2303 * Write a MLS level structure to a policydb binary
2304 * representation file.
2305 */
2306static int mls_write_level(struct mls_level *l, void *fp)
2307{
2308 __le32 buf[1];
2309 int rc;
2310
2311 buf[0] = cpu_to_le32(l->sens);
2312 rc = put_entry(buf, sizeof(u32), 1, fp);
2313 if (rc)
2314 return rc;
2315
2316 rc = ebitmap_write(&l->cat, fp);
2317 if (rc)
2318 return rc;
2319
2320 return 0;
2321}
2322
2323/*
2324 * Write a MLS range structure to a policydb binary
2325 * representation file.
2326 */
2327static int mls_write_range_helper(struct mls_range *r, void *fp)
2328{
2329 __le32 buf[3];
2330 size_t items;
2331 int rc, eq;
2332
2333 eq = mls_level_eq(&r->level[1], &r->level[0]);
2334
2335 if (eq)
2336 items = 2;
2337 else
2338 items = 3;
2339 buf[0] = cpu_to_le32(items-1);
2340 buf[1] = cpu_to_le32(r->level[0].sens);
2341 if (!eq)
2342 buf[2] = cpu_to_le32(r->level[1].sens);
2343
2344 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2345
2346 rc = put_entry(buf, sizeof(u32), items, fp);
2347 if (rc)
2348 return rc;
2349
2350 rc = ebitmap_write(&r->level[0].cat, fp);
2351 if (rc)
2352 return rc;
2353 if (!eq) {
2354 rc = ebitmap_write(&r->level[1].cat, fp);
2355 if (rc)
2356 return rc;
2357 }
2358
2359 return 0;
2360}
2361
2362static int sens_write(void *vkey, void *datum, void *ptr)
2363{
2364 char *key = vkey;
2365 struct level_datum *levdatum = datum;
2366 struct policy_data *pd = ptr;
2367 void *fp = pd->fp;
2368 __le32 buf[2];
2369 size_t len;
2370 int rc;
2371
2372 len = strlen(key);
2373 buf[0] = cpu_to_le32(len);
2374 buf[1] = cpu_to_le32(levdatum->isalias);
2375 rc = put_entry(buf, sizeof(u32), 2, fp);
2376 if (rc)
2377 return rc;
2378
2379 rc = put_entry(key, 1, len, fp);
2380 if (rc)
2381 return rc;
2382
2383 rc = mls_write_level(levdatum->level, fp);
2384 if (rc)
2385 return rc;
2386
2387 return 0;
2388}
2389
2390static int cat_write(void *vkey, void *datum, void *ptr)
2391{
2392 char *key = vkey;
2393 struct cat_datum *catdatum = datum;
2394 struct policy_data *pd = ptr;
2395 void *fp = pd->fp;
2396 __le32 buf[3];
2397 size_t len;
2398 int rc;
2399
2400 len = strlen(key);
2401 buf[0] = cpu_to_le32(len);
2402 buf[1] = cpu_to_le32(catdatum->value);
2403 buf[2] = cpu_to_le32(catdatum->isalias);
2404 rc = put_entry(buf, sizeof(u32), 3, fp);
2405 if (rc)
2406 return rc;
2407
2408 rc = put_entry(key, 1, len, fp);
2409 if (rc)
2410 return rc;
2411
2412 return 0;
2413}
2414
2415static int role_trans_write(struct role_trans *r, void *fp)
2416{
2417 struct role_trans *tr;
2418 u32 buf[3];
2419 size_t nel;
2420 int rc;
2421
2422 nel = 0;
2423 for (tr = r; tr; tr = tr->next)
2424 nel++;
2425 buf[0] = cpu_to_le32(nel);
2426 rc = put_entry(buf, sizeof(u32), 1, fp);
2427 if (rc)
2428 return rc;
2429 for (tr = r; tr; tr = tr->next) {
2430 buf[0] = cpu_to_le32(tr->role);
2431 buf[1] = cpu_to_le32(tr->type);
2432 buf[2] = cpu_to_le32(tr->new_role);
2433 rc = put_entry(buf, sizeof(u32), 3, fp);
2434 if (rc)
2435 return rc;
2436 }
2437
2438 return 0;
2439}
2440
2441static int role_allow_write(struct role_allow *r, void *fp)
2442{
2443 struct role_allow *ra;
2444 u32 buf[2];
2445 size_t nel;
2446 int rc;
2447
2448 nel = 0;
2449 for (ra = r; ra; ra = ra->next)
2450 nel++;
2451 buf[0] = cpu_to_le32(nel);
2452 rc = put_entry(buf, sizeof(u32), 1, fp);
2453 if (rc)
2454 return rc;
2455 for (ra = r; ra; ra = ra->next) {
2456 buf[0] = cpu_to_le32(ra->role);
2457 buf[1] = cpu_to_le32(ra->new_role);
2458 rc = put_entry(buf, sizeof(u32), 2, fp);
2459 if (rc)
2460 return rc;
2461 }
2462 return 0;
2463}
2464
2465/*
2466 * Write a security context structure
2467 * to a policydb binary representation file.
2468 */
2469static int context_write(struct policydb *p, struct context *c,
2470 void *fp)
2471{
2472 int rc;
2473 __le32 buf[3];
2474
2475 buf[0] = cpu_to_le32(c->user);
2476 buf[1] = cpu_to_le32(c->role);
2477 buf[2] = cpu_to_le32(c->type);
2478
2479 rc = put_entry(buf, sizeof(u32), 3, fp);
2480 if (rc)
2481 return rc;
2482
2483 rc = mls_write_range_helper(&c->range, fp);
2484 if (rc)
2485 return rc;
2486
2487 return 0;
2488}
2489
2490/*
2491 * The following *_write functions are used to
2492 * write the symbol data to a policy database
2493 * binary representation file.
2494 */
2495
2496static int perm_write(void *vkey, void *datum, void *fp)
2497{
2498 char *key = vkey;
2499 struct perm_datum *perdatum = datum;
2500 __le32 buf[2];
2501 size_t len;
2502 int rc;
2503
2504 len = strlen(key);
2505 buf[0] = cpu_to_le32(len);
2506 buf[1] = cpu_to_le32(perdatum->value);
2507 rc = put_entry(buf, sizeof(u32), 2, fp);
2508 if (rc)
2509 return rc;
2510
2511 rc = put_entry(key, 1, len, fp);
2512 if (rc)
2513 return rc;
2514
2515 return 0;
2516}
2517
2518static int common_write(void *vkey, void *datum, void *ptr)
2519{
2520 char *key = vkey;
2521 struct common_datum *comdatum = datum;
2522 struct policy_data *pd = ptr;
2523 void *fp = pd->fp;
2524 __le32 buf[4];
2525 size_t len;
2526 int rc;
2527
2528 len = strlen(key);
2529 buf[0] = cpu_to_le32(len);
2530 buf[1] = cpu_to_le32(comdatum->value);
2531 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2532 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2533 rc = put_entry(buf, sizeof(u32), 4, fp);
2534 if (rc)
2535 return rc;
2536
2537 rc = put_entry(key, 1, len, fp);
2538 if (rc)
2539 return rc;
2540
2541 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2542 if (rc)
2543 return rc;
2544
2545 return 0;
2546}
2547
2548static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2549 void *fp)
2550{
2551 struct constraint_node *c;
2552 struct constraint_expr *e;
2553 __le32 buf[3];
2554 u32 nel;
2555 int rc;
2556
2557 for (c = node; c; c = c->next) {
2558 nel = 0;
2559 for (e = c->expr; e; e = e->next)
2560 nel++;
2561 buf[0] = cpu_to_le32(c->permissions);
2562 buf[1] = cpu_to_le32(nel);
2563 rc = put_entry(buf, sizeof(u32), 2, fp);
2564 if (rc)
2565 return rc;
2566 for (e = c->expr; e; e = e->next) {
2567 buf[0] = cpu_to_le32(e->expr_type);
2568 buf[1] = cpu_to_le32(e->attr);
2569 buf[2] = cpu_to_le32(e->op);
2570 rc = put_entry(buf, sizeof(u32), 3, fp);
2571 if (rc)
2572 return rc;
2573
2574 switch (e->expr_type) {
2575 case CEXPR_NAMES:
2576 rc = ebitmap_write(&e->names, fp);
2577 if (rc)
2578 return rc;
2579 break;
2580 default:
2581 break;
2582 }
2583 }
2584 }
2585
2586 return 0;
2587}
2588
2589static int class_write(void *vkey, void *datum, void *ptr)
2590{
2591 char *key = vkey;
2592 struct class_datum *cladatum = datum;
2593 struct policy_data *pd = ptr;
2594 void *fp = pd->fp;
2595 struct policydb *p = pd->p;
2596 struct constraint_node *c;
2597 __le32 buf[6];
2598 u32 ncons;
2599 size_t len, len2;
2600 int rc;
2601
2602 len = strlen(key);
2603 if (cladatum->comkey)
2604 len2 = strlen(cladatum->comkey);
2605 else
2606 len2 = 0;
2607
2608 ncons = 0;
2609 for (c = cladatum->constraints; c; c = c->next)
2610 ncons++;
2611
2612 buf[0] = cpu_to_le32(len);
2613 buf[1] = cpu_to_le32(len2);
2614 buf[2] = cpu_to_le32(cladatum->value);
2615 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2616 if (cladatum->permissions.table)
2617 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2618 else
2619 buf[4] = 0;
2620 buf[5] = cpu_to_le32(ncons);
2621 rc = put_entry(buf, sizeof(u32), 6, fp);
2622 if (rc)
2623 return rc;
2624
2625 rc = put_entry(key, 1, len, fp);
2626 if (rc)
2627 return rc;
2628
2629 if (cladatum->comkey) {
2630 rc = put_entry(cladatum->comkey, 1, len2, fp);
2631 if (rc)
2632 return rc;
2633 }
2634
2635 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2636 if (rc)
2637 return rc;
2638
2639 rc = write_cons_helper(p, cladatum->constraints, fp);
2640 if (rc)
2641 return rc;
2642
2643 /* write out the validatetrans rule */
2644 ncons = 0;
2645 for (c = cladatum->validatetrans; c; c = c->next)
2646 ncons++;
2647
2648 buf[0] = cpu_to_le32(ncons);
2649 rc = put_entry(buf, sizeof(u32), 1, fp);
2650 if (rc)
2651 return rc;
2652
2653 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2654 if (rc)
2655 return rc;
2656
2657 return 0;
2658}
2659
2660static int role_write(void *vkey, void *datum, void *ptr)
2661{
2662 char *key = vkey;
2663 struct role_datum *role = datum;
2664 struct policy_data *pd = ptr;
2665 void *fp = pd->fp;
2666 struct policydb *p = pd->p;
2667 __le32 buf[3];
2668 size_t items, len;
2669 int rc;
2670
2671 len = strlen(key);
2672 items = 0;
2673 buf[items++] = cpu_to_le32(len);
2674 buf[items++] = cpu_to_le32(role->value);
2675 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2676 buf[items++] = cpu_to_le32(role->bounds);
2677
2678 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2679
2680 rc = put_entry(buf, sizeof(u32), items, fp);
2681 if (rc)
2682 return rc;
2683
2684 rc = put_entry(key, 1, len, fp);
2685 if (rc)
2686 return rc;
2687
2688 rc = ebitmap_write(&role->dominates, fp);
2689 if (rc)
2690 return rc;
2691
2692 rc = ebitmap_write(&role->types, fp);
2693 if (rc)
2694 return rc;
2695
2696 return 0;
2697}
2698
2699static int type_write(void *vkey, void *datum, void *ptr)
2700{
2701 char *key = vkey;
2702 struct type_datum *typdatum = datum;
2703 struct policy_data *pd = ptr;
2704 struct policydb *p = pd->p;
2705 void *fp = pd->fp;
2706 __le32 buf[4];
2707 int rc;
2708 size_t items, len;
2709
2710 len = strlen(key);
2711 items = 0;
2712 buf[items++] = cpu_to_le32(len);
2713 buf[items++] = cpu_to_le32(typdatum->value);
2714 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2715 u32 properties = 0;
2716
2717 if (typdatum->primary)
2718 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2719
2720 if (typdatum->attribute)
2721 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2722
2723 buf[items++] = cpu_to_le32(properties);
2724 buf[items++] = cpu_to_le32(typdatum->bounds);
2725 } else {
2726 buf[items++] = cpu_to_le32(typdatum->primary);
2727 }
2728 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2729 rc = put_entry(buf, sizeof(u32), items, fp);
2730 if (rc)
2731 return rc;
2732
2733 rc = put_entry(key, 1, len, fp);
2734 if (rc)
2735 return rc;
2736
2737 return 0;
2738}
2739
2740static int user_write(void *vkey, void *datum, void *ptr)
2741{
2742 char *key = vkey;
2743 struct user_datum *usrdatum = datum;
2744 struct policy_data *pd = ptr;
2745 struct policydb *p = pd->p;
2746 void *fp = pd->fp;
2747 __le32 buf[3];
2748 size_t items, len;
2749 int rc;
2750
2751 len = strlen(key);
2752 items = 0;
2753 buf[items++] = cpu_to_le32(len);
2754 buf[items++] = cpu_to_le32(usrdatum->value);
2755 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2756 buf[items++] = cpu_to_le32(usrdatum->bounds);
2757 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
2758 rc = put_entry(buf, sizeof(u32), items, fp);
2759 if (rc)
2760 return rc;
2761
2762 rc = put_entry(key, 1, len, fp);
2763 if (rc)
2764 return rc;
2765
2766 rc = ebitmap_write(&usrdatum->roles, fp);
2767 if (rc)
2768 return rc;
2769
2770 rc = mls_write_range_helper(&usrdatum->range, fp);
2771 if (rc)
2772 return rc;
2773
2774 rc = mls_write_level(&usrdatum->dfltlevel, fp);
2775 if (rc)
2776 return rc;
2777
2778 return 0;
2779}
2780
2781static int (*write_f[SYM_NUM]) (void *key, void *datum,
2782 void *datap) =
2783{
2784 common_write,
2785 class_write,
2786 role_write,
2787 type_write,
2788 user_write,
2789 cond_write_bool,
2790 sens_write,
2791 cat_write,
2792};
2793
2794static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
2795 void *fp)
2796{
2797 unsigned int i, j, rc;
2798 size_t nel, len;
2799 __le32 buf[3];
2800 u32 nodebuf[8];
2801 struct ocontext *c;
2802 for (i = 0; i < info->ocon_num; i++) {
2803 nel = 0;
2804 for (c = p->ocontexts[i]; c; c = c->next)
2805 nel++;
2806 buf[0] = cpu_to_le32(nel);
2807 rc = put_entry(buf, sizeof(u32), 1, fp);
2808 if (rc)
2809 return rc;
2810 for (c = p->ocontexts[i]; c; c = c->next) {
2811 switch (i) {
2812 case OCON_ISID:
2813 buf[0] = cpu_to_le32(c->sid[0]);
2814 rc = put_entry(buf, sizeof(u32), 1, fp);
2815 if (rc)
2816 return rc;
2817 rc = context_write(p, &c->context[0], fp);
2818 if (rc)
2819 return rc;
2820 break;
2821 case OCON_FS:
2822 case OCON_NETIF:
2823 len = strlen(c->u.name);
2824 buf[0] = cpu_to_le32(len);
2825 rc = put_entry(buf, sizeof(u32), 1, fp);
2826 if (rc)
2827 return rc;
2828 rc = put_entry(c->u.name, 1, len, fp);
2829 if (rc)
2830 return rc;
2831 rc = context_write(p, &c->context[0], fp);
2832 if (rc)
2833 return rc;
2834 rc = context_write(p, &c->context[1], fp);
2835 if (rc)
2836 return rc;
2837 break;
2838 case OCON_PORT:
2839 buf[0] = cpu_to_le32(c->u.port.protocol);
2840 buf[1] = cpu_to_le32(c->u.port.low_port);
2841 buf[2] = cpu_to_le32(c->u.port.high_port);
2842 rc = put_entry(buf, sizeof(u32), 3, fp);
2843 if (rc)
2844 return rc;
2845 rc = context_write(p, &c->context[0], fp);
2846 if (rc)
2847 return rc;
2848 break;
2849 case OCON_NODE:
2850 nodebuf[0] = c->u.node.addr; /* network order */
2851 nodebuf[1] = c->u.node.mask; /* network order */
2852 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
2853 if (rc)
2854 return rc;
2855 rc = context_write(p, &c->context[0], fp);
2856 if (rc)
2857 return rc;
2858 break;
2859 case OCON_FSUSE:
2860 buf[0] = cpu_to_le32(c->v.behavior);
2861 len = strlen(c->u.name);
2862 buf[1] = cpu_to_le32(len);
2863 rc = put_entry(buf, sizeof(u32), 2, fp);
2864 if (rc)
2865 return rc;
2866 rc = put_entry(c->u.name, 1, len, 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_NODE6:
2874 for (j = 0; j < 4; j++)
2875 nodebuf[j] = c->u.node6.addr[j]; /* network order */
2876 for (j = 0; j < 4; j++)
2877 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
2878 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
2879 if (rc)
2880 return rc;
2881 rc = context_write(p, &c->context[0], fp);
2882 if (rc)
2883 return rc;
2884 break;
2885 }
2886 }
2887 }
2888 return 0;
2889}
2890
2891static int genfs_write(struct policydb *p, void *fp)
2892{
2893 struct genfs *genfs;
2894 struct ocontext *c;
2895 size_t len;
2896 __le32 buf[1];
2897 int rc;
2898
2899 len = 0;
2900 for (genfs = p->genfs; genfs; genfs = genfs->next)
2901 len++;
2902 buf[0] = cpu_to_le32(len);
2903 rc = put_entry(buf, sizeof(u32), 1, fp);
2904 if (rc)
2905 return rc;
2906 for (genfs = p->genfs; genfs; genfs = genfs->next) {
2907 len = strlen(genfs->fstype);
2908 buf[0] = cpu_to_le32(len);
2909 rc = put_entry(buf, sizeof(u32), 1, fp);
2910 if (rc)
2911 return rc;
2912 rc = put_entry(genfs->fstype, 1, len, fp);
2913 if (rc)
2914 return rc;
2915 len = 0;
2916 for (c = genfs->head; c; c = c->next)
2917 len++;
2918 buf[0] = cpu_to_le32(len);
2919 rc = put_entry(buf, sizeof(u32), 1, fp);
2920 if (rc)
2921 return rc;
2922 for (c = genfs->head; c; c = c->next) {
2923 len = strlen(c->u.name);
2924 buf[0] = cpu_to_le32(len);
2925 rc = put_entry(buf, sizeof(u32), 1, fp);
2926 if (rc)
2927 return rc;
2928 rc = put_entry(c->u.name, 1, len, fp);
2929 if (rc)
2930 return rc;
2931 buf[0] = cpu_to_le32(c->v.sclass);
2932 rc = put_entry(buf, sizeof(u32), 1, fp);
2933 if (rc)
2934 return rc;
2935 rc = context_write(p, &c->context[0], fp);
2936 if (rc)
2937 return rc;
2938 }
2939 }
2940 return 0;
2941}
2942
2943static int range_count(void *key, void *data, void *ptr)
2944{
2945 int *cnt = ptr;
2946 *cnt = *cnt + 1;
2947
2948 return 0;
2949}
2950
2951static int range_write_helper(void *key, void *data, void *ptr)
2952{
2953 __le32 buf[2];
2954 struct range_trans *rt = key;
2955 struct mls_range *r = data;
2956 struct policy_data *pd = ptr;
2957 void *fp = pd->fp;
2958 struct policydb *p = pd->p;
2959 int rc;
2960
2961 buf[0] = cpu_to_le32(rt->source_type);
2962 buf[1] = cpu_to_le32(rt->target_type);
2963 rc = put_entry(buf, sizeof(u32), 2, fp);
2964 if (rc)
2965 return rc;
2966 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
2967 buf[0] = cpu_to_le32(rt->target_class);
2968 rc = put_entry(buf, sizeof(u32), 1, fp);
2969 if (rc)
2970 return rc;
2971 }
2972 rc = mls_write_range_helper(r, fp);
2973 if (rc)
2974 return rc;
2975
2976 return 0;
2977}
2978
2979static int range_write(struct policydb *p, void *fp)
2980{
2981 size_t nel;
2982 __le32 buf[1];
2983 int rc;
2984 struct policy_data pd;
2985
2986 pd.p = p;
2987 pd.fp = fp;
2988
2989 /* count the number of entries in the hashtab */
2990 nel = 0;
2991 rc = hashtab_map(p->range_tr, range_count, &nel);
2992 if (rc)
2993 return rc;
2994
2995 buf[0] = cpu_to_le32(nel);
2996 rc = put_entry(buf, sizeof(u32), 1, fp);
2997 if (rc)
2998 return rc;
2999
3000 /* actually write all of the entries */
3001 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3002 if (rc)
3003 return rc;
3004
3005 return 0;
3006}
3007
3008/*
3009 * Write the configuration data in a policy database
3010 * structure to a policy database binary representation
3011 * file.
3012 */
3013int policydb_write(struct policydb *p, void *fp)
3014{
3015 unsigned int i, num_syms;
3016 int rc;
3017 __le32 buf[4];
3018 u32 config;
3019 size_t len;
3020 struct policydb_compat_info *info;
3021
3022 /*
3023 * refuse to write policy older than compressed avtab
3024 * to simplify the writer. There are other tests dropped
3025 * since we assume this throughout the writer code. Be
3026 * careful if you ever try to remove this restriction
3027 */
3028 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3029 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3030 " Because it is less than version %d\n", p->policyvers,
3031 POLICYDB_VERSION_AVTAB);
3032 return -EINVAL;
3033 }
3034
3035 config = 0;
3036 if (p->mls_enabled)
3037 config |= POLICYDB_CONFIG_MLS;
3038
3039 if (p->reject_unknown)
3040 config |= REJECT_UNKNOWN;
3041 if (p->allow_unknown)
3042 config |= ALLOW_UNKNOWN;
3043
3044 /* Write the magic number and string identifiers. */
3045 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3046 len = strlen(POLICYDB_STRING);
3047 buf[1] = cpu_to_le32(len);
3048 rc = put_entry(buf, sizeof(u32), 2, fp);
3049 if (rc)
3050 return rc;
3051 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3052 if (rc)
3053 return rc;
3054
3055 /* Write the version, config, and table sizes. */
3056 info = policydb_lookup_compat(p->policyvers);
3057 if (!info) {
3058 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3059 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003060 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003061 }
3062
3063 buf[0] = cpu_to_le32(p->policyvers);
3064 buf[1] = cpu_to_le32(config);
3065 buf[2] = cpu_to_le32(info->sym_num);
3066 buf[3] = cpu_to_le32(info->ocon_num);
3067
3068 rc = put_entry(buf, sizeof(u32), 4, fp);
3069 if (rc)
3070 return rc;
3071
3072 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3073 rc = ebitmap_write(&p->policycaps, fp);
3074 if (rc)
3075 return rc;
3076 }
3077
3078 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3079 rc = ebitmap_write(&p->permissive_map, fp);
3080 if (rc)
3081 return rc;
3082 }
3083
3084 num_syms = info->sym_num;
3085 for (i = 0; i < num_syms; i++) {
3086 struct policy_data pd;
3087
3088 pd.fp = fp;
3089 pd.p = p;
3090
3091 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3092 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3093
3094 rc = put_entry(buf, sizeof(u32), 2, fp);
3095 if (rc)
3096 return rc;
3097 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3098 if (rc)
3099 return rc;
3100 }
3101
3102 rc = avtab_write(p, &p->te_avtab, fp);
3103 if (rc)
3104 return rc;
3105
3106 rc = cond_write_list(p, p->cond_list, fp);
3107 if (rc)
3108 return rc;
3109
3110 rc = role_trans_write(p->role_tr, fp);
3111 if (rc)
3112 return rc;
3113
3114 rc = role_allow_write(p->role_allow, fp);
3115 if (rc)
3116 return rc;
3117
3118 rc = ocontext_write(p, info, fp);
3119 if (rc)
3120 return rc;
3121
3122 rc = genfs_write(p, fp);
3123 if (rc)
3124 return rc;
3125
3126 rc = range_write(p, fp);
3127 if (rc)
3128 return rc;
3129
3130 for (i = 0; i < p->p_types.nprim; i++) {
3131 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3132
3133 BUG_ON(!e);
3134 rc = ebitmap_write(e, fp);
3135 if (rc)
3136 return rc;
3137 }
3138
3139 return 0;
3140}