services.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226
  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. *scontext = kstrdup(context->str, GFP_ATOMIC);
  904. if (!(*scontext))
  905. return -ENOMEM;
  906. return 0;
  907. }
  908. /* Compute the size of the context. */
  909. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  910. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  911. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  912. *scontext_len += mls_compute_context_len(context);
  913. if (!scontext)
  914. return 0;
  915. /* Allocate space for the context; caller must free this space. */
  916. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  917. if (!scontextp)
  918. return -ENOMEM;
  919. *scontext = scontextp;
  920. /*
  921. * Copy the user name, role name and type name into the context.
  922. */
  923. sprintf(scontextp, "%s:%s:%s",
  924. sym_name(&policydb, SYM_USERS, context->user - 1),
  925. sym_name(&policydb, SYM_ROLES, context->role - 1),
  926. sym_name(&policydb, SYM_TYPES, context->type - 1));
  927. scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) +
  928. 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) +
  929. 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1));
  930. mls_sid_to_context(context, &scontextp);
  931. *scontextp = 0;
  932. return 0;
  933. }
  934. #include "initial_sid_to_string.h"
  935. const char *security_get_initial_sid_context(u32 sid)
  936. {
  937. if (unlikely(sid > SECINITSID_NUM))
  938. return NULL;
  939. return initial_sid_to_string[sid];
  940. }
  941. static int security_sid_to_context_core(u32 sid, char **scontext,
  942. u32 *scontext_len, int force)
  943. {
  944. struct context *context;
  945. int rc = 0;
  946. if (scontext)
  947. *scontext = NULL;
  948. *scontext_len = 0;
  949. if (!ss_initialized) {
  950. if (sid <= SECINITSID_NUM) {
  951. char *scontextp;
  952. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  953. if (!scontext)
  954. goto out;
  955. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  956. if (!scontextp) {
  957. rc = -ENOMEM;
  958. goto out;
  959. }
  960. strcpy(scontextp, initial_sid_to_string[sid]);
  961. *scontext = scontextp;
  962. goto out;
  963. }
  964. printk(KERN_ERR "SELinux: %s: called before initial "
  965. "load_policy on unknown SID %d\n", __func__, sid);
  966. rc = -EINVAL;
  967. goto out;
  968. }
  969. read_lock(&policy_rwlock);
  970. if (force)
  971. context = sidtab_search_force(&sidtab, sid);
  972. else
  973. context = sidtab_search(&sidtab, sid);
  974. if (!context) {
  975. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  976. __func__, sid);
  977. rc = -EINVAL;
  978. goto out_unlock;
  979. }
  980. rc = context_struct_to_string(context, scontext, scontext_len);
  981. out_unlock:
  982. read_unlock(&policy_rwlock);
  983. out:
  984. return rc;
  985. }
  986. /**
  987. * security_sid_to_context - Obtain a context for a given SID.
  988. * @sid: security identifier, SID
  989. * @scontext: security context
  990. * @scontext_len: length in bytes
  991. *
  992. * Write the string representation of the context associated with @sid
  993. * into a dynamically allocated string of the correct size. Set @scontext
  994. * to point to this string and set @scontext_len to the length of the string.
  995. */
  996. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  997. {
  998. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  999. }
  1000. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  1001. {
  1002. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  1003. }
  1004. /*
  1005. * Caveat: Mutates scontext.
  1006. */
  1007. static int string_to_context_struct(struct policydb *pol,
  1008. struct sidtab *sidtabp,
  1009. char *scontext,
  1010. u32 scontext_len,
  1011. struct context *ctx,
  1012. u32 def_sid)
  1013. {
  1014. struct role_datum *role;
  1015. struct type_datum *typdatum;
  1016. struct user_datum *usrdatum;
  1017. char *scontextp, *p, oldc;
  1018. int rc = 0;
  1019. context_init(ctx);
  1020. /* Parse the security context. */
  1021. rc = -EINVAL;
  1022. scontextp = (char *) scontext;
  1023. /* Extract the user. */
  1024. p = scontextp;
  1025. while (*p && *p != ':')
  1026. p++;
  1027. if (*p == 0)
  1028. goto out;
  1029. *p++ = 0;
  1030. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  1031. if (!usrdatum)
  1032. goto out;
  1033. ctx->user = usrdatum->value;
  1034. /* Extract role. */
  1035. scontextp = p;
  1036. while (*p && *p != ':')
  1037. p++;
  1038. if (*p == 0)
  1039. goto out;
  1040. *p++ = 0;
  1041. role = hashtab_search(pol->p_roles.table, scontextp);
  1042. if (!role)
  1043. goto out;
  1044. ctx->role = role->value;
  1045. /* Extract type. */
  1046. scontextp = p;
  1047. while (*p && *p != ':')
  1048. p++;
  1049. oldc = *p;
  1050. *p++ = 0;
  1051. typdatum = hashtab_search(pol->p_types.table, scontextp);
  1052. if (!typdatum || typdatum->attribute)
  1053. goto out;
  1054. ctx->type = typdatum->value;
  1055. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  1056. if (rc)
  1057. goto out;
  1058. rc = -EINVAL;
  1059. if ((p - scontext) < scontext_len)
  1060. goto out;
  1061. /* Check the validity of the new context. */
  1062. if (!policydb_context_isvalid(pol, ctx))
  1063. goto out;
  1064. rc = 0;
  1065. out:
  1066. if (rc)
  1067. context_destroy(ctx);
  1068. return rc;
  1069. }
  1070. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  1071. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  1072. int force)
  1073. {
  1074. char *scontext2, *str = NULL;
  1075. struct context context;
  1076. int rc = 0;
  1077. if (!ss_initialized) {
  1078. int i;
  1079. for (i = 1; i < SECINITSID_NUM; i++) {
  1080. if (!strcmp(initial_sid_to_string[i], scontext)) {
  1081. *sid = i;
  1082. return 0;
  1083. }
  1084. }
  1085. *sid = SECINITSID_KERNEL;
  1086. return 0;
  1087. }
  1088. *sid = SECSID_NULL;
  1089. /* Copy the string so that we can modify the copy as we parse it. */
  1090. scontext2 = kmalloc(scontext_len + 1, gfp_flags);
  1091. if (!scontext2)
  1092. return -ENOMEM;
  1093. memcpy(scontext2, scontext, scontext_len);
  1094. scontext2[scontext_len] = 0;
  1095. if (force) {
  1096. /* Save another copy for storing in uninterpreted form */
  1097. rc = -ENOMEM;
  1098. str = kstrdup(scontext2, gfp_flags);
  1099. if (!str)
  1100. goto out;
  1101. }
  1102. read_lock(&policy_rwlock);
  1103. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  1104. scontext_len, &context, def_sid);
  1105. if (rc == -EINVAL && force) {
  1106. context.str = str;
  1107. context.len = scontext_len;
  1108. str = NULL;
  1109. } else if (rc)
  1110. goto out_unlock;
  1111. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  1112. context_destroy(&context);
  1113. out_unlock:
  1114. read_unlock(&policy_rwlock);
  1115. out:
  1116. kfree(scontext2);
  1117. kfree(str);
  1118. return rc;
  1119. }
  1120. /**
  1121. * security_context_to_sid - Obtain a SID for a given security context.
  1122. * @scontext: security context
  1123. * @scontext_len: length in bytes
  1124. * @sid: security identifier, SID
  1125. *
  1126. * Obtains a SID associated with the security context that
  1127. * has the string representation specified by @scontext.
  1128. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1129. * memory is available, or 0 on success.
  1130. */
  1131. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
  1132. {
  1133. return security_context_to_sid_core(scontext, scontext_len,
  1134. sid, SECSID_NULL, GFP_KERNEL, 0);
  1135. }
  1136. /**
  1137. * security_context_to_sid_default - Obtain a SID for a given security context,
  1138. * falling back to specified default if needed.
  1139. *
  1140. * @scontext: security context
  1141. * @scontext_len: length in bytes
  1142. * @sid: security identifier, SID
  1143. * @def_sid: default SID to assign on error
  1144. *
  1145. * Obtains a SID associated with the security context that
  1146. * has the string representation specified by @scontext.
  1147. * The default SID is passed to the MLS layer to be used to allow
  1148. * kernel labeling of the MLS field if the MLS field is not present
  1149. * (for upgrading to MLS without full relabel).
  1150. * Implicitly forces adding of the context even if it cannot be mapped yet.
  1151. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1152. * memory is available, or 0 on success.
  1153. */
  1154. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  1155. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1156. {
  1157. return security_context_to_sid_core(scontext, scontext_len,
  1158. sid, def_sid, gfp_flags, 1);
  1159. }
  1160. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1161. u32 *sid)
  1162. {
  1163. return security_context_to_sid_core(scontext, scontext_len,
  1164. sid, SECSID_NULL, GFP_KERNEL, 1);
  1165. }
  1166. static int compute_sid_handle_invalid_context(
  1167. struct context *scontext,
  1168. struct context *tcontext,
  1169. u16 tclass,
  1170. struct context *newcontext)
  1171. {
  1172. char *s = NULL, *t = NULL, *n = NULL;
  1173. u32 slen, tlen, nlen;
  1174. if (context_struct_to_string(scontext, &s, &slen))
  1175. goto out;
  1176. if (context_struct_to_string(tcontext, &t, &tlen))
  1177. goto out;
  1178. if (context_struct_to_string(newcontext, &n, &nlen))
  1179. goto out;
  1180. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1181. "security_compute_sid: invalid context %s"
  1182. " for scontext=%s"
  1183. " tcontext=%s"
  1184. " tclass=%s",
  1185. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1186. out:
  1187. kfree(s);
  1188. kfree(t);
  1189. kfree(n);
  1190. if (!selinux_enforcing)
  1191. return 0;
  1192. return -EACCES;
  1193. }
  1194. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1195. u32 stype, u32 ttype, u16 tclass,
  1196. const char *objname)
  1197. {
  1198. struct filename_trans ft;
  1199. struct filename_trans_datum *otype;
  1200. /*
  1201. * Most filename trans rules are going to live in specific directories
  1202. * like /dev or /var/run. This bitmap will quickly skip rule searches
  1203. * if the ttype does not contain any rules.
  1204. */
  1205. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1206. return;
  1207. ft.stype = stype;
  1208. ft.ttype = ttype;
  1209. ft.tclass = tclass;
  1210. ft.name = objname;
  1211. otype = hashtab_search(p->filename_trans, &ft);
  1212. if (otype)
  1213. newcontext->type = otype->otype;
  1214. }
  1215. static int security_compute_sid(u32 ssid,
  1216. u32 tsid,
  1217. u16 orig_tclass,
  1218. u32 specified,
  1219. const char *objname,
  1220. u32 *out_sid,
  1221. bool kern)
  1222. {
  1223. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1224. struct role_trans *roletr = NULL;
  1225. struct avtab_key avkey;
  1226. struct avtab_datum *avdatum;
  1227. struct avtab_node *node;
  1228. u16 tclass;
  1229. int rc = 0;
  1230. bool sock;
  1231. if (!ss_initialized) {
  1232. switch (orig_tclass) {
  1233. case SECCLASS_PROCESS: /* kernel value */
  1234. *out_sid = ssid;
  1235. break;
  1236. default:
  1237. *out_sid = tsid;
  1238. break;
  1239. }
  1240. goto out;
  1241. }
  1242. context_init(&newcontext);
  1243. read_lock(&policy_rwlock);
  1244. if (kern) {
  1245. tclass = unmap_class(orig_tclass);
  1246. sock = security_is_socket_class(orig_tclass);
  1247. } else {
  1248. tclass = orig_tclass;
  1249. sock = security_is_socket_class(map_class(tclass));
  1250. }
  1251. scontext = sidtab_search(&sidtab, ssid);
  1252. if (!scontext) {
  1253. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1254. __func__, ssid);
  1255. rc = -EINVAL;
  1256. goto out_unlock;
  1257. }
  1258. tcontext = sidtab_search(&sidtab, tsid);
  1259. if (!tcontext) {
  1260. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1261. __func__, tsid);
  1262. rc = -EINVAL;
  1263. goto out_unlock;
  1264. }
  1265. /* Set the user identity. */
  1266. switch (specified) {
  1267. case AVTAB_TRANSITION:
  1268. case AVTAB_CHANGE:
  1269. /* Use the process user identity. */
  1270. newcontext.user = scontext->user;
  1271. break;
  1272. case AVTAB_MEMBER:
  1273. /* Use the related object owner. */
  1274. newcontext.user = tcontext->user;
  1275. break;
  1276. }
  1277. /* Set the role and type to default values. */
  1278. if ((tclass == policydb.process_class) || (sock == true)) {
  1279. /* Use the current role and type of process. */
  1280. newcontext.role = scontext->role;
  1281. newcontext.type = scontext->type;
  1282. } else {
  1283. /* Use the well-defined object role. */
  1284. newcontext.role = OBJECT_R_VAL;
  1285. /* Use the type of the related object. */
  1286. newcontext.type = tcontext->type;
  1287. }
  1288. /* Look for a type transition/member/change rule. */
  1289. avkey.source_type = scontext->type;
  1290. avkey.target_type = tcontext->type;
  1291. avkey.target_class = tclass;
  1292. avkey.specified = specified;
  1293. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1294. /* If no permanent rule, also check for enabled conditional rules */
  1295. if (!avdatum) {
  1296. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1297. for (; node; node = avtab_search_node_next(node, specified)) {
  1298. if (node->key.specified & AVTAB_ENABLED) {
  1299. avdatum = &node->datum;
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. if (avdatum) {
  1305. /* Use the type from the type transition/member/change rule. */
  1306. newcontext.type = avdatum->data;
  1307. }
  1308. /* if we have a objname this is a file trans check so check those rules */
  1309. if (objname)
  1310. filename_compute_type(&policydb, &newcontext, scontext->type,
  1311. tcontext->type, tclass, objname);
  1312. /* Check for class-specific changes. */
  1313. if (specified & AVTAB_TRANSITION) {
  1314. /* Look for a role transition rule. */
  1315. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1316. if ((roletr->role == scontext->role) &&
  1317. (roletr->type == tcontext->type) &&
  1318. (roletr->tclass == tclass)) {
  1319. /* Use the role transition rule. */
  1320. newcontext.role = roletr->new_role;
  1321. break;
  1322. }
  1323. }
  1324. }
  1325. /* Set the MLS attributes.
  1326. This is done last because it may allocate memory. */
  1327. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1328. &newcontext, sock);
  1329. if (rc)
  1330. goto out_unlock;
  1331. /* Check the validity of the context. */
  1332. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1333. rc = compute_sid_handle_invalid_context(scontext,
  1334. tcontext,
  1335. tclass,
  1336. &newcontext);
  1337. if (rc)
  1338. goto out_unlock;
  1339. }
  1340. /* Obtain the sid for the context. */
  1341. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1342. out_unlock:
  1343. read_unlock(&policy_rwlock);
  1344. context_destroy(&newcontext);
  1345. out:
  1346. return rc;
  1347. }
  1348. /**
  1349. * security_transition_sid - Compute the SID for a new subject/object.
  1350. * @ssid: source security identifier
  1351. * @tsid: target security identifier
  1352. * @tclass: target security class
  1353. * @out_sid: security identifier for new subject/object
  1354. *
  1355. * Compute a SID to use for labeling a new subject or object in the
  1356. * class @tclass based on a SID pair (@ssid, @tsid).
  1357. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1358. * if insufficient memory is available, or %0 if the new SID was
  1359. * computed successfully.
  1360. */
  1361. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1362. const struct qstr *qstr, u32 *out_sid)
  1363. {
  1364. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1365. qstr ? qstr->name : NULL, out_sid, true);
  1366. }
  1367. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1368. const char *objname, u32 *out_sid)
  1369. {
  1370. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1371. objname, out_sid, false);
  1372. }
  1373. /**
  1374. * security_member_sid - Compute the SID for member selection.
  1375. * @ssid: source security identifier
  1376. * @tsid: target security identifier
  1377. * @tclass: target security class
  1378. * @out_sid: security identifier for selected member
  1379. *
  1380. * Compute a SID to use when selecting a member of a polyinstantiated
  1381. * object of class @tclass based on a SID pair (@ssid, @tsid).
  1382. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1383. * if insufficient memory is available, or %0 if the SID was
  1384. * computed successfully.
  1385. */
  1386. int security_member_sid(u32 ssid,
  1387. u32 tsid,
  1388. u16 tclass,
  1389. u32 *out_sid)
  1390. {
  1391. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1392. out_sid, false);
  1393. }
  1394. /**
  1395. * security_change_sid - Compute the SID for object relabeling.
  1396. * @ssid: source security identifier
  1397. * @tsid: target security identifier
  1398. * @tclass: target security class
  1399. * @out_sid: security identifier for selected member
  1400. *
  1401. * Compute a SID to use for relabeling an object of class @tclass
  1402. * based on a SID pair (@ssid, @tsid).
  1403. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1404. * if insufficient memory is available, or %0 if the SID was
  1405. * computed successfully.
  1406. */
  1407. int security_change_sid(u32 ssid,
  1408. u32 tsid,
  1409. u16 tclass,
  1410. u32 *out_sid)
  1411. {
  1412. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1413. out_sid, false);
  1414. }
  1415. /* Clone the SID into the new SID table. */
  1416. static int clone_sid(u32 sid,
  1417. struct context *context,
  1418. void *arg)
  1419. {
  1420. struct sidtab *s = arg;
  1421. if (sid > SECINITSID_NUM)
  1422. return sidtab_insert(s, sid, context);
  1423. else
  1424. return 0;
  1425. }
  1426. static inline int convert_context_handle_invalid_context(struct context *context)
  1427. {
  1428. char *s;
  1429. u32 len;
  1430. if (selinux_enforcing)
  1431. return -EINVAL;
  1432. if (!context_struct_to_string(context, &s, &len)) {
  1433. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1434. kfree(s);
  1435. }
  1436. return 0;
  1437. }
  1438. struct convert_context_args {
  1439. struct policydb *oldp;
  1440. struct policydb *newp;
  1441. };
  1442. /*
  1443. * Convert the values in the security context
  1444. * structure `c' from the values specified
  1445. * in the policy `p->oldp' to the values specified
  1446. * in the policy `p->newp'. Verify that the
  1447. * context is valid under the new policy.
  1448. */
  1449. static int convert_context(u32 key,
  1450. struct context *c,
  1451. void *p)
  1452. {
  1453. struct convert_context_args *args;
  1454. struct context oldc;
  1455. struct ocontext *oc;
  1456. struct mls_range *range;
  1457. struct role_datum *role;
  1458. struct type_datum *typdatum;
  1459. struct user_datum *usrdatum;
  1460. char *s;
  1461. u32 len;
  1462. int rc = 0;
  1463. if (key <= SECINITSID_NUM)
  1464. goto out;
  1465. args = p;
  1466. if (c->str) {
  1467. struct context ctx;
  1468. rc = -ENOMEM;
  1469. s = kstrdup(c->str, GFP_KERNEL);
  1470. if (!s)
  1471. goto out;
  1472. rc = string_to_context_struct(args->newp, NULL, s,
  1473. c->len, &ctx, SECSID_NULL);
  1474. kfree(s);
  1475. if (!rc) {
  1476. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1477. c->str);
  1478. /* Replace string with mapped representation. */
  1479. kfree(c->str);
  1480. memcpy(c, &ctx, sizeof(*c));
  1481. goto out;
  1482. } else if (rc == -EINVAL) {
  1483. /* Retain string representation for later mapping. */
  1484. rc = 0;
  1485. goto out;
  1486. } else {
  1487. /* Other error condition, e.g. ENOMEM. */
  1488. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1489. c->str, -rc);
  1490. goto out;
  1491. }
  1492. }
  1493. rc = context_cpy(&oldc, c);
  1494. if (rc)
  1495. goto out;
  1496. /* Convert the user. */
  1497. rc = -EINVAL;
  1498. usrdatum = hashtab_search(args->newp->p_users.table,
  1499. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1500. if (!usrdatum)
  1501. goto bad;
  1502. c->user = usrdatum->value;
  1503. /* Convert the role. */
  1504. rc = -EINVAL;
  1505. role = hashtab_search(args->newp->p_roles.table,
  1506. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1507. if (!role)
  1508. goto bad;
  1509. c->role = role->value;
  1510. /* Convert the type. */
  1511. rc = -EINVAL;
  1512. typdatum = hashtab_search(args->newp->p_types.table,
  1513. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1514. if (!typdatum)
  1515. goto bad;
  1516. c->type = typdatum->value;
  1517. /* Convert the MLS fields if dealing with MLS policies */
  1518. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1519. rc = mls_convert_context(args->oldp, args->newp, c);
  1520. if (rc)
  1521. goto bad;
  1522. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1523. /*
  1524. * Switching between MLS and non-MLS policy:
  1525. * free any storage used by the MLS fields in the
  1526. * context for all existing entries in the sidtab.
  1527. */
  1528. mls_context_destroy(c);
  1529. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1530. /*
  1531. * Switching between non-MLS and MLS policy:
  1532. * ensure that the MLS fields of the context for all
  1533. * existing entries in the sidtab are filled in with a
  1534. * suitable default value, likely taken from one of the
  1535. * initial SIDs.
  1536. */
  1537. oc = args->newp->ocontexts[OCON_ISID];
  1538. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1539. oc = oc->next;
  1540. rc = -EINVAL;
  1541. if (!oc) {
  1542. printk(KERN_ERR "SELinux: unable to look up"
  1543. " the initial SIDs list\n");
  1544. goto bad;
  1545. }
  1546. range = &oc->context[0].range;
  1547. rc = mls_range_set(c, range);
  1548. if (rc)
  1549. goto bad;
  1550. }
  1551. /* Check the validity of the new context. */
  1552. if (!policydb_context_isvalid(args->newp, c)) {
  1553. rc = convert_context_handle_invalid_context(&oldc);
  1554. if (rc)
  1555. goto bad;
  1556. }
  1557. context_destroy(&oldc);
  1558. rc = 0;
  1559. out:
  1560. return rc;
  1561. bad:
  1562. /* Map old representation to string and save it. */
  1563. rc = context_struct_to_string(&oldc, &s, &len);
  1564. if (rc)
  1565. return rc;
  1566. context_destroy(&oldc);
  1567. context_destroy(c);
  1568. c->str = s;
  1569. c->len = len;
  1570. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1571. c->str);
  1572. rc = 0;
  1573. goto out;
  1574. }
  1575. static void security_load_policycaps(void)
  1576. {
  1577. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1578. POLICYDB_CAPABILITY_NETPEER);
  1579. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1580. POLICYDB_CAPABILITY_OPENPERM);
  1581. }
  1582. static int security_preserve_bools(struct policydb *p);
  1583. /**
  1584. * security_load_policy - Load a security policy configuration.
  1585. * @data: binary policy data
  1586. * @len: length of data in bytes
  1587. *
  1588. * Load a new set of security policy configuration data,
  1589. * validate it and convert the SID table as necessary.
  1590. * This function will flush the access vector cache after
  1591. * loading the new policy.
  1592. */
  1593. int security_load_policy(void *data, size_t len)
  1594. {
  1595. struct policydb oldpolicydb, newpolicydb;
  1596. struct sidtab oldsidtab, newsidtab;
  1597. struct selinux_mapping *oldmap, *map = NULL;
  1598. struct convert_context_args args;
  1599. u32 seqno;
  1600. u16 map_size;
  1601. int rc = 0;
  1602. struct policy_file file = { data, len }, *fp = &file;
  1603. if (!ss_initialized) {
  1604. avtab_cache_init();
  1605. rc = policydb_read(&policydb, fp);
  1606. if (rc) {
  1607. avtab_cache_destroy();
  1608. return rc;
  1609. }
  1610. policydb.len = len;
  1611. rc = selinux_set_mapping(&policydb, secclass_map,
  1612. &current_mapping,
  1613. &current_mapping_size);
  1614. if (rc) {
  1615. policydb_destroy(&policydb);
  1616. avtab_cache_destroy();
  1617. return rc;
  1618. }
  1619. rc = policydb_load_isids(&policydb, &sidtab);
  1620. if (rc) {
  1621. policydb_destroy(&policydb);
  1622. avtab_cache_destroy();
  1623. return rc;
  1624. }
  1625. security_load_policycaps();
  1626. ss_initialized = 1;
  1627. seqno = ++latest_granting;
  1628. selinux_complete_init();
  1629. avc_ss_reset(seqno);
  1630. selnl_notify_policyload(seqno);
  1631. selinux_status_update_policyload(seqno);
  1632. selinux_netlbl_cache_invalidate();
  1633. selinux_xfrm_notify_policyload();
  1634. return 0;
  1635. }
  1636. #if 0
  1637. sidtab_hash_eval(&sidtab, "sids");
  1638. #endif
  1639. rc = policydb_read(&newpolicydb, fp);
  1640. if (rc)
  1641. return rc;
  1642. newpolicydb.len = len;
  1643. /* If switching between different policy types, log MLS status */
  1644. if (policydb.mls_enabled && !newpolicydb.mls_enabled)
  1645. printk(KERN_INFO "SELinux: Disabling MLS support...\n");
  1646. else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
  1647. printk(KERN_INFO "SELinux: Enabling MLS support...\n");
  1648. rc = policydb_load_isids(&newpolicydb, &newsidtab);
  1649. if (rc) {
  1650. printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
  1651. policydb_destroy(&newpolicydb);
  1652. return rc;
  1653. }
  1654. rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);
  1655. if (rc)
  1656. goto err;
  1657. rc = security_preserve_bools(&newpolicydb);
  1658. if (rc) {
  1659. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1660. goto err;
  1661. }
  1662. /* Clone the SID table. */
  1663. sidtab_shutdown(&sidtab);
  1664. rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
  1665. if (rc)
  1666. goto err;
  1667. /*
  1668. * Convert the internal representations of contexts
  1669. * in the new SID table.
  1670. */
  1671. args.oldp = &policydb;
  1672. args.newp = &newpolicydb;
  1673. rc = sidtab_map(&newsidtab, convert_context, &args);
  1674. if (rc) {
  1675. printk(KERN_ERR "SELinux: unable to convert the internal"
  1676. " representation of contexts in the new SID"
  1677. " table\n");
  1678. goto err;
  1679. }
  1680. /* Save the old policydb and SID table to free later. */
  1681. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1682. sidtab_set(&oldsidtab, &sidtab);
  1683. /* Install the new policydb and SID table. */
  1684. write_lock_irq(&policy_rwlock);
  1685. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1686. sidtab_set(&sidtab, &newsidtab);
  1687. security_load_policycaps();
  1688. oldmap = current_mapping;
  1689. current_mapping = map;
  1690. current_mapping_size = map_size;
  1691. seqno = ++latest_granting;
  1692. write_unlock_irq(&policy_rwlock);
  1693. /* Free the old policydb and SID table. */
  1694. policydb_destroy(&oldpolicydb);
  1695. sidtab_destroy(&oldsidtab);
  1696. kfree(oldmap);
  1697. avc_ss_reset(seqno);
  1698. selnl_notify_policyload(seqno);
  1699. selinux_status_update_policyload(seqno);
  1700. selinux_netlbl_cache_invalidate();
  1701. selinux_xfrm_notify_policyload();
  1702. return 0;
  1703. err:
  1704. kfree(map);
  1705. sidtab_destroy(&newsidtab);
  1706. policydb_destroy(&newpolicydb);
  1707. return rc;
  1708. }
  1709. size_t security_policydb_len(void)
  1710. {
  1711. size_t len;
  1712. read_lock(&policy_rwlock);
  1713. len = policydb.len;
  1714. read_unlock(&policy_rwlock);
  1715. return len;
  1716. }
  1717. /**
  1718. * security_port_sid - Obtain the SID for a port.
  1719. * @protocol: protocol number
  1720. * @port: port number
  1721. * @out_sid: security identifier
  1722. */
  1723. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1724. {
  1725. struct ocontext *c;
  1726. int rc = 0;
  1727. read_lock(&policy_rwlock);
  1728. c = policydb.ocontexts[OCON_PORT];
  1729. while (c) {
  1730. if (c->u.port.protocol == protocol &&
  1731. c->u.port.low_port <= port &&
  1732. c->u.port.high_port >= port)
  1733. break;
  1734. c = c->next;
  1735. }
  1736. if (c) {
  1737. if (!c->sid[0]) {
  1738. rc = sidtab_context_to_sid(&sidtab,
  1739. &c->context[0],
  1740. &c->sid[0]);
  1741. if (rc)
  1742. goto out;
  1743. }
  1744. *out_sid = c->sid[0];
  1745. } else {
  1746. *out_sid = SECINITSID_PORT;
  1747. }
  1748. out:
  1749. read_unlock(&policy_rwlock);
  1750. return rc;
  1751. }
  1752. /**
  1753. * security_netif_sid - Obtain the SID for a network interface.
  1754. * @name: interface name
  1755. * @if_sid: interface SID
  1756. */
  1757. int security_netif_sid(char *name, u32 *if_sid)
  1758. {
  1759. int rc = 0;
  1760. struct ocontext *c;
  1761. read_lock(&policy_rwlock);
  1762. c = policydb.ocontexts[OCON_NETIF];
  1763. while (c) {
  1764. if (strcmp(name, c->u.name) == 0)
  1765. break;
  1766. c = c->next;
  1767. }
  1768. if (c) {
  1769. if (!c->sid[0] || !c->sid[1]) {
  1770. rc = sidtab_context_to_sid(&sidtab,
  1771. &c->context[0],
  1772. &c->sid[0]);
  1773. if (rc)
  1774. goto out;
  1775. rc = sidtab_context_to_sid(&sidtab,
  1776. &c->context[1],
  1777. &c->sid[1]);
  1778. if (rc)
  1779. goto out;
  1780. }
  1781. *if_sid = c->sid[0];
  1782. } else
  1783. *if_sid = SECINITSID_NETIF;
  1784. out:
  1785. read_unlock(&policy_rwlock);
  1786. return rc;
  1787. }
  1788. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1789. {
  1790. int i, fail = 0;
  1791. for (i = 0; i < 4; i++)
  1792. if (addr[i] != (input[i] & mask[i])) {
  1793. fail = 1;
  1794. break;
  1795. }
  1796. return !fail;
  1797. }
  1798. /**
  1799. * security_node_sid - Obtain the SID for a node (host).
  1800. * @domain: communication domain aka address family
  1801. * @addrp: address
  1802. * @addrlen: address length in bytes
  1803. * @out_sid: security identifier
  1804. */
  1805. int security_node_sid(u16 domain,
  1806. void *addrp,
  1807. u32 addrlen,
  1808. u32 *out_sid)
  1809. {
  1810. int rc;
  1811. struct ocontext *c;
  1812. read_lock(&policy_rwlock);
  1813. switch (domain) {
  1814. case AF_INET: {
  1815. u32 addr;
  1816. rc = -EINVAL;
  1817. if (addrlen != sizeof(u32))
  1818. goto out;
  1819. addr = *((u32 *)addrp);
  1820. c = policydb.ocontexts[OCON_NODE];
  1821. while (c) {
  1822. if (c->u.node.addr == (addr & c->u.node.mask))
  1823. break;
  1824. c = c->next;
  1825. }
  1826. break;
  1827. }
  1828. case AF_INET6:
  1829. rc = -EINVAL;
  1830. if (addrlen != sizeof(u64) * 2)
  1831. goto out;
  1832. c = policydb.ocontexts[OCON_NODE6];
  1833. while (c) {
  1834. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1835. c->u.node6.mask))
  1836. break;
  1837. c = c->next;
  1838. }
  1839. break;
  1840. default:
  1841. rc = 0;
  1842. *out_sid = SECINITSID_NODE;
  1843. goto out;
  1844. }
  1845. if (c) {
  1846. if (!c->sid[0]) {
  1847. rc = sidtab_context_to_sid(&sidtab,
  1848. &c->context[0],
  1849. &c->sid[0]);
  1850. if (rc)
  1851. goto out;
  1852. }
  1853. *out_sid = c->sid[0];
  1854. } else {
  1855. *out_sid = SECINITSID_NODE;
  1856. }
  1857. rc = 0;
  1858. out:
  1859. read_unlock(&policy_rwlock);
  1860. return rc;
  1861. }
  1862. #define SIDS_NEL 25
  1863. /**
  1864. * security_get_user_sids - Obtain reachable SIDs for a user.
  1865. * @fromsid: starting SID
  1866. * @username: username
  1867. * @sids: array of reachable SIDs for user
  1868. * @nel: number of elements in @sids
  1869. *
  1870. * Generate the set of SIDs for legal security contexts
  1871. * for a given user that can be reached by @fromsid.
  1872. * Set *@sids to point to a dynamically allocated
  1873. * array containing the set of SIDs. Set *@nel to the
  1874. * number of elements in the array.
  1875. */
  1876. int security_get_user_sids(u32 fromsid,
  1877. char *username,
  1878. u32 **sids,
  1879. u32 *nel)
  1880. {
  1881. struct context *fromcon, usercon;
  1882. u32 *mysids = NULL, *mysids2, sid;
  1883. u32 mynel = 0, maxnel = SIDS_NEL;
  1884. struct user_datum *user;
  1885. struct role_datum *role;
  1886. struct ebitmap_node *rnode, *tnode;
  1887. int rc = 0, i, j;
  1888. *sids = NULL;
  1889. *nel = 0;
  1890. if (!ss_initialized)
  1891. goto out;
  1892. read_lock(&policy_rwlock);
  1893. context_init(&usercon);
  1894. rc = -EINVAL;
  1895. fromcon = sidtab_search(&sidtab, fromsid);
  1896. if (!fromcon)
  1897. goto out_unlock;
  1898. rc = -EINVAL;
  1899. user = hashtab_search(policydb.p_users.table, username);
  1900. if (!user)
  1901. goto out_unlock;
  1902. usercon.user = user->value;
  1903. rc = -ENOMEM;
  1904. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1905. if (!mysids)
  1906. goto out_unlock;
  1907. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  1908. role = policydb.role_val_to_struct[i];
  1909. usercon.role = i + 1;
  1910. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  1911. usercon.type = j + 1;
  1912. if (mls_setup_user_range(fromcon, user, &usercon))
  1913. continue;
  1914. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1915. if (rc)
  1916. goto out_unlock;
  1917. if (mynel < maxnel) {
  1918. mysids[mynel++] = sid;
  1919. } else {
  1920. rc = -ENOMEM;
  1921. maxnel += SIDS_NEL;
  1922. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1923. if (!mysids2)
  1924. goto out_unlock;
  1925. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1926. kfree(mysids);
  1927. mysids = mysids2;
  1928. mysids[mynel++] = sid;
  1929. }
  1930. }
  1931. }
  1932. rc = 0;
  1933. out_unlock:
  1934. read_unlock(&policy_rwlock);
  1935. if (rc || !mynel) {
  1936. kfree(mysids);
  1937. goto out;
  1938. }
  1939. rc = -ENOMEM;
  1940. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  1941. if (!mysids2) {
  1942. kfree(mysids);
  1943. goto out;
  1944. }
  1945. for (i = 0, j = 0; i < mynel; i++) {
  1946. struct av_decision dummy_avd;
  1947. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  1948. SECCLASS_PROCESS, /* kernel value */
  1949. PROCESS__TRANSITION, AVC_STRICT,
  1950. &dummy_avd);
  1951. if (!rc)
  1952. mysids2[j++] = mysids[i];
  1953. cond_resched();
  1954. }
  1955. rc = 0;
  1956. kfree(mysids);
  1957. *sids = mysids2;
  1958. *nel = j;
  1959. out:
  1960. return rc;
  1961. }
  1962. /**
  1963. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1964. * @fstype: filesystem type
  1965. * @path: path from root of mount
  1966. * @sclass: file security class
  1967. * @sid: SID for path
  1968. *
  1969. * Obtain a SID to use for a file in a filesystem that
  1970. * cannot support xattr or use a fixed labeling behavior like
  1971. * transition SIDs or task SIDs.
  1972. */
  1973. int security_genfs_sid(const char *fstype,
  1974. char *path,
  1975. u16 orig_sclass,
  1976. u32 *sid)
  1977. {
  1978. int len;
  1979. u16 sclass;
  1980. struct genfs *genfs;
  1981. struct ocontext *c;
  1982. int rc, cmp = 0;
  1983. while (path[0] == '/' && path[1] == '/')
  1984. path++;
  1985. read_lock(&policy_rwlock);
  1986. sclass = unmap_class(orig_sclass);
  1987. *sid = SECINITSID_UNLABELED;
  1988. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1989. cmp = strcmp(fstype, genfs->fstype);
  1990. if (cmp <= 0)
  1991. break;
  1992. }
  1993. rc = -ENOENT;
  1994. if (!genfs || cmp)
  1995. goto out;
  1996. for (c = genfs->head; c; c = c->next) {
  1997. len = strlen(c->u.name);
  1998. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1999. (strncmp(c->u.name, path, len) == 0))
  2000. break;
  2001. }
  2002. rc = -ENOENT;
  2003. if (!c)
  2004. goto out;
  2005. if (!c->sid[0]) {
  2006. rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
  2007. if (rc)
  2008. goto out;
  2009. }
  2010. *sid = c->sid[0];
  2011. rc = 0;
  2012. out:
  2013. read_unlock(&policy_rwlock);
  2014. return rc;
  2015. }
  2016. /**
  2017. * security_fs_use - Determine how to handle labeling for a filesystem.
  2018. * @fstype: filesystem type
  2019. * @behavior: labeling behavior
  2020. * @sid: SID for filesystem (superblock)
  2021. */
  2022. int security_fs_use(
  2023. const char *fstype,
  2024. unsigned int *behavior,
  2025. u32 *sid)
  2026. {
  2027. int rc = 0;
  2028. struct ocontext *c;
  2029. read_lock(&policy_rwlock);
  2030. c = policydb.ocontexts[OCON_FSUSE];
  2031. while (c) {
  2032. if (strcmp(fstype, c->u.name) == 0)
  2033. break;
  2034. c = c->next;
  2035. }
  2036. if (c) {
  2037. *behavior = c->v.behavior;
  2038. if (!c->sid[0]) {
  2039. rc = sidtab_context_to_sid(&sidtab, &c->context[0],
  2040. &c->sid[0]);
  2041. if (rc)
  2042. goto out;
  2043. }
  2044. *sid = c->sid[0];
  2045. } else {
  2046. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  2047. if (rc) {
  2048. *behavior = SECURITY_FS_USE_NONE;
  2049. rc = 0;
  2050. } else {
  2051. *behavior = SECURITY_FS_USE_GENFS;
  2052. }
  2053. }
  2054. out:
  2055. read_unlock(&policy_rwlock);
  2056. return rc;
  2057. }
  2058. int security_get_bools(int *len, char ***names, int **values)
  2059. {
  2060. int i, rc;
  2061. read_lock(&policy_rwlock);
  2062. *names = NULL;
  2063. *values = NULL;
  2064. rc = 0;
  2065. *len = policydb.p_bools.nprim;
  2066. if (!*len)
  2067. goto out;
  2068. rc = -ENOMEM;
  2069. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  2070. if (!*names)
  2071. goto err;
  2072. rc = -ENOMEM;
  2073. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  2074. if (!*values)
  2075. goto err;
  2076. for (i = 0; i < *len; i++) {
  2077. size_t name_len;
  2078. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  2079. name_len = strlen(sym_name(&policydb, SYM_BOOLS, i)) + 1;
  2080. rc = -ENOMEM;
  2081. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  2082. if (!(*names)[i])
  2083. goto err;
  2084. strncpy((*names)[i], sym_name(&policydb, SYM_BOOLS, i), name_len);
  2085. (*names)[i][name_len - 1] = 0;
  2086. }
  2087. rc = 0;
  2088. out:
  2089. read_unlock(&policy_rwlock);
  2090. return rc;
  2091. err:
  2092. if (*names) {
  2093. for (i = 0; i < *len; i++)
  2094. kfree((*names)[i]);
  2095. }
  2096. kfree(*values);
  2097. goto out;
  2098. }
  2099. int security_set_bools(int len, int *values)
  2100. {
  2101. int i, rc;
  2102. int lenp, seqno = 0;
  2103. struct cond_node *cur;
  2104. write_lock_irq(&policy_rwlock);
  2105. rc = -EFAULT;
  2106. lenp = policydb.p_bools.nprim;
  2107. if (len != lenp)
  2108. goto out;
  2109. for (i = 0; i < len; i++) {
  2110. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  2111. audit_log(current->audit_context, GFP_ATOMIC,
  2112. AUDIT_MAC_CONFIG_CHANGE,
  2113. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  2114. sym_name(&policydb, SYM_BOOLS, i),
  2115. !!values[i],
  2116. policydb.bool_val_to_struct[i]->state,
  2117. audit_get_loginuid(current),
  2118. audit_get_sessionid(current));
  2119. }
  2120. if (values[i])
  2121. policydb.bool_val_to_struct[i]->state = 1;
  2122. else
  2123. policydb.bool_val_to_struct[i]->state = 0;
  2124. }
  2125. for (cur = policydb.cond_list; cur; cur = cur->next) {
  2126. rc = evaluate_cond_node(&policydb, cur);
  2127. if (rc)
  2128. goto out;
  2129. }
  2130. seqno = ++latest_granting;
  2131. rc = 0;
  2132. out:
  2133. write_unlock_irq(&policy_rwlock);
  2134. if (!rc) {
  2135. avc_ss_reset(seqno);
  2136. selnl_notify_policyload(seqno);
  2137. selinux_status_update_policyload(seqno);
  2138. selinux_xfrm_notify_policyload();
  2139. }
  2140. return rc;
  2141. }
  2142. int security_get_bool_value(int bool)
  2143. {
  2144. int rc;
  2145. int len;
  2146. read_lock(&policy_rwlock);
  2147. rc = -EFAULT;
  2148. len = policydb.p_bools.nprim;
  2149. if (bool >= len)
  2150. goto out;
  2151. rc = policydb.bool_val_to_struct[bool]->state;
  2152. out:
  2153. read_unlock(&policy_rwlock);
  2154. return rc;
  2155. }
  2156. static int security_preserve_bools(struct policydb *p)
  2157. {
  2158. int rc, nbools = 0, *bvalues = NULL, i;
  2159. char **bnames = NULL;
  2160. struct cond_bool_datum *booldatum;
  2161. struct cond_node *cur;
  2162. rc = security_get_bools(&nbools, &bnames, &bvalues);
  2163. if (rc)
  2164. goto out;
  2165. for (i = 0; i < nbools; i++) {
  2166. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  2167. if (booldatum)
  2168. booldatum->state = bvalues[i];
  2169. }
  2170. for (cur = p->cond_list; cur; cur = cur->next) {
  2171. rc = evaluate_cond_node(p, cur);
  2172. if (rc)
  2173. goto out;
  2174. }
  2175. out:
  2176. if (bnames) {
  2177. for (i = 0; i < nbools; i++)
  2178. kfree(bnames[i]);
  2179. }
  2180. kfree(bnames);
  2181. kfree(bvalues);
  2182. return rc;
  2183. }
  2184. /*
  2185. * security_sid_mls_copy() - computes a new sid based on the given
  2186. * sid and the mls portion of mls_sid.
  2187. */
  2188. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  2189. {
  2190. struct context *context1;
  2191. struct context *context2;
  2192. struct context newcon;
  2193. char *s;
  2194. u32 len;
  2195. int rc;
  2196. rc = 0;
  2197. if (!ss_initialized || !policydb.mls_enabled) {
  2198. *new_sid = sid;
  2199. goto out;
  2200. }
  2201. context_init(&newcon);
  2202. read_lock(&policy_rwlock);
  2203. rc = -EINVAL;
  2204. context1 = sidtab_search(&sidtab, sid);
  2205. if (!context1) {
  2206. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2207. __func__, sid);
  2208. goto out_unlock;
  2209. }
  2210. rc = -EINVAL;
  2211. context2 = sidtab_search(&sidtab, mls_sid);
  2212. if (!context2) {
  2213. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2214. __func__, mls_sid);
  2215. goto out_unlock;
  2216. }
  2217. newcon.user = context1->user;
  2218. newcon.role = context1->role;
  2219. newcon.type = context1->type;
  2220. rc = mls_context_cpy(&newcon, context2);
  2221. if (rc)
  2222. goto out_unlock;
  2223. /* Check the validity of the new context. */
  2224. if (!policydb_context_isvalid(&policydb, &newcon)) {
  2225. rc = convert_context_handle_invalid_context(&newcon);
  2226. if (rc) {
  2227. if (!context_struct_to_string(&newcon, &s, &len)) {
  2228. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2229. "security_sid_mls_copy: invalid context %s", s);
  2230. kfree(s);
  2231. }
  2232. goto out_unlock;
  2233. }
  2234. }
  2235. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  2236. out_unlock:
  2237. read_unlock(&policy_rwlock);
  2238. context_destroy(&newcon);
  2239. out:
  2240. return rc;
  2241. }
  2242. /**
  2243. * security_net_peersid_resolve - Compare and resolve two network peer SIDs
  2244. * @nlbl_sid: NetLabel SID
  2245. * @nlbl_type: NetLabel labeling protocol type
  2246. * @xfrm_sid: XFRM SID
  2247. *
  2248. * Description:
  2249. * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
  2250. * resolved into a single SID it is returned via @peer_sid and the function
  2251. * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
  2252. * returns a negative value. A table summarizing the behavior is below:
  2253. *
  2254. * | function return | @sid
  2255. * ------------------------------+-----------------+-----------------
  2256. * no peer labels | 0 | SECSID_NULL
  2257. * single peer label | 0 | <peer_label>
  2258. * multiple, consistent labels | 0 | <peer_label>
  2259. * multiple, inconsistent labels | -<errno> | SECSID_NULL
  2260. *
  2261. */
  2262. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  2263. u32 xfrm_sid,
  2264. u32 *peer_sid)
  2265. {
  2266. int rc;
  2267. struct context *nlbl_ctx;
  2268. struct context *xfrm_ctx;
  2269. *peer_sid = SECSID_NULL;
  2270. /* handle the common (which also happens to be the set of easy) cases
  2271. * right away, these two if statements catch everything involving a
  2272. * single or absent peer SID/label */
  2273. if (xfrm_sid == SECSID_NULL) {
  2274. *peer_sid = nlbl_sid;
  2275. return 0;
  2276. }
  2277. /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
  2278. * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
  2279. * is present */
  2280. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  2281. *peer_sid = xfrm_sid;
  2282. return 0;
  2283. }
  2284. /* we don't need to check ss_initialized here since the only way both
  2285. * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
  2286. * security server was initialized and ss_initialized was true */
  2287. if (!policydb.mls_enabled)
  2288. return 0;
  2289. read_lock(&policy_rwlock);
  2290. rc = -EINVAL;
  2291. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  2292. if (!nlbl_ctx) {
  2293. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2294. __func__, nlbl_sid);
  2295. goto out;
  2296. }
  2297. rc = -EINVAL;
  2298. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  2299. if (!xfrm_ctx) {
  2300. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2301. __func__, xfrm_sid);
  2302. goto out;
  2303. }
  2304. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  2305. if (rc)
  2306. goto out;
  2307. /* at present NetLabel SIDs/labels really only carry MLS
  2308. * information so if the MLS portion of the NetLabel SID
  2309. * matches the MLS portion of the labeled XFRM SID/label
  2310. * then pass along the XFRM SID as it is the most
  2311. * expressive */
  2312. *peer_sid = xfrm_sid;
  2313. out:
  2314. read_unlock(&policy_rwlock);
  2315. return rc;
  2316. }
  2317. static int get_classes_callback(void *k, void *d, void *args)
  2318. {
  2319. struct class_datum *datum = d;
  2320. char *name = k, **classes = args;
  2321. int value = datum->value - 1;
  2322. classes[value] = kstrdup(name, GFP_ATOMIC);
  2323. if (!classes[value])
  2324. return -ENOMEM;
  2325. return 0;
  2326. }
  2327. int security_get_classes(char ***classes, int *nclasses)
  2328. {
  2329. int rc;
  2330. read_lock(&policy_rwlock);
  2331. rc = -ENOMEM;
  2332. *nclasses = policydb.p_classes.nprim;
  2333. *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
  2334. if (!*classes)
  2335. goto out;
  2336. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  2337. *classes);
  2338. if (rc) {
  2339. int i;
  2340. for (i = 0; i < *nclasses; i++)
  2341. kfree((*classes)[i]);
  2342. kfree(*classes);
  2343. }
  2344. out:
  2345. read_unlock(&policy_rwlock);
  2346. return rc;
  2347. }
  2348. static int get_permissions_callback(void *k, void *d, void *args)
  2349. {
  2350. struct perm_datum *datum = d;
  2351. char *name = k, **perms = args;
  2352. int value = datum->value - 1;
  2353. perms[value] = kstrdup(name, GFP_ATOMIC);
  2354. if (!perms[value])
  2355. return -ENOMEM;
  2356. return 0;
  2357. }
  2358. int security_get_permissions(char *class, char ***perms, int *nperms)
  2359. {
  2360. int rc, i;
  2361. struct class_datum *match;
  2362. read_lock(&policy_rwlock);
  2363. rc = -EINVAL;
  2364. match = hashtab_search(policydb.p_classes.table, class);
  2365. if (!match) {
  2366. printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
  2367. __func__, class);
  2368. goto out;
  2369. }
  2370. rc = -ENOMEM;
  2371. *nperms = match->permissions.nprim;
  2372. *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
  2373. if (!*perms)
  2374. goto out;
  2375. if (match->comdatum) {
  2376. rc = hashtab_map(match->comdatum->permissions.table,
  2377. get_permissions_callback, *perms);
  2378. if (rc)
  2379. goto err;
  2380. }
  2381. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  2382. *perms);
  2383. if (rc)
  2384. goto err;
  2385. out:
  2386. read_unlock(&policy_rwlock);
  2387. return rc;
  2388. err:
  2389. read_unlock(&policy_rwlock);
  2390. for (i = 0; i < *nperms; i++)
  2391. kfree((*perms)[i]);
  2392. kfree(*perms);
  2393. return rc;
  2394. }
  2395. int security_get_reject_unknown(void)
  2396. {
  2397. return policydb.reject_unknown;
  2398. }
  2399. int security_get_allow_unknown(void)
  2400. {
  2401. return policydb.allow_unknown;
  2402. }
  2403. /**
  2404. * security_policycap_supported - Check for a specific policy capability
  2405. * @req_cap: capability
  2406. *
  2407. * Description:
  2408. * This function queries the currently loaded policy to see if it supports the
  2409. * capability specified by @req_cap. Returns true (1) if the capability is
  2410. * supported, false (0) if it isn't supported.
  2411. *
  2412. */
  2413. int security_policycap_supported(unsigned int req_cap)
  2414. {
  2415. int rc;
  2416. read_lock(&policy_rwlock);
  2417. rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
  2418. read_unlock(&policy_rwlock);
  2419. return rc;
  2420. }
  2421. struct selinux_audit_rule {
  2422. u32 au_seqno;
  2423. struct context au_ctxt;
  2424. };
  2425. void selinux_audit_rule_free(void *vrule)
  2426. {
  2427. struct selinux_audit_rule *rule = vrule;
  2428. if (rule) {
  2429. context_destroy(&rule->au_ctxt);
  2430. kfree(rule);
  2431. }
  2432. }
  2433. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2434. {
  2435. struct selinux_audit_rule *tmprule;
  2436. struct role_datum *roledatum;
  2437. struct type_datum *typedatum;
  2438. struct user_datum *userdatum;
  2439. struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
  2440. int rc = 0;
  2441. *rule = NULL;
  2442. if (!ss_initialized)
  2443. return -EOPNOTSUPP;
  2444. switch (field) {
  2445. case AUDIT_SUBJ_USER:
  2446. case AUDIT_SUBJ_ROLE:
  2447. case AUDIT_SUBJ_TYPE:
  2448. case AUDIT_OBJ_USER:
  2449. case AUDIT_OBJ_ROLE:
  2450. case AUDIT_OBJ_TYPE:
  2451. /* only 'equals' and 'not equals' fit user, role, and type */
  2452. if (op != Audit_equal && op != Audit_not_equal)
  2453. return -EINVAL;
  2454. break;
  2455. case AUDIT_SUBJ_SEN:
  2456. case AUDIT_SUBJ_CLR:
  2457. case AUDIT_OBJ_LEV_LOW:
  2458. case AUDIT_OBJ_LEV_HIGH:
  2459. /* we do not allow a range, indicated by the presence of '-' */
  2460. if (strchr(rulestr, '-'))
  2461. return -EINVAL;
  2462. break;
  2463. default:
  2464. /* only the above fields are valid */
  2465. return -EINVAL;
  2466. }
  2467. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  2468. if (!tmprule)
  2469. return -ENOMEM;
  2470. context_init(&tmprule->au_ctxt);
  2471. read_lock(&policy_rwlock);
  2472. tmprule->au_seqno = latest_granting;
  2473. switch (field) {
  2474. case AUDIT_SUBJ_USER:
  2475. case AUDIT_OBJ_USER:
  2476. rc = -EINVAL;
  2477. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  2478. if (!userdatum)
  2479. goto out;
  2480. tmprule->au_ctxt.user = userdatum->value;
  2481. break;
  2482. case AUDIT_SUBJ_ROLE:
  2483. case AUDIT_OBJ_ROLE:
  2484. rc = -EINVAL;
  2485. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  2486. if (!roledatum)
  2487. goto out;
  2488. tmprule->au_ctxt.role = roledatum->value;
  2489. break;
  2490. case AUDIT_SUBJ_TYPE:
  2491. case AUDIT_OBJ_TYPE:
  2492. rc = -EINVAL;
  2493. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  2494. if (!typedatum)
  2495. goto out;
  2496. tmprule->au_ctxt.type = typedatum->value;
  2497. break;
  2498. case AUDIT_SUBJ_SEN:
  2499. case AUDIT_SUBJ_CLR:
  2500. case AUDIT_OBJ_LEV_LOW:
  2501. case AUDIT_OBJ_LEV_HIGH:
  2502. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  2503. if (rc)
  2504. goto out;
  2505. break;
  2506. }
  2507. rc = 0;
  2508. out:
  2509. read_unlock(&policy_rwlock);
  2510. if (rc) {
  2511. selinux_audit_rule_free(tmprule);
  2512. tmprule = NULL;
  2513. }
  2514. *rule = tmprule;
  2515. return rc;
  2516. }
  2517. /* Check to see if the rule contains any selinux fields */
  2518. int selinux_audit_rule_known(struct audit_krule *rule)
  2519. {
  2520. int i;
  2521. for (i = 0; i < rule->field_count; i++) {
  2522. struct audit_field *f = &rule->fields[i];
  2523. switch (f->type) {
  2524. case AUDIT_SUBJ_USER:
  2525. case AUDIT_SUBJ_ROLE:
  2526. case AUDIT_SUBJ_TYPE:
  2527. case AUDIT_SUBJ_SEN:
  2528. case AUDIT_SUBJ_CLR:
  2529. case AUDIT_OBJ_USER:
  2530. case AUDIT_OBJ_ROLE:
  2531. case AUDIT_OBJ_TYPE:
  2532. case AUDIT_OBJ_LEV_LOW:
  2533. case AUDIT_OBJ_LEV_HIGH:
  2534. return 1;
  2535. }
  2536. }
  2537. return 0;
  2538. }
  2539. int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  2540. struct audit_context *actx)
  2541. {
  2542. struct context *ctxt;
  2543. struct mls_level *level;
  2544. struct selinux_audit_rule *rule = vrule;
  2545. int match = 0;
  2546. if (!rule) {
  2547. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2548. "selinux_audit_rule_match: missing rule\n");
  2549. return -ENOENT;
  2550. }
  2551. read_lock(&policy_rwlock);
  2552. if (rule->au_seqno < latest_granting) {
  2553. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2554. "selinux_audit_rule_match: stale rule\n");
  2555. match = -ESTALE;
  2556. goto out;
  2557. }
  2558. ctxt = sidtab_search(&sidtab, sid);
  2559. if (!ctxt) {
  2560. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2561. "selinux_audit_rule_match: unrecognized SID %d\n",
  2562. sid);
  2563. match = -ENOENT;
  2564. goto out;
  2565. }
  2566. /* a field/op pair that is not caught here will simply fall through
  2567. without a match */
  2568. switch (field) {
  2569. case AUDIT_SUBJ_USER:
  2570. case AUDIT_OBJ_USER:
  2571. switch (op) {
  2572. case Audit_equal:
  2573. match = (ctxt->user == rule->au_ctxt.user);
  2574. break;
  2575. case Audit_not_equal:
  2576. match = (ctxt->user != rule->au_ctxt.user);
  2577. break;
  2578. }
  2579. break;
  2580. case AUDIT_SUBJ_ROLE:
  2581. case AUDIT_OBJ_ROLE:
  2582. switch (op) {
  2583. case Audit_equal:
  2584. match = (ctxt->role == rule->au_ctxt.role);
  2585. break;
  2586. case Audit_not_equal:
  2587. match = (ctxt->role != rule->au_ctxt.role);
  2588. break;
  2589. }
  2590. break;
  2591. case AUDIT_SUBJ_TYPE:
  2592. case AUDIT_OBJ_TYPE:
  2593. switch (op) {
  2594. case Audit_equal:
  2595. match = (ctxt->type == rule->au_ctxt.type);
  2596. break;
  2597. case Audit_not_equal:
  2598. match = (ctxt->type != rule->au_ctxt.type);
  2599. break;
  2600. }
  2601. break;
  2602. case AUDIT_SUBJ_SEN:
  2603. case AUDIT_SUBJ_CLR:
  2604. case AUDIT_OBJ_LEV_LOW:
  2605. case AUDIT_OBJ_LEV_HIGH:
  2606. level = ((field == AUDIT_SUBJ_SEN ||
  2607. field == AUDIT_OBJ_LEV_LOW) ?
  2608. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2609. switch (op) {
  2610. case Audit_equal:
  2611. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2612. level);
  2613. break;
  2614. case Audit_not_equal:
  2615. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2616. level);
  2617. break;
  2618. case Audit_lt:
  2619. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2620. level) &&
  2621. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2622. level));
  2623. break;
  2624. case Audit_le:
  2625. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2626. level);
  2627. break;
  2628. case Audit_gt:
  2629. match = (mls_level_dom(level,
  2630. &rule->au_ctxt.range.level[0]) &&
  2631. !mls_level_eq(level,
  2632. &rule->au_ctxt.range.level[0]));
  2633. break;
  2634. case Audit_ge:
  2635. match = mls_level_dom(level,
  2636. &rule->au_ctxt.range.level[0]);
  2637. break;
  2638. }
  2639. }
  2640. out:
  2641. read_unlock(&policy_rwlock);
  2642. return match;
  2643. }
  2644. static int (*aurule_callback)(void) = audit_update_lsm_rules;
  2645. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  2646. u16 class, u32 perms, u32 *retained)
  2647. {
  2648. int err = 0;
  2649. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2650. err = aurule_callback();
  2651. return err;
  2652. }
  2653. static int __init aurule_init(void)
  2654. {
  2655. int err;
  2656. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  2657. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  2658. if (err)
  2659. panic("avc_add_callback() failed, error %d\n", err);
  2660. return err;
  2661. }
  2662. __initcall(aurule_init);
  2663. #ifdef CONFIG_NETLABEL
  2664. /**
  2665. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2666. * @secattr: the NetLabel packet security attributes
  2667. * @sid: the SELinux SID
  2668. *
  2669. * Description:
  2670. * Attempt to cache the context in @ctx, which was derived from the packet in
  2671. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2672. * already been initialized.
  2673. *
  2674. */
  2675. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2676. u32 sid)
  2677. {
  2678. u32 *sid_cache;
  2679. sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
  2680. if (sid_cache == NULL)
  2681. return;
  2682. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2683. if (secattr->cache == NULL) {
  2684. kfree(sid_cache);
  2685. return;
  2686. }
  2687. *sid_cache = sid;
  2688. secattr->cache->free = kfree;
  2689. secattr->cache->data = sid_cache;
  2690. secattr->flags |= NETLBL_SECATTR_CACHE;
  2691. }
  2692. /**
  2693. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2694. * @secattr: the NetLabel packet security attributes
  2695. * @sid: the SELinux SID
  2696. *
  2697. * Description:
  2698. * Convert the given NetLabel security attributes in @secattr into a
  2699. * SELinux SID. If the @secattr field does not contain a full SELinux
  2700. * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
  2701. * 'cache' field of @secattr is set and the CACHE flag is set; this is to
  2702. * allow the @secattr to be used by NetLabel to cache the secattr to SID
  2703. * conversion for future lookups. Returns zero on success, negative values on
  2704. * failure.
  2705. *
  2706. */
  2707. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2708. u32 *sid)
  2709. {
  2710. int rc;
  2711. struct context *ctx;
  2712. struct context ctx_new;
  2713. if (!ss_initialized) {
  2714. *sid = SECSID_NULL;
  2715. return 0;
  2716. }
  2717. read_lock(&policy_rwlock);
  2718. if (secattr->flags & NETLBL_SECATTR_CACHE)
  2719. *sid = *(u32 *)secattr->cache->data;
  2720. else if (secattr->flags & NETLBL_SECATTR_SECID)
  2721. *sid = secattr->attr.secid;
  2722. else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2723. rc = -EIDRM;
  2724. ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
  2725. if (ctx == NULL)
  2726. goto out;
  2727. context_init(&ctx_new);
  2728. ctx_new.user = ctx->user;
  2729. ctx_new.role = ctx->role;
  2730. ctx_new.type = ctx->type;
  2731. mls_import_netlbl_lvl(&ctx_new, secattr);
  2732. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2733. rc = ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2734. secattr->attr.mls.cat);
  2735. if (rc)
  2736. goto out;
  2737. memcpy(&ctx_new.range.level[1].cat,
  2738. &ctx_new.range.level[0].cat,
  2739. sizeof(ctx_new.range.level[0].cat));
  2740. }
  2741. rc = -EIDRM;
  2742. if (!mls_context_isvalid(&policydb, &ctx_new))
  2743. goto out_free;
  2744. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2745. if (rc)
  2746. goto out_free;
  2747. security_netlbl_cache_add(secattr, *sid);
  2748. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2749. } else
  2750. *sid = SECSID_NULL;
  2751. read_unlock(&policy_rwlock);
  2752. return 0;
  2753. out_free:
  2754. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2755. out:
  2756. read_unlock(&policy_rwlock);
  2757. return rc;
  2758. }
  2759. /**
  2760. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2761. * @sid: the SELinux SID
  2762. * @secattr: the NetLabel packet security attributes
  2763. *
  2764. * Description:
  2765. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2766. * Returns zero on success, negative values on failure.
  2767. *
  2768. */
  2769. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2770. {
  2771. int rc;
  2772. struct context *ctx;
  2773. if (!ss_initialized)
  2774. return 0;
  2775. read_lock(&policy_rwlock);
  2776. rc = -ENOENT;
  2777. ctx = sidtab_search(&sidtab, sid);
  2778. if (ctx == NULL)
  2779. goto out;
  2780. rc = -ENOMEM;
  2781. secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
  2782. GFP_ATOMIC);
  2783. if (secattr->domain == NULL)
  2784. goto out;
  2785. secattr->attr.secid = sid;
  2786. secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
  2787. mls_export_netlbl_lvl(ctx, secattr);
  2788. rc = mls_export_netlbl_cat(ctx, secattr);
  2789. out:
  2790. read_unlock(&policy_rwlock);
  2791. return rc;
  2792. }
  2793. #endif /* CONFIG_NETLABEL */
  2794. /**
  2795. * security_read_policy - read the policy.
  2796. * @data: binary policy data
  2797. * @len: length of data in bytes
  2798. *
  2799. */
  2800. int security_read_policy(void **data, size_t *len)
  2801. {
  2802. int rc;
  2803. struct policy_file fp;
  2804. if (!ss_initialized)
  2805. return -EINVAL;
  2806. *len = security_policydb_len();
  2807. *data = vmalloc_user(*len);
  2808. if (!*data)
  2809. return -ENOMEM;
  2810. fp.data = *data;
  2811. fp.len = *len;
  2812. read_lock(&policy_rwlock);
  2813. rc = policydb_write(&policydb, &fp);
  2814. read_unlock(&policy_rwlock);
  2815. if (rc)
  2816. return rc;
  2817. *len = (unsigned long)fp.data - (unsigned long)*data;
  2818. return 0;
  2819. }