services.c 71 KB

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