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