policydb.c 37 KB

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