mls.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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'. Update `*scontext' to
  196. * point to the end of the string representation of
  197. * the MLS fields.
  198. *
  199. * This function modifies the string in place, inserting
  200. * NULL characters to terminate the MLS fields.
  201. *
  202. * If a def_sid is provided and no MLS field is present,
  203. * copy the MLS field of the associated default context.
  204. * Used for upgraded to MLS systems where objects may lack
  205. * MLS fields.
  206. *
  207. * Policy read-lock must be held for sidtab lookup.
  208. *
  209. */
  210. int mls_context_to_sid(struct policydb *pol,
  211. char oldc,
  212. char **scontext,
  213. struct context *context,
  214. struct sidtab *s,
  215. u32 def_sid)
  216. {
  217. char delim;
  218. char *scontextp, *p, *rngptr;
  219. struct level_datum *levdatum;
  220. struct cat_datum *catdatum, *rngdatum;
  221. int l, rc = -EINVAL;
  222. if (!pol->mls_enabled) {
  223. if (def_sid != SECSID_NULL && oldc)
  224. *scontext += strlen(*scontext) + 1;
  225. return 0;
  226. }
  227. /*
  228. * No MLS component to the security context, try and map to
  229. * default if provided.
  230. */
  231. if (!oldc) {
  232. struct context *defcon;
  233. if (def_sid == SECSID_NULL)
  234. goto out;
  235. defcon = sidtab_search(s, def_sid);
  236. if (!defcon)
  237. goto out;
  238. rc = mls_context_cpy(context, defcon);
  239. goto out;
  240. }
  241. /* Extract low sensitivity. */
  242. scontextp = p = *scontext;
  243. while (*p && *p != ':' && *p != '-')
  244. p++;
  245. delim = *p;
  246. if (delim != '\0')
  247. *p++ = '\0';
  248. for (l = 0; l < 2; l++) {
  249. levdatum = hashtab_search(pol->p_levels.table, scontextp);
  250. if (!levdatum) {
  251. rc = -EINVAL;
  252. goto out;
  253. }
  254. context->range.level[l].sens = levdatum->level->sens;
  255. if (delim == ':') {
  256. /* Extract category set. */
  257. while (1) {
  258. scontextp = p;
  259. while (*p && *p != ',' && *p != '-')
  260. p++;
  261. delim = *p;
  262. if (delim != '\0')
  263. *p++ = '\0';
  264. /* Separate into range if exists */
  265. rngptr = strchr(scontextp, '.');
  266. if (rngptr != NULL) {
  267. /* Remove '.' */
  268. *rngptr++ = '\0';
  269. }
  270. catdatum = hashtab_search(pol->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(pol->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(struct policydb *p, char *str, struct context *context,
  332. gfp_t gfp_mask)
  333. {
  334. char *tmpstr, *freestr;
  335. int rc;
  336. if (!p->mls_enabled)
  337. return -EINVAL;
  338. /* we need freestr because mls_context_to_sid will change
  339. the value of tmpstr */
  340. tmpstr = freestr = kstrdup(str, gfp_mask);
  341. if (!tmpstr) {
  342. rc = -ENOMEM;
  343. } else {
  344. rc = mls_context_to_sid(p, ':', &tmpstr, context,
  345. NULL, SECSID_NULL);
  346. kfree(freestr);
  347. }
  348. return rc;
  349. }
  350. /*
  351. * Copies the MLS range `range' into `context'.
  352. */
  353. int mls_range_set(struct context *context,
  354. struct mls_range *range)
  355. {
  356. int l, rc = 0;
  357. /* Copy the MLS range into the context */
  358. for (l = 0; l < 2; l++) {
  359. context->range.level[l].sens = range->level[l].sens;
  360. rc = ebitmap_cpy(&context->range.level[l].cat,
  361. &range->level[l].cat);
  362. if (rc)
  363. break;
  364. }
  365. return rc;
  366. }
  367. int mls_setup_user_range(struct policydb *p,
  368. struct context *fromcon, struct user_datum *user,
  369. struct context *usercon)
  370. {
  371. if (p->mls_enabled) {
  372. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  373. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  374. struct mls_level *user_low = &(user->range.level[0]);
  375. struct mls_level *user_clr = &(user->range.level[1]);
  376. struct mls_level *user_def = &(user->dfltlevel);
  377. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  378. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  379. /* Honor the user's default level if we can */
  380. if (mls_level_between(user_def, fromcon_sen, fromcon_clr))
  381. *usercon_sen = *user_def;
  382. else if (mls_level_between(fromcon_sen, user_def, user_clr))
  383. *usercon_sen = *fromcon_sen;
  384. else if (mls_level_between(fromcon_clr, user_low, user_def))
  385. *usercon_sen = *user_low;
  386. else
  387. return -EINVAL;
  388. /* Lower the clearance of available contexts
  389. if the clearance of "fromcon" is lower than
  390. that of the user's default clearance (but
  391. only if the "fromcon" clearance dominates
  392. the user's computed sensitivity level) */
  393. if (mls_level_dom(user_clr, fromcon_clr))
  394. *usercon_clr = *fromcon_clr;
  395. else if (mls_level_dom(fromcon_clr, user_clr))
  396. *usercon_clr = *user_clr;
  397. else
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. /*
  403. * Convert the MLS fields in the security context
  404. * structure `c' from the values specified in the
  405. * policy `oldp' to the values specified in the policy `newp'.
  406. */
  407. int mls_convert_context(struct policydb *oldp,
  408. struct policydb *newp,
  409. struct context *c)
  410. {
  411. struct level_datum *levdatum;
  412. struct cat_datum *catdatum;
  413. struct ebitmap bitmap;
  414. struct ebitmap_node *node;
  415. int l, i;
  416. if (!oldp->mls_enabled || !newp->mls_enabled)
  417. return 0;
  418. for (l = 0; l < 2; l++) {
  419. levdatum = hashtab_search(newp->p_levels.table,
  420. sym_name(oldp, SYM_LEVELS,
  421. c->range.level[l].sens - 1));
  422. if (!levdatum)
  423. return -EINVAL;
  424. c->range.level[l].sens = levdatum->level->sens;
  425. ebitmap_init(&bitmap);
  426. ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
  427. int rc;
  428. catdatum = hashtab_search(newp->p_cats.table,
  429. sym_name(oldp, SYM_CATS, i));
  430. if (!catdatum)
  431. return -EINVAL;
  432. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  433. if (rc)
  434. return rc;
  435. cond_resched();
  436. }
  437. ebitmap_destroy(&c->range.level[l].cat);
  438. c->range.level[l].cat = bitmap;
  439. }
  440. return 0;
  441. }
  442. int mls_compute_sid(struct policydb *p,
  443. struct context *scontext,
  444. struct context *tcontext,
  445. u16 tclass,
  446. u32 specified,
  447. struct context *newcontext,
  448. bool sock)
  449. {
  450. struct range_trans rtr;
  451. struct mls_range *r;
  452. struct class_datum *cladatum;
  453. int default_range = 0;
  454. if (!p->mls_enabled)
  455. return 0;
  456. switch (specified) {
  457. case AVTAB_TRANSITION:
  458. /* Look for a range transition rule. */
  459. rtr.source_type = scontext->type;
  460. rtr.target_type = tcontext->type;
  461. rtr.target_class = tclass;
  462. r = hashtab_search(p->range_tr, &rtr);
  463. if (r)
  464. return mls_range_set(newcontext, r);
  465. if (tclass && tclass <= p->p_classes.nprim) {
  466. cladatum = p->class_val_to_struct[tclass - 1];
  467. if (cladatum)
  468. default_range = cladatum->default_range;
  469. }
  470. switch (default_range) {
  471. case DEFAULT_SOURCE_LOW:
  472. return mls_context_cpy_low(newcontext, scontext);
  473. case DEFAULT_SOURCE_HIGH:
  474. return mls_context_cpy_high(newcontext, scontext);
  475. case DEFAULT_SOURCE_LOW_HIGH:
  476. return mls_context_cpy(newcontext, scontext);
  477. case DEFAULT_TARGET_LOW:
  478. return mls_context_cpy_low(newcontext, tcontext);
  479. case DEFAULT_TARGET_HIGH:
  480. return mls_context_cpy_high(newcontext, tcontext);
  481. case DEFAULT_TARGET_LOW_HIGH:
  482. return mls_context_cpy(newcontext, tcontext);
  483. }
  484. /* Fallthrough */
  485. case AVTAB_CHANGE:
  486. if ((tclass == p->process_class) || (sock == true))
  487. /* Use the process MLS attributes. */
  488. return mls_context_cpy(newcontext, scontext);
  489. else
  490. /* Use the process effective MLS attributes. */
  491. return mls_context_cpy_low(newcontext, scontext);
  492. case AVTAB_MEMBER:
  493. /* Use the process effective MLS attributes. */
  494. return mls_context_cpy_low(newcontext, scontext);
  495. /* fall through */
  496. }
  497. return -EINVAL;
  498. }
  499. #ifdef CONFIG_NETLABEL
  500. /**
  501. * mls_export_netlbl_lvl - Export the MLS sensitivity levels to NetLabel
  502. * @context: the security context
  503. * @secattr: the NetLabel security attributes
  504. *
  505. * Description:
  506. * Given the security context copy the low MLS sensitivity level into the
  507. * NetLabel MLS sensitivity level field.
  508. *
  509. */
  510. void mls_export_netlbl_lvl(struct policydb *p,
  511. struct context *context,
  512. struct netlbl_lsm_secattr *secattr)
  513. {
  514. if (!p->mls_enabled)
  515. return;
  516. secattr->attr.mls.lvl = context->range.level[0].sens - 1;
  517. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  518. }
  519. /**
  520. * mls_import_netlbl_lvl - Import the NetLabel MLS sensitivity levels
  521. * @context: the security context
  522. * @secattr: the NetLabel security attributes
  523. *
  524. * Description:
  525. * Given the security context and the NetLabel security attributes, copy the
  526. * NetLabel MLS sensitivity level into the context.
  527. *
  528. */
  529. void mls_import_netlbl_lvl(struct policydb *p,
  530. struct context *context,
  531. struct netlbl_lsm_secattr *secattr)
  532. {
  533. if (!p->mls_enabled)
  534. return;
  535. context->range.level[0].sens = secattr->attr.mls.lvl + 1;
  536. context->range.level[1].sens = context->range.level[0].sens;
  537. }
  538. /**
  539. * mls_export_netlbl_cat - Export the MLS categories to NetLabel
  540. * @context: the security context
  541. * @secattr: the NetLabel security attributes
  542. *
  543. * Description:
  544. * Given the security context copy the low MLS categories into the NetLabel
  545. * MLS category field. Returns zero on success, negative values on failure.
  546. *
  547. */
  548. int mls_export_netlbl_cat(struct policydb *p,
  549. struct context *context,
  550. struct netlbl_lsm_secattr *secattr)
  551. {
  552. int rc;
  553. if (!p->mls_enabled)
  554. return 0;
  555. rc = ebitmap_netlbl_export(&context->range.level[0].cat,
  556. &secattr->attr.mls.cat);
  557. if (rc == 0 && secattr->attr.mls.cat != NULL)
  558. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  559. return rc;
  560. }
  561. /**
  562. * mls_import_netlbl_cat - Import the MLS categories from NetLabel
  563. * @context: the security context
  564. * @secattr: the NetLabel security attributes
  565. *
  566. * Description:
  567. * Copy the NetLabel security attributes into the SELinux context; since the
  568. * NetLabel security attribute only contains a single MLS category use it for
  569. * both the low and high categories of the context. Returns zero on success,
  570. * negative values on failure.
  571. *
  572. */
  573. int mls_import_netlbl_cat(struct policydb *p,
  574. struct context *context,
  575. struct netlbl_lsm_secattr *secattr)
  576. {
  577. int rc;
  578. if (!p->mls_enabled)
  579. return 0;
  580. rc = ebitmap_netlbl_import(&context->range.level[0].cat,
  581. secattr->attr.mls.cat);
  582. if (rc)
  583. goto import_netlbl_cat_failure;
  584. memcpy(&context->range.level[1].cat, &context->range.level[0].cat,
  585. sizeof(context->range.level[0].cat));
  586. return 0;
  587. import_netlbl_cat_failure:
  588. ebitmap_destroy(&context->range.level[0].cat);
  589. return rc;
  590. }
  591. #endif /* CONFIG_NETLABEL */