services.c 88 KB

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