mls.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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-2005 Trusted Computer Solutions, Inc.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include "sidtab.h"
  18. #include "mls.h"
  19. #include "policydb.h"
  20. #include "services.h"
  21. /*
  22. * Return the length in bytes for the MLS fields of the
  23. * security context string representation of `context'.
  24. */
  25. int mls_compute_context_len(struct context * context)
  26. {
  27. int i, l, len, range;
  28. struct ebitmap_node *node;
  29. if (!selinux_mls_enabled)
  30. return 0;
  31. len = 1; /* for the beginning ":" */
  32. for (l = 0; l < 2; l++) {
  33. range = 0;
  34. len += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  35. ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
  36. if (ebitmap_node_get_bit(node, i)) {
  37. if (range) {
  38. range++;
  39. continue;
  40. }
  41. len += strlen(policydb.p_cat_val_to_name[i]) + 1;
  42. range++;
  43. } else {
  44. if (range > 1)
  45. len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1;
  46. range = 0;
  47. }
  48. }
  49. /* Handle case where last category is the end of range */
  50. if (range > 1)
  51. len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1;
  52. if (l == 0) {
  53. if (mls_level_eq(&context->range.level[0],
  54. &context->range.level[1]))
  55. break;
  56. else
  57. len++;
  58. }
  59. }
  60. return len;
  61. }
  62. /*
  63. * Write the security context string representation of
  64. * the MLS fields of `context' into the string `*scontext'.
  65. * Update `*scontext' to point to the end of the MLS fields.
  66. */
  67. void mls_sid_to_context(struct context *context,
  68. char **scontext)
  69. {
  70. char *scontextp;
  71. int i, l, range, wrote_sep;
  72. struct ebitmap_node *node;
  73. if (!selinux_mls_enabled)
  74. return;
  75. scontextp = *scontext;
  76. *scontextp = ':';
  77. scontextp++;
  78. for (l = 0; l < 2; l++) {
  79. range = 0;
  80. wrote_sep = 0;
  81. strcpy(scontextp,
  82. policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  83. scontextp += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
  84. /* categories */
  85. ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
  86. if (ebitmap_node_get_bit(node, i)) {
  87. if (range) {
  88. range++;
  89. continue;
  90. }
  91. if (!wrote_sep) {
  92. *scontextp++ = ':';
  93. wrote_sep = 1;
  94. } else
  95. *scontextp++ = ',';
  96. strcpy(scontextp, policydb.p_cat_val_to_name[i]);
  97. scontextp += strlen(policydb.p_cat_val_to_name[i]);
  98. range++;
  99. } else {
  100. if (range > 1) {
  101. if (range > 2)
  102. *scontextp++ = '.';
  103. else
  104. *scontextp++ = ',';
  105. strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
  106. scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
  107. }
  108. range = 0;
  109. }
  110. }
  111. /* Handle case where last category is the end of range */
  112. if (range > 1) {
  113. if (range > 2)
  114. *scontextp++ = '.';
  115. else
  116. *scontextp++ = ',';
  117. strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
  118. scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
  119. }
  120. if (l == 0) {
  121. if (mls_level_eq(&context->range.level[0],
  122. &context->range.level[1]))
  123. break;
  124. else {
  125. *scontextp = '-';
  126. scontextp++;
  127. }
  128. }
  129. }
  130. *scontext = scontextp;
  131. return;
  132. }
  133. /*
  134. * Return 1 if the MLS fields in the security context
  135. * structure `c' are valid. Return 0 otherwise.
  136. */
  137. int mls_context_isvalid(struct policydb *p, struct context *c)
  138. {
  139. struct level_datum *levdatum;
  140. struct user_datum *usrdatum;
  141. struct ebitmap_node *node;
  142. int i, l;
  143. if (!selinux_mls_enabled)
  144. return 1;
  145. /*
  146. * MLS range validity checks: high must dominate low, low level must
  147. * be valid (category set <-> sensitivity check), and high level must
  148. * be valid (category set <-> sensitivity check)
  149. */
  150. if (!mls_level_dom(&c->range.level[1], &c->range.level[0]))
  151. /* High does not dominate low. */
  152. return 0;
  153. for (l = 0; l < 2; l++) {
  154. if (!c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim)
  155. return 0;
  156. levdatum = hashtab_search(p->p_levels.table,
  157. p->p_sens_val_to_name[c->range.level[l].sens - 1]);
  158. if (!levdatum)
  159. return 0;
  160. ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
  161. if (ebitmap_node_get_bit(node, i)) {
  162. if (i > p->p_cats.nprim)
  163. return 0;
  164. if (!ebitmap_get_bit(&levdatum->level->cat, i))
  165. /*
  166. * Category may not be associated with
  167. * sensitivity in low level.
  168. */
  169. return 0;
  170. }
  171. }
  172. }
  173. if (c->role == OBJECT_R_VAL)
  174. return 1;
  175. /*
  176. * User must be authorized for the MLS range.
  177. */
  178. if (!c->user || c->user > p->p_users.nprim)
  179. return 0;
  180. usrdatum = p->user_val_to_struct[c->user - 1];
  181. if (!mls_range_contains(usrdatum->range, c->range))
  182. return 0; /* user may not be associated with range */
  183. return 1;
  184. }
  185. /*
  186. * Copies the MLS range from `src' into `dst'.
  187. */
  188. static inline int mls_copy_context(struct context *dst,
  189. struct context *src)
  190. {
  191. int l, rc = 0;
  192. /* Copy the MLS range from the source context */
  193. for (l = 0; l < 2; l++) {
  194. dst->range.level[l].sens = src->range.level[l].sens;
  195. rc = ebitmap_cpy(&dst->range.level[l].cat,
  196. &src->range.level[l].cat);
  197. if (rc)
  198. break;
  199. }
  200. return rc;
  201. }
  202. /*
  203. * Set the MLS fields in the security context structure
  204. * `context' based on the string representation in
  205. * the string `*scontext'. Update `*scontext' to
  206. * point to the end of the string representation of
  207. * the MLS fields.
  208. *
  209. * This function modifies the string in place, inserting
  210. * NULL characters to terminate the MLS fields.
  211. *
  212. * If a def_sid is provided and no MLS field is present,
  213. * copy the MLS field of the associated default context.
  214. * Used for upgraded to MLS systems where objects may lack
  215. * MLS fields.
  216. *
  217. * Policy read-lock must be held for sidtab lookup.
  218. *
  219. */
  220. int mls_context_to_sid(char oldc,
  221. char **scontext,
  222. struct context *context,
  223. struct sidtab *s,
  224. u32 def_sid)
  225. {
  226. char delim;
  227. char *scontextp, *p, *rngptr;
  228. struct level_datum *levdatum;
  229. struct cat_datum *catdatum, *rngdatum;
  230. int l, rc = -EINVAL;
  231. if (!selinux_mls_enabled) {
  232. if (def_sid != SECSID_NULL && oldc)
  233. *scontext += strlen(*scontext)+1;
  234. return 0;
  235. }
  236. /*
  237. * No MLS component to the security context, try and map to
  238. * default if provided.
  239. */
  240. if (!oldc) {
  241. struct context *defcon;
  242. if (def_sid == SECSID_NULL)
  243. goto out;
  244. defcon = sidtab_search(s, def_sid);
  245. if (!defcon)
  246. goto out;
  247. rc = mls_copy_context(context, defcon);
  248. goto out;
  249. }
  250. /* Extract low sensitivity. */
  251. scontextp = p = *scontext;
  252. while (*p && *p != ':' && *p != '-')
  253. p++;
  254. delim = *p;
  255. if (delim != 0)
  256. *p++ = 0;
  257. for (l = 0; l < 2; l++) {
  258. levdatum = hashtab_search(policydb.p_levels.table, scontextp);
  259. if (!levdatum) {
  260. rc = -EINVAL;
  261. goto out;
  262. }
  263. context->range.level[l].sens = levdatum->level->sens;
  264. if (delim == ':') {
  265. /* Extract category set. */
  266. while (1) {
  267. scontextp = p;
  268. while (*p && *p != ',' && *p != '-')
  269. p++;
  270. delim = *p;
  271. if (delim != 0)
  272. *p++ = 0;
  273. /* Separate into range if exists */
  274. if ((rngptr = strchr(scontextp, '.')) != NULL) {
  275. /* Remove '.' */
  276. *rngptr++ = 0;
  277. }
  278. catdatum = hashtab_search(policydb.p_cats.table,
  279. scontextp);
  280. if (!catdatum) {
  281. rc = -EINVAL;
  282. goto out;
  283. }
  284. rc = ebitmap_set_bit(&context->range.level[l].cat,
  285. catdatum->value - 1, 1);
  286. if (rc)
  287. goto out;
  288. /* If range, set all categories in range */
  289. if (rngptr) {
  290. int i;
  291. rngdatum = hashtab_search(policydb.p_cats.table, rngptr);
  292. if (!rngdatum) {
  293. rc = -EINVAL;
  294. goto out;
  295. }
  296. if (catdatum->value >= rngdatum->value) {
  297. rc = -EINVAL;
  298. goto out;
  299. }
  300. for (i = catdatum->value; i < rngdatum->value; i++) {
  301. rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
  302. if (rc)
  303. goto out;
  304. }
  305. }
  306. if (delim != ',')
  307. break;
  308. }
  309. }
  310. if (delim == '-') {
  311. /* Extract high sensitivity. */
  312. scontextp = p;
  313. while (*p && *p != ':')
  314. p++;
  315. delim = *p;
  316. if (delim != 0)
  317. *p++ = 0;
  318. } else
  319. break;
  320. }
  321. if (l == 0) {
  322. context->range.level[1].sens = context->range.level[0].sens;
  323. rc = ebitmap_cpy(&context->range.level[1].cat,
  324. &context->range.level[0].cat);
  325. if (rc)
  326. goto out;
  327. }
  328. *scontext = ++p;
  329. rc = 0;
  330. out:
  331. return rc;
  332. }
  333. /*
  334. * Copies the effective MLS range from `src' into `dst'.
  335. */
  336. static inline int mls_scopy_context(struct context *dst,
  337. struct context *src)
  338. {
  339. int l, rc = 0;
  340. /* Copy the MLS range from the source context */
  341. for (l = 0; l < 2; l++) {
  342. dst->range.level[l].sens = src->range.level[0].sens;
  343. rc = ebitmap_cpy(&dst->range.level[l].cat,
  344. &src->range.level[0].cat);
  345. if (rc)
  346. break;
  347. }
  348. return rc;
  349. }
  350. /*
  351. * Copies the MLS range `range' into `context'.
  352. */
  353. static inline 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 context *fromcon, struct user_datum *user,
  368. struct context *usercon)
  369. {
  370. if (selinux_mls_enabled) {
  371. struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
  372. struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
  373. struct mls_level *user_low = &(user->range.level[0]);
  374. struct mls_level *user_clr = &(user->range.level[1]);
  375. struct mls_level *user_def = &(user->dfltlevel);
  376. struct mls_level *usercon_sen = &(usercon->range.level[0]);
  377. struct mls_level *usercon_clr = &(usercon->range.level[1]);
  378. /* Honor the user's default level if we can */
  379. if (mls_level_between(user_def, fromcon_sen, fromcon_clr)) {
  380. *usercon_sen = *user_def;
  381. } else if (mls_level_between(fromcon_sen, user_def, user_clr)) {
  382. *usercon_sen = *fromcon_sen;
  383. } else if (mls_level_between(fromcon_clr, user_low, user_def)) {
  384. *usercon_sen = *user_low;
  385. } else
  386. return -EINVAL;
  387. /* Lower the clearance of available contexts
  388. if the clearance of "fromcon" is lower than
  389. that of the user's default clearance (but
  390. only if the "fromcon" clearance dominates
  391. the user's computed sensitivity level) */
  392. if (mls_level_dom(user_clr, fromcon_clr)) {
  393. *usercon_clr = *fromcon_clr;
  394. } else if (mls_level_dom(fromcon_clr, user_clr)) {
  395. *usercon_clr = *user_clr;
  396. } else
  397. return -EINVAL;
  398. }
  399. return 0;
  400. }
  401. /*
  402. * Convert the MLS fields in the security context
  403. * structure `c' from the values specified in the
  404. * policy `oldp' to the values specified in the policy `newp'.
  405. */
  406. int mls_convert_context(struct policydb *oldp,
  407. struct policydb *newp,
  408. struct context *c)
  409. {
  410. struct level_datum *levdatum;
  411. struct cat_datum *catdatum;
  412. struct ebitmap bitmap;
  413. struct ebitmap_node *node;
  414. int l, i;
  415. if (!selinux_mls_enabled)
  416. return 0;
  417. for (l = 0; l < 2; l++) {
  418. levdatum = hashtab_search(newp->p_levels.table,
  419. oldp->p_sens_val_to_name[c->range.level[l].sens - 1]);
  420. if (!levdatum)
  421. return -EINVAL;
  422. c->range.level[l].sens = levdatum->level->sens;
  423. ebitmap_init(&bitmap);
  424. ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
  425. if (ebitmap_node_get_bit(node, i)) {
  426. int rc;
  427. catdatum = hashtab_search(newp->p_cats.table,
  428. oldp->p_cat_val_to_name[i]);
  429. if (!catdatum)
  430. return -EINVAL;
  431. rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
  432. if (rc)
  433. return rc;
  434. }
  435. }
  436. ebitmap_destroy(&c->range.level[l].cat);
  437. c->range.level[l].cat = bitmap;
  438. }
  439. return 0;
  440. }
  441. int mls_compute_sid(struct context *scontext,
  442. struct context *tcontext,
  443. u16 tclass,
  444. u32 specified,
  445. struct context *newcontext)
  446. {
  447. if (!selinux_mls_enabled)
  448. return 0;
  449. switch (specified) {
  450. case AVTAB_TRANSITION:
  451. if (tclass == SECCLASS_PROCESS) {
  452. struct range_trans *rangetr;
  453. /* Look for a range transition rule. */
  454. for (rangetr = policydb.range_tr; rangetr;
  455. rangetr = rangetr->next) {
  456. if (rangetr->dom == scontext->type &&
  457. rangetr->type == tcontext->type) {
  458. /* Set the range from the rule */
  459. return mls_range_set(newcontext,
  460. &rangetr->range);
  461. }
  462. }
  463. }
  464. /* Fallthrough */
  465. case AVTAB_CHANGE:
  466. if (tclass == SECCLASS_PROCESS)
  467. /* Use the process MLS attributes. */
  468. return mls_copy_context(newcontext, scontext);
  469. else
  470. /* Use the process effective MLS attributes. */
  471. return mls_scopy_context(newcontext, scontext);
  472. case AVTAB_MEMBER:
  473. /* Only polyinstantiate the MLS attributes if
  474. the type is being polyinstantiated */
  475. if (newcontext->type != tcontext->type) {
  476. /* Use the process effective MLS attributes. */
  477. return mls_scopy_context(newcontext, scontext);
  478. } else {
  479. /* Use the related object MLS attributes. */
  480. return mls_copy_context(newcontext, tcontext);
  481. }
  482. default:
  483. return -EINVAL;
  484. }
  485. return -EINVAL;
  486. }