services.c 72 KB

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