services.c 42 KB

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