services.c 66 KB

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