policydb.c 38 KB

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