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