policydb.c 46 KB

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