policydb.c 68 KB

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