policydb.c 70 KB

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