policydb.c 66 KB

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