policydb.c 70 KB

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