services.c 66 KB

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