policydb.c 71 KB

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