policydb.c 68 KB

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