policydb.c 40 KB

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