policydb.c 40 KB

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