policydb.c 71 KB

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