blob: 74aa224267c11fd31262dc45dc91c9c726ef6702 [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));
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500292 if (!p->filename_trans) {
293 rc = -ENOMEM;
Eric Paris2463c26d2011-04-28 15:11:21 -0400294 goto out;
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500295 }
Eric Paris2463c26d2011-04-28 15:11:21 -0400296
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500297 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500298 if (!p->range_tr) {
299 rc = -ENOMEM;
Eric Paris9398c7f2010-11-23 11:40:08 -0500300 goto out;
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500301 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500302
Eric Paris03a4c012011-04-28 15:11:21 -0400303 ebitmap_init(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500304 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100305 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500306
Eric Paris9398c7f2010-11-23 11:40:08 -0500307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308out:
Eric Paris2463c26d2011-04-28 15:11:21 -0400309 hashtab_destroy(p->filename_trans);
310 hashtab_destroy(p->range_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 for (i = 0; i < SYM_NUM; i++)
312 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500313 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/*
317 * The following *_index functions are used to
318 * define the val_to_name and val_to_struct arrays
319 * in a policy database structure. The val_to_name
320 * arrays are used when converting security context
321 * structures into string representations. The
322 * val_to_struct arrays are used when the attributes
323 * of a class, role, or user are needed.
324 */
325
326static int common_index(void *key, void *datum, void *datap)
327{
328 struct policydb *p;
329 struct common_datum *comdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500330 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 comdatum = datum;
333 p = datap;
334 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
335 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500336
337 fa = p->sym_val_to_name[SYM_COMMONS];
338 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
339 GFP_KERNEL | __GFP_ZERO))
340 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342}
343
344static int class_index(void *key, void *datum, void *datap)
345{
346 struct policydb *p;
347 struct class_datum *cladatum;
Eric Parisac76c052010-11-29 15:47:09 -0500348 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 cladatum = datum;
351 p = datap;
352 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
353 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500354 fa = p->sym_val_to_name[SYM_CLASSES];
355 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
356 GFP_KERNEL | __GFP_ZERO))
357 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 p->class_val_to_struct[cladatum->value - 1] = cladatum;
359 return 0;
360}
361
362static int role_index(void *key, void *datum, void *datap)
363{
364 struct policydb *p;
365 struct role_datum *role;
Eric Parisac76c052010-11-29 15:47:09 -0500366 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 role = datum;
369 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900370 if (!role->value
371 || role->value > p->p_roles.nprim
372 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500374
375 fa = p->sym_val_to_name[SYM_ROLES];
376 if (flex_array_put_ptr(fa, role->value - 1, key,
377 GFP_KERNEL | __GFP_ZERO))
378 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 p->role_val_to_struct[role->value - 1] = role;
380 return 0;
381}
382
383static int type_index(void *key, void *datum, void *datap)
384{
385 struct policydb *p;
386 struct type_datum *typdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500387 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 typdatum = datum;
390 p = datap;
391
392 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900393 if (!typdatum->value
394 || typdatum->value > p->p_types.nprim
395 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500397 fa = p->sym_val_to_name[SYM_TYPES];
398 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
399 GFP_KERNEL | __GFP_ZERO))
400 BUG();
401
402 fa = p->type_val_to_struct_array;
403 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500404 GFP_KERNEL | __GFP_ZERO))
405 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 return 0;
409}
410
411static int user_index(void *key, void *datum, void *datap)
412{
413 struct policydb *p;
414 struct user_datum *usrdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500415 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 usrdatum = datum;
418 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900419 if (!usrdatum->value
420 || usrdatum->value > p->p_users.nprim
421 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500423
424 fa = p->sym_val_to_name[SYM_USERS];
425 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
426 GFP_KERNEL | __GFP_ZERO))
427 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
429 return 0;
430}
431
432static int sens_index(void *key, void *datum, void *datap)
433{
434 struct policydb *p;
435 struct level_datum *levdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500436 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 levdatum = datum;
439 p = datap;
440
441 if (!levdatum->isalias) {
442 if (!levdatum->level->sens ||
443 levdatum->level->sens > p->p_levels.nprim)
444 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500445 fa = p->sym_val_to_name[SYM_LEVELS];
446 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
447 GFP_KERNEL | __GFP_ZERO))
448 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 return 0;
452}
453
454static int cat_index(void *key, void *datum, void *datap)
455{
456 struct policydb *p;
457 struct cat_datum *catdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500458 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 catdatum = datum;
461 p = datap;
462
463 if (!catdatum->isalias) {
464 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
465 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500466 fa = p->sym_val_to_name[SYM_CATS];
467 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
468 GFP_KERNEL | __GFP_ZERO))
469 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 return 0;
473}
474
475static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
476{
477 common_index,
478 class_index,
479 role_index,
480 type_index,
481 user_index,
482 cond_index_bool,
483 sens_index,
484 cat_index,
485};
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#ifdef DEBUG_HASHES
Eric Parisbe30b162011-04-28 15:11:21 -0400488static void hash_eval(struct hashtab *h, const char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500489{
490 struct hashtab_info info;
491
492 hashtab_stat(h, &info);
Eric Parisbe30b162011-04-28 15:11:21 -0400493 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
494 "longest chain length %d\n", hash_name, h->nel,
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500495 info.slots_used, h->size, info.max_chain_len);
496}
Eric Parisbe30b162011-04-28 15:11:21 -0400497
498static void symtab_hash_eval(struct symtab *s)
499{
500 int i;
501
502 for (i = 0; i < SYM_NUM; i++)
503 hash_eval(s[i].table, symtab_name[i]);
504}
505
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500506#else
Eric Parisbe30b162011-04-28 15:11:21 -0400507static inline void hash_eval(struct hashtab *h, char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500508{
509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#endif
511
512/*
513 * Define the other val_to_name and val_to_struct arrays
514 * in a policy database structure.
515 *
516 * Caller must clean up on failure.
517 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500518static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Eric Paris9398c7f2010-11-23 11:40:08 -0500520 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
James Morris454d9722008-02-26 20:42:02 +1100522 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100524 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 printk(", %d sens, %d cats", p->p_levels.nprim,
526 p->p_cats.nprim);
527 printk("\n");
528
James Morris454d9722008-02-26 20:42:02 +1100529 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 p->p_classes.nprim, p->te_avtab.nel);
531
532#ifdef DEBUG_HASHES
533 avtab_hash_eval(&p->te_avtab, "rules");
534 symtab_hash_eval(p->symtab);
535#endif
536
Eric Paris9398c7f2010-11-23 11:40:08 -0500537 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500538 p->class_val_to_struct =
539 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
540 GFP_KERNEL);
541 if (!p->class_val_to_struct)
542 goto out;
543
544 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 p->role_val_to_struct =
546 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400547 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500548 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Eric Paris9398c7f2010-11-23 11:40:08 -0500551 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 p->user_val_to_struct =
553 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400554 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500555 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Eric Paris23bdecb2010-11-29 15:47:09 -0500558 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500559 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500560 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
561 p->p_types.nprim,
562 GFP_KERNEL | __GFP_ZERO);
563 if (!p->type_val_to_struct_array)
564 goto out;
565
566 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
Eric Paris5a3ea872011-04-28 15:55:52 -0400567 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
Eric Paris23bdecb2010-11-29 15:47:09 -0500568 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900569 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900570
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300571 rc = cond_init_bool_indexes(p);
572 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500575 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500576 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500577 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
578 p->symtab[i].nprim,
579 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500580 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500582
583 rc = flex_array_prealloc(p->sym_val_to_name[i],
Eric Paris5a3ea872011-04-28 15:55:52 -0400584 0, p->symtab[i].nprim,
Eric Parisac76c052010-11-29 15:47:09 -0500585 GFP_KERNEL | __GFP_ZERO);
586 if (rc)
587 goto out;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
590 if (rc)
591 goto out;
592 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500593 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594out:
595 return rc;
596}
597
598/*
599 * The following *_destroy functions are used to
600 * free any memory allocated for each kind of
601 * symbol data in the policy database.
602 */
603
604static int perm_destroy(void *key, void *datum, void *p)
605{
606 kfree(key);
607 kfree(datum);
608 return 0;
609}
610
611static int common_destroy(void *key, void *datum, void *p)
612{
613 struct common_datum *comdatum;
614
615 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500616 if (datum) {
617 comdatum = datum;
618 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
619 hashtab_destroy(comdatum->permissions.table);
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree(datum);
622 return 0;
623}
624
Richard Hainesa660bec2013-11-19 17:34:23 -0500625static void constraint_expr_destroy(struct constraint_expr *expr)
626{
627 if (expr) {
628 ebitmap_destroy(&expr->names);
629 if (expr->type_names) {
630 ebitmap_destroy(&expr->type_names->types);
631 ebitmap_destroy(&expr->type_names->negset);
632 kfree(expr->type_names);
633 }
634 kfree(expr);
635 }
636}
637
James Morris6cbda6b2006-11-29 16:50:27 -0500638static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct class_datum *cladatum;
641 struct constraint_node *constraint, *ctemp;
642 struct constraint_expr *e, *etmp;
643
644 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500645 if (datum) {
646 cladatum = datum;
647 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
648 hashtab_destroy(cladatum->permissions.table);
649 constraint = cladatum->constraints;
650 while (constraint) {
651 e = constraint->expr;
652 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500653 etmp = e;
654 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500655 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500656 }
657 ctemp = constraint;
658 constraint = constraint->next;
659 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Paris9398c7f2010-11-23 11:40:08 -0500662 constraint = cladatum->validatetrans;
663 while (constraint) {
664 e = constraint->expr;
665 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500666 etmp = e;
667 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500668 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500669 }
670 ctemp = constraint;
671 constraint = constraint->next;
672 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500674 kfree(cladatum->comkey);
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 kfree(datum);
677 return 0;
678}
679
680static int role_destroy(void *key, void *datum, void *p)
681{
682 struct role_datum *role;
683
684 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500685 if (datum) {
686 role = datum;
687 ebitmap_destroy(&role->dominates);
688 ebitmap_destroy(&role->types);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree(datum);
691 return 0;
692}
693
694static int type_destroy(void *key, void *datum, void *p)
695{
696 kfree(key);
697 kfree(datum);
698 return 0;
699}
700
701static int user_destroy(void *key, void *datum, void *p)
702{
703 struct user_datum *usrdatum;
704
705 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500706 if (datum) {
707 usrdatum = datum;
708 ebitmap_destroy(&usrdatum->roles);
709 ebitmap_destroy(&usrdatum->range.level[0].cat);
710 ebitmap_destroy(&usrdatum->range.level[1].cat);
711 ebitmap_destroy(&usrdatum->dfltlevel.cat);
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 kfree(datum);
714 return 0;
715}
716
717static int sens_destroy(void *key, void *datum, void *p)
718{
719 struct level_datum *levdatum;
720
721 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500722 if (datum) {
723 levdatum = datum;
724 ebitmap_destroy(&levdatum->level->cat);
725 kfree(levdatum->level);
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 kfree(datum);
728 return 0;
729}
730
731static int cat_destroy(void *key, void *datum, void *p)
732{
733 kfree(key);
734 kfree(datum);
735 return 0;
736}
737
738static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
739{
740 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500741 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 role_destroy,
743 type_destroy,
744 user_destroy,
745 cond_destroy_bool,
746 sens_destroy,
747 cat_destroy,
748};
749
Eric Paris2463c26d2011-04-28 15:11:21 -0400750static int filenametr_destroy(void *key, void *datum, void *p)
751{
752 struct filename_trans *ft = key;
753 kfree(ft->name);
754 kfree(key);
755 kfree(datum);
756 cond_resched();
757 return 0;
758}
759
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500760static int range_tr_destroy(void *key, void *datum, void *p)
761{
762 struct mls_range *rt = datum;
763 kfree(key);
764 ebitmap_destroy(&rt->level[0].cat);
765 ebitmap_destroy(&rt->level[1].cat);
766 kfree(datum);
767 cond_resched();
768 return 0;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static void ocontext_destroy(struct ocontext *c, int i)
772{
Eric Parisd1b43542010-07-21 12:50:57 -0400773 if (!c)
774 return;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 context_destroy(&c->context[0]);
777 context_destroy(&c->context[1]);
778 if (i == OCON_ISID || i == OCON_FS ||
779 i == OCON_NETIF || i == OCON_FSUSE)
780 kfree(c->u.name);
781 kfree(c);
782}
783
784/*
785 * Free any memory allocated by a policy database structure.
786 */
787void policydb_destroy(struct policydb *p)
788{
789 struct ocontext *c, *ctmp;
790 struct genfs *g, *gtmp;
791 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700792 struct role_allow *ra, *lra = NULL;
793 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400796 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
798 hashtab_destroy(p->symtab[i].table);
799 }
800
Eric Parisac76c052010-11-29 15:47:09 -0500801 for (i = 0; i < SYM_NUM; i++) {
802 if (p->sym_val_to_name[i])
803 flex_array_free(p->sym_val_to_name[i]);
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700806 kfree(p->class_val_to_struct);
807 kfree(p->role_val_to_struct);
808 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500809 if (p->type_val_to_struct_array)
810 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 avtab_destroy(&p->te_avtab);
813
814 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400815 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 c = p->ocontexts[i];
817 while (c) {
818 ctmp = c;
819 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400820 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400822 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 g = p->genfs;
826 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400827 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 kfree(g->fstype);
829 c = g->head;
830 while (c) {
831 ctmp = c;
832 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400833 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 gtmp = g;
836 g = g->next;
837 kfree(gtmp);
838 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400839 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 cond_policydb_destroy(p);
842
Stephen Smalley782ebb92005-09-03 15:55:16 -0700843 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400844 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800845 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700846 ltr = tr;
847 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800848 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700849
Eric Paris2ced3df2008-04-17 13:37:12 -0400850 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400851 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800852 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700853 lra = ra;
854 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800855 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700856
Eric Paris2463c26d2011-04-28 15:11:21 -0400857 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
858 hashtab_destroy(p->filename_trans);
859
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500860 hashtab_map(p->range_tr, range_tr_destroy, NULL);
861 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700862
Eric Paris6371dcd2010-07-29 23:02:34 -0400863 if (p->type_attr_map_array) {
864 for (i = 0; i < p->p_types.nprim; i++) {
865 struct ebitmap *e;
866
867 e = flex_array_get(p->type_attr_map_array, i);
868 if (!e)
869 continue;
870 ebitmap_destroy(e);
871 }
872 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700873 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500874
Eric Paris03a4c012011-04-28 15:11:21 -0400875 ebitmap_destroy(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500876 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100877 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return;
880}
881
882/*
883 * Load the initial SIDs specified in a policy database
884 * structure into a SID table.
885 */
886int policydb_load_isids(struct policydb *p, struct sidtab *s)
887{
888 struct ocontext *head, *c;
889 int rc;
890
891 rc = sidtab_init(s);
892 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100893 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto out;
895 }
896
897 head = p->ocontexts[OCON_ISID];
898 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500899 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500901 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
902 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto out;
904 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500905
906 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
907 if (rc) {
908 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
909 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto out;
911 }
912 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500913 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914out:
915 return rc;
916}
917
Stephen Smalley45e54212007-11-07 10:08:00 -0500918int policydb_class_isvalid(struct policydb *p, unsigned int class)
919{
920 if (!class || class > p->p_classes.nprim)
921 return 0;
922 return 1;
923}
924
925int policydb_role_isvalid(struct policydb *p, unsigned int role)
926{
927 if (!role || role > p->p_roles.nprim)
928 return 0;
929 return 1;
930}
931
932int policydb_type_isvalid(struct policydb *p, unsigned int type)
933{
934 if (!type || type > p->p_types.nprim)
935 return 0;
936 return 1;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/*
940 * Return 1 if the fields in the security context
941 * structure `c' are valid. Return 0 otherwise.
942 */
943int policydb_context_isvalid(struct policydb *p, struct context *c)
944{
945 struct role_datum *role;
946 struct user_datum *usrdatum;
947
948 if (!c->role || c->role > p->p_roles.nprim)
949 return 0;
950
951 if (!c->user || c->user > p->p_users.nprim)
952 return 0;
953
954 if (!c->type || c->type > p->p_types.nprim)
955 return 0;
956
957 if (c->role != OBJECT_R_VAL) {
958 /*
959 * Role must be authorized for the type.
960 */
961 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500962 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* role may not be associated with type */
964 return 0;
965
966 /*
967 * User must be authorized for the role.
968 */
969 usrdatum = p->user_val_to_struct[c->user - 1];
970 if (!usrdatum)
971 return 0;
972
Eric Paris9398c7f2010-11-23 11:40:08 -0500973 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* user may not be associated with role */
975 return 0;
976 }
977
978 if (!mls_context_isvalid(p, c))
979 return 0;
980
981 return 1;
982}
983
984/*
985 * Read a MLS range structure from a policydb binary
986 * representation file.
987 */
988static int mls_read_range_helper(struct mls_range *r, void *fp)
989{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700990 __le32 buf[2];
991 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int rc;
993
994 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500995 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto out;
997
Eric Paris9398c7f2010-11-23 11:40:08 -0500998 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 items = le32_to_cpu(buf[0]);
1000 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +11001001 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 goto out;
1003 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -05001006 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001007 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
1009 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 r->level[0].sens = le32_to_cpu(buf[0]);
1012 if (items > 1)
1013 r->level[1].sens = le32_to_cpu(buf[1]);
1014 else
1015 r->level[1].sens = r->level[0].sens;
1016
1017 rc = ebitmap_read(&r->level[0].cat, fp);
1018 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001019 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto out;
1021 }
1022 if (items > 1) {
1023 rc = ebitmap_read(&r->level[1].cat, fp);
1024 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001025 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto bad_high;
1027 }
1028 } else {
1029 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1030 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001031 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto bad_high;
1033 }
1034 }
1035
Eric Paris9398c7f2010-11-23 11:40:08 -05001036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037bad_high:
1038 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -05001039out:
1040 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/*
1044 * Read and validate a security context structure
1045 * from a policydb binary representation file.
1046 */
1047static int context_read_and_validate(struct context *c,
1048 struct policydb *p,
1049 void *fp)
1050{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001051 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int rc;
1053
1054 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001055 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001056 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out;
1058 }
1059 c->user = le32_to_cpu(buf[0]);
1060 c->role = le32_to_cpu(buf[1]);
1061 c->type = le32_to_cpu(buf[2]);
1062 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001063 rc = mls_read_range_helper(&c->range, fp);
1064 if (rc) {
1065 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto out;
1067 }
1068 }
1069
Eric Paris9398c7f2010-11-23 11:40:08 -05001070 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001072 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001076 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077out:
1078 return rc;
1079}
1080
1081/*
1082 * The following *_read functions are used to
1083 * read the symbol data from a policy database
1084 * binary representation file.
1085 */
1086
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001087static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1088{
1089 int rc;
1090 char *str;
1091
1092 str = kmalloc(len + 1, flags);
1093 if (!str)
1094 return -ENOMEM;
1095
1096 /* it's expected the caller should free the str */
1097 *strp = str;
1098
1099 rc = next_entry(str, fp, len);
1100 if (rc)
1101 return rc;
1102
1103 str[len] = '\0';
1104 return 0;
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1108{
1109 char *key = NULL;
1110 struct perm_datum *perdatum;
1111 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001112 __le32 buf[2];
1113 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Eric Paris9398c7f2010-11-23 11:40:08 -05001115 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001116 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001117 if (!perdatum)
1118 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001121 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto bad;
1123
1124 len = le32_to_cpu(buf[0]);
1125 perdatum->value = le32_to_cpu(buf[1]);
1126
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001127 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001128 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 rc = hashtab_insert(h, key, perdatum);
1132 if (rc)
1133 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001134
1135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136bad:
1137 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001138 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1142{
1143 char *key = NULL;
1144 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001145 __le32 buf[4];
1146 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int i, rc;
1148
Eric Paris9398c7f2010-11-23 11:40:08 -05001149 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001150 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001151 if (!comdatum)
1152 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001155 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto bad;
1157
1158 len = le32_to_cpu(buf[0]);
1159 comdatum->value = le32_to_cpu(buf[1]);
1160
1161 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1162 if (rc)
1163 goto bad;
1164 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1165 nel = le32_to_cpu(buf[3]);
1166
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001167 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001168 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 for (i = 0; i < nel; i++) {
1172 rc = perm_read(p, comdatum->permissions.table, fp);
1173 if (rc)
1174 goto bad;
1175 }
1176
1177 rc = hashtab_insert(h, key, comdatum);
1178 if (rc)
1179 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181bad:
1182 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001183 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Richard Hainesa660bec2013-11-19 17:34:23 -05001186static void type_set_init(struct type_set *t)
1187{
1188 ebitmap_init(&t->types);
1189 ebitmap_init(&t->negset);
1190}
1191
1192static int type_set_read(struct type_set *t, void *fp)
1193{
1194 __le32 buf[1];
1195 int rc;
1196
1197 if (ebitmap_read(&t->types, fp))
1198 return -EINVAL;
1199 if (ebitmap_read(&t->negset, fp))
1200 return -EINVAL;
1201
1202 rc = next_entry(buf, fp, sizeof(u32));
1203 if (rc < 0)
1204 return -EINVAL;
1205 t->flags = le32_to_cpu(buf[0]);
1206
1207 return 0;
1208}
1209
1210
1211static int read_cons_helper(struct policydb *p,
1212 struct constraint_node **nodep,
1213 int ncons, int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct constraint_node *c, *lc;
1216 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001217 __le32 buf[3];
1218 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int rc, i, j, depth;
1220
1221 lc = NULL;
1222 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001223 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!c)
1225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Eric Paris2ced3df2008-04-17 13:37:12 -04001227 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001229 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001233 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return rc;
1235 c->permissions = le32_to_cpu(buf[0]);
1236 nexpr = le32_to_cpu(buf[1]);
1237 le = NULL;
1238 depth = -1;
1239 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001240 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!e)
1242 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Eric Paris2ced3df2008-04-17 13:37:12 -04001244 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001246 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001250 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return rc;
1252 e->expr_type = le32_to_cpu(buf[0]);
1253 e->attr = le32_to_cpu(buf[1]);
1254 e->op = le32_to_cpu(buf[2]);
1255
1256 switch (e->expr_type) {
1257 case CEXPR_NOT:
1258 if (depth < 0)
1259 return -EINVAL;
1260 break;
1261 case CEXPR_AND:
1262 case CEXPR_OR:
1263 if (depth < 1)
1264 return -EINVAL;
1265 depth--;
1266 break;
1267 case CEXPR_ATTR:
1268 if (depth == (CEXPR_MAXDEPTH - 1))
1269 return -EINVAL;
1270 depth++;
1271 break;
1272 case CEXPR_NAMES:
1273 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1274 return -EINVAL;
1275 if (depth == (CEXPR_MAXDEPTH - 1))
1276 return -EINVAL;
1277 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001278 rc = ebitmap_read(&e->names, fp);
1279 if (rc)
1280 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05001281 if (p->policyvers >=
1282 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1283 e->type_names = kzalloc(sizeof
1284 (*e->type_names),
1285 GFP_KERNEL);
1286 if (!e->type_names)
1287 return -ENOMEM;
1288 type_set_init(e->type_names);
1289 rc = type_set_read(e->type_names, fp);
1290 if (rc)
1291 return rc;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 default:
1295 return -EINVAL;
1296 }
1297 le = e;
1298 }
1299 if (depth != 0)
1300 return -EINVAL;
1301 lc = c;
1302 }
1303
1304 return 0;
1305}
1306
1307static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1308{
1309 char *key = NULL;
1310 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001311 __le32 buf[6];
1312 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int i, rc;
1314
Eric Paris9398c7f2010-11-23 11:40:08 -05001315 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001316 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001317 if (!cladatum)
1318 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001321 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto bad;
1323
1324 len = le32_to_cpu(buf[0]);
1325 len2 = le32_to_cpu(buf[1]);
1326 cladatum->value = le32_to_cpu(buf[2]);
1327
1328 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1329 if (rc)
1330 goto bad;
1331 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1332 nel = le32_to_cpu(buf[4]);
1333
1334 ncons = le32_to_cpu(buf[5]);
1335
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001336 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001337 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (len2) {
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001341 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001342 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Eric Paris9398c7f2010-11-23 11:40:08 -05001345 rc = -EINVAL;
1346 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001348 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 goto bad;
1350 }
1351 }
1352 for (i = 0; i < nel; i++) {
1353 rc = perm_read(p, cladatum->permissions.table, fp);
1354 if (rc)
1355 goto bad;
1356 }
1357
Richard Hainesa660bec2013-11-19 17:34:23 -05001358 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (rc)
1360 goto bad;
1361
1362 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1363 /* grab the validatetrans rules */
1364 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001365 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto bad;
1367 ncons = le32_to_cpu(buf[0]);
Richard Hainesa660bec2013-11-19 17:34:23 -05001368 rc = read_cons_helper(p, &cladatum->validatetrans,
1369 ncons, 1, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (rc)
1371 goto bad;
1372 }
1373
Eric Parisaa893262012-03-20 14:35:12 -04001374 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1375 rc = next_entry(buf, fp, sizeof(u32) * 3);
1376 if (rc)
1377 goto bad;
1378
1379 cladatum->default_user = le32_to_cpu(buf[0]);
1380 cladatum->default_role = le32_to_cpu(buf[1]);
1381 cladatum->default_range = le32_to_cpu(buf[2]);
1382 }
1383
Eric Pariseed77952012-03-20 14:35:12 -04001384 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1385 rc = next_entry(buf, fp, sizeof(u32) * 1);
1386 if (rc)
1387 goto bad;
1388 cladatum->default_type = le32_to_cpu(buf[0]);
1389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 rc = hashtab_insert(h, key, cladatum);
1392 if (rc)
1393 goto bad;
1394
Eric Paris9398c7f2010-11-23 11:40:08 -05001395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001397 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001398 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1402{
1403 char *key = NULL;
1404 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001405 int rc, to_read = 2;
1406 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001407 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric Paris9398c7f2010-11-23 11:40:08 -05001409 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001410 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001411 if (!role)
1412 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
KaiGai Koheid9250de2008-08-28 16:35:57 +09001414 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1415 to_read = 3;
1416
1417 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001418 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto bad;
1420
1421 len = le32_to_cpu(buf[0]);
1422 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001423 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1424 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001426 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001427 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 rc = ebitmap_read(&role->dominates, fp);
1431 if (rc)
1432 goto bad;
1433
1434 rc = ebitmap_read(&role->types, fp);
1435 if (rc)
1436 goto bad;
1437
1438 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001439 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001441 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto bad;
1444 }
1445 rc = 0;
1446 goto bad;
1447 }
1448
1449 rc = hashtab_insert(h, key, role);
1450 if (rc)
1451 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453bad:
1454 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001455 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1459{
1460 char *key = NULL;
1461 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001462 int rc, to_read = 3;
1463 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001464 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Eric Paris9398c7f2010-11-23 11:40:08 -05001466 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001467 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001468 if (!typdatum)
1469 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
KaiGai Koheid9250de2008-08-28 16:35:57 +09001471 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1472 to_read = 4;
1473
1474 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001475 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 goto bad;
1477
1478 len = le32_to_cpu(buf[0]);
1479 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001480 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1481 u32 prop = le32_to_cpu(buf[2]);
1482
1483 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1484 typdatum->primary = 1;
1485 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1486 typdatum->attribute = 1;
1487
1488 typdatum->bounds = le32_to_cpu(buf[3]);
1489 } else {
1490 typdatum->primary = le32_to_cpu(buf[2]);
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001493 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001494 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 rc = hashtab_insert(h, key, typdatum);
1498 if (rc)
1499 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501bad:
1502 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001503 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506
1507/*
1508 * Read a MLS level structure from a policydb binary
1509 * representation file.
1510 */
1511static int mls_read_level(struct mls_level *lp, void *fp)
1512{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001513 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 int rc;
1515
1516 memset(lp, 0, sizeof(*lp));
1517
1518 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001519 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001520 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001521 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 lp->sens = le32_to_cpu(buf[0]);
1524
Eric Paris9398c7f2010-11-23 11:40:08 -05001525 rc = ebitmap_read(&lp->cat, fp);
1526 if (rc) {
1527 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1534{
1535 char *key = NULL;
1536 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001537 int rc, to_read = 2;
1538 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001539 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Eric Paris9398c7f2010-11-23 11:40:08 -05001541 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001542 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001543 if (!usrdatum)
1544 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
KaiGai Koheid9250de2008-08-28 16:35:57 +09001546 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1547 to_read = 3;
1548
1549 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001550 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto bad;
1552
1553 len = le32_to_cpu(buf[0]);
1554 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001555 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1556 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001558 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001559 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 rc = ebitmap_read(&usrdatum->roles, fp);
1563 if (rc)
1564 goto bad;
1565
1566 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1567 rc = mls_read_range_helper(&usrdatum->range, fp);
1568 if (rc)
1569 goto bad;
1570 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1571 if (rc)
1572 goto bad;
1573 }
1574
1575 rc = hashtab_insert(h, key, usrdatum);
1576 if (rc)
1577 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579bad:
1580 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001581 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1585{
1586 char *key = NULL;
1587 struct level_datum *levdatum;
1588 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001589 __le32 buf[2];
1590 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Eric Paris9398c7f2010-11-23 11:40:08 -05001592 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001593 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001594 if (!levdatum)
1595 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001598 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto bad;
1600
1601 len = le32_to_cpu(buf[0]);
1602 levdatum->isalias = le32_to_cpu(buf[1]);
1603
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001604 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001605 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Eric Paris9398c7f2010-11-23 11:40:08 -05001608 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001610 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001612
1613 rc = mls_read_level(levdatum->level, fp);
1614 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 rc = hashtab_insert(h, key, levdatum);
1618 if (rc)
1619 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621bad:
1622 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001623 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
1626static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1627{
1628 char *key = NULL;
1629 struct cat_datum *catdatum;
1630 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001631 __le32 buf[3];
1632 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Eric Paris9398c7f2010-11-23 11:40:08 -05001634 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001635 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001636 if (!catdatum)
1637 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001640 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 goto bad;
1642
1643 len = le32_to_cpu(buf[0]);
1644 catdatum->value = le32_to_cpu(buf[1]);
1645 catdatum->isalias = le32_to_cpu(buf[2]);
1646
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001647 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001648 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 rc = hashtab_insert(h, key, catdatum);
1652 if (rc)
1653 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655bad:
1656 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001657 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1661{
1662 common_read,
1663 class_read,
1664 role_read,
1665 type_read,
1666 user_read,
1667 cond_read_bool,
1668 sens_read,
1669 cat_read,
1670};
1671
KaiGai Koheid9250de2008-08-28 16:35:57 +09001672static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1673{
1674 struct user_datum *upper, *user;
1675 struct policydb *p = datap;
1676 int depth = 0;
1677
1678 upper = user = datum;
1679 while (upper->bounds) {
1680 struct ebitmap_node *node;
1681 unsigned long bit;
1682
1683 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1684 printk(KERN_ERR "SELinux: user %s: "
1685 "too deep or looped boundary",
1686 (char *) key);
1687 return -EINVAL;
1688 }
1689
1690 upper = p->user_val_to_struct[upper->bounds - 1];
1691 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1692 if (ebitmap_get_bit(&upper->roles, bit))
1693 continue;
1694
1695 printk(KERN_ERR
1696 "SELinux: boundary violated policy: "
1697 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001698 sym_name(p, SYM_USERS, user->value - 1),
1699 sym_name(p, SYM_ROLES, bit),
1700 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001701
1702 return -EINVAL;
1703 }
1704 }
1705
1706 return 0;
1707}
1708
1709static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1710{
1711 struct role_datum *upper, *role;
1712 struct policydb *p = datap;
1713 int depth = 0;
1714
1715 upper = role = datum;
1716 while (upper->bounds) {
1717 struct ebitmap_node *node;
1718 unsigned long bit;
1719
1720 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1721 printk(KERN_ERR "SELinux: role %s: "
1722 "too deep or looped bounds\n",
1723 (char *) key);
1724 return -EINVAL;
1725 }
1726
1727 upper = p->role_val_to_struct[upper->bounds - 1];
1728 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1729 if (ebitmap_get_bit(&upper->types, bit))
1730 continue;
1731
1732 printk(KERN_ERR
1733 "SELinux: boundary violated policy: "
1734 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001735 sym_name(p, SYM_ROLES, role->value - 1),
1736 sym_name(p, SYM_TYPES, bit),
1737 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001738
1739 return -EINVAL;
1740 }
1741 }
1742
1743 return 0;
1744}
1745
1746static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1747{
Eric Parisdaa6d832010-08-03 15:26:05 -04001748 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001749 struct policydb *p = datap;
1750 int depth = 0;
1751
Eric Parisdaa6d832010-08-03 15:26:05 -04001752 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001753 while (upper->bounds) {
1754 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1755 printk(KERN_ERR "SELinux: type %s: "
1756 "too deep or looped boundary\n",
1757 (char *) key);
1758 return -EINVAL;
1759 }
1760
Eric Paris23bdecb2010-11-29 15:47:09 -05001761 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1762 upper->bounds - 1);
1763 BUG_ON(!upper);
1764
KaiGai Koheid9250de2008-08-28 16:35:57 +09001765 if (upper->attribute) {
1766 printk(KERN_ERR "SELinux: type %s: "
1767 "bounded by attribute %s",
1768 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001769 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001770 return -EINVAL;
1771 }
1772 }
1773
1774 return 0;
1775}
1776
1777static int policydb_bounds_sanity_check(struct policydb *p)
1778{
1779 int rc;
1780
1781 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1782 return 0;
1783
1784 rc = hashtab_map(p->p_users.table,
1785 user_bounds_sanity_check, p);
1786 if (rc)
1787 return rc;
1788
1789 rc = hashtab_map(p->p_roles.table,
1790 role_bounds_sanity_check, p);
1791 if (rc)
1792 return rc;
1793
1794 rc = hashtab_map(p->p_types.table,
1795 type_bounds_sanity_check, p);
1796 if (rc)
1797 return rc;
1798
1799 return 0;
1800}
1801
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001802u16 string_to_security_class(struct policydb *p, const char *name)
1803{
1804 struct class_datum *cladatum;
1805
1806 cladatum = hashtab_search(p->p_classes.table, name);
1807 if (!cladatum)
1808 return 0;
1809
1810 return cladatum->value;
1811}
1812
1813u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1814{
1815 struct class_datum *cladatum;
1816 struct perm_datum *perdatum = NULL;
1817 struct common_datum *comdatum;
1818
1819 if (!tclass || tclass > p->p_classes.nprim)
1820 return 0;
1821
1822 cladatum = p->class_val_to_struct[tclass-1];
1823 comdatum = cladatum->comdatum;
1824 if (comdatum)
1825 perdatum = hashtab_search(comdatum->permissions.table,
1826 name);
1827 if (!perdatum)
1828 perdatum = hashtab_search(cladatum->permissions.table,
1829 name);
1830 if (!perdatum)
1831 return 0;
1832
1833 return 1U << (perdatum->value-1);
1834}
1835
Eric Paris9ee0c822010-06-11 12:37:05 -04001836static int range_read(struct policydb *p, void *fp)
1837{
1838 struct range_trans *rt = NULL;
1839 struct mls_range *r = NULL;
1840 int i, rc;
1841 __le32 buf[2];
1842 u32 nel;
1843
1844 if (p->policyvers < POLICYDB_VERSION_MLS)
1845 return 0;
1846
1847 rc = next_entry(buf, fp, sizeof(u32));
1848 if (rc)
1849 goto out;
1850
1851 nel = le32_to_cpu(buf[0]);
1852 for (i = 0; i < nel; i++) {
1853 rc = -ENOMEM;
1854 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1855 if (!rt)
1856 goto out;
1857
1858 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1859 if (rc)
1860 goto out;
1861
1862 rt->source_type = le32_to_cpu(buf[0]);
1863 rt->target_type = le32_to_cpu(buf[1]);
1864 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1865 rc = next_entry(buf, fp, sizeof(u32));
1866 if (rc)
1867 goto out;
1868 rt->target_class = le32_to_cpu(buf[0]);
1869 } else
1870 rt->target_class = p->process_class;
1871
1872 rc = -EINVAL;
1873 if (!policydb_type_isvalid(p, rt->source_type) ||
1874 !policydb_type_isvalid(p, rt->target_type) ||
1875 !policydb_class_isvalid(p, rt->target_class))
1876 goto out;
1877
1878 rc = -ENOMEM;
1879 r = kzalloc(sizeof(*r), GFP_KERNEL);
1880 if (!r)
1881 goto out;
1882
1883 rc = mls_read_range_helper(r, fp);
1884 if (rc)
1885 goto out;
1886
1887 rc = -EINVAL;
1888 if (!mls_range_isvalid(p, r)) {
1889 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1890 goto out;
1891 }
1892
1893 rc = hashtab_insert(p->range_tr, rt, r);
1894 if (rc)
1895 goto out;
1896
1897 rt = NULL;
1898 r = NULL;
1899 }
Eric Parisbe30b162011-04-28 15:11:21 -04001900 hash_eval(p->range_tr, "rangetr");
Eric Paris9ee0c822010-06-11 12:37:05 -04001901 rc = 0;
1902out:
1903 kfree(rt);
1904 kfree(r);
1905 return rc;
1906}
1907
Eric Paris652bb9b2011-02-01 11:05:40 -05001908static int filename_trans_read(struct policydb *p, void *fp)
1909{
Eric Paris2463c26d2011-04-28 15:11:21 -04001910 struct filename_trans *ft;
1911 struct filename_trans_datum *otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001912 char *name;
Eric Paris2463c26d2011-04-28 15:11:21 -04001913 u32 nel, len;
Eric Paris652bb9b2011-02-01 11:05:40 -05001914 __le32 buf[4];
1915 int rc, i;
1916
1917 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1918 return 0;
1919
1920 rc = next_entry(buf, fp, sizeof(u32));
1921 if (rc)
Eric Paris2463c26d2011-04-28 15:11:21 -04001922 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05001923 nel = le32_to_cpu(buf[0]);
1924
Eric Paris652bb9b2011-02-01 11:05:40 -05001925 for (i = 0; i < nel; i++) {
Eric Paris2463c26d2011-04-28 15:11:21 -04001926 ft = NULL;
1927 otype = NULL;
1928 name = NULL;
1929
Eric Paris652bb9b2011-02-01 11:05:40 -05001930 rc = -ENOMEM;
1931 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1932 if (!ft)
1933 goto out;
1934
Eric Paris2463c26d2011-04-28 15:11:21 -04001935 rc = -ENOMEM;
1936 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1937 if (!otype)
1938 goto out;
Eric Paris652bb9b2011-02-01 11:05:40 -05001939
1940 /* length of the path component string */
1941 rc = next_entry(buf, fp, sizeof(u32));
1942 if (rc)
1943 goto out;
1944 len = le32_to_cpu(buf[0]);
1945
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001946 /* path component string */
1947 rc = str_read(&name, GFP_KERNEL, fp, len);
1948 if (rc)
Eric Paris652bb9b2011-02-01 11:05:40 -05001949 goto out;
1950
1951 ft->name = name;
1952
Eric Paris652bb9b2011-02-01 11:05:40 -05001953 rc = next_entry(buf, fp, sizeof(u32) * 4);
1954 if (rc)
1955 goto out;
1956
1957 ft->stype = le32_to_cpu(buf[0]);
1958 ft->ttype = le32_to_cpu(buf[1]);
1959 ft->tclass = le32_to_cpu(buf[2]);
Eric Paris2463c26d2011-04-28 15:11:21 -04001960
1961 otype->otype = le32_to_cpu(buf[3]);
Eric Paris03a4c012011-04-28 15:11:21 -04001962
1963 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1964 if (rc)
1965 goto out;
Eric Paris2463c26d2011-04-28 15:11:21 -04001966
Tetsuo Handa8ed81462014-01-06 21:28:15 +09001967 rc = hashtab_insert(p->filename_trans, ft, otype);
1968 if (rc) {
1969 /*
1970 * Do not return -EEXIST to the caller, or the system
1971 * will not boot.
1972 */
1973 if (rc != -EEXIST)
1974 goto out;
1975 /* But free memory to avoid memory leak. */
1976 kfree(ft);
1977 kfree(name);
1978 kfree(otype);
1979 }
Eric Paris652bb9b2011-02-01 11:05:40 -05001980 }
Eric Paris2463c26d2011-04-28 15:11:21 -04001981 hash_eval(p->filename_trans, "filenametr");
1982 return 0;
Eric Paris652bb9b2011-02-01 11:05:40 -05001983out:
Eric Paris2463c26d2011-04-28 15:11:21 -04001984 kfree(ft);
1985 kfree(name);
1986 kfree(otype);
1987
Eric Paris652bb9b2011-02-01 11:05:40 -05001988 return rc;
1989}
1990
Eric Parisd1b43542010-07-21 12:50:57 -04001991static int genfs_read(struct policydb *p, void *fp)
1992{
1993 int i, j, rc;
1994 u32 nel, nel2, len, len2;
1995 __le32 buf[1];
1996 struct ocontext *l, *c;
1997 struct ocontext *newc = NULL;
1998 struct genfs *genfs_p, *genfs;
1999 struct genfs *newgenfs = NULL;
2000
2001 rc = next_entry(buf, fp, sizeof(u32));
2002 if (rc)
2003 goto out;
2004 nel = le32_to_cpu(buf[0]);
2005
2006 for (i = 0; i < nel; i++) {
2007 rc = next_entry(buf, fp, sizeof(u32));
2008 if (rc)
2009 goto out;
2010 len = le32_to_cpu(buf[0]);
2011
2012 rc = -ENOMEM;
2013 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2014 if (!newgenfs)
2015 goto out;
2016
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002017 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002018 if (rc)
2019 goto out;
2020
Eric Parisd1b43542010-07-21 12:50:57 -04002021 for (genfs_p = NULL, genfs = p->genfs; genfs;
2022 genfs_p = genfs, genfs = genfs->next) {
2023 rc = -EINVAL;
2024 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2025 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2026 newgenfs->fstype);
2027 goto out;
2028 }
2029 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2030 break;
2031 }
2032 newgenfs->next = genfs;
2033 if (genfs_p)
2034 genfs_p->next = newgenfs;
2035 else
2036 p->genfs = newgenfs;
2037 genfs = newgenfs;
2038 newgenfs = NULL;
2039
2040 rc = next_entry(buf, fp, sizeof(u32));
2041 if (rc)
2042 goto out;
2043
2044 nel2 = le32_to_cpu(buf[0]);
2045 for (j = 0; j < nel2; j++) {
2046 rc = next_entry(buf, fp, sizeof(u32));
2047 if (rc)
2048 goto out;
2049 len = le32_to_cpu(buf[0]);
2050
2051 rc = -ENOMEM;
2052 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2053 if (!newc)
2054 goto out;
2055
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002056 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002057 if (rc)
2058 goto out;
Eric Parisd1b43542010-07-21 12:50:57 -04002059
2060 rc = next_entry(buf, fp, sizeof(u32));
2061 if (rc)
2062 goto out;
2063
2064 newc->v.sclass = le32_to_cpu(buf[0]);
2065 rc = context_read_and_validate(&newc->context[0], p, fp);
2066 if (rc)
2067 goto out;
2068
2069 for (l = NULL, c = genfs->head; c;
2070 l = c, c = c->next) {
2071 rc = -EINVAL;
2072 if (!strcmp(newc->u.name, c->u.name) &&
2073 (!c->v.sclass || !newc->v.sclass ||
2074 newc->v.sclass == c->v.sclass)) {
2075 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2076 genfs->fstype, c->u.name);
2077 goto out;
2078 }
2079 len = strlen(newc->u.name);
2080 len2 = strlen(c->u.name);
2081 if (len > len2)
2082 break;
2083 }
2084
2085 newc->next = c;
2086 if (l)
2087 l->next = newc;
2088 else
2089 genfs->head = newc;
2090 newc = NULL;
2091 }
2092 }
2093 rc = 0;
2094out:
2095 if (newgenfs)
2096 kfree(newgenfs->fstype);
2097 kfree(newgenfs);
2098 ocontext_destroy(newc, OCON_FSUSE);
2099
2100 return rc;
2101}
2102
Eric Paris692a8a22010-07-21 12:51:03 -04002103static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2104 void *fp)
2105{
2106 int i, j, rc;
2107 u32 nel, len;
2108 __le32 buf[3];
2109 struct ocontext *l, *c;
2110 u32 nodebuf[8];
2111
2112 for (i = 0; i < info->ocon_num; i++) {
2113 rc = next_entry(buf, fp, sizeof(u32));
2114 if (rc)
2115 goto out;
2116 nel = le32_to_cpu(buf[0]);
2117
2118 l = NULL;
2119 for (j = 0; j < nel; j++) {
2120 rc = -ENOMEM;
2121 c = kzalloc(sizeof(*c), GFP_KERNEL);
2122 if (!c)
2123 goto out;
2124 if (l)
2125 l->next = c;
2126 else
2127 p->ocontexts[i] = c;
2128 l = c;
2129
2130 switch (i) {
2131 case OCON_ISID:
2132 rc = next_entry(buf, fp, sizeof(u32));
2133 if (rc)
2134 goto out;
2135
2136 c->sid[0] = le32_to_cpu(buf[0]);
2137 rc = context_read_and_validate(&c->context[0], p, fp);
2138 if (rc)
2139 goto out;
2140 break;
2141 case OCON_FS:
2142 case OCON_NETIF:
2143 rc = next_entry(buf, fp, sizeof(u32));
2144 if (rc)
2145 goto out;
2146 len = le32_to_cpu(buf[0]);
2147
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002148 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002149 if (rc)
2150 goto out;
2151
Eric Paris692a8a22010-07-21 12:51:03 -04002152 rc = context_read_and_validate(&c->context[0], p, fp);
2153 if (rc)
2154 goto out;
2155 rc = context_read_and_validate(&c->context[1], p, fp);
2156 if (rc)
2157 goto out;
2158 break;
2159 case OCON_PORT:
2160 rc = next_entry(buf, fp, sizeof(u32)*3);
2161 if (rc)
2162 goto out;
2163 c->u.port.protocol = le32_to_cpu(buf[0]);
2164 c->u.port.low_port = le32_to_cpu(buf[1]);
2165 c->u.port.high_port = le32_to_cpu(buf[2]);
2166 rc = context_read_and_validate(&c->context[0], p, fp);
2167 if (rc)
2168 goto out;
2169 break;
2170 case OCON_NODE:
2171 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2172 if (rc)
2173 goto out;
2174 c->u.node.addr = nodebuf[0]; /* network order */
2175 c->u.node.mask = nodebuf[1]; /* network order */
2176 rc = context_read_and_validate(&c->context[0], p, fp);
2177 if (rc)
2178 goto out;
2179 break;
2180 case OCON_FSUSE:
2181 rc = next_entry(buf, fp, sizeof(u32)*2);
2182 if (rc)
2183 goto out;
2184
2185 rc = -EINVAL;
2186 c->v.behavior = le32_to_cpu(buf[0]);
David Quigleyeb9ae682013-05-22 12:50:37 -04002187 /* Determined at runtime, not in policy DB. */
2188 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2189 goto out;
2190 if (c->v.behavior > SECURITY_FS_USE_MAX)
Eric Paris692a8a22010-07-21 12:51:03 -04002191 goto out;
2192
Eric Paris692a8a22010-07-21 12:51:03 -04002193 len = le32_to_cpu(buf[1]);
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002194 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002195 if (rc)
2196 goto out;
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002197
Eric Paris692a8a22010-07-21 12:51:03 -04002198 rc = context_read_and_validate(&c->context[0], p, fp);
2199 if (rc)
2200 goto out;
2201 break;
2202 case OCON_NODE6: {
2203 int k;
2204
2205 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2206 if (rc)
2207 goto out;
2208 for (k = 0; k < 4; k++)
2209 c->u.node6.addr[k] = nodebuf[k];
2210 for (k = 0; k < 4; k++)
2211 c->u.node6.mask[k] = nodebuf[k+4];
2212 rc = context_read_and_validate(&c->context[0], p, fp);
2213 if (rc)
2214 goto out;
2215 break;
2216 }
2217 }
2218 }
2219 }
2220 rc = 0;
2221out:
2222 return rc;
2223}
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/*
2226 * Read the configuration data from a policy database binary
2227 * representation file into a policy database structure.
2228 */
2229int policydb_read(struct policydb *p, void *fp)
2230{
2231 struct role_allow *ra, *lra;
2232 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002234 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002235 u32 len, nprim, nel;
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 char *policydb_str;
2238 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 rc = policydb_init(p);
2241 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002242 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002245 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002246 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto bad;
2248
Eric Paris9398c7f2010-11-23 11:40:08 -05002249 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002250 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002251 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002253 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto bad;
2255 }
2256
Eric Paris9398c7f2010-11-23 11:40:08 -05002257 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002258 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002260 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 "match expected length %Zu\n",
2262 len, strlen(POLICYDB_STRING));
2263 goto bad;
2264 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002265
2266 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002267 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002269 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 goto bad;
2272 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002275 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002276 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 kfree(policydb_str);
2278 goto bad;
2279 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002280
2281 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002282 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002284 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 "my string %s\n", policydb_str, POLICYDB_STRING);
2286 kfree(policydb_str);
2287 goto bad;
2288 }
2289 /* Done with policydb_str. */
2290 kfree(policydb_str);
2291 policydb_str = NULL;
2292
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002293 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002295 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Eric Paris9398c7f2010-11-23 11:40:08 -05002298 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002299 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (p->policyvers < POLICYDB_VERSION_MIN ||
2301 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002302 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002303 "my version range %d-%d\n",
2304 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2305 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002308 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002309 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric Paris9398c7f2010-11-23 11:40:08 -05002311 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002313 printk(KERN_ERR "SELinux: security policydb version %d "
2314 "(MLS) not backwards compatible\n",
2315 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 goto bad;
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
Eric Paris3f120702007-09-21 14:37:10 -04002319 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2320 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Eric Paris9398c7f2010-11-23 11:40:08 -05002322 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2323 rc = ebitmap_read(&p->policycaps, fp);
2324 if (rc)
2325 goto bad;
2326 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002327
Eric Paris9398c7f2010-11-23 11:40:08 -05002328 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2329 rc = ebitmap_read(&p->permissive_map, fp);
2330 if (rc)
2331 goto bad;
2332 }
Eric Paris64dbf072008-03-31 12:17:33 +11002333
Eric Paris9398c7f2010-11-23 11:40:08 -05002334 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 info = policydb_lookup_compat(p->policyvers);
2336 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002337 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 "for version %d\n", p->policyvers);
2339 goto bad;
2340 }
2341
Eric Paris9398c7f2010-11-23 11:40:08 -05002342 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002343 if (le32_to_cpu(buf[2]) != info->sym_num ||
2344 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002345 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002346 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2347 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 info->sym_num, info->ocon_num);
2349 goto bad;
2350 }
2351
2352 for (i = 0; i < info->sym_num; i++) {
2353 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002354 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto bad;
2356 nprim = le32_to_cpu(buf[0]);
2357 nel = le32_to_cpu(buf[1]);
2358 for (j = 0; j < nel; j++) {
2359 rc = read_f[i](p, p->symtab[i].table, fp);
2360 if (rc)
2361 goto bad;
2362 }
2363
2364 p->symtab[i].nprim = nprim;
2365 }
2366
Harry Ciao1214eac2011-04-07 14:12:57 +08002367 rc = -EINVAL;
2368 p->process_class = string_to_security_class(p, "process");
2369 if (!p->process_class)
2370 goto bad;
2371
Stephen Smalley45e54212007-11-07 10:08:00 -05002372 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (rc)
2374 goto bad;
2375
2376 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2377 rc = cond_read_list(p, fp);
2378 if (rc)
2379 goto bad;
2380 }
2381
2382 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002383 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto bad;
2385 nel = le32_to_cpu(buf[0]);
2386 ltr = NULL;
2387 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002388 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002389 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002390 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002392 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002397 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002399
2400 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 tr->role = le32_to_cpu(buf[0]);
2402 tr->type = le32_to_cpu(buf[1]);
2403 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002404 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2405 rc = next_entry(buf, fp, sizeof(u32));
2406 if (rc)
2407 goto bad;
2408 tr->tclass = le32_to_cpu(buf[0]);
2409 } else
2410 tr->tclass = p->process_class;
2411
Stephen Smalley45e54212007-11-07 10:08:00 -05002412 if (!policydb_role_isvalid(p, tr->role) ||
2413 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002414 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002415 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002416 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 ltr = tr;
2418 }
2419
2420 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002421 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto bad;
2423 nel = le32_to_cpu(buf[0]);
2424 lra = NULL;
2425 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002426 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002427 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002428 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002430 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002432 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002435 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002437
2438 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 ra->role = le32_to_cpu(buf[0]);
2440 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002441 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002442 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002443 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 lra = ra;
2445 }
2446
Eric Paris652bb9b2011-02-01 11:05:40 -05002447 rc = filename_trans_read(p, fp);
2448 if (rc)
2449 goto bad;
2450
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002451 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (rc)
2453 goto bad;
2454
Eric Paris9398c7f2010-11-23 11:40:08 -05002455 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002456 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2457 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002458 if (!p->process_trans_perms)
2459 goto bad;
2460
Eric Paris692a8a22010-07-21 12:51:03 -04002461 rc = ocontext_read(p, info, fp);
2462 if (rc)
2463 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Eric Parisd1b43542010-07-21 12:50:57 -04002465 rc = genfs_read(p, fp);
2466 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric Paris9ee0c822010-06-11 12:37:05 -04002469 rc = range_read(p, fp);
2470 if (rc)
2471 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Eric Paris6371dcd2010-07-29 23:02:34 -04002473 rc = -ENOMEM;
2474 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2475 p->p_types.nprim,
2476 GFP_KERNEL | __GFP_ZERO);
2477 if (!p->type_attr_map_array)
2478 goto bad;
2479
2480 /* preallocate so we don't have to worry about the put ever failing */
Eric Paris5a3ea872011-04-28 15:55:52 -04002481 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
Eric Paris6371dcd2010-07-29 23:02:34 -04002482 GFP_KERNEL | __GFP_ZERO);
2483 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002484 goto bad;
2485
2486 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002487 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2488
2489 BUG_ON(!e);
2490 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002491 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002492 rc = ebitmap_read(e, fp);
2493 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002494 goto bad;
2495 }
2496 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002497 rc = ebitmap_set_bit(e, i, 1);
2498 if (rc)
2499 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002500 }
2501
KaiGai Koheid9250de2008-08-28 16:35:57 +09002502 rc = policydb_bounds_sanity_check(p);
2503 if (rc)
2504 goto bad;
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 rc = 0;
2507out:
2508 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 policydb_destroy(p);
2511 goto out;
2512}
Eric Pariscee74f42010-10-13 17:50:25 -04002513
2514/*
2515 * Write a MLS level structure to a policydb binary
2516 * representation file.
2517 */
2518static int mls_write_level(struct mls_level *l, void *fp)
2519{
2520 __le32 buf[1];
2521 int rc;
2522
2523 buf[0] = cpu_to_le32(l->sens);
2524 rc = put_entry(buf, sizeof(u32), 1, fp);
2525 if (rc)
2526 return rc;
2527
2528 rc = ebitmap_write(&l->cat, fp);
2529 if (rc)
2530 return rc;
2531
2532 return 0;
2533}
2534
2535/*
2536 * Write a MLS range structure to a policydb binary
2537 * representation file.
2538 */
2539static int mls_write_range_helper(struct mls_range *r, void *fp)
2540{
2541 __le32 buf[3];
2542 size_t items;
2543 int rc, eq;
2544
2545 eq = mls_level_eq(&r->level[1], &r->level[0]);
2546
2547 if (eq)
2548 items = 2;
2549 else
2550 items = 3;
2551 buf[0] = cpu_to_le32(items-1);
2552 buf[1] = cpu_to_le32(r->level[0].sens);
2553 if (!eq)
2554 buf[2] = cpu_to_le32(r->level[1].sens);
2555
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302556 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002557
2558 rc = put_entry(buf, sizeof(u32), items, fp);
2559 if (rc)
2560 return rc;
2561
2562 rc = ebitmap_write(&r->level[0].cat, fp);
2563 if (rc)
2564 return rc;
2565 if (!eq) {
2566 rc = ebitmap_write(&r->level[1].cat, fp);
2567 if (rc)
2568 return rc;
2569 }
2570
2571 return 0;
2572}
2573
2574static int sens_write(void *vkey, void *datum, void *ptr)
2575{
2576 char *key = vkey;
2577 struct level_datum *levdatum = datum;
2578 struct policy_data *pd = ptr;
2579 void *fp = pd->fp;
2580 __le32 buf[2];
2581 size_t len;
2582 int rc;
2583
2584 len = strlen(key);
2585 buf[0] = cpu_to_le32(len);
2586 buf[1] = cpu_to_le32(levdatum->isalias);
2587 rc = put_entry(buf, sizeof(u32), 2, fp);
2588 if (rc)
2589 return rc;
2590
2591 rc = put_entry(key, 1, len, fp);
2592 if (rc)
2593 return rc;
2594
2595 rc = mls_write_level(levdatum->level, fp);
2596 if (rc)
2597 return rc;
2598
2599 return 0;
2600}
2601
2602static int cat_write(void *vkey, void *datum, void *ptr)
2603{
2604 char *key = vkey;
2605 struct cat_datum *catdatum = datum;
2606 struct policy_data *pd = ptr;
2607 void *fp = pd->fp;
2608 __le32 buf[3];
2609 size_t len;
2610 int rc;
2611
2612 len = strlen(key);
2613 buf[0] = cpu_to_le32(len);
2614 buf[1] = cpu_to_le32(catdatum->value);
2615 buf[2] = cpu_to_le32(catdatum->isalias);
2616 rc = put_entry(buf, sizeof(u32), 3, fp);
2617 if (rc)
2618 return rc;
2619
2620 rc = put_entry(key, 1, len, fp);
2621 if (rc)
2622 return rc;
2623
2624 return 0;
2625}
2626
Harry Ciaoc900ff32011-03-25 13:52:00 +08002627static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002628{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002629 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002630 struct role_trans *tr;
2631 u32 buf[3];
2632 size_t nel;
2633 int rc;
2634
2635 nel = 0;
2636 for (tr = r; tr; tr = tr->next)
2637 nel++;
2638 buf[0] = cpu_to_le32(nel);
2639 rc = put_entry(buf, sizeof(u32), 1, fp);
2640 if (rc)
2641 return rc;
2642 for (tr = r; tr; tr = tr->next) {
2643 buf[0] = cpu_to_le32(tr->role);
2644 buf[1] = cpu_to_le32(tr->type);
2645 buf[2] = cpu_to_le32(tr->new_role);
2646 rc = put_entry(buf, sizeof(u32), 3, fp);
2647 if (rc)
2648 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002649 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2650 buf[0] = cpu_to_le32(tr->tclass);
2651 rc = put_entry(buf, sizeof(u32), 1, fp);
2652 if (rc)
2653 return rc;
2654 }
Eric Pariscee74f42010-10-13 17:50:25 -04002655 }
2656
2657 return 0;
2658}
2659
2660static int role_allow_write(struct role_allow *r, void *fp)
2661{
2662 struct role_allow *ra;
2663 u32 buf[2];
2664 size_t nel;
2665 int rc;
2666
2667 nel = 0;
2668 for (ra = r; ra; ra = ra->next)
2669 nel++;
2670 buf[0] = cpu_to_le32(nel);
2671 rc = put_entry(buf, sizeof(u32), 1, fp);
2672 if (rc)
2673 return rc;
2674 for (ra = r; ra; ra = ra->next) {
2675 buf[0] = cpu_to_le32(ra->role);
2676 buf[1] = cpu_to_le32(ra->new_role);
2677 rc = put_entry(buf, sizeof(u32), 2, fp);
2678 if (rc)
2679 return rc;
2680 }
2681 return 0;
2682}
2683
2684/*
2685 * Write a security context structure
2686 * to a policydb binary representation file.
2687 */
2688static int context_write(struct policydb *p, struct context *c,
2689 void *fp)
2690{
2691 int rc;
2692 __le32 buf[3];
2693
2694 buf[0] = cpu_to_le32(c->user);
2695 buf[1] = cpu_to_le32(c->role);
2696 buf[2] = cpu_to_le32(c->type);
2697
2698 rc = put_entry(buf, sizeof(u32), 3, fp);
2699 if (rc)
2700 return rc;
2701
2702 rc = mls_write_range_helper(&c->range, fp);
2703 if (rc)
2704 return rc;
2705
2706 return 0;
2707}
2708
2709/*
2710 * The following *_write functions are used to
2711 * write the symbol data to a policy database
2712 * binary representation file.
2713 */
2714
2715static int perm_write(void *vkey, void *datum, void *fp)
2716{
2717 char *key = vkey;
2718 struct perm_datum *perdatum = datum;
2719 __le32 buf[2];
2720 size_t len;
2721 int rc;
2722
2723 len = strlen(key);
2724 buf[0] = cpu_to_le32(len);
2725 buf[1] = cpu_to_le32(perdatum->value);
2726 rc = put_entry(buf, sizeof(u32), 2, fp);
2727 if (rc)
2728 return rc;
2729
2730 rc = put_entry(key, 1, len, fp);
2731 if (rc)
2732 return rc;
2733
2734 return 0;
2735}
2736
2737static int common_write(void *vkey, void *datum, void *ptr)
2738{
2739 char *key = vkey;
2740 struct common_datum *comdatum = datum;
2741 struct policy_data *pd = ptr;
2742 void *fp = pd->fp;
2743 __le32 buf[4];
2744 size_t len;
2745 int rc;
2746
2747 len = strlen(key);
2748 buf[0] = cpu_to_le32(len);
2749 buf[1] = cpu_to_le32(comdatum->value);
2750 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2751 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2752 rc = put_entry(buf, sizeof(u32), 4, fp);
2753 if (rc)
2754 return rc;
2755
2756 rc = put_entry(key, 1, len, fp);
2757 if (rc)
2758 return rc;
2759
2760 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2761 if (rc)
2762 return rc;
2763
2764 return 0;
2765}
2766
Richard Hainesa660bec2013-11-19 17:34:23 -05002767static int type_set_write(struct type_set *t, void *fp)
2768{
2769 int rc;
2770 __le32 buf[1];
2771
2772 if (ebitmap_write(&t->types, fp))
2773 return -EINVAL;
2774 if (ebitmap_write(&t->negset, fp))
2775 return -EINVAL;
2776
2777 buf[0] = cpu_to_le32(t->flags);
2778 rc = put_entry(buf, sizeof(u32), 1, fp);
2779 if (rc)
2780 return -EINVAL;
2781
2782 return 0;
2783}
2784
Eric Pariscee74f42010-10-13 17:50:25 -04002785static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2786 void *fp)
2787{
2788 struct constraint_node *c;
2789 struct constraint_expr *e;
2790 __le32 buf[3];
2791 u32 nel;
2792 int rc;
2793
2794 for (c = node; c; c = c->next) {
2795 nel = 0;
2796 for (e = c->expr; e; e = e->next)
2797 nel++;
2798 buf[0] = cpu_to_le32(c->permissions);
2799 buf[1] = cpu_to_le32(nel);
2800 rc = put_entry(buf, sizeof(u32), 2, fp);
2801 if (rc)
2802 return rc;
2803 for (e = c->expr; e; e = e->next) {
2804 buf[0] = cpu_to_le32(e->expr_type);
2805 buf[1] = cpu_to_le32(e->attr);
2806 buf[2] = cpu_to_le32(e->op);
2807 rc = put_entry(buf, sizeof(u32), 3, fp);
2808 if (rc)
2809 return rc;
2810
2811 switch (e->expr_type) {
2812 case CEXPR_NAMES:
2813 rc = ebitmap_write(&e->names, fp);
2814 if (rc)
2815 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05002816 if (p->policyvers >=
2817 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2818 rc = type_set_write(e->type_names, fp);
2819 if (rc)
2820 return rc;
2821 }
Eric Pariscee74f42010-10-13 17:50:25 -04002822 break;
2823 default:
2824 break;
2825 }
2826 }
2827 }
2828
2829 return 0;
2830}
2831
2832static int class_write(void *vkey, void *datum, void *ptr)
2833{
2834 char *key = vkey;
2835 struct class_datum *cladatum = datum;
2836 struct policy_data *pd = ptr;
2837 void *fp = pd->fp;
2838 struct policydb *p = pd->p;
2839 struct constraint_node *c;
2840 __le32 buf[6];
2841 u32 ncons;
2842 size_t len, len2;
2843 int rc;
2844
2845 len = strlen(key);
2846 if (cladatum->comkey)
2847 len2 = strlen(cladatum->comkey);
2848 else
2849 len2 = 0;
2850
2851 ncons = 0;
2852 for (c = cladatum->constraints; c; c = c->next)
2853 ncons++;
2854
2855 buf[0] = cpu_to_le32(len);
2856 buf[1] = cpu_to_le32(len2);
2857 buf[2] = cpu_to_le32(cladatum->value);
2858 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2859 if (cladatum->permissions.table)
2860 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2861 else
2862 buf[4] = 0;
2863 buf[5] = cpu_to_le32(ncons);
2864 rc = put_entry(buf, sizeof(u32), 6, fp);
2865 if (rc)
2866 return rc;
2867
2868 rc = put_entry(key, 1, len, fp);
2869 if (rc)
2870 return rc;
2871
2872 if (cladatum->comkey) {
2873 rc = put_entry(cladatum->comkey, 1, len2, fp);
2874 if (rc)
2875 return rc;
2876 }
2877
2878 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2879 if (rc)
2880 return rc;
2881
2882 rc = write_cons_helper(p, cladatum->constraints, fp);
2883 if (rc)
2884 return rc;
2885
2886 /* write out the validatetrans rule */
2887 ncons = 0;
2888 for (c = cladatum->validatetrans; c; c = c->next)
2889 ncons++;
2890
2891 buf[0] = cpu_to_le32(ncons);
2892 rc = put_entry(buf, sizeof(u32), 1, fp);
2893 if (rc)
2894 return rc;
2895
2896 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2897 if (rc)
2898 return rc;
2899
Eric Parisaa893262012-03-20 14:35:12 -04002900 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2901 buf[0] = cpu_to_le32(cladatum->default_user);
2902 buf[1] = cpu_to_le32(cladatum->default_role);
2903 buf[2] = cpu_to_le32(cladatum->default_range);
2904
2905 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2906 if (rc)
2907 return rc;
2908 }
2909
Eric Pariseed77952012-03-20 14:35:12 -04002910 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2911 buf[0] = cpu_to_le32(cladatum->default_type);
2912 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2913 if (rc)
2914 return rc;
2915 }
2916
Eric Pariscee74f42010-10-13 17:50:25 -04002917 return 0;
2918}
2919
2920static int role_write(void *vkey, void *datum, void *ptr)
2921{
2922 char *key = vkey;
2923 struct role_datum *role = datum;
2924 struct policy_data *pd = ptr;
2925 void *fp = pd->fp;
2926 struct policydb *p = pd->p;
2927 __le32 buf[3];
2928 size_t items, len;
2929 int rc;
2930
2931 len = strlen(key);
2932 items = 0;
2933 buf[items++] = cpu_to_le32(len);
2934 buf[items++] = cpu_to_le32(role->value);
2935 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2936 buf[items++] = cpu_to_le32(role->bounds);
2937
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302938 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002939
2940 rc = put_entry(buf, sizeof(u32), items, fp);
2941 if (rc)
2942 return rc;
2943
2944 rc = put_entry(key, 1, len, fp);
2945 if (rc)
2946 return rc;
2947
2948 rc = ebitmap_write(&role->dominates, fp);
2949 if (rc)
2950 return rc;
2951
2952 rc = ebitmap_write(&role->types, fp);
2953 if (rc)
2954 return rc;
2955
2956 return 0;
2957}
2958
2959static int type_write(void *vkey, void *datum, void *ptr)
2960{
2961 char *key = vkey;
2962 struct type_datum *typdatum = datum;
2963 struct policy_data *pd = ptr;
2964 struct policydb *p = pd->p;
2965 void *fp = pd->fp;
2966 __le32 buf[4];
2967 int rc;
2968 size_t items, len;
2969
2970 len = strlen(key);
2971 items = 0;
2972 buf[items++] = cpu_to_le32(len);
2973 buf[items++] = cpu_to_le32(typdatum->value);
2974 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2975 u32 properties = 0;
2976
2977 if (typdatum->primary)
2978 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2979
2980 if (typdatum->attribute)
2981 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2982
2983 buf[items++] = cpu_to_le32(properties);
2984 buf[items++] = cpu_to_le32(typdatum->bounds);
2985 } else {
2986 buf[items++] = cpu_to_le32(typdatum->primary);
2987 }
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302988 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002989 rc = put_entry(buf, sizeof(u32), items, fp);
2990 if (rc)
2991 return rc;
2992
2993 rc = put_entry(key, 1, len, fp);
2994 if (rc)
2995 return rc;
2996
2997 return 0;
2998}
2999
3000static int user_write(void *vkey, void *datum, void *ptr)
3001{
3002 char *key = vkey;
3003 struct user_datum *usrdatum = datum;
3004 struct policy_data *pd = ptr;
3005 struct policydb *p = pd->p;
3006 void *fp = pd->fp;
3007 __le32 buf[3];
3008 size_t items, len;
3009 int rc;
3010
3011 len = strlen(key);
3012 items = 0;
3013 buf[items++] = cpu_to_le32(len);
3014 buf[items++] = cpu_to_le32(usrdatum->value);
3015 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3016 buf[items++] = cpu_to_le32(usrdatum->bounds);
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303017 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003018 rc = put_entry(buf, sizeof(u32), items, fp);
3019 if (rc)
3020 return rc;
3021
3022 rc = put_entry(key, 1, len, fp);
3023 if (rc)
3024 return rc;
3025
3026 rc = ebitmap_write(&usrdatum->roles, fp);
3027 if (rc)
3028 return rc;
3029
3030 rc = mls_write_range_helper(&usrdatum->range, fp);
3031 if (rc)
3032 return rc;
3033
3034 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3035 if (rc)
3036 return rc;
3037
3038 return 0;
3039}
3040
3041static int (*write_f[SYM_NUM]) (void *key, void *datum,
3042 void *datap) =
3043{
3044 common_write,
3045 class_write,
3046 role_write,
3047 type_write,
3048 user_write,
3049 cond_write_bool,
3050 sens_write,
3051 cat_write,
3052};
3053
3054static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3055 void *fp)
3056{
3057 unsigned int i, j, rc;
3058 size_t nel, len;
3059 __le32 buf[3];
3060 u32 nodebuf[8];
3061 struct ocontext *c;
3062 for (i = 0; i < info->ocon_num; i++) {
3063 nel = 0;
3064 for (c = p->ocontexts[i]; c; c = c->next)
3065 nel++;
3066 buf[0] = cpu_to_le32(nel);
3067 rc = put_entry(buf, sizeof(u32), 1, fp);
3068 if (rc)
3069 return rc;
3070 for (c = p->ocontexts[i]; c; c = c->next) {
3071 switch (i) {
3072 case OCON_ISID:
3073 buf[0] = cpu_to_le32(c->sid[0]);
3074 rc = put_entry(buf, sizeof(u32), 1, fp);
3075 if (rc)
3076 return rc;
3077 rc = context_write(p, &c->context[0], fp);
3078 if (rc)
3079 return rc;
3080 break;
3081 case OCON_FS:
3082 case OCON_NETIF:
3083 len = strlen(c->u.name);
3084 buf[0] = cpu_to_le32(len);
3085 rc = put_entry(buf, sizeof(u32), 1, fp);
3086 if (rc)
3087 return rc;
3088 rc = put_entry(c->u.name, 1, len, fp);
3089 if (rc)
3090 return rc;
3091 rc = context_write(p, &c->context[0], fp);
3092 if (rc)
3093 return rc;
3094 rc = context_write(p, &c->context[1], fp);
3095 if (rc)
3096 return rc;
3097 break;
3098 case OCON_PORT:
3099 buf[0] = cpu_to_le32(c->u.port.protocol);
3100 buf[1] = cpu_to_le32(c->u.port.low_port);
3101 buf[2] = cpu_to_le32(c->u.port.high_port);
3102 rc = put_entry(buf, sizeof(u32), 3, fp);
3103 if (rc)
3104 return rc;
3105 rc = context_write(p, &c->context[0], fp);
3106 if (rc)
3107 return rc;
3108 break;
3109 case OCON_NODE:
3110 nodebuf[0] = c->u.node.addr; /* network order */
3111 nodebuf[1] = c->u.node.mask; /* network order */
3112 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3113 if (rc)
3114 return rc;
3115 rc = context_write(p, &c->context[0], fp);
3116 if (rc)
3117 return rc;
3118 break;
3119 case OCON_FSUSE:
3120 buf[0] = cpu_to_le32(c->v.behavior);
3121 len = strlen(c->u.name);
3122 buf[1] = cpu_to_le32(len);
3123 rc = put_entry(buf, sizeof(u32), 2, fp);
3124 if (rc)
3125 return rc;
3126 rc = put_entry(c->u.name, 1, len, fp);
3127 if (rc)
3128 return rc;
3129 rc = context_write(p, &c->context[0], fp);
3130 if (rc)
3131 return rc;
3132 break;
3133 case OCON_NODE6:
3134 for (j = 0; j < 4; j++)
3135 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3136 for (j = 0; j < 4; j++)
3137 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3138 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3139 if (rc)
3140 return rc;
3141 rc = context_write(p, &c->context[0], fp);
3142 if (rc)
3143 return rc;
3144 break;
3145 }
3146 }
3147 }
3148 return 0;
3149}
3150
3151static int genfs_write(struct policydb *p, void *fp)
3152{
3153 struct genfs *genfs;
3154 struct ocontext *c;
3155 size_t len;
3156 __le32 buf[1];
3157 int rc;
3158
3159 len = 0;
3160 for (genfs = p->genfs; genfs; genfs = genfs->next)
3161 len++;
3162 buf[0] = cpu_to_le32(len);
3163 rc = put_entry(buf, sizeof(u32), 1, fp);
3164 if (rc)
3165 return rc;
3166 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3167 len = strlen(genfs->fstype);
3168 buf[0] = cpu_to_le32(len);
3169 rc = put_entry(buf, sizeof(u32), 1, fp);
3170 if (rc)
3171 return rc;
3172 rc = put_entry(genfs->fstype, 1, len, fp);
3173 if (rc)
3174 return rc;
3175 len = 0;
3176 for (c = genfs->head; c; c = c->next)
3177 len++;
3178 buf[0] = cpu_to_le32(len);
3179 rc = put_entry(buf, sizeof(u32), 1, fp);
3180 if (rc)
3181 return rc;
3182 for (c = genfs->head; c; c = c->next) {
3183 len = strlen(c->u.name);
3184 buf[0] = cpu_to_le32(len);
3185 rc = put_entry(buf, sizeof(u32), 1, fp);
3186 if (rc)
3187 return rc;
3188 rc = put_entry(c->u.name, 1, len, fp);
3189 if (rc)
3190 return rc;
3191 buf[0] = cpu_to_le32(c->v.sclass);
3192 rc = put_entry(buf, sizeof(u32), 1, fp);
3193 if (rc)
3194 return rc;
3195 rc = context_write(p, &c->context[0], fp);
3196 if (rc)
3197 return rc;
3198 }
3199 }
3200 return 0;
3201}
3202
Eric Paris3f058ef2011-04-28 15:11:21 -04003203static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003204{
3205 int *cnt = ptr;
3206 *cnt = *cnt + 1;
3207
3208 return 0;
3209}
3210
3211static int range_write_helper(void *key, void *data, void *ptr)
3212{
3213 __le32 buf[2];
3214 struct range_trans *rt = key;
3215 struct mls_range *r = data;
3216 struct policy_data *pd = ptr;
3217 void *fp = pd->fp;
3218 struct policydb *p = pd->p;
3219 int rc;
3220
3221 buf[0] = cpu_to_le32(rt->source_type);
3222 buf[1] = cpu_to_le32(rt->target_type);
3223 rc = put_entry(buf, sizeof(u32), 2, fp);
3224 if (rc)
3225 return rc;
3226 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3227 buf[0] = cpu_to_le32(rt->target_class);
3228 rc = put_entry(buf, sizeof(u32), 1, fp);
3229 if (rc)
3230 return rc;
3231 }
3232 rc = mls_write_range_helper(r, fp);
3233 if (rc)
3234 return rc;
3235
3236 return 0;
3237}
3238
3239static int range_write(struct policydb *p, void *fp)
3240{
Eric Pariscee74f42010-10-13 17:50:25 -04003241 __le32 buf[1];
Eric Parisb1380042013-07-23 17:38:42 -04003242 int rc, nel;
Eric Pariscee74f42010-10-13 17:50:25 -04003243 struct policy_data pd;
3244
3245 pd.p = p;
3246 pd.fp = fp;
3247
3248 /* count the number of entries in the hashtab */
3249 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003250 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003251 if (rc)
3252 return rc;
3253
3254 buf[0] = cpu_to_le32(nel);
3255 rc = put_entry(buf, sizeof(u32), 1, fp);
3256 if (rc)
3257 return rc;
3258
3259 /* actually write all of the entries */
3260 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3261 if (rc)
3262 return rc;
3263
3264 return 0;
3265}
3266
Eric Paris2463c26d2011-04-28 15:11:21 -04003267static int filename_write_helper(void *key, void *data, void *ptr)
3268{
3269 __le32 buf[4];
3270 struct filename_trans *ft = key;
3271 struct filename_trans_datum *otype = data;
3272 void *fp = ptr;
3273 int rc;
3274 u32 len;
3275
3276 len = strlen(ft->name);
3277 buf[0] = cpu_to_le32(len);
3278 rc = put_entry(buf, sizeof(u32), 1, fp);
3279 if (rc)
3280 return rc;
3281
3282 rc = put_entry(ft->name, sizeof(char), len, fp);
3283 if (rc)
3284 return rc;
3285
Eric Paris9085a642014-02-20 10:56:45 -05003286 buf[0] = cpu_to_le32(ft->stype);
3287 buf[1] = cpu_to_le32(ft->ttype);
3288 buf[2] = cpu_to_le32(ft->tclass);
3289 buf[3] = cpu_to_le32(otype->otype);
Eric Paris2463c26d2011-04-28 15:11:21 -04003290
3291 rc = put_entry(buf, sizeof(u32), 4, fp);
3292 if (rc)
3293 return rc;
3294
3295 return 0;
3296}
3297
Eric Paris652bb9b2011-02-01 11:05:40 -05003298static int filename_trans_write(struct policydb *p, void *fp)
3299{
Eric Paris2463c26d2011-04-28 15:11:21 -04003300 u32 nel;
3301 __le32 buf[1];
Eric Paris652bb9b2011-02-01 11:05:40 -05003302 int rc;
3303
Roy.Lided50982011-05-20 10:38:06 +08003304 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3305 return 0;
3306
Eric Paris2463c26d2011-04-28 15:11:21 -04003307 nel = 0;
3308 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3309 if (rc)
3310 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003311
3312 buf[0] = cpu_to_le32(nel);
3313 rc = put_entry(buf, sizeof(u32), 1, fp);
3314 if (rc)
3315 return rc;
3316
Eric Paris2463c26d2011-04-28 15:11:21 -04003317 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3318 if (rc)
3319 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003320
Eric Paris652bb9b2011-02-01 11:05:40 -05003321 return 0;
3322}
Eric Paris2463c26d2011-04-28 15:11:21 -04003323
Eric Pariscee74f42010-10-13 17:50:25 -04003324/*
3325 * Write the configuration data in a policy database
3326 * structure to a policy database binary representation
3327 * file.
3328 */
3329int policydb_write(struct policydb *p, void *fp)
3330{
3331 unsigned int i, num_syms;
3332 int rc;
3333 __le32 buf[4];
3334 u32 config;
3335 size_t len;
3336 struct policydb_compat_info *info;
3337
3338 /*
3339 * refuse to write policy older than compressed avtab
3340 * to simplify the writer. There are other tests dropped
3341 * since we assume this throughout the writer code. Be
3342 * careful if you ever try to remove this restriction
3343 */
3344 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3345 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3346 " Because it is less than version %d\n", p->policyvers,
3347 POLICYDB_VERSION_AVTAB);
3348 return -EINVAL;
3349 }
3350
3351 config = 0;
3352 if (p->mls_enabled)
3353 config |= POLICYDB_CONFIG_MLS;
3354
3355 if (p->reject_unknown)
3356 config |= REJECT_UNKNOWN;
3357 if (p->allow_unknown)
3358 config |= ALLOW_UNKNOWN;
3359
3360 /* Write the magic number and string identifiers. */
3361 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3362 len = strlen(POLICYDB_STRING);
3363 buf[1] = cpu_to_le32(len);
3364 rc = put_entry(buf, sizeof(u32), 2, fp);
3365 if (rc)
3366 return rc;
3367 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3368 if (rc)
3369 return rc;
3370
3371 /* Write the version, config, and table sizes. */
3372 info = policydb_lookup_compat(p->policyvers);
3373 if (!info) {
3374 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3375 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003376 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003377 }
3378
3379 buf[0] = cpu_to_le32(p->policyvers);
3380 buf[1] = cpu_to_le32(config);
3381 buf[2] = cpu_to_le32(info->sym_num);
3382 buf[3] = cpu_to_le32(info->ocon_num);
3383
3384 rc = put_entry(buf, sizeof(u32), 4, fp);
3385 if (rc)
3386 return rc;
3387
3388 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3389 rc = ebitmap_write(&p->policycaps, fp);
3390 if (rc)
3391 return rc;
3392 }
3393
3394 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3395 rc = ebitmap_write(&p->permissive_map, fp);
3396 if (rc)
3397 return rc;
3398 }
3399
3400 num_syms = info->sym_num;
3401 for (i = 0; i < num_syms; i++) {
3402 struct policy_data pd;
3403
3404 pd.fp = fp;
3405 pd.p = p;
3406
3407 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3408 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3409
3410 rc = put_entry(buf, sizeof(u32), 2, fp);
3411 if (rc)
3412 return rc;
3413 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3414 if (rc)
3415 return rc;
3416 }
3417
3418 rc = avtab_write(p, &p->te_avtab, fp);
3419 if (rc)
3420 return rc;
3421
3422 rc = cond_write_list(p, p->cond_list, fp);
3423 if (rc)
3424 return rc;
3425
Harry Ciaoc900ff32011-03-25 13:52:00 +08003426 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003427 if (rc)
3428 return rc;
3429
3430 rc = role_allow_write(p->role_allow, fp);
3431 if (rc)
3432 return rc;
3433
Eric Paris652bb9b2011-02-01 11:05:40 -05003434 rc = filename_trans_write(p, fp);
3435 if (rc)
3436 return rc;
3437
Eric Pariscee74f42010-10-13 17:50:25 -04003438 rc = ocontext_write(p, info, fp);
3439 if (rc)
3440 return rc;
3441
3442 rc = genfs_write(p, fp);
3443 if (rc)
3444 return rc;
3445
3446 rc = range_write(p, fp);
3447 if (rc)
3448 return rc;
3449
3450 for (i = 0; i < p->p_types.nprim; i++) {
3451 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3452
3453 BUG_ON(!e);
3454 rc = ebitmap_write(e, fp);
3455 if (rc)
3456 return rc;
3457 }
3458
3459 return 0;
3460}