services.c 74 KB

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