policydb.c 38 KB

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