policydb.c 46 KB

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