services.c 70 KB

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