policydb.c 76 KB

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