blob: bc2a586f095c5f73c214f1c475a2798a5f1439e3 [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 Moore82c21bf2011-08-01 11:10:33 +000016 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -050017 *
18 * Added support for the policy capability bitmap
19 *
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
Eric Paris2ced3df2008-04-17 13:37:12 -040024 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the Free Software Foundation, version 2.
26 */
27
28#include <linux/kernel.h>
Eric Paris9dc99782007-06-04 17:41:22 -040029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/string.h>
32#include <linux/errno.h>
KaiGai Koheid9250de2008-08-28 16:35:57 +090033#include <linux/audit.h>
Eric Paris6371dcd2010-07-29 23:02:34 -040034#include <linux/flex_array.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "security.h"
36
37#include "policydb.h"
38#include "conditional.h"
39#include "mls.h"
Eric Pariscee74f42010-10-13 17:50:25 -040040#include "services.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define _DEBUG_HASHES
43
44#ifdef DEBUG_HASHES
Stephen Hemminger634a5392010-03-04 21:59:03 -080045static const char *symtab_name[SYM_NUM] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "common prefixes",
47 "classes",
48 "roles",
49 "types",
50 "users",
51 "bools",
52 "levels",
53 "categories",
54};
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int symtab_sizes[SYM_NUM] = {
58 2,
59 32,
60 16,
61 512,
62 128,
63 16,
64 16,
65 16,
66};
67
68struct policydb_compat_info {
69 int version;
70 int sym_num;
71 int ocon_num;
72};
73
74/* These need to be updated if SYM_NUM or OCON_NUM changes */
75static struct policydb_compat_info policydb_compat[] = {
76 {
Eric Paris2ced3df2008-04-17 13:37:12 -040077 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 },
81 {
Eric Paris2ced3df2008-04-17 13:37:12 -040082 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 },
86 {
Eric Paris2ced3df2008-04-17 13:37:12 -040087 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 },
91 {
Eric Paris2ced3df2008-04-17 13:37:12 -040092 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 },
96 {
Eric Paris2ced3df2008-04-17 13:37:12 -040097 .version = POLICYDB_VERSION_MLS,
98 .sym_num = SYM_NUM,
99 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 },
Stephen Smalley782ebb92005-09-03 15:55:16 -0700101 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400102 .version = POLICYDB_VERSION_AVTAB,
103 .sym_num = SYM_NUM,
104 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700105 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700106 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400107 .version = POLICYDB_VERSION_RANGETRANS,
108 .sym_num = SYM_NUM,
109 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700110 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500111 {
112 .version = POLICYDB_VERSION_POLCAP,
113 .sym_num = SYM_NUM,
114 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100115 },
116 {
117 .version = POLICYDB_VERSION_PERMISSIVE,
118 .sym_num = SYM_NUM,
119 .ocon_num = OCON_NUM,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900120 },
121 {
122 .version = POLICYDB_VERSION_BOUNDARY,
123 .sym_num = SYM_NUM,
124 .ocon_num = OCON_NUM,
125 },
Eric Paris652bb9b2011-02-01 11:05:40 -0500126 {
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
128 .sym_num = SYM_NUM,
129 .ocon_num = OCON_NUM,
130 },
Harry Ciao80239762011-03-25 13:51:56 +0800131 {
132 .version = POLICYDB_VERSION_ROLETRANS,
133 .sym_num = SYM_NUM,
134 .ocon_num = OCON_NUM,
135 },
Eric Parisaa893262012-03-20 14:35:12 -0400136 {
137 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
138 .sym_num = SYM_NUM,
139 .ocon_num = OCON_NUM,
140 },
Eric Pariseed77952012-03-20 14:35:12 -0400141 {
142 .version = POLICYDB_VERSION_DEFAULT_TYPE,
143 .sym_num = SYM_NUM,
144 .ocon_num = OCON_NUM,
145 },
Richard Hainesa660bec2013-11-19 17:34:23 -0500146 {
147 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
148 .sym_num = SYM_NUM,
149 .ocon_num = OCON_NUM,
150 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153static struct policydb_compat_info *policydb_lookup_compat(int version)
154{
155 int i;
156 struct policydb_compat_info *info = NULL;
157
Tobias Klauser32725ad2006-01-06 00:11:23 -0800158 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (policydb_compat[i].version == version) {
160 info = &policydb_compat[i];
161 break;
162 }
163 }
164 return info;
165}
166
167/*
168 * Initialize the role table.
169 */
170static int roles_init(struct policydb *p)
171{
172 char *key = NULL;
173 int rc;
174 struct role_datum *role;
175
Eric Paris9398c7f2010-11-23 11:40:08 -0500176 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800177 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500178 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500180
181 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500183 if (role->value != OBJECT_R_VAL)
184 goto out;
185
186 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200187 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500188 if (!key)
189 goto out;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 rc = hashtab_insert(p->p_roles.table, key, role);
192 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Eric Paris9398c7f2010-11-23 11:40:08 -0500195 return 0;
196out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500199 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Eric Paris2463c26d2011-04-28 15:11:21 -0400202static u32 filenametr_hash(struct hashtab *h, const void *k)
203{
204 const struct filename_trans *ft = k;
205 unsigned long hash;
206 unsigned int byte_num;
207 unsigned char focus;
208
209 hash = ft->stype ^ ft->ttype ^ ft->tclass;
210
211 byte_num = 0;
212 while ((focus = ft->name[byte_num++]))
213 hash = partial_name_hash(focus, hash);
214 return hash & (h->size - 1);
215}
216
217static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
218{
219 const struct filename_trans *ft1 = k1;
220 const struct filename_trans *ft2 = k2;
221 int v;
222
223 v = ft1->stype - ft2->stype;
224 if (v)
225 return v;
226
227 v = ft1->ttype - ft2->ttype;
228 if (v)
229 return v;
230
231 v = ft1->tclass - ft2->tclass;
232 if (v)
233 return v;
234
235 return strcmp(ft1->name, ft2->name);
236
237}
238
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500239static u32 rangetr_hash(struct hashtab *h, const void *k)
240{
241 const struct range_trans *key = k;
242 return (key->source_type + (key->target_type << 3) +
243 (key->target_class << 5)) & (h->size - 1);
244}
245
246static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
247{
248 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400249 int v;
250
251 v = key1->source_type - key2->source_type;
252 if (v)
253 return v;
254
255 v = key1->target_type - key2->target_type;
256 if (v)
257 return v;
258
259 v = key1->target_class - key2->target_class;
260
261 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * Initialize a policy database structure.
266 */
267static int policydb_init(struct policydb *p)
268{
269 int i, rc;
270
271 memset(p, 0, sizeof(*p));
272
273 for (i = 0; i < SYM_NUM; i++) {
274 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
275 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 rc = avtab_init(&p->te_avtab);
280 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 rc = roles_init(p);
284 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 rc = cond_policydb_init(p);
288 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Eric Paris2463c26d2011-04-28 15:11:21 -0400291 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
292 if (!p->filename_trans)
293 goto out;
294
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500295 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
296 if (!p->range_tr)
Eric Paris9398c7f2010-11-23 11:40:08 -0500297 goto out;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500298
Eric Paris03a4c012011-04-28 15:11:21 -0400299 ebitmap_init(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500300 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100301 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500302
Eric Paris9398c7f2010-11-23 11:40:08 -0500303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304out:
Eric Paris2463c26d2011-04-28 15:11:21 -0400305 hashtab_destroy(p->filename_trans);
306 hashtab_destroy(p->range_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 for (i = 0; i < SYM_NUM; i++)
308 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500309 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/*
313 * The following *_index functions are used to
314 * define the val_to_name and val_to_struct arrays
315 * in a policy database structure. The val_to_name
316 * arrays are used when converting security context
317 * structures into string representations. The
318 * val_to_struct arrays are used when the attributes
319 * of a class, role, or user are needed.
320 */
321
322static int common_index(void *key, void *datum, void *datap)
323{
324 struct policydb *p;
325 struct common_datum *comdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500326 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 comdatum = datum;
329 p = datap;
330 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
331 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500332
333 fa = p->sym_val_to_name[SYM_COMMONS];
334 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
335 GFP_KERNEL | __GFP_ZERO))
336 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340static int class_index(void *key, void *datum, void *datap)
341{
342 struct policydb *p;
343 struct class_datum *cladatum;
Eric Parisac76c052010-11-29 15:47:09 -0500344 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 cladatum = datum;
347 p = datap;
348 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
349 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500350 fa = p->sym_val_to_name[SYM_CLASSES];
351 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
352 GFP_KERNEL | __GFP_ZERO))
353 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 p->class_val_to_struct[cladatum->value - 1] = cladatum;
355 return 0;
356}
357
358static int role_index(void *key, void *datum, void *datap)
359{
360 struct policydb *p;
361 struct role_datum *role;
Eric Parisac76c052010-11-29 15:47:09 -0500362 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 role = datum;
365 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900366 if (!role->value
367 || role->value > p->p_roles.nprim
368 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500370
371 fa = p->sym_val_to_name[SYM_ROLES];
372 if (flex_array_put_ptr(fa, role->value - 1, key,
373 GFP_KERNEL | __GFP_ZERO))
374 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 p->role_val_to_struct[role->value - 1] = role;
376 return 0;
377}
378
379static int type_index(void *key, void *datum, void *datap)
380{
381 struct policydb *p;
382 struct type_datum *typdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500383 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 typdatum = datum;
386 p = datap;
387
388 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900389 if (!typdatum->value
390 || typdatum->value > p->p_types.nprim
391 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500393 fa = p->sym_val_to_name[SYM_TYPES];
394 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
396 BUG();
397
398 fa = p->type_val_to_struct_array;
399 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500400 GFP_KERNEL | __GFP_ZERO))
401 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 return 0;
405}
406
407static int user_index(void *key, void *datum, void *datap)
408{
409 struct policydb *p;
410 struct user_datum *usrdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500411 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 usrdatum = datum;
414 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900415 if (!usrdatum->value
416 || usrdatum->value > p->p_users.nprim
417 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500419
420 fa = p->sym_val_to_name[SYM_USERS];
421 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
422 GFP_KERNEL | __GFP_ZERO))
423 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
425 return 0;
426}
427
428static int sens_index(void *key, void *datum, void *datap)
429{
430 struct policydb *p;
431 struct level_datum *levdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500432 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 levdatum = datum;
435 p = datap;
436
437 if (!levdatum->isalias) {
438 if (!levdatum->level->sens ||
439 levdatum->level->sens > p->p_levels.nprim)
440 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500441 fa = p->sym_val_to_name[SYM_LEVELS];
442 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
443 GFP_KERNEL | __GFP_ZERO))
444 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 return 0;
448}
449
450static int cat_index(void *key, void *datum, void *datap)
451{
452 struct policydb *p;
453 struct cat_datum *catdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500454 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 catdatum = datum;
457 p = datap;
458
459 if (!catdatum->isalias) {
460 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
461 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500462 fa = p->sym_val_to_name[SYM_CATS];
463 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
464 GFP_KERNEL | __GFP_ZERO))
465 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 return 0;
469}
470
471static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
472{
473 common_index,
474 class_index,
475 role_index,
476 type_index,
477 user_index,
478 cond_index_bool,
479 sens_index,
480 cat_index,
481};
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef DEBUG_HASHES
Eric Parisbe30b162011-04-28 15:11:21 -0400484static void hash_eval(struct hashtab *h, const char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500485{
486 struct hashtab_info info;
487
488 hashtab_stat(h, &info);
Eric Parisbe30b162011-04-28 15:11:21 -0400489 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
490 "longest chain length %d\n", hash_name, h->nel,
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500491 info.slots_used, h->size, info.max_chain_len);
492}
Eric Parisbe30b162011-04-28 15:11:21 -0400493
494static void symtab_hash_eval(struct symtab *s)
495{
496 int i;
497
498 for (i = 0; i < SYM_NUM; i++)
499 hash_eval(s[i].table, symtab_name[i]);
500}
501
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500502#else
Eric Parisbe30b162011-04-28 15:11:21 -0400503static inline void hash_eval(struct hashtab *h, char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500504{
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#endif
507
508/*
509 * Define the other val_to_name and val_to_struct arrays
510 * in a policy database structure.
511 *
512 * Caller must clean up on failure.
513 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500514static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Eric Paris9398c7f2010-11-23 11:40:08 -0500516 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
James Morris454d9722008-02-26 20:42:02 +1100518 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100520 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 printk(", %d sens, %d cats", p->p_levels.nprim,
522 p->p_cats.nprim);
523 printk("\n");
524
James Morris454d9722008-02-26 20:42:02 +1100525 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 p->p_classes.nprim, p->te_avtab.nel);
527
528#ifdef DEBUG_HASHES
529 avtab_hash_eval(&p->te_avtab, "rules");
530 symtab_hash_eval(p->symtab);
531#endif
532
Eric Paris9398c7f2010-11-23 11:40:08 -0500533 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500534 p->class_val_to_struct =
535 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
536 GFP_KERNEL);
537 if (!p->class_val_to_struct)
538 goto out;
539
540 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 p->role_val_to_struct =
542 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400543 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500544 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Eric Paris9398c7f2010-11-23 11:40:08 -0500547 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 p->user_val_to_struct =
549 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400550 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500551 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Eric Paris23bdecb2010-11-29 15:47:09 -0500554 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500555 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500556 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
557 p->p_types.nprim,
558 GFP_KERNEL | __GFP_ZERO);
559 if (!p->type_val_to_struct_array)
560 goto out;
561
562 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
Eric Paris5a3ea872011-04-28 15:55:52 -0400563 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
Eric Paris23bdecb2010-11-29 15:47:09 -0500564 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900565 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900566
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300567 rc = cond_init_bool_indexes(p);
568 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500571 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500572 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500573 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
574 p->symtab[i].nprim,
575 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500576 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500578
579 rc = flex_array_prealloc(p->sym_val_to_name[i],
Eric Paris5a3ea872011-04-28 15:55:52 -0400580 0, p->symtab[i].nprim,
Eric Parisac76c052010-11-29 15:47:09 -0500581 GFP_KERNEL | __GFP_ZERO);
582 if (rc)
583 goto out;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
586 if (rc)
587 goto out;
588 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500589 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590out:
591 return rc;
592}
593
594/*
595 * The following *_destroy functions are used to
596 * free any memory allocated for each kind of
597 * symbol data in the policy database.
598 */
599
600static int perm_destroy(void *key, void *datum, void *p)
601{
602 kfree(key);
603 kfree(datum);
604 return 0;
605}
606
607static int common_destroy(void *key, void *datum, void *p)
608{
609 struct common_datum *comdatum;
610
611 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500612 if (datum) {
613 comdatum = datum;
614 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
615 hashtab_destroy(comdatum->permissions.table);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 kfree(datum);
618 return 0;
619}
620
Richard Hainesa660bec2013-11-19 17:34:23 -0500621static void constraint_expr_destroy(struct constraint_expr *expr)
622{
623 if (expr) {
624 ebitmap_destroy(&expr->names);
625 if (expr->type_names) {
626 ebitmap_destroy(&expr->type_names->types);
627 ebitmap_destroy(&expr->type_names->negset);
628 kfree(expr->type_names);
629 }
630 kfree(expr);
631 }
632}
633
James Morris6cbda6b2006-11-29 16:50:27 -0500634static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct class_datum *cladatum;
637 struct constraint_node *constraint, *ctemp;
638 struct constraint_expr *e, *etmp;
639
640 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500641 if (datum) {
642 cladatum = datum;
643 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
644 hashtab_destroy(cladatum->permissions.table);
645 constraint = cladatum->constraints;
646 while (constraint) {
647 e = constraint->expr;
648 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500649 etmp = e;
650 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500651 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500652 }
653 ctemp = constraint;
654 constraint = constraint->next;
655 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Paris9398c7f2010-11-23 11:40:08 -0500658 constraint = cladatum->validatetrans;
659 while (constraint) {
660 e = constraint->expr;
661 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500662 etmp = e;
663 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500664 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500665 }
666 ctemp = constraint;
667 constraint = constraint->next;
668 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500670 kfree(cladatum->comkey);
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kfree(datum);
673 return 0;
674}
675
676static int role_destroy(void *key, void *datum, void *p)
677{
678 struct role_datum *role;
679
680 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500681 if (datum) {
682 role = datum;
683 ebitmap_destroy(&role->dominates);
684 ebitmap_destroy(&role->types);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 kfree(datum);
687 return 0;
688}
689
690static int type_destroy(void *key, void *datum, void *p)
691{
692 kfree(key);
693 kfree(datum);
694 return 0;
695}
696
697static int user_destroy(void *key, void *datum, void *p)
698{
699 struct user_datum *usrdatum;
700
701 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500702 if (datum) {
703 usrdatum = datum;
704 ebitmap_destroy(&usrdatum->roles);
705 ebitmap_destroy(&usrdatum->range.level[0].cat);
706 ebitmap_destroy(&usrdatum->range.level[1].cat);
707 ebitmap_destroy(&usrdatum->dfltlevel.cat);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 kfree(datum);
710 return 0;
711}
712
713static int sens_destroy(void *key, void *datum, void *p)
714{
715 struct level_datum *levdatum;
716
717 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500718 if (datum) {
719 levdatum = datum;
720 ebitmap_destroy(&levdatum->level->cat);
721 kfree(levdatum->level);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 kfree(datum);
724 return 0;
725}
726
727static int cat_destroy(void *key, void *datum, void *p)
728{
729 kfree(key);
730 kfree(datum);
731 return 0;
732}
733
734static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
735{
736 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500737 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 role_destroy,
739 type_destroy,
740 user_destroy,
741 cond_destroy_bool,
742 sens_destroy,
743 cat_destroy,
744};
745
Eric Paris2463c26d2011-04-28 15:11:21 -0400746static int filenametr_destroy(void *key, void *datum, void *p)
747{
748 struct filename_trans *ft = key;
749 kfree(ft->name);
750 kfree(key);
751 kfree(datum);
752 cond_resched();
753 return 0;
754}
755
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500756static int range_tr_destroy(void *key, void *datum, void *p)
757{
758 struct mls_range *rt = datum;
759 kfree(key);
760 ebitmap_destroy(&rt->level[0].cat);
761 ebitmap_destroy(&rt->level[1].cat);
762 kfree(datum);
763 cond_resched();
764 return 0;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static void ocontext_destroy(struct ocontext *c, int i)
768{
Eric Parisd1b43542010-07-21 12:50:57 -0400769 if (!c)
770 return;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 context_destroy(&c->context[0]);
773 context_destroy(&c->context[1]);
774 if (i == OCON_ISID || i == OCON_FS ||
775 i == OCON_NETIF || i == OCON_FSUSE)
776 kfree(c->u.name);
777 kfree(c);
778}
779
780/*
781 * Free any memory allocated by a policy database structure.
782 */
783void policydb_destroy(struct policydb *p)
784{
785 struct ocontext *c, *ctmp;
786 struct genfs *g, *gtmp;
787 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700788 struct role_allow *ra, *lra = NULL;
789 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400792 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
794 hashtab_destroy(p->symtab[i].table);
795 }
796
Eric Parisac76c052010-11-29 15:47:09 -0500797 for (i = 0; i < SYM_NUM; i++) {
798 if (p->sym_val_to_name[i])
799 flex_array_free(p->sym_val_to_name[i]);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700802 kfree(p->class_val_to_struct);
803 kfree(p->role_val_to_struct);
804 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500805 if (p->type_val_to_struct_array)
806 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 avtab_destroy(&p->te_avtab);
809
810 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400811 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 c = p->ocontexts[i];
813 while (c) {
814 ctmp = c;
815 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400816 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400818 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 g = p->genfs;
822 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400823 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kfree(g->fstype);
825 c = g->head;
826 while (c) {
827 ctmp = c;
828 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400829 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 gtmp = g;
832 g = g->next;
833 kfree(gtmp);
834 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400835 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 cond_policydb_destroy(p);
838
Stephen Smalley782ebb92005-09-03 15:55:16 -0700839 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400840 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800841 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700842 ltr = tr;
843 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800844 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700845
Eric Paris2ced3df2008-04-17 13:37:12 -0400846 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400847 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800848 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700849 lra = ra;
850 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800851 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700852
Eric Paris2463c26d2011-04-28 15:11:21 -0400853 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
854 hashtab_destroy(p->filename_trans);
855
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500856 hashtab_map(p->range_tr, range_tr_destroy, NULL);
857 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700858
Eric Paris6371dcd2010-07-29 23:02:34 -0400859 if (p->type_attr_map_array) {
860 for (i = 0; i < p->p_types.nprim; i++) {
861 struct ebitmap *e;
862
863 e = flex_array_get(p->type_attr_map_array, i);
864 if (!e)
865 continue;
866 ebitmap_destroy(e);
867 }
868 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700869 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500870
Eric Paris03a4c012011-04-28 15:11:21 -0400871 ebitmap_destroy(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500872 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100873 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return;
876}
877
878/*
879 * Load the initial SIDs specified in a policy database
880 * structure into a SID table.
881 */
882int policydb_load_isids(struct policydb *p, struct sidtab *s)
883{
884 struct ocontext *head, *c;
885 int rc;
886
887 rc = sidtab_init(s);
888 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100889 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto out;
891 }
892
893 head = p->ocontexts[OCON_ISID];
894 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500895 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500897 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
898 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500901
902 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
903 if (rc) {
904 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
905 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out;
907 }
908 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500909 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910out:
911 return rc;
912}
913
Stephen Smalley45e54212007-11-07 10:08:00 -0500914int policydb_class_isvalid(struct policydb *p, unsigned int class)
915{
916 if (!class || class > p->p_classes.nprim)
917 return 0;
918 return 1;
919}
920
921int policydb_role_isvalid(struct policydb *p, unsigned int role)
922{
923 if (!role || role > p->p_roles.nprim)
924 return 0;
925 return 1;
926}
927
928int policydb_type_isvalid(struct policydb *p, unsigned int type)
929{
930 if (!type || type > p->p_types.nprim)
931 return 0;
932 return 1;
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*
936 * Return 1 if the fields in the security context
937 * structure `c' are valid. Return 0 otherwise.
938 */
939int policydb_context_isvalid(struct policydb *p, struct context *c)
940{
941 struct role_datum *role;
942 struct user_datum *usrdatum;
943
944 if (!c->role || c->role > p->p_roles.nprim)
945 return 0;
946
947 if (!c->user || c->user > p->p_users.nprim)
948 return 0;
949
950 if (!c->type || c->type > p->p_types.nprim)
951 return 0;
952
953 if (c->role != OBJECT_R_VAL) {
954 /*
955 * Role must be authorized for the type.
956 */
957 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500958 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* role may not be associated with type */
960 return 0;
961
962 /*
963 * User must be authorized for the role.
964 */
965 usrdatum = p->user_val_to_struct[c->user - 1];
966 if (!usrdatum)
967 return 0;
968
Eric Paris9398c7f2010-11-23 11:40:08 -0500969 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* user may not be associated with role */
971 return 0;
972 }
973
974 if (!mls_context_isvalid(p, c))
975 return 0;
976
977 return 1;
978}
979
980/*
981 * Read a MLS range structure from a policydb binary
982 * representation file.
983 */
984static int mls_read_range_helper(struct mls_range *r, void *fp)
985{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700986 __le32 buf[2];
987 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int rc;
989
990 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500991 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto out;
993
Eric Paris9398c7f2010-11-23 11:40:08 -0500994 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 items = le32_to_cpu(buf[0]);
996 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100997 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto out;
999 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -05001002 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001003 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto out;
1005 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 r->level[0].sens = le32_to_cpu(buf[0]);
1008 if (items > 1)
1009 r->level[1].sens = le32_to_cpu(buf[1]);
1010 else
1011 r->level[1].sens = r->level[0].sens;
1012
1013 rc = ebitmap_read(&r->level[0].cat, fp);
1014 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001015 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017 }
1018 if (items > 1) {
1019 rc = ebitmap_read(&r->level[1].cat, fp);
1020 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001021 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto bad_high;
1023 }
1024 } else {
1025 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1026 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001027 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto bad_high;
1029 }
1030 }
1031
Eric Paris9398c7f2010-11-23 11:40:08 -05001032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033bad_high:
1034 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -05001035out:
1036 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039/*
1040 * Read and validate a security context structure
1041 * from a policydb binary representation file.
1042 */
1043static int context_read_and_validate(struct context *c,
1044 struct policydb *p,
1045 void *fp)
1046{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001047 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int rc;
1049
1050 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001051 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001052 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto out;
1054 }
1055 c->user = le32_to_cpu(buf[0]);
1056 c->role = le32_to_cpu(buf[1]);
1057 c->type = le32_to_cpu(buf[2]);
1058 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001059 rc = mls_read_range_helper(&c->range, fp);
1060 if (rc) {
1061 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto out;
1063 }
1064 }
1065
Eric Paris9398c7f2010-11-23 11:40:08 -05001066 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001068 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001072 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073out:
1074 return rc;
1075}
1076
1077/*
1078 * The following *_read functions are used to
1079 * read the symbol data from a policy database
1080 * binary representation file.
1081 */
1082
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001083static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1084{
1085 int rc;
1086 char *str;
1087
1088 str = kmalloc(len + 1, flags);
1089 if (!str)
1090 return -ENOMEM;
1091
1092 /* it's expected the caller should free the str */
1093 *strp = str;
1094
1095 rc = next_entry(str, fp, len);
1096 if (rc)
1097 return rc;
1098
1099 str[len] = '\0';
1100 return 0;
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1104{
1105 char *key = NULL;
1106 struct perm_datum *perdatum;
1107 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001108 __le32 buf[2];
1109 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Paris9398c7f2010-11-23 11:40:08 -05001111 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001112 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001113 if (!perdatum)
1114 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001117 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto bad;
1119
1120 len = le32_to_cpu(buf[0]);
1121 perdatum->value = le32_to_cpu(buf[1]);
1122
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001123 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001124 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 rc = hashtab_insert(h, key, perdatum);
1128 if (rc)
1129 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001130
1131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132bad:
1133 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1138{
1139 char *key = NULL;
1140 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001141 __le32 buf[4];
1142 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int i, rc;
1144
Eric Paris9398c7f2010-11-23 11:40:08 -05001145 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001146 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001147 if (!comdatum)
1148 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001151 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto bad;
1153
1154 len = le32_to_cpu(buf[0]);
1155 comdatum->value = le32_to_cpu(buf[1]);
1156
1157 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1158 if (rc)
1159 goto bad;
1160 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1161 nel = le32_to_cpu(buf[3]);
1162
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001163 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001164 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 for (i = 0; i < nel; i++) {
1168 rc = perm_read(p, comdatum->permissions.table, fp);
1169 if (rc)
1170 goto bad;
1171 }
1172
1173 rc = hashtab_insert(h, key, comdatum);
1174 if (rc)
1175 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177bad:
1178 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001179 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Richard Hainesa660bec2013-11-19 17:34:23 -05001182static void type_set_init(struct type_set *t)
1183{
1184 ebitmap_init(&t->types);
1185 ebitmap_init(&t->negset);
1186}
1187
1188static int type_set_read(struct type_set *t, void *fp)
1189{
1190 __le32 buf[1];
1191 int rc;
1192
1193 if (ebitmap_read(&t->types, fp))
1194 return -EINVAL;
1195 if (ebitmap_read(&t->negset, fp))
1196 return -EINVAL;
1197
1198 rc = next_entry(buf, fp, sizeof(u32));
1199 if (rc < 0)
1200 return -EINVAL;
1201 t->flags = le32_to_cpu(buf[0]);
1202
1203 return 0;
1204}
1205
1206
1207static int read_cons_helper(struct policydb *p,
1208 struct constraint_node **nodep,
1209 int ncons, int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct constraint_node *c, *lc;
1212 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001213 __le32 buf[3];
1214 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int rc, i, j, depth;
1216
1217 lc = NULL;
1218 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001219 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!c)
1221 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Eric Paris2ced3df2008-04-17 13:37:12 -04001223 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001225 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001229 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return rc;
1231 c->permissions = le32_to_cpu(buf[0]);
1232 nexpr = le32_to_cpu(buf[1]);
1233 le = NULL;
1234 depth = -1;
1235 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001236 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!e)
1238 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Eric Paris2ced3df2008-04-17 13:37:12 -04001240 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001242 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001246 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return rc;
1248 e->expr_type = le32_to_cpu(buf[0]);
1249 e->attr = le32_to_cpu(buf[1]);
1250 e->op = le32_to_cpu(buf[2]);
1251
1252 switch (e->expr_type) {
1253 case CEXPR_NOT:
1254 if (depth < 0)
1255 return -EINVAL;
1256 break;
1257 case CEXPR_AND:
1258 case CEXPR_OR:
1259 if (depth < 1)
1260 return -EINVAL;
1261 depth--;
1262 break;
1263 case CEXPR_ATTR:
1264 if (depth == (CEXPR_MAXDEPTH - 1))
1265 return -EINVAL;
1266 depth++;
1267 break;
1268 case CEXPR_NAMES:
1269 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1270 return -EINVAL;
1271 if (depth == (CEXPR_MAXDEPTH - 1))
1272 return -EINVAL;
1273 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001274 rc = ebitmap_read(&e->names, fp);
1275 if (rc)
1276 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05001277 if (p->policyvers >=
1278 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1279 e->type_names = kzalloc(sizeof
1280 (*e->type_names),
1281 GFP_KERNEL);
1282 if (!e->type_names)
1283 return -ENOMEM;
1284 type_set_init(e->type_names);
1285 rc = type_set_read(e->type_names, fp);
1286 if (rc)
1287 return rc;
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 default:
1291 return -EINVAL;
1292 }
1293 le = e;
1294 }
1295 if (depth != 0)
1296 return -EINVAL;
1297 lc = c;
1298 }
1299
1300 return 0;
1301}
1302
1303static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1304{
1305 char *key = NULL;
1306 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001307 __le32 buf[6];
1308 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int i, rc;
1310
Eric Paris9398c7f2010-11-23 11:40:08 -05001311 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001312 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001313 if (!cladatum)
1314 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001317 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 goto bad;
1319
1320 len = le32_to_cpu(buf[0]);
1321 len2 = le32_to_cpu(buf[1]);
1322 cladatum->value = le32_to_cpu(buf[2]);
1323
1324 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1325 if (rc)
1326 goto bad;
1327 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1328 nel = le32_to_cpu(buf[4]);
1329
1330 ncons = le32_to_cpu(buf[5]);
1331
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001332 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001333 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (len2) {
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001337 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001338 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Eric Paris9398c7f2010-11-23 11:40:08 -05001341 rc = -EINVAL;
1342 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001344 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto bad;
1346 }
1347 }
1348 for (i = 0; i < nel; i++) {
1349 rc = perm_read(p, cladatum->permissions.table, fp);
1350 if (rc)
1351 goto bad;
1352 }
1353
Richard Hainesa660bec2013-11-19 17:34:23 -05001354 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (rc)
1356 goto bad;
1357
1358 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1359 /* grab the validatetrans rules */
1360 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001361 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto bad;
1363 ncons = le32_to_cpu(buf[0]);
Richard Hainesa660bec2013-11-19 17:34:23 -05001364 rc = read_cons_helper(p, &cladatum->validatetrans,
1365 ncons, 1, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (rc)
1367 goto bad;
1368 }
1369
Eric Parisaa893262012-03-20 14:35:12 -04001370 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1371 rc = next_entry(buf, fp, sizeof(u32) * 3);
1372 if (rc)
1373 goto bad;
1374
1375 cladatum->default_user = le32_to_cpu(buf[0]);
1376 cladatum->default_role = le32_to_cpu(buf[1]);
1377 cladatum->default_range = le32_to_cpu(buf[2]);
1378 }
1379
Eric Pariseed77952012-03-20 14:35:12 -04001380 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1381 rc = next_entry(buf, fp, sizeof(u32) * 1);
1382 if (rc)
1383 goto bad;
1384 cladatum->default_type = le32_to_cpu(buf[0]);
1385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 rc = hashtab_insert(h, key, cladatum);
1388 if (rc)
1389 goto bad;
1390
Eric Paris9398c7f2010-11-23 11:40:08 -05001391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001393 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001394 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1398{
1399 char *key = NULL;
1400 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001401 int rc, to_read = 2;
1402 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001403 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Eric Paris9398c7f2010-11-23 11:40:08 -05001405 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001406 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001407 if (!role)
1408 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
KaiGai Koheid9250de2008-08-28 16:35:57 +09001410 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1411 to_read = 3;
1412
1413 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001414 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 goto bad;
1416
1417 len = le32_to_cpu(buf[0]);
1418 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001419 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1420 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001422 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001423 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 rc = ebitmap_read(&role->dominates, fp);
1427 if (rc)
1428 goto bad;
1429
1430 rc = ebitmap_read(&role->types, fp);
1431 if (rc)
1432 goto bad;
1433
1434 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001435 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001437 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto bad;
1440 }
1441 rc = 0;
1442 goto bad;
1443 }
1444
1445 rc = hashtab_insert(h, key, role);
1446 if (rc)
1447 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449bad:
1450 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1455{
1456 char *key = NULL;
1457 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001458 int rc, to_read = 3;
1459 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001460 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Eric Paris9398c7f2010-11-23 11:40:08 -05001462 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001463 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001464 if (!typdatum)
1465 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
KaiGai Koheid9250de2008-08-28 16:35:57 +09001467 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1468 to_read = 4;
1469
1470 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001471 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto bad;
1473
1474 len = le32_to_cpu(buf[0]);
1475 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001476 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1477 u32 prop = le32_to_cpu(buf[2]);
1478
1479 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1480 typdatum->primary = 1;
1481 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1482 typdatum->attribute = 1;
1483
1484 typdatum->bounds = le32_to_cpu(buf[3]);
1485 } else {
1486 typdatum->primary = le32_to_cpu(buf[2]);
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001489 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001490 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 rc = hashtab_insert(h, key, typdatum);
1494 if (rc)
1495 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497bad:
1498 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001499 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502
1503/*
1504 * Read a MLS level structure from a policydb binary
1505 * representation file.
1506 */
1507static int mls_read_level(struct mls_level *lp, void *fp)
1508{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001509 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int rc;
1511
1512 memset(lp, 0, sizeof(*lp));
1513
1514 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001515 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001516 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001517 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 lp->sens = le32_to_cpu(buf[0]);
1520
Eric Paris9398c7f2010-11-23 11:40:08 -05001521 rc = ebitmap_read(&lp->cat, fp);
1522 if (rc) {
1523 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1524 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1530{
1531 char *key = NULL;
1532 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001533 int rc, to_read = 2;
1534 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001535 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric Paris9398c7f2010-11-23 11:40:08 -05001537 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001538 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001539 if (!usrdatum)
1540 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
KaiGai Koheid9250de2008-08-28 16:35:57 +09001542 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1543 to_read = 3;
1544
1545 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001546 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto bad;
1548
1549 len = le32_to_cpu(buf[0]);
1550 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001551 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1552 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001554 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001555 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 rc = ebitmap_read(&usrdatum->roles, fp);
1559 if (rc)
1560 goto bad;
1561
1562 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1563 rc = mls_read_range_helper(&usrdatum->range, fp);
1564 if (rc)
1565 goto bad;
1566 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1567 if (rc)
1568 goto bad;
1569 }
1570
1571 rc = hashtab_insert(h, key, usrdatum);
1572 if (rc)
1573 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575bad:
1576 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001577 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1581{
1582 char *key = NULL;
1583 struct level_datum *levdatum;
1584 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001585 __le32 buf[2];
1586 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Eric Paris9398c7f2010-11-23 11:40:08 -05001588 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001589 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001590 if (!levdatum)
1591 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001594 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto bad;
1596
1597 len = le32_to_cpu(buf[0]);
1598 levdatum->isalias = le32_to_cpu(buf[1]);
1599
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001600 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001601 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Eric Paris9398c7f2010-11-23 11:40:08 -05001604 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001606 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001608
1609 rc = mls_read_level(levdatum->level, fp);
1610 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 rc = hashtab_insert(h, key, levdatum);
1614 if (rc)
1615 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617bad:
1618 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001619 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1623{
1624 char *key = NULL;
1625 struct cat_datum *catdatum;
1626 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001627 __le32 buf[3];
1628 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Eric Paris9398c7f2010-11-23 11:40:08 -05001630 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001631 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001632 if (!catdatum)
1633 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001636 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto bad;
1638
1639 len = le32_to_cpu(buf[0]);
1640 catdatum->value = le32_to_cpu(buf[1]);
1641 catdatum->isalias = le32_to_cpu(buf[2]);
1642
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001643 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001644 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 rc = hashtab_insert(h, key, catdatum);
1648 if (rc)
1649 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651bad:
1652 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001653 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1657{
1658 common_read,
1659 class_read,
1660 role_read,
1661 type_read,
1662 user_read,
1663 cond_read_bool,
1664 sens_read,
1665 cat_read,
1666};
1667
KaiGai Koheid9250de2008-08-28 16:35:57 +09001668static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1669{
1670 struct user_datum *upper, *user;
1671 struct policydb *p = datap;
1672 int depth = 0;
1673
1674 upper = user = datum;
1675 while (upper->bounds) {
1676 struct ebitmap_node *node;
1677 unsigned long bit;
1678
1679 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1680 printk(KERN_ERR "SELinux: user %s: "
1681 "too deep or looped boundary",
1682 (char *) key);
1683 return -EINVAL;
1684 }
1685
1686 upper = p->user_val_to_struct[upper->bounds - 1];
1687 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1688 if (ebitmap_get_bit(&upper->roles, bit))
1689 continue;
1690
1691 printk(KERN_ERR
1692 "SELinux: boundary violated policy: "
1693 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001694 sym_name(p, SYM_USERS, user->value - 1),
1695 sym_name(p, SYM_ROLES, bit),
1696 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001697
1698 return -EINVAL;
1699 }
1700 }
1701
1702 return 0;
1703}
1704
1705static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1706{
1707 struct role_datum *upper, *role;
1708 struct policydb *p = datap;
1709 int depth = 0;
1710
1711 upper = role = datum;
1712 while (upper->bounds) {
1713 struct ebitmap_node *node;
1714 unsigned long bit;
1715
1716 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1717 printk(KERN_ERR "SELinux: role %s: "
1718 "too deep or looped bounds\n",
1719 (char *) key);
1720 return -EINVAL;
1721 }
1722
1723 upper = p->role_val_to_struct[upper->bounds - 1];
1724 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1725 if (ebitmap_get_bit(&upper->types, bit))
1726 continue;
1727
1728 printk(KERN_ERR
1729 "SELinux: boundary violated policy: "
1730 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001731 sym_name(p, SYM_ROLES, role->value - 1),
1732 sym_name(p, SYM_TYPES, bit),
1733 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001734
1735 return -EINVAL;
1736 }
1737 }
1738
1739 return 0;
1740}
1741
1742static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1743{
Eric Parisdaa6d832010-08-03 15:26:05 -04001744 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001745 struct policydb *p = datap;
1746 int depth = 0;
1747
Eric Parisdaa6d832010-08-03 15:26:05 -04001748 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001749 while (upper->bounds) {
1750 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1751 printk(KERN_ERR "SELinux: type %s: "
1752 "too deep or looped boundary\n",
1753 (char *) key);
1754 return -EINVAL;
1755 }
1756
Eric Paris23bdecb2010-11-29 15:47:09 -05001757 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1758 upper->bounds - 1);
1759 BUG_ON(!upper);
1760
KaiGai Koheid9250de2008-08-28 16:35:57 +09001761 if (upper->attribute) {
1762 printk(KERN_ERR "SELinux: type %s: "
1763 "bounded by attribute %s",
1764 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001765 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001766 return -EINVAL;
1767 }
1768 }
1769
1770 return 0;
1771}
1772
1773static int policydb_bounds_sanity_check(struct policydb *p)
1774{
1775 int rc;
1776
1777 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1778 return 0;
1779
1780 rc = hashtab_map(p->p_users.table,
1781 user_bounds_sanity_check, p);
1782 if (rc)
1783 return rc;
1784
1785 rc = hashtab_map(p->p_roles.table,
1786 role_bounds_sanity_check, p);
1787 if (rc)
1788 return rc;
1789
1790 rc = hashtab_map(p->p_types.table,
1791 type_bounds_sanity_check, p);
1792 if (rc)
1793 return rc;
1794
1795 return 0;
1796}
1797
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001798u16 string_to_security_class(struct policydb *p, const char *name)
1799{
1800 struct class_datum *cladatum;
1801
1802 cladatum = hashtab_search(p->p_classes.table, name);
1803 if (!cladatum)
1804 return 0;
1805
1806 return cladatum->value;
1807}
1808
1809u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1810{
1811 struct class_datum *cladatum;
1812 struct perm_datum *perdatum = NULL;
1813 struct common_datum *comdatum;
1814
1815 if (!tclass || tclass > p->p_classes.nprim)
1816 return 0;
1817
1818 cladatum = p->class_val_to_struct[tclass-1];
1819 comdatum = cladatum->comdatum;
1820 if (comdatum)
1821 perdatum = hashtab_search(comdatum->permissions.table,
1822 name);
1823 if (!perdatum)
1824 perdatum = hashtab_search(cladatum->permissions.table,
1825 name);
1826 if (!perdatum)
1827 return 0;
1828
1829 return 1U << (perdatum->value-1);
1830}
1831
Eric Paris9ee0c822010-06-11 12:37:05 -04001832static int range_read(struct policydb *p, void *fp)
1833{
1834 struct range_trans *rt = NULL;
1835 struct mls_range *r = NULL;
1836 int i, rc;
1837 __le32 buf[2];
1838 u32 nel;
1839
1840 if (p->policyvers < POLICYDB_VERSION_MLS)
1841 return 0;
1842
1843 rc = next_entry(buf, fp, sizeof(u32));
1844 if (rc)
1845 goto out;
1846
1847 nel = le32_to_cpu(buf[0]);
1848 for (i = 0; i < nel; i++) {
1849 rc = -ENOMEM;
1850 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1851 if (!rt)
1852 goto out;
1853
1854 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1855 if (rc)
1856 goto out;
1857
1858 rt->source_type = le32_to_cpu(buf[0]);
1859 rt->target_type = le32_to_cpu(buf[1]);
1860 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1861 rc = next_entry(buf, fp, sizeof(u32));
1862 if (rc)
1863 goto out;
1864 rt->target_class = le32_to_cpu(buf[0]);
1865 } else
1866 rt->target_class = p->process_class;
1867
1868 rc = -EINVAL;
1869 if (!policydb_type_isvalid(p, rt->source_type) ||
1870 !policydb_type_isvalid(p, rt->target_type) ||
1871 !policydb_class_isvalid(p, rt->target_class))
1872 goto out;
1873
1874 rc = -ENOMEM;
1875 r = kzalloc(sizeof(*r), GFP_KERNEL);
1876 if (!r)
1877 goto out;
1878
1879 rc = mls_read_range_helper(r, fp);
1880 if (rc)
1881 goto out;
1882
1883 rc = -EINVAL;
1884 if (!mls_range_isvalid(p, r)) {
1885 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1886 goto out;
1887 }
1888
1889 rc = hashtab_insert(p->range_tr, rt, r);
1890 if (rc)
1891 goto out;
1892
1893 rt = NULL;
1894 r = NULL;
1895 }
Eric Parisbe30b162011-04-28 15:11:21 -04001896 hash_eval(p->range_tr, "rangetr");
Eric Paris9ee0c822010-06-11 12:37:05 -04001897 rc = 0;
1898out:
1899 kfree(rt);
1900 kfree(r);
1901 return rc;
1902}
1903
Eric Paris652bb9b2011-02-01 11:05:40 -05001904static int filename_trans_read(struct policydb *p, void *fp)
1905{
Eric Paris2463c26d2011-04-28 15:11:21 -04001906 struct filename_trans *ft;
1907 struct filename_trans_datum *otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001908 char *name;
Eric Paris2463c26d2011-04-28 15:11:21 -04001909 u32 nel, len;
Eric Paris652bb9b2011-02-01 11:05:40 -05001910 __le32 buf[4];
1911 int rc, i;
1912
1913 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1914 return 0;
1915
1916 rc = next_entry(buf, fp, sizeof(u32));
1917 if (rc)
Eric Paris2463c26d2011-04-28 15:11:21 -04001918 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05001919 nel = le32_to_cpu(buf[0]);
1920
Eric Paris652bb9b2011-02-01 11:05:40 -05001921 for (i = 0; i < nel; i++) {
Eric Paris2463c26d2011-04-28 15:11:21 -04001922 ft = NULL;
1923 otype = NULL;
1924 name = NULL;
1925
Eric Paris652bb9b2011-02-01 11:05:40 -05001926 rc = -ENOMEM;
1927 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1928 if (!ft)
1929 goto out;
1930
Eric Paris2463c26d2011-04-28 15:11:21 -04001931 rc = -ENOMEM;
1932 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1933 if (!otype)
1934 goto out;
Eric Paris652bb9b2011-02-01 11:05:40 -05001935
1936 /* length of the path component string */
1937 rc = next_entry(buf, fp, sizeof(u32));
1938 if (rc)
1939 goto out;
1940 len = le32_to_cpu(buf[0]);
1941
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001942 /* path component string */
1943 rc = str_read(&name, GFP_KERNEL, fp, len);
1944 if (rc)
Eric Paris652bb9b2011-02-01 11:05:40 -05001945 goto out;
1946
1947 ft->name = name;
1948
Eric Paris652bb9b2011-02-01 11:05:40 -05001949 rc = next_entry(buf, fp, sizeof(u32) * 4);
1950 if (rc)
1951 goto out;
1952
1953 ft->stype = le32_to_cpu(buf[0]);
1954 ft->ttype = le32_to_cpu(buf[1]);
1955 ft->tclass = le32_to_cpu(buf[2]);
Eric Paris2463c26d2011-04-28 15:11:21 -04001956
1957 otype->otype = le32_to_cpu(buf[3]);
Eric Paris03a4c012011-04-28 15:11:21 -04001958
1959 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1960 if (rc)
1961 goto out;
Eric Paris2463c26d2011-04-28 15:11:21 -04001962
Tetsuo Handa8ed81462014-01-06 21:28:15 +09001963 rc = hashtab_insert(p->filename_trans, ft, otype);
1964 if (rc) {
1965 /*
1966 * Do not return -EEXIST to the caller, or the system
1967 * will not boot.
1968 */
1969 if (rc != -EEXIST)
1970 goto out;
1971 /* But free memory to avoid memory leak. */
1972 kfree(ft);
1973 kfree(name);
1974 kfree(otype);
1975 }
Eric Paris652bb9b2011-02-01 11:05:40 -05001976 }
Eric Paris2463c26d2011-04-28 15:11:21 -04001977 hash_eval(p->filename_trans, "filenametr");
1978 return 0;
Eric Paris652bb9b2011-02-01 11:05:40 -05001979out:
Eric Paris2463c26d2011-04-28 15:11:21 -04001980 kfree(ft);
1981 kfree(name);
1982 kfree(otype);
1983
Eric Paris652bb9b2011-02-01 11:05:40 -05001984 return rc;
1985}
1986
Eric Parisd1b43542010-07-21 12:50:57 -04001987static int genfs_read(struct policydb *p, void *fp)
1988{
1989 int i, j, rc;
1990 u32 nel, nel2, len, len2;
1991 __le32 buf[1];
1992 struct ocontext *l, *c;
1993 struct ocontext *newc = NULL;
1994 struct genfs *genfs_p, *genfs;
1995 struct genfs *newgenfs = NULL;
1996
1997 rc = next_entry(buf, fp, sizeof(u32));
1998 if (rc)
1999 goto out;
2000 nel = le32_to_cpu(buf[0]);
2001
2002 for (i = 0; i < nel; i++) {
2003 rc = next_entry(buf, fp, sizeof(u32));
2004 if (rc)
2005 goto out;
2006 len = le32_to_cpu(buf[0]);
2007
2008 rc = -ENOMEM;
2009 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2010 if (!newgenfs)
2011 goto out;
2012
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002013 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002014 if (rc)
2015 goto out;
2016
Eric Parisd1b43542010-07-21 12:50:57 -04002017 for (genfs_p = NULL, genfs = p->genfs; genfs;
2018 genfs_p = genfs, genfs = genfs->next) {
2019 rc = -EINVAL;
2020 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2021 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2022 newgenfs->fstype);
2023 goto out;
2024 }
2025 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2026 break;
2027 }
2028 newgenfs->next = genfs;
2029 if (genfs_p)
2030 genfs_p->next = newgenfs;
2031 else
2032 p->genfs = newgenfs;
2033 genfs = newgenfs;
2034 newgenfs = NULL;
2035
2036 rc = next_entry(buf, fp, sizeof(u32));
2037 if (rc)
2038 goto out;
2039
2040 nel2 = le32_to_cpu(buf[0]);
2041 for (j = 0; j < nel2; j++) {
2042 rc = next_entry(buf, fp, sizeof(u32));
2043 if (rc)
2044 goto out;
2045 len = le32_to_cpu(buf[0]);
2046
2047 rc = -ENOMEM;
2048 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2049 if (!newc)
2050 goto out;
2051
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002052 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002053 if (rc)
2054 goto out;
Eric Parisd1b43542010-07-21 12:50:57 -04002055
2056 rc = next_entry(buf, fp, sizeof(u32));
2057 if (rc)
2058 goto out;
2059
2060 newc->v.sclass = le32_to_cpu(buf[0]);
2061 rc = context_read_and_validate(&newc->context[0], p, fp);
2062 if (rc)
2063 goto out;
2064
2065 for (l = NULL, c = genfs->head; c;
2066 l = c, c = c->next) {
2067 rc = -EINVAL;
2068 if (!strcmp(newc->u.name, c->u.name) &&
2069 (!c->v.sclass || !newc->v.sclass ||
2070 newc->v.sclass == c->v.sclass)) {
2071 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2072 genfs->fstype, c->u.name);
2073 goto out;
2074 }
2075 len = strlen(newc->u.name);
2076 len2 = strlen(c->u.name);
2077 if (len > len2)
2078 break;
2079 }
2080
2081 newc->next = c;
2082 if (l)
2083 l->next = newc;
2084 else
2085 genfs->head = newc;
2086 newc = NULL;
2087 }
2088 }
2089 rc = 0;
2090out:
2091 if (newgenfs)
2092 kfree(newgenfs->fstype);
2093 kfree(newgenfs);
2094 ocontext_destroy(newc, OCON_FSUSE);
2095
2096 return rc;
2097}
2098
Eric Paris692a8a22010-07-21 12:51:03 -04002099static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2100 void *fp)
2101{
2102 int i, j, rc;
2103 u32 nel, len;
2104 __le32 buf[3];
2105 struct ocontext *l, *c;
2106 u32 nodebuf[8];
2107
2108 for (i = 0; i < info->ocon_num; i++) {
2109 rc = next_entry(buf, fp, sizeof(u32));
2110 if (rc)
2111 goto out;
2112 nel = le32_to_cpu(buf[0]);
2113
2114 l = NULL;
2115 for (j = 0; j < nel; j++) {
2116 rc = -ENOMEM;
2117 c = kzalloc(sizeof(*c), GFP_KERNEL);
2118 if (!c)
2119 goto out;
2120 if (l)
2121 l->next = c;
2122 else
2123 p->ocontexts[i] = c;
2124 l = c;
2125
2126 switch (i) {
2127 case OCON_ISID:
2128 rc = next_entry(buf, fp, sizeof(u32));
2129 if (rc)
2130 goto out;
2131
2132 c->sid[0] = le32_to_cpu(buf[0]);
2133 rc = context_read_and_validate(&c->context[0], p, fp);
2134 if (rc)
2135 goto out;
2136 break;
2137 case OCON_FS:
2138 case OCON_NETIF:
2139 rc = next_entry(buf, fp, sizeof(u32));
2140 if (rc)
2141 goto out;
2142 len = le32_to_cpu(buf[0]);
2143
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002144 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002145 if (rc)
2146 goto out;
2147
Eric Paris692a8a22010-07-21 12:51:03 -04002148 rc = context_read_and_validate(&c->context[0], p, fp);
2149 if (rc)
2150 goto out;
2151 rc = context_read_and_validate(&c->context[1], p, fp);
2152 if (rc)
2153 goto out;
2154 break;
2155 case OCON_PORT:
2156 rc = next_entry(buf, fp, sizeof(u32)*3);
2157 if (rc)
2158 goto out;
2159 c->u.port.protocol = le32_to_cpu(buf[0]);
2160 c->u.port.low_port = le32_to_cpu(buf[1]);
2161 c->u.port.high_port = le32_to_cpu(buf[2]);
2162 rc = context_read_and_validate(&c->context[0], p, fp);
2163 if (rc)
2164 goto out;
2165 break;
2166 case OCON_NODE:
2167 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2168 if (rc)
2169 goto out;
2170 c->u.node.addr = nodebuf[0]; /* network order */
2171 c->u.node.mask = nodebuf[1]; /* network order */
2172 rc = context_read_and_validate(&c->context[0], p, fp);
2173 if (rc)
2174 goto out;
2175 break;
2176 case OCON_FSUSE:
2177 rc = next_entry(buf, fp, sizeof(u32)*2);
2178 if (rc)
2179 goto out;
2180
2181 rc = -EINVAL;
2182 c->v.behavior = le32_to_cpu(buf[0]);
David Quigleyeb9ae682013-05-22 12:50:37 -04002183 /* Determined at runtime, not in policy DB. */
2184 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2185 goto out;
2186 if (c->v.behavior > SECURITY_FS_USE_MAX)
Eric Paris692a8a22010-07-21 12:51:03 -04002187 goto out;
2188
Eric Paris692a8a22010-07-21 12:51:03 -04002189 len = le32_to_cpu(buf[1]);
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002190 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002191 if (rc)
2192 goto out;
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002193
Eric Paris692a8a22010-07-21 12:51:03 -04002194 rc = context_read_and_validate(&c->context[0], p, fp);
2195 if (rc)
2196 goto out;
2197 break;
2198 case OCON_NODE6: {
2199 int k;
2200
2201 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2202 if (rc)
2203 goto out;
2204 for (k = 0; k < 4; k++)
2205 c->u.node6.addr[k] = nodebuf[k];
2206 for (k = 0; k < 4; k++)
2207 c->u.node6.mask[k] = nodebuf[k+4];
2208 rc = context_read_and_validate(&c->context[0], p, fp);
2209 if (rc)
2210 goto out;
2211 break;
2212 }
2213 }
2214 }
2215 }
2216 rc = 0;
2217out:
2218 return rc;
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/*
2222 * Read the configuration data from a policy database binary
2223 * representation file into a policy database structure.
2224 */
2225int policydb_read(struct policydb *p, void *fp)
2226{
2227 struct role_allow *ra, *lra;
2228 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002230 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002231 u32 len, nprim, nel;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 char *policydb_str;
2234 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 rc = policydb_init(p);
2237 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002238 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002241 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002242 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto bad;
2244
Eric Paris9398c7f2010-11-23 11:40:08 -05002245 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002246 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002247 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002249 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto bad;
2251 }
2252
Eric Paris9398c7f2010-11-23 11:40:08 -05002253 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002254 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002256 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 "match expected length %Zu\n",
2258 len, strlen(POLICYDB_STRING));
2259 goto bad;
2260 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002261
2262 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002263 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002265 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto bad;
2268 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002271 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002272 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 kfree(policydb_str);
2274 goto bad;
2275 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002276
2277 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002278 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002280 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 "my string %s\n", policydb_str, POLICYDB_STRING);
2282 kfree(policydb_str);
2283 goto bad;
2284 }
2285 /* Done with policydb_str. */
2286 kfree(policydb_str);
2287 policydb_str = NULL;
2288
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002289 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002291 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Eric Paris9398c7f2010-11-23 11:40:08 -05002294 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002295 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (p->policyvers < POLICYDB_VERSION_MIN ||
2297 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002298 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002299 "my version range %d-%d\n",
2300 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2301 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002304 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002305 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Eric Paris9398c7f2010-11-23 11:40:08 -05002307 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002309 printk(KERN_ERR "SELinux: security policydb version %d "
2310 "(MLS) not backwards compatible\n",
2311 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto bad;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Eric Paris3f120702007-09-21 14:37:10 -04002315 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2316 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Eric Paris9398c7f2010-11-23 11:40:08 -05002318 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2319 rc = ebitmap_read(&p->policycaps, fp);
2320 if (rc)
2321 goto bad;
2322 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002323
Eric Paris9398c7f2010-11-23 11:40:08 -05002324 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2325 rc = ebitmap_read(&p->permissive_map, fp);
2326 if (rc)
2327 goto bad;
2328 }
Eric Paris64dbf072008-03-31 12:17:33 +11002329
Eric Paris9398c7f2010-11-23 11:40:08 -05002330 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 info = policydb_lookup_compat(p->policyvers);
2332 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002333 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 "for version %d\n", p->policyvers);
2335 goto bad;
2336 }
2337
Eric Paris9398c7f2010-11-23 11:40:08 -05002338 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002339 if (le32_to_cpu(buf[2]) != info->sym_num ||
2340 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002341 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002342 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2343 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 info->sym_num, info->ocon_num);
2345 goto bad;
2346 }
2347
2348 for (i = 0; i < info->sym_num; i++) {
2349 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002350 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 goto bad;
2352 nprim = le32_to_cpu(buf[0]);
2353 nel = le32_to_cpu(buf[1]);
2354 for (j = 0; j < nel; j++) {
2355 rc = read_f[i](p, p->symtab[i].table, fp);
2356 if (rc)
2357 goto bad;
2358 }
2359
2360 p->symtab[i].nprim = nprim;
2361 }
2362
Harry Ciao1214eac2011-04-07 14:12:57 +08002363 rc = -EINVAL;
2364 p->process_class = string_to_security_class(p, "process");
2365 if (!p->process_class)
2366 goto bad;
2367
Stephen Smalley45e54212007-11-07 10:08:00 -05002368 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (rc)
2370 goto bad;
2371
2372 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2373 rc = cond_read_list(p, fp);
2374 if (rc)
2375 goto bad;
2376 }
2377
2378 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002379 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto bad;
2381 nel = le32_to_cpu(buf[0]);
2382 ltr = NULL;
2383 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002384 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002385 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002386 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002388 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002390 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002393 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002395
2396 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 tr->role = le32_to_cpu(buf[0]);
2398 tr->type = le32_to_cpu(buf[1]);
2399 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002400 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2401 rc = next_entry(buf, fp, sizeof(u32));
2402 if (rc)
2403 goto bad;
2404 tr->tclass = le32_to_cpu(buf[0]);
2405 } else
2406 tr->tclass = p->process_class;
2407
Stephen Smalley45e54212007-11-07 10:08:00 -05002408 if (!policydb_role_isvalid(p, tr->role) ||
2409 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002410 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002411 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002412 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 ltr = tr;
2414 }
2415
2416 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002417 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 goto bad;
2419 nel = le32_to_cpu(buf[0]);
2420 lra = NULL;
2421 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002422 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002423 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002424 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002426 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002428 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002431 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002433
2434 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 ra->role = le32_to_cpu(buf[0]);
2436 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002437 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002438 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002439 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 lra = ra;
2441 }
2442
Eric Paris652bb9b2011-02-01 11:05:40 -05002443 rc = filename_trans_read(p, fp);
2444 if (rc)
2445 goto bad;
2446
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002447 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (rc)
2449 goto bad;
2450
Eric Paris9398c7f2010-11-23 11:40:08 -05002451 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002452 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2453 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002454 if (!p->process_trans_perms)
2455 goto bad;
2456
Eric Paris692a8a22010-07-21 12:51:03 -04002457 rc = ocontext_read(p, info, fp);
2458 if (rc)
2459 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Eric Parisd1b43542010-07-21 12:50:57 -04002461 rc = genfs_read(p, fp);
2462 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Eric Paris9ee0c822010-06-11 12:37:05 -04002465 rc = range_read(p, fp);
2466 if (rc)
2467 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric Paris6371dcd2010-07-29 23:02:34 -04002469 rc = -ENOMEM;
2470 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2471 p->p_types.nprim,
2472 GFP_KERNEL | __GFP_ZERO);
2473 if (!p->type_attr_map_array)
2474 goto bad;
2475
2476 /* preallocate so we don't have to worry about the put ever failing */
Eric Paris5a3ea872011-04-28 15:55:52 -04002477 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
Eric Paris6371dcd2010-07-29 23:02:34 -04002478 GFP_KERNEL | __GFP_ZERO);
2479 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002480 goto bad;
2481
2482 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002483 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2484
2485 BUG_ON(!e);
2486 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002487 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002488 rc = ebitmap_read(e, fp);
2489 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002490 goto bad;
2491 }
2492 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002493 rc = ebitmap_set_bit(e, i, 1);
2494 if (rc)
2495 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002496 }
2497
KaiGai Koheid9250de2008-08-28 16:35:57 +09002498 rc = policydb_bounds_sanity_check(p);
2499 if (rc)
2500 goto bad;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 rc = 0;
2503out:
2504 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 policydb_destroy(p);
2507 goto out;
2508}
Eric Pariscee74f42010-10-13 17:50:25 -04002509
2510/*
2511 * Write a MLS level structure to a policydb binary
2512 * representation file.
2513 */
2514static int mls_write_level(struct mls_level *l, void *fp)
2515{
2516 __le32 buf[1];
2517 int rc;
2518
2519 buf[0] = cpu_to_le32(l->sens);
2520 rc = put_entry(buf, sizeof(u32), 1, fp);
2521 if (rc)
2522 return rc;
2523
2524 rc = ebitmap_write(&l->cat, fp);
2525 if (rc)
2526 return rc;
2527
2528 return 0;
2529}
2530
2531/*
2532 * Write a MLS range structure to a policydb binary
2533 * representation file.
2534 */
2535static int mls_write_range_helper(struct mls_range *r, void *fp)
2536{
2537 __le32 buf[3];
2538 size_t items;
2539 int rc, eq;
2540
2541 eq = mls_level_eq(&r->level[1], &r->level[0]);
2542
2543 if (eq)
2544 items = 2;
2545 else
2546 items = 3;
2547 buf[0] = cpu_to_le32(items-1);
2548 buf[1] = cpu_to_le32(r->level[0].sens);
2549 if (!eq)
2550 buf[2] = cpu_to_le32(r->level[1].sens);
2551
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302552 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002553
2554 rc = put_entry(buf, sizeof(u32), items, fp);
2555 if (rc)
2556 return rc;
2557
2558 rc = ebitmap_write(&r->level[0].cat, fp);
2559 if (rc)
2560 return rc;
2561 if (!eq) {
2562 rc = ebitmap_write(&r->level[1].cat, fp);
2563 if (rc)
2564 return rc;
2565 }
2566
2567 return 0;
2568}
2569
2570static int sens_write(void *vkey, void *datum, void *ptr)
2571{
2572 char *key = vkey;
2573 struct level_datum *levdatum = datum;
2574 struct policy_data *pd = ptr;
2575 void *fp = pd->fp;
2576 __le32 buf[2];
2577 size_t len;
2578 int rc;
2579
2580 len = strlen(key);
2581 buf[0] = cpu_to_le32(len);
2582 buf[1] = cpu_to_le32(levdatum->isalias);
2583 rc = put_entry(buf, sizeof(u32), 2, fp);
2584 if (rc)
2585 return rc;
2586
2587 rc = put_entry(key, 1, len, fp);
2588 if (rc)
2589 return rc;
2590
2591 rc = mls_write_level(levdatum->level, fp);
2592 if (rc)
2593 return rc;
2594
2595 return 0;
2596}
2597
2598static int cat_write(void *vkey, void *datum, void *ptr)
2599{
2600 char *key = vkey;
2601 struct cat_datum *catdatum = datum;
2602 struct policy_data *pd = ptr;
2603 void *fp = pd->fp;
2604 __le32 buf[3];
2605 size_t len;
2606 int rc;
2607
2608 len = strlen(key);
2609 buf[0] = cpu_to_le32(len);
2610 buf[1] = cpu_to_le32(catdatum->value);
2611 buf[2] = cpu_to_le32(catdatum->isalias);
2612 rc = put_entry(buf, sizeof(u32), 3, fp);
2613 if (rc)
2614 return rc;
2615
2616 rc = put_entry(key, 1, len, fp);
2617 if (rc)
2618 return rc;
2619
2620 return 0;
2621}
2622
Harry Ciaoc900ff32011-03-25 13:52:00 +08002623static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002624{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002625 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002626 struct role_trans *tr;
2627 u32 buf[3];
2628 size_t nel;
2629 int rc;
2630
2631 nel = 0;
2632 for (tr = r; tr; tr = tr->next)
2633 nel++;
2634 buf[0] = cpu_to_le32(nel);
2635 rc = put_entry(buf, sizeof(u32), 1, fp);
2636 if (rc)
2637 return rc;
2638 for (tr = r; tr; tr = tr->next) {
2639 buf[0] = cpu_to_le32(tr->role);
2640 buf[1] = cpu_to_le32(tr->type);
2641 buf[2] = cpu_to_le32(tr->new_role);
2642 rc = put_entry(buf, sizeof(u32), 3, fp);
2643 if (rc)
2644 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002645 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2646 buf[0] = cpu_to_le32(tr->tclass);
2647 rc = put_entry(buf, sizeof(u32), 1, fp);
2648 if (rc)
2649 return rc;
2650 }
Eric Pariscee74f42010-10-13 17:50:25 -04002651 }
2652
2653 return 0;
2654}
2655
2656static int role_allow_write(struct role_allow *r, void *fp)
2657{
2658 struct role_allow *ra;
2659 u32 buf[2];
2660 size_t nel;
2661 int rc;
2662
2663 nel = 0;
2664 for (ra = r; ra; ra = ra->next)
2665 nel++;
2666 buf[0] = cpu_to_le32(nel);
2667 rc = put_entry(buf, sizeof(u32), 1, fp);
2668 if (rc)
2669 return rc;
2670 for (ra = r; ra; ra = ra->next) {
2671 buf[0] = cpu_to_le32(ra->role);
2672 buf[1] = cpu_to_le32(ra->new_role);
2673 rc = put_entry(buf, sizeof(u32), 2, fp);
2674 if (rc)
2675 return rc;
2676 }
2677 return 0;
2678}
2679
2680/*
2681 * Write a security context structure
2682 * to a policydb binary representation file.
2683 */
2684static int context_write(struct policydb *p, struct context *c,
2685 void *fp)
2686{
2687 int rc;
2688 __le32 buf[3];
2689
2690 buf[0] = cpu_to_le32(c->user);
2691 buf[1] = cpu_to_le32(c->role);
2692 buf[2] = cpu_to_le32(c->type);
2693
2694 rc = put_entry(buf, sizeof(u32), 3, fp);
2695 if (rc)
2696 return rc;
2697
2698 rc = mls_write_range_helper(&c->range, fp);
2699 if (rc)
2700 return rc;
2701
2702 return 0;
2703}
2704
2705/*
2706 * The following *_write functions are used to
2707 * write the symbol data to a policy database
2708 * binary representation file.
2709 */
2710
2711static int perm_write(void *vkey, void *datum, void *fp)
2712{
2713 char *key = vkey;
2714 struct perm_datum *perdatum = datum;
2715 __le32 buf[2];
2716 size_t len;
2717 int rc;
2718
2719 len = strlen(key);
2720 buf[0] = cpu_to_le32(len);
2721 buf[1] = cpu_to_le32(perdatum->value);
2722 rc = put_entry(buf, sizeof(u32), 2, fp);
2723 if (rc)
2724 return rc;
2725
2726 rc = put_entry(key, 1, len, fp);
2727 if (rc)
2728 return rc;
2729
2730 return 0;
2731}
2732
2733static int common_write(void *vkey, void *datum, void *ptr)
2734{
2735 char *key = vkey;
2736 struct common_datum *comdatum = datum;
2737 struct policy_data *pd = ptr;
2738 void *fp = pd->fp;
2739 __le32 buf[4];
2740 size_t len;
2741 int rc;
2742
2743 len = strlen(key);
2744 buf[0] = cpu_to_le32(len);
2745 buf[1] = cpu_to_le32(comdatum->value);
2746 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2747 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2748 rc = put_entry(buf, sizeof(u32), 4, fp);
2749 if (rc)
2750 return rc;
2751
2752 rc = put_entry(key, 1, len, fp);
2753 if (rc)
2754 return rc;
2755
2756 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2757 if (rc)
2758 return rc;
2759
2760 return 0;
2761}
2762
Richard Hainesa660bec2013-11-19 17:34:23 -05002763static int type_set_write(struct type_set *t, void *fp)
2764{
2765 int rc;
2766 __le32 buf[1];
2767
2768 if (ebitmap_write(&t->types, fp))
2769 return -EINVAL;
2770 if (ebitmap_write(&t->negset, fp))
2771 return -EINVAL;
2772
2773 buf[0] = cpu_to_le32(t->flags);
2774 rc = put_entry(buf, sizeof(u32), 1, fp);
2775 if (rc)
2776 return -EINVAL;
2777
2778 return 0;
2779}
2780
Eric Pariscee74f42010-10-13 17:50:25 -04002781static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2782 void *fp)
2783{
2784 struct constraint_node *c;
2785 struct constraint_expr *e;
2786 __le32 buf[3];
2787 u32 nel;
2788 int rc;
2789
2790 for (c = node; c; c = c->next) {
2791 nel = 0;
2792 for (e = c->expr; e; e = e->next)
2793 nel++;
2794 buf[0] = cpu_to_le32(c->permissions);
2795 buf[1] = cpu_to_le32(nel);
2796 rc = put_entry(buf, sizeof(u32), 2, fp);
2797 if (rc)
2798 return rc;
2799 for (e = c->expr; e; e = e->next) {
2800 buf[0] = cpu_to_le32(e->expr_type);
2801 buf[1] = cpu_to_le32(e->attr);
2802 buf[2] = cpu_to_le32(e->op);
2803 rc = put_entry(buf, sizeof(u32), 3, fp);
2804 if (rc)
2805 return rc;
2806
2807 switch (e->expr_type) {
2808 case CEXPR_NAMES:
2809 rc = ebitmap_write(&e->names, fp);
2810 if (rc)
2811 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05002812 if (p->policyvers >=
2813 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2814 rc = type_set_write(e->type_names, fp);
2815 if (rc)
2816 return rc;
2817 }
Eric Pariscee74f42010-10-13 17:50:25 -04002818 break;
2819 default:
2820 break;
2821 }
2822 }
2823 }
2824
2825 return 0;
2826}
2827
2828static int class_write(void *vkey, void *datum, void *ptr)
2829{
2830 char *key = vkey;
2831 struct class_datum *cladatum = datum;
2832 struct policy_data *pd = ptr;
2833 void *fp = pd->fp;
2834 struct policydb *p = pd->p;
2835 struct constraint_node *c;
2836 __le32 buf[6];
2837 u32 ncons;
2838 size_t len, len2;
2839 int rc;
2840
2841 len = strlen(key);
2842 if (cladatum->comkey)
2843 len2 = strlen(cladatum->comkey);
2844 else
2845 len2 = 0;
2846
2847 ncons = 0;
2848 for (c = cladatum->constraints; c; c = c->next)
2849 ncons++;
2850
2851 buf[0] = cpu_to_le32(len);
2852 buf[1] = cpu_to_le32(len2);
2853 buf[2] = cpu_to_le32(cladatum->value);
2854 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2855 if (cladatum->permissions.table)
2856 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2857 else
2858 buf[4] = 0;
2859 buf[5] = cpu_to_le32(ncons);
2860 rc = put_entry(buf, sizeof(u32), 6, fp);
2861 if (rc)
2862 return rc;
2863
2864 rc = put_entry(key, 1, len, fp);
2865 if (rc)
2866 return rc;
2867
2868 if (cladatum->comkey) {
2869 rc = put_entry(cladatum->comkey, 1, len2, fp);
2870 if (rc)
2871 return rc;
2872 }
2873
2874 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2875 if (rc)
2876 return rc;
2877
2878 rc = write_cons_helper(p, cladatum->constraints, fp);
2879 if (rc)
2880 return rc;
2881
2882 /* write out the validatetrans rule */
2883 ncons = 0;
2884 for (c = cladatum->validatetrans; c; c = c->next)
2885 ncons++;
2886
2887 buf[0] = cpu_to_le32(ncons);
2888 rc = put_entry(buf, sizeof(u32), 1, fp);
2889 if (rc)
2890 return rc;
2891
2892 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2893 if (rc)
2894 return rc;
2895
Eric Parisaa893262012-03-20 14:35:12 -04002896 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2897 buf[0] = cpu_to_le32(cladatum->default_user);
2898 buf[1] = cpu_to_le32(cladatum->default_role);
2899 buf[2] = cpu_to_le32(cladatum->default_range);
2900
2901 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2902 if (rc)
2903 return rc;
2904 }
2905
Eric Pariseed77952012-03-20 14:35:12 -04002906 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2907 buf[0] = cpu_to_le32(cladatum->default_type);
2908 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2909 if (rc)
2910 return rc;
2911 }
2912
Eric Pariscee74f42010-10-13 17:50:25 -04002913 return 0;
2914}
2915
2916static int role_write(void *vkey, void *datum, void *ptr)
2917{
2918 char *key = vkey;
2919 struct role_datum *role = datum;
2920 struct policy_data *pd = ptr;
2921 void *fp = pd->fp;
2922 struct policydb *p = pd->p;
2923 __le32 buf[3];
2924 size_t items, len;
2925 int rc;
2926
2927 len = strlen(key);
2928 items = 0;
2929 buf[items++] = cpu_to_le32(len);
2930 buf[items++] = cpu_to_le32(role->value);
2931 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2932 buf[items++] = cpu_to_le32(role->bounds);
2933
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302934 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002935
2936 rc = put_entry(buf, sizeof(u32), items, fp);
2937 if (rc)
2938 return rc;
2939
2940 rc = put_entry(key, 1, len, fp);
2941 if (rc)
2942 return rc;
2943
2944 rc = ebitmap_write(&role->dominates, fp);
2945 if (rc)
2946 return rc;
2947
2948 rc = ebitmap_write(&role->types, fp);
2949 if (rc)
2950 return rc;
2951
2952 return 0;
2953}
2954
2955static int type_write(void *vkey, void *datum, void *ptr)
2956{
2957 char *key = vkey;
2958 struct type_datum *typdatum = datum;
2959 struct policy_data *pd = ptr;
2960 struct policydb *p = pd->p;
2961 void *fp = pd->fp;
2962 __le32 buf[4];
2963 int rc;
2964 size_t items, len;
2965
2966 len = strlen(key);
2967 items = 0;
2968 buf[items++] = cpu_to_le32(len);
2969 buf[items++] = cpu_to_le32(typdatum->value);
2970 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2971 u32 properties = 0;
2972
2973 if (typdatum->primary)
2974 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2975
2976 if (typdatum->attribute)
2977 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2978
2979 buf[items++] = cpu_to_le32(properties);
2980 buf[items++] = cpu_to_le32(typdatum->bounds);
2981 } else {
2982 buf[items++] = cpu_to_le32(typdatum->primary);
2983 }
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302984 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002985 rc = put_entry(buf, sizeof(u32), items, fp);
2986 if (rc)
2987 return rc;
2988
2989 rc = put_entry(key, 1, len, fp);
2990 if (rc)
2991 return rc;
2992
2993 return 0;
2994}
2995
2996static int user_write(void *vkey, void *datum, void *ptr)
2997{
2998 char *key = vkey;
2999 struct user_datum *usrdatum = datum;
3000 struct policy_data *pd = ptr;
3001 struct policydb *p = pd->p;
3002 void *fp = pd->fp;
3003 __le32 buf[3];
3004 size_t items, len;
3005 int rc;
3006
3007 len = strlen(key);
3008 items = 0;
3009 buf[items++] = cpu_to_le32(len);
3010 buf[items++] = cpu_to_le32(usrdatum->value);
3011 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3012 buf[items++] = cpu_to_le32(usrdatum->bounds);
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303013 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003014 rc = put_entry(buf, sizeof(u32), items, fp);
3015 if (rc)
3016 return rc;
3017
3018 rc = put_entry(key, 1, len, fp);
3019 if (rc)
3020 return rc;
3021
3022 rc = ebitmap_write(&usrdatum->roles, fp);
3023 if (rc)
3024 return rc;
3025
3026 rc = mls_write_range_helper(&usrdatum->range, fp);
3027 if (rc)
3028 return rc;
3029
3030 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3031 if (rc)
3032 return rc;
3033
3034 return 0;
3035}
3036
3037static int (*write_f[SYM_NUM]) (void *key, void *datum,
3038 void *datap) =
3039{
3040 common_write,
3041 class_write,
3042 role_write,
3043 type_write,
3044 user_write,
3045 cond_write_bool,
3046 sens_write,
3047 cat_write,
3048};
3049
3050static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3051 void *fp)
3052{
3053 unsigned int i, j, rc;
3054 size_t nel, len;
3055 __le32 buf[3];
3056 u32 nodebuf[8];
3057 struct ocontext *c;
3058 for (i = 0; i < info->ocon_num; i++) {
3059 nel = 0;
3060 for (c = p->ocontexts[i]; c; c = c->next)
3061 nel++;
3062 buf[0] = cpu_to_le32(nel);
3063 rc = put_entry(buf, sizeof(u32), 1, fp);
3064 if (rc)
3065 return rc;
3066 for (c = p->ocontexts[i]; c; c = c->next) {
3067 switch (i) {
3068 case OCON_ISID:
3069 buf[0] = cpu_to_le32(c->sid[0]);
3070 rc = put_entry(buf, sizeof(u32), 1, fp);
3071 if (rc)
3072 return rc;
3073 rc = context_write(p, &c->context[0], fp);
3074 if (rc)
3075 return rc;
3076 break;
3077 case OCON_FS:
3078 case OCON_NETIF:
3079 len = strlen(c->u.name);
3080 buf[0] = cpu_to_le32(len);
3081 rc = put_entry(buf, sizeof(u32), 1, fp);
3082 if (rc)
3083 return rc;
3084 rc = put_entry(c->u.name, 1, len, fp);
3085 if (rc)
3086 return rc;
3087 rc = context_write(p, &c->context[0], fp);
3088 if (rc)
3089 return rc;
3090 rc = context_write(p, &c->context[1], fp);
3091 if (rc)
3092 return rc;
3093 break;
3094 case OCON_PORT:
3095 buf[0] = cpu_to_le32(c->u.port.protocol);
3096 buf[1] = cpu_to_le32(c->u.port.low_port);
3097 buf[2] = cpu_to_le32(c->u.port.high_port);
3098 rc = put_entry(buf, sizeof(u32), 3, fp);
3099 if (rc)
3100 return rc;
3101 rc = context_write(p, &c->context[0], fp);
3102 if (rc)
3103 return rc;
3104 break;
3105 case OCON_NODE:
3106 nodebuf[0] = c->u.node.addr; /* network order */
3107 nodebuf[1] = c->u.node.mask; /* network order */
3108 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3109 if (rc)
3110 return rc;
3111 rc = context_write(p, &c->context[0], fp);
3112 if (rc)
3113 return rc;
3114 break;
3115 case OCON_FSUSE:
3116 buf[0] = cpu_to_le32(c->v.behavior);
3117 len = strlen(c->u.name);
3118 buf[1] = cpu_to_le32(len);
3119 rc = put_entry(buf, sizeof(u32), 2, fp);
3120 if (rc)
3121 return rc;
3122 rc = put_entry(c->u.name, 1, len, fp);
3123 if (rc)
3124 return rc;
3125 rc = context_write(p, &c->context[0], fp);
3126 if (rc)
3127 return rc;
3128 break;
3129 case OCON_NODE6:
3130 for (j = 0; j < 4; j++)
3131 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3132 for (j = 0; j < 4; j++)
3133 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3134 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3135 if (rc)
3136 return rc;
3137 rc = context_write(p, &c->context[0], fp);
3138 if (rc)
3139 return rc;
3140 break;
3141 }
3142 }
3143 }
3144 return 0;
3145}
3146
3147static int genfs_write(struct policydb *p, void *fp)
3148{
3149 struct genfs *genfs;
3150 struct ocontext *c;
3151 size_t len;
3152 __le32 buf[1];
3153 int rc;
3154
3155 len = 0;
3156 for (genfs = p->genfs; genfs; genfs = genfs->next)
3157 len++;
3158 buf[0] = cpu_to_le32(len);
3159 rc = put_entry(buf, sizeof(u32), 1, fp);
3160 if (rc)
3161 return rc;
3162 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3163 len = strlen(genfs->fstype);
3164 buf[0] = cpu_to_le32(len);
3165 rc = put_entry(buf, sizeof(u32), 1, fp);
3166 if (rc)
3167 return rc;
3168 rc = put_entry(genfs->fstype, 1, len, fp);
3169 if (rc)
3170 return rc;
3171 len = 0;
3172 for (c = genfs->head; c; c = c->next)
3173 len++;
3174 buf[0] = cpu_to_le32(len);
3175 rc = put_entry(buf, sizeof(u32), 1, fp);
3176 if (rc)
3177 return rc;
3178 for (c = genfs->head; c; c = c->next) {
3179 len = strlen(c->u.name);
3180 buf[0] = cpu_to_le32(len);
3181 rc = put_entry(buf, sizeof(u32), 1, fp);
3182 if (rc)
3183 return rc;
3184 rc = put_entry(c->u.name, 1, len, fp);
3185 if (rc)
3186 return rc;
3187 buf[0] = cpu_to_le32(c->v.sclass);
3188 rc = put_entry(buf, sizeof(u32), 1, fp);
3189 if (rc)
3190 return rc;
3191 rc = context_write(p, &c->context[0], fp);
3192 if (rc)
3193 return rc;
3194 }
3195 }
3196 return 0;
3197}
3198
Eric Paris3f058ef2011-04-28 15:11:21 -04003199static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003200{
3201 int *cnt = ptr;
3202 *cnt = *cnt + 1;
3203
3204 return 0;
3205}
3206
3207static int range_write_helper(void *key, void *data, void *ptr)
3208{
3209 __le32 buf[2];
3210 struct range_trans *rt = key;
3211 struct mls_range *r = data;
3212 struct policy_data *pd = ptr;
3213 void *fp = pd->fp;
3214 struct policydb *p = pd->p;
3215 int rc;
3216
3217 buf[0] = cpu_to_le32(rt->source_type);
3218 buf[1] = cpu_to_le32(rt->target_type);
3219 rc = put_entry(buf, sizeof(u32), 2, fp);
3220 if (rc)
3221 return rc;
3222 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3223 buf[0] = cpu_to_le32(rt->target_class);
3224 rc = put_entry(buf, sizeof(u32), 1, fp);
3225 if (rc)
3226 return rc;
3227 }
3228 rc = mls_write_range_helper(r, fp);
3229 if (rc)
3230 return rc;
3231
3232 return 0;
3233}
3234
3235static int range_write(struct policydb *p, void *fp)
3236{
Eric Pariscee74f42010-10-13 17:50:25 -04003237 __le32 buf[1];
Eric Parisb1380042013-07-23 17:38:42 -04003238 int rc, nel;
Eric Pariscee74f42010-10-13 17:50:25 -04003239 struct policy_data pd;
3240
3241 pd.p = p;
3242 pd.fp = fp;
3243
3244 /* count the number of entries in the hashtab */
3245 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003246 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003247 if (rc)
3248 return rc;
3249
3250 buf[0] = cpu_to_le32(nel);
3251 rc = put_entry(buf, sizeof(u32), 1, fp);
3252 if (rc)
3253 return rc;
3254
3255 /* actually write all of the entries */
3256 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3257 if (rc)
3258 return rc;
3259
3260 return 0;
3261}
3262
Eric Paris2463c26d2011-04-28 15:11:21 -04003263static int filename_write_helper(void *key, void *data, void *ptr)
3264{
3265 __le32 buf[4];
3266 struct filename_trans *ft = key;
3267 struct filename_trans_datum *otype = data;
3268 void *fp = ptr;
3269 int rc;
3270 u32 len;
3271
3272 len = strlen(ft->name);
3273 buf[0] = cpu_to_le32(len);
3274 rc = put_entry(buf, sizeof(u32), 1, fp);
3275 if (rc)
3276 return rc;
3277
3278 rc = put_entry(ft->name, sizeof(char), len, fp);
3279 if (rc)
3280 return rc;
3281
Eric Paris9085a642014-02-20 10:56:45 -05003282 buf[0] = cpu_to_le32(ft->stype);
3283 buf[1] = cpu_to_le32(ft->ttype);
3284 buf[2] = cpu_to_le32(ft->tclass);
3285 buf[3] = cpu_to_le32(otype->otype);
Eric Paris2463c26d2011-04-28 15:11:21 -04003286
3287 rc = put_entry(buf, sizeof(u32), 4, fp);
3288 if (rc)
3289 return rc;
3290
3291 return 0;
3292}
3293
Eric Paris652bb9b2011-02-01 11:05:40 -05003294static int filename_trans_write(struct policydb *p, void *fp)
3295{
Eric Paris2463c26d2011-04-28 15:11:21 -04003296 u32 nel;
3297 __le32 buf[1];
Eric Paris652bb9b2011-02-01 11:05:40 -05003298 int rc;
3299
Roy.Lided50982011-05-20 10:38:06 +08003300 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3301 return 0;
3302
Eric Paris2463c26d2011-04-28 15:11:21 -04003303 nel = 0;
3304 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3305 if (rc)
3306 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003307
3308 buf[0] = cpu_to_le32(nel);
3309 rc = put_entry(buf, sizeof(u32), 1, fp);
3310 if (rc)
3311 return rc;
3312
Eric Paris2463c26d2011-04-28 15:11:21 -04003313 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3314 if (rc)
3315 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003316
Eric Paris652bb9b2011-02-01 11:05:40 -05003317 return 0;
3318}
Eric Paris2463c26d2011-04-28 15:11:21 -04003319
Eric Pariscee74f42010-10-13 17:50:25 -04003320/*
3321 * Write the configuration data in a policy database
3322 * structure to a policy database binary representation
3323 * file.
3324 */
3325int policydb_write(struct policydb *p, void *fp)
3326{
3327 unsigned int i, num_syms;
3328 int rc;
3329 __le32 buf[4];
3330 u32 config;
3331 size_t len;
3332 struct policydb_compat_info *info;
3333
3334 /*
3335 * refuse to write policy older than compressed avtab
3336 * to simplify the writer. There are other tests dropped
3337 * since we assume this throughout the writer code. Be
3338 * careful if you ever try to remove this restriction
3339 */
3340 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3341 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3342 " Because it is less than version %d\n", p->policyvers,
3343 POLICYDB_VERSION_AVTAB);
3344 return -EINVAL;
3345 }
3346
3347 config = 0;
3348 if (p->mls_enabled)
3349 config |= POLICYDB_CONFIG_MLS;
3350
3351 if (p->reject_unknown)
3352 config |= REJECT_UNKNOWN;
3353 if (p->allow_unknown)
3354 config |= ALLOW_UNKNOWN;
3355
3356 /* Write the magic number and string identifiers. */
3357 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3358 len = strlen(POLICYDB_STRING);
3359 buf[1] = cpu_to_le32(len);
3360 rc = put_entry(buf, sizeof(u32), 2, fp);
3361 if (rc)
3362 return rc;
3363 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3364 if (rc)
3365 return rc;
3366
3367 /* Write the version, config, and table sizes. */
3368 info = policydb_lookup_compat(p->policyvers);
3369 if (!info) {
3370 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3371 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003372 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003373 }
3374
3375 buf[0] = cpu_to_le32(p->policyvers);
3376 buf[1] = cpu_to_le32(config);
3377 buf[2] = cpu_to_le32(info->sym_num);
3378 buf[3] = cpu_to_le32(info->ocon_num);
3379
3380 rc = put_entry(buf, sizeof(u32), 4, fp);
3381 if (rc)
3382 return rc;
3383
3384 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3385 rc = ebitmap_write(&p->policycaps, fp);
3386 if (rc)
3387 return rc;
3388 }
3389
3390 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3391 rc = ebitmap_write(&p->permissive_map, fp);
3392 if (rc)
3393 return rc;
3394 }
3395
3396 num_syms = info->sym_num;
3397 for (i = 0; i < num_syms; i++) {
3398 struct policy_data pd;
3399
3400 pd.fp = fp;
3401 pd.p = p;
3402
3403 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3404 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3405
3406 rc = put_entry(buf, sizeof(u32), 2, fp);
3407 if (rc)
3408 return rc;
3409 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3410 if (rc)
3411 return rc;
3412 }
3413
3414 rc = avtab_write(p, &p->te_avtab, fp);
3415 if (rc)
3416 return rc;
3417
3418 rc = cond_write_list(p, p->cond_list, fp);
3419 if (rc)
3420 return rc;
3421
Harry Ciaoc900ff32011-03-25 13:52:00 +08003422 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003423 if (rc)
3424 return rc;
3425
3426 rc = role_allow_write(p->role_allow, fp);
3427 if (rc)
3428 return rc;
3429
Eric Paris652bb9b2011-02-01 11:05:40 -05003430 rc = filename_trans_write(p, fp);
3431 if (rc)
3432 return rc;
3433
Eric Pariscee74f42010-10-13 17:50:25 -04003434 rc = ocontext_write(p, info, fp);
3435 if (rc)
3436 return rc;
3437
3438 rc = genfs_write(p, fp);
3439 if (rc)
3440 return rc;
3441
3442 rc = range_write(p, fp);
3443 if (rc)
3444 return rc;
3445
3446 for (i = 0; i < p->p_types.nprim; i++) {
3447 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3448
3449 BUG_ON(!e);
3450 rc = ebitmap_write(e, fp);
3451 if (rc)
3452 return rc;
3453 }
3454
3455 return 0;
3456}