1 /*
2  * A security context is a set of security attributes
3  * associated with each subject and object controlled
4  * by the security policy.  Security contexts are
5  * externally represented as variable-length strings
6  * that can be interpreted by a user or application
7  * with an understanding of the security policy.
8  * Internally, the security server uses a simple
9  * structure.  This structure is private to the
10  * security server and can be changed without affecting
11  * clients of the security server.
12  *
13  * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
14  */
15 
16 /* Ported to Xen 3.0, George Coker, <gscoker@alpha.ncsc.mil> */
17 
18 #ifndef _SS_CONTEXT_H_
19 #define _SS_CONTEXT_H_
20 
21 #include "ebitmap.h"
22 #include "mls_types.h"
23 #include "security.h"
24 
25 /*
26  * A security context consists of an authenticated user
27  * identity, a role, a type and a MLS range.
28  */
29 struct context {
30     u32 user;
31     u32 role;
32     u32 type;
33     struct mls_range range;
34 };
35 
mls_context_init(struct context * c)36 static inline void mls_context_init(struct context *c)
37 {
38     memset(&c->range, 0, sizeof(c->range));
39 }
40 
mls_context_cpy(struct context * dst,struct context * src)41 static inline int mls_context_cpy(struct context *dst, struct context *src)
42 {
43     int rc;
44 
45     if ( !flask_mls_enabled )
46         return 0;
47 
48     dst->range.level[0].sens = src->range.level[0].sens;
49     rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
50     if ( rc )
51         goto out;
52 
53     dst->range.level[1].sens = src->range.level[1].sens;
54     rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
55     if ( rc )
56         ebitmap_destroy(&dst->range.level[0].cat);
57 out:
58     return rc;
59 }
60 
61 /*
62  * Sets both levels in the MLS range of 'dst' to the low level of 'src'.
63  */
mls_context_cpy_low(struct context * dst,struct context * src)64 static inline int mls_context_cpy_low(struct context *dst, struct context *src)
65 {
66     int rc;
67 
68     if ( !flask_mls_enabled )
69         return 0;
70 
71     dst->range.level[0].sens = src->range.level[0].sens;
72     rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
73     if ( rc )
74         goto out;
75 
76     dst->range.level[1].sens = src->range.level[0].sens;
77     rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat);
78     if ( rc )
79         ebitmap_destroy(&dst->range.level[0].cat);
80 out:
81     return rc;
82 }
83 
mls_context_cmp(struct context * c1,struct context * c2)84 static inline int mls_context_cmp(struct context *c1, struct context *c2)
85 {
86     if ( !flask_mls_enabled )
87         return 1;
88 
89     return ((c1->range.level[0].sens == c2->range.level[0].sens) &&
90         ebitmap_cmp(&c1->range.level[0].cat,&c2->range.level[0].cat) &&
91         (c1->range.level[1].sens == c2->range.level[1].sens) &&
92         ebitmap_cmp(&c1->range.level[1].cat,&c2->range.level[1].cat));
93 }
94 
mls_context_destroy(struct context * c)95 static inline void mls_context_destroy(struct context *c)
96 {
97     if ( !flask_mls_enabled )
98         return;
99 
100     ebitmap_destroy(&c->range.level[0].cat);
101     ebitmap_destroy(&c->range.level[1].cat);
102     mls_context_init(c);
103 }
104 
context_init(struct context * c)105 static inline void context_init(struct context *c)
106 {
107     memset(c, 0, sizeof(*c));
108 }
109 
context_cpy(struct context * dst,struct context * src)110 static inline int context_cpy(struct context *dst, struct context *src)
111 {
112     dst->user = src->user;
113     dst->role = src->role;
114     dst->type = src->type;
115     return mls_context_cpy(dst, src);
116 }
117 
context_destroy(struct context * c)118 static inline void context_destroy(struct context *c)
119 {
120     c->user = c->role = c->type = 0;
121     mls_context_destroy(c);
122 }
123 
context_cmp(struct context * c1,struct context * c2)124 static inline int context_cmp(struct context *c1, struct context *c2)
125 {
126     return ((c1->user == c2->user) &&
127         (c1->role == c2->role) &&
128         (c1->type == c2->type) &&
129         mls_context_cmp(c1, c2));
130 }
131 
132 #endif    /* _SS_CONTEXT_H_ */
133 
134