mls.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. if (!l->sens || l->sens > p->p_levels.nprim)
  148. return 0;
  149. levdatum = hashtab_search(p->p_levels.table,
  150. sym_name(p, SYM_LEVELS, l->sens - 1));
  151. if (!levdatum)
  152. return 0;
  153. /*
  154. * Return 1 iff all the bits set in l->cat are also be set in
  155. * levdatum->level->cat and no bit in l->cat is larger than
  156. * p->p_cats.nprim.
  157. */
  158. return ebitmap_contains(&levdatum->level->cat, &l->cat,
  159. p->p_cats.nprim);
  160. }
  161. int mls_range_isvalid(struct policydb *p, struct mls_range *r)
  162. {
  163. return (mls_level_isvalid(p, &r->level[0]) &&
  164. mls_level_isvalid(p, &r->level[1]) &&
  165. mls_level_dom(&r->level[1], &r->level[0]));
  166. }
  167. /*
  168. * Return 1 if the MLS fields in the security context
  169. * structure `c' are valid. Return 0 otherwise.
  170. */
  171. int mls_context_isvalid(struct policydb *p, struct context *c)
  172. {
  173. struct user_datum *usrdatum;
  174. if (!p->mls_enabled)
  175. return 1;
  176. if (!mls_range_isvalid(p, &c->range))
  177. return 0;
  178. if (c->role == OBJECT_R_VAL)
  179. return 1;
  180. /*
  181. * User must be authorized for the MLS range.
  182. */
  183. if (!c->user || c->user > p->p_users.nprim)
  184. return 0;
  185. usrdatum = p->user_val_to_struct[c->user - 1];
  186. if (!mls_range_contains(usrdatum->range, c->range))
  187. return 0; /* user may not be associated with range */
  188. return 1;
  189. }
  190. /*
  191. * Set the MLS fields in the security context structure
  192. * `context' based on the string representation in
  193. * the string `*scontext'. Update `*scontext' to
  194. * point to the end of the string representation of
  195. * the MLS fields.
  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 delim;
  216. char *scontextp, *p, *rngptr;
  217. struct level_datum *levdatum;
  218. struct cat_datum *catdatum, *rngdatum;
  219. int l, rc = -EINVAL;
  220. if (!pol->mls_enabled) {
  221. if (def_sid != SECSID_NULL && oldc)
  222. *scontext += strlen(*scontext) + 1;
  223. return 0;
  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. goto out;
  233. defcon = sidtab_search(s, def_sid);
  234. if (!defcon)
  235. goto out;
  236. rc = mls_context_cpy(context, defcon);
  237. goto out;
  238. }
  239. /* Extract low sensitivity. */
  240. scontextp = p = *scontext;
  241. while (*p && *p != ':' && *p != '-')
  242. p++;
  243. delim = *p;
  244. if (delim != '\0')
  245. *p++ = '\0';
  246. for (l = 0; l < 2; l++) {
  247. levdatum = hashtab_search(pol->p_levels.table, scontextp);
  248. if (!levdatum) {
  249. rc = -EINVAL;
  250. goto out;
  251. }
  252. context->range.level[l].sens = levdatum->level->sens;
  253. if (delim == ':') {
  254. /* Extract category set. */
  255. while (1) {
  256. scontextp = p;
  257. while (*p && *p != ',' && *p != '-')
  258. p++;
  259. delim = *p;
  260. if (delim != '\0')
  261. *p++ = '\0';
  262. /* Separate into range if exists */
  263. rngptr = strchr(scontextp, '.');
  264. if (rngptr != NULL) {
  265. /* Remove '.' */
  266. *rngptr++ = '\0';
  267. }
  268. catdatum = hashtab_search(pol->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(pol->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 (!policydb.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(&policydb, ':', &tmpstr, context,
  342. NULL, SECSID_NULL);
  343. kfree(freestr);
  344. }
  345. return rc;
  346. }
  347. /*
  348. * Copies the MLS range `range' into `context'.
  349. */
  350. int mls_range_set(struct context *context,
  351. struct mls_range *range)
  352. {
  353. int l, rc = 0;
  354. /* Copy the MLS range into the context */
  355. for (l = 0; l < 2; l++) {
  356. context->range.level[l].sens = range->level[l].sens;
  357. rc = ebitmap_cpy(&context->range.level[l].cat,
  358. &range->level[l].cat);
  359. if (rc)
  360. break;
  361. }
  362. return rc;
  363. }
  364. int mls_setup_user_range(struct context *fromcon, struct user_datum *user,
  365. struct context *usercon)
  366. {
  367. if (policydb.mls_enabled) {
  368. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  369. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  370. struct mls_level *user_low = &(user->range.level[0]);
  371. struct mls_level *user_clr = &(user->range.level[1]);
  372. struct mls_level *user_def = &(user->dfltlevel);
  373. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  374. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  375. /* Honor the user's default level if we can */
  376. if (mls_level_between(user_def, fromcon_sen, fromcon_clr))
  377. *usercon_sen = *user_def;
  378. else if (mls_level_between(fromcon_sen, user_def, user_clr))
  379. *usercon_sen = *fromcon_sen;
  380. else if (mls_level_between(fromcon_clr, user_low, user_def))
  381. *usercon_sen = *user_low;
  382. else
  383. return -EINVAL;
  384. /* Lower the clearance of available contexts
  385. if the clearance of "fromcon" is lower than
  386. that of the user's default clearance (but
  387. only if the "fromcon" clearance dominates
  388. the user's computed sensitivity level) */
  389. if (mls_level_dom(user_clr, fromcon_clr))
  390. *usercon_clr = *fromcon_clr;
  391. else if (mls_level_dom(fromcon_clr, user_clr))
  392. *usercon_clr = *user_clr;
  393. else
  394. return -EINVAL;
  395. }
  396. return 0;
  397. }
  398. /*
  399. * Convert the MLS fields in the security context
  400. * structure `c' from the values specified in the
  401. * policy `oldp' to the values specified in the policy `newp'.
  402. */
  403. int mls_convert_context(struct policydb *oldp,
  404. struct policydb *newp,
  405. struct context *c)
  406. {
  407. struct level_datum *levdatum;
  408. struct cat_datum *catdatum;
  409. struct ebitmap bitmap;
  410. struct ebitmap_node *node;
  411. int l, i;
  412. if (!policydb.mls_enabled)
  413. return 0;
  414. for (l = 0; l < 2; l++) {
  415. levdatum = hashtab_search(newp->p_levels.table,
  416. sym_name(oldp, SYM_LEVELS,
  417. c->range.level[l].sens - 1));
  418. if (!levdatum)
  419. return -EINVAL;
  420. c->range.level[l].sens = levdatum->level->sens;
  421. ebitmap_init(&bitmap);
  422. ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
  423. int rc;
  424. catdatum = hashtab_search(newp->p_cats.table,
  425. sym_name(oldp, SYM_CATS, i));
  426. if (!catdatum)
  427. return -EINVAL;
  428. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  429. if (rc)
  430. return rc;
  431. cond_resched();
  432. }
  433. ebitmap_destroy(&c->range.level[l].cat);
  434. c->range.level[l].cat = bitmap;
  435. }
  436. return 0;
  437. }
  438. int mls_compute_sid(struct context *scontext,
  439. struct context *tcontext,
  440. u16 tclass,
  441. u32 specified,
  442. struct context *newcontext,
  443. bool sock)
  444. {
  445. struct range_trans rtr;
  446. struct mls_range *r;
  447. struct class_datum *cladatum;
  448. int default_range = 0;
  449. if (!policydb.mls_enabled)
  450. return 0;
  451. switch (specified) {
  452. case AVTAB_TRANSITION:
  453. /* Look for a range transition rule. */
  454. rtr.source_type = scontext->type;
  455. rtr.target_type = tcontext->type;
  456. rtr.target_class = tclass;
  457. r = hashtab_search(policydb.range_tr, &rtr);
  458. if (r)
  459. return mls_range_set(newcontext, r);
  460. if (tclass && tclass <= policydb.p_classes.nprim) {
  461. cladatum = policydb.class_val_to_struct[tclass - 1];
  462. if (cladatum)
  463. default_range = cladatum->default_range;
  464. }
  465. switch (default_range) {
  466. case DEFAULT_SOURCE_LOW:
  467. return mls_context_cpy_low(newcontext, scontext);
  468. case DEFAULT_SOURCE_HIGH:
  469. return mls_context_cpy_high(newcontext, scontext);
  470. case DEFAULT_SOURCE_LOW_HIGH:
  471. return mls_context_cpy(newcontext, scontext);
  472. case DEFAULT_TARGET_LOW:
  473. return mls_context_cpy_low(newcontext, tcontext);
  474. case DEFAULT_TARGET_HIGH:
  475. return mls_context_cpy_high(newcontext, tcontext);
  476. case DEFAULT_TARGET_LOW_HIGH:
  477. return mls_context_cpy(newcontext, tcontext);
  478. }
  479. /* Fallthrough */
  480. case AVTAB_CHANGE:
  481. if ((tclass == policydb.process_class) || (sock == true))
  482. /* Use the process MLS attributes. */
  483. return mls_context_cpy(newcontext, scontext);
  484. else
  485. /* Use the process effective MLS attributes. */
  486. return mls_context_cpy_low(newcontext, scontext);
  487. case AVTAB_MEMBER:
  488. /* Use the process effective MLS attributes. */
  489. return mls_context_cpy_low(newcontext, scontext);
  490. /* fall through */
  491. }
  492. return -EINVAL;
  493. }
  494. #ifdef CONFIG_NETLABEL
  495. /**
  496. * mls_export_netlbl_lvl - Export the MLS sensitivity levels to NetLabel
  497. * @context: the security context
  498. * @secattr: the NetLabel security attributes
  499. *
  500. * Description:
  501. * Given the security context copy the low MLS sensitivity level into the
  502. * NetLabel MLS sensitivity level field.
  503. *
  504. */
  505. void mls_export_netlbl_lvl(struct context *context,
  506. struct netlbl_lsm_secattr *secattr)
  507. {
  508. if (!policydb.mls_enabled)
  509. return;
  510. secattr->attr.mls.lvl = context->range.level[0].sens - 1;
  511. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  512. }
  513. /**
  514. * mls_import_netlbl_lvl - Import the NetLabel MLS sensitivity levels
  515. * @context: the security context
  516. * @secattr: the NetLabel security attributes
  517. *
  518. * Description:
  519. * Given the security context and the NetLabel security attributes, copy the
  520. * NetLabel MLS sensitivity level into the context.
  521. *
  522. */
  523. void mls_import_netlbl_lvl(struct context *context,
  524. struct netlbl_lsm_secattr *secattr)
  525. {
  526. if (!policydb.mls_enabled)
  527. return;
  528. context->range.level[0].sens = secattr->attr.mls.lvl + 1;
  529. context->range.level[1].sens = context->range.level[0].sens;
  530. }
  531. /**
  532. * mls_export_netlbl_cat - Export the MLS categories to NetLabel
  533. * @context: the security context
  534. * @secattr: the NetLabel security attributes
  535. *
  536. * Description:
  537. * Given the security context copy the low MLS categories into the NetLabel
  538. * MLS category field. Returns zero on success, negative values on failure.
  539. *
  540. */
  541. int mls_export_netlbl_cat(struct context *context,
  542. struct netlbl_lsm_secattr *secattr)
  543. {
  544. int rc;
  545. if (!policydb.mls_enabled)
  546. return 0;
  547. rc = ebitmap_netlbl_export(&context->range.level[0].cat,
  548. &secattr->attr.mls.cat);
  549. if (rc == 0 && secattr->attr.mls.cat != NULL)
  550. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  551. return rc;
  552. }
  553. /**
  554. * mls_import_netlbl_cat - Import the MLS categories from NetLabel
  555. * @context: the security context
  556. * @secattr: the NetLabel security attributes
  557. *
  558. * Description:
  559. * Copy the NetLabel security attributes into the SELinux context; since the
  560. * NetLabel security attribute only contains a single MLS category use it for
  561. * both the low and high categories of the context. Returns zero on success,
  562. * negative values on failure.
  563. *
  564. */
  565. int mls_import_netlbl_cat(struct context *context,
  566. struct netlbl_lsm_secattr *secattr)
  567. {
  568. int rc;
  569. if (!policydb.mls_enabled)
  570. return 0;
  571. rc = ebitmap_netlbl_import(&context->range.level[0].cat,
  572. secattr->attr.mls.cat);
  573. if (rc)
  574. goto import_netlbl_cat_failure;
  575. memcpy(&context->range.level[1].cat, &context->range.level[0].cat,
  576. sizeof(context->range.level[0].cat));
  577. return 0;
  578. import_netlbl_cat_failure:
  579. ebitmap_destroy(&context->range.level[0].cat);
  580. return rc;
  581. }
  582. #endif /* CONFIG_NETLABEL */