services.c 73 KB

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