policydb.c 70 KB

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