policydb.c 38 KB

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