policydb.c 63 KB

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