policydb.c 69 KB

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