services.c 81 KB

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