policydb.c 38 KB

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