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