blob: 62518b031e5e43229909937672bd608be565cd0e [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
Jeff Vander Stoep2c434f62020-01-22 11:19:58 +01002335 if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE)) {
2336 p->android_netlink_route = 1;
2337 }
2338
Eric Paris9398c7f2010-11-23 11:40:08 -05002339 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
2340 rc = ebitmap_read(&p->policycaps, fp);
2341 if (rc)
2342 goto bad;
2343 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002344
Eric Paris9398c7f2010-11-23 11:40:08 -05002345 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
2346 rc = ebitmap_read(&p->permissive_map, fp);
2347 if (rc)
2348 goto bad;
2349 }
Eric Paris64dbf072008-03-31 12:17:33 +11002350
Eric Paris9398c7f2010-11-23 11:40:08 -05002351 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 info = policydb_lookup_compat(p->policyvers);
2353 if (!info) {
James Morris454d9722008-02-26 20:42:02 +11002354 printk(KERN_ERR "SELinux: unable to find policy compat info "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 "for version %d\n", p->policyvers);
2356 goto bad;
2357 }
2358
Eric Paris9398c7f2010-11-23 11:40:08 -05002359 rc = -EINVAL;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002360 if (le32_to_cpu(buf[2]) != info->sym_num ||
2361 le32_to_cpu(buf[3]) != info->ocon_num) {
James Morris454d9722008-02-26 20:42:02 +11002362 printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -07002363 "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
2364 le32_to_cpu(buf[3]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 info->sym_num, info->ocon_num);
2366 goto bad;
2367 }
2368
2369 for (i = 0; i < info->sym_num; i++) {
2370 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002371 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto bad;
2373 nprim = le32_to_cpu(buf[0]);
2374 nel = le32_to_cpu(buf[1]);
2375 for (j = 0; j < nel; j++) {
2376 rc = read_f[i](p, p->symtab[i].table, fp);
2377 if (rc)
2378 goto bad;
2379 }
2380
2381 p->symtab[i].nprim = nprim;
2382 }
2383
Harry Ciao1214eac2011-04-07 14:12:57 +08002384 rc = -EINVAL;
2385 p->process_class = string_to_security_class(p, "process");
2386 if (!p->process_class)
2387 goto bad;
2388
Stephen Smalley45e54212007-11-07 10:08:00 -05002389 rc = avtab_read(&p->te_avtab, fp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (rc)
2391 goto bad;
2392
2393 if (p->policyvers >= POLICYDB_VERSION_BOOL) {
2394 rc = cond_read_list(p, fp);
2395 if (rc)
2396 goto bad;
2397 }
2398
2399 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002400 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto bad;
2402 nel = le32_to_cpu(buf[0]);
2403 ltr = NULL;
2404 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002405 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002406 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002407 if (!tr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002409 if (ltr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ltr->next = tr;
Eric Paris2ced3df2008-04-17 13:37:12 -04002411 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 p->role_tr = tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 rc = next_entry(buf, fp, sizeof(u32)*3);
Eric Paris9398c7f2010-11-23 11:40:08 -05002414 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002416
2417 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 tr->role = le32_to_cpu(buf[0]);
2419 tr->type = le32_to_cpu(buf[1]);
2420 tr->new_role = le32_to_cpu(buf[2]);
Harry Ciao80239762011-03-25 13:51:56 +08002421 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2422 rc = next_entry(buf, fp, sizeof(u32));
2423 if (rc)
2424 goto bad;
2425 tr->tclass = le32_to_cpu(buf[0]);
2426 } else
2427 tr->tclass = p->process_class;
2428
Wei Yongjun9b6a9ec2016-09-10 07:43:48 +00002429 rc = -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -05002430 if (!policydb_role_isvalid(p, tr->role) ||
2431 !policydb_type_isvalid(p, tr->type) ||
Harry Ciao80239762011-03-25 13:51:56 +08002432 !policydb_class_isvalid(p, tr->tclass) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002433 !policydb_role_isvalid(p, tr->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002434 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 ltr = tr;
2436 }
2437
2438 rc = next_entry(buf, fp, sizeof(u32));
Eric Paris9398c7f2010-11-23 11:40:08 -05002439 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 goto bad;
2441 nel = le32_to_cpu(buf[0]);
2442 lra = NULL;
2443 for (i = 0; i < nel; i++) {
Eric Paris9398c7f2010-11-23 11:40:08 -05002444 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002445 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Eric Paris9398c7f2010-11-23 11:40:08 -05002446 if (!ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto bad;
Eric Paris2ced3df2008-04-17 13:37:12 -04002448 if (lra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 lra->next = ra;
Eric Paris2ced3df2008-04-17 13:37:12 -04002450 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 p->role_allow = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 rc = next_entry(buf, fp, sizeof(u32)*2);
Eric Paris9398c7f2010-11-23 11:40:08 -05002453 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto bad;
Eric Paris9398c7f2010-11-23 11:40:08 -05002455
2456 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ra->role = le32_to_cpu(buf[0]);
2458 ra->new_role = le32_to_cpu(buf[1]);
Stephen Smalley45e54212007-11-07 10:08:00 -05002459 if (!policydb_role_isvalid(p, ra->role) ||
Eric Paris9398c7f2010-11-23 11:40:08 -05002460 !policydb_role_isvalid(p, ra->new_role))
Stephen Smalley45e54212007-11-07 10:08:00 -05002461 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 lra = ra;
2463 }
2464
Eric Paris652bb9b2011-02-01 11:05:40 -05002465 rc = filename_trans_read(p, fp);
2466 if (rc)
2467 goto bad;
2468
Eric Paris1d9bc6dc2010-11-29 15:47:09 -05002469 rc = policydb_index(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (rc)
2471 goto bad;
2472
Eric Paris9398c7f2010-11-23 11:40:08 -05002473 rc = -EINVAL;
Eric Paris9398c7f2010-11-23 11:40:08 -05002474 p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
2475 p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002476 if (!p->process_trans_perms)
2477 goto bad;
2478
Eric Paris692a8a22010-07-21 12:51:03 -04002479 rc = ocontext_read(p, info, fp);
2480 if (rc)
2481 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Eric Parisd1b43542010-07-21 12:50:57 -04002483 rc = genfs_read(p, fp);
2484 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Eric Paris9ee0c822010-06-11 12:37:05 -04002487 rc = range_read(p, fp);
2488 if (rc)
2489 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Eric Paris6371dcd2010-07-29 23:02:34 -04002491 rc = -ENOMEM;
2492 p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
2493 p->p_types.nprim,
2494 GFP_KERNEL | __GFP_ZERO);
2495 if (!p->type_attr_map_array)
2496 goto bad;
2497
2498 /* preallocate so we don't have to worry about the put ever failing */
Eric Paris5a3ea872011-04-28 15:55:52 -04002499 rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
Eric Paris6371dcd2010-07-29 23:02:34 -04002500 GFP_KERNEL | __GFP_ZERO);
2501 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002502 goto bad;
2503
2504 for (i = 0; i < p->p_types.nprim; i++) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002505 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
2506
2507 BUG_ON(!e);
2508 ebitmap_init(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -07002509 if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
Eric Paris6371dcd2010-07-29 23:02:34 -04002510 rc = ebitmap_read(e, fp);
2511 if (rc)
Stephen Smalley782ebb92005-09-03 15:55:16 -07002512 goto bad;
2513 }
2514 /* add the type itself as the degenerate case */
Eric Paris6371dcd2010-07-29 23:02:34 -04002515 rc = ebitmap_set_bit(e, i, 1);
2516 if (rc)
2517 goto bad;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002518 }
2519
KaiGai Koheid9250de2008-08-28 16:35:57 +09002520 rc = policydb_bounds_sanity_check(p);
2521 if (rc)
2522 goto bad;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 rc = 0;
2525out:
2526 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 policydb_destroy(p);
2529 goto out;
2530}
Eric Pariscee74f42010-10-13 17:50:25 -04002531
2532/*
2533 * Write a MLS level structure to a policydb binary
2534 * representation file.
2535 */
2536static int mls_write_level(struct mls_level *l, void *fp)
2537{
2538 __le32 buf[1];
2539 int rc;
2540
2541 buf[0] = cpu_to_le32(l->sens);
2542 rc = put_entry(buf, sizeof(u32), 1, fp);
2543 if (rc)
2544 return rc;
2545
2546 rc = ebitmap_write(&l->cat, fp);
2547 if (rc)
2548 return rc;
2549
2550 return 0;
2551}
2552
2553/*
2554 * Write a MLS range structure to a policydb binary
2555 * representation file.
2556 */
2557static int mls_write_range_helper(struct mls_range *r, void *fp)
2558{
2559 __le32 buf[3];
2560 size_t items;
2561 int rc, eq;
2562
2563 eq = mls_level_eq(&r->level[1], &r->level[0]);
2564
2565 if (eq)
2566 items = 2;
2567 else
2568 items = 3;
2569 buf[0] = cpu_to_le32(items-1);
2570 buf[1] = cpu_to_le32(r->level[0].sens);
2571 if (!eq)
2572 buf[2] = cpu_to_le32(r->level[1].sens);
2573
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302574 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002575
2576 rc = put_entry(buf, sizeof(u32), items, fp);
2577 if (rc)
2578 return rc;
2579
2580 rc = ebitmap_write(&r->level[0].cat, fp);
2581 if (rc)
2582 return rc;
2583 if (!eq) {
2584 rc = ebitmap_write(&r->level[1].cat, fp);
2585 if (rc)
2586 return rc;
2587 }
2588
2589 return 0;
2590}
2591
2592static int sens_write(void *vkey, void *datum, void *ptr)
2593{
2594 char *key = vkey;
2595 struct level_datum *levdatum = datum;
2596 struct policy_data *pd = ptr;
2597 void *fp = pd->fp;
2598 __le32 buf[2];
2599 size_t len;
2600 int rc;
2601
2602 len = strlen(key);
2603 buf[0] = cpu_to_le32(len);
2604 buf[1] = cpu_to_le32(levdatum->isalias);
2605 rc = put_entry(buf, sizeof(u32), 2, fp);
2606 if (rc)
2607 return rc;
2608
2609 rc = put_entry(key, 1, len, fp);
2610 if (rc)
2611 return rc;
2612
2613 rc = mls_write_level(levdatum->level, fp);
2614 if (rc)
2615 return rc;
2616
2617 return 0;
2618}
2619
2620static int cat_write(void *vkey, void *datum, void *ptr)
2621{
2622 char *key = vkey;
2623 struct cat_datum *catdatum = datum;
2624 struct policy_data *pd = ptr;
2625 void *fp = pd->fp;
2626 __le32 buf[3];
2627 size_t len;
2628 int rc;
2629
2630 len = strlen(key);
2631 buf[0] = cpu_to_le32(len);
2632 buf[1] = cpu_to_le32(catdatum->value);
2633 buf[2] = cpu_to_le32(catdatum->isalias);
2634 rc = put_entry(buf, sizeof(u32), 3, fp);
2635 if (rc)
2636 return rc;
2637
2638 rc = put_entry(key, 1, len, fp);
2639 if (rc)
2640 return rc;
2641
2642 return 0;
2643}
2644
Harry Ciaoc900ff32011-03-25 13:52:00 +08002645static int role_trans_write(struct policydb *p, void *fp)
Eric Pariscee74f42010-10-13 17:50:25 -04002646{
Harry Ciaoc900ff32011-03-25 13:52:00 +08002647 struct role_trans *r = p->role_tr;
Eric Pariscee74f42010-10-13 17:50:25 -04002648 struct role_trans *tr;
2649 u32 buf[3];
2650 size_t nel;
2651 int rc;
2652
2653 nel = 0;
2654 for (tr = r; tr; tr = tr->next)
2655 nel++;
2656 buf[0] = cpu_to_le32(nel);
2657 rc = put_entry(buf, sizeof(u32), 1, fp);
2658 if (rc)
2659 return rc;
2660 for (tr = r; tr; tr = tr->next) {
2661 buf[0] = cpu_to_le32(tr->role);
2662 buf[1] = cpu_to_le32(tr->type);
2663 buf[2] = cpu_to_le32(tr->new_role);
2664 rc = put_entry(buf, sizeof(u32), 3, fp);
2665 if (rc)
2666 return rc;
Harry Ciaoc900ff32011-03-25 13:52:00 +08002667 if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
2668 buf[0] = cpu_to_le32(tr->tclass);
2669 rc = put_entry(buf, sizeof(u32), 1, fp);
2670 if (rc)
2671 return rc;
2672 }
Eric Pariscee74f42010-10-13 17:50:25 -04002673 }
2674
2675 return 0;
2676}
2677
2678static int role_allow_write(struct role_allow *r, void *fp)
2679{
2680 struct role_allow *ra;
2681 u32 buf[2];
2682 size_t nel;
2683 int rc;
2684
2685 nel = 0;
2686 for (ra = r; ra; ra = ra->next)
2687 nel++;
2688 buf[0] = cpu_to_le32(nel);
2689 rc = put_entry(buf, sizeof(u32), 1, fp);
2690 if (rc)
2691 return rc;
2692 for (ra = r; ra; ra = ra->next) {
2693 buf[0] = cpu_to_le32(ra->role);
2694 buf[1] = cpu_to_le32(ra->new_role);
2695 rc = put_entry(buf, sizeof(u32), 2, fp);
2696 if (rc)
2697 return rc;
2698 }
2699 return 0;
2700}
2701
2702/*
2703 * Write a security context structure
2704 * to a policydb binary representation file.
2705 */
2706static int context_write(struct policydb *p, struct context *c,
2707 void *fp)
2708{
2709 int rc;
2710 __le32 buf[3];
2711
2712 buf[0] = cpu_to_le32(c->user);
2713 buf[1] = cpu_to_le32(c->role);
2714 buf[2] = cpu_to_le32(c->type);
2715
2716 rc = put_entry(buf, sizeof(u32), 3, fp);
2717 if (rc)
2718 return rc;
2719
2720 rc = mls_write_range_helper(&c->range, fp);
2721 if (rc)
2722 return rc;
2723
2724 return 0;
2725}
2726
2727/*
2728 * The following *_write functions are used to
2729 * write the symbol data to a policy database
2730 * binary representation file.
2731 */
2732
2733static int perm_write(void *vkey, void *datum, void *fp)
2734{
2735 char *key = vkey;
2736 struct perm_datum *perdatum = datum;
2737 __le32 buf[2];
2738 size_t len;
2739 int rc;
2740
2741 len = strlen(key);
2742 buf[0] = cpu_to_le32(len);
2743 buf[1] = cpu_to_le32(perdatum->value);
2744 rc = put_entry(buf, sizeof(u32), 2, fp);
2745 if (rc)
2746 return rc;
2747
2748 rc = put_entry(key, 1, len, fp);
2749 if (rc)
2750 return rc;
2751
2752 return 0;
2753}
2754
2755static int common_write(void *vkey, void *datum, void *ptr)
2756{
2757 char *key = vkey;
2758 struct common_datum *comdatum = datum;
2759 struct policy_data *pd = ptr;
2760 void *fp = pd->fp;
2761 __le32 buf[4];
2762 size_t len;
2763 int rc;
2764
2765 len = strlen(key);
2766 buf[0] = cpu_to_le32(len);
2767 buf[1] = cpu_to_le32(comdatum->value);
2768 buf[2] = cpu_to_le32(comdatum->permissions.nprim);
2769 buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
2770 rc = put_entry(buf, sizeof(u32), 4, fp);
2771 if (rc)
2772 return rc;
2773
2774 rc = put_entry(key, 1, len, fp);
2775 if (rc)
2776 return rc;
2777
2778 rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
2779 if (rc)
2780 return rc;
2781
2782 return 0;
2783}
2784
Richard Hainesa660bec2013-11-19 17:34:23 -05002785static int type_set_write(struct type_set *t, void *fp)
2786{
2787 int rc;
2788 __le32 buf[1];
2789
2790 if (ebitmap_write(&t->types, fp))
2791 return -EINVAL;
2792 if (ebitmap_write(&t->negset, fp))
2793 return -EINVAL;
2794
2795 buf[0] = cpu_to_le32(t->flags);
2796 rc = put_entry(buf, sizeof(u32), 1, fp);
2797 if (rc)
2798 return -EINVAL;
2799
2800 return 0;
2801}
2802
Eric Pariscee74f42010-10-13 17:50:25 -04002803static int write_cons_helper(struct policydb *p, struct constraint_node *node,
2804 void *fp)
2805{
2806 struct constraint_node *c;
2807 struct constraint_expr *e;
2808 __le32 buf[3];
2809 u32 nel;
2810 int rc;
2811
2812 for (c = node; c; c = c->next) {
2813 nel = 0;
2814 for (e = c->expr; e; e = e->next)
2815 nel++;
2816 buf[0] = cpu_to_le32(c->permissions);
2817 buf[1] = cpu_to_le32(nel);
2818 rc = put_entry(buf, sizeof(u32), 2, fp);
2819 if (rc)
2820 return rc;
2821 for (e = c->expr; e; e = e->next) {
2822 buf[0] = cpu_to_le32(e->expr_type);
2823 buf[1] = cpu_to_le32(e->attr);
2824 buf[2] = cpu_to_le32(e->op);
2825 rc = put_entry(buf, sizeof(u32), 3, fp);
2826 if (rc)
2827 return rc;
2828
2829 switch (e->expr_type) {
2830 case CEXPR_NAMES:
2831 rc = ebitmap_write(&e->names, fp);
2832 if (rc)
2833 return rc;
Richard Hainesa660bec2013-11-19 17:34:23 -05002834 if (p->policyvers >=
2835 POLICYDB_VERSION_CONSTRAINT_NAMES) {
2836 rc = type_set_write(e->type_names, fp);
2837 if (rc)
2838 return rc;
2839 }
Eric Pariscee74f42010-10-13 17:50:25 -04002840 break;
2841 default:
2842 break;
2843 }
2844 }
2845 }
2846
2847 return 0;
2848}
2849
2850static int class_write(void *vkey, void *datum, void *ptr)
2851{
2852 char *key = vkey;
2853 struct class_datum *cladatum = datum;
2854 struct policy_data *pd = ptr;
2855 void *fp = pd->fp;
2856 struct policydb *p = pd->p;
2857 struct constraint_node *c;
2858 __le32 buf[6];
2859 u32 ncons;
2860 size_t len, len2;
2861 int rc;
2862
2863 len = strlen(key);
2864 if (cladatum->comkey)
2865 len2 = strlen(cladatum->comkey);
2866 else
2867 len2 = 0;
2868
2869 ncons = 0;
2870 for (c = cladatum->constraints; c; c = c->next)
2871 ncons++;
2872
2873 buf[0] = cpu_to_le32(len);
2874 buf[1] = cpu_to_le32(len2);
2875 buf[2] = cpu_to_le32(cladatum->value);
2876 buf[3] = cpu_to_le32(cladatum->permissions.nprim);
2877 if (cladatum->permissions.table)
2878 buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
2879 else
2880 buf[4] = 0;
2881 buf[5] = cpu_to_le32(ncons);
2882 rc = put_entry(buf, sizeof(u32), 6, fp);
2883 if (rc)
2884 return rc;
2885
2886 rc = put_entry(key, 1, len, fp);
2887 if (rc)
2888 return rc;
2889
2890 if (cladatum->comkey) {
2891 rc = put_entry(cladatum->comkey, 1, len2, fp);
2892 if (rc)
2893 return rc;
2894 }
2895
2896 rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
2897 if (rc)
2898 return rc;
2899
2900 rc = write_cons_helper(p, cladatum->constraints, fp);
2901 if (rc)
2902 return rc;
2903
2904 /* write out the validatetrans rule */
2905 ncons = 0;
2906 for (c = cladatum->validatetrans; c; c = c->next)
2907 ncons++;
2908
2909 buf[0] = cpu_to_le32(ncons);
2910 rc = put_entry(buf, sizeof(u32), 1, fp);
2911 if (rc)
2912 return rc;
2913
2914 rc = write_cons_helper(p, cladatum->validatetrans, fp);
2915 if (rc)
2916 return rc;
2917
Eric Parisaa893262012-03-20 14:35:12 -04002918 if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
2919 buf[0] = cpu_to_le32(cladatum->default_user);
2920 buf[1] = cpu_to_le32(cladatum->default_role);
2921 buf[2] = cpu_to_le32(cladatum->default_range);
2922
2923 rc = put_entry(buf, sizeof(uint32_t), 3, fp);
2924 if (rc)
2925 return rc;
2926 }
2927
Eric Pariseed77952012-03-20 14:35:12 -04002928 if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
2929 buf[0] = cpu_to_le32(cladatum->default_type);
2930 rc = put_entry(buf, sizeof(uint32_t), 1, fp);
2931 if (rc)
2932 return rc;
2933 }
2934
Eric Pariscee74f42010-10-13 17:50:25 -04002935 return 0;
2936}
2937
2938static int role_write(void *vkey, void *datum, void *ptr)
2939{
2940 char *key = vkey;
2941 struct role_datum *role = datum;
2942 struct policy_data *pd = ptr;
2943 void *fp = pd->fp;
2944 struct policydb *p = pd->p;
2945 __le32 buf[3];
2946 size_t items, len;
2947 int rc;
2948
2949 len = strlen(key);
2950 items = 0;
2951 buf[items++] = cpu_to_le32(len);
2952 buf[items++] = cpu_to_le32(role->value);
2953 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
2954 buf[items++] = cpu_to_le32(role->bounds);
2955
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05302956 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04002957
2958 rc = put_entry(buf, sizeof(u32), items, fp);
2959 if (rc)
2960 return rc;
2961
2962 rc = put_entry(key, 1, len, fp);
2963 if (rc)
2964 return rc;
2965
2966 rc = ebitmap_write(&role->dominates, fp);
2967 if (rc)
2968 return rc;
2969
2970 rc = ebitmap_write(&role->types, fp);
2971 if (rc)
2972 return rc;
2973
2974 return 0;
2975}
2976
2977static int type_write(void *vkey, void *datum, void *ptr)
2978{
2979 char *key = vkey;
2980 struct type_datum *typdatum = datum;
2981 struct policy_data *pd = ptr;
2982 struct policydb *p = pd->p;
2983 void *fp = pd->fp;
2984 __le32 buf[4];
2985 int rc;
2986 size_t items, len;
2987
2988 len = strlen(key);
2989 items = 0;
2990 buf[items++] = cpu_to_le32(len);
2991 buf[items++] = cpu_to_le32(typdatum->value);
2992 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
2993 u32 properties = 0;
2994
2995 if (typdatum->primary)
2996 properties |= TYPEDATUM_PROPERTY_PRIMARY;
2997
2998 if (typdatum->attribute)
2999 properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
3000
3001 buf[items++] = cpu_to_le32(properties);
3002 buf[items++] = cpu_to_le32(typdatum->bounds);
3003 } else {
3004 buf[items++] = cpu_to_le32(typdatum->primary);
3005 }
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303006 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003007 rc = put_entry(buf, sizeof(u32), items, fp);
3008 if (rc)
3009 return rc;
3010
3011 rc = put_entry(key, 1, len, fp);
3012 if (rc)
3013 return rc;
3014
3015 return 0;
3016}
3017
3018static int user_write(void *vkey, void *datum, void *ptr)
3019{
3020 char *key = vkey;
3021 struct user_datum *usrdatum = datum;
3022 struct policy_data *pd = ptr;
3023 struct policydb *p = pd->p;
3024 void *fp = pd->fp;
3025 __le32 buf[3];
3026 size_t items, len;
3027 int rc;
3028
3029 len = strlen(key);
3030 items = 0;
3031 buf[items++] = cpu_to_le32(len);
3032 buf[items++] = cpu_to_le32(usrdatum->value);
3033 if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
3034 buf[items++] = cpu_to_le32(usrdatum->bounds);
Himangi Saraogi5c7001b82014-06-17 01:41:05 +05303035 BUG_ON(items > ARRAY_SIZE(buf));
Eric Pariscee74f42010-10-13 17:50:25 -04003036 rc = put_entry(buf, sizeof(u32), items, fp);
3037 if (rc)
3038 return rc;
3039
3040 rc = put_entry(key, 1, len, fp);
3041 if (rc)
3042 return rc;
3043
3044 rc = ebitmap_write(&usrdatum->roles, fp);
3045 if (rc)
3046 return rc;
3047
3048 rc = mls_write_range_helper(&usrdatum->range, fp);
3049 if (rc)
3050 return rc;
3051
3052 rc = mls_write_level(&usrdatum->dfltlevel, fp);
3053 if (rc)
3054 return rc;
3055
3056 return 0;
3057}
3058
3059static int (*write_f[SYM_NUM]) (void *key, void *datum,
3060 void *datap) =
3061{
3062 common_write,
3063 class_write,
3064 role_write,
3065 type_write,
3066 user_write,
3067 cond_write_bool,
3068 sens_write,
3069 cat_write,
3070};
3071
3072static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
3073 void *fp)
3074{
3075 unsigned int i, j, rc;
3076 size_t nel, len;
3077 __le32 buf[3];
3078 u32 nodebuf[8];
3079 struct ocontext *c;
3080 for (i = 0; i < info->ocon_num; i++) {
3081 nel = 0;
3082 for (c = p->ocontexts[i]; c; c = c->next)
3083 nel++;
3084 buf[0] = cpu_to_le32(nel);
3085 rc = put_entry(buf, sizeof(u32), 1, fp);
3086 if (rc)
3087 return rc;
3088 for (c = p->ocontexts[i]; c; c = c->next) {
3089 switch (i) {
3090 case OCON_ISID:
3091 buf[0] = cpu_to_le32(c->sid[0]);
3092 rc = put_entry(buf, sizeof(u32), 1, fp);
3093 if (rc)
3094 return rc;
3095 rc = context_write(p, &c->context[0], fp);
3096 if (rc)
3097 return rc;
3098 break;
3099 case OCON_FS:
3100 case OCON_NETIF:
3101 len = strlen(c->u.name);
3102 buf[0] = cpu_to_le32(len);
3103 rc = put_entry(buf, sizeof(u32), 1, fp);
3104 if (rc)
3105 return rc;
3106 rc = put_entry(c->u.name, 1, len, fp);
3107 if (rc)
3108 return rc;
3109 rc = context_write(p, &c->context[0], fp);
3110 if (rc)
3111 return rc;
3112 rc = context_write(p, &c->context[1], fp);
3113 if (rc)
3114 return rc;
3115 break;
3116 case OCON_PORT:
3117 buf[0] = cpu_to_le32(c->u.port.protocol);
3118 buf[1] = cpu_to_le32(c->u.port.low_port);
3119 buf[2] = cpu_to_le32(c->u.port.high_port);
3120 rc = put_entry(buf, sizeof(u32), 3, fp);
3121 if (rc)
3122 return rc;
3123 rc = context_write(p, &c->context[0], fp);
3124 if (rc)
3125 return rc;
3126 break;
3127 case OCON_NODE:
3128 nodebuf[0] = c->u.node.addr; /* network order */
3129 nodebuf[1] = c->u.node.mask; /* network order */
3130 rc = put_entry(nodebuf, sizeof(u32), 2, fp);
3131 if (rc)
3132 return rc;
3133 rc = context_write(p, &c->context[0], fp);
3134 if (rc)
3135 return rc;
3136 break;
3137 case OCON_FSUSE:
3138 buf[0] = cpu_to_le32(c->v.behavior);
3139 len = strlen(c->u.name);
3140 buf[1] = cpu_to_le32(len);
3141 rc = put_entry(buf, sizeof(u32), 2, fp);
3142 if (rc)
3143 return rc;
3144 rc = put_entry(c->u.name, 1, len, fp);
3145 if (rc)
3146 return rc;
3147 rc = context_write(p, &c->context[0], fp);
3148 if (rc)
3149 return rc;
3150 break;
3151 case OCON_NODE6:
3152 for (j = 0; j < 4; j++)
3153 nodebuf[j] = c->u.node6.addr[j]; /* network order */
3154 for (j = 0; j < 4; j++)
3155 nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
3156 rc = put_entry(nodebuf, sizeof(u32), 8, fp);
3157 if (rc)
3158 return rc;
3159 rc = context_write(p, &c->context[0], fp);
3160 if (rc)
3161 return rc;
3162 break;
3163 }
3164 }
3165 }
3166 return 0;
3167}
3168
3169static int genfs_write(struct policydb *p, void *fp)
3170{
3171 struct genfs *genfs;
3172 struct ocontext *c;
3173 size_t len;
3174 __le32 buf[1];
3175 int rc;
3176
3177 len = 0;
3178 for (genfs = p->genfs; genfs; genfs = genfs->next)
3179 len++;
3180 buf[0] = cpu_to_le32(len);
3181 rc = put_entry(buf, sizeof(u32), 1, fp);
3182 if (rc)
3183 return rc;
3184 for (genfs = p->genfs; genfs; genfs = genfs->next) {
3185 len = strlen(genfs->fstype);
3186 buf[0] = cpu_to_le32(len);
3187 rc = put_entry(buf, sizeof(u32), 1, fp);
3188 if (rc)
3189 return rc;
3190 rc = put_entry(genfs->fstype, 1, len, fp);
3191 if (rc)
3192 return rc;
3193 len = 0;
3194 for (c = genfs->head; c; c = c->next)
3195 len++;
3196 buf[0] = cpu_to_le32(len);
3197 rc = put_entry(buf, sizeof(u32), 1, fp);
3198 if (rc)
3199 return rc;
3200 for (c = genfs->head; c; c = c->next) {
3201 len = strlen(c->u.name);
3202 buf[0] = cpu_to_le32(len);
3203 rc = put_entry(buf, sizeof(u32), 1, fp);
3204 if (rc)
3205 return rc;
3206 rc = put_entry(c->u.name, 1, len, fp);
3207 if (rc)
3208 return rc;
3209 buf[0] = cpu_to_le32(c->v.sclass);
3210 rc = put_entry(buf, sizeof(u32), 1, fp);
3211 if (rc)
3212 return rc;
3213 rc = context_write(p, &c->context[0], fp);
3214 if (rc)
3215 return rc;
3216 }
3217 }
3218 return 0;
3219}
3220
Eric Paris3f058ef2011-04-28 15:11:21 -04003221static int hashtab_cnt(void *key, void *data, void *ptr)
Eric Pariscee74f42010-10-13 17:50:25 -04003222{
3223 int *cnt = ptr;
3224 *cnt = *cnt + 1;
3225
3226 return 0;
3227}
3228
3229static int range_write_helper(void *key, void *data, void *ptr)
3230{
3231 __le32 buf[2];
3232 struct range_trans *rt = key;
3233 struct mls_range *r = data;
3234 struct policy_data *pd = ptr;
3235 void *fp = pd->fp;
3236 struct policydb *p = pd->p;
3237 int rc;
3238
3239 buf[0] = cpu_to_le32(rt->source_type);
3240 buf[1] = cpu_to_le32(rt->target_type);
3241 rc = put_entry(buf, sizeof(u32), 2, fp);
3242 if (rc)
3243 return rc;
3244 if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
3245 buf[0] = cpu_to_le32(rt->target_class);
3246 rc = put_entry(buf, sizeof(u32), 1, fp);
3247 if (rc)
3248 return rc;
3249 }
3250 rc = mls_write_range_helper(r, fp);
3251 if (rc)
3252 return rc;
3253
3254 return 0;
3255}
3256
3257static int range_write(struct policydb *p, void *fp)
3258{
Eric Pariscee74f42010-10-13 17:50:25 -04003259 __le32 buf[1];
Eric Parisb1380042013-07-23 17:38:42 -04003260 int rc, nel;
Eric Pariscee74f42010-10-13 17:50:25 -04003261 struct policy_data pd;
3262
3263 pd.p = p;
3264 pd.fp = fp;
3265
3266 /* count the number of entries in the hashtab */
3267 nel = 0;
Eric Paris3f058ef2011-04-28 15:11:21 -04003268 rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
Eric Pariscee74f42010-10-13 17:50:25 -04003269 if (rc)
3270 return rc;
3271
3272 buf[0] = cpu_to_le32(nel);
3273 rc = put_entry(buf, sizeof(u32), 1, fp);
3274 if (rc)
3275 return rc;
3276
3277 /* actually write all of the entries */
3278 rc = hashtab_map(p->range_tr, range_write_helper, &pd);
3279 if (rc)
3280 return rc;
3281
3282 return 0;
3283}
3284
Eric Paris2463c26d2011-04-28 15:11:21 -04003285static int filename_write_helper(void *key, void *data, void *ptr)
3286{
3287 __le32 buf[4];
3288 struct filename_trans *ft = key;
3289 struct filename_trans_datum *otype = data;
3290 void *fp = ptr;
3291 int rc;
3292 u32 len;
3293
3294 len = strlen(ft->name);
3295 buf[0] = cpu_to_le32(len);
3296 rc = put_entry(buf, sizeof(u32), 1, fp);
3297 if (rc)
3298 return rc;
3299
3300 rc = put_entry(ft->name, sizeof(char), len, fp);
3301 if (rc)
3302 return rc;
3303
Eric Paris9085a642014-02-20 10:56:45 -05003304 buf[0] = cpu_to_le32(ft->stype);
3305 buf[1] = cpu_to_le32(ft->ttype);
3306 buf[2] = cpu_to_le32(ft->tclass);
3307 buf[3] = cpu_to_le32(otype->otype);
Eric Paris2463c26d2011-04-28 15:11:21 -04003308
3309 rc = put_entry(buf, sizeof(u32), 4, fp);
3310 if (rc)
3311 return rc;
3312
3313 return 0;
3314}
3315
Eric Paris652bb9b2011-02-01 11:05:40 -05003316static int filename_trans_write(struct policydb *p, void *fp)
3317{
Eric Paris2463c26d2011-04-28 15:11:21 -04003318 u32 nel;
3319 __le32 buf[1];
Eric Paris652bb9b2011-02-01 11:05:40 -05003320 int rc;
3321
Roy.Lided50982011-05-20 10:38:06 +08003322 if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
3323 return 0;
3324
Eric Paris2463c26d2011-04-28 15:11:21 -04003325 nel = 0;
3326 rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
3327 if (rc)
3328 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003329
3330 buf[0] = cpu_to_le32(nel);
3331 rc = put_entry(buf, sizeof(u32), 1, fp);
3332 if (rc)
3333 return rc;
3334
Eric Paris2463c26d2011-04-28 15:11:21 -04003335 rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
3336 if (rc)
3337 return rc;
Eric Paris652bb9b2011-02-01 11:05:40 -05003338
Eric Paris652bb9b2011-02-01 11:05:40 -05003339 return 0;
3340}
Eric Paris2463c26d2011-04-28 15:11:21 -04003341
Eric Pariscee74f42010-10-13 17:50:25 -04003342/*
3343 * Write the configuration data in a policy database
3344 * structure to a policy database binary representation
3345 * file.
3346 */
3347int policydb_write(struct policydb *p, void *fp)
3348{
3349 unsigned int i, num_syms;
3350 int rc;
3351 __le32 buf[4];
3352 u32 config;
3353 size_t len;
3354 struct policydb_compat_info *info;
3355
3356 /*
3357 * refuse to write policy older than compressed avtab
3358 * to simplify the writer. There are other tests dropped
3359 * since we assume this throughout the writer code. Be
3360 * careful if you ever try to remove this restriction
3361 */
3362 if (p->policyvers < POLICYDB_VERSION_AVTAB) {
3363 printk(KERN_ERR "SELinux: refusing to write policy version %d."
3364 " Because it is less than version %d\n", p->policyvers,
3365 POLICYDB_VERSION_AVTAB);
3366 return -EINVAL;
3367 }
3368
3369 config = 0;
3370 if (p->mls_enabled)
3371 config |= POLICYDB_CONFIG_MLS;
3372
3373 if (p->reject_unknown)
3374 config |= REJECT_UNKNOWN;
3375 if (p->allow_unknown)
3376 config |= ALLOW_UNKNOWN;
3377
3378 /* Write the magic number and string identifiers. */
3379 buf[0] = cpu_to_le32(POLICYDB_MAGIC);
3380 len = strlen(POLICYDB_STRING);
3381 buf[1] = cpu_to_le32(len);
3382 rc = put_entry(buf, sizeof(u32), 2, fp);
3383 if (rc)
3384 return rc;
3385 rc = put_entry(POLICYDB_STRING, 1, len, fp);
3386 if (rc)
3387 return rc;
3388
3389 /* Write the version, config, and table sizes. */
3390 info = policydb_lookup_compat(p->policyvers);
3391 if (!info) {
3392 printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
3393 "version %d", p->policyvers);
Eric Paris9398c7f2010-11-23 11:40:08 -05003394 return -EINVAL;
Eric Pariscee74f42010-10-13 17:50:25 -04003395 }
3396
3397 buf[0] = cpu_to_le32(p->policyvers);
3398 buf[1] = cpu_to_le32(config);
3399 buf[2] = cpu_to_le32(info->sym_num);
3400 buf[3] = cpu_to_le32(info->ocon_num);
3401
3402 rc = put_entry(buf, sizeof(u32), 4, fp);
3403 if (rc)
3404 return rc;
3405
3406 if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
3407 rc = ebitmap_write(&p->policycaps, fp);
3408 if (rc)
3409 return rc;
3410 }
3411
3412 if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
3413 rc = ebitmap_write(&p->permissive_map, fp);
3414 if (rc)
3415 return rc;
3416 }
3417
3418 num_syms = info->sym_num;
3419 for (i = 0; i < num_syms; i++) {
3420 struct policy_data pd;
3421
3422 pd.fp = fp;
3423 pd.p = p;
3424
3425 buf[0] = cpu_to_le32(p->symtab[i].nprim);
3426 buf[1] = cpu_to_le32(p->symtab[i].table->nel);
3427
3428 rc = put_entry(buf, sizeof(u32), 2, fp);
3429 if (rc)
3430 return rc;
3431 rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
3432 if (rc)
3433 return rc;
3434 }
3435
3436 rc = avtab_write(p, &p->te_avtab, fp);
3437 if (rc)
3438 return rc;
3439
3440 rc = cond_write_list(p, p->cond_list, fp);
3441 if (rc)
3442 return rc;
3443
Harry Ciaoc900ff32011-03-25 13:52:00 +08003444 rc = role_trans_write(p, fp);
Eric Pariscee74f42010-10-13 17:50:25 -04003445 if (rc)
3446 return rc;
3447
3448 rc = role_allow_write(p->role_allow, fp);
3449 if (rc)
3450 return rc;
3451
Eric Paris652bb9b2011-02-01 11:05:40 -05003452 rc = filename_trans_write(p, fp);
3453 if (rc)
3454 return rc;
3455
Eric Pariscee74f42010-10-13 17:50:25 -04003456 rc = ocontext_write(p, info, fp);
3457 if (rc)
3458 return rc;
3459
3460 rc = genfs_write(p, fp);
3461 if (rc)
3462 return rc;
3463
3464 rc = range_write(p, fp);
3465 if (rc)
3466 return rc;
3467
3468 for (i = 0; i < p->p_types.nprim; i++) {
3469 struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
3470
3471 BUG_ON(!e);
3472 rc = ebitmap_write(e, fp);
3473 if (rc)
3474 return rc;
3475 }
3476
3477 return 0;
3478}