services.c 95 KB

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