policydb.c 70 KB

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