policydb.c 69 KB

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