policydb.c 71 KB

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