mls.c 16 KB

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