PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/security/selinux/ss/policydb.c

https://github.com/mstsirkin/kvm
C | 2435 lines | 1974 code | 362 blank | 99 comment | 368 complexity | bac0f9ae1f1f5c5096f1b48828d8b6ab MD5 | raw file
  1. /*
  2. * Implementation of the policy database.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /*
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. *
  11. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  12. *
  13. * Added conditional policy language extensions
  14. *
  15. * Updated: Hewlett-Packard <paul@paul-moore.com>
  16. *
  17. * Added support for the policy capability bitmap
  18. *
  19. * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  20. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  21. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation, version 2.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/errno.h>
  31. #include <linux/audit.h>
  32. #include <linux/flex_array.h>
  33. #include "security.h"
  34. #include "policydb.h"
  35. #include "conditional.h"
  36. #include "mls.h"
  37. #include "services.h"
  38. #define _DEBUG_HASHES
  39. #ifdef DEBUG_HASHES
  40. static const char *symtab_name[SYM_NUM] = {
  41. "common prefixes",
  42. "classes",
  43. "roles",
  44. "types",
  45. "users",
  46. "bools",
  47. "levels",
  48. "categories",
  49. };
  50. #endif
  51. static unsigned int symtab_sizes[SYM_NUM] = {
  52. 2,
  53. 32,
  54. 16,
  55. 512,
  56. 128,
  57. 16,
  58. 16,
  59. 16,
  60. };
  61. struct policydb_compat_info {
  62. int version;
  63. int sym_num;
  64. int ocon_num;
  65. };
  66. /* These need to be updated if SYM_NUM or OCON_NUM changes */
  67. static struct policydb_compat_info policydb_compat[] = {
  68. {
  69. .version = POLICYDB_VERSION_BASE,
  70. .sym_num = SYM_NUM - 3,
  71. .ocon_num = OCON_NUM - 1,
  72. },
  73. {
  74. .version = POLICYDB_VERSION_BOOL,
  75. .sym_num = SYM_NUM - 2,
  76. .ocon_num = OCON_NUM - 1,
  77. },
  78. {
  79. .version = POLICYDB_VERSION_IPV6,
  80. .sym_num = SYM_NUM - 2,
  81. .ocon_num = OCON_NUM,
  82. },
  83. {
  84. .version = POLICYDB_VERSION_NLCLASS,
  85. .sym_num = SYM_NUM - 2,
  86. .ocon_num = OCON_NUM,
  87. },
  88. {
  89. .version = POLICYDB_VERSION_MLS,
  90. .sym_num = SYM_NUM,
  91. .ocon_num = OCON_NUM,
  92. },
  93. {
  94. .version = POLICYDB_VERSION_AVTAB,
  95. .sym_num = SYM_NUM,
  96. .ocon_num = OCON_NUM,
  97. },
  98. {
  99. .version = POLICYDB_VERSION_RANGETRANS,
  100. .sym_num = SYM_NUM,
  101. .ocon_num = OCON_NUM,
  102. },
  103. {
  104. .version = POLICYDB_VERSION_POLCAP,
  105. .sym_num = SYM_NUM,
  106. .ocon_num = OCON_NUM,
  107. },
  108. {
  109. .version = POLICYDB_VERSION_PERMISSIVE,
  110. .sym_num = SYM_NUM,
  111. .ocon_num = OCON_NUM,
  112. },
  113. {
  114. .version = POLICYDB_VERSION_BOUNDARY,
  115. .sym_num = SYM_NUM,
  116. .ocon_num = OCON_NUM,
  117. },
  118. {
  119. .version = POLICYDB_VERSION_FILENAME_TRANS,
  120. .sym_num = SYM_NUM,
  121. .ocon_num = OCON_NUM,
  122. },
  123. {
  124. .version = POLICYDB_VERSION_ROLETRANS,
  125. .sym_num = SYM_NUM,
  126. .ocon_num = OCON_NUM,
  127. },
  128. };
  129. static struct policydb_compat_info *policydb_lookup_compat(int version)
  130. {
  131. int i;
  132. struct policydb_compat_info *info = NULL;
  133. for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
  134. if (policydb_compat[i].version == version) {
  135. info = &policydb_compat[i];
  136. break;
  137. }
  138. }
  139. return info;
  140. }
  141. /*
  142. * Initialize the role table.
  143. */
  144. static int roles_init(struct policydb *p)
  145. {
  146. char *key = NULL;
  147. int rc;
  148. struct role_datum *role;
  149. rc = -ENOMEM;
  150. role = kzalloc(sizeof(*role), GFP_KERNEL);
  151. if (!role)
  152. goto out;
  153. rc = -EINVAL;
  154. role->value = ++p->p_roles.nprim;
  155. if (role->value != OBJECT_R_VAL)
  156. goto out;
  157. rc = -ENOMEM;
  158. key = kstrdup(OBJECT_R, GFP_KERNEL);
  159. if (!key)
  160. goto out;
  161. rc = hashtab_insert(p->p_roles.table, key, role);
  162. if (rc)
  163. goto out;
  164. return 0;
  165. out:
  166. kfree(key);
  167. kfree(role);
  168. return rc;
  169. }
  170. static u32 filenametr_hash(struct hashtab *h, const void *k)
  171. {
  172. const struct filename_trans *ft = k;
  173. unsigned long hash;
  174. unsigned int byte_num;
  175. unsigned char focus;
  176. hash = ft->stype ^ ft->ttype ^ ft->tclass;
  177. byte_num = 0;
  178. while ((focus = ft->name[byte_num++]))
  179. hash = partial_name_hash(focus, hash);
  180. return hash & (h->size - 1);
  181. }
  182. static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
  183. {
  184. const struct filename_trans *ft1 = k1;
  185. const struct filename_trans *ft2 = k2;
  186. int v;
  187. v = ft1->stype - ft2->stype;
  188. if (v)
  189. return v;
  190. v = ft1->ttype - ft2->ttype;
  191. if (v)
  192. return v;
  193. v = ft1->tclass - ft2->tclass;
  194. if (v)
  195. return v;
  196. return strcmp(ft1->name, ft2->name);
  197. }
  198. static u32 rangetr_hash(struct hashtab *h, const void *k)
  199. {
  200. const struct range_trans *key = k;
  201. return (key->source_type + (key->target_type << 3) +
  202. (key->target_class << 5)) & (h->size - 1);
  203. }
  204. static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  205. {
  206. const struct range_trans *key1 = k1, *key2 = k2;
  207. int v;
  208. v = key1->source_type - key2->source_type;
  209. if (v)
  210. return v;
  211. v = key1->target_type - key2->target_type;
  212. if (v)
  213. return v;
  214. v = key1->target_class - key2->target_class;
  215. return v;
  216. }
  217. /*
  218. * Initialize a policy database structure.
  219. */
  220. static int policydb_init(struct policydb *p)
  221. {
  222. int i, rc;
  223. memset(p, 0, sizeof(*p));
  224. for (i = 0; i < SYM_NUM; i++) {
  225. rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
  226. if (rc)
  227. goto out;
  228. }
  229. rc = avtab_init(&p->te_avtab);
  230. if (rc)
  231. goto out;
  232. rc = roles_init(p);
  233. if (rc)
  234. goto out;
  235. rc = cond_policydb_init(p);
  236. if (rc)
  237. goto out;
  238. p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
  239. if (!p->filename_trans)
  240. goto out;
  241. p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
  242. if (!p->range_tr)
  243. goto out;
  244. ebitmap_init(&p->filename_trans_ttypes);
  245. ebitmap_init(&p->policycaps);
  246. ebitmap_init(&p->permissive_map);
  247. return 0;
  248. out:
  249. hashtab_destroy(p->filename_trans);
  250. hashtab_destroy(p->range_tr);
  251. for (i = 0; i < SYM_NUM; i++)
  252. hashtab_destroy(p->symtab[i].table);
  253. return rc;
  254. }
  255. /*
  256. * The following *_index functions are used to
  257. * define the val_to_name and val_to_struct arrays
  258. * in a policy database structure. The val_to_name
  259. * arrays are used when converting security context
  260. * structures into string representations. The
  261. * val_to_struct arrays are used when the attributes
  262. * of a class, role, or user are needed.
  263. */
  264. static int common_index(void *key, void *datum, void *datap)
  265. {
  266. struct policydb *p;
  267. struct common_datum *comdatum;
  268. struct flex_array *fa;
  269. comdatum = datum;
  270. p = datap;
  271. if (!comdatum->value || comdatum->value > p->p_commons.nprim)
  272. return -EINVAL;
  273. fa = p->sym_val_to_name[SYM_COMMONS];
  274. if (flex_array_put_ptr(fa, comdatum->value - 1, key,
  275. GFP_KERNEL | __GFP_ZERO))
  276. BUG();
  277. return 0;
  278. }
  279. static int class_index(void *key, void *datum, void *datap)
  280. {
  281. struct policydb *p;
  282. struct class_datum *cladatum;
  283. struct flex_array *fa;
  284. cladatum = datum;
  285. p = datap;
  286. if (!cladatum->value || cladatum->value > p->p_classes.nprim)
  287. return -EINVAL;
  288. fa = p->sym_val_to_name[SYM_CLASSES];
  289. if (flex_array_put_ptr(fa, cladatum->value - 1, key,
  290. GFP_KERNEL | __GFP_ZERO))
  291. BUG();
  292. p->class_val_to_struct[cladatum->value - 1] = cladatum;
  293. return 0;
  294. }
  295. static int role_index(void *key, void *datum, void *datap)
  296. {
  297. struct policydb *p;
  298. struct role_datum *role;
  299. struct flex_array *fa;
  300. role = datum;
  301. p = datap;
  302. if (!role->value
  303. || role->value > p->p_roles.nprim
  304. || role->bounds > p->p_roles.nprim)
  305. return -EINVAL;
  306. fa = p->sym_val_to_name[SYM_ROLES];
  307. if (flex_array_put_ptr(fa, role->value - 1, key,
  308. GFP_KERNEL | __GFP_ZERO))
  309. BUG();
  310. p->role_val_to_struct[role->value - 1] = role;
  311. return 0;
  312. }
  313. static int type_index(void *key, void *datum, void *datap)
  314. {
  315. struct policydb *p;
  316. struct type_datum *typdatum;
  317. struct flex_array *fa;
  318. typdatum = datum;
  319. p = datap;
  320. if (typdatum->primary) {
  321. if (!typdatum->value
  322. || typdatum->value > p->p_types.nprim
  323. || typdatum->bounds > p->p_types.nprim)
  324. return -EINVAL;
  325. fa = p->sym_val_to_name[SYM_TYPES];
  326. if (flex_array_put_ptr(fa, typdatum->value - 1, key,
  327. GFP_KERNEL | __GFP_ZERO))
  328. BUG();
  329. fa = p->type_val_to_struct_array;
  330. if (flex_array_put_ptr(fa, typdatum->value - 1, typdatum,
  331. GFP_KERNEL | __GFP_ZERO))
  332. BUG();
  333. }
  334. return 0;
  335. }
  336. static int user_index(void *key, void *datum, void *datap)
  337. {
  338. struct policydb *p;
  339. struct user_datum *usrdatum;
  340. struct flex_array *fa;
  341. usrdatum = datum;
  342. p = datap;
  343. if (!usrdatum->value
  344. || usrdatum->value > p->p_users.nprim
  345. || usrdatum->bounds > p->p_users.nprim)
  346. return -EINVAL;
  347. fa = p->sym_val_to_name[SYM_USERS];
  348. if (flex_array_put_ptr(fa, usrdatum->value - 1, key,
  349. GFP_KERNEL | __GFP_ZERO))
  350. BUG();
  351. p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
  352. return 0;
  353. }
  354. static int sens_index(void *key, void *datum, void *datap)
  355. {
  356. struct policydb *p;
  357. struct level_datum *levdatum;
  358. struct flex_array *fa;
  359. levdatum = datum;
  360. p = datap;
  361. if (!levdatum->isalias) {
  362. if (!levdatum->level->sens ||
  363. levdatum->level->sens > p->p_levels.nprim)
  364. return -EINVAL;
  365. fa = p->sym_val_to_name[SYM_LEVELS];
  366. if (flex_array_put_ptr(fa, levdatum->level->sens - 1, key,
  367. GFP_KERNEL | __GFP_ZERO))
  368. BUG();
  369. }
  370. return 0;
  371. }
  372. static int cat_index(void *key, void *datum, void *datap)
  373. {
  374. struct policydb *p;
  375. struct cat_datum *catdatum;
  376. struct flex_array *fa;
  377. catdatum = datum;
  378. p = datap;
  379. if (!catdatum->isalias) {
  380. if (!catdatum->value || catdatum->value > p->p_cats.nprim)
  381. return -EINVAL;
  382. fa = p->sym_val_to_name[SYM_CATS];
  383. if (flex_array_put_ptr(fa, catdatum->value - 1, key,
  384. GFP_KERNEL | __GFP_ZERO))
  385. BUG();
  386. }
  387. return 0;
  388. }
  389. static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  390. {
  391. common_index,
  392. class_index,
  393. role_index,
  394. type_index,
  395. user_index,
  396. cond_index_bool,
  397. sens_index,
  398. cat_index,
  399. };
  400. #ifdef DEBUG_HASHES
  401. static void hash_eval(struct hashtab *h, const char *hash_name)
  402. {
  403. struct hashtab_info info;
  404. hashtab_stat(h, &info);
  405. printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
  406. "longest chain length %d\n", hash_name, h->nel,
  407. info.slots_used, h->size, info.max_chain_len);
  408. }
  409. static void symtab_hash_eval(struct symtab *s)
  410. {
  411. int i;
  412. for (i = 0; i < SYM_NUM; i++)
  413. hash_eval(s[i].table, symtab_name[i]);
  414. }
  415. #else
  416. static inline void hash_eval(struct hashtab *h, char *hash_name)
  417. {
  418. }
  419. #endif
  420. /*
  421. * Define the other val_to_name and val_to_struct arrays
  422. * in a policy database structure.
  423. *
  424. * Caller must clean up on failure.
  425. */
  426. static int policydb_index(struct policydb *p)
  427. {
  428. int i, rc;
  429. printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
  430. p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
  431. if (p->mls_enabled)
  432. printk(", %d sens, %d cats", p->p_levels.nprim,
  433. p->p_cats.nprim);
  434. printk("\n");
  435. printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
  436. p->p_classes.nprim, p->te_avtab.nel);
  437. #ifdef DEBUG_HASHES
  438. avtab_hash_eval(&p->te_avtab, "rules");
  439. symtab_hash_eval(p->symtab);
  440. #endif
  441. rc = -ENOMEM;
  442. p->class_val_to_struct =
  443. kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
  444. GFP_KERNEL);
  445. if (!p->class_val_to_struct)
  446. goto out;
  447. rc = -ENOMEM;
  448. p->role_val_to_struct =
  449. kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
  450. GFP_KERNEL);
  451. if (!p->role_val_to_struct)
  452. goto out;
  453. rc = -ENOMEM;
  454. p->user_val_to_struct =
  455. kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
  456. GFP_KERNEL);
  457. if (!p->user_val_to_struct)
  458. goto out;
  459. /* Yes, I want the sizeof the pointer, not the structure */
  460. rc = -ENOMEM;
  461. p->type_val_to_struct_array = flex_array_alloc(sizeof(struct type_datum *),
  462. p->p_types.nprim,
  463. GFP_KERNEL | __GFP_ZERO);
  464. if (!p->type_val_to_struct_array)
  465. goto out;
  466. rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
  467. p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
  468. if (rc)
  469. goto out;
  470. rc = cond_init_bool_indexes(p);
  471. if (rc)
  472. goto out;
  473. for (i = 0; i < SYM_NUM; i++) {
  474. rc = -ENOMEM;
  475. p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *),
  476. p->symtab[i].nprim,
  477. GFP_KERNEL | __GFP_ZERO);
  478. if (!p->sym_val_to_name[i])
  479. goto out;
  480. rc = flex_array_prealloc(p->sym_val_to_name[i],
  481. 0, p->symtab[i].nprim,
  482. GFP_KERNEL | __GFP_ZERO);
  483. if (rc)
  484. goto out;
  485. rc = hashtab_map(p->symtab[i].table, index_f[i], p);
  486. if (rc)
  487. goto out;
  488. }
  489. rc = 0;
  490. out:
  491. return rc;
  492. }
  493. /*
  494. * The following *_destroy functions are used to
  495. * free any memory allocated for each kind of
  496. * symbol data in the policy database.
  497. */
  498. static int perm_destroy(void *key, void *datum, void *p)
  499. {
  500. kfree(key);
  501. kfree(datum);
  502. return 0;
  503. }
  504. static int common_destroy(void *key, void *datum, void *p)
  505. {
  506. struct common_datum *comdatum;
  507. kfree(key);
  508. if (datum) {
  509. comdatum = datum;
  510. hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
  511. hashtab_destroy(comdatum->permissions.table);
  512. }
  513. kfree(datum);
  514. return 0;
  515. }
  516. static int cls_destroy(void *key, void *datum, void *p)
  517. {
  518. struct class_datum *cladatum;
  519. struct constraint_node *constraint, *ctemp;
  520. struct constraint_expr *e, *etmp;
  521. kfree(key);
  522. if (datum) {
  523. cladatum = datum;
  524. hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
  525. hashtab_destroy(cladatum->permissions.table);
  526. constraint = cladatum->constraints;
  527. while (constraint) {
  528. e = constraint->expr;
  529. while (e) {
  530. ebitmap_destroy(&e->names);
  531. etmp = e;
  532. e = e->next;
  533. kfree(etmp);
  534. }
  535. ctemp = constraint;
  536. constraint = constraint->next;
  537. kfree(ctemp);
  538. }
  539. constraint = cladatum->validatetrans;
  540. while (constraint) {
  541. e = constraint->expr;
  542. while (e) {
  543. ebitmap_destroy(&e->names);
  544. etmp = e;
  545. e = e->next;
  546. kfree(etmp);
  547. }
  548. ctemp = constraint;
  549. constraint = constraint->next;
  550. kfree(ctemp);
  551. }
  552. kfree(cladatum->comkey);
  553. }
  554. kfree(datum);
  555. return 0;
  556. }
  557. static int role_destroy(void *key, void *datum, void *p)
  558. {
  559. struct role_datum *role;
  560. kfree(key);
  561. if (datum) {
  562. role = datum;
  563. ebitmap_destroy(&role->dominates);
  564. ebitmap_destroy(&role->types);
  565. }
  566. kfree(datum);
  567. return 0;
  568. }
  569. static int type_destroy(void *key, void *datum, void *p)
  570. {
  571. kfree(key);
  572. kfree(datum);
  573. return 0;
  574. }
  575. static int user_destroy(void *key, void *datum, void *p)
  576. {
  577. struct user_datum *usrdatum;
  578. kfree(key);
  579. if (datum) {
  580. usrdatum = datum;
  581. ebitmap_destroy(&usrdatum->roles);
  582. ebitmap_destroy(&usrdatum->range.level[0].cat);
  583. ebitmap_destroy(&usrdatum->range.level[1].cat);
  584. ebitmap_destroy(&usrdatum->dfltlevel.cat);
  585. }
  586. kfree(datum);
  587. return 0;
  588. }
  589. static int sens_destroy(void *key, void *datum, void *p)
  590. {
  591. struct level_datum *levdatum;
  592. kfree(key);
  593. if (datum) {
  594. levdatum = datum;
  595. ebitmap_destroy(&levdatum->level->cat);
  596. kfree(levdatum->level);
  597. }
  598. kfree(datum);
  599. return 0;
  600. }
  601. static int cat_destroy(void *key, void *datum, void *p)
  602. {
  603. kfree(key);
  604. kfree(datum);
  605. return 0;
  606. }
  607. static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  608. {
  609. common_destroy,
  610. cls_destroy,
  611. role_destroy,
  612. type_destroy,
  613. user_destroy,
  614. cond_destroy_bool,
  615. sens_destroy,
  616. cat_destroy,
  617. };
  618. static int filenametr_destroy(void *key, void *datum, void *p)
  619. {
  620. struct filename_trans *ft = key;
  621. kfree(ft->name);
  622. kfree(key);
  623. kfree(datum);
  624. cond_resched();
  625. return 0;
  626. }
  627. static int range_tr_destroy(void *key, void *datum, void *p)
  628. {
  629. struct mls_range *rt = datum;
  630. kfree(key);
  631. ebitmap_destroy(&rt->level[0].cat);
  632. ebitmap_destroy(&rt->level[1].cat);
  633. kfree(datum);
  634. cond_resched();
  635. return 0;
  636. }
  637. static void ocontext_destroy(struct ocontext *c, int i)
  638. {
  639. if (!c)
  640. return;
  641. context_destroy(&c->context[0]);
  642. context_destroy(&c->context[1]);
  643. if (i == OCON_ISID || i == OCON_FS ||
  644. i == OCON_NETIF || i == OCON_FSUSE)
  645. kfree(c->u.name);
  646. kfree(c);
  647. }
  648. /*
  649. * Free any memory allocated by a policy database structure.
  650. */
  651. void policydb_destroy(struct policydb *p)
  652. {
  653. struct ocontext *c, *ctmp;
  654. struct genfs *g, *gtmp;
  655. int i;
  656. struct role_allow *ra, *lra = NULL;
  657. struct role_trans *tr, *ltr = NULL;
  658. for (i = 0; i < SYM_NUM; i++) {
  659. cond_resched();
  660. hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
  661. hashtab_destroy(p->symtab[i].table);
  662. }
  663. for (i = 0; i < SYM_NUM; i++) {
  664. if (p->sym_val_to_name[i])
  665. flex_array_free(p->sym_val_to_name[i]);
  666. }
  667. kfree(p->class_val_to_struct);
  668. kfree(p->role_val_to_struct);
  669. kfree(p->user_val_to_struct);
  670. if (p->type_val_to_struct_array)
  671. flex_array_free(p->type_val_to_struct_array);
  672. avtab_destroy(&p->te_avtab);
  673. for (i = 0; i < OCON_NUM; i++) {
  674. cond_resched();
  675. c = p->ocontexts[i];
  676. while (c) {
  677. ctmp = c;
  678. c = c->next;
  679. ocontext_destroy(ctmp, i);
  680. }
  681. p->ocontexts[i] = NULL;
  682. }
  683. g = p->genfs;
  684. while (g) {
  685. cond_resched();
  686. kfree(g->fstype);
  687. c = g->head;
  688. while (c) {
  689. ctmp = c;
  690. c = c->next;
  691. ocontext_destroy(ctmp, OCON_FSUSE);
  692. }
  693. gtmp = g;
  694. g = g->next;
  695. kfree(gtmp);
  696. }
  697. p->genfs = NULL;
  698. cond_policydb_destroy(p);
  699. for (tr = p->role_tr; tr; tr = tr->next) {
  700. cond_resched();
  701. kfree(ltr);
  702. ltr = tr;
  703. }
  704. kfree(ltr);
  705. for (ra = p->role_allow; ra; ra = ra->next) {
  706. cond_resched();
  707. kfree(lra);
  708. lra = ra;
  709. }
  710. kfree(lra);
  711. hashtab_map(p->filename_trans, filenametr_destroy, NULL);
  712. hashtab_destroy(p->filename_trans);
  713. hashtab_map(p->range_tr, range_tr_destroy, NULL);
  714. hashtab_destroy(p->range_tr);
  715. if (p->type_attr_map_array) {
  716. for (i = 0; i < p->p_types.nprim; i++) {
  717. struct ebitmap *e;
  718. e = flex_array_get(p->type_attr_map_array, i);
  719. if (!e)
  720. continue;
  721. ebitmap_destroy(e);
  722. }
  723. flex_array_free(p->type_attr_map_array);
  724. }
  725. ebitmap_destroy(&p->filename_trans_ttypes);
  726. ebitmap_destroy(&p->policycaps);
  727. ebitmap_destroy(&p->permissive_map);
  728. return;
  729. }
  730. /*
  731. * Load the initial SIDs specified in a policy database
  732. * structure into a SID table.
  733. */
  734. int policydb_load_isids(struct policydb *p, struct sidtab *s)
  735. {
  736. struct ocontext *head, *c;
  737. int rc;
  738. rc = sidtab_init(s);
  739. if (rc) {
  740. printk(KERN_ERR "SELinux: out of memory on SID table init\n");
  741. goto out;
  742. }
  743. head = p->ocontexts[OCON_ISID];
  744. for (c = head; c; c = c->next) {
  745. rc = -EINVAL;
  746. if (!c->context[0].user) {
  747. printk(KERN_ERR "SELinux: SID %s was never defined.\n",
  748. c->u.name);
  749. goto out;
  750. }
  751. rc = sidtab_insert(s, c->sid[0], &c->context[0]);
  752. if (rc) {
  753. printk(KERN_ERR "SELinux: unable to load initial SID %s.\n",
  754. c->u.name);
  755. goto out;
  756. }
  757. }
  758. rc = 0;
  759. out:
  760. return rc;
  761. }
  762. int policydb_class_isvalid(struct policydb *p, unsigned int class)
  763. {
  764. if (!class || class > p->p_classes.nprim)
  765. return 0;
  766. return 1;
  767. }
  768. int policydb_role_isvalid(struct policydb *p, unsigned int role)
  769. {
  770. if (!role || role > p->p_roles.nprim)
  771. return 0;
  772. return 1;
  773. }
  774. int policydb_type_isvalid(struct policydb *p, unsigned int type)
  775. {
  776. if (!type || type > p->p_types.nprim)
  777. return 0;
  778. return 1;
  779. }
  780. /*
  781. * Return 1 if the fields in the security context
  782. * structure `c' are valid. Return 0 otherwise.
  783. */
  784. int policydb_context_isvalid(struct policydb *p, struct context *c)
  785. {
  786. struct role_datum *role;
  787. struct user_datum *usrdatum;
  788. if (!c->role || c->role > p->p_roles.nprim)
  789. return 0;
  790. if (!c->user || c->user > p->p_users.nprim)
  791. return 0;
  792. if (!c->type || c->type > p->p_types.nprim)
  793. return 0;
  794. if (c->role != OBJECT_R_VAL) {
  795. /*
  796. * Role must be authorized for the type.
  797. */
  798. role = p->role_val_to_struct[c->role - 1];
  799. if (!ebitmap_get_bit(&role->types, c->type - 1))
  800. /* role may not be associated with type */
  801. return 0;
  802. /*
  803. * User must be authorized for the role.
  804. */
  805. usrdatum = p->user_val_to_struct[c->user - 1];
  806. if (!usrdatum)
  807. return 0;
  808. if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
  809. /* user may not be associated with role */
  810. return 0;
  811. }
  812. if (!mls_context_isvalid(p, c))
  813. return 0;
  814. return 1;
  815. }
  816. /*
  817. * Read a MLS range structure from a policydb binary
  818. * representation file.
  819. */
  820. static int mls_read_range_helper(struct mls_range *r, void *fp)
  821. {
  822. __le32 buf[2];
  823. u32 items;
  824. int rc;
  825. rc = next_entry(buf, fp, sizeof(u32));
  826. if (rc)
  827. goto out;
  828. rc = -EINVAL;
  829. items = le32_to_cpu(buf[0]);
  830. if (items > ARRAY_SIZE(buf)) {
  831. printk(KERN_ERR "SELinux: mls: range overflow\n");
  832. goto out;
  833. }
  834. rc = next_entry(buf, fp, sizeof(u32) * items);
  835. if (rc) {
  836. printk(KERN_ERR "SELinux: mls: truncated range\n");
  837. goto out;
  838. }
  839. r->level[0].sens = le32_to_cpu(buf[0]);
  840. if (items > 1)
  841. r->level[1].sens = le32_to_cpu(buf[1]);
  842. else
  843. r->level[1].sens = r->level[0].sens;
  844. rc = ebitmap_read(&r->level[0].cat, fp);
  845. if (rc) {
  846. printk(KERN_ERR "SELinux: mls: error reading low categories\n");
  847. goto out;
  848. }
  849. if (items > 1) {
  850. rc = ebitmap_read(&r->level[1].cat, fp);
  851. if (rc) {
  852. printk(KERN_ERR "SELinux: mls: error reading high categories\n");
  853. goto bad_high;
  854. }
  855. } else {
  856. rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
  857. if (rc) {
  858. printk(KERN_ERR "SELinux: mls: out of memory\n");
  859. goto bad_high;
  860. }
  861. }
  862. return 0;
  863. bad_high:
  864. ebitmap_destroy(&r->level[0].cat);
  865. out:
  866. return rc;
  867. }
  868. /*
  869. * Read and validate a security context structure
  870. * from a policydb binary representation file.
  871. */
  872. static int context_read_and_validate(struct context *c,
  873. struct policydb *p,
  874. void *fp)
  875. {
  876. __le32 buf[3];
  877. int rc;
  878. rc = next_entry(buf, fp, sizeof buf);
  879. if (rc) {
  880. printk(KERN_ERR "SELinux: context truncated\n");
  881. goto out;
  882. }
  883. c->user = le32_to_cpu(buf[0]);
  884. c->role = le32_to_cpu(buf[1]);
  885. c->type = le32_to_cpu(buf[2]);
  886. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  887. rc = mls_read_range_helper(&c->range, fp);
  888. if (rc) {
  889. printk(KERN_ERR "SELinux: error reading MLS range of context\n");
  890. goto out;
  891. }
  892. }
  893. rc = -EINVAL;
  894. if (!policydb_context_isvalid(p, c)) {
  895. printk(KERN_ERR "SELinux: invalid security context\n");
  896. context_destroy(c);
  897. goto out;
  898. }
  899. rc = 0;
  900. out:
  901. return rc;
  902. }
  903. /*
  904. * The following *_read functions are used to
  905. * read the symbol data from a policy database
  906. * binary representation file.
  907. */
  908. static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
  909. {
  910. char *key = NULL;
  911. struct perm_datum *perdatum;
  912. int rc;
  913. __le32 buf[2];
  914. u32 len;
  915. rc = -ENOMEM;
  916. perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
  917. if (!perdatum)
  918. goto bad;
  919. rc = next_entry(buf, fp, sizeof buf);
  920. if (rc)
  921. goto bad;
  922. len = le32_to_cpu(buf[0]);
  923. perdatum->value = le32_to_cpu(buf[1]);
  924. rc = -ENOMEM;
  925. key = kmalloc(len + 1, GFP_KERNEL);
  926. if (!key)
  927. goto bad;
  928. rc = next_entry(key, fp, len);
  929. if (rc)
  930. goto bad;
  931. key[len] = '\0';
  932. rc = hashtab_insert(h, key, perdatum);
  933. if (rc)
  934. goto bad;
  935. return 0;
  936. bad:
  937. perm_destroy(key, perdatum, NULL);
  938. return rc;
  939. }
  940. static int common_read(struct policydb *p, struct hashtab *h, void *fp)
  941. {
  942. char *key = NULL;
  943. struct common_datum *comdatum;
  944. __le32 buf[4];
  945. u32 len, nel;
  946. int i, rc;
  947. rc = -ENOMEM;
  948. comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
  949. if (!comdatum)
  950. goto bad;
  951. rc = next_entry(buf, fp, sizeof buf);
  952. if (rc)
  953. goto bad;
  954. len = le32_to_cpu(buf[0]);
  955. comdatum->value = le32_to_cpu(buf[1]);
  956. rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
  957. if (rc)
  958. goto bad;
  959. comdatum->permissions.nprim = le32_to_cpu(buf[2]);
  960. nel = le32_to_cpu(buf[3]);
  961. rc = -ENOMEM;
  962. key = kmalloc(len + 1, GFP_KERNEL);
  963. if (!key)
  964. goto bad;
  965. rc = next_entry(key, fp, len);
  966. if (rc)
  967. goto bad;
  968. key[len] = '\0';
  969. for (i = 0; i < nel; i++) {
  970. rc = perm_read(p, comdatum->permissions.table, fp);
  971. if (rc)
  972. goto bad;
  973. }
  974. rc = hashtab_insert(h, key, comdatum);
  975. if (rc)
  976. goto bad;
  977. return 0;
  978. bad:
  979. common_destroy(key, comdatum, NULL);
  980. return rc;
  981. }
  982. static int read_cons_helper(struct constraint_node **nodep, int ncons,
  983. int allowxtarget, void *fp)
  984. {
  985. struct constraint_node *c, *lc;
  986. struct constraint_expr *e, *le;
  987. __le32 buf[3];
  988. u32 nexpr;
  989. int rc, i, j, depth;
  990. lc = NULL;
  991. for (i = 0; i < ncons; i++) {
  992. c = kzalloc(sizeof(*c), GFP_KERNEL);
  993. if (!c)
  994. return -ENOMEM;
  995. if (lc)
  996. lc->next = c;
  997. else
  998. *nodep = c;
  999. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1000. if (rc)
  1001. return rc;
  1002. c->permissions = le32_to_cpu(buf[0]);
  1003. nexpr = le32_to_cpu(buf[1]);
  1004. le = NULL;
  1005. depth = -1;
  1006. for (j = 0; j < nexpr; j++) {
  1007. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1008. if (!e)
  1009. return -ENOMEM;
  1010. if (le)
  1011. le->next = e;
  1012. else
  1013. c->expr = e;
  1014. rc = next_entry(buf, fp, (sizeof(u32) * 3));
  1015. if (rc)
  1016. return rc;
  1017. e->expr_type = le32_to_cpu(buf[0]);
  1018. e->attr = le32_to_cpu(buf[1]);
  1019. e->op = le32_to_cpu(buf[2]);
  1020. switch (e->expr_type) {
  1021. case CEXPR_NOT:
  1022. if (depth < 0)
  1023. return -EINVAL;
  1024. break;
  1025. case CEXPR_AND:
  1026. case CEXPR_OR:
  1027. if (depth < 1)
  1028. return -EINVAL;
  1029. depth--;
  1030. break;
  1031. case CEXPR_ATTR:
  1032. if (depth == (CEXPR_MAXDEPTH - 1))
  1033. return -EINVAL;
  1034. depth++;
  1035. break;
  1036. case CEXPR_NAMES:
  1037. if (!allowxtarget && (e->attr & CEXPR_XTARGET))
  1038. return -EINVAL;
  1039. if (depth == (CEXPR_MAXDEPTH - 1))
  1040. return -EINVAL;
  1041. depth++;
  1042. rc = ebitmap_read(&e->names, fp);
  1043. if (rc)
  1044. return rc;
  1045. break;
  1046. default:
  1047. return -EINVAL;
  1048. }
  1049. le = e;
  1050. }
  1051. if (depth != 0)
  1052. return -EINVAL;
  1053. lc = c;
  1054. }
  1055. return 0;
  1056. }
  1057. static int class_read(struct policydb *p, struct hashtab *h, void *fp)
  1058. {
  1059. char *key = NULL;
  1060. struct class_datum *cladatum;
  1061. __le32 buf[6];
  1062. u32 len, len2, ncons, nel;
  1063. int i, rc;
  1064. rc = -ENOMEM;
  1065. cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
  1066. if (!cladatum)
  1067. goto bad;
  1068. rc = next_entry(buf, fp, sizeof(u32)*6);
  1069. if (rc)
  1070. goto bad;
  1071. len = le32_to_cpu(buf[0]);
  1072. len2 = le32_to_cpu(buf[1]);
  1073. cladatum->value = le32_to_cpu(buf[2]);
  1074. rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
  1075. if (rc)
  1076. goto bad;
  1077. cladatum->permissions.nprim = le32_to_cpu(buf[3]);
  1078. nel = le32_to_cpu(buf[4]);
  1079. ncons = le32_to_cpu(buf[5]);
  1080. rc = -ENOMEM;
  1081. key = kmalloc(len + 1, GFP_KERNEL);
  1082. if (!key)
  1083. goto bad;
  1084. rc = next_entry(key, fp, len);
  1085. if (rc)
  1086. goto bad;
  1087. key[len] = '\0';
  1088. if (len2) {
  1089. rc = -ENOMEM;
  1090. cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
  1091. if (!cladatum->comkey)
  1092. goto bad;
  1093. rc = next_entry(cladatum->comkey, fp, len2);
  1094. if (rc)
  1095. goto bad;
  1096. cladatum->comkey[len2] = '\0';
  1097. rc = -EINVAL;
  1098. cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey);
  1099. if (!cladatum->comdatum) {
  1100. printk(KERN_ERR "SELinux: unknown common %s\n", cladatum->comkey);
  1101. goto bad;
  1102. }
  1103. }
  1104. for (i = 0; i < nel; i++) {
  1105. rc = perm_read(p, cladatum->permissions.table, fp);
  1106. if (rc)
  1107. goto bad;
  1108. }
  1109. rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
  1110. if (rc)
  1111. goto bad;
  1112. if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
  1113. /* grab the validatetrans rules */
  1114. rc = next_entry(buf, fp, sizeof(u32));
  1115. if (rc)
  1116. goto bad;
  1117. ncons = le32_to_cpu(buf[0]);
  1118. rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
  1119. if (rc)
  1120. goto bad;
  1121. }
  1122. rc = hashtab_insert(h, key, cladatum);
  1123. if (rc)
  1124. goto bad;
  1125. return 0;
  1126. bad:
  1127. cls_destroy(key, cladatum, NULL);
  1128. return rc;
  1129. }
  1130. static int role_read(struct policydb *p, struct hashtab *h, void *fp)
  1131. {
  1132. char *key = NULL;
  1133. struct role_datum *role;
  1134. int rc, to_read = 2;
  1135. __le32 buf[3];
  1136. u32 len;
  1137. rc = -ENOMEM;
  1138. role = kzalloc(sizeof(*role), GFP_KERNEL);
  1139. if (!role)
  1140. goto bad;
  1141. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1142. to_read = 3;
  1143. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1144. if (rc)
  1145. goto bad;
  1146. len = le32_to_cpu(buf[0]);
  1147. role->value = le32_to_cpu(buf[1]);
  1148. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1149. role->bounds = le32_to_cpu(buf[2]);
  1150. rc = -ENOMEM;
  1151. key = kmalloc(len + 1, GFP_KERNEL);
  1152. if (!key)
  1153. goto bad;
  1154. rc = next_entry(key, fp, len);
  1155. if (rc)
  1156. goto bad;
  1157. key[len] = '\0';
  1158. rc = ebitmap_read(&role->dominates, fp);
  1159. if (rc)
  1160. goto bad;
  1161. rc = ebitmap_read(&role->types, fp);
  1162. if (rc)
  1163. goto bad;
  1164. if (strcmp(key, OBJECT_R) == 0) {
  1165. rc = -EINVAL;
  1166. if (role->value != OBJECT_R_VAL) {
  1167. printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
  1168. OBJECT_R, role->value);
  1169. goto bad;
  1170. }
  1171. rc = 0;
  1172. goto bad;
  1173. }
  1174. rc = hashtab_insert(h, key, role);
  1175. if (rc)
  1176. goto bad;
  1177. return 0;
  1178. bad:
  1179. role_destroy(key, role, NULL);
  1180. return rc;
  1181. }
  1182. static int type_read(struct policydb *p, struct hashtab *h, void *fp)
  1183. {
  1184. char *key = NULL;
  1185. struct type_datum *typdatum;
  1186. int rc, to_read = 3;
  1187. __le32 buf[4];
  1188. u32 len;
  1189. rc = -ENOMEM;
  1190. typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
  1191. if (!typdatum)
  1192. goto bad;
  1193. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1194. to_read = 4;
  1195. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1196. if (rc)
  1197. goto bad;
  1198. len = le32_to_cpu(buf[0]);
  1199. typdatum->value = le32_to_cpu(buf[1]);
  1200. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
  1201. u32 prop = le32_to_cpu(buf[2]);
  1202. if (prop & TYPEDATUM_PROPERTY_PRIMARY)
  1203. typdatum->primary = 1;
  1204. if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
  1205. typdatum->attribute = 1;
  1206. typdatum->bounds = le32_to_cpu(buf[3]);
  1207. } else {
  1208. typdatum->primary = le32_to_cpu(buf[2]);
  1209. }
  1210. rc = -ENOMEM;
  1211. key = kmalloc(len + 1, GFP_KERNEL);
  1212. if (!key)
  1213. goto bad;
  1214. rc = next_entry(key, fp, len);
  1215. if (rc)
  1216. goto bad;
  1217. key[len] = '\0';
  1218. rc = hashtab_insert(h, key, typdatum);
  1219. if (rc)
  1220. goto bad;
  1221. return 0;
  1222. bad:
  1223. type_destroy(key, typdatum, NULL);
  1224. return rc;
  1225. }
  1226. /*
  1227. * Read a MLS level structure from a policydb binary
  1228. * representation file.
  1229. */
  1230. static int mls_read_level(struct mls_level *lp, void *fp)
  1231. {
  1232. __le32 buf[1];
  1233. int rc;
  1234. memset(lp, 0, sizeof(*lp));
  1235. rc = next_entry(buf, fp, sizeof buf);
  1236. if (rc) {
  1237. printk(KERN_ERR "SELinux: mls: truncated level\n");
  1238. return rc;
  1239. }
  1240. lp->sens = le32_to_cpu(buf[0]);
  1241. rc = ebitmap_read(&lp->cat, fp);
  1242. if (rc) {
  1243. printk(KERN_ERR "SELinux: mls: error reading level categories\n");
  1244. return rc;
  1245. }
  1246. return 0;
  1247. }
  1248. static int user_read(struct policydb *p, struct hashtab *h, void *fp)
  1249. {
  1250. char *key = NULL;
  1251. struct user_datum *usrdatum;
  1252. int rc, to_read = 2;
  1253. __le32 buf[3];
  1254. u32 len;
  1255. rc = -ENOMEM;
  1256. usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
  1257. if (!usrdatum)
  1258. goto bad;
  1259. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1260. to_read = 3;
  1261. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1262. if (rc)
  1263. goto bad;
  1264. len = le32_to_cpu(buf[0]);
  1265. usrdatum->value = le32_to_cpu(buf[1]);
  1266. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1267. usrdatum->bounds = le32_to_cpu(buf[2]);
  1268. rc = -ENOMEM;
  1269. key = kmalloc(len + 1, GFP_KERNEL);
  1270. if (!key)
  1271. goto bad;
  1272. rc = next_entry(key, fp, len);
  1273. if (rc)
  1274. goto bad;
  1275. key[len] = '\0';
  1276. rc = ebitmap_read(&usrdatum->roles, fp);
  1277. if (rc)
  1278. goto bad;
  1279. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1280. rc = mls_read_range_helper(&usrdatum->range, fp);
  1281. if (rc)
  1282. goto bad;
  1283. rc = mls_read_level(&usrdatum->dfltlevel, fp);
  1284. if (rc)
  1285. goto bad;
  1286. }
  1287. rc = hashtab_insert(h, key, usrdatum);
  1288. if (rc)
  1289. goto bad;
  1290. return 0;
  1291. bad:
  1292. user_destroy(key, usrdatum, NULL);
  1293. return rc;
  1294. }
  1295. static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
  1296. {
  1297. char *key = NULL;
  1298. struct level_datum *levdatum;
  1299. int rc;
  1300. __le32 buf[2];
  1301. u32 len;
  1302. rc = -ENOMEM;
  1303. levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
  1304. if (!levdatum)
  1305. goto bad;
  1306. rc = next_entry(buf, fp, sizeof buf);
  1307. if (rc)
  1308. goto bad;
  1309. len = le32_to_cpu(buf[0]);
  1310. levdatum->isalias = le32_to_cpu(buf[1]);
  1311. rc = -ENOMEM;
  1312. key = kmalloc(len + 1, GFP_ATOMIC);
  1313. if (!key)
  1314. goto bad;
  1315. rc = next_entry(key, fp, len);
  1316. if (rc)
  1317. goto bad;
  1318. key[len] = '\0';
  1319. rc = -ENOMEM;
  1320. levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
  1321. if (!levdatum->level)
  1322. goto bad;
  1323. rc = mls_read_level(levdatum->level, fp);
  1324. if (rc)
  1325. goto bad;
  1326. rc = hashtab_insert(h, key, levdatum);
  1327. if (rc)
  1328. goto bad;
  1329. return 0;
  1330. bad:
  1331. sens_destroy(key, levdatum, NULL);
  1332. return rc;
  1333. }
  1334. static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
  1335. {
  1336. char *key = NULL;
  1337. struct cat_datum *catdatum;
  1338. int rc;
  1339. __le32 buf[3];
  1340. u32 len;
  1341. rc = -ENOMEM;
  1342. catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
  1343. if (!catdatum)
  1344. goto bad;
  1345. rc = next_entry(buf, fp, sizeof buf);
  1346. if (rc)
  1347. goto bad;
  1348. len = le32_to_cpu(buf[0]);
  1349. catdatum->value = le32_to_cpu(buf[1]);
  1350. catdatum->isalias = le32_to_cpu(buf[2]);
  1351. rc = -ENOMEM;
  1352. key = kmalloc(len + 1, GFP_ATOMIC);
  1353. if (!key)
  1354. goto bad;
  1355. rc = next_entry(key, fp, len);
  1356. if (rc)
  1357. goto bad;
  1358. key[len] = '\0';
  1359. rc = hashtab_insert(h, key, catdatum);
  1360. if (rc)
  1361. goto bad;
  1362. return 0;
  1363. bad:
  1364. cat_destroy(key, catdatum, NULL);
  1365. return rc;
  1366. }
  1367. static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
  1368. {
  1369. common_read,
  1370. class_read,
  1371. role_read,
  1372. type_read,
  1373. user_read,
  1374. cond_read_bool,
  1375. sens_read,
  1376. cat_read,
  1377. };
  1378. static int user_bounds_sanity_check(void *key, void *datum, void *datap)
  1379. {
  1380. struct user_datum *upper, *user;
  1381. struct policydb *p = datap;
  1382. int depth = 0;
  1383. upper = user = datum;
  1384. while (upper->bounds) {
  1385. struct ebitmap_node *node;
  1386. unsigned long bit;
  1387. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1388. printk(KERN_ERR "SELinux: user %s: "
  1389. "too deep or looped boundary",
  1390. (char *) key);
  1391. return -EINVAL;
  1392. }
  1393. upper = p->user_val_to_struct[upper->bounds - 1];
  1394. ebitmap_for_each_positive_bit(&user->roles, node, bit) {
  1395. if (ebitmap_get_bit(&upper->roles, bit))
  1396. continue;
  1397. printk(KERN_ERR
  1398. "SELinux: boundary violated policy: "
  1399. "user=%s role=%s bounds=%s\n",
  1400. sym_name(p, SYM_USERS, user->value - 1),
  1401. sym_name(p, SYM_ROLES, bit),
  1402. sym_name(p, SYM_USERS, upper->value - 1));
  1403. return -EINVAL;
  1404. }
  1405. }
  1406. return 0;
  1407. }
  1408. static int role_bounds_sanity_check(void *key, void *datum, void *datap)
  1409. {
  1410. struct role_datum *upper, *role;
  1411. struct policydb *p = datap;
  1412. int depth = 0;
  1413. upper = role = datum;
  1414. while (upper->bounds) {
  1415. struct ebitmap_node *node;
  1416. unsigned long bit;
  1417. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1418. printk(KERN_ERR "SELinux: role %s: "
  1419. "too deep or looped bounds\n",
  1420. (char *) key);
  1421. return -EINVAL;
  1422. }
  1423. upper = p->role_val_to_struct[upper->bounds - 1];
  1424. ebitmap_for_each_positive_bit(&role->types, node, bit) {
  1425. if (ebitmap_get_bit(&upper->types, bit))
  1426. continue;
  1427. printk(KERN_ERR
  1428. "SELinux: boundary violated policy: "
  1429. "role=%s type=%s bounds=%s\n",
  1430. sym_name(p, SYM_ROLES, role->value - 1),
  1431. sym_name(p, SYM_TYPES, bit),
  1432. sym_name(p, SYM_ROLES, upper->value - 1));
  1433. return -EINVAL;
  1434. }
  1435. }
  1436. return 0;
  1437. }
  1438. static int type_bounds_sanity_check(void *key, void *datum, void *datap)
  1439. {
  1440. struct type_datum *upper;
  1441. struct policydb *p = datap;
  1442. int depth = 0;
  1443. upper = datum;
  1444. while (upper->bounds) {
  1445. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1446. printk(KERN_ERR "SELinux: type %s: "
  1447. "too deep or looped boundary\n",
  1448. (char *) key);
  1449. return -EINVAL;
  1450. }
  1451. upper = flex_array_get_ptr(p->type_val_to_struct_array,
  1452. upper->bounds - 1);
  1453. BUG_ON(!upper);
  1454. if (upper->attribute) {
  1455. printk(KERN_ERR "SELinux: type %s: "
  1456. "bounded by attribute %s",
  1457. (char *) key,
  1458. sym_name(p, SYM_TYPES, upper->value - 1));
  1459. return -EINVAL;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. static int policydb_bounds_sanity_check(struct policydb *p)
  1465. {
  1466. int rc;
  1467. if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
  1468. return 0;
  1469. rc = hashtab_map(p->p_users.table,
  1470. user_bounds_sanity_check, p);
  1471. if (rc)
  1472. return rc;
  1473. rc = hashtab_map(p->p_roles.table,
  1474. role_bounds_sanity_check, p);
  1475. if (rc)
  1476. return rc;
  1477. rc = hashtab_map(p->p_types.table,
  1478. type_bounds_sanity_check, p);
  1479. if (rc)
  1480. return rc;
  1481. return 0;
  1482. }
  1483. extern int ss_initialized;
  1484. u16 string_to_security_class(struct policydb *p, const char *name)
  1485. {
  1486. struct class_datum *cladatum;
  1487. cladatum = hashtab_search(p->p_classes.table, name);
  1488. if (!cladatum)
  1489. return 0;
  1490. return cladatum->value;
  1491. }
  1492. u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
  1493. {
  1494. struct class_datum *cladatum;
  1495. struct perm_datum *perdatum = NULL;
  1496. struct common_datum *comdatum;
  1497. if (!tclass || tclass > p->p_classes.nprim)
  1498. return 0;
  1499. cladatum = p->class_val_to_struct[tclass-1];
  1500. comdatum = cladatum->comdatum;
  1501. if (comdatum)
  1502. perdatum = hashtab_search(comdatum->permissions.table,
  1503. name);
  1504. if (!perdatum)
  1505. perdatum = hashtab_search(cladatum->permissions.table,
  1506. name);
  1507. if (!perdatum)
  1508. return 0;
  1509. return 1U << (perdatum->value-1);
  1510. }
  1511. static int range_read(struct policydb *p, void *fp)
  1512. {
  1513. struct range_trans *rt = NULL;
  1514. struct mls_range *r = NULL;
  1515. int i, rc;
  1516. __le32 buf[2];
  1517. u32 nel;
  1518. if (p->policyvers < POLICYDB_VERSION_MLS)
  1519. return 0;
  1520. rc = next_entry(buf, fp, sizeof(u32));
  1521. if (rc)
  1522. goto out;
  1523. nel = le32_to_cpu(buf[0]);
  1524. for (i = 0; i < nel; i++) {
  1525. rc = -ENOMEM;
  1526. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  1527. if (!rt)
  1528. goto out;
  1529. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1530. if (rc)
  1531. goto out;
  1532. rt->source_type = le32_to_cpu(buf[0]);
  1533. rt->target_type = le32_to_cpu(buf[1]);
  1534. if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
  1535. rc = next_entry(buf, fp, sizeof(u32));
  1536. if (rc)
  1537. goto out;
  1538. rt->target_class = le32_to_cpu(buf[0]);
  1539. } else
  1540. rt->target_class = p->process_class;
  1541. rc = -EINVAL;
  1542. if (!policydb_type_isvalid(p, rt->source_type) ||
  1543. !policydb_type_isvalid(p, rt->target_type) ||
  1544. !policydb_class_isvalid(p, rt->target_class))
  1545. goto out;
  1546. rc = -ENOMEM;
  1547. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1548. if (!r)
  1549. goto out;
  1550. rc = mls_read_range_helper(r, fp);
  1551. if (rc)
  1552. goto out;
  1553. rc = -EINVAL;
  1554. if (!mls_range_isvalid(p, r)) {
  1555. printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
  1556. goto out;
  1557. }
  1558. rc = hashtab_insert(p->range_tr, rt, r);
  1559. if (rc)
  1560. goto out;
  1561. rt = NULL;
  1562. r = NULL;
  1563. }
  1564. hash_eval(p->range_tr, "rangetr");
  1565. rc = 0;
  1566. out:
  1567. kfree(rt);
  1568. kfree(r);
  1569. return rc;
  1570. }
  1571. static int filename_trans_read(struct policydb *p, void *fp)
  1572. {
  1573. struct filename_trans *ft;
  1574. struct filename_trans_datum *otype;
  1575. char *name;
  1576. u32 nel, len;
  1577. __le32 buf[4];
  1578. int rc, i;
  1579. if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
  1580. return 0;
  1581. rc = next_entry(buf, fp, sizeof(u32));
  1582. if (rc)
  1583. return rc;
  1584. nel = le32_to_cpu(buf[0]);
  1585. for (i = 0; i < nel; i++) {
  1586. ft = NULL;
  1587. otype = NULL;
  1588. name = NULL;
  1589. rc = -ENOMEM;
  1590. ft = kzalloc(sizeof(*ft), GFP_KERNEL);
  1591. if (!ft)
  1592. goto out;
  1593. rc = -ENOMEM;
  1594. otype = kmalloc(sizeof(*otype), GFP_KERNEL);
  1595. if (!otype)
  1596. goto out;
  1597. /* length of the path component string */
  1598. rc = next_entry(buf, fp, sizeof(u32));
  1599. if (rc)
  1600. goto out;
  1601. len = le32_to_cpu(buf[0]);
  1602. rc = -ENOMEM;
  1603. name = kmalloc(len + 1, GFP_KERNEL);
  1604. if (!name)
  1605. goto out;
  1606. ft->name = name;
  1607. /* path component string */
  1608. rc = next_entry(name, fp, len);
  1609. if (rc)
  1610. goto out;
  1611. name[len] = 0;
  1612. rc = next_entry(buf, fp, sizeof(u32) * 4);
  1613. if (rc)
  1614. goto out;
  1615. ft->stype = le32_to_cpu(buf[0]);
  1616. ft->ttype = le32_to_cpu(buf[1]);
  1617. ft->tclass = le32_to_cpu(buf[2]);
  1618. otype->otype = le32_to_cpu(buf[3]);
  1619. rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
  1620. if (rc)
  1621. goto out;
  1622. hashtab_insert(p->filename_trans, ft, otype);
  1623. }
  1624. hash_eval(p->filename_trans, "filenametr");
  1625. return 0;
  1626. out:
  1627. kfree(ft);
  1628. kfree(name);
  1629. kfree(otype);
  1630. return rc;
  1631. }
  1632. static int genfs_read(struct policydb *p, void *fp)
  1633. {
  1634. int i, j, rc;
  1635. u32 nel, nel2, len, len2;
  1636. __le32 buf[1];
  1637. struct ocontext *l, *c;
  1638. struct ocontext *newc = NULL;
  1639. struct genfs *genfs_p, *genfs;
  1640. struct genfs *newgenfs = NULL;
  1641. rc = next_entry(buf, fp, sizeof(u32));
  1642. if (rc)
  1643. goto out;
  1644. nel = le32_to_cpu(buf[0]);
  1645. for (i = 0; i < nel; i++) {
  1646. rc = next_entry(buf, fp, sizeof(u32));
  1647. if (rc)
  1648. goto out;
  1649. len = le32_to_cpu(buf[0]);
  1650. rc = -ENOMEM;
  1651. newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
  1652. if (!newgenfs)
  1653. goto out;
  1654. rc = -ENOMEM;
  1655. newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
  1656. if (!newgenfs->fstype)
  1657. goto out;
  1658. rc = next_entry(newgenfs->fstype, fp, len);
  1659. if (rc)
  1660. goto out;
  1661. newgenfs->fstype[len] = 0;
  1662. for (genfs_p = NULL, genfs = p->genfs; genfs;
  1663. genfs_p = genfs, genfs = genfs->next) {
  1664. rc = -EINVAL;
  1665. if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
  1666. printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
  1667. newgenfs->fstype);
  1668. goto out;
  1669. }
  1670. if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
  1671. break;
  1672. }
  1673. newgenfs->next = genfs;
  1674. if (genfs_p)
  1675. genfs_p->next = newgenfs;
  1676. else
  1677. p->genfs = newgenfs;
  1678. genfs = newgenfs;
  1679. newgenfs = NULL;
  1680. rc = next_entry(buf, fp, sizeof(u32));
  1681. if (rc)
  1682. goto out;
  1683. nel2 = le32_to_cpu(buf[0]);
  1684. for (j = 0; j < nel2; j++) {
  1685. rc = next_entry(buf, fp, sizeof(u32));
  1686. if (rc)
  1687. goto out;
  1688. len = le32_to_cpu(buf[0]);
  1689. rc = -ENOMEM;
  1690. newc = kzalloc(sizeof(*newc), GFP_KERNEL);
  1691. if (!newc)
  1692. goto out;
  1693. rc = -ENOMEM;
  1694. newc->u.name = kmalloc(len + 1, GFP_KERNEL);
  1695. if (!newc->u.name)
  1696. goto out;
  1697. rc = next_entry(newc->u.name, fp, len);
  1698. if (rc)
  1699. goto out;
  1700. newc->u.name[len] = 0;
  1701. rc = next_entry(buf, fp, sizeof(u32));
  1702. if (rc)
  1703. goto out;
  1704. newc->v.sclass = le32_to_cpu(buf[0]);
  1705. rc = context_read_and_validate(&newc->context[0], p, fp);
  1706. if (rc)
  1707. goto out;
  1708. for (l = NULL, c = genfs->head; c;
  1709. l = c, c = c->next) {
  1710. rc = -EINVAL;
  1711. if (!strcmp(newc->u.name, c->u.name) &&
  1712. (!c->v.sclass || !newc->v.sclass ||
  1713. newc->v.sclass == c->v.sclass)) {
  1714. printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
  1715. genfs->fstype, c->u.name);
  1716. goto out;
  1717. }
  1718. len = strlen(newc->u.name);
  1719. len2 = strlen(c->u.name);
  1720. if (len > len2)
  1721. break;
  1722. }
  1723. newc->next = c;
  1724. if (l)
  1725. l->next = newc;
  1726. else
  1727. genfs->head = newc;
  1728. newc = NULL;
  1729. }
  1730. }
  1731. rc = 0;
  1732. out:
  1733. if (newgenfs)
  1734. kfree(newgenfs->fstype);
  1735. kfree(newgenfs);
  1736. ocontext_destroy(newc, OCON_FSUSE);
  1737. return rc;
  1738. }
  1739. static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
  1740. void *fp)
  1741. {
  1742. int i, j, rc;
  1743. u32 nel, len;
  1744. __le32 buf[3];
  1745. struct ocontext *l, *c;
  1746. u32 nodebuf[8];
  1747. for (i = 0; i < info->ocon_num; i++) {
  1748. rc = next_entry(buf, fp, sizeof(u32));
  1749. if (rc)
  1750. goto out;
  1751. nel = le32_to_cpu(buf[0]);
  1752. l = NULL;
  1753. for (j = 0; j < nel; j++) {
  1754. rc = -ENOMEM;
  1755. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1756. if (!c)
  1757. goto out;
  1758. if (l)
  1759. l->next = c;
  1760. else
  1761. p->ocontexts[i] = c;
  1762. l = c;
  1763. switch (i) {
  1764. case OCON_ISID:
  1765. rc = next_entry(buf, fp, sizeof(u32));
  1766. if (rc)
  1767. goto out;
  1768. c->sid[0] = le32_to_cpu(buf[0]);
  1769. rc = context_read_and_validate(&c->context[0], p, fp);
  1770. if (rc)
  1771. goto out;
  1772. break;
  1773. case OCON_FS:
  1774. case OCON_NETIF:
  1775. rc = next_entry(buf, fp, sizeof(u32));
  1776. if (rc)
  1777. goto out;
  1778. len = le32_to_cpu(buf[0]);
  1779. rc = -ENOMEM;
  1780. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1781. if (!c->u.name)
  1782. goto out;
  1783. rc = next_entry(c->u.name, fp, len);
  1784. if (rc)
  1785. goto out;
  1786. c->u.name[len] = 0;
  1787. rc = context_read_and_validate(&c->context[0], p, fp);
  1788. if (rc)
  1789. goto out;
  1790. rc = context_read_and_validate(&c->context[1], p, fp);
  1791. if (rc)
  1792. goto out;
  1793. break;
  1794. case OCON_PORT:
  1795. rc = next_entry(buf, fp, sizeof(u32)*3);
  1796. if (rc)
  1797. goto out;
  1798. c->u.port.protocol = le32_to_cpu(buf[0]);
  1799. c->u.port.low_port = le32_to_cpu(buf[1]);
  1800. c->u.port.high_port = le32_to_cpu(buf[2]);
  1801. rc = context_read_and_validate(&c->context[0], p, fp);
  1802. if (rc)
  1803. goto out;
  1804. break;
  1805. case OCON_NODE:
  1806. rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
  1807. if (rc)
  1808. goto out;
  1809. c->u.node.addr = nodebuf[0]; /* network order */
  1810. c->u.node.mask = nodebuf[1]; /* network order */
  1811. rc = context_read_and_validate(&c->context[0], p, fp);
  1812. if (rc)
  1813. goto out;
  1814. break;
  1815. case OCON_FSUSE:
  1816. rc = next_entry(buf, fp, sizeof(u32)*2);
  1817. if (rc)
  1818. goto out;
  1819. rc = -EINVAL;
  1820. c->v.behavior = le32_to_cpu(buf[0]);
  1821. if (c->v.behavior > SECURITY_FS_USE_NONE)
  1822. goto out;
  1823. rc = -ENOMEM;
  1824. len = le32_to_cpu(buf[1]);
  1825. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1826. if (!c->u.name)
  1827. goto out;
  1828. rc = next_entry(c->u.name, fp, len);
  1829. if (rc)
  1830. goto out;
  1831. c->u.name[len] = 0;
  1832. rc = context_read_and_validate(&c->context[0], p, fp);
  1833. if (rc)
  1834. goto out;
  1835. break;
  1836. case OCON_NODE6: {
  1837. int k;
  1838. rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
  1839. if (rc)
  1840. goto out;
  1841. for (k = 0; k < 4; k++)
  1842. c->u.node6.addr[k] = nodebuf[k];
  1843. for (k = 0; k < 4; k++)
  1844. c->u.node6.mask[k] = nodebuf[k+4];
  1845. rc = context_read_and_validate(&c->context[0], p, fp);
  1846. if (rc)
  1847. goto out;
  1848. break;
  1849. }
  1850. }
  1851. }
  1852. }
  1853. rc = 0;
  1854. out:
  1855. return rc;
  1856. }
  1857. /*
  1858. * Read the configuration data from a policy database binary
  1859. * representation file into a policy database structure.
  1860. */
  1861. int policydb_read(struct policydb *p, void *fp)
  1862. {
  1863. struct role_allow *ra, *lra;
  1864. struct role_trans *tr, *ltr;
  1865. int i, j, rc;
  1866. __le32 buf[4];
  1867. u32 len, nprim, nel;
  1868. char *policydb_str;
  1869. struct policydb_compat_info *info;
  1870. rc = policydb_init(p);
  1871. if (rc)
  1872. return rc;
  1873. /* Read the magic number and string length. */
  1874. rc = next_entry(buf, fp, sizeof(u32) * 2);
  1875. if (rc)
  1876. goto bad;
  1877. rc = -EINVAL;
  1878. if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
  1879. printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
  1880. "not match expected magic number 0x%x\n",
  1881. le32_to_cpu(buf[0]), POLICYDB_MAGIC);
  1882. goto bad;
  1883. }
  1884. rc = -EINVAL;
  1885. len = le32_to_cpu(buf[1]);
  1886. if (len != strlen(POLICYDB_STRING)) {
  1887. printk(KERN_ERR "SELinux: policydb string length %d does not "
  1888. "match expected length %Zu\n",
  1889. len, strlen(POLICYDB_STRING));
  1890. goto bad;
  1891. }
  1892. rc = -ENOMEM;
  1893. policydb_str = kmalloc(len + 1, GFP_KERNEL);
  1894. if (!policydb_str) {
  1895. printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
  1896. "string of length %d\n", len);
  1897. goto bad;
  1898. }
  1899. rc = next_entry(policydb_str, fp, len);
  1900. if (rc) {
  1901. printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
  1902. kfree(policydb_str);
  1903. goto bad;
  1904. }
  1905. rc = -EINVAL;
  1906. policydb_str[len] = '\0';
  1907. if (strcmp(policydb_str, POLICYDB_STRING)) {
  1908. printk(KERN_ERR "SELinux: policydb string %s does not match "
  1909. "my string %s\n", policydb_str, POLICYDB_STRING);
  1910. kfree(policydb_str);
  1911. goto bad;
  1912. }
  1913. /* Done with policydb_str. */
  1914. kfree(policydb_str);
  1915. policydb_str = NULL;
  1916. /* Read the version and table sizes. */
  1917. rc = next_entry(buf, fp, sizeof(u32)*4);
  1918. if (rc)
  1919. goto bad;
  1920. rc = -EINVAL;
  1921. p->policyvers = le32_to_cpu(buf[0]);
  1922. if (p->policyvers < POLICYDB_VERSION_MIN ||
  1923. p->policyvers > POLICYDB_VERSION_MAX) {
  1924. printk(KERN_ERR "SELinux: policydb version %d does not match "
  1925. "my version range %d-%d\n",
  1926. le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
  1927. goto bad;
  1928. }
  1929. if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
  1930. p->mls_enabled = 1;
  1931. rc = -EINVAL;
  1932. if (p->policyvers < POLICYDB_VERSION_MLS) {
  1933. printk(KERN_ERR "SELinux: security policydb version %d "
  1934. "(MLS) not backwards compatible\n",
  1935. p->policyvers);
  1936. goto bad;
  1937. }
  1938. }
  1939. p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
  1940. p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
  1941. if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
  1942. rc = ebitmap_read(&p->policycaps, fp);
  1943. if (rc)
  1944. goto bad;
  1945. }
  1946. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
  1947. rc = ebitmap_read(&p->permissive_map, fp);
  1948. if (rc)
  1949. goto bad;
  1950. }
  1951. rc = -EINVAL;
  1952. info = policydb_lookup_compat(p->policyvers);
  1953. if (!info) {
  1954. printk(KERN_ERR "SELinux: unable to find policy compat info "
  1955. "for version %d\n", p->policyvers);
  1956. goto bad;
  1957. }
  1958. rc = -EINVAL;
  1959. if (le32_to_cpu(buf[2]) != info->sym_num ||
  1960. le32_to_cpu(buf[3]) != info->ocon_num) {
  1961. printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
  1962. "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
  1963. le32_to_cpu(buf[3]),
  1964. info->sym_num, info->ocon_num);
  1965. goto bad;
  1966. }
  1967. for (i = 0; i < info->sym_num; i++) {
  1968. rc = next_entry(buf, fp, sizeof(u32)*2);
  1969. if (rc)
  1970. goto bad;
  1971. nprim = le32_to_cpu(buf[0]);
  1972. nel = le32_to_cpu(buf[1]);
  1973. for (j = 0; j < nel; j++) {
  1974. rc = read_f[i](p, p->symtab[i].table, fp);
  1975. if (rc)
  1976. goto bad;
  1977. }
  1978. p->symtab[i].nprim = nprim;
  1979. }
  1980. rc = -EINVAL;
  1981. p->process_class = string_to_security_class(p, "process");
  1982. if (!p->process_class)
  1983. goto bad;
  1984. rc = avtab_read(&p->te_avtab, fp, p);
  1985. if (rc)
  1986. goto bad;
  1987. if (p->policyvers >= POLICYDB_VERSION_BOOL) {
  1988. rc = cond_read_list(p, fp);
  1989. if (rc)
  1990. goto bad;
  1991. }
  1992. rc = next_entry(buf, fp, sizeof(u32));
  1993. if (rc)
  1994. goto bad;
  1995. nel = le32_to_cpu(buf[0]);
  1996. ltr = NULL;
  1997. for (i = 0; i < nel; i++) {
  1998. rc = -ENOMEM;
  1999. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  2000. if (!tr)
  2001. goto bad;
  2002. if (ltr)
  2003. ltr->next = tr;
  2004. else
  2005. p->role_tr = tr;
  2006. rc = next_entry(buf, fp, sizeof(u32)*3);
  2007. if (rc)
  2008. goto bad;
  2009. rc = -EINVAL;
  2010. tr->role = le32_to_cpu(buf[0]);
  2011. tr->type = le32_to_cpu(buf[1]);
  2012. tr->new_role = le32_to_cpu(buf[2]);
  2013. if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
  2014. rc = next_entry(buf, fp, sizeof(u32));
  2015. if (rc)
  2016. goto bad;
  2017. tr->tclass = le32_to_cpu(buf[0]);
  2018. } else
  2019. tr->tclass = p->process_class;
  2020. if (!policydb_role_isvalid(p, tr->role) ||
  2021. !policydb_type_isvalid(p, tr->type) ||
  2022. !policydb_class_isvalid(p, tr->tclass) ||
  2023. !policydb_role_isvalid(p, tr->new_role))
  2024. goto bad;
  2025. ltr = tr;
  2026. }
  2027. rc = next_entry(buf, fp, sizeof(u32));
  2028. if (rc)
  2029. goto bad;
  2030. nel = le32_to_cpu(buf[0]);
  2031. lra = NULL;
  2032. for (i = 0; i < nel; i++) {
  2033. rc = -ENOMEM;
  2034. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  2035. if (!ra)
  2036. goto bad;
  2037. if (lra)
  2038. lra->next = ra;
  2039. else
  2040. p->role_allow = ra;
  2041. rc = next_entry(buf, fp, sizeof(u32)*2);
  2042. if (rc)
  2043. goto bad;
  2044. rc = -EINVAL;
  2045. ra->role = le32_to_cpu(buf[0]);
  2046. ra->new_role = le32_to_cpu(buf[1]);
  2047. if (!policydb_role_isvalid(p, ra->role) ||
  2048. !policydb_role_isvalid(p, ra->new_role))
  2049. goto bad;
  2050. lra = ra;
  2051. }
  2052. rc = filename_trans_read(p, fp);
  2053. if (rc)
  2054. goto bad;
  2055. rc = policydb_index(p);
  2056. if (rc)
  2057. goto bad;
  2058. rc = -EINVAL;
  2059. p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
  2060. p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
  2061. if (!p->process_trans_perms)
  2062. goto bad;
  2063. rc = ocontext_read(p, info, fp);
  2064. if (rc)
  2065. goto bad;
  2066. rc = genfs_read(p, fp);
  2067. if (rc)
  2068. goto bad;
  2069. rc = range_read(p, fp);
  2070. if (rc)
  2071. goto bad;
  2072. rc = -ENOMEM;
  2073. p->type_attr_map_array = flex_arra