policydb.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159
  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 <linux/flex_array.h>
  33. #include "security.h"
  34. #include "policydb.h"
  35. #include "conditional.h"
  36. #include "mls.h"
  37. #include "services.h"
  38. #define _DEBUG_HASHES
  39. #ifdef DEBUG_HASHES
  40. static const char *symtab_name[SYM_NUM] = {
  41. "common prefixes",
  42. "classes",
  43. "roles",
  44. "types",
  45. "users",
  46. "bools",
  47. "levels",
  48. "categories",
  49. };
  50. #endif
  51. static unsigned int symtab_sizes[SYM_NUM] = {
  52. 2,
  53. 32,
  54. 16,
  55. 512,
  56. 128,
  57. 16,
  58. 16,
  59. 16,
  60. };
  61. struct policydb_compat_info {
  62. int version;
  63. int sym_num;
  64. int ocon_num;
  65. };
  66. /* These need to be updated if SYM_NUM or OCON_NUM changes */
  67. static struct policydb_compat_info policydb_compat[] = {
  68. {
  69. .version = POLICYDB_VERSION_BASE,
  70. .sym_num = SYM_NUM - 3,
  71. .ocon_num = OCON_NUM - 1,
  72. },
  73. {
  74. .version = POLICYDB_VERSION_BOOL,
  75. .sym_num = SYM_NUM - 2,
  76. .ocon_num = OCON_NUM - 1,
  77. },
  78. {
  79. .version = POLICYDB_VERSION_IPV6,
  80. .sym_num = SYM_NUM - 2,
  81. .ocon_num = OCON_NUM,
  82. },
  83. {
  84. .version = POLICYDB_VERSION_NLCLASS,
  85. .sym_num = SYM_NUM - 2,
  86. .ocon_num = OCON_NUM,
  87. },
  88. {
  89. .version = POLICYDB_VERSION_MLS,
  90. .sym_num = SYM_NUM,
  91. .ocon_num = OCON_NUM,
  92. },
  93. {
  94. .version = POLICYDB_VERSION_AVTAB,
  95. .sym_num = SYM_NUM,
  96. .ocon_num = OCON_NUM,
  97. },
  98. {
  99. .version = POLICYDB_VERSION_RANGETRANS,
  100. .sym_num = SYM_NUM,
  101. .ocon_num = OCON_NUM,
  102. },
  103. {
  104. .version = POLICYDB_VERSION_POLCAP,
  105. .sym_num = SYM_NUM,
  106. .ocon_num = OCON_NUM,
  107. },
  108. {
  109. .version = POLICYDB_VERSION_PERMISSIVE,
  110. .sym_num = SYM_NUM,
  111. .ocon_num = OCON_NUM,
  112. },
  113. {
  114. .version = POLICYDB_VERSION_BOUNDARY,
  115. .sym_num = SYM_NUM,
  116. .ocon_num = OCON_NUM,
  117. },
  118. };
  119. static struct policydb_compat_info *policydb_lookup_compat(int version)
  120. {
  121. int i;
  122. struct policydb_compat_info *info = NULL;
  123. for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
  124. if (policydb_compat[i].version == version) {
  125. info = &policydb_compat[i];
  126. break;
  127. }
  128. }
  129. return info;
  130. }
  131. /*
  132. * Initialize the role table.
  133. */
  134. static int roles_init(struct policydb *p)
  135. {
  136. char *key = NULL;
  137. int rc;
  138. struct role_datum *role;
  139. role = kzalloc(sizeof(*role), GFP_KERNEL);
  140. if (!role) {
  141. rc = -ENOMEM;
  142. goto out;
  143. }
  144. role->value = ++p->p_roles.nprim;
  145. if (role->value != OBJECT_R_VAL) {
  146. rc = -EINVAL;
  147. goto out_free_role;
  148. }
  149. key = kstrdup(OBJECT_R, GFP_KERNEL);
  150. if (!key) {
  151. rc = -ENOMEM;
  152. goto out_free_role;
  153. }
  154. rc = hashtab_insert(p->p_roles.table, key, role);
  155. if (rc)
  156. goto out_free_key;
  157. out:
  158. return rc;
  159. out_free_key:
  160. kfree(key);
  161. out_free_role:
  162. kfree(role);
  163. goto out;
  164. }
  165. static u32 rangetr_hash(struct hashtab *h, const void *k)
  166. {
  167. const struct range_trans *key = k;
  168. return (key->source_type + (key->target_type << 3) +
  169. (key->target_class << 5)) & (h->size - 1);
  170. }
  171. static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  172. {
  173. const struct range_trans *key1 = k1, *key2 = k2;
  174. int v;
  175. v = key1->source_type - key2->source_type;
  176. if (v)
  177. return v;
  178. v = key1->target_type - key2->target_type;
  179. if (v)
  180. return v;
  181. v = key1->target_class - key2->target_class;
  182. return v;
  183. }
  184. /*
  185. * Initialize a policy database structure.
  186. */
  187. static int policydb_init(struct policydb *p)
  188. {
  189. int i, rc;
  190. memset(p, 0, sizeof(*p));
  191. for (i = 0; i < SYM_NUM; i++) {
  192. rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
  193. if (rc)
  194. goto out_free_symtab;
  195. }
  196. rc = avtab_init(&p->te_avtab);
  197. if (rc)
  198. goto out_free_symtab;
  199. rc = roles_init(p);
  200. if (rc)
  201. goto out_free_symtab;
  202. rc = cond_policydb_init(p);
  203. if (rc)
  204. goto out_free_symtab;
  205. p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
  206. if (!p->range_tr)
  207. goto out_free_symtab;
  208. ebitmap_init(&p->policycaps);
  209. ebitmap_init(&p->permissive_map);
  210. out:
  211. return rc;
  212. out_free_symtab:
  213. for (i = 0; i < SYM_NUM; i++)
  214. hashtab_destroy(p->symtab[i].table);
  215. goto out;
  216. }
  217. /*
  218. * The following *_index functions are used to
  219. * define the val_to_name and val_to_struct arrays
  220. * in a policy database structure. The val_to_name
  221. * arrays are used when converting security context
  222. * structures into string representations. The
  223. * val_to_struct arrays are used when the attributes
  224. * of a class, role, or user are needed.
  225. */
  226. static int common_index(void *key, void *datum, void *datap)
  227. {
  228. struct policydb *p;
  229. struct common_datum *comdatum;
  230. comdatum = datum;
  231. p = datap;
  232. if (!comdatum->value || comdatum->value > p->p_commons.nprim)
  233. return -EINVAL;
  234. p->p_common_val_to_name[comdatum->value - 1] = key;
  235. return 0;
  236. }
  237. static int class_index(void *key, void *datum, void *datap)
  238. {
  239. struct policydb *p;
  240. struct class_datum *cladatum;
  241. cladatum = datum;
  242. p = datap;
  243. if (!cladatum->value || cladatum->value > p->p_classes.nprim)
  244. return -EINVAL;
  245. p->p_class_val_to_name[cladatum->value - 1] = key;
  246. p->class_val_to_struct[cladatum->value - 1] = cladatum;
  247. return 0;
  248. }
  249. static int role_index(void *key, void *datum, void *datap)
  250. {
  251. struct policydb *p;
  252. struct role_datum *role;
  253. role = datum;
  254. p = datap;
  255. if (!role->value
  256. || role->value > p->p_roles.nprim
  257. || role->bounds > p->p_roles.nprim)
  258. return -EINVAL;
  259. p->p_role_val_to_name[role->value - 1] = key;
  260. p->role_val_to_struct[role->value - 1] = role;
  261. return 0;
  262. }
  263. static int type_index(void *key, void *datum, void *datap)
  264. {
  265. struct policydb *p;
  266. struct type_datum *typdatum;
  267. typdatum = datum;
  268. p = datap;
  269. if (typdatum->primary) {
  270. if (!typdatum->value
  271. || typdatum->value > p->p_types.nprim
  272. || typdatum->bounds > p->p_types.nprim)
  273. return -EINVAL;
  274. p->p_type_val_to_name[typdatum->value - 1] = key;
  275. p->type_val_to_struct[typdatum->value - 1] = typdatum;
  276. }
  277. return 0;
  278. }
  279. static int user_index(void *key, void *datum, void *datap)
  280. {
  281. struct policydb *p;
  282. struct user_datum *usrdatum;
  283. usrdatum = datum;
  284. p = datap;
  285. if (!usrdatum->value
  286. || usrdatum->value > p->p_users.nprim
  287. || usrdatum->bounds > p->p_users.nprim)
  288. return -EINVAL;
  289. p->p_user_val_to_name[usrdatum->value - 1] = key;
  290. p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
  291. return 0;
  292. }
  293. static int sens_index(void *key, void *datum, void *datap)
  294. {
  295. struct policydb *p;
  296. struct level_datum *levdatum;
  297. levdatum = datum;
  298. p = datap;
  299. if (!levdatum->isalias) {
  300. if (!levdatum->level->sens ||
  301. levdatum->level->sens > p->p_levels.nprim)
  302. return -EINVAL;
  303. p->p_sens_val_to_name[levdatum->level->sens - 1] = key;
  304. }
  305. return 0;
  306. }
  307. static int cat_index(void *key, void *datum, void *datap)
  308. {
  309. struct policydb *p;
  310. struct cat_datum *catdatum;
  311. catdatum = datum;
  312. p = datap;
  313. if (!catdatum->isalias) {
  314. if (!catdatum->value || catdatum->value > p->p_cats.nprim)
  315. return -EINVAL;
  316. p->p_cat_val_to_name[catdatum->value - 1] = key;
  317. }
  318. return 0;
  319. }
  320. static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  321. {
  322. common_index,
  323. class_index,
  324. role_index,
  325. type_index,
  326. user_index,
  327. cond_index_bool,
  328. sens_index,
  329. cat_index,
  330. };
  331. /*
  332. * Define the common val_to_name array and the class
  333. * val_to_name and val_to_struct arrays in a policy
  334. * database structure.
  335. *
  336. * Caller must clean up upon failure.
  337. */
  338. static int policydb_index_classes(struct policydb *p)
  339. {
  340. int rc;
  341. p->p_common_val_to_name =
  342. kmalloc(p->p_commons.nprim * sizeof(char *), GFP_KERNEL);
  343. if (!p->p_common_val_to_name) {
  344. rc = -ENOMEM;
  345. goto out;
  346. }
  347. rc = hashtab_map(p->p_commons.table, common_index, p);
  348. if (rc)
  349. goto out;
  350. p->class_val_to_struct =
  351. kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)), GFP_KERNEL);
  352. if (!p->class_val_to_struct) {
  353. rc = -ENOMEM;
  354. goto out;
  355. }
  356. p->p_class_val_to_name =
  357. kmalloc(p->p_classes.nprim * sizeof(char *), GFP_KERNEL);
  358. if (!p->p_class_val_to_name) {
  359. rc = -ENOMEM;
  360. goto out;
  361. }
  362. rc = hashtab_map(p->p_classes.table, class_index, p);
  363. out:
  364. return rc;
  365. }
  366. #ifdef DEBUG_HASHES
  367. static void symtab_hash_eval(struct symtab *s)
  368. {
  369. int i;
  370. for (i = 0; i < SYM_NUM; i++) {
  371. struct hashtab *h = s[i].table;
  372. struct hashtab_info info;
  373. hashtab_stat(h, &info);
  374. printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
  375. "longest chain length %d\n", symtab_name[i], h->nel,
  376. info.slots_used, h->size, info.max_chain_len);
  377. }
  378. }
  379. static void rangetr_hash_eval(struct hashtab *h)
  380. {
  381. struct hashtab_info info;
  382. hashtab_stat(h, &info);
  383. printk(KERN_DEBUG "SELinux: rangetr: %d entries and %d/%d buckets used, "
  384. "longest chain length %d\n", h->nel,
  385. info.slots_used, h->size, info.max_chain_len);
  386. }
  387. #else
  388. static inline void rangetr_hash_eval(struct hashtab *h)
  389. {
  390. }
  391. #endif
  392. /*
  393. * Define the other val_to_name and val_to_struct arrays
  394. * in a policy database structure.
  395. *
  396. * Caller must clean up on failure.
  397. */
  398. static int policydb_index_others(struct policydb *p)
  399. {
  400. int i, rc = 0;
  401. printk(KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools",
  402. p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
  403. if (p->mls_enabled)
  404. printk(", %d sens, %d cats", p->p_levels.nprim,
  405. p->p_cats.nprim);
  406. printk("\n");
  407. printk(KERN_DEBUG "SELinux: %d classes, %d rules\n",
  408. p->p_classes.nprim, p->te_avtab.nel);
  409. #ifdef DEBUG_HASHES
  410. avtab_hash_eval(&p->te_avtab, "rules");
  411. symtab_hash_eval(p->symtab);
  412. #endif
  413. p->role_val_to_struct =
  414. kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
  415. GFP_KERNEL);
  416. if (!p->role_val_to_struct) {
  417. rc = -ENOMEM;
  418. goto out;
  419. }
  420. p->user_val_to_struct =
  421. kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
  422. GFP_KERNEL);
  423. if (!p->user_val_to_struct) {
  424. rc = -ENOMEM;
  425. goto out;
  426. }
  427. p->type_val_to_struct =
  428. kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
  429. GFP_KERNEL);
  430. if (!p->type_val_to_struct) {
  431. rc = -ENOMEM;
  432. goto out;
  433. }
  434. if (cond_init_bool_indexes(p)) {
  435. rc = -ENOMEM;
  436. goto out;
  437. }
  438. for (i = SYM_ROLES; i < SYM_NUM; i++) {
  439. p->sym_val_to_name[i] =
  440. kmalloc(p->symtab[i].nprim * sizeof(char *), GFP_KERNEL);
  441. if (!p->sym_val_to_name[i]) {
  442. rc = -ENOMEM;
  443. goto out;
  444. }
  445. rc = hashtab_map(p->symtab[i].table, index_f[i], p);
  446. if (rc)
  447. goto out;
  448. }
  449. out:
  450. return rc;
  451. }
  452. /*
  453. * The following *_destroy functions are used to
  454. * free any memory allocated for each kind of
  455. * symbol data in the policy database.
  456. */
  457. static int perm_destroy(void *key, void *datum, void *p)
  458. {
  459. kfree(key);
  460. kfree(datum);
  461. return 0;
  462. }
  463. static int common_destroy(void *key, void *datum, void *p)
  464. {
  465. struct common_datum *comdatum;
  466. kfree(key);
  467. comdatum = datum;
  468. hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
  469. hashtab_destroy(comdatum->permissions.table);
  470. kfree(datum);
  471. return 0;
  472. }
  473. static int cls_destroy(void *key, void *datum, void *p)
  474. {
  475. struct class_datum *cladatum;
  476. struct constraint_node *constraint, *ctemp;
  477. struct constraint_expr *e, *etmp;
  478. kfree(key);
  479. cladatum = datum;
  480. hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
  481. hashtab_destroy(cladatum->permissions.table);
  482. constraint = cladatum->constraints;
  483. while (constraint) {
  484. e = constraint->expr;
  485. while (e) {
  486. ebitmap_destroy(&e->names);
  487. etmp = e;
  488. e = e->next;
  489. kfree(etmp);
  490. }
  491. ctemp = constraint;
  492. constraint = constraint->next;
  493. kfree(ctemp);
  494. }
  495. constraint = cladatum->validatetrans;
  496. while (constraint) {
  497. e = constraint->expr;
  498. while (e) {
  499. ebitmap_destroy(&e->names);
  500. etmp = e;
  501. e = e->next;
  502. kfree(etmp);
  503. }
  504. ctemp = constraint;
  505. constraint = constraint->next;
  506. kfree(ctemp);
  507. }
  508. kfree(cladatum->comkey);
  509. kfree(datum);
  510. return 0;
  511. }
  512. static int role_destroy(void *key, void *datum, void *p)
  513. {
  514. struct role_datum *role;
  515. kfree(key);
  516. role = datum;
  517. ebitmap_destroy(&role->dominates);
  518. ebitmap_destroy(&role->types);
  519. kfree(datum);
  520. return 0;
  521. }
  522. static int type_destroy(void *key, void *datum, void *p)
  523. {
  524. kfree(key);
  525. kfree(datum);
  526. return 0;
  527. }
  528. static int user_destroy(void *key, void *datum, void *p)
  529. {
  530. struct user_datum *usrdatum;
  531. kfree(key);
  532. usrdatum = datum;
  533. ebitmap_destroy(&usrdatum->roles);
  534. ebitmap_destroy(&usrdatum->range.level[0].cat);
  535. ebitmap_destroy(&usrdatum->range.level[1].cat);
  536. ebitmap_destroy(&usrdatum->dfltlevel.cat);
  537. kfree(datum);
  538. return 0;
  539. }
  540. static int sens_destroy(void *key, void *datum, void *p)
  541. {
  542. struct level_datum *levdatum;
  543. kfree(key);
  544. levdatum = datum;
  545. ebitmap_destroy(&levdatum->level->cat);
  546. kfree(levdatum->level);
  547. kfree(datum);
  548. return 0;
  549. }
  550. static int cat_destroy(void *key, void *datum, void *p)
  551. {
  552. kfree(key);
  553. kfree(datum);
  554. return 0;
  555. }
  556. static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
  557. {
  558. common_destroy,
  559. cls_destroy,
  560. role_destroy,
  561. type_destroy,
  562. user_destroy,
  563. cond_destroy_bool,
  564. sens_destroy,
  565. cat_destroy,
  566. };
  567. static int range_tr_destroy(void *key, void *datum, void *p)
  568. {
  569. struct mls_range *rt = datum;
  570. kfree(key);
  571. ebitmap_destroy(&rt->level[0].cat);
  572. ebitmap_destroy(&rt->level[1].cat);
  573. kfree(datum);
  574. cond_resched();
  575. return 0;
  576. }
  577. static void ocontext_destroy(struct ocontext *c, int i)
  578. {
  579. if (!c)
  580. return;
  581. context_destroy(&c->context[0]);
  582. context_destroy(&c->context[1]);
  583. if (i == OCON_ISID || i == OCON_FS ||
  584. i == OCON_NETIF || i == OCON_FSUSE)
  585. kfree(c->u.name);
  586. kfree(c);
  587. }
  588. /*
  589. * Free any memory allocated by a policy database structure.
  590. */
  591. void policydb_destroy(struct policydb *p)
  592. {
  593. struct ocontext *c, *ctmp;
  594. struct genfs *g, *gtmp;
  595. int i;
  596. struct role_allow *ra, *lra = NULL;
  597. struct role_trans *tr, *ltr = NULL;
  598. for (i = 0; i < SYM_NUM; i++) {
  599. cond_resched();
  600. hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
  601. hashtab_destroy(p->symtab[i].table);
  602. }
  603. for (i = 0; i < SYM_NUM; i++)
  604. kfree(p->sym_val_to_name[i]);
  605. kfree(p->class_val_to_struct);
  606. kfree(p->role_val_to_struct);
  607. kfree(p->user_val_to_struct);
  608. kfree(p->type_val_to_struct);
  609. avtab_destroy(&p->te_avtab);
  610. for (i = 0; i < OCON_NUM; i++) {
  611. cond_resched();
  612. c = p->ocontexts[i];
  613. while (c) {
  614. ctmp = c;
  615. c = c->next;
  616. ocontext_destroy(ctmp, i);
  617. }
  618. p->ocontexts[i] = NULL;
  619. }
  620. g = p->genfs;
  621. while (g) {
  622. cond_resched();
  623. kfree(g->fstype);
  624. c = g->head;
  625. while (c) {
  626. ctmp = c;
  627. c = c->next;
  628. ocontext_destroy(ctmp, OCON_FSUSE);
  629. }
  630. gtmp = g;
  631. g = g->next;
  632. kfree(gtmp);
  633. }
  634. p->genfs = NULL;
  635. cond_policydb_destroy(p);
  636. for (tr = p->role_tr; tr; tr = tr->next) {
  637. cond_resched();
  638. kfree(ltr);
  639. ltr = tr;
  640. }
  641. kfree(ltr);
  642. for (ra = p->role_allow; ra; ra = ra->next) {
  643. cond_resched();
  644. kfree(lra);
  645. lra = ra;
  646. }
  647. kfree(lra);
  648. hashtab_map(p->range_tr, range_tr_destroy, NULL);
  649. hashtab_destroy(p->range_tr);
  650. if (p->type_attr_map_array) {
  651. for (i = 0; i < p->p_types.nprim; i++) {
  652. struct ebitmap *e;
  653. e = flex_array_get(p->type_attr_map_array, i);
  654. if (!e)
  655. continue;
  656. ebitmap_destroy(e);
  657. }
  658. flex_array_free(p->type_attr_map_array);
  659. }
  660. ebitmap_destroy(&p->policycaps);
  661. ebitmap_destroy(&p->permissive_map);
  662. return;
  663. }
  664. /*
  665. * Load the initial SIDs specified in a policy database
  666. * structure into a SID table.
  667. */
  668. int policydb_load_isids(struct policydb *p, struct sidtab *s)
  669. {
  670. struct ocontext *head, *c;
  671. int rc;
  672. rc = sidtab_init(s);
  673. if (rc) {
  674. printk(KERN_ERR "SELinux: out of memory on SID table init\n");
  675. goto out;
  676. }
  677. head = p->ocontexts[OCON_ISID];
  678. for (c = head; c; c = c->next) {
  679. if (!c->context[0].user) {
  680. printk(KERN_ERR "SELinux: SID %s was never "
  681. "defined.\n", c->u.name);
  682. rc = -EINVAL;
  683. goto out;
  684. }
  685. if (sidtab_insert(s, c->sid[0], &c->context[0])) {
  686. printk(KERN_ERR "SELinux: unable to load initial "
  687. "SID %s.\n", c->u.name);
  688. rc = -EINVAL;
  689. goto out;
  690. }
  691. }
  692. out:
  693. return rc;
  694. }
  695. int policydb_class_isvalid(struct policydb *p, unsigned int class)
  696. {
  697. if (!class || class > p->p_classes.nprim)
  698. return 0;
  699. return 1;
  700. }
  701. int policydb_role_isvalid(struct policydb *p, unsigned int role)
  702. {
  703. if (!role || role > p->p_roles.nprim)
  704. return 0;
  705. return 1;
  706. }
  707. int policydb_type_isvalid(struct policydb *p, unsigned int type)
  708. {
  709. if (!type || type > p->p_types.nprim)
  710. return 0;
  711. return 1;
  712. }
  713. /*
  714. * Return 1 if the fields in the security context
  715. * structure `c' are valid. Return 0 otherwise.
  716. */
  717. int policydb_context_isvalid(struct policydb *p, struct context *c)
  718. {
  719. struct role_datum *role;
  720. struct user_datum *usrdatum;
  721. if (!c->role || c->role > p->p_roles.nprim)
  722. return 0;
  723. if (!c->user || c->user > p->p_users.nprim)
  724. return 0;
  725. if (!c->type || c->type > p->p_types.nprim)
  726. return 0;
  727. if (c->role != OBJECT_R_VAL) {
  728. /*
  729. * Role must be authorized for the type.
  730. */
  731. role = p->role_val_to_struct[c->role - 1];
  732. if (!ebitmap_get_bit(&role->types,
  733. c->type - 1))
  734. /* role may not be associated with type */
  735. return 0;
  736. /*
  737. * User must be authorized for the role.
  738. */
  739. usrdatum = p->user_val_to_struct[c->user - 1];
  740. if (!usrdatum)
  741. return 0;
  742. if (!ebitmap_get_bit(&usrdatum->roles,
  743. c->role - 1))
  744. /* user may not be associated with role */
  745. return 0;
  746. }
  747. if (!mls_context_isvalid(p, c))
  748. return 0;
  749. return 1;
  750. }
  751. /*
  752. * Read a MLS range structure from a policydb binary
  753. * representation file.
  754. */
  755. static int mls_read_range_helper(struct mls_range *r, void *fp)
  756. {
  757. __le32 buf[2];
  758. u32 items;
  759. int rc;
  760. rc = next_entry(buf, fp, sizeof(u32));
  761. if (rc < 0)
  762. goto out;
  763. items = le32_to_cpu(buf[0]);
  764. if (items > ARRAY_SIZE(buf)) {
  765. printk(KERN_ERR "SELinux: mls: range overflow\n");
  766. rc = -EINVAL;
  767. goto out;
  768. }
  769. rc = next_entry(buf, fp, sizeof(u32) * items);
  770. if (rc < 0) {
  771. printk(KERN_ERR "SELinux: mls: truncated range\n");
  772. goto out;
  773. }
  774. r->level[0].sens = le32_to_cpu(buf[0]);
  775. if (items > 1)
  776. r->level[1].sens = le32_to_cpu(buf[1]);
  777. else
  778. r->level[1].sens = r->level[0].sens;
  779. rc = ebitmap_read(&r->level[0].cat, fp);
  780. if (rc) {
  781. printk(KERN_ERR "SELinux: mls: error reading low "
  782. "categories\n");
  783. goto out;
  784. }
  785. if (items > 1) {
  786. rc = ebitmap_read(&r->level[1].cat, fp);
  787. if (rc) {
  788. printk(KERN_ERR "SELinux: mls: error reading high "
  789. "categories\n");
  790. goto bad_high;
  791. }
  792. } else {
  793. rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
  794. if (rc) {
  795. printk(KERN_ERR "SELinux: mls: out of memory\n");
  796. goto bad_high;
  797. }
  798. }
  799. rc = 0;
  800. out:
  801. return rc;
  802. bad_high:
  803. ebitmap_destroy(&r->level[0].cat);
  804. goto out;
  805. }
  806. /*
  807. * Read and validate a security context structure
  808. * from a policydb binary representation file.
  809. */
  810. static int context_read_and_validate(struct context *c,
  811. struct policydb *p,
  812. void *fp)
  813. {
  814. __le32 buf[3];
  815. int rc;
  816. rc = next_entry(buf, fp, sizeof buf);
  817. if (rc < 0) {
  818. printk(KERN_ERR "SELinux: context truncated\n");
  819. goto out;
  820. }
  821. c->user = le32_to_cpu(buf[0]);
  822. c->role = le32_to_cpu(buf[1]);
  823. c->type = le32_to_cpu(buf[2]);
  824. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  825. if (mls_read_range_helper(&c->range, fp)) {
  826. printk(KERN_ERR "SELinux: error reading MLS range of "
  827. "context\n");
  828. rc = -EINVAL;
  829. goto out;
  830. }
  831. }
  832. if (!policydb_context_isvalid(p, c)) {
  833. printk(KERN_ERR "SELinux: invalid security context\n");
  834. context_destroy(c);
  835. rc = -EINVAL;
  836. }
  837. out:
  838. return rc;
  839. }
  840. /*
  841. * The following *_read functions are used to
  842. * read the symbol data from a policy database
  843. * binary representation file.
  844. */
  845. static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
  846. {
  847. char *key = NULL;
  848. struct perm_datum *perdatum;
  849. int rc;
  850. __le32 buf[2];
  851. u32 len;
  852. perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
  853. if (!perdatum) {
  854. rc = -ENOMEM;
  855. goto out;
  856. }
  857. rc = next_entry(buf, fp, sizeof buf);
  858. if (rc < 0)
  859. goto bad;
  860. len = le32_to_cpu(buf[0]);
  861. perdatum->value = le32_to_cpu(buf[1]);
  862. key = kmalloc(len + 1, GFP_KERNEL);
  863. if (!key) {
  864. rc = -ENOMEM;
  865. goto bad;
  866. }
  867. rc = next_entry(key, fp, len);
  868. if (rc < 0)
  869. goto bad;
  870. key[len] = '\0';
  871. rc = hashtab_insert(h, key, perdatum);
  872. if (rc)
  873. goto bad;
  874. out:
  875. return rc;
  876. bad:
  877. perm_destroy(key, perdatum, NULL);
  878. goto out;
  879. }
  880. static int common_read(struct policydb *p, struct hashtab *h, void *fp)
  881. {
  882. char *key = NULL;
  883. struct common_datum *comdatum;
  884. __le32 buf[4];
  885. u32 len, nel;
  886. int i, rc;
  887. comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
  888. if (!comdatum) {
  889. rc = -ENOMEM;
  890. goto out;
  891. }
  892. rc = next_entry(buf, fp, sizeof buf);
  893. if (rc < 0)
  894. goto bad;
  895. len = le32_to_cpu(buf[0]);
  896. comdatum->value = le32_to_cpu(buf[1]);
  897. rc = symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE);
  898. if (rc)
  899. goto bad;
  900. comdatum->permissions.nprim = le32_to_cpu(buf[2]);
  901. nel = le32_to_cpu(buf[3]);
  902. key = kmalloc(len + 1, GFP_KERNEL);
  903. if (!key) {
  904. rc = -ENOMEM;
  905. goto bad;
  906. }
  907. rc = next_entry(key, fp, len);
  908. if (rc < 0)
  909. goto bad;
  910. key[len] = '\0';
  911. for (i = 0; i < nel; i++) {
  912. rc = perm_read(p, comdatum->permissions.table, fp);
  913. if (rc)
  914. goto bad;
  915. }
  916. rc = hashtab_insert(h, key, comdatum);
  917. if (rc)
  918. goto bad;
  919. out:
  920. return rc;
  921. bad:
  922. common_destroy(key, comdatum, NULL);
  923. goto out;
  924. }
  925. static int read_cons_helper(struct constraint_node **nodep, int ncons,
  926. int allowxtarget, void *fp)
  927. {
  928. struct constraint_node *c, *lc;
  929. struct constraint_expr *e, *le;
  930. __le32 buf[3];
  931. u32 nexpr;
  932. int rc, i, j, depth;
  933. lc = NULL;
  934. for (i = 0; i < ncons; i++) {
  935. c = kzalloc(sizeof(*c), GFP_KERNEL);
  936. if (!c)
  937. return -ENOMEM;
  938. if (lc)
  939. lc->next = c;
  940. else
  941. *nodep = c;
  942. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  943. if (rc < 0)
  944. return rc;
  945. c->permissions = le32_to_cpu(buf[0]);
  946. nexpr = le32_to_cpu(buf[1]);
  947. le = NULL;
  948. depth = -1;
  949. for (j = 0; j < nexpr; j++) {
  950. e = kzalloc(sizeof(*e), GFP_KERNEL);
  951. if (!e)
  952. return -ENOMEM;
  953. if (le)
  954. le->next = e;
  955. else
  956. c->expr = e;
  957. rc = next_entry(buf, fp, (sizeof(u32) * 3));
  958. if (rc < 0)
  959. return rc;
  960. e->expr_type = le32_to_cpu(buf[0]);
  961. e->attr = le32_to_cpu(buf[1]);
  962. e->op = le32_to_cpu(buf[2]);
  963. switch (e->expr_type) {
  964. case CEXPR_NOT:
  965. if (depth < 0)
  966. return -EINVAL;
  967. break;
  968. case CEXPR_AND:
  969. case CEXPR_OR:
  970. if (depth < 1)
  971. return -EINVAL;
  972. depth--;
  973. break;
  974. case CEXPR_ATTR:
  975. if (depth == (CEXPR_MAXDEPTH - 1))
  976. return -EINVAL;
  977. depth++;
  978. break;
  979. case CEXPR_NAMES:
  980. if (!allowxtarget && (e->attr & CEXPR_XTARGET))
  981. return -EINVAL;
  982. if (depth == (CEXPR_MAXDEPTH - 1))
  983. return -EINVAL;
  984. depth++;
  985. if (ebitmap_read(&e->names, fp))
  986. return -EINVAL;
  987. break;
  988. default:
  989. return -EINVAL;
  990. }
  991. le = e;
  992. }
  993. if (depth != 0)
  994. return -EINVAL;
  995. lc = c;
  996. }
  997. return 0;
  998. }
  999. static int class_read(struct policydb *p, struct hashtab *h, void *fp)
  1000. {
  1001. char *key = NULL;
  1002. struct class_datum *cladatum;
  1003. __le32 buf[6];
  1004. u32 len, len2, ncons, nel;
  1005. int i, rc;
  1006. cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
  1007. if (!cladatum) {
  1008. rc = -ENOMEM;
  1009. goto out;
  1010. }
  1011. rc = next_entry(buf, fp, sizeof(u32)*6);
  1012. if (rc < 0)
  1013. goto bad;
  1014. len = le32_to_cpu(buf[0]);
  1015. len2 = le32_to_cpu(buf[1]);
  1016. cladatum->value = le32_to_cpu(buf[2]);
  1017. rc = symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE);
  1018. if (rc)
  1019. goto bad;
  1020. cladatum->permissions.nprim = le32_to_cpu(buf[3]);
  1021. nel = le32_to_cpu(buf[4]);
  1022. ncons = le32_to_cpu(buf[5]);
  1023. key = kmalloc(len + 1, GFP_KERNEL);
  1024. if (!key) {
  1025. rc = -ENOMEM;
  1026. goto bad;
  1027. }
  1028. rc = next_entry(key, fp, len);
  1029. if (rc < 0)
  1030. goto bad;
  1031. key[len] = '\0';
  1032. if (len2) {
  1033. cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
  1034. if (!cladatum->comkey) {
  1035. rc = -ENOMEM;
  1036. goto bad;
  1037. }
  1038. rc = next_entry(cladatum->comkey, fp, len2);
  1039. if (rc < 0)
  1040. goto bad;
  1041. cladatum->comkey[len2] = '\0';
  1042. cladatum->comdatum = hashtab_search(p->p_commons.table,
  1043. cladatum->comkey);
  1044. if (!cladatum->comdatum) {
  1045. printk(KERN_ERR "SELinux: unknown common %s\n",
  1046. cladatum->comkey);
  1047. rc = -EINVAL;
  1048. goto bad;
  1049. }
  1050. }
  1051. for (i = 0; i < nel; i++) {
  1052. rc = perm_read(p, cladatum->permissions.table, fp);
  1053. if (rc)
  1054. goto bad;
  1055. }
  1056. rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);
  1057. if (rc)
  1058. goto bad;
  1059. if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
  1060. /* grab the validatetrans rules */
  1061. rc = next_entry(buf, fp, sizeof(u32));
  1062. if (rc < 0)
  1063. goto bad;
  1064. ncons = le32_to_cpu(buf[0]);
  1065. rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);
  1066. if (rc)
  1067. goto bad;
  1068. }
  1069. rc = hashtab_insert(h, key, cladatum);
  1070. if (rc)
  1071. goto bad;
  1072. rc = 0;
  1073. out:
  1074. return rc;
  1075. bad:
  1076. cls_destroy(key, cladatum, NULL);
  1077. goto out;
  1078. }
  1079. static int role_read(struct policydb *p, struct hashtab *h, void *fp)
  1080. {
  1081. char *key = NULL;
  1082. struct role_datum *role;
  1083. int rc, to_read = 2;
  1084. __le32 buf[3];
  1085. u32 len;
  1086. role = kzalloc(sizeof(*role), GFP_KERNEL);
  1087. if (!role) {
  1088. rc = -ENOMEM;
  1089. goto out;
  1090. }
  1091. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1092. to_read = 3;
  1093. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1094. if (rc < 0)
  1095. goto bad;
  1096. len = le32_to_cpu(buf[0]);
  1097. role->value = le32_to_cpu(buf[1]);
  1098. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1099. role->bounds = le32_to_cpu(buf[2]);
  1100. key = kmalloc(len + 1, GFP_KERNEL);
  1101. if (!key) {
  1102. rc = -ENOMEM;
  1103. goto bad;
  1104. }
  1105. rc = next_entry(key, fp, len);
  1106. if (rc < 0)
  1107. goto bad;
  1108. key[len] = '\0';
  1109. rc = ebitmap_read(&role->dominates, fp);
  1110. if (rc)
  1111. goto bad;
  1112. rc = ebitmap_read(&role->types, fp);
  1113. if (rc)
  1114. goto bad;
  1115. if (strcmp(key, OBJECT_R) == 0) {
  1116. if (role->value != OBJECT_R_VAL) {
  1117. printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
  1118. OBJECT_R, role->value);
  1119. rc = -EINVAL;
  1120. goto bad;
  1121. }
  1122. rc = 0;
  1123. goto bad;
  1124. }
  1125. rc = hashtab_insert(h, key, role);
  1126. if (rc)
  1127. goto bad;
  1128. out:
  1129. return rc;
  1130. bad:
  1131. role_destroy(key, role, NULL);
  1132. goto out;
  1133. }
  1134. static int type_read(struct policydb *p, struct hashtab *h, void *fp)
  1135. {
  1136. char *key = NULL;
  1137. struct type_datum *typdatum;
  1138. int rc, to_read = 3;
  1139. __le32 buf[4];
  1140. u32 len;
  1141. typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
  1142. if (!typdatum) {
  1143. rc = -ENOMEM;
  1144. return rc;
  1145. }
  1146. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1147. to_read = 4;
  1148. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1149. if (rc < 0)
  1150. goto bad;
  1151. len = le32_to_cpu(buf[0]);
  1152. typdatum->value = le32_to_cpu(buf[1]);
  1153. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
  1154. u32 prop = le32_to_cpu(buf[2]);
  1155. if (prop & TYPEDATUM_PROPERTY_PRIMARY)
  1156. typdatum->primary = 1;
  1157. if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
  1158. typdatum->attribute = 1;
  1159. typdatum->bounds = le32_to_cpu(buf[3]);
  1160. } else {
  1161. typdatum->primary = le32_to_cpu(buf[2]);
  1162. }
  1163. key = kmalloc(len + 1, GFP_KERNEL);
  1164. if (!key) {
  1165. rc = -ENOMEM;
  1166. goto bad;
  1167. }
  1168. rc = next_entry(key, fp, len);
  1169. if (rc < 0)
  1170. goto bad;
  1171. key[len] = '\0';
  1172. rc = hashtab_insert(h, key, typdatum);
  1173. if (rc)
  1174. goto bad;
  1175. out:
  1176. return rc;
  1177. bad:
  1178. type_destroy(key, typdatum, NULL);
  1179. goto out;
  1180. }
  1181. /*
  1182. * Read a MLS level structure from a policydb binary
  1183. * representation file.
  1184. */
  1185. static int mls_read_level(struct mls_level *lp, void *fp)
  1186. {
  1187. __le32 buf[1];
  1188. int rc;
  1189. memset(lp, 0, sizeof(*lp));
  1190. rc = next_entry(buf, fp, sizeof buf);
  1191. if (rc < 0) {
  1192. printk(KERN_ERR "SELinux: mls: truncated level\n");
  1193. goto bad;
  1194. }
  1195. lp->sens = le32_to_cpu(buf[0]);
  1196. if (ebitmap_read(&lp->cat, fp)) {
  1197. printk(KERN_ERR "SELinux: mls: error reading level "
  1198. "categories\n");
  1199. goto bad;
  1200. }
  1201. return 0;
  1202. bad:
  1203. return -EINVAL;
  1204. }
  1205. static int user_read(struct policydb *p, struct hashtab *h, void *fp)
  1206. {
  1207. char *key = NULL;
  1208. struct user_datum *usrdatum;
  1209. int rc, to_read = 2;
  1210. __le32 buf[3];
  1211. u32 len;
  1212. usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
  1213. if (!usrdatum) {
  1214. rc = -ENOMEM;
  1215. goto out;
  1216. }
  1217. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1218. to_read = 3;
  1219. rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
  1220. if (rc < 0)
  1221. goto bad;
  1222. len = le32_to_cpu(buf[0]);
  1223. usrdatum->value = le32_to_cpu(buf[1]);
  1224. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  1225. usrdatum->bounds = le32_to_cpu(buf[2]);
  1226. key = kmalloc(len + 1, GFP_KERNEL);
  1227. if (!key) {
  1228. rc = -ENOMEM;
  1229. goto bad;
  1230. }
  1231. rc = next_entry(key, fp, len);
  1232. if (rc < 0)
  1233. goto bad;
  1234. key[len] = '\0';
  1235. rc = ebitmap_read(&usrdatum->roles, fp);
  1236. if (rc)
  1237. goto bad;
  1238. if (p->policyvers >= POLICYDB_VERSION_MLS) {
  1239. rc = mls_read_range_helper(&usrdatum->range, fp);
  1240. if (rc)
  1241. goto bad;
  1242. rc = mls_read_level(&usrdatum->dfltlevel, fp);
  1243. if (rc)
  1244. goto bad;
  1245. }
  1246. rc = hashtab_insert(h, key, usrdatum);
  1247. if (rc)
  1248. goto bad;
  1249. out:
  1250. return rc;
  1251. bad:
  1252. user_destroy(key, usrdatum, NULL);
  1253. goto out;
  1254. }
  1255. static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
  1256. {
  1257. char *key = NULL;
  1258. struct level_datum *levdatum;
  1259. int rc;
  1260. __le32 buf[2];
  1261. u32 len;
  1262. levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
  1263. if (!levdatum) {
  1264. rc = -ENOMEM;
  1265. goto out;
  1266. }
  1267. rc = next_entry(buf, fp, sizeof buf);
  1268. if (rc < 0)
  1269. goto bad;
  1270. len = le32_to_cpu(buf[0]);
  1271. levdatum->isalias = le32_to_cpu(buf[1]);
  1272. key = kmalloc(len + 1, GFP_ATOMIC);
  1273. if (!key) {
  1274. rc = -ENOMEM;
  1275. goto bad;
  1276. }
  1277. rc = next_entry(key, fp, len);
  1278. if (rc < 0)
  1279. goto bad;
  1280. key[len] = '\0';
  1281. levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
  1282. if (!levdatum->level) {
  1283. rc = -ENOMEM;
  1284. goto bad;
  1285. }
  1286. if (mls_read_level(levdatum->level, fp)) {
  1287. rc = -EINVAL;
  1288. goto bad;
  1289. }
  1290. rc = hashtab_insert(h, key, levdatum);
  1291. if (rc)
  1292. goto bad;
  1293. out:
  1294. return rc;
  1295. bad:
  1296. sens_destroy(key, levdatum, NULL);
  1297. goto out;
  1298. }
  1299. static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
  1300. {
  1301. char *key = NULL;
  1302. struct cat_datum *catdatum;
  1303. int rc;
  1304. __le32 buf[3];
  1305. u32 len;
  1306. catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
  1307. if (!catdatum) {
  1308. rc = -ENOMEM;
  1309. goto out;
  1310. }
  1311. rc = next_entry(buf, fp, sizeof buf);
  1312. if (rc < 0)
  1313. goto bad;
  1314. len = le32_to_cpu(buf[0]);
  1315. catdatum->value = le32_to_cpu(buf[1]);
  1316. catdatum->isalias = le32_to_cpu(buf[2]);
  1317. key = kmalloc(len + 1, GFP_ATOMIC);
  1318. if (!key) {
  1319. rc = -ENOMEM;
  1320. goto bad;
  1321. }
  1322. rc = next_entry(key, fp, len);
  1323. if (rc < 0)
  1324. goto bad;
  1325. key[len] = '\0';
  1326. rc = hashtab_insert(h, key, catdatum);
  1327. if (rc)
  1328. goto bad;
  1329. out:
  1330. return rc;
  1331. bad:
  1332. cat_destroy(key, catdatum, NULL);
  1333. goto out;
  1334. }
  1335. static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
  1336. {
  1337. common_read,
  1338. class_read,
  1339. role_read,
  1340. type_read,
  1341. user_read,
  1342. cond_read_bool,
  1343. sens_read,
  1344. cat_read,
  1345. };
  1346. static int user_bounds_sanity_check(void *key, void *datum, void *datap)
  1347. {
  1348. struct user_datum *upper, *user;
  1349. struct policydb *p = datap;
  1350. int depth = 0;
  1351. upper = user = datum;
  1352. while (upper->bounds) {
  1353. struct ebitmap_node *node;
  1354. unsigned long bit;
  1355. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1356. printk(KERN_ERR "SELinux: user %s: "
  1357. "too deep or looped boundary",
  1358. (char *) key);
  1359. return -EINVAL;
  1360. }
  1361. upper = p->user_val_to_struct[upper->bounds - 1];
  1362. ebitmap_for_each_positive_bit(&user->roles, node, bit) {
  1363. if (ebitmap_get_bit(&upper->roles, bit))
  1364. continue;
  1365. printk(KERN_ERR
  1366. "SELinux: boundary violated policy: "
  1367. "user=%s role=%s bounds=%s\n",
  1368. p->p_user_val_to_name[user->value - 1],
  1369. p->p_role_val_to_name[bit],
  1370. p->p_user_val_to_name[upper->value - 1]);
  1371. return -EINVAL;
  1372. }
  1373. }
  1374. return 0;
  1375. }
  1376. static int role_bounds_sanity_check(void *key, void *datum, void *datap)
  1377. {
  1378. struct role_datum *upper, *role;
  1379. struct policydb *p = datap;
  1380. int depth = 0;
  1381. upper = role = datum;
  1382. while (upper->bounds) {
  1383. struct ebitmap_node *node;
  1384. unsigned long bit;
  1385. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1386. printk(KERN_ERR "SELinux: role %s: "
  1387. "too deep or looped bounds\n",
  1388. (char *) key);
  1389. return -EINVAL;
  1390. }
  1391. upper = p->role_val_to_struct[upper->bounds - 1];
  1392. ebitmap_for_each_positive_bit(&role->types, node, bit) {
  1393. if (ebitmap_get_bit(&upper->types, bit))
  1394. continue;
  1395. printk(KERN_ERR
  1396. "SELinux: boundary violated policy: "
  1397. "role=%s type=%s bounds=%s\n",
  1398. p->p_role_val_to_name[role->value - 1],
  1399. p->p_type_val_to_name[bit],
  1400. p->p_role_val_to_name[upper->value - 1]);
  1401. return -EINVAL;
  1402. }
  1403. }
  1404. return 0;
  1405. }
  1406. static int type_bounds_sanity_check(void *key, void *datum, void *datap)
  1407. {
  1408. struct type_datum *upper;
  1409. struct policydb *p = datap;
  1410. int depth = 0;
  1411. upper = datum;
  1412. while (upper->bounds) {
  1413. if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
  1414. printk(KERN_ERR "SELinux: type %s: "
  1415. "too deep or looped boundary\n",
  1416. (char *) key);
  1417. return -EINVAL;
  1418. }
  1419. upper = p->type_val_to_struct[upper->bounds - 1];
  1420. if (upper->attribute) {
  1421. printk(KERN_ERR "SELinux: type %s: "
  1422. "bounded by attribute %s",
  1423. (char *) key,
  1424. p->p_type_val_to_name[upper->value - 1]);
  1425. return -EINVAL;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. static int policydb_bounds_sanity_check(struct policydb *p)
  1431. {
  1432. int rc;
  1433. if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
  1434. return 0;
  1435. rc = hashtab_map(p->p_users.table,
  1436. user_bounds_sanity_check, p);
  1437. if (rc)
  1438. return rc;
  1439. rc = hashtab_map(p->p_roles.table,
  1440. role_bounds_sanity_check, p);
  1441. if (rc)
  1442. return rc;
  1443. rc = hashtab_map(p->p_types.table,
  1444. type_bounds_sanity_check, p);
  1445. if (rc)
  1446. return rc;
  1447. return 0;
  1448. }
  1449. extern int ss_initialized;
  1450. u16 string_to_security_class(struct policydb *p, const char *name)
  1451. {
  1452. struct class_datum *cladatum;
  1453. cladatum = hashtab_search(p->p_classes.table, name);
  1454. if (!cladatum)
  1455. return 0;
  1456. return cladatum->value;
  1457. }
  1458. u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
  1459. {
  1460. struct class_datum *cladatum;
  1461. struct perm_datum *perdatum = NULL;
  1462. struct common_datum *comdatum;
  1463. if (!tclass || tclass > p->p_classes.nprim)
  1464. return 0;
  1465. cladatum = p->class_val_to_struct[tclass-1];
  1466. comdatum = cladatum->comdatum;
  1467. if (comdatum)
  1468. perdatum = hashtab_search(comdatum->permissions.table,
  1469. name);
  1470. if (!perdatum)
  1471. perdatum = hashtab_search(cladatum->permissions.table,
  1472. name);
  1473. if (!perdatum)
  1474. return 0;
  1475. return 1U << (perdatum->value-1);
  1476. }
  1477. static int range_read(struct policydb *p, void *fp)
  1478. {
  1479. struct range_trans *rt = NULL;
  1480. struct mls_range *r = NULL;
  1481. int i, rc;
  1482. __le32 buf[2];
  1483. u32 nel;
  1484. if (p->policyvers < POLICYDB_VERSION_MLS)
  1485. return 0;
  1486. rc = next_entry(buf, fp, sizeof(u32));
  1487. if (rc)
  1488. goto out;
  1489. nel = le32_to_cpu(buf[0]);
  1490. for (i = 0; i < nel; i++) {
  1491. rc = -ENOMEM;
  1492. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  1493. if (!rt)
  1494. goto out;
  1495. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1496. if (rc)
  1497. goto out;
  1498. rt->source_type = le32_to_cpu(buf[0]);
  1499. rt->target_type = le32_to_cpu(buf[1]);
  1500. if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
  1501. rc = next_entry(buf, fp, sizeof(u32));
  1502. if (rc)
  1503. goto out;
  1504. rt->target_class = le32_to_cpu(buf[0]);
  1505. } else
  1506. rt->target_class = p->process_class;
  1507. rc = -EINVAL;
  1508. if (!policydb_type_isvalid(p, rt->source_type) ||
  1509. !policydb_type_isvalid(p, rt->target_type) ||
  1510. !policydb_class_isvalid(p, rt->target_class))
  1511. goto out;
  1512. rc = -ENOMEM;
  1513. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1514. if (!r)
  1515. goto out;
  1516. rc = mls_read_range_helper(r, fp);
  1517. if (rc)
  1518. goto out;
  1519. rc = -EINVAL;
  1520. if (!mls_range_isvalid(p, r)) {
  1521. printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
  1522. goto out;
  1523. }
  1524. rc = hashtab_insert(p->range_tr, rt, r);
  1525. if (rc)
  1526. goto out;
  1527. rt = NULL;
  1528. r = NULL;
  1529. }
  1530. rangetr_hash_eval(p->range_tr);
  1531. rc = 0;
  1532. out:
  1533. kfree(rt);
  1534. kfree(r);
  1535. return rc;
  1536. }
  1537. static int genfs_read(struct policydb *p, void *fp)
  1538. {
  1539. int i, j, rc;
  1540. u32 nel, nel2, len, len2;
  1541. __le32 buf[1];
  1542. struct ocontext *l, *c;
  1543. struct ocontext *newc = NULL;
  1544. struct genfs *genfs_p, *genfs;
  1545. struct genfs *newgenfs = NULL;
  1546. rc = next_entry(buf, fp, sizeof(u32));
  1547. if (rc)
  1548. goto out;
  1549. nel = le32_to_cpu(buf[0]);
  1550. for (i = 0; i < nel; i++) {
  1551. rc = next_entry(buf, fp, sizeof(u32));
  1552. if (rc)
  1553. goto out;
  1554. len = le32_to_cpu(buf[0]);
  1555. rc = -ENOMEM;
  1556. newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
  1557. if (!newgenfs)
  1558. goto out;
  1559. rc = -ENOMEM;
  1560. newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
  1561. if (!newgenfs->fstype)
  1562. goto out;
  1563. rc = next_entry(newgenfs->fstype, fp, len);
  1564. if (rc)
  1565. goto out;
  1566. newgenfs->fstype[len] = 0;
  1567. for (genfs_p = NULL, genfs = p->genfs; genfs;
  1568. genfs_p = genfs, genfs = genfs->next) {
  1569. rc = -EINVAL;
  1570. if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
  1571. printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
  1572. newgenfs->fstype);
  1573. goto out;
  1574. }
  1575. if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
  1576. break;
  1577. }
  1578. newgenfs->next = genfs;
  1579. if (genfs_p)
  1580. genfs_p->next = newgenfs;
  1581. else
  1582. p->genfs = newgenfs;
  1583. genfs = newgenfs;
  1584. newgenfs = NULL;
  1585. rc = next_entry(buf, fp, sizeof(u32));
  1586. if (rc)
  1587. goto out;
  1588. nel2 = le32_to_cpu(buf[0]);
  1589. for (j = 0; j < nel2; j++) {
  1590. rc = next_entry(buf, fp, sizeof(u32));
  1591. if (rc)
  1592. goto out;
  1593. len = le32_to_cpu(buf[0]);
  1594. rc = -ENOMEM;
  1595. newc = kzalloc(sizeof(*newc), GFP_KERNEL);
  1596. if (!newc)
  1597. goto out;
  1598. rc = -ENOMEM;
  1599. newc->u.name = kmalloc(len + 1, GFP_KERNEL);
  1600. if (!newc->u.name)
  1601. goto out;
  1602. rc = next_entry(newc->u.name, fp, len);
  1603. if (rc)
  1604. goto out;
  1605. newc->u.name[len] = 0;
  1606. rc = next_entry(buf, fp, sizeof(u32));
  1607. if (rc)
  1608. goto out;
  1609. newc->v.sclass = le32_to_cpu(buf[0]);
  1610. rc = context_read_and_validate(&newc->context[0], p, fp);
  1611. if (rc)
  1612. goto out;
  1613. for (l = NULL, c = genfs->head; c;
  1614. l = c, c = c->next) {
  1615. rc = -EINVAL;
  1616. if (!strcmp(newc->u.name, c->u.name) &&
  1617. (!c->v.sclass || !newc->v.sclass ||
  1618. newc->v.sclass == c->v.sclass)) {
  1619. printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
  1620. genfs->fstype, c->u.name);
  1621. goto out;
  1622. }
  1623. len = strlen(newc->u.name);
  1624. len2 = strlen(c->u.name);
  1625. if (len > len2)
  1626. break;
  1627. }
  1628. newc->next = c;
  1629. if (l)
  1630. l->next = newc;
  1631. else
  1632. genfs->head = newc;
  1633. newc = NULL;
  1634. }
  1635. }
  1636. rc = 0;
  1637. out:
  1638. if (newgenfs)
  1639. kfree(newgenfs->fstype);
  1640. kfree(newgenfs);
  1641. ocontext_destroy(newc, OCON_FSUSE);
  1642. return rc;
  1643. }
  1644. static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
  1645. void *fp)
  1646. {
  1647. int i, j, rc;
  1648. u32 nel, len;
  1649. __le32 buf[3];
  1650. struct ocontext *l, *c;
  1651. u32 nodebuf[8];
  1652. for (i = 0; i < info->ocon_num; i++) {
  1653. rc = next_entry(buf, fp, sizeof(u32));
  1654. if (rc)
  1655. goto out;
  1656. nel = le32_to_cpu(buf[0]);
  1657. l = NULL;
  1658. for (j = 0; j < nel; j++) {
  1659. rc = -ENOMEM;
  1660. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1661. if (!c)
  1662. goto out;
  1663. if (l)
  1664. l->next = c;
  1665. else
  1666. p->ocontexts[i] = c;
  1667. l = c;
  1668. switch (i) {
  1669. case OCON_ISID:
  1670. rc = next_entry(buf, fp, sizeof(u32));
  1671. if (rc)
  1672. goto out;
  1673. c->sid[0] = le32_to_cpu(buf[0]);
  1674. rc = context_read_and_validate(&c->context[0], p, fp);
  1675. if (rc)
  1676. goto out;
  1677. break;
  1678. case OCON_FS:
  1679. case OCON_NETIF:
  1680. rc = next_entry(buf, fp, sizeof(u32));
  1681. if (rc)
  1682. goto out;
  1683. len = le32_to_cpu(buf[0]);
  1684. rc = -ENOMEM;
  1685. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1686. if (!c->u.name)
  1687. goto out;
  1688. rc = next_entry(c->u.name, fp, len);
  1689. if (rc)
  1690. goto out;
  1691. c->u.name[len] = 0;
  1692. rc = context_read_and_validate(&c->context[0], p, fp);
  1693. if (rc)
  1694. goto out;
  1695. rc = context_read_and_validate(&c->context[1], p, fp);
  1696. if (rc)
  1697. goto out;
  1698. break;
  1699. case OCON_PORT:
  1700. rc = next_entry(buf, fp, sizeof(u32)*3);
  1701. if (rc)
  1702. goto out;
  1703. c->u.port.protocol = le32_to_cpu(buf[0]);
  1704. c->u.port.low_port = le32_to_cpu(buf[1]);
  1705. c->u.port.high_port = le32_to_cpu(buf[2]);
  1706. rc = context_read_and_validate(&c->context[0], p, fp);
  1707. if (rc)
  1708. goto out;
  1709. break;
  1710. case OCON_NODE:
  1711. rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
  1712. if (rc)
  1713. goto out;
  1714. c->u.node.addr = nodebuf[0]; /* network order */
  1715. c->u.node.mask = nodebuf[1]; /* network order */
  1716. rc = context_read_and_validate(&c->context[0], p, fp);
  1717. if (rc)
  1718. goto out;
  1719. break;
  1720. case OCON_FSUSE:
  1721. rc = next_entry(buf, fp, sizeof(u32)*2);
  1722. if (rc)
  1723. goto out;
  1724. rc = -EINVAL;
  1725. c->v.behavior = le32_to_cpu(buf[0]);
  1726. if (c->v.behavior > SECURITY_FS_USE_NONE)
  1727. goto out;
  1728. rc = -ENOMEM;
  1729. len = le32_to_cpu(buf[1]);
  1730. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1731. if (!c->u.name)
  1732. goto out;
  1733. rc = next_entry(c->u.name, fp, len);
  1734. if (rc)
  1735. goto out;
  1736. c->u.name[len] = 0;
  1737. rc = context_read_and_validate(&c->context[0], p, fp);
  1738. if (rc)
  1739. goto out;
  1740. break;
  1741. case OCON_NODE6: {
  1742. int k;
  1743. rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
  1744. if (rc)
  1745. goto out;
  1746. for (k = 0; k < 4; k++)
  1747. c->u.node6.addr[k] = nodebuf[k];
  1748. for (k = 0; k < 4; k++)
  1749. c->u.node6.mask[k] = nodebuf[k+4];
  1750. rc = context_read_and_validate(&c->context[0], p, fp);
  1751. if (rc)
  1752. goto out;
  1753. break;
  1754. }
  1755. }
  1756. }
  1757. }
  1758. rc = 0;
  1759. out:
  1760. return rc;
  1761. }
  1762. /*
  1763. * Read the configuration data from a policy database binary
  1764. * representation file into a policy database structure.
  1765. */
  1766. int policydb_read(struct policydb *p, void *fp)
  1767. {
  1768. struct role_allow *ra, *lra;
  1769. struct role_trans *tr, *ltr;
  1770. int i, j, rc;
  1771. __le32 buf[4];
  1772. u32 len, nprim, nel;
  1773. char *policydb_str;
  1774. struct policydb_compat_info *info;
  1775. rc = policydb_init(p);
  1776. if (rc)
  1777. goto out;
  1778. /* Read the magic number and string length. */
  1779. rc = next_entry(buf, fp, sizeof(u32) * 2);
  1780. if (rc < 0)
  1781. goto bad;
  1782. if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
  1783. printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
  1784. "not match expected magic number 0x%x\n",
  1785. le32_to_cpu(buf[0]), POLICYDB_MAGIC);
  1786. goto bad;
  1787. }
  1788. len = le32_to_cpu(buf[1]);
  1789. if (len != strlen(POLICYDB_STRING)) {
  1790. printk(KERN_ERR "SELinux: policydb string length %d does not "
  1791. "match expected length %Zu\n",
  1792. len, strlen(POLICYDB_STRING));
  1793. goto bad;
  1794. }
  1795. policydb_str = kmalloc(len + 1, GFP_KERNEL);
  1796. if (!policydb_str) {
  1797. printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
  1798. "string of length %d\n", len);
  1799. rc = -ENOMEM;
  1800. goto bad;
  1801. }
  1802. rc = next_entry(policydb_str, fp, len);
  1803. if (rc < 0) {
  1804. printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
  1805. kfree(policydb_str);
  1806. goto bad;
  1807. }
  1808. policydb_str[len] = '\0';
  1809. if (strcmp(policydb_str, POLICYDB_STRING)) {
  1810. printk(KERN_ERR "SELinux: policydb string %s does not match "
  1811. "my string %s\n", policydb_str, POLICYDB_STRING);
  1812. kfree(policydb_str);
  1813. goto bad;
  1814. }
  1815. /* Done with policydb_str. */
  1816. kfree(policydb_str);
  1817. policydb_str = NULL;
  1818. /* Read the version and table sizes. */
  1819. rc = next_entry(buf, fp, sizeof(u32)*4);
  1820. if (rc < 0)
  1821. goto bad;
  1822. p->policyvers = le32_to_cpu(buf[0]);
  1823. if (p->policyvers < POLICYDB_VERSION_MIN ||
  1824. p->policyvers > POLICYDB_VERSION_MAX) {
  1825. printk(KERN_ERR "SELinux: policydb version %d does not match "
  1826. "my version range %d-%d\n",
  1827. le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
  1828. goto bad;
  1829. }
  1830. if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
  1831. p->mls_enabled = 1;
  1832. if (p->policyvers < POLICYDB_VERSION_MLS) {
  1833. printk(KERN_ERR "SELinux: security policydb version %d "
  1834. "(MLS) not backwards compatible\n",
  1835. p->policyvers);
  1836. goto bad;
  1837. }
  1838. }
  1839. p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
  1840. p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
  1841. if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
  1842. ebitmap_read(&p->policycaps, fp) != 0)
  1843. goto bad;
  1844. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
  1845. ebitmap_read(&p->permissive_map, fp) != 0)
  1846. goto bad;
  1847. info = policydb_lookup_compat(p->policyvers);
  1848. if (!info) {
  1849. printk(KERN_ERR "SELinux: unable to find policy compat info "
  1850. "for version %d\n", p->policyvers);
  1851. goto bad;
  1852. }
  1853. if (le32_to_cpu(buf[2]) != info->sym_num ||
  1854. le32_to_cpu(buf[3]) != info->ocon_num) {
  1855. printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
  1856. "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
  1857. le32_to_cpu(buf[3]),
  1858. info->sym_num, info->ocon_num);
  1859. goto bad;
  1860. }
  1861. for (i = 0; i < info->sym_num; i++) {
  1862. rc = next_entry(buf, fp, sizeof(u32)*2);
  1863. if (rc < 0)
  1864. goto bad;
  1865. nprim = le32_to_cpu(buf[0]);
  1866. nel = le32_to_cpu(buf[1]);
  1867. for (j = 0; j < nel; j++) {
  1868. rc = read_f[i](p, p->symtab[i].table, fp);
  1869. if (rc)
  1870. goto bad;
  1871. }
  1872. p->symtab[i].nprim = nprim;
  1873. }
  1874. rc = avtab_read(&p->te_avtab, fp, p);
  1875. if (rc)
  1876. goto bad;
  1877. if (p->policyvers >= POLICYDB_VERSION_BOOL) {
  1878. rc = cond_read_list(p, fp);
  1879. if (rc)
  1880. goto bad;
  1881. }
  1882. rc = next_entry(buf, fp, sizeof(u32));
  1883. if (rc < 0)
  1884. goto bad;
  1885. nel = le32_to_cpu(buf[0]);
  1886. ltr = NULL;
  1887. for (i = 0; i < nel; i++) {
  1888. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  1889. if (!tr) {
  1890. rc = -ENOMEM;
  1891. goto bad;
  1892. }
  1893. if (ltr)
  1894. ltr->next = tr;
  1895. else
  1896. p->role_tr = tr;
  1897. rc = next_entry(buf, fp, sizeof(u32)*3);
  1898. if (rc < 0)
  1899. goto bad;
  1900. tr->role = le32_to_cpu(buf[0]);
  1901. tr->type = le32_to_cpu(buf[1]);
  1902. tr->new_role = le32_to_cpu(buf[2]);
  1903. if (!policydb_role_isvalid(p, tr->role) ||
  1904. !policydb_type_isvalid(p, tr->type) ||
  1905. !policydb_role_isvalid(p, tr->new_role)) {
  1906. rc = -EINVAL;
  1907. goto bad;
  1908. }
  1909. ltr = tr;
  1910. }
  1911. rc = next_entry(buf, fp, sizeof(u32));
  1912. if (rc < 0)
  1913. goto bad;
  1914. nel = le32_to_cpu(buf[0]);
  1915. lra = NULL;
  1916. for (i = 0; i < nel; i++) {
  1917. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  1918. if (!ra) {
  1919. rc = -ENOMEM;
  1920. goto bad;
  1921. }
  1922. if (lra)
  1923. lra->next = ra;
  1924. else
  1925. p->role_allow = ra;
  1926. rc = next_entry(buf, fp, sizeof(u32)*2);
  1927. if (rc < 0)
  1928. goto bad;
  1929. ra->role = le32_to_cpu(buf[0]);
  1930. ra->new_role = le32_to_cpu(buf[1]);
  1931. if (!policydb_role_isvalid(p, ra->role) ||
  1932. !policydb_role_isvalid(p, ra->new_role)) {
  1933. rc = -EINVAL;
  1934. goto bad;
  1935. }
  1936. lra = ra;
  1937. }
  1938. rc = policydb_index_classes(p);
  1939. if (rc)
  1940. goto bad;
  1941. rc = policydb_index_others(p);
  1942. if (rc)
  1943. goto bad;
  1944. p->process_class = string_to_security_class(p, "process");
  1945. if (!p->process_class)
  1946. goto bad;
  1947. p->process_trans_perms = string_to_av_perm(p, p->process_class,
  1948. "transition");
  1949. p->process_trans_perms |= string_to_av_perm(p, p->process_class,
  1950. "dyntransition");
  1951. if (!p->process_trans_perms)
  1952. goto bad;
  1953. rc = ocontext_read(p, info, fp);
  1954. if (rc)
  1955. goto bad;
  1956. rc = genfs_read(p, fp);
  1957. if (rc)
  1958. goto bad;
  1959. rc = range_read(p, fp);
  1960. if (rc)
  1961. goto bad;
  1962. rc = -ENOMEM;
  1963. p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
  1964. p->p_types.nprim,
  1965. GFP_KERNEL | __GFP_ZERO);
  1966. if (!p->type_attr_map_array)
  1967. goto bad;
  1968. /* preallocate so we don't have to worry about the put ever failing */
  1969. rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
  1970. GFP_KERNEL | __GFP_ZERO);
  1971. if (rc)
  1972. goto bad;
  1973. for (i = 0; i < p->p_types.nprim; i++) {
  1974. struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
  1975. BUG_ON(!e);
  1976. ebitmap_init(e);
  1977. if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
  1978. rc = ebitmap_read(e, fp);
  1979. if (rc)
  1980. goto bad;
  1981. }
  1982. /* add the type itself as the degenerate case */
  1983. rc = ebitmap_set_bit(e, i, 1);
  1984. if (rc)
  1985. goto bad;
  1986. }
  1987. rc = policydb_bounds_sanity_check(p);
  1988. if (rc)
  1989. goto bad;
  1990. rc = 0;
  1991. out:
  1992. return rc;
  1993. bad:
  1994. if (!rc)
  1995. rc = -EINVAL;
  1996. policydb_destroy(p);
  1997. goto out;
  1998. }
  1999. /*
  2000. * Write a MLS level structure to a policydb binary
  2001. * representation file.
  2002. */
  2003. static int mls_write_level(struct mls_level *l, void *fp)
  2004. {
  2005. __le32 buf[1];
  2006. int rc;
  2007. buf[0] = cpu_to_le32(l->sens);
  2008. rc = put_entry(buf, sizeof(u32), 1, fp);
  2009. if (rc)
  2010. return rc;
  2011. rc = ebitmap_write(&l->cat, fp);
  2012. if (rc)
  2013. return rc;
  2014. return 0;
  2015. }
  2016. /*
  2017. * Write a MLS range structure to a policydb binary
  2018. * representation file.
  2019. */
  2020. static int mls_write_range_helper(struct mls_range *r, void *fp)
  2021. {
  2022. __le32 buf[3];
  2023. size_t items;
  2024. int rc, eq;
  2025. eq = mls_level_eq(&r->level[1], &r->level[0]);
  2026. if (eq)
  2027. items = 2;
  2028. else
  2029. items = 3;
  2030. buf[0] = cpu_to_le32(items-1);
  2031. buf[1] = cpu_to_le32(r->level[0].sens);
  2032. if (!eq)
  2033. buf[2] = cpu_to_le32(r->level[1].sens);
  2034. BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
  2035. rc = put_entry(buf, sizeof(u32), items, fp);
  2036. if (rc)
  2037. return rc;
  2038. rc = ebitmap_write(&r->level[0].cat, fp);
  2039. if (rc)
  2040. return rc;
  2041. if (!eq) {
  2042. rc = ebitmap_write(&r->level[1].cat, fp);
  2043. if (rc)
  2044. return rc;
  2045. }
  2046. return 0;
  2047. }
  2048. static int sens_write(void *vkey, void *datum, void *ptr)
  2049. {
  2050. char *key = vkey;
  2051. struct level_datum *levdatum = datum;
  2052. struct policy_data *pd = ptr;
  2053. void *fp = pd->fp;
  2054. __le32 buf[2];
  2055. size_t len;
  2056. int rc;
  2057. len = strlen(key);
  2058. buf[0] = cpu_to_le32(len);
  2059. buf[1] = cpu_to_le32(levdatum->isalias);
  2060. rc = put_entry(buf, sizeof(u32), 2, fp);
  2061. if (rc)
  2062. return rc;
  2063. rc = put_entry(key, 1, len, fp);
  2064. if (rc)
  2065. return rc;
  2066. rc = mls_write_level(levdatum->level, fp);
  2067. if (rc)
  2068. return rc;
  2069. return 0;
  2070. }
  2071. static int cat_write(void *vkey, void *datum, void *ptr)
  2072. {
  2073. char *key = vkey;
  2074. struct cat_datum *catdatum = datum;
  2075. struct policy_data *pd = ptr;
  2076. void *fp = pd->fp;
  2077. __le32 buf[3];
  2078. size_t len;
  2079. int rc;
  2080. len = strlen(key);
  2081. buf[0] = cpu_to_le32(len);
  2082. buf[1] = cpu_to_le32(catdatum->value);
  2083. buf[2] = cpu_to_le32(catdatum->isalias);
  2084. rc = put_entry(buf, sizeof(u32), 3, fp);
  2085. if (rc)
  2086. return rc;
  2087. rc = put_entry(key, 1, len, fp);
  2088. if (rc)
  2089. return rc;
  2090. return 0;
  2091. }
  2092. static int role_trans_write(struct role_trans *r, void *fp)
  2093. {
  2094. struct role_trans *tr;
  2095. u32 buf[3];
  2096. size_t nel;
  2097. int rc;
  2098. nel = 0;
  2099. for (tr = r; tr; tr = tr->next)
  2100. nel++;
  2101. buf[0] = cpu_to_le32(nel);
  2102. rc = put_entry(buf, sizeof(u32), 1, fp);
  2103. if (rc)
  2104. return rc;
  2105. for (tr = r; tr; tr = tr->next) {
  2106. buf[0] = cpu_to_le32(tr->role);
  2107. buf[1] = cpu_to_le32(tr->type);
  2108. buf[2] = cpu_to_le32(tr->new_role);
  2109. rc = put_entry(buf, sizeof(u32), 3, fp);
  2110. if (rc)
  2111. return rc;
  2112. }
  2113. return 0;
  2114. }
  2115. static int role_allow_write(struct role_allow *r, void *fp)
  2116. {
  2117. struct role_allow *ra;
  2118. u32 buf[2];
  2119. size_t nel;
  2120. int rc;
  2121. nel = 0;
  2122. for (ra = r; ra; ra = ra->next)
  2123. nel++;
  2124. buf[0] = cpu_to_le32(nel);
  2125. rc = put_entry(buf, sizeof(u32), 1, fp);
  2126. if (rc)
  2127. return rc;
  2128. for (ra = r; ra; ra = ra->next) {
  2129. buf[0] = cpu_to_le32(ra->role);
  2130. buf[1] = cpu_to_le32(ra->new_role);
  2131. rc = put_entry(buf, sizeof(u32), 2, fp);
  2132. if (rc)
  2133. return rc;
  2134. }
  2135. return 0;
  2136. }
  2137. /*
  2138. * Write a security context structure
  2139. * to a policydb binary representation file.
  2140. */
  2141. static int context_write(struct policydb *p, struct context *c,
  2142. void *fp)
  2143. {
  2144. int rc;
  2145. __le32 buf[3];
  2146. buf[0] = cpu_to_le32(c->user);
  2147. buf[1] = cpu_to_le32(c->role);
  2148. buf[2] = cpu_to_le32(c->type);
  2149. rc = put_entry(buf, sizeof(u32), 3, fp);
  2150. if (rc)
  2151. return rc;
  2152. rc = mls_write_range_helper(&c->range, fp);
  2153. if (rc)
  2154. return rc;
  2155. return 0;
  2156. }
  2157. /*
  2158. * The following *_write functions are used to
  2159. * write the symbol data to a policy database
  2160. * binary representation file.
  2161. */
  2162. static int perm_write(void *vkey, void *datum, void *fp)
  2163. {
  2164. char *key = vkey;
  2165. struct perm_datum *perdatum = datum;
  2166. __le32 buf[2];
  2167. size_t len;
  2168. int rc;
  2169. len = strlen(key);
  2170. buf[0] = cpu_to_le32(len);
  2171. buf[1] = cpu_to_le32(perdatum->value);
  2172. rc = put_entry(buf, sizeof(u32), 2, fp);
  2173. if (rc)
  2174. return rc;
  2175. rc = put_entry(key, 1, len, fp);
  2176. if (rc)
  2177. return rc;
  2178. return 0;
  2179. }
  2180. static int common_write(void *vkey, void *datum, void *ptr)
  2181. {
  2182. char *key = vkey;
  2183. struct common_datum *comdatum = datum;
  2184. struct policy_data *pd = ptr;
  2185. void *fp = pd->fp;
  2186. __le32 buf[4];
  2187. size_t len;
  2188. int rc;
  2189. len = strlen(key);
  2190. buf[0] = cpu_to_le32(len);
  2191. buf[1] = cpu_to_le32(comdatum->value);
  2192. buf[2] = cpu_to_le32(comdatum->permissions.nprim);
  2193. buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
  2194. rc = put_entry(buf, sizeof(u32), 4, fp);
  2195. if (rc)
  2196. return rc;
  2197. rc = put_entry(key, 1, len, fp);
  2198. if (rc)
  2199. return rc;
  2200. rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
  2201. if (rc)
  2202. return rc;
  2203. return 0;
  2204. }
  2205. static int write_cons_helper(struct policydb *p, struct constraint_node *node,
  2206. void *fp)
  2207. {
  2208. struct constraint_node *c;
  2209. struct constraint_expr *e;
  2210. __le32 buf[3];
  2211. u32 nel;
  2212. int rc;
  2213. for (c = node; c; c = c->next) {
  2214. nel = 0;
  2215. for (e = c->expr; e; e = e->next)
  2216. nel++;
  2217. buf[0] = cpu_to_le32(c->permissions);
  2218. buf[1] = cpu_to_le32(nel);
  2219. rc = put_entry(buf, sizeof(u32), 2, fp);
  2220. if (rc)
  2221. return rc;
  2222. for (e = c->expr; e; e = e->next) {
  2223. buf[0] = cpu_to_le32(e->expr_type);
  2224. buf[1] = cpu_to_le32(e->attr);
  2225. buf[2] = cpu_to_le32(e->op);
  2226. rc = put_entry(buf, sizeof(u32), 3, fp);
  2227. if (rc)
  2228. return rc;
  2229. switch (e->expr_type) {
  2230. case CEXPR_NAMES:
  2231. rc = ebitmap_write(&e->names, fp);
  2232. if (rc)
  2233. return rc;
  2234. break;
  2235. default:
  2236. break;
  2237. }
  2238. }
  2239. }
  2240. return 0;
  2241. }
  2242. static int class_write(void *vkey, void *datum, void *ptr)
  2243. {
  2244. char *key = vkey;
  2245. struct class_datum *cladatum = datum;
  2246. struct policy_data *pd = ptr;
  2247. void *fp = pd->fp;
  2248. struct policydb *p = pd->p;
  2249. struct constraint_node *c;
  2250. __le32 buf[6];
  2251. u32 ncons;
  2252. size_t len, len2;
  2253. int rc;
  2254. len = strlen(key);
  2255. if (cladatum->comkey)
  2256. len2 = strlen(cladatum->comkey);
  2257. else
  2258. len2 = 0;
  2259. ncons = 0;
  2260. for (c = cladatum->constraints; c; c = c->next)
  2261. ncons++;
  2262. buf[0] = cpu_to_le32(len);
  2263. buf[1] = cpu_to_le32(len2);
  2264. buf[2] = cpu_to_le32(cladatum->value);
  2265. buf[3] = cpu_to_le32(cladatum->permissions.nprim);
  2266. if (cladatum->permissions.table)
  2267. buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
  2268. else
  2269. buf[4] = 0;
  2270. buf[5] = cpu_to_le32(ncons);
  2271. rc = put_entry(buf, sizeof(u32), 6, fp);
  2272. if (rc)
  2273. return rc;
  2274. rc = put_entry(key, 1, len, fp);
  2275. if (rc)
  2276. return rc;
  2277. if (cladatum->comkey) {
  2278. rc = put_entry(cladatum->comkey, 1, len2, fp);
  2279. if (rc)
  2280. return rc;
  2281. }
  2282. rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
  2283. if (rc)
  2284. return rc;
  2285. rc = write_cons_helper(p, cladatum->constraints, fp);
  2286. if (rc)
  2287. return rc;
  2288. /* write out the validatetrans rule */
  2289. ncons = 0;
  2290. for (c = cladatum->validatetrans; c; c = c->next)
  2291. ncons++;
  2292. buf[0] = cpu_to_le32(ncons);
  2293. rc = put_entry(buf, sizeof(u32), 1, fp);
  2294. if (rc)
  2295. return rc;
  2296. rc = write_cons_helper(p, cladatum->validatetrans, fp);
  2297. if (rc)
  2298. return rc;
  2299. return 0;
  2300. }
  2301. static int role_write(void *vkey, void *datum, void *ptr)
  2302. {
  2303. char *key = vkey;
  2304. struct role_datum *role = datum;
  2305. struct policy_data *pd = ptr;
  2306. void *fp = pd->fp;
  2307. struct policydb *p = pd->p;
  2308. __le32 buf[3];
  2309. size_t items, len;
  2310. int rc;
  2311. len = strlen(key);
  2312. items = 0;
  2313. buf[items++] = cpu_to_le32(len);
  2314. buf[items++] = cpu_to_le32(role->value);
  2315. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  2316. buf[items++] = cpu_to_le32(role->bounds);
  2317. BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
  2318. rc = put_entry(buf, sizeof(u32), items, fp);
  2319. if (rc)
  2320. return rc;
  2321. rc = put_entry(key, 1, len, fp);
  2322. if (rc)
  2323. return rc;
  2324. rc = ebitmap_write(&role->dominates, fp);
  2325. if (rc)
  2326. return rc;
  2327. rc = ebitmap_write(&role->types, fp);
  2328. if (rc)
  2329. return rc;
  2330. return 0;
  2331. }
  2332. static int type_write(void *vkey, void *datum, void *ptr)
  2333. {
  2334. char *key = vkey;
  2335. struct type_datum *typdatum = datum;
  2336. struct policy_data *pd = ptr;
  2337. struct policydb *p = pd->p;
  2338. void *fp = pd->fp;
  2339. __le32 buf[4];
  2340. int rc;
  2341. size_t items, len;
  2342. len = strlen(key);
  2343. items = 0;
  2344. buf[items++] = cpu_to_le32(len);
  2345. buf[items++] = cpu_to_le32(typdatum->value);
  2346. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
  2347. u32 properties = 0;
  2348. if (typdatum->primary)
  2349. properties |= TYPEDATUM_PROPERTY_PRIMARY;
  2350. if (typdatum->attribute)
  2351. properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
  2352. buf[items++] = cpu_to_le32(properties);
  2353. buf[items++] = cpu_to_le32(typdatum->bounds);
  2354. } else {
  2355. buf[items++] = cpu_to_le32(typdatum->primary);
  2356. }
  2357. BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
  2358. rc = put_entry(buf, sizeof(u32), items, fp);
  2359. if (rc)
  2360. return rc;
  2361. rc = put_entry(key, 1, len, fp);
  2362. if (rc)
  2363. return rc;
  2364. return 0;
  2365. }
  2366. static int user_write(void *vkey, void *datum, void *ptr)
  2367. {
  2368. char *key = vkey;
  2369. struct user_datum *usrdatum = datum;
  2370. struct policy_data *pd = ptr;
  2371. struct policydb *p = pd->p;
  2372. void *fp = pd->fp;
  2373. __le32 buf[3];
  2374. size_t items, len;
  2375. int rc;
  2376. len = strlen(key);
  2377. items = 0;
  2378. buf[items++] = cpu_to_le32(len);
  2379. buf[items++] = cpu_to_le32(usrdatum->value);
  2380. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  2381. buf[items++] = cpu_to_le32(usrdatum->bounds);
  2382. BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
  2383. rc = put_entry(buf, sizeof(u32), items, fp);
  2384. if (rc)
  2385. return rc;
  2386. rc = put_entry(key, 1, len, fp);
  2387. if (rc)
  2388. return rc;
  2389. rc = ebitmap_write(&usrdatum->roles, fp);
  2390. if (rc)
  2391. return rc;
  2392. rc = mls_write_range_helper(&usrdatum->range, fp);
  2393. if (rc)
  2394. return rc;
  2395. rc = mls_write_level(&usrdatum->dfltlevel, fp);
  2396. if (rc)
  2397. return rc;
  2398. return 0;
  2399. }
  2400. static int (*write_f[SYM_NUM]) (void *key, void *datum,
  2401. void *datap) =
  2402. {
  2403. common_write,
  2404. class_write,
  2405. role_write,
  2406. type_write,
  2407. user_write,
  2408. cond_write_bool,
  2409. sens_write,
  2410. cat_write,
  2411. };
  2412. static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
  2413. void *fp)
  2414. {
  2415. unsigned int i, j, rc;
  2416. size_t nel, len;
  2417. __le32 buf[3];
  2418. u32 nodebuf[8];
  2419. struct ocontext *c;
  2420. for (i = 0; i < info->ocon_num; i++) {
  2421. nel = 0;
  2422. for (c = p->ocontexts[i]; c; c = c->next)
  2423. nel++;
  2424. buf[0] = cpu_to_le32(nel);
  2425. rc = put_entry(buf, sizeof(u32), 1, fp);
  2426. if (rc)
  2427. return rc;
  2428. for (c = p->ocontexts[i]; c; c = c->next) {
  2429. switch (i) {
  2430. case OCON_ISID:
  2431. buf[0] = cpu_to_le32(c->sid[0]);
  2432. rc = put_entry(buf, sizeof(u32), 1, fp);
  2433. if (rc)
  2434. return rc;
  2435. rc = context_write(p, &c->context[0], fp);
  2436. if (rc)
  2437. return rc;
  2438. break;
  2439. case OCON_FS:
  2440. case OCON_NETIF:
  2441. len = strlen(c->u.name);
  2442. buf[0] = cpu_to_le32(len);
  2443. rc = put_entry(buf, sizeof(u32), 1, fp);
  2444. if (rc)
  2445. return rc;
  2446. rc = put_entry(c->u.name, 1, len, fp);
  2447. if (rc)
  2448. return rc;
  2449. rc = context_write(p, &c->context[0], fp);
  2450. if (rc)
  2451. return rc;
  2452. rc = context_write(p, &c->context[1], fp);
  2453. if (rc)
  2454. return rc;
  2455. break;
  2456. case OCON_PORT:
  2457. buf[0] = cpu_to_le32(c->u.port.protocol);
  2458. buf[1] = cpu_to_le32(c->u.port.low_port);
  2459. buf[2] = cpu_to_le32(c->u.port.high_port);
  2460. rc = put_entry(buf, sizeof(u32), 3, fp);
  2461. if (rc)
  2462. return rc;
  2463. rc = context_write(p, &c->context[0], fp);
  2464. if (rc)
  2465. return rc;
  2466. break;
  2467. case OCON_NODE:
  2468. nodebuf[0] = c->u.node.addr; /* network order */
  2469. nodebuf[1] = c->u.node.mask; /* network order */
  2470. rc = put_entry(nodebuf, sizeof(u32), 2, fp);
  2471. if (rc)
  2472. return rc;
  2473. rc = context_write(p, &c->context[0], fp);
  2474. if (rc)
  2475. return rc;
  2476. break;
  2477. case OCON_FSUSE:
  2478. buf[0] = cpu_to_le32(c->v.behavior);
  2479. len = strlen(c->u.name);
  2480. buf[1] = cpu_to_le32(len);
  2481. rc = put_entry(buf, sizeof(u32), 2, fp);
  2482. if (rc)
  2483. return rc;
  2484. rc = put_entry(c->u.name, 1, len, fp);
  2485. if (rc)
  2486. return rc;
  2487. rc = context_write(p, &c->context[0], fp);
  2488. if (rc)
  2489. return rc;
  2490. break;
  2491. case OCON_NODE6:
  2492. for (j = 0; j < 4; j++)
  2493. nodebuf[j] = c->u.node6.addr[j]; /* network order */
  2494. for (j = 0; j < 4; j++)
  2495. nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
  2496. rc = put_entry(nodebuf, sizeof(u32), 8, fp);
  2497. if (rc)
  2498. return rc;
  2499. rc = context_write(p, &c->context[0], fp);
  2500. if (rc)
  2501. return rc;
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. return 0;
  2507. }
  2508. static int genfs_write(struct policydb *p, void *fp)
  2509. {
  2510. struct genfs *genfs;
  2511. struct ocontext *c;
  2512. size_t len;
  2513. __le32 buf[1];
  2514. int rc;
  2515. len = 0;
  2516. for (genfs = p->genfs; genfs; genfs = genfs->next)
  2517. len++;
  2518. buf[0] = cpu_to_le32(len);
  2519. rc = put_entry(buf, sizeof(u32), 1, fp);
  2520. if (rc)
  2521. return rc;
  2522. for (genfs = p->genfs; genfs; genfs = genfs->next) {
  2523. len = strlen(genfs->fstype);
  2524. buf[0] = cpu_to_le32(len);
  2525. rc = put_entry(buf, sizeof(u32), 1, fp);
  2526. if (rc)
  2527. return rc;
  2528. rc = put_entry(genfs->fstype, 1, len, fp);
  2529. if (rc)
  2530. return rc;
  2531. len = 0;
  2532. for (c = genfs->head; c; c = c->next)
  2533. len++;
  2534. buf[0] = cpu_to_le32(len);
  2535. rc = put_entry(buf, sizeof(u32), 1, fp);
  2536. if (rc)
  2537. return rc;
  2538. for (c = genfs->head; c; c = c->next) {
  2539. len = strlen(c->u.name);
  2540. buf[0] = cpu_to_le32(len);
  2541. rc = put_entry(buf, sizeof(u32), 1, fp);
  2542. if (rc)
  2543. return rc;
  2544. rc = put_entry(c->u.name, 1, len, fp);
  2545. if (rc)
  2546. return rc;
  2547. buf[0] = cpu_to_le32(c->v.sclass);
  2548. rc = put_entry(buf, sizeof(u32), 1, fp);
  2549. if (rc)
  2550. return rc;
  2551. rc = context_write(p, &c->context[0], fp);
  2552. if (rc)
  2553. return rc;
  2554. }
  2555. }
  2556. return 0;
  2557. }
  2558. static int range_count(void *key, void *data, void *ptr)
  2559. {
  2560. int *cnt = ptr;
  2561. *cnt = *cnt + 1;
  2562. return 0;
  2563. }
  2564. static int range_write_helper(void *key, void *data, void *ptr)
  2565. {
  2566. __le32 buf[2];
  2567. struct range_trans *rt = key;
  2568. struct mls_range *r = data;
  2569. struct policy_data *pd = ptr;
  2570. void *fp = pd->fp;
  2571. struct policydb *p = pd->p;
  2572. int rc;
  2573. buf[0] = cpu_to_le32(rt->source_type);
  2574. buf[1] = cpu_to_le32(rt->target_type);
  2575. rc = put_entry(buf, sizeof(u32), 2, fp);
  2576. if (rc)
  2577. return rc;
  2578. if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
  2579. buf[0] = cpu_to_le32(rt->target_class);
  2580. rc = put_entry(buf, sizeof(u32), 1, fp);
  2581. if (rc)
  2582. return rc;
  2583. }
  2584. rc = mls_write_range_helper(r, fp);
  2585. if (rc)
  2586. return rc;
  2587. return 0;
  2588. }
  2589. static int range_write(struct policydb *p, void *fp)
  2590. {
  2591. size_t nel;
  2592. __le32 buf[1];
  2593. int rc;
  2594. struct policy_data pd;
  2595. pd.p = p;
  2596. pd.fp = fp;
  2597. /* count the number of entries in the hashtab */
  2598. nel = 0;
  2599. rc = hashtab_map(p->range_tr, range_count, &nel);
  2600. if (rc)
  2601. return rc;
  2602. buf[0] = cpu_to_le32(nel);
  2603. rc = put_entry(buf, sizeof(u32), 1, fp);
  2604. if (rc)
  2605. return rc;
  2606. /* actually write all of the entries */
  2607. rc = hashtab_map(p->range_tr, range_write_helper, &pd);
  2608. if (rc)
  2609. return rc;
  2610. return 0;
  2611. }
  2612. /*
  2613. * Write the configuration data in a policy database
  2614. * structure to a policy database binary representation
  2615. * file.
  2616. */
  2617. int policydb_write(struct policydb *p, void *fp)
  2618. {
  2619. unsigned int i, num_syms;
  2620. int rc;
  2621. __le32 buf[4];
  2622. u32 config;
  2623. size_t len;
  2624. struct policydb_compat_info *info;
  2625. /*
  2626. * refuse to write policy older than compressed avtab
  2627. * to simplify the writer. There are other tests dropped
  2628. * since we assume this throughout the writer code. Be
  2629. * careful if you ever try to remove this restriction
  2630. */
  2631. if (p->policyvers < POLICYDB_VERSION_AVTAB) {
  2632. printk(KERN_ERR "SELinux: refusing to write policy version %d."
  2633. " Because it is less than version %d\n", p->policyvers,
  2634. POLICYDB_VERSION_AVTAB);
  2635. return -EINVAL;
  2636. }
  2637. config = 0;
  2638. if (p->mls_enabled)
  2639. config |= POLICYDB_CONFIG_MLS;
  2640. if (p->reject_unknown)
  2641. config |= REJECT_UNKNOWN;
  2642. if (p->allow_unknown)
  2643. config |= ALLOW_UNKNOWN;
  2644. /* Write the magic number and string identifiers. */
  2645. buf[0] = cpu_to_le32(POLICYDB_MAGIC);
  2646. len = strlen(POLICYDB_STRING);
  2647. buf[1] = cpu_to_le32(len);
  2648. rc = put_entry(buf, sizeof(u32), 2, fp);
  2649. if (rc)
  2650. return rc;
  2651. rc = put_entry(POLICYDB_STRING, 1, len, fp);
  2652. if (rc)
  2653. return rc;
  2654. /* Write the version, config, and table sizes. */
  2655. info = policydb_lookup_compat(p->policyvers);
  2656. if (!info) {
  2657. printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
  2658. "version %d", p->policyvers);
  2659. return rc;
  2660. }
  2661. buf[0] = cpu_to_le32(p->policyvers);
  2662. buf[1] = cpu_to_le32(config);
  2663. buf[2] = cpu_to_le32(info->sym_num);
  2664. buf[3] = cpu_to_le32(info->ocon_num);
  2665. rc = put_entry(buf, sizeof(u32), 4, fp);
  2666. if (rc)
  2667. return rc;
  2668. if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
  2669. rc = ebitmap_write(&p->policycaps, fp);
  2670. if (rc)
  2671. return rc;
  2672. }
  2673. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
  2674. rc = ebitmap_write(&p->permissive_map, fp);
  2675. if (rc)
  2676. return rc;
  2677. }
  2678. num_syms = info->sym_num;
  2679. for (i = 0; i < num_syms; i++) {
  2680. struct policy_data pd;
  2681. pd.fp = fp;
  2682. pd.p = p;
  2683. buf[0] = cpu_to_le32(p->symtab[i].nprim);
  2684. buf[1] = cpu_to_le32(p->symtab[i].table->nel);
  2685. rc = put_entry(buf, sizeof(u32), 2, fp);
  2686. if (rc)
  2687. return rc;
  2688. rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
  2689. if (rc)
  2690. return rc;
  2691. }
  2692. rc = avtab_write(p, &p->te_avtab, fp);
  2693. if (rc)
  2694. return rc;
  2695. rc = cond_write_list(p, p->cond_list, fp);
  2696. if (rc)
  2697. return rc;
  2698. rc = role_trans_write(p->role_tr, fp);
  2699. if (rc)
  2700. return rc;
  2701. rc = role_allow_write(p->role_allow, fp);
  2702. if (rc)
  2703. return rc;
  2704. rc = ocontext_write(p, info, fp);
  2705. if (rc)
  2706. return rc;
  2707. rc = genfs_write(p, fp);
  2708. if (rc)
  2709. return rc;
  2710. rc = range_write(p, fp);
  2711. if (rc)
  2712. return rc;
  2713. for (i = 0; i < p->p_types.nprim; i++) {
  2714. struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
  2715. BUG_ON(!e);
  2716. rc = ebitmap_write(e, fp);
  2717. if (rc)
  2718. return rc;
  2719. }
  2720. return 0;
  2721. }