services.c 60 KB

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