policydb.c 66 KB

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