policydb.c 70 KB

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