services.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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. selinux_xfrm_notify_policyload();
  1184. return 0;
  1185. }
  1186. #if 0
  1187. sidtab_hash_eval(&sidtab, "sids");
  1188. #endif
  1189. if (policydb_read(&newpolicydb, fp)) {
  1190. LOAD_UNLOCK;
  1191. return -EINVAL;
  1192. }
  1193. sidtab_init(&newsidtab);
  1194. /* Verify that the kernel defined classes are correct. */
  1195. if (validate_classes(&newpolicydb)) {
  1196. printk(KERN_ERR
  1197. "security: the definition of a class is incorrect\n");
  1198. rc = -EINVAL;
  1199. goto err;
  1200. }
  1201. /* Clone the SID table. */
  1202. sidtab_shutdown(&sidtab);
  1203. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1204. rc = -ENOMEM;
  1205. goto err;
  1206. }
  1207. /* Convert the internal representations of contexts
  1208. in the new SID table and remove invalid SIDs. */
  1209. args.oldp = &policydb;
  1210. args.newp = &newpolicydb;
  1211. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1212. /* Save the old policydb and SID table to free later. */
  1213. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1214. sidtab_set(&oldsidtab, &sidtab);
  1215. /* Install the new policydb and SID table. */
  1216. POLICY_WRLOCK;
  1217. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1218. sidtab_set(&sidtab, &newsidtab);
  1219. seqno = ++latest_granting;
  1220. policydb_loaded_version = policydb.policyvers;
  1221. POLICY_WRUNLOCK;
  1222. LOAD_UNLOCK;
  1223. /* Free the old policydb and SID table. */
  1224. policydb_destroy(&oldpolicydb);
  1225. sidtab_destroy(&oldsidtab);
  1226. avc_ss_reset(seqno);
  1227. selnl_notify_policyload(seqno);
  1228. selinux_netlbl_cache_invalidate();
  1229. selinux_xfrm_notify_policyload();
  1230. return 0;
  1231. err:
  1232. LOAD_UNLOCK;
  1233. sidtab_destroy(&newsidtab);
  1234. policydb_destroy(&newpolicydb);
  1235. return rc;
  1236. }
  1237. /**
  1238. * security_port_sid - Obtain the SID for a port.
  1239. * @domain: communication domain aka address family
  1240. * @type: socket type
  1241. * @protocol: protocol number
  1242. * @port: port number
  1243. * @out_sid: security identifier
  1244. */
  1245. int security_port_sid(u16 domain,
  1246. u16 type,
  1247. u8 protocol,
  1248. u16 port,
  1249. u32 *out_sid)
  1250. {
  1251. struct ocontext *c;
  1252. int rc = 0;
  1253. POLICY_RDLOCK;
  1254. c = policydb.ocontexts[OCON_PORT];
  1255. while (c) {
  1256. if (c->u.port.protocol == protocol &&
  1257. c->u.port.low_port <= port &&
  1258. c->u.port.high_port >= port)
  1259. break;
  1260. c = c->next;
  1261. }
  1262. if (c) {
  1263. if (!c->sid[0]) {
  1264. rc = sidtab_context_to_sid(&sidtab,
  1265. &c->context[0],
  1266. &c->sid[0]);
  1267. if (rc)
  1268. goto out;
  1269. }
  1270. *out_sid = c->sid[0];
  1271. } else {
  1272. *out_sid = SECINITSID_PORT;
  1273. }
  1274. out:
  1275. POLICY_RDUNLOCK;
  1276. return rc;
  1277. }
  1278. /**
  1279. * security_netif_sid - Obtain the SID for a network interface.
  1280. * @name: interface name
  1281. * @if_sid: interface SID
  1282. * @msg_sid: default SID for received packets
  1283. */
  1284. int security_netif_sid(char *name,
  1285. u32 *if_sid,
  1286. u32 *msg_sid)
  1287. {
  1288. int rc = 0;
  1289. struct ocontext *c;
  1290. POLICY_RDLOCK;
  1291. c = policydb.ocontexts[OCON_NETIF];
  1292. while (c) {
  1293. if (strcmp(name, c->u.name) == 0)
  1294. break;
  1295. c = c->next;
  1296. }
  1297. if (c) {
  1298. if (!c->sid[0] || !c->sid[1]) {
  1299. rc = sidtab_context_to_sid(&sidtab,
  1300. &c->context[0],
  1301. &c->sid[0]);
  1302. if (rc)
  1303. goto out;
  1304. rc = sidtab_context_to_sid(&sidtab,
  1305. &c->context[1],
  1306. &c->sid[1]);
  1307. if (rc)
  1308. goto out;
  1309. }
  1310. *if_sid = c->sid[0];
  1311. *msg_sid = c->sid[1];
  1312. } else {
  1313. *if_sid = SECINITSID_NETIF;
  1314. *msg_sid = SECINITSID_NETMSG;
  1315. }
  1316. out:
  1317. POLICY_RDUNLOCK;
  1318. return rc;
  1319. }
  1320. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1321. {
  1322. int i, fail = 0;
  1323. for(i = 0; i < 4; i++)
  1324. if(addr[i] != (input[i] & mask[i])) {
  1325. fail = 1;
  1326. break;
  1327. }
  1328. return !fail;
  1329. }
  1330. /**
  1331. * security_node_sid - Obtain the SID for a node (host).
  1332. * @domain: communication domain aka address family
  1333. * @addrp: address
  1334. * @addrlen: address length in bytes
  1335. * @out_sid: security identifier
  1336. */
  1337. int security_node_sid(u16 domain,
  1338. void *addrp,
  1339. u32 addrlen,
  1340. u32 *out_sid)
  1341. {
  1342. int rc = 0;
  1343. struct ocontext *c;
  1344. POLICY_RDLOCK;
  1345. switch (domain) {
  1346. case AF_INET: {
  1347. u32 addr;
  1348. if (addrlen != sizeof(u32)) {
  1349. rc = -EINVAL;
  1350. goto out;
  1351. }
  1352. addr = *((u32 *)addrp);
  1353. c = policydb.ocontexts[OCON_NODE];
  1354. while (c) {
  1355. if (c->u.node.addr == (addr & c->u.node.mask))
  1356. break;
  1357. c = c->next;
  1358. }
  1359. break;
  1360. }
  1361. case AF_INET6:
  1362. if (addrlen != sizeof(u64) * 2) {
  1363. rc = -EINVAL;
  1364. goto out;
  1365. }
  1366. c = policydb.ocontexts[OCON_NODE6];
  1367. while (c) {
  1368. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1369. c->u.node6.mask))
  1370. break;
  1371. c = c->next;
  1372. }
  1373. break;
  1374. default:
  1375. *out_sid = SECINITSID_NODE;
  1376. goto out;
  1377. }
  1378. if (c) {
  1379. if (!c->sid[0]) {
  1380. rc = sidtab_context_to_sid(&sidtab,
  1381. &c->context[0],
  1382. &c->sid[0]);
  1383. if (rc)
  1384. goto out;
  1385. }
  1386. *out_sid = c->sid[0];
  1387. } else {
  1388. *out_sid = SECINITSID_NODE;
  1389. }
  1390. out:
  1391. POLICY_RDUNLOCK;
  1392. return rc;
  1393. }
  1394. #define SIDS_NEL 25
  1395. /**
  1396. * security_get_user_sids - Obtain reachable SIDs for a user.
  1397. * @fromsid: starting SID
  1398. * @username: username
  1399. * @sids: array of reachable SIDs for user
  1400. * @nel: number of elements in @sids
  1401. *
  1402. * Generate the set of SIDs for legal security contexts
  1403. * for a given user that can be reached by @fromsid.
  1404. * Set *@sids to point to a dynamically allocated
  1405. * array containing the set of SIDs. Set *@nel to the
  1406. * number of elements in the array.
  1407. */
  1408. int security_get_user_sids(u32 fromsid,
  1409. char *username,
  1410. u32 **sids,
  1411. u32 *nel)
  1412. {
  1413. struct context *fromcon, usercon;
  1414. u32 *mysids, *mysids2, sid;
  1415. u32 mynel = 0, maxnel = SIDS_NEL;
  1416. struct user_datum *user;
  1417. struct role_datum *role;
  1418. struct av_decision avd;
  1419. struct ebitmap_node *rnode, *tnode;
  1420. int rc = 0, i, j;
  1421. if (!ss_initialized) {
  1422. *sids = NULL;
  1423. *nel = 0;
  1424. goto out;
  1425. }
  1426. POLICY_RDLOCK;
  1427. fromcon = sidtab_search(&sidtab, fromsid);
  1428. if (!fromcon) {
  1429. rc = -EINVAL;
  1430. goto out_unlock;
  1431. }
  1432. user = hashtab_search(policydb.p_users.table, username);
  1433. if (!user) {
  1434. rc = -EINVAL;
  1435. goto out_unlock;
  1436. }
  1437. usercon.user = user->value;
  1438. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1439. if (!mysids) {
  1440. rc = -ENOMEM;
  1441. goto out_unlock;
  1442. }
  1443. ebitmap_for_each_bit(&user->roles, rnode, i) {
  1444. if (!ebitmap_node_get_bit(rnode, i))
  1445. continue;
  1446. role = policydb.role_val_to_struct[i];
  1447. usercon.role = i+1;
  1448. ebitmap_for_each_bit(&role->types, tnode, j) {
  1449. if (!ebitmap_node_get_bit(tnode, j))
  1450. continue;
  1451. usercon.type = j+1;
  1452. if (mls_setup_user_range(fromcon, user, &usercon))
  1453. continue;
  1454. rc = context_struct_compute_av(fromcon, &usercon,
  1455. SECCLASS_PROCESS,
  1456. PROCESS__TRANSITION,
  1457. &avd);
  1458. if (rc || !(avd.allowed & PROCESS__TRANSITION))
  1459. continue;
  1460. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1461. if (rc) {
  1462. kfree(mysids);
  1463. goto out_unlock;
  1464. }
  1465. if (mynel < maxnel) {
  1466. mysids[mynel++] = sid;
  1467. } else {
  1468. maxnel += SIDS_NEL;
  1469. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1470. if (!mysids2) {
  1471. rc = -ENOMEM;
  1472. kfree(mysids);
  1473. goto out_unlock;
  1474. }
  1475. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1476. kfree(mysids);
  1477. mysids = mysids2;
  1478. mysids[mynel++] = sid;
  1479. }
  1480. }
  1481. }
  1482. *sids = mysids;
  1483. *nel = mynel;
  1484. out_unlock:
  1485. POLICY_RDUNLOCK;
  1486. out:
  1487. return rc;
  1488. }
  1489. /**
  1490. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1491. * @fstype: filesystem type
  1492. * @path: path from root of mount
  1493. * @sclass: file security class
  1494. * @sid: SID for path
  1495. *
  1496. * Obtain a SID to use for a file in a filesystem that
  1497. * cannot support xattr or use a fixed labeling behavior like
  1498. * transition SIDs or task SIDs.
  1499. */
  1500. int security_genfs_sid(const char *fstype,
  1501. char *path,
  1502. u16 sclass,
  1503. u32 *sid)
  1504. {
  1505. int len;
  1506. struct genfs *genfs;
  1507. struct ocontext *c;
  1508. int rc = 0, cmp = 0;
  1509. POLICY_RDLOCK;
  1510. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1511. cmp = strcmp(fstype, genfs->fstype);
  1512. if (cmp <= 0)
  1513. break;
  1514. }
  1515. if (!genfs || cmp) {
  1516. *sid = SECINITSID_UNLABELED;
  1517. rc = -ENOENT;
  1518. goto out;
  1519. }
  1520. for (c = genfs->head; c; c = c->next) {
  1521. len = strlen(c->u.name);
  1522. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1523. (strncmp(c->u.name, path, len) == 0))
  1524. break;
  1525. }
  1526. if (!c) {
  1527. *sid = SECINITSID_UNLABELED;
  1528. rc = -ENOENT;
  1529. goto out;
  1530. }
  1531. if (!c->sid[0]) {
  1532. rc = sidtab_context_to_sid(&sidtab,
  1533. &c->context[0],
  1534. &c->sid[0]);
  1535. if (rc)
  1536. goto out;
  1537. }
  1538. *sid = c->sid[0];
  1539. out:
  1540. POLICY_RDUNLOCK;
  1541. return rc;
  1542. }
  1543. /**
  1544. * security_fs_use - Determine how to handle labeling for a filesystem.
  1545. * @fstype: filesystem type
  1546. * @behavior: labeling behavior
  1547. * @sid: SID for filesystem (superblock)
  1548. */
  1549. int security_fs_use(
  1550. const char *fstype,
  1551. unsigned int *behavior,
  1552. u32 *sid)
  1553. {
  1554. int rc = 0;
  1555. struct ocontext *c;
  1556. POLICY_RDLOCK;
  1557. c = policydb.ocontexts[OCON_FSUSE];
  1558. while (c) {
  1559. if (strcmp(fstype, c->u.name) == 0)
  1560. break;
  1561. c = c->next;
  1562. }
  1563. if (c) {
  1564. *behavior = c->v.behavior;
  1565. if (!c->sid[0]) {
  1566. rc = sidtab_context_to_sid(&sidtab,
  1567. &c->context[0],
  1568. &c->sid[0]);
  1569. if (rc)
  1570. goto out;
  1571. }
  1572. *sid = c->sid[0];
  1573. } else {
  1574. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1575. if (rc) {
  1576. *behavior = SECURITY_FS_USE_NONE;
  1577. rc = 0;
  1578. } else {
  1579. *behavior = SECURITY_FS_USE_GENFS;
  1580. }
  1581. }
  1582. out:
  1583. POLICY_RDUNLOCK;
  1584. return rc;
  1585. }
  1586. int security_get_bools(int *len, char ***names, int **values)
  1587. {
  1588. int i, rc = -ENOMEM;
  1589. POLICY_RDLOCK;
  1590. *names = NULL;
  1591. *values = NULL;
  1592. *len = policydb.p_bools.nprim;
  1593. if (!*len) {
  1594. rc = 0;
  1595. goto out;
  1596. }
  1597. *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
  1598. if (!*names)
  1599. goto err;
  1600. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1601. if (!*values)
  1602. goto err;
  1603. for (i = 0; i < *len; i++) {
  1604. size_t name_len;
  1605. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1606. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1607. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1608. if (!(*names)[i])
  1609. goto err;
  1610. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1611. (*names)[i][name_len - 1] = 0;
  1612. }
  1613. rc = 0;
  1614. out:
  1615. POLICY_RDUNLOCK;
  1616. return rc;
  1617. err:
  1618. if (*names) {
  1619. for (i = 0; i < *len; i++)
  1620. kfree((*names)[i]);
  1621. }
  1622. kfree(*values);
  1623. goto out;
  1624. }
  1625. int security_set_bools(int len, int *values)
  1626. {
  1627. int i, rc = 0;
  1628. int lenp, seqno = 0;
  1629. struct cond_node *cur;
  1630. POLICY_WRLOCK;
  1631. lenp = policydb.p_bools.nprim;
  1632. if (len != lenp) {
  1633. rc = -EFAULT;
  1634. goto out;
  1635. }
  1636. for (i = 0; i < len; i++) {
  1637. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1638. audit_log(current->audit_context, GFP_ATOMIC,
  1639. AUDIT_MAC_CONFIG_CHANGE,
  1640. "bool=%s val=%d old_val=%d auid=%u",
  1641. policydb.p_bool_val_to_name[i],
  1642. !!values[i],
  1643. policydb.bool_val_to_struct[i]->state,
  1644. audit_get_loginuid(current->audit_context));
  1645. }
  1646. if (values[i]) {
  1647. policydb.bool_val_to_struct[i]->state = 1;
  1648. } else {
  1649. policydb.bool_val_to_struct[i]->state = 0;
  1650. }
  1651. }
  1652. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1653. rc = evaluate_cond_node(&policydb, cur);
  1654. if (rc)
  1655. goto out;
  1656. }
  1657. seqno = ++latest_granting;
  1658. out:
  1659. POLICY_WRUNLOCK;
  1660. if (!rc) {
  1661. avc_ss_reset(seqno);
  1662. selnl_notify_policyload(seqno);
  1663. selinux_xfrm_notify_policyload();
  1664. }
  1665. return rc;
  1666. }
  1667. int security_get_bool_value(int bool)
  1668. {
  1669. int rc = 0;
  1670. int len;
  1671. POLICY_RDLOCK;
  1672. len = policydb.p_bools.nprim;
  1673. if (bool >= len) {
  1674. rc = -EFAULT;
  1675. goto out;
  1676. }
  1677. rc = policydb.bool_val_to_struct[bool]->state;
  1678. out:
  1679. POLICY_RDUNLOCK;
  1680. return rc;
  1681. }
  1682. /*
  1683. * security_sid_mls_copy() - computes a new sid based on the given
  1684. * sid and the mls portion of mls_sid.
  1685. */
  1686. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1687. {
  1688. struct context *context1;
  1689. struct context *context2;
  1690. struct context newcon;
  1691. char *s;
  1692. u32 len;
  1693. int rc = 0;
  1694. if (!ss_initialized || !selinux_mls_enabled) {
  1695. *new_sid = sid;
  1696. goto out;
  1697. }
  1698. context_init(&newcon);
  1699. POLICY_RDLOCK;
  1700. context1 = sidtab_search(&sidtab, sid);
  1701. if (!context1) {
  1702. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1703. "%d\n", sid);
  1704. rc = -EINVAL;
  1705. goto out_unlock;
  1706. }
  1707. context2 = sidtab_search(&sidtab, mls_sid);
  1708. if (!context2) {
  1709. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1710. "%d\n", mls_sid);
  1711. rc = -EINVAL;
  1712. goto out_unlock;
  1713. }
  1714. newcon.user = context1->user;
  1715. newcon.role = context1->role;
  1716. newcon.type = context1->type;
  1717. rc = mls_context_cpy(&newcon, context2);
  1718. if (rc)
  1719. goto out_unlock;
  1720. /* Check the validity of the new context. */
  1721. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1722. rc = convert_context_handle_invalid_context(&newcon);
  1723. if (rc)
  1724. goto bad;
  1725. }
  1726. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1727. goto out_unlock;
  1728. bad:
  1729. if (!context_struct_to_string(&newcon, &s, &len)) {
  1730. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1731. "security_sid_mls_copy: invalid context %s", s);
  1732. kfree(s);
  1733. }
  1734. out_unlock:
  1735. POLICY_RDUNLOCK;
  1736. context_destroy(&newcon);
  1737. out:
  1738. return rc;
  1739. }
  1740. struct selinux_audit_rule {
  1741. u32 au_seqno;
  1742. struct context au_ctxt;
  1743. };
  1744. void selinux_audit_rule_free(struct selinux_audit_rule *rule)
  1745. {
  1746. if (rule) {
  1747. context_destroy(&rule->au_ctxt);
  1748. kfree(rule);
  1749. }
  1750. }
  1751. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
  1752. struct selinux_audit_rule **rule)
  1753. {
  1754. struct selinux_audit_rule *tmprule;
  1755. struct role_datum *roledatum;
  1756. struct type_datum *typedatum;
  1757. struct user_datum *userdatum;
  1758. int rc = 0;
  1759. *rule = NULL;
  1760. if (!ss_initialized)
  1761. return -ENOTSUPP;
  1762. switch (field) {
  1763. case AUDIT_SUBJ_USER:
  1764. case AUDIT_SUBJ_ROLE:
  1765. case AUDIT_SUBJ_TYPE:
  1766. case AUDIT_OBJ_USER:
  1767. case AUDIT_OBJ_ROLE:
  1768. case AUDIT_OBJ_TYPE:
  1769. /* only 'equals' and 'not equals' fit user, role, and type */
  1770. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  1771. return -EINVAL;
  1772. break;
  1773. case AUDIT_SUBJ_SEN:
  1774. case AUDIT_SUBJ_CLR:
  1775. case AUDIT_OBJ_LEV_LOW:
  1776. case AUDIT_OBJ_LEV_HIGH:
  1777. /* we do not allow a range, indicated by the presense of '-' */
  1778. if (strchr(rulestr, '-'))
  1779. return -EINVAL;
  1780. break;
  1781. default:
  1782. /* only the above fields are valid */
  1783. return -EINVAL;
  1784. }
  1785. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  1786. if (!tmprule)
  1787. return -ENOMEM;
  1788. context_init(&tmprule->au_ctxt);
  1789. POLICY_RDLOCK;
  1790. tmprule->au_seqno = latest_granting;
  1791. switch (field) {
  1792. case AUDIT_SUBJ_USER:
  1793. case AUDIT_OBJ_USER:
  1794. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  1795. if (!userdatum)
  1796. rc = -EINVAL;
  1797. else
  1798. tmprule->au_ctxt.user = userdatum->value;
  1799. break;
  1800. case AUDIT_SUBJ_ROLE:
  1801. case AUDIT_OBJ_ROLE:
  1802. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  1803. if (!roledatum)
  1804. rc = -EINVAL;
  1805. else
  1806. tmprule->au_ctxt.role = roledatum->value;
  1807. break;
  1808. case AUDIT_SUBJ_TYPE:
  1809. case AUDIT_OBJ_TYPE:
  1810. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  1811. if (!typedatum)
  1812. rc = -EINVAL;
  1813. else
  1814. tmprule->au_ctxt.type = typedatum->value;
  1815. break;
  1816. case AUDIT_SUBJ_SEN:
  1817. case AUDIT_SUBJ_CLR:
  1818. case AUDIT_OBJ_LEV_LOW:
  1819. case AUDIT_OBJ_LEV_HIGH:
  1820. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  1821. break;
  1822. }
  1823. POLICY_RDUNLOCK;
  1824. if (rc) {
  1825. selinux_audit_rule_free(tmprule);
  1826. tmprule = NULL;
  1827. }
  1828. *rule = tmprule;
  1829. return rc;
  1830. }
  1831. int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
  1832. struct selinux_audit_rule *rule,
  1833. struct audit_context *actx)
  1834. {
  1835. struct context *ctxt;
  1836. struct mls_level *level;
  1837. int match = 0;
  1838. if (!rule) {
  1839. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1840. "selinux_audit_rule_match: missing rule\n");
  1841. return -ENOENT;
  1842. }
  1843. POLICY_RDLOCK;
  1844. if (rule->au_seqno < latest_granting) {
  1845. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1846. "selinux_audit_rule_match: stale rule\n");
  1847. match = -ESTALE;
  1848. goto out;
  1849. }
  1850. ctxt = sidtab_search(&sidtab, sid);
  1851. if (!ctxt) {
  1852. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1853. "selinux_audit_rule_match: unrecognized SID %d\n",
  1854. sid);
  1855. match = -ENOENT;
  1856. goto out;
  1857. }
  1858. /* a field/op pair that is not caught here will simply fall through
  1859. without a match */
  1860. switch (field) {
  1861. case AUDIT_SUBJ_USER:
  1862. case AUDIT_OBJ_USER:
  1863. switch (op) {
  1864. case AUDIT_EQUAL:
  1865. match = (ctxt->user == rule->au_ctxt.user);
  1866. break;
  1867. case AUDIT_NOT_EQUAL:
  1868. match = (ctxt->user != rule->au_ctxt.user);
  1869. break;
  1870. }
  1871. break;
  1872. case AUDIT_SUBJ_ROLE:
  1873. case AUDIT_OBJ_ROLE:
  1874. switch (op) {
  1875. case AUDIT_EQUAL:
  1876. match = (ctxt->role == rule->au_ctxt.role);
  1877. break;
  1878. case AUDIT_NOT_EQUAL:
  1879. match = (ctxt->role != rule->au_ctxt.role);
  1880. break;
  1881. }
  1882. break;
  1883. case AUDIT_SUBJ_TYPE:
  1884. case AUDIT_OBJ_TYPE:
  1885. switch (op) {
  1886. case AUDIT_EQUAL:
  1887. match = (ctxt->type == rule->au_ctxt.type);
  1888. break;
  1889. case AUDIT_NOT_EQUAL:
  1890. match = (ctxt->type != rule->au_ctxt.type);
  1891. break;
  1892. }
  1893. break;
  1894. case AUDIT_SUBJ_SEN:
  1895. case AUDIT_SUBJ_CLR:
  1896. case AUDIT_OBJ_LEV_LOW:
  1897. case AUDIT_OBJ_LEV_HIGH:
  1898. level = ((field == AUDIT_SUBJ_SEN ||
  1899. field == AUDIT_OBJ_LEV_LOW) ?
  1900. &ctxt->range.level[0] : &ctxt->range.level[1]);
  1901. switch (op) {
  1902. case AUDIT_EQUAL:
  1903. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  1904. level);
  1905. break;
  1906. case AUDIT_NOT_EQUAL:
  1907. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  1908. level);
  1909. break;
  1910. case AUDIT_LESS_THAN:
  1911. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  1912. level) &&
  1913. !mls_level_eq(&rule->au_ctxt.range.level[0],
  1914. level));
  1915. break;
  1916. case AUDIT_LESS_THAN_OR_EQUAL:
  1917. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  1918. level);
  1919. break;
  1920. case AUDIT_GREATER_THAN:
  1921. match = (mls_level_dom(level,
  1922. &rule->au_ctxt.range.level[0]) &&
  1923. !mls_level_eq(level,
  1924. &rule->au_ctxt.range.level[0]));
  1925. break;
  1926. case AUDIT_GREATER_THAN_OR_EQUAL:
  1927. match = mls_level_dom(level,
  1928. &rule->au_ctxt.range.level[0]);
  1929. break;
  1930. }
  1931. }
  1932. out:
  1933. POLICY_RDUNLOCK;
  1934. return match;
  1935. }
  1936. static int (*aurule_callback)(void) = NULL;
  1937. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  1938. u16 class, u32 perms, u32 *retained)
  1939. {
  1940. int err = 0;
  1941. if (event == AVC_CALLBACK_RESET && aurule_callback)
  1942. err = aurule_callback();
  1943. return err;
  1944. }
  1945. static int __init aurule_init(void)
  1946. {
  1947. int err;
  1948. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  1949. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  1950. if (err)
  1951. panic("avc_add_callback() failed, error %d\n", err);
  1952. return err;
  1953. }
  1954. __initcall(aurule_init);
  1955. void selinux_audit_set_callback(int (*callback)(void))
  1956. {
  1957. aurule_callback = callback;
  1958. }
  1959. /**
  1960. * security_skb_extlbl_sid - Determine the external label of a packet
  1961. * @skb: the packet
  1962. * @base_sid: the SELinux SID to use as a context for MLS only external labels
  1963. * @sid: the packet's SID
  1964. *
  1965. * Description:
  1966. * Check the various different forms of external packet labeling and determine
  1967. * the external SID for the packet.
  1968. *
  1969. */
  1970. void security_skb_extlbl_sid(struct sk_buff *skb, u32 base_sid, u32 *sid)
  1971. {
  1972. u32 xfrm_sid;
  1973. u32 nlbl_sid;
  1974. selinux_skb_xfrm_sid(skb, &xfrm_sid);
  1975. if (selinux_netlbl_skbuff_getsid(skb,
  1976. (xfrm_sid == SECSID_NULL ?
  1977. base_sid : xfrm_sid),
  1978. &nlbl_sid) != 0)
  1979. nlbl_sid = SECSID_NULL;
  1980. *sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
  1981. }
  1982. #ifdef CONFIG_NETLABEL
  1983. /*
  1984. * This is the structure we store inside the NetLabel cache block.
  1985. */
  1986. #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
  1987. #define NETLBL_CACHE_T_NONE 0
  1988. #define NETLBL_CACHE_T_SID 1
  1989. #define NETLBL_CACHE_T_MLS 2
  1990. struct netlbl_cache {
  1991. u32 type;
  1992. union {
  1993. u32 sid;
  1994. struct mls_range mls_label;
  1995. } data;
  1996. };
  1997. /**
  1998. * selinux_netlbl_cache_free - Free the NetLabel cached data
  1999. * @data: the data to free
  2000. *
  2001. * Description:
  2002. * This function is intended to be used as the free() callback inside the
  2003. * netlbl_lsm_cache structure.
  2004. *
  2005. */
  2006. static void selinux_netlbl_cache_free(const void *data)
  2007. {
  2008. struct netlbl_cache *cache;
  2009. if (data == NULL)
  2010. return;
  2011. cache = NETLBL_CACHE(data);
  2012. switch (cache->type) {
  2013. case NETLBL_CACHE_T_MLS:
  2014. ebitmap_destroy(&cache->data.mls_label.level[0].cat);
  2015. break;
  2016. }
  2017. kfree(data);
  2018. }
  2019. /**
  2020. * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
  2021. * @skb: the packet
  2022. * @ctx: the SELinux context
  2023. *
  2024. * Description:
  2025. * Attempt to cache the context in @ctx, which was derived from the packet in
  2026. * @skb, in the NetLabel subsystem cache.
  2027. *
  2028. */
  2029. static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
  2030. {
  2031. struct netlbl_cache *cache = NULL;
  2032. struct netlbl_lsm_secattr secattr;
  2033. netlbl_secattr_init(&secattr);
  2034. secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2035. if (secattr.cache == NULL)
  2036. goto netlbl_cache_add_return;
  2037. cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
  2038. if (cache == NULL)
  2039. goto netlbl_cache_add_return;
  2040. cache->type = NETLBL_CACHE_T_MLS;
  2041. if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
  2042. &ctx->range.level[0].cat) != 0)
  2043. goto netlbl_cache_add_return;
  2044. cache->data.mls_label.level[1].cat.highbit =
  2045. cache->data.mls_label.level[0].cat.highbit;
  2046. cache->data.mls_label.level[1].cat.node =
  2047. cache->data.mls_label.level[0].cat.node;
  2048. cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
  2049. cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
  2050. secattr.cache->free = selinux_netlbl_cache_free;
  2051. secattr.cache->data = (void *)cache;
  2052. secattr.flags = NETLBL_SECATTR_CACHE;
  2053. netlbl_cache_add(skb, &secattr);
  2054. netlbl_cache_add_return:
  2055. netlbl_secattr_destroy(&secattr);
  2056. }
  2057. /**
  2058. * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
  2059. *
  2060. * Description:
  2061. * Invalidate the NetLabel security attribute mapping cache.
  2062. *
  2063. */
  2064. void selinux_netlbl_cache_invalidate(void)
  2065. {
  2066. netlbl_cache_invalidate();
  2067. }
  2068. /**
  2069. * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2070. * @skb: the network packet
  2071. * @secattr: the NetLabel packet security attributes
  2072. * @base_sid: the SELinux SID to use as a context for MLS only attributes
  2073. * @sid: the SELinux SID
  2074. *
  2075. * Description:
  2076. * Convert the given NetLabel packet security attributes in @secattr into a
  2077. * SELinux SID. If the @secattr field does not contain a full SELinux
  2078. * SID/context then use the context in @base_sid as the foundation. If @skb
  2079. * is not NULL attempt to cache as much data as possibile. Returns zero on
  2080. * success, negative values on failure.
  2081. *
  2082. */
  2083. static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
  2084. struct netlbl_lsm_secattr *secattr,
  2085. u32 base_sid,
  2086. u32 *sid)
  2087. {
  2088. int rc = -EIDRM;
  2089. struct context *ctx;
  2090. struct context ctx_new;
  2091. struct netlbl_cache *cache;
  2092. POLICY_RDLOCK;
  2093. if (secattr->flags & NETLBL_SECATTR_CACHE) {
  2094. cache = NETLBL_CACHE(secattr->cache->data);
  2095. switch (cache->type) {
  2096. case NETLBL_CACHE_T_SID:
  2097. *sid = cache->data.sid;
  2098. rc = 0;
  2099. break;
  2100. case NETLBL_CACHE_T_MLS:
  2101. ctx = sidtab_search(&sidtab, base_sid);
  2102. if (ctx == NULL)
  2103. goto netlbl_secattr_to_sid_return;
  2104. ctx_new.user = ctx->user;
  2105. ctx_new.role = ctx->role;
  2106. ctx_new.type = ctx->type;
  2107. ctx_new.range.level[0].sens =
  2108. cache->data.mls_label.level[0].sens;
  2109. ctx_new.range.level[0].cat.highbit =
  2110. cache->data.mls_label.level[0].cat.highbit;
  2111. ctx_new.range.level[0].cat.node =
  2112. cache->data.mls_label.level[0].cat.node;
  2113. ctx_new.range.level[1].sens =
  2114. cache->data.mls_label.level[1].sens;
  2115. ctx_new.range.level[1].cat.highbit =
  2116. cache->data.mls_label.level[1].cat.highbit;
  2117. ctx_new.range.level[1].cat.node =
  2118. cache->data.mls_label.level[1].cat.node;
  2119. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2120. break;
  2121. default:
  2122. goto netlbl_secattr_to_sid_return;
  2123. }
  2124. } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2125. ctx = sidtab_search(&sidtab, base_sid);
  2126. if (ctx == NULL)
  2127. goto netlbl_secattr_to_sid_return;
  2128. ctx_new.user = ctx->user;
  2129. ctx_new.role = ctx->role;
  2130. ctx_new.type = ctx->type;
  2131. mls_import_netlbl_lvl(&ctx_new, secattr);
  2132. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2133. if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2134. secattr->mls_cat) != 0)
  2135. goto netlbl_secattr_to_sid_return;
  2136. ctx_new.range.level[1].cat.highbit =
  2137. ctx_new.range.level[0].cat.highbit;
  2138. ctx_new.range.level[1].cat.node =
  2139. ctx_new.range.level[0].cat.node;
  2140. } else {
  2141. ebitmap_init(&ctx_new.range.level[0].cat);
  2142. ebitmap_init(&ctx_new.range.level[1].cat);
  2143. }
  2144. if (mls_context_isvalid(&policydb, &ctx_new) != 1)
  2145. goto netlbl_secattr_to_sid_return_cleanup;
  2146. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2147. if (rc != 0)
  2148. goto netlbl_secattr_to_sid_return_cleanup;
  2149. if (skb != NULL)
  2150. selinux_netlbl_cache_add(skb, &ctx_new);
  2151. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2152. } else {
  2153. *sid = SECSID_NULL;
  2154. rc = 0;
  2155. }
  2156. netlbl_secattr_to_sid_return:
  2157. POLICY_RDUNLOCK;
  2158. return rc;
  2159. netlbl_secattr_to_sid_return_cleanup:
  2160. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2161. goto netlbl_secattr_to_sid_return;
  2162. }
  2163. /**
  2164. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  2165. * @skb: the packet
  2166. * @base_sid: the SELinux SID to use as a context for MLS only attributes
  2167. * @sid: the SID
  2168. *
  2169. * Description:
  2170. * Call the NetLabel mechanism to get the security attributes of the given
  2171. * packet and use those attributes to determine the correct context/SID to
  2172. * assign to the packet. Returns zero on success, negative values on failure.
  2173. *
  2174. */
  2175. int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, u32 base_sid, u32 *sid)
  2176. {
  2177. int rc;
  2178. struct netlbl_lsm_secattr secattr;
  2179. netlbl_secattr_init(&secattr);
  2180. rc = netlbl_skbuff_getattr(skb, &secattr);
  2181. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  2182. rc = selinux_netlbl_secattr_to_sid(skb,
  2183. &secattr,
  2184. base_sid,
  2185. sid);
  2186. else
  2187. *sid = SECSID_NULL;
  2188. netlbl_secattr_destroy(&secattr);
  2189. return rc;
  2190. }
  2191. /**
  2192. * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
  2193. * @sock: the socket to label
  2194. * @sid: the SID to use
  2195. *
  2196. * Description:
  2197. * Attempt to label a socket using the NetLabel mechanism using the given
  2198. * SID. Returns zero values on success, negative values on failure. The
  2199. * caller is responsibile for calling rcu_read_lock() before calling this
  2200. * this function and rcu_read_unlock() after this function returns.
  2201. *
  2202. */
  2203. static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
  2204. {
  2205. int rc = -ENOENT;
  2206. struct sk_security_struct *sksec = sock->sk->sk_security;
  2207. struct netlbl_lsm_secattr secattr;
  2208. struct context *ctx;
  2209. if (!ss_initialized)
  2210. return 0;
  2211. netlbl_secattr_init(&secattr);
  2212. POLICY_RDLOCK;
  2213. ctx = sidtab_search(&sidtab, sid);
  2214. if (ctx == NULL)
  2215. goto netlbl_socket_setsid_return;
  2216. secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
  2217. GFP_ATOMIC);
  2218. secattr.flags |= NETLBL_SECATTR_DOMAIN;
  2219. mls_export_netlbl_lvl(ctx, &secattr);
  2220. rc = mls_export_netlbl_cat(ctx, &secattr);
  2221. if (rc != 0)
  2222. goto netlbl_socket_setsid_return;
  2223. rc = netlbl_socket_setattr(sock, &secattr);
  2224. if (rc == 0) {
  2225. spin_lock_bh(&sksec->nlbl_lock);
  2226. sksec->nlbl_state = NLBL_LABELED;
  2227. spin_unlock_bh(&sksec->nlbl_lock);
  2228. }
  2229. netlbl_socket_setsid_return:
  2230. POLICY_RDUNLOCK;
  2231. netlbl_secattr_destroy(&secattr);
  2232. return rc;
  2233. }
  2234. /**
  2235. * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
  2236. * @ssec: the sk_security_struct
  2237. * @family: the socket family
  2238. *
  2239. * Description:
  2240. * Called when the NetLabel state of a sk_security_struct needs to be reset.
  2241. * The caller is responsibile for all the NetLabel sk_security_struct locking.
  2242. *
  2243. */
  2244. void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
  2245. int family)
  2246. {
  2247. if (family == PF_INET)
  2248. ssec->nlbl_state = NLBL_REQUIRE;
  2249. else
  2250. ssec->nlbl_state = NLBL_UNSET;
  2251. }
  2252. /**
  2253. * selinux_netlbl_sk_security_init - Setup the NetLabel fields
  2254. * @ssec: the sk_security_struct
  2255. * @family: the socket family
  2256. *
  2257. * Description:
  2258. * Called when a new sk_security_struct is allocated to initialize the NetLabel
  2259. * fields.
  2260. *
  2261. */
  2262. void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
  2263. int family)
  2264. {
  2265. /* No locking needed, we are the only one who has access to ssec */
  2266. selinux_netlbl_sk_security_reset(ssec, family);
  2267. spin_lock_init(&ssec->nlbl_lock);
  2268. }
  2269. /**
  2270. * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
  2271. * @ssec: the original sk_security_struct
  2272. * @newssec: the cloned sk_security_struct
  2273. *
  2274. * Description:
  2275. * Clone the NetLabel specific sk_security_struct fields from @ssec to
  2276. * @newssec.
  2277. *
  2278. */
  2279. void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
  2280. struct sk_security_struct *newssec)
  2281. {
  2282. /* We don't need to take newssec->nlbl_lock because we are the only
  2283. * thread with access to newssec, but we do need to take the RCU read
  2284. * lock as other threads could have access to ssec */
  2285. rcu_read_lock();
  2286. selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
  2287. newssec->sclass = ssec->sclass;
  2288. rcu_read_unlock();
  2289. }
  2290. /**
  2291. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  2292. * @sock: the socket to label
  2293. *
  2294. * Description:
  2295. * Attempt to label a socket using the NetLabel mechanism using the given
  2296. * SID. Returns zero values on success, negative values on failure.
  2297. *
  2298. */
  2299. int selinux_netlbl_socket_post_create(struct socket *sock)
  2300. {
  2301. int rc = 0;
  2302. struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
  2303. struct sk_security_struct *sksec = sock->sk->sk_security;
  2304. sksec->sclass = isec->sclass;
  2305. rcu_read_lock();
  2306. if (sksec->nlbl_state == NLBL_REQUIRE)
  2307. rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
  2308. rcu_read_unlock();
  2309. return rc;
  2310. }
  2311. /**
  2312. * selinux_netlbl_sock_graft - Netlabel the new socket
  2313. * @sk: the new connection
  2314. * @sock: the new socket
  2315. *
  2316. * Description:
  2317. * The connection represented by @sk is being grafted onto @sock so set the
  2318. * socket's NetLabel to match the SID of @sk.
  2319. *
  2320. */
  2321. void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
  2322. {
  2323. struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
  2324. struct sk_security_struct *sksec = sk->sk_security;
  2325. struct netlbl_lsm_secattr secattr;
  2326. u32 nlbl_peer_sid;
  2327. sksec->sclass = isec->sclass;
  2328. rcu_read_lock();
  2329. if (sksec->nlbl_state != NLBL_REQUIRE) {
  2330. rcu_read_unlock();
  2331. return;
  2332. }
  2333. netlbl_secattr_init(&secattr);
  2334. if (netlbl_sock_getattr(sk, &secattr) == 0 &&
  2335. secattr.flags != NETLBL_SECATTR_NONE &&
  2336. selinux_netlbl_secattr_to_sid(NULL,
  2337. &secattr,
  2338. SECINITSID_UNLABELED,
  2339. &nlbl_peer_sid) == 0)
  2340. sksec->peer_sid = nlbl_peer_sid;
  2341. netlbl_secattr_destroy(&secattr);
  2342. /* Try to set the NetLabel on the socket to save time later, if we fail
  2343. * here we will pick up the pieces in later calls to
  2344. * selinux_netlbl_inode_permission(). */
  2345. selinux_netlbl_socket_setsid(sock, sksec->sid);
  2346. rcu_read_unlock();
  2347. }
  2348. /**
  2349. * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
  2350. * @inode: the file descriptor's inode
  2351. * @mask: the permission mask
  2352. *
  2353. * Description:
  2354. * Looks at a file's inode and if it is marked as a socket protected by
  2355. * NetLabel then verify that the socket has been labeled, if not try to label
  2356. * the socket now with the inode's SID. Returns zero on success, negative
  2357. * values on failure.
  2358. *
  2359. */
  2360. int selinux_netlbl_inode_permission(struct inode *inode, int mask)
  2361. {
  2362. int rc;
  2363. struct sk_security_struct *sksec;
  2364. struct socket *sock;
  2365. if (!S_ISSOCK(inode->i_mode) ||
  2366. ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
  2367. return 0;
  2368. sock = SOCKET_I(inode);
  2369. sksec = sock->sk->sk_security;
  2370. rcu_read_lock();
  2371. if (sksec->nlbl_state != NLBL_REQUIRE) {
  2372. rcu_read_unlock();
  2373. return 0;
  2374. }
  2375. local_bh_disable();
  2376. bh_lock_sock_nested(sock->sk);
  2377. rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
  2378. bh_unlock_sock(sock->sk);
  2379. local_bh_enable();
  2380. rcu_read_unlock();
  2381. return rc;
  2382. }
  2383. /**
  2384. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  2385. * @sksec: the sock's sk_security_struct
  2386. * @skb: the packet
  2387. * @ad: the audit data
  2388. *
  2389. * Description:
  2390. * Fetch the NetLabel security attributes from @skb and perform an access check
  2391. * against the receiving socket. Returns zero on success, negative values on
  2392. * error.
  2393. *
  2394. */
  2395. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  2396. struct sk_buff *skb,
  2397. struct avc_audit_data *ad)
  2398. {
  2399. int rc;
  2400. u32 netlbl_sid;
  2401. u32 recv_perm;
  2402. rc = selinux_netlbl_skbuff_getsid(skb,
  2403. SECINITSID_UNLABELED,
  2404. &netlbl_sid);
  2405. if (rc != 0)
  2406. return rc;
  2407. if (netlbl_sid == SECSID_NULL)
  2408. return 0;
  2409. switch (sksec->sclass) {
  2410. case SECCLASS_UDP_SOCKET:
  2411. recv_perm = UDP_SOCKET__RECVFROM;
  2412. break;
  2413. case SECCLASS_TCP_SOCKET:
  2414. recv_perm = TCP_SOCKET__RECVFROM;
  2415. break;
  2416. default:
  2417. recv_perm = RAWIP_SOCKET__RECVFROM;
  2418. }
  2419. rc = avc_has_perm(sksec->sid,
  2420. netlbl_sid,
  2421. sksec->sclass,
  2422. recv_perm,
  2423. ad);
  2424. if (rc == 0)
  2425. return 0;
  2426. netlbl_skbuff_err(skb, rc);
  2427. return rc;
  2428. }
  2429. /**
  2430. * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
  2431. * @sock: the socket
  2432. * @level: the socket level or protocol
  2433. * @optname: the socket option name
  2434. *
  2435. * Description:
  2436. * Check the setsockopt() call and if the user is trying to replace the IP
  2437. * options on a socket and a NetLabel is in place for the socket deny the
  2438. * access; otherwise allow the access. Returns zero when the access is
  2439. * allowed, -EACCES when denied, and other negative values on error.
  2440. *
  2441. */
  2442. int selinux_netlbl_socket_setsockopt(struct socket *sock,
  2443. int level,
  2444. int optname)
  2445. {
  2446. int rc = 0;
  2447. struct sk_security_struct *sksec = sock->sk->sk_security;
  2448. struct netlbl_lsm_secattr secattr;
  2449. rcu_read_lock();
  2450. if (level == IPPROTO_IP && optname == IP_OPTIONS &&
  2451. sksec->nlbl_state == NLBL_LABELED) {
  2452. netlbl_secattr_init(&secattr);
  2453. rc = netlbl_socket_getattr(sock, &secattr);
  2454. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  2455. rc = -EACCES;
  2456. netlbl_secattr_destroy(&secattr);
  2457. }
  2458. rcu_read_unlock();
  2459. return rc;
  2460. }
  2461. #endif /* CONFIG_NETLABEL */