policydb.c 38 KB

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