blob: 9c5cdc2caaef7637b4bf9178a8efcbe69558c6b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the policy database.
3 *
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
6
7/*
8 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 *
10 * Support for enhanced MLS infrastructure.
11 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
Eric Paris2ced3df2008-04-17 13:37:12 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Paul Moore82c21bf2011-08-01 11:10:33 +000016 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -050017 *
18 * Added support for the policy capability bitmap
19 *
20 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
22 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
23 * This program is free software; you can redistribute it and/or modify
Eric Paris2ced3df2008-04-17 13:37:12 -040024 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the Free Software Foundation, version 2.
26 */
27
28#include <linux/kernel.h>
Eric Paris9dc99782007-06-04 17:41:22 -040029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/string.h>
32#include <linux/errno.h>
KaiGai Koheid9250de2008-08-28 16:35:57 +090033#include <linux/audit.h>
Eric Paris6371dcd2010-07-29 23:02:34 -040034#include <linux/flex_array.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "security.h"
36
37#include "policydb.h"
38#include "conditional.h"
39#include "mls.h"
Eric Pariscee74f42010-10-13 17:50:25 -040040#include "services.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define _DEBUG_HASHES
43
44#ifdef DEBUG_HASHES
Stephen Hemminger634a5392010-03-04 21:59:03 -080045static const char *symtab_name[SYM_NUM] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "common prefixes",
47 "classes",
48 "roles",
49 "types",
50 "users",
51 "bools",
52 "levels",
53 "categories",
54};
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int symtab_sizes[SYM_NUM] = {
58 2,
59 32,
60 16,
61 512,
62 128,
63 16,
64 16,
65 16,
66};
67
68struct policydb_compat_info {
69 int version;
70 int sym_num;
71 int ocon_num;
72};
73
74/* These need to be updated if SYM_NUM or OCON_NUM changes */
75static struct policydb_compat_info policydb_compat[] = {
76 {
Eric Paris2ced3df2008-04-17 13:37:12 -040077 .version = POLICYDB_VERSION_BASE,
78 .sym_num = SYM_NUM - 3,
79 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 },
81 {
Eric Paris2ced3df2008-04-17 13:37:12 -040082 .version = POLICYDB_VERSION_BOOL,
83 .sym_num = SYM_NUM - 2,
84 .ocon_num = OCON_NUM - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 },
86 {
Eric Paris2ced3df2008-04-17 13:37:12 -040087 .version = POLICYDB_VERSION_IPV6,
88 .sym_num = SYM_NUM - 2,
89 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 },
91 {
Eric Paris2ced3df2008-04-17 13:37:12 -040092 .version = POLICYDB_VERSION_NLCLASS,
93 .sym_num = SYM_NUM - 2,
94 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 },
96 {
Eric Paris2ced3df2008-04-17 13:37:12 -040097 .version = POLICYDB_VERSION_MLS,
98 .sym_num = SYM_NUM,
99 .ocon_num = OCON_NUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 },
Stephen Smalley782ebb92005-09-03 15:55:16 -0700101 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400102 .version = POLICYDB_VERSION_AVTAB,
103 .sym_num = SYM_NUM,
104 .ocon_num = OCON_NUM,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700105 },
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700106 {
Eric Paris2ced3df2008-04-17 13:37:12 -0400107 .version = POLICYDB_VERSION_RANGETRANS,
108 .sym_num = SYM_NUM,
109 .ocon_num = OCON_NUM,
Darrel Goeddelf3f87712006-09-25 23:31:59 -0700110 },
Paul Moore3bb56b22008-01-29 08:38:19 -0500111 {
112 .version = POLICYDB_VERSION_POLCAP,
113 .sym_num = SYM_NUM,
114 .ocon_num = OCON_NUM,
Eric Paris64dbf072008-03-31 12:17:33 +1100115 },
116 {
117 .version = POLICYDB_VERSION_PERMISSIVE,
118 .sym_num = SYM_NUM,
119 .ocon_num = OCON_NUM,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900120 },
121 {
122 .version = POLICYDB_VERSION_BOUNDARY,
123 .sym_num = SYM_NUM,
124 .ocon_num = OCON_NUM,
125 },
Eric Paris652bb9b2011-02-01 11:05:40 -0500126 {
127 .version = POLICYDB_VERSION_FILENAME_TRANS,
128 .sym_num = SYM_NUM,
129 .ocon_num = OCON_NUM,
130 },
Harry Ciao80239762011-03-25 13:51:56 +0800131 {
132 .version = POLICYDB_VERSION_ROLETRANS,
133 .sym_num = SYM_NUM,
134 .ocon_num = OCON_NUM,
135 },
Eric Parisaa893262012-03-20 14:35:12 -0400136 {
137 .version = POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
138 .sym_num = SYM_NUM,
139 .ocon_num = OCON_NUM,
140 },
Eric Pariseed77952012-03-20 14:35:12 -0400141 {
142 .version = POLICYDB_VERSION_DEFAULT_TYPE,
143 .sym_num = SYM_NUM,
144 .ocon_num = OCON_NUM,
145 },
Richard Hainesa660bec2013-11-19 17:34:23 -0500146 {
147 .version = POLICYDB_VERSION_CONSTRAINT_NAMES,
148 .sym_num = SYM_NUM,
149 .ocon_num = OCON_NUM,
150 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153static struct policydb_compat_info *policydb_lookup_compat(int version)
154{
155 int i;
156 struct policydb_compat_info *info = NULL;
157
Tobias Klauser32725ad2006-01-06 00:11:23 -0800158 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (policydb_compat[i].version == version) {
160 info = &policydb_compat[i];
161 break;
162 }
163 }
164 return info;
165}
166
167/*
168 * Initialize the role table.
169 */
170static int roles_init(struct policydb *p)
171{
172 char *key = NULL;
173 int rc;
174 struct role_datum *role;
175
Eric Paris9398c7f2010-11-23 11:40:08 -0500176 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800177 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500178 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500180
181 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500183 if (role->value != OBJECT_R_VAL)
184 goto out;
185
186 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200187 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500188 if (!key)
189 goto out;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 rc = hashtab_insert(p->p_roles.table, key, role);
192 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Eric Paris9398c7f2010-11-23 11:40:08 -0500195 return 0;
196out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500199 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Eric Paris2463c26d2011-04-28 15:11:21 -0400202static u32 filenametr_hash(struct hashtab *h, const void *k)
203{
204 const struct filename_trans *ft = k;
205 unsigned long hash;
206 unsigned int byte_num;
207 unsigned char focus;
208
209 hash = ft->stype ^ ft->ttype ^ ft->tclass;
210
211 byte_num = 0;
212 while ((focus = ft->name[byte_num++]))
213 hash = partial_name_hash(focus, hash);
214 return hash & (h->size - 1);
215}
216
217static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
218{
219 const struct filename_trans *ft1 = k1;
220 const struct filename_trans *ft2 = k2;
221 int v;
222
223 v = ft1->stype - ft2->stype;
224 if (v)
225 return v;
226
227 v = ft1->ttype - ft2->ttype;
228 if (v)
229 return v;
230
231 v = ft1->tclass - ft2->tclass;
232 if (v)
233 return v;
234
235 return strcmp(ft1->name, ft2->name);
236
237}
238
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500239static u32 rangetr_hash(struct hashtab *h, const void *k)
240{
241 const struct range_trans *key = k;
242 return (key->source_type + (key->target_type << 3) +
243 (key->target_class << 5)) & (h->size - 1);
244}
245
246static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
247{
248 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400249 int v;
250
251 v = key1->source_type - key2->source_type;
252 if (v)
253 return v;
254
255 v = key1->target_type - key2->target_type;
256 if (v)
257 return v;
258
259 v = key1->target_class - key2->target_class;
260
261 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * Initialize a policy database structure.
266 */
267static int policydb_init(struct policydb *p)
268{
269 int i, rc;
270
271 memset(p, 0, sizeof(*p));
272
273 for (i = 0; i < SYM_NUM; i++) {
274 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
275 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 rc = avtab_init(&p->te_avtab);
280 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 rc = roles_init(p);
284 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 rc = cond_policydb_init(p);
288 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Eric Paris2463c26d2011-04-28 15:11:21 -0400291 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
292 if (!p->filename_trans)
293 goto out;
294
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500295 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
296 if (!p->range_tr)
Eric Paris9398c7f2010-11-23 11:40:08 -0500297 goto out;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500298
Eric Paris03a4c012011-04-28 15:11:21 -0400299 ebitmap_init(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500300 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100301 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500302
Eric Paris9398c7f2010-11-23 11:40:08 -0500303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304out:
Eric Paris2463c26d2011-04-28 15:11:21 -0400305 hashtab_destroy(p->filename_trans);
306 hashtab_destroy(p->range_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 for (i = 0; i < SYM_NUM; i++)
308 hashtab_destroy(p->symtab[i].table);
Eric Paris9398c7f2010-11-23 11:40:08 -0500309 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/*
313 * The following *_index functions are used to
314 * define the val_to_name and val_to_struct arrays
315 * in a policy database structure. The val_to_name
316 * arrays are used when converting security context
317 * structures into string representations. The
318 * val_to_struct arrays are used when the attributes
319 * of a class, role, or user are needed.
320 */
321
322static int common_index(void *key, void *datum, void *datap)
323{
324 struct policydb *p;
325 struct common_datum *comdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500326 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 comdatum = datum;
329 p = datap;
330 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
331 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500332
333 fa = p->sym_val_to_name[SYM_COMMONS];
334 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
335 GFP_KERNEL | __GFP_ZERO))
336 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340static int class_index(void *key, void *datum, void *datap)
341{
342 struct policydb *p;
343 struct class_datum *cladatum;
Eric Parisac76c052010-11-29 15:47:09 -0500344 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 cladatum = datum;
347 p = datap;
348 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
349 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500350 fa = p->sym_val_to_name[SYM_CLASSES];
351 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
352 GFP_KERNEL | __GFP_ZERO))
353 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 p->class_val_to_struct[cladatum->value - 1] = cladatum;
355 return 0;
356}
357
358static int role_index(void *key, void *datum, void *datap)
359{
360 struct policydb *p;
361 struct role_datum *role;
Eric Parisac76c052010-11-29 15:47:09 -0500362 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 role = datum;
365 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900366 if (!role->value
367 || role->value > p->p_roles.nprim
368 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500370
371 fa = p->sym_val_to_name[SYM_ROLES];
372 if (flex_array_put_ptr(fa, role->value - 1, key,
373 GFP_KERNEL | __GFP_ZERO))
374 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 p->role_val_to_struct[role->value - 1] = role;
376 return 0;
377}
378
379static int type_index(void *key, void *datum, void *datap)
380{
381 struct policydb *p;
382 struct type_datum *typdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500383 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 typdatum = datum;
386 p = datap;
387
388 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900389 if (!typdatum->value
390 || typdatum->value > p->p_types.nprim
391 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500393 fa = p->sym_val_to_name[SYM_TYPES];
394 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
395 GFP_KERNEL | __GFP_ZERO))
396 BUG();
397
398 fa = p->type_val_to_struct_array;
399 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500400 GFP_KERNEL | __GFP_ZERO))
401 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 return 0;
405}
406
407static int user_index(void *key, void *datum, void *datap)
408{
409 struct policydb *p;
410 struct user_datum *usrdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500411 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 usrdatum = datum;
414 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900415 if (!usrdatum->value
416 || usrdatum->value > p->p_users.nprim
417 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500419
420 fa = p->sym_val_to_name[SYM_USERS];
421 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
422 GFP_KERNEL | __GFP_ZERO))
423 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
425 return 0;
426}
427
428static int sens_index(void *key, void *datum, void *datap)
429{
430 struct policydb *p;
431 struct level_datum *levdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500432 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 levdatum = datum;
435 p = datap;
436
437 if (!levdatum->isalias) {
438 if (!levdatum->level->sens ||
439 levdatum->level->sens > p->p_levels.nprim)
440 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500441 fa = p->sym_val_to_name[SYM_LEVELS];
442 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
443 GFP_KERNEL | __GFP_ZERO))
444 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 return 0;
448}
449
450static int cat_index(void *key, void *datum, void *datap)
451{
452 struct policydb *p;
453 struct cat_datum *catdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500454 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 catdatum = datum;
457 p = datap;
458
459 if (!catdatum->isalias) {
460 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
461 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500462 fa = p->sym_val_to_name[SYM_CATS];
463 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
464 GFP_KERNEL | __GFP_ZERO))
465 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 return 0;
469}
470
471static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
472{
473 common_index,
474 class_index,
475 role_index,
476 type_index,
477 user_index,
478 cond_index_bool,
479 sens_index,
480 cat_index,
481};
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef DEBUG_HASHES
Eric Parisbe30b162011-04-28 15:11:21 -0400484static void hash_eval(struct hashtab *h, const char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500485{
486 struct hashtab_info info;
487
488 hashtab_stat(h, &info);
Eric Parisbe30b162011-04-28 15:11:21 -0400489 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
490 "longest chain length %d\n", hash_name, h->nel,
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500491 info.slots_used, h->size, info.max_chain_len);
492}
Eric Parisbe30b162011-04-28 15:11:21 -0400493
494static void symtab_hash_eval(struct symtab *s)
495{
496 int i;
497
498 for (i = 0; i < SYM_NUM; i++)
499 hash_eval(s[i].table, symtab_name[i]);
500}
501
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500502#else
Eric Parisbe30b162011-04-28 15:11:21 -0400503static inline void hash_eval(struct hashtab *h, char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500504{
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#endif
507
508/*
509 * Define the other val_to_name and val_to_struct arrays
510 * in a policy database structure.
511 *
512 * Caller must clean up on failure.
513 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500514static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Eric Paris9398c7f2010-11-23 11:40:08 -0500516 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
James Morris454d9722008-02-26 20:42:02 +1100518 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100520 if (p->mls_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 printk(", %d sens, %d cats", p->p_levels.nprim,
522 p->p_cats.nprim);
523 printk("\n");
524
James Morris454d9722008-02-26 20:42:02 +1100525 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 p->p_classes.nprim, p->te_avtab.nel);
527
528#ifdef DEBUG_HASHES
529 avtab_hash_eval(&p->te_avtab, "rules");
530 symtab_hash_eval(p->symtab);
531#endif
532
Eric Paris9398c7f2010-11-23 11:40:08 -0500533 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500534 p->class_val_to_struct =
535 kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
536 GFP_KERNEL);
537 if (!p->class_val_to_struct)
538 goto out;
539
540 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 p->role_val_to_struct =
542 kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400543 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500544 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Eric Paris9398c7f2010-11-23 11:40:08 -0500547 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 p->user_val_to_struct =
549 kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400550 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500551 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Eric Paris23bdecb2010-11-29 15:47:09 -0500554 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500555 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500556 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
557 p->p_types.nprim,
558 GFP_KERNEL | __GFP_ZERO);
559 if (!p->type_val_to_struct_array)
560 goto out;
561
562 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
Eric Paris5a3ea872011-04-28 15:55:52 -0400563 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
Eric Paris23bdecb2010-11-29 15:47:09 -0500564 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900565 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900566
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300567 rc = cond_init_bool_indexes(p);
568 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500571 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500572 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500573 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
574 p->symtab[i].nprim,
575 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500576 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500578
579 rc = flex_array_prealloc(p->sym_val_to_name[i],
Eric Paris5a3ea872011-04-28 15:55:52 -0400580 0, p->symtab[i].nprim,
Eric Parisac76c052010-11-29 15:47:09 -0500581 GFP_KERNEL | __GFP_ZERO);
582 if (rc)
583 goto out;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
586 if (rc)
587 goto out;
588 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500589 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590out:
591 return rc;
592}
593
594/*
595 * The following *_destroy functions are used to
596 * free any memory allocated for each kind of
597 * symbol data in the policy database.
598 */
599
600static int perm_destroy(void *key, void *datum, void *p)
601{
602 kfree(key);
603 kfree(datum);
604 return 0;
605}
606
607static int common_destroy(void *key, void *datum, void *p)
608{
609 struct common_datum *comdatum;
610
611 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500612 if (datum) {
613 comdatum = datum;
614 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
615 hashtab_destroy(comdatum->permissions.table);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 kfree(datum);
618 return 0;
619}
620
Richard Hainesa660bec2013-11-19 17:34:23 -0500621static void constraint_expr_destroy(struct constraint_expr *expr)
622{
623 if (expr) {
624 ebitmap_destroy(&expr->names);
625 if (expr->type_names) {
626 ebitmap_destroy(&expr->type_names->types);
627 ebitmap_destroy(&expr->type_names->negset);
628 kfree(expr->type_names);
629 }
630 kfree(expr);
631 }
632}
633
James Morris6cbda6b2006-11-29 16:50:27 -0500634static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct class_datum *cladatum;
637 struct constraint_node *constraint, *ctemp;
638 struct constraint_expr *e, *etmp;
639
640 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500641 if (datum) {
642 cladatum = datum;
643 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
644 hashtab_destroy(cladatum->permissions.table);
645 constraint = cladatum->constraints;
646 while (constraint) {
647 e = constraint->expr;
648 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500649 etmp = e;
650 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500651 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500652 }
653 ctemp = constraint;
654 constraint = constraint->next;
655 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Paris9398c7f2010-11-23 11:40:08 -0500658 constraint = cladatum->validatetrans;
659 while (constraint) {
660 e = constraint->expr;
661 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500662 etmp = e;
663 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500664 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500665 }
666 ctemp = constraint;
667 constraint = constraint->next;
668 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500670 kfree(cladatum->comkey);
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kfree(datum);
673 return 0;
674}
675
676static int role_destroy(void *key, void *datum, void *p)
677{
678 struct role_datum *role;
679
680 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500681 if (datum) {
682 role = datum;
683 ebitmap_destroy(&role->dominates);
684 ebitmap_destroy(&role->types);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 kfree(datum);
687 return 0;
688}
689
690static int type_destroy(void *key, void *datum, void *p)
691{
692 kfree(key);
693 kfree(datum);
694 return 0;
695}
696
697static int user_destroy(void *key, void *datum, void *p)
698{
699 struct user_datum *usrdatum;
700
701 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500702 if (datum) {
703 usrdatum = datum;
704 ebitmap_destroy(&usrdatum->roles);
705 ebitmap_destroy(&usrdatum->range.level[0].cat);
706 ebitmap_destroy(&usrdatum->range.level[1].cat);
707 ebitmap_destroy(&usrdatum->dfltlevel.cat);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 kfree(datum);
710 return 0;
711}
712
713static int sens_destroy(void *key, void *datum, void *p)
714{
715 struct level_datum *levdatum;
716
717 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500718 if (datum) {
719 levdatum = datum;
720 ebitmap_destroy(&levdatum->level->cat);
721 kfree(levdatum->level);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 kfree(datum);
724 return 0;
725}
726
727static int cat_destroy(void *key, void *datum, void *p)
728{
729 kfree(key);
730 kfree(datum);
731 return 0;
732}
733
734static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
735{
736 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500737 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 role_destroy,
739 type_destroy,
740 user_destroy,
741 cond_destroy_bool,
742 sens_destroy,
743 cat_destroy,
744};
745
Eric Paris2463c26d2011-04-28 15:11:21 -0400746static int filenametr_destroy(void *key, void *datum, void *p)
747{
748 struct filename_trans *ft = key;
749 kfree(ft->name);
750 kfree(key);
751 kfree(datum);
752 cond_resched();
753 return 0;
754}
755
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500756static int range_tr_destroy(void *key, void *datum, void *p)
757{
758 struct mls_range *rt = datum;
759 kfree(key);
760 ebitmap_destroy(&rt->level[0].cat);
761 ebitmap_destroy(&rt->level[1].cat);
762 kfree(datum);
763 cond_resched();
764 return 0;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static void ocontext_destroy(struct ocontext *c, int i)
768{
Eric Parisd1b43542010-07-21 12:50:57 -0400769 if (!c)
770 return;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 context_destroy(&c->context[0]);
773 context_destroy(&c->context[1]);
774 if (i == OCON_ISID || i == OCON_FS ||
775 i == OCON_NETIF || i == OCON_FSUSE)
776 kfree(c->u.name);
777 kfree(c);
778}
779
780/*
781 * Free any memory allocated by a policy database structure.
782 */
783void policydb_destroy(struct policydb *p)
784{
785 struct ocontext *c, *ctmp;
786 struct genfs *g, *gtmp;
787 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700788 struct role_allow *ra, *lra = NULL;
789 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400792 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
794 hashtab_destroy(p->symtab[i].table);
795 }
796
Eric Parisac76c052010-11-29 15:47:09 -0500797 for (i = 0; i < SYM_NUM; i++) {
798 if (p->sym_val_to_name[i])
799 flex_array_free(p->sym_val_to_name[i]);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700802 kfree(p->class_val_to_struct);
803 kfree(p->role_val_to_struct);
804 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500805 if (p->type_val_to_struct_array)
806 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 avtab_destroy(&p->te_avtab);
809
810 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400811 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 c = p->ocontexts[i];
813 while (c) {
814 ctmp = c;
815 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400816 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400818 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 g = p->genfs;
822 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400823 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kfree(g->fstype);
825 c = g->head;
826 while (c) {
827 ctmp = c;
828 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400829 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 gtmp = g;
832 g = g->next;
833 kfree(gtmp);
834 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400835 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 cond_policydb_destroy(p);
838
Stephen Smalley782ebb92005-09-03 15:55:16 -0700839 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400840 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800841 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700842 ltr = tr;
843 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800844 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700845
Eric Paris2ced3df2008-04-17 13:37:12 -0400846 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400847 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800848 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700849 lra = ra;
850 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800851 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700852
Eric Paris2463c26d2011-04-28 15:11:21 -0400853 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
854 hashtab_destroy(p->filename_trans);
855
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500856 hashtab_map(p->range_tr, range_tr_destroy, NULL);
857 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700858
Eric Paris6371dcd2010-07-29 23:02:34 -0400859 if (p->type_attr_map_array) {
860 for (i = 0; i < p->p_types.nprim; i++) {
861 struct ebitmap *e;
862
863 e = flex_array_get(p->type_attr_map_array, i);
864 if (!e)
865 continue;
866 ebitmap_destroy(e);
867 }
868 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700869 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500870
Eric Paris03a4c012011-04-28 15:11:21 -0400871 ebitmap_destroy(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500872 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100873 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return;
876}
877
878/*
879 * Load the initial SIDs specified in a policy database
880 * structure into a SID table.
881 */
882int policydb_load_isids(struct policydb *p, struct sidtab *s)
883{
884 struct ocontext *head, *c;
885 int rc;
886
887 rc = sidtab_init(s);
888 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100889 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto out;
891 }
892
893 head = p->ocontexts[OCON_ISID];
894 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500895 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500897 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
898 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500901
902 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
903 if (rc) {
904 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
905 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out;
907 }
908 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500909 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910out:
911 return rc;
912}
913
Stephen Smalley45e54212007-11-07 10:08:00 -0500914int policydb_class_isvalid(struct policydb *p, unsigned int class)
915{
916 if (!class || class > p->p_classes.nprim)
917 return 0;
918 return 1;
919}
920
921int policydb_role_isvalid(struct policydb *p, unsigned int role)
922{
923 if (!role || role > p->p_roles.nprim)
924 return 0;
925 return 1;
926}
927
928int policydb_type_isvalid(struct policydb *p, unsigned int type)
929{
930 if (!type || type > p->p_types.nprim)
931 return 0;
932 return 1;
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*
936 * Return 1 if the fields in the security context
937 * structure `c' are valid. Return 0 otherwise.
938 */
939int policydb_context_isvalid(struct policydb *p, struct context *c)
940{
941 struct role_datum *role;
942 struct user_datum *usrdatum;
943
944 if (!c->role || c->role > p->p_roles.nprim)
945 return 0;
946
947 if (!c->user || c->user > p->p_users.nprim)
948 return 0;
949
950 if (!c->type || c->type > p->p_types.nprim)
951 return 0;
952
953 if (c->role != OBJECT_R_VAL) {
954 /*
955 * Role must be authorized for the type.
956 */
957 role = p->role_val_to_struct[c->role - 1];
Eric Paris9398c7f2010-11-23 11:40:08 -0500958 if (!ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* role may not be associated with type */
960 return 0;
961
962 /*
963 * User must be authorized for the role.
964 */
965 usrdatum = p->user_val_to_struct[c->user - 1];
966 if (!usrdatum)
967 return 0;
968
Eric Paris9398c7f2010-11-23 11:40:08 -0500969 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* user may not be associated with role */
971 return 0;
972 }
973
974 if (!mls_context_isvalid(p, c))
975 return 0;
976
977 return 1;
978}
979
980/*
981 * Read a MLS range structure from a policydb binary
982 * representation file.
983 */
984static int mls_read_range_helper(struct mls_range *r, void *fp)
985{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700986 __le32 buf[2];
987 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int rc;
989
990 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -0500991 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto out;
993
Eric Paris9398c7f2010-11-23 11:40:08 -0500994 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 items = le32_to_cpu(buf[0]);
996 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +1100997 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto out;
999 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -05001002 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001003 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto out;
1005 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 r->level[0].sens = le32_to_cpu(buf[0]);
1008 if (items > 1)
1009 r->level[1].sens = le32_to_cpu(buf[1]);
1010 else
1011 r->level[1].sens = r->level[0].sens;
1012
1013 rc = ebitmap_read(&r->level[0].cat, fp);
1014 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001015 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017 }
1018 if (items > 1) {
1019 rc = ebitmap_read(&r->level[1].cat, fp);
1020 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001021 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto bad_high;
1023 }
1024 } else {
1025 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1026 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001027 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto bad_high;
1029 }
1030 }
1031
Eric Paris9398c7f2010-11-23 11:40:08 -05001032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033bad_high:
1034 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -05001035out:
1036 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039/*
1040 * Read and validate a security context structure
1041 * from a policydb binary representation file.
1042 */
1043static int context_read_and_validate(struct context *c,
1044 struct policydb *p,
1045 void *fp)
1046{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001047 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int rc;
1049
1050 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001051 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001052 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto out;
1054 }
1055 c->user = le32_to_cpu(buf[0]);
1056 c->role = le32_to_cpu(buf[1]);
1057 c->type = le32_to_cpu(buf[2]);
1058 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001059 rc = mls_read_range_helper(&c->range, fp);
1060 if (rc) {
1061 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto out;
1063 }
1064 }
1065
Eric Paris9398c7f2010-11-23 11:40:08 -05001066 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001068 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001072 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073out:
1074 return rc;
1075}
1076
1077/*
1078 * The following *_read functions are used to
1079 * read the symbol data from a policy database
1080 * binary representation file.
1081 */
1082
1083static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1084{
1085 char *key = NULL;
1086 struct perm_datum *perdatum;
1087 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001088 __le32 buf[2];
1089 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Eric Paris9398c7f2010-11-23 11:40:08 -05001091 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001092 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001093 if (!perdatum)
1094 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001097 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto bad;
1099
1100 len = le32_to_cpu(buf[0]);
1101 perdatum->value = le32_to_cpu(buf[1]);
1102
Eric Paris9398c7f2010-11-23 11:40:08 -05001103 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001104 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001105 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001109 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001111 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 rc = hashtab_insert(h, key, perdatum);
1114 if (rc)
1115 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001116
1117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118bad:
1119 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001120 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1124{
1125 char *key = NULL;
1126 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001127 __le32 buf[4];
1128 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int i, rc;
1130
Eric Paris9398c7f2010-11-23 11:40:08 -05001131 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001132 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001133 if (!comdatum)
1134 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001137 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto bad;
1139
1140 len = le32_to_cpu(buf[0]);
1141 comdatum->value = le32_to_cpu(buf[1]);
1142
1143 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1144 if (rc)
1145 goto bad;
1146 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1147 nel = le32_to_cpu(buf[3]);
1148
Eric Paris9398c7f2010-11-23 11:40:08 -05001149 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001150 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001151 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001155 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001157 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 for (i = 0; i < nel; i++) {
1160 rc = perm_read(p, comdatum->permissions.table, fp);
1161 if (rc)
1162 goto bad;
1163 }
1164
1165 rc = hashtab_insert(h, key, comdatum);
1166 if (rc)
1167 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169bad:
1170 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001171 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Richard Hainesa660bec2013-11-19 17:34:23 -05001174static void type_set_init(struct type_set *t)
1175{
1176 ebitmap_init(&t->types);
1177 ebitmap_init(&t->negset);
1178}
1179
1180static int type_set_read(struct type_set *t, void *fp)
1181{
1182 __le32 buf[1];
1183 int rc;
1184
1185 if (ebitmap_read(&t->types, fp))
1186 return -EINVAL;
1187 if (ebitmap_read(&t->negset, fp))
1188 return -EINVAL;
1189
1190 rc = next_entry(buf, fp, sizeof(u32));
1191 if (rc < 0)
1192 return -EINVAL;
1193 t->flags = le32_to_cpu(buf[0]);
1194
1195 return 0;
1196}
1197
1198
1199static int read_cons_helper(struct policydb *p,
1200 struct constraint_node **nodep,
1201 int ncons, int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct constraint_node *c, *lc;
1204 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001205 __le32 buf[3];
1206 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int rc, i, j, depth;
1208
1209 lc = NULL;
1210 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001211 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!c)
1213 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Eric Paris2ced3df2008-04-17 13:37:12 -04001215 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001221 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return rc;
1223 c->permissions = le32_to_cpu(buf[0]);
1224 nexpr = le32_to_cpu(buf[1]);
1225 le = NULL;
1226 depth = -1;
1227 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001228 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!e)
1230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric Paris2ced3df2008-04-17 13:37:12 -04001232 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001234 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001238 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return rc;
1240 e->expr_type = le32_to_cpu(buf[0]);
1241 e->attr = le32_to_cpu(buf[1]);
1242 e->op = le32_to_cpu(buf[2]);
1243
1244 switch (e->expr_type) {
1245 case CEXPR_NOT:
1246 if (depth < 0)
1247 return -EINVAL;
1248 break;
1249 case CEXPR_AND:
1250 case CEXPR_OR:
1251 if (depth < 1)
1252 return -EINVAL;
1253 depth--;
1254 break;
1255 case CEXPR_ATTR:
1256 if (depth == (CEXPR_MAXDEPTH - 1))
1257 return -EINVAL;
1258 depth++;
1259 break;
1260 case CEXPR_NAMES:
1261 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1262 return -EINVAL;
1263 if (depth == (CEXPR_MAXDEPTH - 1))
1264 return -EINVAL;
1265 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001266 rc = ebitmap_read(&e->names, fp);
1267 if (rc)
1268 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05001269 if (p->policyvers >=
1270 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1271 e->type_names = kzalloc(sizeof
1272 (*e->type_names),
1273 GFP_KERNEL);
1274 if (!e->type_names)
1275 return -ENOMEM;
1276 type_set_init(e->type_names);
1277 rc = type_set_read(e->type_names, fp);
1278 if (rc)
1279 return rc;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 default:
1283 return -EINVAL;
1284 }
1285 le = e;
1286 }
1287 if (depth != 0)
1288 return -EINVAL;
1289 lc = c;
1290 }
1291
1292 return 0;
1293}
1294
1295static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1296{
1297 char *key = NULL;
1298 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001299 __le32 buf[6];
1300 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int i, rc;
1302
Eric Paris9398c7f2010-11-23 11:40:08 -05001303 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001304 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001305 if (!cladatum)
1306 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001309 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 goto bad;
1311
1312 len = le32_to_cpu(buf[0]);
1313 len2 = le32_to_cpu(buf[1]);
1314 cladatum->value = le32_to_cpu(buf[2]);
1315
1316 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1317 if (rc)
1318 goto bad;
1319 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1320 nel = le32_to_cpu(buf[4]);
1321
1322 ncons = le32_to_cpu(buf[5]);
1323
Eric Paris9398c7f2010-11-23 11:40:08 -05001324 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001325 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001326 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001330 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001332 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (len2) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001335 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001336 cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001337 if (!cladatum->comkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 rc = next_entry(cladatum->comkey, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001340 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001342 cladatum->comkey[len2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Paris9398c7f2010-11-23 11:40:08 -05001344 rc = -EINVAL;
1345 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001347 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto bad;
1349 }
1350 }
1351 for (i = 0; i < nel; i++) {
1352 rc = perm_read(p, cladatum->permissions.table, fp);
1353 if (rc)
1354 goto bad;
1355 }
1356
Richard Hainesa660bec2013-11-19 17:34:23 -05001357 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (rc)
1359 goto bad;
1360
1361 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1362 /* grab the validatetrans rules */
1363 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001364 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto bad;
1366 ncons = le32_to_cpu(buf[0]);
Richard Hainesa660bec2013-11-19 17:34:23 -05001367 rc = read_cons_helper(p, &cladatum->validatetrans,
1368 ncons, 1, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (rc)
1370 goto bad;
1371 }
1372
Eric Parisaa893262012-03-20 14:35:12 -04001373 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1374 rc = next_entry(buf, fp, sizeof(u32) * 3);
1375 if (rc)
1376 goto bad;
1377
1378 cladatum->default_user = le32_to_cpu(buf[0]);
1379 cladatum->default_role = le32_to_cpu(buf[1]);
1380 cladatum->default_range = le32_to_cpu(buf[2]);
1381 }
1382
Eric Pariseed77952012-03-20 14:35:12 -04001383 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1384 rc = next_entry(buf, fp, sizeof(u32) * 1);
1385 if (rc)
1386 goto bad;
1387 cladatum->default_type = le32_to_cpu(buf[0]);
1388 }
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 rc = hashtab_insert(h, key, cladatum);
1391 if (rc)
1392 goto bad;
1393
Eric Paris9398c7f2010-11-23 11:40:08 -05001394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001396 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001397 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1401{
1402 char *key = NULL;
1403 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001404 int rc, to_read = 2;
1405 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001406 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Eric Paris9398c7f2010-11-23 11:40:08 -05001408 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001409 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001410 if (!role)
1411 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
KaiGai Koheid9250de2008-08-28 16:35:57 +09001413 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1414 to_read = 3;
1415
1416 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001417 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto bad;
1419
1420 len = le32_to_cpu(buf[0]);
1421 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001422 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1423 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Eric Paris9398c7f2010-11-23 11:40:08 -05001425 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001426 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001427 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001431 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001433 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 rc = ebitmap_read(&role->dominates, fp);
1436 if (rc)
1437 goto bad;
1438
1439 rc = ebitmap_read(&role->types, fp);
1440 if (rc)
1441 goto bad;
1442
1443 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001444 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001446 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto bad;
1449 }
1450 rc = 0;
1451 goto bad;
1452 }
1453
1454 rc = hashtab_insert(h, key, role);
1455 if (rc)
1456 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458bad:
1459 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001460 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1464{
1465 char *key = NULL;
1466 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001467 int rc, to_read = 3;
1468 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001469 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Eric Paris9398c7f2010-11-23 11:40:08 -05001471 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001472 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001473 if (!typdatum)
1474 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
KaiGai Koheid9250de2008-08-28 16:35:57 +09001476 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1477 to_read = 4;
1478
1479 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001480 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto bad;
1482
1483 len = le32_to_cpu(buf[0]);
1484 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001485 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1486 u32 prop = le32_to_cpu(buf[2]);
1487
1488 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1489 typdatum->primary = 1;
1490 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1491 typdatum->attribute = 1;
1492
1493 typdatum->bounds = le32_to_cpu(buf[3]);
1494 } else {
1495 typdatum->primary = le32_to_cpu(buf[2]);
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Eric Paris9398c7f2010-11-23 11:40:08 -05001498 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001499 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001500 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001503 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001505 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 rc = hashtab_insert(h, key, typdatum);
1508 if (rc)
1509 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511bad:
1512 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001513 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516
1517/*
1518 * Read a MLS level structure from a policydb binary
1519 * representation file.
1520 */
1521static int mls_read_level(struct mls_level *lp, void *fp)
1522{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001523 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int rc;
1525
1526 memset(lp, 0, sizeof(*lp));
1527
1528 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001529 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001530 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001531 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 lp->sens = le32_to_cpu(buf[0]);
1534
Eric Paris9398c7f2010-11-23 11:40:08 -05001535 rc = ebitmap_read(&lp->cat, fp);
1536 if (rc) {
1537 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1538 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1544{
1545 char *key = NULL;
1546 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001547 int rc, to_read = 2;
1548 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001549 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Eric Paris9398c7f2010-11-23 11:40:08 -05001551 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001552 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001553 if (!usrdatum)
1554 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
KaiGai Koheid9250de2008-08-28 16:35:57 +09001556 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1557 to_read = 3;
1558
1559 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001560 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 goto bad;
1562
1563 len = le32_to_cpu(buf[0]);
1564 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001565 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1566 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Eric Paris9398c7f2010-11-23 11:40:08 -05001568 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001569 key = kmalloc(len + 1, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001570 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001573 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001575 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 rc = ebitmap_read(&usrdatum->roles, fp);
1578 if (rc)
1579 goto bad;
1580
1581 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1582 rc = mls_read_range_helper(&usrdatum->range, fp);
1583 if (rc)
1584 goto bad;
1585 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1586 if (rc)
1587 goto bad;
1588 }
1589
1590 rc = hashtab_insert(h, key, usrdatum);
1591 if (rc)
1592 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594bad:
1595 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001596 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
1599static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1600{
1601 char *key = NULL;
1602 struct level_datum *levdatum;
1603 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001604 __le32 buf[2];
1605 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Eric Paris9398c7f2010-11-23 11:40:08 -05001607 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001608 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001609 if (!levdatum)
1610 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001613 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto bad;
1615
1616 len = le32_to_cpu(buf[0]);
1617 levdatum->isalias = le32_to_cpu(buf[1]);
1618
Eric Paris9398c7f2010-11-23 11:40:08 -05001619 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001620 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001621 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001624 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001626 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Eric Paris9398c7f2010-11-23 11:40:08 -05001628 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001630 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001632
1633 rc = mls_read_level(levdatum->level, fp);
1634 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 rc = hashtab_insert(h, key, levdatum);
1638 if (rc)
1639 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641bad:
1642 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001643 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
1646static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1647{
1648 char *key = NULL;
1649 struct cat_datum *catdatum;
1650 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001651 __le32 buf[3];
1652 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Paris9398c7f2010-11-23 11:40:08 -05001654 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001655 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001656 if (!catdatum)
1657 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001660 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto bad;
1662
1663 len = le32_to_cpu(buf[0]);
1664 catdatum->value = le32_to_cpu(buf[1]);
1665 catdatum->isalias = le32_to_cpu(buf[2]);
1666
Eric Paris9398c7f2010-11-23 11:40:08 -05001667 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001668 key = kmalloc(len + 1, GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001669 if (!key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rc = next_entry(key, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001672 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto bad;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03001674 key[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 rc = hashtab_insert(h, key, catdatum);
1677 if (rc)
1678 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680bad:
1681 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001682 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1686{
1687 common_read,
1688 class_read,
1689 role_read,
1690 type_read,
1691 user_read,
1692 cond_read_bool,
1693 sens_read,
1694 cat_read,
1695};
1696
KaiGai Koheid9250de2008-08-28 16:35:57 +09001697static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1698{
1699 struct user_datum *upper, *user;
1700 struct policydb *p = datap;
1701 int depth = 0;
1702
1703 upper = user = datum;
1704 while (upper->bounds) {
1705 struct ebitmap_node *node;
1706 unsigned long bit;
1707
1708 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1709 printk(KERN_ERR "SELinux: user %s: "
1710 "too deep or looped boundary",
1711 (char *) key);
1712 return -EINVAL;
1713 }
1714
1715 upper = p->user_val_to_struct[upper->bounds - 1];
1716 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1717 if (ebitmap_get_bit(&upper->roles, bit))
1718 continue;
1719
1720 printk(KERN_ERR
1721 "SELinux: boundary violated policy: "
1722 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001723 sym_name(p, SYM_USERS, user->value - 1),
1724 sym_name(p, SYM_ROLES, bit),
1725 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001726
1727 return -EINVAL;
1728 }
1729 }
1730
1731 return 0;
1732}
1733
1734static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1735{
1736 struct role_datum *upper, *role;
1737 struct policydb *p = datap;
1738 int depth = 0;
1739
1740 upper = role = datum;
1741 while (upper->bounds) {
1742 struct ebitmap_node *node;
1743 unsigned long bit;
1744
1745 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1746 printk(KERN_ERR "SELinux: role %s: "
1747 "too deep or looped bounds\n",
1748 (char *) key);
1749 return -EINVAL;
1750 }
1751
1752 upper = p->role_val_to_struct[upper->bounds - 1];
1753 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1754 if (ebitmap_get_bit(&upper->types, bit))
1755 continue;
1756
1757 printk(KERN_ERR
1758 "SELinux: boundary violated policy: "
1759 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001760 sym_name(p, SYM_ROLES, role->value - 1),
1761 sym_name(p, SYM_TYPES, bit),
1762 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001763
1764 return -EINVAL;
1765 }
1766 }
1767
1768 return 0;
1769}
1770
1771static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1772{
Eric Parisdaa6d832010-08-03 15:26:05 -04001773 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001774 struct policydb *p = datap;
1775 int depth = 0;
1776
Eric Parisdaa6d832010-08-03 15:26:05 -04001777 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001778 while (upper->bounds) {
1779 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1780 printk(KERN_ERR "SELinux: type %s: "
1781 "too deep or looped boundary\n",
1782 (char *) key);
1783 return -EINVAL;
1784 }
1785
Eric Paris23bdecb2010-11-29 15:47:09 -05001786 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1787 upper->bounds - 1);
1788 BUG_ON(!upper);
1789
KaiGai Koheid9250de2008-08-28 16:35:57 +09001790 if (upper->attribute) {
1791 printk(KERN_ERR "SELinux: type %s: "
1792 "bounded by attribute %s",
1793 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001794 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001795 return -EINVAL;
1796 }
1797 }
1798
1799 return 0;
1800}
1801
1802static int policydb_bounds_sanity_check(struct policydb *p)
1803{
1804 int rc;
1805
1806 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1807 return 0;
1808
1809 rc = hashtab_map(p->p_users.table,
1810 user_bounds_sanity_check, p);
1811 if (rc)
1812 return rc;
1813
1814 rc = hashtab_map(p->p_roles.table,
1815 role_bounds_sanity_check, p);
1816 if (rc)
1817 return rc;
1818
1819 rc = hashtab_map(p->p_types.table,
1820 type_bounds_sanity_check, p);
1821 if (rc)
1822 return rc;
1823
1824 return 0;
1825}
1826
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001827u16 string_to_security_class(struct policydb *p, const char *name)
1828{
1829 struct class_datum *cladatum;
1830
1831 cladatum = hashtab_search(p->p_classes.table, name);
1832 if (!cladatum)
1833 return 0;
1834
1835 return cladatum->value;
1836}
1837
1838u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1839{
1840 struct class_datum *cladatum;
1841 struct perm_datum *perdatum = NULL;
1842 struct common_datum *comdatum;
1843
1844 if (!tclass || tclass > p->p_classes.nprim)
1845 return 0;
1846
1847 cladatum = p->class_val_to_struct[tclass-1];
1848 comdatum = cladatum->comdatum;
1849 if (comdatum)
1850 perdatum = hashtab_search(comdatum->permissions.table,
1851 name);
1852 if (!perdatum)
1853 perdatum = hashtab_search(cladatum->permissions.table,
1854 name);
1855 if (!perdatum)
1856 return 0;
1857
1858 return 1U << (perdatum->value-1);
1859}
1860
Eric Paris9ee0c822010-06-11 12:37:05 -04001861static int range_read(struct policydb *p, void *fp)
1862{
1863 struct range_trans *rt = NULL;
1864 struct mls_range *r = NULL;
1865 int i, rc;
1866 __le32 buf[2];
1867 u32 nel;
1868
1869 if (p->policyvers < POLICYDB_VERSION_MLS)
1870 return 0;
1871
1872 rc = next_entry(buf, fp, sizeof(u32));
1873 if (rc)
1874 goto out;
1875
1876 nel = le32_to_cpu(buf[0]);
1877 for (i = 0; i < nel; i++) {
1878 rc = -ENOMEM;
1879 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1880 if (!rt)
1881 goto out;
1882
1883 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1884 if (rc)
1885 goto out;
1886
1887 rt->source_type = le32_to_cpu(buf[0]);
1888 rt->target_type = le32_to_cpu(buf[1]);
1889 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1890 rc = next_entry(buf, fp, sizeof(u32));
1891 if (rc)
1892 goto out;
1893 rt->target_class = le32_to_cpu(buf[0]);
1894 } else
1895 rt->target_class = p->process_class;
1896
1897 rc = -EINVAL;
1898 if (!policydb_type_isvalid(p, rt->source_type) ||
1899 !policydb_type_isvalid(p, rt->target_type) ||
1900 !policydb_class_isvalid(p, rt->target_class))
1901 goto out;
1902
1903 rc = -ENOMEM;
1904 r = kzalloc(sizeof(*r), GFP_KERNEL);
1905 if (!r)
1906 goto out;
1907
1908 rc = mls_read_range_helper(r, fp);
1909 if (rc)
1910 goto out;
1911
1912 rc = -EINVAL;
1913 if (!mls_range_isvalid(p, r)) {
1914 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1915 goto out;
1916 }
1917
1918 rc = hashtab_insert(p->range_tr, rt, r);
1919 if (rc)
1920 goto out;
1921
1922 rt = NULL;
1923 r = NULL;
1924 }
Eric Parisbe30b162011-04-28 15:11:21 -04001925 hash_eval(p->range_tr, "rangetr");
Eric Paris9ee0c822010-06-11 12:37:05 -04001926 rc = 0;
1927out:
1928 kfree(rt);
1929 kfree(r);
1930 return rc;
1931}
1932
Eric Paris652bb9b2011-02-01 11:05:40 -05001933static int filename_trans_read(struct policydb *p, void *fp)
1934{
Eric Paris2463c26d2011-04-28 15:11:21 -04001935 struct filename_trans *ft;
1936 struct filename_trans_datum *otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001937 char *name;
Eric Paris2463c26d2011-04-28 15:11:21 -04001938 u32 nel, len;
Eric Paris652bb9b2011-02-01 11:05:40 -05001939 __le32 buf[4];
1940 int rc, i;
1941
1942 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1943 return 0;
1944
1945 rc = next_entry(buf, fp, sizeof(u32));
1946 if (rc)
Eric Paris2463c26d2011-04-28 15:11:21 -04001947 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05001948 nel = le32_to_cpu(buf[0]);
1949
Eric Paris652bb9b2011-02-01 11:05:40 -05001950 for (i = 0; i < nel; i++) {
Eric Paris2463c26d2011-04-28 15:11:21 -04001951 ft = NULL;
1952 otype = NULL;
1953 name = NULL;
1954
Eric Paris652bb9b2011-02-01 11:05:40 -05001955 rc = -ENOMEM;
1956 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1957 if (!ft)
1958 goto out;
1959
Eric Paris2463c26d2011-04-28 15:11:21 -04001960 rc = -ENOMEM;
1961 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1962 if (!otype)
1963 goto out;
Eric Paris652bb9b2011-02-01 11:05:40 -05001964
1965 /* length of the path component string */
1966 rc = next_entry(buf, fp, sizeof(u32));
1967 if (rc)
1968 goto out;
1969 len = le32_to_cpu(buf[0]);
1970
1971 rc = -ENOMEM;
1972 name = kmalloc(len + 1, GFP_KERNEL);
1973 if (!name)
1974 goto out;
1975
1976 ft->name = name;
1977
1978 /* path component string */
1979 rc = next_entry(name, fp, len);
1980 if (rc)
1981 goto out;
1982 name[len] = 0;
1983
Eric Paris652bb9b2011-02-01 11:05:40 -05001984 rc = next_entry(buf, fp, sizeof(u32) * 4);
1985 if (rc)
1986 goto out;
1987
1988 ft->stype = le32_to_cpu(buf[0]);
1989 ft->ttype = le32_to_cpu(buf[1]);
1990 ft->tclass = le32_to_cpu(buf[2]);
Eric Paris2463c26d2011-04-28 15:11:21 -04001991
1992 otype->otype = le32_to_cpu(buf[3]);
Eric Paris03a4c012011-04-28 15:11:21 -04001993
1994 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1995 if (rc)
1996 goto out;
Eric Paris2463c26d2011-04-28 15:11:21 -04001997
Tetsuo Handa8ed81462014-01-06 21:28:15 +09001998 rc = hashtab_insert(p->filename_trans, ft, otype);
1999 if (rc) {
2000 /*
2001 * Do not return -EEXIST to the caller, or the system
2002 * will not boot.
2003 */
2004 if (rc != -EEXIST)
2005 goto out;
2006 /* But free memory to avoid memory leak. */
2007 kfree(ft);
2008 kfree(name);
2009 kfree(otype);
2010 }
Eric Paris652bb9b2011-02-01 11:05:40 -05002011 }
Eric Paris2463c26d2011-04-28 15:11:21 -04002012 hash_eval(p->filename_trans, "filenametr");
2013 return 0;
Eric Paris652bb9b2011-02-01 11:05:40 -05002014out:
Eric Paris2463c26d2011-04-28 15:11:21 -04002015 kfree(ft);
2016 kfree(name);
2017 kfree(otype);
2018
Eric Paris652bb9b2011-02-01 11:05:40 -05002019 return rc;
2020}
2021
Eric Parisd1b43542010-07-21 12:50:57 -04002022static int genfs_read(struct policydb *p, void *fp)
2023{
2024 int i, j, rc;
2025 u32 nel, nel2, len, len2;
2026 __le32 buf[1];
2027 struct ocontext *l, *c;
2028 struct ocontext *newc = NULL;
2029 struct genfs *genfs_p, *genfs;
2030 struct genfs *newgenfs = NULL;
2031
2032 rc = next_entry(buf, fp, sizeof(u32));
2033 if (rc)
2034 goto out;
2035 nel = le32_to_cpu(buf[0]);
2036
2037 for (i = 0; i < nel; i++) {
2038 rc = next_entry(buf, fp, sizeof(u32));
2039 if (rc)
2040 goto out;
2041 len = le32_to_cpu(buf[0]);
2042
2043 rc = -ENOMEM;
2044 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2045 if (!newgenfs)
2046 goto out;
2047
2048 rc = -ENOMEM;
2049 newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
2050 if (!newgenfs->fstype)
2051 goto out;
2052
2053 rc = next_entry(newgenfs->fstype, fp, len);
2054 if (rc)
2055 goto out;
2056
2057 newgenfs->fstype[len] = 0;
2058
2059 for (genfs_p = NULL, genfs = p->genfs; genfs;
2060 genfs_p = genfs, genfs = genfs->next) {
2061 rc = -EINVAL;
2062 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2063 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2064 newgenfs->fstype);
2065 goto out;
2066 }
2067 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2068 break;
2069 }
2070 newgenfs->next = genfs;
2071 if (genfs_p)
2072 genfs_p->next = newgenfs;
2073 else
2074 p->genfs = newgenfs;
2075 genfs = newgenfs;
2076 newgenfs = NULL;
2077
2078 rc = next_entry(buf, fp, sizeof(u32));
2079 if (rc)
2080 goto out;
2081
2082 nel2 = le32_to_cpu(buf[0]);
2083 for (j = 0; j < nel2; j++) {
2084 rc = next_entry(buf, fp, sizeof(u32));
2085 if (rc)
2086 goto out;
2087 len = le32_to_cpu(buf[0]);
2088
2089 rc = -ENOMEM;
2090 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2091 if (!newc)
2092 goto out;
2093
2094 rc = -ENOMEM;
2095 newc->u.name = kmalloc(len + 1, GFP_KERNEL);
2096 if (!newc->u.name)
2097 goto out;
2098
2099 rc = next_entry(newc->u.name, fp, len);
2100 if (rc)
2101 goto out;
2102 newc->u.name[len] = 0;
2103
2104 rc = next_entry(buf, fp, sizeof(u32));
2105 if (rc)
2106 goto out;
2107
2108 newc->v.sclass = le32_to_cpu(buf[0]);
2109 rc = context_read_and_validate(&newc->context[0], p, fp);
2110 if (rc)
2111 goto out;
2112
2113 for (l = NULL, c = genfs->head; c;
2114 l = c, c = c->next) {
2115 rc = -EINVAL;
2116 if (!strcmp(newc->u.name, c->u.name) &&
2117 (!c->v.sclass || !newc->v.sclass ||
2118 newc->v.sclass == c->v.sclass)) {
2119 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2120 genfs->fstype, c->u.name);
2121 goto out;
2122 }
2123 len = strlen(newc->u.name);
2124 len2 = strlen(c->u.name);
2125 if (len > len2)
2126 break;
2127 }
2128
2129 newc->next = c;
2130 if (l)
2131 l->next = newc;
2132 else
2133 genfs->head = newc;
2134 newc = NULL;
2135 }
2136 }
2137 rc = 0;
2138out:
2139 if (newgenfs)
2140 kfree(newgenfs->fstype);
2141 kfree(newgenfs);
2142 ocontext_destroy(newc, OCON_FSUSE);
2143
2144 return rc;
2145}
2146
Eric Paris692a8a22010-07-21 12:51:03 -04002147static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2148 void *fp)
2149{
2150 int i, j, rc;
2151 u32 nel, len;
2152 __le32 buf[3];
2153 struct ocontext *l, *c;
2154 u32 nodebuf[8];
2155
2156 for (i = 0; i < info->ocon_num; i++) {
2157 rc = next_entry(buf, fp, sizeof(u32));
2158 if (rc)
2159 goto out;
2160 nel = le32_to_cpu(buf[0]);
2161
2162 l = NULL;
2163 for (j = 0; j < nel; j++) {
2164 rc = -ENOMEM;
2165 c = kzalloc(sizeof(*c), GFP_KERNEL);
2166 if (!c)
2167 goto out;
2168 if (l)
2169 l->next = c;
2170 else
2171 p->ocontexts[i] = c;
2172 l = c;
2173
2174 switch (i) {
2175 case OCON_ISID:
2176 rc = next_entry(buf, fp, sizeof(u32));
2177 if (rc)
2178 goto out;
2179
2180 c->sid[0] = le32_to_cpu(buf[0]);
2181 rc = context_read_and_validate(&c->context[0], p, fp);
2182 if (rc)
2183 goto out;
2184 break;
2185 case OCON_FS:
2186 case OCON_NETIF:
2187 rc = next_entry(buf, fp, sizeof(u32));
2188 if (rc)
2189 goto out;
2190 len = le32_to_cpu(buf[0]);
2191
2192 rc = -ENOMEM;
2193 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2194 if (!c->u.name)
2195 goto out;
2196
2197 rc = next_entry(c->u.name, fp, len);
2198 if (rc)
2199 goto out;
2200
2201 c->u.name[len] = 0;
2202 rc = context_read_and_validate(&c->context[0], p, fp);
2203 if (rc)
2204 goto out;
2205 rc = context_read_and_validate(&c->context[1], p, fp);
2206 if (rc)
2207 goto out;
2208 break;
2209 case OCON_PORT:
2210 rc = next_entry(buf, fp, sizeof(u32)*3);
2211 if (rc)
2212 goto out;
2213 c->u.port.protocol = le32_to_cpu(buf[0]);
2214 c->u.port.low_port = le32_to_cpu(buf[1]);
2215 c->u.port.high_port = le32_to_cpu(buf[2]);
2216 rc = context_read_and_validate(&c->context[0], p, fp);
2217 if (rc)
2218 goto out;
2219 break;
2220 case OCON_NODE:
2221 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2222 if (rc)
2223 goto out;
2224 c->u.node.addr = nodebuf[0]; /* network order */
2225 c->u.node.mask = nodebuf[1]; /* network order */
2226 rc = context_read_and_validate(&c->context[0], p, fp);
2227 if (rc)
2228 goto out;
2229 break;
2230 case OCON_FSUSE:
2231 rc = next_entry(buf, fp, sizeof(u32)*2);
2232 if (rc)
2233 goto out;
2234
2235 rc = -EINVAL;
2236 c->v.behavior = le32_to_cpu(buf[0]);
David Quigleyeb9ae682013-05-22 12:50:37 -04002237 /* Determined at runtime, not in policy DB. */
2238 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2239 goto out;
2240 if (c->v.behavior > SECURITY_FS_USE_MAX)
Eric Paris692a8a22010-07-21 12:51:03 -04002241 goto out;
2242
2243 rc = -ENOMEM;
2244 len = le32_to_cpu(buf[1]);
2245 c->u.name = kmalloc(len + 1, GFP_KERNEL);
2246 if (!c->u.name)
2247 goto out;
2248
2249 rc = next_entry(c->u.name, fp, len);
2250 if (rc)
2251 goto out;
2252 c->u.name[len] = 0;
2253 rc = context_read_and_validate(&c->context[0], p, fp);
2254 if (rc)
2255 goto out;
2256 break;
2257 case OCON_NODE6: {
2258 int k;
2259
2260 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2261 if (rc)
2262 goto out;
2263 for (k = 0; k < 4; k++)
2264 c->u.node6.addr[k] = nodebuf[k];
2265 for (k = 0; k < 4; k++)
2266 c->u.node6.mask[k] = nodebuf[k+4];
2267 rc = context_read_and_validate(&c->context[0], p, fp);
2268 if (rc)
2269 goto out;
2270 break;
2271 }
2272 }
2273 }
2274 }
2275 rc = 0;
2276out:
2277 return rc;
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/*
2281 * Read the configuration data from a policy database binary
2282 * representation file into a policy database structure.
2283 */
2284int policydb_read(struct policydb *p, void *fp)
2285{
2286 struct role_allow *ra, *lra;
2287 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002289 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002290 u32 len, nprim, nel;
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 char *policydb_str;
2293 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 rc = policydb_init(p);
2296 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002297 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002300 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002301 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto bad;
2303
Eric Paris9398c7f2010-11-23 11:40:08 -05002304 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002305 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002306 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002308 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto bad;
2310 }
2311
Eric Paris9398c7f2010-11-23 11:40:08 -05002312 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002313 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002315 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 "match expected length %Zu\n",
2317 len, strlen(POLICYDB_STRING));
2318 goto bad;
2319 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002320
2321 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002322 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002324 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto bad;
2327 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002330 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002331 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 kfree(policydb_str);
2333 goto bad;
2334 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002335
2336 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002337 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002339 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 "my string %s\n", policydb_str, POLICYDB_STRING);
2341 kfree(policydb_str);
2342 goto bad;
2343 }
2344 /* Done with policydb_str. */
2345 kfree(policydb_str);
2346 policydb_str = NULL;
2347
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002348 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002350 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Eric Paris9398c7f2010-11-23 11:40:08 -05002353 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002354 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (p->policyvers < POLICYDB_VERSION_MIN ||
2356 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002357 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002358 "my version range %d-%d\n",
2359 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2360 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002363 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002364 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Eric Paris9398c7f2010-11-23 11:40:08 -05002366 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002368 printk(KERN_ERR "SELinux: security policydb version %d "
2369 "(MLS) not backwards compatible\n",
2370 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto bad;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Eric Paris3f120702007-09-21 14:37:10 -04002374 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2375 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Eric Paris9398c7f2010-11-23 11:40:08 -05002377 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2378 rc = ebitmap_read(&p->policycaps, fp);
2379 if (rc)
2380 goto bad;
2381 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002382
Eric Paris9398c7f2010-11-23 11:40:08 -05002383 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2384 rc = ebitmap_read(&p->permissive_map, fp);
2385 if (rc)
2386 goto bad;
2387 }
Eric Paris64dbf072008-03-31 12:17:33 +11002388
Eric Paris9398c7f2010-11-23 11:40:08 -05002389 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 info = policydb_lookup_compat(p->policyvers);
2391 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002392 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 "for version %d\n", p->policyvers);
2394 goto bad;
2395 }
2396
Eric Paris9398c7f2010-11-23 11:40:08 -05002397 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002398 if (le32_to_cpu(buf[2]) != info->sym_num ||
2399 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002400 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002401 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2402 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 info->sym_num, info->ocon_num);
2404 goto bad;
2405 }
2406
2407 for (i = 0; i < info->sym_num; i++) {
2408 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002409 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto bad;
2411 nprim = le32_to_cpu(buf[0]);
2412 nel = le32_to_cpu(buf[1]);
2413 for (j = 0; j < nel; j++) {
2414 rc = read_f[i](p, p->symtab[i].table, fp);
2415 if (rc)
2416 goto bad;
2417 }
2418
2419 p->symtab[i].nprim = nprim;
2420 }
2421
Harry Ciao1214eac2011-04-07 14:12:57 +08002422 rc = -EINVAL;
2423 p->process_class = string_to_security_class(p, "process");
2424 if (!p->process_class)
2425 goto bad;
2426
Stephen Smalley45e54212007-11-07 10:08:00 -05002427 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (rc)
2429 goto bad;
2430
2431 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2432 rc = cond_read_list(p, fp);
2433 if (rc)
2434 goto bad;
2435 }
2436
2437 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002438 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto bad;
2440 nel = le32_to_cpu(buf[0]);
2441 ltr = NULL;
2442 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002443 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002444 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002445 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002447 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002449 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002452 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002454
2455 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 tr->role = le32_to_cpu(buf[0]);
2457 tr->type = le32_to_cpu(buf[1]);
2458 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002459 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2460 rc = next_entry(buf, fp, sizeof(u32));
2461 if (rc)
2462 goto bad;
2463 tr->tclass = le32_to_cpu(buf[0]);
2464 } else
2465 tr->tclass = p->process_class;
2466
Stephen Smalley45e54212007-11-07 10:08:00 -05002467 if (!policydb_role_isvalid(p, tr->role) ||
2468 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002469 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002470 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002471 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 ltr = tr;
2473 }
2474
2475 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002476 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto bad;
2478 nel = le32_to_cpu(buf[0]);
2479 lra = NULL;
2480 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002481 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002482 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002483 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002485 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002487 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002490 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002492
2493 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 ra->role = le32_to_cpu(buf[0]);
2495 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002496 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002497 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002498 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 lra = ra;
2500 }
2501
Eric Paris652bb9b2011-02-01 11:05:40 -05002502 rc = filename_trans_read(p, fp);
2503 if (rc)
2504 goto bad;
2505
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002506 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (rc)
2508 goto bad;
2509
Eric Paris9398c7f2010-11-23 11:40:08 -05002510 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002511 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2512 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002513 if (!p->process_trans_perms)
2514 goto bad;
2515
Eric Paris692a8a22010-07-21 12:51:03 -04002516 rc = ocontext_read(p, info, fp);
2517 if (rc)
2518 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Eric Parisd1b43542010-07-21 12:50:57 -04002520 rc = genfs_read(p, fp);
2521 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Eric Paris9ee0c822010-06-11 12:37:05 -04002524 rc = range_read(p, fp);
2525 if (rc)
2526 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Eric Paris6371dcd2010-07-29 23:02:34 -04002528 rc = -ENOMEM;
2529 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2530 p->p_types.nprim,
2531 GFP_KERNEL | __GFP_ZERO);
2532 if (!p->type_attr_map_array)
2533 goto bad;
2534
2535 /* preallocate so we don't have to worry about the put ever failing */
Eric Paris5a3ea872011-04-28 15:55:52 -04002536 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
Eric Paris6371dcd2010-07-29 23:02:34 -04002537 GFP_KERNEL | __GFP_ZERO);
2538 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002539 goto bad;
2540
2541 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002542 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2543
2544 BUG_ON(!e);
2545 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002546 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002547 rc = ebitmap_read(e, fp);
2548 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002549 goto bad;
2550 }
2551 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002552 rc = ebitmap_set_bit(e, i, 1);
2553 if (rc)
2554 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002555 }
2556
KaiGai Koheid9250de2008-08-28 16:35:57 +09002557 rc = policydb_bounds_sanity_check(p);
2558 if (rc)
2559 goto bad;
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 rc = 0;
2562out:
2563 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 policydb_destroy(p);
2566 goto out;
2567}
Eric Pariscee74f42010-10-13 17:50:25 -04002568
2569/*
2570 * Write a MLS level structure to a policydb binary
2571 * representation file.
2572 */
2573static int mls_write_level(struct mls_level *l, void *fp)
2574{
2575 __le32 buf[1];
2576 int rc;
2577
2578 buf[0] = cpu_to_le32(l->sens);
2579 rc = put_entry(buf, sizeof(u32), 1, fp);
2580 if (rc)
2581 return rc;
2582
2583 rc = ebitmap_write(&l->cat, fp);
2584 if (rc)
2585 return rc;
2586
2587 return 0;
2588}
2589
2590/*
2591 * Write a MLS range structure to a policydb binary
2592 * representation file.
2593 */
2594static int mls_write_range_helper(struct mls_range *r, void *fp)
2595{
2596 __le32 buf[3];
2597 size_t items;
2598 int rc, eq;
2599
2600 eq = mls_level_eq(&r->level[1], &r->level[0]);
2601
2602 if (eq)
2603 items = 2;
2604 else
2605 items = 3;
2606 buf[0] = cpu_to_le32(items-1);
2607 buf[1] = cpu_to_le32(r->level[0].sens);
2608 if (!eq)
2609 buf[2] = cpu_to_le32(r->level[1].sens);
2610
2611 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2612
2613 rc = put_entry(buf, sizeof(u32), items, fp);
2614 if (rc)
2615 return rc;
2616
2617 rc = ebitmap_write(&r->level[0].cat, fp);
2618 if (rc)
2619 return rc;
2620 if (!eq) {
2621 rc = ebitmap_write(&r->level[1].cat, fp);
2622 if (rc)
2623 return rc;
2624 }
2625
2626 return 0;
2627}
2628
2629static int sens_write(void *vkey, void *datum, void *ptr)
2630{
2631 char *key = vkey;
2632 struct level_datum *levdatum = datum;
2633 struct policy_data *pd = ptr;
2634 void *fp = pd->fp;
2635 __le32 buf[2];
2636 size_t len;
2637 int rc;
2638
2639 len = strlen(key);
2640 buf[0] = cpu_to_le32(len);
2641 buf[1] = cpu_to_le32(levdatum->isalias);
2642 rc = put_entry(buf, sizeof(u32), 2, fp);
2643 if (rc)
2644 return rc;
2645
2646 rc = put_entry(key, 1, len, fp);
2647 if (rc)
2648 return rc;
2649
2650 rc = mls_write_level(levdatum->level, fp);
2651 if (rc)
2652 return rc;
2653
2654 return 0;
2655}
2656
2657static int cat_write(void *vkey, void *datum, void *ptr)
2658{
2659 char *key = vkey;
2660 struct cat_datum *catdatum = datum;
2661 struct policy_data *pd = ptr;
2662 void *fp = pd->fp;
2663 __le32 buf[3];
2664 size_t len;
2665 int rc;
2666
2667 len = strlen(key);
2668 buf[0] = cpu_to_le32(len);
2669 buf[1] = cpu_to_le32(catdatum->value);
2670 buf[2] = cpu_to_le32(catdatum->isalias);
2671 rc = put_entry(buf, sizeof(u32), 3, fp);
2672 if (rc)
2673 return rc;
2674
2675 rc = put_entry(key, 1, len, fp);
2676 if (rc)
2677 return rc;
2678
2679 return 0;
2680}
2681
Harry Ciaoc900ff32011-03-25 13:52:00 +08002682static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002683{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002684 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002685 struct role_trans *tr;
2686 u32 buf[3];
2687 size_t nel;
2688 int rc;
2689
2690 nel = 0;
2691 for (tr = r; tr; tr = tr->next)
2692 nel++;
2693 buf[0] = cpu_to_le32(nel);
2694 rc = put_entry(buf, sizeof(u32), 1, fp);
2695 if (rc)
2696 return rc;
2697 for (tr = r; tr; tr = tr->next) {
2698 buf[0] = cpu_to_le32(tr->role);
2699 buf[1] = cpu_to_le32(tr->type);
2700 buf[2] = cpu_to_le32(tr->new_role);
2701 rc = put_entry(buf, sizeof(u32), 3, fp);
2702 if (rc)
2703 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002704 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2705 buf[0] = cpu_to_le32(tr->tclass);
2706 rc = put_entry(buf, sizeof(u32), 1, fp);
2707 if (rc)
2708 return rc;
2709 }
Eric Pariscee74f42010-10-13 17:50:25 -04002710 }
2711
2712 return 0;
2713}
2714
2715static int role_allow_write(struct role_allow *r, void *fp)
2716{
2717 struct role_allow *ra;
2718 u32 buf[2];
2719 size_t nel;
2720 int rc;
2721
2722 nel = 0;
2723 for (ra = r; ra; ra = ra->next)
2724 nel++;
2725 buf[0] = cpu_to_le32(nel);
2726 rc = put_entry(buf, sizeof(u32), 1, fp);
2727 if (rc)
2728 return rc;
2729 for (ra = r; ra; ra = ra->next) {
2730 buf[0] = cpu_to_le32(ra->role);
2731 buf[1] = cpu_to_le32(ra->new_role);
2732 rc = put_entry(buf, sizeof(u32), 2, fp);
2733 if (rc)
2734 return rc;
2735 }
2736 return 0;
2737}
2738
2739/*
2740 * Write a security context structure
2741 * to a policydb binary representation file.
2742 */
2743static int context_write(struct policydb *p, struct context *c,
2744 void *fp)
2745{
2746 int rc;
2747 __le32 buf[3];
2748
2749 buf[0] = cpu_to_le32(c->user);
2750 buf[1] = cpu_to_le32(c->role);
2751 buf[2] = cpu_to_le32(c->type);
2752
2753 rc = put_entry(buf, sizeof(u32), 3, fp);
2754 if (rc)
2755 return rc;
2756
2757 rc = mls_write_range_helper(&c->range, fp);
2758 if (rc)
2759 return rc;
2760
2761 return 0;
2762}
2763
2764/*
2765 * The following *_write functions are used to
2766 * write the symbol data to a policy database
2767 * binary representation file.
2768 */
2769
2770static int perm_write(void *vkey, void *datum, void *fp)
2771{
2772 char *key = vkey;
2773 struct perm_datum *perdatum = datum;
2774 __le32 buf[2];
2775 size_t len;
2776 int rc;
2777
2778 len = strlen(key);
2779 buf[0] = cpu_to_le32(len);
2780 buf[1] = cpu_to_le32(perdatum->value);
2781 rc = put_entry(buf, sizeof(u32), 2, fp);
2782 if (rc)
2783 return rc;
2784
2785 rc = put_entry(key, 1, len, fp);
2786 if (rc)
2787 return rc;
2788
2789 return 0;
2790}
2791
2792static int common_write(void *vkey, void *datum, void *ptr)
2793{
2794 char *key = vkey;
2795 struct common_datum *comdatum = datum;
2796 struct policy_data *pd = ptr;
2797 void *fp = pd->fp;
2798 __le32 buf[4];
2799 size_t len;
2800 int rc;
2801
2802 len = strlen(key);
2803 buf[0] = cpu_to_le32(len);
2804 buf[1] = cpu_to_le32(comdatum->value);
2805 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2806 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2807 rc = put_entry(buf, sizeof(u32), 4, fp);
2808 if (rc)
2809 return rc;
2810
2811 rc = put_entry(key, 1, len, fp);
2812 if (rc)
2813 return rc;
2814
2815 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2816 if (rc)
2817 return rc;
2818
2819 return 0;
2820}
2821
Richard Hainesa660bec2013-11-19 17:34:23 -05002822static int type_set_write(struct type_set *t, void *fp)
2823{
2824 int rc;
2825 __le32 buf[1];
2826
2827 if (ebitmap_write(&t->types, fp))
2828 return -EINVAL;
2829 if (ebitmap_write(&t->negset, fp))
2830 return -EINVAL;
2831
2832 buf[0] = cpu_to_le32(t->flags);
2833 rc = put_entry(buf, sizeof(u32), 1, fp);
2834 if (rc)
2835 return -EINVAL;
2836
2837 return 0;
2838}
2839
Eric Pariscee74f42010-10-13 17:50:25 -04002840static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2841 void *fp)
2842{
2843 struct constraint_node *c;
2844 struct constraint_expr *e;
2845 __le32 buf[3];
2846 u32 nel;
2847 int rc;
2848
2849 for (c = node; c; c = c->next) {
2850 nel = 0;
2851 for (e = c->expr; e; e = e->next)
2852 nel++;
2853 buf[0] = cpu_to_le32(c->permissions);
2854 buf[1] = cpu_to_le32(nel);
2855 rc = put_entry(buf, sizeof(u32), 2, fp);
2856 if (rc)
2857 return rc;
2858 for (e = c->expr; e; e = e->next) {
2859 buf[0] = cpu_to_le32(e->expr_type);
2860 buf[1] = cpu_to_le32(e->attr);
2861 buf[2] = cpu_to_le32(e->op);
2862 rc = put_entry(buf, sizeof(u32), 3, fp);
2863 if (rc)
2864 return rc;
2865
2866 switch (e->expr_type) {
2867 case CEXPR_NAMES:
2868 rc = ebitmap_write(&e->names, fp);
2869 if (rc)
2870 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05002871 if (p->policyvers >=
2872 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2873 rc = type_set_write(e->type_names, fp);
2874 if (rc)
2875 return rc;
2876 }
Eric Pariscee74f42010-10-13 17:50:25 -04002877 break;
2878 default:
2879 break;
2880 }
2881 }
2882 }
2883
2884 return 0;
2885}
2886
2887static int class_write(void *vkey, void *datum, void *ptr)
2888{
2889 char *key = vkey;
2890 struct class_datum *cladatum = datum;
2891 struct policy_data *pd = ptr;
2892 void *fp = pd->fp;
2893 struct policydb *p = pd->p;
2894 struct constraint_node *c;
2895 __le32 buf[6];
2896 u32 ncons;
2897 size_t len, len2;
2898 int rc;
2899
2900 len = strlen(key);
2901 if (cladatum->comkey)
2902 len2 = strlen(cladatum->comkey);
2903 else
2904 len2 = 0;
2905
2906 ncons = 0;
2907 for (c = cladatum->constraints; c; c = c->next)
2908 ncons++;
2909
2910 buf[0] = cpu_to_le32(len);
2911 buf[1] = cpu_to_le32(len2);
2912 buf[2] = cpu_to_le32(cladatum->value);
2913 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2914 if (cladatum->permissions.table)
2915 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2916 else
2917 buf[4] = 0;
2918 buf[5] = cpu_to_le32(ncons);
2919 rc = put_entry(buf, sizeof(u32), 6, fp);
2920 if (rc)
2921 return rc;
2922
2923 rc = put_entry(key, 1, len, fp);
2924 if (rc)
2925 return rc;
2926
2927 if (cladatum->comkey) {
2928 rc = put_entry(cladatum->comkey, 1, len2, fp);
2929 if (rc)
2930 return rc;
2931 }
2932
2933 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2934 if (rc)
2935 return rc;
2936
2937 rc = write_cons_helper(p, cladatum->constraints, fp);
2938 if (rc)
2939 return rc;
2940
2941 /* write out the validatetrans rule */
2942 ncons = 0;
2943 for (c = cladatum->validatetrans; c; c = c->next)
2944 ncons++;
2945
2946 buf[0] = cpu_to_le32(ncons);
2947 rc = put_entry(buf, sizeof(u32), 1, fp);
2948 if (rc)
2949 return rc;
2950
2951 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2952 if (rc)
2953 return rc;
2954
Eric Parisaa893262012-03-20 14:35:12 -04002955 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2956 buf[0] = cpu_to_le32(cladatum->default_user);
2957 buf[1] = cpu_to_le32(cladatum->default_role);
2958 buf[2] = cpu_to_le32(cladatum->default_range);
2959
2960 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2961 if (rc)
2962 return rc;
2963 }
2964
Eric Pariseed77952012-03-20 14:35:12 -04002965 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2966 buf[0] = cpu_to_le32(cladatum->default_type);
2967 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2968 if (rc)
2969 return rc;
2970 }
2971
Eric Pariscee74f42010-10-13 17:50:25 -04002972 return 0;
2973}
2974
2975static int role_write(void *vkey, void *datum, void *ptr)
2976{
2977 char *key = vkey;
2978 struct role_datum *role = datum;
2979 struct policy_data *pd = ptr;
2980 void *fp = pd->fp;
2981 struct policydb *p = pd->p;
2982 __le32 buf[3];
2983 size_t items, len;
2984 int rc;
2985
2986 len = strlen(key);
2987 items = 0;
2988 buf[items++] = cpu_to_le32(len);
2989 buf[items++] = cpu_to_le32(role->value);
2990 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2991 buf[items++] = cpu_to_le32(role->bounds);
2992
2993 BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
2994
2995 rc = put_entry(buf, sizeof(u32), items, fp);
2996 if (rc)
2997 return rc;
2998
2999 rc = put_entry(key, 1, len, fp);
3000 if (rc)
3001 return rc;
3002
3003 rc = ebitmap_write(&role->dominates, fp);
3004 if (rc)
3005 return rc;
3006
3007 rc = ebitmap_write(&role->types, fp);
3008 if (rc)
3009 return rc;
3010
3011 return 0;
3012}
3013
3014static int type_write(void *vkey, void *datum, void *ptr)
3015{
3016 char *key = vkey;
3017 struct type_datum *typdatum = datum;
3018 struct policy_data *pd = ptr;
3019 struct policydb *p = pd->p;
3020 void *fp = pd->fp;
3021 __le32 buf[4];
3022 int rc;
3023 size_t items, len;
3024
3025 len = strlen(key);
3026 items = 0;
3027 buf[items++] = cpu_to_le32(len);
3028 buf[items++] = cpu_to_le32(typdatum->value);
3029 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
3030 u32 properties = 0;
3031
3032 if (typdatum->primary)
3033 properties |= TYPEDATUM_PROPERTY_PRIMARY;
3034
3035 if (typdatum->attribute)
3036 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3037
3038 buf[items++] = cpu_to_le32(properties);
3039 buf[items++] = cpu_to_le32(typdatum->bounds);
3040 } else {
3041 buf[items++] = cpu_to_le32(typdatum->primary);
3042 }
3043 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
3044 rc = put_entry(buf, sizeof(u32), items, fp);
3045 if (rc)
3046 return rc;
3047
3048 rc = put_entry(key, 1, len, fp);
3049 if (rc)
3050 return rc;
3051
3052 return 0;
3053}
3054
3055static int user_write(void *vkey, void *datum, void *ptr)
3056{
3057 char *key = vkey;
3058 struct user_datum *usrdatum = datum;
3059 struct policy_data *pd = ptr;
3060 struct policydb *p = pd->p;
3061 void *fp = pd->fp;
3062 __le32 buf[3];
3063 size_t items, len;
3064 int rc;
3065
3066 len = strlen(key);
3067 items = 0;
3068 buf[items++] = cpu_to_le32(len);
3069 buf[items++] = cpu_to_le32(usrdatum->value);
3070 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3071 buf[items++] = cpu_to_le32(usrdatum->bounds);
3072 BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
3073 rc = put_entry(buf, sizeof(u32), items, fp);
3074 if (rc)
3075 return rc;
3076
3077 rc = put_entry(key, 1, len, fp);
3078 if (rc)
3079 return rc;
3080
3081 rc = ebitmap_write(&usrdatum->roles, fp);
3082 if (rc)
3083 return rc;
3084
3085 rc = mls_write_range_helper(&usrdatum->range, fp);
3086 if (rc)
3087 return rc;
3088
3089 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3090 if (rc)
3091 return rc;
3092
3093 return 0;
3094}
3095
3096static int (*write_f[SYM_NUM]) (void *key, void *datum,
3097 void *datap) =
3098{
3099 common_write,
3100 class_write,
3101 role_write,
3102 type_write,
3103 user_write,
3104 cond_write_bool,
3105 sens_write,
3106 cat_write,
3107};
3108
3109static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3110 void *fp)
3111{
3112 unsigned int i, j, rc;
3113 size_t nel, len;
3114 __le32 buf[3];
3115 u32 nodebuf[8];
3116 struct ocontext *c;
3117 for (i = 0; i < info->ocon_num; i++) {
3118 nel = 0;
3119 for (c = p->ocontexts[i]; c; c = c->next)
3120 nel++;
3121 buf[0] = cpu_to_le32(nel);
3122 rc = put_entry(buf, sizeof(u32), 1, fp);
3123 if (rc)
3124 return rc;
3125 for (c = p->ocontexts[i]; c; c = c->next) {
3126 switch (i) {
3127 case OCON_ISID:
3128 buf[0] = cpu_to_le32(c->sid[0]);
3129 rc = put_entry(buf, sizeof(u32), 1, fp);
3130 if (rc)
3131 return rc;
3132 rc = context_write(p, &c->context[0], fp);
3133 if (rc)
3134 return rc;
3135 break;
3136 case OCON_FS:
3137 case OCON_NETIF:
3138 len = strlen(c->u.name);
3139 buf[0] = cpu_to_le32(len);
3140 rc = put_entry(buf, sizeof(u32), 1, fp);
3141 if (rc)
3142 return rc;
3143 rc = put_entry(c->u.name, 1, len, fp);
3144 if (rc)
3145 return rc;
3146 rc = context_write(p, &c->context[0], fp);
3147 if (rc)
3148 return rc;
3149 rc = context_write(p, &c->context[1], fp);
3150 if (rc)
3151 return rc;
3152 break;
3153 case OCON_PORT:
3154 buf[0] = cpu_to_le32(c->u.port.protocol);
3155 buf[1] = cpu_to_le32(c->u.port.low_port);
3156 buf[2] = cpu_to_le32(c->u.port.high_port);
3157 rc = put_entry(buf, sizeof(u32), 3, fp);
3158 if (rc)
3159 return rc;
3160 rc = context_write(p, &c->context[0], fp);
3161 if (rc)
3162 return rc;
3163 break;
3164 case OCON_NODE:
3165 nodebuf[0] = c->u.node.addr; /* network order */
3166 nodebuf[1] = c->u.node.mask; /* network order */
3167 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3168 if (rc)
3169 return rc;
3170 rc = context_write(p, &c->context[0], fp);
3171 if (rc)
3172 return rc;
3173 break;
3174 case OCON_FSUSE:
3175 buf[0] = cpu_to_le32(c->v.behavior);
3176 len = strlen(c->u.name);
3177 buf[1] = cpu_to_le32(len);
3178 rc = put_entry(buf, sizeof(u32), 2, fp);
3179 if (rc)
3180 return rc;
3181 rc = put_entry(c->u.name, 1, len, fp);
3182 if (rc)
3183 return rc;
3184 rc = context_write(p, &c->context[0], fp);
3185 if (rc)
3186 return rc;
3187 break;
3188 case OCON_NODE6:
3189 for (j = 0; j < 4; j++)
3190 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3191 for (j = 0; j < 4; j++)
3192 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3193 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3194 if (rc)
3195 return rc;
3196 rc = context_write(p, &c->context[0], fp);
3197 if (rc)
3198 return rc;
3199 break;
3200 }
3201 }
3202 }
3203 return 0;
3204}
3205
3206static int genfs_write(struct policydb *p, void *fp)
3207{
3208 struct genfs *genfs;
3209 struct ocontext *c;
3210 size_t len;
3211 __le32 buf[1];
3212 int rc;
3213
3214 len = 0;
3215 for (genfs = p->genfs; genfs; genfs = genfs->next)
3216 len++;
3217 buf[0] = cpu_to_le32(len);
3218 rc = put_entry(buf, sizeof(u32), 1, fp);
3219 if (rc)
3220 return rc;
3221 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3222 len = strlen(genfs->fstype);
3223 buf[0] = cpu_to_le32(len);
3224 rc = put_entry(buf, sizeof(u32), 1, fp);
3225 if (rc)
3226 return rc;
3227 rc = put_entry(genfs->fstype, 1, len, fp);
3228 if (rc)
3229 return rc;
3230 len = 0;
3231 for (c = genfs->head; c; c = c->next)
3232 len++;
3233 buf[0] = cpu_to_le32(len);
3234 rc = put_entry(buf, sizeof(u32), 1, fp);
3235 if (rc)
3236 return rc;
3237 for (c = genfs->head; c; c = c->next) {
3238 len = strlen(c->u.name);
3239 buf[0] = cpu_to_le32(len);
3240 rc = put_entry(buf, sizeof(u32), 1, fp);
3241 if (rc)
3242 return rc;
3243 rc = put_entry(c->u.name, 1, len, fp);
3244 if (rc)
3245 return rc;
3246 buf[0] = cpu_to_le32(c->v.sclass);
3247 rc = put_entry(buf, sizeof(u32), 1, fp);
3248 if (rc)
3249 return rc;
3250 rc = context_write(p, &c->context[0], fp);
3251 if (rc)
3252 return rc;
3253 }
3254 }
3255 return 0;
3256}
3257
Eric Paris3f058ef2011-04-28 15:11:21 -04003258static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003259{
3260 int *cnt = ptr;
3261 *cnt = *cnt + 1;
3262
3263 return 0;
3264}
3265
3266static int range_write_helper(void *key, void *data, void *ptr)
3267{
3268 __le32 buf[2];
3269 struct range_trans *rt = key;
3270 struct mls_range *r = data;
3271 struct policy_data *pd = ptr;
3272 void *fp = pd->fp;
3273 struct policydb *p = pd->p;
3274 int rc;
3275
3276 buf[0] = cpu_to_le32(rt->source_type);
3277 buf[1] = cpu_to_le32(rt->target_type);
3278 rc = put_entry(buf, sizeof(u32), 2, fp);
3279 if (rc)
3280 return rc;
3281 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3282 buf[0] = cpu_to_le32(rt->target_class);
3283 rc = put_entry(buf, sizeof(u32), 1, fp);
3284 if (rc)
3285 return rc;
3286 }
3287 rc = mls_write_range_helper(r, fp);
3288 if (rc)
3289 return rc;
3290
3291 return 0;
3292}
3293
3294static int range_write(struct policydb *p, void *fp)
3295{
Eric Pariscee74f42010-10-13 17:50:25 -04003296 __le32 buf[1];
Eric Parisb1380042013-07-23 17:38:42 -04003297 int rc, nel;
Eric Pariscee74f42010-10-13 17:50:25 -04003298 struct policy_data pd;
3299
3300 pd.p = p;
3301 pd.fp = fp;
3302
3303 /* count the number of entries in the hashtab */
3304 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003305 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003306 if (rc)
3307 return rc;
3308
3309 buf[0] = cpu_to_le32(nel);
3310 rc = put_entry(buf, sizeof(u32), 1, fp);
3311 if (rc)
3312 return rc;
3313
3314 /* actually write all of the entries */
3315 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3316 if (rc)
3317 return rc;
3318
3319 return 0;
3320}
3321
Eric Paris2463c26d2011-04-28 15:11:21 -04003322static int filename_write_helper(void *key, void *data, void *ptr)
3323{
3324 __le32 buf[4];
3325 struct filename_trans *ft = key;
3326 struct filename_trans_datum *otype = data;
3327 void *fp = ptr;
3328 int rc;
3329 u32 len;
3330
3331 len = strlen(ft->name);
3332 buf[0] = cpu_to_le32(len);
3333 rc = put_entry(buf, sizeof(u32), 1, fp);
3334 if (rc)
3335 return rc;
3336
3337 rc = put_entry(ft->name, sizeof(char), len, fp);
3338 if (rc)
3339 return rc;
3340
Eric Paris9085a642014-02-20 10:56:45 -05003341 buf[0] = cpu_to_le32(ft->stype);
3342 buf[1] = cpu_to_le32(ft->ttype);
3343 buf[2] = cpu_to_le32(ft->tclass);
3344 buf[3] = cpu_to_le32(otype->otype);
Eric Paris2463c26d2011-04-28 15:11:21 -04003345
3346 rc = put_entry(buf, sizeof(u32), 4, fp);
3347 if (rc)
3348 return rc;
3349
3350 return 0;
3351}
3352
Eric Paris652bb9b2011-02-01 11:05:40 -05003353static int filename_trans_write(struct policydb *p, void *fp)
3354{
Eric Paris2463c26d2011-04-28 15:11:21 -04003355 u32 nel;
3356 __le32 buf[1];
Eric Paris652bb9b2011-02-01 11:05:40 -05003357 int rc;
3358
Roy.Lided50982011-05-20 10:38:06 +08003359 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3360 return 0;
3361
Eric Paris2463c26d2011-04-28 15:11:21 -04003362 nel = 0;
3363 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3364 if (rc)
3365 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003366
3367 buf[0] = cpu_to_le32(nel);
3368 rc = put_entry(buf, sizeof(u32), 1, fp);
3369 if (rc)
3370 return rc;
3371
Eric Paris2463c26d2011-04-28 15:11:21 -04003372 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3373 if (rc)
3374 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003375
Eric Paris652bb9b2011-02-01 11:05:40 -05003376 return 0;
3377}
Eric Paris2463c26d2011-04-28 15:11:21 -04003378
Eric Pariscee74f42010-10-13 17:50:25 -04003379/*
3380 * Write the configuration data in a policy database
3381 * structure to a policy database binary representation
3382 * file.
3383 */
3384int policydb_write(struct policydb *p, void *fp)
3385{
3386 unsigned int i, num_syms;
3387 int rc;
3388 __le32 buf[4];
3389 u32 config;
3390 size_t len;
3391 struct policydb_compat_info *info;
3392
3393 /*
3394 * refuse to write policy older than compressed avtab
3395 * to simplify the writer. There are other tests dropped
3396 * since we assume this throughout the writer code. Be
3397 * careful if you ever try to remove this restriction
3398 */
3399 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3400 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3401 " Because it is less than version %d\n", p->policyvers,
3402 POLICYDB_VERSION_AVTAB);
3403 return -EINVAL;
3404 }
3405
3406 config = 0;
3407 if (p->mls_enabled)
3408 config |= POLICYDB_CONFIG_MLS;
3409
3410 if (p->reject_unknown)
3411 config |= REJECT_UNKNOWN;
3412 if (p->allow_unknown)
3413 config |= ALLOW_UNKNOWN;
3414
3415 /* Write the magic number and string identifiers. */
3416 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3417 len = strlen(POLICYDB_STRING);
3418 buf[1] = cpu_to_le32(len);
3419 rc = put_entry(buf, sizeof(u32), 2, fp);
3420 if (rc)
3421 return rc;
3422 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3423 if (rc)
3424 return rc;
3425
3426 /* Write the version, config, and table sizes. */
3427 info = policydb_lookup_compat(p->policyvers);
3428 if (!info) {
3429 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3430 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003431 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003432 }
3433
3434 buf[0] = cpu_to_le32(p->policyvers);
3435 buf[1] = cpu_to_le32(config);
3436 buf[2] = cpu_to_le32(info->sym_num);
3437 buf[3] = cpu_to_le32(info->ocon_num);
3438
3439 rc = put_entry(buf, sizeof(u32), 4, fp);
3440 if (rc)
3441 return rc;
3442
3443 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3444 rc = ebitmap_write(&p->policycaps, fp);
3445 if (rc)
3446 return rc;
3447 }
3448
3449 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3450 rc = ebitmap_write(&p->permissive_map, fp);
3451 if (rc)
3452 return rc;
3453 }
3454
3455 num_syms = info->sym_num;
3456 for (i = 0; i < num_syms; i++) {
3457 struct policy_data pd;
3458
3459 pd.fp = fp;
3460 pd.p = p;
3461
3462 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3463 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3464
3465 rc = put_entry(buf, sizeof(u32), 2, fp);
3466 if (rc)
3467 return rc;
3468 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3469 if (rc)
3470 return rc;
3471 }
3472
3473 rc = avtab_write(p, &p->te_avtab, fp);
3474 if (rc)
3475 return rc;
3476
3477 rc = cond_write_list(p, p->cond_list, fp);
3478 if (rc)
3479 return rc;
3480
Harry Ciaoc900ff32011-03-25 13:52:00 +08003481 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003482 if (rc)
3483 return rc;
3484
3485 rc = role_allow_write(p->role_allow, fp);
3486 if (rc)
3487 return rc;
3488
Eric Paris652bb9b2011-02-01 11:05:40 -05003489 rc = filename_trans_write(p, fp);
3490 if (rc)
3491 return rc;
3492
Eric Pariscee74f42010-10-13 17:50:25 -04003493 rc = ocontext_write(p, info, fp);
3494 if (rc)
3495 return rc;
3496
3497 rc = genfs_write(p, fp);
3498 if (rc)
3499 return rc;
3500
3501 rc = range_write(p, fp);
3502 if (rc)
3503 return rc;
3504
3505 for (i = 0; i < p->p_types.nprim; i++) {
3506 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3507
3508 BUG_ON(!e);
3509 rc = ebitmap_write(e, fp);
3510 if (rc)
3511 return rc;
3512 }
3513
3514 return 0;
3515}