📄 mls.c
字号:
/* * Implementation of the multi-level security (MLS) policy. * * Author : Stephen Smalley, <sds@epoch.ncsc.mil> *//* * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> * * Support for enhanced MLS infrastructure. * * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. *//* Ported to Xen 3.0, George Coker, <gscoker@alpha.ncsc.mil> */#include <xen/lib.h>#include <xen/xmalloc.h>#include <xen/string.h>#include <xen/errno.h>#include "sidtab.h"#include "mls.h"#include "policydb.h"#include "services.h"/* * Return the length in bytes for the MLS fields of the * security context string representation of `context'. */int mls_compute_context_len(struct context * context){ int i, l, len, range; struct ebitmap_node *node; if (!flask_mls_enabled) return 0; len = 1; /* for the beginning ":" */ for ( l = 0; l < 2; l++ ) { range = 0; len += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]); ebitmap_for_each_bit(&context->range.level[l].cat, node, i) { if ( ebitmap_node_get_bit(node, i) ) { if ( range ) { range++; continue; } len += strlen(policydb.p_cat_val_to_name[i]) + 1; range++; } else { if ( range > 1 ) len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1; range = 0; } } /* Handle case where last category is the end of range */ if ( range > 1 ) len += strlen(policydb.p_cat_val_to_name[i - 1]) + 1; if ( l == 0 ) { if ( mls_level_eq(&context->range.level[0], &context->range.level[1]) ) break; else len++; } } return len;}/* * Write the security context string representation of * the MLS fields of `context' into the string `*scontext'. * Update `*scontext' to point to the end of the MLS fields. */void mls_sid_to_context(struct context *context, char **scontext){ char *scontextp; int i, l, range, wrote_sep; struct ebitmap_node *node; if ( !flask_mls_enabled ) return; scontextp = *scontext; *scontextp = ':'; scontextp++; for ( l = 0; l < 2; l++ ) { range = 0; wrote_sep = 0; strlcpy(scontextp, policydb.p_sens_val_to_name[context->range.level[l].sens - 1], strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1])); scontextp += strlen(policydb.p_sens_val_to_name[context->range.level[l].sens - 1]); /* categories */ ebitmap_for_each_bit(&context->range.level[l].cat, node, i) { if ( ebitmap_node_get_bit(node, i) ) { if ( range ) { range++; continue; } if ( !wrote_sep ) { *scontextp++ = ':'; wrote_sep = 1; } else *scontextp++ = ','; strlcpy(scontextp, policydb.p_cat_val_to_name[i], strlen(policydb.p_cat_val_to_name[i])); scontextp += strlen(policydb.p_cat_val_to_name[i]); range++; } else { if ( range > 1 ) { if ( range > 2 ) *scontextp++ = '.'; else *scontextp++ = ','; strlcpy(scontextp, policydb.p_cat_val_to_name[i - 1], strlen(policydb.p_cat_val_to_name[i - 1])); scontextp += strlen(policydb.p_cat_val_to_name[i - 1]); } range = 0; } } /* Handle case where last category is the end of range */ if ( range > 1 ) { if ( range > 2 ) *scontextp++ = '.'; else *scontextp++ = ','; strlcpy(scontextp, policydb.p_cat_val_to_name[i - 1], strlen(policydb.p_cat_val_to_name[i - 1])); scontextp += strlen(policydb.p_cat_val_to_name[i - 1]); } if ( l == 0 ) { if ( mls_level_eq(&context->range.level[0], &context->range.level[1]) ) break; else { *scontextp = '-'; scontextp++; } } } *scontext = scontextp; return;}/* * Return 1 if the MLS fields in the security context * structure `c' are valid. Return 0 otherwise. */int mls_context_isvalid(struct policydb *p, struct context *c){ struct level_datum *levdatum; struct user_datum *usrdatum; struct ebitmap_node *node; int i, l; if ( !flask_mls_enabled ) return 1; /* * MLS range validity checks: high must dominate low, low level must * be valid (category set <-> sensitivity check), and high level must * be valid (category set <-> sensitivity check) */ if ( !mls_level_dom(&c->range.level[1], &c->range.level[0]) ) /* High does not dominate low. */ return 0; for ( l = 0; l < 2; l++ ) { if ( !c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim ) return 0; levdatum = hashtab_search(p->p_levels.table, p->p_sens_val_to_name[c->range.level[l].sens - 1]); if ( !levdatum ) return 0; ebitmap_for_each_bit(&c->range.level[l].cat, node, i) { if ( ebitmap_node_get_bit(node, i) ) { if ( i > p->p_cats.nprim ) return 0; if ( !ebitmap_get_bit(&levdatum->level->cat, i) ) /* * Category may not be associated with * sensitivity in low level. */ return 0; } } } if ( c->role == OBJECT_R_VAL ) return 1; /* * User must be authorized for the MLS range. */ if ( !c->user || c->user > p->p_users.nprim ) return 0; usrdatum = p->user_val_to_struct[c->user - 1]; if ( !mls_range_contains(usrdatum->range, c->range) ) return 0; /* user may not be associated with range */ return 1;}/* * Copies the MLS range from `src' into `dst'. */static inline int mls_copy_context(struct context *dst, struct context *src){ int l, rc = 0; /* Copy the MLS range from the source context */ for ( l = 0; l < 2; l++ ) { dst->range.level[l].sens = src->range.level[l].sens; rc = ebitmap_cpy(&dst->range.level[l].cat, &src->range.level[l].cat); if ( rc ) break; } return rc;}/* * Set the MLS fields in the security context structure * `context' based on the string representation in * the string `*scontext'. Update `*scontext' to * point to the end of the string representation of * the MLS fields. * * This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * * If a def_sid is provided and no MLS field is present, * copy the MLS field of the associated default context. * Used for upgraded to MLS systems where objects may lack * MLS fields. * * Policy read-lock must be held for sidtab lookup. * */int mls_context_to_sid(char oldc, char **scontext, struct context *context, struct sidtab *s, u32 def_sid){ char delim; char *scontextp, *p, *rngptr; struct level_datum *levdatum; struct cat_datum *catdatum, *rngdatum; int l, rc = -EINVAL; if ( !flask_mls_enabled ) return 0; /* * No MLS component to the security context, try and map to * default if provided. */ if ( !oldc ) { struct context *defcon; if ( def_sid == SECSID_NULL ) goto out; defcon = sidtab_search(s, def_sid); if ( !defcon ) goto out;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -