services.c 66 KB

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