services.c 62 KB

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