services.c 63 KB

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