policydb.c 70 KB

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