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