services.c 66 KB

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