policydb.c 38 KB

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