Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of the policy database. |
| 3 | * |
| 4 | * Author : Stephen Smalley, <sds@epoch.ncsc.mil> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> |
| 9 | * |
| 10 | * Support for enhanced MLS infrastructure. |
| 11 | * |
| 12 | * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com> |
| 13 | * |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 14 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 16 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 17 | * |
| 18 | * Added support for the policy capability bitmap |
| 19 | * |
| 20 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
| 22 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC |
| 23 | * This program is free software; you can redistribute it and/or modify |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 24 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * the Free Software Foundation, version 2. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/errno.h> |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 33 | #include <linux/audit.h> |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 34 | #include <linux/flex_array.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "security.h" |
| 36 | |
| 37 | #include "policydb.h" |
| 38 | #include "conditional.h" |
| 39 | #include "mls.h" |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 40 | #include "services.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #define _DEBUG_HASHES |
| 43 | |
| 44 | #ifdef DEBUG_HASHES |
Stephen Hemminger | 634a539 | 2010-03-04 21:59:03 -0800 | [diff] [blame] | 45 | static const char *symtab_name[SYM_NUM] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | "common prefixes", |
| 47 | "classes", |
| 48 | "roles", |
| 49 | "types", |
| 50 | "users", |
| 51 | "bools", |
| 52 | "levels", |
| 53 | "categories", |
| 54 | }; |
| 55 | #endif |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static unsigned int symtab_sizes[SYM_NUM] = { |
| 58 | 2, |
| 59 | 32, |
| 60 | 16, |
| 61 | 512, |
| 62 | 128, |
| 63 | 16, |
| 64 | 16, |
| 65 | 16, |
| 66 | }; |
| 67 | |
| 68 | struct policydb_compat_info { |
| 69 | int version; |
| 70 | int sym_num; |
| 71 | int ocon_num; |
| 72 | }; |
| 73 | |
| 74 | /* These need to be updated if SYM_NUM or OCON_NUM changes */ |
| 75 | static struct policydb_compat_info policydb_compat[] = { |
| 76 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 77 | .version = POLICYDB_VERSION_BASE, |
| 78 | .sym_num = SYM_NUM - 3, |
| 79 | .ocon_num = OCON_NUM - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | }, |
| 81 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 82 | .version = POLICYDB_VERSION_BOOL, |
| 83 | .sym_num = SYM_NUM - 2, |
| 84 | .ocon_num = OCON_NUM - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }, |
| 86 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 87 | .version = POLICYDB_VERSION_IPV6, |
| 88 | .sym_num = SYM_NUM - 2, |
| 89 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | }, |
| 91 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 92 | .version = POLICYDB_VERSION_NLCLASS, |
| 93 | .sym_num = SYM_NUM - 2, |
| 94 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | }, |
| 96 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 97 | .version = POLICYDB_VERSION_MLS, |
| 98 | .sym_num = SYM_NUM, |
| 99 | .ocon_num = OCON_NUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }, |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 101 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 102 | .version = POLICYDB_VERSION_AVTAB, |
| 103 | .sym_num = SYM_NUM, |
| 104 | .ocon_num = OCON_NUM, |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 105 | }, |
Darrel Goeddel | f3f8771 | 2006-09-25 23:31:59 -0700 | [diff] [blame] | 106 | { |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 107 | .version = POLICYDB_VERSION_RANGETRANS, |
| 108 | .sym_num = SYM_NUM, |
| 109 | .ocon_num = OCON_NUM, |
Darrel Goeddel | f3f8771 | 2006-09-25 23:31:59 -0700 | [diff] [blame] | 110 | }, |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 111 | { |
| 112 | .version = POLICYDB_VERSION_POLCAP, |
| 113 | .sym_num = SYM_NUM, |
| 114 | .ocon_num = OCON_NUM, |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 115 | }, |
| 116 | { |
| 117 | .version = POLICYDB_VERSION_PERMISSIVE, |
| 118 | .sym_num = SYM_NUM, |
| 119 | .ocon_num = OCON_NUM, |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 120 | }, |
| 121 | { |
| 122 | .version = POLICYDB_VERSION_BOUNDARY, |
| 123 | .sym_num = SYM_NUM, |
| 124 | .ocon_num = OCON_NUM, |
| 125 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static struct policydb_compat_info *policydb_lookup_compat(int version) |
| 129 | { |
| 130 | int i; |
| 131 | struct policydb_compat_info *info = NULL; |
| 132 | |
Tobias Klauser | 32725ad | 2006-01-06 00:11:23 -0800 | [diff] [blame] | 133 | for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (policydb_compat[i].version == version) { |
| 135 | info = &policydb_compat[i]; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | return info; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Initialize the role table. |
| 144 | */ |
| 145 | static int roles_init(struct policydb *p) |
| 146 | { |
| 147 | char *key = NULL; |
| 148 | int rc; |
| 149 | struct role_datum *role; |
| 150 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 151 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 152 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 153 | if (!role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | goto out; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 155 | |
| 156 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | role->value = ++p->p_roles.nprim; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 158 | if (role->value != OBJECT_R_VAL) |
| 159 | goto out; |
| 160 | |
| 161 | rc = -ENOMEM; |
Julia Lawall | b3139bb | 2010-05-14 21:30:30 +0200 | [diff] [blame] | 162 | key = kstrdup(OBJECT_R, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 163 | if (!key) |
| 164 | goto out; |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | rc = hashtab_insert(p->p_roles.table, key, role); |
| 167 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 168 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 170 | return 0; |
| 171 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | kfree(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | kfree(role); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 174 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Eric Paris | 4419aae | 2010-10-13 17:50:14 -0400 | [diff] [blame] | 187 | int v; |
| 188 | |
| 189 | v = key1->source_type - key2->source_type; |
| 190 | if (v) |
| 191 | return v; |
| 192 | |
| 193 | v = key1->target_type - key2->target_type; |
| 194 | if (v) |
| 195 | return v; |
| 196 | |
| 197 | v = key1->target_class - key2->target_class; |
| 198 | |
| 199 | return v; |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Initialize a policy database structure. |
| 204 | */ |
| 205 | static int policydb_init(struct policydb *p) |
| 206 | { |
| 207 | int i, rc; |
| 208 | |
| 209 | memset(p, 0, sizeof(*p)); |
| 210 | |
| 211 | for (i = 0; i < SYM_NUM; i++) { |
| 212 | rc = symtab_init(&p->symtab[i], symtab_sizes[i]); |
| 213 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 214 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | rc = avtab_init(&p->te_avtab); |
| 218 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 219 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | rc = roles_init(p); |
| 222 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 223 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | rc = cond_policydb_init(p); |
| 226 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 227 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 229 | p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256); |
| 230 | if (!p->range_tr) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 231 | goto out; |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 232 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 233 | ebitmap_init(&p->policycaps); |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 234 | ebitmap_init(&p->permissive_map); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 235 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 236 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | for (i = 0; i < SYM_NUM; i++) |
| 239 | hashtab_destroy(p->symtab[i].table); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 240 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * The following *_index functions are used to |
| 245 | * define the val_to_name and val_to_struct arrays |
| 246 | * in a policy database structure. The val_to_name |
| 247 | * arrays are used when converting security context |
| 248 | * structures into string representations. The |
| 249 | * val_to_struct arrays are used when the attributes |
| 250 | * of a class, role, or user are needed. |
| 251 | */ |
| 252 | |
| 253 | static int common_index(void *key, void *datum, void *datap) |
| 254 | { |
| 255 | struct policydb *p; |
| 256 | struct common_datum *comdatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 257 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | comdatum = datum; |
| 260 | p = datap; |
| 261 | if (!comdatum->value || comdatum->value > p->p_commons.nprim) |
| 262 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 263 | |
| 264 | fa = p->sym_val_to_name[SYM_COMMONS]; |
| 265 | if (flex_array_put_ptr(fa, comdatum->value - 1, key, |
| 266 | GFP_KERNEL | __GFP_ZERO)) |
| 267 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int class_index(void *key, void *datum, void *datap) |
| 272 | { |
| 273 | struct policydb *p; |
| 274 | struct class_datum *cladatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 275 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | cladatum = datum; |
| 278 | p = datap; |
| 279 | if (!cladatum->value || cladatum->value > p->p_classes.nprim) |
| 280 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 281 | fa = p->sym_val_to_name[SYM_CLASSES]; |
| 282 | if (flex_array_put_ptr(fa, cladatum->value - 1, key, |
| 283 | GFP_KERNEL | __GFP_ZERO)) |
| 284 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | p->class_val_to_struct[cladatum->value - 1] = cladatum; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int role_index(void *key, void *datum, void *datap) |
| 290 | { |
| 291 | struct policydb *p; |
| 292 | struct role_datum *role; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 293 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | role = datum; |
| 296 | p = datap; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 297 | if (!role->value |
| 298 | || role->value > p->p_roles.nprim |
| 299 | || role->bounds > p->p_roles.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 301 | |
| 302 | fa = p->sym_val_to_name[SYM_ROLES]; |
| 303 | if (flex_array_put_ptr(fa, role->value - 1, key, |
| 304 | GFP_KERNEL | __GFP_ZERO)) |
| 305 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | p->role_val_to_struct[role->value - 1] = role; |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int type_index(void *key, void *datum, void *datap) |
| 311 | { |
| 312 | struct policydb *p; |
| 313 | struct type_datum *typdatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 314 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | typdatum = datum; |
| 317 | p = datap; |
| 318 | |
| 319 | if (typdatum->primary) { |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 320 | if (!typdatum->value |
| 321 | || typdatum->value > p->p_types.nprim |
| 322 | || typdatum->bounds > p->p_types.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 324 | fa = p->sym_val_to_name[SYM_TYPES]; |
| 325 | if (flex_array_put_ptr(fa, typdatum->value - 1, key, |
| 326 | GFP_KERNEL | __GFP_ZERO)) |
| 327 | BUG(); |
| 328 | |
| 329 | fa = p->type_val_to_struct_array; |
| 330 | if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum, |
Eric Paris | 23bdecb | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 331 | GFP_KERNEL | __GFP_ZERO)) |
| 332 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int user_index(void *key, void *datum, void *datap) |
| 339 | { |
| 340 | struct policydb *p; |
| 341 | struct user_datum *usrdatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 342 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | usrdatum = datum; |
| 345 | p = datap; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 346 | if (!usrdatum->value |
| 347 | || usrdatum->value > p->p_users.nprim |
| 348 | || usrdatum->bounds > p->p_users.nprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 350 | |
| 351 | fa = p->sym_val_to_name[SYM_USERS]; |
| 352 | if (flex_array_put_ptr(fa, usrdatum->value - 1, key, |
| 353 | GFP_KERNEL | __GFP_ZERO)) |
| 354 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | p->user_val_to_struct[usrdatum->value - 1] = usrdatum; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int sens_index(void *key, void *datum, void *datap) |
| 360 | { |
| 361 | struct policydb *p; |
| 362 | struct level_datum *levdatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 363 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | levdatum = datum; |
| 366 | p = datap; |
| 367 | |
| 368 | if (!levdatum->isalias) { |
| 369 | if (!levdatum->level->sens || |
| 370 | levdatum->level->sens > p->p_levels.nprim) |
| 371 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 372 | fa = p->sym_val_to_name[SYM_LEVELS]; |
| 373 | if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key, |
| 374 | GFP_KERNEL | __GFP_ZERO)) |
| 375 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int cat_index(void *key, void *datum, void *datap) |
| 382 | { |
| 383 | struct policydb *p; |
| 384 | struct cat_datum *catdatum; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 385 | struct flex_array *fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | catdatum = datum; |
| 388 | p = datap; |
| 389 | |
| 390 | if (!catdatum->isalias) { |
| 391 | if (!catdatum->value || catdatum->value > p->p_cats.nprim) |
| 392 | return -EINVAL; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 393 | fa = p->sym_val_to_name[SYM_CATS]; |
| 394 | if (flex_array_put_ptr(fa, catdatum->value - 1, key, |
| 395 | GFP_KERNEL | __GFP_ZERO)) |
| 396 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) = |
| 403 | { |
| 404 | common_index, |
| 405 | class_index, |
| 406 | role_index, |
| 407 | type_index, |
| 408 | user_index, |
| 409 | cond_index_bool, |
| 410 | sens_index, |
| 411 | cat_index, |
| 412 | }; |
| 413 | |
| 414 | /* |
| 415 | * Define the common val_to_name array and the class |
| 416 | * val_to_name and val_to_struct arrays in a policy |
| 417 | * database structure. |
| 418 | * |
| 419 | * Caller must clean up upon failure. |
| 420 | */ |
| 421 | static int policydb_index_classes(struct policydb *p) |
| 422 | { |
| 423 | int rc; |
| 424 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 425 | rc = -ENOMEM; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 426 | p->sym_val_to_name[SYM_COMMONS] = flex_array_alloc(sizeof(char *), |
| 427 | p->p_commons.nprim, |
| 428 | GFP_KERNEL | __GFP_ZERO); |
| 429 | if (!p->sym_val_to_name[SYM_COMMONS]) |
| 430 | goto out; |
| 431 | |
| 432 | rc = flex_array_prealloc(p->sym_val_to_name[SYM_COMMONS], |
| 433 | 0, p->p_commons.nprim - 1, |
| 434 | GFP_KERNEL | __GFP_ZERO); |
| 435 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | rc = hashtab_map(p->p_commons.table, common_index, p); |
| 439 | if (rc) |
| 440 | goto out; |
| 441 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 442 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | p->class_val_to_struct = |
| 444 | kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 445 | if (!p->class_val_to_struct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 448 | rc = -ENOMEM; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 449 | p->sym_val_to_name[SYM_CLASSES] = flex_array_alloc(sizeof(char *), |
| 450 | p->p_classes.nprim, |
| 451 | GFP_KERNEL | __GFP_ZERO); |
| 452 | if (!p->sym_val_to_name[SYM_CLASSES]) |
| 453 | goto out; |
| 454 | |
| 455 | rc = flex_array_prealloc(p->sym_val_to_name[SYM_CLASSES], |
| 456 | 0, p->p_classes.nprim - 1, |
| 457 | GFP_KERNEL | __GFP_ZERO); |
| 458 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | rc = hashtab_map(p->p_classes.table, class_index, p); |
| 462 | out: |
| 463 | return rc; |
| 464 | } |
| 465 | |
| 466 | #ifdef DEBUG_HASHES |
| 467 | static void symtab_hash_eval(struct symtab *s) |
| 468 | { |
| 469 | int i; |
| 470 | |
| 471 | for (i = 0; i < SYM_NUM; i++) { |
| 472 | struct hashtab *h = s[i].table; |
| 473 | struct hashtab_info info; |
| 474 | |
| 475 | hashtab_stat(h, &info); |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 476 | printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | "longest chain length %d\n", symtab_name[i], h->nel, |
| 478 | info.slots_used, h->size, info.max_chain_len); |
| 479 | } |
| 480 | } |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 481 | |
| 482 | static void rangetr_hash_eval(struct hashtab *h) |
| 483 | { |
| 484 | struct hashtab_info info; |
| 485 | |
| 486 | hashtab_stat(h, &info); |
| 487 | printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, " |
| 488 | "longest chain length %d\n", h->nel, |
| 489 | info.slots_used, h->size, info.max_chain_len); |
| 490 | } |
| 491 | #else |
| 492 | static inline void rangetr_hash_eval(struct hashtab *h) |
| 493 | { |
| 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | #endif |
| 496 | |
| 497 | /* |
| 498 | * Define the other val_to_name and val_to_struct arrays |
| 499 | * in a policy database structure. |
| 500 | * |
| 501 | * Caller must clean up on failure. |
| 502 | */ |
| 503 | static int policydb_index_others(struct policydb *p) |
| 504 | { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 505 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 507 | printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | 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] | 509 | if (p->mls_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | printk(", %d sens, %d cats", p->p_levels.nprim, |
| 511 | p->p_cats.nprim); |
| 512 | printk("\n"); |
| 513 | |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 514 | printk(KERN_DEBUG "SELinux: %d classes, %d rules\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | p->p_classes.nprim, p->te_avtab.nel); |
| 516 | |
| 517 | #ifdef DEBUG_HASHES |
| 518 | avtab_hash_eval(&p->te_avtab, "rules"); |
| 519 | symtab_hash_eval(p->symtab); |
| 520 | #endif |
| 521 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 522 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | p->role_val_to_struct = |
| 524 | kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)), |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 525 | GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 526 | if (!p->role_val_to_struct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 529 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | p->user_val_to_struct = |
| 531 | kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)), |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 532 | GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 533 | if (!p->user_val_to_struct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Eric Paris | 23bdecb | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 536 | /* Yes, I want the sizeof the pointer, not the structure */ |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 537 | rc = -ENOMEM; |
Eric Paris | 23bdecb | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 538 | p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *), |
| 539 | p->p_types.nprim, |
| 540 | GFP_KERNEL | __GFP_ZERO); |
| 541 | if (!p->type_val_to_struct_array) |
| 542 | goto out; |
| 543 | |
| 544 | rc = flex_array_prealloc(p->type_val_to_struct_array, 0, |
| 545 | p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO); |
| 546 | if (rc) |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 547 | goto out; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 548 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 549 | rc = -ENOMEM; |
| 550 | if (cond_init_bool_indexes(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | for (i = SYM_ROLES; i < SYM_NUM; i++) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 554 | rc = -ENOMEM; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 555 | p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *), |
| 556 | p->symtab[i].nprim, |
| 557 | GFP_KERNEL | __GFP_ZERO); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 558 | if (!p->sym_val_to_name[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | goto out; |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 560 | |
| 561 | rc = flex_array_prealloc(p->sym_val_to_name[i], |
| 562 | 0, p->symtab[i].nprim - 1, |
| 563 | GFP_KERNEL | __GFP_ZERO); |
| 564 | if (rc) |
| 565 | goto out; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | rc = hashtab_map(p->symtab[i].table, index_f[i], p); |
| 568 | if (rc) |
| 569 | goto out; |
| 570 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 571 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | out: |
| 573 | return rc; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * The following *_destroy functions are used to |
| 578 | * free any memory allocated for each kind of |
| 579 | * symbol data in the policy database. |
| 580 | */ |
| 581 | |
| 582 | static int perm_destroy(void *key, void *datum, void *p) |
| 583 | { |
| 584 | kfree(key); |
| 585 | kfree(datum); |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int common_destroy(void *key, void *datum, void *p) |
| 590 | { |
| 591 | struct common_datum *comdatum; |
| 592 | |
| 593 | kfree(key); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 594 | if (datum) { |
| 595 | comdatum = datum; |
| 596 | hashtab_map(comdatum->permissions.table, perm_destroy, NULL); |
| 597 | hashtab_destroy(comdatum->permissions.table); |
| 598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | kfree(datum); |
| 600 | return 0; |
| 601 | } |
| 602 | |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 603 | static int cls_destroy(void *key, void *datum, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
| 605 | struct class_datum *cladatum; |
| 606 | struct constraint_node *constraint, *ctemp; |
| 607 | struct constraint_expr *e, *etmp; |
| 608 | |
| 609 | kfree(key); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 610 | if (datum) { |
| 611 | cladatum = datum; |
| 612 | hashtab_map(cladatum->permissions.table, perm_destroy, NULL); |
| 613 | hashtab_destroy(cladatum->permissions.table); |
| 614 | constraint = cladatum->constraints; |
| 615 | while (constraint) { |
| 616 | e = constraint->expr; |
| 617 | while (e) { |
| 618 | ebitmap_destroy(&e->names); |
| 619 | etmp = e; |
| 620 | e = e->next; |
| 621 | kfree(etmp); |
| 622 | } |
| 623 | ctemp = constraint; |
| 624 | constraint = constraint->next; |
| 625 | kfree(ctemp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 628 | constraint = cladatum->validatetrans; |
| 629 | while (constraint) { |
| 630 | e = constraint->expr; |
| 631 | while (e) { |
| 632 | ebitmap_destroy(&e->names); |
| 633 | etmp = e; |
| 634 | e = e->next; |
| 635 | kfree(etmp); |
| 636 | } |
| 637 | ctemp = constraint; |
| 638 | constraint = constraint->next; |
| 639 | kfree(ctemp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 642 | kfree(cladatum->comkey); |
| 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | kfree(datum); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int role_destroy(void *key, void *datum, void *p) |
| 649 | { |
| 650 | struct role_datum *role; |
| 651 | |
| 652 | kfree(key); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 653 | if (datum) { |
| 654 | role = datum; |
| 655 | ebitmap_destroy(&role->dominates); |
| 656 | ebitmap_destroy(&role->types); |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | kfree(datum); |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static int type_destroy(void *key, void *datum, void *p) |
| 663 | { |
| 664 | kfree(key); |
| 665 | kfree(datum); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int user_destroy(void *key, void *datum, void *p) |
| 670 | { |
| 671 | struct user_datum *usrdatum; |
| 672 | |
| 673 | kfree(key); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 674 | if (datum) { |
| 675 | usrdatum = datum; |
| 676 | ebitmap_destroy(&usrdatum->roles); |
| 677 | ebitmap_destroy(&usrdatum->range.level[0].cat); |
| 678 | ebitmap_destroy(&usrdatum->range.level[1].cat); |
| 679 | ebitmap_destroy(&usrdatum->dfltlevel.cat); |
| 680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | kfree(datum); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static int sens_destroy(void *key, void *datum, void *p) |
| 686 | { |
| 687 | struct level_datum *levdatum; |
| 688 | |
| 689 | kfree(key); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 690 | if (datum) { |
| 691 | levdatum = datum; |
| 692 | ebitmap_destroy(&levdatum->level->cat); |
| 693 | kfree(levdatum->level); |
| 694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | kfree(datum); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int cat_destroy(void *key, void *datum, void *p) |
| 700 | { |
| 701 | kfree(key); |
| 702 | kfree(datum); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) = |
| 707 | { |
| 708 | common_destroy, |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 709 | cls_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | role_destroy, |
| 711 | type_destroy, |
| 712 | user_destroy, |
| 713 | cond_destroy_bool, |
| 714 | sens_destroy, |
| 715 | cat_destroy, |
| 716 | }; |
| 717 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 718 | static int range_tr_destroy(void *key, void *datum, void *p) |
| 719 | { |
| 720 | struct mls_range *rt = datum; |
| 721 | kfree(key); |
| 722 | ebitmap_destroy(&rt->level[0].cat); |
| 723 | ebitmap_destroy(&rt->level[1].cat); |
| 724 | kfree(datum); |
| 725 | cond_resched(); |
| 726 | return 0; |
| 727 | } |
| 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | static void ocontext_destroy(struct ocontext *c, int i) |
| 730 | { |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 731 | if (!c) |
| 732 | return; |
| 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | context_destroy(&c->context[0]); |
| 735 | context_destroy(&c->context[1]); |
| 736 | if (i == OCON_ISID || i == OCON_FS || |
| 737 | i == OCON_NETIF || i == OCON_FSUSE) |
| 738 | kfree(c->u.name); |
| 739 | kfree(c); |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Free any memory allocated by a policy database structure. |
| 744 | */ |
| 745 | void policydb_destroy(struct policydb *p) |
| 746 | { |
| 747 | struct ocontext *c, *ctmp; |
| 748 | struct genfs *g, *gtmp; |
| 749 | int i; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 750 | struct role_allow *ra, *lra = NULL; |
| 751 | struct role_trans *tr, *ltr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | for (i = 0; i < SYM_NUM; i++) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 754 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | hashtab_map(p->symtab[i].table, destroy_f[i], NULL); |
| 756 | hashtab_destroy(p->symtab[i].table); |
| 757 | } |
| 758 | |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 759 | for (i = 0; i < SYM_NUM; i++) { |
| 760 | if (p->sym_val_to_name[i]) |
| 761 | flex_array_free(p->sym_val_to_name[i]); |
| 762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 764 | kfree(p->class_val_to_struct); |
| 765 | kfree(p->role_val_to_struct); |
| 766 | kfree(p->user_val_to_struct); |
Eric Paris | 23bdecb | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 767 | if (p->type_val_to_struct_array) |
| 768 | flex_array_free(p->type_val_to_struct_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | avtab_destroy(&p->te_avtab); |
| 771 | |
| 772 | for (i = 0; i < OCON_NUM; i++) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 773 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | c = p->ocontexts[i]; |
| 775 | while (c) { |
| 776 | ctmp = c; |
| 777 | c = c->next; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 778 | ocontext_destroy(ctmp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
Chad Sellers | 6e8c751 | 2006-10-06 16:09:52 -0400 | [diff] [blame] | 780 | p->ocontexts[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | g = p->genfs; |
| 784 | while (g) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 785 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | kfree(g->fstype); |
| 787 | c = g->head; |
| 788 | while (c) { |
| 789 | ctmp = c; |
| 790 | c = c->next; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 791 | ocontext_destroy(ctmp, OCON_FSUSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | gtmp = g; |
| 794 | g = g->next; |
| 795 | kfree(gtmp); |
| 796 | } |
Chad Sellers | 6e8c751 | 2006-10-06 16:09:52 -0400 | [diff] [blame] | 797 | p->genfs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | cond_policydb_destroy(p); |
| 800 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 801 | for (tr = p->role_tr; tr; tr = tr->next) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 802 | cond_resched(); |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 803 | kfree(ltr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 804 | ltr = tr; |
| 805 | } |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 806 | kfree(ltr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 807 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 808 | for (ra = p->role_allow; ra; ra = ra->next) { |
Eric Paris | 9dc9978 | 2007-06-04 17:41:22 -0400 | [diff] [blame] | 809 | cond_resched(); |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 810 | kfree(lra); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 811 | lra = ra; |
| 812 | } |
Jesper Juhl | a7f988b | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 813 | kfree(lra); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 814 | |
Stephen Smalley | 2f3e82d | 2010-01-07 15:55:16 -0500 | [diff] [blame] | 815 | hashtab_map(p->range_tr, range_tr_destroy, NULL); |
| 816 | hashtab_destroy(p->range_tr); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 817 | |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 818 | if (p->type_attr_map_array) { |
| 819 | for (i = 0; i < p->p_types.nprim; i++) { |
| 820 | struct ebitmap *e; |
| 821 | |
| 822 | e = flex_array_get(p->type_attr_map_array, i); |
| 823 | if (!e) |
| 824 | continue; |
| 825 | ebitmap_destroy(e); |
| 826 | } |
| 827 | flex_array_free(p->type_attr_map_array); |
Stephen Smalley | 282c1f5 | 2005-10-23 12:57:15 -0700 | [diff] [blame] | 828 | } |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 829 | ebitmap_destroy(&p->policycaps); |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 830 | ebitmap_destroy(&p->permissive_map); |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Load the initial SIDs specified in a policy database |
| 837 | * structure into a SID table. |
| 838 | */ |
| 839 | int policydb_load_isids(struct policydb *p, struct sidtab *s) |
| 840 | { |
| 841 | struct ocontext *head, *c; |
| 842 | int rc; |
| 843 | |
| 844 | rc = sidtab_init(s); |
| 845 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 846 | printk(KERN_ERR "SELinux: out of memory on SID table init\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | goto out; |
| 848 | } |
| 849 | |
| 850 | head = p->ocontexts[OCON_ISID]; |
| 851 | for (c = head; c; c = c->next) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 852 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (!c->context[0].user) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 854 | printk(KERN_ERR "SELinux: SID %s was never defined.\n", |
| 855 | c->u.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | goto out; |
| 857 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 858 | |
| 859 | rc = sidtab_insert(s, c->sid[0], &c->context[0]); |
| 860 | if (rc) { |
| 861 | printk(KERN_ERR "SELinux: unable to load initial SID %s.\n", |
| 862 | c->u.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | goto out; |
| 864 | } |
| 865 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 866 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | out: |
| 868 | return rc; |
| 869 | } |
| 870 | |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 871 | int policydb_class_isvalid(struct policydb *p, unsigned int class) |
| 872 | { |
| 873 | if (!class || class > p->p_classes.nprim) |
| 874 | return 0; |
| 875 | return 1; |
| 876 | } |
| 877 | |
| 878 | int policydb_role_isvalid(struct policydb *p, unsigned int role) |
| 879 | { |
| 880 | if (!role || role > p->p_roles.nprim) |
| 881 | return 0; |
| 882 | return 1; |
| 883 | } |
| 884 | |
| 885 | int policydb_type_isvalid(struct policydb *p, unsigned int type) |
| 886 | { |
| 887 | if (!type || type > p->p_types.nprim) |
| 888 | return 0; |
| 889 | return 1; |
| 890 | } |
| 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* |
| 893 | * Return 1 if the fields in the security context |
| 894 | * structure `c' are valid. Return 0 otherwise. |
| 895 | */ |
| 896 | int policydb_context_isvalid(struct policydb *p, struct context *c) |
| 897 | { |
| 898 | struct role_datum *role; |
| 899 | struct user_datum *usrdatum; |
| 900 | |
| 901 | if (!c->role || c->role > p->p_roles.nprim) |
| 902 | return 0; |
| 903 | |
| 904 | if (!c->user || c->user > p->p_users.nprim) |
| 905 | return 0; |
| 906 | |
| 907 | if (!c->type || c->type > p->p_types.nprim) |
| 908 | return 0; |
| 909 | |
| 910 | if (c->role != OBJECT_R_VAL) { |
| 911 | /* |
| 912 | * Role must be authorized for the type. |
| 913 | */ |
| 914 | role = p->role_val_to_struct[c->role - 1]; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 915 | if (!ebitmap_get_bit(&role->types, c->type - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | /* role may not be associated with type */ |
| 917 | return 0; |
| 918 | |
| 919 | /* |
| 920 | * User must be authorized for the role. |
| 921 | */ |
| 922 | usrdatum = p->user_val_to_struct[c->user - 1]; |
| 923 | if (!usrdatum) |
| 924 | return 0; |
| 925 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 926 | if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | /* user may not be associated with role */ |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | if (!mls_context_isvalid(p, c)) |
| 932 | return 0; |
| 933 | |
| 934 | return 1; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Read a MLS range structure from a policydb binary |
| 939 | * representation file. |
| 940 | */ |
| 941 | static int mls_read_range_helper(struct mls_range *r, void *fp) |
| 942 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 943 | __le32 buf[2]; |
| 944 | u32 items; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | int rc; |
| 946 | |
| 947 | rc = next_entry(buf, fp, sizeof(u32)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 948 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | goto out; |
| 950 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 951 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | items = le32_to_cpu(buf[0]); |
| 953 | if (items > ARRAY_SIZE(buf)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 954 | printk(KERN_ERR "SELinux: mls: range overflow\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | goto out; |
| 956 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | rc = next_entry(buf, fp, sizeof(u32) * items); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 959 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 960 | printk(KERN_ERR "SELinux: mls: truncated range\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | goto out; |
| 962 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | r->level[0].sens = le32_to_cpu(buf[0]); |
| 965 | if (items > 1) |
| 966 | r->level[1].sens = le32_to_cpu(buf[1]); |
| 967 | else |
| 968 | r->level[1].sens = r->level[0].sens; |
| 969 | |
| 970 | rc = ebitmap_read(&r->level[0].cat, fp); |
| 971 | if (rc) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 972 | printk(KERN_ERR "SELinux: mls: error reading low categories\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | goto out; |
| 974 | } |
| 975 | if (items > 1) { |
| 976 | rc = ebitmap_read(&r->level[1].cat, fp); |
| 977 | if (rc) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 978 | printk(KERN_ERR "SELinux: mls: error reading high categories\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | goto bad_high; |
| 980 | } |
| 981 | } else { |
| 982 | rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat); |
| 983 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 984 | printk(KERN_ERR "SELinux: mls: out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | goto bad_high; |
| 986 | } |
| 987 | } |
| 988 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 989 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | bad_high: |
| 991 | ebitmap_destroy(&r->level[0].cat); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 992 | out: |
| 993 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Read and validate a security context structure |
| 998 | * from a policydb binary representation file. |
| 999 | */ |
| 1000 | static int context_read_and_validate(struct context *c, |
| 1001 | struct policydb *p, |
| 1002 | void *fp) |
| 1003 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1004 | __le32 buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | int rc; |
| 1006 | |
| 1007 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1008 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1009 | printk(KERN_ERR "SELinux: context truncated\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | goto out; |
| 1011 | } |
| 1012 | c->user = le32_to_cpu(buf[0]); |
| 1013 | c->role = le32_to_cpu(buf[1]); |
| 1014 | c->type = le32_to_cpu(buf[2]); |
| 1015 | if (p->policyvers >= POLICYDB_VERSION_MLS) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1016 | rc = mls_read_range_helper(&c->range, fp); |
| 1017 | if (rc) { |
| 1018 | printk(KERN_ERR "SELinux: error reading MLS range of context\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | goto out; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1023 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | if (!policydb_context_isvalid(p, c)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1025 | printk(KERN_ERR "SELinux: invalid security context\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | context_destroy(c); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1027 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1029 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | out: |
| 1031 | return rc; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * The following *_read functions are used to |
| 1036 | * read the symbol data from a policy database |
| 1037 | * binary representation file. |
| 1038 | */ |
| 1039 | |
| 1040 | static int perm_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1041 | { |
| 1042 | char *key = NULL; |
| 1043 | struct perm_datum *perdatum; |
| 1044 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1045 | __le32 buf[2]; |
| 1046 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1048 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1049 | perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1050 | if (!perdatum) |
| 1051 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1054 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | goto bad; |
| 1056 | |
| 1057 | len = le32_to_cpu(buf[0]); |
| 1058 | perdatum->value = le32_to_cpu(buf[1]); |
| 1059 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1060 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1061 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1062 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1066 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1068 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | rc = hashtab_insert(h, key, perdatum); |
| 1071 | if (rc) |
| 1072 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1073 | |
| 1074 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | bad: |
| 1076 | perm_destroy(key, perdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1077 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static int common_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1081 | { |
| 1082 | char *key = NULL; |
| 1083 | struct common_datum *comdatum; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1084 | __le32 buf[4]; |
| 1085 | u32 len, nel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | int i, rc; |
| 1087 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1088 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1089 | comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1090 | if (!comdatum) |
| 1091 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
| 1093 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1094 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | goto bad; |
| 1096 | |
| 1097 | len = le32_to_cpu(buf[0]); |
| 1098 | comdatum->value = le32_to_cpu(buf[1]); |
| 1099 | |
| 1100 | rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE); |
| 1101 | if (rc) |
| 1102 | goto bad; |
| 1103 | comdatum->permissions.nprim = le32_to_cpu(buf[2]); |
| 1104 | nel = le32_to_cpu(buf[3]); |
| 1105 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1106 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1107 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1108 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1112 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1114 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | for (i = 0; i < nel; i++) { |
| 1117 | rc = perm_read(p, comdatum->permissions.table, fp); |
| 1118 | if (rc) |
| 1119 | goto bad; |
| 1120 | } |
| 1121 | |
| 1122 | rc = hashtab_insert(h, key, comdatum); |
| 1123 | if (rc) |
| 1124 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1125 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | bad: |
| 1127 | common_destroy(key, comdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1128 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int read_cons_helper(struct constraint_node **nodep, int ncons, |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1132 | int allowxtarget, void *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | { |
| 1134 | struct constraint_node *c, *lc; |
| 1135 | struct constraint_expr *e, *le; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1136 | __le32 buf[3]; |
| 1137 | u32 nexpr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | int rc, i, j, depth; |
| 1139 | |
| 1140 | lc = NULL; |
| 1141 | for (i = 0; i < ncons; i++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1142 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | if (!c) |
| 1144 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1146 | if (lc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | lc->next = c; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1148 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | *nodep = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1152 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | return rc; |
| 1154 | c->permissions = le32_to_cpu(buf[0]); |
| 1155 | nexpr = le32_to_cpu(buf[1]); |
| 1156 | le = NULL; |
| 1157 | depth = -1; |
| 1158 | for (j = 0; j < nexpr; j++) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1159 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | if (!e) |
| 1161 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1163 | if (le) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | le->next = e; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1165 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | c->expr = e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
| 1168 | rc = next_entry(buf, fp, (sizeof(u32) * 3)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1169 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | return rc; |
| 1171 | e->expr_type = le32_to_cpu(buf[0]); |
| 1172 | e->attr = le32_to_cpu(buf[1]); |
| 1173 | e->op = le32_to_cpu(buf[2]); |
| 1174 | |
| 1175 | switch (e->expr_type) { |
| 1176 | case CEXPR_NOT: |
| 1177 | if (depth < 0) |
| 1178 | return -EINVAL; |
| 1179 | break; |
| 1180 | case CEXPR_AND: |
| 1181 | case CEXPR_OR: |
| 1182 | if (depth < 1) |
| 1183 | return -EINVAL; |
| 1184 | depth--; |
| 1185 | break; |
| 1186 | case CEXPR_ATTR: |
| 1187 | if (depth == (CEXPR_MAXDEPTH - 1)) |
| 1188 | return -EINVAL; |
| 1189 | depth++; |
| 1190 | break; |
| 1191 | case CEXPR_NAMES: |
| 1192 | if (!allowxtarget && (e->attr & CEXPR_XTARGET)) |
| 1193 | return -EINVAL; |
| 1194 | if (depth == (CEXPR_MAXDEPTH - 1)) |
| 1195 | return -EINVAL; |
| 1196 | depth++; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1197 | rc = ebitmap_read(&e->names, fp); |
| 1198 | if (rc) |
| 1199 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | break; |
| 1201 | default: |
| 1202 | return -EINVAL; |
| 1203 | } |
| 1204 | le = e; |
| 1205 | } |
| 1206 | if (depth != 0) |
| 1207 | return -EINVAL; |
| 1208 | lc = c; |
| 1209 | } |
| 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | static int class_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1215 | { |
| 1216 | char *key = NULL; |
| 1217 | struct class_datum *cladatum; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1218 | __le32 buf[6]; |
| 1219 | u32 len, len2, ncons, nel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | int i, rc; |
| 1221 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1222 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1223 | cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1224 | if (!cladatum) |
| 1225 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
| 1227 | rc = next_entry(buf, fp, sizeof(u32)*6); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1228 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | goto bad; |
| 1230 | |
| 1231 | len = le32_to_cpu(buf[0]); |
| 1232 | len2 = le32_to_cpu(buf[1]); |
| 1233 | cladatum->value = le32_to_cpu(buf[2]); |
| 1234 | |
| 1235 | rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE); |
| 1236 | if (rc) |
| 1237 | goto bad; |
| 1238 | cladatum->permissions.nprim = le32_to_cpu(buf[3]); |
| 1239 | nel = le32_to_cpu(buf[4]); |
| 1240 | |
| 1241 | ncons = le32_to_cpu(buf[5]); |
| 1242 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1243 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1244 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1245 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1249 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1251 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
| 1253 | if (len2) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1254 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1255 | cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1256 | if (!cladatum->comkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | rc = next_entry(cladatum->comkey, fp, len2); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1259 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1261 | cladatum->comkey[len2] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1263 | rc = -EINVAL; |
| 1264 | cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | if (!cladatum->comdatum) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1266 | printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | goto bad; |
| 1268 | } |
| 1269 | } |
| 1270 | for (i = 0; i < nel; i++) { |
| 1271 | rc = perm_read(p, cladatum->permissions.table, fp); |
| 1272 | if (rc) |
| 1273 | goto bad; |
| 1274 | } |
| 1275 | |
| 1276 | rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp); |
| 1277 | if (rc) |
| 1278 | goto bad; |
| 1279 | |
| 1280 | if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) { |
| 1281 | /* grab the validatetrans rules */ |
| 1282 | rc = next_entry(buf, fp, sizeof(u32)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1283 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | goto bad; |
| 1285 | ncons = le32_to_cpu(buf[0]); |
| 1286 | rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp); |
| 1287 | if (rc) |
| 1288 | goto bad; |
| 1289 | } |
| 1290 | |
| 1291 | rc = hashtab_insert(h, key, cladatum); |
| 1292 | if (rc) |
| 1293 | goto bad; |
| 1294 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1295 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | bad: |
James Morris | 6cbda6b | 2006-11-29 16:50:27 -0500 | [diff] [blame] | 1297 | cls_destroy(key, cladatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1298 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | static int role_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1302 | { |
| 1303 | char *key = NULL; |
| 1304 | struct role_datum *role; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1305 | int rc, to_read = 2; |
| 1306 | __le32 buf[3]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1307 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1309 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1310 | role = kzalloc(sizeof(*role), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1311 | if (!role) |
| 1312 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1314 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1315 | to_read = 3; |
| 1316 | |
| 1317 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1318 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | goto bad; |
| 1320 | |
| 1321 | len = le32_to_cpu(buf[0]); |
| 1322 | role->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1323 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1324 | role->bounds = le32_to_cpu(buf[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1326 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1327 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1328 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1332 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1334 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | rc = ebitmap_read(&role->dominates, fp); |
| 1337 | if (rc) |
| 1338 | goto bad; |
| 1339 | |
| 1340 | rc = ebitmap_read(&role->types, fp); |
| 1341 | if (rc) |
| 1342 | goto bad; |
| 1343 | |
| 1344 | if (strcmp(key, OBJECT_R) == 0) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1345 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (role->value != OBJECT_R_VAL) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1347 | printk(KERN_ERR "SELinux: Role %s has wrong value %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | OBJECT_R, role->value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | goto bad; |
| 1350 | } |
| 1351 | rc = 0; |
| 1352 | goto bad; |
| 1353 | } |
| 1354 | |
| 1355 | rc = hashtab_insert(h, key, role); |
| 1356 | if (rc) |
| 1357 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1358 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | bad: |
| 1360 | role_destroy(key, role, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1361 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static int type_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1365 | { |
| 1366 | char *key = NULL; |
| 1367 | struct type_datum *typdatum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1368 | int rc, to_read = 3; |
| 1369 | __le32 buf[4]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1370 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1372 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1373 | typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1374 | if (!typdatum) |
| 1375 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1377 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1378 | to_read = 4; |
| 1379 | |
| 1380 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1381 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | goto bad; |
| 1383 | |
| 1384 | len = le32_to_cpu(buf[0]); |
| 1385 | typdatum->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1386 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { |
| 1387 | u32 prop = le32_to_cpu(buf[2]); |
| 1388 | |
| 1389 | if (prop & TYPEDATUM_PROPERTY_PRIMARY) |
| 1390 | typdatum->primary = 1; |
| 1391 | if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE) |
| 1392 | typdatum->attribute = 1; |
| 1393 | |
| 1394 | typdatum->bounds = le32_to_cpu(buf[3]); |
| 1395 | } else { |
| 1396 | typdatum->primary = le32_to_cpu(buf[2]); |
| 1397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1399 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1400 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1401 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1404 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1406 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
| 1408 | rc = hashtab_insert(h, key, typdatum); |
| 1409 | if (rc) |
| 1410 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1411 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | bad: |
| 1413 | type_destroy(key, typdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1414 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | /* |
| 1419 | * Read a MLS level structure from a policydb binary |
| 1420 | * representation file. |
| 1421 | */ |
| 1422 | static int mls_read_level(struct mls_level *lp, void *fp) |
| 1423 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1424 | __le32 buf[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | int rc; |
| 1426 | |
| 1427 | memset(lp, 0, sizeof(*lp)); |
| 1428 | |
| 1429 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1430 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1431 | printk(KERN_ERR "SELinux: mls: truncated level\n"); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1432 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | lp->sens = le32_to_cpu(buf[0]); |
| 1435 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1436 | rc = ebitmap_read(&lp->cat, fp); |
| 1437 | if (rc) { |
| 1438 | printk(KERN_ERR "SELinux: mls: error reading level categories\n"); |
| 1439 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int user_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1445 | { |
| 1446 | char *key = NULL; |
| 1447 | struct user_datum *usrdatum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1448 | int rc, to_read = 2; |
| 1449 | __le32 buf[3]; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1450 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1452 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1453 | usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1454 | if (!usrdatum) |
| 1455 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1457 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1458 | to_read = 3; |
| 1459 | |
| 1460 | rc = next_entry(buf, fp, sizeof(buf[0]) * to_read); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1461 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | goto bad; |
| 1463 | |
| 1464 | len = le32_to_cpu(buf[0]); |
| 1465 | usrdatum->value = le32_to_cpu(buf[1]); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1466 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 1467 | usrdatum->bounds = le32_to_cpu(buf[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1469 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1470 | key = kmalloc(len + 1, GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1471 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1474 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1476 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
| 1478 | rc = ebitmap_read(&usrdatum->roles, fp); |
| 1479 | if (rc) |
| 1480 | goto bad; |
| 1481 | |
| 1482 | if (p->policyvers >= POLICYDB_VERSION_MLS) { |
| 1483 | rc = mls_read_range_helper(&usrdatum->range, fp); |
| 1484 | if (rc) |
| 1485 | goto bad; |
| 1486 | rc = mls_read_level(&usrdatum->dfltlevel, fp); |
| 1487 | if (rc) |
| 1488 | goto bad; |
| 1489 | } |
| 1490 | |
| 1491 | rc = hashtab_insert(h, key, usrdatum); |
| 1492 | if (rc) |
| 1493 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1494 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | bad: |
| 1496 | user_destroy(key, usrdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1497 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static int sens_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1501 | { |
| 1502 | char *key = NULL; |
| 1503 | struct level_datum *levdatum; |
| 1504 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1505 | __le32 buf[2]; |
| 1506 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1508 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1509 | levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1510 | if (!levdatum) |
| 1511 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1514 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | goto bad; |
| 1516 | |
| 1517 | len = le32_to_cpu(buf[0]); |
| 1518 | levdatum->isalias = le32_to_cpu(buf[1]); |
| 1519 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1520 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1521 | key = kmalloc(len + 1, GFP_ATOMIC); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1522 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1525 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1527 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1529 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1531 | if (!levdatum->level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1533 | |
| 1534 | rc = mls_read_level(levdatum->level, fp); |
| 1535 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | rc = hashtab_insert(h, key, levdatum); |
| 1539 | if (rc) |
| 1540 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1541 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | bad: |
| 1543 | sens_destroy(key, levdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1544 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | static int cat_read(struct policydb *p, struct hashtab *h, void *fp) |
| 1548 | { |
| 1549 | char *key = NULL; |
| 1550 | struct cat_datum *catdatum; |
| 1551 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 1552 | __le32 buf[3]; |
| 1553 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1555 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 1556 | catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1557 | if (!catdatum) |
| 1558 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
| 1560 | rc = next_entry(buf, fp, sizeof buf); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1561 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | goto bad; |
| 1563 | |
| 1564 | len = le32_to_cpu(buf[0]); |
| 1565 | catdatum->value = le32_to_cpu(buf[1]); |
| 1566 | catdatum->isalias = le32_to_cpu(buf[2]); |
| 1567 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1568 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 1569 | key = kmalloc(len + 1, GFP_ATOMIC); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1570 | if (!key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | rc = next_entry(key, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1573 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | goto bad; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 1575 | key[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
| 1577 | rc = hashtab_insert(h, key, catdatum); |
| 1578 | if (rc) |
| 1579 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1580 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | bad: |
| 1582 | cat_destroy(key, catdatum, NULL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 1583 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) = |
| 1587 | { |
| 1588 | common_read, |
| 1589 | class_read, |
| 1590 | role_read, |
| 1591 | type_read, |
| 1592 | user_read, |
| 1593 | cond_read_bool, |
| 1594 | sens_read, |
| 1595 | cat_read, |
| 1596 | }; |
| 1597 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1598 | static int user_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1599 | { |
| 1600 | struct user_datum *upper, *user; |
| 1601 | struct policydb *p = datap; |
| 1602 | int depth = 0; |
| 1603 | |
| 1604 | upper = user = datum; |
| 1605 | while (upper->bounds) { |
| 1606 | struct ebitmap_node *node; |
| 1607 | unsigned long bit; |
| 1608 | |
| 1609 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1610 | printk(KERN_ERR "SELinux: user %s: " |
| 1611 | "too deep or looped boundary", |
| 1612 | (char *) key); |
| 1613 | return -EINVAL; |
| 1614 | } |
| 1615 | |
| 1616 | upper = p->user_val_to_struct[upper->bounds - 1]; |
| 1617 | ebitmap_for_each_positive_bit(&user->roles, node, bit) { |
| 1618 | if (ebitmap_get_bit(&upper->roles, bit)) |
| 1619 | continue; |
| 1620 | |
| 1621 | printk(KERN_ERR |
| 1622 | "SELinux: boundary violated policy: " |
| 1623 | "user=%s role=%s bounds=%s\n", |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 1624 | sym_name(p, SYM_USERS, user->value - 1), |
| 1625 | sym_name(p, SYM_ROLES, bit), |
| 1626 | sym_name(p, SYM_USERS, upper->value - 1)); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1627 | |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static int role_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1636 | { |
| 1637 | struct role_datum *upper, *role; |
| 1638 | struct policydb *p = datap; |
| 1639 | int depth = 0; |
| 1640 | |
| 1641 | upper = role = datum; |
| 1642 | while (upper->bounds) { |
| 1643 | struct ebitmap_node *node; |
| 1644 | unsigned long bit; |
| 1645 | |
| 1646 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1647 | printk(KERN_ERR "SELinux: role %s: " |
| 1648 | "too deep or looped bounds\n", |
| 1649 | (char *) key); |
| 1650 | return -EINVAL; |
| 1651 | } |
| 1652 | |
| 1653 | upper = p->role_val_to_struct[upper->bounds - 1]; |
| 1654 | ebitmap_for_each_positive_bit(&role->types, node, bit) { |
| 1655 | if (ebitmap_get_bit(&upper->types, bit)) |
| 1656 | continue; |
| 1657 | |
| 1658 | printk(KERN_ERR |
| 1659 | "SELinux: boundary violated policy: " |
| 1660 | "role=%s type=%s bounds=%s\n", |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 1661 | sym_name(p, SYM_ROLES, role->value - 1), |
| 1662 | sym_name(p, SYM_TYPES, bit), |
| 1663 | sym_name(p, SYM_ROLES, upper->value - 1)); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1664 | |
| 1665 | return -EINVAL; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | static int type_bounds_sanity_check(void *key, void *datum, void *datap) |
| 1673 | { |
Eric Paris | daa6d83 | 2010-08-03 15:26:05 -0400 | [diff] [blame] | 1674 | struct type_datum *upper; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1675 | struct policydb *p = datap; |
| 1676 | int depth = 0; |
| 1677 | |
Eric Paris | daa6d83 | 2010-08-03 15:26:05 -0400 | [diff] [blame] | 1678 | upper = datum; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1679 | while (upper->bounds) { |
| 1680 | if (++depth == POLICYDB_BOUNDS_MAXDEPTH) { |
| 1681 | printk(KERN_ERR "SELinux: type %s: " |
| 1682 | "too deep or looped boundary\n", |
| 1683 | (char *) key); |
| 1684 | return -EINVAL; |
| 1685 | } |
| 1686 | |
Eric Paris | 23bdecb | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 1687 | upper = flex_array_get_ptr(p->type_val_to_struct_array, |
| 1688 | upper->bounds - 1); |
| 1689 | BUG_ON(!upper); |
| 1690 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1691 | if (upper->attribute) { |
| 1692 | printk(KERN_ERR "SELinux: type %s: " |
| 1693 | "bounded by attribute %s", |
| 1694 | (char *) key, |
Eric Paris | ac76c05 | 2010-11-29 15:47:09 -0500 | [diff] [blame^] | 1695 | sym_name(p, SYM_TYPES, upper->value - 1)); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1696 | return -EINVAL; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | static int policydb_bounds_sanity_check(struct policydb *p) |
| 1704 | { |
| 1705 | int rc; |
| 1706 | |
| 1707 | if (p->policyvers < POLICYDB_VERSION_BOUNDARY) |
| 1708 | return 0; |
| 1709 | |
| 1710 | rc = hashtab_map(p->p_users.table, |
| 1711 | user_bounds_sanity_check, p); |
| 1712 | if (rc) |
| 1713 | return rc; |
| 1714 | |
| 1715 | rc = hashtab_map(p->p_roles.table, |
| 1716 | role_bounds_sanity_check, p); |
| 1717 | if (rc) |
| 1718 | return rc; |
| 1719 | |
| 1720 | rc = hashtab_map(p->p_types.table, |
| 1721 | type_bounds_sanity_check, p); |
| 1722 | if (rc) |
| 1723 | return rc; |
| 1724 | |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | extern int ss_initialized; |
| 1729 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1730 | u16 string_to_security_class(struct policydb *p, const char *name) |
| 1731 | { |
| 1732 | struct class_datum *cladatum; |
| 1733 | |
| 1734 | cladatum = hashtab_search(p->p_classes.table, name); |
| 1735 | if (!cladatum) |
| 1736 | return 0; |
| 1737 | |
| 1738 | return cladatum->value; |
| 1739 | } |
| 1740 | |
| 1741 | u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name) |
| 1742 | { |
| 1743 | struct class_datum *cladatum; |
| 1744 | struct perm_datum *perdatum = NULL; |
| 1745 | struct common_datum *comdatum; |
| 1746 | |
| 1747 | if (!tclass || tclass > p->p_classes.nprim) |
| 1748 | return 0; |
| 1749 | |
| 1750 | cladatum = p->class_val_to_struct[tclass-1]; |
| 1751 | comdatum = cladatum->comdatum; |
| 1752 | if (comdatum) |
| 1753 | perdatum = hashtab_search(comdatum->permissions.table, |
| 1754 | name); |
| 1755 | if (!perdatum) |
| 1756 | perdatum = hashtab_search(cladatum->permissions.table, |
| 1757 | name); |
| 1758 | if (!perdatum) |
| 1759 | return 0; |
| 1760 | |
| 1761 | return 1U << (perdatum->value-1); |
| 1762 | } |
| 1763 | |
Eric Paris | 9ee0c82 | 2010-06-11 12:37:05 -0400 | [diff] [blame] | 1764 | static int range_read(struct policydb *p, void *fp) |
| 1765 | { |
| 1766 | struct range_trans *rt = NULL; |
| 1767 | struct mls_range *r = NULL; |
| 1768 | int i, rc; |
| 1769 | __le32 buf[2]; |
| 1770 | u32 nel; |
| 1771 | |
| 1772 | if (p->policyvers < POLICYDB_VERSION_MLS) |
| 1773 | return 0; |
| 1774 | |
| 1775 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1776 | if (rc) |
| 1777 | goto out; |
| 1778 | |
| 1779 | nel = le32_to_cpu(buf[0]); |
| 1780 | for (i = 0; i < nel; i++) { |
| 1781 | rc = -ENOMEM; |
| 1782 | rt = kzalloc(sizeof(*rt), GFP_KERNEL); |
| 1783 | if (!rt) |
| 1784 | goto out; |
| 1785 | |
| 1786 | rc = next_entry(buf, fp, (sizeof(u32) * 2)); |
| 1787 | if (rc) |
| 1788 | goto out; |
| 1789 | |
| 1790 | rt->source_type = le32_to_cpu(buf[0]); |
| 1791 | rt->target_type = le32_to_cpu(buf[1]); |
| 1792 | if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { |
| 1793 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1794 | if (rc) |
| 1795 | goto out; |
| 1796 | rt->target_class = le32_to_cpu(buf[0]); |
| 1797 | } else |
| 1798 | rt->target_class = p->process_class; |
| 1799 | |
| 1800 | rc = -EINVAL; |
| 1801 | if (!policydb_type_isvalid(p, rt->source_type) || |
| 1802 | !policydb_type_isvalid(p, rt->target_type) || |
| 1803 | !policydb_class_isvalid(p, rt->target_class)) |
| 1804 | goto out; |
| 1805 | |
| 1806 | rc = -ENOMEM; |
| 1807 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 1808 | if (!r) |
| 1809 | goto out; |
| 1810 | |
| 1811 | rc = mls_read_range_helper(r, fp); |
| 1812 | if (rc) |
| 1813 | goto out; |
| 1814 | |
| 1815 | rc = -EINVAL; |
| 1816 | if (!mls_range_isvalid(p, r)) { |
| 1817 | printk(KERN_WARNING "SELinux: rangetrans: invalid range\n"); |
| 1818 | goto out; |
| 1819 | } |
| 1820 | |
| 1821 | rc = hashtab_insert(p->range_tr, rt, r); |
| 1822 | if (rc) |
| 1823 | goto out; |
| 1824 | |
| 1825 | rt = NULL; |
| 1826 | r = NULL; |
| 1827 | } |
| 1828 | rangetr_hash_eval(p->range_tr); |
| 1829 | rc = 0; |
| 1830 | out: |
| 1831 | kfree(rt); |
| 1832 | kfree(r); |
| 1833 | return rc; |
| 1834 | } |
| 1835 | |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 1836 | static int genfs_read(struct policydb *p, void *fp) |
| 1837 | { |
| 1838 | int i, j, rc; |
| 1839 | u32 nel, nel2, len, len2; |
| 1840 | __le32 buf[1]; |
| 1841 | struct ocontext *l, *c; |
| 1842 | struct ocontext *newc = NULL; |
| 1843 | struct genfs *genfs_p, *genfs; |
| 1844 | struct genfs *newgenfs = NULL; |
| 1845 | |
| 1846 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1847 | if (rc) |
| 1848 | goto out; |
| 1849 | nel = le32_to_cpu(buf[0]); |
| 1850 | |
| 1851 | for (i = 0; i < nel; i++) { |
| 1852 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1853 | if (rc) |
| 1854 | goto out; |
| 1855 | len = le32_to_cpu(buf[0]); |
| 1856 | |
| 1857 | rc = -ENOMEM; |
| 1858 | newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL); |
| 1859 | if (!newgenfs) |
| 1860 | goto out; |
| 1861 | |
| 1862 | rc = -ENOMEM; |
| 1863 | newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL); |
| 1864 | if (!newgenfs->fstype) |
| 1865 | goto out; |
| 1866 | |
| 1867 | rc = next_entry(newgenfs->fstype, fp, len); |
| 1868 | if (rc) |
| 1869 | goto out; |
| 1870 | |
| 1871 | newgenfs->fstype[len] = 0; |
| 1872 | |
| 1873 | for (genfs_p = NULL, genfs = p->genfs; genfs; |
| 1874 | genfs_p = genfs, genfs = genfs->next) { |
| 1875 | rc = -EINVAL; |
| 1876 | if (strcmp(newgenfs->fstype, genfs->fstype) == 0) { |
| 1877 | printk(KERN_ERR "SELinux: dup genfs fstype %s\n", |
| 1878 | newgenfs->fstype); |
| 1879 | goto out; |
| 1880 | } |
| 1881 | if (strcmp(newgenfs->fstype, genfs->fstype) < 0) |
| 1882 | break; |
| 1883 | } |
| 1884 | newgenfs->next = genfs; |
| 1885 | if (genfs_p) |
| 1886 | genfs_p->next = newgenfs; |
| 1887 | else |
| 1888 | p->genfs = newgenfs; |
| 1889 | genfs = newgenfs; |
| 1890 | newgenfs = NULL; |
| 1891 | |
| 1892 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1893 | if (rc) |
| 1894 | goto out; |
| 1895 | |
| 1896 | nel2 = le32_to_cpu(buf[0]); |
| 1897 | for (j = 0; j < nel2; j++) { |
| 1898 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1899 | if (rc) |
| 1900 | goto out; |
| 1901 | len = le32_to_cpu(buf[0]); |
| 1902 | |
| 1903 | rc = -ENOMEM; |
| 1904 | newc = kzalloc(sizeof(*newc), GFP_KERNEL); |
| 1905 | if (!newc) |
| 1906 | goto out; |
| 1907 | |
| 1908 | rc = -ENOMEM; |
| 1909 | newc->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 1910 | if (!newc->u.name) |
| 1911 | goto out; |
| 1912 | |
| 1913 | rc = next_entry(newc->u.name, fp, len); |
| 1914 | if (rc) |
| 1915 | goto out; |
| 1916 | newc->u.name[len] = 0; |
| 1917 | |
| 1918 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1919 | if (rc) |
| 1920 | goto out; |
| 1921 | |
| 1922 | newc->v.sclass = le32_to_cpu(buf[0]); |
| 1923 | rc = context_read_and_validate(&newc->context[0], p, fp); |
| 1924 | if (rc) |
| 1925 | goto out; |
| 1926 | |
| 1927 | for (l = NULL, c = genfs->head; c; |
| 1928 | l = c, c = c->next) { |
| 1929 | rc = -EINVAL; |
| 1930 | if (!strcmp(newc->u.name, c->u.name) && |
| 1931 | (!c->v.sclass || !newc->v.sclass || |
| 1932 | newc->v.sclass == c->v.sclass)) { |
| 1933 | printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n", |
| 1934 | genfs->fstype, c->u.name); |
| 1935 | goto out; |
| 1936 | } |
| 1937 | len = strlen(newc->u.name); |
| 1938 | len2 = strlen(c->u.name); |
| 1939 | if (len > len2) |
| 1940 | break; |
| 1941 | } |
| 1942 | |
| 1943 | newc->next = c; |
| 1944 | if (l) |
| 1945 | l->next = newc; |
| 1946 | else |
| 1947 | genfs->head = newc; |
| 1948 | newc = NULL; |
| 1949 | } |
| 1950 | } |
| 1951 | rc = 0; |
| 1952 | out: |
| 1953 | if (newgenfs) |
| 1954 | kfree(newgenfs->fstype); |
| 1955 | kfree(newgenfs); |
| 1956 | ocontext_destroy(newc, OCON_FSUSE); |
| 1957 | |
| 1958 | return rc; |
| 1959 | } |
| 1960 | |
Eric Paris | 692a8a2 | 2010-07-21 12:51:03 -0400 | [diff] [blame] | 1961 | static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, |
| 1962 | void *fp) |
| 1963 | { |
| 1964 | int i, j, rc; |
| 1965 | u32 nel, len; |
| 1966 | __le32 buf[3]; |
| 1967 | struct ocontext *l, *c; |
| 1968 | u32 nodebuf[8]; |
| 1969 | |
| 1970 | for (i = 0; i < info->ocon_num; i++) { |
| 1971 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1972 | if (rc) |
| 1973 | goto out; |
| 1974 | nel = le32_to_cpu(buf[0]); |
| 1975 | |
| 1976 | l = NULL; |
| 1977 | for (j = 0; j < nel; j++) { |
| 1978 | rc = -ENOMEM; |
| 1979 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 1980 | if (!c) |
| 1981 | goto out; |
| 1982 | if (l) |
| 1983 | l->next = c; |
| 1984 | else |
| 1985 | p->ocontexts[i] = c; |
| 1986 | l = c; |
| 1987 | |
| 1988 | switch (i) { |
| 1989 | case OCON_ISID: |
| 1990 | rc = next_entry(buf, fp, sizeof(u32)); |
| 1991 | if (rc) |
| 1992 | goto out; |
| 1993 | |
| 1994 | c->sid[0] = le32_to_cpu(buf[0]); |
| 1995 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 1996 | if (rc) |
| 1997 | goto out; |
| 1998 | break; |
| 1999 | case OCON_FS: |
| 2000 | case OCON_NETIF: |
| 2001 | rc = next_entry(buf, fp, sizeof(u32)); |
| 2002 | if (rc) |
| 2003 | goto out; |
| 2004 | len = le32_to_cpu(buf[0]); |
| 2005 | |
| 2006 | rc = -ENOMEM; |
| 2007 | c->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 2008 | if (!c->u.name) |
| 2009 | goto out; |
| 2010 | |
| 2011 | rc = next_entry(c->u.name, fp, len); |
| 2012 | if (rc) |
| 2013 | goto out; |
| 2014 | |
| 2015 | c->u.name[len] = 0; |
| 2016 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2017 | if (rc) |
| 2018 | goto out; |
| 2019 | rc = context_read_and_validate(&c->context[1], p, fp); |
| 2020 | if (rc) |
| 2021 | goto out; |
| 2022 | break; |
| 2023 | case OCON_PORT: |
| 2024 | rc = next_entry(buf, fp, sizeof(u32)*3); |
| 2025 | if (rc) |
| 2026 | goto out; |
| 2027 | c->u.port.protocol = le32_to_cpu(buf[0]); |
| 2028 | c->u.port.low_port = le32_to_cpu(buf[1]); |
| 2029 | c->u.port.high_port = le32_to_cpu(buf[2]); |
| 2030 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2031 | if (rc) |
| 2032 | goto out; |
| 2033 | break; |
| 2034 | case OCON_NODE: |
| 2035 | rc = next_entry(nodebuf, fp, sizeof(u32) * 2); |
| 2036 | if (rc) |
| 2037 | goto out; |
| 2038 | c->u.node.addr = nodebuf[0]; /* network order */ |
| 2039 | c->u.node.mask = nodebuf[1]; /* network order */ |
| 2040 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2041 | if (rc) |
| 2042 | goto out; |
| 2043 | break; |
| 2044 | case OCON_FSUSE: |
| 2045 | rc = next_entry(buf, fp, sizeof(u32)*2); |
| 2046 | if (rc) |
| 2047 | goto out; |
| 2048 | |
| 2049 | rc = -EINVAL; |
| 2050 | c->v.behavior = le32_to_cpu(buf[0]); |
| 2051 | if (c->v.behavior > SECURITY_FS_USE_NONE) |
| 2052 | goto out; |
| 2053 | |
| 2054 | rc = -ENOMEM; |
| 2055 | len = le32_to_cpu(buf[1]); |
| 2056 | c->u.name = kmalloc(len + 1, GFP_KERNEL); |
| 2057 | if (!c->u.name) |
| 2058 | goto out; |
| 2059 | |
| 2060 | rc = next_entry(c->u.name, fp, len); |
| 2061 | if (rc) |
| 2062 | goto out; |
| 2063 | c->u.name[len] = 0; |
| 2064 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2065 | if (rc) |
| 2066 | goto out; |
| 2067 | break; |
| 2068 | case OCON_NODE6: { |
| 2069 | int k; |
| 2070 | |
| 2071 | rc = next_entry(nodebuf, fp, sizeof(u32) * 8); |
| 2072 | if (rc) |
| 2073 | goto out; |
| 2074 | for (k = 0; k < 4; k++) |
| 2075 | c->u.node6.addr[k] = nodebuf[k]; |
| 2076 | for (k = 0; k < 4; k++) |
| 2077 | c->u.node6.mask[k] = nodebuf[k+4]; |
| 2078 | rc = context_read_and_validate(&c->context[0], p, fp); |
| 2079 | if (rc) |
| 2080 | goto out; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | rc = 0; |
| 2087 | out: |
| 2088 | return rc; |
| 2089 | } |
| 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | /* |
| 2092 | * Read the configuration data from a policy database binary |
| 2093 | * representation file into a policy database structure. |
| 2094 | */ |
| 2095 | int policydb_read(struct policydb *p, void *fp) |
| 2096 | { |
| 2097 | struct role_allow *ra, *lra; |
| 2098 | struct role_trans *tr, *ltr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | int i, j, rc; |
Stephen Smalley | 59dbd1b | 2008-06-05 09:48:51 -0400 | [diff] [blame] | 2100 | __le32 buf[4]; |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 2101 | u32 len, nprim, nel; |
| 2102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | char *policydb_str; |
| 2104 | struct policydb_compat_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | rc = policydb_init(p); |
| 2107 | if (rc) |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2108 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
| 2110 | /* Read the magic number and string length. */ |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2111 | rc = next_entry(buf, fp, sizeof(u32) * 2); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2112 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | goto bad; |
| 2114 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2115 | rc = -EINVAL; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2116 | if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2117 | printk(KERN_ERR "SELinux: policydb magic number 0x%x does " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | "not match expected magic number 0x%x\n", |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2119 | le32_to_cpu(buf[0]), POLICYDB_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | goto bad; |
| 2121 | } |
| 2122 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2123 | rc = -EINVAL; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2124 | len = le32_to_cpu(buf[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | if (len != strlen(POLICYDB_STRING)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2126 | printk(KERN_ERR "SELinux: policydb string length %d does not " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | "match expected length %Zu\n", |
| 2128 | len, strlen(POLICYDB_STRING)); |
| 2129 | goto bad; |
| 2130 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2131 | |
| 2132 | rc = -ENOMEM; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2133 | policydb_str = kmalloc(len + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | if (!policydb_str) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2135 | printk(KERN_ERR "SELinux: unable to allocate memory for policydb " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | "string of length %d\n", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | goto bad; |
| 2138 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | rc = next_entry(policydb_str, fp, len); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2141 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2142 | printk(KERN_ERR "SELinux: truncated policydb string identifier\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | kfree(policydb_str); |
| 2144 | goto bad; |
| 2145 | } |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2146 | |
| 2147 | rc = -EINVAL; |
Vesa-Matti J Kari | df4ea86 | 2008-07-20 23:57:01 +0300 | [diff] [blame] | 2148 | policydb_str[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | if (strcmp(policydb_str, POLICYDB_STRING)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2150 | printk(KERN_ERR "SELinux: policydb string %s does not match " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | "my string %s\n", policydb_str, POLICYDB_STRING); |
| 2152 | kfree(policydb_str); |
| 2153 | goto bad; |
| 2154 | } |
| 2155 | /* Done with policydb_str. */ |
| 2156 | kfree(policydb_str); |
| 2157 | policydb_str = NULL; |
| 2158 | |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2159 | /* Read the version and table sizes. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | rc = next_entry(buf, fp, sizeof(u32)*4); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2161 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2164 | rc = -EINVAL; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2165 | p->policyvers = le32_to_cpu(buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | if (p->policyvers < POLICYDB_VERSION_MIN || |
| 2167 | p->policyvers > POLICYDB_VERSION_MAX) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2168 | printk(KERN_ERR "SELinux: policydb version %d does not match " |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2169 | "my version range %d-%d\n", |
| 2170 | le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX); |
| 2171 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2174 | if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) { |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2175 | p->mls_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2177 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | if (p->policyvers < POLICYDB_VERSION_MLS) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2179 | printk(KERN_ERR "SELinux: security policydb version %d " |
| 2180 | "(MLS) not backwards compatible\n", |
| 2181 | p->policyvers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | goto bad; |
| 2183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 2185 | p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN); |
| 2186 | p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2188 | if (p->policyvers >= POLICYDB_VERSION_POLCAP) { |
| 2189 | rc = ebitmap_read(&p->policycaps, fp); |
| 2190 | if (rc) |
| 2191 | goto bad; |
| 2192 | } |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2193 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2194 | if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { |
| 2195 | rc = ebitmap_read(&p->permissive_map, fp); |
| 2196 | if (rc) |
| 2197 | goto bad; |
| 2198 | } |
Eric Paris | 64dbf07 | 2008-03-31 12:17:33 +1100 | [diff] [blame] | 2199 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2200 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | info = policydb_lookup_compat(p->policyvers); |
| 2202 | if (!info) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2203 | printk(KERN_ERR "SELinux: unable to find policy compat info " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | "for version %d\n", p->policyvers); |
| 2205 | goto bad; |
| 2206 | } |
| 2207 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2208 | rc = -EINVAL; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2209 | if (le32_to_cpu(buf[2]) != info->sym_num || |
| 2210 | le32_to_cpu(buf[3]) != info->ocon_num) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 2211 | printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do " |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 2212 | "not match mine (%d,%d)\n", le32_to_cpu(buf[2]), |
| 2213 | le32_to_cpu(buf[3]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | info->sym_num, info->ocon_num); |
| 2215 | goto bad; |
| 2216 | } |
| 2217 | |
| 2218 | for (i = 0; i < info->sym_num; i++) { |
| 2219 | rc = next_entry(buf, fp, sizeof(u32)*2); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2220 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | goto bad; |
| 2222 | nprim = le32_to_cpu(buf[0]); |
| 2223 | nel = le32_to_cpu(buf[1]); |
| 2224 | for (j = 0; j < nel; j++) { |
| 2225 | rc = read_f[i](p, p->symtab[i].table, fp); |
| 2226 | if (rc) |
| 2227 | goto bad; |
| 2228 | } |
| 2229 | |
| 2230 | p->symtab[i].nprim = nprim; |
| 2231 | } |
| 2232 | |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2233 | rc = avtab_read(&p->te_avtab, fp, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | if (rc) |
| 2235 | goto bad; |
| 2236 | |
| 2237 | if (p->policyvers >= POLICYDB_VERSION_BOOL) { |
| 2238 | rc = cond_read_list(p, fp); |
| 2239 | if (rc) |
| 2240 | goto bad; |
| 2241 | } |
| 2242 | |
| 2243 | rc = next_entry(buf, fp, sizeof(u32)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2244 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | goto bad; |
| 2246 | nel = le32_to_cpu(buf[0]); |
| 2247 | ltr = NULL; |
| 2248 | for (i = 0; i < nel; i++) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2249 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2250 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2251 | if (!tr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | goto bad; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2253 | if (ltr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | ltr->next = tr; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2255 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | p->role_tr = tr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | rc = next_entry(buf, fp, sizeof(u32)*3); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2258 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2260 | |
| 2261 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | tr->role = le32_to_cpu(buf[0]); |
| 2263 | tr->type = le32_to_cpu(buf[1]); |
| 2264 | tr->new_role = le32_to_cpu(buf[2]); |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2265 | if (!policydb_role_isvalid(p, tr->role) || |
| 2266 | !policydb_type_isvalid(p, tr->type) || |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2267 | !policydb_role_isvalid(p, tr->new_role)) |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2268 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | ltr = tr; |
| 2270 | } |
| 2271 | |
| 2272 | rc = next_entry(buf, fp, sizeof(u32)); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2273 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | goto bad; |
| 2275 | nel = le32_to_cpu(buf[0]); |
| 2276 | lra = NULL; |
| 2277 | for (i = 0; i < nel; i++) { |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2278 | rc = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2279 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2280 | if (!ra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | goto bad; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2282 | if (lra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | lra->next = ra; |
Eric Paris | 2ced3df | 2008-04-17 13:37:12 -0400 | [diff] [blame] | 2284 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | p->role_allow = ra; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | rc = next_entry(buf, fp, sizeof(u32)*2); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2287 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2289 | |
| 2290 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | ra->role = le32_to_cpu(buf[0]); |
| 2292 | ra->new_role = le32_to_cpu(buf[1]); |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2293 | if (!policydb_role_isvalid(p, ra->role) || |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2294 | !policydb_role_isvalid(p, ra->new_role)) |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 2295 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | lra = ra; |
| 2297 | } |
| 2298 | |
| 2299 | rc = policydb_index_classes(p); |
| 2300 | if (rc) |
| 2301 | goto bad; |
| 2302 | |
| 2303 | rc = policydb_index_others(p); |
| 2304 | if (rc) |
| 2305 | goto bad; |
| 2306 | |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2307 | rc = -EINVAL; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2308 | p->process_class = string_to_security_class(p, "process"); |
| 2309 | if (!p->process_class) |
| 2310 | goto bad; |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 2311 | |
| 2312 | rc = -EINVAL; |
| 2313 | p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition"); |
| 2314 | p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition"); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2315 | if (!p->process_trans_perms) |
| 2316 | goto bad; |
| 2317 | |
Eric Paris | 692a8a2 | 2010-07-21 12:51:03 -0400 | [diff] [blame] | 2318 | rc = ocontext_read(p, info, fp); |
| 2319 | if (rc) |
| 2320 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
Eric Paris | d1b4354 | 2010-07-21 12:50:57 -0400 | [diff] [blame] | 2322 | rc = genfs_read(p, fp); |
| 2323 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | |
Eric Paris | 9ee0c82 | 2010-06-11 12:37:05 -0400 | [diff] [blame] | 2326 | rc = range_read(p, fp); |
| 2327 | if (rc) |
| 2328 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2330 | rc = -ENOMEM; |
| 2331 | p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap), |
| 2332 | p->p_types.nprim, |
| 2333 | GFP_KERNEL | __GFP_ZERO); |
| 2334 | if (!p->type_attr_map_array) |
| 2335 | goto bad; |
| 2336 | |
| 2337 | /* preallocate so we don't have to worry about the put ever failing */ |
| 2338 | rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1, |
| 2339 | GFP_KERNEL | __GFP_ZERO); |
| 2340 | if (rc) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2341 | goto bad; |
| 2342 | |
| 2343 | for (i = 0; i < p->p_types.nprim; i++) { |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2344 | struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); |
| 2345 | |
| 2346 | BUG_ON(!e); |
| 2347 | ebitmap_init(e); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2348 | if (p->policyvers >= POLICYDB_VERSION_AVTAB) { |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2349 | rc = ebitmap_read(e, fp); |
| 2350 | if (rc) |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2351 | goto bad; |
| 2352 | } |
| 2353 | /* add the type itself as the degenerate case */ |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 2354 | rc = ebitmap_set_bit(e, i, 1); |
| 2355 | if (rc) |
| 2356 | goto bad; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 2359 | rc = policydb_bounds_sanity_check(p); |
| 2360 | if (rc) |
| 2361 | goto bad; |
| 2362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | rc = 0; |
| 2364 | out: |
| 2365 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | bad: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | policydb_destroy(p); |
| 2368 | goto out; |
| 2369 | } |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 2370 | |
| 2371 | /* |
| 2372 | * Write a MLS level structure to a policydb binary |
| 2373 | * representation file. |
| 2374 | */ |
| 2375 | static int mls_write_level(struct mls_level *l, void *fp) |
| 2376 | { |
| 2377 | __le32 buf[1]; |
| 2378 | int rc; |
| 2379 | |
| 2380 | buf[0] = cpu_to_le32(l->sens); |
| 2381 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2382 | if (rc) |
| 2383 | return rc; |
| 2384 | |
| 2385 | rc = ebitmap_write(&l->cat, fp); |
| 2386 | if (rc) |
| 2387 | return rc; |
| 2388 | |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * Write a MLS range structure to a policydb binary |
| 2394 | * representation file. |
| 2395 | */ |
| 2396 | static int mls_write_range_helper(struct mls_range *r, void *fp) |
| 2397 | { |
| 2398 | __le32 buf[3]; |
| 2399 | size_t items; |
| 2400 | int rc, eq; |
| 2401 | |
| 2402 | eq = mls_level_eq(&r->level[1], &r->level[0]); |
| 2403 | |
| 2404 | if (eq) |
| 2405 | items = 2; |
| 2406 | else |
| 2407 | items = 3; |
| 2408 | buf[0] = cpu_to_le32(items-1); |
| 2409 | buf[1] = cpu_to_le32(r->level[0].sens); |
| 2410 | if (!eq) |
| 2411 | buf[2] = cpu_to_le32(r->level[1].sens); |
| 2412 | |
| 2413 | BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); |
| 2414 | |
| 2415 | rc = put_entry(buf, sizeof(u32), items, fp); |
| 2416 | if (rc) |
| 2417 | return rc; |
| 2418 | |
| 2419 | rc = ebitmap_write(&r->level[0].cat, fp); |
| 2420 | if (rc) |
| 2421 | return rc; |
| 2422 | if (!eq) { |
| 2423 | rc = ebitmap_write(&r->level[1].cat, fp); |
| 2424 | if (rc) |
| 2425 | return rc; |
| 2426 | } |
| 2427 | |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| 2431 | static int sens_write(void *vkey, void *datum, void *ptr) |
| 2432 | { |
| 2433 | char *key = vkey; |
| 2434 | struct level_datum *levdatum = datum; |
| 2435 | struct policy_data *pd = ptr; |
| 2436 | void *fp = pd->fp; |
| 2437 | __le32 buf[2]; |
| 2438 | size_t len; |
| 2439 | int rc; |
| 2440 | |
| 2441 | len = strlen(key); |
| 2442 | buf[0] = cpu_to_le32(len); |
| 2443 | buf[1] = cpu_to_le32(levdatum->isalias); |
| 2444 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 2445 | if (rc) |
| 2446 | return rc; |
| 2447 | |
| 2448 | rc = put_entry(key, 1, len, fp); |
| 2449 | if (rc) |
| 2450 | return rc; |
| 2451 | |
| 2452 | rc = mls_write_level(levdatum->level, fp); |
| 2453 | if (rc) |
| 2454 | return rc; |
| 2455 | |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
| 2459 | static int cat_write(void *vkey, void *datum, void *ptr) |
| 2460 | { |
| 2461 | char *key = vkey; |
| 2462 | struct cat_datum *catdatum = datum; |
| 2463 | struct policy_data *pd = ptr; |
| 2464 | void *fp = pd->fp; |
| 2465 | __le32 buf[3]; |
| 2466 | size_t len; |
| 2467 | int rc; |
| 2468 | |
| 2469 | len = strlen(key); |
| 2470 | buf[0] = cpu_to_le32(len); |
| 2471 | buf[1] = cpu_to_le32(catdatum->value); |
| 2472 | buf[2] = cpu_to_le32(catdatum->isalias); |
| 2473 | rc = put_entry(buf, sizeof(u32), 3, fp); |
| 2474 | if (rc) |
| 2475 | return rc; |
| 2476 | |
| 2477 | rc = put_entry(key, 1, len, fp); |
| 2478 | if (rc) |
| 2479 | return rc; |
| 2480 | |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | static int role_trans_write(struct role_trans *r, void *fp) |
| 2485 | { |
| 2486 | struct role_trans *tr; |
| 2487 | u32 buf[3]; |
| 2488 | size_t nel; |
| 2489 | int rc; |
| 2490 | |
| 2491 | nel = 0; |
| 2492 | for (tr = r; tr; tr = tr->next) |
| 2493 | nel++; |
| 2494 | buf[0] = cpu_to_le32(nel); |
| 2495 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2496 | if (rc) |
| 2497 | return rc; |
| 2498 | for (tr = r; tr; tr = tr->next) { |
| 2499 | buf[0] = cpu_to_le32(tr->role); |
| 2500 | buf[1] = cpu_to_le32(tr->type); |
| 2501 | buf[2] = cpu_to_le32(tr->new_role); |
| 2502 | rc = put_entry(buf, sizeof(u32), 3, fp); |
| 2503 | if (rc) |
| 2504 | return rc; |
| 2505 | } |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | static int role_allow_write(struct role_allow *r, void *fp) |
| 2511 | { |
| 2512 | struct role_allow *ra; |
| 2513 | u32 buf[2]; |
| 2514 | size_t nel; |
| 2515 | int rc; |
| 2516 | |
| 2517 | nel = 0; |
| 2518 | for (ra = r; ra; ra = ra->next) |
| 2519 | nel++; |
| 2520 | buf[0] = cpu_to_le32(nel); |
| 2521 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2522 | if (rc) |
| 2523 | return rc; |
| 2524 | for (ra = r; ra; ra = ra->next) { |
| 2525 | buf[0] = cpu_to_le32(ra->role); |
| 2526 | buf[1] = cpu_to_le32(ra->new_role); |
| 2527 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 2528 | if (rc) |
| 2529 | return rc; |
| 2530 | } |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | * Write a security context structure |
| 2536 | * to a policydb binary representation file. |
| 2537 | */ |
| 2538 | static int context_write(struct policydb *p, struct context *c, |
| 2539 | void *fp) |
| 2540 | { |
| 2541 | int rc; |
| 2542 | __le32 buf[3]; |
| 2543 | |
| 2544 | buf[0] = cpu_to_le32(c->user); |
| 2545 | buf[1] = cpu_to_le32(c->role); |
| 2546 | buf[2] = cpu_to_le32(c->type); |
| 2547 | |
| 2548 | rc = put_entry(buf, sizeof(u32), 3, fp); |
| 2549 | if (rc) |
| 2550 | return rc; |
| 2551 | |
| 2552 | rc = mls_write_range_helper(&c->range, fp); |
| 2553 | if (rc) |
| 2554 | return rc; |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
| 2559 | /* |
| 2560 | * The following *_write functions are used to |
| 2561 | * write the symbol data to a policy database |
| 2562 | * binary representation file. |
| 2563 | */ |
| 2564 | |
| 2565 | static int perm_write(void *vkey, void *datum, void *fp) |
| 2566 | { |
| 2567 | char *key = vkey; |
| 2568 | struct perm_datum *perdatum = datum; |
| 2569 | __le32 buf[2]; |
| 2570 | size_t len; |
| 2571 | int rc; |
| 2572 | |
| 2573 | len = strlen(key); |
| 2574 | buf[0] = cpu_to_le32(len); |
| 2575 | buf[1] = cpu_to_le32(perdatum->value); |
| 2576 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 2577 | if (rc) |
| 2578 | return rc; |
| 2579 | |
| 2580 | rc = put_entry(key, 1, len, fp); |
| 2581 | if (rc) |
| 2582 | return rc; |
| 2583 | |
| 2584 | return 0; |
| 2585 | } |
| 2586 | |
| 2587 | static int common_write(void *vkey, void *datum, void *ptr) |
| 2588 | { |
| 2589 | char *key = vkey; |
| 2590 | struct common_datum *comdatum = datum; |
| 2591 | struct policy_data *pd = ptr; |
| 2592 | void *fp = pd->fp; |
| 2593 | __le32 buf[4]; |
| 2594 | size_t len; |
| 2595 | int rc; |
| 2596 | |
| 2597 | len = strlen(key); |
| 2598 | buf[0] = cpu_to_le32(len); |
| 2599 | buf[1] = cpu_to_le32(comdatum->value); |
| 2600 | buf[2] = cpu_to_le32(comdatum->permissions.nprim); |
| 2601 | buf[3] = cpu_to_le32(comdatum->permissions.table->nel); |
| 2602 | rc = put_entry(buf, sizeof(u32), 4, fp); |
| 2603 | if (rc) |
| 2604 | return rc; |
| 2605 | |
| 2606 | rc = put_entry(key, 1, len, fp); |
| 2607 | if (rc) |
| 2608 | return rc; |
| 2609 | |
| 2610 | rc = hashtab_map(comdatum->permissions.table, perm_write, fp); |
| 2611 | if (rc) |
| 2612 | return rc; |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | static int write_cons_helper(struct policydb *p, struct constraint_node *node, |
| 2618 | void *fp) |
| 2619 | { |
| 2620 | struct constraint_node *c; |
| 2621 | struct constraint_expr *e; |
| 2622 | __le32 buf[3]; |
| 2623 | u32 nel; |
| 2624 | int rc; |
| 2625 | |
| 2626 | for (c = node; c; c = c->next) { |
| 2627 | nel = 0; |
| 2628 | for (e = c->expr; e; e = e->next) |
| 2629 | nel++; |
| 2630 | buf[0] = cpu_to_le32(c->permissions); |
| 2631 | buf[1] = cpu_to_le32(nel); |
| 2632 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 2633 | if (rc) |
| 2634 | return rc; |
| 2635 | for (e = c->expr; e; e = e->next) { |
| 2636 | buf[0] = cpu_to_le32(e->expr_type); |
| 2637 | buf[1] = cpu_to_le32(e->attr); |
| 2638 | buf[2] = cpu_to_le32(e->op); |
| 2639 | rc = put_entry(buf, sizeof(u32), 3, fp); |
| 2640 | if (rc) |
| 2641 | return rc; |
| 2642 | |
| 2643 | switch (e->expr_type) { |
| 2644 | case CEXPR_NAMES: |
| 2645 | rc = ebitmap_write(&e->names, fp); |
| 2646 | if (rc) |
| 2647 | return rc; |
| 2648 | break; |
| 2649 | default: |
| 2650 | break; |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
| 2658 | static int class_write(void *vkey, void *datum, void *ptr) |
| 2659 | { |
| 2660 | char *key = vkey; |
| 2661 | struct class_datum *cladatum = datum; |
| 2662 | struct policy_data *pd = ptr; |
| 2663 | void *fp = pd->fp; |
| 2664 | struct policydb *p = pd->p; |
| 2665 | struct constraint_node *c; |
| 2666 | __le32 buf[6]; |
| 2667 | u32 ncons; |
| 2668 | size_t len, len2; |
| 2669 | int rc; |
| 2670 | |
| 2671 | len = strlen(key); |
| 2672 | if (cladatum->comkey) |
| 2673 | len2 = strlen(cladatum->comkey); |
| 2674 | else |
| 2675 | len2 = 0; |
| 2676 | |
| 2677 | ncons = 0; |
| 2678 | for (c = cladatum->constraints; c; c = c->next) |
| 2679 | ncons++; |
| 2680 | |
| 2681 | buf[0] = cpu_to_le32(len); |
| 2682 | buf[1] = cpu_to_le32(len2); |
| 2683 | buf[2] = cpu_to_le32(cladatum->value); |
| 2684 | buf[3] = cpu_to_le32(cladatum->permissions.nprim); |
| 2685 | if (cladatum->permissions.table) |
| 2686 | buf[4] = cpu_to_le32(cladatum->permissions.table->nel); |
| 2687 | else |
| 2688 | buf[4] = 0; |
| 2689 | buf[5] = cpu_to_le32(ncons); |
| 2690 | rc = put_entry(buf, sizeof(u32), 6, fp); |
| 2691 | if (rc) |
| 2692 | return rc; |
| 2693 | |
| 2694 | rc = put_entry(key, 1, len, fp); |
| 2695 | if (rc) |
| 2696 | return rc; |
| 2697 | |
| 2698 | if (cladatum->comkey) { |
| 2699 | rc = put_entry(cladatum->comkey, 1, len2, fp); |
| 2700 | if (rc) |
| 2701 | return rc; |
| 2702 | } |
| 2703 | |
| 2704 | rc = hashtab_map(cladatum->permissions.table, perm_write, fp); |
| 2705 | if (rc) |
| 2706 | return rc; |
| 2707 | |
| 2708 | rc = write_cons_helper(p, cladatum->constraints, fp); |
| 2709 | if (rc) |
| 2710 | return rc; |
| 2711 | |
| 2712 | /* write out the validatetrans rule */ |
| 2713 | ncons = 0; |
| 2714 | for (c = cladatum->validatetrans; c; c = c->next) |
| 2715 | ncons++; |
| 2716 | |
| 2717 | buf[0] = cpu_to_le32(ncons); |
| 2718 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2719 | if (rc) |
| 2720 | return rc; |
| 2721 | |
| 2722 | rc = write_cons_helper(p, cladatum->validatetrans, fp); |
| 2723 | if (rc) |
| 2724 | return rc; |
| 2725 | |
| 2726 | return 0; |
| 2727 | } |
| 2728 | |
| 2729 | static int role_write(void *vkey, void *datum, void *ptr) |
| 2730 | { |
| 2731 | char *key = vkey; |
| 2732 | struct role_datum *role = datum; |
| 2733 | struct policy_data *pd = ptr; |
| 2734 | void *fp = pd->fp; |
| 2735 | struct policydb *p = pd->p; |
| 2736 | __le32 buf[3]; |
| 2737 | size_t items, len; |
| 2738 | int rc; |
| 2739 | |
| 2740 | len = strlen(key); |
| 2741 | items = 0; |
| 2742 | buf[items++] = cpu_to_le32(len); |
| 2743 | buf[items++] = cpu_to_le32(role->value); |
| 2744 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 2745 | buf[items++] = cpu_to_le32(role->bounds); |
| 2746 | |
| 2747 | BUG_ON(items > (sizeof(buf)/sizeof(buf[0]))); |
| 2748 | |
| 2749 | rc = put_entry(buf, sizeof(u32), items, fp); |
| 2750 | if (rc) |
| 2751 | return rc; |
| 2752 | |
| 2753 | rc = put_entry(key, 1, len, fp); |
| 2754 | if (rc) |
| 2755 | return rc; |
| 2756 | |
| 2757 | rc = ebitmap_write(&role->dominates, fp); |
| 2758 | if (rc) |
| 2759 | return rc; |
| 2760 | |
| 2761 | rc = ebitmap_write(&role->types, fp); |
| 2762 | if (rc) |
| 2763 | return rc; |
| 2764 | |
| 2765 | return 0; |
| 2766 | } |
| 2767 | |
| 2768 | static int type_write(void *vkey, void *datum, void *ptr) |
| 2769 | { |
| 2770 | char *key = vkey; |
| 2771 | struct type_datum *typdatum = datum; |
| 2772 | struct policy_data *pd = ptr; |
| 2773 | struct policydb *p = pd->p; |
| 2774 | void *fp = pd->fp; |
| 2775 | __le32 buf[4]; |
| 2776 | int rc; |
| 2777 | size_t items, len; |
| 2778 | |
| 2779 | len = strlen(key); |
| 2780 | items = 0; |
| 2781 | buf[items++] = cpu_to_le32(len); |
| 2782 | buf[items++] = cpu_to_le32(typdatum->value); |
| 2783 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) { |
| 2784 | u32 properties = 0; |
| 2785 | |
| 2786 | if (typdatum->primary) |
| 2787 | properties |= TYPEDATUM_PROPERTY_PRIMARY; |
| 2788 | |
| 2789 | if (typdatum->attribute) |
| 2790 | properties |= TYPEDATUM_PROPERTY_ATTRIBUTE; |
| 2791 | |
| 2792 | buf[items++] = cpu_to_le32(properties); |
| 2793 | buf[items++] = cpu_to_le32(typdatum->bounds); |
| 2794 | } else { |
| 2795 | buf[items++] = cpu_to_le32(typdatum->primary); |
| 2796 | } |
| 2797 | BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); |
| 2798 | rc = put_entry(buf, sizeof(u32), items, fp); |
| 2799 | if (rc) |
| 2800 | return rc; |
| 2801 | |
| 2802 | rc = put_entry(key, 1, len, fp); |
| 2803 | if (rc) |
| 2804 | return rc; |
| 2805 | |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
| 2809 | static int user_write(void *vkey, void *datum, void *ptr) |
| 2810 | { |
| 2811 | char *key = vkey; |
| 2812 | struct user_datum *usrdatum = datum; |
| 2813 | struct policy_data *pd = ptr; |
| 2814 | struct policydb *p = pd->p; |
| 2815 | void *fp = pd->fp; |
| 2816 | __le32 buf[3]; |
| 2817 | size_t items, len; |
| 2818 | int rc; |
| 2819 | |
| 2820 | len = strlen(key); |
| 2821 | items = 0; |
| 2822 | buf[items++] = cpu_to_le32(len); |
| 2823 | buf[items++] = cpu_to_le32(usrdatum->value); |
| 2824 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
| 2825 | buf[items++] = cpu_to_le32(usrdatum->bounds); |
| 2826 | BUG_ON(items > (sizeof(buf) / sizeof(buf[0]))); |
| 2827 | rc = put_entry(buf, sizeof(u32), items, fp); |
| 2828 | if (rc) |
| 2829 | return rc; |
| 2830 | |
| 2831 | rc = put_entry(key, 1, len, fp); |
| 2832 | if (rc) |
| 2833 | return rc; |
| 2834 | |
| 2835 | rc = ebitmap_write(&usrdatum->roles, fp); |
| 2836 | if (rc) |
| 2837 | return rc; |
| 2838 | |
| 2839 | rc = mls_write_range_helper(&usrdatum->range, fp); |
| 2840 | if (rc) |
| 2841 | return rc; |
| 2842 | |
| 2843 | rc = mls_write_level(&usrdatum->dfltlevel, fp); |
| 2844 | if (rc) |
| 2845 | return rc; |
| 2846 | |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | static int (*write_f[SYM_NUM]) (void *key, void *datum, |
| 2851 | void *datap) = |
| 2852 | { |
| 2853 | common_write, |
| 2854 | class_write, |
| 2855 | role_write, |
| 2856 | type_write, |
| 2857 | user_write, |
| 2858 | cond_write_bool, |
| 2859 | sens_write, |
| 2860 | cat_write, |
| 2861 | }; |
| 2862 | |
| 2863 | static int ocontext_write(struct policydb *p, struct policydb_compat_info *info, |
| 2864 | void *fp) |
| 2865 | { |
| 2866 | unsigned int i, j, rc; |
| 2867 | size_t nel, len; |
| 2868 | __le32 buf[3]; |
| 2869 | u32 nodebuf[8]; |
| 2870 | struct ocontext *c; |
| 2871 | for (i = 0; i < info->ocon_num; i++) { |
| 2872 | nel = 0; |
| 2873 | for (c = p->ocontexts[i]; c; c = c->next) |
| 2874 | nel++; |
| 2875 | buf[0] = cpu_to_le32(nel); |
| 2876 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2877 | if (rc) |
| 2878 | return rc; |
| 2879 | for (c = p->ocontexts[i]; c; c = c->next) { |
| 2880 | switch (i) { |
| 2881 | case OCON_ISID: |
| 2882 | buf[0] = cpu_to_le32(c->sid[0]); |
| 2883 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2884 | if (rc) |
| 2885 | return rc; |
| 2886 | rc = context_write(p, &c->context[0], fp); |
| 2887 | if (rc) |
| 2888 | return rc; |
| 2889 | break; |
| 2890 | case OCON_FS: |
| 2891 | case OCON_NETIF: |
| 2892 | len = strlen(c->u.name); |
| 2893 | buf[0] = cpu_to_le32(len); |
| 2894 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2895 | if (rc) |
| 2896 | return rc; |
| 2897 | rc = put_entry(c->u.name, 1, len, fp); |
| 2898 | if (rc) |
| 2899 | return rc; |
| 2900 | rc = context_write(p, &c->context[0], fp); |
| 2901 | if (rc) |
| 2902 | return rc; |
| 2903 | rc = context_write(p, &c->context[1], fp); |
| 2904 | if (rc) |
| 2905 | return rc; |
| 2906 | break; |
| 2907 | case OCON_PORT: |
| 2908 | buf[0] = cpu_to_le32(c->u.port.protocol); |
| 2909 | buf[1] = cpu_to_le32(c->u.port.low_port); |
| 2910 | buf[2] = cpu_to_le32(c->u.port.high_port); |
| 2911 | rc = put_entry(buf, sizeof(u32), 3, fp); |
| 2912 | if (rc) |
| 2913 | return rc; |
| 2914 | rc = context_write(p, &c->context[0], fp); |
| 2915 | if (rc) |
| 2916 | return rc; |
| 2917 | break; |
| 2918 | case OCON_NODE: |
| 2919 | nodebuf[0] = c->u.node.addr; /* network order */ |
| 2920 | nodebuf[1] = c->u.node.mask; /* network order */ |
| 2921 | rc = put_entry(nodebuf, sizeof(u32), 2, fp); |
| 2922 | if (rc) |
| 2923 | return rc; |
| 2924 | rc = context_write(p, &c->context[0], fp); |
| 2925 | if (rc) |
| 2926 | return rc; |
| 2927 | break; |
| 2928 | case OCON_FSUSE: |
| 2929 | buf[0] = cpu_to_le32(c->v.behavior); |
| 2930 | len = strlen(c->u.name); |
| 2931 | buf[1] = cpu_to_le32(len); |
| 2932 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 2933 | if (rc) |
| 2934 | return rc; |
| 2935 | rc = put_entry(c->u.name, 1, len, fp); |
| 2936 | if (rc) |
| 2937 | return rc; |
| 2938 | rc = context_write(p, &c->context[0], fp); |
| 2939 | if (rc) |
| 2940 | return rc; |
| 2941 | break; |
| 2942 | case OCON_NODE6: |
| 2943 | for (j = 0; j < 4; j++) |
| 2944 | nodebuf[j] = c->u.node6.addr[j]; /* network order */ |
| 2945 | for (j = 0; j < 4; j++) |
| 2946 | nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */ |
| 2947 | rc = put_entry(nodebuf, sizeof(u32), 8, fp); |
| 2948 | if (rc) |
| 2949 | return rc; |
| 2950 | rc = context_write(p, &c->context[0], fp); |
| 2951 | if (rc) |
| 2952 | return rc; |
| 2953 | break; |
| 2954 | } |
| 2955 | } |
| 2956 | } |
| 2957 | return 0; |
| 2958 | } |
| 2959 | |
| 2960 | static int genfs_write(struct policydb *p, void *fp) |
| 2961 | { |
| 2962 | struct genfs *genfs; |
| 2963 | struct ocontext *c; |
| 2964 | size_t len; |
| 2965 | __le32 buf[1]; |
| 2966 | int rc; |
| 2967 | |
| 2968 | len = 0; |
| 2969 | for (genfs = p->genfs; genfs; genfs = genfs->next) |
| 2970 | len++; |
| 2971 | buf[0] = cpu_to_le32(len); |
| 2972 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2973 | if (rc) |
| 2974 | return rc; |
| 2975 | for (genfs = p->genfs; genfs; genfs = genfs->next) { |
| 2976 | len = strlen(genfs->fstype); |
| 2977 | buf[0] = cpu_to_le32(len); |
| 2978 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2979 | if (rc) |
| 2980 | return rc; |
| 2981 | rc = put_entry(genfs->fstype, 1, len, fp); |
| 2982 | if (rc) |
| 2983 | return rc; |
| 2984 | len = 0; |
| 2985 | for (c = genfs->head; c; c = c->next) |
| 2986 | len++; |
| 2987 | buf[0] = cpu_to_le32(len); |
| 2988 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2989 | if (rc) |
| 2990 | return rc; |
| 2991 | for (c = genfs->head; c; c = c->next) { |
| 2992 | len = strlen(c->u.name); |
| 2993 | buf[0] = cpu_to_le32(len); |
| 2994 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 2995 | if (rc) |
| 2996 | return rc; |
| 2997 | rc = put_entry(c->u.name, 1, len, fp); |
| 2998 | if (rc) |
| 2999 | return rc; |
| 3000 | buf[0] = cpu_to_le32(c->v.sclass); |
| 3001 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 3002 | if (rc) |
| 3003 | return rc; |
| 3004 | rc = context_write(p, &c->context[0], fp); |
| 3005 | if (rc) |
| 3006 | return rc; |
| 3007 | } |
| 3008 | } |
| 3009 | return 0; |
| 3010 | } |
| 3011 | |
| 3012 | static int range_count(void *key, void *data, void *ptr) |
| 3013 | { |
| 3014 | int *cnt = ptr; |
| 3015 | *cnt = *cnt + 1; |
| 3016 | |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
| 3020 | static int range_write_helper(void *key, void *data, void *ptr) |
| 3021 | { |
| 3022 | __le32 buf[2]; |
| 3023 | struct range_trans *rt = key; |
| 3024 | struct mls_range *r = data; |
| 3025 | struct policy_data *pd = ptr; |
| 3026 | void *fp = pd->fp; |
| 3027 | struct policydb *p = pd->p; |
| 3028 | int rc; |
| 3029 | |
| 3030 | buf[0] = cpu_to_le32(rt->source_type); |
| 3031 | buf[1] = cpu_to_le32(rt->target_type); |
| 3032 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 3033 | if (rc) |
| 3034 | return rc; |
| 3035 | if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) { |
| 3036 | buf[0] = cpu_to_le32(rt->target_class); |
| 3037 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 3038 | if (rc) |
| 3039 | return rc; |
| 3040 | } |
| 3041 | rc = mls_write_range_helper(r, fp); |
| 3042 | if (rc) |
| 3043 | return rc; |
| 3044 | |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
| 3048 | static int range_write(struct policydb *p, void *fp) |
| 3049 | { |
| 3050 | size_t nel; |
| 3051 | __le32 buf[1]; |
| 3052 | int rc; |
| 3053 | struct policy_data pd; |
| 3054 | |
| 3055 | pd.p = p; |
| 3056 | pd.fp = fp; |
| 3057 | |
| 3058 | /* count the number of entries in the hashtab */ |
| 3059 | nel = 0; |
| 3060 | rc = hashtab_map(p->range_tr, range_count, &nel); |
| 3061 | if (rc) |
| 3062 | return rc; |
| 3063 | |
| 3064 | buf[0] = cpu_to_le32(nel); |
| 3065 | rc = put_entry(buf, sizeof(u32), 1, fp); |
| 3066 | if (rc) |
| 3067 | return rc; |
| 3068 | |
| 3069 | /* actually write all of the entries */ |
| 3070 | rc = hashtab_map(p->range_tr, range_write_helper, &pd); |
| 3071 | if (rc) |
| 3072 | return rc; |
| 3073 | |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | /* |
| 3078 | * Write the configuration data in a policy database |
| 3079 | * structure to a policy database binary representation |
| 3080 | * file. |
| 3081 | */ |
| 3082 | int policydb_write(struct policydb *p, void *fp) |
| 3083 | { |
| 3084 | unsigned int i, num_syms; |
| 3085 | int rc; |
| 3086 | __le32 buf[4]; |
| 3087 | u32 config; |
| 3088 | size_t len; |
| 3089 | struct policydb_compat_info *info; |
| 3090 | |
| 3091 | /* |
| 3092 | * refuse to write policy older than compressed avtab |
| 3093 | * to simplify the writer. There are other tests dropped |
| 3094 | * since we assume this throughout the writer code. Be |
| 3095 | * careful if you ever try to remove this restriction |
| 3096 | */ |
| 3097 | if (p->policyvers < POLICYDB_VERSION_AVTAB) { |
| 3098 | printk(KERN_ERR "SELinux: refusing to write policy version %d." |
| 3099 | " Because it is less than version %d\n", p->policyvers, |
| 3100 | POLICYDB_VERSION_AVTAB); |
| 3101 | return -EINVAL; |
| 3102 | } |
| 3103 | |
| 3104 | config = 0; |
| 3105 | if (p->mls_enabled) |
| 3106 | config |= POLICYDB_CONFIG_MLS; |
| 3107 | |
| 3108 | if (p->reject_unknown) |
| 3109 | config |= REJECT_UNKNOWN; |
| 3110 | if (p->allow_unknown) |
| 3111 | config |= ALLOW_UNKNOWN; |
| 3112 | |
| 3113 | /* Write the magic number and string identifiers. */ |
| 3114 | buf[0] = cpu_to_le32(POLICYDB_MAGIC); |
| 3115 | len = strlen(POLICYDB_STRING); |
| 3116 | buf[1] = cpu_to_le32(len); |
| 3117 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 3118 | if (rc) |
| 3119 | return rc; |
| 3120 | rc = put_entry(POLICYDB_STRING, 1, len, fp); |
| 3121 | if (rc) |
| 3122 | return rc; |
| 3123 | |
| 3124 | /* Write the version, config, and table sizes. */ |
| 3125 | info = policydb_lookup_compat(p->policyvers); |
| 3126 | if (!info) { |
| 3127 | printk(KERN_ERR "SELinux: compatibility lookup failed for policy " |
| 3128 | "version %d", p->policyvers); |
Eric Paris | 9398c7f | 2010-11-23 11:40:08 -0500 | [diff] [blame] | 3129 | return -EINVAL; |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | buf[0] = cpu_to_le32(p->policyvers); |
| 3133 | buf[1] = cpu_to_le32(config); |
| 3134 | buf[2] = cpu_to_le32(info->sym_num); |
| 3135 | buf[3] = cpu_to_le32(info->ocon_num); |
| 3136 | |
| 3137 | rc = put_entry(buf, sizeof(u32), 4, fp); |
| 3138 | if (rc) |
| 3139 | return rc; |
| 3140 | |
| 3141 | if (p->policyvers >= POLICYDB_VERSION_POLCAP) { |
| 3142 | rc = ebitmap_write(&p->policycaps, fp); |
| 3143 | if (rc) |
| 3144 | return rc; |
| 3145 | } |
| 3146 | |
| 3147 | if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) { |
| 3148 | rc = ebitmap_write(&p->permissive_map, fp); |
| 3149 | if (rc) |
| 3150 | return rc; |
| 3151 | } |
| 3152 | |
| 3153 | num_syms = info->sym_num; |
| 3154 | for (i = 0; i < num_syms; i++) { |
| 3155 | struct policy_data pd; |
| 3156 | |
| 3157 | pd.fp = fp; |
| 3158 | pd.p = p; |
| 3159 | |
| 3160 | buf[0] = cpu_to_le32(p->symtab[i].nprim); |
| 3161 | buf[1] = cpu_to_le32(p->symtab[i].table->nel); |
| 3162 | |
| 3163 | rc = put_entry(buf, sizeof(u32), 2, fp); |
| 3164 | if (rc) |
| 3165 | return rc; |
| 3166 | rc = hashtab_map(p->symtab[i].table, write_f[i], &pd); |
| 3167 | if (rc) |
| 3168 | return rc; |
| 3169 | } |
| 3170 | |
| 3171 | rc = avtab_write(p, &p->te_avtab, fp); |
| 3172 | if (rc) |
| 3173 | return rc; |
| 3174 | |
| 3175 | rc = cond_write_list(p, p->cond_list, fp); |
| 3176 | if (rc) |
| 3177 | return rc; |
| 3178 | |
| 3179 | rc = role_trans_write(p->role_tr, fp); |
| 3180 | if (rc) |
| 3181 | return rc; |
| 3182 | |
| 3183 | rc = role_allow_write(p->role_allow, fp); |
| 3184 | if (rc) |
| 3185 | return rc; |
| 3186 | |
| 3187 | rc = ocontext_write(p, info, fp); |
| 3188 | if (rc) |
| 3189 | return rc; |
| 3190 | |
| 3191 | rc = genfs_write(p, fp); |
| 3192 | if (rc) |
| 3193 | return rc; |
| 3194 | |
| 3195 | rc = range_write(p, fp); |
| 3196 | if (rc) |
| 3197 | return rc; |
| 3198 | |
| 3199 | for (i = 0; i < p->p_types.nprim; i++) { |
| 3200 | struct ebitmap *e = flex_array_get(p->type_attr_map_array, i); |
| 3201 | |
| 3202 | BUG_ON(!e); |
| 3203 | rc = ebitmap_write(e, fp); |
| 3204 | if (rc) |
| 3205 | return rc; |
| 3206 | } |
| 3207 | |
| 3208 | return 0; |
| 3209 | } |