policydb.c 44 KB

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