policydb.c 66 KB

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