policydb.c 70 KB

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