policydb.c 45 KB

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