policydb.c 37 KB

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