Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 14 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 16 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 17 | * |
| 18 | * Added support for the policy capability bitmap |
| 19 | * |
| 20 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * 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 Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 24 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * the Free Software Foundation, version 2. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/errno.h> |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 33 | #include <linux/audit.h> |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 34 | #include <linux/flex_array.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "security.h" |
| 36 | |
| 37 | #include "policydb.h" |
| 38 | #include "conditional.h" |
| 39 | #include "mls.h" |
| 40 | |
| 41 | #define _DEBUG_HASHES |
| 42 | |
| 43 | #ifdef DEBUG_HASHES |
Stephen Hemminger | 634a539 | 2010-03-04 21:59:03 -0800 | [diff] [blame] | 44 | static const char *symtab_name[SYM_NUM] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | "common prefixes", |
| 46 | "classes", |
| 47 | "roles", |
| 48 | "types", |
| 49 | "users", |
| 50 | "bools", |
| 51 | "levels", |
| 52 | "categories", |
| 53 | }; |
| 54 | #endif |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static unsigned int symtab_sizes[SYM_NUM] = { |
| 57 | 2, |
| 58 | 32, |
| 59 | 16, |
| 60 | 512, |
| 61 | 128, |
| 62 | 16, |
| 63 | 16, |
| 64 | 16, |
| 65 | }; |
| 66 | |
| 67 | struct policydb_compat_info { |
| 68 | int version; |
| 69 | int sym_num; |
| 70 | int ocon_num; |
| 71 | }; |
| 72 | |
| 73 | /* These need to be updated if SYM_NUM or OCON_NUM changes */ |
| 74 | static struct policydb_compat_info policydb_compat[] = { |
| 75 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 76 | .version = POLICYDB_VERSION_BASE, |
| 77 | .sym_num = SYM_NUM - 3, |
| 78 | .ocon_num = OCON_NUM - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }, |
| 80 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 81 | .version = POLICYDB_VERSION_BOOL, |
| 82 | .sym_num = SYM_NUM - 2, |
| 83 | .ocon_num = OCON_NUM - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }, |
| 85 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 86 | .version = POLICYDB_VERSION_IPV6, |
| 87 | .sym_num = SYM_NUM - 2, |
| 88 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }, |
| 90 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 91 | .version = POLICYDB_VERSION_NLCLASS, |
| 92 | .sym_num = SYM_NUM - 2, |
| 93 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | }, |
| 95 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 96 | .version = POLICYDB_VERSION_MLS, |
| 97 | .sym_num = SYM_NUM, |
| 98 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }, |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 100 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 101 | .version = POLICYDB_VERSION_AVTAB, |
| 102 | .sym_num = SYM_NUM, |
| 103 | .ocon_num = OCON_NUM, |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 104 | }, |
Darrel Goeddel | f3f8771 | 2006-09-25 23:31:59 -0700 | [diff] [blame] | 105 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 106 | .version = POLICYDB_VERSION_RANGETRANS, |
| 107 | .sym_num = SYM_NUM, |
| 108 | .ocon_num = OCON_NUM, |
Darrel Goeddel | f3f8771 | 2006-09-25 23:31:59 -0700 | [diff] [blame] | 109 | }, |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 110 | { |
| 111 | .version = POLICYDB_VERSION_POLCAP, |
| 112 | .sym_num = SYM_NUM, |
| 113 | .ocon_num = OCON_NUM, |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 114 | }, |
| 115 | { |
| 116 | .version = POLICYDB_VERSION_PERMISSIVE, |
| 117 | .sym_num = SYM_NUM, |
| 118 | .ocon_num = OCON_NUM, |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 119 | }, |
| 120 | { |
| 121 | .version = POLICYDB_VERSION_BOUNDARY, |
| 122 | .sym_num = SYM_NUM, |
| 123 | .ocon_num = OCON_NUM, |
| 124 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static struct policydb_compat_info *policydb_lookup_compat(int version) |
| 128 | { |
| 129 | int i; |
| 130 | struct policydb_compat_info *info = NULL; |
| 131 | |
Tobias Klauser | 32725ad | 2006-01-06 00:11:23 -0800 | [diff] [blame] | 132 | for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (policydb_compat[i].version == version) { |
| 134 | info = &policydb_compat[i]; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | return info; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Initialize the role table. |
| 143 | */ |
| 144 | static int roles_init(struct policydb *p) |
| 145 | { |
| 146 | char *key = NULL; |
| 147 | int rc; |
| 148 | struct role_datum *role; |
| 149 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 150 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (!role) { |
| 152 | rc = -ENOMEM; |
| 153 | goto out; |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | role->value = ++p->p_roles.nprim; |
| 156 | if (role->value != OBJECT_R_VAL) { |
| 157 | rc = -EINVAL; |
| 158 | goto out_free_role; |
| 159 | } |
Julia Lawall | b3139bb | 2010-05-14 21:30:30 +0200 | [diff] [blame] | 160 | key = kstrdup(OBJECT_R, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (!key) { |
| 162 | rc = -ENOMEM; |
| 163 | goto out_free_role; |
| 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | rc = hashtab_insert(p->p_roles.table, key, role); |
| 166 | if (rc) |
| 167 | goto out_free_key; |
| 168 | out: |
| 169 | return rc; |
| 170 | |
| 171 | out_free_key: |
| 172 | kfree(key); |
| 173 | out_free_role: |
| 174 | kfree(role); |
| 175 | goto out; |
| 176 | } |
| 177 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 178 | static u32 rangetr_hash(struct hashtab *h, const void *k) |
| 179 | { |
| 180 | const struct range_trans *key = k; |
| 181 | return (key->source_type + (key->target_type << 3) + |
| 182 | (key->target_class << 5)) & (h->size - 1); |
| 183 | } |
| 184 | |
| 185 | static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2) |
| 186 | { |
| 187 | const struct range_trans *key1 = k1, *key2 = k2; |
Eric Paris | 4419aae | 2010-10-13 17:50:14 -0400 | [diff] [blame^] | 188 | int v; |
| 189 | |
| 190 | v = key1->source_type - key2->source_type; |
| 191 | if (v) |
| 192 | return v; |
| 193 | |
| 194 | v = key1->target_type - key2->target_type; |
| 195 | if (v) |
| 196 | return v; |
| 197 | |
| 198 | v = key1->target_class - key2->target_class; |
| 199 | |
| 200 | return v; |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Initialize a policy database structure. |
| 205 | */ |
| 206 | static int policydb_init(struct policydb *p) |
| 207 | { |
| 208 | int i, rc; |
| 209 | |
| 210 | memset(p, 0, sizeof(*p)); |
| 211 | |
| 212 | for (i = 0; i < SYM_NUM; i++) { |
| 213 | rc = symtab_init(&p->symtab[i], symtab_sizes[i]); |
| 214 | if (rc) |
| 215 | goto out_free_symtab; |
| 216 | } |
| 217 | |
| 218 | rc = avtab_init(&p->te_avtab); |
| 219 | if (rc) |
| 220 | goto out_free_symtab; |
| 221 | |
| 222 | rc = roles_init(p); |
| 223 | if (rc) |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 224 | goto out_free_symtab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | rc = cond_policydb_init(p); |
| 227 | if (rc) |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 228 | goto out_free_symtab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 230 | p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256); |
| 231 | if (!p->range_tr) |
| 232 | goto out_free_symtab; |
| 233 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 234 | ebitmap_init(&p->policycaps); |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 235 | ebitmap_init(&p->permissive_map); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | out: |
| 238 | return rc; |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | out_free_symtab: |
| 241 | for (i = 0; i < SYM_NUM; i++) |
| 242 | hashtab_destroy(p->symtab[i].table); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * The following *_index functions are used to |
| 248 | * define the val_to_name and val_to_struct arrays |
| 249 | * in a policy database structure. The val_to_name |
| 250 | * arrays are used when converting security context |
| 251 | * structures into string representations. The |
| 252 | * val_to_struct arrays are used when the attributes |
| 253 | * of a class, role, or user are needed. |
| 254 | */ |
| 255 | |
| 256 | static int common_index(void *key, void *datum, void *datap) |
| 257 | { |
| 258 | struct policydb *p; |
| 259 | struct common_datum *comdatum; |
| 260 | |
| 261 | comdatum = datum; |
| 262 | p = datap; |
| 263 | if (!comdatum->value || comdatum->value > p->p_commons.nprim) |
| 264 | return -EINVAL; |
| 265 | p->p_common_val_to_name[comdatum->value - 1] = key; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int class_index(void *key, void *datum, void *datap) |
| 270 | { |
| 271 | struct policydb *p; |
| 272 | struct class_datum *cladatum; |
| 273 | |
| 274 | cladatum = datum; |
| 275 | p = datap; |
| 276 | if (!cladatum->value || cladatum->value > p->p_classes.nprim) |
| 277 | return -EINVAL; |
| 278 | p->p_class_val_to_name[cladatum->value - 1] = key; |
| 279 | p->class_val_to_struct[cladatum->value - 1] = cladatum; |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int role_index(void *key, void *datum, void *datap) |
| 284 | { |
| 285 | struct policydb *p; |
| 286 | struct role_datum *role; |
| 287 | |
| 288 | role = datum; |
| 289 | p = datap; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 290 | if (!role->value |
| 291 | || role->value > p->p_roles.nprim |
| 292 | || role->bounds > p->p_roles.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | p->p_role_val_to_name[role->value - 1] = key; |
| 295 | p->role_val_to_struct[role->value - 1] = role; |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int type_index(void *key, void *datum, void *datap) |
| 300 | { |
| 301 | struct policydb *p; |
| 302 | struct type_datum *typdatum; |
| 303 | |
| 304 | typdatum = datum; |
| 305 | p = datap; |
| 306 | |
| 307 | if (typdatum->primary) { |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 308 | if (!typdatum->value |
| 309 | || typdatum->value > p->p_types.nprim |
| 310 | || typdatum->bounds > p->p_types.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | p->p_type_val_to_name[typdatum->value - 1] = key; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 313 | p->type_val_to_struct[typdatum->value - 1] = typdatum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int user_index(void *key, void *datum, void *datap) |
| 320 | { |
| 321 | struct policydb *p; |
| 322 | struct user_datum *usrdatum; |
| 323 | |
| 324 | usrdatum = datum; |
| 325 | p = datap; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 326 | if (!usrdatum->value |
| 327 | || usrdatum->value > p->p_users.nprim |
| 328 | || usrdatum->bounds > p->p_users.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return -EINVAL; |
| 330 | p->p_user_val_to_name[usrdatum->value - 1] = key; |
| 331 | p->user_val_to_struct[usrdatum->value - 1] = usrdatum; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int sens_index(void *key, void *datum, void *datap) |
| 336 | { |
| 337 | struct policydb *p; |
| 338 | struct level_datum *levdatum; |
| 339 | |
| 340 | levdatum = datum; |
| 341 | p = datap; |
| 342 | |
| 343 | if (!levdatum->isalias) { |
| 344 | if (!levdatum->level->sens || |
| 345 | levdatum->level->sens > p->p_levels.nprim) |
| 346 | return -EINVAL; |
| 347 | p->p_sens_val_to_name[levdatum->level->sens - 1] = key; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int cat_index(void *key, void *datum, void *datap) |
| 354 | { |
| 355 | struct policydb *p; |
| 356 | struct cat_datum *catdatum; |
| 357 | |
| 358 | catdatum = datum; |
| 359 | p = datap; |
| 360 | |
| 361 | if (!catdatum->isalias) { |
| 362 | if (!catdatum->value || catdatum->value > p->p_cats.nprim) |
| 363 | return -EINVAL; |
| 364 | p->p_cat_val_to_name[catdatum->value - 1] = key; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) = |
| 371 | { |
| 372 | common_index, |
| 373 | class_index, |
| 374 | role_index, |
| 375 | type_index, |
| 376 | user_index, |
| 377 | cond_index_bool, |
| 378 | sens_index, |
| 379 | cat_index, |
| 380 | }; |
| 381 | |
| 382 | /* |
| 383 | * Define the common val_to_name array and the class |
| 384 | * val_to_name and val_to_struct arrays in a policy |
| 385 | * database structure. |
| 386 | * |
| 387 | * Caller must clean up upon failure. |
| 388 | */ |
| 389 | static int policydb_index_classes(struct policydb *p) |
| 390 | { |
| 391 | int rc; |
| 392 | |
| 393 | p->p_common_val_to_name = |
| 394 | kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL); |
| 395 | if (!p->p_common_val_to_name) { |
| 396 | rc = -ENOMEM; |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | rc = hashtab_map(p->p_commons.table, common_index, p); |
| 401 | if (rc) |
| 402 | goto out; |
| 403 | |
| 404 | p->class_val_to_struct = |
| 405 | kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL); |
| 406 | if (!p->class_val_to_struct) { |
| 407 | rc = -ENOMEM; |
| 408 | goto out; |
| 409 | } |
| 410 | |
| 411 | p->p_class_val_to_name = |
| 412 | kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL); |
| 413 | if (!p->p_class_val_to_name) { |
| 414 | rc = -ENOMEM; |
| 415 | goto out; |
| 416 | } |
| 417 | |
| 418 | rc = hashtab_map(p->p_classes.table, class_index, p); |
| 419 | out: |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | #ifdef DEBUG_HASHES |
| 424 | static void symtab_hash_eval(struct symtab *s) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | for (i = 0; i < SYM_NUM; i++) { |
| 429 | struct hashtab *h = s[i].table; |
| 430 | struct hashtab_info info; |
| 431 | |
| 432 | hashtab_stat(h, &info); |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 433 | printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | "longest chain length %d\n", symtab_name[i], h->nel, |
| 435 | info.slots_used, h->size, info.max_chain_len); |
| 436 | } |
| 437 | } |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 438 | |
| 439 | static void rangetr_hash_eval(struct hashtab *h) |
| 440 | { |
| 441 | struct hashtab_info info; |
| 442 | |
| 443 | hashtab_stat(h, &info); |
| 444 | printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, " |
| 445 | "longest chain length %d\n", h->nel, |
| 446 | info.slots_used, h->size, info.max_chain_len); |
| 447 | } |
| 448 | #else |
| 449 | static inline void rangetr_hash_eval(struct hashtab *h) |
| 450 | { |
| 451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | #endif |
| 453 | |
| 454 | /* |
| 455 | * Define the other val_to_name and val_to_struct arrays |
| 456 | * in a policy database structure. |
| 457 | * |
| 458 | * Caller must clean up on failure. |
| 459 | */ |
| 460 | static int policydb_index_others(struct policydb *p) |
| 461 | { |
| 462 | int i, rc = 0; |
| 463 | |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 464 | printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim); |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 466 | if (p->mls_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | printk(", %d sens, %d cats", p->p_levels.nprim, |
| 468 | p->p_cats.nprim); |
| 469 | printk("\n"); |
| 470 | |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 471 | printk(KERN_DEBUG "SELinux: %d classes, %d rules\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | p->p_classes.nprim, p->te_avtab.nel); |
| 473 | |
| 474 | #ifdef DEBUG_HASHES |
| 475 | avtab_hash_eval(&p->te_avtab, "rules"); |
| 476 | symtab_hash_eval(p->symtab); |
| 477 | #endif |
| 478 | |
| 479 | p->role_val_to_struct = |
| 480 | kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)), |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 481 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (!p->role_val_to_struct) { |
| 483 | rc = -ENOMEM; |
| 484 | goto out; |
| 485 | } |
| 486 | |
| 487 | p->user_val_to_struct = |
| 488 | kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)), |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 489 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (!p->user_val_to_struct) { |
| 491 | rc = -ENOMEM; |
| 492 | goto out; |
| 493 | } |
| 494 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 495 | p->type_val_to_struct = |
| 496 | kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)), |
| 497 | GFP_KERNEL); |
| 498 | if (!p->type_val_to_struct) { |
| 499 | rc = -ENOMEM; |
| 500 | goto out; |
| 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (cond_init_bool_indexes(p)) { |
| 504 | rc = -ENOMEM; |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | for (i = SYM_ROLES; i < SYM_NUM; i++) { |
| 509 | p->sym_val_to_name[i] = |
| 510 | kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL); |
| 511 | if (!p->sym_val_to_name[i]) { |
| 512 | rc = -ENOMEM; |
| 513 | goto out; |
| 514 | } |
| 515 | rc = hashtab_map(p->symtab[i].table, index_f[i], p); |
| 516 | if (rc) |
| 517 | goto out; |
| 518 | } |
| 519 | |
| 520 | out: |
| 521 | return rc; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * The following *_destroy functions are used to |
| 526 | * free any memory allocated for each kind of |
| 527 | * symbol data in the policy database. |
| 528 | */ |
| 529 | |
| 530 | static int perm_destroy(void *key, void *datum, void *p) |
| 531 | { |
| 532 | kfree(key); |
| 533 | kfree(datum); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int common_destroy(void *key, void *datum, void *p) |
| 538 | { |
| 539 | struct common_datum *comdatum; |
| 540 | |
| 541 | kfree(key); |
| 542 | comdatum = datum; |
| 543 | hashtab_map(comdatum->permissions.table, perm_destroy, NULL); |
| 544 | hashtab_destroy(comdatum->permissions.table); |
| 545 | kfree(datum); |
| 546 | return 0; |
| 547 | } |
| 548 | |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 549 | static int cls_destroy(void *key, void *datum, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | struct class_datum *cladatum; |
| 552 | struct constraint_node *constraint, *ctemp; |
| 553 | struct constraint_expr *e, *etmp; |
| 554 | |
| 555 | kfree(key); |
| 556 | cladatum = datum; |
| 557 | hashtab_map(cladatum->permissions.table, perm_destroy, NULL); |
| 558 | hashtab_destroy(cladatum->permissions.table); |
| 559 | constraint = cladatum->constraints; |
| 560 | while (constraint) { |
| 561 | e = constraint->expr; |
| 562 | while (e) { |
| 563 | ebitmap_destroy(&e->names); |
| 564 | etmp = e; |
| 565 | e = e->next; |
| 566 | kfree(etmp); |
| 567 | } |
| 568 | ctemp = constraint; |
| 569 | constraint = constraint->next; |
| 570 | kfree(ctemp); |
| 571 | } |
| 572 | |
| 573 | constraint = cladatum->validatetrans; |
| 574 | while (constraint) { |
| 575 | e = constraint->expr; |
| 576 | while (e) { |
| 577 | ebitmap_destroy(&e->names); |
| 578 | etmp = e; |
| 579 | e = e->next; |
| 580 | kfree(etmp); |
| 581 | } |
| 582 | ctemp = constraint; |
| 583 | constraint = constraint->next; |
| 584 | kfree(ctemp); |
| 585 | } |
| 586 | |
| 587 | kfree(cladatum->comkey); |
| 588 | kfree(datum); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int role_destroy(void *key, void *datum, void *p) |
| 593 | { |
| 594 | struct role_datum *role; |
| 595 | |
| 596 | kfree(key); |
| 597 | role = datum; |
| 598 | ebitmap_destroy(&role->dominates); |
| 599 | ebitmap_destroy(&role->types); |
| 600 | kfree(datum); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int type_destroy(void *key, void *datum, void *p) |
| 605 | { |
| 606 | kfree(key); |
| 607 | kfree(datum); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int user_destroy(void *key, void *datum, void *p) |
| 612 | { |
| 613 | struct user_datum *usrdatum; |
| 614 | |
| 615 | kfree(key); |
| 616 | usrdatum = datum; |
| 617 | ebitmap_destroy(&usrdatum->roles); |
| 618 | ebitmap_destroy(&usrdatum->range.level[0].cat); |
| 619 | ebitmap_destroy(&usrdatum->range.level[1].cat); |
| 620 | ebitmap_destroy(&usrdatum->dfltlevel.cat); |
| 621 | kfree(datum); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int sens_destroy(void *key, void *datum, void *p) |
| 626 | { |
| 627 | struct level_datum *levdatum; |
| 628 | |
| 629 | kfree(key); |
| 630 | levdatum = datum; |
| 631 | ebitmap_destroy(&levdatum->level->cat); |
| 632 | kfree(levdatum->level); |
| 633 | kfree(datum); |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int cat_destroy(void *key, void *datum, void *p) |
| 638 | { |
| 639 | kfree(key); |
| 640 | kfree(datum); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = |
| 645 | { |
| 646 | common_destroy, |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 647 | cls_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | role_destroy, |
| 649 | type_destroy, |
| 650 | user_destroy, |
| 651 | cond_destroy_bool, |
| 652 | sens_destroy, |
| 653 | cat_destroy, |
| 654 | }; |
| 655 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 656 | static int range_tr_destroy(void *key, void *datum, void *p) |
| 657 | { |
| 658 | struct mls_range *rt = datum; |
| 659 | kfree(key); |
| 660 | ebitmap_destroy(&rt->level[0].cat); |
| 661 | ebitmap_destroy(&rt->level[1].cat); |
| 662 | kfree(datum); |
| 663 | cond_resched(); |
| 664 | return 0; |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | static void ocontext_destroy(struct ocontext *c, int i) |
| 668 | { |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 669 | if (!c) |
| 670 | return; |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | context_destroy(&c->context[0]); |
| 673 | context_destroy(&c->context[1]); |
| 674 | if (i == OCON_ISID || i == OCON_FS || |
| 675 | i == OCON_NETIF || i == OCON_FSUSE) |
| 676 | kfree(c->u.name); |
| 677 | kfree(c); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Free any memory allocated by a policy database structure. |
| 682 | */ |
| 683 | void policydb_destroy(struct policydb *p) |
| 684 | { |
| 685 | struct ocontext *c, *ctmp; |
| 686 | struct genfs *g, *gtmp; |
| 687 | int i; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 688 | struct role_allow *ra, *lra = NULL; |
| 689 | struct role_trans *tr, *ltr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | for (i = 0; i < SYM_NUM; i++) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 692 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | hashtab_map(p->symtab[i].table, destroy_f[i], NULL); |
| 694 | hashtab_destroy(p->symtab[i].table); |
| 695 | } |
| 696 | |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 697 | for (i = 0; i < SYM_NUM; i++) |
| 698 | kfree(p->sym_val_to_name[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 700 | kfree(p->class_val_to_struct); |
| 701 | kfree(p->role_val_to_struct); |
| 702 | kfree(p->user_val_to_struct); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 703 | kfree(p->type_val_to_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | avtab_destroy(&p->te_avtab); |
| 706 | |
| 707 | for (i = 0; i < OCON_NUM; i++) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 708 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | c = p->ocontexts[i]; |
| 710 | while (c) { |
| 711 | ctmp = c; |
| 712 | c = c->next; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 713 | ocontext_destroy(ctmp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
Chad Sellers | 6e8c751 | 2006-10-06 16:09:52 -0400 | [diff] [blame] | 715 | p->ocontexts[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | g = p->genfs; |
| 719 | while (g) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 720 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | kfree(g->fstype); |
| 722 | c = g->head; |
| 723 | while (c) { |
| 724 | ctmp = c; |
| 725 | c = c->next; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 726 | ocontext_destroy(ctmp, OCON_FSUSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | gtmp = g; |
| 729 | g = g->next; |
| 730 | kfree(gtmp); |
| 731 | } |
Chad Sellers | 6e8c751 | 2006-10-06 16:09:52 -0400 | [diff] [blame] | 732 | p->genfs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | cond_policydb_destroy(p); |
| 735 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 736 | for (tr = p->role_tr; tr; tr = tr->next) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 737 | cond_resched(); |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 738 | kfree(ltr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 739 | ltr = tr; |
| 740 | } |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 741 | kfree(ltr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 742 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 743 | for (ra = p->role_allow; ra; ra = ra->next) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 744 | cond_resched(); |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 745 | kfree(lra); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 746 | lra = ra; |
| 747 | } |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 748 | kfree(lra); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 749 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 750 | hashtab_map(p->range_tr, range_tr_destroy, NULL); |
| 751 | hashtab_destroy(p->range_tr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 752 | |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 753 | if (p->type_attr_map_array) { |
| 754 | for (i = 0; i < p->p_types.nprim; i++) { |
| 755 | struct ebitmap *e; |
| 756 | |
| 757 | e = flex_array_get(p->type_attr_map_array, i); |
| 758 | if (!e) |
| 759 | continue; |
| 760 | ebitmap_destroy(e); |
| 761 | } |
| 762 | flex_array_free(p->type_attr_map_array); |
Stephen Smalley | 282c1f5 | 2005-10-23 12:57:15 -0700 | [diff] [blame] | 763 | } |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 764 | ebitmap_destroy(&p->policycaps); |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 765 | ebitmap_destroy(&p->permissive_map); |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Load the initial SIDs specified in a policy database |
| 772 | * structure into a SID table. |
| 773 | */ |
| 774 | int policydb_load_isids(struct policydb *p, struct sidtab *s) |
| 775 | { |
| 776 | struct ocontext *head, *c; |
| 777 | int rc; |
| 778 | |
| 779 | rc = sidtab_init(s); |
| 780 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 781 | printk(KERN_ERR "SELinux: out of memory on SID table init\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | head = p->ocontexts[OCON_ISID]; |
| 786 | for (c = head; c; c = c->next) { |
| 787 | if (!c->context[0].user) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 788 | printk(KERN_ERR "SELinux: SID %s was never " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | "defined.\n", c->u.name); |
| 790 | rc = -EINVAL; |
| 791 | goto out; |
| 792 | } |
| 793 | if (sidtab_insert(s, c->sid[0], &c->context[0])) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 794 | printk(KERN_ERR "SELinux: unable to load initial " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | "SID %s.\n", c->u.name); |
| 796 | rc = -EINVAL; |
| 797 | goto out; |
| 798 | } |
| 799 | } |
| 800 | out: |
| 801 | return rc; |
| 802 | } |
| 803 | |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 804 | int policydb_class_isvalid(struct policydb *p, unsigned int class) |
| 805 | { |
| 806 | if (!class || class > p->p_classes.nprim) |
| 807 | return 0; |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | int policydb_role_isvalid(struct policydb *p, unsigned int role) |
| 812 | { |
| 813 | if (!role || role > p->p_roles.nprim) |
| 814 | return 0; |
| 815 | return 1; |
| 816 | } |
| 817 | |
| 818 | int policydb_type_isvalid(struct policydb *p, unsigned int type) |
| 819 | { |
| 820 | if (!type || type > p->p_types.nprim) |
| 821 | return 0; |
| 822 | return 1; |
| 823 | } |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /* |
| 826 | * Return 1 if the fields in the security context |
| 827 | * structure `c' are valid. Return 0 otherwise. |
| 828 | */ |
| 829 | int policydb_context_isvalid(struct policydb *p, struct context *c) |
| 830 | { |
| 831 | struct role_datum *role; |
| 832 | struct user_datum *usrdatum; |
| 833 | |
| 834 | if (!c->role || c->role > p->p_roles.nprim) |
| 835 | return 0; |
| 836 | |
| 837 | if (!c->user || c->user > p->p_users.nprim) |
| 838 | return 0; |
| 839 | |
| 840 | if (!c->type || c->type > p->p_types.nprim) |
| 841 | return 0; |
| 842 | |
| 843 | if (c->role != OBJECT_R_VAL) { |
| 844 | /* |
| 845 | * Role must be authorized for the type. |
| 846 | */ |
| 847 | role = p->role_val_to_struct[c->role - 1]; |
| 848 | if (!ebitmap_get_bit(&role->types, |
| 849 | c->type - 1)) |
| 850 | /* role may not be associated with type */ |
| 851 | return 0; |
| 852 | |
| 853 | /* |
| 854 | * User must be authorized for the role. |
| 855 | */ |
| 856 | usrdatum = p->user_val_to_struct[c->user - 1]; |
| 857 | if (!usrdatum) |
| 858 | return 0; |
| 859 | |
| 860 | if (!ebitmap_get_bit(&usrdatum->roles, |
| 861 | c->role - 1)) |
| 862 | /* user may not be associated with role */ |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | if (!mls_context_isvalid(p, c)) |
| 867 | return 0; |
| 868 | |
| 869 | return 1; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Read a MLS range structure from a policydb binary |
| 874 | * representation file. |
| 875 | */ |
| 876 | static int mls_read_range_helper(struct mls_range *r, void *fp) |
| 877 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 878 | __le32 buf[2]; |
| 879 | u32 items; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | int rc; |
| 881 | |
| 882 | rc = next_entry(buf, fp, sizeof(u32)); |
| 883 | if (rc < 0) |
| 884 | goto out; |
| 885 | |
| 886 | items = le32_to_cpu(buf[0]); |
| 887 | if (items > ARRAY_SIZE(buf)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 888 | printk(KERN_ERR "SELinux: mls: range overflow\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | rc = -EINVAL; |
| 890 | goto out; |
| 891 | } |
| 892 | rc = next_entry(buf, fp, sizeof(u32) * items); |
| 893 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 894 | printk(KERN_ERR "SELinux: mls: truncated range\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | goto out; |
| 896 | } |
| 897 | r->level[0].sens = le32_to_cpu(buf[0]); |
| 898 | if (items > 1) |
| 899 | r->level[1].sens = le32_to_cpu(buf[1]); |
| 900 | else |
| 901 | r->level[1].sens = r->level[0].sens; |
| 902 | |
| 903 | rc = ebitmap_read(&r->level[0].cat, fp); |
| 904 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 905 | printk(KERN_ERR "SELinux: mls: error reading low " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | "categories\n"); |
| 907 | goto out; |
| 908 | } |
| 909 | if (items > 1) { |
| 910 | rc = ebitmap_read(&r->level[1].cat, fp); |
| 911 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 912 | printk(KERN_ERR "SELinux: mls: error reading high " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | "categories\n"); |
| 914 | goto bad_high; |
| 915 | } |
| 916 | } else { |
| 917 | rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat); |
| 918 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 919 | printk(KERN_ERR "SELinux: mls: out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | goto bad_high; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | rc = 0; |
| 925 | out: |
| 926 | return rc; |
| 927 | bad_high: |
| 928 | ebitmap_destroy(&r->level[0].cat); |
| 929 | goto out; |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Read and validate a security context structure |
| 934 | * from a policydb binary representation file. |
| 935 | */ |
| 936 | static int context_read_and_validate(struct context *c, |
| 937 | struct policydb *p, |
| 938 | void *fp) |
| 939 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 940 | __le32 buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | int rc; |
| 942 | |
| 943 | rc = next_entry(buf, fp, sizeof buf); |
| 944 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 945 | printk(KERN_ERR "SELinux: context truncated\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto out; |
| 947 | } |
| 948 | c->user = le32_to_cpu(buf[0]); |
| 949 | c->role = le32_to_cpu(buf[1]); |
| 950 | c->type = le32_to_cpu(buf[2]); |
| 951 | if (p->policyvers >= POLICYDB_VERSION_MLS) { |
| 952 | if (mls_read_range_helper(&c->range, fp)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 953 | printk(KERN_ERR "SELinux: error reading MLS range of " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | "context\n"); |
| 955 | rc = -EINVAL; |
| 956 | goto out; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (!policydb_context_isvalid(p, c)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 961 | printk(KERN_ERR "SELinux: invalid security context\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | context_destroy(c); |
| 963 | rc = -EINVAL; |
| 964 | } |
| 965 | out: |
| 966 | return rc; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * The following *_read functions are used to |
| 971 | * read the symbol data from a policy database |
| 972 | * binary representation file. |
| 973 | */ |
| 974 | |
| 975 | static int perm_read(struct policydb *p, struct hashtab *h, void *fp) |
| 976 | { |
| 977 | char *key = NULL; |
| 978 | struct perm_datum *perdatum; |
| 979 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 980 | __le32 buf[2]; |
| 981 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 983 | perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (!perdatum) { |
| 985 | rc = -ENOMEM; |
| 986 | goto out; |
| 987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
| 989 | rc = next_entry(buf, fp, sizeof buf); |
| 990 | if (rc < 0) |
| 991 | goto bad; |
| 992 | |
| 993 | len = le32_to_cpu(buf[0]); |
| 994 | perdatum->value = le32_to_cpu(buf[1]); |
| 995 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 996 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | if (!key) { |
| 998 | rc = -ENOMEM; |
| 999 | goto bad; |
| 1000 | } |
| 1001 | rc = next_entry(key, fp, len); |
| 1002 | if (rc < 0) |
| 1003 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1004 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
| 1006 | rc = hashtab_insert(h, key, perdatum); |
| 1007 | if (rc) |
| 1008 | goto bad; |
| 1009 | out: |
| 1010 | return rc; |
| 1011 | bad: |
| 1012 | perm_destroy(key, perdatum, NULL); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
| 1016 | static int common_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1017 | { |
| 1018 | char *key = NULL; |
| 1019 | struct common_datum *comdatum; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1020 | __le32 buf[4]; |
| 1021 | u32 len, nel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | int i, rc; |
| 1023 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1024 | comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (!comdatum) { |
| 1026 | rc = -ENOMEM; |
| 1027 | goto out; |
| 1028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | rc = next_entry(buf, fp, sizeof buf); |
| 1031 | if (rc < 0) |
| 1032 | goto bad; |
| 1033 | |
| 1034 | len = le32_to_cpu(buf[0]); |
| 1035 | comdatum->value = le32_to_cpu(buf[1]); |
| 1036 | |
| 1037 | rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE); |
| 1038 | if (rc) |
| 1039 | goto bad; |
| 1040 | comdatum->permissions.nprim = le32_to_cpu(buf[2]); |
| 1041 | nel = le32_to_cpu(buf[3]); |
| 1042 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1043 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (!key) { |
| 1045 | rc = -ENOMEM; |
| 1046 | goto bad; |
| 1047 | } |
| 1048 | rc = next_entry(key, fp, len); |
| 1049 | if (rc < 0) |
| 1050 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1051 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | for (i = 0; i < nel; i++) { |
| 1054 | rc = perm_read(p, comdatum->permissions.table, fp); |
| 1055 | if (rc) |
| 1056 | goto bad; |
| 1057 | } |
| 1058 | |
| 1059 | rc = hashtab_insert(h, key, comdatum); |
| 1060 | if (rc) |
| 1061 | goto bad; |
| 1062 | out: |
| 1063 | return rc; |
| 1064 | bad: |
| 1065 | common_destroy(key, comdatum, NULL); |
| 1066 | goto out; |
| 1067 | } |
| 1068 | |
| 1069 | static int read_cons_helper(struct constraint_node **nodep, int ncons, |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1070 | int allowxtarget, void *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | { |
| 1072 | struct constraint_node *c, *lc; |
| 1073 | struct constraint_expr *e, *le; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1074 | __le32 buf[3]; |
| 1075 | u32 nexpr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | int rc, i, j, depth; |
| 1077 | |
| 1078 | lc = NULL; |
| 1079 | for (i = 0; i < ncons; i++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1080 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (!c) |
| 1082 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1084 | if (lc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | lc->next = c; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1086 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | *nodep = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); |
| 1090 | if (rc < 0) |
| 1091 | return rc; |
| 1092 | c->permissions = le32_to_cpu(buf[0]); |
| 1093 | nexpr = le32_to_cpu(buf[1]); |
| 1094 | le = NULL; |
| 1095 | depth = -1; |
| 1096 | for (j = 0; j < nexpr; j++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1097 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | if (!e) |
| 1099 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1101 | if (le) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | le->next = e; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1103 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | c->expr = e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
| 1106 | rc = next_entry(buf, fp, (sizeof(u32) * 3)); |
| 1107 | if (rc < 0) |
| 1108 | return rc; |
| 1109 | e->expr_type = le32_to_cpu(buf[0]); |
| 1110 | e->attr = le32_to_cpu(buf[1]); |
| 1111 | e->op = le32_to_cpu(buf[2]); |
| 1112 | |
| 1113 | switch (e->expr_type) { |
| 1114 | case CEXPR_NOT: |
| 1115 | if (depth < 0) |
| 1116 | return -EINVAL; |
| 1117 | break; |
| 1118 | case CEXPR_AND: |
| 1119 | case CEXPR_OR: |
| 1120 | if (depth < 1) |
| 1121 | return -EINVAL; |
| 1122 | depth--; |
| 1123 | break; |
| 1124 | case CEXPR_ATTR: |
| 1125 | if (depth == (CEXPR_MAXDEPTH - 1)) |
| 1126 | return -EINVAL; |
| 1127 | depth++; |
| 1128 | break; |
| 1129 | case CEXPR_NAMES: |
| 1130 | if (!allowxtarget && (e->attr & CEXPR_XTARGET)) |
| 1131 | return -EINVAL; |
| 1132 | if (depth == (CEXPR_MAXDEPTH - 1)) |
| 1133 | return -EINVAL; |
| 1134 | depth++; |
| 1135 | if (ebitmap_read(&e->names, fp)) |
| 1136 | return -EINVAL; |
| 1137 | break; |
| 1138 | default: |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | le = e; |
| 1142 | } |
| 1143 | if (depth != 0) |
| 1144 | return -EINVAL; |
| 1145 | lc = c; |
| 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | static int class_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1152 | { |
| 1153 | char *key = NULL; |
| 1154 | struct class_datum *cladatum; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1155 | __le32 buf[6]; |
| 1156 | u32 len, len2, ncons, nel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | int i, rc; |
| 1158 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1159 | cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | if (!cladatum) { |
| 1161 | rc = -ENOMEM; |
| 1162 | goto out; |
| 1163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
| 1165 | rc = next_entry(buf, fp, sizeof(u32)*6); |
| 1166 | if (rc < 0) |
| 1167 | goto bad; |
| 1168 | |
| 1169 | len = le32_to_cpu(buf[0]); |
| 1170 | len2 = le32_to_cpu(buf[1]); |
| 1171 | cladatum->value = le32_to_cpu(buf[2]); |
| 1172 | |
| 1173 | rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE); |
| 1174 | if (rc) |
| 1175 | goto bad; |
| 1176 | cladatum->permissions.nprim = le32_to_cpu(buf[3]); |
| 1177 | nel = le32_to_cpu(buf[4]); |
| 1178 | |
| 1179 | ncons = le32_to_cpu(buf[5]); |
| 1180 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1181 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | if (!key) { |
| 1183 | rc = -ENOMEM; |
| 1184 | goto bad; |
| 1185 | } |
| 1186 | rc = next_entry(key, fp, len); |
| 1187 | if (rc < 0) |
| 1188 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1189 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | if (len2) { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1192 | cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | if (!cladatum->comkey) { |
| 1194 | rc = -ENOMEM; |
| 1195 | goto bad; |
| 1196 | } |
| 1197 | rc = next_entry(cladatum->comkey, fp, len2); |
| 1198 | if (rc < 0) |
| 1199 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1200 | cladatum->comkey[len2] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | cladatum->comdatum = hashtab_search(p->p_commons.table, |
| 1203 | cladatum->comkey); |
| 1204 | if (!cladatum->comdatum) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1205 | printk(KERN_ERR "SELinux: unknown common %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | cladatum->comkey); |
| 1207 | rc = -EINVAL; |
| 1208 | goto bad; |
| 1209 | } |
| 1210 | } |
| 1211 | for (i = 0; i < nel; i++) { |
| 1212 | rc = perm_read(p, cladatum->permissions.table, fp); |
| 1213 | if (rc) |
| 1214 | goto bad; |
| 1215 | } |
| 1216 | |
| 1217 | rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp); |
| 1218 | if (rc) |
| 1219 | goto bad; |
| 1220 | |
| 1221 | if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) { |
| 1222 | /* grab the validatetrans rules */ |
| 1223 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1224 | if (rc < 0) |
| 1225 | goto bad; |
| 1226 | ncons = le32_to_cpu(buf[0]); |
| 1227 | rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp); |
| 1228 | if (rc) |
| 1229 | goto bad; |
| 1230 | } |
| 1231 | |
| 1232 | rc = hashtab_insert(h, key, cladatum); |
| 1233 | if (rc) |
| 1234 | goto bad; |
| 1235 | |
| 1236 | rc = 0; |
| 1237 | out: |
| 1238 | return rc; |
| 1239 | bad: |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 1240 | cls_destroy(key, cladatum, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | goto out; |
| 1242 | } |
| 1243 | |
| 1244 | static int role_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1245 | { |
| 1246 | char *key = NULL; |
| 1247 | struct role_datum *role; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1248 | int rc, to_read = 2; |
| 1249 | __le32 buf[3]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1250 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1252 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | if (!role) { |
| 1254 | rc = -ENOMEM; |
| 1255 | goto out; |
| 1256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1258 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1259 | to_read = 3; |
| 1260 | |
| 1261 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | if (rc < 0) |
| 1263 | goto bad; |
| 1264 | |
| 1265 | len = le32_to_cpu(buf[0]); |
| 1266 | role->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1267 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1268 | role->bounds = le32_to_cpu(buf[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1270 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | if (!key) { |
| 1272 | rc = -ENOMEM; |
| 1273 | goto bad; |
| 1274 | } |
| 1275 | rc = next_entry(key, fp, len); |
| 1276 | if (rc < 0) |
| 1277 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1278 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | rc = ebitmap_read(&role->dominates, fp); |
| 1281 | if (rc) |
| 1282 | goto bad; |
| 1283 | |
| 1284 | rc = ebitmap_read(&role->types, fp); |
| 1285 | if (rc) |
| 1286 | goto bad; |
| 1287 | |
| 1288 | if (strcmp(key, OBJECT_R) == 0) { |
| 1289 | if (role->value != OBJECT_R_VAL) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1290 | printk(KERN_ERR "SELinux: Role %s has wrong value %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | OBJECT_R, role->value); |
| 1292 | rc = -EINVAL; |
| 1293 | goto bad; |
| 1294 | } |
| 1295 | rc = 0; |
| 1296 | goto bad; |
| 1297 | } |
| 1298 | |
| 1299 | rc = hashtab_insert(h, key, role); |
| 1300 | if (rc) |
| 1301 | goto bad; |
| 1302 | out: |
| 1303 | return rc; |
| 1304 | bad: |
| 1305 | role_destroy(key, role, NULL); |
| 1306 | goto out; |
| 1307 | } |
| 1308 | |
| 1309 | static int type_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1310 | { |
| 1311 | char *key = NULL; |
| 1312 | struct type_datum *typdatum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1313 | int rc, to_read = 3; |
| 1314 | __le32 buf[4]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1315 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1317 | typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | if (!typdatum) { |
| 1319 | rc = -ENOMEM; |
| 1320 | return rc; |
| 1321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1323 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1324 | to_read = 4; |
| 1325 | |
| 1326 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | if (rc < 0) |
| 1328 | goto bad; |
| 1329 | |
| 1330 | len = le32_to_cpu(buf[0]); |
| 1331 | typdatum->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1332 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { |
| 1333 | u32 prop = le32_to_cpu(buf[2]); |
| 1334 | |
| 1335 | if (prop & TYPEDATUM_PROPERTY_PRIMARY) |
| 1336 | typdatum->primary = 1; |
| 1337 | if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE) |
| 1338 | typdatum->attribute = 1; |
| 1339 | |
| 1340 | typdatum->bounds = le32_to_cpu(buf[3]); |
| 1341 | } else { |
| 1342 | typdatum->primary = le32_to_cpu(buf[2]); |
| 1343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1345 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (!key) { |
| 1347 | rc = -ENOMEM; |
| 1348 | goto bad; |
| 1349 | } |
| 1350 | rc = next_entry(key, fp, len); |
| 1351 | if (rc < 0) |
| 1352 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1353 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | rc = hashtab_insert(h, key, typdatum); |
| 1356 | if (rc) |
| 1357 | goto bad; |
| 1358 | out: |
| 1359 | return rc; |
| 1360 | bad: |
| 1361 | type_destroy(key, typdatum, NULL); |
| 1362 | goto out; |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | /* |
| 1367 | * Read a MLS level structure from a policydb binary |
| 1368 | * representation file. |
| 1369 | */ |
| 1370 | static int mls_read_level(struct mls_level *lp, void *fp) |
| 1371 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1372 | __le32 buf[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | int rc; |
| 1374 | |
| 1375 | memset(lp, 0, sizeof(*lp)); |
| 1376 | |
| 1377 | rc = next_entry(buf, fp, sizeof buf); |
| 1378 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1379 | printk(KERN_ERR "SELinux: mls: truncated level\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | goto bad; |
| 1381 | } |
| 1382 | lp->sens = le32_to_cpu(buf[0]); |
| 1383 | |
| 1384 | if (ebitmap_read(&lp->cat, fp)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1385 | printk(KERN_ERR "SELinux: mls: error reading level " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | "categories\n"); |
| 1387 | goto bad; |
| 1388 | } |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 1389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return 0; |
| 1391 | |
| 1392 | bad: |
| 1393 | return -EINVAL; |
| 1394 | } |
| 1395 | |
| 1396 | static int user_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1397 | { |
| 1398 | char *key = NULL; |
| 1399 | struct user_datum *usrdatum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1400 | int rc, to_read = 2; |
| 1401 | __le32 buf[3]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1402 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1404 | usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | if (!usrdatum) { |
| 1406 | rc = -ENOMEM; |
| 1407 | goto out; |
| 1408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1410 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1411 | to_read = 3; |
| 1412 | |
| 1413 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | if (rc < 0) |
| 1415 | goto bad; |
| 1416 | |
| 1417 | len = le32_to_cpu(buf[0]); |
| 1418 | usrdatum->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1419 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1420 | usrdatum->bounds = le32_to_cpu(buf[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1422 | key = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | if (!key) { |
| 1424 | rc = -ENOMEM; |
| 1425 | goto bad; |
| 1426 | } |
| 1427 | rc = next_entry(key, fp, len); |
| 1428 | if (rc < 0) |
| 1429 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1430 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
| 1432 | rc = ebitmap_read(&usrdatum->roles, fp); |
| 1433 | if (rc) |
| 1434 | goto bad; |
| 1435 | |
| 1436 | if (p->policyvers >= POLICYDB_VERSION_MLS) { |
| 1437 | rc = mls_read_range_helper(&usrdatum->range, fp); |
| 1438 | if (rc) |
| 1439 | goto bad; |
| 1440 | rc = mls_read_level(&usrdatum->dfltlevel, fp); |
| 1441 | if (rc) |
| 1442 | goto bad; |
| 1443 | } |
| 1444 | |
| 1445 | rc = hashtab_insert(h, key, usrdatum); |
| 1446 | if (rc) |
| 1447 | goto bad; |
| 1448 | out: |
| 1449 | return rc; |
| 1450 | bad: |
| 1451 | user_destroy(key, usrdatum, NULL); |
| 1452 | goto out; |
| 1453 | } |
| 1454 | |
| 1455 | static int sens_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1456 | { |
| 1457 | char *key = NULL; |
| 1458 | struct level_datum *levdatum; |
| 1459 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1460 | __le32 buf[2]; |
| 1461 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1463 | levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | if (!levdatum) { |
| 1465 | rc = -ENOMEM; |
| 1466 | goto out; |
| 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
| 1469 | rc = next_entry(buf, fp, sizeof buf); |
| 1470 | if (rc < 0) |
| 1471 | goto bad; |
| 1472 | |
| 1473 | len = le32_to_cpu(buf[0]); |
| 1474 | levdatum->isalias = le32_to_cpu(buf[1]); |
| 1475 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1476 | key = kmalloc(len + 1, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!key) { |
| 1478 | rc = -ENOMEM; |
| 1479 | goto bad; |
| 1480 | } |
| 1481 | rc = next_entry(key, fp, len); |
| 1482 | if (rc < 0) |
| 1483 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1484 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
| 1486 | levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); |
| 1487 | if (!levdatum->level) { |
| 1488 | rc = -ENOMEM; |
| 1489 | goto bad; |
| 1490 | } |
| 1491 | if (mls_read_level(levdatum->level, fp)) { |
| 1492 | rc = -EINVAL; |
| 1493 | goto bad; |
| 1494 | } |
| 1495 | |
| 1496 | rc = hashtab_insert(h, key, levdatum); |
| 1497 | if (rc) |
| 1498 | goto bad; |
| 1499 | out: |
| 1500 | return rc; |
| 1501 | bad: |
| 1502 | sens_destroy(key, levdatum, NULL); |
| 1503 | goto out; |
| 1504 | } |
| 1505 | |
| 1506 | static int cat_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1507 | { |
| 1508 | char *key = NULL; |
| 1509 | struct cat_datum *catdatum; |
| 1510 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1511 | __le32 buf[3]; |
| 1512 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1514 | catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | if (!catdatum) { |
| 1516 | rc = -ENOMEM; |
| 1517 | goto out; |
| 1518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
| 1520 | rc = next_entry(buf, fp, sizeof buf); |
| 1521 | if (rc < 0) |
| 1522 | goto bad; |
| 1523 | |
| 1524 | len = le32_to_cpu(buf[0]); |
| 1525 | catdatum->value = le32_to_cpu(buf[1]); |
| 1526 | catdatum->isalias = le32_to_cpu(buf[2]); |
| 1527 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1528 | key = kmalloc(len + 1, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | if (!key) { |
| 1530 | rc = -ENOMEM; |
| 1531 | goto bad; |
| 1532 | } |
| 1533 | rc = next_entry(key, fp, len); |
| 1534 | if (rc < 0) |
| 1535 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1536 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | rc = hashtab_insert(h, key, catdatum); |
| 1539 | if (rc) |
| 1540 | goto bad; |
| 1541 | out: |
| 1542 | return rc; |
| 1543 | |
| 1544 | bad: |
| 1545 | cat_destroy(key, catdatum, NULL); |
| 1546 | goto out; |
| 1547 | } |
| 1548 | |
| 1549 | static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) = |
| 1550 | { |
| 1551 | common_read, |
| 1552 | class_read, |
| 1553 | role_read, |
| 1554 | type_read, |
| 1555 | user_read, |
| 1556 | cond_read_bool, |
| 1557 | sens_read, |
| 1558 | cat_read, |
| 1559 | }; |
| 1560 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1561 | static int user_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1562 | { |
| 1563 | struct user_datum *upper, *user; |
| 1564 | struct policydb *p = datap; |
| 1565 | int depth = 0; |
| 1566 | |
| 1567 | upper = user = datum; |
| 1568 | while (upper->bounds) { |
| 1569 | struct ebitmap_node *node; |
| 1570 | unsigned long bit; |
| 1571 | |
| 1572 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1573 | printk(KERN_ERR "SELinux: user %s: " |
| 1574 | "too deep or looped boundary", |
| 1575 | (char *) key); |
| 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
| 1579 | upper = p->user_val_to_struct[upper->bounds - 1]; |
| 1580 | ebitmap_for_each_positive_bit(&user->roles, node, bit) { |
| 1581 | if (ebitmap_get_bit(&upper->roles, bit)) |
| 1582 | continue; |
| 1583 | |
| 1584 | printk(KERN_ERR |
| 1585 | "SELinux: boundary violated policy: " |
| 1586 | "user=%s role=%s bounds=%s\n", |
| 1587 | p->p_user_val_to_name[user->value - 1], |
| 1588 | p->p_role_val_to_name[bit], |
| 1589 | p->p_user_val_to_name[upper->value - 1]); |
| 1590 | |
| 1591 | return -EINVAL; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | static int role_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1599 | { |
| 1600 | struct role_datum *upper, *role; |
| 1601 | struct policydb *p = datap; |
| 1602 | int depth = 0; |
| 1603 | |
| 1604 | upper = role = datum; |
| 1605 | while (upper->bounds) { |
| 1606 | struct ebitmap_node *node; |
| 1607 | unsigned long bit; |
| 1608 | |
| 1609 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1610 | printk(KERN_ERR "SELinux: role %s: " |
| 1611 | "too deep or looped bounds\n", |
| 1612 | (char *) key); |
| 1613 | return -EINVAL; |
| 1614 | } |
| 1615 | |
| 1616 | upper = p->role_val_to_struct[upper->bounds - 1]; |
| 1617 | ebitmap_for_each_positive_bit(&role->types, node, bit) { |
| 1618 | if (ebitmap_get_bit(&upper->types, bit)) |
| 1619 | continue; |
| 1620 | |
| 1621 | printk(KERN_ERR |
| 1622 | "SELinux: boundary violated policy: " |
| 1623 | "role=%s type=%s bounds=%s\n", |
| 1624 | p->p_role_val_to_name[role->value - 1], |
| 1625 | p->p_type_val_to_name[bit], |
| 1626 | p->p_role_val_to_name[upper->value - 1]); |
| 1627 | |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static int type_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1636 | { |
Eric Paris | daa6d83 | 2010-08-03 15:26:05 -0400 | [diff] [blame] | 1637 | struct type_datum *upper; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1638 | struct policydb *p = datap; |
| 1639 | int depth = 0; |
| 1640 | |
Eric Paris | daa6d83 | 2010-08-03 15:26:05 -0400 | [diff] [blame] | 1641 | upper = datum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1642 | while (upper->bounds) { |
| 1643 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1644 | printk(KERN_ERR "SELinux: type %s: " |
| 1645 | "too deep or looped boundary\n", |
| 1646 | (char *) key); |
| 1647 | return -EINVAL; |
| 1648 | } |
| 1649 | |
| 1650 | upper = p->type_val_to_struct[upper->bounds - 1]; |
| 1651 | if (upper->attribute) { |
| 1652 | printk(KERN_ERR "SELinux: type %s: " |
| 1653 | "bounded by attribute %s", |
| 1654 | (char *) key, |
| 1655 | p->p_type_val_to_name[upper->value - 1]); |
| 1656 | return -EINVAL; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | static int policydb_bounds_sanity_check(struct policydb *p) |
| 1664 | { |
| 1665 | int rc; |
| 1666 | |
| 1667 | if (p->policyvers < POLICYDB_VERSION_BOUNDARY) |
| 1668 | return 0; |
| 1669 | |
| 1670 | rc = hashtab_map(p->p_users.table, |
| 1671 | user_bounds_sanity_check, p); |
| 1672 | if (rc) |
| 1673 | return rc; |
| 1674 | |
| 1675 | rc = hashtab_map(p->p_roles.table, |
| 1676 | role_bounds_sanity_check, p); |
| 1677 | if (rc) |
| 1678 | return rc; |
| 1679 | |
| 1680 | rc = hashtab_map(p->p_types.table, |
| 1681 | type_bounds_sanity_check, p); |
| 1682 | if (rc) |
| 1683 | return rc; |
| 1684 | |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | extern int ss_initialized; |
| 1689 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1690 | u16 string_to_security_class(struct policydb *p, const char *name) |
| 1691 | { |
| 1692 | struct class_datum *cladatum; |
| 1693 | |
| 1694 | cladatum = hashtab_search(p->p_classes.table, name); |
| 1695 | if (!cladatum) |
| 1696 | return 0; |
| 1697 | |
| 1698 | return cladatum->value; |
| 1699 | } |
| 1700 | |
| 1701 | u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name) |
| 1702 | { |
| 1703 | struct class_datum *cladatum; |
| 1704 | struct perm_datum *perdatum = NULL; |
| 1705 | struct common_datum *comdatum; |
| 1706 | |
| 1707 | if (!tclass || tclass > p->p_classes.nprim) |
| 1708 | return 0; |
| 1709 | |
| 1710 | cladatum = p->class_val_to_struct[tclass-1]; |
| 1711 | comdatum = cladatum->comdatum; |
| 1712 | if (comdatum) |
| 1713 | perdatum = hashtab_search(comdatum->permissions.table, |
| 1714 | name); |
| 1715 | if (!perdatum) |
| 1716 | perdatum = hashtab_search(cladatum->permissions.table, |
| 1717 | name); |
| 1718 | if (!perdatum) |
| 1719 | return 0; |
| 1720 | |
| 1721 | return 1U << (perdatum->value-1); |
| 1722 | } |
| 1723 | |
Eric Paris | 9ee0c82 | 2010-06-11 12:37:05 -0400 | [diff] [blame] | 1724 | static int range_read(struct policydb *p, void *fp) |
| 1725 | { |
| 1726 | struct range_trans *rt = NULL; |
| 1727 | struct mls_range *r = NULL; |
| 1728 | int i, rc; |
| 1729 | __le32 buf[2]; |
| 1730 | u32 nel; |
| 1731 | |
| 1732 | if (p->policyvers < POLICYDB_VERSION_MLS) |
| 1733 | return 0; |
| 1734 | |
| 1735 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1736 | if (rc) |
| 1737 | goto out; |
| 1738 | |
| 1739 | nel = le32_to_cpu(buf[0]); |
| 1740 | for (i = 0; i < nel; i++) { |
| 1741 | rc = -ENOMEM; |
| 1742 | rt = kzalloc(sizeof(*rt), GFP_KERNEL); |
| 1743 | if (!rt) |
| 1744 | goto out; |
| 1745 | |
| 1746 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); |
| 1747 | if (rc) |
| 1748 | goto out; |
| 1749 | |
| 1750 | rt->source_type = le32_to_cpu(buf[0]); |
| 1751 | rt->target_type = le32_to_cpu(buf[1]); |
| 1752 | if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { |
| 1753 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1754 | if (rc) |
| 1755 | goto out; |
| 1756 | rt->target_class = le32_to_cpu(buf[0]); |
| 1757 | } else |
| 1758 | rt->target_class = p->process_class; |
| 1759 | |
| 1760 | rc = -EINVAL; |
| 1761 | if (!policydb_type_isvalid(p, rt->source_type) || |
| 1762 | !policydb_type_isvalid(p, rt->target_type) || |
| 1763 | !policydb_class_isvalid(p, rt->target_class)) |
| 1764 | goto out; |
| 1765 | |
| 1766 | rc = -ENOMEM; |
| 1767 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 1768 | if (!r) |
| 1769 | goto out; |
| 1770 | |
| 1771 | rc = mls_read_range_helper(r, fp); |
| 1772 | if (rc) |
| 1773 | goto out; |
| 1774 | |
| 1775 | rc = -EINVAL; |
| 1776 | if (!mls_range_isvalid(p, r)) { |
| 1777 | printk(KERN_WARNING "SELinux: rangetrans: invalid range\n"); |
| 1778 | goto out; |
| 1779 | } |
| 1780 | |
| 1781 | rc = hashtab_insert(p->range_tr, rt, r); |
| 1782 | if (rc) |
| 1783 | goto out; |
| 1784 | |
| 1785 | rt = NULL; |
| 1786 | r = NULL; |
| 1787 | } |
| 1788 | rangetr_hash_eval(p->range_tr); |
| 1789 | rc = 0; |
| 1790 | out: |
| 1791 | kfree(rt); |
| 1792 | kfree(r); |
| 1793 | return rc; |
| 1794 | } |
| 1795 | |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 1796 | static int genfs_read(struct policydb *p, void *fp) |
| 1797 | { |
| 1798 | int i, j, rc; |
| 1799 | u32 nel, nel2, len, len2; |
| 1800 | __le32 buf[1]; |
| 1801 | struct ocontext *l, *c; |
| 1802 | struct ocontext *newc = NULL; |
| 1803 | struct genfs *genfs_p, *genfs; |
| 1804 | struct genfs *newgenfs = NULL; |
| 1805 | |
| 1806 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1807 | if (rc) |
| 1808 | goto out; |
| 1809 | nel = le32_to_cpu(buf[0]); |
| 1810 | |
| 1811 | for (i = 0; i < nel; i++) { |
| 1812 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1813 | if (rc) |
| 1814 | goto out; |
| 1815 | len = le32_to_cpu(buf[0]); |
| 1816 | |
| 1817 | rc = -ENOMEM; |
| 1818 | newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL); |
| 1819 | if (!newgenfs) |
| 1820 | goto out; |
| 1821 | |
| 1822 | rc = -ENOMEM; |
| 1823 | newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL); |
| 1824 | if (!newgenfs->fstype) |
| 1825 | goto out; |
| 1826 | |
| 1827 | rc = next_entry(newgenfs->fstype, fp, len); |
| 1828 | if (rc) |
| 1829 | goto out; |
| 1830 | |
| 1831 | newgenfs->fstype[len] = 0; |
| 1832 | |
| 1833 | for (genfs_p = NULL, genfs = p->genfs; genfs; |
| 1834 | genfs_p = genfs, genfs = genfs->next) { |
| 1835 | rc = -EINVAL; |
| 1836 | if (strcmp(newgenfs->fstype, genfs->fstype) == 0) { |
| 1837 | printk(KERN_ERR "SELinux: dup genfs fstype %s\n", |
| 1838 | newgenfs->fstype); |
| 1839 | goto out; |
| 1840 | } |
| 1841 | if (strcmp(newgenfs->fstype, genfs->fstype) < 0) |
| 1842 | break; |
| 1843 | } |
| 1844 | newgenfs->next = genfs; |
| 1845 | if (genfs_p) |
| 1846 | genfs_p->next = newgenfs; |
| 1847 | else |
| 1848 | p->genfs = newgenfs; |
| 1849 | genfs = newgenfs; |
| 1850 | newgenfs = NULL; |
| 1851 | |
| 1852 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1853 | if (rc) |
| 1854 | goto out; |
| 1855 | |
| 1856 | nel2 = le32_to_cpu(buf[0]); |
| 1857 | for (j = 0; j < nel2; j++) { |
| 1858 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1859 | if (rc) |
| 1860 | goto out; |
| 1861 | len = le32_to_cpu(buf[0]); |
| 1862 | |
| 1863 | rc = -ENOMEM; |
| 1864 | newc = kzalloc(sizeof(*newc), GFP_KERNEL); |
| 1865 | if (!newc) |
| 1866 | goto out; |
| 1867 | |
| 1868 | rc = -ENOMEM; |
| 1869 | newc->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 1870 | if (!newc->u.name) |
| 1871 | goto out; |
| 1872 | |
| 1873 | rc = next_entry(newc->u.name, fp, len); |
| 1874 | if (rc) |
| 1875 | goto out; |
| 1876 | newc->u.name[len] = 0; |
| 1877 | |
| 1878 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1879 | if (rc) |
| 1880 | goto out; |
| 1881 | |
| 1882 | newc->v.sclass = le32_to_cpu(buf[0]); |
| 1883 | rc = context_read_and_validate(&newc->context[0], p, fp); |
| 1884 | if (rc) |
| 1885 | goto out; |
| 1886 | |
| 1887 | for (l = NULL, c = genfs->head; c; |
| 1888 | l = c, c = c->next) { |
| 1889 | rc = -EINVAL; |
| 1890 | if (!strcmp(newc->u.name, c->u.name) && |
| 1891 | (!c->v.sclass || !newc->v.sclass || |
| 1892 | newc->v.sclass == c->v.sclass)) { |
| 1893 | printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n", |
| 1894 | genfs->fstype, c->u.name); |
| 1895 | goto out; |
| 1896 | } |
| 1897 | len = strlen(newc->u.name); |
| 1898 | len2 = strlen(c->u.name); |
| 1899 | if (len > len2) |
| 1900 | break; |
| 1901 | } |
| 1902 | |
| 1903 | newc->next = c; |
| 1904 | if (l) |
| 1905 | l->next = newc; |
| 1906 | else |
| 1907 | genfs->head = newc; |
| 1908 | newc = NULL; |
| 1909 | } |
| 1910 | } |
| 1911 | rc = 0; |
| 1912 | out: |
| 1913 | if (newgenfs) |
| 1914 | kfree(newgenfs->fstype); |
| 1915 | kfree(newgenfs); |
| 1916 | ocontext_destroy(newc, OCON_FSUSE); |
| 1917 | |
| 1918 | return rc; |
| 1919 | } |
| 1920 | |
Eric Paris | 692a8a2 | 2010-07-21 12:51:03 -0400 | [diff] [blame] | 1921 | static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, |
| 1922 | void *fp) |
| 1923 | { |
| 1924 | int i, j, rc; |
| 1925 | u32 nel, len; |
| 1926 | __le32 buf[3]; |
| 1927 | struct ocontext *l, *c; |
| 1928 | u32 nodebuf[8]; |
| 1929 | |
| 1930 | for (i = 0; i < info->ocon_num; i++) { |
| 1931 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1932 | if (rc) |
| 1933 | goto out; |
| 1934 | nel = le32_to_cpu(buf[0]); |
| 1935 | |
| 1936 | l = NULL; |
| 1937 | for (j = 0; j < nel; j++) { |
| 1938 | rc = -ENOMEM; |
| 1939 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 1940 | if (!c) |
| 1941 | goto out; |
| 1942 | if (l) |
| 1943 | l->next = c; |
| 1944 | else |
| 1945 | p->ocontexts[i] = c; |
| 1946 | l = c; |
| 1947 | |
| 1948 | switch (i) { |
| 1949 | case OCON_ISID: |
| 1950 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1951 | if (rc) |
| 1952 | goto out; |
| 1953 | |
| 1954 | c->sid[0] = le32_to_cpu(buf[0]); |
| 1955 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 1956 | if (rc) |
| 1957 | goto out; |
| 1958 | break; |
| 1959 | case OCON_FS: |
| 1960 | case OCON_NETIF: |
| 1961 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1962 | if (rc) |
| 1963 | goto out; |
| 1964 | len = le32_to_cpu(buf[0]); |
| 1965 | |
| 1966 | rc = -ENOMEM; |
| 1967 | c->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 1968 | if (!c->u.name) |
| 1969 | goto out; |
| 1970 | |
| 1971 | rc = next_entry(c->u.name, fp, len); |
| 1972 | if (rc) |
| 1973 | goto out; |
| 1974 | |
| 1975 | c->u.name[len] = 0; |
| 1976 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 1977 | if (rc) |
| 1978 | goto out; |
| 1979 | rc = context_read_and_validate(&c->context[1], p, fp); |
| 1980 | if (rc) |
| 1981 | goto out; |
| 1982 | break; |
| 1983 | case OCON_PORT: |
| 1984 | rc = next_entry(buf, fp, sizeof(u32)*3); |
| 1985 | if (rc) |
| 1986 | goto out; |
| 1987 | c->u.port.protocol = le32_to_cpu(buf[0]); |
| 1988 | c->u.port.low_port = le32_to_cpu(buf[1]); |
| 1989 | c->u.port.high_port = le32_to_cpu(buf[2]); |
| 1990 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 1991 | if (rc) |
| 1992 | goto out; |
| 1993 | break; |
| 1994 | case OCON_NODE: |
| 1995 | rc = next_entry(nodebuf, fp, sizeof(u32) * 2); |
| 1996 | if (rc) |
| 1997 | goto out; |
| 1998 | c->u.node.addr = nodebuf[0]; /* network order */ |
| 1999 | c->u.node.mask = nodebuf[1]; /* network order */ |
| 2000 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2001 | if (rc) |
| 2002 | goto out; |
| 2003 | break; |
| 2004 | case OCON_FSUSE: |
| 2005 | rc = next_entry(buf, fp, sizeof(u32)*2); |
| 2006 | if (rc) |
| 2007 | goto out; |
| 2008 | |
| 2009 | rc = -EINVAL; |
| 2010 | c->v.behavior = le32_to_cpu(buf[0]); |
| 2011 | if (c->v.behavior > SECURITY_FS_USE_NONE) |
| 2012 | goto out; |
| 2013 | |
| 2014 | rc = -ENOMEM; |
| 2015 | len = le32_to_cpu(buf[1]); |
| 2016 | c->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 2017 | if (!c->u.name) |
| 2018 | goto out; |
| 2019 | |
| 2020 | rc = next_entry(c->u.name, fp, len); |
| 2021 | if (rc) |
| 2022 | goto out; |
| 2023 | c->u.name[len] = 0; |
| 2024 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2025 | if (rc) |
| 2026 | goto out; |
| 2027 | break; |
| 2028 | case OCON_NODE6: { |
| 2029 | int k; |
| 2030 | |
| 2031 | rc = next_entry(nodebuf, fp, sizeof(u32) * 8); |
| 2032 | if (rc) |
| 2033 | goto out; |
| 2034 | for (k = 0; k < 4; k++) |
| 2035 | c->u.node6.addr[k] = nodebuf[k]; |
| 2036 | for (k = 0; k < 4; k++) |
| 2037 | c->u.node6.mask[k] = nodebuf[k+4]; |
| 2038 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2039 | if (rc) |
| 2040 | goto out; |
| 2041 | break; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | rc = 0; |
| 2047 | out: |
| 2048 | return rc; |
| 2049 | } |
| 2050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | /* |
| 2052 | * Read the configuration data from a policy database binary |
| 2053 | * representation file into a policy database structure. |
| 2054 | */ |
| 2055 | int policydb_read(struct policydb *p, void *fp) |
| 2056 | { |
| 2057 | struct role_allow *ra, *lra; |
| 2058 | struct role_trans *tr, *ltr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | int i, j, rc; |
Stephen Smalley | 59dbd1b | 2008-06-05 09:48:51 -0400 | [diff] [blame] | 2060 | __le32 buf[4]; |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 2061 | u32 len, nprim, nel; |
| 2062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | char *policydb_str; |
| 2064 | struct policydb_compat_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | rc = policydb_init(p); |
| 2067 | if (rc) |
| 2068 | goto out; |
| 2069 | |
| 2070 | /* Read the magic number and string length. */ |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2071 | rc = next_entry(buf, fp, sizeof(u32) * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | if (rc < 0) |
| 2073 | goto bad; |
| 2074 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2075 | if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2076 | printk(KERN_ERR "SELinux: policydb magic number 0x%x does " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | "not match expected magic number 0x%x\n", |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2078 | le32_to_cpu(buf[0]), POLICYDB_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | goto bad; |
| 2080 | } |
| 2081 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2082 | len = le32_to_cpu(buf[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | if (len != strlen(POLICYDB_STRING)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2084 | printk(KERN_ERR "SELinux: policydb string length %d does not " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | "match expected length %Zu\n", |
| 2086 | len, strlen(POLICYDB_STRING)); |
| 2087 | goto bad; |
| 2088 | } |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2089 | policydb_str = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | if (!policydb_str) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2091 | printk(KERN_ERR "SELinux: unable to allocate memory for policydb " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | "string of length %d\n", len); |
| 2093 | rc = -ENOMEM; |
| 2094 | goto bad; |
| 2095 | } |
| 2096 | rc = next_entry(policydb_str, fp, len); |
| 2097 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2098 | printk(KERN_ERR "SELinux: truncated policydb string identifier\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | kfree(policydb_str); |
| 2100 | goto bad; |
| 2101 | } |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 2102 | policydb_str[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | if (strcmp(policydb_str, POLICYDB_STRING)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2104 | printk(KERN_ERR "SELinux: policydb string %s does not match " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | "my string %s\n", policydb_str, POLICYDB_STRING); |
| 2106 | kfree(policydb_str); |
| 2107 | goto bad; |
| 2108 | } |
| 2109 | /* Done with policydb_str. */ |
| 2110 | kfree(policydb_str); |
| 2111 | policydb_str = NULL; |
| 2112 | |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2113 | /* Read the version and table sizes. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | rc = next_entry(buf, fp, sizeof(u32)*4); |
| 2115 | if (rc < 0) |
| 2116 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2118 | p->policyvers = le32_to_cpu(buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | if (p->policyvers < POLICYDB_VERSION_MIN || |
| 2120 | p->policyvers > POLICYDB_VERSION_MAX) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2121 | printk(KERN_ERR "SELinux: policydb version %d does not match " |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2122 | "my version range %d-%d\n", |
| 2123 | le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX); |
| 2124 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2127 | if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) { |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2128 | p->mls_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | if (p->policyvers < POLICYDB_VERSION_MLS) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2131 | printk(KERN_ERR "SELinux: security policydb version %d " |
| 2132 | "(MLS) not backwards compatible\n", |
| 2133 | p->policyvers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | goto bad; |
| 2135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 2137 | p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN); |
| 2138 | p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2140 | if (p->policyvers >= POLICYDB_VERSION_POLCAP && |
| 2141 | ebitmap_read(&p->policycaps, fp) != 0) |
| 2142 | goto bad; |
| 2143 | |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 2144 | if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE && |
| 2145 | ebitmap_read(&p->permissive_map, fp) != 0) |
| 2146 | goto bad; |
| 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | info = policydb_lookup_compat(p->policyvers); |
| 2149 | if (!info) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2150 | printk(KERN_ERR "SELinux: unable to find policy compat info " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | "for version %d\n", p->policyvers); |
| 2152 | goto bad; |
| 2153 | } |
| 2154 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2155 | if (le32_to_cpu(buf[2]) != info->sym_num || |
| 2156 | le32_to_cpu(buf[3]) != info->ocon_num) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2157 | printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do " |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2158 | "not match mine (%d,%d)\n", le32_to_cpu(buf[2]), |
| 2159 | le32_to_cpu(buf[3]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | info->sym_num, info->ocon_num); |
| 2161 | goto bad; |
| 2162 | } |
| 2163 | |
| 2164 | for (i = 0; i < info->sym_num; i++) { |
| 2165 | rc = next_entry(buf, fp, sizeof(u32)*2); |
| 2166 | if (rc < 0) |
| 2167 | goto bad; |
| 2168 | nprim = le32_to_cpu(buf[0]); |
| 2169 | nel = le32_to_cpu(buf[1]); |
| 2170 | for (j = 0; j < nel; j++) { |
| 2171 | rc = read_f[i](p, p->symtab[i].table, fp); |
| 2172 | if (rc) |
| 2173 | goto bad; |
| 2174 | } |
| 2175 | |
| 2176 | p->symtab[i].nprim = nprim; |
| 2177 | } |
| 2178 | |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2179 | rc = avtab_read(&p->te_avtab, fp, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | if (rc) |
| 2181 | goto bad; |
| 2182 | |
| 2183 | if (p->policyvers >= POLICYDB_VERSION_BOOL) { |
| 2184 | rc = cond_read_list(p, fp); |
| 2185 | if (rc) |
| 2186 | goto bad; |
| 2187 | } |
| 2188 | |
| 2189 | rc = next_entry(buf, fp, sizeof(u32)); |
| 2190 | if (rc < 0) |
| 2191 | goto bad; |
| 2192 | nel = le32_to_cpu(buf[0]); |
| 2193 | ltr = NULL; |
| 2194 | for (i = 0; i < nel; i++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2195 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | if (!tr) { |
| 2197 | rc = -ENOMEM; |
| 2198 | goto bad; |
| 2199 | } |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2200 | if (ltr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | ltr->next = tr; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2202 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | p->role_tr = tr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | rc = next_entry(buf, fp, sizeof(u32)*3); |
| 2205 | if (rc < 0) |
| 2206 | goto bad; |
| 2207 | tr->role = le32_to_cpu(buf[0]); |
| 2208 | tr->type = le32_to_cpu(buf[1]); |
| 2209 | tr->new_role = le32_to_cpu(buf[2]); |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2210 | if (!policydb_role_isvalid(p, tr->role) || |
| 2211 | !policydb_type_isvalid(p, tr->type) || |
| 2212 | !policydb_role_isvalid(p, tr->new_role)) { |
| 2213 | rc = -EINVAL; |
| 2214 | goto bad; |
| 2215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | ltr = tr; |
| 2217 | } |
| 2218 | |
| 2219 | rc = next_entry(buf, fp, sizeof(u32)); |
| 2220 | if (rc < 0) |
| 2221 | goto bad; |
| 2222 | nel = le32_to_cpu(buf[0]); |
| 2223 | lra = NULL; |
| 2224 | for (i = 0; i < nel; i++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2225 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | if (!ra) { |
| 2227 | rc = -ENOMEM; |
| 2228 | goto bad; |
| 2229 | } |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2230 | if (lra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | lra->next = ra; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2232 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | p->role_allow = ra; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | rc = next_entry(buf, fp, sizeof(u32)*2); |
| 2235 | if (rc < 0) |
| 2236 | goto bad; |
| 2237 | ra->role = le32_to_cpu(buf[0]); |
| 2238 | ra->new_role = le32_to_cpu(buf[1]); |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2239 | if (!policydb_role_isvalid(p, ra->role) || |
| 2240 | !policydb_role_isvalid(p, ra->new_role)) { |
| 2241 | rc = -EINVAL; |
| 2242 | goto bad; |
| 2243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | lra = ra; |
| 2245 | } |
| 2246 | |
| 2247 | rc = policydb_index_classes(p); |
| 2248 | if (rc) |
| 2249 | goto bad; |
| 2250 | |
| 2251 | rc = policydb_index_others(p); |
| 2252 | if (rc) |
| 2253 | goto bad; |
| 2254 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2255 | p->process_class = string_to_security_class(p, "process"); |
| 2256 | if (!p->process_class) |
| 2257 | goto bad; |
| 2258 | p->process_trans_perms = string_to_av_perm(p, p->process_class, |
| 2259 | "transition"); |
| 2260 | p->process_trans_perms |= string_to_av_perm(p, p->process_class, |
| 2261 | "dyntransition"); |
| 2262 | if (!p->process_trans_perms) |
| 2263 | goto bad; |
| 2264 | |
Eric Paris | 692a8a2 | 2010-07-21 12:51:03 -0400 | [diff] [blame] | 2265 | rc = ocontext_read(p, info, fp); |
| 2266 | if (rc) |
| 2267 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 2269 | rc = genfs_read(p, fp); |
| 2270 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
Eric Paris | 9ee0c82 | 2010-06-11 12:37:05 -0400 | [diff] [blame] | 2273 | rc = range_read(p, fp); |
| 2274 | if (rc) |
| 2275 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2277 | rc = -ENOMEM; |
| 2278 | p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap), |
| 2279 | p->p_types.nprim, |
| 2280 | GFP_KERNEL | __GFP_ZERO); |
| 2281 | if (!p->type_attr_map_array) |
| 2282 | goto bad; |
| 2283 | |
| 2284 | /* preallocate so we don't have to worry about the put ever failing */ |
| 2285 | rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1, |
| 2286 | GFP_KERNEL | __GFP_ZERO); |
| 2287 | if (rc) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2288 | goto bad; |
| 2289 | |
| 2290 | for (i = 0; i < p->p_types.nprim; i++) { |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2291 | struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); |
| 2292 | |
| 2293 | BUG_ON(!e); |
| 2294 | ebitmap_init(e); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2295 | if (p->policyvers >= POLICYDB_VERSION_AVTAB) { |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2296 | rc = ebitmap_read(e, fp); |
| 2297 | if (rc) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2298 | goto bad; |
| 2299 | } |
| 2300 | /* add the type itself as the degenerate case */ |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2301 | rc = ebitmap_set_bit(e, i, 1); |
| 2302 | if (rc) |
| 2303 | goto bad; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 2306 | rc = policydb_bounds_sanity_check(p); |
| 2307 | if (rc) |
| 2308 | goto bad; |
| 2309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | rc = 0; |
| 2311 | out: |
| 2312 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | bad: |
| 2314 | if (!rc) |
| 2315 | rc = -EINVAL; |
| 2316 | policydb_destroy(p); |
| 2317 | goto out; |
| 2318 | } |