mls.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of the multi-level security (MLS) policy.
  4. *
  5. * Author : Stephen Smalley, <[email protected]>
  6. */
  7. /*
  8. * Updated: Trusted Computer Solutions, Inc. <[email protected]>
  9. *
  10. * Support for enhanced MLS infrastructure.
  11. *
  12. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  13. */
  14. /*
  15. * Updated: Hewlett-Packard <[email protected]>
  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. }
  145. int mls_level_isvalid(struct policydb *p, struct mls_level *l)
  146. {
  147. struct level_datum *levdatum;
  148. if (!l->sens || l->sens > p->p_levels.nprim)
  149. return 0;
  150. levdatum = symtab_search(&p->p_levels,
  151. sym_name(p, SYM_LEVELS, l->sens - 1));
  152. if (!levdatum)
  153. return 0;
  154. /*
  155. * Return 1 iff all the bits set in l->cat are also be set in
  156. * levdatum->level->cat and no bit in l->cat is larger than
  157. * p->p_cats.nprim.
  158. */
  159. return ebitmap_contains(&levdatum->level->cat, &l->cat,
  160. p->p_cats.nprim);
  161. }
  162. int mls_range_isvalid(struct policydb *p, struct mls_range *r)
  163. {
  164. return (mls_level_isvalid(p, &r->level[0]) &&
  165. mls_level_isvalid(p, &r->level[1]) &&
  166. mls_level_dom(&r->level[1], &r->level[0]));
  167. }
  168. /*
  169. * Return 1 if the MLS fields in the security context
  170. * structure `c' are valid. Return 0 otherwise.
  171. */
  172. int mls_context_isvalid(struct policydb *p, struct context *c)
  173. {
  174. struct user_datum *usrdatum;
  175. if (!p->mls_enabled)
  176. return 1;
  177. if (!mls_range_isvalid(p, &c->range))
  178. return 0;
  179. if (c->role == OBJECT_R_VAL)
  180. return 1;
  181. /*
  182. * User must be authorized for the MLS range.
  183. */
  184. if (!c->user || c->user > p->p_users.nprim)
  185. return 0;
  186. usrdatum = p->user_val_to_struct[c->user - 1];
  187. if (!mls_range_contains(usrdatum->range, c->range))
  188. return 0; /* user may not be associated with range */
  189. return 1;
  190. }
  191. /*
  192. * Set the MLS fields in the security context structure
  193. * `context' based on the string representation in
  194. * the string `scontext'.
  195. *
  196. * This function modifies the string in place, inserting
  197. * NULL characters to terminate the MLS fields.
  198. *
  199. * If a def_sid is provided and no MLS field is present,
  200. * copy the MLS field of the associated default context.
  201. * Used for upgraded to MLS systems where objects may lack
  202. * MLS fields.
  203. *
  204. * Policy read-lock must be held for sidtab lookup.
  205. *
  206. */
  207. int mls_context_to_sid(struct policydb *pol,
  208. char oldc,
  209. char *scontext,
  210. struct context *context,
  211. struct sidtab *s,
  212. u32 def_sid)
  213. {
  214. char *sensitivity, *cur_cat, *next_cat, *rngptr;
  215. struct level_datum *levdatum;
  216. struct cat_datum *catdatum, *rngdatum;
  217. int l, rc, i;
  218. char *rangep[2];
  219. if (!pol->mls_enabled) {
  220. /*
  221. * With no MLS, only return -EINVAL if there is a MLS field
  222. * and it did not come from an xattr.
  223. */
  224. if (oldc && def_sid == SECSID_NULL)
  225. return -EINVAL;
  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. return -EINVAL;
  236. defcon = sidtab_search(s, def_sid);
  237. if (!defcon)
  238. return -EINVAL;
  239. return mls_context_cpy(context, defcon);
  240. }
  241. /*
  242. * If we're dealing with a range, figure out where the two parts
  243. * of the range begin.
  244. */
  245. rangep[0] = scontext;
  246. rangep[1] = strchr(scontext, '-');
  247. if (rangep[1]) {
  248. rangep[1][0] = '\0';
  249. rangep[1]++;
  250. }
  251. /* For each part of the range: */
  252. for (l = 0; l < 2; l++) {
  253. /* Split sensitivity and category set. */
  254. sensitivity = rangep[l];
  255. if (sensitivity == NULL)
  256. break;
  257. next_cat = strchr(sensitivity, ':');
  258. if (next_cat)
  259. *(next_cat++) = '\0';
  260. /* Parse sensitivity. */
  261. levdatum = symtab_search(&pol->p_levels, sensitivity);
  262. if (!levdatum)
  263. return -EINVAL;
  264. context->range.level[l].sens = levdatum->level->sens;
  265. /* Extract category set. */
  266. while (next_cat != NULL) {
  267. cur_cat = next_cat;
  268. next_cat = strchr(next_cat, ',');
  269. if (next_cat != NULL)
  270. *(next_cat++) = '\0';
  271. /* Separate into range if exists */
  272. rngptr = strchr(cur_cat, '.');
  273. if (rngptr != NULL) {
  274. /* Remove '.' */
  275. *rngptr++ = '\0';
  276. }
  277. catdatum = symtab_search(&pol->p_cats, cur_cat);
  278. if (!catdatum)
  279. return -EINVAL;
  280. rc = ebitmap_set_bit(&context->range.level[l].cat,
  281. catdatum->value - 1, 1);
  282. if (rc)
  283. return rc;
  284. /* If range, set all categories in range */
  285. if (rngptr == NULL)
  286. continue;
  287. rngdatum = symtab_search(&pol->p_cats, rngptr);
  288. if (!rngdatum)
  289. return -EINVAL;
  290. if (catdatum->value >= rngdatum->value)
  291. return -EINVAL;
  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. return rc;
  296. }
  297. }
  298. }
  299. /* If we didn't see a '-', the range start is also the range end. */
  300. if (rangep[1] == NULL) {
  301. context->range.level[1].sens = context->range.level[0].sens;
  302. rc = ebitmap_cpy(&context->range.level[1].cat,
  303. &context->range.level[0].cat);
  304. if (rc)
  305. return rc;
  306. }
  307. return 0;
  308. }
  309. /*
  310. * Set the MLS fields in the security context structure
  311. * `context' based on the string representation in
  312. * the string `str'. This function will allocate temporary memory with the
  313. * given constraints of gfp_mask.
  314. */
  315. int mls_from_string(struct policydb *p, char *str, struct context *context,
  316. gfp_t gfp_mask)
  317. {
  318. char *tmpstr;
  319. int rc;
  320. if (!p->mls_enabled)
  321. return -EINVAL;
  322. tmpstr = kstrdup(str, gfp_mask);
  323. if (!tmpstr) {
  324. rc = -ENOMEM;
  325. } else {
  326. rc = mls_context_to_sid(p, ':', tmpstr, context,
  327. NULL, SECSID_NULL);
  328. kfree(tmpstr);
  329. }
  330. return rc;
  331. }
  332. /*
  333. * Copies the MLS range `range' into `context'.
  334. */
  335. int mls_range_set(struct context *context,
  336. struct mls_range *range)
  337. {
  338. int l, rc = 0;
  339. /* Copy the MLS range into the context */
  340. for (l = 0; l < 2; l++) {
  341. context->range.level[l].sens = range->level[l].sens;
  342. rc = ebitmap_cpy(&context->range.level[l].cat,
  343. &range->level[l].cat);
  344. if (rc)
  345. break;
  346. }
  347. return rc;
  348. }
  349. int mls_setup_user_range(struct policydb *p,
  350. struct context *fromcon, struct user_datum *user,
  351. struct context *usercon)
  352. {
  353. if (p->mls_enabled) {
  354. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  355. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  356. struct mls_level *user_low = &(user->range.level[0]);
  357. struct mls_level *user_clr = &(user->range.level[1]);
  358. struct mls_level *user_def = &(user->dfltlevel);
  359. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  360. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  361. /* Honor the user's default level if we can */
  362. if (mls_level_between(user_def, fromcon_sen, fromcon_clr))
  363. *usercon_sen = *user_def;
  364. else if (mls_level_between(fromcon_sen, user_def, user_clr))
  365. *usercon_sen = *fromcon_sen;
  366. else if (mls_level_between(fromcon_clr, user_low, user_def))
  367. *usercon_sen = *user_low;
  368. else
  369. return -EINVAL;
  370. /* Lower the clearance of available contexts
  371. if the clearance of "fromcon" is lower than
  372. that of the user's default clearance (but
  373. only if the "fromcon" clearance dominates
  374. the user's computed sensitivity level) */
  375. if (mls_level_dom(user_clr, fromcon_clr))
  376. *usercon_clr = *fromcon_clr;
  377. else if (mls_level_dom(fromcon_clr, user_clr))
  378. *usercon_clr = *user_clr;
  379. else
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. /*
  385. * Convert the MLS fields in the security context
  386. * structure `oldc' from the values specified in the
  387. * policy `oldp' to the values specified in the policy `newp',
  388. * storing the resulting context in `newc'.
  389. */
  390. int mls_convert_context(struct policydb *oldp,
  391. struct policydb *newp,
  392. struct context *oldc,
  393. struct context *newc)
  394. {
  395. struct level_datum *levdatum;
  396. struct cat_datum *catdatum;
  397. struct ebitmap_node *node;
  398. int l, i;
  399. if (!oldp->mls_enabled || !newp->mls_enabled)
  400. return 0;
  401. for (l = 0; l < 2; l++) {
  402. char *name = sym_name(oldp, SYM_LEVELS,
  403. oldc->range.level[l].sens - 1);
  404. levdatum = symtab_search(&newp->p_levels, name);
  405. if (!levdatum)
  406. return -EINVAL;
  407. newc->range.level[l].sens = levdatum->level->sens;
  408. ebitmap_for_each_positive_bit(&oldc->range.level[l].cat,
  409. node, i) {
  410. int rc;
  411. catdatum = symtab_search(&newp->p_cats,
  412. sym_name(oldp, SYM_CATS, i));
  413. if (!catdatum)
  414. return -EINVAL;
  415. rc = ebitmap_set_bit(&newc->range.level[l].cat,
  416. catdatum->value - 1, 1);
  417. if (rc)
  418. return rc;
  419. }
  420. }
  421. return 0;
  422. }
  423. int mls_compute_sid(struct policydb *p,
  424. struct context *scontext,
  425. struct context *tcontext,
  426. u16 tclass,
  427. u32 specified,
  428. struct context *newcontext,
  429. bool sock)
  430. {
  431. struct range_trans rtr;
  432. struct mls_range *r;
  433. struct class_datum *cladatum;
  434. int default_range = 0;
  435. if (!p->mls_enabled)
  436. return 0;
  437. switch (specified) {
  438. case AVTAB_TRANSITION:
  439. /* Look for a range transition rule. */
  440. rtr.source_type = scontext->type;
  441. rtr.target_type = tcontext->type;
  442. rtr.target_class = tclass;
  443. r = policydb_rangetr_search(p, &rtr);
  444. if (r)
  445. return mls_range_set(newcontext, r);
  446. if (tclass && tclass <= p->p_classes.nprim) {
  447. cladatum = p->class_val_to_struct[tclass - 1];
  448. if (cladatum)
  449. default_range = cladatum->default_range;
  450. }
  451. switch (default_range) {
  452. case DEFAULT_SOURCE_LOW:
  453. return mls_context_cpy_low(newcontext, scontext);
  454. case DEFAULT_SOURCE_HIGH:
  455. return mls_context_cpy_high(newcontext, scontext);
  456. case DEFAULT_SOURCE_LOW_HIGH:
  457. return mls_context_cpy(newcontext, scontext);
  458. case DEFAULT_TARGET_LOW:
  459. return mls_context_cpy_low(newcontext, tcontext);
  460. case DEFAULT_TARGET_HIGH:
  461. return mls_context_cpy_high(newcontext, tcontext);
  462. case DEFAULT_TARGET_LOW_HIGH:
  463. return mls_context_cpy(newcontext, tcontext);
  464. case DEFAULT_GLBLUB:
  465. return mls_context_glblub(newcontext,
  466. scontext, tcontext);
  467. }
  468. fallthrough;
  469. case AVTAB_CHANGE:
  470. if ((tclass == p->process_class) || sock)
  471. /* Use the process MLS attributes. */
  472. return mls_context_cpy(newcontext, scontext);
  473. else
  474. /* Use the process effective MLS attributes. */
  475. return mls_context_cpy_low(newcontext, scontext);
  476. case AVTAB_MEMBER:
  477. /* Use the process effective MLS attributes. */
  478. return mls_context_cpy_low(newcontext, scontext);
  479. }
  480. return -EINVAL;
  481. }
  482. #ifdef CONFIG_NETLABEL
  483. /**
  484. * mls_export_netlbl_lvl - Export the MLS sensitivity levels to NetLabel
  485. * @p: the policy
  486. * @context: the security context
  487. * @secattr: the NetLabel security attributes
  488. *
  489. * Description:
  490. * Given the security context copy the low MLS sensitivity level into the
  491. * NetLabel MLS sensitivity level field.
  492. *
  493. */
  494. void mls_export_netlbl_lvl(struct policydb *p,
  495. struct context *context,
  496. struct netlbl_lsm_secattr *secattr)
  497. {
  498. if (!p->mls_enabled)
  499. return;
  500. secattr->attr.mls.lvl = context->range.level[0].sens - 1;
  501. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  502. }
  503. /**
  504. * mls_import_netlbl_lvl - Import the NetLabel MLS sensitivity levels
  505. * @p: the policy
  506. * @context: the security context
  507. * @secattr: the NetLabel security attributes
  508. *
  509. * Description:
  510. * Given the security context and the NetLabel security attributes, copy the
  511. * NetLabel MLS sensitivity level into the context.
  512. *
  513. */
  514. void mls_import_netlbl_lvl(struct policydb *p,
  515. struct context *context,
  516. struct netlbl_lsm_secattr *secattr)
  517. {
  518. if (!p->mls_enabled)
  519. return;
  520. context->range.level[0].sens = secattr->attr.mls.lvl + 1;
  521. context->range.level[1].sens = context->range.level[0].sens;
  522. }
  523. /**
  524. * mls_export_netlbl_cat - Export the MLS categories to NetLabel
  525. * @p: the policy
  526. * @context: the security context
  527. * @secattr: the NetLabel security attributes
  528. *
  529. * Description:
  530. * Given the security context copy the low MLS categories into the NetLabel
  531. * MLS category field. Returns zero on success, negative values on failure.
  532. *
  533. */
  534. int mls_export_netlbl_cat(struct policydb *p,
  535. struct context *context,
  536. struct netlbl_lsm_secattr *secattr)
  537. {
  538. int rc;
  539. if (!p->mls_enabled)
  540. return 0;
  541. rc = ebitmap_netlbl_export(&context->range.level[0].cat,
  542. &secattr->attr.mls.cat);
  543. if (rc == 0 && secattr->attr.mls.cat != NULL)
  544. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  545. return rc;
  546. }
  547. /**
  548. * mls_import_netlbl_cat - Import the MLS categories from NetLabel
  549. * @p: the policy
  550. * @context: the security context
  551. * @secattr: the NetLabel security attributes
  552. *
  553. * Description:
  554. * Copy the NetLabel security attributes into the SELinux context; since the
  555. * NetLabel security attribute only contains a single MLS category use it for
  556. * both the low and high categories of the context. Returns zero on success,
  557. * negative values on failure.
  558. *
  559. */
  560. int mls_import_netlbl_cat(struct policydb *p,
  561. struct context *context,
  562. struct netlbl_lsm_secattr *secattr)
  563. {
  564. int rc;
  565. if (!p->mls_enabled)
  566. return 0;
  567. rc = ebitmap_netlbl_import(&context->range.level[0].cat,
  568. secattr->attr.mls.cat);
  569. if (rc)
  570. goto import_netlbl_cat_failure;
  571. memcpy(&context->range.level[1].cat, &context->range.level[0].cat,
  572. sizeof(context->range.level[0].cat));
  573. return 0;
  574. import_netlbl_cat_failure:
  575. ebitmap_destroy(&context->range.level[0].cat);
  576. return rc;
  577. }
  578. #endif /* CONFIG_NETLABEL */