policydb.c 71 KB

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