policydb.c 70 KB

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