policydb.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  16. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, version 2.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include "security.h"
  26. #include "policydb.h"
  27. #include "conditional.h"
  28. #include "mls.h"
  29. #define _DEBUG_HASHES
  30. #ifdef DEBUG_HASHES
  31. static char *symtab_name[SYM_NUM] = {
  32. "common prefixes",
  33. "classes",
  34. "roles",
  35. "types",
  36. "users",
  37. "bools",
  38. "levels",
  39. "categories",
  40. };
  41. #endif
  42. int selinux_mls_enabled = 0;
  43. static unsigned int symtab_sizes[SYM_NUM] = {
  44. 2,
  45. 32,
  46. 16,
  47. 512,
  48. 128,
  49. 16,
  50. 16,
  51. 16,
  52. };
  53. struct policydb_compat_info {
  54. int version;
  55. int sym_num;
  56. int ocon_num;
  57. };
  58. /* These need to be updated if SYM_NUM or OCON_NUM changes */
  59. static struct policydb_compat_info policydb_compat[] = {
  60. {
  61. .version = POLICYDB_VERSION_BASE,
  62. .sym_num = SYM_NUM - 3,
  63. .ocon_num = OCON_NUM - 1,
  64. },
  65. {
  66. .version = POLICYDB_VERSION_BOOL,
  67. .sym_num = SYM_NUM - 2,
  68. .ocon_num = OCON_NUM - 1,
  69. },
  70. {
  71. .version = POLICYDB_VERSION_IPV6,
  72. .sym_num = SYM_NUM - 2,
  73. .ocon_num = OCON_NUM,
  74. },
  75. {
  76. .version = POLICYDB_VERSION_NLCLASS,
  77. .sym_num = SYM_NUM - 2,
  78. .ocon_num = OCON_NUM,
  79. },
  80. {
  81. .version = POLICYDB_VERSION_MLS,
  82. .sym_num = SYM_NUM,
  83. .ocon_num = OCON_NUM,
  84. },
  85. {
  86. .version = POLICYDB_VERSION_AVTAB,
  87. .sym_num = SYM_NUM,
  88. .ocon_num = OCON_NUM,
  89. },
  90. };
  91. static struct policydb_compat_info *policydb_lookup_compat(int version)
  92. {
  93. int i;
  94. struct policydb_compat_info *info = NULL;
  95. for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
  96. if (policydb_compat[i].version == version) {
  97. info = &policydb_compat[i];
  98. break;
  99. }
  100. }
  101. return info;
  102. }
  103. /*
  104. * Initialize the role table.
  105. */
  106. static int roles_init(struct policydb *p)
  107. {
  108. char *key = NULL;
  109. int rc;
  110. struct role_datum *role;
  111. role = kzalloc(sizeof(*role), GFP_KERNEL);
  112. if (!role) {
  113. rc = -ENOMEM;
  114. goto out;
  115. }
  116. role->value = ++p->p_roles.nprim;
  117. if (role->value != OBJECT_R_VAL) {
  118. rc = -EINVAL;
  119. goto out_free_role;
  120. }
  121. key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
  122. if (!key) {
  123. rc = -ENOMEM;
  124. goto out_free_role;
  125. }
  126. strcpy(key, OBJECT_R);
  127. rc = hashtab_insert(p->p_roles.table, key, role);
  128. if (rc)
  129. goto out_free_key;
  130. out:
  131. return rc;
  132. out_free_key:
  133. kfree(key);
  134. out_free_role:
  135. kfree(role);
  136. goto out;
  137. }
  138. /*
  139. * Initialize a policy database structure.
  140. */
  141. static int policydb_init(struct policydb *p)
  142. {
  143. int i, rc;
  144. memset(p, 0, sizeof(*p));
  145. for (i = 0; i < SYM_NUM; i++) {
  146. rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
  147. if (rc)
  148. goto out_free_symtab;
  149. }
  150. rc = avtab_init(&p->te_avtab);
  151. if (rc)
  152. goto out_free_symtab;
  153. rc = roles_init(p);
  154. if (rc)
  155. goto out_free_avtab;
  156. rc = cond_policydb_init(p);
  157. if (rc)
  158. goto out_free_avtab;
  159. out:
  160. return rc;
  161. out_free_avtab:
  162. avtab_destroy(&p->te_avtab);
  163. out_free_symtab:
  164. for (i = 0; i < SYM_NUM; i++)
  165. hashtab_destroy(p->symtab[i].table);
  166. goto out;
  167. }
  168. /*
  169. * The following *_index functions are used to
  170. * define the val_to_name and val_to_struct arrays
  171. * in a policy database structure. The val_to_name
  172. * arrays are used when converting security context
  173. * structures into string representations. The
  174. * val_to_struct arrays are used when the attributes
  175. * of a class, role, or user are needed.
  176. */
  177. static int common_index(void *key, void *datum, void *datap)
  178. {
  179. struct policydb *p;
  180. struct common_datum *comdatum;
  181. comdatum = datum;
  182. p = datap;
  183. if (!comdatum->value || comdatum->value > p->p_commons.nprim)
  184. return -EINVAL;
  185. p->p_common_val_to_name[comdatum->value - 1] = key;
  186. return 0;
  187. }
  188. static int class_index(void *key, void *datum, void *datap)
  189. {
  190. struct policydb *p;
  191. struct class_datum *cladatum;
  192. cladatum = datum;
  193. p = datap;
  194. if (!cladatum->value || cladatum->value > p->p_classes.nprim)
  195. return -EINVAL;
  196. p->p_class_val_to_name[cladatum->value - 1] = key;
  197. p->class_val_to_struct[cladatum->value - 1] = cladatum;
  198. return 0;
  199. }
  200. static int role_index(void *key, void *datum, void *datap)
  201. {
  202. struct policydb *p;
  203. struct role_datum *role;
  204. role = datum;
  205. p = datap;
  206. if (!role->value || role->value > p->p_roles.nprim)
  207. return -EINVAL;
  208. p->p_role_val_to_name[role->value - 1] = key;
  209. p->role_val_to_struct[role->value - 1] = role;
  210. return 0;
  211. }
  212. static int type_index(void *key, void *datum, void *datap)
  213. {
  214. struct policydb *p;
  215. struct type_datum *typdatum;
  216. typdatum = datum;
  217. p = datap;
  218. if (typdatum->primary) {
  219. if (!typdatum->value || typdatum->value > p->p_types.nprim)
  220. return -EINVAL;
  221. p->p_type_val_to_name[typdatum->value - 1] = key;
  222. }
  223. return 0;
  224. }
  225. static int user_index(void *key, void *datum, void *datap)
  226. {
  227. struct policydb *p;
  228. struct user_datum *usrdatum;
  229. usrdatum = datum;
  230. p = datap;
  231. if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
  232. return -EINVAL;
  233. p->p_user_val_to_name[usrdatum->value - 1] = key;
  234. p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
  235. return 0;
  236. }
  237. static int sens_index(void *key, void *datum, void *datap)
  238. {
  239. struct policydb *p;
  240. struct level_datum *levdatum;
  241. levdatum = datum;
  242. p = datap;
  243. if (!levdatum->isalias) {
  244. if (!levdatum->level->sens ||
  245. levdatum->level->sens > p->p_levels.nprim)
  246. return -EINVAL;
  247. p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
  248. }
  249. return 0;
  250. }
  251. static int cat_index(void *key, void *datum, void *datap)
  252. {
  253. struct policydb *p;
  254. struct cat_datum *catdatum;
  255. catdatum = datum;
  256. p = datap;
  257. if (!catdatum->isalias) {
  258. if (!catdatum->value || catdatum->value > p->p_cats.nprim)
  259. return -EINVAL;
  260. p->p_cat_val_to_name[catdatum->value - 1] = key;
  261. }
  262. return 0;
  263. }
  264. static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  265. {
  266. common_index,
  267. class_index,
  268. role_index,
  269. type_index,
  270. user_index,
  271. cond_index_bool,
  272. sens_index,
  273. cat_index,
  274. };
  275. /*
  276. * Define the common val_to_name array and the class
  277. * val_to_name and val_to_struct arrays in a policy
  278. * database structure.
  279. *
  280. * Caller must clean up upon failure.
  281. */
  282. static int policydb_index_classes(struct policydb *p)
  283. {
  284. int rc;
  285. p->p_common_val_to_name =
  286. kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
  287. if (!p->p_common_val_to_name) {
  288. rc = -ENOMEM;
  289. goto out;
  290. }
  291. rc = hashtab_map(p->p_commons.table, common_index, p);
  292. if (rc)
  293. goto out;
  294. p->class_val_to_struct =
  295. kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
  296. if (!p->class_val_to_struct) {
  297. rc = -ENOMEM;
  298. goto out;
  299. }
  300. p->p_class_val_to_name =
  301. kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
  302. if (!p->p_class_val_to_name) {
  303. rc = -ENOMEM;
  304. goto out;
  305. }
  306. rc = hashtab_map(p->p_classes.table, class_index, p);
  307. out:
  308. return rc;
  309. }
  310. #ifdef DEBUG_HASHES
  311. static void symtab_hash_eval(struct symtab *s)
  312. {
  313. int i;
  314. for (i = 0; i < SYM_NUM; i++) {
  315. struct hashtab *h = s[i].table;
  316. struct hashtab_info info;
  317. hashtab_stat(h, &info);
  318. printk(KERN_INFO "%s: %d entries and %d/%d buckets used, "
  319. "longest chain length %d\n", symtab_name[i], h->nel,
  320. info.slots_used, h->size, info.max_chain_len);
  321. }
  322. }
  323. #endif
  324. /*
  325. * Define the other val_to_name and val_to_struct arrays
  326. * in a policy database structure.
  327. *
  328. * Caller must clean up on failure.
  329. */
  330. static int policydb_index_others(struct policydb *p)
  331. {
  332. int i, rc = 0;
  333. printk(KERN_INFO "security: %d users, %d roles, %d types, %d bools",
  334. p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
  335. if (selinux_mls_enabled)
  336. printk(", %d sens, %d cats", p->p_levels.nprim,
  337. p->p_cats.nprim);
  338. printk("\n");
  339. printk(KERN_INFO "security: %d classes, %d rules\n",
  340. p->p_classes.nprim, p->te_avtab.nel);
  341. #ifdef DEBUG_HASHES
  342. avtab_hash_eval(&p->te_avtab, "rules");
  343. symtab_hash_eval(p->symtab);
  344. #endif
  345. p->role_val_to_struct =
  346. kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
  347. GFP_KERNEL);
  348. if (!p->role_val_to_struct) {
  349. rc = -ENOMEM;
  350. goto out;
  351. }
  352. p->user_val_to_struct =
  353. kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
  354. GFP_KERNEL);
  355. if (!p->user_val_to_struct) {
  356. rc = -ENOMEM;
  357. goto out;
  358. }
  359. if (cond_init_bool_indexes(p)) {
  360. rc = -ENOMEM;
  361. goto out;
  362. }
  363. for (i = SYM_ROLES; i < SYM_NUM; i++) {
  364. p->sym_val_to_name[i] =
  365. kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
  366. if (!p->sym_val_to_name[i]) {
  367. rc = -ENOMEM;
  368. goto out;
  369. }
  370. rc = hashtab_map(p->symtab[i].table, index_f[i], p);
  371. if (rc)
  372. goto out;
  373. }
  374. out:
  375. return rc;
  376. }
  377. /*
  378. * The following *_destroy functions are used to
  379. * free any memory allocated for each kind of
  380. * symbol data in the policy database.
  381. */
  382. static int perm_destroy(void *key, void *datum, void *p)
  383. {
  384. kfree(key);
  385. kfree(datum);
  386. return 0;
  387. }
  388. static int common_destroy(void *key, void *datum, void *p)
  389. {
  390. struct common_datum *comdatum;
  391. kfree(key);
  392. comdatum = datum;
  393. hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
  394. hashtab_destroy(comdatum->permissions.table);
  395. kfree(datum);
  396. return 0;
  397. }
  398. static int class_destroy(void *key, void *datum, void *p)
  399. {
  400. struct class_datum *cladatum;
  401. struct constraint_node *constraint, *ctemp;
  402. struct constraint_expr *e, *etmp;
  403. kfree(key);
  404. cladatum = datum;
  405. hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
  406. hashtab_destroy(cladatum->permissions.table);
  407. constraint = cladatum->constraints;
  408. while (constraint) {
  409. e = constraint->expr;
  410. while (e) {
  411. ebitmap_destroy(&e->names);
  412. etmp = e;
  413. e = e->next;
  414. kfree(etmp);
  415. }
  416. ctemp = constraint;
  417. constraint = constraint->next;
  418. kfree(ctemp);
  419. }
  420. constraint = cladatum->validatetrans;
  421. while (constraint) {
  422. e = constraint->expr;
  423. while (e) {
  424. ebitmap_destroy(&e->names);
  425. etmp = e;
  426. e = e->next;
  427. kfree(etmp);
  428. }
  429. ctemp = constraint;
  430. constraint = constraint->next;
  431. kfree(ctemp);
  432. }
  433. kfree(cladatum->comkey);
  434. kfree(datum);
  435. return 0;
  436. }
  437. static int role_destroy(void *key, void *datum, void *p)
  438. {
  439. struct role_datum *role;
  440. kfree(key);
  441. role = datum;
  442. ebitmap_destroy(&role->dominates);
  443. ebitmap_destroy(&role->types);
  444. kfree(datum);
  445. return 0;
  446. }
  447. static int type_destroy(void *key, void *datum, void *p)
  448. {
  449. kfree(key);
  450. kfree(datum);
  451. return 0;
  452. }
  453. static int user_destroy(void *key, void *datum, void *p)
  454. {
  455. struct user_datum *usrdatum;
  456. kfree(key);
  457. usrdatum = datum;
  458. ebitmap_destroy(&usrdatum->roles);
  459. ebitmap_destroy(&usrdatum->range.level[0].cat);
  460. ebitmap_destroy(&usrdatum->range.level[1].cat);
  461. ebitmap_destroy(&usrdatum->dfltlevel.cat);
  462. kfree(datum);
  463. return 0;
  464. }
  465. static int sens_destroy(void *key, void *datum, void *p)
  466. {
  467. struct level_datum *levdatum;
  468. kfree(key);
  469. levdatum = datum;
  470. ebitmap_destroy(&levdatum->level->cat);
  471. kfree(levdatum->level);
  472. kfree(datum);
  473. return 0;
  474. }
  475. static int cat_destroy(void *key, void *datum, void *p)
  476. {
  477. kfree(key);
  478. kfree(datum);
  479. return 0;
  480. }
  481. static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  482. {
  483. common_destroy,
  484. class_destroy,
  485. role_destroy,
  486. type_destroy,
  487. user_destroy,
  488. cond_destroy_bool,
  489. sens_destroy,
  490. cat_destroy,
  491. };
  492. static void ocontext_destroy(struct ocontext *c, int i)
  493. {
  494. context_destroy(&c->context[0]);
  495. context_destroy(&c->context[1]);
  496. if (i == OCON_ISID || i == OCON_FS ||
  497. i == OCON_NETIF || i == OCON_FSUSE)
  498. kfree(c->u.name);
  499. kfree(c);
  500. }
  501. /*
  502. * Free any memory allocated by a policy database structure.
  503. */
  504. void policydb_destroy(struct policydb *p)
  505. {
  506. struct ocontext *c, *ctmp;
  507. struct genfs *g, *gtmp;
  508. int i;
  509. struct role_allow *ra, *lra = NULL;
  510. struct role_trans *tr, *ltr = NULL;
  511. struct range_trans *rt, *lrt = NULL;
  512. for (i = 0; i < SYM_NUM; i++) {
  513. hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
  514. hashtab_destroy(p->symtab[i].table);
  515. }
  516. for (i = 0; i < SYM_NUM; i++)
  517. kfree(p->sym_val_to_name[i]);
  518. kfree(p->class_val_to_struct);
  519. kfree(p->role_val_to_struct);
  520. kfree(p->user_val_to_struct);
  521. avtab_destroy(&p->te_avtab);
  522. for (i = 0; i < OCON_NUM; i++) {
  523. c = p->ocontexts[i];
  524. while (c) {
  525. ctmp = c;
  526. c = c->next;
  527. ocontext_destroy(ctmp,i);
  528. }
  529. }
  530. g = p->genfs;
  531. while (g) {
  532. kfree(g->fstype);
  533. c = g->head;
  534. while (c) {
  535. ctmp = c;
  536. c = c->next;
  537. ocontext_destroy(ctmp,OCON_FSUSE);
  538. }
  539. gtmp = g;
  540. g = g->next;
  541. kfree(gtmp);
  542. }
  543. cond_policydb_destroy(p);
  544. for (tr = p->role_tr; tr; tr = tr->next) {
  545. kfree(ltr);
  546. ltr = tr;
  547. }
  548. kfree(ltr);
  549. for (ra = p->role_allow; ra; ra = ra -> next) {
  550. kfree(lra);
  551. lra = ra;
  552. }
  553. kfree(lra);
  554. for (rt = p->range_tr; rt; rt = rt -> next) {
  555. if (lrt) {
  556. ebitmap_destroy(&lrt->range.level[0].cat);
  557. ebitmap_destroy(&lrt->range.level[1].cat);
  558. kfree(lrt);
  559. }
  560. lrt = rt;
  561. }
  562. if (lrt) {
  563. ebitmap_destroy(&lrt->range.level[0].cat);
  564. ebitmap_destroy(&lrt->range.level[1].cat);
  565. kfree(lrt);
  566. }
  567. if (p->type_attr_map) {
  568. for (i = 0; i < p->p_types.nprim; i++)
  569. ebitmap_destroy(&p->type_attr_map[i]);
  570. }
  571. kfree(p->type_attr_map);
  572. return;
  573. }
  574. /*
  575. * Load the initial SIDs specified in a policy database
  576. * structure into a SID table.
  577. */
  578. int policydb_load_isids(struct policydb *p, struct sidtab *s)
  579. {
  580. struct ocontext *head, *c;
  581. int rc;
  582. rc = sidtab_init(s);
  583. if (rc) {
  584. printk(KERN_ERR "security: out of memory on SID table init\n");
  585. goto out;
  586. }
  587. head = p->ocontexts[OCON_ISID];
  588. for (c = head; c; c = c->next) {
  589. if (!c->context[0].user) {
  590. printk(KERN_ERR "security: SID %s was never "
  591. "defined.\n", c->u.name);
  592. rc = -EINVAL;
  593. goto out;
  594. }
  595. if (sidtab_insert(s, c->sid[0], &c->context[0])) {
  596. printk(KERN_ERR "security: unable to load initial "
  597. "SID %s.\n", c->u.name);
  598. rc = -EINVAL;
  599. goto out;
  600. }
  601. }
  602. out:
  603. return rc;
  604. }
  605. /*
  606. * Return 1 if the fields in the security context
  607. * structure `c' are valid. Return 0 otherwise.
  608. */
  609. int policydb_context_isvalid(struct policydb *p, struct context *c)
  610. {
  611. struct role_datum *role;
  612. struct user_datum *usrdatum;
  613. if (!c->role || c->role > p->p_roles.nprim)
  614. return 0;
  615. if (!c->user || c->user > p->p_users.nprim)
  616. return 0;
  617. if (!c->type || c->type > p->p_types.nprim)
  618. return 0;
  619. if (c->role != OBJECT_R_VAL) {
  620. /*
  621. * Role must be authorized for the type.
  622. */
  623. role = p->role_val_to_struct[c->role - 1];
  624. if (!ebitmap_get_bit(&role->types,
  625. c->type - 1))
  626. /* role may not be associated with type */
  627. return 0;
  628. /*
  629. * User must be authorized for the role.
  630. */
  631. usrdatum = p->user_val_to_struct[c->user - 1];
  632. if (!usrdatum)
  633. return 0;
  634. if (!ebitmap_get_bit(&usrdatum->roles,
  635. c->role - 1))
  636. /* user may not be associated with role */
  637. return 0;
  638. }
  639. if (!mls_context_isvalid(p, c))
  640. return 0;
  641. return 1;
  642. }
  643. /*
  644. * Read a MLS range structure from a policydb binary
  645. * representation file.
  646. */
  647. static int mls_read_range_helper(struct mls_range *r, void *fp)
  648. {
  649. __le32 buf[2];
  650. u32 items;
  651. int rc;
  652. rc = next_entry(buf, fp, sizeof(u32));
  653. if (rc < 0)
  654. goto out;
  655. items = le32_to_cpu(buf[0]);
  656. if (items > ARRAY_SIZE(buf)) {
  657. printk(KERN_ERR "security: mls: range overflow\n");
  658. rc = -EINVAL;
  659. goto out;
  660. }
  661. rc = next_entry(buf, fp, sizeof(u32) * items);
  662. if (rc < 0) {
  663. printk(KERN_ERR "security: mls: truncated range\n");
  664. goto out;
  665. }
  666. r->level[0].sens = le32_to_cpu(buf[0]);
  667. if (items > 1)
  668. r->level[1].sens = le32_to_cpu(buf[1]);
  669. else
  670. r->level[1].sens = r->level[0].sens;
  671. rc = ebitmap_read(&r->level[0].cat, fp);
  672. if (rc) {
  673. printk(KERN_ERR "security: mls: error reading low "
  674. "categories\n");
  675. goto out;
  676. }
  677. if (items > 1) {
  678. rc = ebitmap_read(&r->level[1].cat, fp);
  679. if (rc) {
  680. printk(KERN_ERR "security: mls: error reading high "
  681. "categories\n");
  682. goto bad_high;
  683. }
  684. } else {
  685. rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
  686. if (rc) {
  687. printk(KERN_ERR "security: mls: out of memory\n");
  688. goto bad_high;
  689. }
  690. }
  691. rc = 0;
  692. out:
  693. return rc;
  694. bad_high:
  695. ebitmap_destroy(&r->level[0].cat);
  696. goto out;
  697. }
  698. /*
  699. * Read and validate a security context structure
  700. * from a policydb binary representation file.
  701. */
  702. static int context_read_and_validate(struct context *c,
  703. struct policydb *p,
  704. void *fp)
  705. {
  706. __le32 buf[3];
  707. int rc;
  708. rc = next_entry(buf, fp, sizeof buf);
  709. if (rc < 0) {
  710. printk(KERN_ERR "security: context truncated\n");
  711. goto out;
  712. }
  713. c->user = le32_to_cpu(buf[0]);
  714. c->role = le32_to_cpu(buf[1]);
  715. c->type = le32_to_cpu(buf[2]);
  716. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  717. if (mls_read_range_helper(&c->range, fp)) {
  718. printk(KERN_ERR "security: error reading MLS range of "
  719. "context\n");
  720. rc = -EINVAL;
  721. goto out;
  722. }
  723. }
  724. if (!policydb_context_isvalid(p, c)) {
  725. printk(KERN_ERR "security: invalid security context\n");
  726. context_destroy(c);
  727. rc = -EINVAL;
  728. }
  729. out:
  730. return rc;
  731. }
  732. /*
  733. * The following *_read functions are used to
  734. * read the symbol data from a policy database
  735. * binary representation file.
  736. */
  737. static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
  738. {
  739. char *key = NULL;
  740. struct perm_datum *perdatum;
  741. int rc;
  742. __le32 buf[2];
  743. u32 len;
  744. perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
  745. if (!perdatum) {
  746. rc = -ENOMEM;
  747. goto out;
  748. }
  749. rc = next_entry(buf, fp, sizeof buf);
  750. if (rc < 0)
  751. goto bad;
  752. len = le32_to_cpu(buf[0]);
  753. perdatum->value = le32_to_cpu(buf[1]);
  754. key = kmalloc(len + 1,GFP_KERNEL);
  755. if (!key) {
  756. rc = -ENOMEM;
  757. goto bad;
  758. }
  759. rc = next_entry(key, fp, len);
  760. if (rc < 0)
  761. goto bad;
  762. key[len] = 0;
  763. rc = hashtab_insert(h, key, perdatum);
  764. if (rc)
  765. goto bad;
  766. out:
  767. return rc;
  768. bad:
  769. perm_destroy(key, perdatum, NULL);
  770. goto out;
  771. }
  772. static int common_read(struct policydb *p, struct hashtab *h, void *fp)
  773. {
  774. char *key = NULL;
  775. struct common_datum *comdatum;
  776. __le32 buf[4];
  777. u32 len, nel;
  778. int i, rc;
  779. comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
  780. if (!comdatum) {
  781. rc = -ENOMEM;
  782. goto out;
  783. }
  784. rc = next_entry(buf, fp, sizeof buf);
  785. if (rc < 0)
  786. goto bad;
  787. len = le32_to_cpu(buf[0]);
  788. comdatum->value = le32_to_cpu(buf[1]);
  789. rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
  790. if (rc)
  791. goto bad;
  792. comdatum->permissions.nprim = le32_to_cpu(buf[2]);
  793. nel = le32_to_cpu(buf[3]);
  794. key = kmalloc(len + 1,GFP_KERNEL);
  795. if (!key) {
  796. rc = -ENOMEM;
  797. goto bad;
  798. }
  799. rc = next_entry(key, fp, len);
  800. if (rc < 0)
  801. goto bad;
  802. key[len] = 0;
  803. for (i = 0; i < nel; i++) {
  804. rc = perm_read(p, comdatum->permissions.table, fp);
  805. if (rc)
  806. goto bad;
  807. }
  808. rc = hashtab_insert(h, key, comdatum);
  809. if (rc)
  810. goto bad;
  811. out:
  812. return rc;
  813. bad:
  814. common_destroy(key, comdatum, NULL);
  815. goto out;
  816. }
  817. static int read_cons_helper(struct constraint_node **nodep, int ncons,
  818. int allowxtarget, void *fp)
  819. {
  820. struct constraint_node *c, *lc;
  821. struct constraint_expr *e, *le;
  822. __le32 buf[3];
  823. u32 nexpr;
  824. int rc, i, j, depth;
  825. lc = NULL;
  826. for (i = 0; i < ncons; i++) {
  827. c = kzalloc(sizeof(*c), GFP_KERNEL);
  828. if (!c)
  829. return -ENOMEM;
  830. if (lc) {
  831. lc->next = c;
  832. } else {
  833. *nodep = c;
  834. }
  835. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  836. if (rc < 0)
  837. return rc;
  838. c->permissions = le32_to_cpu(buf[0]);
  839. nexpr = le32_to_cpu(buf[1]);
  840. le = NULL;
  841. depth = -1;
  842. for (j = 0; j < nexpr; j++) {
  843. e = kzalloc(sizeof(*e), GFP_KERNEL);
  844. if (!e)
  845. return -ENOMEM;
  846. if (le) {
  847. le->next = e;
  848. } else {
  849. c->expr = e;
  850. }
  851. rc = next_entry(buf, fp, (sizeof(u32) * 3));
  852. if (rc < 0)
  853. return rc;
  854. e->expr_type = le32_to_cpu(buf[0]);
  855. e->attr = le32_to_cpu(buf[1]);
  856. e->op = le32_to_cpu(buf[2]);
  857. switch (e->expr_type) {
  858. case CEXPR_NOT:
  859. if (depth < 0)
  860. return -EINVAL;
  861. break;
  862. case CEXPR_AND:
  863. case CEXPR_OR:
  864. if (depth < 1)
  865. return -EINVAL;
  866. depth--;
  867. break;
  868. case CEXPR_ATTR:
  869. if (depth == (CEXPR_MAXDEPTH - 1))
  870. return -EINVAL;
  871. depth++;
  872. break;
  873. case CEXPR_NAMES:
  874. if (!allowxtarget && (e->attr & CEXPR_XTARGET))
  875. return -EINVAL;
  876. if (depth == (CEXPR_MAXDEPTH - 1))
  877. return -EINVAL;
  878. depth++;
  879. if (ebitmap_read(&e->names, fp))
  880. return -EINVAL;
  881. break;
  882. default:
  883. return -EINVAL;
  884. }
  885. le = e;
  886. }
  887. if (depth != 0)
  888. return -EINVAL;
  889. lc = c;
  890. }
  891. return 0;
  892. }
  893. static int class_read(struct policydb *p, struct hashtab *h, void *fp)
  894. {
  895. char *key = NULL;
  896. struct class_datum *cladatum;
  897. __le32 buf[6];
  898. u32 len, len2, ncons, nel;
  899. int i, rc;
  900. cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
  901. if (!cladatum) {
  902. rc = -ENOMEM;
  903. goto out;
  904. }
  905. rc = next_entry(buf, fp, sizeof(u32)*6);
  906. if (rc < 0)
  907. goto bad;
  908. len = le32_to_cpu(buf[0]);
  909. len2 = le32_to_cpu(buf[1]);
  910. cladatum->value = le32_to_cpu(buf[2]);
  911. rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
  912. if (rc)
  913. goto bad;
  914. cladatum->permissions.nprim = le32_to_cpu(buf[3]);
  915. nel = le32_to_cpu(buf[4]);
  916. ncons = le32_to_cpu(buf[5]);
  917. key = kmalloc(len + 1,GFP_KERNEL);
  918. if (!key) {
  919. rc = -ENOMEM;
  920. goto bad;
  921. }
  922. rc = next_entry(key, fp, len);
  923. if (rc < 0)
  924. goto bad;
  925. key[len] = 0;
  926. if (len2) {
  927. cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
  928. if (!cladatum->comkey) {
  929. rc = -ENOMEM;
  930. goto bad;
  931. }
  932. rc = next_entry(cladatum->comkey, fp, len2);
  933. if (rc < 0)
  934. goto bad;
  935. cladatum->comkey[len2] = 0;
  936. cladatum->comdatum = hashtab_search(p->p_commons.table,
  937. cladatum->comkey);
  938. if (!cladatum->comdatum) {
  939. printk(KERN_ERR "security: unknown common %s\n",
  940. cladatum->comkey);
  941. rc = -EINVAL;
  942. goto bad;
  943. }
  944. }
  945. for (i = 0; i < nel; i++) {
  946. rc = perm_read(p, cladatum->permissions.table, fp);
  947. if (rc)
  948. goto bad;
  949. }
  950. rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
  951. if (rc)
  952. goto bad;
  953. if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
  954. /* grab the validatetrans rules */
  955. rc = next_entry(buf, fp, sizeof(u32));
  956. if (rc < 0)
  957. goto bad;
  958. ncons = le32_to_cpu(buf[0]);
  959. rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
  960. if (rc)
  961. goto bad;
  962. }
  963. rc = hashtab_insert(h, key, cladatum);
  964. if (rc)
  965. goto bad;
  966. rc = 0;
  967. out:
  968. return rc;
  969. bad:
  970. class_destroy(key, cladatum, NULL);
  971. goto out;
  972. }
  973. static int role_read(struct policydb *p, struct hashtab *h, void *fp)
  974. {
  975. char *key = NULL;
  976. struct role_datum *role;
  977. int rc;
  978. __le32 buf[2];
  979. u32 len;
  980. role = kzalloc(sizeof(*role), GFP_KERNEL);
  981. if (!role) {
  982. rc = -ENOMEM;
  983. goto out;
  984. }
  985. rc = next_entry(buf, fp, sizeof buf);
  986. if (rc < 0)
  987. goto bad;
  988. len = le32_to_cpu(buf[0]);
  989. role->value = le32_to_cpu(buf[1]);
  990. key = kmalloc(len + 1,GFP_KERNEL);
  991. if (!key) {
  992. rc = -ENOMEM;
  993. goto bad;
  994. }
  995. rc = next_entry(key, fp, len);
  996. if (rc < 0)
  997. goto bad;
  998. key[len] = 0;
  999. rc = ebitmap_read(&role->dominates, fp);
  1000. if (rc)
  1001. goto bad;
  1002. rc = ebitmap_read(&role->types, fp);
  1003. if (rc)
  1004. goto bad;
  1005. if (strcmp(key, OBJECT_R) == 0) {
  1006. if (role->value != OBJECT_R_VAL) {
  1007. printk(KERN_ERR "Role %s has wrong value %d\n",
  1008. OBJECT_R, role->value);
  1009. rc = -EINVAL;
  1010. goto bad;
  1011. }
  1012. rc = 0;
  1013. goto bad;
  1014. }
  1015. rc = hashtab_insert(h, key, role);
  1016. if (rc)
  1017. goto bad;
  1018. out:
  1019. return rc;
  1020. bad:
  1021. role_destroy(key, role, NULL);
  1022. goto out;
  1023. }
  1024. static int type_read(struct policydb *p, struct hashtab *h, void *fp)
  1025. {
  1026. char *key = NULL;
  1027. struct type_datum *typdatum;
  1028. int rc;
  1029. __le32 buf[3];
  1030. u32 len;
  1031. typdatum = kzalloc(sizeof(*typdatum),GFP_KERNEL);
  1032. if (!typdatum) {
  1033. rc = -ENOMEM;
  1034. return rc;
  1035. }
  1036. rc = next_entry(buf, fp, sizeof buf);
  1037. if (rc < 0)
  1038. goto bad;
  1039. len = le32_to_cpu(buf[0]);
  1040. typdatum->value = le32_to_cpu(buf[1]);
  1041. typdatum->primary = le32_to_cpu(buf[2]);
  1042. key = kmalloc(len + 1,GFP_KERNEL);
  1043. if (!key) {
  1044. rc = -ENOMEM;
  1045. goto bad;
  1046. }
  1047. rc = next_entry(key, fp, len);
  1048. if (rc < 0)
  1049. goto bad;
  1050. key[len] = 0;
  1051. rc = hashtab_insert(h, key, typdatum);
  1052. if (rc)
  1053. goto bad;
  1054. out:
  1055. return rc;
  1056. bad:
  1057. type_destroy(key, typdatum, NULL);
  1058. goto out;
  1059. }
  1060. /*
  1061. * Read a MLS level structure from a policydb binary
  1062. * representation file.
  1063. */
  1064. static int mls_read_level(struct mls_level *lp, void *fp)
  1065. {
  1066. __le32 buf[1];
  1067. int rc;
  1068. memset(lp, 0, sizeof(*lp));
  1069. rc = next_entry(buf, fp, sizeof buf);
  1070. if (rc < 0) {
  1071. printk(KERN_ERR "security: mls: truncated level\n");
  1072. goto bad;
  1073. }
  1074. lp->sens = le32_to_cpu(buf[0]);
  1075. if (ebitmap_read(&lp->cat, fp)) {
  1076. printk(KERN_ERR "security: mls: error reading level "
  1077. "categories\n");
  1078. goto bad;
  1079. }
  1080. return 0;
  1081. bad:
  1082. return -EINVAL;
  1083. }
  1084. static int user_read(struct policydb *p, struct hashtab *h, void *fp)
  1085. {
  1086. char *key = NULL;
  1087. struct user_datum *usrdatum;
  1088. int rc;
  1089. __le32 buf[2];
  1090. u32 len;
  1091. usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
  1092. if (!usrdatum) {
  1093. rc = -ENOMEM;
  1094. goto out;
  1095. }
  1096. rc = next_entry(buf, fp, sizeof buf);
  1097. if (rc < 0)
  1098. goto bad;
  1099. len = le32_to_cpu(buf[0]);
  1100. usrdatum->value = le32_to_cpu(buf[1]);
  1101. key = kmalloc(len + 1,GFP_KERNEL);
  1102. if (!key) {
  1103. rc = -ENOMEM;
  1104. goto bad;
  1105. }
  1106. rc = next_entry(key, fp, len);
  1107. if (rc < 0)
  1108. goto bad;
  1109. key[len] = 0;
  1110. rc = ebitmap_read(&usrdatum->roles, fp);
  1111. if (rc)
  1112. goto bad;
  1113. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1114. rc = mls_read_range_helper(&usrdatum->range, fp);
  1115. if (rc)
  1116. goto bad;
  1117. rc = mls_read_level(&usrdatum->dfltlevel, fp);
  1118. if (rc)
  1119. goto bad;
  1120. }
  1121. rc = hashtab_insert(h, key, usrdatum);
  1122. if (rc)
  1123. goto bad;
  1124. out:
  1125. return rc;
  1126. bad:
  1127. user_destroy(key, usrdatum, NULL);
  1128. goto out;
  1129. }
  1130. static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
  1131. {
  1132. char *key = NULL;
  1133. struct level_datum *levdatum;
  1134. int rc;
  1135. __le32 buf[2];
  1136. u32 len;
  1137. levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
  1138. if (!levdatum) {
  1139. rc = -ENOMEM;
  1140. goto out;
  1141. }
  1142. rc = next_entry(buf, fp, sizeof buf);
  1143. if (rc < 0)
  1144. goto bad;
  1145. len = le32_to_cpu(buf[0]);
  1146. levdatum->isalias = le32_to_cpu(buf[1]);
  1147. key = kmalloc(len + 1,GFP_ATOMIC);
  1148. if (!key) {
  1149. rc = -ENOMEM;
  1150. goto bad;
  1151. }
  1152. rc = next_entry(key, fp, len);
  1153. if (rc < 0)
  1154. goto bad;
  1155. key[len] = 0;
  1156. levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
  1157. if (!levdatum->level) {
  1158. rc = -ENOMEM;
  1159. goto bad;
  1160. }
  1161. if (mls_read_level(levdatum->level, fp)) {
  1162. rc = -EINVAL;
  1163. goto bad;
  1164. }
  1165. rc = hashtab_insert(h, key, levdatum);
  1166. if (rc)
  1167. goto bad;
  1168. out:
  1169. return rc;
  1170. bad:
  1171. sens_destroy(key, levdatum, NULL);
  1172. goto out;
  1173. }
  1174. static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
  1175. {
  1176. char *key = NULL;
  1177. struct cat_datum *catdatum;
  1178. int rc;
  1179. __le32 buf[3];
  1180. u32 len;
  1181. catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
  1182. if (!catdatum) {
  1183. rc = -ENOMEM;
  1184. goto out;
  1185. }
  1186. rc = next_entry(buf, fp, sizeof buf);
  1187. if (rc < 0)
  1188. goto bad;
  1189. len = le32_to_cpu(buf[0]);
  1190. catdatum->value = le32_to_cpu(buf[1]);
  1191. catdatum->isalias = le32_to_cpu(buf[2]);
  1192. key = kmalloc(len + 1,GFP_ATOMIC);
  1193. if (!key) {
  1194. rc = -ENOMEM;
  1195. goto bad;
  1196. }
  1197. rc = next_entry(key, fp, len);
  1198. if (rc < 0)
  1199. goto bad;
  1200. key[len] = 0;
  1201. rc = hashtab_insert(h, key, catdatum);
  1202. if (rc)
  1203. goto bad;
  1204. out:
  1205. return rc;
  1206. bad:
  1207. cat_destroy(key, catdatum, NULL);
  1208. goto out;
  1209. }
  1210. static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
  1211. {
  1212. common_read,
  1213. class_read,
  1214. role_read,
  1215. type_read,
  1216. user_read,
  1217. cond_read_bool,
  1218. sens_read,
  1219. cat_read,
  1220. };
  1221. extern int ss_initialized;
  1222. /*
  1223. * Read the configuration data from a policy database binary
  1224. * representation file into a policy database structure.
  1225. */
  1226. int policydb_read(struct policydb *p, void *fp)
  1227. {
  1228. struct role_allow *ra, *lra;
  1229. struct role_trans *tr, *ltr;
  1230. struct ocontext *l, *c, *newc;
  1231. struct genfs *genfs_p, *genfs, *newgenfs;
  1232. int i, j, rc;
  1233. __le32 buf[8];
  1234. u32 len, len2, config, nprim, nel, nel2;
  1235. char *policydb_str;
  1236. struct policydb_compat_info *info;
  1237. struct range_trans *rt, *lrt;
  1238. config = 0;
  1239. rc = policydb_init(p);
  1240. if (rc)
  1241. goto out;
  1242. /* Read the magic number and string length. */
  1243. rc = next_entry(buf, fp, sizeof(u32)* 2);
  1244. if (rc < 0)
  1245. goto bad;
  1246. if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
  1247. printk(KERN_ERR "security: policydb magic number 0x%x does "
  1248. "not match expected magic number 0x%x\n",
  1249. le32_to_cpu(buf[0]), POLICYDB_MAGIC);
  1250. goto bad;
  1251. }
  1252. len = le32_to_cpu(buf[1]);
  1253. if (len != strlen(POLICYDB_STRING)) {
  1254. printk(KERN_ERR "security: policydb string length %d does not "
  1255. "match expected length %Zu\n",
  1256. len, strlen(POLICYDB_STRING));
  1257. goto bad;
  1258. }
  1259. policydb_str = kmalloc(len + 1,GFP_KERNEL);
  1260. if (!policydb_str) {
  1261. printk(KERN_ERR "security: unable to allocate memory for policydb "
  1262. "string of length %d\n", len);
  1263. rc = -ENOMEM;
  1264. goto bad;
  1265. }
  1266. rc = next_entry(policydb_str, fp, len);
  1267. if (rc < 0) {
  1268. printk(KERN_ERR "security: truncated policydb string identifier\n");
  1269. kfree(policydb_str);
  1270. goto bad;
  1271. }
  1272. policydb_str[len] = 0;
  1273. if (strcmp(policydb_str, POLICYDB_STRING)) {
  1274. printk(KERN_ERR "security: policydb string %s does not match "
  1275. "my string %s\n", policydb_str, POLICYDB_STRING);
  1276. kfree(policydb_str);
  1277. goto bad;
  1278. }
  1279. /* Done with policydb_str. */
  1280. kfree(policydb_str);
  1281. policydb_str = NULL;
  1282. /* Read the version, config, and table sizes. */
  1283. rc = next_entry(buf, fp, sizeof(u32)*4);
  1284. if (rc < 0)
  1285. goto bad;
  1286. p->policyvers = le32_to_cpu(buf[0]);
  1287. if (p->policyvers < POLICYDB_VERSION_MIN ||
  1288. p->policyvers > POLICYDB_VERSION_MAX) {
  1289. printk(KERN_ERR "security: policydb version %d does not match "
  1290. "my version range %d-%d\n",
  1291. le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
  1292. goto bad;
  1293. }
  1294. if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
  1295. if (ss_initialized && !selinux_mls_enabled) {
  1296. printk(KERN_ERR "Cannot switch between non-MLS and MLS "
  1297. "policies\n");
  1298. goto bad;
  1299. }
  1300. selinux_mls_enabled = 1;
  1301. config |= POLICYDB_CONFIG_MLS;
  1302. if (p->policyvers < POLICYDB_VERSION_MLS) {
  1303. printk(KERN_ERR "security policydb version %d (MLS) "
  1304. "not backwards compatible\n", p->policyvers);
  1305. goto bad;
  1306. }
  1307. } else {
  1308. if (ss_initialized && selinux_mls_enabled) {
  1309. printk(KERN_ERR "Cannot switch between MLS and non-MLS "
  1310. "policies\n");
  1311. goto bad;
  1312. }
  1313. }
  1314. info = policydb_lookup_compat(p->policyvers);
  1315. if (!info) {
  1316. printk(KERN_ERR "security: unable to find policy compat info "
  1317. "for version %d\n", p->policyvers);
  1318. goto bad;
  1319. }
  1320. if (le32_to_cpu(buf[2]) != info->sym_num ||
  1321. le32_to_cpu(buf[3]) != info->ocon_num) {
  1322. printk(KERN_ERR "security: policydb table sizes (%d,%d) do "
  1323. "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
  1324. le32_to_cpu(buf[3]),
  1325. info->sym_num, info->ocon_num);
  1326. goto bad;
  1327. }
  1328. for (i = 0; i < info->sym_num; i++) {
  1329. rc = next_entry(buf, fp, sizeof(u32)*2);
  1330. if (rc < 0)
  1331. goto bad;
  1332. nprim = le32_to_cpu(buf[0]);
  1333. nel = le32_to_cpu(buf[1]);
  1334. for (j = 0; j < nel; j++) {
  1335. rc = read_f[i](p, p->symtab[i].table, fp);
  1336. if (rc)
  1337. goto bad;
  1338. }
  1339. p->symtab[i].nprim = nprim;
  1340. }
  1341. rc = avtab_read(&p->te_avtab, fp, p->policyvers);
  1342. if (rc)
  1343. goto bad;
  1344. if (p->policyvers >= POLICYDB_VERSION_BOOL) {
  1345. rc = cond_read_list(p, fp);
  1346. if (rc)
  1347. goto bad;
  1348. }
  1349. rc = next_entry(buf, fp, sizeof(u32));
  1350. if (rc < 0)
  1351. goto bad;
  1352. nel = le32_to_cpu(buf[0]);
  1353. ltr = NULL;
  1354. for (i = 0; i < nel; i++) {
  1355. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  1356. if (!tr) {
  1357. rc = -ENOMEM;
  1358. goto bad;
  1359. }
  1360. if (ltr) {
  1361. ltr->next = tr;
  1362. } else {
  1363. p->role_tr = tr;
  1364. }
  1365. rc = next_entry(buf, fp, sizeof(u32)*3);
  1366. if (rc < 0)
  1367. goto bad;
  1368. tr->role = le32_to_cpu(buf[0]);
  1369. tr->type = le32_to_cpu(buf[1]);
  1370. tr->new_role = le32_to_cpu(buf[2]);
  1371. ltr = tr;
  1372. }
  1373. rc = next_entry(buf, fp, sizeof(u32));
  1374. if (rc < 0)
  1375. goto bad;
  1376. nel = le32_to_cpu(buf[0]);
  1377. lra = NULL;
  1378. for (i = 0; i < nel; i++) {
  1379. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  1380. if (!ra) {
  1381. rc = -ENOMEM;
  1382. goto bad;
  1383. }
  1384. if (lra) {
  1385. lra->next = ra;
  1386. } else {
  1387. p->role_allow = ra;
  1388. }
  1389. rc = next_entry(buf, fp, sizeof(u32)*2);
  1390. if (rc < 0)
  1391. goto bad;
  1392. ra->role = le32_to_cpu(buf[0]);
  1393. ra->new_role = le32_to_cpu(buf[1]);
  1394. lra = ra;
  1395. }
  1396. rc = policydb_index_classes(p);
  1397. if (rc)
  1398. goto bad;
  1399. rc = policydb_index_others(p);
  1400. if (rc)
  1401. goto bad;
  1402. for (i = 0; i < info->ocon_num; i++) {
  1403. rc = next_entry(buf, fp, sizeof(u32));
  1404. if (rc < 0)
  1405. goto bad;
  1406. nel = le32_to_cpu(buf[0]);
  1407. l = NULL;
  1408. for (j = 0; j < nel; j++) {
  1409. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1410. if (!c) {
  1411. rc = -ENOMEM;
  1412. goto bad;
  1413. }
  1414. if (l) {
  1415. l->next = c;
  1416. } else {
  1417. p->ocontexts[i] = c;
  1418. }
  1419. l = c;
  1420. rc = -EINVAL;
  1421. switch (i) {
  1422. case OCON_ISID:
  1423. rc = next_entry(buf, fp, sizeof(u32));
  1424. if (rc < 0)
  1425. goto bad;
  1426. c->sid[0] = le32_to_cpu(buf[0]);
  1427. rc = context_read_and_validate(&c->context[0], p, fp);
  1428. if (rc)
  1429. goto bad;
  1430. break;
  1431. case OCON_FS:
  1432. case OCON_NETIF:
  1433. rc = next_entry(buf, fp, sizeof(u32));
  1434. if (rc < 0)
  1435. goto bad;
  1436. len = le32_to_cpu(buf[0]);
  1437. c->u.name = kmalloc(len + 1,GFP_KERNEL);
  1438. if (!c->u.name) {
  1439. rc = -ENOMEM;
  1440. goto bad;
  1441. }
  1442. rc = next_entry(c->u.name, fp, len);
  1443. if (rc < 0)
  1444. goto bad;
  1445. c->u.name[len] = 0;
  1446. rc = context_read_and_validate(&c->context[0], p, fp);
  1447. if (rc)
  1448. goto bad;
  1449. rc = context_read_and_validate(&c->context[1], p, fp);
  1450. if (rc)
  1451. goto bad;
  1452. break;
  1453. case OCON_PORT:
  1454. rc = next_entry(buf, fp, sizeof(u32)*3);
  1455. if (rc < 0)
  1456. goto bad;
  1457. c->u.port.protocol = le32_to_cpu(buf[0]);
  1458. c->u.port.low_port = le32_to_cpu(buf[1]);
  1459. c->u.port.high_port = le32_to_cpu(buf[2]);
  1460. rc = context_read_and_validate(&c->context[0], p, fp);
  1461. if (rc)
  1462. goto bad;
  1463. break;
  1464. case OCON_NODE:
  1465. rc = next_entry(buf, fp, sizeof(u32)* 2);
  1466. if (rc < 0)
  1467. goto bad;
  1468. c->u.node.addr = le32_to_cpu(buf[0]);
  1469. c->u.node.mask = le32_to_cpu(buf[1]);
  1470. rc = context_read_and_validate(&c->context[0], p, fp);
  1471. if (rc)
  1472. goto bad;
  1473. break;
  1474. case OCON_FSUSE:
  1475. rc = next_entry(buf, fp, sizeof(u32)*2);
  1476. if (rc < 0)
  1477. goto bad;
  1478. c->v.behavior = le32_to_cpu(buf[0]);
  1479. if (c->v.behavior > SECURITY_FS_USE_NONE)
  1480. goto bad;
  1481. len = le32_to_cpu(buf[1]);
  1482. c->u.name = kmalloc(len + 1,GFP_KERNEL);
  1483. if (!c->u.name) {
  1484. rc = -ENOMEM;
  1485. goto bad;
  1486. }
  1487. rc = next_entry(c->u.name, fp, len);
  1488. if (rc < 0)
  1489. goto bad;
  1490. c->u.name[len] = 0;
  1491. rc = context_read_and_validate(&c->context[0], p, fp);
  1492. if (rc)
  1493. goto bad;
  1494. break;
  1495. case OCON_NODE6: {
  1496. int k;
  1497. rc = next_entry(buf, fp, sizeof(u32) * 8);
  1498. if (rc < 0)
  1499. goto bad;
  1500. for (k = 0; k < 4; k++)
  1501. c->u.node6.addr[k] = le32_to_cpu(buf[k]);
  1502. for (k = 0; k < 4; k++)
  1503. c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
  1504. if (context_read_and_validate(&c->context[0], p, fp))
  1505. goto bad;
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. rc = next_entry(buf, fp, sizeof(u32));
  1512. if (rc < 0)
  1513. goto bad;
  1514. nel = le32_to_cpu(buf[0]);
  1515. genfs_p = NULL;
  1516. rc = -EINVAL;
  1517. for (i = 0; i < nel; i++) {
  1518. rc = next_entry(buf, fp, sizeof(u32));
  1519. if (rc < 0)
  1520. goto bad;
  1521. len = le32_to_cpu(buf[0]);
  1522. newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
  1523. if (!newgenfs) {
  1524. rc = -ENOMEM;
  1525. goto bad;
  1526. }
  1527. newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
  1528. if (!newgenfs->fstype) {
  1529. rc = -ENOMEM;
  1530. kfree(newgenfs);
  1531. goto bad;
  1532. }
  1533. rc = next_entry(newgenfs->fstype, fp, len);
  1534. if (rc < 0) {
  1535. kfree(newgenfs->fstype);
  1536. kfree(newgenfs);
  1537. goto bad;
  1538. }
  1539. newgenfs->fstype[len] = 0;
  1540. for (genfs_p = NULL, genfs = p->genfs; genfs;
  1541. genfs_p = genfs, genfs = genfs->next) {
  1542. if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
  1543. printk(KERN_ERR "security: dup genfs "
  1544. "fstype %s\n", newgenfs->fstype);
  1545. kfree(newgenfs->fstype);
  1546. kfree(newgenfs);
  1547. goto bad;
  1548. }
  1549. if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
  1550. break;
  1551. }
  1552. newgenfs->next = genfs;
  1553. if (genfs_p)
  1554. genfs_p->next = newgenfs;
  1555. else
  1556. p->genfs = newgenfs;
  1557. rc = next_entry(buf, fp, sizeof(u32));
  1558. if (rc < 0)
  1559. goto bad;
  1560. nel2 = le32_to_cpu(buf[0]);
  1561. for (j = 0; j < nel2; j++) {
  1562. rc = next_entry(buf, fp, sizeof(u32));
  1563. if (rc < 0)
  1564. goto bad;
  1565. len = le32_to_cpu(buf[0]);
  1566. newc = kzalloc(sizeof(*newc), GFP_KERNEL);
  1567. if (!newc) {
  1568. rc = -ENOMEM;
  1569. goto bad;
  1570. }
  1571. newc->u.name = kmalloc(len + 1,GFP_KERNEL);
  1572. if (!newc->u.name) {
  1573. rc = -ENOMEM;
  1574. goto bad_newc;
  1575. }
  1576. rc = next_entry(newc->u.name, fp, len);
  1577. if (rc < 0)
  1578. goto bad_newc;
  1579. newc->u.name[len] = 0;
  1580. rc = next_entry(buf, fp, sizeof(u32));
  1581. if (rc < 0)
  1582. goto bad_newc;
  1583. newc->v.sclass = le32_to_cpu(buf[0]);
  1584. if (context_read_and_validate(&newc->context[0], p, fp))
  1585. goto bad_newc;
  1586. for (l = NULL, c = newgenfs->head; c;
  1587. l = c, c = c->next) {
  1588. if (!strcmp(newc->u.name, c->u.name) &&
  1589. (!c->v.sclass || !newc->v.sclass ||
  1590. newc->v.sclass == c->v.sclass)) {
  1591. printk(KERN_ERR "security: dup genfs "
  1592. "entry (%s,%s)\n",
  1593. newgenfs->fstype, c->u.name);
  1594. goto bad_newc;
  1595. }
  1596. len = strlen(newc->u.name);
  1597. len2 = strlen(c->u.name);
  1598. if (len > len2)
  1599. break;
  1600. }
  1601. newc->next = c;
  1602. if (l)
  1603. l->next = newc;
  1604. else
  1605. newgenfs->head = newc;
  1606. }
  1607. }
  1608. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1609. rc = next_entry(buf, fp, sizeof(u32));
  1610. if (rc < 0)
  1611. goto bad;
  1612. nel = le32_to_cpu(buf[0]);
  1613. lrt = NULL;
  1614. for (i = 0; i < nel; i++) {
  1615. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  1616. if (!rt) {
  1617. rc = -ENOMEM;
  1618. goto bad;
  1619. }
  1620. if (lrt)
  1621. lrt->next = rt;
  1622. else
  1623. p->range_tr = rt;
  1624. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1625. if (rc < 0)
  1626. goto bad;
  1627. rt->dom = le32_to_cpu(buf[0]);
  1628. rt->type = le32_to_cpu(buf[1]);
  1629. rc = mls_read_range_helper(&rt->range, fp);
  1630. if (rc)
  1631. goto bad;
  1632. lrt = rt;
  1633. }
  1634. }
  1635. p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
  1636. if (!p->type_attr_map)
  1637. goto bad;
  1638. for (i = 0; i < p->p_types.nprim; i++) {
  1639. ebitmap_init(&p->type_attr_map[i]);
  1640. if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
  1641. if (ebitmap_read(&p->type_attr_map[i], fp))
  1642. goto bad;
  1643. }
  1644. /* add the type itself as the degenerate case */
  1645. if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
  1646. goto bad;
  1647. }
  1648. rc = 0;
  1649. out:
  1650. return rc;
  1651. bad_newc:
  1652. ocontext_destroy(newc,OCON_FSUSE);
  1653. bad:
  1654. if (!rc)
  1655. rc = -EINVAL;
  1656. policydb_destroy(p);
  1657. goto out;
  1658. }