policydb.c 68 KB

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