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