mls.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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
  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 "sidtab.h"
  25. #include "mls.h"
  26. #include "policydb.h"
  27. #include "services.h"
  28. /*
  29. * Return the length in bytes for the MLS fields of the
  30. * security context string representation of `context'.
  31. */
  32. int mls_compute_context_len(struct context * context)
  33. {
  34. int i, l, len, range;
  35. struct ebitmap_node *node;
  36. if (!selinux_mls_enabled)
  37. return 0;
  38. len = 1; /* for the beginning ":" */
  39. for (l = 0; l < 2; l++) {
  40. range = 0;
  41. len += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  42. ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
  43. if (ebitmap_node_get_bit(node, i)) {
  44. if (range) {
  45. range++;
  46. continue;
  47. }
  48. len += strlen(policydb.p_cat_val_to_name[i]) + 1;
  49. range++;
  50. } else {
  51. if (range > 1)
  52. len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1;
  53. range = 0;
  54. }
  55. }
  56. /* Handle case where last category is the end of range */
  57. if (range > 1)
  58. len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1;
  59. if (l == 0) {
  60. if (mls_level_eq(&context->range.level[0],
  61. &context->range.level[1]))
  62. break;
  63. else
  64. len++;
  65. }
  66. }
  67. return len;
  68. }
  69. /*
  70. * Write the security context string representation of
  71. * the MLS fields of `context' into the string `*scontext'.
  72. * Update `*scontext' to point to the end of the MLS fields.
  73. */
  74. void mls_sid_to_context(struct context *context,
  75. char **scontext)
  76. {
  77. char *scontextp;
  78. int i, l, range, wrote_sep;
  79. struct ebitmap_node *node;
  80. if (!selinux_mls_enabled)
  81. return;
  82. scontextp = *scontext;
  83. *scontextp = ':';
  84. scontextp++;
  85. for (l = 0; l < 2; l++) {
  86. range = 0;
  87. wrote_sep = 0;
  88. strcpy(scontextp,
  89. policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  90. scontextp += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  91. /* categories */
  92. ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
  93. if (ebitmap_node_get_bit(node, i)) {
  94. if (range) {
  95. range++;
  96. continue;
  97. }
  98. if (!wrote_sep) {
  99. *scontextp++ = ':';
  100. wrote_sep = 1;
  101. } else
  102. *scontextp++ = ',';
  103. strcpy(scontextp, policydb.p_cat_val_to_name[i]);
  104. scontextp += strlen(policydb.p_cat_val_to_name[i]);
  105. range++;
  106. } else {
  107. if (range > 1) {
  108. if (range > 2)
  109. *scontextp++ = '.';
  110. else
  111. *scontextp++ = ',';
  112. strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
  113. scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
  114. }
  115. range = 0;
  116. }
  117. }
  118. /* Handle case where last category is the end of range */
  119. if (range > 1) {
  120. if (range > 2)
  121. *scontextp++ = '.';
  122. else
  123. *scontextp++ = ',';
  124. strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
  125. scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
  126. }
  127. if (l == 0) {
  128. if (mls_level_eq(&context->range.level[0],
  129. &context->range.level[1]))
  130. break;
  131. else {
  132. *scontextp = '-';
  133. scontextp++;
  134. }
  135. }
  136. }
  137. *scontext = scontextp;
  138. return;
  139. }
  140. /*
  141. * Return 1 if the MLS fields in the security context
  142. * structure `c' are valid. Return 0 otherwise.
  143. */
  144. int mls_context_isvalid(struct policydb *p, struct context *c)
  145. {
  146. struct level_datum *levdatum;
  147. struct user_datum *usrdatum;
  148. struct ebitmap_node *node;
  149. int i, l;
  150. if (!selinux_mls_enabled)
  151. return 1;
  152. /*
  153. * MLS range validity checks: high must dominate low, low level must
  154. * be valid (category set <-> sensitivity check), and high level must
  155. * be valid (category set <-> sensitivity check)
  156. */
  157. if (!mls_level_dom(&c->range.level[1], &c->range.level[0]))
  158. /* High does not dominate low. */
  159. return 0;
  160. for (l = 0; l < 2; l++) {
  161. if (!c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim)
  162. return 0;
  163. levdatum = hashtab_search(p->p_levels.table,
  164. p->p_sens_val_to_name[c->range.level[l].sens - 1]);
  165. if (!levdatum)
  166. return 0;
  167. ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
  168. if (ebitmap_node_get_bit(node, i)) {
  169. if (i > p->p_cats.nprim)
  170. return 0;
  171. if (!ebitmap_get_bit(&levdatum->level->cat, i))
  172. /*
  173. * Category may not be associated with
  174. * sensitivity in low level.
  175. */
  176. return 0;
  177. }
  178. }
  179. }
  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(char oldc,
  211. char **scontext,
  212. struct context *context,
  213. struct sidtab *s,
  214. u32 def_sid)
  215. {
  216. char delim;
  217. char *scontextp, *p, *rngptr;
  218. struct level_datum *levdatum;
  219. struct cat_datum *catdatum, *rngdatum;
  220. int l, rc = -EINVAL;
  221. if (!selinux_mls_enabled) {
  222. if (def_sid != SECSID_NULL && oldc)
  223. *scontext += strlen(*scontext)+1;
  224. return 0;
  225. }
  226. /*
  227. * No MLS component to the security context, try and map to
  228. * default if provided.
  229. */
  230. if (!oldc) {
  231. struct context *defcon;
  232. if (def_sid == SECSID_NULL)
  233. goto out;
  234. defcon = sidtab_search(s, def_sid);
  235. if (!defcon)
  236. goto out;
  237. rc = mls_copy_context(context, defcon);
  238. goto out;
  239. }
  240. /* Extract low sensitivity. */
  241. scontextp = p = *scontext;
  242. while (*p && *p != ':' && *p != '-')
  243. p++;
  244. delim = *p;
  245. if (delim != 0)
  246. *p++ = 0;
  247. for (l = 0; l < 2; l++) {
  248. levdatum = hashtab_search(policydb.p_levels.table, scontextp);
  249. if (!levdatum) {
  250. rc = -EINVAL;
  251. goto out;
  252. }
  253. context->range.level[l].sens = levdatum->level->sens;
  254. if (delim == ':') {
  255. /* Extract category set. */
  256. while (1) {
  257. scontextp = p;
  258. while (*p && *p != ',' && *p != '-')
  259. p++;
  260. delim = *p;
  261. if (delim != 0)
  262. *p++ = 0;
  263. /* Separate into range if exists */
  264. if ((rngptr = strchr(scontextp, '.')) != NULL) {
  265. /* Remove '.' */
  266. *rngptr++ = 0;
  267. }
  268. catdatum = hashtab_search(policydb.p_cats.table,
  269. scontextp);
  270. if (!catdatum) {
  271. rc = -EINVAL;
  272. goto out;
  273. }
  274. rc = ebitmap_set_bit(&context->range.level[l].cat,
  275. catdatum->value - 1, 1);
  276. if (rc)
  277. goto out;
  278. /* If range, set all categories in range */
  279. if (rngptr) {
  280. int i;
  281. rngdatum = hashtab_search(policydb.p_cats.table, rngptr);
  282. if (!rngdatum) {
  283. rc = -EINVAL;
  284. goto out;
  285. }
  286. if (catdatum->value >= rngdatum->value) {
  287. rc = -EINVAL;
  288. goto out;
  289. }
  290. for (i = catdatum->value; i < rngdatum->value; i++) {
  291. rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
  292. if (rc)
  293. goto out;
  294. }
  295. }
  296. if (delim != ',')
  297. break;
  298. }
  299. }
  300. if (delim == '-') {
  301. /* Extract high sensitivity. */
  302. scontextp = p;
  303. while (*p && *p != ':')
  304. p++;
  305. delim = *p;
  306. if (delim != 0)
  307. *p++ = 0;
  308. } else
  309. break;
  310. }
  311. if (l == 0) {
  312. context->range.level[1].sens = context->range.level[0].sens;
  313. rc = ebitmap_cpy(&context->range.level[1].cat,
  314. &context->range.level[0].cat);
  315. if (rc)
  316. goto out;
  317. }
  318. *scontext = ++p;
  319. rc = 0;
  320. out:
  321. return rc;
  322. }
  323. /*
  324. * Set the MLS fields in the security context structure
  325. * `context' based on the string representation in
  326. * the string `str'. This function will allocate temporary memory with the
  327. * given constraints of gfp_mask.
  328. */
  329. int mls_from_string(char *str, struct context *context, gfp_t gfp_mask)
  330. {
  331. char *tmpstr, *freestr;
  332. int rc;
  333. if (!selinux_mls_enabled)
  334. return -EINVAL;
  335. /* we need freestr because mls_context_to_sid will change
  336. the value of tmpstr */
  337. tmpstr = freestr = kstrdup(str, gfp_mask);
  338. if (!tmpstr) {
  339. rc = -ENOMEM;
  340. } else {
  341. rc = mls_context_to_sid(':', &tmpstr, context,
  342. NULL, SECSID_NULL);
  343. kfree(freestr);
  344. }
  345. return rc;
  346. }
  347. /*
  348. * Copies the effective MLS range from `src' into `dst'.
  349. */
  350. static inline int mls_scopy_context(struct context *dst,
  351. struct context *src)
  352. {
  353. int l, rc = 0;
  354. /* Copy the MLS range from the source context */
  355. for (l = 0; l < 2; l++) {
  356. dst->range.level[l].sens = src->range.level[0].sens;
  357. rc = ebitmap_cpy(&dst->range.level[l].cat,
  358. &src->range.level[0].cat);
  359. if (rc)
  360. break;
  361. }
  362. return rc;
  363. }
  364. /*
  365. * Copies the MLS range `range' into `context'.
  366. */
  367. static inline int mls_range_set(struct context *context,
  368. struct mls_range *range)
  369. {
  370. int l, rc = 0;
  371. /* Copy the MLS range into the context */
  372. for (l = 0; l < 2; l++) {
  373. context->range.level[l].sens = range->level[l].sens;
  374. rc = ebitmap_cpy(&context->range.level[l].cat,
  375. &range->level[l].cat);
  376. if (rc)
  377. break;
  378. }
  379. return rc;
  380. }
  381. int mls_setup_user_range(struct context *fromcon, struct user_datum *user,
  382. struct context *usercon)
  383. {
  384. if (selinux_mls_enabled) {
  385. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  386. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  387. struct mls_level *user_low = &(user->range.level[0]);
  388. struct mls_level *user_clr = &(user->range.level[1]);
  389. struct mls_level *user_def = &(user->dfltlevel);
  390. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  391. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  392. /* Honor the user's default level if we can */
  393. if (mls_level_between(user_def, fromcon_sen, fromcon_clr)) {
  394. *usercon_sen = *user_def;
  395. } else if (mls_level_between(fromcon_sen, user_def, user_clr)) {
  396. *usercon_sen = *fromcon_sen;
  397. } else if (mls_level_between(fromcon_clr, user_low, user_def)) {
  398. *usercon_sen = *user_low;
  399. } else
  400. return -EINVAL;
  401. /* Lower the clearance of available contexts
  402. if the clearance of "fromcon" is lower than
  403. that of the user's default clearance (but
  404. only if the "fromcon" clearance dominates
  405. the user's computed sensitivity level) */
  406. if (mls_level_dom(user_clr, fromcon_clr)) {
  407. *usercon_clr = *fromcon_clr;
  408. } else if (mls_level_dom(fromcon_clr, user_clr)) {
  409. *usercon_clr = *user_clr;
  410. } else
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. /*
  416. * Convert the MLS fields in the security context
  417. * structure `c' from the values specified in the
  418. * policy `oldp' to the values specified in the policy `newp'.
  419. */
  420. int mls_convert_context(struct policydb *oldp,
  421. struct policydb *newp,
  422. struct context *c)
  423. {
  424. struct level_datum *levdatum;
  425. struct cat_datum *catdatum;
  426. struct ebitmap bitmap;
  427. struct ebitmap_node *node;
  428. int l, i;
  429. if (!selinux_mls_enabled)
  430. return 0;
  431. for (l = 0; l < 2; l++) {
  432. levdatum = hashtab_search(newp->p_levels.table,
  433. oldp->p_sens_val_to_name[c->range.level[l].sens - 1]);
  434. if (!levdatum)
  435. return -EINVAL;
  436. c->range.level[l].sens = levdatum->level->sens;
  437. ebitmap_init(&bitmap);
  438. ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
  439. if (ebitmap_node_get_bit(node, i)) {
  440. int rc;
  441. catdatum = hashtab_search(newp->p_cats.table,
  442. oldp->p_cat_val_to_name[i]);
  443. if (!catdatum)
  444. return -EINVAL;
  445. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  446. if (rc)
  447. return rc;
  448. }
  449. }
  450. ebitmap_destroy(&c->range.level[l].cat);
  451. c->range.level[l].cat = bitmap;
  452. }
  453. return 0;
  454. }
  455. int mls_compute_sid(struct context *scontext,
  456. struct context *tcontext,
  457. u16 tclass,
  458. u32 specified,
  459. struct context *newcontext)
  460. {
  461. struct range_trans *rtr;
  462. if (!selinux_mls_enabled)
  463. return 0;
  464. switch (specified) {
  465. case AVTAB_TRANSITION:
  466. /* Look for a range transition rule. */
  467. for (rtr = policydb.range_tr; rtr; rtr = rtr->next) {
  468. if (rtr->source_type == scontext->type &&
  469. rtr->target_type == tcontext->type &&
  470. rtr->target_class == tclass) {
  471. /* Set the range from the rule */
  472. return mls_range_set(newcontext,
  473. &rtr->target_range);
  474. }
  475. }
  476. /* Fallthrough */
  477. case AVTAB_CHANGE:
  478. if (tclass == SECCLASS_PROCESS)
  479. /* Use the process MLS attributes. */
  480. return mls_copy_context(newcontext, scontext);
  481. else
  482. /* Use the process effective MLS attributes. */
  483. return mls_scopy_context(newcontext, scontext);
  484. case AVTAB_MEMBER:
  485. /* Only polyinstantiate the MLS attributes if
  486. the type is being polyinstantiated */
  487. if (newcontext->type != tcontext->type) {
  488. /* Use the process effective MLS attributes. */
  489. return mls_scopy_context(newcontext, scontext);
  490. } else {
  491. /* Use the related object MLS attributes. */
  492. return mls_copy_context(newcontext, tcontext);
  493. }
  494. default:
  495. return -EINVAL;
  496. }
  497. return -EINVAL;
  498. }
  499. /**
  500. * mls_export_lvl - Export the MLS sensitivity levels
  501. * @context: the security context
  502. * @low: the low sensitivity level
  503. * @high: the high sensitivity level
  504. *
  505. * Description:
  506. * Given the security context copy the low MLS sensitivity level into lvl_low
  507. * and the high sensitivity level in lvl_high. The MLS levels are only
  508. * exported if the pointers are not NULL, if they are NULL then that level is
  509. * not exported.
  510. *
  511. */
  512. void mls_export_lvl(const struct context *context, u32 *low, u32 *high)
  513. {
  514. if (!selinux_mls_enabled)
  515. return;
  516. if (low != NULL)
  517. *low = context->range.level[0].sens - 1;
  518. if (high != NULL)
  519. *high = context->range.level[1].sens - 1;
  520. }
  521. /**
  522. * mls_import_lvl - Import the MLS sensitivity levels
  523. * @context: the security context
  524. * @low: the low sensitivity level
  525. * @high: the high sensitivity level
  526. *
  527. * Description:
  528. * Given the security context and the two sensitivty levels, set the MLS levels
  529. * in the context according the two given as parameters. Returns zero on
  530. * success, negative values on failure.
  531. *
  532. */
  533. void mls_import_lvl(struct context *context, u32 low, u32 high)
  534. {
  535. if (!selinux_mls_enabled)
  536. return;
  537. context->range.level[0].sens = low + 1;
  538. context->range.level[1].sens = high + 1;
  539. }
  540. /**
  541. * mls_export_cat - Export the MLS categories
  542. * @context: the security context
  543. * @low: the low category
  544. * @low_len: length of the cat_low bitmap in bytes
  545. * @high: the high category
  546. * @high_len: length of the cat_high bitmap in bytes
  547. *
  548. * Description:
  549. * Given the security context export the low MLS category bitmap into cat_low
  550. * and the high category bitmap into cat_high. The MLS categories are only
  551. * exported if the pointers are not NULL, if they are NULL then that level is
  552. * not exported. The caller is responsibile for freeing the memory when
  553. * finished. Returns zero on success, negative values on failure.
  554. *
  555. */
  556. int mls_export_cat(const struct context *context,
  557. unsigned char **low,
  558. size_t *low_len,
  559. unsigned char **high,
  560. size_t *high_len)
  561. {
  562. int rc = -EPERM;
  563. if (!selinux_mls_enabled) {
  564. *low = NULL;
  565. *low_len = 0;
  566. *high = NULL;
  567. *high_len = 0;
  568. return 0;
  569. }
  570. if (low != NULL) {
  571. rc = ebitmap_export(&context->range.level[0].cat,
  572. low,
  573. low_len);
  574. if (rc != 0)
  575. goto export_cat_failure;
  576. }
  577. if (high != NULL) {
  578. rc = ebitmap_export(&context->range.level[1].cat,
  579. high,
  580. high_len);
  581. if (rc != 0)
  582. goto export_cat_failure;
  583. }
  584. return 0;
  585. export_cat_failure:
  586. if (low != NULL) {
  587. kfree(*low);
  588. *low = NULL;
  589. *low_len = 0;
  590. }
  591. if (high != NULL) {
  592. kfree(*high);
  593. *high = NULL;
  594. *high_len = 0;
  595. }
  596. return rc;
  597. }
  598. /**
  599. * mls_import_cat - Import the MLS categories
  600. * @context: the security context
  601. * @low: the low category
  602. * @low_len: length of the cat_low bitmap in bytes
  603. * @high: the high category
  604. * @high_len: length of the cat_high bitmap in bytes
  605. *
  606. * Description:
  607. * Given the security context and the two category bitmap strings import the
  608. * categories into the security context. The MLS categories are only imported
  609. * if the pointers are not NULL, if they are NULL they are skipped. Returns
  610. * zero on success, negative values on failure.
  611. *
  612. */
  613. int mls_import_cat(struct context *context,
  614. const unsigned char *low,
  615. size_t low_len,
  616. const unsigned char *high,
  617. size_t high_len)
  618. {
  619. int rc = -EPERM;
  620. if (!selinux_mls_enabled)
  621. return 0;
  622. if (low != NULL) {
  623. rc = ebitmap_import(low,
  624. low_len,
  625. &context->range.level[0].cat);
  626. if (rc != 0)
  627. goto import_cat_failure;
  628. }
  629. if (high != NULL) {
  630. if (high == low)
  631. rc = ebitmap_cpy(&context->range.level[1].cat,
  632. &context->range.level[0].cat);
  633. else
  634. rc = ebitmap_import(high,
  635. high_len,
  636. &context->range.level[1].cat);
  637. if (rc != 0)
  638. goto import_cat_failure;
  639. }
  640. return 0;
  641. import_cat_failure:
  642. ebitmap_destroy(&context->range.level[0].cat);
  643. ebitmap_destroy(&context->range.level[1].cat);
  644. return rc;
  645. }