policydb.c 46 KB

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