policydb.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. /*
  2. * Implementation of the policy database.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /*
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. *
  11. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  12. *
  13. * Added conditional policy language extensions
  14. *
  15. * Updated: Hewlett-Packard <paul.moore@hp.com>
  16. *
  17. * Added support for the policy capability bitmap
  18. *
  19. * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  20. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  21. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation, version 2.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/errno.h>
  31. #include <linux/audit.h>
  32. #include "security.h"
  33. #include "policydb.h"
  34. #include "conditional.h"
  35. #include "mls.h"
  36. #define _DEBUG_HASHES
  37. #ifdef DEBUG_HASHES
  38. static char *symtab_name[SYM_NUM] = {
  39. "common prefixes",
  40. "classes",
  41. "roles",
  42. "types",
  43. "users",
  44. "bools",
  45. "levels",
  46. "categories",
  47. };
  48. #endif
  49. 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 = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
  148. if (!key) {
  149. rc = -ENOMEM;
  150. goto out_free_role;
  151. }
  152. strcpy(key, OBJECT_R);
  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. context_destroy(&c->context[0]);
  573. context_destroy(&c->context[1]);
  574. if (i == OCON_ISID || i == OCON_FS ||
  575. i == OCON_NETIF || i == OCON_FSUSE)
  576. kfree(c->u.name);
  577. kfree(c);
  578. }
  579. /*
  580. * Free any memory allocated by a policy database structure.
  581. */
  582. void policydb_destroy(struct policydb *p)
  583. {
  584. struct ocontext *c, *ctmp;
  585. struct genfs *g, *gtmp;
  586. int i;
  587. struct role_allow *ra, *lra = NULL;
  588. struct role_trans *tr, *ltr = NULL;
  589. for (i = 0; i < SYM_NUM; i++) {
  590. cond_resched();
  591. hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
  592. hashtab_destroy(p->symtab[i].table);
  593. }
  594. for (i = 0; i < SYM_NUM; i++)
  595. kfree(p->sym_val_to_name[i]);
  596. kfree(p->class_val_to_struct);
  597. kfree(p->role_val_to_struct);
  598. kfree(p->user_val_to_struct);
  599. kfree(p->type_val_to_struct);
  600. avtab_destroy(&p->te_avtab);
  601. for (i = 0; i < OCON_NUM; i++) {
  602. cond_resched();
  603. c = p->ocontexts[i];
  604. while (c) {
  605. ctmp = c;
  606. c = c->next;
  607. ocontext_destroy(ctmp, i);
  608. }
  609. p->ocontexts[i] = NULL;
  610. }
  611. g = p->genfs;
  612. while (g) {
  613. cond_resched();
  614. kfree(g->fstype);
  615. c = g->head;
  616. while (c) {
  617. ctmp = c;
  618. c = c->next;
  619. ocontext_destroy(ctmp, OCON_FSUSE);
  620. }
  621. gtmp = g;
  622. g = g->next;
  623. kfree(gtmp);
  624. }
  625. p->genfs = NULL;
  626. cond_policydb_destroy(p);
  627. for (tr = p->role_tr; tr; tr = tr->next) {
  628. cond_resched();
  629. kfree(ltr);
  630. ltr = tr;
  631. }
  632. kfree(ltr);
  633. for (ra = p->role_allow; ra; ra = ra->next) {
  634. cond_resched();
  635. kfree(lra);
  636. lra = ra;
  637. }
  638. kfree(lra);
  639. hashtab_map(p->range_tr, range_tr_destroy, NULL);
  640. hashtab_destroy(p->range_tr);
  641. if (p->type_attr_map) {
  642. for (i = 0; i < p->p_types.nprim; i++)
  643. ebitmap_destroy(&p->type_attr_map[i]);
  644. }
  645. kfree(p->type_attr_map);
  646. ebitmap_destroy(&p->policycaps);
  647. ebitmap_destroy(&p->permissive_map);
  648. return;
  649. }
  650. /*
  651. * Load the initial SIDs specified in a policy database
  652. * structure into a SID table.
  653. */
  654. int policydb_load_isids(struct policydb *p, struct sidtab *s)
  655. {
  656. struct ocontext *head, *c;
  657. int rc;
  658. rc = sidtab_init(s);
  659. if (rc) {
  660. printk(KERN_ERR "SELinux: out of memory on SID table init\n");
  661. goto out;
  662. }
  663. head = p->ocontexts[OCON_ISID];
  664. for (c = head; c; c = c->next) {
  665. if (!c->context[0].user) {
  666. printk(KERN_ERR "SELinux: SID %s was never "
  667. "defined.\n", c->u.name);
  668. rc = -EINVAL;
  669. goto out;
  670. }
  671. if (sidtab_insert(s, c->sid[0], &c->context[0])) {
  672. printk(KERN_ERR "SELinux: unable to load initial "
  673. "SID %s.\n", c->u.name);
  674. rc = -EINVAL;
  675. goto out;
  676. }
  677. }
  678. out:
  679. return rc;
  680. }
  681. int policydb_class_isvalid(struct policydb *p, unsigned int class)
  682. {
  683. if (!class || class > p->p_classes.nprim)
  684. return 0;
  685. return 1;
  686. }
  687. int policydb_role_isvalid(struct policydb *p, unsigned int role)
  688. {
  689. if (!role || role > p->p_roles.nprim)
  690. return 0;
  691. return 1;
  692. }
  693. int policydb_type_isvalid(struct policydb *p, unsigned int type)
  694. {
  695. if (!type || type > p->p_types.nprim)
  696. return 0;
  697. return 1;
  698. }
  699. /*
  700. * Return 1 if the fields in the security context
  701. * structure `c' are valid. Return 0 otherwise.
  702. */
  703. int policydb_context_isvalid(struct policydb *p, struct context *c)
  704. {
  705. struct role_datum *role;
  706. struct user_datum *usrdatum;
  707. if (!c->role || c->role > p->p_roles.nprim)
  708. return 0;
  709. if (!c->user || c->user > p->p_users.nprim)
  710. return 0;
  711. if (!c->type || c->type > p->p_types.nprim)
  712. return 0;
  713. if (c->role != OBJECT_R_VAL) {
  714. /*
  715. * Role must be authorized for the type.
  716. */
  717. role = p->role_val_to_struct[c->role - 1];
  718. if (!ebitmap_get_bit(&role->types,
  719. c->type - 1))
  720. /* role may not be associated with type */
  721. return 0;
  722. /*
  723. * User must be authorized for the role.
  724. */
  725. usrdatum = p->user_val_to_struct[c->user - 1];
  726. if (!usrdatum)
  727. return 0;
  728. if (!ebitmap_get_bit(&usrdatum->roles,
  729. c->role - 1))
  730. /* user may not be associated with role */
  731. return 0;
  732. }
  733. if (!mls_context_isvalid(p, c))
  734. return 0;
  735. return 1;
  736. }
  737. /*
  738. * Read a MLS range structure from a policydb binary
  739. * representation file.
  740. */
  741. static int mls_read_range_helper(struct mls_range *r, void *fp)
  742. {
  743. __le32 buf[2];
  744. u32 items;
  745. int rc;
  746. rc = next_entry(buf, fp, sizeof(u32));
  747. if (rc < 0)
  748. goto out;
  749. items = le32_to_cpu(buf[0]);
  750. if (items > ARRAY_SIZE(buf)) {
  751. printk(KERN_ERR "SELinux: mls: range overflow\n");
  752. rc = -EINVAL;
  753. goto out;
  754. }
  755. rc = next_entry(buf, fp, sizeof(u32) * items);
  756. if (rc < 0) {
  757. printk(KERN_ERR "SELinux: mls: truncated range\n");
  758. goto out;
  759. }
  760. r->level[0].sens = le32_to_cpu(buf[0]);
  761. if (items > 1)
  762. r->level[1].sens = le32_to_cpu(buf[1]);
  763. else
  764. r->level[1].sens = r->level[0].sens;
  765. rc = ebitmap_read(&r->level[0].cat, fp);
  766. if (rc) {
  767. printk(KERN_ERR "SELinux: mls: error reading low "
  768. "categories\n");
  769. goto out;
  770. }
  771. if (items > 1) {
  772. rc = ebitmap_read(&r->level[1].cat, fp);
  773. if (rc) {
  774. printk(KERN_ERR "SELinux: mls: error reading high "
  775. "categories\n");
  776. goto bad_high;
  777. }
  778. } else {
  779. rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
  780. if (rc) {
  781. printk(KERN_ERR "SELinux: mls: out of memory\n");
  782. goto bad_high;
  783. }
  784. }
  785. rc = 0;
  786. out:
  787. return rc;
  788. bad_high:
  789. ebitmap_destroy(&r->level[0].cat);
  790. goto out;
  791. }
  792. /*
  793. * Read and validate a security context structure
  794. * from a policydb binary representation file.
  795. */
  796. static int context_read_and_validate(struct context *c,
  797. struct policydb *p,
  798. void *fp)
  799. {
  800. __le32 buf[3];
  801. int rc;
  802. rc = next_entry(buf, fp, sizeof buf);
  803. if (rc < 0) {
  804. printk(KERN_ERR "SELinux: context truncated\n");
  805. goto out;
  806. }
  807. c->user = le32_to_cpu(buf[0]);
  808. c->role = le32_to_cpu(buf[1]);
  809. c->type = le32_to_cpu(buf[2]);
  810. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  811. if (mls_read_range_helper(&c->range, fp)) {
  812. printk(KERN_ERR "SELinux: error reading MLS range of "
  813. "context\n");
  814. rc = -EINVAL;
  815. goto out;
  816. }
  817. }
  818. if (!policydb_context_isvalid(p, c)) {
  819. printk(KERN_ERR "SELinux: invalid security context\n");
  820. context_destroy(c);
  821. rc = -EINVAL;
  822. }
  823. out:
  824. return rc;
  825. }
  826. /*
  827. * The following *_read functions are used to
  828. * read the symbol data from a policy database
  829. * binary representation file.
  830. */
  831. static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
  832. {
  833. char *key = NULL;
  834. struct perm_datum *perdatum;
  835. int rc;
  836. __le32 buf[2];
  837. u32 len;
  838. perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
  839. if (!perdatum) {
  840. rc = -ENOMEM;
  841. goto out;
  842. }
  843. rc = next_entry(buf, fp, sizeof buf);
  844. if (rc < 0)
  845. goto bad;
  846. len = le32_to_cpu(buf[0]);
  847. perdatum->value = le32_to_cpu(buf[1]);
  848. key = kmalloc(len + 1, GFP_KERNEL);
  849. if (!key) {
  850. rc = -ENOMEM;
  851. goto bad;
  852. }
  853. rc = next_entry(key, fp, len);
  854. if (rc < 0)
  855. goto bad;
  856. key[len] = '\0';
  857. rc = hashtab_insert(h, key, perdatum);
  858. if (rc)
  859. goto bad;
  860. out:
  861. return rc;
  862. bad:
  863. perm_destroy(key, perdatum, NULL);
  864. goto out;
  865. }
  866. static int common_read(struct policydb *p, struct hashtab *h, void *fp)
  867. {
  868. char *key = NULL;
  869. struct common_datum *comdatum;
  870. __le32 buf[4];
  871. u32 len, nel;
  872. int i, rc;
  873. comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
  874. if (!comdatum) {
  875. rc = -ENOMEM;
  876. goto out;
  877. }
  878. rc = next_entry(buf, fp, sizeof buf);
  879. if (rc < 0)
  880. goto bad;
  881. len = le32_to_cpu(buf[0]);
  882. comdatum->value = le32_to_cpu(buf[1]);
  883. rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
  884. if (rc)
  885. goto bad;
  886. comdatum->permissions.nprim = le32_to_cpu(buf[2]);
  887. nel = le32_to_cpu(buf[3]);
  888. key = kmalloc(len + 1, GFP_KERNEL);
  889. if (!key) {
  890. rc = -ENOMEM;
  891. goto bad;
  892. }
  893. rc = next_entry(key, fp, len);
  894. if (rc < 0)
  895. goto bad;
  896. key[len] = '\0';
  897. for (i = 0; i < nel; i++) {
  898. rc = perm_read(p, comdatum->permissions.table, fp);
  899. if (rc)
  900. goto bad;
  901. }
  902. rc = hashtab_insert(h, key, comdatum);
  903. if (rc)
  904. goto bad;
  905. out:
  906. return rc;
  907. bad:
  908. common_destroy(key, comdatum, NULL);
  909. goto out;
  910. }
  911. static int read_cons_helper(struct constraint_node **nodep, int ncons,
  912. int allowxtarget, void *fp)
  913. {
  914. struct constraint_node *c, *lc;
  915. struct constraint_expr *e, *le;
  916. __le32 buf[3];
  917. u32 nexpr;
  918. int rc, i, j, depth;
  919. lc = NULL;
  920. for (i = 0; i < ncons; i++) {
  921. c = kzalloc(sizeof(*c), GFP_KERNEL);
  922. if (!c)
  923. return -ENOMEM;
  924. if (lc)
  925. lc->next = c;
  926. else
  927. *nodep = c;
  928. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  929. if (rc < 0)
  930. return rc;
  931. c->permissions = le32_to_cpu(buf[0]);
  932. nexpr = le32_to_cpu(buf[1]);
  933. le = NULL;
  934. depth = -1;
  935. for (j = 0; j < nexpr; j++) {
  936. e = kzalloc(sizeof(*e), GFP_KERNEL);
  937. if (!e)
  938. return -ENOMEM;
  939. if (le)
  940. le->next = e;
  941. else
  942. c->expr = e;
  943. rc = next_entry(buf, fp, (sizeof(u32) * 3));
  944. if (rc < 0)
  945. return rc;
  946. e->expr_type = le32_to_cpu(buf[0]);
  947. e->attr = le32_to_cpu(buf[1]);
  948. e->op = le32_to_cpu(buf[2]);
  949. switch (e->expr_type) {
  950. case CEXPR_NOT:
  951. if (depth < 0)
  952. return -EINVAL;
  953. break;
  954. case CEXPR_AND:
  955. case CEXPR_OR:
  956. if (depth < 1)
  957. return -EINVAL;
  958. depth--;
  959. break;
  960. case CEXPR_ATTR:
  961. if (depth == (CEXPR_MAXDEPTH - 1))
  962. return -EINVAL;
  963. depth++;
  964. break;
  965. case CEXPR_NAMES:
  966. if (!allowxtarget && (e->attr & CEXPR_XTARGET))
  967. return -EINVAL;
  968. if (depth == (CEXPR_MAXDEPTH - 1))
  969. return -EINVAL;
  970. depth++;
  971. if (ebitmap_read(&e->names, fp))
  972. return -EINVAL;
  973. break;
  974. default:
  975. return -EINVAL;
  976. }
  977. le = e;
  978. }
  979. if (depth != 0)
  980. return -EINVAL;
  981. lc = c;
  982. }
  983. return 0;
  984. }
  985. static int class_read(struct policydb *p, struct hashtab *h, void *fp)
  986. {
  987. char *key = NULL;
  988. struct class_datum *cladatum;
  989. __le32 buf[6];
  990. u32 len, len2, ncons, nel;
  991. int i, rc;
  992. cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
  993. if (!cladatum) {
  994. rc = -ENOMEM;
  995. goto out;
  996. }
  997. rc = next_entry(buf, fp, sizeof(u32)*6);
  998. if (rc < 0)
  999. goto bad;
  1000. len = le32_to_cpu(buf[0]);
  1001. len2 = le32_to_cpu(buf[1]);
  1002. cladatum->value = le32_to_cpu(buf[2]);
  1003. rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
  1004. if (rc)
  1005. goto bad;
  1006. cladatum->permissions.nprim = le32_to_cpu(buf[3]);
  1007. nel = le32_to_cpu(buf[4]);
  1008. ncons = le32_to_cpu(buf[5]);
  1009. key = kmalloc(len + 1, GFP_KERNEL);
  1010. if (!key) {
  1011. rc = -ENOMEM;
  1012. goto bad;
  1013. }
  1014. rc = next_entry(key, fp, len);
  1015. if (rc < 0)
  1016. goto bad;
  1017. key[len] = '\0';
  1018. if (len2) {
  1019. cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
  1020. if (!cladatum->comkey) {
  1021. rc = -ENOMEM;
  1022. goto bad;
  1023. }
  1024. rc = next_entry(cladatum->comkey, fp, len2);
  1025. if (rc < 0)
  1026. goto bad;
  1027. cladatum->comkey[len2] = '\0';
  1028. cladatum->comdatum = hashtab_search(p->p_commons.table,
  1029. cladatum->comkey);
  1030. if (!cladatum->comdatum) {
  1031. printk(KERN_ERR "SELinux: unknown common %s\n",
  1032. cladatum->comkey);
  1033. rc = -EINVAL;
  1034. goto bad;
  1035. }
  1036. }
  1037. for (i = 0; i < nel; i++) {
  1038. rc = perm_read(p, cladatum->permissions.table, fp);
  1039. if (rc)
  1040. goto bad;
  1041. }
  1042. rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
  1043. if (rc)
  1044. goto bad;
  1045. if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
  1046. /* grab the validatetrans rules */
  1047. rc = next_entry(buf, fp, sizeof(u32));
  1048. if (rc < 0)
  1049. goto bad;
  1050. ncons = le32_to_cpu(buf[0]);
  1051. rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
  1052. if (rc)
  1053. goto bad;
  1054. }
  1055. rc = hashtab_insert(h, key, cladatum);
  1056. if (rc)
  1057. goto bad;
  1058. rc = 0;
  1059. out:
  1060. return rc;
  1061. bad:
  1062. cls_destroy(key, cladatum, NULL);
  1063. goto out;
  1064. }
  1065. static int role_read(struct policydb *p, struct hashtab *h, void *fp)
  1066. {
  1067. char *key = NULL;
  1068. struct role_datum *role;
  1069. int rc, to_read = 2;
  1070. __le32 buf[3];
  1071. u32 len;
  1072. role = kzalloc(sizeof(*role), GFP_KERNEL);
  1073. if (!role) {
  1074. rc = -ENOMEM;
  1075. goto out;
  1076. }
  1077. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1078. to_read = 3;
  1079. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1080. if (rc < 0)
  1081. goto bad;
  1082. len = le32_to_cpu(buf[0]);
  1083. role->value = le32_to_cpu(buf[1]);
  1084. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1085. role->bounds = le32_to_cpu(buf[2]);
  1086. key = kmalloc(len + 1, GFP_KERNEL);
  1087. if (!key) {
  1088. rc = -ENOMEM;
  1089. goto bad;
  1090. }
  1091. rc = next_entry(key, fp, len);
  1092. if (rc < 0)
  1093. goto bad;
  1094. key[len] = '\0';
  1095. rc = ebitmap_read(&role->dominates, fp);
  1096. if (rc)
  1097. goto bad;
  1098. rc = ebitmap_read(&role->types, fp);
  1099. if (rc)
  1100. goto bad;
  1101. if (strcmp(key, OBJECT_R) == 0) {
  1102. if (role->value != OBJECT_R_VAL) {
  1103. printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
  1104. OBJECT_R, role->value);
  1105. rc = -EINVAL;
  1106. goto bad;
  1107. }
  1108. rc = 0;
  1109. goto bad;
  1110. }
  1111. rc = hashtab_insert(h, key, role);
  1112. if (rc)
  1113. goto bad;
  1114. out:
  1115. return rc;
  1116. bad:
  1117. role_destroy(key, role, NULL);
  1118. goto out;
  1119. }
  1120. static int type_read(struct policydb *p, struct hashtab *h, void *fp)
  1121. {
  1122. char *key = NULL;
  1123. struct type_datum *typdatum;
  1124. int rc, to_read = 3;
  1125. __le32 buf[4];
  1126. u32 len;
  1127. typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
  1128. if (!typdatum) {
  1129. rc = -ENOMEM;
  1130. return rc;
  1131. }
  1132. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1133. to_read = 4;
  1134. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1135. if (rc < 0)
  1136. goto bad;
  1137. len = le32_to_cpu(buf[0]);
  1138. typdatum->value = le32_to_cpu(buf[1]);
  1139. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
  1140. u32 prop = le32_to_cpu(buf[2]);
  1141. if (prop & TYPEDATUM_PROPERTY_PRIMARY)
  1142. typdatum->primary = 1;
  1143. if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
  1144. typdatum->attribute = 1;
  1145. typdatum->bounds = le32_to_cpu(buf[3]);
  1146. } else {
  1147. typdatum->primary = le32_to_cpu(buf[2]);
  1148. }
  1149. key = kmalloc(len + 1, GFP_KERNEL);
  1150. if (!key) {
  1151. rc = -ENOMEM;
  1152. goto bad;
  1153. }
  1154. rc = next_entry(key, fp, len);
  1155. if (rc < 0)
  1156. goto bad;
  1157. key[len] = '\0';
  1158. rc = hashtab_insert(h, key, typdatum);
  1159. if (rc)
  1160. goto bad;
  1161. out:
  1162. return rc;
  1163. bad:
  1164. type_destroy(key, typdatum, NULL);
  1165. goto out;
  1166. }
  1167. /*
  1168. * Read a MLS level structure from a policydb binary
  1169. * representation file.
  1170. */
  1171. static int mls_read_level(struct mls_level *lp, void *fp)
  1172. {
  1173. __le32 buf[1];
  1174. int rc;
  1175. memset(lp, 0, sizeof(*lp));
  1176. rc = next_entry(buf, fp, sizeof buf);
  1177. if (rc < 0) {
  1178. printk(KERN_ERR "SELinux: mls: truncated level\n");
  1179. goto bad;
  1180. }
  1181. lp->sens = le32_to_cpu(buf[0]);
  1182. if (ebitmap_read(&lp->cat, fp)) {
  1183. printk(KERN_ERR "SELinux: mls: error reading level "
  1184. "categories\n");
  1185. goto bad;
  1186. }
  1187. return 0;
  1188. bad:
  1189. return -EINVAL;
  1190. }
  1191. static int user_read(struct policydb *p, struct hashtab *h, void *fp)
  1192. {
  1193. char *key = NULL;
  1194. struct user_datum *usrdatum;
  1195. int rc, to_read = 2;
  1196. __le32 buf[3];
  1197. u32 len;
  1198. usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
  1199. if (!usrdatum) {
  1200. rc = -ENOMEM;
  1201. goto out;
  1202. }
  1203. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1204. to_read = 3;
  1205. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1206. if (rc < 0)
  1207. goto bad;
  1208. len = le32_to_cpu(buf[0]);
  1209. usrdatum->value = le32_to_cpu(buf[1]);
  1210. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1211. usrdatum->bounds = le32_to_cpu(buf[2]);
  1212. key = kmalloc(len + 1, GFP_KERNEL);
  1213. if (!key) {
  1214. rc = -ENOMEM;
  1215. goto bad;
  1216. }
  1217. rc = next_entry(key, fp, len);
  1218. if (rc < 0)
  1219. goto bad;
  1220. key[len] = '\0';
  1221. rc = ebitmap_read(&usrdatum->roles, fp);
  1222. if (rc)
  1223. goto bad;
  1224. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1225. rc = mls_read_range_helper(&usrdatum->range, fp);
  1226. if (rc)
  1227. goto bad;
  1228. rc = mls_read_level(&usrdatum->dfltlevel, fp);
  1229. if (rc)
  1230. goto bad;
  1231. }
  1232. rc = hashtab_insert(h, key, usrdatum);
  1233. if (rc)
  1234. goto bad;
  1235. out:
  1236. return rc;
  1237. bad:
  1238. user_destroy(key, usrdatum, NULL);
  1239. goto out;
  1240. }
  1241. static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
  1242. {
  1243. char *key = NULL;
  1244. struct level_datum *levdatum;
  1245. int rc;
  1246. __le32 buf[2];
  1247. u32 len;
  1248. levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
  1249. if (!levdatum) {
  1250. rc = -ENOMEM;
  1251. goto out;
  1252. }
  1253. rc = next_entry(buf, fp, sizeof buf);
  1254. if (rc < 0)
  1255. goto bad;
  1256. len = le32_to_cpu(buf[0]);
  1257. levdatum->isalias = le32_to_cpu(buf[1]);
  1258. key = kmalloc(len + 1, GFP_ATOMIC);
  1259. if (!key) {
  1260. rc = -ENOMEM;
  1261. goto bad;
  1262. }
  1263. rc = next_entry(key, fp, len);
  1264. if (rc < 0)
  1265. goto bad;
  1266. key[len] = '\0';
  1267. levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
  1268. if (!levdatum->level) {
  1269. rc = -ENOMEM;
  1270. goto bad;
  1271. }
  1272. if (mls_read_level(levdatum->level, fp)) {
  1273. rc = -EINVAL;
  1274. goto bad;
  1275. }
  1276. rc = hashtab_insert(h, key, levdatum);
  1277. if (rc)
  1278. goto bad;
  1279. out:
  1280. return rc;
  1281. bad:
  1282. sens_destroy(key, levdatum, NULL);
  1283. goto out;
  1284. }
  1285. static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
  1286. {
  1287. char *key = NULL;
  1288. struct cat_datum *catdatum;
  1289. int rc;
  1290. __le32 buf[3];
  1291. u32 len;
  1292. catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
  1293. if (!catdatum) {
  1294. rc = -ENOMEM;
  1295. goto out;
  1296. }
  1297. rc = next_entry(buf, fp, sizeof buf);
  1298. if (rc < 0)
  1299. goto bad;
  1300. len = le32_to_cpu(buf[0]);
  1301. catdatum->value = le32_to_cpu(buf[1]);
  1302. catdatum->isalias = le32_to_cpu(buf[2]);
  1303. key = kmalloc(len + 1, GFP_ATOMIC);
  1304. if (!key) {
  1305. rc = -ENOMEM;
  1306. goto bad;
  1307. }
  1308. rc = next_entry(key, fp, len);
  1309. if (rc < 0)
  1310. goto bad;
  1311. key[len] = '\0';
  1312. rc = hashtab_insert(h, key, catdatum);
  1313. if (rc)
  1314. goto bad;
  1315. out:
  1316. return rc;
  1317. bad:
  1318. cat_destroy(key, catdatum, NULL);
  1319. goto out;
  1320. }
  1321. static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
  1322. {
  1323. common_read,
  1324. class_read,
  1325. role_read,
  1326. type_read,
  1327. user_read,
  1328. cond_read_bool,
  1329. sens_read,
  1330. cat_read,
  1331. };
  1332. static int user_bounds_sanity_check(void *key, void *datum, void *datap)
  1333. {
  1334. struct user_datum *upper, *user;
  1335. struct policydb *p = datap;
  1336. int depth = 0;
  1337. upper = user = datum;
  1338. while (upper->bounds) {
  1339. struct ebitmap_node *node;
  1340. unsigned long bit;
  1341. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1342. printk(KERN_ERR "SELinux: user %s: "
  1343. "too deep or looped boundary",
  1344. (char *) key);
  1345. return -EINVAL;
  1346. }
  1347. upper = p->user_val_to_struct[upper->bounds - 1];
  1348. ebitmap_for_each_positive_bit(&user->roles, node, bit) {
  1349. if (ebitmap_get_bit(&upper->roles, bit))
  1350. continue;
  1351. printk(KERN_ERR
  1352. "SELinux: boundary violated policy: "
  1353. "user=%s role=%s bounds=%s\n",
  1354. p->p_user_val_to_name[user->value - 1],
  1355. p->p_role_val_to_name[bit],
  1356. p->p_user_val_to_name[upper->value - 1]);
  1357. return -EINVAL;
  1358. }
  1359. }
  1360. return 0;
  1361. }
  1362. static int role_bounds_sanity_check(void *key, void *datum, void *datap)
  1363. {
  1364. struct role_datum *upper, *role;
  1365. struct policydb *p = datap;
  1366. int depth = 0;
  1367. upper = role = datum;
  1368. while (upper->bounds) {
  1369. struct ebitmap_node *node;
  1370. unsigned long bit;
  1371. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1372. printk(KERN_ERR "SELinux: role %s: "
  1373. "too deep or looped bounds\n",
  1374. (char *) key);
  1375. return -EINVAL;
  1376. }
  1377. upper = p->role_val_to_struct[upper->bounds - 1];
  1378. ebitmap_for_each_positive_bit(&role->types, node, bit) {
  1379. if (ebitmap_get_bit(&upper->types, bit))
  1380. continue;
  1381. printk(KERN_ERR
  1382. "SELinux: boundary violated policy: "
  1383. "role=%s type=%s bounds=%s\n",
  1384. p->p_role_val_to_name[role->value - 1],
  1385. p->p_type_val_to_name[bit],
  1386. p->p_role_val_to_name[upper->value - 1]);
  1387. return -EINVAL;
  1388. }
  1389. }
  1390. return 0;
  1391. }
  1392. static int type_bounds_sanity_check(void *key, void *datum, void *datap)
  1393. {
  1394. struct type_datum *upper, *type;
  1395. struct policydb *p = datap;
  1396. int depth = 0;
  1397. upper = type = datum;
  1398. while (upper->bounds) {
  1399. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1400. printk(KERN_ERR "SELinux: type %s: "
  1401. "too deep or looped boundary\n",
  1402. (char *) key);
  1403. return -EINVAL;
  1404. }
  1405. upper = p->type_val_to_struct[upper->bounds - 1];
  1406. if (upper->attribute) {
  1407. printk(KERN_ERR "SELinux: type %s: "
  1408. "bounded by attribute %s",
  1409. (char *) key,
  1410. p->p_type_val_to_name[upper->value - 1]);
  1411. return -EINVAL;
  1412. }
  1413. }
  1414. return 0;
  1415. }
  1416. static int policydb_bounds_sanity_check(struct policydb *p)
  1417. {
  1418. int rc;
  1419. if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
  1420. return 0;
  1421. rc = hashtab_map(p->p_users.table,
  1422. user_bounds_sanity_check, p);
  1423. if (rc)
  1424. return rc;
  1425. rc = hashtab_map(p->p_roles.table,
  1426. role_bounds_sanity_check, p);
  1427. if (rc)
  1428. return rc;
  1429. rc = hashtab_map(p->p_types.table,
  1430. type_bounds_sanity_check, p);
  1431. if (rc)
  1432. return rc;
  1433. return 0;
  1434. }
  1435. extern int ss_initialized;
  1436. u16 string_to_security_class(struct policydb *p, const char *name)
  1437. {
  1438. struct class_datum *cladatum;
  1439. cladatum = hashtab_search(p->p_classes.table, name);
  1440. if (!cladatum)
  1441. return 0;
  1442. return cladatum->value;
  1443. }
  1444. u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
  1445. {
  1446. struct class_datum *cladatum;
  1447. struct perm_datum *perdatum = NULL;
  1448. struct common_datum *comdatum;
  1449. if (!tclass || tclass > p->p_classes.nprim)
  1450. return 0;
  1451. cladatum = p->class_val_to_struct[tclass-1];
  1452. comdatum = cladatum->comdatum;
  1453. if (comdatum)
  1454. perdatum = hashtab_search(comdatum->permissions.table,
  1455. name);
  1456. if (!perdatum)
  1457. perdatum = hashtab_search(cladatum->permissions.table,
  1458. name);
  1459. if (!perdatum)
  1460. return 0;
  1461. return 1U << (perdatum->value-1);
  1462. }
  1463. /*
  1464. * Read the configuration data from a policy database binary
  1465. * representation file into a policy database structure.
  1466. */
  1467. int policydb_read(struct policydb *p, void *fp)
  1468. {
  1469. struct role_allow *ra, *lra;
  1470. struct role_trans *tr, *ltr;
  1471. struct ocontext *l, *c, *newc;
  1472. struct genfs *genfs_p, *genfs, *newgenfs;
  1473. int i, j, rc;
  1474. __le32 buf[4];
  1475. u32 nodebuf[8];
  1476. u32 len, len2, nprim, nel, nel2;
  1477. char *policydb_str;
  1478. struct policydb_compat_info *info;
  1479. struct range_trans *rt;
  1480. struct mls_range *r;
  1481. rc = policydb_init(p);
  1482. if (rc)
  1483. goto out;
  1484. /* Read the magic number and string length. */
  1485. rc = next_entry(buf, fp, sizeof(u32) * 2);
  1486. if (rc < 0)
  1487. goto bad;
  1488. if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
  1489. printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
  1490. "not match expected magic number 0x%x\n",
  1491. le32_to_cpu(buf[0]), POLICYDB_MAGIC);
  1492. goto bad;
  1493. }
  1494. len = le32_to_cpu(buf[1]);
  1495. if (len != strlen(POLICYDB_STRING)) {
  1496. printk(KERN_ERR "SELinux: policydb string length %d does not "
  1497. "match expected length %Zu\n",
  1498. len, strlen(POLICYDB_STRING));
  1499. goto bad;
  1500. }
  1501. policydb_str = kmalloc(len + 1, GFP_KERNEL);
  1502. if (!policydb_str) {
  1503. printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
  1504. "string of length %d\n", len);
  1505. rc = -ENOMEM;
  1506. goto bad;
  1507. }
  1508. rc = next_entry(policydb_str, fp, len);
  1509. if (rc < 0) {
  1510. printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
  1511. kfree(policydb_str);
  1512. goto bad;
  1513. }
  1514. policydb_str[len] = '\0';
  1515. if (strcmp(policydb_str, POLICYDB_STRING)) {
  1516. printk(KERN_ERR "SELinux: policydb string %s does not match "
  1517. "my string %s\n", policydb_str, POLICYDB_STRING);
  1518. kfree(policydb_str);
  1519. goto bad;
  1520. }
  1521. /* Done with policydb_str. */
  1522. kfree(policydb_str);
  1523. policydb_str = NULL;
  1524. /* Read the version and table sizes. */
  1525. rc = next_entry(buf, fp, sizeof(u32)*4);
  1526. if (rc < 0)
  1527. goto bad;
  1528. p->policyvers = le32_to_cpu(buf[0]);
  1529. if (p->policyvers < POLICYDB_VERSION_MIN ||
  1530. p->policyvers > POLICYDB_VERSION_MAX) {
  1531. printk(KERN_ERR "SELinux: policydb version %d does not match "
  1532. "my version range %d-%d\n",
  1533. le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
  1534. goto bad;
  1535. }
  1536. if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
  1537. p->mls_enabled = 1;
  1538. if (p->policyvers < POLICYDB_VERSION_MLS) {
  1539. printk(KERN_ERR "SELinux: security policydb version %d "
  1540. "(MLS) not backwards compatible\n",
  1541. p->policyvers);
  1542. goto bad;
  1543. }
  1544. }
  1545. p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
  1546. p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
  1547. if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
  1548. ebitmap_read(&p->policycaps, fp) != 0)
  1549. goto bad;
  1550. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
  1551. ebitmap_read(&p->permissive_map, fp) != 0)
  1552. goto bad;
  1553. info = policydb_lookup_compat(p->policyvers);
  1554. if (!info) {
  1555. printk(KERN_ERR "SELinux: unable to find policy compat info "
  1556. "for version %d\n", p->policyvers);
  1557. goto bad;
  1558. }
  1559. if (le32_to_cpu(buf[2]) != info->sym_num ||
  1560. le32_to_cpu(buf[3]) != info->ocon_num) {
  1561. printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
  1562. "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
  1563. le32_to_cpu(buf[3]),
  1564. info->sym_num, info->ocon_num);
  1565. goto bad;
  1566. }
  1567. for (i = 0; i < info->sym_num; i++) {
  1568. rc = next_entry(buf, fp, sizeof(u32)*2);
  1569. if (rc < 0)
  1570. goto bad;
  1571. nprim = le32_to_cpu(buf[0]);
  1572. nel = le32_to_cpu(buf[1]);
  1573. for (j = 0; j < nel; j++) {
  1574. rc = read_f[i](p, p->symtab[i].table, fp);
  1575. if (rc)
  1576. goto bad;
  1577. }
  1578. p->symtab[i].nprim = nprim;
  1579. }
  1580. rc = avtab_read(&p->te_avtab, fp, p);
  1581. if (rc)
  1582. goto bad;
  1583. if (p->policyvers >= POLICYDB_VERSION_BOOL) {
  1584. rc = cond_read_list(p, fp);
  1585. if (rc)
  1586. goto bad;
  1587. }
  1588. rc = next_entry(buf, fp, sizeof(u32));
  1589. if (rc < 0)
  1590. goto bad;
  1591. nel = le32_to_cpu(buf[0]);
  1592. ltr = NULL;
  1593. for (i = 0; i < nel; i++) {
  1594. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  1595. if (!tr) {
  1596. rc = -ENOMEM;
  1597. goto bad;
  1598. }
  1599. if (ltr)
  1600. ltr->next = tr;
  1601. else
  1602. p->role_tr = tr;
  1603. rc = next_entry(buf, fp, sizeof(u32)*3);
  1604. if (rc < 0)
  1605. goto bad;
  1606. tr->role = le32_to_cpu(buf[0]);
  1607. tr->type = le32_to_cpu(buf[1]);
  1608. tr->new_role = le32_to_cpu(buf[2]);
  1609. if (!policydb_role_isvalid(p, tr->role) ||
  1610. !policydb_type_isvalid(p, tr->type) ||
  1611. !policydb_role_isvalid(p, tr->new_role)) {
  1612. rc = -EINVAL;
  1613. goto bad;
  1614. }
  1615. ltr = tr;
  1616. }
  1617. rc = next_entry(buf, fp, sizeof(u32));
  1618. if (rc < 0)
  1619. goto bad;
  1620. nel = le32_to_cpu(buf[0]);
  1621. lra = NULL;
  1622. for (i = 0; i < nel; i++) {
  1623. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  1624. if (!ra) {
  1625. rc = -ENOMEM;
  1626. goto bad;
  1627. }
  1628. if (lra)
  1629. lra->next = ra;
  1630. else
  1631. p->role_allow = ra;
  1632. rc = next_entry(buf, fp, sizeof(u32)*2);
  1633. if (rc < 0)
  1634. goto bad;
  1635. ra->role = le32_to_cpu(buf[0]);
  1636. ra->new_role = le32_to_cpu(buf[1]);
  1637. if (!policydb_role_isvalid(p, ra->role) ||
  1638. !policydb_role_isvalid(p, ra->new_role)) {
  1639. rc = -EINVAL;
  1640. goto bad;
  1641. }
  1642. lra = ra;
  1643. }
  1644. rc = policydb_index_classes(p);
  1645. if (rc)
  1646. goto bad;
  1647. rc = policydb_index_others(p);
  1648. if (rc)
  1649. goto bad;
  1650. p->process_class = string_to_security_class(p, "process");
  1651. if (!p->process_class)
  1652. goto bad;
  1653. p->process_trans_perms = string_to_av_perm(p, p->process_class,
  1654. "transition");
  1655. p->process_trans_perms |= string_to_av_perm(p, p->process_class,
  1656. "dyntransition");
  1657. if (!p->process_trans_perms)
  1658. goto bad;
  1659. for (i = 0; i < info->ocon_num; i++) {
  1660. rc = next_entry(buf, fp, sizeof(u32));
  1661. if (rc < 0)
  1662. goto bad;
  1663. nel = le32_to_cpu(buf[0]);
  1664. l = NULL;
  1665. for (j = 0; j < nel; j++) {
  1666. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1667. if (!c) {
  1668. rc = -ENOMEM;
  1669. goto bad;
  1670. }
  1671. if (l)
  1672. l->next = c;
  1673. else
  1674. p->ocontexts[i] = c;
  1675. l = c;
  1676. rc = -EINVAL;
  1677. switch (i) {
  1678. case OCON_ISID:
  1679. rc = next_entry(buf, fp, sizeof(u32));
  1680. if (rc < 0)
  1681. goto bad;
  1682. c->sid[0] = le32_to_cpu(buf[0]);
  1683. rc = context_read_and_validate(&c->context[0], p, fp);
  1684. if (rc)
  1685. goto bad;
  1686. break;
  1687. case OCON_FS:
  1688. case OCON_NETIF:
  1689. rc = next_entry(buf, fp, sizeof(u32));
  1690. if (rc < 0)
  1691. goto bad;
  1692. len = le32_to_cpu(buf[0]);
  1693. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1694. if (!c->u.name) {
  1695. rc = -ENOMEM;
  1696. goto bad;
  1697. }
  1698. rc = next_entry(c->u.name, fp, len);
  1699. if (rc < 0)
  1700. goto bad;
  1701. c->u.name[len] = 0;
  1702. rc = context_read_and_validate(&c->context[0], p, fp);
  1703. if (rc)
  1704. goto bad;
  1705. rc = context_read_and_validate(&c->context[1], p, fp);
  1706. if (rc)
  1707. goto bad;
  1708. break;
  1709. case OCON_PORT:
  1710. rc = next_entry(buf, fp, sizeof(u32)*3);
  1711. if (rc < 0)
  1712. goto bad;
  1713. c->u.port.protocol = le32_to_cpu(buf[0]);
  1714. c->u.port.low_port = le32_to_cpu(buf[1]);
  1715. c->u.port.high_port = le32_to_cpu(buf[2]);
  1716. rc = context_read_and_validate(&c->context[0], p, fp);
  1717. if (rc)
  1718. goto bad;
  1719. break;
  1720. case OCON_NODE:
  1721. rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
  1722. if (rc < 0)
  1723. goto bad;
  1724. c->u.node.addr = nodebuf[0]; /* network order */
  1725. c->u.node.mask = nodebuf[1]; /* network order */
  1726. rc = context_read_and_validate(&c->context[0], p, fp);
  1727. if (rc)
  1728. goto bad;
  1729. break;
  1730. case OCON_FSUSE:
  1731. rc = next_entry(buf, fp, sizeof(u32)*2);
  1732. if (rc < 0)
  1733. goto bad;
  1734. c->v.behavior = le32_to_cpu(buf[0]);
  1735. if (c->v.behavior > SECURITY_FS_USE_NONE)
  1736. goto bad;
  1737. len = le32_to_cpu(buf[1]);
  1738. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1739. if (!c->u.name) {
  1740. rc = -ENOMEM;
  1741. goto bad;
  1742. }
  1743. rc = next_entry(c->u.name, fp, len);
  1744. if (rc < 0)
  1745. goto bad;
  1746. c->u.name[len] = 0;
  1747. rc = context_read_and_validate(&c->context[0], p, fp);
  1748. if (rc)
  1749. goto bad;
  1750. break;
  1751. case OCON_NODE6: {
  1752. int k;
  1753. rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
  1754. if (rc < 0)
  1755. goto bad;
  1756. for (k = 0; k < 4; k++)
  1757. c->u.node6.addr[k] = nodebuf[k];
  1758. for (k = 0; k < 4; k++)
  1759. c->u.node6.mask[k] = nodebuf[k+4];
  1760. if (context_read_and_validate(&c->context[0], p, fp))
  1761. goto bad;
  1762. break;
  1763. }
  1764. }
  1765. }
  1766. }
  1767. rc = next_entry(buf, fp, sizeof(u32));
  1768. if (rc < 0)
  1769. goto bad;
  1770. nel = le32_to_cpu(buf[0]);
  1771. genfs_p = NULL;
  1772. rc = -EINVAL;
  1773. for (i = 0; i < nel; i++) {
  1774. rc = next_entry(buf, fp, sizeof(u32));
  1775. if (rc < 0)
  1776. goto bad;
  1777. len = le32_to_cpu(buf[0]);
  1778. newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
  1779. if (!newgenfs) {
  1780. rc = -ENOMEM;
  1781. goto bad;
  1782. }
  1783. newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
  1784. if (!newgenfs->fstype) {
  1785. rc = -ENOMEM;
  1786. kfree(newgenfs);
  1787. goto bad;
  1788. }
  1789. rc = next_entry(newgenfs->fstype, fp, len);
  1790. if (rc < 0) {
  1791. kfree(newgenfs->fstype);
  1792. kfree(newgenfs);
  1793. goto bad;
  1794. }
  1795. newgenfs->fstype[len] = 0;
  1796. for (genfs_p = NULL, genfs = p->genfs; genfs;
  1797. genfs_p = genfs, genfs = genfs->next) {
  1798. if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
  1799. printk(KERN_ERR "SELinux: dup genfs "
  1800. "fstype %s\n", newgenfs->fstype);
  1801. kfree(newgenfs->fstype);
  1802. kfree(newgenfs);
  1803. goto bad;
  1804. }
  1805. if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
  1806. break;
  1807. }
  1808. newgenfs->next = genfs;
  1809. if (genfs_p)
  1810. genfs_p->next = newgenfs;
  1811. else
  1812. p->genfs = newgenfs;
  1813. rc = next_entry(buf, fp, sizeof(u32));
  1814. if (rc < 0)
  1815. goto bad;
  1816. nel2 = le32_to_cpu(buf[0]);
  1817. for (j = 0; j < nel2; j++) {
  1818. rc = next_entry(buf, fp, sizeof(u32));
  1819. if (rc < 0)
  1820. goto bad;
  1821. len = le32_to_cpu(buf[0]);
  1822. newc = kzalloc(sizeof(*newc), GFP_KERNEL);
  1823. if (!newc) {
  1824. rc = -ENOMEM;
  1825. goto bad;
  1826. }
  1827. newc->u.name = kmalloc(len + 1, GFP_KERNEL);
  1828. if (!newc->u.name) {
  1829. rc = -ENOMEM;
  1830. goto bad_newc;
  1831. }
  1832. rc = next_entry(newc->u.name, fp, len);
  1833. if (rc < 0)
  1834. goto bad_newc;
  1835. newc->u.name[len] = 0;
  1836. rc = next_entry(buf, fp, sizeof(u32));
  1837. if (rc < 0)
  1838. goto bad_newc;
  1839. newc->v.sclass = le32_to_cpu(buf[0]);
  1840. if (context_read_and_validate(&newc->context[0], p, fp))
  1841. goto bad_newc;
  1842. for (l = NULL, c = newgenfs->head; c;
  1843. l = c, c = c->next) {
  1844. if (!strcmp(newc->u.name, c->u.name) &&
  1845. (!c->v.sclass || !newc->v.sclass ||
  1846. newc->v.sclass == c->v.sclass)) {
  1847. printk(KERN_ERR "SELinux: dup genfs "
  1848. "entry (%s,%s)\n",
  1849. newgenfs->fstype, c->u.name);
  1850. goto bad_newc;
  1851. }
  1852. len = strlen(newc->u.name);
  1853. len2 = strlen(c->u.name);
  1854. if (len > len2)
  1855. break;
  1856. }
  1857. newc->next = c;
  1858. if (l)
  1859. l->next = newc;
  1860. else
  1861. newgenfs->head = newc;
  1862. }
  1863. }
  1864. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1865. int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
  1866. rc = next_entry(buf, fp, sizeof(u32));
  1867. if (rc < 0)
  1868. goto bad;
  1869. nel = le32_to_cpu(buf[0]);
  1870. for (i = 0; i < nel; i++) {
  1871. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  1872. if (!rt) {
  1873. rc = -ENOMEM;
  1874. goto bad;
  1875. }
  1876. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1877. if (rc < 0) {
  1878. kfree(rt);
  1879. goto bad;
  1880. }
  1881. rt->source_type = le32_to_cpu(buf[0]);
  1882. rt->target_type = le32_to_cpu(buf[1]);
  1883. if (new_rangetr) {
  1884. rc = next_entry(buf, fp, sizeof(u32));
  1885. if (rc < 0) {
  1886. kfree(rt);
  1887. goto bad;
  1888. }
  1889. rt->target_class = le32_to_cpu(buf[0]);
  1890. } else
  1891. rt->target_class = p->process_class;
  1892. if (!policydb_type_isvalid(p, rt->source_type) ||
  1893. !policydb_type_isvalid(p, rt->target_type) ||
  1894. !policydb_class_isvalid(p, rt->target_class)) {
  1895. kfree(rt);
  1896. rc = -EINVAL;
  1897. goto bad;
  1898. }
  1899. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1900. if (!r) {
  1901. kfree(rt);
  1902. rc = -ENOMEM;
  1903. goto bad;
  1904. }
  1905. rc = mls_read_range_helper(r, fp);
  1906. if (rc) {
  1907. kfree(rt);
  1908. kfree(r);
  1909. goto bad;
  1910. }
  1911. if (!mls_range_isvalid(p, r)) {
  1912. printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
  1913. kfree(rt);
  1914. kfree(r);
  1915. goto bad;
  1916. }
  1917. rc = hashtab_insert(p->range_tr, rt, r);
  1918. if (rc) {
  1919. kfree(rt);
  1920. kfree(r);
  1921. goto bad;
  1922. }
  1923. }
  1924. rangetr_hash_eval(p->range_tr);
  1925. }
  1926. p->type_attr_map = kmalloc(p->p_types.nprim*sizeof(struct ebitmap), GFP_KERNEL);
  1927. if (!p->type_attr_map)
  1928. goto bad;
  1929. for (i = 0; i < p->p_types.nprim; i++) {
  1930. ebitmap_init(&p->type_attr_map[i]);
  1931. if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
  1932. if (ebitmap_read(&p->type_attr_map[i], fp))
  1933. goto bad;
  1934. }
  1935. /* add the type itself as the degenerate case */
  1936. if (ebitmap_set_bit(&p->type_attr_map[i], i, 1))
  1937. goto bad;
  1938. }
  1939. rc = policydb_bounds_sanity_check(p);
  1940. if (rc)
  1941. goto bad;
  1942. rc = 0;
  1943. out:
  1944. return rc;
  1945. bad_newc:
  1946. ocontext_destroy(newc, OCON_FSUSE);
  1947. bad:
  1948. if (!rc)
  1949. rc = -EINVAL;
  1950. policydb_destroy(p);
  1951. goto out;
  1952. }