services.c 63 KB

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