mls.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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@paul-moore.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 (!policydb.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(sym_name(&policydb, SYM_LEVELS, 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 = sym_name(&policydb, SYM_CATS, prev);
  54. len += strlen(nm) + 1;
  55. }
  56. nm = sym_name(&policydb, SYM_CATS, i);
  57. len += strlen(nm) + 1;
  58. head = i;
  59. }
  60. prev = i;
  61. }
  62. if (prev != head) {
  63. nm = sym_name(&policydb, SYM_CATS, 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 (!policydb.mls_enabled)
  89. return;
  90. scontextp = *scontext;
  91. *scontextp = ':';
  92. scontextp++;
  93. for (l = 0; l < 2; l++) {
  94. strcpy(scontextp, sym_name(&policydb, SYM_LEVELS,
  95. 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 = sym_name(&policydb, SYM_CATS, prev);
  110. strcpy(scontextp, nm);
  111. scontextp += strlen(nm);
  112. }
  113. if (prev < 0)
  114. *scontextp++ = ':';
  115. else
  116. *scontextp++ = ',';
  117. nm = sym_name(&policydb, SYM_CATS, 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 = sym_name(&policydb, SYM_CATS, 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. int mls_level_isvalid(struct policydb *p, struct mls_level *l)
  145. {
  146. struct level_datum *levdatum;
  147. struct ebitmap_node *node;
  148. int i;
  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. ebitmap_for_each_positive_bit(&l->cat, node, i) {
  156. if (i > p->p_cats.nprim)
  157. return 0;
  158. if (!ebitmap_get_bit(&levdatum->level->cat, i)) {
  159. /*
  160. * Category may not be associated with
  161. * sensitivity.
  162. */
  163. return 0;
  164. }
  165. }
  166. return 1;
  167. }
  168. int mls_range_isvalid(struct policydb *p, struct mls_range *r)
  169. {
  170. return (mls_level_isvalid(p, &r->level[0]) &&
  171. mls_level_isvalid(p, &r->level[1]) &&
  172. mls_level_dom(&r->level[1], &r->level[0]));
  173. }
  174. /*
  175. * Return 1 if the MLS fields in the security context
  176. * structure `c' are valid. Return 0 otherwise.
  177. */
  178. int mls_context_isvalid(struct policydb *p, struct context *c)
  179. {
  180. struct user_datum *usrdatum;
  181. if (!p->mls_enabled)
  182. return 1;
  183. if (!mls_range_isvalid(p, &c->range))
  184. return 0;
  185. if (c->role == OBJECT_R_VAL)
  186. return 1;
  187. /*
  188. * User must be authorized for the MLS range.
  189. */
  190. if (!c->user || c->user > p->p_users.nprim)
  191. return 0;
  192. usrdatum = p->user_val_to_struct[c->user - 1];
  193. if (!mls_range_contains(usrdatum->range, c->range))
  194. return 0; /* user may not be associated with range */
  195. return 1;
  196. }
  197. /*
  198. * Set the MLS fields in the security context structure
  199. * `context' based on the string representation in
  200. * the string `*scontext'. Update `*scontext' to
  201. * point to the end of the string representation of
  202. * the MLS fields.
  203. *
  204. * This function modifies the string in place, inserting
  205. * NULL characters to terminate the MLS fields.
  206. *
  207. * If a def_sid is provided and no MLS field is present,
  208. * copy the MLS field of the associated default context.
  209. * Used for upgraded to MLS systems where objects may lack
  210. * MLS fields.
  211. *
  212. * Policy read-lock must be held for sidtab lookup.
  213. *
  214. */
  215. int mls_context_to_sid(struct policydb *pol,
  216. char oldc,
  217. char **scontext,
  218. struct context *context,
  219. struct sidtab *s,
  220. u32 def_sid)
  221. {
  222. char delim;
  223. char *scontextp, *p, *rngptr;
  224. struct level_datum *levdatum;
  225. struct cat_datum *catdatum, *rngdatum;
  226. int l, rc = -EINVAL;
  227. if (!pol->mls_enabled) {
  228. if (def_sid != SECSID_NULL && oldc)
  229. *scontext += strlen(*scontext) + 1;
  230. return 0;
  231. }
  232. /*
  233. * No MLS component to the security context, try and map to
  234. * default if provided.
  235. */
  236. if (!oldc) {
  237. struct context *defcon;
  238. if (def_sid == SECSID_NULL)
  239. goto out;
  240. defcon = sidtab_search(s, def_sid);
  241. if (!defcon)
  242. goto out;
  243. rc = mls_context_cpy(context, defcon);
  244. goto out;
  245. }
  246. /* Extract low sensitivity. */
  247. scontextp = p = *scontext;
  248. while (*p && *p != ':' && *p != '-')
  249. p++;
  250. delim = *p;
  251. if (delim != '\0')
  252. *p++ = '\0';
  253. for (l = 0; l < 2; l++) {
  254. levdatum = hashtab_search(pol->p_levels.table, scontextp);
  255. if (!levdatum) {
  256. rc = -EINVAL;
  257. goto out;
  258. }
  259. context->range.level[l].sens = levdatum->level->sens;
  260. if (delim == ':') {
  261. /* Extract category set. */
  262. while (1) {
  263. scontextp = p;
  264. while (*p && *p != ',' && *p != '-')
  265. p++;
  266. delim = *p;
  267. if (delim != '\0')
  268. *p++ = '\0';
  269. /* Separate into range if exists */
  270. rngptr = strchr(scontextp, '.');
  271. if (rngptr != NULL) {
  272. /* Remove '.' */
  273. *rngptr++ = '\0';
  274. }
  275. catdatum = hashtab_search(pol->p_cats.table,
  276. scontextp);
  277. if (!catdatum) {
  278. rc = -EINVAL;
  279. goto out;
  280. }
  281. rc = ebitmap_set_bit(&context->range.level[l].cat,
  282. catdatum->value - 1, 1);
  283. if (rc)
  284. goto out;
  285. /* If range, set all categories in range */
  286. if (rngptr) {
  287. int i;
  288. rngdatum = hashtab_search(pol->p_cats.table, rngptr);
  289. if (!rngdatum) {
  290. rc = -EINVAL;
  291. goto out;
  292. }
  293. if (catdatum->value >= rngdatum->value) {
  294. rc = -EINVAL;
  295. goto out;
  296. }
  297. for (i = catdatum->value; i < rngdatum->value; i++) {
  298. rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
  299. if (rc)
  300. goto out;
  301. }
  302. }
  303. if (delim != ',')
  304. break;
  305. }
  306. }
  307. if (delim == '-') {
  308. /* Extract high sensitivity. */
  309. scontextp = p;
  310. while (*p && *p != ':')
  311. p++;
  312. delim = *p;
  313. if (delim != '\0')
  314. *p++ = '\0';
  315. } else
  316. break;
  317. }
  318. if (l == 0) {
  319. context->range.level[1].sens = context->range.level[0].sens;
  320. rc = ebitmap_cpy(&context->range.level[1].cat,
  321. &context->range.level[0].cat);
  322. if (rc)
  323. goto out;
  324. }
  325. *scontext = ++p;
  326. rc = 0;
  327. out:
  328. return rc;
  329. }
  330. /*
  331. * Set the MLS fields in the security context structure
  332. * `context' based on the string representation in
  333. * the string `str'. This function will allocate temporary memory with the
  334. * given constraints of gfp_mask.
  335. */
  336. int mls_from_string(char *str, struct context *context, gfp_t gfp_mask)
  337. {
  338. char *tmpstr, *freestr;
  339. int rc;
  340. if (!policydb.mls_enabled)
  341. return -EINVAL;
  342. /* we need freestr because mls_context_to_sid will change
  343. the value of tmpstr */
  344. tmpstr = freestr = kstrdup(str, gfp_mask);
  345. if (!tmpstr) {
  346. rc = -ENOMEM;
  347. } else {
  348. rc = mls_context_to_sid(&policydb, ':', &tmpstr, context,
  349. NULL, SECSID_NULL);
  350. kfree(freestr);
  351. }
  352. return rc;
  353. }
  354. /*
  355. * Copies the MLS range `range' into `context'.
  356. */
  357. int mls_range_set(struct context *context,
  358. struct mls_range *range)
  359. {
  360. int l, rc = 0;
  361. /* Copy the MLS range into the context */
  362. for (l = 0; l < 2; l++) {
  363. context->range.level[l].sens = range->level[l].sens;
  364. rc = ebitmap_cpy(&context->range.level[l].cat,
  365. &range->level[l].cat);
  366. if (rc)
  367. break;
  368. }
  369. return rc;
  370. }
  371. int mls_setup_user_range(struct context *fromcon, struct user_datum *user,
  372. struct context *usercon)
  373. {
  374. if (policydb.mls_enabled) {
  375. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  376. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  377. struct mls_level *user_low = &(user->range.level[0]);
  378. struct mls_level *user_clr = &(user->range.level[1]);
  379. struct mls_level *user_def = &(user->dfltlevel);
  380. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  381. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  382. /* Honor the user's default level if we can */
  383. if (mls_level_between(user_def, fromcon_sen, fromcon_clr))
  384. *usercon_sen = *user_def;
  385. else if (mls_level_between(fromcon_sen, user_def, user_clr))
  386. *usercon_sen = *fromcon_sen;
  387. else if (mls_level_between(fromcon_clr, user_low, user_def))
  388. *usercon_sen = *user_low;
  389. else
  390. return -EINVAL;
  391. /* Lower the clearance of available contexts
  392. if the clearance of "fromcon" is lower than
  393. that of the user's default clearance (but
  394. only if the "fromcon" clearance dominates
  395. the user's computed sensitivity level) */
  396. if (mls_level_dom(user_clr, fromcon_clr))
  397. *usercon_clr = *fromcon_clr;
  398. else if (mls_level_dom(fromcon_clr, user_clr))
  399. *usercon_clr = *user_clr;
  400. else
  401. return -EINVAL;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * Convert the MLS fields in the security context
  407. * structure `c' from the values specified in the
  408. * policy `oldp' to the values specified in the policy `newp'.
  409. */
  410. int mls_convert_context(struct policydb *oldp,
  411. struct policydb *newp,
  412. struct context *c)
  413. {
  414. struct level_datum *levdatum;
  415. struct cat_datum *catdatum;
  416. struct ebitmap bitmap;
  417. struct ebitmap_node *node;
  418. int l, i;
  419. if (!policydb.mls_enabled)
  420. return 0;
  421. for (l = 0; l < 2; l++) {
  422. levdatum = hashtab_search(newp->p_levels.table,
  423. sym_name(oldp, SYM_LEVELS,
  424. c->range.level[l].sens - 1));
  425. if (!levdatum)
  426. return -EINVAL;
  427. c->range.level[l].sens = levdatum->level->sens;
  428. ebitmap_init(&bitmap);
  429. ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
  430. int rc;
  431. catdatum = hashtab_search(newp->p_cats.table,
  432. sym_name(oldp, SYM_CATS, i));
  433. if (!catdatum)
  434. return -EINVAL;
  435. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  436. if (rc)
  437. return rc;
  438. }
  439. ebitmap_destroy(&c->range.level[l].cat);
  440. c->range.level[l].cat = bitmap;
  441. }
  442. return 0;
  443. }
  444. int mls_compute_sid(struct context *scontext,
  445. struct context *tcontext,
  446. u16 tclass,
  447. u32 specified,
  448. struct context *newcontext,
  449. bool sock)
  450. {
  451. struct range_trans rtr;
  452. struct mls_range *r;
  453. struct class_datum *cladatum;
  454. int default_range = 0;
  455. if (!policydb.mls_enabled)
  456. return 0;
  457. switch (specified) {
  458. case AVTAB_TRANSITION:
  459. /* Look for a range transition rule. */
  460. rtr.source_type = scontext->type;
  461. rtr.target_type = tcontext->type;
  462. rtr.target_class = tclass;
  463. r = hashtab_search(policydb.range_tr, &rtr);
  464. if (r)
  465. return mls_range_set(newcontext, r);
  466. if (tclass && tclass <= policydb.p_classes.nprim) {
  467. cladatum = policydb.class_val_to_struct[tclass - 1];
  468. if (cladatum)
  469. default_range = cladatum->default_range;
  470. }
  471. switch (default_range) {
  472. case DEFAULT_SOURCE_LOW:
  473. return mls_context_cpy_low(newcontext, scontext);
  474. case DEFAULT_SOURCE_HIGH:
  475. return mls_context_cpy_high(newcontext, scontext);
  476. case DEFAULT_SOURCE_LOW_HIGH:
  477. return mls_context_cpy(newcontext, scontext);
  478. case DEFAULT_TARGET_LOW:
  479. return mls_context_cpy_low(newcontext, tcontext);
  480. case DEFAULT_TARGET_HIGH:
  481. return mls_context_cpy_high(newcontext, tcontext);
  482. case DEFAULT_TARGET_LOW_HIGH:
  483. return mls_context_cpy(newcontext, tcontext);
  484. }
  485. /* Fallthrough */
  486. case AVTAB_CHANGE:
  487. if ((tclass == policydb.process_class) || (sock == true))
  488. /* Use the process MLS attributes. */
  489. return mls_context_cpy(newcontext, scontext);
  490. else
  491. /* Use the process effective MLS attributes. */
  492. return mls_context_cpy_low(newcontext, scontext);
  493. case AVTAB_MEMBER:
  494. /* Use the process effective MLS attributes. */
  495. return mls_context_cpy_low(newcontext, scontext);
  496. /* fall through */
  497. }
  498. return -EINVAL;
  499. }
  500. #ifdef CONFIG_NETLABEL
  501. /**
  502. * mls_export_netlbl_lvl - Export the MLS sensitivity levels to NetLabel
  503. * @context: the security context
  504. * @secattr: the NetLabel security attributes
  505. *
  506. * Description:
  507. * Given the security context copy the low MLS sensitivity level into the
  508. * NetLabel MLS sensitivity level field.
  509. *
  510. */
  511. void mls_export_netlbl_lvl(struct context *context,
  512. struct netlbl_lsm_secattr *secattr)
  513. {
  514. if (!policydb.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 context *context,
  530. struct netlbl_lsm_secattr *secattr)
  531. {
  532. if (!policydb.mls_enabled)
  533. return;
  534. context->range.level[0].sens = secattr->attr.mls.lvl + 1;
  535. context->range.level[1].sens = context->range.level[0].sens;
  536. }
  537. /**
  538. * mls_export_netlbl_cat - Export the MLS categories to NetLabel
  539. * @context: the security context
  540. * @secattr: the NetLabel security attributes
  541. *
  542. * Description:
  543. * Given the security context copy the low MLS categories into the NetLabel
  544. * MLS category field. Returns zero on success, negative values on failure.
  545. *
  546. */
  547. int mls_export_netlbl_cat(struct context *context,
  548. struct netlbl_lsm_secattr *secattr)
  549. {
  550. int rc;
  551. if (!policydb.mls_enabled)
  552. return 0;
  553. rc = ebitmap_netlbl_export(&context->range.level[0].cat,
  554. &secattr->attr.mls.cat);
  555. if (rc == 0 && secattr->attr.mls.cat != NULL)
  556. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  557. return rc;
  558. }
  559. /**
  560. * mls_import_netlbl_cat - Import the MLS categories from NetLabel
  561. * @context: the security context
  562. * @secattr: the NetLabel security attributes
  563. *
  564. * Description:
  565. * Copy the NetLabel security attributes into the SELinux context; since the
  566. * NetLabel security attribute only contains a single MLS category use it for
  567. * both the low and high categories of the context. Returns zero on success,
  568. * negative values on failure.
  569. *
  570. */
  571. int mls_import_netlbl_cat(struct context *context,
  572. struct netlbl_lsm_secattr *secattr)
  573. {
  574. int rc;
  575. if (!policydb.mls_enabled)
  576. return 0;
  577. rc = ebitmap_netlbl_import(&context->range.level[0].cat,
  578. secattr->attr.mls.cat);
  579. if (rc != 0)
  580. goto import_netlbl_cat_failure;
  581. rc = ebitmap_cpy(&context->range.level[1].cat,
  582. &context->range.level[0].cat);
  583. if (rc != 0)
  584. goto import_netlbl_cat_failure;
  585. return 0;
  586. import_netlbl_cat_failure:
  587. ebitmap_destroy(&context->range.level[0].cat);
  588. ebitmap_destroy(&context->range.level[1].cat);
  589. return rc;
  590. }
  591. #endif /* CONFIG_NETLABEL */