services.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. *
  11. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  12. *
  13. * Added conditional policy language extensions
  14. *
  15. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  16. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  17. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, version 2.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/sched.h>
  29. #include <linux/audit.h>
  30. #include <asm/semaphore.h>
  31. #include "flask.h"
  32. #include "avc.h"
  33. #include "avc_ss.h"
  34. #include "security.h"
  35. #include "context.h"
  36. #include "policydb.h"
  37. #include "sidtab.h"
  38. #include "services.h"
  39. #include "conditional.h"
  40. #include "mls.h"
  41. extern void selnl_notify_policyload(u32 seqno);
  42. unsigned int policydb_loaded_version;
  43. static DEFINE_RWLOCK(policy_rwlock);
  44. #define POLICY_RDLOCK read_lock(&policy_rwlock)
  45. #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
  46. #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
  47. #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
  48. static DECLARE_MUTEX(load_sem);
  49. #define LOAD_LOCK down(&load_sem)
  50. #define LOAD_UNLOCK up(&load_sem)
  51. static struct sidtab sidtab;
  52. struct policydb policydb;
  53. int ss_initialized = 0;
  54. /*
  55. * The largest sequence number that has been used when
  56. * providing an access decision to the access vector cache.
  57. * The sequence number only changes when a policy change
  58. * occurs.
  59. */
  60. static u32 latest_granting = 0;
  61. /* Forward declaration. */
  62. static int context_struct_to_string(struct context *context, char **scontext,
  63. u32 *scontext_len);
  64. /*
  65. * Return the boolean value of a constraint expression
  66. * when it is applied to the specified source and target
  67. * security contexts.
  68. *
  69. * xcontext is a special beast... It is used by the validatetrans rules
  70. * only. For these rules, scontext is the context before the transition,
  71. * tcontext is the context after the transition, and xcontext is the context
  72. * of the process performing the transition. All other callers of
  73. * constraint_expr_eval should pass in NULL for xcontext.
  74. */
  75. static int constraint_expr_eval(struct context *scontext,
  76. struct context *tcontext,
  77. struct context *xcontext,
  78. struct constraint_expr *cexpr)
  79. {
  80. u32 val1, val2;
  81. struct context *c;
  82. struct role_datum *r1, *r2;
  83. struct mls_level *l1, *l2;
  84. struct constraint_expr *e;
  85. int s[CEXPR_MAXDEPTH];
  86. int sp = -1;
  87. for (e = cexpr; e; e = e->next) {
  88. switch (e->expr_type) {
  89. case CEXPR_NOT:
  90. BUG_ON(sp < 0);
  91. s[sp] = !s[sp];
  92. break;
  93. case CEXPR_AND:
  94. BUG_ON(sp < 1);
  95. sp--;
  96. s[sp] &= s[sp+1];
  97. break;
  98. case CEXPR_OR:
  99. BUG_ON(sp < 1);
  100. sp--;
  101. s[sp] |= s[sp+1];
  102. break;
  103. case CEXPR_ATTR:
  104. if (sp == (CEXPR_MAXDEPTH-1))
  105. return 0;
  106. switch (e->attr) {
  107. case CEXPR_USER:
  108. val1 = scontext->user;
  109. val2 = tcontext->user;
  110. break;
  111. case CEXPR_TYPE:
  112. val1 = scontext->type;
  113. val2 = tcontext->type;
  114. break;
  115. case CEXPR_ROLE:
  116. val1 = scontext->role;
  117. val2 = tcontext->role;
  118. r1 = policydb.role_val_to_struct[val1 - 1];
  119. r2 = policydb.role_val_to_struct[val2 - 1];
  120. switch (e->op) {
  121. case CEXPR_DOM:
  122. s[++sp] = ebitmap_get_bit(&r1->dominates,
  123. val2 - 1);
  124. continue;
  125. case CEXPR_DOMBY:
  126. s[++sp] = ebitmap_get_bit(&r2->dominates,
  127. val1 - 1);
  128. continue;
  129. case CEXPR_INCOMP:
  130. s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
  131. val2 - 1) &&
  132. !ebitmap_get_bit(&r2->dominates,
  133. val1 - 1) );
  134. continue;
  135. default:
  136. break;
  137. }
  138. break;
  139. case CEXPR_L1L2:
  140. l1 = &(scontext->range.level[0]);
  141. l2 = &(tcontext->range.level[0]);
  142. goto mls_ops;
  143. case CEXPR_L1H2:
  144. l1 = &(scontext->range.level[0]);
  145. l2 = &(tcontext->range.level[1]);
  146. goto mls_ops;
  147. case CEXPR_H1L2:
  148. l1 = &(scontext->range.level[1]);
  149. l2 = &(tcontext->range.level[0]);
  150. goto mls_ops;
  151. case CEXPR_H1H2:
  152. l1 = &(scontext->range.level[1]);
  153. l2 = &(tcontext->range.level[1]);
  154. goto mls_ops;
  155. case CEXPR_L1H1:
  156. l1 = &(scontext->range.level[0]);
  157. l2 = &(scontext->range.level[1]);
  158. goto mls_ops;
  159. case CEXPR_L2H2:
  160. l1 = &(tcontext->range.level[0]);
  161. l2 = &(tcontext->range.level[1]);
  162. goto mls_ops;
  163. mls_ops:
  164. switch (e->op) {
  165. case CEXPR_EQ:
  166. s[++sp] = mls_level_eq(l1, l2);
  167. continue;
  168. case CEXPR_NEQ:
  169. s[++sp] = !mls_level_eq(l1, l2);
  170. continue;
  171. case CEXPR_DOM:
  172. s[++sp] = mls_level_dom(l1, l2);
  173. continue;
  174. case CEXPR_DOMBY:
  175. s[++sp] = mls_level_dom(l2, l1);
  176. continue;
  177. case CEXPR_INCOMP:
  178. s[++sp] = mls_level_incomp(l2, l1);
  179. continue;
  180. default:
  181. BUG();
  182. return 0;
  183. }
  184. break;
  185. default:
  186. BUG();
  187. return 0;
  188. }
  189. switch (e->op) {
  190. case CEXPR_EQ:
  191. s[++sp] = (val1 == val2);
  192. break;
  193. case CEXPR_NEQ:
  194. s[++sp] = (val1 != val2);
  195. break;
  196. default:
  197. BUG();
  198. return 0;
  199. }
  200. break;
  201. case CEXPR_NAMES:
  202. if (sp == (CEXPR_MAXDEPTH-1))
  203. return 0;
  204. c = scontext;
  205. if (e->attr & CEXPR_TARGET)
  206. c = tcontext;
  207. else if (e->attr & CEXPR_XTARGET) {
  208. c = xcontext;
  209. if (!c) {
  210. BUG();
  211. return 0;
  212. }
  213. }
  214. if (e->attr & CEXPR_USER)
  215. val1 = c->user;
  216. else if (e->attr & CEXPR_ROLE)
  217. val1 = c->role;
  218. else if (e->attr & CEXPR_TYPE)
  219. val1 = c->type;
  220. else {
  221. BUG();
  222. return 0;
  223. }
  224. switch (e->op) {
  225. case CEXPR_EQ:
  226. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  227. break;
  228. case CEXPR_NEQ:
  229. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  230. break;
  231. default:
  232. BUG();
  233. return 0;
  234. }
  235. break;
  236. default:
  237. BUG();
  238. return 0;
  239. }
  240. }
  241. BUG_ON(sp != 0);
  242. return s[0];
  243. }
  244. /*
  245. * Compute access vectors based on a context structure pair for
  246. * the permissions in a particular class.
  247. */
  248. static int context_struct_compute_av(struct context *scontext,
  249. struct context *tcontext,
  250. u16 tclass,
  251. u32 requested,
  252. struct av_decision *avd)
  253. {
  254. struct constraint_node *constraint;
  255. struct role_allow *ra;
  256. struct avtab_key avkey;
  257. struct avtab_datum *avdatum;
  258. struct class_datum *tclass_datum;
  259. /*
  260. * Remap extended Netlink classes for old policy versions.
  261. * Do this here rather than socket_type_to_security_class()
  262. * in case a newer policy version is loaded, allowing sockets
  263. * to remain in the correct class.
  264. */
  265. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  266. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  267. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  268. tclass = SECCLASS_NETLINK_SOCKET;
  269. if (!tclass || tclass > policydb.p_classes.nprim) {
  270. printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
  271. tclass);
  272. return -EINVAL;
  273. }
  274. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  275. /*
  276. * Initialize the access vectors to the default values.
  277. */
  278. avd->allowed = 0;
  279. avd->decided = 0xffffffff;
  280. avd->auditallow = 0;
  281. avd->auditdeny = 0xffffffff;
  282. avd->seqno = latest_granting;
  283. /*
  284. * If a specific type enforcement rule was defined for
  285. * this permission check, then use it.
  286. */
  287. avkey.source_type = scontext->type;
  288. avkey.target_type = tcontext->type;
  289. avkey.target_class = tclass;
  290. avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_AV);
  291. if (avdatum) {
  292. if (avdatum->specified & AVTAB_ALLOWED)
  293. avd->allowed = avtab_allowed(avdatum);
  294. if (avdatum->specified & AVTAB_AUDITDENY)
  295. avd->auditdeny = avtab_auditdeny(avdatum);
  296. if (avdatum->specified & AVTAB_AUDITALLOW)
  297. avd->auditallow = avtab_auditallow(avdatum);
  298. }
  299. /* Check conditional av table for additional permissions */
  300. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  301. /*
  302. * Remove any permissions prohibited by a constraint (this includes
  303. * the MLS policy).
  304. */
  305. constraint = tclass_datum->constraints;
  306. while (constraint) {
  307. if ((constraint->permissions & (avd->allowed)) &&
  308. !constraint_expr_eval(scontext, tcontext, NULL,
  309. constraint->expr)) {
  310. avd->allowed = (avd->allowed) & ~(constraint->permissions);
  311. }
  312. constraint = constraint->next;
  313. }
  314. /*
  315. * If checking process transition permission and the
  316. * role is changing, then check the (current_role, new_role)
  317. * pair.
  318. */
  319. if (tclass == SECCLASS_PROCESS &&
  320. (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
  321. scontext->role != tcontext->role) {
  322. for (ra = policydb.role_allow; ra; ra = ra->next) {
  323. if (scontext->role == ra->role &&
  324. tcontext->role == ra->new_role)
  325. break;
  326. }
  327. if (!ra)
  328. avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
  329. PROCESS__DYNTRANSITION);
  330. }
  331. return 0;
  332. }
  333. static int security_validtrans_handle_fail(struct context *ocontext,
  334. struct context *ncontext,
  335. struct context *tcontext,
  336. u16 tclass)
  337. {
  338. char *o = NULL, *n = NULL, *t = NULL;
  339. u32 olen, nlen, tlen;
  340. if (context_struct_to_string(ocontext, &o, &olen) < 0)
  341. goto out;
  342. if (context_struct_to_string(ncontext, &n, &nlen) < 0)
  343. goto out;
  344. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  345. goto out;
  346. audit_log(current->audit_context, AUDIT_SELINUX_ERR,
  347. "security_validate_transition: denied for"
  348. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  349. o, n, t, policydb.p_class_val_to_name[tclass-1]);
  350. out:
  351. kfree(o);
  352. kfree(n);
  353. kfree(t);
  354. if (!selinux_enforcing)
  355. return 0;
  356. return -EPERM;
  357. }
  358. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  359. u16 tclass)
  360. {
  361. struct context *ocontext;
  362. struct context *ncontext;
  363. struct context *tcontext;
  364. struct class_datum *tclass_datum;
  365. struct constraint_node *constraint;
  366. int rc = 0;
  367. if (!ss_initialized)
  368. return 0;
  369. POLICY_RDLOCK;
  370. /*
  371. * Remap extended Netlink classes for old policy versions.
  372. * Do this here rather than socket_type_to_security_class()
  373. * in case a newer policy version is loaded, allowing sockets
  374. * to remain in the correct class.
  375. */
  376. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  377. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  378. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  379. tclass = SECCLASS_NETLINK_SOCKET;
  380. if (!tclass || tclass > policydb.p_classes.nprim) {
  381. printk(KERN_ERR "security_validate_transition: "
  382. "unrecognized class %d\n", tclass);
  383. rc = -EINVAL;
  384. goto out;
  385. }
  386. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  387. ocontext = sidtab_search(&sidtab, oldsid);
  388. if (!ocontext) {
  389. printk(KERN_ERR "security_validate_transition: "
  390. " unrecognized SID %d\n", oldsid);
  391. rc = -EINVAL;
  392. goto out;
  393. }
  394. ncontext = sidtab_search(&sidtab, newsid);
  395. if (!ncontext) {
  396. printk(KERN_ERR "security_validate_transition: "
  397. " unrecognized SID %d\n", newsid);
  398. rc = -EINVAL;
  399. goto out;
  400. }
  401. tcontext = sidtab_search(&sidtab, tasksid);
  402. if (!tcontext) {
  403. printk(KERN_ERR "security_validate_transition: "
  404. " unrecognized SID %d\n", tasksid);
  405. rc = -EINVAL;
  406. goto out;
  407. }
  408. constraint = tclass_datum->validatetrans;
  409. while (constraint) {
  410. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  411. constraint->expr)) {
  412. rc = security_validtrans_handle_fail(ocontext, ncontext,
  413. tcontext, tclass);
  414. goto out;
  415. }
  416. constraint = constraint->next;
  417. }
  418. out:
  419. POLICY_RDUNLOCK;
  420. return rc;
  421. }
  422. /**
  423. * security_compute_av - Compute access vector decisions.
  424. * @ssid: source security identifier
  425. * @tsid: target security identifier
  426. * @tclass: target security class
  427. * @requested: requested permissions
  428. * @avd: access vector decisions
  429. *
  430. * Compute a set of access vector decisions based on the
  431. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  432. * Return -%EINVAL if any of the parameters are invalid or %0
  433. * if the access vector decisions were computed successfully.
  434. */
  435. int security_compute_av(u32 ssid,
  436. u32 tsid,
  437. u16 tclass,
  438. u32 requested,
  439. struct av_decision *avd)
  440. {
  441. struct context *scontext = NULL, *tcontext = NULL;
  442. int rc = 0;
  443. if (!ss_initialized) {
  444. avd->allowed = 0xffffffff;
  445. avd->decided = 0xffffffff;
  446. avd->auditallow = 0;
  447. avd->auditdeny = 0xffffffff;
  448. avd->seqno = latest_granting;
  449. return 0;
  450. }
  451. POLICY_RDLOCK;
  452. scontext = sidtab_search(&sidtab, ssid);
  453. if (!scontext) {
  454. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  455. ssid);
  456. rc = -EINVAL;
  457. goto out;
  458. }
  459. tcontext = sidtab_search(&sidtab, tsid);
  460. if (!tcontext) {
  461. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  462. tsid);
  463. rc = -EINVAL;
  464. goto out;
  465. }
  466. rc = context_struct_compute_av(scontext, tcontext, tclass,
  467. requested, avd);
  468. out:
  469. POLICY_RDUNLOCK;
  470. return rc;
  471. }
  472. /*
  473. * Write the security context string representation of
  474. * the context structure `context' into a dynamically
  475. * allocated string of the correct size. Set `*scontext'
  476. * to point to this string and set `*scontext_len' to
  477. * the length of the string.
  478. */
  479. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  480. {
  481. char *scontextp;
  482. *scontext = NULL;
  483. *scontext_len = 0;
  484. /* Compute the size of the context. */
  485. *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
  486. *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
  487. *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
  488. *scontext_len += mls_compute_context_len(context);
  489. /* Allocate space for the context; caller must free this space. */
  490. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  491. if (!scontextp) {
  492. return -ENOMEM;
  493. }
  494. *scontext = scontextp;
  495. /*
  496. * Copy the user name, role name and type name into the context.
  497. */
  498. sprintf(scontextp, "%s:%s:%s",
  499. policydb.p_user_val_to_name[context->user - 1],
  500. policydb.p_role_val_to_name[context->role - 1],
  501. policydb.p_type_val_to_name[context->type - 1]);
  502. scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
  503. 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
  504. 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
  505. mls_sid_to_context(context, &scontextp);
  506. *scontextp = 0;
  507. return 0;
  508. }
  509. #include "initial_sid_to_string.h"
  510. /**
  511. * security_sid_to_context - Obtain a context for a given SID.
  512. * @sid: security identifier, SID
  513. * @scontext: security context
  514. * @scontext_len: length in bytes
  515. *
  516. * Write the string representation of the context associated with @sid
  517. * into a dynamically allocated string of the correct size. Set @scontext
  518. * to point to this string and set @scontext_len to the length of the string.
  519. */
  520. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  521. {
  522. struct context *context;
  523. int rc = 0;
  524. if (!ss_initialized) {
  525. if (sid <= SECINITSID_NUM) {
  526. char *scontextp;
  527. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  528. scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
  529. strcpy(scontextp, initial_sid_to_string[sid]);
  530. *scontext = scontextp;
  531. goto out;
  532. }
  533. printk(KERN_ERR "security_sid_to_context: called before initial "
  534. "load_policy on unknown SID %d\n", sid);
  535. rc = -EINVAL;
  536. goto out;
  537. }
  538. POLICY_RDLOCK;
  539. context = sidtab_search(&sidtab, sid);
  540. if (!context) {
  541. printk(KERN_ERR "security_sid_to_context: unrecognized SID "
  542. "%d\n", sid);
  543. rc = -EINVAL;
  544. goto out_unlock;
  545. }
  546. rc = context_struct_to_string(context, scontext, scontext_len);
  547. out_unlock:
  548. POLICY_RDUNLOCK;
  549. out:
  550. return rc;
  551. }
  552. /**
  553. * security_context_to_sid - Obtain a SID for a given security context.
  554. * @scontext: security context
  555. * @scontext_len: length in bytes
  556. * @sid: security identifier, SID
  557. *
  558. * Obtains a SID associated with the security context that
  559. * has the string representation specified by @scontext.
  560. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  561. * memory is available, or 0 on success.
  562. */
  563. int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
  564. {
  565. char *scontext2;
  566. struct context context;
  567. struct role_datum *role;
  568. struct type_datum *typdatum;
  569. struct user_datum *usrdatum;
  570. char *scontextp, *p, oldc;
  571. int rc = 0;
  572. if (!ss_initialized) {
  573. int i;
  574. for (i = 1; i < SECINITSID_NUM; i++) {
  575. if (!strcmp(initial_sid_to_string[i], scontext)) {
  576. *sid = i;
  577. goto out;
  578. }
  579. }
  580. *sid = SECINITSID_KERNEL;
  581. goto out;
  582. }
  583. *sid = SECSID_NULL;
  584. /* Copy the string so that we can modify the copy as we parse it.
  585. The string should already by null terminated, but we append a
  586. null suffix to the copy to avoid problems with the existing
  587. attr package, which doesn't view the null terminator as part
  588. of the attribute value. */
  589. scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
  590. if (!scontext2) {
  591. rc = -ENOMEM;
  592. goto out;
  593. }
  594. memcpy(scontext2, scontext, scontext_len);
  595. scontext2[scontext_len] = 0;
  596. context_init(&context);
  597. *sid = SECSID_NULL;
  598. POLICY_RDLOCK;
  599. /* Parse the security context. */
  600. rc = -EINVAL;
  601. scontextp = (char *) scontext2;
  602. /* Extract the user. */
  603. p = scontextp;
  604. while (*p && *p != ':')
  605. p++;
  606. if (*p == 0)
  607. goto out_unlock;
  608. *p++ = 0;
  609. usrdatum = hashtab_search(policydb.p_users.table, scontextp);
  610. if (!usrdatum)
  611. goto out_unlock;
  612. context.user = usrdatum->value;
  613. /* Extract role. */
  614. scontextp = p;
  615. while (*p && *p != ':')
  616. p++;
  617. if (*p == 0)
  618. goto out_unlock;
  619. *p++ = 0;
  620. role = hashtab_search(policydb.p_roles.table, scontextp);
  621. if (!role)
  622. goto out_unlock;
  623. context.role = role->value;
  624. /* Extract type. */
  625. scontextp = p;
  626. while (*p && *p != ':')
  627. p++;
  628. oldc = *p;
  629. *p++ = 0;
  630. typdatum = hashtab_search(policydb.p_types.table, scontextp);
  631. if (!typdatum)
  632. goto out_unlock;
  633. context.type = typdatum->value;
  634. rc = mls_context_to_sid(oldc, &p, &context);
  635. if (rc)
  636. goto out_unlock;
  637. if ((p - scontext2) < scontext_len) {
  638. rc = -EINVAL;
  639. goto out_unlock;
  640. }
  641. /* Check the validity of the new context. */
  642. if (!policydb_context_isvalid(&policydb, &context)) {
  643. rc = -EINVAL;
  644. goto out_unlock;
  645. }
  646. /* Obtain the new sid. */
  647. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  648. out_unlock:
  649. POLICY_RDUNLOCK;
  650. context_destroy(&context);
  651. kfree(scontext2);
  652. out:
  653. return rc;
  654. }
  655. static int compute_sid_handle_invalid_context(
  656. struct context *scontext,
  657. struct context *tcontext,
  658. u16 tclass,
  659. struct context *newcontext)
  660. {
  661. char *s = NULL, *t = NULL, *n = NULL;
  662. u32 slen, tlen, nlen;
  663. if (context_struct_to_string(scontext, &s, &slen) < 0)
  664. goto out;
  665. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  666. goto out;
  667. if (context_struct_to_string(newcontext, &n, &nlen) < 0)
  668. goto out;
  669. audit_log(current->audit_context, AUDIT_SELINUX_ERR,
  670. "security_compute_sid: invalid context %s"
  671. " for scontext=%s"
  672. " tcontext=%s"
  673. " tclass=%s",
  674. n, s, t, policydb.p_class_val_to_name[tclass-1]);
  675. out:
  676. kfree(s);
  677. kfree(t);
  678. kfree(n);
  679. if (!selinux_enforcing)
  680. return 0;
  681. return -EACCES;
  682. }
  683. static int security_compute_sid(u32 ssid,
  684. u32 tsid,
  685. u16 tclass,
  686. u32 specified,
  687. u32 *out_sid)
  688. {
  689. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  690. struct role_trans *roletr = NULL;
  691. struct avtab_key avkey;
  692. struct avtab_datum *avdatum;
  693. struct avtab_node *node;
  694. unsigned int type_change = 0;
  695. int rc = 0;
  696. if (!ss_initialized) {
  697. switch (tclass) {
  698. case SECCLASS_PROCESS:
  699. *out_sid = ssid;
  700. break;
  701. default:
  702. *out_sid = tsid;
  703. break;
  704. }
  705. goto out;
  706. }
  707. POLICY_RDLOCK;
  708. scontext = sidtab_search(&sidtab, ssid);
  709. if (!scontext) {
  710. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  711. ssid);
  712. rc = -EINVAL;
  713. goto out_unlock;
  714. }
  715. tcontext = sidtab_search(&sidtab, tsid);
  716. if (!tcontext) {
  717. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  718. tsid);
  719. rc = -EINVAL;
  720. goto out_unlock;
  721. }
  722. context_init(&newcontext);
  723. /* Set the user identity. */
  724. switch (specified) {
  725. case AVTAB_TRANSITION:
  726. case AVTAB_CHANGE:
  727. /* Use the process user identity. */
  728. newcontext.user = scontext->user;
  729. break;
  730. case AVTAB_MEMBER:
  731. /* Use the related object owner. */
  732. newcontext.user = tcontext->user;
  733. break;
  734. }
  735. /* Set the role and type to default values. */
  736. switch (tclass) {
  737. case SECCLASS_PROCESS:
  738. /* Use the current role and type of process. */
  739. newcontext.role = scontext->role;
  740. newcontext.type = scontext->type;
  741. break;
  742. default:
  743. /* Use the well-defined object role. */
  744. newcontext.role = OBJECT_R_VAL;
  745. /* Use the type of the related object. */
  746. newcontext.type = tcontext->type;
  747. }
  748. /* Look for a type transition/member/change rule. */
  749. avkey.source_type = scontext->type;
  750. avkey.target_type = tcontext->type;
  751. avkey.target_class = tclass;
  752. avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
  753. /* If no permanent rule, also check for enabled conditional rules */
  754. if(!avdatum) {
  755. node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
  756. for (; node != NULL; node = avtab_search_node_next(node, specified)) {
  757. if (node->datum.specified & AVTAB_ENABLED) {
  758. avdatum = &node->datum;
  759. break;
  760. }
  761. }
  762. }
  763. type_change = (avdatum && (avdatum->specified & specified));
  764. if (type_change) {
  765. /* Use the type from the type transition/member/change rule. */
  766. switch (specified) {
  767. case AVTAB_TRANSITION:
  768. newcontext.type = avtab_transition(avdatum);
  769. break;
  770. case AVTAB_MEMBER:
  771. newcontext.type = avtab_member(avdatum);
  772. break;
  773. case AVTAB_CHANGE:
  774. newcontext.type = avtab_change(avdatum);
  775. break;
  776. }
  777. }
  778. /* Check for class-specific changes. */
  779. switch (tclass) {
  780. case SECCLASS_PROCESS:
  781. if (specified & AVTAB_TRANSITION) {
  782. /* Look for a role transition rule. */
  783. for (roletr = policydb.role_tr; roletr;
  784. roletr = roletr->next) {
  785. if (roletr->role == scontext->role &&
  786. roletr->type == tcontext->type) {
  787. /* Use the role transition rule. */
  788. newcontext.role = roletr->new_role;
  789. break;
  790. }
  791. }
  792. }
  793. break;
  794. default:
  795. break;
  796. }
  797. /* Set the MLS attributes.
  798. This is done last because it may allocate memory. */
  799. rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
  800. if (rc)
  801. goto out_unlock;
  802. /* Check the validity of the context. */
  803. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  804. rc = compute_sid_handle_invalid_context(scontext,
  805. tcontext,
  806. tclass,
  807. &newcontext);
  808. if (rc)
  809. goto out_unlock;
  810. }
  811. /* Obtain the sid for the context. */
  812. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  813. out_unlock:
  814. POLICY_RDUNLOCK;
  815. context_destroy(&newcontext);
  816. out:
  817. return rc;
  818. }
  819. /**
  820. * security_transition_sid - Compute the SID for a new subject/object.
  821. * @ssid: source security identifier
  822. * @tsid: target security identifier
  823. * @tclass: target security class
  824. * @out_sid: security identifier for new subject/object
  825. *
  826. * Compute a SID to use for labeling a new subject or object in the
  827. * class @tclass based on a SID pair (@ssid, @tsid).
  828. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  829. * if insufficient memory is available, or %0 if the new SID was
  830. * computed successfully.
  831. */
  832. int security_transition_sid(u32 ssid,
  833. u32 tsid,
  834. u16 tclass,
  835. u32 *out_sid)
  836. {
  837. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
  838. }
  839. /**
  840. * security_member_sid - Compute the SID for member selection.
  841. * @ssid: source security identifier
  842. * @tsid: target security identifier
  843. * @tclass: target security class
  844. * @out_sid: security identifier for selected member
  845. *
  846. * Compute a SID to use when selecting a member of a polyinstantiated
  847. * object of class @tclass based on a SID pair (@ssid, @tsid).
  848. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  849. * if insufficient memory is available, or %0 if the SID was
  850. * computed successfully.
  851. */
  852. int security_member_sid(u32 ssid,
  853. u32 tsid,
  854. u16 tclass,
  855. u32 *out_sid)
  856. {
  857. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
  858. }
  859. /**
  860. * security_change_sid - Compute the SID for object relabeling.
  861. * @ssid: source security identifier
  862. * @tsid: target security identifier
  863. * @tclass: target security class
  864. * @out_sid: security identifier for selected member
  865. *
  866. * Compute a SID to use for relabeling an object of class @tclass
  867. * based on a SID pair (@ssid, @tsid).
  868. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  869. * if insufficient memory is available, or %0 if the SID was
  870. * computed successfully.
  871. */
  872. int security_change_sid(u32 ssid,
  873. u32 tsid,
  874. u16 tclass,
  875. u32 *out_sid)
  876. {
  877. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
  878. }
  879. /*
  880. * Verify that each permission that is defined under the
  881. * existing policy is still defined with the same value
  882. * in the new policy.
  883. */
  884. static int validate_perm(void *key, void *datum, void *p)
  885. {
  886. struct hashtab *h;
  887. struct perm_datum *perdatum, *perdatum2;
  888. int rc = 0;
  889. h = p;
  890. perdatum = datum;
  891. perdatum2 = hashtab_search(h, key);
  892. if (!perdatum2) {
  893. printk(KERN_ERR "security: permission %s disappeared",
  894. (char *)key);
  895. rc = -ENOENT;
  896. goto out;
  897. }
  898. if (perdatum->value != perdatum2->value) {
  899. printk(KERN_ERR "security: the value of permission %s changed",
  900. (char *)key);
  901. rc = -EINVAL;
  902. }
  903. out:
  904. return rc;
  905. }
  906. /*
  907. * Verify that each class that is defined under the
  908. * existing policy is still defined with the same
  909. * attributes in the new policy.
  910. */
  911. static int validate_class(void *key, void *datum, void *p)
  912. {
  913. struct policydb *newp;
  914. struct class_datum *cladatum, *cladatum2;
  915. int rc;
  916. newp = p;
  917. cladatum = datum;
  918. cladatum2 = hashtab_search(newp->p_classes.table, key);
  919. if (!cladatum2) {
  920. printk(KERN_ERR "security: class %s disappeared\n",
  921. (char *)key);
  922. rc = -ENOENT;
  923. goto out;
  924. }
  925. if (cladatum->value != cladatum2->value) {
  926. printk(KERN_ERR "security: the value of class %s changed\n",
  927. (char *)key);
  928. rc = -EINVAL;
  929. goto out;
  930. }
  931. if ((cladatum->comdatum && !cladatum2->comdatum) ||
  932. (!cladatum->comdatum && cladatum2->comdatum)) {
  933. printk(KERN_ERR "security: the inherits clause for the access "
  934. "vector definition for class %s changed\n", (char *)key);
  935. rc = -EINVAL;
  936. goto out;
  937. }
  938. if (cladatum->comdatum) {
  939. rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
  940. cladatum2->comdatum->permissions.table);
  941. if (rc) {
  942. printk(" in the access vector definition for class "
  943. "%s\n", (char *)key);
  944. goto out;
  945. }
  946. }
  947. rc = hashtab_map(cladatum->permissions.table, validate_perm,
  948. cladatum2->permissions.table);
  949. if (rc)
  950. printk(" in access vector definition for class %s\n",
  951. (char *)key);
  952. out:
  953. return rc;
  954. }
  955. /* Clone the SID into the new SID table. */
  956. static int clone_sid(u32 sid,
  957. struct context *context,
  958. void *arg)
  959. {
  960. struct sidtab *s = arg;
  961. return sidtab_insert(s, sid, context);
  962. }
  963. static inline int convert_context_handle_invalid_context(struct context *context)
  964. {
  965. int rc = 0;
  966. if (selinux_enforcing) {
  967. rc = -EINVAL;
  968. } else {
  969. char *s;
  970. u32 len;
  971. context_struct_to_string(context, &s, &len);
  972. printk(KERN_ERR "security: context %s is invalid\n", s);
  973. kfree(s);
  974. }
  975. return rc;
  976. }
  977. struct convert_context_args {
  978. struct policydb *oldp;
  979. struct policydb *newp;
  980. };
  981. /*
  982. * Convert the values in the security context
  983. * structure `c' from the values specified
  984. * in the policy `p->oldp' to the values specified
  985. * in the policy `p->newp'. Verify that the
  986. * context is valid under the new policy.
  987. */
  988. static int convert_context(u32 key,
  989. struct context *c,
  990. void *p)
  991. {
  992. struct convert_context_args *args;
  993. struct context oldc;
  994. struct role_datum *role;
  995. struct type_datum *typdatum;
  996. struct user_datum *usrdatum;
  997. char *s;
  998. u32 len;
  999. int rc;
  1000. args = p;
  1001. rc = context_cpy(&oldc, c);
  1002. if (rc)
  1003. goto out;
  1004. rc = -EINVAL;
  1005. /* Convert the user. */
  1006. usrdatum = hashtab_search(args->newp->p_users.table,
  1007. args->oldp->p_user_val_to_name[c->user - 1]);
  1008. if (!usrdatum) {
  1009. goto bad;
  1010. }
  1011. c->user = usrdatum->value;
  1012. /* Convert the role. */
  1013. role = hashtab_search(args->newp->p_roles.table,
  1014. args->oldp->p_role_val_to_name[c->role - 1]);
  1015. if (!role) {
  1016. goto bad;
  1017. }
  1018. c->role = role->value;
  1019. /* Convert the type. */
  1020. typdatum = hashtab_search(args->newp->p_types.table,
  1021. args->oldp->p_type_val_to_name[c->type - 1]);
  1022. if (!typdatum) {
  1023. goto bad;
  1024. }
  1025. c->type = typdatum->value;
  1026. rc = mls_convert_context(args->oldp, args->newp, c);
  1027. if (rc)
  1028. goto bad;
  1029. /* Check the validity of the new context. */
  1030. if (!policydb_context_isvalid(args->newp, c)) {
  1031. rc = convert_context_handle_invalid_context(&oldc);
  1032. if (rc)
  1033. goto bad;
  1034. }
  1035. context_destroy(&oldc);
  1036. out:
  1037. return rc;
  1038. bad:
  1039. context_struct_to_string(&oldc, &s, &len);
  1040. context_destroy(&oldc);
  1041. printk(KERN_ERR "security: invalidating context %s\n", s);
  1042. kfree(s);
  1043. goto out;
  1044. }
  1045. extern void selinux_complete_init(void);
  1046. /**
  1047. * security_load_policy - Load a security policy configuration.
  1048. * @data: binary policy data
  1049. * @len: length of data in bytes
  1050. *
  1051. * Load a new set of security policy configuration data,
  1052. * validate it and convert the SID table as necessary.
  1053. * This function will flush the access vector cache after
  1054. * loading the new policy.
  1055. */
  1056. int security_load_policy(void *data, size_t len)
  1057. {
  1058. struct policydb oldpolicydb, newpolicydb;
  1059. struct sidtab oldsidtab, newsidtab;
  1060. struct convert_context_args args;
  1061. u32 seqno;
  1062. int rc = 0;
  1063. struct policy_file file = { data, len }, *fp = &file;
  1064. LOAD_LOCK;
  1065. if (!ss_initialized) {
  1066. avtab_cache_init();
  1067. if (policydb_read(&policydb, fp)) {
  1068. LOAD_UNLOCK;
  1069. avtab_cache_destroy();
  1070. return -EINVAL;
  1071. }
  1072. if (policydb_load_isids(&policydb, &sidtab)) {
  1073. LOAD_UNLOCK;
  1074. policydb_destroy(&policydb);
  1075. avtab_cache_destroy();
  1076. return -EINVAL;
  1077. }
  1078. policydb_loaded_version = policydb.policyvers;
  1079. ss_initialized = 1;
  1080. seqno = ++latest_granting;
  1081. LOAD_UNLOCK;
  1082. selinux_complete_init();
  1083. avc_ss_reset(seqno);
  1084. selnl_notify_policyload(seqno);
  1085. return 0;
  1086. }
  1087. #if 0
  1088. sidtab_hash_eval(&sidtab, "sids");
  1089. #endif
  1090. if (policydb_read(&newpolicydb, fp)) {
  1091. LOAD_UNLOCK;
  1092. return -EINVAL;
  1093. }
  1094. sidtab_init(&newsidtab);
  1095. /* Verify that the existing classes did not change. */
  1096. if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
  1097. printk(KERN_ERR "security: the definition of an existing "
  1098. "class changed\n");
  1099. rc = -EINVAL;
  1100. goto err;
  1101. }
  1102. /* Clone the SID table. */
  1103. sidtab_shutdown(&sidtab);
  1104. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1105. rc = -ENOMEM;
  1106. goto err;
  1107. }
  1108. /* Convert the internal representations of contexts
  1109. in the new SID table and remove invalid SIDs. */
  1110. args.oldp = &policydb;
  1111. args.newp = &newpolicydb;
  1112. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1113. /* Save the old policydb and SID table to free later. */
  1114. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1115. sidtab_set(&oldsidtab, &sidtab);
  1116. /* Install the new policydb and SID table. */
  1117. POLICY_WRLOCK;
  1118. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1119. sidtab_set(&sidtab, &newsidtab);
  1120. seqno = ++latest_granting;
  1121. policydb_loaded_version = policydb.policyvers;
  1122. POLICY_WRUNLOCK;
  1123. LOAD_UNLOCK;
  1124. /* Free the old policydb and SID table. */
  1125. policydb_destroy(&oldpolicydb);
  1126. sidtab_destroy(&oldsidtab);
  1127. avc_ss_reset(seqno);
  1128. selnl_notify_policyload(seqno);
  1129. return 0;
  1130. err:
  1131. LOAD_UNLOCK;
  1132. sidtab_destroy(&newsidtab);
  1133. policydb_destroy(&newpolicydb);
  1134. return rc;
  1135. }
  1136. /**
  1137. * security_port_sid - Obtain the SID for a port.
  1138. * @domain: communication domain aka address family
  1139. * @type: socket type
  1140. * @protocol: protocol number
  1141. * @port: port number
  1142. * @out_sid: security identifier
  1143. */
  1144. int security_port_sid(u16 domain,
  1145. u16 type,
  1146. u8 protocol,
  1147. u16 port,
  1148. u32 *out_sid)
  1149. {
  1150. struct ocontext *c;
  1151. int rc = 0;
  1152. POLICY_RDLOCK;
  1153. c = policydb.ocontexts[OCON_PORT];
  1154. while (c) {
  1155. if (c->u.port.protocol == protocol &&
  1156. c->u.port.low_port <= port &&
  1157. c->u.port.high_port >= port)
  1158. break;
  1159. c = c->next;
  1160. }
  1161. if (c) {
  1162. if (!c->sid[0]) {
  1163. rc = sidtab_context_to_sid(&sidtab,
  1164. &c->context[0],
  1165. &c->sid[0]);
  1166. if (rc)
  1167. goto out;
  1168. }
  1169. *out_sid = c->sid[0];
  1170. } else {
  1171. *out_sid = SECINITSID_PORT;
  1172. }
  1173. out:
  1174. POLICY_RDUNLOCK;
  1175. return rc;
  1176. }
  1177. /**
  1178. * security_netif_sid - Obtain the SID for a network interface.
  1179. * @name: interface name
  1180. * @if_sid: interface SID
  1181. * @msg_sid: default SID for received packets
  1182. */
  1183. int security_netif_sid(char *name,
  1184. u32 *if_sid,
  1185. u32 *msg_sid)
  1186. {
  1187. int rc = 0;
  1188. struct ocontext *c;
  1189. POLICY_RDLOCK;
  1190. c = policydb.ocontexts[OCON_NETIF];
  1191. while (c) {
  1192. if (strcmp(name, c->u.name) == 0)
  1193. break;
  1194. c = c->next;
  1195. }
  1196. if (c) {
  1197. if (!c->sid[0] || !c->sid[1]) {
  1198. rc = sidtab_context_to_sid(&sidtab,
  1199. &c->context[0],
  1200. &c->sid[0]);
  1201. if (rc)
  1202. goto out;
  1203. rc = sidtab_context_to_sid(&sidtab,
  1204. &c->context[1],
  1205. &c->sid[1]);
  1206. if (rc)
  1207. goto out;
  1208. }
  1209. *if_sid = c->sid[0];
  1210. *msg_sid = c->sid[1];
  1211. } else {
  1212. *if_sid = SECINITSID_NETIF;
  1213. *msg_sid = SECINITSID_NETMSG;
  1214. }
  1215. out:
  1216. POLICY_RDUNLOCK;
  1217. return rc;
  1218. }
  1219. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1220. {
  1221. int i, fail = 0;
  1222. for(i = 0; i < 4; i++)
  1223. if(addr[i] != (input[i] & mask[i])) {
  1224. fail = 1;
  1225. break;
  1226. }
  1227. return !fail;
  1228. }
  1229. /**
  1230. * security_node_sid - Obtain the SID for a node (host).
  1231. * @domain: communication domain aka address family
  1232. * @addrp: address
  1233. * @addrlen: address length in bytes
  1234. * @out_sid: security identifier
  1235. */
  1236. int security_node_sid(u16 domain,
  1237. void *addrp,
  1238. u32 addrlen,
  1239. u32 *out_sid)
  1240. {
  1241. int rc = 0;
  1242. struct ocontext *c;
  1243. POLICY_RDLOCK;
  1244. switch (domain) {
  1245. case AF_INET: {
  1246. u32 addr;
  1247. if (addrlen != sizeof(u32)) {
  1248. rc = -EINVAL;
  1249. goto out;
  1250. }
  1251. addr = *((u32 *)addrp);
  1252. c = policydb.ocontexts[OCON_NODE];
  1253. while (c) {
  1254. if (c->u.node.addr == (addr & c->u.node.mask))
  1255. break;
  1256. c = c->next;
  1257. }
  1258. break;
  1259. }
  1260. case AF_INET6:
  1261. if (addrlen != sizeof(u64) * 2) {
  1262. rc = -EINVAL;
  1263. goto out;
  1264. }
  1265. c = policydb.ocontexts[OCON_NODE6];
  1266. while (c) {
  1267. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1268. c->u.node6.mask))
  1269. break;
  1270. c = c->next;
  1271. }
  1272. break;
  1273. default:
  1274. *out_sid = SECINITSID_NODE;
  1275. goto out;
  1276. }
  1277. if (c) {
  1278. if (!c->sid[0]) {
  1279. rc = sidtab_context_to_sid(&sidtab,
  1280. &c->context[0],
  1281. &c->sid[0]);
  1282. if (rc)
  1283. goto out;
  1284. }
  1285. *out_sid = c->sid[0];
  1286. } else {
  1287. *out_sid = SECINITSID_NODE;
  1288. }
  1289. out:
  1290. POLICY_RDUNLOCK;
  1291. return rc;
  1292. }
  1293. #define SIDS_NEL 25
  1294. /**
  1295. * security_get_user_sids - Obtain reachable SIDs for a user.
  1296. * @fromsid: starting SID
  1297. * @username: username
  1298. * @sids: array of reachable SIDs for user
  1299. * @nel: number of elements in @sids
  1300. *
  1301. * Generate the set of SIDs for legal security contexts
  1302. * for a given user that can be reached by @fromsid.
  1303. * Set *@sids to point to a dynamically allocated
  1304. * array containing the set of SIDs. Set *@nel to the
  1305. * number of elements in the array.
  1306. */
  1307. int security_get_user_sids(u32 fromsid,
  1308. char *username,
  1309. u32 **sids,
  1310. u32 *nel)
  1311. {
  1312. struct context *fromcon, usercon;
  1313. u32 *mysids, *mysids2, sid;
  1314. u32 mynel = 0, maxnel = SIDS_NEL;
  1315. struct user_datum *user;
  1316. struct role_datum *role;
  1317. struct av_decision avd;
  1318. int rc = 0, i, j;
  1319. if (!ss_initialized) {
  1320. *sids = NULL;
  1321. *nel = 0;
  1322. goto out;
  1323. }
  1324. POLICY_RDLOCK;
  1325. fromcon = sidtab_search(&sidtab, fromsid);
  1326. if (!fromcon) {
  1327. rc = -EINVAL;
  1328. goto out_unlock;
  1329. }
  1330. user = hashtab_search(policydb.p_users.table, username);
  1331. if (!user) {
  1332. rc = -EINVAL;
  1333. goto out_unlock;
  1334. }
  1335. usercon.user = user->value;
  1336. mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
  1337. if (!mysids) {
  1338. rc = -ENOMEM;
  1339. goto out_unlock;
  1340. }
  1341. memset(mysids, 0, maxnel*sizeof(*mysids));
  1342. for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
  1343. if (!ebitmap_get_bit(&user->roles, i))
  1344. continue;
  1345. role = policydb.role_val_to_struct[i];
  1346. usercon.role = i+1;
  1347. for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
  1348. if (!ebitmap_get_bit(&role->types, j))
  1349. continue;
  1350. usercon.type = j+1;
  1351. if (mls_setup_user_range(fromcon, user, &usercon))
  1352. continue;
  1353. rc = context_struct_compute_av(fromcon, &usercon,
  1354. SECCLASS_PROCESS,
  1355. PROCESS__TRANSITION,
  1356. &avd);
  1357. if (rc || !(avd.allowed & PROCESS__TRANSITION))
  1358. continue;
  1359. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1360. if (rc) {
  1361. kfree(mysids);
  1362. goto out_unlock;
  1363. }
  1364. if (mynel < maxnel) {
  1365. mysids[mynel++] = sid;
  1366. } else {
  1367. maxnel += SIDS_NEL;
  1368. mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
  1369. if (!mysids2) {
  1370. rc = -ENOMEM;
  1371. kfree(mysids);
  1372. goto out_unlock;
  1373. }
  1374. memset(mysids2, 0, maxnel*sizeof(*mysids2));
  1375. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1376. kfree(mysids);
  1377. mysids = mysids2;
  1378. mysids[mynel++] = sid;
  1379. }
  1380. }
  1381. }
  1382. *sids = mysids;
  1383. *nel = mynel;
  1384. out_unlock:
  1385. POLICY_RDUNLOCK;
  1386. out:
  1387. return rc;
  1388. }
  1389. /**
  1390. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1391. * @fstype: filesystem type
  1392. * @path: path from root of mount
  1393. * @sclass: file security class
  1394. * @sid: SID for path
  1395. *
  1396. * Obtain a SID to use for a file in a filesystem that
  1397. * cannot support xattr or use a fixed labeling behavior like
  1398. * transition SIDs or task SIDs.
  1399. */
  1400. int security_genfs_sid(const char *fstype,
  1401. char *path,
  1402. u16 sclass,
  1403. u32 *sid)
  1404. {
  1405. int len;
  1406. struct genfs *genfs;
  1407. struct ocontext *c;
  1408. int rc = 0, cmp = 0;
  1409. POLICY_RDLOCK;
  1410. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1411. cmp = strcmp(fstype, genfs->fstype);
  1412. if (cmp <= 0)
  1413. break;
  1414. }
  1415. if (!genfs || cmp) {
  1416. *sid = SECINITSID_UNLABELED;
  1417. rc = -ENOENT;
  1418. goto out;
  1419. }
  1420. for (c = genfs->head; c; c = c->next) {
  1421. len = strlen(c->u.name);
  1422. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1423. (strncmp(c->u.name, path, len) == 0))
  1424. break;
  1425. }
  1426. if (!c) {
  1427. *sid = SECINITSID_UNLABELED;
  1428. rc = -ENOENT;
  1429. goto out;
  1430. }
  1431. if (!c->sid[0]) {
  1432. rc = sidtab_context_to_sid(&sidtab,
  1433. &c->context[0],
  1434. &c->sid[0]);
  1435. if (rc)
  1436. goto out;
  1437. }
  1438. *sid = c->sid[0];
  1439. out:
  1440. POLICY_RDUNLOCK;
  1441. return rc;
  1442. }
  1443. /**
  1444. * security_fs_use - Determine how to handle labeling for a filesystem.
  1445. * @fstype: filesystem type
  1446. * @behavior: labeling behavior
  1447. * @sid: SID for filesystem (superblock)
  1448. */
  1449. int security_fs_use(
  1450. const char *fstype,
  1451. unsigned int *behavior,
  1452. u32 *sid)
  1453. {
  1454. int rc = 0;
  1455. struct ocontext *c;
  1456. POLICY_RDLOCK;
  1457. c = policydb.ocontexts[OCON_FSUSE];
  1458. while (c) {
  1459. if (strcmp(fstype, c->u.name) == 0)
  1460. break;
  1461. c = c->next;
  1462. }
  1463. if (c) {
  1464. *behavior = c->v.behavior;
  1465. if (!c->sid[0]) {
  1466. rc = sidtab_context_to_sid(&sidtab,
  1467. &c->context[0],
  1468. &c->sid[0]);
  1469. if (rc)
  1470. goto out;
  1471. }
  1472. *sid = c->sid[0];
  1473. } else {
  1474. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1475. if (rc) {
  1476. *behavior = SECURITY_FS_USE_NONE;
  1477. rc = 0;
  1478. } else {
  1479. *behavior = SECURITY_FS_USE_GENFS;
  1480. }
  1481. }
  1482. out:
  1483. POLICY_RDUNLOCK;
  1484. return rc;
  1485. }
  1486. int security_get_bools(int *len, char ***names, int **values)
  1487. {
  1488. int i, rc = -ENOMEM;
  1489. POLICY_RDLOCK;
  1490. *names = NULL;
  1491. *values = NULL;
  1492. *len = policydb.p_bools.nprim;
  1493. if (!*len) {
  1494. rc = 0;
  1495. goto out;
  1496. }
  1497. *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
  1498. if (!*names)
  1499. goto err;
  1500. memset(*names, 0, sizeof(char*) * *len);
  1501. *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
  1502. if (!*values)
  1503. goto err;
  1504. for (i = 0; i < *len; i++) {
  1505. size_t name_len;
  1506. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1507. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1508. (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1509. if (!(*names)[i])
  1510. goto err;
  1511. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1512. (*names)[i][name_len - 1] = 0;
  1513. }
  1514. rc = 0;
  1515. out:
  1516. POLICY_RDUNLOCK;
  1517. return rc;
  1518. err:
  1519. if (*names) {
  1520. for (i = 0; i < *len; i++)
  1521. kfree((*names)[i]);
  1522. }
  1523. kfree(*values);
  1524. goto out;
  1525. }
  1526. int security_set_bools(int len, int *values)
  1527. {
  1528. int i, rc = 0;
  1529. int lenp, seqno = 0;
  1530. struct cond_node *cur;
  1531. POLICY_WRLOCK;
  1532. lenp = policydb.p_bools.nprim;
  1533. if (len != lenp) {
  1534. rc = -EFAULT;
  1535. goto out;
  1536. }
  1537. printk(KERN_INFO "security: committed booleans { ");
  1538. for (i = 0; i < len; i++) {
  1539. if (values[i]) {
  1540. policydb.bool_val_to_struct[i]->state = 1;
  1541. } else {
  1542. policydb.bool_val_to_struct[i]->state = 0;
  1543. }
  1544. if (i != 0)
  1545. printk(", ");
  1546. printk("%s:%d", policydb.p_bool_val_to_name[i],
  1547. policydb.bool_val_to_struct[i]->state);
  1548. }
  1549. printk(" }\n");
  1550. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1551. rc = evaluate_cond_node(&policydb, cur);
  1552. if (rc)
  1553. goto out;
  1554. }
  1555. seqno = ++latest_granting;
  1556. out:
  1557. POLICY_WRUNLOCK;
  1558. if (!rc) {
  1559. avc_ss_reset(seqno);
  1560. selnl_notify_policyload(seqno);
  1561. }
  1562. return rc;
  1563. }
  1564. int security_get_bool_value(int bool)
  1565. {
  1566. int rc = 0;
  1567. int len;
  1568. POLICY_RDLOCK;
  1569. len = policydb.p_bools.nprim;
  1570. if (bool >= len) {
  1571. rc = -EFAULT;
  1572. goto out;
  1573. }
  1574. rc = policydb.bool_val_to_struct[bool]->state;
  1575. out:
  1576. POLICY_RDUNLOCK;
  1577. return rc;
  1578. }