policydb.c 39 KB

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