services.c 75 KB

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