policydb.c 46 KB

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