policydb.c 40 KB

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