services.c 74 KB

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