policydb.c 38 KB

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