[SCSI] zfcp: remove zfcp_ccw_unregister function
[linux-2.6/x86.git] / security / selinux / ss / mls.c
blob7bc5b6440f70ba1ab45a66d0a7b3152d394096af
1 /*
2 * Implementation of the multi-level security (MLS) policy.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
6 /*
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
9 * Support for enhanced MLS infrastructure.
11 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include "sidtab.h"
19 #include "mls.h"
20 #include "policydb.h"
21 #include "services.h"
24 * Return the length in bytes for the MLS fields of the
25 * security context string representation of `context'.
27 int mls_compute_context_len(struct context * context)
29 int i, l, len, range;
30 struct ebitmap_node *node;
32 if (!selinux_mls_enabled)
33 return 0;
35 len = 1; /* for the beginning ":" */
36 for (l = 0; l < 2; l++) {
37 range = 0;
38 len += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
40 ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
41 if (ebitmap_node_get_bit(node, i)) {
42 if (range) {
43 range++;
44 continue;
47 len += strlen(policydb.p_cat_val_to_name[i]) + 1;
48 range++;
49 } else {
50 if (range > 1)
51 len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1;
52 range = 0;
55 /* Handle case where last category is the end of range */
56 if (range > 1)
57 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++;
68 return len;
72 * Write the security context string representation of
73 * the MLS fields of `context' into the string `*scontext'.
74 * Update `*scontext' to point to the end of the MLS fields.
76 void mls_sid_to_context(struct context *context,
77 char **scontext)
79 char *scontextp;
80 int i, l, range, wrote_sep;
81 struct ebitmap_node *node;
83 if (!selinux_mls_enabled)
84 return;
86 scontextp = *scontext;
88 *scontextp = ':';
89 scontextp++;
91 for (l = 0; l < 2; l++) {
92 range = 0;
93 wrote_sep = 0;
94 strcpy(scontextp,
95 policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
96 scontextp += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]);
98 /* categories */
99 ebitmap_for_each_bit(&context->range.level[l].cat, node, i) {
100 if (ebitmap_node_get_bit(node, i)) {
101 if (range) {
102 range++;
103 continue;
106 if (!wrote_sep) {
107 *scontextp++ = ':';
108 wrote_sep = 1;
109 } else
110 *scontextp++ = ',';
111 strcpy(scontextp, policydb.p_cat_val_to_name[i]);
112 scontextp += strlen(policydb.p_cat_val_to_name[i]);
113 range++;
114 } else {
115 if (range > 1) {
116 if (range > 2)
117 *scontextp++ = '.';
118 else
119 *scontextp++ = ',';
121 strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
122 scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
124 range = 0;
128 /* Handle case where last category is the end of range */
129 if (range > 1) {
130 if (range > 2)
131 *scontextp++ = '.';
132 else
133 *scontextp++ = ',';
135 strcpy(scontextp, policydb.p_cat_val_to_name[i - 1]);
136 scontextp += strlen(policydb.p_cat_val_to_name[i - 1]);
139 if (l == 0) {
140 if (mls_level_eq(&context->range.level[0],
141 &context->range.level[1]))
142 break;
143 else {
144 *scontextp = '-';
145 scontextp++;
150 *scontext = scontextp;
151 return;
155 * Return 1 if the MLS fields in the security context
156 * structure `c' are valid. Return 0 otherwise.
158 int mls_context_isvalid(struct policydb *p, struct context *c)
160 struct level_datum *levdatum;
161 struct user_datum *usrdatum;
162 struct ebitmap_node *node;
163 int i, l;
165 if (!selinux_mls_enabled)
166 return 1;
169 * MLS range validity checks: high must dominate low, low level must
170 * be valid (category set <-> sensitivity check), and high level must
171 * be valid (category set <-> sensitivity check)
173 if (!mls_level_dom(&c->range.level[1], &c->range.level[0]))
174 /* High does not dominate low. */
175 return 0;
177 for (l = 0; l < 2; l++) {
178 if (!c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim)
179 return 0;
180 levdatum = hashtab_search(p->p_levels.table,
181 p->p_sens_val_to_name[c->range.level[l].sens - 1]);
182 if (!levdatum)
183 return 0;
185 ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
186 if (ebitmap_node_get_bit(node, i)) {
187 if (i > p->p_cats.nprim)
188 return 0;
189 if (!ebitmap_get_bit(&levdatum->level->cat, i))
191 * Category may not be associated with
192 * sensitivity in low level.
194 return 0;
199 if (c->role == OBJECT_R_VAL)
200 return 1;
203 * User must be authorized for the MLS range.
205 if (!c->user || c->user > p->p_users.nprim)
206 return 0;
207 usrdatum = p->user_val_to_struct[c->user - 1];
208 if (!mls_range_contains(usrdatum->range, c->range))
209 return 0; /* user may not be associated with range */
211 return 1;
215 * Copies the MLS range from `src' into `dst'.
217 static inline int mls_copy_context(struct context *dst,
218 struct context *src)
220 int l, rc = 0;
222 /* Copy the MLS range from the source context */
223 for (l = 0; l < 2; l++) {
224 dst->range.level[l].sens = src->range.level[l].sens;
225 rc = ebitmap_cpy(&dst->range.level[l].cat,
226 &src->range.level[l].cat);
227 if (rc)
228 break;
231 return rc;
235 * Set the MLS fields in the security context structure
236 * `context' based on the string representation in
237 * the string `*scontext'. Update `*scontext' to
238 * point to the end of the string representation of
239 * the MLS fields.
241 * This function modifies the string in place, inserting
242 * NULL characters to terminate the MLS fields.
244 * If a def_sid is provided and no MLS field is present,
245 * copy the MLS field of the associated default context.
246 * Used for upgraded to MLS systems where objects may lack
247 * MLS fields.
249 * Policy read-lock must be held for sidtab lookup.
252 int mls_context_to_sid(char oldc,
253 char **scontext,
254 struct context *context,
255 struct sidtab *s,
256 u32 def_sid)
259 char delim;
260 char *scontextp, *p, *rngptr;
261 struct level_datum *levdatum;
262 struct cat_datum *catdatum, *rngdatum;
263 int l, rc = -EINVAL;
265 if (!selinux_mls_enabled) {
266 if (def_sid != SECSID_NULL && oldc)
267 *scontext += strlen(*scontext)+1;
268 return 0;
272 * No MLS component to the security context, try and map to
273 * default if provided.
275 if (!oldc) {
276 struct context *defcon;
278 if (def_sid == SECSID_NULL)
279 goto out;
281 defcon = sidtab_search(s, def_sid);
282 if (!defcon)
283 goto out;
285 rc = mls_copy_context(context, defcon);
286 goto out;
289 /* Extract low sensitivity. */
290 scontextp = p = *scontext;
291 while (*p && *p != ':' && *p != '-')
292 p++;
294 delim = *p;
295 if (delim != 0)
296 *p++ = 0;
298 for (l = 0; l < 2; l++) {
299 levdatum = hashtab_search(policydb.p_levels.table, scontextp);
300 if (!levdatum) {
301 rc = -EINVAL;
302 goto out;
305 context->range.level[l].sens = levdatum->level->sens;
307 if (delim == ':') {
308 /* Extract category set. */
309 while (1) {
310 scontextp = p;
311 while (*p && *p != ',' && *p != '-')
312 p++;
313 delim = *p;
314 if (delim != 0)
315 *p++ = 0;
317 /* Separate into range if exists */
318 if ((rngptr = strchr(scontextp, '.')) != NULL) {
319 /* Remove '.' */
320 *rngptr++ = 0;
323 catdatum = hashtab_search(policydb.p_cats.table,
324 scontextp);
325 if (!catdatum) {
326 rc = -EINVAL;
327 goto out;
330 rc = ebitmap_set_bit(&context->range.level[l].cat,
331 catdatum->value - 1, 1);
332 if (rc)
333 goto out;
335 /* If range, set all categories in range */
336 if (rngptr) {
337 int i;
339 rngdatum = hashtab_search(policydb.p_cats.table, rngptr);
340 if (!rngdatum) {
341 rc = -EINVAL;
342 goto out;
345 if (catdatum->value >= rngdatum->value) {
346 rc = -EINVAL;
347 goto out;
350 for (i = catdatum->value; i < rngdatum->value; i++) {
351 rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
352 if (rc)
353 goto out;
357 if (delim != ',')
358 break;
361 if (delim == '-') {
362 /* Extract high sensitivity. */
363 scontextp = p;
364 while (*p && *p != ':')
365 p++;
367 delim = *p;
368 if (delim != 0)
369 *p++ = 0;
370 } else
371 break;
374 if (l == 0) {
375 context->range.level[1].sens = context->range.level[0].sens;
376 rc = ebitmap_cpy(&context->range.level[1].cat,
377 &context->range.level[0].cat);
378 if (rc)
379 goto out;
381 *scontext = ++p;
382 rc = 0;
383 out:
384 return rc;
388 * Set the MLS fields in the security context structure
389 * `context' based on the string representation in
390 * the string `str'. This function will allocate temporary memory with the
391 * given constraints of gfp_mask.
393 int mls_from_string(char *str, struct context *context, gfp_t gfp_mask)
395 char *tmpstr, *freestr;
396 int rc;
398 if (!selinux_mls_enabled)
399 return -EINVAL;
401 /* we need freestr because mls_context_to_sid will change
402 the value of tmpstr */
403 tmpstr = freestr = kstrdup(str, gfp_mask);
404 if (!tmpstr) {
405 rc = -ENOMEM;
406 } else {
407 rc = mls_context_to_sid(':', &tmpstr, context,
408 NULL, SECSID_NULL);
409 kfree(freestr);
412 return rc;
416 * Copies the effective MLS range from `src' into `dst'.
418 static inline int mls_scopy_context(struct context *dst,
419 struct context *src)
421 int l, rc = 0;
423 /* Copy the MLS range from the source context */
424 for (l = 0; l < 2; l++) {
425 dst->range.level[l].sens = src->range.level[0].sens;
426 rc = ebitmap_cpy(&dst->range.level[l].cat,
427 &src->range.level[0].cat);
428 if (rc)
429 break;
432 return rc;
436 * Copies the MLS range `range' into `context'.
438 static inline int mls_range_set(struct context *context,
439 struct mls_range *range)
441 int l, rc = 0;
443 /* Copy the MLS range into the context */
444 for (l = 0; l < 2; l++) {
445 context->range.level[l].sens = range->level[l].sens;
446 rc = ebitmap_cpy(&context->range.level[l].cat,
447 &range->level[l].cat);
448 if (rc)
449 break;
452 return rc;
455 int mls_setup_user_range(struct context *fromcon, struct user_datum *user,
456 struct context *usercon)
458 if (selinux_mls_enabled) {
459 struct mls_level *fromcon_sen = &(fromcon->range.level[0]);
460 struct mls_level *fromcon_clr = &(fromcon->range.level[1]);
461 struct mls_level *user_low = &(user->range.level[0]);
462 struct mls_level *user_clr = &(user->range.level[1]);
463 struct mls_level *user_def = &(user->dfltlevel);
464 struct mls_level *usercon_sen = &(usercon->range.level[0]);
465 struct mls_level *usercon_clr = &(usercon->range.level[1]);
467 /* Honor the user's default level if we can */
468 if (mls_level_between(user_def, fromcon_sen, fromcon_clr)) {
469 *usercon_sen = *user_def;
470 } else if (mls_level_between(fromcon_sen, user_def, user_clr)) {
471 *usercon_sen = *fromcon_sen;
472 } else if (mls_level_between(fromcon_clr, user_low, user_def)) {
473 *usercon_sen = *user_low;
474 } else
475 return -EINVAL;
477 /* Lower the clearance of available contexts
478 if the clearance of "fromcon" is lower than
479 that of the user's default clearance (but
480 only if the "fromcon" clearance dominates
481 the user's computed sensitivity level) */
482 if (mls_level_dom(user_clr, fromcon_clr)) {
483 *usercon_clr = *fromcon_clr;
484 } else if (mls_level_dom(fromcon_clr, user_clr)) {
485 *usercon_clr = *user_clr;
486 } else
487 return -EINVAL;
490 return 0;
494 * Convert the MLS fields in the security context
495 * structure `c' from the values specified in the
496 * policy `oldp' to the values specified in the policy `newp'.
498 int mls_convert_context(struct policydb *oldp,
499 struct policydb *newp,
500 struct context *c)
502 struct level_datum *levdatum;
503 struct cat_datum *catdatum;
504 struct ebitmap bitmap;
505 struct ebitmap_node *node;
506 int l, i;
508 if (!selinux_mls_enabled)
509 return 0;
511 for (l = 0; l < 2; l++) {
512 levdatum = hashtab_search(newp->p_levels.table,
513 oldp->p_sens_val_to_name[c->range.level[l].sens - 1]);
515 if (!levdatum)
516 return -EINVAL;
517 c->range.level[l].sens = levdatum->level->sens;
519 ebitmap_init(&bitmap);
520 ebitmap_for_each_bit(&c->range.level[l].cat, node, i) {
521 if (ebitmap_node_get_bit(node, i)) {
522 int rc;
524 catdatum = hashtab_search(newp->p_cats.table,
525 oldp->p_cat_val_to_name[i]);
526 if (!catdatum)
527 return -EINVAL;
528 rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
529 if (rc)
530 return rc;
533 ebitmap_destroy(&c->range.level[l].cat);
534 c->range.level[l].cat = bitmap;
537 return 0;
540 int mls_compute_sid(struct context *scontext,
541 struct context *tcontext,
542 u16 tclass,
543 u32 specified,
544 struct context *newcontext)
546 if (!selinux_mls_enabled)
547 return 0;
549 switch (specified) {
550 case AVTAB_TRANSITION:
551 if (tclass == SECCLASS_PROCESS) {
552 struct range_trans *rangetr;
553 /* Look for a range transition rule. */
554 for (rangetr = policydb.range_tr; rangetr;
555 rangetr = rangetr->next) {
556 if (rangetr->dom == scontext->type &&
557 rangetr->type == tcontext->type) {
558 /* Set the range from the rule */
559 return mls_range_set(newcontext,
560 &rangetr->range);
564 /* Fallthrough */
565 case AVTAB_CHANGE:
566 if (tclass == SECCLASS_PROCESS)
567 /* Use the process MLS attributes. */
568 return mls_copy_context(newcontext, scontext);
569 else
570 /* Use the process effective MLS attributes. */
571 return mls_scopy_context(newcontext, scontext);
572 case AVTAB_MEMBER:
573 /* Only polyinstantiate the MLS attributes if
574 the type is being polyinstantiated */
575 if (newcontext->type != tcontext->type) {
576 /* Use the process effective MLS attributes. */
577 return mls_scopy_context(newcontext, scontext);
578 } else {
579 /* Use the related object MLS attributes. */
580 return mls_copy_context(newcontext, tcontext);
582 default:
583 return -EINVAL;
585 return -EINVAL;