services.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809
  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. if (p->allow_unknown) {
  1059. u32 num_classes = kdefs->cts_len;
  1060. p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
  1061. if (!p->undefined_perms)
  1062. return -ENOMEM;
  1063. }
  1064. for (i = 1; i < kdefs->cts_len; i++) {
  1065. def_class = kdefs->class_to_string[i];
  1066. if (!def_class)
  1067. continue;
  1068. if (i > p->p_classes.nprim) {
  1069. printk(KERN_INFO
  1070. "SELinux: class %s not defined in policy\n",
  1071. def_class);
  1072. if (p->reject_unknown)
  1073. return -EINVAL;
  1074. if (p->allow_unknown)
  1075. p->undefined_perms[i-1] = ~0U;
  1076. continue;
  1077. }
  1078. pol_class = p->p_class_val_to_name[i-1];
  1079. if (strcmp(pol_class, def_class)) {
  1080. printk(KERN_ERR
  1081. "SELinux: class %d is incorrect, found %s but should be %s\n",
  1082. i, pol_class, def_class);
  1083. return -EINVAL;
  1084. }
  1085. }
  1086. for (i = 0; i < kdefs->av_pts_len; i++) {
  1087. class_val = kdefs->av_perm_to_string[i].tclass;
  1088. perm_val = kdefs->av_perm_to_string[i].value;
  1089. def_perm = kdefs->av_perm_to_string[i].name;
  1090. if (class_val > p->p_classes.nprim)
  1091. continue;
  1092. pol_class = p->p_class_val_to_name[class_val-1];
  1093. cladatum = hashtab_search(p->p_classes.table, pol_class);
  1094. BUG_ON(!cladatum);
  1095. perms = &cladatum->permissions;
  1096. nprim = 1 << (perms->nprim - 1);
  1097. if (perm_val > nprim) {
  1098. printk(KERN_INFO
  1099. "SELinux: permission %s in class %s not defined in policy\n",
  1100. def_perm, pol_class);
  1101. if (p->reject_unknown)
  1102. return -EINVAL;
  1103. if (p->allow_unknown)
  1104. p->undefined_perms[class_val-1] |= perm_val;
  1105. continue;
  1106. }
  1107. perdatum = hashtab_search(perms->table, def_perm);
  1108. if (perdatum == NULL) {
  1109. printk(KERN_ERR
  1110. "SELinux: permission %s in class %s not found in policy, bad policy\n",
  1111. def_perm, pol_class);
  1112. return -EINVAL;
  1113. }
  1114. pol_val = 1 << (perdatum->value - 1);
  1115. if (pol_val != perm_val) {
  1116. printk(KERN_ERR
  1117. "SELinux: permission %s in class %s has incorrect value\n",
  1118. def_perm, pol_class);
  1119. return -EINVAL;
  1120. }
  1121. }
  1122. for (i = 0; i < kdefs->av_inherit_len; i++) {
  1123. class_val = kdefs->av_inherit[i].tclass;
  1124. if (class_val > p->p_classes.nprim)
  1125. continue;
  1126. pol_class = p->p_class_val_to_name[class_val-1];
  1127. cladatum = hashtab_search(p->p_classes.table, pol_class);
  1128. BUG_ON(!cladatum);
  1129. if (!cladatum->comdatum) {
  1130. printk(KERN_ERR
  1131. "SELinux: class %s should have an inherits clause but does not\n",
  1132. pol_class);
  1133. return -EINVAL;
  1134. }
  1135. tmp = kdefs->av_inherit[i].common_base;
  1136. common_pts_len = 0;
  1137. while (!(tmp & 0x01)) {
  1138. common_pts_len++;
  1139. tmp >>= 1;
  1140. }
  1141. perms = &cladatum->comdatum->permissions;
  1142. for (j = 0; j < common_pts_len; j++) {
  1143. def_perm = kdefs->av_inherit[i].common_pts[j];
  1144. if (j >= perms->nprim) {
  1145. printk(KERN_INFO
  1146. "SELinux: permission %s in class %s not defined in policy\n",
  1147. def_perm, pol_class);
  1148. if (p->reject_unknown)
  1149. return -EINVAL;
  1150. if (p->allow_unknown)
  1151. p->undefined_perms[class_val-1] |= (1 << j);
  1152. continue;
  1153. }
  1154. perdatum = hashtab_search(perms->table, def_perm);
  1155. if (perdatum == NULL) {
  1156. printk(KERN_ERR
  1157. "SELinux: permission %s in class %s not found in policy, bad policy\n",
  1158. def_perm, pol_class);
  1159. return -EINVAL;
  1160. }
  1161. if (perdatum->value != j + 1) {
  1162. printk(KERN_ERR
  1163. "SELinux: permission %s in class %s has incorrect value\n",
  1164. def_perm, pol_class);
  1165. return -EINVAL;
  1166. }
  1167. }
  1168. }
  1169. return 0;
  1170. }
  1171. /* Clone the SID into the new SID table. */
  1172. static int clone_sid(u32 sid,
  1173. struct context *context,
  1174. void *arg)
  1175. {
  1176. struct sidtab *s = arg;
  1177. return sidtab_insert(s, sid, context);
  1178. }
  1179. static inline int convert_context_handle_invalid_context(struct context *context)
  1180. {
  1181. int rc = 0;
  1182. if (selinux_enforcing) {
  1183. rc = -EINVAL;
  1184. } else {
  1185. char *s;
  1186. u32 len;
  1187. if (!context_struct_to_string(context, &s, &len)) {
  1188. printk(KERN_WARNING
  1189. "SELinux: Context %s would be invalid if enforcing\n",
  1190. s);
  1191. kfree(s);
  1192. }
  1193. }
  1194. return rc;
  1195. }
  1196. struct convert_context_args {
  1197. struct policydb *oldp;
  1198. struct policydb *newp;
  1199. };
  1200. /*
  1201. * Convert the values in the security context
  1202. * structure `c' from the values specified
  1203. * in the policy `p->oldp' to the values specified
  1204. * in the policy `p->newp'. Verify that the
  1205. * context is valid under the new policy.
  1206. */
  1207. static int convert_context(u32 key,
  1208. struct context *c,
  1209. void *p)
  1210. {
  1211. struct convert_context_args *args;
  1212. struct context oldc;
  1213. struct role_datum *role;
  1214. struct type_datum *typdatum;
  1215. struct user_datum *usrdatum;
  1216. char *s;
  1217. u32 len;
  1218. int rc;
  1219. args = p;
  1220. if (c->str) {
  1221. struct context ctx;
  1222. s = kstrdup(c->str, GFP_KERNEL);
  1223. if (!s) {
  1224. rc = -ENOMEM;
  1225. goto out;
  1226. }
  1227. rc = string_to_context_struct(args->newp, NULL, s,
  1228. c->len, &ctx, SECSID_NULL);
  1229. kfree(s);
  1230. if (!rc) {
  1231. printk(KERN_INFO
  1232. "SELinux: Context %s became valid (mapped).\n",
  1233. c->str);
  1234. /* Replace string with mapped representation. */
  1235. kfree(c->str);
  1236. memcpy(c, &ctx, sizeof(*c));
  1237. goto out;
  1238. } else if (rc == -EINVAL) {
  1239. /* Retain string representation for later mapping. */
  1240. rc = 0;
  1241. goto out;
  1242. } else {
  1243. /* Other error condition, e.g. ENOMEM. */
  1244. printk(KERN_ERR
  1245. "SELinux: Unable to map context %s, rc = %d.\n",
  1246. c->str, -rc);
  1247. goto out;
  1248. }
  1249. }
  1250. rc = context_cpy(&oldc, c);
  1251. if (rc)
  1252. goto out;
  1253. rc = -EINVAL;
  1254. /* Convert the user. */
  1255. usrdatum = hashtab_search(args->newp->p_users.table,
  1256. args->oldp->p_user_val_to_name[c->user - 1]);
  1257. if (!usrdatum)
  1258. goto bad;
  1259. c->user = usrdatum->value;
  1260. /* Convert the role. */
  1261. role = hashtab_search(args->newp->p_roles.table,
  1262. args->oldp->p_role_val_to_name[c->role - 1]);
  1263. if (!role)
  1264. goto bad;
  1265. c->role = role->value;
  1266. /* Convert the type. */
  1267. typdatum = hashtab_search(args->newp->p_types.table,
  1268. args->oldp->p_type_val_to_name[c->type - 1]);
  1269. if (!typdatum)
  1270. goto bad;
  1271. c->type = typdatum->value;
  1272. rc = mls_convert_context(args->oldp, args->newp, c);
  1273. if (rc)
  1274. goto bad;
  1275. /* Check the validity of the new context. */
  1276. if (!policydb_context_isvalid(args->newp, c)) {
  1277. rc = convert_context_handle_invalid_context(&oldc);
  1278. if (rc)
  1279. goto bad;
  1280. }
  1281. context_destroy(&oldc);
  1282. rc = 0;
  1283. out:
  1284. return rc;
  1285. bad:
  1286. /* Map old representation to string and save it. */
  1287. if (context_struct_to_string(&oldc, &s, &len))
  1288. return -ENOMEM;
  1289. context_destroy(&oldc);
  1290. context_destroy(c);
  1291. c->str = s;
  1292. c->len = len;
  1293. printk(KERN_INFO
  1294. "SELinux: Context %s became invalid (unmapped).\n",
  1295. c->str);
  1296. rc = 0;
  1297. goto out;
  1298. }
  1299. static void security_load_policycaps(void)
  1300. {
  1301. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1302. POLICYDB_CAPABILITY_NETPEER);
  1303. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1304. POLICYDB_CAPABILITY_OPENPERM);
  1305. }
  1306. extern void selinux_complete_init(void);
  1307. static int security_preserve_bools(struct policydb *p);
  1308. /**
  1309. * security_load_policy - Load a security policy configuration.
  1310. * @data: binary policy data
  1311. * @len: length of data in bytes
  1312. *
  1313. * Load a new set of security policy configuration data,
  1314. * validate it and convert the SID table as necessary.
  1315. * This function will flush the access vector cache after
  1316. * loading the new policy.
  1317. */
  1318. int security_load_policy(void *data, size_t len)
  1319. {
  1320. struct policydb oldpolicydb, newpolicydb;
  1321. struct sidtab oldsidtab, newsidtab;
  1322. struct convert_context_args args;
  1323. u32 seqno;
  1324. int rc = 0;
  1325. struct policy_file file = { data, len }, *fp = &file;
  1326. if (!ss_initialized) {
  1327. avtab_cache_init();
  1328. if (policydb_read(&policydb, fp)) {
  1329. avtab_cache_destroy();
  1330. return -EINVAL;
  1331. }
  1332. if (policydb_load_isids(&policydb, &sidtab)) {
  1333. policydb_destroy(&policydb);
  1334. avtab_cache_destroy();
  1335. return -EINVAL;
  1336. }
  1337. /* Verify that the kernel defined classes are correct. */
  1338. if (validate_classes(&policydb)) {
  1339. printk(KERN_ERR
  1340. "SELinux: the definition of a class is incorrect\n");
  1341. sidtab_destroy(&sidtab);
  1342. policydb_destroy(&policydb);
  1343. avtab_cache_destroy();
  1344. return -EINVAL;
  1345. }
  1346. security_load_policycaps();
  1347. policydb_loaded_version = policydb.policyvers;
  1348. ss_initialized = 1;
  1349. seqno = ++latest_granting;
  1350. selinux_complete_init();
  1351. avc_ss_reset(seqno);
  1352. selnl_notify_policyload(seqno);
  1353. selinux_netlbl_cache_invalidate();
  1354. selinux_xfrm_notify_policyload();
  1355. return 0;
  1356. }
  1357. #if 0
  1358. sidtab_hash_eval(&sidtab, "sids");
  1359. #endif
  1360. if (policydb_read(&newpolicydb, fp))
  1361. return -EINVAL;
  1362. if (sidtab_init(&newsidtab)) {
  1363. policydb_destroy(&newpolicydb);
  1364. return -ENOMEM;
  1365. }
  1366. /* Verify that the kernel defined classes are correct. */
  1367. if (validate_classes(&newpolicydb)) {
  1368. printk(KERN_ERR
  1369. "SELinux: the definition of a class is incorrect\n");
  1370. rc = -EINVAL;
  1371. goto err;
  1372. }
  1373. rc = security_preserve_bools(&newpolicydb);
  1374. if (rc) {
  1375. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1376. goto err;
  1377. }
  1378. /* Clone the SID table. */
  1379. sidtab_shutdown(&sidtab);
  1380. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1381. rc = -ENOMEM;
  1382. goto err;
  1383. }
  1384. /*
  1385. * Convert the internal representations of contexts
  1386. * in the new SID table.
  1387. */
  1388. args.oldp = &policydb;
  1389. args.newp = &newpolicydb;
  1390. rc = sidtab_map(&newsidtab, convert_context, &args);
  1391. if (rc)
  1392. goto err;
  1393. /* Save the old policydb and SID table to free later. */
  1394. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1395. sidtab_set(&oldsidtab, &sidtab);
  1396. /* Install the new policydb and SID table. */
  1397. write_lock_irq(&policy_rwlock);
  1398. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1399. sidtab_set(&sidtab, &newsidtab);
  1400. security_load_policycaps();
  1401. seqno = ++latest_granting;
  1402. policydb_loaded_version = policydb.policyvers;
  1403. write_unlock_irq(&policy_rwlock);
  1404. /* Free the old policydb and SID table. */
  1405. policydb_destroy(&oldpolicydb);
  1406. sidtab_destroy(&oldsidtab);
  1407. avc_ss_reset(seqno);
  1408. selnl_notify_policyload(seqno);
  1409. selinux_netlbl_cache_invalidate();
  1410. selinux_xfrm_notify_policyload();
  1411. return 0;
  1412. err:
  1413. sidtab_destroy(&newsidtab);
  1414. policydb_destroy(&newpolicydb);
  1415. return rc;
  1416. }
  1417. /**
  1418. * security_port_sid - Obtain the SID for a port.
  1419. * @protocol: protocol number
  1420. * @port: port number
  1421. * @out_sid: security identifier
  1422. */
  1423. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1424. {
  1425. struct ocontext *c;
  1426. int rc = 0;
  1427. read_lock(&policy_rwlock);
  1428. c = policydb.ocontexts[OCON_PORT];
  1429. while (c) {
  1430. if (c->u.port.protocol == protocol &&
  1431. c->u.port.low_port <= port &&
  1432. c->u.port.high_port >= port)
  1433. break;
  1434. c = c->next;
  1435. }
  1436. if (c) {
  1437. if (!c->sid[0]) {
  1438. rc = sidtab_context_to_sid(&sidtab,
  1439. &c->context[0],
  1440. &c->sid[0]);
  1441. if (rc)
  1442. goto out;
  1443. }
  1444. *out_sid = c->sid[0];
  1445. } else {
  1446. *out_sid = SECINITSID_PORT;
  1447. }
  1448. out:
  1449. read_unlock(&policy_rwlock);
  1450. return rc;
  1451. }
  1452. /**
  1453. * security_netif_sid - Obtain the SID for a network interface.
  1454. * @name: interface name
  1455. * @if_sid: interface SID
  1456. */
  1457. int security_netif_sid(char *name, u32 *if_sid)
  1458. {
  1459. int rc = 0;
  1460. struct ocontext *c;
  1461. read_lock(&policy_rwlock);
  1462. c = policydb.ocontexts[OCON_NETIF];
  1463. while (c) {
  1464. if (strcmp(name, c->u.name) == 0)
  1465. break;
  1466. c = c->next;
  1467. }
  1468. if (c) {
  1469. if (!c->sid[0] || !c->sid[1]) {
  1470. rc = sidtab_context_to_sid(&sidtab,
  1471. &c->context[0],
  1472. &c->sid[0]);
  1473. if (rc)
  1474. goto out;
  1475. rc = sidtab_context_to_sid(&sidtab,
  1476. &c->context[1],
  1477. &c->sid[1]);
  1478. if (rc)
  1479. goto out;
  1480. }
  1481. *if_sid = c->sid[0];
  1482. } else
  1483. *if_sid = SECINITSID_NETIF;
  1484. out:
  1485. read_unlock(&policy_rwlock);
  1486. return rc;
  1487. }
  1488. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1489. {
  1490. int i, fail = 0;
  1491. for (i = 0; i < 4; i++)
  1492. if (addr[i] != (input[i] & mask[i])) {
  1493. fail = 1;
  1494. break;
  1495. }
  1496. return !fail;
  1497. }
  1498. /**
  1499. * security_node_sid - Obtain the SID for a node (host).
  1500. * @domain: communication domain aka address family
  1501. * @addrp: address
  1502. * @addrlen: address length in bytes
  1503. * @out_sid: security identifier
  1504. */
  1505. int security_node_sid(u16 domain,
  1506. void *addrp,
  1507. u32 addrlen,
  1508. u32 *out_sid)
  1509. {
  1510. int rc = 0;
  1511. struct ocontext *c;
  1512. read_lock(&policy_rwlock);
  1513. switch (domain) {
  1514. case AF_INET: {
  1515. u32 addr;
  1516. if (addrlen != sizeof(u32)) {
  1517. rc = -EINVAL;
  1518. goto out;
  1519. }
  1520. addr = *((u32 *)addrp);
  1521. c = policydb.ocontexts[OCON_NODE];
  1522. while (c) {
  1523. if (c->u.node.addr == (addr & c->u.node.mask))
  1524. break;
  1525. c = c->next;
  1526. }
  1527. break;
  1528. }
  1529. case AF_INET6:
  1530. if (addrlen != sizeof(u64) * 2) {
  1531. rc = -EINVAL;
  1532. goto out;
  1533. }
  1534. c = policydb.ocontexts[OCON_NODE6];
  1535. while (c) {
  1536. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1537. c->u.node6.mask))
  1538. break;
  1539. c = c->next;
  1540. }
  1541. break;
  1542. default:
  1543. *out_sid = SECINITSID_NODE;
  1544. goto out;
  1545. }
  1546. if (c) {
  1547. if (!c->sid[0]) {
  1548. rc = sidtab_context_to_sid(&sidtab,
  1549. &c->context[0],
  1550. &c->sid[0]);
  1551. if (rc)
  1552. goto out;
  1553. }
  1554. *out_sid = c->sid[0];
  1555. } else {
  1556. *out_sid = SECINITSID_NODE;
  1557. }
  1558. out:
  1559. read_unlock(&policy_rwlock);
  1560. return rc;
  1561. }
  1562. #define SIDS_NEL 25
  1563. /**
  1564. * security_get_user_sids - Obtain reachable SIDs for a user.
  1565. * @fromsid: starting SID
  1566. * @username: username
  1567. * @sids: array of reachable SIDs for user
  1568. * @nel: number of elements in @sids
  1569. *
  1570. * Generate the set of SIDs for legal security contexts
  1571. * for a given user that can be reached by @fromsid.
  1572. * Set *@sids to point to a dynamically allocated
  1573. * array containing the set of SIDs. Set *@nel to the
  1574. * number of elements in the array.
  1575. */
  1576. int security_get_user_sids(u32 fromsid,
  1577. char *username,
  1578. u32 **sids,
  1579. u32 *nel)
  1580. {
  1581. struct context *fromcon, usercon;
  1582. u32 *mysids = NULL, *mysids2, sid;
  1583. u32 mynel = 0, maxnel = SIDS_NEL;
  1584. struct user_datum *user;
  1585. struct role_datum *role;
  1586. struct ebitmap_node *rnode, *tnode;
  1587. int rc = 0, i, j;
  1588. *sids = NULL;
  1589. *nel = 0;
  1590. if (!ss_initialized)
  1591. goto out;
  1592. read_lock(&policy_rwlock);
  1593. context_init(&usercon);
  1594. fromcon = sidtab_search(&sidtab, fromsid);
  1595. if (!fromcon) {
  1596. rc = -EINVAL;
  1597. goto out_unlock;
  1598. }
  1599. user = hashtab_search(policydb.p_users.table, username);
  1600. if (!user) {
  1601. rc = -EINVAL;
  1602. goto out_unlock;
  1603. }
  1604. usercon.user = user->value;
  1605. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1606. if (!mysids) {
  1607. rc = -ENOMEM;
  1608. goto out_unlock;
  1609. }
  1610. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  1611. role = policydb.role_val_to_struct[i];
  1612. usercon.role = i+1;
  1613. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  1614. usercon.type = j+1;
  1615. if (mls_setup_user_range(fromcon, user, &usercon))
  1616. continue;
  1617. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1618. if (rc)
  1619. goto out_unlock;
  1620. if (mynel < maxnel) {
  1621. mysids[mynel++] = sid;
  1622. } else {
  1623. maxnel += SIDS_NEL;
  1624. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1625. if (!mysids2) {
  1626. rc = -ENOMEM;
  1627. goto out_unlock;
  1628. }
  1629. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1630. kfree(mysids);
  1631. mysids = mysids2;
  1632. mysids[mynel++] = sid;
  1633. }
  1634. }
  1635. }
  1636. out_unlock:
  1637. read_unlock(&policy_rwlock);
  1638. if (rc || !mynel) {
  1639. kfree(mysids);
  1640. goto out;
  1641. }
  1642. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  1643. if (!mysids2) {
  1644. rc = -ENOMEM;
  1645. kfree(mysids);
  1646. goto out;
  1647. }
  1648. for (i = 0, j = 0; i < mynel; i++) {
  1649. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  1650. SECCLASS_PROCESS,
  1651. PROCESS__TRANSITION, AVC_STRICT,
  1652. NULL);
  1653. if (!rc)
  1654. mysids2[j++] = mysids[i];
  1655. cond_resched();
  1656. }
  1657. rc = 0;
  1658. kfree(mysids);
  1659. *sids = mysids2;
  1660. *nel = j;
  1661. out:
  1662. return rc;
  1663. }
  1664. /**
  1665. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1666. * @fstype: filesystem type
  1667. * @path: path from root of mount
  1668. * @sclass: file security class
  1669. * @sid: SID for path
  1670. *
  1671. * Obtain a SID to use for a file in a filesystem that
  1672. * cannot support xattr or use a fixed labeling behavior like
  1673. * transition SIDs or task SIDs.
  1674. */
  1675. int security_genfs_sid(const char *fstype,
  1676. char *path,
  1677. u16 sclass,
  1678. u32 *sid)
  1679. {
  1680. int len;
  1681. struct genfs *genfs;
  1682. struct ocontext *c;
  1683. int rc = 0, cmp = 0;
  1684. while (path[0] == '/' && path[1] == '/')
  1685. path++;
  1686. read_lock(&policy_rwlock);
  1687. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1688. cmp = strcmp(fstype, genfs->fstype);
  1689. if (cmp <= 0)
  1690. break;
  1691. }
  1692. if (!genfs || cmp) {
  1693. *sid = SECINITSID_UNLABELED;
  1694. rc = -ENOENT;
  1695. goto out;
  1696. }
  1697. for (c = genfs->head; c; c = c->next) {
  1698. len = strlen(c->u.name);
  1699. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1700. (strncmp(c->u.name, path, len) == 0))
  1701. break;
  1702. }
  1703. if (!c) {
  1704. *sid = SECINITSID_UNLABELED;
  1705. rc = -ENOENT;
  1706. goto out;
  1707. }
  1708. if (!c->sid[0]) {
  1709. rc = sidtab_context_to_sid(&sidtab,
  1710. &c->context[0],
  1711. &c->sid[0]);
  1712. if (rc)
  1713. goto out;
  1714. }
  1715. *sid = c->sid[0];
  1716. out:
  1717. read_unlock(&policy_rwlock);
  1718. return rc;
  1719. }
  1720. /**
  1721. * security_fs_use - Determine how to handle labeling for a filesystem.
  1722. * @fstype: filesystem type
  1723. * @behavior: labeling behavior
  1724. * @sid: SID for filesystem (superblock)
  1725. */
  1726. int security_fs_use(
  1727. const char *fstype,
  1728. unsigned int *behavior,
  1729. u32 *sid)
  1730. {
  1731. int rc = 0;
  1732. struct ocontext *c;
  1733. read_lock(&policy_rwlock);
  1734. c = policydb.ocontexts[OCON_FSUSE];
  1735. while (c) {
  1736. if (strcmp(fstype, c->u.name) == 0)
  1737. break;
  1738. c = c->next;
  1739. }
  1740. if (c) {
  1741. *behavior = c->v.behavior;
  1742. if (!c->sid[0]) {
  1743. rc = sidtab_context_to_sid(&sidtab,
  1744. &c->context[0],
  1745. &c->sid[0]);
  1746. if (rc)
  1747. goto out;
  1748. }
  1749. *sid = c->sid[0];
  1750. } else {
  1751. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1752. if (rc) {
  1753. *behavior = SECURITY_FS_USE_NONE;
  1754. rc = 0;
  1755. } else {
  1756. *behavior = SECURITY_FS_USE_GENFS;
  1757. }
  1758. }
  1759. out:
  1760. read_unlock(&policy_rwlock);
  1761. return rc;
  1762. }
  1763. int security_get_bools(int *len, char ***names, int **values)
  1764. {
  1765. int i, rc = -ENOMEM;
  1766. read_lock(&policy_rwlock);
  1767. *names = NULL;
  1768. *values = NULL;
  1769. *len = policydb.p_bools.nprim;
  1770. if (!*len) {
  1771. rc = 0;
  1772. goto out;
  1773. }
  1774. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  1775. if (!*names)
  1776. goto err;
  1777. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1778. if (!*values)
  1779. goto err;
  1780. for (i = 0; i < *len; i++) {
  1781. size_t name_len;
  1782. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1783. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1784. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1785. if (!(*names)[i])
  1786. goto err;
  1787. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1788. (*names)[i][name_len - 1] = 0;
  1789. }
  1790. rc = 0;
  1791. out:
  1792. read_unlock(&policy_rwlock);
  1793. return rc;
  1794. err:
  1795. if (*names) {
  1796. for (i = 0; i < *len; i++)
  1797. kfree((*names)[i]);
  1798. }
  1799. kfree(*values);
  1800. goto out;
  1801. }
  1802. int security_set_bools(int len, int *values)
  1803. {
  1804. int i, rc = 0;
  1805. int lenp, seqno = 0;
  1806. struct cond_node *cur;
  1807. write_lock_irq(&policy_rwlock);
  1808. lenp = policydb.p_bools.nprim;
  1809. if (len != lenp) {
  1810. rc = -EFAULT;
  1811. goto out;
  1812. }
  1813. for (i = 0; i < len; i++) {
  1814. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1815. audit_log(current->audit_context, GFP_ATOMIC,
  1816. AUDIT_MAC_CONFIG_CHANGE,
  1817. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  1818. policydb.p_bool_val_to_name[i],
  1819. !!values[i],
  1820. policydb.bool_val_to_struct[i]->state,
  1821. audit_get_loginuid(current),
  1822. audit_get_sessionid(current));
  1823. }
  1824. if (values[i])
  1825. policydb.bool_val_to_struct[i]->state = 1;
  1826. else
  1827. policydb.bool_val_to_struct[i]->state = 0;
  1828. }
  1829. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1830. rc = evaluate_cond_node(&policydb, cur);
  1831. if (rc)
  1832. goto out;
  1833. }
  1834. seqno = ++latest_granting;
  1835. out:
  1836. write_unlock_irq(&policy_rwlock);
  1837. if (!rc) {
  1838. avc_ss_reset(seqno);
  1839. selnl_notify_policyload(seqno);
  1840. selinux_xfrm_notify_policyload();
  1841. }
  1842. return rc;
  1843. }
  1844. int security_get_bool_value(int bool)
  1845. {
  1846. int rc = 0;
  1847. int len;
  1848. read_lock(&policy_rwlock);
  1849. len = policydb.p_bools.nprim;
  1850. if (bool >= len) {
  1851. rc = -EFAULT;
  1852. goto out;
  1853. }
  1854. rc = policydb.bool_val_to_struct[bool]->state;
  1855. out:
  1856. read_unlock(&policy_rwlock);
  1857. return rc;
  1858. }
  1859. static int security_preserve_bools(struct policydb *p)
  1860. {
  1861. int rc, nbools = 0, *bvalues = NULL, i;
  1862. char **bnames = NULL;
  1863. struct cond_bool_datum *booldatum;
  1864. struct cond_node *cur;
  1865. rc = security_get_bools(&nbools, &bnames, &bvalues);
  1866. if (rc)
  1867. goto out;
  1868. for (i = 0; i < nbools; i++) {
  1869. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  1870. if (booldatum)
  1871. booldatum->state = bvalues[i];
  1872. }
  1873. for (cur = p->cond_list; cur != NULL; cur = cur->next) {
  1874. rc = evaluate_cond_node(p, cur);
  1875. if (rc)
  1876. goto out;
  1877. }
  1878. out:
  1879. if (bnames) {
  1880. for (i = 0; i < nbools; i++)
  1881. kfree(bnames[i]);
  1882. }
  1883. kfree(bnames);
  1884. kfree(bvalues);
  1885. return rc;
  1886. }
  1887. /*
  1888. * security_sid_mls_copy() - computes a new sid based on the given
  1889. * sid and the mls portion of mls_sid.
  1890. */
  1891. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1892. {
  1893. struct context *context1;
  1894. struct context *context2;
  1895. struct context newcon;
  1896. char *s;
  1897. u32 len;
  1898. int rc = 0;
  1899. if (!ss_initialized || !selinux_mls_enabled) {
  1900. *new_sid = sid;
  1901. goto out;
  1902. }
  1903. context_init(&newcon);
  1904. read_lock(&policy_rwlock);
  1905. context1 = sidtab_search(&sidtab, sid);
  1906. if (!context1) {
  1907. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1908. __func__, sid);
  1909. rc = -EINVAL;
  1910. goto out_unlock;
  1911. }
  1912. context2 = sidtab_search(&sidtab, mls_sid);
  1913. if (!context2) {
  1914. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1915. __func__, mls_sid);
  1916. rc = -EINVAL;
  1917. goto out_unlock;
  1918. }
  1919. newcon.user = context1->user;
  1920. newcon.role = context1->role;
  1921. newcon.type = context1->type;
  1922. rc = mls_context_cpy(&newcon, context2);
  1923. if (rc)
  1924. goto out_unlock;
  1925. /* Check the validity of the new context. */
  1926. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1927. rc = convert_context_handle_invalid_context(&newcon);
  1928. if (rc)
  1929. goto bad;
  1930. }
  1931. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1932. goto out_unlock;
  1933. bad:
  1934. if (!context_struct_to_string(&newcon, &s, &len)) {
  1935. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1936. "security_sid_mls_copy: invalid context %s", s);
  1937. kfree(s);
  1938. }
  1939. out_unlock:
  1940. read_unlock(&policy_rwlock);
  1941. context_destroy(&newcon);
  1942. out:
  1943. return rc;
  1944. }
  1945. /**
  1946. * security_net_peersid_resolve - Compare and resolve two network peer SIDs
  1947. * @nlbl_sid: NetLabel SID
  1948. * @nlbl_type: NetLabel labeling protocol type
  1949. * @xfrm_sid: XFRM SID
  1950. *
  1951. * Description:
  1952. * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
  1953. * resolved into a single SID it is returned via @peer_sid and the function
  1954. * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
  1955. * returns a negative value. A table summarizing the behavior is below:
  1956. *
  1957. * | function return | @sid
  1958. * ------------------------------+-----------------+-----------------
  1959. * no peer labels | 0 | SECSID_NULL
  1960. * single peer label | 0 | <peer_label>
  1961. * multiple, consistent labels | 0 | <peer_label>
  1962. * multiple, inconsistent labels | -<errno> | SECSID_NULL
  1963. *
  1964. */
  1965. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  1966. u32 xfrm_sid,
  1967. u32 *peer_sid)
  1968. {
  1969. int rc;
  1970. struct context *nlbl_ctx;
  1971. struct context *xfrm_ctx;
  1972. /* handle the common (which also happens to be the set of easy) cases
  1973. * right away, these two if statements catch everything involving a
  1974. * single or absent peer SID/label */
  1975. if (xfrm_sid == SECSID_NULL) {
  1976. *peer_sid = nlbl_sid;
  1977. return 0;
  1978. }
  1979. /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
  1980. * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
  1981. * is present */
  1982. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  1983. *peer_sid = xfrm_sid;
  1984. return 0;
  1985. }
  1986. /* we don't need to check ss_initialized here since the only way both
  1987. * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
  1988. * security server was initialized and ss_initialized was true */
  1989. if (!selinux_mls_enabled) {
  1990. *peer_sid = SECSID_NULL;
  1991. return 0;
  1992. }
  1993. read_lock(&policy_rwlock);
  1994. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  1995. if (!nlbl_ctx) {
  1996. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1997. __func__, nlbl_sid);
  1998. rc = -EINVAL;
  1999. goto out_slowpath;
  2000. }
  2001. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  2002. if (!xfrm_ctx) {
  2003. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2004. __func__, xfrm_sid);
  2005. rc = -EINVAL;
  2006. goto out_slowpath;
  2007. }
  2008. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  2009. out_slowpath:
  2010. read_unlock(&policy_rwlock);
  2011. if (rc == 0)
  2012. /* at present NetLabel SIDs/labels really only carry MLS
  2013. * information so if the MLS portion of the NetLabel SID
  2014. * matches the MLS portion of the labeled XFRM SID/label
  2015. * then pass along the XFRM SID as it is the most
  2016. * expressive */
  2017. *peer_sid = xfrm_sid;
  2018. else
  2019. *peer_sid = SECSID_NULL;
  2020. return rc;
  2021. }
  2022. static int get_classes_callback(void *k, void *d, void *args)
  2023. {
  2024. struct class_datum *datum = d;
  2025. char *name = k, **classes = args;
  2026. int value = datum->value - 1;
  2027. classes[value] = kstrdup(name, GFP_ATOMIC);
  2028. if (!classes[value])
  2029. return -ENOMEM;
  2030. return 0;
  2031. }
  2032. int security_get_classes(char ***classes, int *nclasses)
  2033. {
  2034. int rc = -ENOMEM;
  2035. read_lock(&policy_rwlock);
  2036. *nclasses = policydb.p_classes.nprim;
  2037. *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
  2038. if (!*classes)
  2039. goto out;
  2040. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  2041. *classes);
  2042. if (rc < 0) {
  2043. int i;
  2044. for (i = 0; i < *nclasses; i++)
  2045. kfree((*classes)[i]);
  2046. kfree(*classes);
  2047. }
  2048. out:
  2049. read_unlock(&policy_rwlock);
  2050. return rc;
  2051. }
  2052. static int get_permissions_callback(void *k, void *d, void *args)
  2053. {
  2054. struct perm_datum *datum = d;
  2055. char *name = k, **perms = args;
  2056. int value = datum->value - 1;
  2057. perms[value] = kstrdup(name, GFP_ATOMIC);
  2058. if (!perms[value])
  2059. return -ENOMEM;
  2060. return 0;
  2061. }
  2062. int security_get_permissions(char *class, char ***perms, int *nperms)
  2063. {
  2064. int rc = -ENOMEM, i;
  2065. struct class_datum *match;
  2066. read_lock(&policy_rwlock);
  2067. match = hashtab_search(policydb.p_classes.table, class);
  2068. if (!match) {
  2069. printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
  2070. __func__, class);
  2071. rc = -EINVAL;
  2072. goto out;
  2073. }
  2074. *nperms = match->permissions.nprim;
  2075. *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
  2076. if (!*perms)
  2077. goto out;
  2078. if (match->comdatum) {
  2079. rc = hashtab_map(match->comdatum->permissions.table,
  2080. get_permissions_callback, *perms);
  2081. if (rc < 0)
  2082. goto err;
  2083. }
  2084. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  2085. *perms);
  2086. if (rc < 0)
  2087. goto err;
  2088. out:
  2089. read_unlock(&policy_rwlock);
  2090. return rc;
  2091. err:
  2092. read_unlock(&policy_rwlock);
  2093. for (i = 0; i < *nperms; i++)
  2094. kfree((*perms)[i]);
  2095. kfree(*perms);
  2096. return rc;
  2097. }
  2098. int security_get_reject_unknown(void)
  2099. {
  2100. return policydb.reject_unknown;
  2101. }
  2102. int security_get_allow_unknown(void)
  2103. {
  2104. return policydb.allow_unknown;
  2105. }
  2106. /**
  2107. * security_policycap_supported - Check for a specific policy capability
  2108. * @req_cap: capability
  2109. *
  2110. * Description:
  2111. * This function queries the currently loaded policy to see if it supports the
  2112. * capability specified by @req_cap. Returns true (1) if the capability is
  2113. * supported, false (0) if it isn't supported.
  2114. *
  2115. */
  2116. int security_policycap_supported(unsigned int req_cap)
  2117. {
  2118. int rc;
  2119. read_lock(&policy_rwlock);
  2120. rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
  2121. read_unlock(&policy_rwlock);
  2122. return rc;
  2123. }
  2124. struct selinux_audit_rule {
  2125. u32 au_seqno;
  2126. struct context au_ctxt;
  2127. };
  2128. void selinux_audit_rule_free(void *vrule)
  2129. {
  2130. struct selinux_audit_rule *rule = vrule;
  2131. if (rule) {
  2132. context_destroy(&rule->au_ctxt);
  2133. kfree(rule);
  2134. }
  2135. }
  2136. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2137. {
  2138. struct selinux_audit_rule *tmprule;
  2139. struct role_datum *roledatum;
  2140. struct type_datum *typedatum;
  2141. struct user_datum *userdatum;
  2142. struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
  2143. int rc = 0;
  2144. *rule = NULL;
  2145. if (!ss_initialized)
  2146. return -EOPNOTSUPP;
  2147. switch (field) {
  2148. case AUDIT_SUBJ_USER:
  2149. case AUDIT_SUBJ_ROLE:
  2150. case AUDIT_SUBJ_TYPE:
  2151. case AUDIT_OBJ_USER:
  2152. case AUDIT_OBJ_ROLE:
  2153. case AUDIT_OBJ_TYPE:
  2154. /* only 'equals' and 'not equals' fit user, role, and type */
  2155. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  2156. return -EINVAL;
  2157. break;
  2158. case AUDIT_SUBJ_SEN:
  2159. case AUDIT_SUBJ_CLR:
  2160. case AUDIT_OBJ_LEV_LOW:
  2161. case AUDIT_OBJ_LEV_HIGH:
  2162. /* we do not allow a range, indicated by the presense of '-' */
  2163. if (strchr(rulestr, '-'))
  2164. return -EINVAL;
  2165. break;
  2166. default:
  2167. /* only the above fields are valid */
  2168. return -EINVAL;
  2169. }
  2170. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  2171. if (!tmprule)
  2172. return -ENOMEM;
  2173. context_init(&tmprule->au_ctxt);
  2174. read_lock(&policy_rwlock);
  2175. tmprule->au_seqno = latest_granting;
  2176. switch (field) {
  2177. case AUDIT_SUBJ_USER:
  2178. case AUDIT_OBJ_USER:
  2179. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  2180. if (!userdatum)
  2181. rc = -EINVAL;
  2182. else
  2183. tmprule->au_ctxt.user = userdatum->value;
  2184. break;
  2185. case AUDIT_SUBJ_ROLE:
  2186. case AUDIT_OBJ_ROLE:
  2187. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  2188. if (!roledatum)
  2189. rc = -EINVAL;
  2190. else
  2191. tmprule->au_ctxt.role = roledatum->value;
  2192. break;
  2193. case AUDIT_SUBJ_TYPE:
  2194. case AUDIT_OBJ_TYPE:
  2195. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  2196. if (!typedatum)
  2197. rc = -EINVAL;
  2198. else
  2199. tmprule->au_ctxt.type = typedatum->value;
  2200. break;
  2201. case AUDIT_SUBJ_SEN:
  2202. case AUDIT_SUBJ_CLR:
  2203. case AUDIT_OBJ_LEV_LOW:
  2204. case AUDIT_OBJ_LEV_HIGH:
  2205. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  2206. break;
  2207. }
  2208. read_unlock(&policy_rwlock);
  2209. if (rc) {
  2210. selinux_audit_rule_free(tmprule);
  2211. tmprule = NULL;
  2212. }
  2213. *rule = tmprule;
  2214. return rc;
  2215. }
  2216. /* Check to see if the rule contains any selinux fields */
  2217. int selinux_audit_rule_known(struct audit_krule *rule)
  2218. {
  2219. int i;
  2220. for (i = 0; i < rule->field_count; i++) {
  2221. struct audit_field *f = &rule->fields[i];
  2222. switch (f->type) {
  2223. case AUDIT_SUBJ_USER:
  2224. case AUDIT_SUBJ_ROLE:
  2225. case AUDIT_SUBJ_TYPE:
  2226. case AUDIT_SUBJ_SEN:
  2227. case AUDIT_SUBJ_CLR:
  2228. case AUDIT_OBJ_USER:
  2229. case AUDIT_OBJ_ROLE:
  2230. case AUDIT_OBJ_TYPE:
  2231. case AUDIT_OBJ_LEV_LOW:
  2232. case AUDIT_OBJ_LEV_HIGH:
  2233. return 1;
  2234. }
  2235. }
  2236. return 0;
  2237. }
  2238. int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  2239. struct audit_context *actx)
  2240. {
  2241. struct context *ctxt;
  2242. struct mls_level *level;
  2243. struct selinux_audit_rule *rule = vrule;
  2244. int match = 0;
  2245. if (!rule) {
  2246. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2247. "selinux_audit_rule_match: missing rule\n");
  2248. return -ENOENT;
  2249. }
  2250. read_lock(&policy_rwlock);
  2251. if (rule->au_seqno < latest_granting) {
  2252. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2253. "selinux_audit_rule_match: stale rule\n");
  2254. match = -ESTALE;
  2255. goto out;
  2256. }
  2257. ctxt = sidtab_search(&sidtab, sid);
  2258. if (!ctxt) {
  2259. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2260. "selinux_audit_rule_match: unrecognized SID %d\n",
  2261. sid);
  2262. match = -ENOENT;
  2263. goto out;
  2264. }
  2265. /* a field/op pair that is not caught here will simply fall through
  2266. without a match */
  2267. switch (field) {
  2268. case AUDIT_SUBJ_USER:
  2269. case AUDIT_OBJ_USER:
  2270. switch (op) {
  2271. case AUDIT_EQUAL:
  2272. match = (ctxt->user == rule->au_ctxt.user);
  2273. break;
  2274. case AUDIT_NOT_EQUAL:
  2275. match = (ctxt->user != rule->au_ctxt.user);
  2276. break;
  2277. }
  2278. break;
  2279. case AUDIT_SUBJ_ROLE:
  2280. case AUDIT_OBJ_ROLE:
  2281. switch (op) {
  2282. case AUDIT_EQUAL:
  2283. match = (ctxt->role == rule->au_ctxt.role);
  2284. break;
  2285. case AUDIT_NOT_EQUAL:
  2286. match = (ctxt->role != rule->au_ctxt.role);
  2287. break;
  2288. }
  2289. break;
  2290. case AUDIT_SUBJ_TYPE:
  2291. case AUDIT_OBJ_TYPE:
  2292. switch (op) {
  2293. case AUDIT_EQUAL:
  2294. match = (ctxt->type == rule->au_ctxt.type);
  2295. break;
  2296. case AUDIT_NOT_EQUAL:
  2297. match = (ctxt->type != rule->au_ctxt.type);
  2298. break;
  2299. }
  2300. break;
  2301. case AUDIT_SUBJ_SEN:
  2302. case AUDIT_SUBJ_CLR:
  2303. case AUDIT_OBJ_LEV_LOW:
  2304. case AUDIT_OBJ_LEV_HIGH:
  2305. level = ((field == AUDIT_SUBJ_SEN ||
  2306. field == AUDIT_OBJ_LEV_LOW) ?
  2307. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2308. switch (op) {
  2309. case AUDIT_EQUAL:
  2310. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2311. level);
  2312. break;
  2313. case AUDIT_NOT_EQUAL:
  2314. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2315. level);
  2316. break;
  2317. case AUDIT_LESS_THAN:
  2318. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2319. level) &&
  2320. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2321. level));
  2322. break;
  2323. case AUDIT_LESS_THAN_OR_EQUAL:
  2324. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2325. level);
  2326. break;
  2327. case AUDIT_GREATER_THAN:
  2328. match = (mls_level_dom(level,
  2329. &rule->au_ctxt.range.level[0]) &&
  2330. !mls_level_eq(level,
  2331. &rule->au_ctxt.range.level[0]));
  2332. break;
  2333. case AUDIT_GREATER_THAN_OR_EQUAL:
  2334. match = mls_level_dom(level,
  2335. &rule->au_ctxt.range.level[0]);
  2336. break;
  2337. }
  2338. }
  2339. out:
  2340. read_unlock(&policy_rwlock);
  2341. return match;
  2342. }
  2343. static int (*aurule_callback)(void) = audit_update_lsm_rules;
  2344. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  2345. u16 class, u32 perms, u32 *retained)
  2346. {
  2347. int err = 0;
  2348. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2349. err = aurule_callback();
  2350. return err;
  2351. }
  2352. static int __init aurule_init(void)
  2353. {
  2354. int err;
  2355. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  2356. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  2357. if (err)
  2358. panic("avc_add_callback() failed, error %d\n", err);
  2359. return err;
  2360. }
  2361. __initcall(aurule_init);
  2362. #ifdef CONFIG_NETLABEL
  2363. /**
  2364. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2365. * @secattr: the NetLabel packet security attributes
  2366. * @sid: the SELinux SID
  2367. *
  2368. * Description:
  2369. * Attempt to cache the context in @ctx, which was derived from the packet in
  2370. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2371. * already been initialized.
  2372. *
  2373. */
  2374. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2375. u32 sid)
  2376. {
  2377. u32 *sid_cache;
  2378. sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
  2379. if (sid_cache == NULL)
  2380. return;
  2381. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2382. if (secattr->cache == NULL) {
  2383. kfree(sid_cache);
  2384. return;
  2385. }
  2386. *sid_cache = sid;
  2387. secattr->cache->free = kfree;
  2388. secattr->cache->data = sid_cache;
  2389. secattr->flags |= NETLBL_SECATTR_CACHE;
  2390. }
  2391. /**
  2392. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2393. * @secattr: the NetLabel packet security attributes
  2394. * @sid: the SELinux SID
  2395. *
  2396. * Description:
  2397. * Convert the given NetLabel security attributes in @secattr into a
  2398. * SELinux SID. If the @secattr field does not contain a full SELinux
  2399. * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
  2400. * 'cache' field of @secattr is set and the CACHE flag is set; this is to
  2401. * allow the @secattr to be used by NetLabel to cache the secattr to SID
  2402. * conversion for future lookups. Returns zero on success, negative values on
  2403. * failure.
  2404. *
  2405. */
  2406. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2407. u32 *sid)
  2408. {
  2409. int rc = -EIDRM;
  2410. struct context *ctx;
  2411. struct context ctx_new;
  2412. if (!ss_initialized) {
  2413. *sid = SECSID_NULL;
  2414. return 0;
  2415. }
  2416. read_lock(&policy_rwlock);
  2417. if (secattr->flags & NETLBL_SECATTR_CACHE) {
  2418. *sid = *(u32 *)secattr->cache->data;
  2419. rc = 0;
  2420. } else if (secattr->flags & NETLBL_SECATTR_SECID) {
  2421. *sid = secattr->attr.secid;
  2422. rc = 0;
  2423. } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2424. ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
  2425. if (ctx == NULL)
  2426. goto netlbl_secattr_to_sid_return;
  2427. ctx_new.user = ctx->user;
  2428. ctx_new.role = ctx->role;
  2429. ctx_new.type = ctx->type;
  2430. mls_import_netlbl_lvl(&ctx_new, secattr);
  2431. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2432. if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2433. secattr->attr.mls.cat) != 0)
  2434. goto netlbl_secattr_to_sid_return;
  2435. ctx_new.range.level[1].cat.highbit =
  2436. ctx_new.range.level[0].cat.highbit;
  2437. ctx_new.range.level[1].cat.node =
  2438. ctx_new.range.level[0].cat.node;
  2439. } else {
  2440. ebitmap_init(&ctx_new.range.level[0].cat);
  2441. ebitmap_init(&ctx_new.range.level[1].cat);
  2442. }
  2443. if (mls_context_isvalid(&policydb, &ctx_new) != 1)
  2444. goto netlbl_secattr_to_sid_return_cleanup;
  2445. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2446. if (rc != 0)
  2447. goto netlbl_secattr_to_sid_return_cleanup;
  2448. security_netlbl_cache_add(secattr, *sid);
  2449. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2450. } else {
  2451. *sid = SECSID_NULL;
  2452. rc = 0;
  2453. }
  2454. netlbl_secattr_to_sid_return:
  2455. read_unlock(&policy_rwlock);
  2456. return rc;
  2457. netlbl_secattr_to_sid_return_cleanup:
  2458. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2459. goto netlbl_secattr_to_sid_return;
  2460. }
  2461. /**
  2462. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2463. * @sid: the SELinux SID
  2464. * @secattr: the NetLabel packet security attributes
  2465. *
  2466. * Description:
  2467. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2468. * Returns zero on success, negative values on failure.
  2469. *
  2470. */
  2471. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2472. {
  2473. int rc = -ENOENT;
  2474. struct context *ctx;
  2475. if (!ss_initialized)
  2476. return 0;
  2477. read_lock(&policy_rwlock);
  2478. ctx = sidtab_search(&sidtab, sid);
  2479. if (ctx == NULL)
  2480. goto netlbl_sid_to_secattr_failure;
  2481. secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
  2482. GFP_ATOMIC);
  2483. secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
  2484. mls_export_netlbl_lvl(ctx, secattr);
  2485. rc = mls_export_netlbl_cat(ctx, secattr);
  2486. if (rc != 0)
  2487. goto netlbl_sid_to_secattr_failure;
  2488. read_unlock(&policy_rwlock);
  2489. return 0;
  2490. netlbl_sid_to_secattr_failure:
  2491. read_unlock(&policy_rwlock);
  2492. return rc;
  2493. }
  2494. #endif /* CONFIG_NETLABEL */