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