policydb.c 66 KB

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