policydb.c 64 KB

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