policydb.c 40 KB

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