services.c 72 KB

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