mls.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Implementation of the multi-level security (MLS) policy.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /*
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. *
  11. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  12. */
  13. /*
  14. * Updated: Hewlett-Packard <paul.moore@hp.com>
  15. *
  16. * Added support to import/export the MLS label from NetLabel
  17. *
  18. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <net/netlabel.h>
  25. #include "sidtab.h"
  26. #include "mls.h"
  27. #include "policydb.h"
  28. #include "services.h"
  29. /*
  30. * Return the length in bytes for the MLS fields of the
  31. * security context string representation of `context'.
  32. */
  33. int mls_compute_context_len(struct context * context)
  34. {
  35. int i, l, len, head, prev;
  36. char *nm;
  37. struct ebitmap *e;
  38. struct ebitmap_node *node;
  39. if (!selinux_mls_enabled)
  40. return 0;
  41. len = 1; /* for the beginning ":" */
  42. for (l = 0; l < 2; l++) {
  43. int index_sens = context->range.level[l].sens;
  44. len += strlen(policydb.p_sens_val_to_name[index_sens - 1]);
  45. /* categories */
  46. head = -2;
  47. prev = -2;
  48. e = &context->range.level[l].cat;
  49. ebitmap_for_each_positive_bit(e, node, i) {
  50. if (i - prev > 1) {
  51. /* one or more negative bits are skipped */
  52. if (head != prev) {
  53. nm = policydb.p_cat_val_to_name[prev];
  54. len += strlen(nm) + 1;
  55. }
  56. nm = policydb.p_cat_val_to_name[i];
  57. len += strlen(nm) + 1;
  58. head = i;
  59. }
  60. prev = i;
  61. }
  62. if (prev != head) {
  63. nm = policydb.p_cat_val_to_name[prev];
  64. len += strlen(nm) + 1;
  65. }
  66. if (l == 0) {
  67. if (mls_level_eq(&context->range.level[0],
  68. &context->range.level[1]))
  69. break;
  70. else
  71. len++;
  72. }
  73. }
  74. return len;
  75. }
  76. /*
  77. * Write the security context string representation of
  78. * the MLS fields of `context' into the string `*scontext'.
  79. * Update `*scontext' to point to the end of the MLS fields.
  80. */
  81. void mls_sid_to_context(struct context *context,
  82. char **scontext)
  83. {
  84. char *scontextp, *nm;
  85. int i, l, head, prev;
  86. struct ebitmap *e;
  87. struct ebitmap_node *node;
  88. if (!selinux_mls_enabled)
  89. return;
  90. scontextp = *scontext;
  91. *scontextp = ':';
  92. scontextp++;
  93. for (l = 0; l < 2; l++) {
  94. strcpy(scontextp,
  95. policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  96. scontextp += strlen(scontextp);
  97. /* categories */
  98. head = -2;
  99. prev = -2;
  100. e = &context->range.level[l].cat;
  101. ebitmap_for_each_positive_bit(e, node, i) {
  102. if (i - prev > 1) {
  103. /* one or more negative bits are skipped */
  104. if (prev != head) {
  105. if (prev - head > 1)
  106. *scontextp++ = '.';
  107. else
  108. *scontextp++ = ',';
  109. nm = policydb.p_cat_val_to_name[prev];
  110. strcpy(scontextp, nm);
  111. scontextp += strlen(nm);
  112. }
  113. if (prev < 0)
  114. *scontextp++ = ':';
  115. else
  116. *scontextp++ = ',';
  117. nm = policydb.p_cat_val_to_name[i];
  118. strcpy(scontextp, nm);
  119. scontextp += strlen(nm);
  120. head = i;
  121. }
  122. prev = i;
  123. }
  124. if (prev != head) {
  125. if (prev - head > 1)
  126. *scontextp++ = '.';
  127. else
  128. *scontextp++ = ',';
  129. nm = policydb.p_cat_val_to_name[prev];
  130. strcpy(scontextp, nm);
  131. scontextp += strlen(nm);
  132. }
  133. if (l == 0) {
  134. if (mls_level_eq(&context->range.level[0],
  135. &context->range.level[1]))
  136. break;
  137. else
  138. *scontextp++ = '-';
  139. }
  140. }
  141. *scontext = scontextp;
  142. return;
  143. }
  144. /*
  145. * Return 1 if the MLS fields in the security context
  146. * structure `c' are valid. Return 0 otherwise.
  147. */
  148. int mls_context_isvalid(struct policydb *p, struct context *c)
  149. {
  150. struct level_datum *levdatum;
  151. struct user_datum *usrdatum;
  152. struct ebitmap_node *node;
  153. int i, l;
  154. if (!selinux_mls_enabled)
  155. return 1;
  156. /*
  157. * MLS range validity checks: high must dominate low, low level must
  158. * be valid (category set <-> sensitivity check), and high level must
  159. * be valid (category set <-> sensitivity check)
  160. */
  161. if (!mls_level_dom(&c->range.level[1], &c->range.level[0]))
  162. /* High does not dominate low. */
  163. return 0;
  164. for (l = 0; l < 2; l++) {
  165. if (!c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim)
  166. return 0;
  167. levdatum = hashtab_search(p->p_levels.table,
  168. p->p_sens_val_to_name[c->range.level[l].sens - 1]);
  169. if (!levdatum)
  170. return 0;
  171. ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
  172. if (i > p->p_cats.nprim)
  173. return 0;
  174. if (!ebitmap_get_bit(&levdatum->level->cat, i))
  175. /*
  176. * Category may not be associated with
  177. * sensitivity in low level.
  178. */
  179. return 0;
  180. }
  181. }
  182. if (c->role == OBJECT_R_VAL)
  183. return 1;
  184. /*
  185. * User must be authorized for the MLS range.
  186. */
  187. if (!c->user || c->user > p->p_users.nprim)
  188. return 0;
  189. usrdatum = p->user_val_to_struct[c->user - 1];
  190. if (!mls_range_contains(usrdatum->range, c->range))
  191. return 0; /* user may not be associated with range */
  192. return 1;
  193. }
  194. /*
  195. * Set the MLS fields in the security context structure
  196. * `context' based on the string representation in
  197. * the string `*scontext'. Update `*scontext' to
  198. * point to the end of the string representation of
  199. * the MLS fields.
  200. *
  201. * This function modifies the string in place, inserting
  202. * NULL characters to terminate the MLS fields.
  203. *
  204. * If a def_sid is provided and no MLS field is present,
  205. * copy the MLS field of the associated default context.
  206. * Used for upgraded to MLS systems where objects may lack
  207. * MLS fields.
  208. *
  209. * Policy read-lock must be held for sidtab lookup.
  210. *
  211. */
  212. int mls_context_to_sid(char oldc,
  213. char **scontext,
  214. struct context *context,
  215. struct sidtab *s,
  216. u32 def_sid)
  217. {
  218. char delim;
  219. char *scontextp, *p, *rngptr;
  220. struct level_datum *levdatum;
  221. struct cat_datum *catdatum, *rngdatum;
  222. int l, rc = -EINVAL;
  223. if (!selinux_mls_enabled) {
  224. if (def_sid != SECSID_NULL && oldc)
  225. *scontext += strlen(*scontext)+1;
  226. return 0;
  227. }
  228. /*
  229. * No MLS component to the security context, try and map to
  230. * default if provided.
  231. */
  232. if (!oldc) {
  233. struct context *defcon;
  234. if (def_sid == SECSID_NULL)
  235. goto out;
  236. defcon = sidtab_search(s, def_sid);
  237. if (!defcon)
  238. goto out;
  239. rc = mls_context_cpy(context, defcon);
  240. goto out;
  241. }
  242. /* Extract low sensitivity. */
  243. scontextp = p = *scontext;
  244. while (*p && *p != ':' && *p != '-')
  245. p++;
  246. delim = *p;
  247. if (delim != 0)
  248. *p++ = 0;
  249. for (l = 0; l < 2; l++) {
  250. levdatum = hashtab_search(policydb.p_levels.table, scontextp);
  251. if (!levdatum) {
  252. rc = -EINVAL;
  253. goto out;
  254. }
  255. context->range.level[l].sens = levdatum->level->sens;
  256. if (delim == ':') {
  257. /* Extract category set. */
  258. while (1) {
  259. scontextp = p;
  260. while (*p && *p != ',' && *p != '-')
  261. p++;
  262. delim = *p;
  263. if (delim != 0)
  264. *p++ = 0;
  265. /* Separate into range if exists */
  266. if ((rngptr = strchr(scontextp, '.')) != NULL) {
  267. /* Remove '.' */
  268. *rngptr++ = 0;
  269. }
  270. catdatum = hashtab_search(policydb.p_cats.table,
  271. scontextp);
  272. if (!catdatum) {
  273. rc = -EINVAL;
  274. goto out;
  275. }
  276. rc = ebitmap_set_bit(&context->range.level[l].cat,
  277. catdatum->value - 1, 1);
  278. if (rc)
  279. goto out;
  280. /* If range, set all categories in range */
  281. if (rngptr) {
  282. int i;
  283. rngdatum = hashtab_search(policydb.p_cats.table, rngptr);
  284. if (!rngdatum) {
  285. rc = -EINVAL;
  286. goto out;
  287. }
  288. if (catdatum->value >= rngdatum->value) {
  289. rc = -EINVAL;
  290. goto out;
  291. }
  292. for (i = catdatum->value; i < rngdatum->value; i++) {
  293. rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
  294. if (rc)
  295. goto out;
  296. }
  297. }
  298. if (delim != ',')
  299. break;
  300. }
  301. }
  302. if (delim == '-') {
  303. /* Extract high sensitivity. */
  304. scontextp = p;
  305. while (*p && *p != ':')
  306. p++;
  307. delim = *p;
  308. if (delim != 0)
  309. *p++ = 0;
  310. } else
  311. break;
  312. }
  313. if (l == 0) {
  314. context->range.level[1].sens = context->range.level[0].sens;
  315. rc = ebitmap_cpy(&context->range.level[1].cat,
  316. &context->range.level[0].cat);
  317. if (rc)
  318. goto out;
  319. }
  320. *scontext = ++p;
  321. rc = 0;
  322. out:
  323. return rc;
  324. }
  325. /*
  326. * Set the MLS fields in the security context structure
  327. * `context' based on the string representation in
  328. * the string `str'. This function will allocate temporary memory with the
  329. * given constraints of gfp_mask.
  330. */
  331. int mls_from_string(char *str, struct context *context, gfp_t gfp_mask)
  332. {
  333. char *tmpstr, *freestr;
  334. int rc;
  335. if (!selinux_mls_enabled)
  336. return -EINVAL;
  337. /* we need freestr because mls_context_to_sid will change
  338. the value of tmpstr */
  339. tmpstr = freestr = kstrdup(str, gfp_mask);
  340. if (!tmpstr) {
  341. rc = -ENOMEM;
  342. } else {
  343. rc = mls_context_to_sid(':', &tmpstr, context,
  344. NULL, SECSID_NULL);
  345. kfree(freestr);
  346. }
  347. return rc;
  348. }
  349. /*
  350. * Copies the MLS range `range' into `context'.
  351. */
  352. static inline int mls_range_set(struct context *context,
  353. struct mls_range *range)
  354. {
  355. int l, rc = 0;
  356. /* Copy the MLS range into the context */
  357. for (l = 0; l < 2; l++) {
  358. context->range.level[l].sens = range->level[l].sens;
  359. rc = ebitmap_cpy(&context->range.level[l].cat,
  360. &range->level[l].cat);
  361. if (rc)
  362. break;
  363. }
  364. return rc;
  365. }
  366. int mls_setup_user_range(struct context *fromcon, struct user_datum *user,
  367. struct context *usercon)
  368. {
  369. if (selinux_mls_enabled) {
  370. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  371. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  372. struct mls_level *user_low = &(user->range.level[0]);
  373. struct mls_level *user_clr = &(user->range.level[1]);
  374. struct mls_level *user_def = &(user->dfltlevel);
  375. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  376. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  377. /* Honor the user's default level if we can */
  378. if (mls_level_between(user_def, fromcon_sen, fromcon_clr)) {
  379. *usercon_sen = *user_def;
  380. } else if (mls_level_between(fromcon_sen, user_def, user_clr)) {
  381. *usercon_sen = *fromcon_sen;
  382. } else if (mls_level_between(fromcon_clr, user_low, user_def)) {
  383. *usercon_sen = *user_low;
  384. } else
  385. return -EINVAL;
  386. /* Lower the clearance of available contexts
  387. if the clearance of "fromcon" is lower than
  388. that of the user's default clearance (but
  389. only if the "fromcon" clearance dominates
  390. the user's computed sensitivity level) */
  391. if (mls_level_dom(user_clr, fromcon_clr)) {
  392. *usercon_clr = *fromcon_clr;
  393. } else if (mls_level_dom(fromcon_clr, user_clr)) {
  394. *usercon_clr = *user_clr;
  395. } else
  396. return -EINVAL;
  397. }
  398. return 0;
  399. }
  400. /*
  401. * Convert the MLS fields in the security context
  402. * structure `c' from the values specified in the
  403. * policy `oldp' to the values specified in the policy `newp'.
  404. */
  405. int mls_convert_context(struct policydb *oldp,
  406. struct policydb *newp,
  407. struct context *c)
  408. {
  409. struct level_datum *levdatum;
  410. struct cat_datum *catdatum;
  411. struct ebitmap bitmap;
  412. struct ebitmap_node *node;
  413. int l, i;
  414. if (!selinux_mls_enabled)
  415. return 0;
  416. for (l = 0; l < 2; l++) {
  417. levdatum = hashtab_search(newp->p_levels.table,
  418. oldp->p_sens_val_to_name[c->range.level[l].sens - 1]);
  419. if (!levdatum)
  420. return -EINVAL;
  421. c->range.level[l].sens = levdatum->level->sens;
  422. ebitmap_init(&bitmap);
  423. ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
  424. int rc;
  425. catdatum = hashtab_search(newp->p_cats.table,
  426. oldp->p_cat_val_to_name[i]);
  427. if (!catdatum)
  428. return -EINVAL;
  429. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  430. if (rc)
  431. return rc;
  432. }
  433. ebitmap_destroy(&c->range.level[l].cat);
  434. c->range.level[l].cat = bitmap;
  435. }
  436. return 0;
  437. }
  438. int mls_compute_sid(struct context *scontext,
  439. struct context *tcontext,
  440. u16 tclass,
  441. u32 specified,
  442. struct context *newcontext)
  443. {
  444. struct range_trans *rtr;
  445. if (!selinux_mls_enabled)
  446. return 0;
  447. switch (specified) {
  448. case AVTAB_TRANSITION:
  449. /* Look for a range transition rule. */
  450. for (rtr = policydb.range_tr; rtr; rtr = rtr->next) {
  451. if (rtr->source_type == scontext->type &&
  452. rtr->target_type == tcontext->type &&
  453. rtr->target_class == tclass) {
  454. /* Set the range from the rule */
  455. return mls_range_set(newcontext,
  456. &rtr->target_range);
  457. }
  458. }
  459. /* Fallthrough */
  460. case AVTAB_CHANGE:
  461. if (tclass == SECCLASS_PROCESS)
  462. /* Use the process MLS attributes. */
  463. return mls_context_cpy(newcontext, scontext);
  464. else
  465. /* Use the process effective MLS attributes. */
  466. return mls_context_cpy_low(newcontext, scontext);
  467. case AVTAB_MEMBER:
  468. /* Only polyinstantiate the MLS attributes if
  469. the type is being polyinstantiated */
  470. if (newcontext->type != tcontext->type) {
  471. /* Use the process effective MLS attributes. */
  472. return mls_context_cpy_low(newcontext, scontext);
  473. } else {
  474. /* Use the related object MLS attributes. */
  475. return mls_context_cpy(newcontext, tcontext);
  476. }
  477. default:
  478. return -EINVAL;
  479. }
  480. return -EINVAL;
  481. }
  482. #ifdef CONFIG_NETLABEL
  483. /**
  484. * mls_export_netlbl_lvl - Export the MLS sensitivity levels to NetLabel
  485. * @context: the security context
  486. * @secattr: the NetLabel security attributes
  487. *
  488. * Description:
  489. * Given the security context copy the low MLS sensitivity level into the
  490. * NetLabel MLS sensitivity level field.
  491. *
  492. */
  493. void mls_export_netlbl_lvl(struct context *context,
  494. struct netlbl_lsm_secattr *secattr)
  495. {
  496. if (!selinux_mls_enabled)
  497. return;
  498. secattr->mls_lvl = context->range.level[0].sens - 1;
  499. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  500. }
  501. /**
  502. * mls_import_netlbl_lvl - Import the NetLabel MLS sensitivity levels
  503. * @context: the security context
  504. * @secattr: the NetLabel security attributes
  505. *
  506. * Description:
  507. * Given the security context and the NetLabel security attributes, copy the
  508. * NetLabel MLS sensitivity level into the context.
  509. *
  510. */
  511. void mls_import_netlbl_lvl(struct context *context,
  512. struct netlbl_lsm_secattr *secattr)
  513. {
  514. if (!selinux_mls_enabled)
  515. return;
  516. context->range.level[0].sens = secattr->mls_lvl + 1;
  517. context->range.level[1].sens = context->range.level[0].sens;
  518. }
  519. /**
  520. * mls_export_netlbl_cat - Export the MLS categories to NetLabel
  521. * @context: the security context
  522. * @secattr: the NetLabel security attributes
  523. *
  524. * Description:
  525. * Given the security context copy the low MLS categories into the NetLabel
  526. * MLS category field. Returns zero on success, negative values on failure.
  527. *
  528. */
  529. int mls_export_netlbl_cat(struct context *context,
  530. struct netlbl_lsm_secattr *secattr)
  531. {
  532. int rc;
  533. if (!selinux_mls_enabled)
  534. return 0;
  535. rc = ebitmap_netlbl_export(&context->range.level[0].cat,
  536. &secattr->mls_cat);
  537. if (rc == 0 && secattr->mls_cat != NULL)
  538. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  539. return rc;
  540. }
  541. /**
  542. * mls_import_netlbl_cat - Import the MLS categories from NetLabel
  543. * @context: the security context
  544. * @secattr: the NetLabel security attributes
  545. *
  546. * Description:
  547. * Copy the NetLabel security attributes into the SELinux context; since the
  548. * NetLabel security attribute only contains a single MLS category use it for
  549. * both the low and high categories of the context. Returns zero on success,
  550. * negative values on failure.
  551. *
  552. */
  553. int mls_import_netlbl_cat(struct context *context,
  554. struct netlbl_lsm_secattr *secattr)
  555. {
  556. int rc;
  557. if (!selinux_mls_enabled)
  558. return 0;
  559. rc = ebitmap_netlbl_import(&context->range.level[0].cat,
  560. secattr->mls_cat);
  561. if (rc != 0)
  562. goto import_netlbl_cat_failure;
  563. rc = ebitmap_cpy(&context->range.level[1].cat,
  564. &context->range.level[0].cat);
  565. if (rc != 0)
  566. goto import_netlbl_cat_failure;
  567. return 0;
  568. import_netlbl_cat_failure:
  569. ebitmap_destroy(&context->range.level[0].cat);
  570. ebitmap_destroy(&context->range.level[1].cat);
  571. return rc;
  572. }
  573. #endif /* CONFIG_NETLABEL */