services.c 63 KB

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