policydb.c 72 KB

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