services.c 81 KB

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