services.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  1. /*
  2. * Implementation of the security services.
  3. *
  4. * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. * James Morris <jmorris@redhat.com>
  6. *
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. * Support for context based audit filters.
  11. *
  12. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  13. *
  14. * Added conditional policy language extensions
  15. *
  16. * Updated: Hewlett-Packard <paul@paul-moore.com>
  17. *
  18. * Added support for NetLabel
  19. * Added support for the policy capability bitmap
  20. *
  21. * Updated: Chad Sellers <csellers@tresys.com>
  22. *
  23. * Added validation of kernel classes and permissions
  24. *
  25. * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
  26. *
  27. * Added support for bounds domain and audit messaged on masked permissions
  28. *
  29. * Updated: Guido Trentalancia <guido@trentalancia.com>
  30. *
  31. * Added support for runtime switching of the policy type
  32. *
  33. * Copyright (C) 2008, 2009 NEC Corporation
  34. * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
  35. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  36. * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
  37. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation, version 2.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/rcupdate.h>
  47. #include <linux/errno.h>
  48. #include <linux/in.h>
  49. #include <linux/sched.h>
  50. #include <linux/audit.h>
  51. #include <linux/mutex.h>
  52. #include <linux/selinux.h>
  53. #include <linux/flex_array.h>
  54. #include <linux/vmalloc.h>
  55. #include <net/netlabel.h>
  56. #include "flask.h"
  57. #include "avc.h"
  58. #include "avc_ss.h"
  59. #include "security.h"
  60. #include "context.h"
  61. #include "policydb.h"
  62. #include "sidtab.h"
  63. #include "services.h"
  64. #include "conditional.h"
  65. #include "mls.h"
  66. #include "objsec.h"
  67. #include "netlabel.h"
  68. #include "xfrm.h"
  69. #include "ebitmap.h"
  70. #include "audit.h"
  71. int selinux_policycap_netpeer;
  72. int selinux_policycap_openperm;
  73. int selinux_policycap_alwaysnetwork;
  74. static DEFINE_RWLOCK(policy_rwlock);
  75. static struct sidtab sidtab;
  76. struct policydb policydb;
  77. int ss_initialized;
  78. /*
  79. * The largest sequence number that has been used when
  80. * providing an access decision to the access vector cache.
  81. * The sequence number only changes when a policy change
  82. * occurs.
  83. */
  84. static u32 latest_granting;
  85. /* Forward declaration. */
  86. static int context_struct_to_string(struct context *context, char **scontext,
  87. u32 *scontext_len);
  88. static void context_struct_compute_av(struct context *scontext,
  89. struct context *tcontext,
  90. u16 tclass,
  91. struct av_decision *avd,
  92. struct extended_perms *xperms);
  93. struct selinux_mapping {
  94. u16 value; /* policy value */
  95. unsigned num_perms;
  96. u32 perms[sizeof(u32) * 8];
  97. };
  98. static struct selinux_mapping *current_mapping;
  99. static u16 current_mapping_size;
  100. static int selinux_set_mapping(struct policydb *pol,
  101. struct security_class_mapping *map,
  102. struct selinux_mapping **out_map_p,
  103. u16 *out_map_size)
  104. {
  105. struct selinux_mapping *out_map = NULL;
  106. size_t size = sizeof(struct selinux_mapping);
  107. u16 i, j;
  108. unsigned k;
  109. bool print_unknown_handle = false;
  110. /* Find number of classes in the input mapping */
  111. if (!map)
  112. return -EINVAL;
  113. i = 0;
  114. while (map[i].name)
  115. i++;
  116. /* Allocate space for the class records, plus one for class zero */
  117. out_map = kcalloc(++i, size, GFP_ATOMIC);
  118. if (!out_map)
  119. return -ENOMEM;
  120. /* Store the raw class and permission values */
  121. j = 0;
  122. while (map[j].name) {
  123. struct security_class_mapping *p_in = map + (j++);
  124. struct selinux_mapping *p_out = out_map + j;
  125. /* An empty class string skips ahead */
  126. if (!strcmp(p_in->name, "")) {
  127. p_out->num_perms = 0;
  128. continue;
  129. }
  130. p_out->value = string_to_security_class(pol, p_in->name);
  131. if (!p_out->value) {
  132. printk(KERN_INFO
  133. "SELinux: Class %s not defined in policy.\n",
  134. p_in->name);
  135. if (pol->reject_unknown)
  136. goto err;
  137. p_out->num_perms = 0;
  138. print_unknown_handle = true;
  139. continue;
  140. }
  141. k = 0;
  142. while (p_in->perms[k]) {
  143. /* An empty permission string skips ahead */
  144. if (!*p_in->perms[k]) {
  145. k++;
  146. continue;
  147. }
  148. p_out->perms[k] = string_to_av_perm(pol, p_out->value,
  149. p_in->perms[k]);
  150. if (!p_out->perms[k]) {
  151. printk(KERN_INFO
  152. "SELinux: Permission %s in class %s not defined in policy.\n",
  153. p_in->perms[k], p_in->name);
  154. if (pol->reject_unknown)
  155. goto err;
  156. print_unknown_handle = true;
  157. }
  158. k++;
  159. }
  160. p_out->num_perms = k;
  161. }
  162. if (print_unknown_handle)
  163. printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
  164. pol->allow_unknown ? "allowed" : "denied");
  165. *out_map_p = out_map;
  166. *out_map_size = i;
  167. return 0;
  168. err:
  169. kfree(out_map);
  170. return -EINVAL;
  171. }
  172. /*
  173. * Get real, policy values from mapped values
  174. */
  175. static u16 unmap_class(u16 tclass)
  176. {
  177. if (tclass < current_mapping_size)
  178. return current_mapping[tclass].value;
  179. return tclass;
  180. }
  181. /*
  182. * Get kernel value for class from its policy value
  183. */
  184. static u16 map_class(u16 pol_value)
  185. {
  186. u16 i;
  187. for (i = 1; i < current_mapping_size; i++) {
  188. if (current_mapping[i].value == pol_value)
  189. return i;
  190. }
  191. return SECCLASS_NULL;
  192. }
  193. static void map_decision(u16 tclass, struct av_decision *avd,
  194. int allow_unknown)
  195. {
  196. if (tclass < current_mapping_size) {
  197. unsigned i, n = current_mapping[tclass].num_perms;
  198. u32 result;
  199. for (i = 0, result = 0; i < n; i++) {
  200. if (avd->allowed & current_mapping[tclass].perms[i])
  201. result |= 1<<i;
  202. if (allow_unknown && !current_mapping[tclass].perms[i])
  203. result |= 1<<i;
  204. }
  205. avd->allowed = result;
  206. for (i = 0, result = 0; i < n; i++)
  207. if (avd->auditallow & current_mapping[tclass].perms[i])
  208. result |= 1<<i;
  209. avd->auditallow = result;
  210. for (i = 0, result = 0; i < n; i++) {
  211. if (avd->auditdeny & current_mapping[tclass].perms[i])
  212. result |= 1<<i;
  213. if (!allow_unknown && !current_mapping[tclass].perms[i])
  214. result |= 1<<i;
  215. }
  216. /*
  217. * In case the kernel has a bug and requests a permission
  218. * between num_perms and the maximum permission number, we
  219. * should audit that denial
  220. */
  221. for (; i < (sizeof(u32)*8); i++)
  222. result |= 1<<i;
  223. avd->auditdeny = result;
  224. }
  225. }
  226. int security_mls_enabled(void)
  227. {
  228. return policydb.mls_enabled;
  229. }
  230. /*
  231. * Return the boolean value of a constraint expression
  232. * when it is applied to the specified source and target
  233. * security contexts.
  234. *
  235. * xcontext is a special beast... It is used by the validatetrans rules
  236. * only. For these rules, scontext is the context before the transition,
  237. * tcontext is the context after the transition, and xcontext is the context
  238. * of the process performing the transition. All other callers of
  239. * constraint_expr_eval should pass in NULL for xcontext.
  240. */
  241. static int constraint_expr_eval(struct context *scontext,
  242. struct context *tcontext,
  243. struct context *xcontext,
  244. struct constraint_expr *cexpr)
  245. {
  246. u32 val1, val2;
  247. struct context *c;
  248. struct role_datum *r1, *r2;
  249. struct mls_level *l1, *l2;
  250. struct constraint_expr *e;
  251. int s[CEXPR_MAXDEPTH];
  252. int sp = -1;
  253. for (e = cexpr; e; e = e->next) {
  254. switch (e->expr_type) {
  255. case CEXPR_NOT:
  256. BUG_ON(sp < 0);
  257. s[sp] = !s[sp];
  258. break;
  259. case CEXPR_AND:
  260. BUG_ON(sp < 1);
  261. sp--;
  262. s[sp] &= s[sp + 1];
  263. break;
  264. case CEXPR_OR:
  265. BUG_ON(sp < 1);
  266. sp--;
  267. s[sp] |= s[sp + 1];
  268. break;
  269. case CEXPR_ATTR:
  270. if (sp == (CEXPR_MAXDEPTH - 1))
  271. return 0;
  272. switch (e->attr) {
  273. case CEXPR_USER:
  274. val1 = scontext->user;
  275. val2 = tcontext->user;
  276. break;
  277. case CEXPR_TYPE:
  278. val1 = scontext->type;
  279. val2 = tcontext->type;
  280. break;
  281. case CEXPR_ROLE:
  282. val1 = scontext->role;
  283. val2 = tcontext->role;
  284. r1 = policydb.role_val_to_struct[val1 - 1];
  285. r2 = policydb.role_val_to_struct[val2 - 1];
  286. switch (e->op) {
  287. case CEXPR_DOM:
  288. s[++sp] = ebitmap_get_bit(&r1->dominates,
  289. val2 - 1);
  290. continue;
  291. case CEXPR_DOMBY:
  292. s[++sp] = ebitmap_get_bit(&r2->dominates,
  293. val1 - 1);
  294. continue;
  295. case CEXPR_INCOMP:
  296. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  297. val2 - 1) &&
  298. !ebitmap_get_bit(&r2->dominates,
  299. val1 - 1));
  300. continue;
  301. default:
  302. break;
  303. }
  304. break;
  305. case CEXPR_L1L2:
  306. l1 = &(scontext->range.level[0]);
  307. l2 = &(tcontext->range.level[0]);
  308. goto mls_ops;
  309. case CEXPR_L1H2:
  310. l1 = &(scontext->range.level[0]);
  311. l2 = &(tcontext->range.level[1]);
  312. goto mls_ops;
  313. case CEXPR_H1L2:
  314. l1 = &(scontext->range.level[1]);
  315. l2 = &(tcontext->range.level[0]);
  316. goto mls_ops;
  317. case CEXPR_H1H2:
  318. l1 = &(scontext->range.level[1]);
  319. l2 = &(tcontext->range.level[1]);
  320. goto mls_ops;
  321. case CEXPR_L1H1:
  322. l1 = &(scontext->range.level[0]);
  323. l2 = &(scontext->range.level[1]);
  324. goto mls_ops;
  325. case CEXPR_L2H2:
  326. l1 = &(tcontext->range.level[0]);
  327. l2 = &(tcontext->range.level[1]);
  328. goto mls_ops;
  329. mls_ops:
  330. switch (e->op) {
  331. case CEXPR_EQ:
  332. s[++sp] = mls_level_eq(l1, l2);
  333. continue;
  334. case CEXPR_NEQ:
  335. s[++sp] = !mls_level_eq(l1, l2);
  336. continue;
  337. case CEXPR_DOM:
  338. s[++sp] = mls_level_dom(l1, l2);
  339. continue;
  340. case CEXPR_DOMBY:
  341. s[++sp] = mls_level_dom(l2, l1);
  342. continue;
  343. case CEXPR_INCOMP:
  344. s[++sp] = mls_level_incomp(l2, l1);
  345. continue;
  346. default:
  347. BUG();
  348. return 0;
  349. }
  350. break;
  351. default:
  352. BUG();
  353. return 0;
  354. }
  355. switch (e->op) {
  356. case CEXPR_EQ:
  357. s[++sp] = (val1 == val2);
  358. break;
  359. case CEXPR_NEQ:
  360. s[++sp] = (val1 != val2);
  361. break;
  362. default:
  363. BUG();
  364. return 0;
  365. }
  366. break;
  367. case CEXPR_NAMES:
  368. if (sp == (CEXPR_MAXDEPTH-1))
  369. return 0;
  370. c = scontext;
  371. if (e->attr & CEXPR_TARGET)
  372. c = tcontext;
  373. else if (e->attr & CEXPR_XTARGET) {
  374. c = xcontext;
  375. if (!c) {
  376. BUG();
  377. return 0;
  378. }
  379. }
  380. if (e->attr & CEXPR_USER)
  381. val1 = c->user;
  382. else if (e->attr & CEXPR_ROLE)
  383. val1 = c->role;
  384. else if (e->attr & CEXPR_TYPE)
  385. val1 = c->type;
  386. else {
  387. BUG();
  388. return 0;
  389. }
  390. switch (e->op) {
  391. case CEXPR_EQ:
  392. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  393. break;
  394. case CEXPR_NEQ:
  395. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  396. break;
  397. default:
  398. BUG();
  399. return 0;
  400. }
  401. break;
  402. default:
  403. BUG();
  404. return 0;
  405. }
  406. }
  407. BUG_ON(sp != 0);
  408. return s[0];
  409. }
  410. /*
  411. * security_dump_masked_av - dumps masked permissions during
  412. * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
  413. */
  414. static int dump_masked_av_helper(void *k, void *d, void *args)
  415. {
  416. struct perm_datum *pdatum = d;
  417. char **permission_names = args;
  418. BUG_ON(pdatum->value < 1 || pdatum->value > 32);
  419. permission_names[pdatum->value - 1] = (char *)k;
  420. return 0;
  421. }
  422. static void security_dump_masked_av(struct context *scontext,
  423. struct context *tcontext,
  424. u16 tclass,
  425. u32 permissions,
  426. const char *reason)
  427. {
  428. struct common_datum *common_dat;
  429. struct class_datum *tclass_dat;
  430. struct audit_buffer *ab;
  431. char *tclass_name;
  432. char *scontext_name = NULL;
  433. char *tcontext_name = NULL;
  434. char *permission_names[32];
  435. int index;
  436. u32 length;
  437. bool need_comma = false;
  438. if (!permissions)
  439. return;
  440. tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
  441. tclass_dat = policydb.class_val_to_struct[tclass - 1];
  442. common_dat = tclass_dat->comdatum;
  443. /* init permission_names */
  444. if (common_dat &&
  445. hashtab_map(common_dat->permissions.table,
  446. dump_masked_av_helper, permission_names) < 0)
  447. goto out;
  448. if (hashtab_map(tclass_dat->permissions.table,
  449. dump_masked_av_helper, permission_names) < 0)
  450. goto out;
  451. /* get scontext/tcontext in text form */
  452. if (context_struct_to_string(scontext,
  453. &scontext_name, &length) < 0)
  454. goto out;
  455. if (context_struct_to_string(tcontext,
  456. &tcontext_name, &length) < 0)
  457. goto out;
  458. /* audit a message */
  459. ab = audit_log_start(current->audit_context,
  460. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  461. if (!ab)
  462. goto out;
  463. audit_log_format(ab, "op=security_compute_av reason=%s "
  464. "scontext=%s tcontext=%s tclass=%s perms=",
  465. reason, scontext_name, tcontext_name, tclass_name);
  466. for (index = 0; index < 32; index++) {
  467. u32 mask = (1 << index);
  468. if ((mask & permissions) == 0)
  469. continue;
  470. audit_log_format(ab, "%s%s",
  471. need_comma ? "," : "",
  472. permission_names[index]
  473. ? permission_names[index] : "????");
  474. need_comma = true;
  475. }
  476. audit_log_end(ab);
  477. out:
  478. /* release scontext/tcontext */
  479. kfree(tcontext_name);
  480. kfree(scontext_name);
  481. return;
  482. }
  483. /*
  484. * security_boundary_permission - drops violated permissions
  485. * on boundary constraint.
  486. */
  487. static void type_attribute_bounds_av(struct context *scontext,
  488. struct context *tcontext,
  489. u16 tclass,
  490. struct av_decision *avd)
  491. {
  492. struct context lo_scontext;
  493. struct context lo_tcontext, *tcontextp = tcontext;
  494. struct av_decision lo_avd;
  495. struct type_datum *source;
  496. struct type_datum *target;
  497. u32 masked = 0;
  498. source = flex_array_get_ptr(policydb.type_val_to_struct_array,
  499. scontext->type - 1);
  500. BUG_ON(!source);
  501. if (!source->bounds)
  502. return;
  503. target = flex_array_get_ptr(policydb.type_val_to_struct_array,
  504. tcontext->type - 1);
  505. BUG_ON(!target);
  506. memset(&lo_avd, 0, sizeof(lo_avd));
  507. memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
  508. lo_scontext.type = source->bounds;
  509. if (target->bounds) {
  510. memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
  511. lo_tcontext.type = target->bounds;
  512. tcontextp = &lo_tcontext;
  513. }
  514. context_struct_compute_av(&lo_scontext,
  515. tcontextp,
  516. tclass,
  517. &lo_avd,
  518. NULL);
  519. masked = ~lo_avd.allowed & avd->allowed;
  520. if (likely(!masked))
  521. return; /* no masked permission */
  522. /* mask violated permissions */
  523. avd->allowed &= ~masked;
  524. /* audit masked permissions */
  525. security_dump_masked_av(scontext, tcontext,
  526. tclass, masked, "bounds");
  527. }
  528. /*
  529. * flag which drivers have permissions
  530. * only looking for ioctl based extended permssions
  531. */
  532. void services_compute_xperms_drivers(
  533. struct extended_perms *xperms,
  534. struct avtab_node *node)
  535. {
  536. unsigned int i;
  537. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  538. /* if one or more driver has all permissions allowed */
  539. for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
  540. xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
  541. } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  542. /* if allowing permissions within a driver */
  543. security_xperm_set(xperms->drivers.p,
  544. node->datum.u.xperms->driver);
  545. }
  546. /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
  547. if (node->key.specified & AVTAB_XPERMS_ALLOWED)
  548. xperms->len = 1;
  549. }
  550. /*
  551. * Compute access vectors and extended permissions based on a context
  552. * structure pair for the permissions in a particular class.
  553. */
  554. static void context_struct_compute_av(struct context *scontext,
  555. struct context *tcontext,
  556. u16 tclass,
  557. struct av_decision *avd,
  558. struct extended_perms *xperms)
  559. {
  560. struct constraint_node *constraint;
  561. struct role_allow *ra;
  562. struct avtab_key avkey;
  563. struct avtab_node *node;
  564. struct class_datum *tclass_datum;
  565. struct ebitmap *sattr, *tattr;
  566. struct ebitmap_node *snode, *tnode;
  567. unsigned int i, j;
  568. avd->allowed = 0;
  569. avd->auditallow = 0;
  570. avd->auditdeny = 0xffffffff;
  571. if (xperms) {
  572. memset(&xperms->drivers, 0, sizeof(xperms->drivers));
  573. xperms->len = 0;
  574. }
  575. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  576. if (printk_ratelimit())
  577. printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
  578. return;
  579. }
  580. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  581. /*
  582. * If a specific type enforcement rule was defined for
  583. * this permission check, then use it.
  584. */
  585. avkey.target_class = tclass;
  586. avkey.specified = AVTAB_AV | AVTAB_XPERMS;
  587. sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
  588. BUG_ON(!sattr);
  589. tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
  590. BUG_ON(!tattr);
  591. ebitmap_for_each_positive_bit(sattr, snode, i) {
  592. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  593. avkey.source_type = i + 1;
  594. avkey.target_type = j + 1;
  595. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  596. node;
  597. node = avtab_search_node_next(node, avkey.specified)) {
  598. if (node->key.specified == AVTAB_ALLOWED)
  599. avd->allowed |= node->datum.u.data;
  600. else if (node->key.specified == AVTAB_AUDITALLOW)
  601. avd->auditallow |= node->datum.u.data;
  602. else if (node->key.specified == AVTAB_AUDITDENY)
  603. avd->auditdeny &= node->datum.u.data;
  604. else if (xperms && (node->key.specified & AVTAB_XPERMS))
  605. services_compute_xperms_drivers(xperms, node);
  606. }
  607. /* Check conditional av table for additional permissions */
  608. cond_compute_av(&policydb.te_cond_avtab, &avkey,
  609. avd, xperms);
  610. }
  611. }
  612. /*
  613. * Remove any permissions prohibited by a constraint (this includes
  614. * the MLS policy).
  615. */
  616. constraint = tclass_datum->constraints;
  617. while (constraint) {
  618. if ((constraint->permissions & (avd->allowed)) &&
  619. !constraint_expr_eval(scontext, tcontext, NULL,
  620. constraint->expr)) {
  621. avd->allowed &= ~(constraint->permissions);
  622. }
  623. constraint = constraint->next;
  624. }
  625. /*
  626. * If checking process transition permission and the
  627. * role is changing, then check the (current_role, new_role)
  628. * pair.
  629. */
  630. if (tclass == policydb.process_class &&
  631. (avd->allowed & policydb.process_trans_perms) &&
  632. scontext->role != tcontext->role) {
  633. for (ra = policydb.role_allow; ra; ra = ra->next) {
  634. if (scontext->role == ra->role &&
  635. tcontext->role == ra->new_role)
  636. break;
  637. }
  638. if (!ra)
  639. avd->allowed &= ~policydb.process_trans_perms;
  640. }
  641. /*
  642. * If the given source and target types have boundary
  643. * constraint, lazy checks have to mask any violated
  644. * permission and notice it to userspace via audit.
  645. */
  646. type_attribute_bounds_av(scontext, tcontext,
  647. tclass, avd);
  648. }
  649. static int security_validtrans_handle_fail(struct context *ocontext,
  650. struct context *ncontext,
  651. struct context *tcontext,
  652. u16 tclass)
  653. {
  654. char *o = NULL, *n = NULL, *t = NULL;
  655. u32 olen, nlen, tlen;
  656. if (context_struct_to_string(ocontext, &o, &olen))
  657. goto out;
  658. if (context_struct_to_string(ncontext, &n, &nlen))
  659. goto out;
  660. if (context_struct_to_string(tcontext, &t, &tlen))
  661. goto out;
  662. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  663. "op=security_validate_transition seresult=denied"
  664. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  665. o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  666. out:
  667. kfree(o);
  668. kfree(n);
  669. kfree(t);
  670. if (!selinux_enforcing)
  671. return 0;
  672. return -EPERM;
  673. }
  674. static int security_compute_validatetrans(u32 oldsid, u32 newsid, u32 tasksid,
  675. u16 orig_tclass, bool user)
  676. {
  677. struct context *ocontext;
  678. struct context *ncontext;
  679. struct context *tcontext;
  680. struct class_datum *tclass_datum;
  681. struct constraint_node *constraint;
  682. u16 tclass;
  683. int rc = 0;
  684. if (!ss_initialized)
  685. return 0;
  686. read_lock(&policy_rwlock);
  687. if (!user)
  688. tclass = unmap_class(orig_tclass);
  689. else
  690. tclass = orig_tclass;
  691. if (!tclass || tclass > policydb.p_classes.nprim) {
  692. rc = -EINVAL;
  693. goto out;
  694. }
  695. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  696. ocontext = sidtab_search(&sidtab, oldsid);
  697. if (!ocontext) {
  698. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  699. __func__, oldsid);
  700. rc = -EINVAL;
  701. goto out;
  702. }
  703. ncontext = sidtab_search(&sidtab, newsid);
  704. if (!ncontext) {
  705. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  706. __func__, newsid);
  707. rc = -EINVAL;
  708. goto out;
  709. }
  710. tcontext = sidtab_search(&sidtab, tasksid);
  711. if (!tcontext) {
  712. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  713. __func__, tasksid);
  714. rc = -EINVAL;
  715. goto out;
  716. }
  717. constraint = tclass_datum->validatetrans;
  718. while (constraint) {
  719. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  720. constraint->expr)) {
  721. if (user)
  722. rc = -EPERM;
  723. else
  724. rc = security_validtrans_handle_fail(ocontext,
  725. ncontext,
  726. tcontext,
  727. tclass);
  728. goto out;
  729. }
  730. constraint = constraint->next;
  731. }
  732. out:
  733. read_unlock(&policy_rwlock);
  734. return rc;
  735. }
  736. int security_validate_transition_user(u32 oldsid, u32 newsid, u32 tasksid,
  737. u16 tclass)
  738. {
  739. return security_compute_validatetrans(oldsid, newsid, tasksid,
  740. tclass, true);
  741. }
  742. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  743. u16 orig_tclass)
  744. {
  745. return security_compute_validatetrans(oldsid, newsid, tasksid,
  746. orig_tclass, false);
  747. }
  748. /*
  749. * security_bounded_transition - check whether the given
  750. * transition is directed to bounded, or not.
  751. * It returns 0, if @newsid is bounded by @oldsid.
  752. * Otherwise, it returns error code.
  753. *
  754. * @oldsid : current security identifier
  755. * @newsid : destinated security identifier
  756. */
  757. int security_bounded_transition(u32 old_sid, u32 new_sid)
  758. {
  759. struct context *old_context, *new_context;
  760. struct type_datum *type;
  761. int index;
  762. int rc;
  763. if (!ss_initialized)
  764. return 0;
  765. read_lock(&policy_rwlock);
  766. rc = -EINVAL;
  767. old_context = sidtab_search(&sidtab, old_sid);
  768. if (!old_context) {
  769. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  770. __func__, old_sid);
  771. goto out;
  772. }
  773. rc = -EINVAL;
  774. new_context = sidtab_search(&sidtab, new_sid);
  775. if (!new_context) {
  776. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  777. __func__, new_sid);
  778. goto out;
  779. }
  780. rc = 0;
  781. /* type/domain unchanged */
  782. if (old_context->type == new_context->type)
  783. goto out;
  784. index = new_context->type;
  785. while (true) {
  786. type = flex_array_get_ptr(policydb.type_val_to_struct_array,
  787. index - 1);
  788. BUG_ON(!type);
  789. /* not bounded anymore */
  790. rc = -EPERM;
  791. if (!type->bounds)
  792. break;
  793. /* @newsid is bounded by @oldsid */
  794. rc = 0;
  795. if (type->bounds == old_context->type)
  796. break;
  797. index = type->bounds;
  798. }
  799. if (rc) {
  800. char *old_name = NULL;
  801. char *new_name = NULL;
  802. u32 length;
  803. if (!context_struct_to_string(old_context,
  804. &old_name, &length) &&
  805. !context_struct_to_string(new_context,
  806. &new_name, &length)) {
  807. audit_log(current->audit_context,
  808. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  809. "op=security_bounded_transition "
  810. "seresult=denied "
  811. "oldcontext=%s newcontext=%s",
  812. old_name, new_name);
  813. }
  814. kfree(new_name);
  815. kfree(old_name);
  816. }
  817. out:
  818. read_unlock(&policy_rwlock);
  819. return rc;
  820. }
  821. static void avd_init(struct av_decision *avd)
  822. {
  823. avd->allowed = 0;
  824. avd->auditallow = 0;
  825. avd->auditdeny = 0xffffffff;
  826. avd->seqno = latest_granting;
  827. avd->flags = 0;
  828. }
  829. void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
  830. struct avtab_node *node)
  831. {
  832. unsigned int i;
  833. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  834. if (xpermd->driver != node->datum.u.xperms->driver)
  835. return;
  836. } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  837. if (!security_xperm_test(node->datum.u.xperms->perms.p,
  838. xpermd->driver))
  839. return;
  840. } else {
  841. BUG();
  842. }
  843. if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
  844. xpermd->used |= XPERMS_ALLOWED;
  845. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  846. memset(xpermd->allowed->p, 0xff,
  847. sizeof(xpermd->allowed->p));
  848. }
  849. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  850. for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
  851. xpermd->allowed->p[i] |=
  852. node->datum.u.xperms->perms.p[i];
  853. }
  854. } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
  855. xpermd->used |= XPERMS_AUDITALLOW;
  856. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  857. memset(xpermd->auditallow->p, 0xff,
  858. sizeof(xpermd->auditallow->p));
  859. }
  860. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  861. for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
  862. xpermd->auditallow->p[i] |=
  863. node->datum.u.xperms->perms.p[i];
  864. }
  865. } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
  866. xpermd->used |= XPERMS_DONTAUDIT;
  867. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  868. memset(xpermd->dontaudit->p, 0xff,
  869. sizeof(xpermd->dontaudit->p));
  870. }
  871. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  872. for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
  873. xpermd->dontaudit->p[i] |=
  874. node->datum.u.xperms->perms.p[i];
  875. }
  876. } else {
  877. BUG();
  878. }
  879. }
  880. void security_compute_xperms_decision(u32 ssid,
  881. u32 tsid,
  882. u16 orig_tclass,
  883. u8 driver,
  884. struct extended_perms_decision *xpermd)
  885. {
  886. u16 tclass;
  887. struct context *scontext, *tcontext;
  888. struct avtab_key avkey;
  889. struct avtab_node *node;
  890. struct ebitmap *sattr, *tattr;
  891. struct ebitmap_node *snode, *tnode;
  892. unsigned int i, j;
  893. xpermd->driver = driver;
  894. xpermd->used = 0;
  895. memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
  896. memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
  897. memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
  898. read_lock(&policy_rwlock);
  899. if (!ss_initialized)
  900. goto allow;
  901. scontext = sidtab_search(&sidtab, ssid);
  902. if (!scontext) {
  903. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  904. __func__, ssid);
  905. goto out;
  906. }
  907. tcontext = sidtab_search(&sidtab, tsid);
  908. if (!tcontext) {
  909. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  910. __func__, tsid);
  911. goto out;
  912. }
  913. tclass = unmap_class(orig_tclass);
  914. if (unlikely(orig_tclass && !tclass)) {
  915. if (policydb.allow_unknown)
  916. goto allow;
  917. goto out;
  918. }
  919. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  920. pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
  921. goto out;
  922. }
  923. avkey.target_class = tclass;
  924. avkey.specified = AVTAB_XPERMS;
  925. sattr = flex_array_get(policydb.type_attr_map_array,
  926. scontext->type - 1);
  927. BUG_ON(!sattr);
  928. tattr = flex_array_get(policydb.type_attr_map_array,
  929. tcontext->type - 1);
  930. BUG_ON(!tattr);
  931. ebitmap_for_each_positive_bit(sattr, snode, i) {
  932. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  933. avkey.source_type = i + 1;
  934. avkey.target_type = j + 1;
  935. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  936. node;
  937. node = avtab_search_node_next(node, avkey.specified))
  938. services_compute_xperms_decision(xpermd, node);
  939. cond_compute_xperms(&policydb.te_cond_avtab,
  940. &avkey, xpermd);
  941. }
  942. }
  943. out:
  944. read_unlock(&policy_rwlock);
  945. return;
  946. allow:
  947. memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
  948. goto out;
  949. }
  950. /**
  951. * security_compute_av - Compute access vector decisions.
  952. * @ssid: source security identifier
  953. * @tsid: target security identifier
  954. * @tclass: target security class
  955. * @avd: access vector decisions
  956. * @xperms: extended permissions
  957. *
  958. * Compute a set of access vector decisions based on the
  959. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  960. */
  961. void security_compute_av(u32 ssid,
  962. u32 tsid,
  963. u16 orig_tclass,
  964. struct av_decision *avd,
  965. struct extended_perms *xperms)
  966. {
  967. u16 tclass;
  968. struct context *scontext = NULL, *tcontext = NULL;
  969. read_lock(&policy_rwlock);
  970. avd_init(avd);
  971. xperms->len = 0;
  972. if (!ss_initialized)
  973. goto allow;
  974. scontext = sidtab_search(&sidtab, ssid);
  975. if (!scontext) {
  976. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  977. __func__, ssid);
  978. goto out;
  979. }
  980. /* permissive domain? */
  981. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  982. avd->flags |= AVD_FLAGS_PERMISSIVE;
  983. tcontext = sidtab_search(&sidtab, tsid);
  984. if (!tcontext) {
  985. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  986. __func__, tsid);
  987. goto out;
  988. }
  989. tclass = unmap_class(orig_tclass);
  990. if (unlikely(orig_tclass && !tclass)) {
  991. if (policydb.allow_unknown)
  992. goto allow;
  993. goto out;
  994. }
  995. context_struct_compute_av(scontext, tcontext, tclass, avd, xperms);
  996. map_decision(orig_tclass, avd, policydb.allow_unknown);
  997. out:
  998. read_unlock(&policy_rwlock);
  999. return;
  1000. allow:
  1001. avd->allowed = 0xffffffff;
  1002. goto out;
  1003. }
  1004. void security_compute_av_user(u32 ssid,
  1005. u32 tsid,
  1006. u16 tclass,
  1007. struct av_decision *avd)
  1008. {
  1009. struct context *scontext = NULL, *tcontext = NULL;
  1010. read_lock(&policy_rwlock);
  1011. avd_init(avd);
  1012. if (!ss_initialized)
  1013. goto allow;
  1014. scontext = sidtab_search(&sidtab, ssid);
  1015. if (!scontext) {
  1016. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1017. __func__, ssid);
  1018. goto out;
  1019. }
  1020. /* permissive domain? */
  1021. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  1022. avd->flags |= AVD_FLAGS_PERMISSIVE;
  1023. tcontext = sidtab_search(&sidtab, tsid);
  1024. if (!tcontext) {
  1025. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1026. __func__, tsid);
  1027. goto out;
  1028. }
  1029. if (unlikely(!tclass)) {
  1030. if (policydb.allow_unknown)
  1031. goto allow;
  1032. goto out;
  1033. }
  1034. context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
  1035. out:
  1036. read_unlock(&policy_rwlock);
  1037. return;
  1038. allow:
  1039. avd->allowed = 0xffffffff;
  1040. goto out;
  1041. }
  1042. /*
  1043. * Write the security context string representation of
  1044. * the context structure `context' into a dynamically
  1045. * allocated string of the correct size. Set `*scontext'
  1046. * to point to this string and set `*scontext_len' to
  1047. * the length of the string.
  1048. */
  1049. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  1050. {
  1051. char *scontextp;
  1052. if (scontext)
  1053. *scontext = NULL;
  1054. *scontext_len = 0;
  1055. if (context->len) {
  1056. *scontext_len = context->len;
  1057. if (scontext) {
  1058. *scontext = kstrdup(context->str, GFP_ATOMIC);
  1059. if (!(*scontext))
  1060. return -ENOMEM;
  1061. }
  1062. return 0;
  1063. }
  1064. /* Compute the size of the context. */
  1065. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  1066. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  1067. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  1068. *scontext_len += mls_compute_context_len(context);
  1069. if (!scontext)
  1070. return 0;
  1071. /* Allocate space for the context; caller must free this space. */
  1072. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  1073. if (!scontextp)
  1074. return -ENOMEM;
  1075. *scontext = scontextp;
  1076. /*
  1077. * Copy the user name, role name and type name into the context.
  1078. */
  1079. scontextp += sprintf(scontextp, "%s:%s:%s",
  1080. sym_name(&policydb, SYM_USERS, context->user - 1),
  1081. sym_name(&policydb, SYM_ROLES, context->role - 1),
  1082. sym_name(&policydb, SYM_TYPES, context->type - 1));
  1083. mls_sid_to_context(context, &scontextp);
  1084. *scontextp = 0;
  1085. return 0;
  1086. }
  1087. #include "initial_sid_to_string.h"
  1088. const char *security_get_initial_sid_context(u32 sid)
  1089. {
  1090. if (unlikely(sid > SECINITSID_NUM))
  1091. return NULL;
  1092. return initial_sid_to_string[sid];
  1093. }
  1094. static int security_sid_to_context_core(u32 sid, char **scontext,
  1095. u32 *scontext_len, int force)
  1096. {
  1097. struct context *context;
  1098. int rc = 0;
  1099. if (scontext)
  1100. *scontext = NULL;
  1101. *scontext_len = 0;
  1102. if (!ss_initialized) {
  1103. if (sid <= SECINITSID_NUM) {
  1104. char *scontextp;
  1105. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  1106. if (!scontext)
  1107. goto out;
  1108. scontextp = kmemdup(initial_sid_to_string[sid],
  1109. *scontext_len, GFP_ATOMIC);
  1110. if (!scontextp) {
  1111. rc = -ENOMEM;
  1112. goto out;
  1113. }
  1114. *scontext = scontextp;
  1115. goto out;
  1116. }
  1117. printk(KERN_ERR "SELinux: %s: called before initial "
  1118. "load_policy on unknown SID %d\n", __func__, sid);
  1119. rc = -EINVAL;
  1120. goto out;
  1121. }
  1122. read_lock(&policy_rwlock);
  1123. if (force)
  1124. context = sidtab_search_force(&sidtab, sid);
  1125. else
  1126. context = sidtab_search(&sidtab, sid);
  1127. if (!context) {
  1128. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1129. __func__, sid);
  1130. rc = -EINVAL;
  1131. goto out_unlock;
  1132. }
  1133. rc = context_struct_to_string(context, scontext, scontext_len);
  1134. out_unlock:
  1135. read_unlock(&policy_rwlock);
  1136. out:
  1137. return rc;
  1138. }
  1139. /**
  1140. * security_sid_to_context - Obtain a context for a given SID.
  1141. * @sid: security identifier, SID
  1142. * @scontext: security context
  1143. * @scontext_len: length in bytes
  1144. *
  1145. * Write the string representation of the context associated with @sid
  1146. * into a dynamically allocated string of the correct size. Set @scontext
  1147. * to point to this string and set @scontext_len to the length of the string.
  1148. */
  1149. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  1150. {
  1151. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  1152. }
  1153. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  1154. {
  1155. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  1156. }
  1157. /*
  1158. * Caveat: Mutates scontext.
  1159. */
  1160. static int string_to_context_struct(struct policydb *pol,
  1161. struct sidtab *sidtabp,
  1162. char *scontext,
  1163. u32 scontext_len,
  1164. struct context *ctx,
  1165. u32 def_sid)
  1166. {
  1167. struct role_datum *role;
  1168. struct type_datum *typdatum;
  1169. struct user_datum *usrdatum;
  1170. char *scontextp, *p, oldc;
  1171. int rc = 0;
  1172. context_init(ctx);
  1173. /* Parse the security context. */
  1174. rc = -EINVAL;
  1175. scontextp = (char *) scontext;
  1176. /* Extract the user. */
  1177. p = scontextp;
  1178. while (*p && *p != ':')
  1179. p++;
  1180. if (*p == 0)
  1181. goto out;
  1182. *p++ = 0;
  1183. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  1184. if (!usrdatum)
  1185. goto out;
  1186. ctx->user = usrdatum->value;
  1187. /* Extract role. */
  1188. scontextp = p;
  1189. while (*p && *p != ':')
  1190. p++;
  1191. if (*p == 0)
  1192. goto out;
  1193. *p++ = 0;
  1194. role = hashtab_search(pol->p_roles.table, scontextp);
  1195. if (!role)
  1196. goto out;
  1197. ctx->role = role->value;
  1198. /* Extract type. */
  1199. scontextp = p;
  1200. while (*p && *p != ':')
  1201. p++;
  1202. oldc = *p;
  1203. *p++ = 0;
  1204. typdatum = hashtab_search(pol->p_types.table, scontextp);
  1205. if (!typdatum || typdatum->attribute)
  1206. goto out;
  1207. ctx->type = typdatum->value;
  1208. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  1209. if (rc)
  1210. goto out;
  1211. rc = -EINVAL;
  1212. if ((p - scontext) < scontext_len)
  1213. goto out;
  1214. /* Check the validity of the new context. */
  1215. if (!policydb_context_isvalid(pol, ctx))
  1216. goto out;
  1217. rc = 0;
  1218. out:
  1219. if (rc)
  1220. context_destroy(ctx);
  1221. return rc;
  1222. }
  1223. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  1224. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  1225. int force)
  1226. {
  1227. char *scontext2, *str = NULL;
  1228. struct context context;
  1229. int rc = 0;
  1230. /* An empty security context is never valid. */
  1231. if (!scontext_len)
  1232. return -EINVAL;
  1233. /* Copy the string to allow changes and ensure a NUL terminator */
  1234. scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
  1235. if (!scontext2)
  1236. return -ENOMEM;
  1237. if (!ss_initialized) {
  1238. int i;
  1239. for (i = 1; i < SECINITSID_NUM; i++) {
  1240. if (!strcmp(initial_sid_to_string[i], scontext2)) {
  1241. *sid = i;
  1242. goto out;
  1243. }
  1244. }
  1245. *sid = SECINITSID_KERNEL;
  1246. goto out;
  1247. }
  1248. *sid = SECSID_NULL;
  1249. if (force) {
  1250. /* Save another copy for storing in uninterpreted form */
  1251. rc = -ENOMEM;
  1252. str = kstrdup(scontext2, gfp_flags);
  1253. if (!str)
  1254. goto out;
  1255. }
  1256. read_lock(&policy_rwlock);
  1257. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  1258. scontext_len, &context, def_sid);
  1259. if (rc == -EINVAL && force) {
  1260. context.str = str;
  1261. context.len = strlen(str) + 1;
  1262. str = NULL;
  1263. } else if (rc)
  1264. goto out_unlock;
  1265. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  1266. context_destroy(&context);
  1267. out_unlock:
  1268. read_unlock(&policy_rwlock);
  1269. out:
  1270. kfree(scontext2);
  1271. kfree(str);
  1272. return rc;
  1273. }
  1274. /**
  1275. * security_context_to_sid - Obtain a SID for a given security context.
  1276. * @scontext: security context
  1277. * @scontext_len: length in bytes
  1278. * @sid: security identifier, SID
  1279. * @gfp: context for the allocation
  1280. *
  1281. * Obtains a SID associated with the security context that
  1282. * has the string representation specified by @scontext.
  1283. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1284. * memory is available, or 0 on success.
  1285. */
  1286. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid,
  1287. gfp_t gfp)
  1288. {
  1289. return security_context_to_sid_core(scontext, scontext_len,
  1290. sid, SECSID_NULL, gfp, 0);
  1291. }
  1292. int security_context_str_to_sid(const char *scontext, u32 *sid, gfp_t gfp)
  1293. {
  1294. return security_context_to_sid(scontext, strlen(scontext), sid, gfp);
  1295. }
  1296. /**
  1297. * security_context_to_sid_default - Obtain a SID for a given security context,
  1298. * falling back to specified default if needed.
  1299. *
  1300. * @scontext: security context
  1301. * @scontext_len: length in bytes
  1302. * @sid: security identifier, SID
  1303. * @def_sid: default SID to assign on error
  1304. *
  1305. * Obtains a SID associated with the security context that
  1306. * has the string representation specified by @scontext.
  1307. * The default SID is passed to the MLS layer to be used to allow
  1308. * kernel labeling of the MLS field if the MLS field is not present
  1309. * (for upgrading to MLS without full relabel).
  1310. * Implicitly forces adding of the context even if it cannot be mapped yet.
  1311. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1312. * memory is available, or 0 on success.
  1313. */
  1314. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  1315. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1316. {
  1317. return security_context_to_sid_core(scontext, scontext_len,
  1318. sid, def_sid, gfp_flags, 1);
  1319. }
  1320. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1321. u32 *sid)
  1322. {
  1323. return security_context_to_sid_core(scontext, scontext_len,
  1324. sid, SECSID_NULL, GFP_KERNEL, 1);
  1325. }
  1326. static int compute_sid_handle_invalid_context(
  1327. struct context *scontext,
  1328. struct context *tcontext,
  1329. u16 tclass,
  1330. struct context *newcontext)
  1331. {
  1332. char *s = NULL, *t = NULL, *n = NULL;
  1333. u32 slen, tlen, nlen;
  1334. if (context_struct_to_string(scontext, &s, &slen))
  1335. goto out;
  1336. if (context_struct_to_string(tcontext, &t, &tlen))
  1337. goto out;
  1338. if (context_struct_to_string(newcontext, &n, &nlen))
  1339. goto out;
  1340. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1341. "op=security_compute_sid invalid_context=%s"
  1342. " scontext=%s"
  1343. " tcontext=%s"
  1344. " tclass=%s",
  1345. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1346. out:
  1347. kfree(s);
  1348. kfree(t);
  1349. kfree(n);
  1350. if (!selinux_enforcing)
  1351. return 0;
  1352. return -EACCES;
  1353. }
  1354. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1355. u32 stype, u32 ttype, u16 tclass,
  1356. const char *objname)
  1357. {
  1358. struct filename_trans ft;
  1359. struct filename_trans_datum *otype;
  1360. /*
  1361. * Most filename trans rules are going to live in specific directories
  1362. * like /dev or /var/run. This bitmap will quickly skip rule searches
  1363. * if the ttype does not contain any rules.
  1364. */
  1365. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1366. return;
  1367. ft.stype = stype;
  1368. ft.ttype = ttype;
  1369. ft.tclass = tclass;
  1370. ft.name = objname;
  1371. otype = hashtab_search(p->filename_trans, &ft);
  1372. if (otype)
  1373. newcontext->type = otype->otype;
  1374. }
  1375. static int security_compute_sid(u32 ssid,
  1376. u32 tsid,
  1377. u16 orig_tclass,
  1378. u32 specified,
  1379. const char *objname,
  1380. u32 *out_sid,
  1381. bool kern)
  1382. {
  1383. struct class_datum *cladatum = NULL;
  1384. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1385. struct role_trans *roletr = NULL;
  1386. struct avtab_key avkey;
  1387. struct avtab_datum *avdatum;
  1388. struct avtab_node *node;
  1389. u16 tclass;
  1390. int rc = 0;
  1391. bool sock;
  1392. if (!ss_initialized) {
  1393. switch (orig_tclass) {
  1394. case SECCLASS_PROCESS: /* kernel value */
  1395. *out_sid = ssid;
  1396. break;
  1397. default:
  1398. *out_sid = tsid;
  1399. break;
  1400. }
  1401. goto out;
  1402. }
  1403. context_init(&newcontext);
  1404. read_lock(&policy_rwlock);
  1405. if (kern) {
  1406. tclass = unmap_class(orig_tclass);
  1407. sock = security_is_socket_class(orig_tclass);
  1408. } else {
  1409. tclass = orig_tclass;
  1410. sock = security_is_socket_class(map_class(tclass));
  1411. }
  1412. scontext = sidtab_search(&sidtab, ssid);
  1413. if (!scontext) {
  1414. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1415. __func__, ssid);
  1416. rc = -EINVAL;
  1417. goto out_unlock;
  1418. }
  1419. tcontext = sidtab_search(&sidtab, tsid);
  1420. if (!tcontext) {
  1421. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1422. __func__, tsid);
  1423. rc = -EINVAL;
  1424. goto out_unlock;
  1425. }
  1426. if (tclass && tclass <= policydb.p_classes.nprim)
  1427. cladatum = policydb.class_val_to_struct[tclass - 1];
  1428. /* Set the user identity. */
  1429. switch (specified) {
  1430. case AVTAB_TRANSITION:
  1431. case AVTAB_CHANGE:
  1432. if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
  1433. newcontext.user = tcontext->user;
  1434. } else {
  1435. /* notice this gets both DEFAULT_SOURCE and unset */
  1436. /* Use the process user identity. */
  1437. newcontext.user = scontext->user;
  1438. }
  1439. break;
  1440. case AVTAB_MEMBER:
  1441. /* Use the related object owner. */
  1442. newcontext.user = tcontext->user;
  1443. break;
  1444. }
  1445. /* Set the role to default values. */
  1446. if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
  1447. newcontext.role = scontext->role;
  1448. } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
  1449. newcontext.role = tcontext->role;
  1450. } else {
  1451. if ((tclass == policydb.process_class) || (sock == true))
  1452. newcontext.role = scontext->role;
  1453. else
  1454. newcontext.role = OBJECT_R_VAL;
  1455. }
  1456. /* Set the type to default values. */
  1457. if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
  1458. newcontext.type = scontext->type;
  1459. } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
  1460. newcontext.type = tcontext->type;
  1461. } else {
  1462. if ((tclass == policydb.process_class) || (sock == true)) {
  1463. /* Use the type of process. */
  1464. newcontext.type = scontext->type;
  1465. } else {
  1466. /* Use the type of the related object. */
  1467. newcontext.type = tcontext->type;
  1468. }
  1469. }
  1470. /* Look for a type transition/member/change rule. */
  1471. avkey.source_type = scontext->type;
  1472. avkey.target_type = tcontext->type;
  1473. avkey.target_class = tclass;
  1474. avkey.specified = specified;
  1475. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1476. /* If no permanent rule, also check for enabled conditional rules */
  1477. if (!avdatum) {
  1478. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1479. for (; node; node = avtab_search_node_next(node, specified)) {
  1480. if (node->key.specified & AVTAB_ENABLED) {
  1481. avdatum = &node->datum;
  1482. break;
  1483. }
  1484. }
  1485. }
  1486. if (avdatum) {
  1487. /* Use the type from the type transition/member/change rule. */
  1488. newcontext.type = avdatum->u.data;
  1489. }
  1490. /* if we have a objname this is a file trans check so check those rules */
  1491. if (objname)
  1492. filename_compute_type(&policydb, &newcontext, scontext->type,
  1493. tcontext->type, tclass, objname);
  1494. /* Check for class-specific changes. */
  1495. if (specified & AVTAB_TRANSITION) {
  1496. /* Look for a role transition rule. */
  1497. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1498. if ((roletr->role == scontext->role) &&
  1499. (roletr->type == tcontext->type) &&
  1500. (roletr->tclass == tclass)) {
  1501. /* Use the role transition rule. */
  1502. newcontext.role = roletr->new_role;
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. /* Set the MLS attributes.
  1508. This is done last because it may allocate memory. */
  1509. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1510. &newcontext, sock);
  1511. if (rc)
  1512. goto out_unlock;
  1513. /* Check the validity of the context. */
  1514. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1515. rc = compute_sid_handle_invalid_context(scontext,
  1516. tcontext,
  1517. tclass,
  1518. &newcontext);
  1519. if (rc)
  1520. goto out_unlock;
  1521. }
  1522. /* Obtain the sid for the context. */
  1523. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1524. out_unlock:
  1525. read_unlock(&policy_rwlock);
  1526. context_destroy(&newcontext);
  1527. out:
  1528. return rc;
  1529. }
  1530. /**
  1531. * security_transition_sid - Compute the SID for a new subject/object.
  1532. * @ssid: source security identifier
  1533. * @tsid: target security identifier
  1534. * @tclass: target security class
  1535. * @out_sid: security identifier for new subject/object
  1536. *
  1537. * Compute a SID to use for labeling a new subject or object in the
  1538. * class @tclass based on a SID pair (@ssid, @tsid).
  1539. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1540. * if insufficient memory is available, or %0 if the new SID was
  1541. * computed successfully.
  1542. */
  1543. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1544. const struct qstr *qstr, u32 *out_sid)
  1545. {
  1546. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1547. qstr ? qstr->name : NULL, out_sid, true);
  1548. }
  1549. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1550. const char *objname, u32 *out_sid)
  1551. {
  1552. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1553. objname, out_sid, false);
  1554. }
  1555. /**
  1556. * security_member_sid - Compute the SID for member selection.
  1557. * @ssid: source security identifier
  1558. * @tsid: target security identifier
  1559. * @tclass: target security class
  1560. * @out_sid: security identifier for selected member
  1561. *
  1562. * Compute a SID to use when selecting a member of a polyinstantiated
  1563. * object of class @tclass based on a SID pair (@ssid, @tsid).
  1564. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1565. * if insufficient memory is available, or %0 if the SID was
  1566. * computed successfully.
  1567. */
  1568. int security_member_sid(u32 ssid,
  1569. u32 tsid,
  1570. u16 tclass,
  1571. u32 *out_sid)
  1572. {
  1573. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1574. out_sid, false);
  1575. }
  1576. /**
  1577. * security_change_sid - Compute the SID for object relabeling.
  1578. * @ssid: source security identifier
  1579. * @tsid: target security identifier
  1580. * @tclass: target security class
  1581. * @out_sid: security identifier for selected member
  1582. *
  1583. * Compute a SID to use for relabeling an object of class @tclass
  1584. * based on a SID pair (@ssid, @tsid).
  1585. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1586. * if insufficient memory is available, or %0 if the SID was
  1587. * computed successfully.
  1588. */
  1589. int security_change_sid(u32 ssid,
  1590. u32 tsid,
  1591. u16 tclass,
  1592. u32 *out_sid)
  1593. {
  1594. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1595. out_sid, false);
  1596. }
  1597. /* Clone the SID into the new SID table. */
  1598. static int clone_sid(u32 sid,
  1599. struct context *context,
  1600. void *arg)
  1601. {
  1602. struct sidtab *s = arg;
  1603. if (sid > SECINITSID_NUM)
  1604. return sidtab_insert(s, sid, context);
  1605. else
  1606. return 0;
  1607. }
  1608. static inline int convert_context_handle_invalid_context(struct context *context)
  1609. {
  1610. char *s;
  1611. u32 len;
  1612. if (selinux_enforcing)
  1613. return -EINVAL;
  1614. if (!context_struct_to_string(context, &s, &len)) {
  1615. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1616. kfree(s);
  1617. }
  1618. return 0;
  1619. }
  1620. struct convert_context_args {
  1621. struct policydb *oldp;
  1622. struct policydb *newp;
  1623. };
  1624. /*
  1625. * Convert the values in the security context
  1626. * structure `c' from the values specified
  1627. * in the policy `p->oldp' to the values specified
  1628. * in the policy `p->newp'. Verify that the
  1629. * context is valid under the new policy.
  1630. */
  1631. static int convert_context(u32 key,
  1632. struct context *c,
  1633. void *p)
  1634. {
  1635. struct convert_context_args *args;
  1636. struct context oldc;
  1637. struct ocontext *oc;
  1638. struct mls_range *range;
  1639. struct role_datum *role;
  1640. struct type_datum *typdatum;
  1641. struct user_datum *usrdatum;
  1642. char *s;
  1643. u32 len;
  1644. int rc = 0;
  1645. if (key <= SECINITSID_NUM)
  1646. goto out;
  1647. args = p;
  1648. if (c->str) {
  1649. struct context ctx;
  1650. rc = -ENOMEM;
  1651. s = kstrdup(c->str, GFP_KERNEL);
  1652. if (!s)
  1653. goto out;
  1654. rc = string_to_context_struct(args->newp, NULL, s,
  1655. c->len, &ctx, SECSID_NULL);
  1656. kfree(s);
  1657. if (!rc) {
  1658. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1659. c->str);
  1660. /* Replace string with mapped representation. */
  1661. kfree(c->str);
  1662. memcpy(c, &ctx, sizeof(*c));
  1663. goto out;
  1664. } else if (rc == -EINVAL) {
  1665. /* Retain string representation for later mapping. */
  1666. rc = 0;
  1667. goto out;
  1668. } else {
  1669. /* Other error condition, e.g. ENOMEM. */
  1670. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1671. c->str, -rc);
  1672. goto out;
  1673. }
  1674. }
  1675. rc = context_cpy(&oldc, c);
  1676. if (rc)
  1677. goto out;
  1678. /* Convert the user. */
  1679. rc = -EINVAL;
  1680. usrdatum = hashtab_search(args->newp->p_users.table,
  1681. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1682. if (!usrdatum)
  1683. goto bad;
  1684. c->user = usrdatum->value;
  1685. /* Convert the role. */
  1686. rc = -EINVAL;
  1687. role = hashtab_search(args->newp->p_roles.table,
  1688. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1689. if (!role)
  1690. goto bad;
  1691. c->role = role->value;
  1692. /* Convert the type. */
  1693. rc = -EINVAL;
  1694. typdatum = hashtab_search(args->newp->p_types.table,
  1695. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1696. if (!typdatum)
  1697. goto bad;
  1698. c->type = typdatum->value;
  1699. /* Convert the MLS fields if dealing with MLS policies */
  1700. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1701. rc = mls_convert_context(args->oldp, args->newp, c);
  1702. if (rc)
  1703. goto bad;
  1704. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1705. /*
  1706. * Switching between MLS and non-MLS policy:
  1707. * free any storage used by the MLS fields in the
  1708. * context for all existing entries in the sidtab.
  1709. */
  1710. mls_context_destroy(c);
  1711. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1712. /*
  1713. * Switching between non-MLS and MLS policy:
  1714. * ensure that the MLS fields of the context for all
  1715. * existing entries in the sidtab are filled in with a
  1716. * suitable default value, likely taken from one of the
  1717. * initial SIDs.
  1718. */
  1719. oc = args->newp->ocontexts[OCON_ISID];
  1720. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1721. oc = oc->next;
  1722. rc = -EINVAL;
  1723. if (!oc) {
  1724. printk(KERN_ERR "SELinux: unable to look up"
  1725. " the initial SIDs list\n");
  1726. goto bad;
  1727. }
  1728. range = &oc->context[0].range;
  1729. rc = mls_range_set(c, range);
  1730. if (rc)
  1731. goto bad;
  1732. }
  1733. /* Check the validity of the new context. */
  1734. if (!policydb_context_isvalid(args->newp, c)) {
  1735. rc = convert_context_handle_invalid_context(&oldc);
  1736. if (rc)
  1737. goto bad;
  1738. }
  1739. context_destroy(&oldc);
  1740. rc = 0;
  1741. out:
  1742. return rc;
  1743. bad:
  1744. /* Map old representation to string and save it. */
  1745. rc = context_struct_to_string(&oldc, &s, &len);
  1746. if (rc)
  1747. return rc;
  1748. context_destroy(&oldc);
  1749. context_destroy(c);
  1750. c->str = s;
  1751. c->len = len;
  1752. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1753. c->str);
  1754. rc = 0;
  1755. goto out;
  1756. }
  1757. static void security_load_policycaps(void)
  1758. {
  1759. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1760. POLICYDB_CAPABILITY_NETPEER);
  1761. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1762. POLICYDB_CAPABILITY_OPENPERM);
  1763. selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps,
  1764. POLICYDB_CAPABILITY_ALWAYSNETWORK);
  1765. }
  1766. static int security_preserve_bools(struct policydb *p);
  1767. /**
  1768. * security_load_policy - Load a security policy configuration.
  1769. * @data: binary policy data
  1770. * @len: length of data in bytes
  1771. *
  1772. * Load a new set of security policy configuration data,
  1773. * validate it and convert the SID table as necessary.
  1774. * This function will flush the access vector cache after
  1775. * loading the new policy.
  1776. */
  1777. int security_load_policy(void *data, size_t len)
  1778. {
  1779. struct policydb *oldpolicydb, *newpolicydb;
  1780. struct sidtab oldsidtab, newsidtab;
  1781. struct selinux_mapping *oldmap, *map = NULL;
  1782. struct convert_context_args args;
  1783. u32 seqno;
  1784. u16 map_size;
  1785. int rc = 0;
  1786. struct policy_file file = { data, len }, *fp = &file;
  1787. oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL);
  1788. if (!oldpolicydb) {
  1789. rc = -ENOMEM;
  1790. goto out;
  1791. }
  1792. newpolicydb = oldpolicydb + 1;
  1793. if (!ss_initialized) {
  1794. avtab_cache_init();
  1795. rc = policydb_read(&policydb, fp);
  1796. if (rc) {
  1797. avtab_cache_destroy();
  1798. goto out;
  1799. }
  1800. policydb.len = len;
  1801. rc = selinux_set_mapping(&policydb, secclass_map,
  1802. &current_mapping,
  1803. &current_mapping_size);
  1804. if (rc) {
  1805. policydb_destroy(&policydb);
  1806. avtab_cache_destroy();
  1807. goto out;
  1808. }
  1809. rc = policydb_load_isids(&policydb, &sidtab);
  1810. if (rc) {
  1811. policydb_destroy(&policydb);
  1812. avtab_cache_destroy();
  1813. goto out;
  1814. }
  1815. security_load_policycaps();
  1816. ss_initialized = 1;
  1817. seqno = ++latest_granting;
  1818. selinux_complete_init();
  1819. avc_ss_reset(seqno);
  1820. selnl_notify_policyload(seqno);
  1821. selinux_status_update_policyload(seqno);
  1822. selinux_netlbl_cache_invalidate();
  1823. selinux_xfrm_notify_policyload();
  1824. goto out;
  1825. }
  1826. #if 0
  1827. sidtab_hash_eval(&sidtab, "sids");
  1828. #endif
  1829. rc = policydb_read(newpolicydb, fp);
  1830. if (rc)
  1831. goto out;
  1832. newpolicydb->len = len;
  1833. /* If switching between different policy types, log MLS status */
  1834. if (policydb.mls_enabled && !newpolicydb->mls_enabled)
  1835. printk(KERN_INFO "SELinux: Disabling MLS support...\n");
  1836. else if (!policydb.mls_enabled && newpolicydb->mls_enabled)
  1837. printk(KERN_INFO "SELinux: Enabling MLS support...\n");
  1838. rc = policydb_load_isids(newpolicydb, &newsidtab);
  1839. if (rc) {
  1840. printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
  1841. policydb_destroy(newpolicydb);
  1842. goto out;
  1843. }
  1844. rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size);
  1845. if (rc)
  1846. goto err;
  1847. rc = security_preserve_bools(newpolicydb);
  1848. if (rc) {
  1849. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1850. goto err;
  1851. }
  1852. /* Clone the SID table. */
  1853. sidtab_shutdown(&sidtab);
  1854. rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
  1855. if (rc)
  1856. goto err;
  1857. /*
  1858. * Convert the internal representations of contexts
  1859. * in the new SID table.
  1860. */
  1861. args.oldp = &policydb;
  1862. args.newp = newpolicydb;
  1863. rc = sidtab_map(&newsidtab, convert_context, &args);
  1864. if (rc) {
  1865. printk(KERN_ERR "SELinux: unable to convert the internal"
  1866. " representation of contexts in the new SID"
  1867. " table\n");
  1868. goto err;
  1869. }
  1870. /* Save the old policydb and SID table to free later. */
  1871. memcpy(oldpolicydb, &policydb, sizeof(policydb));
  1872. sidtab_set(&oldsidtab, &sidtab);
  1873. /* Install the new policydb and SID table. */
  1874. write_lock_irq(&policy_rwlock);
  1875. memcpy(&policydb, newpolicydb, sizeof(policydb));
  1876. sidtab_set(&sidtab, &newsidtab);
  1877. security_load_policycaps();
  1878. oldmap = current_mapping;
  1879. current_mapping = map;
  1880. current_mapping_size = map_size;
  1881. seqno = ++latest_granting;
  1882. write_unlock_irq(&policy_rwlock);
  1883. /* Free the old policydb and SID table. */
  1884. policydb_destroy(oldpolicydb);
  1885. sidtab_destroy(&oldsidtab);
  1886. kfree(oldmap);
  1887. avc_ss_reset(seqno);
  1888. selnl_notify_policyload(seqno);
  1889. selinux_status_update_policyload(seqno);
  1890. selinux_netlbl_cache_invalidate();
  1891. selinux_xfrm_notify_policyload();
  1892. rc = 0;
  1893. goto out;
  1894. err:
  1895. kfree(map);
  1896. sidtab_destroy(&newsidtab);
  1897. policydb_destroy(newpolicydb);
  1898. out:
  1899. kfree(oldpolicydb);
  1900. return rc;
  1901. }
  1902. size_t security_policydb_len(void)
  1903. {
  1904. size_t len;
  1905. read_lock(&policy_rwlock);
  1906. len = policydb.len;
  1907. read_unlock(&policy_rwlock);
  1908. return len;
  1909. }
  1910. /**
  1911. * security_port_sid - Obtain the SID for a port.
  1912. * @protocol: protocol number
  1913. * @port: port number
  1914. * @out_sid: security identifier
  1915. */
  1916. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1917. {
  1918. struct ocontext *c;
  1919. int rc = 0;
  1920. read_lock(&policy_rwlock);
  1921. c = policydb.ocontexts[OCON_PORT];
  1922. while (c) {
  1923. if (c->u.port.protocol == protocol &&
  1924. c->u.port.low_port <= port &&
  1925. c->u.port.high_port >= port)
  1926. break;
  1927. c = c->next;
  1928. }
  1929. if (c) {
  1930. if (!c->sid[0]) {
  1931. rc = sidtab_context_to_sid(&sidtab,
  1932. &c->context[0],
  1933. &c->sid[0]);
  1934. if (rc)
  1935. goto out;
  1936. }
  1937. *out_sid = c->sid[0];
  1938. } else {
  1939. *out_sid = SECINITSID_PORT;
  1940. }
  1941. out:
  1942. read_unlock(&policy_rwlock);
  1943. return rc;
  1944. }
  1945. /**
  1946. * security_netif_sid - Obtain the SID for a network interface.
  1947. * @name: interface name
  1948. * @if_sid: interface SID
  1949. */
  1950. int security_netif_sid(char *name, u32 *if_sid)
  1951. {
  1952. int rc = 0;
  1953. struct ocontext *c;
  1954. read_lock(&policy_rwlock);
  1955. c = policydb.ocontexts[OCON_NETIF];
  1956. while (c) {
  1957. if (strcmp(name, c->u.name) == 0)
  1958. break;
  1959. c = c->next;
  1960. }
  1961. if (c) {
  1962. if (!c->sid[0] || !c->sid[1]) {
  1963. rc = sidtab_context_to_sid(&sidtab,
  1964. &c->context[0],
  1965. &c->sid[0]);
  1966. if (rc)
  1967. goto out;
  1968. rc = sidtab_context_to_sid(&sidtab,
  1969. &c->context[1],
  1970. &c->sid[1]);
  1971. if (rc)
  1972. goto out;
  1973. }
  1974. *if_sid = c->sid[0];
  1975. } else
  1976. *if_sid = SECINITSID_NETIF;
  1977. out:
  1978. read_unlock(&policy_rwlock);
  1979. return rc;
  1980. }
  1981. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1982. {
  1983. int i, fail = 0;
  1984. for (i = 0; i < 4; i++)
  1985. if (addr[i] != (input[i] & mask[i])) {
  1986. fail = 1;
  1987. break;
  1988. }
  1989. return !fail;
  1990. }
  1991. /**
  1992. * security_node_sid - Obtain the SID for a node (host).
  1993. * @domain: communication domain aka address family
  1994. * @addrp: address
  1995. * @addrlen: address length in bytes
  1996. * @out_sid: security identifier
  1997. */
  1998. int security_node_sid(u16 domain,
  1999. void *addrp,
  2000. u32 addrlen,
  2001. u32 *out_sid)
  2002. {
  2003. int rc;
  2004. struct ocontext *c;
  2005. read_lock(&policy_rwlock);
  2006. switch (domain) {
  2007. case AF_INET: {
  2008. u32 addr;
  2009. rc = -EINVAL;
  2010. if (addrlen != sizeof(u32))
  2011. goto out;
  2012. addr = *((u32 *)addrp);
  2013. c = policydb.ocontexts[OCON_NODE];
  2014. while (c) {
  2015. if (c->u.node.addr == (addr & c->u.node.mask))
  2016. break;
  2017. c = c->next;
  2018. }
  2019. break;
  2020. }
  2021. case AF_INET6:
  2022. rc = -EINVAL;
  2023. if (addrlen != sizeof(u64) * 2)
  2024. goto out;
  2025. c = policydb.ocontexts[OCON_NODE6];
  2026. while (c) {
  2027. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  2028. c->u.node6.mask))
  2029. break;
  2030. c = c->next;
  2031. }
  2032. break;
  2033. default:
  2034. rc = 0;
  2035. *out_sid = SECINITSID_NODE;
  2036. goto out;
  2037. }
  2038. if (c) {
  2039. if (!c->sid[0]) {
  2040. rc = sidtab_context_to_sid(&sidtab,
  2041. &c->context[0],
  2042. &c->sid[0]);
  2043. if (rc)
  2044. goto out;
  2045. }
  2046. *out_sid = c->sid[0];
  2047. } else {
  2048. *out_sid = SECINITSID_NODE;
  2049. }
  2050. rc = 0;
  2051. out:
  2052. read_unlock(&policy_rwlock);
  2053. return rc;
  2054. }
  2055. #define SIDS_NEL 25
  2056. /**
  2057. * security_get_user_sids - Obtain reachable SIDs for a user.
  2058. * @fromsid: starting SID
  2059. * @username: username
  2060. * @sids: array of reachable SIDs for user
  2061. * @nel: number of elements in @sids
  2062. *
  2063. * Generate the set of SIDs for legal security contexts
  2064. * for a given user that can be reached by @fromsid.
  2065. * Set *@sids to point to a dynamically allocated
  2066. * array containing the set of SIDs. Set *@nel to the
  2067. * number of elements in the array.
  2068. */
  2069. int security_get_user_sids(u32 fromsid,
  2070. char *username,
  2071. u32 **sids,
  2072. u32 *nel)
  2073. {
  2074. struct context *fromcon, usercon;
  2075. u32 *mysids = NULL, *mysids2, sid;
  2076. u32 mynel = 0, maxnel = SIDS_NEL;
  2077. struct user_datum *user;
  2078. struct role_datum *role;
  2079. struct ebitmap_node *rnode, *tnode;
  2080. int rc = 0, i, j;
  2081. *sids = NULL;
  2082. *nel = 0;
  2083. if (!ss_initialized)
  2084. goto out;
  2085. read_lock(&policy_rwlock);
  2086. context_init(&usercon);
  2087. rc = -EINVAL;
  2088. fromcon = sidtab_search(&sidtab, fromsid);
  2089. if (!fromcon)
  2090. goto out_unlock;
  2091. rc = -EINVAL;
  2092. user = hashtab_search(policydb.p_users.table, username);
  2093. if (!user)
  2094. goto out_unlock;
  2095. usercon.user = user->value;
  2096. rc = -ENOMEM;
  2097. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  2098. if (!mysids)
  2099. goto out_unlock;
  2100. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  2101. role = policydb.role_val_to_struct[i];
  2102. usercon.role = i + 1;
  2103. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  2104. usercon.type = j + 1;
  2105. if (mls_setup_user_range(fromcon, user, &usercon))
  2106. continue;
  2107. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  2108. if (rc)
  2109. goto out_unlock;
  2110. if (mynel < maxnel) {
  2111. mysids[mynel++] = sid;
  2112. } else {
  2113. rc = -ENOMEM;
  2114. maxnel += SIDS_NEL;
  2115. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  2116. if (!mysids2)
  2117. goto out_unlock;
  2118. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  2119. kfree(mysids);
  2120. mysids = mysids2;
  2121. mysids[mynel++] = sid;
  2122. }
  2123. }
  2124. }
  2125. rc = 0;
  2126. out_unlock:
  2127. read_unlock(&policy_rwlock);
  2128. if (rc || !mynel) {
  2129. kfree(mysids);
  2130. goto out;
  2131. }
  2132. rc = -ENOMEM;
  2133. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  2134. if (!mysids2) {
  2135. kfree(mysids);
  2136. goto out;
  2137. }
  2138. for (i = 0, j = 0; i < mynel; i++) {
  2139. struct av_decision dummy_avd;
  2140. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  2141. SECCLASS_PROCESS, /* kernel value */
  2142. PROCESS__TRANSITION, AVC_STRICT,
  2143. &dummy_avd);
  2144. if (!rc)
  2145. mysids2[j++] = mysids[i];
  2146. cond_resched();
  2147. }
  2148. rc = 0;
  2149. kfree(mysids);
  2150. *sids = mysids2;
  2151. *nel = j;
  2152. out:
  2153. return rc;
  2154. }
  2155. /**
  2156. * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
  2157. * @fstype: filesystem type
  2158. * @path: path from root of mount
  2159. * @sclass: file security class
  2160. * @sid: SID for path
  2161. *
  2162. * Obtain a SID to use for a file in a filesystem that
  2163. * cannot support xattr or use a fixed labeling behavior like
  2164. * transition SIDs or task SIDs.
  2165. *
  2166. * The caller must acquire the policy_rwlock before calling this function.
  2167. */
  2168. static inline int __security_genfs_sid(const char *fstype,
  2169. char *path,
  2170. u16 orig_sclass,
  2171. u32 *sid)
  2172. {
  2173. int len;
  2174. u16 sclass;
  2175. struct genfs *genfs;
  2176. struct ocontext *c;
  2177. int rc, cmp = 0;
  2178. while (path[0] == '/' && path[1] == '/')
  2179. path++;
  2180. sclass = unmap_class(orig_sclass);
  2181. *sid = SECINITSID_UNLABELED;
  2182. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  2183. cmp = strcmp(fstype, genfs->fstype);
  2184. if (cmp <= 0)
  2185. break;
  2186. }
  2187. rc = -ENOENT;
  2188. if (!genfs || cmp)
  2189. goto out;
  2190. for (c = genfs->head; c; c = c->next) {
  2191. len = strlen(c->u.name);
  2192. if ((!c->v.sclass || sclass == c->v.sclass) &&
  2193. (strncmp(c->u.name, path, len) == 0))
  2194. break;
  2195. }
  2196. rc = -ENOENT;
  2197. if (!c)
  2198. goto out;
  2199. if (!c->sid[0]) {
  2200. rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
  2201. if (rc)
  2202. goto out;
  2203. }
  2204. *sid = c->sid[0];
  2205. rc = 0;
  2206. out:
  2207. return rc;
  2208. }
  2209. /**
  2210. * security_genfs_sid - Obtain a SID for a file in a filesystem
  2211. * @fstype: filesystem type
  2212. * @path: path from root of mount
  2213. * @sclass: file security class
  2214. * @sid: SID for path
  2215. *
  2216. * Acquire policy_rwlock before calling __security_genfs_sid() and release
  2217. * it afterward.
  2218. */
  2219. int security_genfs_sid(const char *fstype,
  2220. char *path,
  2221. u16 orig_sclass,
  2222. u32 *sid)
  2223. {
  2224. int retval;
  2225. read_lock(&policy_rwlock);
  2226. retval = __security_genfs_sid(fstype, path, orig_sclass, sid);
  2227. read_unlock(&policy_rwlock);
  2228. return retval;
  2229. }
  2230. /**
  2231. * security_fs_use - Determine how to handle labeling for a filesystem.
  2232. * @sb: superblock in question
  2233. */
  2234. int security_fs_use(struct super_block *sb)
  2235. {
  2236. int rc = 0;
  2237. struct ocontext *c;
  2238. struct superblock_security_struct *sbsec = sb->s_security;
  2239. const char *fstype = sb->s_type->name;
  2240. read_lock(&policy_rwlock);
  2241. c = policydb.ocontexts[OCON_FSUSE];
  2242. while (c) {
  2243. if (strcmp(fstype, c->u.name) == 0)
  2244. break;
  2245. c = c->next;
  2246. }
  2247. if (c) {
  2248. sbsec->behavior = c->v.behavior;
  2249. if (!c->sid[0]) {
  2250. rc = sidtab_context_to_sid(&sidtab, &c->context[0],
  2251. &c->sid[0]);
  2252. if (rc)
  2253. goto out;
  2254. }
  2255. sbsec->sid = c->sid[0];
  2256. } else {
  2257. rc = __security_genfs_sid(fstype, "/", SECCLASS_DIR,
  2258. &sbsec->sid);
  2259. if (rc) {
  2260. sbsec->behavior = SECURITY_FS_USE_NONE;
  2261. rc = 0;
  2262. } else {
  2263. sbsec->behavior = SECURITY_FS_USE_GENFS;
  2264. }
  2265. }
  2266. out:
  2267. read_unlock(&policy_rwlock);
  2268. return rc;
  2269. }
  2270. int security_get_bools(int *len, char ***names, int **values)
  2271. {
  2272. int i, rc;
  2273. read_lock(&policy_rwlock);
  2274. *names = NULL;
  2275. *values = NULL;
  2276. rc = 0;
  2277. *len = policydb.p_bools.nprim;
  2278. if (!*len)
  2279. goto out;
  2280. rc = -ENOMEM;
  2281. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  2282. if (!*names)
  2283. goto err;
  2284. rc = -ENOMEM;
  2285. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  2286. if (!*values)
  2287. goto err;
  2288. for (i = 0; i < *len; i++) {
  2289. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  2290. rc = -ENOMEM;
  2291. (*names)[i] = kstrdup(sym_name(&policydb, SYM_BOOLS, i), GFP_ATOMIC);
  2292. if (!(*names)[i])
  2293. goto err;
  2294. }
  2295. rc = 0;
  2296. out:
  2297. read_unlock(&policy_rwlock);
  2298. return rc;
  2299. err:
  2300. if (*names) {
  2301. for (i = 0; i < *len; i++)
  2302. kfree((*names)[i]);
  2303. }
  2304. kfree(*values);
  2305. goto out;
  2306. }
  2307. int security_set_bools(int len, int *values)
  2308. {
  2309. int i, rc;
  2310. int lenp, seqno = 0;
  2311. struct cond_node *cur;
  2312. write_lock_irq(&policy_rwlock);
  2313. rc = -EFAULT;
  2314. lenp = policydb.p_bools.nprim;
  2315. if (len != lenp)
  2316. goto out;
  2317. for (i = 0; i < len; i++) {
  2318. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  2319. audit_log(current->audit_context, GFP_ATOMIC,
  2320. AUDIT_MAC_CONFIG_CHANGE,
  2321. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  2322. sym_name(&policydb, SYM_BOOLS, i),
  2323. !!values[i],
  2324. policydb.bool_val_to_struct[i]->state,
  2325. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  2326. audit_get_sessionid(current));
  2327. }
  2328. if (values[i])
  2329. policydb.bool_val_to_struct[i]->state = 1;
  2330. else
  2331. policydb.bool_val_to_struct[i]->state = 0;
  2332. }
  2333. for (cur = policydb.cond_list; cur; cur = cur->next) {
  2334. rc = evaluate_cond_node(&policydb, cur);
  2335. if (rc)
  2336. goto out;
  2337. }
  2338. seqno = ++latest_granting;
  2339. rc = 0;
  2340. out:
  2341. write_unlock_irq(&policy_rwlock);
  2342. if (!rc) {
  2343. avc_ss_reset(seqno);
  2344. selnl_notify_policyload(seqno);
  2345. selinux_status_update_policyload(seqno);
  2346. selinux_xfrm_notify_policyload();
  2347. }
  2348. return rc;
  2349. }
  2350. int security_get_bool_value(int index)
  2351. {
  2352. int rc;
  2353. int len;
  2354. read_lock(&policy_rwlock);
  2355. rc = -EFAULT;
  2356. len = policydb.p_bools.nprim;
  2357. if (index >= len)
  2358. goto out;
  2359. rc = policydb.bool_val_to_struct[index]->state;
  2360. out:
  2361. read_unlock(&policy_rwlock);
  2362. return rc;
  2363. }
  2364. static int security_preserve_bools(struct policydb *p)
  2365. {
  2366. int rc, nbools = 0, *bvalues = NULL, i;
  2367. char **bnames = NULL;
  2368. struct cond_bool_datum *booldatum;
  2369. struct cond_node *cur;
  2370. rc = security_get_bools(&nbools, &bnames, &bvalues);
  2371. if (rc)
  2372. goto out;
  2373. for (i = 0; i < nbools; i++) {
  2374. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  2375. if (booldatum)
  2376. booldatum->state = bvalues[i];
  2377. }
  2378. for (cur = p->cond_list; cur; cur = cur->next) {
  2379. rc = evaluate_cond_node(p, cur);
  2380. if (rc)
  2381. goto out;
  2382. }
  2383. out:
  2384. if (bnames) {
  2385. for (i = 0; i < nbools; i++)
  2386. kfree(bnames[i]);
  2387. }
  2388. kfree(bnames);
  2389. kfree(bvalues);
  2390. return rc;
  2391. }
  2392. /*
  2393. * security_sid_mls_copy() - computes a new sid based on the given
  2394. * sid and the mls portion of mls_sid.
  2395. */
  2396. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  2397. {
  2398. struct context *context1;
  2399. struct context *context2;
  2400. struct context newcon;
  2401. char *s;
  2402. u32 len;
  2403. int rc;
  2404. rc = 0;
  2405. if (!ss_initialized || !policydb.mls_enabled) {
  2406. *new_sid = sid;
  2407. goto out;
  2408. }
  2409. context_init(&newcon);
  2410. read_lock(&policy_rwlock);
  2411. rc = -EINVAL;
  2412. context1 = sidtab_search(&sidtab, sid);
  2413. if (!context1) {
  2414. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2415. __func__, sid);
  2416. goto out_unlock;
  2417. }
  2418. rc = -EINVAL;
  2419. context2 = sidtab_search(&sidtab, mls_sid);
  2420. if (!context2) {
  2421. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2422. __func__, mls_sid);
  2423. goto out_unlock;
  2424. }
  2425. newcon.user = context1->user;
  2426. newcon.role = context1->role;
  2427. newcon.type = context1->type;
  2428. rc = mls_context_cpy(&newcon, context2);
  2429. if (rc)
  2430. goto out_unlock;
  2431. /* Check the validity of the new context. */
  2432. if (!policydb_context_isvalid(&policydb, &newcon)) {
  2433. rc = convert_context_handle_invalid_context(&newcon);
  2434. if (rc) {
  2435. if (!context_struct_to_string(&newcon, &s, &len)) {
  2436. audit_log(current->audit_context,
  2437. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2438. "op=security_sid_mls_copy "
  2439. "invalid_context=%s", s);
  2440. kfree(s);
  2441. }
  2442. goto out_unlock;
  2443. }
  2444. }
  2445. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  2446. out_unlock:
  2447. read_unlock(&policy_rwlock);
  2448. context_destroy(&newcon);
  2449. out:
  2450. return rc;
  2451. }
  2452. /**
  2453. * security_net_peersid_resolve - Compare and resolve two network peer SIDs
  2454. * @nlbl_sid: NetLabel SID
  2455. * @nlbl_type: NetLabel labeling protocol type
  2456. * @xfrm_sid: XFRM SID
  2457. *
  2458. * Description:
  2459. * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
  2460. * resolved into a single SID it is returned via @peer_sid and the function
  2461. * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
  2462. * returns a negative value. A table summarizing the behavior is below:
  2463. *
  2464. * | function return | @sid
  2465. * ------------------------------+-----------------+-----------------
  2466. * no peer labels | 0 | SECSID_NULL
  2467. * single peer label | 0 | <peer_label>
  2468. * multiple, consistent labels | 0 | <peer_label>
  2469. * multiple, inconsistent labels | -<errno> | SECSID_NULL
  2470. *
  2471. */
  2472. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  2473. u32 xfrm_sid,
  2474. u32 *peer_sid)
  2475. {
  2476. int rc;
  2477. struct context *nlbl_ctx;
  2478. struct context *xfrm_ctx;
  2479. *peer_sid = SECSID_NULL;
  2480. /* handle the common (which also happens to be the set of easy) cases
  2481. * right away, these two if statements catch everything involving a
  2482. * single or absent peer SID/label */
  2483. if (xfrm_sid == SECSID_NULL) {
  2484. *peer_sid = nlbl_sid;
  2485. return 0;
  2486. }
  2487. /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
  2488. * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
  2489. * is present */
  2490. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  2491. *peer_sid = xfrm_sid;
  2492. return 0;
  2493. }
  2494. /* we don't need to check ss_initialized here since the only way both
  2495. * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
  2496. * security server was initialized and ss_initialized was true */
  2497. if (!policydb.mls_enabled)
  2498. return 0;
  2499. read_lock(&policy_rwlock);
  2500. rc = -EINVAL;
  2501. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  2502. if (!nlbl_ctx) {
  2503. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2504. __func__, nlbl_sid);
  2505. goto out;
  2506. }
  2507. rc = -EINVAL;
  2508. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  2509. if (!xfrm_ctx) {
  2510. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2511. __func__, xfrm_sid);
  2512. goto out;
  2513. }
  2514. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  2515. if (rc)
  2516. goto out;
  2517. /* at present NetLabel SIDs/labels really only carry MLS
  2518. * information so if the MLS portion of the NetLabel SID
  2519. * matches the MLS portion of the labeled XFRM SID/label
  2520. * then pass along the XFRM SID as it is the most
  2521. * expressive */
  2522. *peer_sid = xfrm_sid;
  2523. out:
  2524. read_unlock(&policy_rwlock);
  2525. return rc;
  2526. }
  2527. static int get_classes_callback(void *k, void *d, void *args)
  2528. {
  2529. struct class_datum *datum = d;
  2530. char *name = k, **classes = args;
  2531. int value = datum->value - 1;
  2532. classes[value] = kstrdup(name, GFP_ATOMIC);
  2533. if (!classes[value])
  2534. return -ENOMEM;
  2535. return 0;
  2536. }
  2537. int security_get_classes(char ***classes, int *nclasses)
  2538. {
  2539. int rc;
  2540. read_lock(&policy_rwlock);
  2541. rc = -ENOMEM;
  2542. *nclasses = policydb.p_classes.nprim;
  2543. *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
  2544. if (!*classes)
  2545. goto out;
  2546. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  2547. *classes);
  2548. if (rc) {
  2549. int i;
  2550. for (i = 0; i < *nclasses; i++)
  2551. kfree((*classes)[i]);
  2552. kfree(*classes);
  2553. }
  2554. out:
  2555. read_unlock(&policy_rwlock);
  2556. return rc;
  2557. }
  2558. static int get_permissions_callback(void *k, void *d, void *args)
  2559. {
  2560. struct perm_datum *datum = d;
  2561. char *name = k, **perms = args;
  2562. int value = datum->value - 1;
  2563. perms[value] = kstrdup(name, GFP_ATOMIC);
  2564. if (!perms[value])
  2565. return -ENOMEM;
  2566. return 0;
  2567. }
  2568. int security_get_permissions(char *class, char ***perms, int *nperms)
  2569. {
  2570. int rc, i;
  2571. struct class_datum *match;
  2572. read_lock(&policy_rwlock);
  2573. rc = -EINVAL;
  2574. match = hashtab_search(policydb.p_classes.table, class);
  2575. if (!match) {
  2576. printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
  2577. __func__, class);
  2578. goto out;
  2579. }
  2580. rc = -ENOMEM;
  2581. *nperms = match->permissions.nprim;
  2582. *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
  2583. if (!*perms)
  2584. goto out;
  2585. if (match->comdatum) {
  2586. rc = hashtab_map(match->comdatum->permissions.table,
  2587. get_permissions_callback, *perms);
  2588. if (rc)
  2589. goto err;
  2590. }
  2591. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  2592. *perms);
  2593. if (rc)
  2594. goto err;
  2595. out:
  2596. read_unlock(&policy_rwlock);
  2597. return rc;
  2598. err:
  2599. read_unlock(&policy_rwlock);
  2600. for (i = 0; i < *nperms; i++)
  2601. kfree((*perms)[i]);
  2602. kfree(*perms);
  2603. return rc;
  2604. }
  2605. int security_get_reject_unknown(void)
  2606. {
  2607. return policydb.reject_unknown;
  2608. }
  2609. int security_get_allow_unknown(void)
  2610. {
  2611. return policydb.allow_unknown;
  2612. }
  2613. /**
  2614. * security_policycap_supported - Check for a specific policy capability
  2615. * @req_cap: capability
  2616. *
  2617. * Description:
  2618. * This function queries the currently loaded policy to see if it supports the
  2619. * capability specified by @req_cap. Returns true (1) if the capability is
  2620. * supported, false (0) if it isn't supported.
  2621. *
  2622. */
  2623. int security_policycap_supported(unsigned int req_cap)
  2624. {
  2625. int rc;
  2626. read_lock(&policy_rwlock);
  2627. rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
  2628. read_unlock(&policy_rwlock);
  2629. return rc;
  2630. }
  2631. struct selinux_audit_rule {
  2632. u32 au_seqno;
  2633. struct context au_ctxt;
  2634. };
  2635. void selinux_audit_rule_free(void *vrule)
  2636. {
  2637. struct selinux_audit_rule *rule = vrule;
  2638. if (rule) {
  2639. context_destroy(&rule->au_ctxt);
  2640. kfree(rule);
  2641. }
  2642. }
  2643. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2644. {
  2645. struct selinux_audit_rule *tmprule;
  2646. struct role_datum *roledatum;
  2647. struct type_datum *typedatum;
  2648. struct user_datum *userdatum;
  2649. struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
  2650. int rc = 0;
  2651. *rule = NULL;
  2652. if (!ss_initialized)
  2653. return -EOPNOTSUPP;
  2654. switch (field) {
  2655. case AUDIT_SUBJ_USER:
  2656. case AUDIT_SUBJ_ROLE:
  2657. case AUDIT_SUBJ_TYPE:
  2658. case AUDIT_OBJ_USER:
  2659. case AUDIT_OBJ_ROLE:
  2660. case AUDIT_OBJ_TYPE:
  2661. /* only 'equals' and 'not equals' fit user, role, and type */
  2662. if (op != Audit_equal && op != Audit_not_equal)
  2663. return -EINVAL;
  2664. break;
  2665. case AUDIT_SUBJ_SEN:
  2666. case AUDIT_SUBJ_CLR:
  2667. case AUDIT_OBJ_LEV_LOW:
  2668. case AUDIT_OBJ_LEV_HIGH:
  2669. /* we do not allow a range, indicated by the presence of '-' */
  2670. if (strchr(rulestr, '-'))
  2671. return -EINVAL;
  2672. break;
  2673. default:
  2674. /* only the above fields are valid */
  2675. return -EINVAL;
  2676. }
  2677. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  2678. if (!tmprule)
  2679. return -ENOMEM;
  2680. context_init(&tmprule->au_ctxt);
  2681. read_lock(&policy_rwlock);
  2682. tmprule->au_seqno = latest_granting;
  2683. switch (field) {
  2684. case AUDIT_SUBJ_USER:
  2685. case AUDIT_OBJ_USER:
  2686. rc = -EINVAL;
  2687. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  2688. if (!userdatum)
  2689. goto out;
  2690. tmprule->au_ctxt.user = userdatum->value;
  2691. break;
  2692. case AUDIT_SUBJ_ROLE:
  2693. case AUDIT_OBJ_ROLE:
  2694. rc = -EINVAL;
  2695. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  2696. if (!roledatum)
  2697. goto out;
  2698. tmprule->au_ctxt.role = roledatum->value;
  2699. break;
  2700. case AUDIT_SUBJ_TYPE:
  2701. case AUDIT_OBJ_TYPE:
  2702. rc = -EINVAL;
  2703. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  2704. if (!typedatum)
  2705. goto out;
  2706. tmprule->au_ctxt.type = typedatum->value;
  2707. break;
  2708. case AUDIT_SUBJ_SEN:
  2709. case AUDIT_SUBJ_CLR:
  2710. case AUDIT_OBJ_LEV_LOW:
  2711. case AUDIT_OBJ_LEV_HIGH:
  2712. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  2713. if (rc)
  2714. goto out;
  2715. break;
  2716. }
  2717. rc = 0;
  2718. out:
  2719. read_unlock(&policy_rwlock);
  2720. if (rc) {
  2721. selinux_audit_rule_free(tmprule);
  2722. tmprule = NULL;
  2723. }
  2724. *rule = tmprule;
  2725. return rc;
  2726. }
  2727. /* Check to see if the rule contains any selinux fields */
  2728. int selinux_audit_rule_known(struct audit_krule *rule)
  2729. {
  2730. int i;
  2731. for (i = 0; i < rule->field_count; i++) {
  2732. struct audit_field *f = &rule->fields[i];
  2733. switch (f->type) {
  2734. case AUDIT_SUBJ_USER:
  2735. case AUDIT_SUBJ_ROLE:
  2736. case AUDIT_SUBJ_TYPE:
  2737. case AUDIT_SUBJ_SEN:
  2738. case AUDIT_SUBJ_CLR:
  2739. case AUDIT_OBJ_USER:
  2740. case AUDIT_OBJ_ROLE:
  2741. case AUDIT_OBJ_TYPE:
  2742. case AUDIT_OBJ_LEV_LOW:
  2743. case AUDIT_OBJ_LEV_HIGH:
  2744. return 1;
  2745. }
  2746. }
  2747. return 0;
  2748. }
  2749. int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  2750. struct audit_context *actx)
  2751. {
  2752. struct context *ctxt;
  2753. struct mls_level *level;
  2754. struct selinux_audit_rule *rule = vrule;
  2755. int match = 0;
  2756. if (unlikely(!rule)) {
  2757. WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
  2758. return -ENOENT;
  2759. }
  2760. read_lock(&policy_rwlock);
  2761. if (rule->au_seqno < latest_granting) {
  2762. match = -ESTALE;
  2763. goto out;
  2764. }
  2765. ctxt = sidtab_search(&sidtab, sid);
  2766. if (unlikely(!ctxt)) {
  2767. WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
  2768. sid);
  2769. match = -ENOENT;
  2770. goto out;
  2771. }
  2772. /* a field/op pair that is not caught here will simply fall through
  2773. without a match */
  2774. switch (field) {
  2775. case AUDIT_SUBJ_USER:
  2776. case AUDIT_OBJ_USER:
  2777. switch (op) {
  2778. case Audit_equal:
  2779. match = (ctxt->user == rule->au_ctxt.user);
  2780. break;
  2781. case Audit_not_equal:
  2782. match = (ctxt->user != rule->au_ctxt.user);
  2783. break;
  2784. }
  2785. break;
  2786. case AUDIT_SUBJ_ROLE:
  2787. case AUDIT_OBJ_ROLE:
  2788. switch (op) {
  2789. case Audit_equal:
  2790. match = (ctxt->role == rule->au_ctxt.role);
  2791. break;
  2792. case Audit_not_equal:
  2793. match = (ctxt->role != rule->au_ctxt.role);
  2794. break;
  2795. }
  2796. break;
  2797. case AUDIT_SUBJ_TYPE:
  2798. case AUDIT_OBJ_TYPE:
  2799. switch (op) {
  2800. case Audit_equal:
  2801. match = (ctxt->type == rule->au_ctxt.type);
  2802. break;
  2803. case Audit_not_equal:
  2804. match = (ctxt->type != rule->au_ctxt.type);
  2805. break;
  2806. }
  2807. break;
  2808. case AUDIT_SUBJ_SEN:
  2809. case AUDIT_SUBJ_CLR:
  2810. case AUDIT_OBJ_LEV_LOW:
  2811. case AUDIT_OBJ_LEV_HIGH:
  2812. level = ((field == AUDIT_SUBJ_SEN ||
  2813. field == AUDIT_OBJ_LEV_LOW) ?
  2814. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2815. switch (op) {
  2816. case Audit_equal:
  2817. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2818. level);
  2819. break;
  2820. case Audit_not_equal:
  2821. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2822. level);
  2823. break;
  2824. case Audit_lt:
  2825. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2826. level) &&
  2827. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2828. level));
  2829. break;
  2830. case Audit_le:
  2831. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2832. level);
  2833. break;
  2834. case Audit_gt:
  2835. match = (mls_level_dom(level,
  2836. &rule->au_ctxt.range.level[0]) &&
  2837. !mls_level_eq(level,
  2838. &rule->au_ctxt.range.level[0]));
  2839. break;
  2840. case Audit_ge:
  2841. match = mls_level_dom(level,
  2842. &rule->au_ctxt.range.level[0]);
  2843. break;
  2844. }
  2845. }
  2846. out:
  2847. read_unlock(&policy_rwlock);
  2848. return match;
  2849. }
  2850. static int (*aurule_callback)(void) = audit_update_lsm_rules;
  2851. static int aurule_avc_callback(u32 event)
  2852. {
  2853. int err = 0;
  2854. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2855. err = aurule_callback();
  2856. return err;
  2857. }
  2858. static int __init aurule_init(void)
  2859. {
  2860. int err;
  2861. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
  2862. if (err)
  2863. panic("avc_add_callback() failed, error %d\n", err);
  2864. return err;
  2865. }
  2866. __initcall(aurule_init);
  2867. #ifdef CONFIG_NETLABEL
  2868. /**
  2869. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2870. * @secattr: the NetLabel packet security attributes
  2871. * @sid: the SELinux SID
  2872. *
  2873. * Description:
  2874. * Attempt to cache the context in @ctx, which was derived from the packet in
  2875. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2876. * already been initialized.
  2877. *
  2878. */
  2879. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2880. u32 sid)
  2881. {
  2882. u32 *sid_cache;
  2883. sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
  2884. if (sid_cache == NULL)
  2885. return;
  2886. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2887. if (secattr->cache == NULL) {
  2888. kfree(sid_cache);
  2889. return;
  2890. }
  2891. *sid_cache = sid;
  2892. secattr->cache->free = kfree;
  2893. secattr->cache->data = sid_cache;
  2894. secattr->flags |= NETLBL_SECATTR_CACHE;
  2895. }
  2896. /**
  2897. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2898. * @secattr: the NetLabel packet security attributes
  2899. * @sid: the SELinux SID
  2900. *
  2901. * Description:
  2902. * Convert the given NetLabel security attributes in @secattr into a
  2903. * SELinux SID. If the @secattr field does not contain a full SELinux
  2904. * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
  2905. * 'cache' field of @secattr is set and the CACHE flag is set; this is to
  2906. * allow the @secattr to be used by NetLabel to cache the secattr to SID
  2907. * conversion for future lookups. Returns zero on success, negative values on
  2908. * failure.
  2909. *
  2910. */
  2911. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2912. u32 *sid)
  2913. {
  2914. int rc;
  2915. struct context *ctx;
  2916. struct context ctx_new;
  2917. if (!ss_initialized) {
  2918. *sid = SECSID_NULL;
  2919. return 0;
  2920. }
  2921. read_lock(&policy_rwlock);
  2922. if (secattr->flags & NETLBL_SECATTR_CACHE)
  2923. *sid = *(u32 *)secattr->cache->data;
  2924. else if (secattr->flags & NETLBL_SECATTR_SECID)
  2925. *sid = secattr->attr.secid;
  2926. else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2927. rc = -EIDRM;
  2928. ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
  2929. if (ctx == NULL)
  2930. goto out;
  2931. context_init(&ctx_new);
  2932. ctx_new.user = ctx->user;
  2933. ctx_new.role = ctx->role;
  2934. ctx_new.type = ctx->type;
  2935. mls_import_netlbl_lvl(&ctx_new, secattr);
  2936. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2937. rc = mls_import_netlbl_cat(&ctx_new, secattr);
  2938. if (rc)
  2939. goto out;
  2940. }
  2941. rc = -EIDRM;
  2942. if (!mls_context_isvalid(&policydb, &ctx_new))
  2943. goto out_free;
  2944. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2945. if (rc)
  2946. goto out_free;
  2947. security_netlbl_cache_add(secattr, *sid);
  2948. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2949. } else
  2950. *sid = SECSID_NULL;
  2951. read_unlock(&policy_rwlock);
  2952. return 0;
  2953. out_free:
  2954. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2955. out:
  2956. read_unlock(&policy_rwlock);
  2957. return rc;
  2958. }
  2959. /**
  2960. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2961. * @sid: the SELinux SID
  2962. * @secattr: the NetLabel packet security attributes
  2963. *
  2964. * Description:
  2965. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2966. * Returns zero on success, negative values on failure.
  2967. *
  2968. */
  2969. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2970. {
  2971. int rc;
  2972. struct context *ctx;
  2973. if (!ss_initialized)
  2974. return 0;
  2975. read_lock(&policy_rwlock);
  2976. rc = -ENOENT;
  2977. ctx = sidtab_search(&sidtab, sid);
  2978. if (ctx == NULL)
  2979. goto out;
  2980. rc = -ENOMEM;
  2981. secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
  2982. GFP_ATOMIC);
  2983. if (secattr->domain == NULL)
  2984. goto out;
  2985. secattr->attr.secid = sid;
  2986. secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
  2987. mls_export_netlbl_lvl(ctx, secattr);
  2988. rc = mls_export_netlbl_cat(ctx, secattr);
  2989. out:
  2990. read_unlock(&policy_rwlock);
  2991. return rc;
  2992. }
  2993. #endif /* CONFIG_NETLABEL */
  2994. /**
  2995. * security_read_policy - read the policy.
  2996. * @data: binary policy data
  2997. * @len: length of data in bytes
  2998. *
  2999. */
  3000. int security_read_policy(void **data, size_t *len)
  3001. {
  3002. int rc;
  3003. struct policy_file fp;
  3004. if (!ss_initialized)
  3005. return -EINVAL;
  3006. *len = security_policydb_len();
  3007. *data = vmalloc_user(*len);
  3008. if (!*data)
  3009. return -ENOMEM;
  3010. fp.data = *data;
  3011. fp.len = *len;
  3012. read_lock(&policy_rwlock);
  3013. rc = policydb_write(&policydb, &fp);
  3014. read_unlock(&policy_rwlock);
  3015. if (rc)
  3016. return rc;
  3017. *len = (unsigned long)fp.data - (unsigned long)*data;
  3018. return 0;
  3019. }