policydb.c 37 KB

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