services.c 70 KB

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