blob: af9cc839856f65ad81f6e7267b431d97776dd518 [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 },
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400151 {
152 .version = POLICYDB_VERSION_XPERMS_IOCTL,
153 .sym_num = SYM_NUM,
154 .ocon_num = OCON_NUM,
155 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158static struct policydb_compat_info *policydb_lookup_compat(int version)
159{
160 int i;
161 struct policydb_compat_info *info = NULL;
162
Tobias Klauser32725ad2006-01-06 00:11:23 -0800163 for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (policydb_compat[i].version == version) {
165 info = &policydb_compat[i];
166 break;
167 }
168 }
169 return info;
170}
171
172/*
173 * Initialize the role table.
174 */
175static int roles_init(struct policydb *p)
176{
177 char *key = NULL;
178 int rc;
179 struct role_datum *role;
180
Eric Paris9398c7f2010-11-23 11:40:08 -0500181 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800182 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500183 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
Eric Paris9398c7f2010-11-23 11:40:08 -0500185
186 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 role->value = ++p->p_roles.nprim;
Eric Paris9398c7f2010-11-23 11:40:08 -0500188 if (role->value != OBJECT_R_VAL)
189 goto out;
190
191 rc = -ENOMEM;
Julia Lawallb3139bb2010-05-14 21:30:30 +0200192 key = kstrdup(OBJECT_R, GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500193 if (!key)
194 goto out;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 rc = hashtab_insert(p->p_roles.table, key, role);
197 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Paris9398c7f2010-11-23 11:40:08 -0500200 return 0;
201out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 kfree(role);
Eric Paris9398c7f2010-11-23 11:40:08 -0500204 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Eric Paris2463c26d2011-04-28 15:11:21 -0400207static u32 filenametr_hash(struct hashtab *h, const void *k)
208{
209 const struct filename_trans *ft = k;
210 unsigned long hash;
211 unsigned int byte_num;
212 unsigned char focus;
213
214 hash = ft->stype ^ ft->ttype ^ ft->tclass;
215
216 byte_num = 0;
217 while ((focus = ft->name[byte_num++]))
218 hash = partial_name_hash(focus, hash);
219 return hash & (h->size - 1);
220}
221
222static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
223{
224 const struct filename_trans *ft1 = k1;
225 const struct filename_trans *ft2 = k2;
226 int v;
227
228 v = ft1->stype - ft2->stype;
229 if (v)
230 return v;
231
232 v = ft1->ttype - ft2->ttype;
233 if (v)
234 return v;
235
236 v = ft1->tclass - ft2->tclass;
237 if (v)
238 return v;
239
240 return strcmp(ft1->name, ft2->name);
241
242}
243
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500244static u32 rangetr_hash(struct hashtab *h, const void *k)
245{
246 const struct range_trans *key = k;
247 return (key->source_type + (key->target_type << 3) +
248 (key->target_class << 5)) & (h->size - 1);
249}
250
251static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
252{
253 const struct range_trans *key1 = k1, *key2 = k2;
Eric Paris4419aae2010-10-13 17:50:14 -0400254 int v;
255
256 v = key1->source_type - key2->source_type;
257 if (v)
258 return v;
259
260 v = key1->target_type - key2->target_type;
261 if (v)
262 return v;
263
264 v = key1->target_class - key2->target_class;
265
266 return v;
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500267}
268
Ondrej Mosnacekae190f02019-07-25 12:52:43 +0200269static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
272 * Initialize a policy database structure.
273 */
274static int policydb_init(struct policydb *p)
275{
276 int i, rc;
277
278 memset(p, 0, sizeof(*p));
279
280 for (i = 0; i < SYM_NUM; i++) {
281 rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
282 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 rc = avtab_init(&p->te_avtab);
287 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 rc = roles_init(p);
291 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500292 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 rc = cond_policydb_init(p);
295 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -0500296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Eric Paris2463c26d2011-04-28 15:11:21 -0400298 p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500299 if (!p->filename_trans) {
300 rc = -ENOMEM;
Eric Paris2463c26d2011-04-28 15:11:21 -0400301 goto out;
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500302 }
Eric Paris2463c26d2011-04-28 15:11:21 -0400303
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500304 p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500305 if (!p->range_tr) {
306 rc = -ENOMEM;
Eric Paris9398c7f2010-11-23 11:40:08 -0500307 goto out;
Dan Carpenter6eb4e2b2015-02-04 11:34:30 -0500308 }
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500309
Eric Paris03a4c012011-04-28 15:11:21 -0400310 ebitmap_init(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500311 ebitmap_init(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100312 ebitmap_init(&p->permissive_map);
Paul Moore3bb56b22008-01-29 08:38:19 -0500313
Eric Paris9398c7f2010-11-23 11:40:08 -0500314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315out:
Eric Paris2463c26d2011-04-28 15:11:21 -0400316 hashtab_destroy(p->filename_trans);
317 hashtab_destroy(p->range_tr);
Ondrej Mosnacekae190f02019-07-25 12:52:43 +0200318 for (i = 0; i < SYM_NUM; i++) {
319 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 hashtab_destroy(p->symtab[i].table);
Ondrej Mosnacekae190f02019-07-25 12:52:43 +0200321 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500322 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * The following *_index functions are used to
327 * define the val_to_name and val_to_struct arrays
328 * in a policy database structure. The val_to_name
329 * arrays are used when converting security context
330 * structures into string representations. The
331 * val_to_struct arrays are used when the attributes
332 * of a class, role, or user are needed.
333 */
334
335static int common_index(void *key, void *datum, void *datap)
336{
337 struct policydb *p;
338 struct common_datum *comdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500339 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 comdatum = datum;
342 p = datap;
343 if (!comdatum->value || comdatum->value > p->p_commons.nprim)
344 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500345
346 fa = p->sym_val_to_name[SYM_COMMONS];
347 if (flex_array_put_ptr(fa, comdatum->value - 1, key,
348 GFP_KERNEL | __GFP_ZERO))
349 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
353static int class_index(void *key, void *datum, void *datap)
354{
355 struct policydb *p;
356 struct class_datum *cladatum;
Eric Parisac76c052010-11-29 15:47:09 -0500357 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 cladatum = datum;
360 p = datap;
361 if (!cladatum->value || cladatum->value > p->p_classes.nprim)
362 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500363 fa = p->sym_val_to_name[SYM_CLASSES];
364 if (flex_array_put_ptr(fa, cladatum->value - 1, key,
365 GFP_KERNEL | __GFP_ZERO))
366 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 p->class_val_to_struct[cladatum->value - 1] = cladatum;
368 return 0;
369}
370
371static int role_index(void *key, void *datum, void *datap)
372{
373 struct policydb *p;
374 struct role_datum *role;
Eric Parisac76c052010-11-29 15:47:09 -0500375 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 role = datum;
378 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900379 if (!role->value
380 || role->value > p->p_roles.nprim
381 || role->bounds > p->p_roles.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500383
384 fa = p->sym_val_to_name[SYM_ROLES];
385 if (flex_array_put_ptr(fa, role->value - 1, key,
386 GFP_KERNEL | __GFP_ZERO))
387 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 p->role_val_to_struct[role->value - 1] = role;
389 return 0;
390}
391
392static int type_index(void *key, void *datum, void *datap)
393{
394 struct policydb *p;
395 struct type_datum *typdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500396 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 typdatum = datum;
399 p = datap;
400
401 if (typdatum->primary) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900402 if (!typdatum->value
403 || typdatum->value > p->p_types.nprim
404 || typdatum->bounds > p->p_types.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500406 fa = p->sym_val_to_name[SYM_TYPES];
407 if (flex_array_put_ptr(fa, typdatum->value - 1, key,
408 GFP_KERNEL | __GFP_ZERO))
409 BUG();
410
411 fa = p->type_val_to_struct_array;
412 if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
Eric Paris23bdecb2010-11-29 15:47:09 -0500413 GFP_KERNEL | __GFP_ZERO))
414 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 return 0;
418}
419
420static int user_index(void *key, void *datum, void *datap)
421{
422 struct policydb *p;
423 struct user_datum *usrdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500424 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 usrdatum = datum;
427 p = datap;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900428 if (!usrdatum->value
429 || usrdatum->value > p->p_users.nprim
430 || usrdatum->bounds > p->p_users.nprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500432
433 fa = p->sym_val_to_name[SYM_USERS];
434 if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
435 GFP_KERNEL | __GFP_ZERO))
436 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
438 return 0;
439}
440
441static int sens_index(void *key, void *datum, void *datap)
442{
443 struct policydb *p;
444 struct level_datum *levdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500445 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 levdatum = datum;
448 p = datap;
449
450 if (!levdatum->isalias) {
451 if (!levdatum->level->sens ||
452 levdatum->level->sens > p->p_levels.nprim)
453 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500454 fa = p->sym_val_to_name[SYM_LEVELS];
455 if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
456 GFP_KERNEL | __GFP_ZERO))
457 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 return 0;
461}
462
463static int cat_index(void *key, void *datum, void *datap)
464{
465 struct policydb *p;
466 struct cat_datum *catdatum;
Eric Parisac76c052010-11-29 15:47:09 -0500467 struct flex_array *fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 catdatum = datum;
470 p = datap;
471
472 if (!catdatum->isalias) {
473 if (!catdatum->value || catdatum->value > p->p_cats.nprim)
474 return -EINVAL;
Eric Parisac76c052010-11-29 15:47:09 -0500475 fa = p->sym_val_to_name[SYM_CATS];
476 if (flex_array_put_ptr(fa, catdatum->value - 1, key,
477 GFP_KERNEL | __GFP_ZERO))
478 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 return 0;
482}
483
484static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
485{
486 common_index,
487 class_index,
488 role_index,
489 type_index,
490 user_index,
491 cond_index_bool,
492 sens_index,
493 cat_index,
494};
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#ifdef DEBUG_HASHES
Eric Parisbe30b162011-04-28 15:11:21 -0400497static void hash_eval(struct hashtab *h, const char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500498{
499 struct hashtab_info info;
500
501 hashtab_stat(h, &info);
Eric Parisbe30b162011-04-28 15:11:21 -0400502 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
503 "longest chain length %d\n", hash_name, h->nel,
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500504 info.slots_used, h->size, info.max_chain_len);
505}
Eric Parisbe30b162011-04-28 15:11:21 -0400506
507static void symtab_hash_eval(struct symtab *s)
508{
509 int i;
510
511 for (i = 0; i < SYM_NUM; i++)
512 hash_eval(s[i].table, symtab_name[i]);
513}
514
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500515#else
Eric Parisbe30b162011-04-28 15:11:21 -0400516static inline void hash_eval(struct hashtab *h, char *hash_name)
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500517{
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#endif
520
521/*
522 * Define the other val_to_name and val_to_struct arrays
523 * in a policy database structure.
524 *
525 * Caller must clean up on failure.
526 */
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500527static int policydb_index(struct policydb *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric Paris9398c7f2010-11-23 11:40:08 -0500529 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
James Morris454d9722008-02-26 20:42:02 +1100531 printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100533 if (p->mls_enabled)
Linus Torvalds4bcc5952016-10-08 20:32:40 -0700534 printk(KERN_CONT ", %d sens, %d cats", p->p_levels.nprim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 p->p_cats.nprim);
Linus Torvalds4bcc5952016-10-08 20:32:40 -0700536 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
James Morris454d9722008-02-26 20:42:02 +1100538 printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 p->p_classes.nprim, p->te_avtab.nel);
540
541#ifdef DEBUG_HASHES
542 avtab_hash_eval(&p->te_avtab, "rules");
543 symtab_hash_eval(p->symtab);
544#endif
545
Eric Paris9398c7f2010-11-23 11:40:08 -0500546 rc = -ENOMEM;
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500547 p->class_val_to_struct =
William Roberts3bc7bcf2016-08-23 13:49:24 -0700548 kzalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500549 GFP_KERNEL);
550 if (!p->class_val_to_struct)
551 goto out;
552
553 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 p->role_val_to_struct =
William Roberts3bc7bcf2016-08-23 13:49:24 -0700555 kzalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400556 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500557 if (!p->role_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Eric Paris9398c7f2010-11-23 11:40:08 -0500560 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 p->user_val_to_struct =
William Roberts3bc7bcf2016-08-23 13:49:24 -0700562 kzalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
Eric Paris2ced3df2008-04-17 13:37:12 -0400563 GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -0500564 if (!p->user_val_to_struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Eric Paris23bdecb2010-11-29 15:47:09 -0500567 /* Yes, I want the sizeof the pointer, not the structure */
Eric Paris9398c7f2010-11-23 11:40:08 -0500568 rc = -ENOMEM;
Eric Paris23bdecb2010-11-29 15:47:09 -0500569 p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
570 p->p_types.nprim,
571 GFP_KERNEL | __GFP_ZERO);
572 if (!p->type_val_to_struct_array)
573 goto out;
574
575 rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
Eric Paris5a3ea872011-04-28 15:55:52 -0400576 p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
Eric Paris23bdecb2010-11-29 15:47:09 -0500577 if (rc)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900578 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900579
Davidlohr Bueso3ac285ff2011-01-21 12:28:04 -0300580 rc = cond_init_bool_indexes(p);
581 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Eric Paris1d9bc6dc2010-11-29 15:47:09 -0500584 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500585 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -0500586 p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
587 p->symtab[i].nprim,
588 GFP_KERNEL | __GFP_ZERO);
Eric Paris9398c7f2010-11-23 11:40:08 -0500589 if (!p->sym_val_to_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto out;
Eric Parisac76c052010-11-29 15:47:09 -0500591
592 rc = flex_array_prealloc(p->sym_val_to_name[i],
Eric Paris5a3ea872011-04-28 15:55:52 -0400593 0, p->symtab[i].nprim,
Eric Parisac76c052010-11-29 15:47:09 -0500594 GFP_KERNEL | __GFP_ZERO);
595 if (rc)
596 goto out;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 rc = hashtab_map(p->symtab[i].table, index_f[i], p);
599 if (rc)
600 goto out;
601 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500602 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603out:
604 return rc;
605}
606
607/*
608 * The following *_destroy functions are used to
609 * free any memory allocated for each kind of
610 * symbol data in the policy database.
611 */
612
613static int perm_destroy(void *key, void *datum, void *p)
614{
615 kfree(key);
616 kfree(datum);
617 return 0;
618}
619
620static int common_destroy(void *key, void *datum, void *p)
621{
622 struct common_datum *comdatum;
623
624 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500625 if (datum) {
626 comdatum = datum;
627 hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
628 hashtab_destroy(comdatum->permissions.table);
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 kfree(datum);
631 return 0;
632}
633
Richard Hainesa660bec2013-11-19 17:34:23 -0500634static void constraint_expr_destroy(struct constraint_expr *expr)
635{
636 if (expr) {
637 ebitmap_destroy(&expr->names);
638 if (expr->type_names) {
639 ebitmap_destroy(&expr->type_names->types);
640 ebitmap_destroy(&expr->type_names->negset);
641 kfree(expr->type_names);
642 }
643 kfree(expr);
644 }
645}
646
James Morris6cbda6b2006-11-29 16:50:27 -0500647static int cls_destroy(void *key, void *datum, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct class_datum *cladatum;
650 struct constraint_node *constraint, *ctemp;
651 struct constraint_expr *e, *etmp;
652
653 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500654 if (datum) {
655 cladatum = datum;
656 hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
657 hashtab_destroy(cladatum->permissions.table);
658 constraint = cladatum->constraints;
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Eric Paris9398c7f2010-11-23 11:40:08 -0500671 constraint = cladatum->validatetrans;
672 while (constraint) {
673 e = constraint->expr;
674 while (e) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500675 etmp = e;
676 e = e->next;
Richard Hainesa660bec2013-11-19 17:34:23 -0500677 constraint_expr_destroy(etmp);
Eric Paris9398c7f2010-11-23 11:40:08 -0500678 }
679 ctemp = constraint;
680 constraint = constraint->next;
681 kfree(ctemp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500683 kfree(cladatum->comkey);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 kfree(datum);
686 return 0;
687}
688
689static int role_destroy(void *key, void *datum, void *p)
690{
691 struct role_datum *role;
692
693 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500694 if (datum) {
695 role = datum;
696 ebitmap_destroy(&role->dominates);
697 ebitmap_destroy(&role->types);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(datum);
700 return 0;
701}
702
703static int type_destroy(void *key, void *datum, void *p)
704{
705 kfree(key);
706 kfree(datum);
707 return 0;
708}
709
710static int user_destroy(void *key, void *datum, void *p)
711{
712 struct user_datum *usrdatum;
713
714 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500715 if (datum) {
716 usrdatum = datum;
717 ebitmap_destroy(&usrdatum->roles);
718 ebitmap_destroy(&usrdatum->range.level[0].cat);
719 ebitmap_destroy(&usrdatum->range.level[1].cat);
720 ebitmap_destroy(&usrdatum->dfltlevel.cat);
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 kfree(datum);
723 return 0;
724}
725
726static int sens_destroy(void *key, void *datum, void *p)
727{
728 struct level_datum *levdatum;
729
730 kfree(key);
Eric Paris9398c7f2010-11-23 11:40:08 -0500731 if (datum) {
732 levdatum = datum;
Stephen Smalleyaedbb452019-01-09 10:55:10 -0500733 if (levdatum->level)
734 ebitmap_destroy(&levdatum->level->cat);
Eric Paris9398c7f2010-11-23 11:40:08 -0500735 kfree(levdatum->level);
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 kfree(datum);
738 return 0;
739}
740
741static int cat_destroy(void *key, void *datum, void *p)
742{
743 kfree(key);
744 kfree(datum);
745 return 0;
746}
747
748static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
749{
750 common_destroy,
James Morris6cbda6b2006-11-29 16:50:27 -0500751 cls_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 role_destroy,
753 type_destroy,
754 user_destroy,
755 cond_destroy_bool,
756 sens_destroy,
757 cat_destroy,
758};
759
Eric Paris2463c26d2011-04-28 15:11:21 -0400760static int filenametr_destroy(void *key, void *datum, void *p)
761{
762 struct filename_trans *ft = key;
763 kfree(ft->name);
764 kfree(key);
765 kfree(datum);
766 cond_resched();
767 return 0;
768}
769
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500770static int range_tr_destroy(void *key, void *datum, void *p)
771{
772 struct mls_range *rt = datum;
773 kfree(key);
774 ebitmap_destroy(&rt->level[0].cat);
775 ebitmap_destroy(&rt->level[1].cat);
776 kfree(datum);
777 cond_resched();
778 return 0;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static void ocontext_destroy(struct ocontext *c, int i)
782{
Eric Parisd1b43542010-07-21 12:50:57 -0400783 if (!c)
784 return;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 context_destroy(&c->context[0]);
787 context_destroy(&c->context[1]);
788 if (i == OCON_ISID || i == OCON_FS ||
789 i == OCON_NETIF || i == OCON_FSUSE)
790 kfree(c->u.name);
791 kfree(c);
792}
793
794/*
795 * Free any memory allocated by a policy database structure.
796 */
797void policydb_destroy(struct policydb *p)
798{
799 struct ocontext *c, *ctmp;
800 struct genfs *g, *gtmp;
801 int i;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700802 struct role_allow *ra, *lra = NULL;
803 struct role_trans *tr, *ltr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 for (i = 0; i < SYM_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400806 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
808 hashtab_destroy(p->symtab[i].table);
809 }
810
Eric Parisac76c052010-11-29 15:47:09 -0500811 for (i = 0; i < SYM_NUM; i++) {
812 if (p->sym_val_to_name[i])
813 flex_array_free(p->sym_val_to_name[i]);
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700816 kfree(p->class_val_to_struct);
817 kfree(p->role_val_to_struct);
818 kfree(p->user_val_to_struct);
Eric Paris23bdecb2010-11-29 15:47:09 -0500819 if (p->type_val_to_struct_array)
820 flex_array_free(p->type_val_to_struct_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 avtab_destroy(&p->te_avtab);
823
824 for (i = 0; i < OCON_NUM; i++) {
Eric Paris9dc99782007-06-04 17:41:22 -0400825 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 c = p->ocontexts[i];
827 while (c) {
828 ctmp = c;
829 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400830 ocontext_destroy(ctmp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400832 p->ocontexts[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 g = p->genfs;
836 while (g) {
Eric Paris9dc99782007-06-04 17:41:22 -0400837 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree(g->fstype);
839 c = g->head;
840 while (c) {
841 ctmp = c;
842 c = c->next;
Eric Paris2ced3df2008-04-17 13:37:12 -0400843 ocontext_destroy(ctmp, OCON_FSUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 gtmp = g;
846 g = g->next;
847 kfree(gtmp);
848 }
Chad Sellers6e8c7512006-10-06 16:09:52 -0400849 p->genfs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 cond_policydb_destroy(p);
852
Stephen Smalley782ebb92005-09-03 15:55:16 -0700853 for (tr = p->role_tr; tr; tr = tr->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400854 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800855 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700856 ltr = tr;
857 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800858 kfree(ltr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700859
Eric Paris2ced3df2008-04-17 13:37:12 -0400860 for (ra = p->role_allow; ra; ra = ra->next) {
Eric Paris9dc99782007-06-04 17:41:22 -0400861 cond_resched();
Jesper Juhla7f988b2005-11-07 01:01:35 -0800862 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700863 lra = ra;
864 }
Jesper Juhla7f988b2005-11-07 01:01:35 -0800865 kfree(lra);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700866
Eric Paris2463c26d2011-04-28 15:11:21 -0400867 hashtab_map(p->filename_trans, filenametr_destroy, NULL);
868 hashtab_destroy(p->filename_trans);
869
Stephen Smalley2f3e82d2010-01-07 15:55:16 -0500870 hashtab_map(p->range_tr, range_tr_destroy, NULL);
871 hashtab_destroy(p->range_tr);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700872
Eric Paris6371dcd2010-07-29 23:02:34 -0400873 if (p->type_attr_map_array) {
874 for (i = 0; i < p->p_types.nprim; i++) {
875 struct ebitmap *e;
876
877 e = flex_array_get(p->type_attr_map_array, i);
878 if (!e)
879 continue;
880 ebitmap_destroy(e);
881 }
882 flex_array_free(p->type_attr_map_array);
Stephen Smalley282c1f52005-10-23 12:57:15 -0700883 }
Eric Paris652bb9b2011-02-01 11:05:40 -0500884
Eric Paris03a4c012011-04-28 15:11:21 -0400885 ebitmap_destroy(&p->filename_trans_ttypes);
Paul Moore3bb56b22008-01-29 08:38:19 -0500886 ebitmap_destroy(&p->policycaps);
Eric Paris64dbf072008-03-31 12:17:33 +1100887 ebitmap_destroy(&p->permissive_map);
Eric Paris3f120702007-09-21 14:37:10 -0400888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return;
890}
891
892/*
893 * Load the initial SIDs specified in a policy database
894 * structure into a SID table.
895 */
896int policydb_load_isids(struct policydb *p, struct sidtab *s)
897{
898 struct ocontext *head, *c;
899 int rc;
900
901 rc = sidtab_init(s);
902 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100903 printk(KERN_ERR "SELinux: out of memory on SID table init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto out;
905 }
906
907 head = p->ocontexts[OCON_ISID];
908 for (c = head; c; c = c->next) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500909 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!c->context[0].user) {
Eric Paris9398c7f2010-11-23 11:40:08 -0500911 printk(KERN_ERR "SELinux: SID %s was never defined.\n",
912 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto out;
914 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500915
916 rc = sidtab_insert(s, c->sid[0], &c->context[0]);
917 if (rc) {
918 printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
919 c->u.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto out;
921 }
922 }
Eric Paris9398c7f2010-11-23 11:40:08 -0500923 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924out:
925 return rc;
926}
927
Stephen Smalley45e54212007-11-07 10:08:00 -0500928int policydb_class_isvalid(struct policydb *p, unsigned int class)
929{
930 if (!class || class > p->p_classes.nprim)
931 return 0;
932 return 1;
933}
934
935int policydb_role_isvalid(struct policydb *p, unsigned int role)
936{
937 if (!role || role > p->p_roles.nprim)
938 return 0;
939 return 1;
940}
941
942int policydb_type_isvalid(struct policydb *p, unsigned int type)
943{
944 if (!type || type > p->p_types.nprim)
945 return 0;
946 return 1;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
950 * Return 1 if the fields in the security context
951 * structure `c' are valid. Return 0 otherwise.
952 */
953int policydb_context_isvalid(struct policydb *p, struct context *c)
954{
955 struct role_datum *role;
956 struct user_datum *usrdatum;
957
958 if (!c->role || c->role > p->p_roles.nprim)
959 return 0;
960
961 if (!c->user || c->user > p->p_users.nprim)
962 return 0;
963
964 if (!c->type || c->type > p->p_types.nprim)
965 return 0;
966
967 if (c->role != OBJECT_R_VAL) {
968 /*
969 * Role must be authorized for the type.
970 */
971 role = p->role_val_to_struct[c->role - 1];
William Roberts3bc7bcf2016-08-23 13:49:24 -0700972 if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* role may not be associated with type */
974 return 0;
975
976 /*
977 * User must be authorized for the role.
978 */
979 usrdatum = p->user_val_to_struct[c->user - 1];
980 if (!usrdatum)
981 return 0;
982
Eric Paris9398c7f2010-11-23 11:40:08 -0500983 if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* user may not be associated with role */
985 return 0;
986 }
987
988 if (!mls_context_isvalid(p, c))
989 return 0;
990
991 return 1;
992}
993
994/*
995 * Read a MLS range structure from a policydb binary
996 * representation file.
997 */
998static int mls_read_range_helper(struct mls_range *r, void *fp)
999{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001000 __le32 buf[2];
1001 u32 items;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int rc;
1003
1004 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001005 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 goto out;
1007
Eric Paris9398c7f2010-11-23 11:40:08 -05001008 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 items = le32_to_cpu(buf[0]);
1010 if (items > ARRAY_SIZE(buf)) {
James Morris454d9722008-02-26 20:42:02 +11001011 printk(KERN_ERR "SELinux: mls: range overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto out;
1013 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 rc = next_entry(buf, fp, sizeof(u32) * items);
Eric Paris9398c7f2010-11-23 11:40:08 -05001016 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001017 printk(KERN_ERR "SELinux: mls: truncated range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto out;
1019 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 r->level[0].sens = le32_to_cpu(buf[0]);
1022 if (items > 1)
1023 r->level[1].sens = le32_to_cpu(buf[1]);
1024 else
1025 r->level[1].sens = r->level[0].sens;
1026
1027 rc = ebitmap_read(&r->level[0].cat, fp);
1028 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001029 printk(KERN_ERR "SELinux: mls: error reading low categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto out;
1031 }
1032 if (items > 1) {
1033 rc = ebitmap_read(&r->level[1].cat, fp);
1034 if (rc) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001035 printk(KERN_ERR "SELinux: mls: error reading high categories\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto bad_high;
1037 }
1038 } else {
1039 rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
1040 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001041 printk(KERN_ERR "SELinux: mls: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto bad_high;
1043 }
1044 }
1045
Eric Paris9398c7f2010-11-23 11:40:08 -05001046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047bad_high:
1048 ebitmap_destroy(&r->level[0].cat);
Eric Paris9398c7f2010-11-23 11:40:08 -05001049out:
1050 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * Read and validate a security context structure
1055 * from a policydb binary representation file.
1056 */
1057static int context_read_and_validate(struct context *c,
1058 struct policydb *p,
1059 void *fp)
1060{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001061 __le32 buf[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int rc;
1063
1064 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001065 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001066 printk(KERN_ERR "SELinux: context truncated\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto out;
1068 }
1069 c->user = le32_to_cpu(buf[0]);
1070 c->role = le32_to_cpu(buf[1]);
1071 c->type = le32_to_cpu(buf[2]);
1072 if (p->policyvers >= POLICYDB_VERSION_MLS) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001073 rc = mls_read_range_helper(&c->range, fp);
1074 if (rc) {
1075 printk(KERN_ERR "SELinux: error reading MLS range of context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto out;
1077 }
1078 }
1079
Eric Paris9398c7f2010-11-23 11:40:08 -05001080 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!policydb_context_isvalid(p, c)) {
James Morris454d9722008-02-26 20:42:02 +11001082 printk(KERN_ERR "SELinux: invalid security context\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 context_destroy(c);
Eric Paris9398c7f2010-11-23 11:40:08 -05001084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Eric Paris9398c7f2010-11-23 11:40:08 -05001086 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087out:
1088 return rc;
1089}
1090
1091/*
1092 * The following *_read functions are used to
1093 * read the symbol data from a policy database
1094 * binary representation file.
1095 */
1096
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001097static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
1098{
1099 int rc;
1100 char *str;
1101
William Roberts7c686af2016-08-30 09:28:11 -07001102 if ((len == 0) || (len == (u32)-1))
1103 return -EINVAL;
1104
Tetsuo Handa47ff7622018-09-08 01:42:58 +09001105 str = kmalloc(len + 1, flags | __GFP_NOWARN);
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001106 if (!str)
1107 return -ENOMEM;
1108
1109 /* it's expected the caller should free the str */
1110 *strp = str;
1111
1112 rc = next_entry(str, fp, len);
1113 if (rc)
1114 return rc;
1115
1116 str[len] = '\0';
1117 return 0;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
1121{
1122 char *key = NULL;
1123 struct perm_datum *perdatum;
1124 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001125 __le32 buf[2];
1126 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Eric Paris9398c7f2010-11-23 11:40:08 -05001128 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001129 perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001130 if (!perdatum)
1131 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001134 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto bad;
1136
1137 len = le32_to_cpu(buf[0]);
1138 perdatum->value = le32_to_cpu(buf[1]);
1139
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001140 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001141 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 rc = hashtab_insert(h, key, perdatum);
1145 if (rc)
1146 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001147
1148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149bad:
1150 perm_destroy(key, perdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001151 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static int common_read(struct policydb *p, struct hashtab *h, void *fp)
1155{
1156 char *key = NULL;
1157 struct common_datum *comdatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001158 __le32 buf[4];
1159 u32 len, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int i, rc;
1161
Eric Paris9398c7f2010-11-23 11:40:08 -05001162 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001163 comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001164 if (!comdatum)
1165 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001168 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto bad;
1170
1171 len = le32_to_cpu(buf[0]);
1172 comdatum->value = le32_to_cpu(buf[1]);
1173
1174 rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
1175 if (rc)
1176 goto bad;
1177 comdatum->permissions.nprim = le32_to_cpu(buf[2]);
1178 nel = le32_to_cpu(buf[3]);
1179
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001180 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001181 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 for (i = 0; i < nel; i++) {
1185 rc = perm_read(p, comdatum->permissions.table, fp);
1186 if (rc)
1187 goto bad;
1188 }
1189
1190 rc = hashtab_insert(h, key, comdatum);
1191 if (rc)
1192 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194bad:
1195 common_destroy(key, comdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001196 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Richard Hainesa660bec2013-11-19 17:34:23 -05001199static void type_set_init(struct type_set *t)
1200{
1201 ebitmap_init(&t->types);
1202 ebitmap_init(&t->negset);
1203}
1204
1205static int type_set_read(struct type_set *t, void *fp)
1206{
1207 __le32 buf[1];
1208 int rc;
1209
1210 if (ebitmap_read(&t->types, fp))
1211 return -EINVAL;
1212 if (ebitmap_read(&t->negset, fp))
1213 return -EINVAL;
1214
1215 rc = next_entry(buf, fp, sizeof(u32));
1216 if (rc < 0)
1217 return -EINVAL;
1218 t->flags = le32_to_cpu(buf[0]);
1219
1220 return 0;
1221}
1222
1223
1224static int read_cons_helper(struct policydb *p,
1225 struct constraint_node **nodep,
1226 int ncons, int allowxtarget, void *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct constraint_node *c, *lc;
1229 struct constraint_expr *e, *le;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001230 __le32 buf[3];
1231 u32 nexpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int rc, i, j, depth;
1233
1234 lc = NULL;
1235 for (i = 0; i < ncons; i++) {
James Morris89d155e2005-10-30 14:59:21 -08001236 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!c)
1238 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Eric Paris2ced3df2008-04-17 13:37:12 -04001240 if (lc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 lc->next = c;
Eric Paris2ced3df2008-04-17 13:37:12 -04001242 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *nodep = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 rc = next_entry(buf, fp, (sizeof(u32) * 2));
Eric Paris9398c7f2010-11-23 11:40:08 -05001246 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return rc;
1248 c->permissions = le32_to_cpu(buf[0]);
1249 nexpr = le32_to_cpu(buf[1]);
1250 le = NULL;
1251 depth = -1;
1252 for (j = 0; j < nexpr; j++) {
James Morris89d155e2005-10-30 14:59:21 -08001253 e = kzalloc(sizeof(*e), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!e)
1255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Eric Paris2ced3df2008-04-17 13:37:12 -04001257 if (le)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 le->next = e;
Eric Paris2ced3df2008-04-17 13:37:12 -04001259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 c->expr = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 rc = next_entry(buf, fp, (sizeof(u32) * 3));
Eric Paris9398c7f2010-11-23 11:40:08 -05001263 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return rc;
1265 e->expr_type = le32_to_cpu(buf[0]);
1266 e->attr = le32_to_cpu(buf[1]);
1267 e->op = le32_to_cpu(buf[2]);
1268
1269 switch (e->expr_type) {
1270 case CEXPR_NOT:
1271 if (depth < 0)
1272 return -EINVAL;
1273 break;
1274 case CEXPR_AND:
1275 case CEXPR_OR:
1276 if (depth < 1)
1277 return -EINVAL;
1278 depth--;
1279 break;
1280 case CEXPR_ATTR:
1281 if (depth == (CEXPR_MAXDEPTH - 1))
1282 return -EINVAL;
1283 depth++;
1284 break;
1285 case CEXPR_NAMES:
1286 if (!allowxtarget && (e->attr & CEXPR_XTARGET))
1287 return -EINVAL;
1288 if (depth == (CEXPR_MAXDEPTH - 1))
1289 return -EINVAL;
1290 depth++;
Eric Paris9398c7f2010-11-23 11:40:08 -05001291 rc = ebitmap_read(&e->names, fp);
1292 if (rc)
1293 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05001294 if (p->policyvers >=
1295 POLICYDB_VERSION_CONSTRAINT_NAMES) {
1296 e->type_names = kzalloc(sizeof
1297 (*e->type_names),
1298 GFP_KERNEL);
1299 if (!e->type_names)
1300 return -ENOMEM;
1301 type_set_init(e->type_names);
1302 rc = type_set_read(e->type_names, fp);
1303 if (rc)
1304 return rc;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
1307 default:
1308 return -EINVAL;
1309 }
1310 le = e;
1311 }
1312 if (depth != 0)
1313 return -EINVAL;
1314 lc = c;
1315 }
1316
1317 return 0;
1318}
1319
1320static int class_read(struct policydb *p, struct hashtab *h, void *fp)
1321{
1322 char *key = NULL;
1323 struct class_datum *cladatum;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001324 __le32 buf[6];
1325 u32 len, len2, ncons, nel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int i, rc;
1327
Eric Paris9398c7f2010-11-23 11:40:08 -05001328 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001329 cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001330 if (!cladatum)
1331 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 rc = next_entry(buf, fp, sizeof(u32)*6);
Eric Paris9398c7f2010-11-23 11:40:08 -05001334 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto bad;
1336
1337 len = le32_to_cpu(buf[0]);
1338 len2 = le32_to_cpu(buf[1]);
1339 cladatum->value = le32_to_cpu(buf[2]);
1340
1341 rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
1342 if (rc)
1343 goto bad;
1344 cladatum->permissions.nprim = le32_to_cpu(buf[3]);
1345 nel = le32_to_cpu(buf[4]);
1346
1347 ncons = le32_to_cpu(buf[5]);
1348
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001349 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001350 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (len2) {
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001354 rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
Eric Paris9398c7f2010-11-23 11:40:08 -05001355 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Eric Paris9398c7f2010-11-23 11:40:08 -05001358 rc = -EINVAL;
1359 cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!cladatum->comdatum) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001361 printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto bad;
1363 }
1364 }
1365 for (i = 0; i < nel; i++) {
1366 rc = perm_read(p, cladatum->permissions.table, fp);
1367 if (rc)
1368 goto bad;
1369 }
1370
Richard Hainesa660bec2013-11-19 17:34:23 -05001371 rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (rc)
1373 goto bad;
1374
1375 if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
1376 /* grab the validatetrans rules */
1377 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05001378 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto bad;
1380 ncons = le32_to_cpu(buf[0]);
Richard Hainesa660bec2013-11-19 17:34:23 -05001381 rc = read_cons_helper(p, &cladatum->validatetrans,
1382 ncons, 1, fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (rc)
1384 goto bad;
1385 }
1386
Eric Parisaa893262012-03-20 14:35:12 -04001387 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
1388 rc = next_entry(buf, fp, sizeof(u32) * 3);
1389 if (rc)
1390 goto bad;
1391
1392 cladatum->default_user = le32_to_cpu(buf[0]);
1393 cladatum->default_role = le32_to_cpu(buf[1]);
1394 cladatum->default_range = le32_to_cpu(buf[2]);
1395 }
1396
Eric Pariseed77952012-03-20 14:35:12 -04001397 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
1398 rc = next_entry(buf, fp, sizeof(u32) * 1);
1399 if (rc)
1400 goto bad;
1401 cladatum->default_type = le32_to_cpu(buf[0]);
1402 }
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 rc = hashtab_insert(h, key, cladatum);
1405 if (rc)
1406 goto bad;
1407
Eric Paris9398c7f2010-11-23 11:40:08 -05001408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409bad:
James Morris6cbda6b2006-11-29 16:50:27 -05001410 cls_destroy(key, cladatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001411 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414static int role_read(struct policydb *p, struct hashtab *h, void *fp)
1415{
1416 char *key = NULL;
1417 struct role_datum *role;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001418 int rc, to_read = 2;
1419 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001420 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Eric Paris9398c7f2010-11-23 11:40:08 -05001422 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001423 role = kzalloc(sizeof(*role), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001424 if (!role)
1425 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
KaiGai Koheid9250de2008-08-28 16:35:57 +09001427 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1428 to_read = 3;
1429
1430 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001431 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto bad;
1433
1434 len = le32_to_cpu(buf[0]);
1435 role->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001436 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1437 role->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001439 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001440 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 rc = ebitmap_read(&role->dominates, fp);
1444 if (rc)
1445 goto bad;
1446
1447 rc = ebitmap_read(&role->types, fp);
1448 if (rc)
1449 goto bad;
1450
1451 if (strcmp(key, OBJECT_R) == 0) {
Eric Paris9398c7f2010-11-23 11:40:08 -05001452 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (role->value != OBJECT_R_VAL) {
Eric Paris744ba352008-04-17 11:52:44 -04001454 printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 OBJECT_R, role->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto bad;
1457 }
1458 rc = 0;
1459 goto bad;
1460 }
1461
1462 rc = hashtab_insert(h, key, role);
1463 if (rc)
1464 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466bad:
1467 role_destroy(key, role, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001468 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471static int type_read(struct policydb *p, struct hashtab *h, void *fp)
1472{
1473 char *key = NULL;
1474 struct type_datum *typdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001475 int rc, to_read = 3;
1476 __le32 buf[4];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001477 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric Paris9398c7f2010-11-23 11:40:08 -05001479 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04001480 typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001481 if (!typdatum)
1482 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
KaiGai Koheid9250de2008-08-28 16:35:57 +09001484 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1485 to_read = 4;
1486
1487 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001488 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto bad;
1490
1491 len = le32_to_cpu(buf[0]);
1492 typdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001493 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
1494 u32 prop = le32_to_cpu(buf[2]);
1495
1496 if (prop & TYPEDATUM_PROPERTY_PRIMARY)
1497 typdatum->primary = 1;
1498 if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
1499 typdatum->attribute = 1;
1500
1501 typdatum->bounds = le32_to_cpu(buf[3]);
1502 } else {
1503 typdatum->primary = le32_to_cpu(buf[2]);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001506 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001507 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 rc = hashtab_insert(h, key, typdatum);
1511 if (rc)
1512 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514bad:
1515 type_destroy(key, typdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001516 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519
1520/*
1521 * Read a MLS level structure from a policydb binary
1522 * representation file.
1523 */
1524static int mls_read_level(struct mls_level *lp, void *fp)
1525{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001526 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int rc;
1528
1529 memset(lp, 0, sizeof(*lp));
1530
1531 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001532 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001533 printk(KERN_ERR "SELinux: mls: truncated level\n");
Eric Paris9398c7f2010-11-23 11:40:08 -05001534 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 lp->sens = le32_to_cpu(buf[0]);
1537
Eric Paris9398c7f2010-11-23 11:40:08 -05001538 rc = ebitmap_read(&lp->cat, fp);
1539 if (rc) {
1540 printk(KERN_ERR "SELinux: mls: error reading level categories\n");
1541 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546static int user_read(struct policydb *p, struct hashtab *h, void *fp)
1547{
1548 char *key = NULL;
1549 struct user_datum *usrdatum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001550 int rc, to_read = 2;
1551 __le32 buf[3];
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001552 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Eric Paris9398c7f2010-11-23 11:40:08 -05001554 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001555 usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001556 if (!usrdatum)
1557 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
KaiGai Koheid9250de2008-08-28 16:35:57 +09001559 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1560 to_read = 3;
1561
1562 rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
Eric Paris9398c7f2010-11-23 11:40:08 -05001563 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto bad;
1565
1566 len = le32_to_cpu(buf[0]);
1567 usrdatum->value = le32_to_cpu(buf[1]);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001568 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
1569 usrdatum->bounds = le32_to_cpu(buf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001571 rc = str_read(&key, GFP_KERNEL, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001572 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 rc = ebitmap_read(&usrdatum->roles, fp);
1576 if (rc)
1577 goto bad;
1578
1579 if (p->policyvers >= POLICYDB_VERSION_MLS) {
1580 rc = mls_read_range_helper(&usrdatum->range, fp);
1581 if (rc)
1582 goto bad;
1583 rc = mls_read_level(&usrdatum->dfltlevel, fp);
1584 if (rc)
1585 goto bad;
1586 }
1587
1588 rc = hashtab_insert(h, key, usrdatum);
1589 if (rc)
1590 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592bad:
1593 user_destroy(key, usrdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001594 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
1598{
1599 char *key = NULL;
1600 struct level_datum *levdatum;
1601 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001602 __le32 buf[2];
1603 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Eric Paris9398c7f2010-11-23 11:40:08 -05001605 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001606 levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001607 if (!levdatum)
1608 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001611 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto bad;
1613
1614 len = le32_to_cpu(buf[0]);
1615 levdatum->isalias = le32_to_cpu(buf[1]);
1616
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001617 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001618 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Eric Paris9398c7f2010-11-23 11:40:08 -05001621 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001623 if (!levdatum->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001625
1626 rc = mls_read_level(levdatum->level, fp);
1627 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 rc = hashtab_insert(h, key, levdatum);
1631 if (rc)
1632 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634bad:
1635 sens_destroy(key, levdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001636 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
1640{
1641 char *key = NULL;
1642 struct cat_datum *catdatum;
1643 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07001644 __le32 buf[3];
1645 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric Paris9398c7f2010-11-23 11:40:08 -05001647 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08001648 catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
Eric Paris9398c7f2010-11-23 11:40:08 -05001649 if (!catdatum)
1650 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 rc = next_entry(buf, fp, sizeof buf);
Eric Paris9398c7f2010-11-23 11:40:08 -05001653 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 goto bad;
1655
1656 len = le32_to_cpu(buf[0]);
1657 catdatum->value = le32_to_cpu(buf[1]);
1658 catdatum->isalias = le32_to_cpu(buf[2]);
1659
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001660 rc = str_read(&key, GFP_ATOMIC, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05001661 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 rc = hashtab_insert(h, key, catdatum);
1665 if (rc)
1666 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05001667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668bad:
1669 cat_destroy(key, catdatum, NULL);
Eric Paris9398c7f2010-11-23 11:40:08 -05001670 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
1673static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
1674{
1675 common_read,
1676 class_read,
1677 role_read,
1678 type_read,
1679 user_read,
1680 cond_read_bool,
1681 sens_read,
1682 cat_read,
1683};
1684
KaiGai Koheid9250de2008-08-28 16:35:57 +09001685static int user_bounds_sanity_check(void *key, void *datum, void *datap)
1686{
1687 struct user_datum *upper, *user;
1688 struct policydb *p = datap;
1689 int depth = 0;
1690
1691 upper = user = datum;
1692 while (upper->bounds) {
1693 struct ebitmap_node *node;
1694 unsigned long bit;
1695
1696 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1697 printk(KERN_ERR "SELinux: user %s: "
1698 "too deep or looped boundary",
1699 (char *) key);
1700 return -EINVAL;
1701 }
1702
1703 upper = p->user_val_to_struct[upper->bounds - 1];
1704 ebitmap_for_each_positive_bit(&user->roles, node, bit) {
1705 if (ebitmap_get_bit(&upper->roles, bit))
1706 continue;
1707
1708 printk(KERN_ERR
1709 "SELinux: boundary violated policy: "
1710 "user=%s role=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001711 sym_name(p, SYM_USERS, user->value - 1),
1712 sym_name(p, SYM_ROLES, bit),
1713 sym_name(p, SYM_USERS, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001714
1715 return -EINVAL;
1716 }
1717 }
1718
1719 return 0;
1720}
1721
1722static int role_bounds_sanity_check(void *key, void *datum, void *datap)
1723{
1724 struct role_datum *upper, *role;
1725 struct policydb *p = datap;
1726 int depth = 0;
1727
1728 upper = role = datum;
1729 while (upper->bounds) {
1730 struct ebitmap_node *node;
1731 unsigned long bit;
1732
1733 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1734 printk(KERN_ERR "SELinux: role %s: "
1735 "too deep or looped bounds\n",
1736 (char *) key);
1737 return -EINVAL;
1738 }
1739
1740 upper = p->role_val_to_struct[upper->bounds - 1];
1741 ebitmap_for_each_positive_bit(&role->types, node, bit) {
1742 if (ebitmap_get_bit(&upper->types, bit))
1743 continue;
1744
1745 printk(KERN_ERR
1746 "SELinux: boundary violated policy: "
1747 "role=%s type=%s bounds=%s\n",
Eric Parisac76c052010-11-29 15:47:09 -05001748 sym_name(p, SYM_ROLES, role->value - 1),
1749 sym_name(p, SYM_TYPES, bit),
1750 sym_name(p, SYM_ROLES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001751
1752 return -EINVAL;
1753 }
1754 }
1755
1756 return 0;
1757}
1758
1759static int type_bounds_sanity_check(void *key, void *datum, void *datap)
1760{
Eric Parisdaa6d832010-08-03 15:26:05 -04001761 struct type_datum *upper;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001762 struct policydb *p = datap;
1763 int depth = 0;
1764
Eric Parisdaa6d832010-08-03 15:26:05 -04001765 upper = datum;
KaiGai Koheid9250de2008-08-28 16:35:57 +09001766 while (upper->bounds) {
1767 if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
1768 printk(KERN_ERR "SELinux: type %s: "
1769 "too deep or looped boundary\n",
1770 (char *) key);
1771 return -EINVAL;
1772 }
1773
Eric Paris23bdecb2010-11-29 15:47:09 -05001774 upper = flex_array_get_ptr(p->type_val_to_struct_array,
1775 upper->bounds - 1);
1776 BUG_ON(!upper);
1777
KaiGai Koheid9250de2008-08-28 16:35:57 +09001778 if (upper->attribute) {
1779 printk(KERN_ERR "SELinux: type %s: "
1780 "bounded by attribute %s",
1781 (char *) key,
Eric Parisac76c052010-11-29 15:47:09 -05001782 sym_name(p, SYM_TYPES, upper->value - 1));
KaiGai Koheid9250de2008-08-28 16:35:57 +09001783 return -EINVAL;
1784 }
1785 }
1786
1787 return 0;
1788}
1789
1790static int policydb_bounds_sanity_check(struct policydb *p)
1791{
1792 int rc;
1793
1794 if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
1795 return 0;
1796
1797 rc = hashtab_map(p->p_users.table,
1798 user_bounds_sanity_check, p);
1799 if (rc)
1800 return rc;
1801
1802 rc = hashtab_map(p->p_roles.table,
1803 role_bounds_sanity_check, p);
1804 if (rc)
1805 return rc;
1806
1807 rc = hashtab_map(p->p_types.table,
1808 type_bounds_sanity_check, p);
1809 if (rc)
1810 return rc;
1811
1812 return 0;
1813}
1814
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001815u16 string_to_security_class(struct policydb *p, const char *name)
1816{
1817 struct class_datum *cladatum;
1818
1819 cladatum = hashtab_search(p->p_classes.table, name);
1820 if (!cladatum)
1821 return 0;
1822
1823 return cladatum->value;
1824}
1825
1826u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
1827{
1828 struct class_datum *cladatum;
1829 struct perm_datum *perdatum = NULL;
1830 struct common_datum *comdatum;
1831
1832 if (!tclass || tclass > p->p_classes.nprim)
1833 return 0;
1834
1835 cladatum = p->class_val_to_struct[tclass-1];
1836 comdatum = cladatum->comdatum;
1837 if (comdatum)
1838 perdatum = hashtab_search(comdatum->permissions.table,
1839 name);
1840 if (!perdatum)
1841 perdatum = hashtab_search(cladatum->permissions.table,
1842 name);
1843 if (!perdatum)
1844 return 0;
1845
1846 return 1U << (perdatum->value-1);
1847}
1848
Eric Paris9ee0c822010-06-11 12:37:05 -04001849static int range_read(struct policydb *p, void *fp)
1850{
1851 struct range_trans *rt = NULL;
1852 struct mls_range *r = NULL;
1853 int i, rc;
1854 __le32 buf[2];
1855 u32 nel;
1856
1857 if (p->policyvers < POLICYDB_VERSION_MLS)
1858 return 0;
1859
1860 rc = next_entry(buf, fp, sizeof(u32));
1861 if (rc)
1862 goto out;
1863
1864 nel = le32_to_cpu(buf[0]);
1865 for (i = 0; i < nel; i++) {
1866 rc = -ENOMEM;
1867 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
1868 if (!rt)
1869 goto out;
1870
1871 rc = next_entry(buf, fp, (sizeof(u32) * 2));
1872 if (rc)
1873 goto out;
1874
1875 rt->source_type = le32_to_cpu(buf[0]);
1876 rt->target_type = le32_to_cpu(buf[1]);
1877 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
1878 rc = next_entry(buf, fp, sizeof(u32));
1879 if (rc)
1880 goto out;
1881 rt->target_class = le32_to_cpu(buf[0]);
1882 } else
1883 rt->target_class = p->process_class;
1884
1885 rc = -EINVAL;
1886 if (!policydb_type_isvalid(p, rt->source_type) ||
1887 !policydb_type_isvalid(p, rt->target_type) ||
1888 !policydb_class_isvalid(p, rt->target_class))
1889 goto out;
1890
1891 rc = -ENOMEM;
1892 r = kzalloc(sizeof(*r), GFP_KERNEL);
1893 if (!r)
1894 goto out;
1895
1896 rc = mls_read_range_helper(r, fp);
1897 if (rc)
1898 goto out;
1899
1900 rc = -EINVAL;
1901 if (!mls_range_isvalid(p, r)) {
1902 printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
1903 goto out;
1904 }
1905
1906 rc = hashtab_insert(p->range_tr, rt, r);
1907 if (rc)
1908 goto out;
1909
1910 rt = NULL;
1911 r = NULL;
1912 }
Eric Parisbe30b162011-04-28 15:11:21 -04001913 hash_eval(p->range_tr, "rangetr");
Eric Paris9ee0c822010-06-11 12:37:05 -04001914 rc = 0;
1915out:
1916 kfree(rt);
1917 kfree(r);
1918 return rc;
1919}
1920
Eric Paris652bb9b2011-02-01 11:05:40 -05001921static int filename_trans_read(struct policydb *p, void *fp)
1922{
Eric Paris2463c26d2011-04-28 15:11:21 -04001923 struct filename_trans *ft;
1924 struct filename_trans_datum *otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001925 char *name;
Eric Paris2463c26d2011-04-28 15:11:21 -04001926 u32 nel, len;
Eric Paris652bb9b2011-02-01 11:05:40 -05001927 __le32 buf[4];
1928 int rc, i;
1929
1930 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1931 return 0;
1932
1933 rc = next_entry(buf, fp, sizeof(u32));
1934 if (rc)
Eric Paris2463c26d2011-04-28 15:11:21 -04001935 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05001936 nel = le32_to_cpu(buf[0]);
1937
Eric Paris652bb9b2011-02-01 11:05:40 -05001938 for (i = 0; i < nel; i++) {
Eric Paris2463c26d2011-04-28 15:11:21 -04001939 ft = NULL;
1940 otype = NULL;
1941 name = NULL;
1942
Eric Paris652bb9b2011-02-01 11:05:40 -05001943 rc = -ENOMEM;
1944 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1945 if (!ft)
1946 goto out;
1947
Eric Paris2463c26d2011-04-28 15:11:21 -04001948 rc = -ENOMEM;
1949 otype = kmalloc(sizeof(*otype), GFP_KERNEL);
1950 if (!otype)
1951 goto out;
Eric Paris652bb9b2011-02-01 11:05:40 -05001952
1953 /* length of the path component string */
1954 rc = next_entry(buf, fp, sizeof(u32));
1955 if (rc)
1956 goto out;
1957 len = le32_to_cpu(buf[0]);
1958
Namhyung Kim4b6f4052014-06-15 23:02:51 +09001959 /* path component string */
1960 rc = str_read(&name, GFP_KERNEL, fp, len);
1961 if (rc)
Eric Paris652bb9b2011-02-01 11:05:40 -05001962 goto out;
1963
1964 ft->name = name;
1965
Eric Paris652bb9b2011-02-01 11:05:40 -05001966 rc = next_entry(buf, fp, sizeof(u32) * 4);
1967 if (rc)
1968 goto out;
1969
1970 ft->stype = le32_to_cpu(buf[0]);
1971 ft->ttype = le32_to_cpu(buf[1]);
1972 ft->tclass = le32_to_cpu(buf[2]);
Eric Paris2463c26d2011-04-28 15:11:21 -04001973
1974 otype->otype = le32_to_cpu(buf[3]);
Eric Paris03a4c012011-04-28 15:11:21 -04001975
1976 rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
1977 if (rc)
1978 goto out;
Eric Paris2463c26d2011-04-28 15:11:21 -04001979
Tetsuo Handa8ed81462014-01-06 21:28:15 +09001980 rc = hashtab_insert(p->filename_trans, ft, otype);
1981 if (rc) {
1982 /*
1983 * Do not return -EEXIST to the caller, or the system
1984 * will not boot.
1985 */
1986 if (rc != -EEXIST)
1987 goto out;
1988 /* But free memory to avoid memory leak. */
1989 kfree(ft);
1990 kfree(name);
1991 kfree(otype);
1992 }
Eric Paris652bb9b2011-02-01 11:05:40 -05001993 }
Eric Paris2463c26d2011-04-28 15:11:21 -04001994 hash_eval(p->filename_trans, "filenametr");
1995 return 0;
Eric Paris652bb9b2011-02-01 11:05:40 -05001996out:
Eric Paris2463c26d2011-04-28 15:11:21 -04001997 kfree(ft);
1998 kfree(name);
1999 kfree(otype);
2000
Eric Paris652bb9b2011-02-01 11:05:40 -05002001 return rc;
2002}
2003
Eric Parisd1b43542010-07-21 12:50:57 -04002004static int genfs_read(struct policydb *p, void *fp)
2005{
2006 int i, j, rc;
2007 u32 nel, nel2, len, len2;
2008 __le32 buf[1];
2009 struct ocontext *l, *c;
2010 struct ocontext *newc = NULL;
2011 struct genfs *genfs_p, *genfs;
2012 struct genfs *newgenfs = NULL;
2013
2014 rc = next_entry(buf, fp, sizeof(u32));
2015 if (rc)
2016 goto out;
2017 nel = le32_to_cpu(buf[0]);
2018
2019 for (i = 0; i < nel; i++) {
2020 rc = next_entry(buf, fp, sizeof(u32));
2021 if (rc)
2022 goto out;
2023 len = le32_to_cpu(buf[0]);
2024
2025 rc = -ENOMEM;
2026 newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
2027 if (!newgenfs)
2028 goto out;
2029
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002030 rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002031 if (rc)
2032 goto out;
2033
Eric Parisd1b43542010-07-21 12:50:57 -04002034 for (genfs_p = NULL, genfs = p->genfs; genfs;
2035 genfs_p = genfs, genfs = genfs->next) {
2036 rc = -EINVAL;
2037 if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
2038 printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
2039 newgenfs->fstype);
2040 goto out;
2041 }
2042 if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
2043 break;
2044 }
2045 newgenfs->next = genfs;
2046 if (genfs_p)
2047 genfs_p->next = newgenfs;
2048 else
2049 p->genfs = newgenfs;
2050 genfs = newgenfs;
2051 newgenfs = NULL;
2052
2053 rc = next_entry(buf, fp, sizeof(u32));
2054 if (rc)
2055 goto out;
2056
2057 nel2 = le32_to_cpu(buf[0]);
2058 for (j = 0; j < nel2; j++) {
2059 rc = next_entry(buf, fp, sizeof(u32));
2060 if (rc)
2061 goto out;
2062 len = le32_to_cpu(buf[0]);
2063
2064 rc = -ENOMEM;
2065 newc = kzalloc(sizeof(*newc), GFP_KERNEL);
2066 if (!newc)
2067 goto out;
2068
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002069 rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
Eric Parisd1b43542010-07-21 12:50:57 -04002070 if (rc)
2071 goto out;
Eric Parisd1b43542010-07-21 12:50:57 -04002072
2073 rc = next_entry(buf, fp, sizeof(u32));
2074 if (rc)
2075 goto out;
2076
2077 newc->v.sclass = le32_to_cpu(buf[0]);
2078 rc = context_read_and_validate(&newc->context[0], p, fp);
2079 if (rc)
2080 goto out;
2081
2082 for (l = NULL, c = genfs->head; c;
2083 l = c, c = c->next) {
2084 rc = -EINVAL;
2085 if (!strcmp(newc->u.name, c->u.name) &&
2086 (!c->v.sclass || !newc->v.sclass ||
2087 newc->v.sclass == c->v.sclass)) {
2088 printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
2089 genfs->fstype, c->u.name);
2090 goto out;
2091 }
2092 len = strlen(newc->u.name);
2093 len2 = strlen(c->u.name);
2094 if (len > len2)
2095 break;
2096 }
2097
2098 newc->next = c;
2099 if (l)
2100 l->next = newc;
2101 else
2102 genfs->head = newc;
2103 newc = NULL;
2104 }
2105 }
2106 rc = 0;
2107out:
2108 if (newgenfs)
2109 kfree(newgenfs->fstype);
2110 kfree(newgenfs);
2111 ocontext_destroy(newc, OCON_FSUSE);
2112
2113 return rc;
2114}
2115
Eric Paris692a8a22010-07-21 12:51:03 -04002116static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
2117 void *fp)
2118{
2119 int i, j, rc;
2120 u32 nel, len;
2121 __le32 buf[3];
2122 struct ocontext *l, *c;
2123 u32 nodebuf[8];
2124
2125 for (i = 0; i < info->ocon_num; i++) {
2126 rc = next_entry(buf, fp, sizeof(u32));
2127 if (rc)
2128 goto out;
2129 nel = le32_to_cpu(buf[0]);
2130
2131 l = NULL;
2132 for (j = 0; j < nel; j++) {
2133 rc = -ENOMEM;
2134 c = kzalloc(sizeof(*c), GFP_KERNEL);
2135 if (!c)
2136 goto out;
2137 if (l)
2138 l->next = c;
2139 else
2140 p->ocontexts[i] = c;
2141 l = c;
2142
2143 switch (i) {
2144 case OCON_ISID:
2145 rc = next_entry(buf, fp, sizeof(u32));
2146 if (rc)
2147 goto out;
2148
2149 c->sid[0] = le32_to_cpu(buf[0]);
2150 rc = context_read_and_validate(&c->context[0], p, fp);
2151 if (rc)
2152 goto out;
2153 break;
2154 case OCON_FS:
2155 case OCON_NETIF:
2156 rc = next_entry(buf, fp, sizeof(u32));
2157 if (rc)
2158 goto out;
2159 len = le32_to_cpu(buf[0]);
2160
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002161 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002162 if (rc)
2163 goto out;
2164
Eric Paris692a8a22010-07-21 12:51:03 -04002165 rc = context_read_and_validate(&c->context[0], p, fp);
2166 if (rc)
2167 goto out;
2168 rc = context_read_and_validate(&c->context[1], p, fp);
2169 if (rc)
2170 goto out;
2171 break;
2172 case OCON_PORT:
2173 rc = next_entry(buf, fp, sizeof(u32)*3);
2174 if (rc)
2175 goto out;
2176 c->u.port.protocol = le32_to_cpu(buf[0]);
2177 c->u.port.low_port = le32_to_cpu(buf[1]);
2178 c->u.port.high_port = le32_to_cpu(buf[2]);
2179 rc = context_read_and_validate(&c->context[0], p, fp);
2180 if (rc)
2181 goto out;
2182 break;
2183 case OCON_NODE:
2184 rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
2185 if (rc)
2186 goto out;
2187 c->u.node.addr = nodebuf[0]; /* network order */
2188 c->u.node.mask = nodebuf[1]; /* network order */
2189 rc = context_read_and_validate(&c->context[0], p, fp);
2190 if (rc)
2191 goto out;
2192 break;
2193 case OCON_FSUSE:
2194 rc = next_entry(buf, fp, sizeof(u32)*2);
2195 if (rc)
2196 goto out;
2197
2198 rc = -EINVAL;
2199 c->v.behavior = le32_to_cpu(buf[0]);
David Quigleyeb9ae682013-05-22 12:50:37 -04002200 /* Determined at runtime, not in policy DB. */
2201 if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
2202 goto out;
2203 if (c->v.behavior > SECURITY_FS_USE_MAX)
Eric Paris692a8a22010-07-21 12:51:03 -04002204 goto out;
2205
Eric Paris692a8a22010-07-21 12:51:03 -04002206 len = le32_to_cpu(buf[1]);
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002207 rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
Eric Paris692a8a22010-07-21 12:51:03 -04002208 if (rc)
2209 goto out;
Namhyung Kim4b6f4052014-06-15 23:02:51 +09002210
Eric Paris692a8a22010-07-21 12:51:03 -04002211 rc = context_read_and_validate(&c->context[0], p, fp);
2212 if (rc)
2213 goto out;
2214 break;
2215 case OCON_NODE6: {
2216 int k;
2217
2218 rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
2219 if (rc)
2220 goto out;
2221 for (k = 0; k < 4; k++)
2222 c->u.node6.addr[k] = nodebuf[k];
2223 for (k = 0; k < 4; k++)
2224 c->u.node6.mask[k] = nodebuf[k+4];
2225 rc = context_read_and_validate(&c->context[0], p, fp);
2226 if (rc)
2227 goto out;
2228 break;
2229 }
2230 }
2231 }
2232 }
2233 rc = 0;
2234out:
2235 return rc;
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/*
2239 * Read the configuration data from a policy database binary
2240 * representation file into a policy database structure.
2241 */
2242int policydb_read(struct policydb *p, void *fp)
2243{
2244 struct role_allow *ra, *lra;
2245 struct role_trans *tr, *ltr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 int i, j, rc;
Stephen Smalley59dbd1b2008-06-05 09:48:51 -04002247 __le32 buf[4];
Eric Parisd1b43542010-07-21 12:50:57 -04002248 u32 len, nprim, nel;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 char *policydb_str;
2251 struct policydb_compat_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rc = policydb_init(p);
2254 if (rc)
Eric Paris9398c7f2010-11-23 11:40:08 -05002255 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* Read the magic number and string length. */
Eric Paris2ced3df2008-04-17 13:37:12 -04002258 rc = next_entry(buf, fp, sizeof(u32) * 2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002259 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 goto bad;
2261
Eric Paris9398c7f2010-11-23 11:40:08 -05002262 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002263 if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
James Morris454d9722008-02-26 20:42:02 +11002264 printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 "not match expected magic number 0x%x\n",
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002266 le32_to_cpu(buf[0]), POLICYDB_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto bad;
2268 }
2269
Eric Paris9398c7f2010-11-23 11:40:08 -05002270 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002271 len = le32_to_cpu(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (len != strlen(POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002273 printk(KERN_ERR "SELinux: policydb string length %d does not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 "match expected length %Zu\n",
2275 len, strlen(POLICYDB_STRING));
2276 goto bad;
2277 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002278
2279 rc = -ENOMEM;
Eric Paris2ced3df2008-04-17 13:37:12 -04002280 policydb_str = kmalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!policydb_str) {
James Morris454d9722008-02-26 20:42:02 +11002282 printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 "string of length %d\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto bad;
2285 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 rc = next_entry(policydb_str, fp, len);
Eric Paris9398c7f2010-11-23 11:40:08 -05002288 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002289 printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 kfree(policydb_str);
2291 goto bad;
2292 }
Eric Paris9398c7f2010-11-23 11:40:08 -05002293
2294 rc = -EINVAL;
Vesa-Matti J Karidf4ea862008-07-20 23:57:01 +03002295 policydb_str[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (strcmp(policydb_str, POLICYDB_STRING)) {
James Morris454d9722008-02-26 20:42:02 +11002297 printk(KERN_ERR "SELinux: policydb string %s does not match "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 "my string %s\n", policydb_str, POLICYDB_STRING);
2299 kfree(policydb_str);
2300 goto bad;
2301 }
2302 /* Done with policydb_str. */
2303 kfree(policydb_str);
2304 policydb_str = NULL;
2305
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002306 /* Read the version and table sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 rc = next_entry(buf, fp, sizeof(u32)*4);
Eric Paris9398c7f2010-11-23 11:40:08 -05002308 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric Paris9398c7f2010-11-23 11:40:08 -05002311 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002312 p->policyvers = le32_to_cpu(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (p->policyvers < POLICYDB_VERSION_MIN ||
2314 p->policyvers > POLICYDB_VERSION_MAX) {
James Morris454d9722008-02-26 20:42:02 +11002315 printk(KERN_ERR "SELinux: policydb version %d does not match "
Eric Paris2ced3df2008-04-17 13:37:12 -04002316 "my version range %d-%d\n",
2317 le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
2318 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002321 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002322 p->mls_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Eric Paris9398c7f2010-11-23 11:40:08 -05002324 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (p->policyvers < POLICYDB_VERSION_MLS) {
Eric Paris744ba352008-04-17 11:52:44 -04002326 printk(KERN_ERR "SELinux: security policydb version %d "
2327 "(MLS) not backwards compatible\n",
2328 p->policyvers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto bad;
2330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Eric Paris3f120702007-09-21 14:37:10 -04002332 p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
2333 p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Eric Paris9398c7f2010-11-23 11:40:08 -05002335 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2336 rc = ebitmap_read(&p->policycaps, fp);
2337 if (rc)
2338 goto bad;
2339 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002340
Eric Paris9398c7f2010-11-23 11:40:08 -05002341 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2342 rc = ebitmap_read(&p->permissive_map, fp);
2343 if (rc)
2344 goto bad;
2345 }
Eric Paris64dbf072008-03-31 12:17:33 +11002346
Eric Paris9398c7f2010-11-23 11:40:08 -05002347 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 info = policydb_lookup_compat(p->policyvers);
2349 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002350 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 "for version %d\n", p->policyvers);
2352 goto bad;
2353 }
2354
Eric Paris9398c7f2010-11-23 11:40:08 -05002355 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002356 if (le32_to_cpu(buf[2]) != info->sym_num ||
2357 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002358 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002359 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2360 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 info->sym_num, info->ocon_num);
2362 goto bad;
2363 }
2364
2365 for (i = 0; i < info->sym_num; i++) {
2366 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002367 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto bad;
2369 nprim = le32_to_cpu(buf[0]);
2370 nel = le32_to_cpu(buf[1]);
2371 for (j = 0; j < nel; j++) {
2372 rc = read_f[i](p, p->symtab[i].table, fp);
2373 if (rc)
2374 goto bad;
2375 }
2376
2377 p->symtab[i].nprim = nprim;
2378 }
2379
Harry Ciao1214eac2011-04-07 14:12:57 +08002380 rc = -EINVAL;
2381 p->process_class = string_to_security_class(p, "process");
2382 if (!p->process_class)
2383 goto bad;
2384
Stephen Smalley45e54212007-11-07 10:08:00 -05002385 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (rc)
2387 goto bad;
2388
2389 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2390 rc = cond_read_list(p, fp);
2391 if (rc)
2392 goto bad;
2393 }
2394
2395 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002396 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 goto bad;
2398 nel = le32_to_cpu(buf[0]);
2399 ltr = NULL;
2400 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002401 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002402 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002403 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002405 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002407 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002410 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002412
2413 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 tr->role = le32_to_cpu(buf[0]);
2415 tr->type = le32_to_cpu(buf[1]);
2416 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002417 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2418 rc = next_entry(buf, fp, sizeof(u32));
2419 if (rc)
2420 goto bad;
2421 tr->tclass = le32_to_cpu(buf[0]);
2422 } else
2423 tr->tclass = p->process_class;
2424
Wei Yongjun9b6a9ec2016-09-10 07:43:48 +00002425 rc = -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -05002426 if (!policydb_role_isvalid(p, tr->role) ||
2427 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002428 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002429 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002430 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 ltr = tr;
2432 }
2433
2434 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002435 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto bad;
2437 nel = le32_to_cpu(buf[0]);
2438 lra = NULL;
2439 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002440 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002441 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002442 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002444 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002446 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002449 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002451
2452 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 ra->role = le32_to_cpu(buf[0]);
2454 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002455 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002456 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002457 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 lra = ra;
2459 }
2460
Eric Paris652bb9b2011-02-01 11:05:40 -05002461 rc = filename_trans_read(p, fp);
2462 if (rc)
2463 goto bad;
2464
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002465 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (rc)
2467 goto bad;
2468
Eric Paris9398c7f2010-11-23 11:40:08 -05002469 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002470 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2471 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002472 if (!p->process_trans_perms)
2473 goto bad;
2474
Eric Paris692a8a22010-07-21 12:51:03 -04002475 rc = ocontext_read(p, info, fp);
2476 if (rc)
2477 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Eric Parisd1b43542010-07-21 12:50:57 -04002479 rc = genfs_read(p, fp);
2480 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Eric Paris9ee0c822010-06-11 12:37:05 -04002483 rc = range_read(p, fp);
2484 if (rc)
2485 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Eric Paris6371dcd2010-07-29 23:02:34 -04002487 rc = -ENOMEM;
2488 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2489 p->p_types.nprim,
2490 GFP_KERNEL | __GFP_ZERO);
2491 if (!p->type_attr_map_array)
2492 goto bad;
2493
2494 /* preallocate so we don't have to worry about the put ever failing */
Eric Paris5a3ea872011-04-28 15:55:52 -04002495 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
Eric Paris6371dcd2010-07-29 23:02:34 -04002496 GFP_KERNEL | __GFP_ZERO);
2497 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002498 goto bad;
2499
2500 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002501 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2502
2503 BUG_ON(!e);
2504 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002505 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002506 rc = ebitmap_read(e, fp);
2507 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002508 goto bad;
2509 }
2510 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002511 rc = ebitmap_set_bit(e, i, 1);
2512 if (rc)
2513 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002514 }
2515
KaiGai Koheid9250de2008-08-28 16:35:57 +09002516 rc = policydb_bounds_sanity_check(p);
2517 if (rc)
2518 goto bad;
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 rc = 0;
2521out:
2522 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 policydb_destroy(p);
2525 goto out;
2526}
Eric Pariscee74f42010-10-13 17:50:25 -04002527
2528/*
2529 * Write a MLS level structure to a policydb binary
2530 * representation file.
2531 */
2532static int mls_write_level(struct mls_level *l, void *fp)
2533{
2534 __le32 buf[1];
2535 int rc;
2536
2537 buf[0] = cpu_to_le32(l->sens);
2538 rc = put_entry(buf, sizeof(u32), 1, fp);
2539 if (rc)
2540 return rc;
2541
2542 rc = ebitmap_write(&l->cat, fp);
2543 if (rc)
2544 return rc;
2545
2546 return 0;
2547}
2548
2549/*
2550 * Write a MLS range structure to a policydb binary
2551 * representation file.
2552 */
2553static int mls_write_range_helper(struct mls_range *r, void *fp)
2554{
2555 __le32 buf[3];
2556 size_t items;
2557 int rc, eq;
2558
2559 eq = mls_level_eq(&r->level[1], &r->level[0]);
2560
2561 if (eq)
2562 items = 2;
2563 else
2564 items = 3;
2565 buf[0] = cpu_to_le32(items-1);
2566 buf[1] = cpu_to_le32(r->level[0].sens);
2567 if (!eq)
2568 buf[2] = cpu_to_le32(r->level[1].sens);
2569
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302570 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002571
2572 rc = put_entry(buf, sizeof(u32), items, fp);
2573 if (rc)
2574 return rc;
2575
2576 rc = ebitmap_write(&r->level[0].cat, fp);
2577 if (rc)
2578 return rc;
2579 if (!eq) {
2580 rc = ebitmap_write(&r->level[1].cat, fp);
2581 if (rc)
2582 return rc;
2583 }
2584
2585 return 0;
2586}
2587
2588static int sens_write(void *vkey, void *datum, void *ptr)
2589{
2590 char *key = vkey;
2591 struct level_datum *levdatum = datum;
2592 struct policy_data *pd = ptr;
2593 void *fp = pd->fp;
2594 __le32 buf[2];
2595 size_t len;
2596 int rc;
2597
2598 len = strlen(key);
2599 buf[0] = cpu_to_le32(len);
2600 buf[1] = cpu_to_le32(levdatum->isalias);
2601 rc = put_entry(buf, sizeof(u32), 2, fp);
2602 if (rc)
2603 return rc;
2604
2605 rc = put_entry(key, 1, len, fp);
2606 if (rc)
2607 return rc;
2608
2609 rc = mls_write_level(levdatum->level, fp);
2610 if (rc)
2611 return rc;
2612
2613 return 0;
2614}
2615
2616static int cat_write(void *vkey, void *datum, void *ptr)
2617{
2618 char *key = vkey;
2619 struct cat_datum *catdatum = datum;
2620 struct policy_data *pd = ptr;
2621 void *fp = pd->fp;
2622 __le32 buf[3];
2623 size_t len;
2624 int rc;
2625
2626 len = strlen(key);
2627 buf[0] = cpu_to_le32(len);
2628 buf[1] = cpu_to_le32(catdatum->value);
2629 buf[2] = cpu_to_le32(catdatum->isalias);
2630 rc = put_entry(buf, sizeof(u32), 3, fp);
2631 if (rc)
2632 return rc;
2633
2634 rc = put_entry(key, 1, len, fp);
2635 if (rc)
2636 return rc;
2637
2638 return 0;
2639}
2640
Harry Ciaoc900ff32011-03-25 13:52:00 +08002641static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002642{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002643 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002644 struct role_trans *tr;
2645 u32 buf[3];
2646 size_t nel;
2647 int rc;
2648
2649 nel = 0;
2650 for (tr = r; tr; tr = tr->next)
2651 nel++;
2652 buf[0] = cpu_to_le32(nel);
2653 rc = put_entry(buf, sizeof(u32), 1, fp);
2654 if (rc)
2655 return rc;
2656 for (tr = r; tr; tr = tr->next) {
2657 buf[0] = cpu_to_le32(tr->role);
2658 buf[1] = cpu_to_le32(tr->type);
2659 buf[2] = cpu_to_le32(tr->new_role);
2660 rc = put_entry(buf, sizeof(u32), 3, fp);
2661 if (rc)
2662 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002663 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2664 buf[0] = cpu_to_le32(tr->tclass);
2665 rc = put_entry(buf, sizeof(u32), 1, fp);
2666 if (rc)
2667 return rc;
2668 }
Eric Pariscee74f42010-10-13 17:50:25 -04002669 }
2670
2671 return 0;
2672}
2673
2674static int role_allow_write(struct role_allow *r, void *fp)
2675{
2676 struct role_allow *ra;
2677 u32 buf[2];
2678 size_t nel;
2679 int rc;
2680
2681 nel = 0;
2682 for (ra = r; ra; ra = ra->next)
2683 nel++;
2684 buf[0] = cpu_to_le32(nel);
2685 rc = put_entry(buf, sizeof(u32), 1, fp);
2686 if (rc)
2687 return rc;
2688 for (ra = r; ra; ra = ra->next) {
2689 buf[0] = cpu_to_le32(ra->role);
2690 buf[1] = cpu_to_le32(ra->new_role);
2691 rc = put_entry(buf, sizeof(u32), 2, fp);
2692 if (rc)
2693 return rc;
2694 }
2695 return 0;
2696}
2697
2698/*
2699 * Write a security context structure
2700 * to a policydb binary representation file.
2701 */
2702static int context_write(struct policydb *p, struct context *c,
2703 void *fp)
2704{
2705 int rc;
2706 __le32 buf[3];
2707
2708 buf[0] = cpu_to_le32(c->user);
2709 buf[1] = cpu_to_le32(c->role);
2710 buf[2] = cpu_to_le32(c->type);
2711
2712 rc = put_entry(buf, sizeof(u32), 3, fp);
2713 if (rc)
2714 return rc;
2715
2716 rc = mls_write_range_helper(&c->range, fp);
2717 if (rc)
2718 return rc;
2719
2720 return 0;
2721}
2722
2723/*
2724 * The following *_write functions are used to
2725 * write the symbol data to a policy database
2726 * binary representation file.
2727 */
2728
2729static int perm_write(void *vkey, void *datum, void *fp)
2730{
2731 char *key = vkey;
2732 struct perm_datum *perdatum = datum;
2733 __le32 buf[2];
2734 size_t len;
2735 int rc;
2736
2737 len = strlen(key);
2738 buf[0] = cpu_to_le32(len);
2739 buf[1] = cpu_to_le32(perdatum->value);
2740 rc = put_entry(buf, sizeof(u32), 2, fp);
2741 if (rc)
2742 return rc;
2743
2744 rc = put_entry(key, 1, len, fp);
2745 if (rc)
2746 return rc;
2747
2748 return 0;
2749}
2750
2751static int common_write(void *vkey, void *datum, void *ptr)
2752{
2753 char *key = vkey;
2754 struct common_datum *comdatum = datum;
2755 struct policy_data *pd = ptr;
2756 void *fp = pd->fp;
2757 __le32 buf[4];
2758 size_t len;
2759 int rc;
2760
2761 len = strlen(key);
2762 buf[0] = cpu_to_le32(len);
2763 buf[1] = cpu_to_le32(comdatum->value);
2764 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2765 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2766 rc = put_entry(buf, sizeof(u32), 4, fp);
2767 if (rc)
2768 return rc;
2769
2770 rc = put_entry(key, 1, len, fp);
2771 if (rc)
2772 return rc;
2773
2774 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2775 if (rc)
2776 return rc;
2777
2778 return 0;
2779}
2780
Richard Hainesa660bec2013-11-19 17:34:23 -05002781static int type_set_write(struct type_set *t, void *fp)
2782{
2783 int rc;
2784 __le32 buf[1];
2785
2786 if (ebitmap_write(&t->types, fp))
2787 return -EINVAL;
2788 if (ebitmap_write(&t->negset, fp))
2789 return -EINVAL;
2790
2791 buf[0] = cpu_to_le32(t->flags);
2792 rc = put_entry(buf, sizeof(u32), 1, fp);
2793 if (rc)
2794 return -EINVAL;
2795
2796 return 0;
2797}
2798
Eric Pariscee74f42010-10-13 17:50:25 -04002799static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2800 void *fp)
2801{
2802 struct constraint_node *c;
2803 struct constraint_expr *e;
2804 __le32 buf[3];
2805 u32 nel;
2806 int rc;
2807
2808 for (c = node; c; c = c->next) {
2809 nel = 0;
2810 for (e = c->expr; e; e = e->next)
2811 nel++;
2812 buf[0] = cpu_to_le32(c->permissions);
2813 buf[1] = cpu_to_le32(nel);
2814 rc = put_entry(buf, sizeof(u32), 2, fp);
2815 if (rc)
2816 return rc;
2817 for (e = c->expr; e; e = e->next) {
2818 buf[0] = cpu_to_le32(e->expr_type);
2819 buf[1] = cpu_to_le32(e->attr);
2820 buf[2] = cpu_to_le32(e->op);
2821 rc = put_entry(buf, sizeof(u32), 3, fp);
2822 if (rc)
2823 return rc;
2824
2825 switch (e->expr_type) {
2826 case CEXPR_NAMES:
2827 rc = ebitmap_write(&e->names, fp);
2828 if (rc)
2829 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05002830 if (p->policyvers >=
2831 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2832 rc = type_set_write(e->type_names, fp);
2833 if (rc)
2834 return rc;
2835 }
Eric Pariscee74f42010-10-13 17:50:25 -04002836 break;
2837 default:
2838 break;
2839 }
2840 }
2841 }
2842
2843 return 0;
2844}
2845
2846static int class_write(void *vkey, void *datum, void *ptr)
2847{
2848 char *key = vkey;
2849 struct class_datum *cladatum = datum;
2850 struct policy_data *pd = ptr;
2851 void *fp = pd->fp;
2852 struct policydb *p = pd->p;
2853 struct constraint_node *c;
2854 __le32 buf[6];
2855 u32 ncons;
2856 size_t len, len2;
2857 int rc;
2858
2859 len = strlen(key);
2860 if (cladatum->comkey)
2861 len2 = strlen(cladatum->comkey);
2862 else
2863 len2 = 0;
2864
2865 ncons = 0;
2866 for (c = cladatum->constraints; c; c = c->next)
2867 ncons++;
2868
2869 buf[0] = cpu_to_le32(len);
2870 buf[1] = cpu_to_le32(len2);
2871 buf[2] = cpu_to_le32(cladatum->value);
2872 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2873 if (cladatum->permissions.table)
2874 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2875 else
2876 buf[4] = 0;
2877 buf[5] = cpu_to_le32(ncons);
2878 rc = put_entry(buf, sizeof(u32), 6, fp);
2879 if (rc)
2880 return rc;
2881
2882 rc = put_entry(key, 1, len, fp);
2883 if (rc)
2884 return rc;
2885
2886 if (cladatum->comkey) {
2887 rc = put_entry(cladatum->comkey, 1, len2, fp);
2888 if (rc)
2889 return rc;
2890 }
2891
2892 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2893 if (rc)
2894 return rc;
2895
2896 rc = write_cons_helper(p, cladatum->constraints, fp);
2897 if (rc)
2898 return rc;
2899
2900 /* write out the validatetrans rule */
2901 ncons = 0;
2902 for (c = cladatum->validatetrans; c; c = c->next)
2903 ncons++;
2904
2905 buf[0] = cpu_to_le32(ncons);
2906 rc = put_entry(buf, sizeof(u32), 1, fp);
2907 if (rc)
2908 return rc;
2909
2910 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2911 if (rc)
2912 return rc;
2913
Eric Parisaa893262012-03-20 14:35:12 -04002914 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2915 buf[0] = cpu_to_le32(cladatum->default_user);
2916 buf[1] = cpu_to_le32(cladatum->default_role);
2917 buf[2] = cpu_to_le32(cladatum->default_range);
2918
2919 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2920 if (rc)
2921 return rc;
2922 }
2923
Eric Pariseed77952012-03-20 14:35:12 -04002924 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2925 buf[0] = cpu_to_le32(cladatum->default_type);
2926 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2927 if (rc)
2928 return rc;
2929 }
2930
Eric Pariscee74f42010-10-13 17:50:25 -04002931 return 0;
2932}
2933
2934static int role_write(void *vkey, void *datum, void *ptr)
2935{
2936 char *key = vkey;
2937 struct role_datum *role = datum;
2938 struct policy_data *pd = ptr;
2939 void *fp = pd->fp;
2940 struct policydb *p = pd->p;
2941 __le32 buf[3];
2942 size_t items, len;
2943 int rc;
2944
2945 len = strlen(key);
2946 items = 0;
2947 buf[items++] = cpu_to_le32(len);
2948 buf[items++] = cpu_to_le32(role->value);
2949 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2950 buf[items++] = cpu_to_le32(role->bounds);
2951
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302952 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002953
2954 rc = put_entry(buf, sizeof(u32), items, fp);
2955 if (rc)
2956 return rc;
2957
2958 rc = put_entry(key, 1, len, fp);
2959 if (rc)
2960 return rc;
2961
2962 rc = ebitmap_write(&role->dominates, fp);
2963 if (rc)
2964 return rc;
2965
2966 rc = ebitmap_write(&role->types, fp);
2967 if (rc)
2968 return rc;
2969
2970 return 0;
2971}
2972
2973static int type_write(void *vkey, void *datum, void *ptr)
2974{
2975 char *key = vkey;
2976 struct type_datum *typdatum = datum;
2977 struct policy_data *pd = ptr;
2978 struct policydb *p = pd->p;
2979 void *fp = pd->fp;
2980 __le32 buf[4];
2981 int rc;
2982 size_t items, len;
2983
2984 len = strlen(key);
2985 items = 0;
2986 buf[items++] = cpu_to_le32(len);
2987 buf[items++] = cpu_to_le32(typdatum->value);
2988 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2989 u32 properties = 0;
2990
2991 if (typdatum->primary)
2992 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2993
2994 if (typdatum->attribute)
2995 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
2996
2997 buf[items++] = cpu_to_le32(properties);
2998 buf[items++] = cpu_to_le32(typdatum->bounds);
2999 } else {
3000 buf[items++] = cpu_to_le32(typdatum->primary);
3001 }
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303002 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003003 rc = put_entry(buf, sizeof(u32), items, fp);
3004 if (rc)
3005 return rc;
3006
3007 rc = put_entry(key, 1, len, fp);
3008 if (rc)
3009 return rc;
3010
3011 return 0;
3012}
3013
3014static int user_write(void *vkey, void *datum, void *ptr)
3015{
3016 char *key = vkey;
3017 struct user_datum *usrdatum = datum;
3018 struct policy_data *pd = ptr;
3019 struct policydb *p = pd->p;
3020 void *fp = pd->fp;
3021 __le32 buf[3];
3022 size_t items, len;
3023 int rc;
3024
3025 len = strlen(key);
3026 items = 0;
3027 buf[items++] = cpu_to_le32(len);
3028 buf[items++] = cpu_to_le32(usrdatum->value);
3029 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3030 buf[items++] = cpu_to_le32(usrdatum->bounds);
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303031 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003032 rc = put_entry(buf, sizeof(u32), items, fp);
3033 if (rc)
3034 return rc;
3035
3036 rc = put_entry(key, 1, len, fp);
3037 if (rc)
3038 return rc;
3039
3040 rc = ebitmap_write(&usrdatum->roles, fp);
3041 if (rc)
3042 return rc;
3043
3044 rc = mls_write_range_helper(&usrdatum->range, fp);
3045 if (rc)
3046 return rc;
3047
3048 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3049 if (rc)
3050 return rc;
3051
3052 return 0;
3053}
3054
3055static int (*write_f[SYM_NUM]) (void *key, void *datum,
3056 void *datap) =
3057{
3058 common_write,
3059 class_write,
3060 role_write,
3061 type_write,
3062 user_write,
3063 cond_write_bool,
3064 sens_write,
3065 cat_write,
3066};
3067
3068static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3069 void *fp)
3070{
3071 unsigned int i, j, rc;
3072 size_t nel, len;
3073 __le32 buf[3];
3074 u32 nodebuf[8];
3075 struct ocontext *c;
3076 for (i = 0; i < info->ocon_num; i++) {
3077 nel = 0;
3078 for (c = p->ocontexts[i]; c; c = c->next)
3079 nel++;
3080 buf[0] = cpu_to_le32(nel);
3081 rc = put_entry(buf, sizeof(u32), 1, fp);
3082 if (rc)
3083 return rc;
3084 for (c = p->ocontexts[i]; c; c = c->next) {
3085 switch (i) {
3086 case OCON_ISID:
3087 buf[0] = cpu_to_le32(c->sid[0]);
3088 rc = put_entry(buf, sizeof(u32), 1, fp);
3089 if (rc)
3090 return rc;
3091 rc = context_write(p, &c->context[0], fp);
3092 if (rc)
3093 return rc;
3094 break;
3095 case OCON_FS:
3096 case OCON_NETIF:
3097 len = strlen(c->u.name);
3098 buf[0] = cpu_to_le32(len);
3099 rc = put_entry(buf, sizeof(u32), 1, fp);
3100 if (rc)
3101 return rc;
3102 rc = put_entry(c->u.name, 1, len, fp);
3103 if (rc)
3104 return rc;
3105 rc = context_write(p, &c->context[0], fp);
3106 if (rc)
3107 return rc;
3108 rc = context_write(p, &c->context[1], fp);
3109 if (rc)
3110 return rc;
3111 break;
3112 case OCON_PORT:
3113 buf[0] = cpu_to_le32(c->u.port.protocol);
3114 buf[1] = cpu_to_le32(c->u.port.low_port);
3115 buf[2] = cpu_to_le32(c->u.port.high_port);
3116 rc = put_entry(buf, sizeof(u32), 3, fp);
3117 if (rc)
3118 return rc;
3119 rc = context_write(p, &c->context[0], fp);
3120 if (rc)
3121 return rc;
3122 break;
3123 case OCON_NODE:
3124 nodebuf[0] = c->u.node.addr; /* network order */
3125 nodebuf[1] = c->u.node.mask; /* network order */
3126 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3127 if (rc)
3128 return rc;
3129 rc = context_write(p, &c->context[0], fp);
3130 if (rc)
3131 return rc;
3132 break;
3133 case OCON_FSUSE:
3134 buf[0] = cpu_to_le32(c->v.behavior);
3135 len = strlen(c->u.name);
3136 buf[1] = cpu_to_le32(len);
3137 rc = put_entry(buf, sizeof(u32), 2, fp);
3138 if (rc)
3139 return rc;
3140 rc = put_entry(c->u.name, 1, len, fp);
3141 if (rc)
3142 return rc;
3143 rc = context_write(p, &c->context[0], fp);
3144 if (rc)
3145 return rc;
3146 break;
3147 case OCON_NODE6:
3148 for (j = 0; j < 4; j++)
3149 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3150 for (j = 0; j < 4; j++)
3151 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3152 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3153 if (rc)
3154 return rc;
3155 rc = context_write(p, &c->context[0], fp);
3156 if (rc)
3157 return rc;
3158 break;
3159 }
3160 }
3161 }
3162 return 0;
3163}
3164
3165static int genfs_write(struct policydb *p, void *fp)
3166{
3167 struct genfs *genfs;
3168 struct ocontext *c;
3169 size_t len;
3170 __le32 buf[1];
3171 int rc;
3172
3173 len = 0;
3174 for (genfs = p->genfs; genfs; genfs = genfs->next)
3175 len++;
3176 buf[0] = cpu_to_le32(len);
3177 rc = put_entry(buf, sizeof(u32), 1, fp);
3178 if (rc)
3179 return rc;
3180 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3181 len = strlen(genfs->fstype);
3182 buf[0] = cpu_to_le32(len);
3183 rc = put_entry(buf, sizeof(u32), 1, fp);
3184 if (rc)
3185 return rc;
3186 rc = put_entry(genfs->fstype, 1, len, fp);
3187 if (rc)
3188 return rc;
3189 len = 0;
3190 for (c = genfs->head; c; c = c->next)
3191 len++;
3192 buf[0] = cpu_to_le32(len);
3193 rc = put_entry(buf, sizeof(u32), 1, fp);
3194 if (rc)
3195 return rc;
3196 for (c = genfs->head; c; c = c->next) {
3197 len = strlen(c->u.name);
3198 buf[0] = cpu_to_le32(len);
3199 rc = put_entry(buf, sizeof(u32), 1, fp);
3200 if (rc)
3201 return rc;
3202 rc = put_entry(c->u.name, 1, len, fp);
3203 if (rc)
3204 return rc;
3205 buf[0] = cpu_to_le32(c->v.sclass);
3206 rc = put_entry(buf, sizeof(u32), 1, fp);
3207 if (rc)
3208 return rc;
3209 rc = context_write(p, &c->context[0], fp);
3210 if (rc)
3211 return rc;
3212 }
3213 }
3214 return 0;
3215}
3216
Eric Paris3f058ef2011-04-28 15:11:21 -04003217static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003218{
3219 int *cnt = ptr;
3220 *cnt = *cnt + 1;
3221
3222 return 0;
3223}
3224
3225static int range_write_helper(void *key, void *data, void *ptr)
3226{
3227 __le32 buf[2];
3228 struct range_trans *rt = key;
3229 struct mls_range *r = data;
3230 struct policy_data *pd = ptr;
3231 void *fp = pd->fp;
3232 struct policydb *p = pd->p;
3233 int rc;
3234
3235 buf[0] = cpu_to_le32(rt->source_type);
3236 buf[1] = cpu_to_le32(rt->target_type);
3237 rc = put_entry(buf, sizeof(u32), 2, fp);
3238 if (rc)
3239 return rc;
3240 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3241 buf[0] = cpu_to_le32(rt->target_class);
3242 rc = put_entry(buf, sizeof(u32), 1, fp);
3243 if (rc)
3244 return rc;
3245 }
3246 rc = mls_write_range_helper(r, fp);
3247 if (rc)
3248 return rc;
3249
3250 return 0;
3251}
3252
3253static int range_write(struct policydb *p, void *fp)
3254{
Eric Pariscee74f42010-10-13 17:50:25 -04003255 __le32 buf[1];
Eric Parisb1380042013-07-23 17:38:42 -04003256 int rc, nel;
Eric Pariscee74f42010-10-13 17:50:25 -04003257 struct policy_data pd;
3258
3259 pd.p = p;
3260 pd.fp = fp;
3261
3262 /* count the number of entries in the hashtab */
3263 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003264 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003265 if (rc)
3266 return rc;
3267
3268 buf[0] = cpu_to_le32(nel);
3269 rc = put_entry(buf, sizeof(u32), 1, fp);
3270 if (rc)
3271 return rc;
3272
3273 /* actually write all of the entries */
3274 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3275 if (rc)
3276 return rc;
3277
3278 return 0;
3279}
3280
Eric Paris2463c26d2011-04-28 15:11:21 -04003281static int filename_write_helper(void *key, void *data, void *ptr)
3282{
3283 __le32 buf[4];
3284 struct filename_trans *ft = key;
3285 struct filename_trans_datum *otype = data;
3286 void *fp = ptr;
3287 int rc;
3288 u32 len;
3289
3290 len = strlen(ft->name);
3291 buf[0] = cpu_to_le32(len);
3292 rc = put_entry(buf, sizeof(u32), 1, fp);
3293 if (rc)
3294 return rc;
3295
3296 rc = put_entry(ft->name, sizeof(char), len, fp);
3297 if (rc)
3298 return rc;
3299
Eric Paris9085a642014-02-20 10:56:45 -05003300 buf[0] = cpu_to_le32(ft->stype);
3301 buf[1] = cpu_to_le32(ft->ttype);
3302 buf[2] = cpu_to_le32(ft->tclass);
3303 buf[3] = cpu_to_le32(otype->otype);
Eric Paris2463c26d2011-04-28 15:11:21 -04003304
3305 rc = put_entry(buf, sizeof(u32), 4, fp);
3306 if (rc)
3307 return rc;
3308
3309 return 0;
3310}
3311
Eric Paris652bb9b2011-02-01 11:05:40 -05003312static int filename_trans_write(struct policydb *p, void *fp)
3313{
Eric Paris2463c26d2011-04-28 15:11:21 -04003314 u32 nel;
3315 __le32 buf[1];
Eric Paris652bb9b2011-02-01 11:05:40 -05003316 int rc;
3317
Roy.Lided50982011-05-20 10:38:06 +08003318 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3319 return 0;
3320
Eric Paris2463c26d2011-04-28 15:11:21 -04003321 nel = 0;
3322 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3323 if (rc)
3324 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003325
3326 buf[0] = cpu_to_le32(nel);
3327 rc = put_entry(buf, sizeof(u32), 1, fp);
3328 if (rc)
3329 return rc;
3330
Eric Paris2463c26d2011-04-28 15:11:21 -04003331 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3332 if (rc)
3333 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003334
Eric Paris652bb9b2011-02-01 11:05:40 -05003335 return 0;
3336}
Eric Paris2463c26d2011-04-28 15:11:21 -04003337
Eric Pariscee74f42010-10-13 17:50:25 -04003338/*
3339 * Write the configuration data in a policy database
3340 * structure to a policy database binary representation
3341 * file.
3342 */
3343int policydb_write(struct policydb *p, void *fp)
3344{
3345 unsigned int i, num_syms;
3346 int rc;
3347 __le32 buf[4];
3348 u32 config;
3349 size_t len;
3350 struct policydb_compat_info *info;
3351
3352 /*
3353 * refuse to write policy older than compressed avtab
3354 * to simplify the writer. There are other tests dropped
3355 * since we assume this throughout the writer code. Be
3356 * careful if you ever try to remove this restriction
3357 */
3358 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3359 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3360 " Because it is less than version %d\n", p->policyvers,
3361 POLICYDB_VERSION_AVTAB);
3362 return -EINVAL;
3363 }
3364
3365 config = 0;
3366 if (p->mls_enabled)
3367 config |= POLICYDB_CONFIG_MLS;
3368
3369 if (p->reject_unknown)
3370 config |= REJECT_UNKNOWN;
3371 if (p->allow_unknown)
3372 config |= ALLOW_UNKNOWN;
3373
3374 /* Write the magic number and string identifiers. */
3375 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3376 len = strlen(POLICYDB_STRING);
3377 buf[1] = cpu_to_le32(len);
3378 rc = put_entry(buf, sizeof(u32), 2, fp);
3379 if (rc)
3380 return rc;
3381 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3382 if (rc)
3383 return rc;
3384
3385 /* Write the version, config, and table sizes. */
3386 info = policydb_lookup_compat(p->policyvers);
3387 if (!info) {
3388 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3389 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003390 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003391 }
3392
3393 buf[0] = cpu_to_le32(p->policyvers);
3394 buf[1] = cpu_to_le32(config);
3395 buf[2] = cpu_to_le32(info->sym_num);
3396 buf[3] = cpu_to_le32(info->ocon_num);
3397
3398 rc = put_entry(buf, sizeof(u32), 4, fp);
3399 if (rc)
3400 return rc;
3401
3402 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3403 rc = ebitmap_write(&p->policycaps, fp);
3404 if (rc)
3405 return rc;
3406 }
3407
3408 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3409 rc = ebitmap_write(&p->permissive_map, fp);
3410 if (rc)
3411 return rc;
3412 }
3413
3414 num_syms = info->sym_num;
3415 for (i = 0; i < num_syms; i++) {
3416 struct policy_data pd;
3417
3418 pd.fp = fp;
3419 pd.p = p;
3420
3421 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3422 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3423
3424 rc = put_entry(buf, sizeof(u32), 2, fp);
3425 if (rc)
3426 return rc;
3427 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3428 if (rc)
3429 return rc;
3430 }
3431
3432 rc = avtab_write(p, &p->te_avtab, fp);
3433 if (rc)
3434 return rc;
3435
3436 rc = cond_write_list(p, p->cond_list, fp);
3437 if (rc)
3438 return rc;
3439
Harry Ciaoc900ff32011-03-25 13:52:00 +08003440 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003441 if (rc)
3442 return rc;
3443
3444 rc = role_allow_write(p->role_allow, fp);
3445 if (rc)
3446 return rc;
3447
Eric Paris652bb9b2011-02-01 11:05:40 -05003448 rc = filename_trans_write(p, fp);
3449 if (rc)
3450 return rc;
3451
Eric Pariscee74f42010-10-13 17:50:25 -04003452 rc = ocontext_write(p, info, fp);
3453 if (rc)
3454 return rc;
3455
3456 rc = genfs_write(p, fp);
3457 if (rc)
3458 return rc;
3459
3460 rc = range_write(p, fp);
3461 if (rc)
3462 return rc;
3463
3464 for (i = 0; i < p->p_types.nprim; i++) {
3465 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3466
3467 BUG_ON(!e);
3468 rc = ebitmap_write(e, fp);
3469 if (rc)
3470 return rc;
3471 }
3472
3473 return 0;
3474}