policydb.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379
  1. /*
  2. * Implementation of the policy database.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /*
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. *
  11. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  12. *
  13. * Added conditional policy language extensions
  14. *
  15. * Updated: Hewlett-Packard <paul@paul-moore.com>
  16. *
  17. * Added support for the policy capability bitmap
  18. *
  19. * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  20. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  21. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation, version 2.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/errno.h>
  31. #include <linux/audit.h>
  32. #include <linux/flex_array.h>
  33. #include "security.h"
  34. #include "policydb.h"
  35. #include "conditional.h"
  36. #include "mls.h"
  37. #include "services.h"
  38. #define _DEBUG_HASHES
  39. #ifdef DEBUG_HASHES
  40. static const char *symtab_name[SYM_NUM] = {
  41. "common prefixes",
  42. "classes",
  43. "roles",
  44. "types",
  45. "users",
  46. "bools",
  47. "levels",
  48. "categories",
  49. };
  50. #endif
  51. static unsigned int symtab_sizes[SYM_NUM] = {
  52. 2,
  53. 32,
  54. 16,
  55. 512,
  56. 128,
  57. 16,
  58. 16,
  59. 16,
  60. };
  61. struct policydb_compat_info {
  62. int version;
  63. int sym_num;
  64. int ocon_num;
  65. };
  66. /* These need to be updated if SYM_NUM or OCON_NUM changes */
  67. static struct policydb_compat_info policydb_compat[] = {
  68. {
  69. .version = POLICYDB_VERSION_BASE,
  70. .sym_num = SYM_NUM - 3,
  71. .ocon_num = OCON_NUM - 1,
  72. },
  73. {
  74. .version = POLICYDB_VERSION_BOOL,
  75. .sym_num = SYM_NUM - 2,
  76. .ocon_num = OCON_NUM - 1,
  77. },
  78. {
  79. .version = POLICYDB_VERSION_IPV6,
  80. .sym_num = SYM_NUM - 2,
  81. .ocon_num = OCON_NUM,
  82. },
  83. {
  84. .version = POLICYDB_VERSION_NLCLASS,
  85. .sym_num = SYM_NUM - 2,
  86. .ocon_num = OCON_NUM,
  87. },
  88. {
  89. .version = POLICYDB_VERSION_MLS,
  90. .sym_num = SYM_NUM,
  91. .ocon_num = OCON_NUM,
  92. },
  93. {
  94. .version = POLICYDB_VERSION_AVTAB,
  95. .sym_num = SYM_NUM,
  96. .ocon_num = OCON_NUM,
  97. },
  98. {
  99. .version = POLICYDB_VERSION_RANGETRANS,
  100. .sym_num = SYM_NUM,
  101. .ocon_num = OCON_NUM,
  102. },
  103. {
  104. .version = POLICYDB_VERSION_POLCAP,
  105. .sym_num = SYM_NUM,
  106. .ocon_num = OCON_NUM,
  107. },
  108. {
  109. .version = POLICYDB_VERSION_PERMISSIVE,
  110. .sym_num = SYM_NUM,
  111. .ocon_num = OCON_NUM,
  112. },
  113. {
  114. .version = POLICYDB_VERSION_BOUNDARY,
  115. .sym_num = SYM_NUM,
  116. .ocon_num = OCON_NUM,
  117. },
  118. {
  119. .version = POLICYDB_VERSION_FILENAME_TRANS,
  120. .sym_num = SYM_NUM,
  121. .ocon_num = OCON_NUM,
  122. },
  123. {
  124. .version = POLICYDB_VERSION_ROLETRANS,
  125. .sym_num = SYM_NUM,
  126. .ocon_num = OCON_NUM,
  127. },
  128. };
  129. 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. u16 string_to_security_class(struct policydb *p, const char *name)
  1484. {
  1485. struct class_datum *cladatum;
  1486. cladatum = hashtab_search(p->p_classes.table, name);
  1487. if (!cladatum)
  1488. return 0;
  1489. return cladatum->value;
  1490. }
  1491. u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
  1492. {
  1493. struct class_datum *cladatum;
  1494. struct perm_datum *perdatum = NULL;
  1495. struct common_datum *comdatum;
  1496. if (!tclass || tclass > p->p_classes.nprim)
  1497. return 0;
  1498. cladatum = p->class_val_to_struct[tclass-1];
  1499. comdatum = cladatum->comdatum;
  1500. if (comdatum)
  1501. perdatum = hashtab_search(comdatum->permissions.table,
  1502. name);
  1503. if (!perdatum)
  1504. perdatum = hashtab_search(cladatum->permissions.table,
  1505. name);
  1506. if (!perdatum)
  1507. return 0;
  1508. return 1U << (perdatum->value-1);
  1509. }
  1510. static int range_read(struct policydb *p, void *fp)
  1511. {
  1512. struct range_trans *rt = NULL;
  1513. struct mls_range *r = NULL;
  1514. int i, rc;
  1515. __le32 buf[2];
  1516. u32 nel;
  1517. if (p->policyvers < POLICYDB_VERSION_MLS)
  1518. return 0;
  1519. rc = next_entry(buf, fp, sizeof(u32));
  1520. if (rc)
  1521. goto out;
  1522. nel = le32_to_cpu(buf[0]);
  1523. for (i = 0; i < nel; i++) {
  1524. rc = -ENOMEM;
  1525. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  1526. if (!rt)
  1527. goto out;
  1528. rc = next_entry(buf, fp, (sizeof(u32) * 2));
  1529. if (rc)
  1530. goto out;
  1531. rt->source_type = le32_to_cpu(buf[0]);
  1532. rt->target_type = le32_to_cpu(buf[1]);
  1533. if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
  1534. rc = next_entry(buf, fp, sizeof(u32));
  1535. if (rc)
  1536. goto out;
  1537. rt->target_class = le32_to_cpu(buf[0]);
  1538. } else
  1539. rt->target_class = p->process_class;
  1540. rc = -EINVAL;
  1541. if (!policydb_type_isvalid(p, rt->source_type) ||
  1542. !policydb_type_isvalid(p, rt->target_type) ||
  1543. !policydb_class_isvalid(p, rt->target_class))
  1544. goto out;
  1545. rc = -ENOMEM;
  1546. r = kzalloc(sizeof(*r), GFP_KERNEL);
  1547. if (!r)
  1548. goto out;
  1549. rc = mls_read_range_helper(r, fp);
  1550. if (rc)
  1551. goto out;
  1552. rc = -EINVAL;
  1553. if (!mls_range_isvalid(p, r)) {
  1554. printk(KERN_WARNING "SELinux: rangetrans: invalid range\n");
  1555. goto out;
  1556. }
  1557. rc = hashtab_insert(p->range_tr, rt, r);
  1558. if (rc)
  1559. goto out;
  1560. rt = NULL;
  1561. r = NULL;
  1562. }
  1563. hash_eval(p->range_tr, "rangetr");
  1564. rc = 0;
  1565. out:
  1566. kfree(rt);
  1567. kfree(r);
  1568. return rc;
  1569. }
  1570. static int filename_trans_read(struct policydb *p, void *fp)
  1571. {
  1572. struct filename_trans *ft;
  1573. struct filename_trans_datum *otype;
  1574. char *name;
  1575. u32 nel, len;
  1576. __le32 buf[4];
  1577. int rc, i;
  1578. if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
  1579. return 0;
  1580. rc = next_entry(buf, fp, sizeof(u32));
  1581. if (rc)
  1582. return rc;
  1583. nel = le32_to_cpu(buf[0]);
  1584. for (i = 0; i < nel; i++) {
  1585. ft = NULL;
  1586. otype = NULL;
  1587. name = NULL;
  1588. rc = -ENOMEM;
  1589. ft = kzalloc(sizeof(*ft), GFP_KERNEL);
  1590. if (!ft)
  1591. goto out;
  1592. rc = -ENOMEM;
  1593. otype = kmalloc(sizeof(*otype), GFP_KERNEL);
  1594. if (!otype)
  1595. goto out;
  1596. /* length of the path component string */
  1597. rc = next_entry(buf, fp, sizeof(u32));
  1598. if (rc)
  1599. goto out;
  1600. len = le32_to_cpu(buf[0]);
  1601. rc = -ENOMEM;
  1602. name = kmalloc(len + 1, GFP_KERNEL);
  1603. if (!name)
  1604. goto out;
  1605. ft->name = name;
  1606. /* path component string */
  1607. rc = next_entry(name, fp, len);
  1608. if (rc)
  1609. goto out;
  1610. name[len] = 0;
  1611. rc = next_entry(buf, fp, sizeof(u32) * 4);
  1612. if (rc)
  1613. goto out;
  1614. ft->stype = le32_to_cpu(buf[0]);
  1615. ft->ttype = le32_to_cpu(buf[1]);
  1616. ft->tclass = le32_to_cpu(buf[2]);
  1617. otype->otype = le32_to_cpu(buf[3]);
  1618. rc = ebitmap_set_bit(&p->filename_trans_ttypes, ft->ttype, 1);
  1619. if (rc)
  1620. goto out;
  1621. hashtab_insert(p->filename_trans, ft, otype);
  1622. }
  1623. hash_eval(p->filename_trans, "filenametr");
  1624. return 0;
  1625. out:
  1626. kfree(ft);
  1627. kfree(name);
  1628. kfree(otype);
  1629. return rc;
  1630. }
  1631. static int genfs_read(struct policydb *p, void *fp)
  1632. {
  1633. int i, j, rc;
  1634. u32 nel, nel2, len, len2;
  1635. __le32 buf[1];
  1636. struct ocontext *l, *c;
  1637. struct ocontext *newc = NULL;
  1638. struct genfs *genfs_p, *genfs;
  1639. struct genfs *newgenfs = NULL;
  1640. rc = next_entry(buf, fp, sizeof(u32));
  1641. if (rc)
  1642. goto out;
  1643. nel = le32_to_cpu(buf[0]);
  1644. for (i = 0; i < nel; i++) {
  1645. rc = next_entry(buf, fp, sizeof(u32));
  1646. if (rc)
  1647. goto out;
  1648. len = le32_to_cpu(buf[0]);
  1649. rc = -ENOMEM;
  1650. newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
  1651. if (!newgenfs)
  1652. goto out;
  1653. rc = -ENOMEM;
  1654. newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
  1655. if (!newgenfs->fstype)
  1656. goto out;
  1657. rc = next_entry(newgenfs->fstype, fp, len);
  1658. if (rc)
  1659. goto out;
  1660. newgenfs->fstype[len] = 0;
  1661. for (genfs_p = NULL, genfs = p->genfs; genfs;
  1662. genfs_p = genfs, genfs = genfs->next) {
  1663. rc = -EINVAL;
  1664. if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
  1665. printk(KERN_ERR "SELinux: dup genfs fstype %s\n",
  1666. newgenfs->fstype);
  1667. goto out;
  1668. }
  1669. if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
  1670. break;
  1671. }
  1672. newgenfs->next = genfs;
  1673. if (genfs_p)
  1674. genfs_p->next = newgenfs;
  1675. else
  1676. p->genfs = newgenfs;
  1677. genfs = newgenfs;
  1678. newgenfs = NULL;
  1679. rc = next_entry(buf, fp, sizeof(u32));
  1680. if (rc)
  1681. goto out;
  1682. nel2 = le32_to_cpu(buf[0]);
  1683. for (j = 0; j < nel2; j++) {
  1684. rc = next_entry(buf, fp, sizeof(u32));
  1685. if (rc)
  1686. goto out;
  1687. len = le32_to_cpu(buf[0]);
  1688. rc = -ENOMEM;
  1689. newc = kzalloc(sizeof(*newc), GFP_KERNEL);
  1690. if (!newc)
  1691. goto out;
  1692. rc = -ENOMEM;
  1693. newc->u.name = kmalloc(len + 1, GFP_KERNEL);
  1694. if (!newc->u.name)
  1695. goto out;
  1696. rc = next_entry(newc->u.name, fp, len);
  1697. if (rc)
  1698. goto out;
  1699. newc->u.name[len] = 0;
  1700. rc = next_entry(buf, fp, sizeof(u32));
  1701. if (rc)
  1702. goto out;
  1703. newc->v.sclass = le32_to_cpu(buf[0]);
  1704. rc = context_read_and_validate(&newc->context[0], p, fp);
  1705. if (rc)
  1706. goto out;
  1707. for (l = NULL, c = genfs->head; c;
  1708. l = c, c = c->next) {
  1709. rc = -EINVAL;
  1710. if (!strcmp(newc->u.name, c->u.name) &&
  1711. (!c->v.sclass || !newc->v.sclass ||
  1712. newc->v.sclass == c->v.sclass)) {
  1713. printk(KERN_ERR "SELinux: dup genfs entry (%s,%s)\n",
  1714. genfs->fstype, c->u.name);
  1715. goto out;
  1716. }
  1717. len = strlen(newc->u.name);
  1718. len2 = strlen(c->u.name);
  1719. if (len > len2)
  1720. break;
  1721. }
  1722. newc->next = c;
  1723. if (l)
  1724. l->next = newc;
  1725. else
  1726. genfs->head = newc;
  1727. newc = NULL;
  1728. }
  1729. }
  1730. rc = 0;
  1731. out:
  1732. if (newgenfs)
  1733. kfree(newgenfs->fstype);
  1734. kfree(newgenfs);
  1735. ocontext_destroy(newc, OCON_FSUSE);
  1736. return rc;
  1737. }
  1738. static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
  1739. void *fp)
  1740. {
  1741. int i, j, rc;
  1742. u32 nel, len;
  1743. __le32 buf[3];
  1744. struct ocontext *l, *c;
  1745. u32 nodebuf[8];
  1746. for (i = 0; i < info->ocon_num; i++) {
  1747. rc = next_entry(buf, fp, sizeof(u32));
  1748. if (rc)
  1749. goto out;
  1750. nel = le32_to_cpu(buf[0]);
  1751. l = NULL;
  1752. for (j = 0; j < nel; j++) {
  1753. rc = -ENOMEM;
  1754. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1755. if (!c)
  1756. goto out;
  1757. if (l)
  1758. l->next = c;
  1759. else
  1760. p->ocontexts[i] = c;
  1761. l = c;
  1762. switch (i) {
  1763. case OCON_ISID:
  1764. rc = next_entry(buf, fp, sizeof(u32));
  1765. if (rc)
  1766. goto out;
  1767. c->sid[0] = le32_to_cpu(buf[0]);
  1768. rc = context_read_and_validate(&c->context[0], p, fp);
  1769. if (rc)
  1770. goto out;
  1771. break;
  1772. case OCON_FS:
  1773. case OCON_NETIF:
  1774. rc = next_entry(buf, fp, sizeof(u32));
  1775. if (rc)
  1776. goto out;
  1777. len = le32_to_cpu(buf[0]);
  1778. rc = -ENOMEM;
  1779. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1780. if (!c->u.name)
  1781. goto out;
  1782. rc = next_entry(c->u.name, fp, len);
  1783. if (rc)
  1784. goto out;
  1785. c->u.name[len] = 0;
  1786. rc = context_read_and_validate(&c->context[0], p, fp);
  1787. if (rc)
  1788. goto out;
  1789. rc = context_read_and_validate(&c->context[1], p, fp);
  1790. if (rc)
  1791. goto out;
  1792. break;
  1793. case OCON_PORT:
  1794. rc = next_entry(buf, fp, sizeof(u32)*3);
  1795. if (rc)
  1796. goto out;
  1797. c->u.port.protocol = le32_to_cpu(buf[0]);
  1798. c->u.port.low_port = le32_to_cpu(buf[1]);
  1799. c->u.port.high_port = le32_to_cpu(buf[2]);
  1800. rc = context_read_and_validate(&c->context[0], p, fp);
  1801. if (rc)
  1802. goto out;
  1803. break;
  1804. case OCON_NODE:
  1805. rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
  1806. if (rc)
  1807. goto out;
  1808. c->u.node.addr = nodebuf[0]; /* network order */
  1809. c->u.node.mask = nodebuf[1]; /* network order */
  1810. rc = context_read_and_validate(&c->context[0], p, fp);
  1811. if (rc)
  1812. goto out;
  1813. break;
  1814. case OCON_FSUSE:
  1815. rc = next_entry(buf, fp, sizeof(u32)*2);
  1816. if (rc)
  1817. goto out;
  1818. rc = -EINVAL;
  1819. c->v.behavior = le32_to_cpu(buf[0]);
  1820. if (c->v.behavior > SECURITY_FS_USE_NONE)
  1821. goto out;
  1822. rc = -ENOMEM;
  1823. len = le32_to_cpu(buf[1]);
  1824. c->u.name = kmalloc(len + 1, GFP_KERNEL);
  1825. if (!c->u.name)
  1826. goto out;
  1827. rc = next_entry(c->u.name, fp, len);
  1828. if (rc)
  1829. goto out;
  1830. c->u.name[len] = 0;
  1831. rc = context_read_and_validate(&c->context[0], p, fp);
  1832. if (rc)
  1833. goto out;
  1834. break;
  1835. case OCON_NODE6: {
  1836. int k;
  1837. rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
  1838. if (rc)
  1839. goto out;
  1840. for (k = 0; k < 4; k++)
  1841. c->u.node6.addr[k] = nodebuf[k];
  1842. for (k = 0; k < 4; k++)
  1843. c->u.node6.mask[k] = nodebuf[k+4];
  1844. rc = context_read_and_validate(&c->context[0], p, fp);
  1845. if (rc)
  1846. goto out;
  1847. break;
  1848. }
  1849. }
  1850. }
  1851. }
  1852. rc = 0;
  1853. out:
  1854. return rc;
  1855. }
  1856. /*
  1857. * Read the configuration data from a policy database binary
  1858. * representation file into a policy database structure.
  1859. */
  1860. int policydb_read(struct policydb *p, void *fp)
  1861. {
  1862. struct role_allow *ra, *lra;
  1863. struct role_trans *tr, *ltr;
  1864. int i, j, rc;
  1865. __le32 buf[4];
  1866. u32 len, nprim, nel;
  1867. char *policydb_str;
  1868. struct policydb_compat_info *info;
  1869. rc = policydb_init(p);
  1870. if (rc)
  1871. return rc;
  1872. /* Read the magic number and string length. */
  1873. rc = next_entry(buf, fp, sizeof(u32) * 2);
  1874. if (rc)
  1875. goto bad;
  1876. rc = -EINVAL;
  1877. if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
  1878. printk(KERN_ERR "SELinux: policydb magic number 0x%x does "
  1879. "not match expected magic number 0x%x\n",
  1880. le32_to_cpu(buf[0]), POLICYDB_MAGIC);
  1881. goto bad;
  1882. }
  1883. rc = -EINVAL;
  1884. len = le32_to_cpu(buf[1]);
  1885. if (len != strlen(POLICYDB_STRING)) {
  1886. printk(KERN_ERR "SELinux: policydb string length %d does not "
  1887. "match expected length %Zu\n",
  1888. len, strlen(POLICYDB_STRING));
  1889. goto bad;
  1890. }
  1891. rc = -ENOMEM;
  1892. policydb_str = kmalloc(len + 1, GFP_KERNEL);
  1893. if (!policydb_str) {
  1894. printk(KERN_ERR "SELinux: unable to allocate memory for policydb "
  1895. "string of length %d\n", len);
  1896. goto bad;
  1897. }
  1898. rc = next_entry(policydb_str, fp, len);
  1899. if (rc) {
  1900. printk(KERN_ERR "SELinux: truncated policydb string identifier\n");
  1901. kfree(policydb_str);
  1902. goto bad;
  1903. }
  1904. rc = -EINVAL;
  1905. policydb_str[len] = '\0';
  1906. if (strcmp(policydb_str, POLICYDB_STRING)) {
  1907. printk(KERN_ERR "SELinux: policydb string %s does not match "
  1908. "my string %s\n", policydb_str, POLICYDB_STRING);
  1909. kfree(policydb_str);
  1910. goto bad;
  1911. }
  1912. /* Done with policydb_str. */
  1913. kfree(policydb_str);
  1914. policydb_str = NULL;
  1915. /* Read the version and table sizes. */
  1916. rc = next_entry(buf, fp, sizeof(u32)*4);
  1917. if (rc)
  1918. goto bad;
  1919. rc = -EINVAL;
  1920. p->policyvers = le32_to_cpu(buf[0]);
  1921. if (p->policyvers < POLICYDB_VERSION_MIN ||
  1922. p->policyvers > POLICYDB_VERSION_MAX) {
  1923. printk(KERN_ERR "SELinux: policydb version %d does not match "
  1924. "my version range %d-%d\n",
  1925. le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
  1926. goto bad;
  1927. }
  1928. if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
  1929. p->mls_enabled = 1;
  1930. rc = -EINVAL;
  1931. if (p->policyvers < POLICYDB_VERSION_MLS) {
  1932. printk(KERN_ERR "SELinux: security policydb version %d "
  1933. "(MLS) not backwards compatible\n",
  1934. p->policyvers);
  1935. goto bad;
  1936. }
  1937. }
  1938. p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
  1939. p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
  1940. if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
  1941. rc = ebitmap_read(&p->policycaps, fp);
  1942. if (rc)
  1943. goto bad;
  1944. }
  1945. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
  1946. rc = ebitmap_read(&p->permissive_map, fp);
  1947. if (rc)
  1948. goto bad;
  1949. }
  1950. rc = -EINVAL;
  1951. info = policydb_lookup_compat(p->policyvers);
  1952. if (!info) {
  1953. printk(KERN_ERR "SELinux: unable to find policy compat info "
  1954. "for version %d\n", p->policyvers);
  1955. goto bad;
  1956. }
  1957. rc = -EINVAL;
  1958. if (le32_to_cpu(buf[2]) != info->sym_num ||
  1959. le32_to_cpu(buf[3]) != info->ocon_num) {
  1960. printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
  1961. "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
  1962. le32_to_cpu(buf[3]),
  1963. info->sym_num, info->ocon_num);
  1964. goto bad;
  1965. }
  1966. for (i = 0; i < info->sym_num; i++) {
  1967. rc = next_entry(buf, fp, sizeof(u32)*2);
  1968. if (rc)
  1969. goto bad;
  1970. nprim = le32_to_cpu(buf[0]);
  1971. nel = le32_to_cpu(buf[1]);
  1972. for (j = 0; j < nel; j++) {
  1973. rc = read_f[i](p, p->symtab[i].table, fp);
  1974. if (rc)
  1975. goto bad;
  1976. }
  1977. p->symtab[i].nprim = nprim;
  1978. }
  1979. rc = -EINVAL;
  1980. p->process_class = string_to_security_class(p, "process");
  1981. if (!p->process_class)
  1982. goto bad;
  1983. rc = avtab_read(&p->te_avtab, fp, p);
  1984. if (rc)
  1985. goto bad;
  1986. if (p->policyvers >= POLICYDB_VERSION_BOOL) {
  1987. rc = cond_read_list(p, fp);
  1988. if (rc)
  1989. goto bad;
  1990. }
  1991. rc = next_entry(buf, fp, sizeof(u32));
  1992. if (rc)
  1993. goto bad;
  1994. nel = le32_to_cpu(buf[0]);
  1995. ltr = NULL;
  1996. for (i = 0; i < nel; i++) {
  1997. rc = -ENOMEM;
  1998. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  1999. if (!tr)
  2000. goto bad;
  2001. if (ltr)
  2002. ltr->next = tr;
  2003. else
  2004. p->role_tr = tr;
  2005. rc = next_entry(buf, fp, sizeof(u32)*3);
  2006. if (rc)
  2007. goto bad;
  2008. rc = -EINVAL;
  2009. tr->role = le32_to_cpu(buf[0]);
  2010. tr->type = le32_to_cpu(buf[1]);
  2011. tr->new_role = le32_to_cpu(buf[2]);
  2012. if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
  2013. rc = next_entry(buf, fp, sizeof(u32));
  2014. if (rc)
  2015. goto bad;
  2016. tr->tclass = le32_to_cpu(buf[0]);
  2017. } else
  2018. tr->tclass = p->process_class;
  2019. if (!policydb_role_isvalid(p, tr->role) ||
  2020. !policydb_type_isvalid(p, tr->type) ||
  2021. !policydb_class_isvalid(p, tr->tclass) ||
  2022. !policydb_role_isvalid(p, tr->new_role))
  2023. goto bad;
  2024. ltr = tr;
  2025. }
  2026. rc = next_entry(buf, fp, sizeof(u32));
  2027. if (rc)
  2028. goto bad;
  2029. nel = le32_to_cpu(buf[0]);
  2030. lra = NULL;
  2031. for (i = 0; i < nel; i++) {
  2032. rc = -ENOMEM;
  2033. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  2034. if (!ra)
  2035. goto bad;
  2036. if (lra)
  2037. lra->next = ra;
  2038. else
  2039. p->role_allow = ra;
  2040. rc = next_entry(buf, fp, sizeof(u32)*2);
  2041. if (rc)
  2042. goto bad;
  2043. rc = -EINVAL;
  2044. ra->role = le32_to_cpu(buf[0]);
  2045. ra->new_role = le32_to_cpu(buf[1]);
  2046. if (!policydb_role_isvalid(p, ra->role) ||
  2047. !policydb_role_isvalid(p, ra->new_role))
  2048. goto bad;
  2049. lra = ra;
  2050. }
  2051. rc = filename_trans_read(p, fp);
  2052. if (rc)
  2053. goto bad;
  2054. rc = policydb_index(p);
  2055. if (rc)
  2056. goto bad;
  2057. rc = -EINVAL;
  2058. p->process_trans_perms = string_to_av_perm(p, p->process_class, "transition");
  2059. p->process_trans_perms |= string_to_av_perm(p, p->process_class, "dyntransition");
  2060. if (!p->process_trans_perms)
  2061. goto bad;
  2062. rc = ocontext_read(p, info, fp);
  2063. if (rc)
  2064. goto bad;
  2065. rc = genfs_read(p, fp);
  2066. if (rc)
  2067. goto bad;
  2068. rc = range_read(p, fp);
  2069. if (rc)
  2070. goto bad;
  2071. rc = -ENOMEM;
  2072. p->type_attr_map_array = flex_array_alloc(sizeof(struct ebitmap),
  2073. p->p_types.nprim,
  2074. GFP_KERNEL | __GFP_ZERO);
  2075. if (!p->type_attr_map_array)
  2076. goto bad;
  2077. /* preallocate so we don't have to worry about the put ever failing */
  2078. rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
  2079. GFP_KERNEL | __GFP_ZERO);
  2080. if (rc)
  2081. goto bad;
  2082. for (i = 0; i < p->p_types.nprim; i++) {
  2083. struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
  2084. BUG_ON(!e);
  2085. ebitmap_init(e);
  2086. if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
  2087. rc = ebitmap_read(e, fp);
  2088. if (rc)
  2089. goto bad;
  2090. }
  2091. /* add the type itself as the degenerate case */
  2092. rc = ebitmap_set_bit(e, i, 1);
  2093. if (rc)
  2094. goto bad;
  2095. }
  2096. rc = policydb_bounds_sanity_check(p);
  2097. if (rc)
  2098. goto bad;
  2099. rc = 0;
  2100. out:
  2101. return rc;
  2102. bad:
  2103. policydb_destroy(p);
  2104. goto out;
  2105. }
  2106. /*
  2107. * Write a MLS level structure to a policydb binary
  2108. * representation file.
  2109. */
  2110. static int mls_write_level(struct mls_level *l, void *fp)
  2111. {
  2112. __le32 buf[1];
  2113. int rc;
  2114. buf[0] = cpu_to_le32(l->sens);
  2115. rc = put_entry(buf, sizeof(u32), 1, fp);
  2116. if (rc)
  2117. return rc;
  2118. rc = ebitmap_write(&l->cat, fp);
  2119. if (rc)
  2120. return rc;
  2121. return 0;
  2122. }
  2123. /*
  2124. * Write a MLS range structure to a policydb binary
  2125. * representation file.
  2126. */
  2127. static int mls_write_range_helper(struct mls_range *r, void *fp)
  2128. {
  2129. __le32 buf[3];
  2130. size_t items;
  2131. int rc, eq;
  2132. eq = mls_level_eq(&r->level[1], &r->level[0]);
  2133. if (eq)
  2134. items = 2;
  2135. else
  2136. items = 3;
  2137. buf[0] = cpu_to_le32(items-1);
  2138. buf[1] = cpu_to_le32(r->level[0].sens);
  2139. if (!eq)
  2140. buf[2] = cpu_to_le32(r->level[1].sens);
  2141. BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
  2142. rc = put_entry(buf, sizeof(u32), items, fp);
  2143. if (rc)
  2144. return rc;
  2145. rc = ebitmap_write(&r->level[0].cat, fp);
  2146. if (rc)
  2147. return rc;
  2148. if (!eq) {
  2149. rc = ebitmap_write(&r->level[1].cat, fp);
  2150. if (rc)
  2151. return rc;
  2152. }
  2153. return 0;
  2154. }
  2155. static int sens_write(void *vkey, void *datum, void *ptr)
  2156. {
  2157. char *key = vkey;
  2158. struct level_datum *levdatum = datum;
  2159. struct policy_data *pd = ptr;
  2160. void *fp = pd->fp;
  2161. __le32 buf[2];
  2162. size_t len;
  2163. int rc;
  2164. len = strlen(key);
  2165. buf[0] = cpu_to_le32(len);
  2166. buf[1] = cpu_to_le32(levdatum->isalias);
  2167. rc = put_entry(buf, sizeof(u32), 2, fp);
  2168. if (rc)
  2169. return rc;
  2170. rc = put_entry(key, 1, len, fp);
  2171. if (rc)
  2172. return rc;
  2173. rc = mls_write_level(levdatum->level, fp);
  2174. if (rc)
  2175. return rc;
  2176. return 0;
  2177. }
  2178. static int cat_write(void *vkey, void *datum, void *ptr)
  2179. {
  2180. char *key = vkey;
  2181. struct cat_datum *catdatum = datum;
  2182. struct policy_data *pd = ptr;
  2183. void *fp = pd->fp;
  2184. __le32 buf[3];
  2185. size_t len;
  2186. int rc;
  2187. len = strlen(key);
  2188. buf[0] = cpu_to_le32(len);
  2189. buf[1] = cpu_to_le32(catdatum->value);
  2190. buf[2] = cpu_to_le32(catdatum->isalias);
  2191. rc = put_entry(buf, sizeof(u32), 3, fp);
  2192. if (rc)
  2193. return rc;
  2194. rc = put_entry(key, 1, len, fp);
  2195. if (rc)
  2196. return rc;
  2197. return 0;
  2198. }
  2199. static int role_trans_write(struct policydb *p, void *fp)
  2200. {
  2201. struct role_trans *r = p->role_tr;
  2202. struct role_trans *tr;
  2203. u32 buf[3];
  2204. size_t nel;
  2205. int rc;
  2206. nel = 0;
  2207. for (tr = r; tr; tr = tr->next)
  2208. nel++;
  2209. buf[0] = cpu_to_le32(nel);
  2210. rc = put_entry(buf, sizeof(u32), 1, fp);
  2211. if (rc)
  2212. return rc;
  2213. for (tr = r; tr; tr = tr->next) {
  2214. buf[0] = cpu_to_le32(tr->role);
  2215. buf[1] = cpu_to_le32(tr->type);
  2216. buf[2] = cpu_to_le32(tr->new_role);
  2217. rc = put_entry(buf, sizeof(u32), 3, fp);
  2218. if (rc)
  2219. return rc;
  2220. if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
  2221. buf[0] = cpu_to_le32(tr->tclass);
  2222. rc = put_entry(buf, sizeof(u32), 1, fp);
  2223. if (rc)
  2224. return rc;
  2225. }
  2226. }
  2227. return 0;
  2228. }
  2229. static int role_allow_write(struct role_allow *r, void *fp)
  2230. {
  2231. struct role_allow *ra;
  2232. u32 buf[2];
  2233. size_t nel;
  2234. int rc;
  2235. nel = 0;
  2236. for (ra = r; ra; ra = ra->next)
  2237. nel++;
  2238. buf[0] = cpu_to_le32(nel);
  2239. rc = put_entry(buf, sizeof(u32), 1, fp);
  2240. if (rc)
  2241. return rc;
  2242. for (ra = r; ra; ra = ra->next) {
  2243. buf[0] = cpu_to_le32(ra->role);
  2244. buf[1] = cpu_to_le32(ra->new_role);
  2245. rc = put_entry(buf, sizeof(u32), 2, fp);
  2246. if (rc)
  2247. return rc;
  2248. }
  2249. return 0;
  2250. }
  2251. /*
  2252. * Write a security context structure
  2253. * to a policydb binary representation file.
  2254. */
  2255. static int context_write(struct policydb *p, struct context *c,
  2256. void *fp)
  2257. {
  2258. int rc;
  2259. __le32 buf[3];
  2260. buf[0] = cpu_to_le32(c->user);
  2261. buf[1] = cpu_to_le32(c->role);
  2262. buf[2] = cpu_to_le32(c->type);
  2263. rc = put_entry(buf, sizeof(u32), 3, fp);
  2264. if (rc)
  2265. return rc;
  2266. rc = mls_write_range_helper(&c->range, fp);
  2267. if (rc)
  2268. return rc;
  2269. return 0;
  2270. }
  2271. /*
  2272. * The following *_write functions are used to
  2273. * write the symbol data to a policy database
  2274. * binary representation file.
  2275. */
  2276. static int perm_write(void *vkey, void *datum, void *fp)
  2277. {
  2278. char *key = vkey;
  2279. struct perm_datum *perdatum = datum;
  2280. __le32 buf[2];
  2281. size_t len;
  2282. int rc;
  2283. len = strlen(key);
  2284. buf[0] = cpu_to_le32(len);
  2285. buf[1] = cpu_to_le32(perdatum->value);
  2286. rc = put_entry(buf, sizeof(u32), 2, fp);
  2287. if (rc)
  2288. return rc;
  2289. rc = put_entry(key, 1, len, fp);
  2290. if (rc)
  2291. return rc;
  2292. return 0;
  2293. }
  2294. static int common_write(void *vkey, void *datum, void *ptr)
  2295. {
  2296. char *key = vkey;
  2297. struct common_datum *comdatum = datum;
  2298. struct policy_data *pd = ptr;
  2299. void *fp = pd->fp;
  2300. __le32 buf[4];
  2301. size_t len;
  2302. int rc;
  2303. len = strlen(key);
  2304. buf[0] = cpu_to_le32(len);
  2305. buf[1] = cpu_to_le32(comdatum->value);
  2306. buf[2] = cpu_to_le32(comdatum->permissions.nprim);
  2307. buf[3] = cpu_to_le32(comdatum->permissions.table->nel);
  2308. rc = put_entry(buf, sizeof(u32), 4, fp);
  2309. if (rc)
  2310. return rc;
  2311. rc = put_entry(key, 1, len, fp);
  2312. if (rc)
  2313. return rc;
  2314. rc = hashtab_map(comdatum->permissions.table, perm_write, fp);
  2315. if (rc)
  2316. return rc;
  2317. return 0;
  2318. }
  2319. static int write_cons_helper(struct policydb *p, struct constraint_node *node,
  2320. void *fp)
  2321. {
  2322. struct constraint_node *c;
  2323. struct constraint_expr *e;
  2324. __le32 buf[3];
  2325. u32 nel;
  2326. int rc;
  2327. for (c = node; c; c = c->next) {
  2328. nel = 0;
  2329. for (e = c->expr; e; e = e->next)
  2330. nel++;
  2331. buf[0] = cpu_to_le32(c->permissions);
  2332. buf[1] = cpu_to_le32(nel);
  2333. rc = put_entry(buf, sizeof(u32), 2, fp);
  2334. if (rc)
  2335. return rc;
  2336. for (e = c->expr; e; e = e->next) {
  2337. buf[0] = cpu_to_le32(e->expr_type);
  2338. buf[1] = cpu_to_le32(e->attr);
  2339. buf[2] = cpu_to_le32(e->op);
  2340. rc = put_entry(buf, sizeof(u32), 3, fp);
  2341. if (rc)
  2342. return rc;
  2343. switch (e->expr_type) {
  2344. case CEXPR_NAMES:
  2345. rc = ebitmap_write(&e->names, fp);
  2346. if (rc)
  2347. return rc;
  2348. break;
  2349. default:
  2350. break;
  2351. }
  2352. }
  2353. }
  2354. return 0;
  2355. }
  2356. static int class_write(void *vkey, void *datum, void *ptr)
  2357. {
  2358. char *key = vkey;
  2359. struct class_datum *cladatum = datum;
  2360. struct policy_data *pd = ptr;
  2361. void *fp = pd->fp;
  2362. struct policydb *p = pd->p;
  2363. struct constraint_node *c;
  2364. __le32 buf[6];
  2365. u32 ncons;
  2366. size_t len, len2;
  2367. int rc;
  2368. len = strlen(key);
  2369. if (cladatum->comkey)
  2370. len2 = strlen(cladatum->comkey);
  2371. else
  2372. len2 = 0;
  2373. ncons = 0;
  2374. for (c = cladatum->constraints; c; c = c->next)
  2375. ncons++;
  2376. buf[0] = cpu_to_le32(len);
  2377. buf[1] = cpu_to_le32(len2);
  2378. buf[2] = cpu_to_le32(cladatum->value);
  2379. buf[3] = cpu_to_le32(cladatum->permissions.nprim);
  2380. if (cladatum->permissions.table)
  2381. buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
  2382. else
  2383. buf[4] = 0;
  2384. buf[5] = cpu_to_le32(ncons);
  2385. rc = put_entry(buf, sizeof(u32), 6, fp);
  2386. if (rc)
  2387. return rc;
  2388. rc = put_entry(key, 1, len, fp);
  2389. if (rc)
  2390. return rc;
  2391. if (cladatum->comkey) {
  2392. rc = put_entry(cladatum->comkey, 1, len2, fp);
  2393. if (rc)
  2394. return rc;
  2395. }
  2396. rc = hashtab_map(cladatum->permissions.table, perm_write, fp);
  2397. if (rc)
  2398. return rc;
  2399. rc = write_cons_helper(p, cladatum->constraints, fp);
  2400. if (rc)
  2401. return rc;
  2402. /* write out the validatetrans rule */
  2403. ncons = 0;
  2404. for (c = cladatum->validatetrans; c; c = c->next)
  2405. ncons++;
  2406. buf[0] = cpu_to_le32(ncons);
  2407. rc = put_entry(buf, sizeof(u32), 1, fp);
  2408. if (rc)
  2409. return rc;
  2410. rc = write_cons_helper(p, cladatum->validatetrans, fp);
  2411. if (rc)
  2412. return rc;
  2413. return 0;
  2414. }
  2415. static int role_write(void *vkey, void *datum, void *ptr)
  2416. {
  2417. char *key = vkey;
  2418. struct role_datum *role = datum;
  2419. struct policy_data *pd = ptr;
  2420. void *fp = pd->fp;
  2421. struct policydb *p = pd->p;
  2422. __le32 buf[3];
  2423. size_t items, len;
  2424. int rc;
  2425. len = strlen(key);
  2426. items = 0;
  2427. buf[items++] = cpu_to_le32(len);
  2428. buf[items++] = cpu_to_le32(role->value);
  2429. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  2430. buf[items++] = cpu_to_le32(role->bounds);
  2431. BUG_ON(items > (sizeof(buf)/sizeof(buf[0])));
  2432. rc = put_entry(buf, sizeof(u32), items, fp);
  2433. if (rc)
  2434. return rc;
  2435. rc = put_entry(key, 1, len, fp);
  2436. if (rc)
  2437. return rc;
  2438. rc = ebitmap_write(&role->dominates, fp);
  2439. if (rc)
  2440. return rc;
  2441. rc = ebitmap_write(&role->types, fp);
  2442. if (rc)
  2443. return rc;
  2444. return 0;
  2445. }
  2446. static int type_write(void *vkey, void *datum, void *ptr)
  2447. {
  2448. char *key = vkey;
  2449. struct type_datum *typdatum = datum;
  2450. struct policy_data *pd = ptr;
  2451. struct policydb *p = pd->p;
  2452. void *fp = pd->fp;
  2453. __le32 buf[4];
  2454. int rc;
  2455. size_t items, len;
  2456. len = strlen(key);
  2457. items = 0;
  2458. buf[items++] = cpu_to_le32(len);
  2459. buf[items++] = cpu_to_le32(typdatum->value);
  2460. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
  2461. u32 properties = 0;
  2462. if (typdatum->primary)
  2463. properties |= TYPEDATUM_PROPERTY_PRIMARY;
  2464. if (typdatum->attribute)
  2465. properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
  2466. buf[items++] = cpu_to_le32(properties);
  2467. buf[items++] = cpu_to_le32(typdatum->bounds);
  2468. } else {
  2469. buf[items++] = cpu_to_le32(typdatum->primary);
  2470. }
  2471. BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
  2472. rc = put_entry(buf, sizeof(u32), items, fp);
  2473. if (rc)
  2474. return rc;
  2475. rc = put_entry(key, 1, len, fp);
  2476. if (rc)
  2477. return rc;
  2478. return 0;
  2479. }
  2480. static int user_write(void *vkey, void *datum, void *ptr)
  2481. {
  2482. char *key = vkey;
  2483. struct user_datum *usrdatum = datum;
  2484. struct policy_data *pd = ptr;
  2485. struct policydb *p = pd->p;
  2486. void *fp = pd->fp;
  2487. __le32 buf[3];
  2488. size_t items, len;
  2489. int rc;
  2490. len = strlen(key);
  2491. items = 0;
  2492. buf[items++] = cpu_to_le32(len);
  2493. buf[items++] = cpu_to_le32(usrdatum->value);
  2494. if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
  2495. buf[items++] = cpu_to_le32(usrdatum->bounds);
  2496. BUG_ON(items > (sizeof(buf) / sizeof(buf[0])));
  2497. rc = put_entry(buf, sizeof(u32), items, fp);
  2498. if (rc)
  2499. return rc;
  2500. rc = put_entry(key, 1, len, fp);
  2501. if (rc)
  2502. return rc;
  2503. rc = ebitmap_write(&usrdatum->roles, fp);
  2504. if (rc)
  2505. return rc;
  2506. rc = mls_write_range_helper(&usrdatum->range, fp);
  2507. if (rc)
  2508. return rc;
  2509. rc = mls_write_level(&usrdatum->dfltlevel, fp);
  2510. if (rc)
  2511. return rc;
  2512. return 0;
  2513. }
  2514. static int (*write_f[SYM_NUM]) (void *key, void *datum,
  2515. void *datap) =
  2516. {
  2517. common_write,
  2518. class_write,
  2519. role_write,
  2520. type_write,
  2521. user_write,
  2522. cond_write_bool,
  2523. sens_write,
  2524. cat_write,
  2525. };
  2526. static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
  2527. void *fp)
  2528. {
  2529. unsigned int i, j, rc;
  2530. size_t nel, len;
  2531. __le32 buf[3];
  2532. u32 nodebuf[8];
  2533. struct ocontext *c;
  2534. for (i = 0; i < info->ocon_num; i++) {
  2535. nel = 0;
  2536. for (c = p->ocontexts[i]; c; c = c->next)
  2537. nel++;
  2538. buf[0] = cpu_to_le32(nel);
  2539. rc = put_entry(buf, sizeof(u32), 1, fp);
  2540. if (rc)
  2541. return rc;
  2542. for (c = p->ocontexts[i]; c; c = c->next) {
  2543. switch (i) {
  2544. case OCON_ISID:
  2545. buf[0] = cpu_to_le32(c->sid[0]);
  2546. rc = put_entry(buf, sizeof(u32), 1, fp);
  2547. if (rc)
  2548. return rc;
  2549. rc = context_write(p, &c->context[0], fp);
  2550. if (rc)
  2551. return rc;
  2552. break;
  2553. case OCON_FS:
  2554. case OCON_NETIF:
  2555. len = strlen(c->u.name);
  2556. buf[0] = cpu_to_le32(len);
  2557. rc = put_entry(buf, sizeof(u32), 1, fp);
  2558. if (rc)
  2559. return rc;
  2560. rc = put_entry(c->u.name, 1, len, fp);
  2561. if (rc)
  2562. return rc;
  2563. rc = context_write(p, &c->context[0], fp);
  2564. if (rc)
  2565. return rc;
  2566. rc = context_write(p, &c->context[1], fp);
  2567. if (rc)
  2568. return rc;
  2569. break;
  2570. case OCON_PORT:
  2571. buf[0] = cpu_to_le32(c->u.port.protocol);
  2572. buf[1] = cpu_to_le32(c->u.port.low_port);
  2573. buf[2] = cpu_to_le32(c->u.port.high_port);
  2574. rc = put_entry(buf, sizeof(u32), 3, fp);
  2575. if (rc)
  2576. return rc;
  2577. rc = context_write(p, &c->context[0], fp);
  2578. if (rc)
  2579. return rc;
  2580. break;
  2581. case OCON_NODE:
  2582. nodebuf[0] = c->u.node.addr; /* network order */
  2583. nodebuf[1] = c->u.node.mask; /* network order */
  2584. rc = put_entry(nodebuf, sizeof(u32), 2, fp);
  2585. if (rc)
  2586. return rc;
  2587. rc = context_write(p, &c->context[0], fp);
  2588. if (rc)
  2589. return rc;
  2590. break;
  2591. case OCON_FSUSE:
  2592. buf[0] = cpu_to_le32(c->v.behavior);
  2593. len = strlen(c->u.name);
  2594. buf[1] = cpu_to_le32(len);
  2595. rc = put_entry(buf, sizeof(u32), 2, fp);
  2596. if (rc)
  2597. return rc;
  2598. rc = put_entry(c->u.name, 1, len, fp);
  2599. if (rc)
  2600. return rc;
  2601. rc = context_write(p, &c->context[0], fp);
  2602. if (rc)
  2603. return rc;
  2604. break;
  2605. case OCON_NODE6:
  2606. for (j = 0; j < 4; j++)
  2607. nodebuf[j] = c->u.node6.addr[j]; /* network order */
  2608. for (j = 0; j < 4; j++)
  2609. nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
  2610. rc = put_entry(nodebuf, sizeof(u32), 8, fp);
  2611. if (rc)
  2612. return rc;
  2613. rc = context_write(p, &c->context[0], fp);
  2614. if (rc)
  2615. return rc;
  2616. break;
  2617. }
  2618. }
  2619. }
  2620. return 0;
  2621. }
  2622. static int genfs_write(struct policydb *p, void *fp)
  2623. {
  2624. struct genfs *genfs;
  2625. struct ocontext *c;
  2626. size_t len;
  2627. __le32 buf[1];
  2628. int rc;
  2629. len = 0;
  2630. for (genfs = p->genfs; genfs; genfs = genfs->next)
  2631. len++;
  2632. buf[0] = cpu_to_le32(len);
  2633. rc = put_entry(buf, sizeof(u32), 1, fp);
  2634. if (rc)
  2635. return rc;
  2636. for (genfs = p->genfs; genfs; genfs = genfs->next) {
  2637. len = strlen(genfs->fstype);
  2638. buf[0] = cpu_to_le32(len);
  2639. rc = put_entry(buf, sizeof(u32), 1, fp);
  2640. if (rc)
  2641. return rc;
  2642. rc = put_entry(genfs->fstype, 1, len, fp);
  2643. if (rc)
  2644. return rc;
  2645. len = 0;
  2646. for (c = genfs->head; c; c = c->next)
  2647. len++;
  2648. buf[0] = cpu_to_le32(len);
  2649. rc = put_entry(buf, sizeof(u32), 1, fp);
  2650. if (rc)
  2651. return rc;
  2652. for (c = genfs->head; c; c = c->next) {
  2653. len = strlen(c->u.name);
  2654. buf[0] = cpu_to_le32(len);
  2655. rc = put_entry(buf, sizeof(u32), 1, fp);
  2656. if (rc)
  2657. return rc;
  2658. rc = put_entry(c->u.name, 1, len, fp);
  2659. if (rc)
  2660. return rc;
  2661. buf[0] = cpu_to_le32(c->v.sclass);
  2662. rc = put_entry(buf, sizeof(u32), 1, fp);
  2663. if (rc)
  2664. return rc;
  2665. rc = context_write(p, &c->context[0], fp);
  2666. if (rc)
  2667. return rc;
  2668. }
  2669. }
  2670. return 0;
  2671. }
  2672. static int hashtab_cnt(void *key, void *data, void *ptr)
  2673. {
  2674. int *cnt = ptr;
  2675. *cnt = *cnt + 1;
  2676. return 0;
  2677. }
  2678. static int range_write_helper(void *key, void *data, void *ptr)
  2679. {
  2680. __le32 buf[2];
  2681. struct range_trans *rt = key;
  2682. struct mls_range *r = data;
  2683. struct policy_data *pd = ptr;
  2684. void *fp = pd->fp;
  2685. struct policydb *p = pd->p;
  2686. int rc;
  2687. buf[0] = cpu_to_le32(rt->source_type);
  2688. buf[1] = cpu_to_le32(rt->target_type);
  2689. rc = put_entry(buf, sizeof(u32), 2, fp);
  2690. if (rc)
  2691. return rc;
  2692. if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
  2693. buf[0] = cpu_to_le32(rt->target_class);
  2694. rc = put_entry(buf, sizeof(u32), 1, fp);
  2695. if (rc)
  2696. return rc;
  2697. }
  2698. rc = mls_write_range_helper(r, fp);
  2699. if (rc)
  2700. return rc;
  2701. return 0;
  2702. }
  2703. static int range_write(struct policydb *p, void *fp)
  2704. {
  2705. size_t nel;
  2706. __le32 buf[1];
  2707. int rc;
  2708. struct policy_data pd;
  2709. pd.p = p;
  2710. pd.fp = fp;
  2711. /* count the number of entries in the hashtab */
  2712. nel = 0;
  2713. rc = hashtab_map(p->range_tr, hashtab_cnt, &nel);
  2714. if (rc)
  2715. return rc;
  2716. buf[0] = cpu_to_le32(nel);
  2717. rc = put_entry(buf, sizeof(u32), 1, fp);
  2718. if (rc)
  2719. return rc;
  2720. /* actually write all of the entries */
  2721. rc = hashtab_map(p->range_tr, range_write_helper, &pd);
  2722. if (rc)
  2723. return rc;
  2724. return 0;
  2725. }
  2726. static int filename_write_helper(void *key, void *data, void *ptr)
  2727. {
  2728. __le32 buf[4];
  2729. struct filename_trans *ft = key;
  2730. struct filename_trans_datum *otype = data;
  2731. void *fp = ptr;
  2732. int rc;
  2733. u32 len;
  2734. len = strlen(ft->name);
  2735. buf[0] = cpu_to_le32(len);
  2736. rc = put_entry(buf, sizeof(u32), 1, fp);
  2737. if (rc)
  2738. return rc;
  2739. rc = put_entry(ft->name, sizeof(char), len, fp);
  2740. if (rc)
  2741. return rc;
  2742. buf[0] = ft->stype;
  2743. buf[1] = ft->ttype;
  2744. buf[2] = ft->tclass;
  2745. buf[3] = otype->otype;
  2746. rc = put_entry(buf, sizeof(u32), 4, fp);
  2747. if (rc)
  2748. return rc;
  2749. return 0;
  2750. }
  2751. static int filename_trans_write(struct policydb *p, void *fp)
  2752. {
  2753. u32 nel;
  2754. __le32 buf[1];
  2755. int rc;
  2756. if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
  2757. return 0;
  2758. nel = 0;
  2759. rc = hashtab_map(p->filename_trans, hashtab_cnt, &nel);
  2760. if (rc)
  2761. return rc;
  2762. buf[0] = cpu_to_le32(nel);
  2763. rc = put_entry(buf, sizeof(u32), 1, fp);
  2764. if (rc)
  2765. return rc;
  2766. rc = hashtab_map(p->filename_trans, filename_write_helper, fp);
  2767. if (rc)
  2768. return rc;
  2769. return 0;
  2770. }
  2771. /*
  2772. * Write the configuration data in a policy database
  2773. * structure to a policy database binary representation
  2774. * file.
  2775. */
  2776. int policydb_write(struct policydb *p, void *fp)
  2777. {
  2778. unsigned int i, num_syms;
  2779. int rc;
  2780. __le32 buf[4];
  2781. u32 config;
  2782. size_t len;
  2783. struct policydb_compat_info *info;
  2784. /*
  2785. * refuse to write policy older than compressed avtab
  2786. * to simplify the writer. There are other tests dropped
  2787. * since we assume this throughout the writer code. Be
  2788. * careful if you ever try to remove this restriction
  2789. */
  2790. if (p->policyvers < POLICYDB_VERSION_AVTAB) {
  2791. printk(KERN_ERR "SELinux: refusing to write policy version %d."
  2792. " Because it is less than version %d\n", p->policyvers,
  2793. POLICYDB_VERSION_AVTAB);
  2794. return -EINVAL;
  2795. }
  2796. config = 0;
  2797. if (p->mls_enabled)
  2798. config |= POLICYDB_CONFIG_MLS;
  2799. if (p->reject_unknown)
  2800. config |= REJECT_UNKNOWN;
  2801. if (p->allow_unknown)
  2802. config |= ALLOW_UNKNOWN;
  2803. /* Write the magic number and string identifiers. */
  2804. buf[0] = cpu_to_le32(POLICYDB_MAGIC);
  2805. len = strlen(POLICYDB_STRING);
  2806. buf[1] = cpu_to_le32(len);
  2807. rc = put_entry(buf, sizeof(u32), 2, fp);
  2808. if (rc)
  2809. return rc;
  2810. rc = put_entry(POLICYDB_STRING, 1, len, fp);
  2811. if (rc)
  2812. return rc;
  2813. /* Write the version, config, and table sizes. */
  2814. info = policydb_lookup_compat(p->policyvers);
  2815. if (!info) {
  2816. printk(KERN_ERR "SELinux: compatibility lookup failed for policy "
  2817. "version %d", p->policyvers);
  2818. return -EINVAL;
  2819. }
  2820. buf[0] = cpu_to_le32(p->policyvers);
  2821. buf[1] = cpu_to_le32(config);
  2822. buf[2] = cpu_to_le32(info->sym_num);
  2823. buf[3] = cpu_to_le32(info->ocon_num);
  2824. rc = put_entry(buf, sizeof(u32), 4, fp);
  2825. if (rc)
  2826. return rc;
  2827. if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
  2828. rc = ebitmap_write(&p->policycaps, fp);
  2829. if (rc)
  2830. return rc;
  2831. }
  2832. if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
  2833. rc = ebitmap_write(&p->permissive_map, fp);
  2834. if (rc)
  2835. return rc;
  2836. }
  2837. num_syms = info->sym_num;
  2838. for (i = 0; i < num_syms; i++) {
  2839. struct policy_data pd;
  2840. pd.fp = fp;
  2841. pd.p = p;
  2842. buf[0] = cpu_to_le32(p->symtab[i].nprim);
  2843. buf[1] = cpu_to_le32(p->symtab[i].table->nel);
  2844. rc = put_entry(buf, sizeof(u32), 2, fp);
  2845. if (rc)
  2846. return rc;
  2847. rc = hashtab_map(p->symtab[i].table, write_f[i], &pd);
  2848. if (rc)
  2849. return rc;
  2850. }
  2851. rc = avtab_write(p, &p->te_avtab, fp);
  2852. if (rc)
  2853. return rc;
  2854. rc = cond_write_list(p, p->cond_list, fp);
  2855. if (rc)
  2856. return rc;
  2857. rc = role_trans_write(p, fp);
  2858. if (rc)
  2859. return rc;
  2860. rc = role_allow_write(p->role_allow, fp);
  2861. if (rc)
  2862. return rc;
  2863. rc = filename_trans_write(p, fp);
  2864. if (rc)
  2865. return rc;
  2866. rc = ocontext_write(p, info, fp);
  2867. if (rc)
  2868. return rc;
  2869. rc = genfs_write(p, fp);
  2870. if (rc)
  2871. return rc;
  2872. rc = range_write(p, fp);
  2873. if (rc)
  2874. return rc;
  2875. for (i = 0; i < p->p_types.nprim; i++) {
  2876. struct ebitmap *e = flex_array_get(p->type_attr_map_array, i);
  2877. BUG_ON(!e);
  2878. rc = ebitmap_write(e, fp);
  2879. if (rc)
  2880. return rc;
  2881. }
  2882. return 0;
  2883. }